root/net/tcp/sock.c

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

DEFINITIONS

This source file includes following definitions.
  1. print_sk
  2. print_skb
  3. dummy_routine
  4. lock_skb
  5. kfree_skb
  6. unlock_skb
  7. sk_inuse
  8. get_new_socknum
  9. put_sock
  10. remove_sock
  11. destroy_sock
  12. ip_proto_fcntl
  13. ip_proto_setsockopt
  14. ip_proto_getsockopt
  15. ip_proto_listen
  16. ip_proto_init
  17. ip_proto_create
  18. ip_proto_dup
  19. ip_proto_release
  20. ip_proto_bind
  21. ip_proto_connect
  22. ip_proto_socketpair
  23. ip_proto_accept
  24. ip_proto_getname
  25. ip_proto_read
  26. ip_proto_recv
  27. ip_proto_write
  28. ip_proto_send
  29. ip_proto_sendto
  30. ip_proto_recvfrom
  31. ip_proto_shutdown
  32. ip_proto_select
  33. ip_proto_ioctl
  34. sock_wmalloc
  35. sock_rmalloc
  36. sock_rspace
  37. sock_wspace
  38. sock_wfree
  39. sock_rfree
  40. get_sock
  41. release_sock

   1 /* sock.c */
   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: sock.c,v 0.8.4.12 1992/12/12 19:25:04 bir7 Exp $ */
  23 /* $Log: sock.c,v $
  24  * Revision 0.8.4.12  1992/12/12  19:25:04  bir7
  25  * Made memory leak checking more leanent.
  26  *
  27  * Revision 0.8.4.11  1992/12/12  01:50:49  bir7
  28  * Fixed memory leak in accept.
  29  *
  30  * Revision 0.8.4.10  1992/12/08  20:49:15  bir7
  31  * Added support for -EINPROGRESS
  32  *
  33  * Revision 0.8.4.9  1992/12/06  23:29:59  bir7
  34  * Added mss and support for half completed packets.
  35  *
  36  * Revision 0.8.4.8  1992/12/05  21:35:53  bir7
  37  * changed dev->init to return an int.
  38  *
  39  * Revision 0.8.4.7  1992/12/03  19:52:20  bir7
  40  * added paranoid queue checking
  41  *
  42  * Revision 0.8.4.6  1992/11/18  15:38:03  bir7
  43  * Fixed minor problem in setsockopt.
  44  *
  45  * Revision 0.8.4.5  1992/11/17  14:19:47  bir7
  46  *
  47  * Revision 0.8.4.4  1992/11/16  16:13:40  bir7
  48  * Fixed some error returns and undid one of the accept changes.
  49  *
  50  * Revision 0.8.4.3  1992/11/15  14:55:30  bir7
  51  * Added more checking for a packet being on a queue before it's
  52  * dropped when a socket is closed.  Added check to see if it's
  53  * on the arp_q also.
  54  *
  55  * Revision 0.8.4.2  1992/11/10  10:38:48  bir7
  56  * Change free_s to kfree_s and accidently changed free_skb to kfree_skb.
  57  *
  58  * Revision 0.8.4.1  1992/11/10  00:17:18  bir7
  59  * version change only.
  60  *
  61  * Revision 0.8.3.5  1992/11/10  00:14:47  bir7
  62  * Changed malloc to kmalloc and added Id and Log
  63  * */
  64 
  65 #include <linux/errno.h>
  66 #include <linux/types.h>
  67 #include <linux/socket.h>
  68 #include <netinet/in.h>
  69 #include <linux/kernel.h>
  70 #include <linux/sched.h>
  71 #include <linux/timer.h>
  72 #include <linux/string.h>
  73 #include <linux/sock_ioctl.h>
  74 #include "../kern_sock.h"
  75 #include "timer.h"
  76 #include "ip.h"
  77 #include "tcp.h"
  78 #include "udp.h"
  79 #include "sock.h"
  80 #include "arp.h"
  81 #include <asm/segment.h>
  82 #include <asm/system.h>
  83 #include <linux/fcntl.h>
  84 #include <linux/mm.h>
  85 #include <linux/interrupt.h>
  86 
  87 #undef ISOCK_DEBUG
  88 
  89 #ifdef PRINTK
  90 #undef PRINTK
  91 #endif
  92 
  93 #ifdef ISOCK_DEBUG
  94 #define PRINTK printk
  95 #else
  96 #define PRINTK dummy_routine
  97 #endif
  98 
  99 #ifdef MEM_DEBUG
 100 #define MPRINTK printk
 101 #else
 102 #define MPRINTK dummy_routine
 103 #endif
 104 
 105 #define min(a,b) ((a)<(b)?(a):(b))
 106 #define swap(a,b) {unsigned long c; c=a; a=b; b=c;}
 107 
 108 extern struct proto tcp_prot;
 109 extern struct proto udp_prot;
 110 extern struct proto raw_prot;
 111 extern struct proto packet_prot;
 112 
 113 static int ip_proto_init(void);
 114 static int ip_proto_create(struct socket *sock, int protocol);
 115 static int ip_proto_dup(struct socket *newsock, struct socket *oldsock);
 116 static int ip_proto_release(struct socket *sock, struct socket *peer);
 117 static int ip_proto_bind(struct socket *sock, struct sockaddr *umyaddr,
 118                            int sockaddr_len);
 119 static int ip_proto_connect(struct socket *sock, struct sockaddr *uservaddr,
 120                               int sockaddr_len, int flags);
 121 static int ip_proto_socketpair(struct socket *sock1, struct socket *sock2);
 122 static int ip_proto_accept(struct socket *sock, struct socket *newsock, int flags);
 123 static int ip_proto_getname(struct socket *sock, struct sockaddr *usockaddr,
 124                               int *usockaddr_len, int peer);
 125 static int ip_proto_read(struct socket *sock, char *ubuf, int size,
 126                            int nonblock);
 127 static int ip_proto_write(struct socket *sock, char *ubuf, int size,
 128                             int nonblock);
 129 static int ip_proto_select(struct socket *sock, int which, select_table *wait);
 130 static int ip_proto_ioctl(struct socket *sock, unsigned int cmd,
 131                             unsigned long arg);
 132 static int ip_proto_listen(struct socket *sock, int backlog);
 133 
 134 static int ip_proto_send (struct socket *sock, void *buff, int len,
 135                           int nonblock, unsigned flags);
 136 static int ip_proto_recv (struct socket *sock, void *buff, int len,
 137                           int nonblock, unsigned flags);
 138 static int ip_proto_sendto (struct socket *sock, void *buff, int len,
 139                             int nonblock, unsigned flags,
 140                             struct sockaddr *addr, int addr_len);
 141 static int ip_proto_recvfrom (struct socket *sock, void *buff, int len,
 142                               int nonblock, unsigned flags,
 143                               struct sockaddr *addr, int *addr_len);
 144 
 145 static int ip_proto_shutdown (struct socket *sock, int how);
 146 
 147 
 148 static int ip_proto_setsockopt (struct socket *sock, int level, int optname,
 149                                 char *optval, int optlen);
 150 static int ip_proto_getsockopt (struct socket *sock, int level, int optname,
 151                                 char *optval, int *optlen);
 152 static int ip_proto_fcntl (struct socket *sock, unsigned int cmd,
 153                            unsigned long arg);
 154 
 155 
 156 struct proto_ops inet_proto_ops = 
 157 {
 158   ip_proto_init,
 159   ip_proto_create,
 160   ip_proto_dup,
 161   ip_proto_release,
 162   ip_proto_bind,
 163   ip_proto_connect,
 164   ip_proto_socketpair,
 165   ip_proto_accept,
 166   ip_proto_getname, 
 167   ip_proto_read,
 168   ip_proto_write,
 169   ip_proto_select,
 170   ip_proto_ioctl,
 171   ip_proto_listen,
 172   ip_proto_send,
 173   ip_proto_recv,
 174   ip_proto_sendto,
 175   ip_proto_recvfrom,
 176   ip_proto_shutdown,
 177   ip_proto_setsockopt,
 178   ip_proto_getsockopt,
 179   ip_proto_fcntl
 180 };
 181 
 182 void
 183 print_sk (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185   if (!sk) {
 186     PRINTK ("  print_sk(NULL)\n");
 187     return;
 188   }
 189   PRINTK ("  wmem_alloc = %d\n", sk->wmem_alloc);
 190   PRINTK ("  rmem_alloc = %d\n", sk->rmem_alloc);
 191   PRINTK ("  send_head = %X\n", sk->send_head);
 192   PRINTK ("  state = %d\n",sk->state);
 193   PRINTK ("  wback = %X, rqueue = %X\n", sk->wback, sk->rqueue);
 194   PRINTK ("  wfront = %X\n", sk->wfront);
 195   PRINTK ("  daddr = %X, saddr = %X\n", sk->daddr,sk->saddr);
 196   PRINTK ("  num = %d", sk->num);
 197   PRINTK (" next = %X\n", sk->next);
 198   PRINTK ("  send_seq = %d, acked_seq = %d, copied_seq = %d\n",
 199           sk->send_seq, sk->acked_seq, sk->copied_seq);
 200   PRINTK ("  rcv_ack_seq = %d, window_seq = %d, fin_seq = %d\n",
 201           sk->rcv_ack_seq, sk->window_seq, sk->fin_seq);
 202   PRINTK ("  prot = %X\n", sk->prot);
 203   PRINTK ("  pair = %X, back_log = %X\n", sk->pair,sk->back_log);
 204   PRINTK ("  inuse = %d , blog = %d\n", sk->inuse, sk->blog);
 205   PRINTK ("  dead = %d delay_acks=%d\n", sk->dead, sk->delay_acks);
 206   PRINTK ("  retransmits = %d, timeout = %d\n", sk->retransmits, sk->timeout);
 207   PRINTK ("  cong_window = %d, packets_out = %d\n", sk->cong_window,
 208           sk->packets_out);
 209 }
 210 
 211 void
 212 print_skb(struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 213 {
 214   if (!skb) {
 215     PRINTK ("  print_skb(NULL)\n");
 216     return;
 217   }
 218   PRINTK ("  prev = %X, next = %X\n", skb->prev, skb->next);
 219   PRINTK ("  sk = %X link3 = %X\n", skb->sk, skb->link3);
 220   PRINTK ("  mem_addr = %X, mem_len = %d\n", skb->mem_addr, skb->mem_len);
 221   PRINTK ("  used = %d free = %d\n", skb->used,skb->free);
 222 }
 223 
 224 /* just used to reference some pointers to keep gcc from over optimizing
 225    my code so that it doesn't work. */
 226 void dummy_routine(void *dummy, ...)
     /* [previous][next][first][last][top][bottom][index][help] */
 227 {
 228    return;
 229 }
 230 
 231 void
 232 lock_skb (struct sk_buff *skb)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234    if (skb->lock)
 235      {
 236         printk ("*** bug more than one lock on sk_buff. \n");
 237      }
 238    skb->lock = 1;
 239 }
 240 
 241 
 242 void
 243 kfree_skb (struct sk_buff *skb, int rw)
     /* [previous][next][first][last][top][bottom][index][help] */
 244 {
 245   if (skb == NULL)
 246     {
 247       printk ("kfree_skb: skb = NULL\n");
 248       return;
 249     }
 250 
 251    if (skb->lock)
 252      {
 253         skb->free = 1;
 254         return;
 255      }
 256    skb->magic = 0;
 257    if (skb->sk)
 258      {
 259         if (rw)
 260           {
 261              skb->sk->prot->rfree (skb->sk, skb->mem_addr, skb->mem_len);
 262           }
 263         else
 264           {
 265              skb->sk->prot->wfree (skb->sk, skb->mem_addr, skb->mem_len);
 266           }
 267      }
 268    else
 269      {
 270         kfree_s (skb->mem_addr, skb->mem_len);
 271      }
 272 }
 273 
 274 void
 275 unlock_skb (struct sk_buff *skb, int rw)
     /* [previous][next][first][last][top][bottom][index][help] */
 276 {
 277    if (skb->lock != 1)
 278      {
 279         printk ("*** bug unlocking non-locked sk_buff. \n");
 280      }
 281    skb->lock = 0;
 282    if (skb->free)
 283      kfree_skb (skb, rw);
 284 }
 285 
 286 static  int
 287 sk_inuse( struct proto *prot, int num)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289   volatile struct sock *sk;
 290   for (sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];
 291        sk != NULL; sk=sk->next)
 292     {
 293       if (sk->dummy_th.source == num) return (1);
 294     }
 295   return (0);
 296 }
 297 
 298 unsigned short
 299 get_new_socknum(struct proto *prot, unsigned short base)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301   static int start=0;
 302   /* used to cycle through the port numbers so the chances of
 303      a confused connection drop. */
 304 
 305   int i,j;
 306   int best=0;
 307   int size=32767; /* a big num. */
 308   volatile struct sock *sk;
 309 
 310   if (base == 0) base = PROT_SOCK+1+(start % 1024);
 311   if (base <= PROT_SOCK)
 312     {
 313       base += PROT_SOCK+(start % 1024);
 314     }
 315 
 316   /* now look through the entire array and try to find an empty
 317      ptr. */
 318   for (i=0; i < SOCK_ARRAY_SIZE; i++)
 319     {
 320       j = 0;
 321       sk = prot->sock_array[(i+base+1) & (SOCK_ARRAY_SIZE -1)];
 322       while (sk != NULL)
 323         {
 324           sk = sk->next;
 325           j++;
 326         }
 327       if (j == 0)
 328         {
 329           start = (i+1+start )%1024;
 330           PRINTK ("get_new_socknum returning %d, start = %d\n",
 331                     i+base+1,start);
 332           return (i+base+1);
 333         }
 334       if (j < size) 
 335         {
 336           best = i;
 337           size = j;
 338         }
 339     }
 340   /* now make sure the one we want is not in use. */
 341   while (sk_inuse (prot, base +best+1))
 342     {
 343       best += SOCK_ARRAY_SIZE;
 344     }
 345   PRINTK ("get_new_socknum returning %d, start = %d\n", best+base+1,start);
 346   return (best+base+1);
 347   
 348 }
 349 
 350 void
 351 put_sock(unsigned short num, volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 352 {
 353    volatile struct sock *sk1;
 354    volatile struct sock *sk2;
 355    int mask;
 356 
 357    PRINTK ("put_sock (num = %d, sk = %X\n", num, sk);
 358    sk->num = num;
 359    sk->next = NULL;
 360    num = num & (SOCK_ARRAY_SIZE -1);
 361 
 362    /* we can't have an interupt renter here. */
 363    cli();
 364    if (sk->prot->sock_array[num] == NULL)
 365      {
 366         sk->prot->sock_array[num] = sk;
 367         sti();
 368         return;
 369      }
 370    sti();
 371    for (mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask)
 372      {
 373         if (mask & sk->saddr)
 374           {
 375              mask = mask << 8;
 376              break;
 377           }
 378      }
 379 
 380    PRINTK ("mask = %X\n", mask);
 381 
 382    cli();
 383    sk1 = sk->prot->sock_array[num];
 384    for (sk2 = sk1; sk2 != NULL; sk2=sk2->next)
 385      {
 386         if (!(sk2->saddr & mask))
 387           {
 388              if (sk2 == sk1)
 389                {
 390                   sk->next = sk->prot->sock_array[num];
 391                   sk->prot->sock_array[num] = sk;
 392                   sti();
 393                   return;
 394                }
 395              sk->next = sk2;
 396              sk1->next= sk;
 397              sti();
 398              return;
 399           }
 400         sk1 = sk2;
 401      }
 402    /* goes at the end. */
 403    sk->next = NULL;
 404    sk1->next = sk;
 405    sti();
 406 }
 407 
 408 
 409 static void
 410 remove_sock(volatile struct sock *sk1)
     /* [previous][next][first][last][top][bottom][index][help] */
 411 {
 412   volatile struct sock *sk2;
 413   PRINTK ("remove_sock(sk1=%X)\n",sk1);
 414 
 415   if (!sk1)
 416     {
 417       printk ("sock.c: remove_sock: sk1 == NULL\n");
 418       return;
 419     }
 420 
 421   if (!sk1->prot)
 422     {
 423       printk ("sock.c: remove_sock: sk1->prot == NULL\n");
 424       return;
 425     }
 426 
 427   /* we can't have this changing out from under us. */
 428   cli();
 429   sk2 = sk1->prot->sock_array[sk1->num & (SOCK_ARRAY_SIZE -1)];
 430   if (sk2 == sk1)
 431     {
 432        sk1->prot->sock_array[sk1->num & (SOCK_ARRAY_SIZE -1)] = sk1->next;
 433        sti();
 434        return;
 435     }
 436 
 437   while (sk2 && sk2->next != sk1)
 438     sk2 = sk2->next;
 439 
 440   if (sk2)
 441     {
 442       sk2->next = sk1->next;
 443       sti();
 444       return;
 445     }
 446   sti();
 447 
 448   if (sk1->num != 0)
 449     PRINTK ("remove_sock: sock  not found.\n");
 450 }
 451 
 452 void
 453 destroy_sock(volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 454 {
 455 
 456   struct sk_buff *skb;
 457   PRINTK ("destroying socket %X\n",sk);
 458   /* just to be safe. */
 459   sk->inuse = 1;
 460 
 461   /* incase it's sleeping somewhere. */
 462   if (!sk->dead) wake_up (sk->sleep);
 463 
 464   remove_sock (sk);
 465   /* now we can no longer get new packets. */
 466 
 467   delete_timer((struct timer *)&sk->time_wait);
 468 
 469   if (sk->send_tmp != NULL) kfree_skb (sk->send_tmp, FREE_WRITE);
 470 
 471   /* cleanup up the write buffer. */
 472   for (skb = sk->wfront; skb != NULL; )
 473     {
 474       struct sk_buff *skb2;
 475       skb2=skb->next;
 476       if (skb->magic != TCP_WRITE_QUEUE_MAGIC)
 477         {
 478           printk ("sock.c:destroy_sock write queue with bad magic (%X)\n",
 479                   skb->magic);
 480           break;
 481         }
 482       kfree_skb(skb, FREE_WRITE);
 483       skb=skb2;
 484     }
 485 
 486   sk->wfront = NULL;
 487   sk->wback = NULL;
 488 
 489   if (sk->rqueue != NULL)
 490     {
 491        skb = sk->rqueue;
 492        do {
 493           struct sk_buff *skb2;
 494           skb2=skb->next;
 495 
 496           /* this will take care of closing sockets that were
 497              listening and didn't accept everything. */
 498 
 499           if (skb->sk != NULL && skb->sk != sk)
 500             {
 501                skb->sk->dead = 1;
 502                skb->sk->prot->close (skb->sk, 0);
 503             }
 504           kfree_skb(skb, FREE_READ);
 505           skb=skb2;
 506        } while (skb != sk->rqueue);
 507     }
 508 
 509   sk->rqueue = NULL;
 510 
 511   /* now we need to clean up the send head. */
 512   for (skb = sk->send_head; skb != NULL; )
 513     {
 514       struct sk_buff *skb2;
 515       /* we need to remove skb from the transmit queue, or
 516        maybe the arp queue */
 517       cli();
 518       /* see if it's in a transmit queue. */
 519       /* this can be simplified quite a bit.  Look
 520          at tcp.c:tcp_ack to see how. */
 521 
 522       if (skb->next != NULL)
 523         {
 524           extern struct sk_buff *arp_q;
 525           int i;
 526            if (skb->next != skb)
 527              {
 528                 skb->next->prev = skb->prev;
 529                 skb->prev->next = skb->next;
 530 
 531                 if (skb == arp_q)
 532                   {
 533                     if (skb->magic != ARP_QUEUE_MAGIC)
 534                       {
 535                         sti();
 536                         printk ("sock.c: destroy_sock skb on arp queue with"
 537                                 "bas magic (%X)\n", skb->magic);
 538                         cli();
 539                         arp_q = NULL;
 540                         continue;
 541                       }
 542                     arp_q = skb->next;
 543                   }
 544                 else
 545                   {
 546                     for (i = 0; i < DEV_NUMBUFFS; i++)
 547                       {
 548                         if (skb->dev && skb->dev->buffs[i] == skb)
 549                           {
 550                             if (skb->magic != DEV_QUEUE_MAGIC)
 551                               {
 552                                 sti();
 553                                 printk ("sock.c: destroy sock skb on dev queue"
 554                                         "with bad magic (%X)\n", skb->magic);
 555                                 cli();
 556                                 break;
 557                               }
 558                             skb->dev->buffs[i]= skb->next;
 559                             break;
 560                           }
 561                       }
 562                   }
 563               }
 564            else
 565              {
 566                if (skb == arp_q)
 567                  {
 568                     if (skb->magic != ARP_QUEUE_MAGIC)
 569                       {
 570                         sti();
 571                         printk ("sock.c: destroy_sock skb on arp queue with"
 572                                 "bas magic (%X)\n", skb->magic);
 573                         cli();
 574                       }
 575                    arp_q = NULL;
 576                  }
 577                else
 578                  {
 579                    for (i = 0; i < DEV_NUMBUFFS; i++)
 580                      {
 581                        if (skb->dev && skb->dev->buffs[i] == skb)
 582                          {
 583                             if (skb->magic != DEV_QUEUE_MAGIC)
 584                               {
 585                                 sti();
 586                                 printk ("sock.c: destroy sock skb on dev queue"
 587                                         "with bad magic (%X)\n", skb->magic);
 588                                 cli();
 589                                 break;
 590                               }
 591                            skb->dev->buffs[i]= NULL;
 592                            break;
 593                          }
 594                      }
 595                  }
 596              }
 597         }
 598       skb->dev = NULL;
 599       sti();
 600       skb2=skb->link3;
 601       kfree_skb(skb, FREE_WRITE);
 602       skb=skb2;
 603     }
 604 
 605   sk->send_head = NULL;
 606 
 607   /* and now the backlog. */
 608 
 609   if (sk->back_log != NULL)
 610     {
 611        /* this should never happen. */
 612        printk ("cleaning back_log. \n");
 613        cli();
 614        skb = sk->back_log;
 615        do {
 616           struct sk_buff *skb2;
 617           skb2=skb->next;
 618           kfree_skb(skb, FREE_READ);
 619           skb=skb2;
 620        } while (skb != sk->back_log);
 621        sti();
 622     }
 623 
 624   sk->back_log = NULL;
 625 
 626   /* Now if it has a half accepted/ closed socket. */
 627   if (sk->pair)
 628     {
 629       sk->pair->dead = 1;
 630       sk->pair->prot->close (sk->pair, 0);
 631       sk->pair = NULL;
 632     }
 633 
 634   /* now if everything is gone we can free the socket structure, 
 635      otherwise we need to keep it around until everything is gone. */
 636   if (sk->rmem_alloc == 0 && sk->wmem_alloc == 0)
 637     {
 638        kfree_s ((void *)sk,sizeof (*sk));
 639     }
 640   else
 641     {
 642        /* this should never happen. */
 643        /* actually it can if an ack has just been sent. */
 644        PRINTK ("possible memory leak in socket = %X\n", sk);
 645        print_sk (sk);
 646        sk->destroy = 1;
 647        sk->ack_backlog = 0;
 648        sk->inuse = 0;
 649        sk->time_wait.len = SOCK_DESTROY_TIME;
 650        sk->timeout = TIME_DESTROY;
 651        reset_timer ((struct timer *)&sk->time_wait);
 652     }
 653 
 654   PRINTK ("leaving destroy_sock\n");
 655   
 656 }
 657 
 658 
 659 static int
 660 ip_proto_fcntl (struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 661 {
 662    volatile struct sock *sk;
 663    sk=sock->data;
 664    if (sk == NULL)
 665      {
 666         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
 667         return (0);
 668      }
 669 
 670    switch (cmd)
 671      {
 672        case F_SETOWN:
 673         sk->proc = arg;
 674         return (0);
 675 
 676        case F_GETOWN:
 677         return (sk->proc);
 678 
 679        default:
 680         return (-EINVAL);
 681      }
 682 }
 683 
 684 static int
 685 ip_proto_setsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 686                     char *optval, int optlen)
 687 {
 688     volatile struct sock *sk;
 689     int val;
 690     /* This should really pass things on to the other levels. */
 691     if (level != SOL_SOCKET) return (-EOPNOTSUPP);
 692     sk = sock->data;
 693    if (sk == NULL)
 694      {
 695         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
 696         return (0);
 697      }
 698     if (optval == NULL) return (-EINVAL);
 699 
 700 /*    verify_area (optval, sizeof (int));*/
 701     val = get_fs_long ((unsigned long *)optval);
 702     switch (optname)
 703       {
 704         case SO_TYPE:
 705         case SO_ERROR:
 706         default:
 707           return (-ENOPROTOOPT);
 708 
 709         case SO_DEBUG: /* not implemented. */
 710         case SO_DONTROUTE:
 711         case SO_BROADCAST:
 712         case SO_SNDBUF:
 713         case SO_RCVBUF:
 714           return (0);
 715 
 716         case SO_REUSEADDR:
 717           if (val)
 718             sk->reuse = 1;
 719           else 
 720             sk->reuse = 0;
 721           return (0);
 722 
 723         case SO_KEEPALIVE:
 724           if (val)
 725             sk->keepopen = 1;
 726           else
 727             sk->keepopen = 0;
 728           return (0);
 729 
 730          case SO_OOBINLINE:
 731           if (val)
 732             sk->urginline = 1;
 733           else
 734             sk->urginline = 0;
 735           return (0);
 736 
 737          case SO_NO_CHECK:
 738           if (val)
 739             sk->no_check = 1;
 740           else
 741             sk->no_check = 0;
 742           return (0);
 743 
 744          case SO_PRIORITY:
 745           if (val >= 0 && val < DEV_NUMBUFFS)
 746             {
 747                sk->priority = val;
 748             }
 749           else
 750             {
 751                return (-EINVAL);
 752             }
 753           return (0);
 754 
 755       }
 756 }
 757 
 758 static int
 759 ip_proto_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 760                     char *optval, int *optlen)
 761 {
 762     volatile struct sock *sk;
 763     int val;
 764     /* This should really pass things on to the other levels. */
 765     if (level != SOL_SOCKET) return (-EOPNOTSUPP);
 766     sk = sock->data;
 767    if (sk == NULL)
 768      {
 769         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
 770         return (0);
 771      }
 772 
 773     switch (optname)
 774       {
 775         default:
 776           return (-ENOPROTOOPT);
 777 
 778         case SO_DEBUG: /* not implemented. */
 779         case SO_DONTROUTE:
 780         case SO_BROADCAST:
 781         case SO_SNDBUF:
 782         case SO_RCVBUF:
 783           val = 0;
 784           break;
 785 
 786         case SO_REUSEADDR:
 787           val = sk->reuse;
 788           break;
 789 
 790         case SO_KEEPALIVE:
 791           val = sk->keepopen;
 792           break;
 793 
 794         case SO_TYPE:
 795           if (sk->prot == &tcp_prot)
 796             val = SOCK_STREAM;
 797           else
 798             val = SOCK_DGRAM;
 799           break;
 800 
 801         case SO_ERROR:
 802           val = sk->err;
 803           sk->err = 0;
 804           break;
 805 
 806          case SO_OOBINLINE:
 807           val = sk->urginline;
 808           break;
 809 
 810          case SO_NO_CHECK:
 811           val = sk->no_check;
 812           break;
 813 
 814          case SO_PRIORITY:
 815           val = sk->priority;
 816           break;
 817       }
 818     verify_area (optlen, sizeof (int));
 819     put_fs_long (sizeof(int),(unsigned long *) optlen);
 820 
 821     verify_area(optval, sizeof (int));
 822     put_fs_long (val, (unsigned long *)optval);
 823     return (0);
 824 }
 825 
 826 static int
 827 ip_proto_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 828 {
 829   volatile struct sock *sk;
 830   sk = sock->data;
 831    if (sk == NULL)
 832      {
 833         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
 834         return (0);
 835      }
 836 
 837   /* we may need to bind the socket. */
 838   if (sk->num == 0)
 839     {
 840       sk->num = get_new_socknum (sk->prot, 0);
 841       if (sk->num == 0) return (-EAGAIN);
 842       put_sock (sk->num, sk);
 843       sk->dummy_th.source = net16(sk->num);
 844     }
 845 
 846   /* we might as well re use these. */ 
 847   sk->max_ack_backlog = backlog;
 848   sk->ack_backlog = 0;
 849   sk->state = TCP_LISTEN;
 850   return (0);
 851 }
 852 
 853 /* Hardware should be inited here. */
 854 static int ip_proto_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 855 {
 856   int i;
 857   struct device *dev, *dev2;
 858   struct ip_protocol *p;
 859   seq_offset = CURRENT_TIME*250;
 860   /* add all the protocols. */
 861   for (i = 0; i < SOCK_ARRAY_SIZE; i++)
 862     {
 863        tcp_prot.sock_array[i] = NULL;
 864        udp_prot.sock_array[i] = NULL;
 865        raw_prot.sock_array[i] = NULL;
 866     }
 867 
 868   for (p = ip_protocol_base; p != NULL;)
 869     {
 870        struct ip_protocol *tmp;
 871        /* add all the protocols. */
 872        tmp = p->next;
 873        add_ip_protocol (p);
 874        p = tmp;
 875     }
 876 
 877   /* add the devices */
 878   /* if the call to dev->init fails, the dev is removed
 879      from the chain disconnecting the device until the
 880      next reboot. */
 881 
 882   dev2 = NULL;
 883   for (dev = dev_base; dev != NULL; dev=dev->next)
 884     {
 885        if (dev->init && dev->init(dev))
 886          {
 887            if (dev2 == NULL)
 888              dev_base = dev->next;
 889            else
 890              dev2->next = dev->next;
 891          }
 892        else
 893          {
 894            dev2 = dev;
 895          }
 896     }
 897   bh_base[INET_BH].routine = inet_bh;
 898   timer_table[NET_TIMER].fn = net_timer;
 899   return (0);
 900 }
 901 
 902 static int
 903 ip_proto_create (struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 904 {
 905   volatile struct sock *sk;
 906   struct proto *prot;
 907   int err;
 908 
 909   sk = kmalloc (sizeof (*sk), GFP_KERNEL);
 910   if (sk == NULL)
 911     return (-ENOMEM);
 912   sk->num = 0;
 913 
 914 
 915   switch (sock->type)
 916     {
 917     case SOCK_STREAM:
 918     case SOCK_SEQPACKET:
 919        if (protocol && protocol != IPPROTO_TCP)
 920          {
 921             kfree_s ((void *)sk, sizeof (*sk));
 922             return (-EPROTONOSUPPORT);
 923          }
 924        sk->no_check = TCP_NO_CHECK;
 925        prot = &tcp_prot;
 926        break;
 927 
 928     case SOCK_DGRAM:
 929        if (protocol && protocol != IPPROTO_UDP)
 930          {
 931             kfree_s ((void *)sk, sizeof (*sk));
 932             return (-EPROTONOSUPPORT);
 933          }
 934        sk->no_check = UDP_NO_CHECK;
 935        prot=&udp_prot;
 936        break;
 937       
 938      case SOCK_RAW:
 939        if (!suser())
 940          {
 941             kfree_s ((void *)sk, sizeof (*sk));
 942             return (-EPERM);
 943          }
 944 
 945        if (!protocol)
 946          {
 947             kfree_s ((void *)sk, sizeof (*sk));
 948             return (-EPROTONOSUPPORT);
 949          }
 950        prot = &raw_prot;
 951        sk->reuse = 1;
 952        sk->no_check = 0; /* doesn't matter no checksum is preformed
 953                             anyway. */
 954        sk->num = protocol;
 955        break;
 956 
 957     case SOCK_PACKET:
 958        if (!suser())
 959          {
 960             kfree_s ((void *)sk, sizeof (*sk));
 961             return (-EPERM);
 962          }
 963 
 964        if (!protocol)
 965          {
 966             kfree_s ((void *)sk, sizeof (*sk));
 967             return (-EPROTONOSUPPORT);
 968          }
 969        prot = &packet_prot;
 970        sk->reuse = 1;
 971        sk->no_check = 0; /* doesn't matter no checksum is preformed
 972                             anyway. */
 973        sk->num = protocol;
 974        break;
 975 
 976       
 977     default:
 978        kfree_s ((void *)sk, sizeof (*sk));
 979        return (-ESOCKTNOSUPPORT);
 980 
 981     }
 982   sk->protocol = protocol;
 983   sk->wmem_alloc = 0;
 984   sk->rmem_alloc = 0;
 985   sk->pair = NULL;
 986   sk->opt = NULL;
 987   sk->send_seq = 0;
 988   sk->acked_seq = 0;
 989   sk->copied_seq = 0;
 990   sk->fin_seq = 0;
 991   sk->proc = 0;
 992   sk->rtt = TCP_WRITE_TIME;
 993   sk->packets_out = 0;
 994   sk->cong_window = 1; /* start with only sending one packet at a time. */
 995   sk->exp_growth = 1;  /* if set cong_window grow exponentially every time
 996                           we get an ack. */
 997   sk->urginline = 0;
 998   sk->intr = 0;
 999   sk->linger = 0;
1000   sk->destroy = 0;
1001   sk->reuse = 0;
1002   sk->priority = 1;
1003   sk->shutdown = 0;
1004   sk->urg = 0;
1005   sk->keepopen = 0;
1006   sk->done = 0;
1007   sk->ack_backlog = 0;
1008   sk->window = 0;
1009   sk->bytes_rcv = 0;
1010   sk->state = TCP_CLOSE;
1011   sk->dead = 0;
1012   sk->ack_timed = 0;
1013   sk->send_tmp = NULL;
1014   sk->mss = 0; /* we will try not to send any packets smaller
1015                    than this. */
1016 
1017   /* this is how many unacked bytes we will accept for
1018      this socket.  */
1019 
1020   sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
1021 
1022   /* how many packets we should send before forcing an ack. 
1023      if this is set to zero it is the same as sk->delay_acks = 0 */
1024 
1025   sk->max_ack_backlog = 0;
1026   sk->inuse = 0;
1027   sk->delay_acks = 0;
1028   sk->wback = NULL;
1029   sk->wfront = NULL;
1030   sk->rqueue = NULL;
1031   sk->mtu = 576;
1032   sk->prot = prot;
1033   sk->sleep = sock->wait;
1034   sk->daddr = 0;
1035   sk->saddr = MY_IP_ADDR;
1036   sk->err = 0;
1037   sk->next = NULL;
1038   sk->pair = NULL;
1039   sk->send_tail = NULL;
1040   sk->send_head = NULL;
1041   sk->time_wait.len = TCP_CONNECT_TIME;
1042   sk->time_wait.when = 0;
1043   sk->time_wait.sk = sk;
1044   sk->time_wait.next = NULL;
1045   sk->timeout = 0;
1046   sk->back_log = NULL;
1047   sk->blog = 0;
1048   sock->data =(void *) sk;
1049   sk->dummy_th.doff = sizeof (sk->dummy_th)/4;
1050   sk->dummy_th.res1=0;
1051   sk->dummy_th.res2=0;
1052   sk->dummy_th.urg_ptr = 0;
1053   sk->dummy_th.fin = 0;
1054   sk->dummy_th.syn = 0;
1055   sk->dummy_th.rst = 0;
1056   sk->dummy_th.psh = 0;
1057   sk->dummy_th.ack = 0;
1058   sk->dummy_th.urg = 0;
1059   sk->dummy_th.dest = 0;
1060 
1061   if (sk->num)
1062     {
1063       /* it assumes that any protocol which allows
1064          the user to assign a number at socket
1065          creation time automatically
1066          shares. */
1067       put_sock (sk->num, sk);
1068       sk->dummy_th.source = net16(sk->num);
1069     }
1070 
1071   if (sk->prot->init)
1072     {
1073        err = sk->prot->init(sk);
1074        if (err != 0)
1075          {
1076             destroy_sock (sk);
1077             return (err);
1078          }
1079     }
1080   return (0);
1081 }
1082 
1083 static int
1084 ip_proto_dup (struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
1085 {
1086   return (ip_proto_create (newsock,
1087                            ((volatile struct sock *)(oldsock->data))->protocol));
1088 }
1089 
1090 /* the peer socket should always be NULL. */
1091 static int
1092 ip_proto_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
1093 {
1094   volatile struct sock *sk;
1095   sk = sock->data;
1096   if (sk == NULL) return (0);
1097   PRINTK ("ip_proto_release (sock = %X, peer = %X)\n", sock, peer);
1098   wake_up (sk->sleep);
1099   /* start closing the connection.  This may take a while. */
1100   /* if linger is set, we don't return until the close is
1101      complete.  Other wise we return immediately.  The
1102      actually closing is done the same either way. */
1103   if (sk->linger == 0)
1104     {
1105        sk->prot->close(sk,0);
1106        sk->dead = 1;
1107     }
1108   else
1109     {
1110       PRINTK ("sk->linger set.\n");
1111        sk->prot->close(sk, 0);
1112        cli();
1113        while (sk->state != TCP_CLOSE)
1114          {
1115             interruptible_sleep_on (sk->sleep);
1116             if (current->signal & ~current->blocked)
1117               {
1118                  sti();
1119                  return (-ERESTARTSYS);
1120               }
1121          }
1122        sti();
1123        sk->dead = 1;
1124     }
1125 
1126   sk->inuse = 1;
1127   /* this will destroy it. */
1128   release_sock (sk);
1129   sock->data = NULL;
1130   PRINTK ("ip_proto_release returning\n");
1131   return (0);
1132 }
1133 
1134 
1135 /* this needs to be changed to dissallow
1136    the rebinding of sockets.   What error
1137    should it return? */
1138 
1139 static int
1140 ip_proto_bind (struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1141                int addr_len)
1142 {
1143   struct sockaddr_in addr;
1144   volatile struct sock *sk, *sk2;
1145   unsigned short snum;
1146 
1147   sk = sock->data;
1148    if (sk == NULL)
1149      {
1150         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1151         return (0);
1152      }
1153   /* check this error. */
1154   if (sk->state != TCP_CLOSE) return (-EIO);
1155   if (sk->num != 0) return (-EINVAL);
1156 
1157 /*  verify_area (uaddr, addr_len);*/
1158   memcpy_fromfs (&addr, uaddr, min (sizeof (addr), addr_len));
1159   if (addr.sin_family && addr.sin_family != AF_INET)
1160     return (-EINVAL); /* this needs to be changed. */
1161 
1162   snum = net16(addr.sin_port);
1163   PRINTK ("bind sk =%X to port = %d\n", sk, snum);
1164   print_sk (sk);
1165   sk = sock->data;
1166 
1167   /* we can't just leave the socket bound wherever it is, it might be bound
1168      to a priveledged port. However, since there seems to be a bug here,
1169      we will leave it if the port is not priveledged(sp?) */
1170 
1171   if (snum == 0)
1172     {
1173        if ( sk->num > PROT_SOCK) return (0);
1174        snum = get_new_socknum (sk->prot, 0);
1175     }
1176 
1177   if (snum <= PROT_SOCK && !suser())
1178     return (-EACCES);
1179 
1180   if (my_ip_addr(addr.sin_addr.s_addr) || addr.sin_addr.s_addr == 0)
1181     sk->saddr = addr.sin_addr.s_addr;
1182 
1183   PRINTK ("sock_array[%d] = %X:\n", snum & (SOCK_ARRAY_SIZE -1),
1184           sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)]);
1185   print_sk (sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)]);
1186 
1187   /* make sure we are allowed to bind here. */
1188   for (sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
1189        sk2 != NULL;
1190        sk2 = sk2->next)
1191     {
1192        if (sk2->num != snum) continue;
1193        if (sk2->saddr != sk->saddr) continue;
1194        if (!sk->reuse) return (-EADDRINUSE);
1195        if (!sk2->reuse) return (-EADDRINUSE);
1196     }
1197   remove_sock (sk);
1198   put_sock(snum, sk);
1199   sk->dummy_th.source = net16(sk->num);
1200   sk->daddr = 0;
1201   sk->dummy_th.dest = 0;
1202   return (0);
1203 }
1204 
1205 static int
1206 ip_proto_connect (struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1207                   int addr_len, int flags)
1208 {
1209   volatile struct sock *sk;
1210   int err;
1211   sock->conn = NULL;
1212   sk = sock->data;
1213    if (sk == NULL)
1214      {
1215         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1216         return (0);
1217      }
1218 
1219   if (sk->state == TCP_ESTABLISHED)
1220     {
1221       sock->state = SS_CONNECTED;
1222       return (-EISCONN);
1223     }
1224 
1225   if (sock->state == SS_CONNECTING)
1226     {
1227       if (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
1228         return (-EINPROGRESS);
1229 
1230       if (sk->err) return (-sk->err);
1231 
1232       sock->state = SS_CONNECTED;
1233       return (-EISCONN);
1234     }
1235 
1236   /* we may need to bind the socket. */
1237   if (sk->num == 0)
1238     {
1239       sk->num = get_new_socknum (sk->prot, 0);
1240       if (sk->num == 0) return (-EAGAIN);
1241       put_sock (sk->num, sk);
1242       sk->dummy_th.source = net16(sk->num);
1243     }
1244 
1245   if (sk->prot->connect == NULL)
1246     return (-EOPNOTSUPP);
1247 
1248   if (sk->intr == 0)
1249     {
1250       err = sk->prot->connect (sk, (struct sockaddr_in *)uaddr, addr_len);
1251       if (err < 0) return (err);
1252     }
1253 
1254   sock->state = SS_CONNECTING;
1255   if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1256     return (-EINPROGRESS);
1257 
1258   cli(); /* avoid the race condition */
1259 
1260   while (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
1261     {
1262       interruptible_sleep_on (sk->sleep);
1263       if (current->signal & ~current->blocked)
1264         {
1265            sti();
1266            sk->intr = 1;
1267            sock->state = SS_UNCONNECTED;
1268            return (-ERESTARTSYS);
1269         }
1270     }
1271   sti();
1272   sock->state = SS_CONNECTED;
1273   sk->intr = 0;
1274   if (sk->state != TCP_ESTABLISHED && sk->err)
1275     {
1276       sock->state = SS_UNCONNECTED;
1277       return (-sk->err);
1278     }
1279   return (0);
1280 }
1281 
1282 static int
1283 ip_proto_socketpair (struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1284 {
1285   return (-EOPNOTSUPP);
1286 }
1287 
1288 static int
1289 ip_proto_accept (struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1290 {
1291   volatile struct sock *sk1, *sk2;
1292   sk1= sock->data;
1293    if (sk1 == NULL)
1294      {
1295         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1296         return (0);
1297      }
1298 
1299   /* we've been passed an extra socket. We need to free it up because
1300    the tcp module creates it's own when it accepts one. */
1301   if (newsock->data)
1302     kfree_s (newsock->data, sizeof (struct sock));
1303 
1304   newsock->data = NULL;
1305 
1306   
1307 if (sk1->prot->accept == NULL) return (-EOPNOTSUPP);
1308 
1309   /* restore the state if we have been interrupted, and
1310      then returned. */
1311   if (sk1->pair != NULL )
1312     {
1313       sk2 = sk1->pair;
1314       sk1->pair = NULL;
1315     }
1316   else
1317     {
1318       sk2 = sk1->prot->accept (sk1,flags);
1319       if (sk2 == NULL)
1320         {
1321           if (sk1->err <= 0)
1322             printk ("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
1323           return (-sk1->err);
1324         }
1325     }
1326   newsock->data = (void *)sk2;
1327   sk2->sleep = (void *)newsock->wait;
1328   newsock->conn = NULL;
1329   if (flags & O_NONBLOCK)
1330     return (0);
1331 
1332   cli(); /* avoid the race. */
1333   while (sk2->state == TCP_SYN_RECV)
1334     {
1335       interruptible_sleep_on (sk2->sleep);
1336       if (current->signal & ~current->blocked)
1337         {
1338            sti();
1339            sk1->pair = sk2;
1340            sk2->sleep = NULL;
1341            newsock->data = NULL;
1342            return (-ERESTARTSYS);
1343         }
1344     }
1345   sti();
1346 
1347   if (sk2->state != TCP_ESTABLISHED && sk2->err > 0)
1348     {
1349       int err;
1350       err = -sk2->err;
1351       destroy_sock (sk2);
1352       newsock->data = NULL;
1353       return (err);
1354     }
1355   newsock->state = SS_CONNECTED;
1356   return (0);
1357 }
1358 
1359 static int
1360 ip_proto_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1361                  int *uaddr_len, int peer)
1362 {
1363   struct sockaddr_in sin;
1364   volatile struct sock *sk;
1365   int len;
1366   len = get_fs_long(uaddr_len);
1367   /* check this error. */
1368   if (len < sizeof (sin)) return (-EINVAL);
1369   sin.sin_family=AF_INET;
1370   sk = sock->data;
1371   if (sk == NULL)
1372     {
1373        printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1374        return (0);
1375     }
1376   if (peer)
1377     {
1378       if (!tcp_connected(sk->state))
1379         return (-ENOTCONN);
1380       sin.sin_port = sk->dummy_th.dest;
1381       sin.sin_addr.s_addr = sk->daddr;
1382       }
1383   else
1384     {
1385       sin.sin_port = sk->dummy_th.source;
1386       sin.sin_addr.s_addr = sk->saddr;
1387     }
1388   len = sizeof (sin);
1389   verify_area (uaddr, len);
1390   memcpy_tofs(uaddr, &sin, sizeof (sin));
1391   verify_area(uaddr_len, sizeof (len));
1392   put_fs_long (len, uaddr_len);
1393   return (0);
1394 }
1395 
1396 static int
1397 ip_proto_read (struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1398 {
1399   volatile struct sock *sk;
1400   sk = sock->data;
1401    if (sk == NULL)
1402      {
1403         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1404         return (0);
1405      }
1406 
1407   /* we may need to bind the socket. */
1408   if (sk->num == 0)
1409     {
1410       sk->num = get_new_socknum (sk->prot, 0);
1411       if (sk->num == 0) return (-EAGAIN);
1412       put_sock (sk->num, sk);
1413       sk->dummy_th.source = net16(sk->num);
1414     }
1415 
1416   return (sk->prot->read (sk, ubuf, size, noblock,0));
1417 }
1418 
1419 static int
1420 ip_proto_recv (struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1421                unsigned flags)
1422 {
1423   volatile struct sock *sk;
1424   sk = sock->data;
1425    if (sk == NULL)
1426      {
1427         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1428         return (0);
1429      }
1430 
1431   /* we may need to bind the socket. */
1432   if (sk->num == 0)
1433     {
1434       sk->num = get_new_socknum (sk->prot, 0);
1435       if (sk->num == 0) return (-EAGAIN);
1436       put_sock (sk->num, sk);
1437       sk->dummy_th.source = net16(sk->num);
1438     }
1439 
1440   return (sk->prot->read (sk, ubuf, size, noblock, flags));
1441 }
1442 
1443 static int
1444 ip_proto_write (struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1445 {
1446   volatile struct sock *sk;
1447   sk = sock->data;
1448    if (sk == NULL)
1449      {
1450         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1451         return (0);
1452      }
1453   if (sk->shutdown & SEND_SHUTDOWN)
1454     {
1455       send_sig (SIGPIPE, current, 1);
1456       return (-EPIPE);
1457     }
1458 
1459   /* we may need to bind the socket. */
1460   if (sk->num == 0)
1461     {
1462       sk->num = get_new_socknum (sk->prot, 0);
1463       if (sk->num == 0) return (-EAGAIN);
1464       put_sock (sk->num, sk);
1465       sk->dummy_th.source = net16(sk->num);
1466     }
1467 
1468   return (sk->prot->write (sk, ubuf, size, noblock, 0));
1469 }
1470 
1471 
1472 static int
1473 ip_proto_send (struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1474                unsigned flags)
1475 {
1476   volatile struct sock *sk;
1477   sk = sock->data;
1478    if (sk == NULL)
1479      {
1480         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1481         return (0);
1482      }
1483   if (sk->shutdown & SEND_SHUTDOWN)
1484     {
1485       send_sig (SIGPIPE, current, 1);
1486       return (-EPIPE);
1487     }
1488 
1489   /* we may need to bind the socket. */
1490   if (sk->num == 0)
1491     {
1492       sk->num = get_new_socknum (sk->prot, 0);
1493       if (sk->num == 0) return (-EAGAIN);
1494       put_sock (sk->num, sk);
1495       sk->dummy_th.source = net16(sk->num);
1496     }
1497 
1498   return (sk->prot->write (sk, ubuf, size, noblock, flags));
1499 }
1500 
1501 
1502 static int
1503 ip_proto_sendto (struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1504                  unsigned flags, struct sockaddr *sin, int addr_len )
1505 {
1506   volatile struct sock *sk;
1507   sk = sock->data;
1508    if (sk == NULL)
1509      {
1510         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1511         return (0);
1512      }
1513   if (sk->shutdown & SEND_SHUTDOWN)
1514     {
1515       send_sig (SIGPIPE, current, 1);
1516       return (-EPIPE);
1517     }
1518 
1519   if (sk->prot->sendto == NULL) return (-EOPNOTSUPP);
1520 
1521   /* we may need to bind the socket. */
1522   if (sk->num == 0)
1523     {
1524       sk->num = get_new_socknum (sk->prot, 0);
1525       if (sk->num == 0) return (-EAGAIN);
1526       put_sock (sk->num, sk);
1527       sk->dummy_th.source = net16(sk->num);
1528     }
1529 
1530   return (sk->prot->sendto (sk, ubuf, size, noblock, flags, 
1531                             (struct sockaddr_in *)sin, addr_len));
1532 }
1533 
1534 static int
1535 ip_proto_recvfrom (struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1536                    unsigned flags, struct sockaddr *sin, int *addr_len )
1537 {
1538   volatile struct sock *sk;
1539   sk = sock->data;
1540    if (sk == NULL)
1541      {
1542         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1543         return (0);
1544      }
1545 
1546   if (sk->prot->recvfrom == NULL) return (-EOPNOTSUPP);
1547 
1548   /* we may need to bind the socket. */
1549   if (sk->num == 0)
1550     {
1551       sk->num = get_new_socknum (sk->prot, 0);
1552       if (sk->num == 0) return (-EAGAIN);
1553       put_sock (sk->num, sk);
1554       sk->dummy_th.source = net16(sk->num);
1555     }
1556 
1557   return (sk->prot->recvfrom (sk, ubuf, size, noblock, flags,
1558                               (struct sockaddr_in*)sin, addr_len));
1559 }
1560 
1561 static int
1562 ip_proto_shutdown (struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1563 {
1564         volatile struct sock *sk;
1565         /* this should really check to make sure the socket is
1566            a tcp socket. */
1567         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1568                        1->2 bit 2 snds.
1569                        2->3 */
1570         if (how & ~SHUTDOWN_MASK) return (-EINVAL);
1571         sk = sock->data;
1572         if (sk == NULL)
1573           {
1574              printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1575              return (0);
1576           }
1577         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1578           sock->state = SS_CONNECTED;
1579 
1580         if (!tcp_connected(sk->state)) return (-ENOTCONN);
1581         sk->shutdown |= how;
1582         if (sk->prot->shutdown)
1583           sk->prot->shutdown (sk, how);
1584         return (0);
1585 }
1586 
1587 static int
1588 ip_proto_select (struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1589 {
1590   volatile struct sock *sk;
1591   sk = sock->data;
1592    if (sk == NULL)
1593      {
1594         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1595         return (0);
1596      }
1597 
1598   if (sk->prot->select == NULL)
1599     {
1600        PRINTK ("select on non-selectable socket. \n");
1601        return (0);
1602     }
1603   return (sk->prot->select(sk, sel_type, wait));
1604 }
1605 
1606 /* these should be distributed to the different protocol routines. */
1607 static int
1608 ip_proto_ioctl (struct socket *sock, unsigned int cmd, 
     /* [previous][next][first][last][top][bottom][index][help] */
1609                 unsigned long arg)
1610 {
1611    volatile struct sock *sk;
1612    sk = sock->data;
1613    if (sk == NULL)
1614      {
1615         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1616         return (0);
1617      }
1618 
1619   PRINTK ("in ip_proto_ioctl\n");
1620   switch (cmd)
1621     {
1622 
1623       case IP_SET_DEV:
1624        if (!suser())
1625          return (-EPERM);
1626        return (ip_set_dev((struct ip_config *)arg));
1627 
1628      case FIOSETOWN:
1629      case SIOCSPGRP:
1630         {
1631          long user;
1632          user = get_fs_long ((void *) arg);
1633          sk->proc = user;
1634          return (0);
1635        }
1636 
1637      case FIOGETOWN:
1638      case SIOCGPGRP:
1639        {
1640          verify_area ((void *)arg, sizeof (long));
1641          put_fs_long (sk->proc, (void *)arg);
1642          return (0);
1643        }
1644 
1645     default:
1646        if (!sk->prot->ioctl)
1647          return (-EINVAL);
1648        return (sk->prot->ioctl (sk, cmd, arg));
1649     }
1650 }
1651 
1652 void *
1653 sock_wmalloc(volatile struct sock *sk, unsigned long size, int force,
     /* [previous][next][first][last][top][bottom][index][help] */
1654              int priority)
1655 {
1656   if (sk)
1657     {
1658        if (sk->wmem_alloc + size < SK_WMEM_MAX || force)
1659          {
1660            cli();
1661            sk->wmem_alloc+= size;
1662            sti();
1663            return (kmalloc (size, priority));
1664          }
1665        MPRINTK ("sock_wmalloc(%X,%d,%d,%d) returning NULL\n",
1666                 sk, size, force, priority);
1667        return (NULL);
1668     }
1669   return (kmalloc(size, priority));
1670 }
1671 
1672 void *
1673 sock_rmalloc(volatile struct sock *sk, unsigned long size, int force,
     /* [previous][next][first][last][top][bottom][index][help] */
1674              int priority)
1675 {
1676    if (sk )
1677      {
1678         if (sk->rmem_alloc + size < SK_RMEM_MAX || force)
1679           {
1680             cli();
1681             sk->rmem_alloc+= size;
1682             sti();
1683             return (kmalloc (size, priority));
1684           }
1685         MPRINTK ("sock_rmalloc(%X,%d,%d,%d) returning NULL\n",
1686                  sk,size,force, priority);
1687         return (NULL);
1688       }
1689    return (kmalloc (size, priority));
1690 }
1691 
1692 
1693 unsigned long
1694 sock_rspace (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1695 {
1696    int amt;
1697    if (sk != NULL)
1698      {
1699         if (sk->rmem_alloc >= SK_RMEM_MAX-2*MIN_WINDOW) return (0);
1700         amt = min ((SK_RMEM_MAX-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
1701         if (amt < 0) return (0);
1702         return (amt);
1703      }
1704    return (0);
1705 }
1706 
1707 unsigned long
1708 sock_wspace (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1709 {
1710   if (sk != NULL)
1711     {
1712        if (sk->shutdown & SEND_SHUTDOWN) return (0);
1713        if (sk->wmem_alloc >= SK_WMEM_MAX) return (0);
1714        return (SK_WMEM_MAX-sk->wmem_alloc );
1715     }
1716   return (0);
1717 }
1718 
1719 
1720 void
1721 sock_wfree (volatile struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1722 {
1723    MPRINTK ("sock_wfree (sk=%X, mem=%X, size=%d)\n",sk, mem, size);
1724    kfree_s (mem, size);
1725    if (sk)
1726      {
1727         sk->wmem_alloc -= size;
1728         /* in case it might be waiting for more memory. */
1729         if (!sk->dead) wake_up(sk->sleep);
1730         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0)
1731           {
1732              MPRINTK ("recovered lost memory, destroying sock = %X\n",sk);
1733              delete_timer ((struct timer *)&sk->time_wait);
1734              kfree_s ((void *)sk, sizeof (*sk));
1735           }
1736         return;
1737      }
1738 }
1739 
1740 void
1741 sock_rfree (volatile struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1742 {
1743    MPRINTK ("sock_rfree (sk=%X, mem=%X, size=%d)\n",sk, mem, size);
1744    kfree_s (mem, size);
1745    if (sk)
1746      {
1747         sk->rmem_alloc -= size;
1748         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0)
1749           {
1750              delete_timer ((struct timer *)&sk->time_wait);
1751              kfree_s ((void *)sk, sizeof (*sk));
1752           }
1753      }
1754 }
1755 
1756 
1757 /* This routine must find a socket given a tcp header.  Everyhting
1758    is assumed to be in net order. */
1759 
1760 volatile struct sock *get_sock (struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1761                                 unsigned long raddr,
1762                                 unsigned short rnum, unsigned long laddr)
1763 {
1764   volatile struct sock *s;
1765   PRINTK ("get_sock (prot=%X, num=%d, raddr=%X, rnum=%d, laddr=%X)\n",
1766           prot, num, raddr, rnum, laddr);
1767 
1768   /* SOCK_ARRAY_SIZE must be a power of two.  This will work better
1769      than a prime unless 3 or more sockets end up using the same
1770      array entry.  This should not be a problem because most
1771      well known sockets don't overlap that much, and for
1772      the other ones, we can just be careful about picking our
1773      socket number when we choose an arbitrary one. */
1774 
1775   for (s=prot->sock_array[num&(SOCK_ARRAY_SIZE-1)]; s != NULL; s=s->next)
1776     {
1777       if (s->num == num)
1778         {
1779           /* we need to see if this is the socket that we want. */
1780           if (!ip_addr_match (s->daddr, raddr))
1781             continue;
1782           if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0)
1783             continue;
1784           if (!ip_addr_match (s->saddr, laddr))
1785             continue;
1786           return (s);
1787         }
1788     }
1789   return (NULL);
1790 }
1791 
1792 void release_sock (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1793 {
1794   if (!sk)
1795     {
1796       printk ("sock.c: release_sock sk == NULL\n");
1797       return;
1798     }
1799 
1800   if (!sk->prot)
1801     {
1802       printk ("sock.c: release_sock sk->prot == NULL\n");
1803       return;
1804     }
1805 
1806   if (sk->blog) return;
1807   /* see if we have any packets built up. */
1808 
1809   cli();
1810   sk->inuse = 1;
1811   while (sk->back_log != NULL)
1812     {
1813       struct sk_buff *skb;
1814       sk->blog = 1;
1815       skb = sk->back_log;
1816       PRINTK ("release_sock: skb = %X:\n",skb);
1817       print_skb(skb);
1818       if (skb->next != skb)
1819         {
1820           sk->back_log = skb->next;
1821           skb->prev->next = skb->next;
1822           skb->next->prev = skb->prev;
1823         }
1824       else
1825         {
1826           sk->back_log = NULL;
1827         }
1828       sti();
1829       PRINTK ("sk->back_log = %X\n",sk->back_log);
1830       if (sk->prot->rcv)
1831         sk->prot->rcv(skb, skb->dev, sk->opt,
1832                       skb->saddr, skb->len, skb->daddr, 1,
1833                       /* only used for/by raw sockets. */
1834                       (struct ip_protocol *)sk->pair); 
1835       cli();
1836     }
1837   sk->blog = 0;
1838   sk->inuse = 0;
1839   sti();
1840   if (sk->dead && sk->state == TCP_CLOSE)
1841     {
1842         /* should be about 2 rtt's */
1843        sk->time_wait.len = min (sk->rtt * 2, TCP_DONE_TIME);
1844        sk->timeout = TIME_DONE;
1845        reset_timer ((struct timer *)&sk->time_wait);
1846     }
1847 }

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