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->dummy_th.source == 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 (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 (optlen, sizeof (int));
 841     put_fs_long (sizeof(int),(unsigned long *) optlen);
 842 
 843     verify_area(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->rtt = 0;
1023   sk->destroy = 0;
1024   sk->reuse = 0;
1025   sk->priority = 1;
1026   sk->shutdown = 0;
1027   sk->urg = 0;
1028   sk->keepopen = 0;
1029   sk->done = 0;
1030   sk->ack_backlog = 0;
1031   sk->window = 0;
1032   sk->bytes_rcv = 0;
1033   sk->state = TCP_CLOSE;
1034   sk->dead = 0;
1035   sk->ack_timed = 0;
1036   sk->send_tmp = NULL;
1037   sk->mss = 0; /* we will try not to send any packets smaller
1038                    than this. */
1039 
1040   /* this is how many unacked bytes we will accept for
1041      this socket.  */
1042 
1043   sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
1044 
1045   /* how many packets we should send before forcing an ack. 
1046      if this is set to zero it is the same as sk->delay_acks = 0 */
1047 
1048   sk->max_ack_backlog = 0;
1049   sk->inuse = 0;
1050   sk->delay_acks = 0;
1051   sk->wback = NULL;
1052   sk->wfront = NULL;
1053   sk->rqueue = NULL;
1054   sk->mtu = 576;
1055   sk->prot = prot;
1056   sk->sleep = sock->wait;
1057   sk->daddr = 0;
1058   sk->saddr = MY_IP_ADDR;
1059   sk->err = 0;
1060   sk->next = NULL;
1061   sk->pair = NULL;
1062   sk->send_tail = NULL;
1063   sk->send_head = NULL;
1064   sk->time_wait.len = TCP_CONNECT_TIME;
1065   sk->time_wait.when = 0;
1066   sk->time_wait.sk = sk;
1067   sk->time_wait.next = NULL;
1068   sk->timeout = 0;
1069   sk->back_log = NULL;
1070   sk->blog = 0;
1071   sock->data =(void *) sk;
1072   sk->dummy_th.doff = sizeof (sk->dummy_th)/4;
1073   sk->dummy_th.res1=0;
1074   sk->dummy_th.res2=0;
1075   sk->dummy_th.urg_ptr = 0;
1076   sk->dummy_th.fin = 0;
1077   sk->dummy_th.syn = 0;
1078   sk->dummy_th.rst = 0;
1079   sk->dummy_th.psh = 0;
1080   sk->dummy_th.ack = 0;
1081   sk->dummy_th.urg = 0;
1082   sk->dummy_th.dest = 0;
1083 
1084   if (sk->num)
1085     {
1086       /* it assumes that any protocol which allows
1087          the user to assign a number at socket
1088          creation time automatically
1089          shares. */
1090       put_sock (sk->num, sk);
1091       sk->dummy_th.source = net16(sk->num);
1092     }
1093 
1094   if (sk->prot->init)
1095     {
1096        err = sk->prot->init(sk);
1097        if (err != 0)
1098          {
1099             destroy_sock (sk);
1100             return (err);
1101          }
1102     }
1103   return (0);
1104 }
1105 
1106 static int
1107 ip_proto_dup (struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
1108 {
1109   return (ip_proto_create (newsock,
1110                            ((volatile struct sock *)(oldsock->data))->protocol));
1111 }
1112 
1113 /* the peer socket should always be NULL. */
1114 static int
1115 ip_proto_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
1116 {
1117   volatile struct sock *sk;
1118   sk = sock->data;
1119   if (sk == NULL) return (0);
1120   PRINTK (("ip_proto_release (sock = %X, peer = %X)\n", sock, peer));
1121   wake_up (sk->sleep);
1122   /* start closing the connection.  This may take a while. */
1123   /* if linger is set, we don't return until the close is
1124      complete.  Other wise we return immediately.  The
1125      actually closing is done the same either way. */
1126   if (sk->linger == 0)
1127     {
1128        sk->prot->close(sk,0);
1129        sk->dead = 1;
1130     }
1131   else
1132     {
1133       PRINTK (("sk->linger set.\n"));
1134        sk->prot->close(sk, 0);
1135        cli();
1136        while (sk->state != TCP_CLOSE)
1137          {
1138             interruptible_sleep_on (sk->sleep);
1139             if (current->signal & ~current->blocked)
1140               {
1141                  sti();
1142                  return (-ERESTARTSYS);
1143               }
1144          }
1145        sti();
1146        sk->dead = 1;
1147     }
1148 
1149   sk->inuse = 1;
1150   /* this will destroy it. */
1151   release_sock (sk);
1152   sock->data = NULL;
1153   PRINTK (("ip_proto_release returning\n"));
1154   return (0);
1155 }
1156 
1157 
1158 /* this needs to be changed to dissallow
1159    the rebinding of sockets.   What error
1160    should it return? */
1161 
1162 static int
1163 ip_proto_bind (struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1164                int addr_len)
1165 {
1166   struct sockaddr_in addr;
1167   volatile struct sock *sk, *sk2;
1168   unsigned short snum;
1169 
1170   sk = sock->data;
1171    if (sk == NULL)
1172      {
1173         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1174         return (0);
1175      }
1176   /* check this error. */
1177   if (sk->state != TCP_CLOSE) return (-EIO);
1178   if (sk->num != 0) return (-EINVAL);
1179 
1180 /*  verify_area (uaddr, addr_len);*/
1181   memcpy_fromfs (&addr, uaddr, min (sizeof (addr), addr_len));
1182 
1183 #if 0
1184   if (addr.sin_family && addr.sin_family != AF_INET)
1185     {
1186       /* this is really a bug in BSD which we need to emulate because
1187          ftp expects it. */
1188       return (-EINVAL);
1189     }
1190 #endif
1191 
1192   snum = net16(addr.sin_port);
1193   PRINTK (("bind sk =%X to port = %d\n", sk, snum));
1194   sk = sock->data;
1195 
1196   /* we can't just leave the socket bound wherever it is, it might be bound
1197      to a priveledged port. However, since there seems to be a bug here,
1198      we will leave it if the port is not priveledged(sp?) */
1199 
1200   if (snum == 0)
1201     {
1202        if ( sk->num > PROT_SOCK) return (0);
1203        snum = get_new_socknum (sk->prot, 0);
1204     }
1205 
1206   if (snum <= PROT_SOCK && !suser())
1207     return (-EACCES);
1208 
1209   if (my_ip_addr(addr.sin_addr.s_addr) || addr.sin_addr.s_addr == 0)
1210     sk->saddr = addr.sin_addr.s_addr;
1211 
1212   PRINTK (("sock_array[%d] = %X:\n", snum & (SOCK_ARRAY_SIZE -1),
1213           sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)]));
1214 
1215   /* make sure we are allowed to bind here. */
1216   for (sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
1217        sk2 != NULL;
1218        sk2 = sk2->next)
1219     {
1220        if (sk2->num != snum) continue;
1221        if (sk2->saddr != sk->saddr) continue;
1222        if (!sk->reuse) return (-EADDRINUSE);
1223        if (!sk2->reuse) return (-EADDRINUSE);
1224     }
1225   remove_sock (sk);
1226   put_sock(snum, sk);
1227   sk->dummy_th.source = net16(sk->num);
1228   sk->daddr = 0;
1229   sk->dummy_th.dest = 0;
1230   return (0);
1231 }
1232 
1233 static int
1234 ip_proto_connect (struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1235                   int addr_len, int flags)
1236 {
1237   volatile struct sock *sk;
1238   int err;
1239   sock->conn = NULL;
1240   sk = sock->data;
1241    if (sk == NULL)
1242      {
1243         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1244         return (0);
1245      }
1246 
1247   if (sk->state == TCP_ESTABLISHED)
1248     {
1249       sock->state = SS_CONNECTED;
1250       return (-EISCONN);
1251     }
1252 
1253   if (sock->state == SS_CONNECTING)
1254     {
1255       if (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
1256         return (-EINPROGRESS);
1257 
1258       if (sk->err) return (-sk->err);
1259 
1260       sock->state = SS_CONNECTED;
1261       return (-EISCONN);
1262     }
1263 
1264   /* we may need to bind the socket. */
1265   if (sk->num == 0)
1266     {
1267       sk->num = get_new_socknum (sk->prot, 0);
1268       if (sk->num == 0) return (-EAGAIN);
1269       put_sock (sk->num, sk);
1270       sk->dummy_th.source = net16(sk->num);
1271     }
1272 
1273   if (sk->prot->connect == NULL)
1274     return (-EOPNOTSUPP);
1275 
1276   err = sk->prot->connect (sk, (struct sockaddr_in *)uaddr, addr_len);
1277   if (err < 0) return (err);
1278 
1279   sock->state = SS_CONNECTING;
1280   if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1281     return (-EINPROGRESS);
1282 
1283   cli(); /* avoid the race condition */
1284 
1285   while (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV)
1286     {
1287       interruptible_sleep_on (sk->sleep);
1288       if (current->signal & ~current->blocked)
1289         {
1290            sti();
1291            return (-ERESTARTSYS);
1292         }
1293     }
1294   sti();
1295   sock->state = SS_CONNECTED;
1296 
1297   if (sk->state != TCP_ESTABLISHED && sk->err)
1298     {
1299       sock->state = SS_UNCONNECTED;
1300       return (-sk->err);
1301     }
1302   return (0);
1303 }
1304 
1305 static int
1306 ip_proto_socketpair (struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1307 {
1308   return (-EOPNOTSUPP);
1309 }
1310 
1311 static int
1312 ip_proto_accept (struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1313 {
1314   volatile struct sock *sk1, *sk2;
1315   sk1= sock->data;
1316    if (sk1 == NULL)
1317      {
1318         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1319         return (0);
1320      }
1321 
1322   /* we've been passed an extra socket. We need to free it up because
1323    the tcp module creates it's own when it accepts one. */
1324 
1325   if (newsock->data)
1326     kfree_s (newsock->data, sizeof (struct sock));
1327 
1328   newsock->data = NULL;
1329 
1330   
1331 if (sk1->prot->accept == NULL) return (-EOPNOTSUPP);
1332 
1333   /* restore the state if we have been interrupted, and
1334      then returned. */
1335   if (sk1->pair != NULL )
1336     {
1337       sk2 = sk1->pair;
1338       sk1->pair = NULL;
1339     }
1340   else
1341     {
1342       sk2 = sk1->prot->accept (sk1,flags);
1343       if (sk2 == NULL)
1344         {
1345           if (sk1->err <= 0)
1346             printk ("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
1347           return (-sk1->err);
1348         }
1349     }
1350   newsock->data = (void *)sk2;
1351   sk2->sleep = (void *)newsock->wait;
1352   newsock->conn = NULL;
1353   if (flags & O_NONBLOCK)
1354     return (0);
1355 
1356   cli(); /* avoid the race. */
1357   while (sk2->state == TCP_SYN_RECV)
1358     {
1359       interruptible_sleep_on (sk2->sleep);
1360       if (current->signal & ~current->blocked)
1361         {
1362            sti();
1363            sk1->pair = sk2;
1364            sk2->sleep = NULL;
1365            newsock->data = NULL;
1366            return (-ERESTARTSYS);
1367         }
1368     }
1369   sti();
1370 
1371   if (sk2->state != TCP_ESTABLISHED && sk2->err > 0)
1372     {
1373       int err;
1374       err = -sk2->err;
1375       destroy_sock (sk2);
1376       newsock->data = NULL;
1377       return (err);
1378     }
1379   newsock->state = SS_CONNECTED;
1380   return (0);
1381 }
1382 
1383 static int
1384 ip_proto_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1385                  int *uaddr_len, int peer)
1386 {
1387   struct sockaddr_in sin;
1388   volatile struct sock *sk;
1389   int len;
1390   len = get_fs_long(uaddr_len);
1391   /* check this error. */
1392   if (len < sizeof (sin)) return (-EINVAL);
1393   sin.sin_family=AF_INET;
1394   sk = sock->data;
1395   if (sk == NULL)
1396     {
1397        printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1398        return (0);
1399     }
1400   if (peer)
1401     {
1402       if (!tcp_connected(sk->state))
1403         return (-ENOTCONN);
1404       sin.sin_port = sk->dummy_th.dest;
1405       sin.sin_addr.s_addr = sk->daddr;
1406       }
1407   else
1408     {
1409       sin.sin_port = sk->dummy_th.source;
1410       if (sk->saddr == 0)
1411         sin.sin_addr.s_addr = MY_IP_ADDR;
1412       else
1413         sin.sin_addr.s_addr = sk->saddr;
1414     }
1415   len = sizeof (sin);
1416   verify_area (uaddr, len);
1417   memcpy_tofs(uaddr, &sin, sizeof (sin));
1418   verify_area(uaddr_len, sizeof (len));
1419   put_fs_long (len, uaddr_len);
1420   return (0);
1421 }
1422 
1423 static int
1424 ip_proto_read (struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1425 {
1426   volatile struct sock *sk;
1427   sk = sock->data;
1428    if (sk == NULL)
1429      {
1430         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1431         return (0);
1432      }
1433 
1434   /* we may need to bind the socket. */
1435   if (sk->num == 0)
1436     {
1437       sk->num = get_new_socknum (sk->prot, 0);
1438       if (sk->num == 0) return (-EAGAIN);
1439       put_sock (sk->num, sk);
1440       sk->dummy_th.source = net16(sk->num);
1441     }
1442 
1443   return (sk->prot->read (sk, ubuf, size, noblock,0));
1444 }
1445 
1446 static int
1447 ip_proto_recv (struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1448                unsigned flags)
1449 {
1450   volatile struct sock *sk;
1451   sk = sock->data;
1452    if (sk == NULL)
1453      {
1454         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1455         return (0);
1456      }
1457 
1458   /* we may need to bind the socket. */
1459   if (sk->num == 0)
1460     {
1461       sk->num = get_new_socknum (sk->prot, 0);
1462       if (sk->num == 0) return (-EAGAIN);
1463       put_sock (sk->num, sk);
1464       sk->dummy_th.source = net16(sk->num);
1465     }
1466 
1467   return (sk->prot->read (sk, ubuf, size, noblock, flags));
1468 }
1469 
1470 static int
1471 ip_proto_write (struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1472 {
1473   volatile struct sock *sk;
1474   sk = sock->data;
1475    if (sk == NULL)
1476      {
1477         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1478         return (0);
1479      }
1480   if (sk->shutdown & SEND_SHUTDOWN)
1481     {
1482       send_sig (SIGPIPE, current, 1);
1483       return (-EPIPE);
1484     }
1485 
1486   /* we may need to bind the socket. */
1487   if (sk->num == 0)
1488     {
1489       sk->num = get_new_socknum (sk->prot, 0);
1490       if (sk->num == 0) return (-EAGAIN);
1491       put_sock (sk->num, sk);
1492       sk->dummy_th.source = net16(sk->num);
1493     }
1494 
1495   return (sk->prot->write (sk, ubuf, size, noblock, 0));
1496 }
1497 
1498 
1499 static int
1500 ip_proto_send (struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1501                unsigned flags)
1502 {
1503   volatile struct sock *sk;
1504   sk = sock->data;
1505    if (sk == NULL)
1506      {
1507         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1508         return (0);
1509      }
1510   if (sk->shutdown & SEND_SHUTDOWN)
1511     {
1512       send_sig (SIGPIPE, current, 1);
1513       return (-EPIPE);
1514     }
1515 
1516   /* we may need to bind the socket. */
1517   if (sk->num == 0)
1518     {
1519       sk->num = get_new_socknum (sk->prot, 0);
1520       if (sk->num == 0) return (-EAGAIN);
1521       put_sock (sk->num, sk);
1522       sk->dummy_th.source = net16(sk->num);
1523     }
1524 
1525   return (sk->prot->write (sk, ubuf, size, noblock, flags));
1526 }
1527 
1528 
1529 static int
1530 ip_proto_sendto (struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1531                  unsigned flags, struct sockaddr *sin, int addr_len )
1532 {
1533   volatile struct sock *sk;
1534   sk = sock->data;
1535    if (sk == NULL)
1536      {
1537         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1538         return (0);
1539      }
1540   if (sk->shutdown & SEND_SHUTDOWN)
1541     {
1542       send_sig (SIGPIPE, current, 1);
1543       return (-EPIPE);
1544     }
1545 
1546   if (sk->prot->sendto == 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->sendto (sk, ubuf, size, noblock, flags, 
1558                             (struct sockaddr_in *)sin, addr_len));
1559 }
1560 
1561 static int
1562 ip_proto_recvfrom (struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1563                    unsigned flags, struct sockaddr *sin, int *addr_len )
1564 {
1565   volatile struct sock *sk;
1566   sk = sock->data;
1567    if (sk == NULL)
1568      {
1569         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1570         return (0);
1571      }
1572 
1573   if (sk->prot->recvfrom == NULL) return (-EOPNOTSUPP);
1574 
1575   /* we may need to bind the socket. */
1576   if (sk->num == 0)
1577     {
1578       sk->num = get_new_socknum (sk->prot, 0);
1579       if (sk->num == 0) return (-EAGAIN);
1580       put_sock (sk->num, sk);
1581       sk->dummy_th.source = net16(sk->num);
1582     }
1583 
1584   return (sk->prot->recvfrom (sk, ubuf, size, noblock, flags,
1585                               (struct sockaddr_in*)sin, addr_len));
1586 }
1587 
1588 static int
1589 ip_proto_shutdown (struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1590 {
1591         volatile struct sock *sk;
1592         /* this should really check to make sure the socket is
1593            a tcp socket. */
1594         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1595                        1->2 bit 2 snds.
1596                        2->3 */
1597         if (how & ~SHUTDOWN_MASK) return (-EINVAL);
1598         sk = sock->data;
1599         if (sk == NULL)
1600           {
1601              printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1602              return (0);
1603           }
1604         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1605           sock->state = SS_CONNECTED;
1606 
1607         if (!tcp_connected(sk->state)) return (-ENOTCONN);
1608         sk->shutdown |= how;
1609         if (sk->prot->shutdown)
1610           sk->prot->shutdown (sk, how);
1611         return (0);
1612 }
1613 
1614 static int
1615 ip_proto_select (struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1616 {
1617   volatile struct sock *sk;
1618   sk = sock->data;
1619    if (sk == NULL)
1620      {
1621         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1622         return (0);
1623      }
1624 
1625   if (sk->prot->select == NULL)
1626     {
1627        PRINTK (("select on non-selectable socket. \n"));
1628        return (0);
1629     }
1630   return (sk->prot->select(sk, sel_type, wait));
1631 }
1632 
1633 /* these should be distributed to the different protocol routines. */
1634 static int
1635 ip_proto_ioctl (struct socket *sock, unsigned int cmd, 
     /* [previous][next][first][last][top][bottom][index][help] */
1636                 unsigned long arg)
1637 {
1638    volatile struct sock *sk;
1639    sk = sock->data;
1640    if (sk == NULL)
1641      {
1642         printk ("Warning: sock->data = NULL: %d\n" ,__LINE__);
1643         return (0);
1644      }
1645 
1646   PRINTK (("in ip_proto_ioctl\n"));
1647   switch (cmd)
1648     {
1649 
1650       case IP_SET_DEV:
1651        if (!suser())
1652          return (-EPERM);
1653        return (ip_set_dev((struct ip_config *)arg));
1654 
1655      case SIOCSARP:
1656        if (!suser())
1657          return (-EPERM);
1658        return (arp_ioctl_set((struct arpreq *)arg));
1659 
1660      case SIOCGARP:
1661        return (arp_ioctl_get((struct arpreq *)arg));
1662 
1663      case SIOCDARP:
1664        if (!suser())
1665          return (-EPERM);
1666        return (arp_ioctl_del((struct arpreq *)arg));
1667 
1668      case FIOSETOWN:
1669      case SIOCSPGRP:
1670         {
1671          long user;
1672          user = get_fs_long ((void *) arg);
1673          sk->proc = user;
1674          return (0);
1675        }
1676 
1677      case FIOGETOWN:
1678      case SIOCGPGRP:
1679        {
1680          verify_area ((void *)arg, sizeof (long));
1681          put_fs_long (sk->proc, (void *)arg);
1682          return (0);
1683        }
1684 
1685     default:
1686        if (!sk->prot->ioctl)
1687          return (-EINVAL);
1688        return (sk->prot->ioctl (sk, cmd, arg));
1689     }
1690 }
1691 
1692 void *
1693 sock_wmalloc(volatile struct sock *sk, unsigned long size, int force,
     /* [previous][next][first][last][top][bottom][index][help] */
1694              int priority)
1695 {
1696   if (sk)
1697     {
1698        if (sk->wmem_alloc + size < SK_WMEM_MAX || force)
1699          {
1700            cli();
1701            sk->wmem_alloc+= size;
1702            sti();
1703            return (kmalloc (size, priority));
1704          }
1705        MPRINTK (("sock_wmalloc(%X,%d,%d,%d) returning NULL\n",
1706                 sk, size, force, priority));
1707        return (NULL);
1708     }
1709   return (kmalloc(size, priority));
1710 }
1711 
1712 void *
1713 sock_rmalloc(volatile struct sock *sk, unsigned long size, int force,
     /* [previous][next][first][last][top][bottom][index][help] */
1714              int priority)
1715 {
1716    if (sk )
1717      {
1718         if (sk->rmem_alloc + size < SK_RMEM_MAX || force)
1719           {
1720             cli();
1721             sk->rmem_alloc+= size;
1722             sti();
1723             return (kmalloc (size, priority));
1724           }
1725         MPRINTK (("sock_rmalloc(%X,%d,%d,%d) returning NULL\n",
1726                  sk,size,force, priority));
1727         return (NULL);
1728       }
1729    return (kmalloc (size, priority));
1730 }
1731 
1732 
1733 unsigned long
1734 sock_rspace (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1735 {
1736    int amt;
1737    if (sk != NULL)
1738      {
1739         if (sk->rmem_alloc >= SK_RMEM_MAX-2*MIN_WINDOW) return (0);
1740         amt = min ((SK_RMEM_MAX-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
1741         if (amt < 0) return (0);
1742         return (amt);
1743      }
1744    return (0);
1745 }
1746 
1747 unsigned long
1748 sock_wspace (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1749 {
1750   if (sk != NULL)
1751     {
1752        if (sk->shutdown & SEND_SHUTDOWN) return (0);
1753        if (sk->wmem_alloc >= SK_WMEM_MAX) return (0);
1754        return (SK_WMEM_MAX-sk->wmem_alloc );
1755     }
1756   return (0);
1757 }
1758 
1759 
1760 void
1761 sock_wfree (volatile struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1762 {
1763    MPRINTK (("sock_wfree (sk=%X, mem=%X, size=%d)\n",sk, mem, size));
1764    kfree_s (mem, size);
1765    if (sk)
1766      {
1767         sk->wmem_alloc -= size;
1768         /* in case it might be waiting for more memory. */
1769         if (!sk->dead) wake_up(sk->sleep);
1770         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0)
1771           {
1772              MPRINTK (("recovered lost memory, destroying sock = %X\n",sk));
1773              delete_timer ((struct timer *)&sk->time_wait);
1774              kfree_s ((void *)sk, sizeof (*sk));
1775           }
1776         return;
1777      }
1778 }
1779 
1780 void
1781 sock_rfree (volatile struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1782 {
1783    MPRINTK (("sock_rfree (sk=%X, mem=%X, size=%d)\n",sk, mem, size));
1784    kfree_s (mem, size);
1785    if (sk)
1786      {
1787         sk->rmem_alloc -= size;
1788         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0)
1789           {
1790              delete_timer ((struct timer *)&sk->time_wait);
1791              kfree_s ((void *)sk, sizeof (*sk));
1792           }
1793      }
1794 }
1795 
1796 
1797 /* This routine must find a socket given a tcp header.  Everyhting
1798    is assumed to be in net order. */
1799 
1800 volatile struct sock *get_sock (struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1801                                 unsigned long raddr,
1802                                 unsigned short rnum, unsigned long laddr)
1803 {
1804   volatile struct sock *s;
1805   PRINTK (("get_sock (prot=%X, num=%d, raddr=%X, rnum=%d, laddr=%X)\n",
1806           prot, num, raddr, rnum, laddr));
1807 
1808   /* SOCK_ARRAY_SIZE must be a power of two.  This will work better
1809      than a prime unless 3 or more sockets end up using the same
1810      array entry.  This should not be a problem because most
1811      well known sockets don't overlap that much, and for
1812      the other ones, we can just be careful about picking our
1813      socket number when we choose an arbitrary one. */
1814 
1815   for (s=prot->sock_array[num&(SOCK_ARRAY_SIZE-1)]; s != NULL; s=s->next)
1816     {
1817       if (s->num == num)
1818         {
1819           /* we need to see if this is the socket that we want. */
1820           if (!ip_addr_match (s->daddr, raddr))
1821             continue;
1822           if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0)
1823             continue;
1824           if (!ip_addr_match (s->saddr, laddr))
1825             continue;
1826           return (s);
1827         }
1828     }
1829   return (NULL);
1830 }
1831 
1832 void release_sock (volatile struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1833 {
1834   if (!sk)
1835     {
1836       printk ("sock.c: release_sock sk == NULL\n");
1837       return;
1838     }
1839 
1840   if (!sk->prot)
1841     {
1842       printk ("sock.c: release_sock sk->prot == NULL\n");
1843       return;
1844     }
1845 
1846   if (sk->blog) return;
1847   /* see if we have any packets built up. */
1848 
1849   cli();
1850   sk->inuse = 1;
1851   while (sk->back_log != NULL)
1852     {
1853       struct sk_buff *skb;
1854       sk->blog = 1;
1855       skb = (struct sk_buff *)sk->back_log;
1856       PRINTK (("release_sock: skb = %X:\n",skb));
1857       if (skb->next != skb)
1858         {
1859           sk->back_log = skb->next;
1860           skb->prev->next = skb->next;
1861           skb->next->prev = skb->prev;
1862         }
1863       else
1864         {
1865           sk->back_log = NULL;
1866         }
1867       sti();
1868       PRINTK (("sk->back_log = %X\n",sk->back_log));
1869       if (sk->prot->rcv)
1870         sk->prot->rcv(skb, skb->dev, sk->opt,
1871                       skb->saddr, skb->len, skb->daddr, 1,
1872                       /* only used for/by raw sockets. */
1873                       (struct ip_protocol *)sk->pair); 
1874       cli();
1875     }
1876   sk->blog = 0;
1877   sk->inuse = 0;
1878   sti();
1879   if (sk->dead && sk->state == TCP_CLOSE)
1880     {
1881         /* should be about 2 rtt's */
1882        sk->time_wait.len = min (sk->rtt * 2, TCP_DONE_TIME);
1883        sk->timeout = TIME_DONE;
1884        reset_timer ((struct timer *)&sk->time_wait);
1885     }
1886 }

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