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

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