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 = (struct sock *) 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 = (struct sock *) 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 = (struct sock *) 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 = (struct sock *) 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 = (struct sock *) 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->mdev = 0;
 763   sk->backoff = 0;
 764   sk->packets_out = 0;
 765   sk->cong_window = 1; /* start with only sending one packet at a time. */
 766   sk->exp_growth = 1;  /* if set cong_window grow exponentially every time
 767                           we get an ack. */
 768   sk->urginline = 0;
 769   sk->intr = 0;
 770   sk->linger = 0;
 771   sk->destroy = 0;
 772   sk->reuse = 0;
 773   sk->priority = 1;
 774   sk->shutdown = 0;
 775   sk->urg = 0;
 776   sk->keepopen = 0;
 777   sk->done = 0;
 778   sk->ack_backlog = 0;
 779   sk->window = 0;
 780   sk->bytes_rcv = 0;
 781   sk->state = TCP_CLOSE;
 782   sk->dead = 0;
 783   sk->ack_timed = 0;
 784   sk->send_tmp = NULL;
 785   sk->mss = 0; /* we will try not to send any packets smaller than this. */
 786 
 787   /* this is how many unacked bytes we will accept for this socket.  */
 788   sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
 789 
 790   /* how many packets we should send before forcing an ack. 
 791      if this is set to zero it is the same as sk->delay_acks = 0 */
 792   sk->max_ack_backlog = 0;
 793   sk->inuse = 0;
 794   sk->delay_acks = 0;
 795   sk->wback = NULL;
 796   sk->wfront = NULL;
 797   sk->rqueue = NULL;
 798   sk->mtu = 576;
 799   sk->prot = prot;
 800   sk->sleep = sock->wait;
 801   sk->daddr = 0;
 802   sk->saddr = my_addr();
 803   sk->err = 0;
 804   sk->next = NULL;
 805   sk->pair = NULL;
 806   sk->send_tail = NULL;
 807   sk->send_head = NULL;
 808   sk->time_wait.len = TCP_CONNECT_TIME;
 809   sk->time_wait.when = 0;
 810   sk->time_wait.sk = sk;
 811   sk->time_wait.next = NULL;
 812   sk->timeout = 0;
 813   sk->back_log = NULL;
 814   sk->blog = 0;
 815   sock->data =(void *) sk;
 816   sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 817   sk->dummy_th.res1=0;
 818   sk->dummy_th.res2=0;
 819   sk->dummy_th.urg_ptr = 0;
 820   sk->dummy_th.fin = 0;
 821   sk->dummy_th.syn = 0;
 822   sk->dummy_th.rst = 0;
 823   sk->dummy_th.psh = 0;
 824   sk->dummy_th.ack = 0;
 825   sk->dummy_th.urg = 0;
 826   sk->dummy_th.dest = 0;
 827 
 828   if (sk->num) {
 829         /*
 830          * It assumes that any protocol which allows
 831          * the user to assign a number at socket
 832          * creation time automatically
 833          * shares.
 834          */
 835         put_sock(sk->num, sk);
 836         sk->dummy_th.source = ntohs(sk->num);
 837   }
 838 
 839   if (sk->prot->init) {
 840         err = sk->prot->init(sk);
 841         if (err != 0) {
 842                 destroy_sock(sk);
 843                 return(err);
 844         }
 845   }
 846   return(0);
 847 }
 848 
 849 
 850 static int
 851 inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 852 {
 853   return(inet_create(newsock,
 854                    ((struct sock *)(oldsock->data))->protocol));
 855 }
 856 
 857 
 858 /* The peer socket should always be NULL. */
 859 static int
 860 inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 861 {
 862   struct sock *sk;
 863 
 864   sk = (struct sock *) sock->data;
 865   if (sk == NULL) return(0);
 866 
 867   DPRINTF((DBG_INET, "inet_release(sock = %X, peer = %X)\n", sock, peer));
 868   wake_up(sk->sleep);
 869 
 870   /* Start closing the connection.  This may take a while. */
 871   /*
 872    * If linger is set, we don't return until the close
 873    * is complete.  Other wise we return immediately. The
 874    * actually closing is done the same either way.
 875    */
 876   if (sk->linger == 0) {
 877         sk->prot->close(sk,0);
 878         sk->dead = 1;
 879   } else {
 880         DPRINTF((DBG_INET, "sk->linger set.\n"));
 881         sk->prot->close(sk, 0);
 882         cli();
 883         while(sk->state != TCP_CLOSE) {
 884                 interruptible_sleep_on(sk->sleep);
 885                 if (current->signal & ~current->blocked) {
 886                         sti();
 887                         return(-ERESTARTSYS);
 888                 }
 889         }
 890         sti();
 891         sk->dead = 1;
 892   }
 893   sk->inuse = 1;
 894 
 895   /* This will destroy it. */
 896   release_sock(sk);
 897   sock->data = NULL;
 898   DPRINTF((DBG_INET, "inet_release returning\n"));
 899   return(0);
 900 }
 901 
 902 
 903 /* this needs to be changed to dissallow
 904    the rebinding of sockets.   What error
 905    should it return? */
 906 
 907 static int
 908 inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 909                int addr_len)
 910 {
 911   struct sockaddr_in addr;
 912   struct sock *sk, *sk2;
 913   unsigned short snum;
 914 
 915   sk = (struct sock *) sock->data;
 916   if (sk == NULL) {
 917         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
 918         return(0);
 919   }
 920 
 921   /* check this error. */
 922   if (sk->state != TCP_CLOSE) return(-EIO);
 923   if (sk->num != 0) return(-EINVAL);
 924 
 925   /* verify_area(VERIFY_WRITE, uaddr, addr_len);*/
 926   memcpy_fromfs(&addr, uaddr, min(sizeof(addr), addr_len));
 927 
 928 #if 0   /* FIXME: */
 929   if (addr.sin_family && addr.sin_family != AF_INET) {
 930         /*
 931          * This is really a bug in BSD which we need
 932          * to emulate because ftp expects it.
 933          */
 934         return(-EINVAL);
 935   }
 936 #endif
 937 
 938   snum = ntohs(addr.sin_port);
 939   DPRINTF((DBG_INET, "bind sk =%X to port = %d\n", sk, snum));
 940   sk = (struct sock *) sock->data;
 941 
 942   /*
 943    * We can't just leave the socket bound wherever it is, it might
 944    * be bound to a privileged port. However, since there seems to
 945    * be a bug here, we will leave it if the port is not privileged.
 946    */
 947   if (snum == 0) {
 948 /*      if (sk->num > PROT_SOCK) return(0); */
 949         snum = get_new_socknum(sk->prot, 0);
 950   }
 951   if (snum <= PROT_SOCK && !suser()) return(-EACCES);
 952 
 953   if (chk_addr(addr.sin_addr.s_addr) || addr.sin_addr.s_addr == 0)
 954                                         sk->saddr = addr.sin_addr.s_addr;
 955 
 956   DPRINTF((DBG_INET, "sock_array[%d] = %X:\n", snum &(SOCK_ARRAY_SIZE -1),
 957                         sk->prot->sock_array[snum &(SOCK_ARRAY_SIZE -1)]));
 958 
 959   /* Make sure we are allowed to bind here. */
 960   cli();
 961 outside_loop:
 962   for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
 963                                         sk2 != NULL; sk2 = sk2->next) {
 964 #if     1       /* should be below! */
 965         if (sk2->num != snum) continue;
 966 /*      if (sk2->saddr != sk->saddr) continue; */
 967 #endif
 968         if (sk2->dead) {
 969                 destroy_sock(sk2);
 970                 goto outside_loop;
 971         }
 972         if (!sk->reuse) {
 973                 sti();
 974                 return(-EADDRINUSE);
 975         }
 976         if (!sk2->reuse) {
 977                 sti();
 978                 return(-EADDRINUSE);
 979         }
 980   }
 981   sti();
 982 
 983   remove_sock(sk);
 984   put_sock(snum, sk);
 985   sk->dummy_th.source = ntohs(sk->num);
 986   sk->daddr = 0;
 987   sk->dummy_th.dest = 0;
 988   return(0);
 989 }
 990 
 991 
 992 static int
 993 inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 994                   int addr_len, int flags)
 995 {
 996   struct sock *sk;
 997   int err;
 998 
 999   sock->conn = NULL;
1000   sk = (struct sock *) sock->data;
1001   if (sk == NULL) {
1002         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1003         return(0);
1004   }
1005 
1006   if (sock->state != SS_CONNECTING) {
1007         /* We may need to bind the socket. */
1008         if (sk->num == 0) {
1009                 sk->num = get_new_socknum(sk->prot, 0);
1010                 if (sk->num == 0) return(-EAGAIN);
1011                 put_sock(sk->num, sk);
1012                 sk->dummy_th.source = htons(sk->num);
1013         }
1014 
1015         if (sk->prot->connect == NULL) return(-EOPNOTSUPP);
1016   
1017         err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
1018         if (err < 0) return(err);
1019   
1020         sock->state = SS_CONNECTING;
1021   }
1022 
1023   if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) return(-EINPROGRESS);
1024 
1025   cli(); /* avoid the race condition */
1026   while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) {
1027         interruptible_sleep_on(sk->sleep);
1028         if (current->signal & ~current->blocked) {
1029                 sti();
1030                 return(-ERESTARTSYS);
1031         }
1032   }
1033   sti();
1034   sock->state = SS_CONNECTED;
1035 
1036   if (sk->state != TCP_ESTABLISHED && sk->err) {
1037         sock->state = SS_UNCONNECTED;
1038         return(-sk->err);
1039   }
1040   return(0);
1041 }
1042 
1043 
1044 static int
1045 inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1046 {
1047   return(-EOPNOTSUPP);
1048 }
1049 
1050 
1051 static int
1052 inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1053 {
1054   struct sock *sk1, *sk2;
1055 
1056   sk1 = (struct sock *) sock->data;
1057   if (sk1 == NULL) {
1058         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1059         return(0);
1060   }
1061 
1062   /*
1063    * We've been passed an extra socket.
1064    * We need to free it up because the tcp module creates
1065    * it's own when it accepts one.
1066    */
1067   if (newsock->data) kfree_s(newsock->data, sizeof(struct sock));
1068   newsock->data = NULL;
1069 
1070   if (sk1->prot->accept == NULL) return(-EOPNOTSUPP);
1071 
1072   /* Restore the state if we have been interrupted, and then returned. */
1073   if (sk1->pair != NULL ) {
1074         sk2 = sk1->pair;
1075         sk1->pair = NULL;
1076   } else {
1077         sk2 = sk1->prot->accept(sk1,flags);
1078         if (sk2 == NULL) {
1079                 if (sk1->err <= 0)
1080                         printk("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
1081                 return(-sk1->err);
1082         }
1083   }
1084   newsock->data = (void *)sk2;
1085   sk2->sleep = newsock->wait;
1086   newsock->conn = NULL;
1087   if (flags & O_NONBLOCK) return(0);
1088 
1089   cli(); /* avoid the race. */
1090   while(sk2->state == TCP_SYN_RECV) {
1091         interruptible_sleep_on(sk2->sleep);
1092         if (current->signal & ~current->blocked) {
1093                 sti();
1094                 sk1->pair = sk2;
1095                 sk2->sleep = NULL;
1096                 newsock->data = NULL;
1097                 return(-ERESTARTSYS);
1098         }
1099   }
1100   sti();
1101 
1102   if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) {
1103         int err;
1104 
1105         err = -sk2->err;
1106         destroy_sock(sk2);
1107         newsock->data = NULL;
1108         return(err);
1109   }
1110   newsock->state = SS_CONNECTED;
1111   return(0);
1112 }
1113 
1114 
1115 static int
1116 inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1117                  int *uaddr_len, int peer)
1118 {
1119   struct sockaddr_in sin;
1120   struct sock *sk;
1121   int len;
1122 
1123   len = get_fs_long(uaddr_len);
1124 
1125   /* Check this error. */
1126   if (len < sizeof(sin)) return(-EINVAL);
1127 
1128   sin.sin_family = AF_INET;
1129   sk = (struct sock *) sock->data;
1130   if (sk == NULL) {
1131         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1132         return(0);
1133   }
1134   if (peer) {
1135         if (!tcp_connected(sk->state)) return(-ENOTCONN);
1136         sin.sin_port = sk->dummy_th.dest;
1137         sin.sin_addr.s_addr = sk->daddr;
1138   } else {
1139         sin.sin_port = sk->dummy_th.source;
1140         if (sk->saddr == 0) sin.sin_addr.s_addr = my_addr();
1141           else sin.sin_addr.s_addr = sk->saddr;
1142   }
1143   len = sizeof(sin);
1144   verify_area(VERIFY_WRITE, uaddr, len);
1145   memcpy_tofs(uaddr, &sin, sizeof(sin));
1146   verify_area(VERIFY_WRITE, uaddr_len, sizeof(len));
1147   put_fs_long(len, uaddr_len);
1148   return(0);
1149 }
1150 
1151 
1152 static int
1153 inet_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1154 {
1155   struct sock *sk;
1156 
1157   sk = (struct sock *) sock->data;
1158   if (sk == NULL) {
1159         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1160         return(0);
1161   }
1162 
1163   /* We may need to bind the socket. */
1164   if (sk->num == 0) {
1165         sk->num = get_new_socknum(sk->prot, 0);
1166         if (sk->num == 0) return(-EAGAIN);
1167         put_sock(sk->num, sk);
1168         sk->dummy_th.source = ntohs(sk->num);
1169   }
1170   return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock,0));
1171 }
1172 
1173 
1174 static int
1175 inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1176           unsigned flags)
1177 {
1178   struct sock *sk;
1179 
1180   sk = (struct sock *) sock->data;
1181   if (sk == NULL) {
1182         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1183         return(0);
1184   }
1185 
1186   /* We may need to bind the socket. */
1187   if (sk->num == 0) {
1188         sk->num = get_new_socknum(sk->prot, 0);
1189         if (sk->num == 0) return(-EAGAIN);
1190         put_sock(sk->num, sk);
1191         sk->dummy_th.source = ntohs(sk->num);
1192   }
1193   return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, flags));
1194 }
1195 
1196 
1197 static int
1198 inet_write(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1199 {
1200   struct sock *sk;
1201 
1202   sk = (struct sock *) sock->data;
1203   if (sk == NULL) {
1204         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1205         return(0);
1206   }
1207   if (sk->shutdown & SEND_SHUTDOWN) {
1208         send_sig(SIGPIPE, current, 1);
1209         return(-EPIPE);
1210   }
1211 
1212   /* We may need to bind the socket. */
1213   if (sk->num == 0) {
1214         sk->num = get_new_socknum(sk->prot, 0);
1215         if (sk->num == 0) return(-EAGAIN);
1216         put_sock(sk->num, sk);
1217         sk->dummy_th.source = ntohs(sk->num);
1218   }
1219 
1220   return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, 0));
1221 }
1222 
1223 
1224 static int
1225 inet_send(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1226                unsigned flags)
1227 {
1228   struct sock *sk;
1229 
1230   sk = (struct sock *) sock->data;
1231   if (sk == NULL) {
1232         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1233         return(0);
1234   }
1235   if (sk->shutdown & SEND_SHUTDOWN) {
1236         send_sig(SIGPIPE, current, 1);
1237         return(-EPIPE);
1238   }
1239 
1240   /* We may need to bind the socket. */
1241   if (sk->num == 0) {
1242         sk->num = get_new_socknum(sk->prot, 0);
1243         if (sk->num == 0) return(-EAGAIN);
1244         put_sock(sk->num, sk);
1245         sk->dummy_th.source = ntohs(sk->num);
1246   }
1247 
1248   return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
1249 }
1250 
1251 
1252 static int
1253 inet_sendto(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1254             unsigned flags, struct sockaddr *sin, int addr_len)
1255 {
1256   struct sock *sk;
1257 
1258   sk = (struct sock *) sock->data;
1259   if (sk == NULL) {
1260         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1261         return(0);
1262   }
1263   if (sk->shutdown & SEND_SHUTDOWN) {
1264         send_sig(SIGPIPE, current, 1);
1265         return(-EPIPE);
1266   }
1267 
1268   if (sk->prot->sendto == NULL) return(-EOPNOTSUPP);
1269 
1270   /* We may need to bind the socket. */
1271   if (sk->num == 0) {
1272         sk->num = get_new_socknum(sk->prot, 0);
1273         if (sk->num == 0) return(-EAGAIN);
1274         put_sock(sk->num, sk);
1275         sk->dummy_th.source = ntohs(sk->num);
1276   }
1277 
1278   return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags, 
1279                            (struct sockaddr_in *)sin, addr_len));
1280 }
1281 
1282 
1283 static int
1284 inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1285                    unsigned flags, struct sockaddr *sin, int *addr_len )
1286 {
1287   struct sock *sk;
1288 
1289   sk = (struct sock *) sock->data;
1290   if (sk == NULL) {
1291         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1292         return(0);
1293   }
1294 
1295   if (sk->prot->recvfrom == NULL) return(-EOPNOTSUPP);
1296 
1297   /* We may need to bind the socket. */
1298   if (sk->num == 0) {
1299         sk->num = get_new_socknum(sk->prot, 0);
1300         if (sk->num == 0) return(-EAGAIN);
1301         put_sock(sk->num, sk);
1302         sk->dummy_th.source = ntohs(sk->num);
1303   }
1304 
1305   return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1306                              (struct sockaddr_in*)sin, addr_len));
1307 }
1308 
1309 
1310 static int
1311 inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1312 {
1313   struct sock *sk;
1314 
1315   /*
1316    * This should really check to make sure
1317    * the socket is a TCP socket.
1318    */
1319   how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1320                        1->2 bit 2 snds.
1321                        2->3 */
1322   if (how & ~SHUTDOWN_MASK) return(-EINVAL);
1323   sk = (struct sock *) sock->data;
1324   if (sk == NULL) {
1325         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1326         return(0);
1327   }
1328   if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1329                                                 sock->state = SS_CONNECTED;
1330 
1331   if (!tcp_connected(sk->state)) return(-ENOTCONN);
1332   sk->shutdown |= how;
1333   if (sk->prot->shutdown) sk->prot->shutdown(sk, how);
1334   return(0);
1335 }
1336 
1337 
1338 static int
1339 inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1340 {
1341   struct sock *sk;
1342 
1343   sk = (struct sock *) sock->data;
1344   if (sk == NULL) {
1345         printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
1346         return(0);
1347   }
1348 
1349   if (sk->prot->select == NULL) {
1350         DPRINTF((DBG_INET, "select on non-selectable socket.\n"));
1351         return(0);
1352   }
1353   return(sk->prot->select(sk, sel_type, wait));
1354 }
1355 
1356 
1357 static int
1358 inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1359 {
1360   struct sock *sk;
1361 
1362   DPRINTF((DBG_INET, "INET: in inet_ioctl\n"));
1363   sk = NULL;
1364   if (sock && (sk = (struct sock *) sock->data) == NULL) {
1365         printk("AF_INET: Warning: sock->data = NULL: %d\n" , __LINE__);
1366         return(0);
1367   }
1368 
1369   switch(cmd) {
1370         case FIOSETOWN:
1371         case SIOCSPGRP:
1372                 if (sk)
1373                         sk->proc = get_fs_long((int *) arg);
1374                 return(0);
1375         case FIOGETOWN:
1376         case SIOCGPGRP:
1377                 if (sk) {
1378                         verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1379                         put_fs_long(sk->proc,(int *)arg);
1380                 }
1381                 return(0);
1382 #if 0   /* FIXME: */
1383         case SIOCATMARK:
1384                 printk("AF_INET: ioctl(SIOCATMARK, 0x%08X)\n",(void *) arg);
1385                 return(-EINVAL);
1386 #endif
1387 
1388         case DDIOCSDBG:
1389                 return(dbg_ioctl((void *) arg, DBG_INET));
1390 
1391         case SIOCADDRT:
1392         case SIOCDELRT:
1393                 return(rt_ioctl(cmd,(void *) arg));
1394 
1395         case SIOCDARP:
1396         case SIOCGARP:
1397         case SIOCSARP:
1398                 return(arp_ioctl(cmd,(void *) arg));
1399 
1400         case IP_SET_DEV:
1401         case SIOCGIFCONF:
1402         case SIOCGIFFLAGS:
1403         case SIOCSIFFLAGS:
1404         case SIOCGIFADDR:
1405         case SIOCSIFADDR:
1406         case SIOCGIFDSTADDR:
1407         case SIOCSIFDSTADDR:
1408         case SIOCGIFBRDADDR:
1409         case SIOCSIFBRDADDR:
1410         case SIOCGIFNETMASK:
1411         case SIOCSIFNETMASK:
1412         case SIOCGIFMETRIC:
1413         case SIOCSIFMETRIC:
1414         case SIOCGIFMEM:
1415         case SIOCSIFMEM:
1416         case SIOCGIFMTU:
1417         case SIOCSIFMTU:
1418         case SIOCSIFLINK:
1419                 return(dev_ioctl(cmd,(void *) arg));
1420 
1421         default:
1422                 if (!sk || !sk->prot->ioctl) return(-EINVAL);
1423                 return(sk->prot->ioctl(sk, cmd, arg));
1424   }
1425   /*NOTREACHED*/
1426   return(0);
1427 }
1428 
1429 
1430 void *
1431 sock_wmalloc(struct sock *sk, unsigned long size, int force,
     /* [previous][next][first][last][top][bottom][index][help] */
1432              int priority)
1433 {
1434   if (sk) {
1435         if (sk->wmem_alloc + size < SK_WMEM_MAX || force) {
1436                 cli();
1437                 sk->wmem_alloc+= size;
1438                 sti();
1439                 return(kmalloc(size, priority));
1440         }
1441         DPRINTF((DBG_INET, "sock_wmalloc(%X,%d,%d,%d) returning NULL\n",
1442                                                 sk, size, force, priority));
1443         return(NULL);
1444   }
1445   return(kmalloc(size, priority));
1446 }
1447 
1448 
1449 void *
1450 sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
1451 {
1452   if (sk) {
1453         if (sk->rmem_alloc + size < SK_RMEM_MAX || force) {
1454                 void *c = kmalloc(size, priority);
1455                 cli();
1456                 if (c) sk->rmem_alloc += size;
1457                 sti();
1458                 return(c);
1459         }
1460         DPRINTF((DBG_INET, "sock_rmalloc(%X,%d,%d,%d) returning NULL\n",
1461                                                 sk,size,force, priority));
1462         return(NULL);
1463   }
1464   return(kmalloc(size, priority));
1465 }
1466 
1467 
1468 unsigned long
1469 sock_rspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1470 {
1471   int amt;
1472 
1473   if (sk != NULL) {
1474         if (sk->rmem_alloc >= SK_RMEM_MAX-2*MIN_WINDOW) return(0);
1475         amt = min((SK_RMEM_MAX-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
1476         if (amt < 0) return(0);
1477         return(amt);
1478   }
1479   return(0);
1480 }
1481 
1482 
1483 unsigned long
1484 sock_wspace(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1485 {
1486   if (sk != NULL) {
1487         if (sk->shutdown & SEND_SHUTDOWN) return(0);
1488         if (sk->wmem_alloc >= SK_WMEM_MAX) return(0);
1489         return(SK_WMEM_MAX-sk->wmem_alloc );
1490   }
1491   return(0);
1492 }
1493 
1494 
1495 void
1496 sock_wfree(struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1497 {
1498   DPRINTF((DBG_INET, "sock_wfree(sk=%X, mem=%X, size=%d)\n", sk, mem, size));
1499 
1500   kfree_s(mem, size);
1501   if (sk) {
1502         sk->wmem_alloc -= size;
1503 
1504         /* In case it might be waiting for more memory. */
1505         if (!sk->dead) wake_up(sk->sleep);
1506         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0) {
1507                 DPRINTF((DBG_INET,
1508                         "recovered lost memory, destroying sock = %X\n", sk));
1509                 delete_timer((struct timer *)&sk->time_wait);
1510                 kfree_s((void *)sk, sizeof(*sk));
1511         }
1512         return;
1513   }
1514 }
1515 
1516 
1517 void
1518 sock_rfree(struct sock *sk, void *mem, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
1519 {
1520   DPRINTF((DBG_INET, "sock_rfree(sk=%X, mem=%X, size=%d)\n", sk, mem, size));
1521 
1522   kfree_s(mem, size);
1523   if (sk) {
1524         sk->rmem_alloc -= size;
1525         if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0) {
1526                 delete_timer((struct timer *)&sk->time_wait);
1527                 kfree_s((void *)sk, sizeof(*sk));
1528         }
1529   }
1530 }
1531 
1532 
1533 /*
1534  * This routine must find a socket given a TCP or UDP header.
1535  * Everyhting is assumed to be in net order.
1536  */
1537 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1538                                 unsigned long raddr,
1539                                 unsigned short rnum, unsigned long laddr)
1540 {
1541   struct sock *s;
1542   unsigned short hnum;
1543 
1544   hnum = ntohs(num);
1545   DPRINTF((DBG_INET, "get_sock(prot=%X, num=%d, raddr=%X, rnum=%d, laddr=%X)\n",
1546           prot, num, raddr, rnum, laddr));
1547 
1548   /*
1549    * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1550    * than a prime unless 3 or more sockets end up using the same
1551    * array entry.  This should not be a problem because most
1552    * well known sockets don't overlap that much, and for
1553    * the other ones, we can just be careful about picking our
1554    * socket number when we choose an arbitrary one.
1555    */
1556   for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1557       s != NULL; s = s->next) {
1558         if (s->num == hnum) {
1559                 /* We need to see if this is the socket that we want. */
1560                 if (ip_addr_match(s->daddr, raddr) == 0) continue;
1561                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) continue;
1562 #if 1   /* C.E. Hawkins  ceh@eng.cam.ac.uk */
1563                 if (s->prot != &udp_prot || prot != &udp_prot)
1564 #endif
1565                         if (ip_addr_match(s->saddr, laddr) == 0) continue;
1566                 if (s->dead && (s->state == TCP_CLOSE)) continue;
1567                 return(s);
1568         }
1569   }
1570   return(NULL);
1571 }
1572 
1573 
1574 void release_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
1575 {
1576   if (!sk) {
1577         printk("sock.c: release_sock sk == NULL\n");
1578         return;
1579   }
1580   if (!sk->prot) {
1581         printk("sock.c: release_sock sk->prot == NULL\n");
1582         return;
1583   }
1584 
1585   if (sk->blog) return;
1586 
1587   /* See if we have any packets built up. */
1588   cli();
1589   sk->inuse = 1;
1590   while(sk->back_log != NULL) {
1591         struct sk_buff *skb;
1592 
1593         sk->blog = 1;
1594         skb =(struct sk_buff *)sk->back_log;
1595         DPRINTF((DBG_INET, "release_sock: skb = %X:\n", skb));
1596         if (skb->next != skb) {
1597                 sk->back_log = skb->next;
1598                 skb->prev->next = skb->next;
1599                 skb->next->prev = skb->prev;
1600         } else {
1601                 sk->back_log = NULL;
1602         }
1603         sti();
1604         DPRINTF((DBG_INET, "sk->back_log = %X\n", sk->back_log));
1605         if (sk->prot->rcv) sk->prot->rcv(skb, skb->dev, sk->opt,
1606                                          skb->saddr, skb->len, skb->daddr, 1,
1607 
1608         /* Only used for/by raw sockets. */
1609         (struct inet_protocol *)sk->pair); 
1610         cli();
1611   }
1612   sk->blog = 0;
1613   sk->inuse = 0;
1614   sti();
1615   if (sk->dead && sk->state == TCP_CLOSE) {
1616         /* Should be about 2 rtt's */
1617         sk->time_wait.len = min(sk->rtt * 2, TCP_DONE_TIME);
1618         sk->timeout = TIME_DONE;
1619         reset_timer((struct timer *)&sk->time_wait);
1620   }
1621 }
1622 
1623 
1624 static int
1625 inet_fioctl(struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1626          unsigned int cmd, unsigned long arg)
1627 {
1628   int minor, ret;
1629 
1630   /* Extract the minor number on which we work. */
1631   minor = MINOR(inode->i_rdev);
1632   if (minor != 0) return(-ENODEV);
1633 
1634   /* Now dispatch on the minor device. */
1635   switch(minor) {
1636         case 0:         /* INET */
1637                 ret = inet_ioctl(NULL, cmd, arg);
1638                 break;
1639         case 1:         /* IP */
1640                 ret = ip_ioctl(NULL, cmd, arg);
1641                 break;
1642         case 2:         /* ICMP */
1643                 ret = icmp_ioctl(NULL, cmd, arg);
1644                 break;
1645         case 3:         /* TCP */
1646                 ret = tcp_ioctl(NULL, cmd, arg);
1647                 break;
1648         case 4:         /* UDP */
1649                 ret = udp_ioctl(NULL, cmd, arg);
1650                 break;
1651         default:
1652                 ret = -ENODEV;
1653   }
1654 
1655   return(ret);
1656 }
1657 
1658 
1659 static struct file_operations inet_fops = {
1660   NULL,         /* LSEEK        */
1661   NULL,         /* READ         */
1662   NULL,         /* WRITE        */
1663   NULL,         /* READDIR      */
1664   NULL,         /* SELECT       */
1665   inet_fioctl,  /* IOCTL        */
1666   NULL,         /* MMAP         */
1667   NULL,         /* OPEN         */
1668   NULL          /* CLOSE        */
1669 };
1670 
1671 
1672 static struct proto_ops inet_proto_ops = {
1673   AF_INET,
1674 
1675   inet_create,
1676   inet_dup,
1677   inet_release,
1678   inet_bind,
1679   inet_connect,
1680   inet_socketpair,
1681   inet_accept,
1682   inet_getname, 
1683   inet_read,
1684   inet_write,
1685   inet_select,
1686   inet_ioctl,
1687   inet_listen,
1688   inet_send,
1689   inet_recv,
1690   inet_sendto,
1691   inet_recvfrom,
1692   inet_shutdown,
1693   inet_setsockopt,
1694   inet_getsockopt,
1695   inet_fcntl,
1696 };
1697 
1698 
1699 void inet_proto_init(struct ddi_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1700 {
1701   struct inet_protocol *p;
1702   int i;
1703 
1704   /* Set up our UNIX VFS major device. */
1705   if (register_chrdev(AF_INET_MAJOR, "af_inet", &inet_fops) < 0) {
1706         printk("5s: cannot register major device %d!\n",
1707                                         pro->name, AF_INET_MAJOR);
1708         return;
1709   }
1710 
1711   /* Tell SOCKET that we are alive... */
1712   (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1713 
1714   seq_offset = CURRENT_TIME*250;
1715 
1716   /* Add all the protocols. */
1717   for(i = 0; i < SOCK_ARRAY_SIZE; i++) {
1718         tcp_prot.sock_array[i] = NULL;
1719         udp_prot.sock_array[i] = NULL;
1720         raw_prot.sock_array[i] = NULL;
1721   }
1722   for(p = inet_protocol_base; p != NULL;) {
1723         struct inet_protocol *tmp;
1724 
1725         tmp = (struct inet_protocol *) p->next;
1726         inet_add_protocol(p);
1727         p = tmp;
1728   }
1729 
1730   /* Initialize the DEV module. */
1731   dev_init();
1732 
1733   /* Initialize the "Buffer Head" pointers. */
1734   bh_base[INET_BH].routine = inet_bh;
1735   timer_table[NET_TIMER].fn = net_timer;
1736 }

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