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

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