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

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