root/net/tcp/sock.c

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

DEFINITIONS

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

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

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