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

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