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

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