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

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