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                 return(-ENOBUFS);
 197         }
 198         
 199         /*
 200          *      Fill it in 
 201          */
 202          
 203         skb->sk = sk;
 204         skb->free = 1;
 205         memcpy_fromfs(skb->data, from, len);
 206         skb->len = len;
 207         skb->arp = 1;           /* No ARP needs doing on this (complete) frame */
 208 
 209         /*
 210          *      Now send it
 211          */
 212 
 213         if (dev->flags & IFF_UP) 
 214                 dev_queue_xmit(skb, dev, sk->priority);
 215         else
 216                 kfree_skb(skb, FREE_WRITE);
 217         return(len);
 218 }
 219 
 220 /*
 221  *      A write to a SOCK_PACKET can't actually do anything useful and will
 222  *      always fail but we include it for completeness and future expansion.
 223  */
 224 
 225 static int packet_write(struct sock *sk, unsigned char *buff, 
     /* [previous][next][first][last][top][bottom][index][help] */
 226              int len, int noblock,  unsigned flags)
 227 {
 228         return(packet_sendto(sk, buff, len, noblock, flags, NULL, 0));
 229 }
 230 
 231 /*
 232  *      Close a SOCK_PACKET socket. This is fairly simple. We immediately go
 233  *      to 'closed' state and remove our protocol entry in the device list.
 234  *      The release_sock() will destroy the socket if a user has closed the
 235  *      file side of the object.
 236  */
 237 
 238 static void packet_close(struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240         sk->inuse = 1;
 241         sk->state = TCP_CLOSE;
 242         dev_remove_pack((struct packet_type *)sk->pair);
 243         kfree_s((void *)sk->pair, sizeof(struct packet_type));
 244         sk->pair = NULL;
 245         release_sock(sk);
 246 }
 247 
 248 /*
 249  *      Create a packet of type SOCK_PACKET. We do one slightly irregular
 250  *      thing here that wants tidying up. We borrow the 'pair' pointer in
 251  *      the socket object so we can find the packet_type entry in the
 252  *      device list. The reverse is easy as we use the data field of the
 253  *      packet type to point to our socket.
 254  */
 255 
 256 static int packet_init(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258         struct packet_type *p;
 259 
 260         p = (struct packet_type *) kmalloc(sizeof(*p), GFP_KERNEL);
 261         if (p == NULL) 
 262                 return(-ENOMEM);
 263 
 264         p->func = packet_rcv;
 265         p->type = sk->num;
 266         p->data = (void *)sk;
 267         dev_add_pack(p);
 268    
 269         /*
 270          *      We need to remember this somewhere. 
 271          */
 272    
 273         sk->pair = (struct sock *)p;
 274 
 275         return(0);
 276 }
 277 
 278 
 279 /*
 280  *      Pull a packet from our receive queue and hand it to the user.
 281  *      If neccessary we block.
 282  */
 283  
 284 int packet_recvfrom(struct sock *sk, unsigned char *to, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 285                 int noblock, unsigned flags, struct sockaddr_in *sin,
 286                 int *addr_len)
 287 {
 288         int copied=0;
 289         struct sk_buff *skb;
 290         struct sockaddr *saddr;
 291         int err;
 292 
 293         saddr = (struct sockaddr *)sin;
 294         if (len == 0) 
 295                 return(0);
 296         if (len < 0)
 297                 return(-EINVAL);
 298 
 299         if (sk->shutdown & RCV_SHUTDOWN) 
 300                 return(0);
 301                 
 302         /*
 303          *      If the address length field is there to be filled in, we fill
 304          *      it in now.
 305          */
 306 
 307         if (addr_len) 
 308         {
 309                 err=verify_area(VERIFY_WRITE, addr_len, sizeof(*addr_len));
 310                 if(err)
 311                         return err;
 312                 put_fs_long(sizeof(*saddr), addr_len);
 313         }
 314         
 315         if(saddr)
 316         {
 317                 err=verify_area(VERIFY_WRITE, saddr, sizeof(*saddr));           
 318                 if(err)
 319                         return err;
 320         }
 321         
 322         /*
 323          *      Check the user given area can be written to.
 324          */
 325          
 326         err=verify_area(VERIFY_WRITE,to,len);
 327         if(err)
 328                 return err;
 329                 
 330         /*
 331          *      Call the generic datagram receiver. This handles all sorts
 332          *      of horrible races and re-entrancy so we can forget about it
 333          *      in the protocol layers.
 334          */
 335          
 336         skb=skb_recv_datagram(sk,flags,noblock,&err);
 337         
 338         /*
 339          *      An error occured so return it. Because skb_recv_datagram() 
 340          *      handles the blocking we don't see and worry about blocking
 341          *      retries.
 342          */
 343          
 344         if(skb==NULL)
 345                 return err;
 346                 
 347         /*
 348          *      You lose any data beyond the buffer you gave. If it worries a
 349          *      user program they can ask the device for its MTU anyway.
 350          */
 351          
 352         copied = min(len, skb->len);
 353 
 354         memcpy_tofs(to, skb->data, copied);     /* We can't use skb_copy_datagram here */
 355 
 356         /*
 357          *      Copy the address. 
 358          */
 359          
 360         if (saddr) 
 361         {
 362                 struct sockaddr addr;
 363 
 364                 addr.sa_family = skb->dev->type;
 365                 memcpy(addr.sa_data,skb->dev->name, 14);
 366                 memcpy_tofs(saddr, &addr, sizeof(*saddr));
 367         }
 368         
 369         /*
 370          *      Free or return the buffer as appropriate. Again this hides all the
 371          *      races and re-entrancy issues from us.
 372          */
 373 
 374         skb_free_datagram(skb);
 375 
 376         /*
 377          *      We are done.
 378          */
 379          
 380         release_sock(sk);
 381         return(copied);
 382 }
 383 
 384 
 385 /*
 386  *      A packet read can succeed and is just the same as a recvfrom but without the
 387  *      addresses being recorded.
 388  */
 389 
 390 int packet_read(struct sock *sk, unsigned char *buff,
     /* [previous][next][first][last][top][bottom][index][help] */
 391             int len, int noblock, unsigned flags)
 392 {
 393         return(packet_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
 394 }
 395 
 396 
 397 /*
 398  *      This structure declares to the lower layer socket subsystem currently
 399  *      incorrectly embedded in the IP code how to behave. This interface needs
 400  *      a lot of work and will change.
 401  */
 402  
 403 struct proto packet_prot = 
 404 {
 405         sock_wmalloc,
 406         sock_rmalloc,
 407         sock_wfree,
 408         sock_rfree,
 409         sock_rspace,
 410         sock_wspace,
 411         packet_close,
 412         packet_read,
 413         packet_write,
 414         packet_sendto,
 415         packet_recvfrom,
 416         ip_build_header,        /* Not actually used */
 417         NULL,
 418         NULL,
 419         ip_queue_xmit,          /* These two are not actually used */
 420         ip_retransmit,
 421         NULL,
 422         NULL,
 423         NULL, 
 424         datagram_select,
 425         NULL,
 426         packet_init,
 427         NULL,
 428         NULL,                   /* No set/get socket options */
 429         NULL,
 430         128,
 431         0,
 432         {NULL,},
 433         "PACKET"
 434 };

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