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

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