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

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