root/net/inet/packet.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. min
  2. packet_rcv
  3. packet_sendto
  4. packet_write
  5. packet_close
  6. packet_init
  7. packet_recvfrom
  8. packet_read

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              PACKET - implements raw packet sockets.
   7  *
   8  * Version:     @(#)packet.c    1.0.6   05/25/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
  13  *
  14  * Fixes:       
  15  *              Alan Cox        :       verify_area() now used correctly
  16  *              Alan Cox        :       new skbuff lists, look ma no backlogs!
  17  *              Alan Cox        :       tidied skbuff lists.
  18  *              Alan Cox        :       Now uses generic datagram routines I
  19  *                                      added. Also fixed the peek/read crash
  20  *                                      from all old Linux datagram code.
  21  *              Alan Cox        :       Uses the improved datagram code.
  22  *              Alan Cox        :       Added NULL's for socket options.
  23  *              Alan Cox        :       Re-commented the code.
  24  *              Alan Cox        :       Use new kernel side addressing
  25  *              Rob Janssen     :       Correct MTU usage.
  26  *
  27  *
  28  *              This program is free software; you can redistribute it and/or
  29  *              modify it under the terms of the GNU General Public License
  30  *              as published by the Free Software Foundation; either version
  31  *              2 of the License, or (at your option) any later version.
  32  *
  33  */
  34  
  35 #include <linux/types.h>
  36 #include <linux/sched.h>
  37 #include <linux/fcntl.h>
  38 #include <linux/socket.h>
  39 #include <linux/in.h>
  40 #include <linux/inet.h>
  41 #include <linux/netdevice.h>
  42 #include "ip.h"
  43 #include "protocol.h"
  44 #include <linux/skbuff.h>
  45 #include "sock.h"
  46 #include <linux/errno.h>
  47 #include <linux/timer.h>
  48 #include <asm/system.h>
  49 #include <asm/segment.h>
  50 
  51 /*
  52  *      We really ought to have a single public _inline_ min function!
  53  */
  54 
  55 static unsigned long min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57         if (a < b) 
  58                 return(a);
  59         return(b);
  60 }
  61 
  62 
  63 /*
  64  *      This should be the easiest of all, all we do is copy it into a buffer. 
  65  */
  66  
  67 int packet_rcv(struct sk_buff *skb, struct device *dev,  struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69         struct sock *sk;
  70         
  71         /*
  72          *      When we registered the protocol we saved the socket in the data
  73          *      field for just this event.
  74          */
  75 
  76         sk = (struct sock *) pt->data;  
  77 
  78         /*
  79          *      The SOCK_PACKET socket receives _all_ frames, and as such 
  80          *      therefore needs to put the header back onto the buffer.
  81          *      (it was removed by inet_bh()).
  82          */
  83          
  84         skb->dev = dev;
  85         skb->len += dev->hard_header_len;
  86 
  87         skb->sk = sk;
  88 
  89         /*
  90          *      Charge the memory to the socket. This is done specifically
  91          *      to prevent sockets using all the memory up.
  92          */
  93          
  94         if (sk->rmem_alloc + skb->mem_len >= sk->rcvbuf) 
  95         {
  96                 skb->sk = NULL;
  97                 kfree_skb(skb, FREE_READ);
  98                 return(0);
  99         }
 100         sk->rmem_alloc += skb->mem_len; 
 101 
 102         /*
 103          *      Queue the packet up, and wake anyone waiting for it.
 104          */
 105 
 106         skb_queue_tail(&sk->receive_queue,skb);
 107         wake_up_interruptible(sk->sleep);
 108 
 109         /*
 110          *      Processing complete.
 111          */
 112          
 113         release_sock(sk);       /* This is now effectively surplus in this layer */
 114         return(0);
 115 }
 116 
 117 
 118 /*
 119  *      Output a raw packet to a device layer. This bypasses all the other
 120  *      protocol layers and you must therefore supply it with a complete frame
 121  */
 122  
 123 static int packet_sendto(struct sock *sk, unsigned char *from, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 124               int noblock, unsigned flags, struct sockaddr_in *usin,
 125               int addr_len)
 126 {
 127         struct sk_buff *skb;
 128         struct device *dev;
 129         struct sockaddr *saddr=(struct sockaddr *)usin;
 130 
 131         /*
 132          *      Check the flags. 
 133          */
 134 
 135         if (flags) 
 136                 return(-EINVAL);
 137 
 138         /*
 139          *      Get and verify the address. 
 140          */
 141          
 142         if (usin) 
 143         {
 144                 if (addr_len < sizeof(*saddr)) 
 145                         return(-EINVAL);
 146         } 
 147         else
 148                 return(-EINVAL);        /* SOCK_PACKET must be sent giving an address */
 149         
 150         /*
 151          *      Find the device first to size check it 
 152          */
 153 
 154         saddr->sa_data[13] = 0;
 155         dev = dev_get(saddr->sa_data);
 156         if (dev == NULL) 
 157         {
 158                 return(-ENXIO);
 159         }
 160         
 161         /*
 162          *      You may not queue a frame bigger than the mtu. This is the lowest level
 163          *      raw protocol and you must do your own fragmentation at this level.
 164          */
 165          
 166         if(len>dev->mtu+dev->hard_header_len)
 167                 return -EMSGSIZE;
 168 
 169         skb = sk->prot->wmalloc(sk, len, 0, GFP_KERNEL);
 170 
 171         /*
 172          *      If the write buffer is full, then tough. At this level the user gets to
 173          *      deal with the problem - do your own algorithmic backoffs.
 174          */
 175          
 176         if (skb == NULL) 
 177         {
 178                 return(-ENOBUFS);
 179         }
 180         
 181         /*
 182          *      Fill it in 
 183          */
 184          
 185         skb->sk = sk;
 186         skb->free = 1;
 187         memcpy_fromfs(skb->data, from, len);
 188         skb->len = len;
 189         skb->arp = 1;           /* No ARP needs doing on this (complete) frame */
 190 
 191         /*
 192          *      Now send it
 193          */
 194 
 195         if (dev->flags & IFF_UP) 
 196                 dev_queue_xmit(skb, dev, sk->priority);
 197         else
 198                 kfree_skb(skb, FREE_WRITE);
 199         return(len);
 200 }
 201 
 202 /*
 203  *      A write to a SOCK_PACKET can't actually do anything useful and will
 204  *      always fail but we include it for completeness and future expansion.
 205  */
 206 
 207 static int packet_write(struct sock *sk, unsigned char *buff, 
     /* [previous][next][first][last][top][bottom][index][help] */
 208              int len, int noblock,  unsigned flags)
 209 {
 210         return(packet_sendto(sk, buff, len, noblock, flags, NULL, 0));
 211 }
 212 
 213 /*
 214  *      Close a SOCK_PACKET socket. This is fairly simple. We immediately go
 215  *      to 'closed' state and remove our protocol entry in the device list.
 216  *      The release_sock() will destroy the socket if a user has closed the
 217  *      file side of the object.
 218  */
 219 
 220 static void packet_close(struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 221 {
 222         sk->inuse = 1;
 223         sk->state = TCP_CLOSE;
 224         dev_remove_pack((struct packet_type *)sk->pair);
 225         kfree_s((void *)sk->pair, sizeof(struct packet_type));
 226         sk->pair = NULL;
 227         release_sock(sk);
 228 }
 229 
 230 /*
 231  *      Create a packet of type SOCK_PACKET. We do one slightly irregular
 232  *      thing here that wants tidying up. We borrow the 'pair' pointer in
 233  *      the socket object so we can find the packet_type entry in the
 234  *      device list. The reverse is easy as we use the data field of the
 235  *      packet type to point to our socket.
 236  */
 237 
 238 static int packet_init(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240         struct packet_type *p;
 241 
 242         p = (struct packet_type *) kmalloc(sizeof(*p), GFP_KERNEL);
 243         if (p == NULL) 
 244                 return(-ENOMEM);
 245 
 246         p->func = packet_rcv;
 247         p->type = sk->num;
 248         p->data = (void *)sk;
 249         dev_add_pack(p);
 250    
 251         /*
 252          *      We need to remember this somewhere. 
 253          */
 254    
 255         sk->pair = (struct sock *)p;
 256 
 257         return(0);
 258 }
 259 
 260 
 261 /*
 262  *      Pull a packet from our receive queue and hand it to the user.
 263  *      If necessary we block.
 264  */
 265  
 266 int packet_recvfrom(struct sock *sk, unsigned char *to, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 267                 int noblock, unsigned flags, struct sockaddr_in *sin,
 268                 int *addr_len)
 269 {
 270         int copied=0;
 271         struct sk_buff *skb;
 272         struct sockaddr *saddr;
 273         int err;
 274         int truesize;
 275 
 276         saddr = (struct sockaddr *)sin;
 277 
 278         if (sk->shutdown & RCV_SHUTDOWN) 
 279                 return(0);
 280                 
 281         /*
 282          *      If the address length field is there to be filled in, we fill
 283          *      it in now.
 284          */
 285 
 286         if (addr_len) 
 287                 *addr_len=sizeof(*saddr);
 288         
 289         /*
 290          *      Call the generic datagram receiver. This handles all sorts
 291          *      of horrible races and re-entrancy so we can forget about it
 292          *      in the protocol layers.
 293          */
 294          
 295         skb=skb_recv_datagram(sk,flags,noblock,&err);
 296         
 297         /*
 298          *      An error occurred so return it. Because skb_recv_datagram() 
 299          *      handles the blocking we don't see and worry about blocking
 300          *      retries.
 301          */
 302          
 303         if(skb==NULL)
 304                 return err;
 305                 
 306         /*
 307          *      You lose any data beyond the buffer you gave. If it worries a
 308          *      user program they can ask the device for its MTU anyway.
 309          */
 310          
 311         truesize = skb->len;
 312         copied = min(len, truesize);
 313 
 314         memcpy_tofs(to, skb->data, copied);     /* We can't use skb_copy_datagram here */
 315 
 316         /*
 317          *      Copy the address. 
 318          */
 319          
 320         if (saddr) 
 321         {
 322                 saddr->sa_family = skb->dev->type;
 323                 memcpy(saddr->sa_data,skb->dev->name, 14);
 324         }
 325         
 326         /*
 327          *      Free or return the buffer as appropriate. Again this hides all the
 328          *      races and re-entrancy issues from us.
 329          */
 330 
 331         skb_free_datagram(skb);
 332 
 333         /*
 334          *      We are done.
 335          */
 336          
 337         release_sock(sk);
 338         return(truesize);
 339 }
 340 
 341 
 342 /*
 343  *      A packet read can succeed and is just the same as a recvfrom but without the
 344  *      addresses being recorded.
 345  */
 346 
 347 int packet_read(struct sock *sk, unsigned char *buff,
     /* [previous][next][first][last][top][bottom][index][help] */
 348             int len, int noblock, unsigned flags)
 349 {
 350         return(packet_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
 351 }
 352 
 353 
 354 /*
 355  *      This structure declares to the lower layer socket subsystem currently
 356  *      incorrectly embedded in the IP code how to behave. This interface needs
 357  *      a lot of work and will change.
 358  */
 359  
 360 struct proto packet_prot = 
 361 {
 362         sock_wmalloc,
 363         sock_rmalloc,
 364         sock_wfree,
 365         sock_rfree,
 366         sock_rspace,
 367         sock_wspace,
 368         packet_close,
 369         packet_read,
 370         packet_write,
 371         packet_sendto,
 372         packet_recvfrom,
 373         ip_build_header,        /* Not actually used */
 374         NULL,
 375         NULL,
 376         ip_queue_xmit,          /* These two are not actually used */
 377         NULL,
 378         NULL,
 379         NULL,
 380         NULL, 
 381         datagram_select,
 382         NULL,
 383         packet_init,
 384         NULL,
 385         NULL,                   /* No set/get socket options */
 386         NULL,
 387         128,
 388         0,
 389         {NULL,},
 390         "PACKET"
 391 };

/* [previous][next][first][last][top][bottom][index][help] */