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

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