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                 release_sock(sk);               
 108                 return 0;
 109         }
 110         /*
 111          *      Processing complete.
 112          */
 113          
 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_sendmsg(struct sock *sk, struct msghdr *msg, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 124               int noblock, int flags)
 125 {
 126         struct sk_buff *skb;
 127         struct device *dev;
 128         struct sockaddr *saddr=(struct sockaddr *)msg->msg_name;
 129 
 130         /*
 131          *      Check the flags. 
 132          */
 133 
 134         if (flags) 
 135                 return(-EINVAL);
 136 
 137         /*
 138          *      Get and verify the address. 
 139          */
 140          
 141         if (saddr) 
 142         {
 143                 if (msg->msg_namelen < sizeof(*saddr)) 
 144                         return(-EINVAL);
 145         } 
 146         else
 147                 return(-ENOTCONN);      /* SOCK_PACKET must be sent giving an address */
 148         
 149         /*
 150          *      Find the device first to size check it 
 151          */
 152 
 153         saddr->sa_data[13] = 0;
 154         dev = dev_get(saddr->sa_data);
 155         if (dev == NULL) 
 156         {
 157                 return(-ENODEV);
 158         }
 159         
 160         /*
 161          *      You may not queue a frame bigger than the mtu. This is the lowest level
 162          *      raw protocol and you must do your own fragmentation at this level.
 163          */
 164          
 165         if(len>dev->mtu+dev->hard_header_len)
 166                 return -EMSGSIZE;
 167 
 168         skb = sock_wmalloc(sk, len, 0, GFP_KERNEL);
 169 
 170         /*
 171          *      If the write buffer is full, then tough. At this level the user gets to
 172          *      deal with the problem - do your own algorithmic backoffs. Thats far
 173          *      more flexible.
 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_fromiovec(skb_put(skb,len), msg->msg_iov, len);
 188         skb->arp = 1;           /* No ARP needs doing on this (complete) frame */
 189 
 190         /*
 191          *      Now send it
 192          */
 193 
 194         if (dev->flags & IFF_UP) 
 195                 dev_queue_xmit(skb, dev, sk->priority);
 196         else
 197                 kfree_skb(skb, FREE_WRITE);
 198         return(len);
 199 }
 200 
 201 /*
 202  *      Close a SOCK_PACKET socket. This is fairly simple. We immediately go
 203  *      to 'closed' state and remove our protocol entry in the device list.
 204  *      The release_sock() will destroy the socket if a user has closed the
 205  *      file side of the object.
 206  */
 207 
 208 static void packet_close(struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 209 {
 210         /*
 211          *      Stop more data and kill the socket off.
 212          */
 213 
 214         sk->inuse = 1;
 215         sk->state = TCP_CLOSE;
 216 
 217         /*
 218          *      Unhook the notifier
 219          */
 220 
 221         unregister_netdevice_notifier(&sk->protinfo.af_packet.notifier);
 222 
 223         if(sk->protinfo.af_packet.prot_hook)
 224         {
 225                 /*
 226                  *      Remove the protocol hook
 227                  */
 228                  
 229                 dev_remove_pack((struct packet_type *)sk->protinfo.af_packet.prot_hook);
 230 
 231                 /*
 232                  *      Dispose of litter carefully.
 233                  */
 234          
 235                 kfree_s((void *)sk->protinfo.af_packet.prot_hook, sizeof(struct packet_type));
 236                 sk->protinfo.af_packet.prot_hook = NULL;
 237         }
 238         
 239         release_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                 memcpy(saddr->sa_data,skb->dev->name, 14);
 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(skb);
 448 
 449         /*
 450          *      We are done.
 451          */
 452          
 453         release_sock(sk);
 454         return(copied);
 455 }
 456 
 457 /*
 458  *      This structure declares to the lower layer socket subsystem currently
 459  *      incorrectly embedded in the IP code how to behave. This interface needs
 460  *      a lot of work and will change.
 461  */
 462  
 463 struct proto packet_prot = 
 464 {
 465         packet_close,
 466         ip_build_header,        /* Not actually used */
 467         NULL,
 468         NULL,
 469         ip_queue_xmit,          /* These two are not actually used */
 470         NULL,
 471         NULL,
 472         NULL,
 473         NULL, 
 474         datagram_select,
 475         NULL,                   /* No ioctl */
 476         packet_init,
 477         NULL,
 478         NULL,                   /* No set/get socket options */
 479         NULL,
 480         packet_sendmsg,         /* Sendmsg */
 481         packet_recvmsg,         /* Recvmsg */
 482         packet_bind,            /* Bind */
 483         128,
 484         0,
 485         "PACKET",
 486         0, 0
 487 };
 488 
 489         

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