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

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