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

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