root/net/inet/sock.c

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

DEFINITIONS

This source file includes following definitions.
  1. print_sk
  2. print_skb
  3. lock_skb
  4. kfree_skb
  5. unlock_skb
  6. sk_inuse
  7. get_new_socknum
  8. put_sock
  9. remove_sock
  10. destroy_sock
  11. inet_fcntl
  12. inet_setsockopt
  13. inet_getsockopt
  14. inet_listen
  15. inet_create
  16. inet_dup
  17. inet_release
  18. inet_bind
  19. inet_connect
  20. inet_socketpair
  21. inet_accept
  22. inet_getname
  23. inet_read
  24. inet_recv
  25. inet_write
  26. inet_send
  27. inet_sendto
  28. inet_recvfrom
  29. inet_shutdown
  30. inet_select
  31. inet_ioctl
  32. sock_wmalloc
  33. sock_rmalloc
  34. sock_rspace
  35. sock_wspace
  36. sock_wfree
  37. sock_rfree
  38. get_sock
  39. release_sock
  40. inet_fioctl
  41. inet_proto_init

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

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