root/net/tcp/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 /* packet.c - implements raw packet sockets. */
   2 /*
   3     Copyright (C) 1992  Ross Biro
   4 
   5     This program is free software; you can redistribute it and/or modify
   6     it under the terms of the GNU General Public License as published by
   7     the Free Software Foundation; either version 2, or (at your option)
   8     any later version.
   9 
  10     This program is distributed in the hope that it will be useful,
  11     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13     GNU General Public License for more details.
  14 
  15     You should have received a copy of the GNU General Public License
  16     along with this program; if not, write to the Free Software
  17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
  18 
  19     The Author may be reached as bir7@leland.stanford.edu or
  20     C/O Department of Mathematics; Stanford University; Stanford, CA 94305
  21 */
  22 /* $Id: packet.c,v 0.8.4.7 1993/01/26 22:04:00 bir7 Exp $ */
  23 /* $Log: packet.c,v $
  24  * Revision 0.8.4.7  1993/01/26  22:04:00  bir7
  25  * Added support for proc fs.
  26  *
  27  * Revision 0.8.4.6  1993/01/23  18:00:11  bir7
  28  * Added volatile keyword
  29  *
  30  * Revision 0.8.4.5  1992/12/12  19:25:04  bir7
  31  * Cleaned up Log messages.
  32  *
  33  * Revision 0.8.4.4  1992/12/12  01:50:49  bir7
  34  * Fixed bug in call to err routine.
  35  *
  36  * Revision 0.8.4.3  1992/11/17  14:19:47  bir7
  37  * *** empty log message ***
  38  *
  39  * Revision 0.8.4.2  1992/11/10  10:38:48  bir7
  40  * Change free_s to kfree_s and accidently changed free_skb to kfree_skb.
  41  *
  42  * Revision 0.8.4.1  1992/11/10  00:17:18  bir7
  43  * version change only.
  44  *
  45  * Revision 0.8.3.3  1992/11/10  00:14:47  bir7
  46  * Changed malloc to kmalloc and added Id and Log
  47  * */
  48 
  49 #include <linux/types.h>
  50 #include <linux/sched.h>
  51 #include <linux/fcntl.h>
  52 #include <linux/socket.h>
  53 #include <netinet/in.h>
  54 #include "timer.h"
  55 #include "ip.h"
  56 #include "tcp.h"
  57 #include "sock.h"
  58 #include <linux/errno.h>
  59 #include <linux/timer.h>
  60 #include <asm/system.h>
  61 #include <asm/segment.h>
  62 #include "../kern_sock.h" /* for PRINTK */
  63 
  64 extern struct proto raw_prot;
  65 
  66 static  unsigned long
  67 min(unsigned long a, unsigned long b)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69    if (a < b) return (a);
  70    return (b);
  71 }
  72 
  73 /* this should be the easiest of all, all we do is copy it into
  74    a buffer. */
  75 int
  76 packet_rcv (struct sk_buff *skb, struct device *dev,  struct packet_type *pt)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78    volatile struct sock *sk;
  79 
  80    sk = pt->data;
  81    skb->dev = dev;
  82    skb->len += dev->hard_header_len;
  83 
  84    /* now see if we are in use. */
  85    cli();
  86    if (sk->inuse)
  87      {
  88         sti();
  89         /* drop any packets if we can't currently deal with them.
  90            Assume that the other end will retransmit if it was
  91            important. */
  92         skb->sk = NULL;
  93         kfree_skb (skb, FREE_READ);
  94         return (0);
  95 
  96      }
  97    sk->inuse = 1;
  98    sti ();
  99 
 100    skb->sk = sk;
 101 
 102    /* charge it too the socket. */
 103    if (sk->rmem_alloc + skb->mem_len >= SK_RMEM_MAX)
 104      {
 105         skb->sk = NULL;
 106         kfree_skb (skb, FREE_READ);
 107         return (0);
 108      }
 109              
 110    sk->rmem_alloc += skb->mem_len;
 111 
 112    /* now just put it onto the queue. */
 113    if (sk->rqueue == NULL)
 114      {
 115         sk->rqueue = skb;
 116         skb->next = skb;
 117         skb->prev = skb;
 118      }
 119    else
 120      {
 121         skb->next = sk->rqueue;
 122         skb->prev = sk->rqueue->prev;
 123         skb->prev->next = skb;
 124         skb->next->prev = skb;
 125      }
 126    wake_up (sk->sleep);
 127    release_sock (sk);
 128    return (0);
 129 }
 130 
 131 /* this will do terrible things if len + ipheader + devheader > dev->mtu */
 132 static int
 133 packet_sendto (volatile struct sock *sk, unsigned char *from, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 134                int noblock,
 135                unsigned flags, struct sockaddr_in *usin, int addr_len)
 136 {
 137    struct sk_buff *skb;
 138    struct device *dev;
 139    struct sockaddr saddr;
 140 
 141    /* check the flags. */
 142    if (flags) return (-EINVAL);
 143    if (len < 0) return (-EINVAL);
 144 
 145    /* get and verify the address. */
 146    if (usin)
 147      {
 148         if (addr_len < sizeof (saddr))
 149           return (-EINVAL);
 150 /*      verify_area (VERIFY_WRITE, usin, sizeof (saddr));*/
 151         memcpy_fromfs (&saddr, usin, sizeof(saddr));
 152      }
 153    else
 154      return (-EINVAL);
 155 
 156    skb = sk->prot->wmalloc (sk, len+sizeof (*skb), 0, GFP_KERNEL);
 157 
 158    /* this shouldn't happen, but it could. */
 159    if (skb == NULL)
 160      {
 161         PRINTK (("packet_sendto: write buffer full?\n"));
 162         return (-EAGAIN);
 163      }
 164    skb->lock = 0;
 165    skb->mem_addr = skb;
 166    skb->mem_len = len + sizeof (*skb);
 167    skb->sk = sk;
 168    skb->free = 1;
 169    saddr.sa_data[13] = 0;
 170    dev = get_dev (saddr.sa_data);
 171    if (dev == NULL)
 172      {
 173         sk->prot->wfree (sk, skb->mem_addr, skb->mem_len);
 174         return (-ENXIO);
 175      }
 176 /*   verify_area (VERIFY_WRITE, from, len);*/
 177    memcpy_fromfs (skb+1, from, len);
 178    skb->len = len;
 179    skb->next = NULL;
 180    if (dev->up)
 181      dev->queue_xmit (skb, dev, sk->priority);
 182    else
 183      kfree_skb (skb, FREE_WRITE);
 184    return (len);
 185 }
 186 
 187 static int
 188 packet_write (volatile struct sock *sk, unsigned char *buff, 
     /* [previous][next][first][last][top][bottom][index][help] */
 189               int len, int noblock,  unsigned flags)
 190 {
 191    return (packet_sendto (sk, buff, len, noblock, flags, NULL, 0));
 192 }
 193 
 194 static void
 195 packet_close (volatile struct sock *sk, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 196 {
 197    sk->inuse = 1;
 198    sk->state = TCP_CLOSE;
 199    dev_remove_pack ((struct packet_type *)sk->pair);
 200    kfree_s ((void *)sk->pair, sizeof (struct packet_type));
 201    sk->pair = NULL;
 202    release_sock (sk);
 203 }
 204 
 205 static int
 206 packet_init (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208    struct packet_type *p;
 209    p = kmalloc (sizeof (*p), GFP_KERNEL);
 210    if (p == NULL) return (-ENOMEM);
 211 
 212    p->func = packet_rcv;
 213    p->type = sk->num;
 214    p->data = (void *)sk;
 215    dev_add_pack (p);
 216    
 217    /* we need to remember this somewhere. */
 218    sk->pair = (volatile struct sock *)p;
 219 
 220    return (0);
 221 }
 222 
 223 
 224 int
 225 packet_recvfrom (volatile struct sock *sk, unsigned char *to, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 226               int noblock,
 227               unsigned flags, struct sockaddr_in *sin, int *addr_len)
 228 {
 229         /* this should be easy, if there is something there we
 230            return it, otherwise we block. */
 231         int copied=0;
 232         struct sk_buff *skb;
 233         struct sockaddr *saddr;
 234         saddr = (struct sockaddr *)sin;
 235 
 236         if (len == 0) return (0);
 237         if (len < 0) return (-EINVAL);
 238 
 239         if (sk->shutdown & RCV_SHUTDOWN) return (0);
 240 
 241         if (addr_len)
 242           {
 243                   verify_area (VERIFY_WRITE, addr_len, sizeof(*addr_len));
 244                   put_fs_long (sizeof (*saddr), addr_len);
 245           }
 246 
 247         sk->inuse = 1;
 248         while (sk->rqueue == NULL)
 249           {
 250              if (noblock)
 251                {
 252                   release_sock (sk);
 253                   return (-EAGAIN);
 254                }
 255              release_sock (sk);
 256              cli();
 257              if (sk->rqueue == NULL)
 258                {
 259                   interruptible_sleep_on (sk->sleep);
 260                   if (current->signal & ~current->blocked)
 261                     {
 262                        sti();
 263                        return (-ERESTARTSYS);
 264                     }
 265                }
 266              sk->inuse = 1;
 267              sti();
 268           }
 269         skb = sk->rqueue;
 270 
 271         if (!(flags & MSG_PEEK))
 272           {
 273                   if (skb->next == skb )
 274                     {
 275                             sk->rqueue = NULL;
 276                     }
 277                   else
 278                     {
 279                             sk->rqueue = (struct sk_buff *)sk->rqueue ->next;
 280                             skb->prev->next = skb->next;
 281                             skb->next->prev = skb->prev;
 282                     }
 283           }
 284         copied = min (len, skb->len);
 285         verify_area (VERIFY_WRITE, to, copied);
 286         memcpy_tofs (to, skb+1,  copied);
 287         /* copy the address. */
 288         if (saddr)
 289           {
 290                   struct sockaddr addr;
 291                   addr.sa_family = skb->dev->type;
 292                   memcpy (addr.sa_data,skb->dev->name, 14);
 293                   verify_area (VERIFY_WRITE, saddr, sizeof (*saddr));
 294                   memcpy_tofs(saddr, &addr, sizeof (*saddr));
 295           }
 296 
 297         if (!(flags & MSG_PEEK))
 298           {
 299              kfree_skb (skb, FREE_READ);
 300           }
 301 
 302         release_sock (sk);
 303         return (copied);
 304 
 305 }
 306 
 307 int
 308 packet_read (volatile struct sock *sk, unsigned char *buff,
     /* [previous][next][first][last][top][bottom][index][help] */
 309              int len, int noblock, unsigned flags)
 310 {
 311         return (packet_recvfrom (sk, buff, len, noblock, flags, NULL, NULL));
 312 }
 313 
 314 
 315 int udp_connect (volatile struct sock *sk, struct sockaddr_in *usin,
 316                  int addr_len);
 317 
 318 int udp_select (volatile struct sock *sk, int sel_type, select_table *wait);
 319 
 320 
 321 struct proto packet_prot =
 322 {
 323   sock_wmalloc,
 324   sock_rmalloc,
 325   sock_wfree,
 326   sock_rfree,
 327   sock_rspace,
 328   sock_wspace,
 329   packet_close,
 330   packet_read,
 331   packet_write,
 332   packet_sendto,
 333   packet_recvfrom,
 334   ip_build_header,
 335   udp_connect,
 336   NULL,
 337   ip_queue_xmit,
 338   ip_retransmit,
 339   NULL,
 340   NULL,
 341   NULL, 
 342   udp_select,
 343   NULL,
 344   packet_init,
 345   NULL,
 346   128,
 347   0,
 348   {NULL,},
 349   "PACKET"
 350 };

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