root/net/ipv4/packet.c

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

DEFINITIONS

This source file includes following definitions.
  1. min
  2. packet_rcv
  3. packet_sendmsg
  4. packet_close
  5. packet_attach
  6. packet_bind
  7. packet_unbind
  8. packet_init
  9. packet_recvmsg

   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  *              Dave Platt      :       Counter leaks caused by incorrect
  27  *                                      interrupt locking and some slightly
  28  *                                      dubious gcc output. Can you read
  29  *                                      compiler: it said _VOLATILE_
  30  *      Richard Kooijman        :       Timestamp fixes.
  31  *              Alan Cox        :       New buffers. Use sk->mac.raw.
  32  *              Alan Cox        :       sendmsg/recvmsg support.
  33  *
  34  *              This program is free software; you can redistribute it and/or
  35  *              modify it under the terms of the GNU General Public License
  36  *              as published by the Free Software Foundation; either version
  37  *              2 of the License, or (at your option) any later version.
  38  *
  39  */
  40  
  41 #include <linux/types.h>
  42 #include <linux/sched.h>
  43 #include <linux/mm.h>
  44 #include <linux/fcntl.h>
  45 #include <linux/socket.h>
  46 #include <linux/in.h>
  47 #include <linux/inet.h>
  48 #include <linux/netdevice.h>
  49 #include <net/ip.h>
  50 #include <net/protocol.h>
  51 #include <linux/skbuff.h>
  52 #include <net/sock.h>
  53 #include <linux/errno.h>
  54 #include <linux/timer.h>
  55 #include <asm/system.h>
  56 #include <asm/segment.h>
  57 
  58 /*
  59  *      We really ought to have a single public _inline_ min function!
  60  */
  61 
  62 static unsigned long min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
  63 {
  64         if (a < b) 
  65                 return(a);
  66         return(b);
  67 }
  68 
  69 
  70 /*
  71  *      This should be the easiest of all, all we do is copy it into a buffer. 
  72  */
  73  
  74 int packet_rcv(struct sk_buff *skb, struct device *dev,  struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         struct sock *sk;
  77         
  78         /*
  79          *      When we registered the protocol we saved the socket in the data
  80          *      field for just this event.
  81          */
  82 
  83         sk = (struct sock *) pt->data;  
  84         
  85         /*
  86          *      Yank back the headers [hope the device set this
  87          *      right or kerboom...]
  88          */
  89          
  90         skb_push(skb,skb->data-skb->mac.raw);
  91 
  92         /*
  93          *      The SOCK_PACKET socket receives _all_ frames.
  94          */
  95          
  96         skb->dev = dev;
  97 
  98         /*
  99          *      Charge the memory to the socket. This is done specifically
 100          *      to prevent sockets using all the memory up.
 101          */
 102          
 103         if(sock_queue_rcv_skb(sk,skb)<0)
 104         {
 105                 skb->sk = NULL;
 106                 kfree_skb(skb, FREE_READ);
 107                 return 0;
 108         }
 109         /*
 110          *      Processing complete.
 111          */
 112          
 113         return(0);
 114 }
 115 
 116 
 117 /*
 118  *      Output a raw packet to a device layer. This bypasses all the other
 119  *      protocol layers and you must therefore supply it with a complete frame
 120  */
 121  
 122 static int packet_sendmsg(struct sock *sk, struct msghdr *msg, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 123               int noblock, int flags)
 124 {
 125         struct sk_buff *skb;
 126         struct device *dev;
 127         struct sockaddr *saddr=(struct sockaddr *)msg->msg_name;
 128 
 129         /*
 130          *      Check the flags. 
 131          */
 132 
 133         if (flags) 
 134                 return(-EINVAL);
 135 
 136         /*
 137          *      Get and verify the address. 
 138          */
 139          
 140         if (saddr) 
 141         {
 142                 if (msg->msg_namelen < sizeof(*saddr)) 
 143                         return(-EINVAL);
 144         } 
 145         else
 146                 return(-ENOTCONN);      /* SOCK_PACKET must be sent giving an address */
 147         
 148         /*
 149          *      Find the device first to size check it 
 150          */
 151 
 152         saddr->sa_data[13] = 0;
 153         dev = dev_get(saddr->sa_data);
 154         if (dev == NULL) 
 155         {
 156                 return(-ENODEV);
 157         }
 158         
 159         /*
 160          *      You may not queue a frame bigger than the mtu. This is the lowest level
 161          *      raw protocol and you must do your own fragmentation at this level.
 162          */
 163          
 164         if(len>dev->mtu+dev->hard_header_len)
 165                 return -EMSGSIZE;
 166 
 167         skb = sock_wmalloc(sk, len, 0, GFP_KERNEL);
 168 
 169         /*
 170          *      If the write buffer is full, then tough. At this level the user gets to
 171          *      deal with the problem - do your own algorithmic backoffs. Thats far
 172          *      more flexible.
 173          */
 174          
 175         if (skb == NULL) 
 176         {
 177                 return(-ENOBUFS);
 178         }
 179         
 180         /*
 181          *      Fill it in 
 182          */
 183          
 184         skb->sk = sk;
 185         skb->free = 1;
 186         memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
 187         skb->arp = 1;           /* No ARP needs doing on this (complete) frame */
 188 
 189         /*
 190          *      Now send it
 191          */
 192 
 193         if (dev->flags & IFF_UP) 
 194                 dev_queue_xmit(skb, dev, sk->priority);
 195         else
 196                 kfree_skb(skb, FREE_WRITE);
 197         return(len);
 198 }
 199 
 200 /*
 201  *      Close a SOCK_PACKET socket. This is fairly simple. We immediately go
 202  *      to 'closed' state and remove our protocol entry in the device list.
 203  *      The release_sock() will destroy the socket if a user has closed the
 204  *      file side of the object.
 205  */
 206 
 207 static void packet_close(struct sock *sk, unsigned long timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 208 {
 209         /*
 210          *      Stop more data and kill the socket off.
 211          */
 212 
 213         lock_sock(sk);
 214         sk->state = TCP_CLOSE;
 215 
 216         /*
 217          *      Unhook the notifier
 218          */
 219 
 220         unregister_netdevice_notifier(&sk->protinfo.af_packet.notifier);
 221 
 222         if(sk->protinfo.af_packet.prot_hook)
 223         {
 224                 /*
 225                  *      Remove the protocol hook
 226                  */
 227                  
 228                 dev_remove_pack((struct packet_type *)sk->protinfo.af_packet.prot_hook);
 229 
 230                 /*
 231                  *      Dispose of litter carefully.
 232                  */
 233          
 234                 kfree_s((void *)sk->protinfo.af_packet.prot_hook, sizeof(struct packet_type));
 235                 sk->protinfo.af_packet.prot_hook = NULL;
 236         }
 237         
 238         release_sock(sk);
 239         destroy_sock(sk);
 240 }
 241 
 242 /*
 243  *      Attach a packer hook to a device.
 244  */
 245 
 246 int packet_attach(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 247 {
 248         struct packet_type *p = (struct packet_type *) kmalloc(sizeof(*p), GFP_KERNEL);
 249         if (p == NULL) 
 250                 return(-ENOMEM);
 251 
 252         p->func = packet_rcv;
 253         p->type = sk->num;
 254         p->data = (void *)sk;
 255         p->dev = NULL;
 256         dev_add_pack(p);
 257    
 258         /*
 259          *      We need to remember this somewhere. 
 260          */
 261    
 262         sk->protinfo.af_packet.prot_hook = p;
 263         return 0;       
 264 }
 265  
 266 /*
 267  *      Bind a packet socket to a device
 268  */
 269 
 270 static int packet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
     /* [previous][next][first][last][top][bottom][index][help] */
 271 {
 272         char dev[15];
 273         
 274         /*
 275          *      Check legality
 276          */
 277          
 278         if(addr_len!=sizeof(struct sockaddr))
 279                 return -EINVAL;
 280         strncpy(dev,uaddr->sa_data,14);
 281         dev[14]=0;
 282         
 283         /*
 284          *      Lock the device chain while we sanity check
 285          *      the bind request.
 286          */
 287          
 288         dev_lock_list();
 289         if((sk->protinfo.af_packet.bound_dev=dev_get(dev))==NULL)
 290         {
 291                 dev_unlock_list();
 292                 return -ENODEV;
 293         }
 294         if(!(sk->protinfo.af_packet.bound_dev->flags&IFF_UP))
 295         {
 296                 dev_unlock_list();
 297                 return -ENETDOWN;
 298         }
 299         
 300         /*
 301          *      Perform the request.
 302          */
 303          
 304         memcpy(sk->protinfo.af_packet.device_name,dev,15);
 305         if(sk->protinfo.af_packet.prot_hook)
 306                 dev_remove_pack(sk->protinfo.af_packet.prot_hook);
 307         else
 308         {
 309                 int err=packet_attach(sk);
 310                 if(err)
 311                 {
 312                         dev_unlock_list();
 313                         return err;
 314                 }
 315         }
 316         sk->protinfo.af_packet.prot_hook->dev=sk->protinfo.af_packet.bound_dev;
 317         dev_add_pack(sk->protinfo.af_packet.prot_hook);
 318         /*
 319          *      Now the notifier is set up right this lot is safe.
 320          */
 321         dev_unlock_list();
 322         return 0;
 323 }
 324 
 325 /*
 326  *      This hook is called when a device goes up or down so that
 327  *      SOCK_PACKET sockets can come unbound properly.
 328  */
 329 
 330 static int packet_unbind(struct notifier_block *this, unsigned long msg, void *data)
     /* [previous][next][first][last][top][bottom][index][help] */
 331 {
 332         struct inet_packet_opt *ipo=(struct inet_packet_opt *)this;
 333         if(msg==NETDEV_DOWN && data==ipo->bound_dev)
 334         {
 335                 /*
 336                  *      Our device has gone down.
 337                  */
 338                 ipo->bound_dev=NULL;
 339                 dev_remove_pack(ipo->prot_hook);
 340                 kfree(ipo->prot_hook);
 341                 ipo->prot_hook=NULL;
 342         }
 343         return NOTIFY_DONE;
 344 }
 345 
 346 
 347 /*
 348  *      Create a packet of type SOCK_PACKET. 
 349  */
 350 
 351 static int packet_init(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 352 {
 353         /*
 354          *      Attach a protocol block
 355          */
 356          
 357         int err=packet_attach(sk);
 358         if(err)
 359                 return err;
 360                 
 361         /*
 362          *      Set up the per socket notifier.
 363          */
 364          
 365         sk->protinfo.af_packet.notifier.notifier_call=packet_unbind;
 366         sk->protinfo.af_packet.notifier.priority=0;
 367 
 368         register_netdevice_notifier(&sk->protinfo.af_packet.notifier);
 369 
 370         return(0);
 371 }
 372 
 373 
 374 /*
 375  *      Pull a packet from our receive queue and hand it to the user.
 376  *      If necessary we block.
 377  */
 378  
 379 int packet_recvmsg(struct sock *sk, struct msghdr *msg, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 380                 int noblock, int flags,int *addr_len)
 381 {
 382         int copied=0;
 383         struct sk_buff *skb;
 384         struct sockaddr *saddr=(struct sockaddr *)msg->msg_name;
 385         int err;
 386 
 387         if (sk->shutdown & RCV_SHUTDOWN) 
 388                 return(0);
 389                 
 390         /*
 391          *      If there is no protocol hook then the device is down.
 392          */
 393          
 394         if(sk->protinfo.af_packet.prot_hook==NULL)
 395                 return -ENETDOWN;
 396                 
 397         /*
 398          *      If the address length field is there to be filled in, we fill
 399          *      it in now.
 400          */
 401 
 402         if (addr_len) 
 403                 *addr_len=sizeof(*saddr);
 404         
 405         /*
 406          *      Call the generic datagram receiver. This handles all sorts
 407          *      of horrible races and re-entrancy so we can forget about it
 408          *      in the protocol layers.
 409          */
 410          
 411         skb=skb_recv_datagram(sk,flags,noblock,&err);
 412         
 413         /*
 414          *      An error occurred so return it. Because skb_recv_datagram() 
 415          *      handles the blocking we don't see and worry about blocking
 416          *      retries.
 417          */
 418          
 419         if(skb==NULL)
 420                 return err;
 421                 
 422         /*
 423          *      You lose any data beyond the buffer you gave. If it worries a
 424          *      user program they can ask the device for its MTU anyway.
 425          */
 426          
 427         copied = min(len, skb->len);
 428 
 429         memcpy_toiovec(msg->msg_iov, skb->data, copied);        /* We can't use skb_copy_datagram here */
 430         sk->stamp=skb->stamp;
 431 
 432         /*
 433          *      Copy the address. 
 434          */
 435          
 436         if (saddr) 
 437         {
 438                 saddr->sa_family = skb->dev->type;
 439                 strncpy(saddr->sa_data,skb->dev->name, 15);
 440         }
 441         
 442         /*
 443          *      Free or return the buffer as appropriate. Again this hides all the
 444          *      races and re-entrancy issues from us.
 445          */
 446 
 447         skb_free_datagram(sk, skb);
 448 
 449         return(copied);
 450 }
 451 
 452 /*
 453  *      This structure declares to the lower layer socket subsystem currently
 454  *      incorrectly embedded in the IP code how to behave. This interface needs
 455  *      a lot of work and will change.
 456  */
 457  
 458 struct proto packet_prot = 
 459 {
 460         packet_close,
 461         ip_build_header,        /* Not actually used */
 462         NULL,
 463         NULL,
 464         ip_queue_xmit,          /* These two are not actually used */
 465         NULL,
 466         NULL,
 467         NULL,
 468         NULL, 
 469         datagram_select,
 470         NULL,                   /* No ioctl */
 471         packet_init,
 472         NULL,
 473         NULL,                   /* No set/get socket options */
 474         NULL,
 475         packet_sendmsg,         /* Sendmsg */
 476         packet_recvmsg,         /* Recvmsg */
 477         packet_bind,            /* Bind */
 478         128,
 479         0,
 480         "PACKET",
 481         0, 0
 482 };
 483 
 484         

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