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

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