root/net/ipv4/af_inet.c

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

DEFINITIONS

This source file includes following definitions.
  1. sk_inuse
  2. get_new_socknum
  3. put_sock
  4. remove_sock
  5. destroy_sock
  6. inet_fcntl
  7. inet_setsockopt
  8. inet_getsockopt
  9. inet_autobind
  10. inet_listen
  11. def_callback1
  12. def_callback2
  13. def_callback3
  14. inet_create
  15. inet_dup
  16. closing
  17. inet_release
  18. inet_bind
  19. inet_error
  20. inet_connect
  21. inet_socketpair
  22. inet_accept
  23. inet_getname
  24. inet_recvfrom
  25. inet_recvmsg
  26. inet_recv
  27. inet_read
  28. inet_send
  29. inet_write
  30. inet_sendto
  31. inet_sendmsg
  32. inet_shutdown
  33. inet_select
  34. inet_ioctl
  35. get_sock
  36. get_sock_raw
  37. get_sock_mcast
  38. 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  *              AF_INET protocol family socket handler.
   7  *
   8  * Version:     @(#)af_inet.c   (from 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  *              Alan Cox, <A.Cox@swansea.ac.uk>
  14  *
  15  * Changes (see also sock.c)
  16  *
  17  *              A.N.Kuznetsov   :       Socket death error in accept().
  18  *              John Richardson :       Fix non blocking error in connect()
  19  *                                      so sockets that fail to connect
  20  *                                      don't return -EINPROGRESS.
  21  *              Alan Cox        :       Asynchronous I/O support
  22  *              Alan Cox        :       Keep correct socket pointer on sock structures
  23  *                                      when accept() ed
  24  *              Alan Cox        :       Semantics of SO_LINGER aren't state moved
  25  *                                      to close when you look carefully. With
  26  *                                      this fixed and the accept bug fixed 
  27  *                                      some RPC stuff seems happier.
  28  *              Niibe Yutaka    :       4.4BSD style write async I/O
  29  *              Alan Cox, 
  30  *              Tony Gale       :       Fixed reuse semantics.
  31  *              Alan Cox        :       bind() shouldn't abort existing but dead
  32  *                                      sockets. Stops FTP netin:.. I hope.
  33  *              Alan Cox        :       bind() works correctly for RAW sockets. Note
  34  *                                      that FreeBSD at least was broken in this respect
  35  *                                      so be careful with compatibility tests...
  36  *              Alan Cox        :       routing cache support
  37  *              Alan Cox        :       memzero the socket structure for compactness.
  38  *              Matt Day        :       nonblock connect error handler
  39  *              Alan Cox        :       Allow large numbers of pending sockets
  40  *                                      (eg for big web sites), but only if
  41  *                                      specifically application requested.
  42  *              Alan Cox        :       New buffering throughout IP. Used dumbly.
  43  *              Alan Cox        :       New buffering now used smartly.
  44  *              Alan Cox        :       BSD rather than common sense interpretation of
  45  *                                      listen.
  46  *              Germano Caronni :       Assorted small races.
  47  *              Alan Cox        :       sendmsg/recvmsg basic support.
  48  *
  49  *              This program is free software; you can redistribute it and/or
  50  *              modify it under the terms of the GNU General Public License
  51  *              as published by the Free Software Foundation; either version
  52  *              2 of the License, or (at your option) any later version.
  53  */
  54 
  55 #include <linux/config.h>
  56 #include <linux/errno.h>
  57 #include <linux/types.h>
  58 #include <linux/socket.h>
  59 #include <linux/in.h>
  60 #include <linux/kernel.h>
  61 #include <linux/major.h>
  62 #include <linux/sched.h>
  63 #include <linux/timer.h>
  64 #include <linux/string.h>
  65 #include <linux/sockios.h>
  66 #include <linux/net.h>
  67 #include <linux/fcntl.h>
  68 #include <linux/mm.h>
  69 #include <linux/interrupt.h>
  70 #include <linux/proc_fs.h>
  71 #include <linux/stat.h>
  72 
  73 #include <asm/segment.h>
  74 #include <asm/system.h>
  75 
  76 #include <linux/inet.h>
  77 #include <linux/netdevice.h>
  78 #include <net/ip.h>
  79 #include <net/protocol.h>
  80 #include <net/arp.h>
  81 #include <net/rarp.h>
  82 #include <net/route.h>
  83 #include <net/tcp.h>
  84 #include <net/udp.h>
  85 #include <linux/skbuff.h>
  86 #include <net/sock.h>
  87 #include <net/raw.h>
  88 #include <net/icmp.h>
  89 #include <linux/ip_fw.h>
  90 
  91 #define min(a,b)        ((a)<(b)?(a):(b))
  92 
  93 extern struct proto packet_prot;
  94 extern int raw_get_info(char *, char **, off_t, int, int);
  95 extern int snmp_get_info(char *, char **, off_t, int, int);
  96 extern int afinet_get_info(char *, char **, off_t, int, int);
  97 extern int tcp_get_info(char *, char **, off_t, int, int);
  98 extern int udp_get_info(char *, char **, off_t, int, int);
  99 
 100 int (*rarp_ioctl_hook)(unsigned int,void*) = NULL;
 101 
 102 /*
 103  *      See if a socket number is in use.
 104  */
 105  
 106 static int sk_inuse(struct proto *prot, int num)
     /* [previous][next][first][last][top][bottom][index][help] */
 107 {
 108         struct sock *sk;
 109 
 110         for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];
 111                 sk != NULL;  sk=sk->next) 
 112         {
 113                 if (sk->num == num) 
 114                         return(1);
 115         }
 116         return(0);
 117 }
 118 
 119 
 120 /*
 121  *      Pick a new socket number
 122  */
 123 
 124 unsigned short get_new_socknum(struct proto *prot, unsigned short base)
     /* [previous][next][first][last][top][bottom][index][help] */
 125 {
 126         static int start=0;
 127 
 128         /*
 129          * Used to cycle through the port numbers so the
 130          * chances of a confused connection drop.
 131          */
 132          
 133         int i, j;
 134         int best = 0;
 135         int size = 32767; /* a big num. */
 136         struct sock *sk;
 137 
 138         if (base == 0) 
 139                 base = PROT_SOCK+1+(start % 1024);
 140         if (base <= PROT_SOCK) 
 141         {
 142                 base += PROT_SOCK+(start % 1024);
 143         }
 144 
 145         /*
 146          *      Now look through the entire array and try to find an empty ptr. 
 147          */
 148          
 149         for(i=0; i < SOCK_ARRAY_SIZE; i++) 
 150         {
 151                 j = 0;
 152                 sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
 153                 while(sk != NULL) 
 154                 {
 155                         sk = sk->next;
 156                         j++;
 157                 }
 158                 if (j == 0) 
 159                 {
 160                         start =(i+1+start )%1024;
 161                         return(i+base+1);
 162                 }
 163                 if (j < size) 
 164                 {
 165                         best = i;
 166                         size = j;
 167                 }
 168         }
 169 
 170         /* Now make sure the one we want is not in use. */
 171 
 172         while(sk_inuse(prot, base +best+1)) 
 173         {
 174                 best += SOCK_ARRAY_SIZE;
 175         }
 176         return(best+base+1);
 177 }
 178 
 179 /*
 180  *      Add a socket into the socket tables by number.
 181  */
 182 
 183 void put_sock(unsigned short num, struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 184 {
 185         struct sock **skp, *tmp;
 186         int mask;
 187         unsigned long flags;
 188         
 189         if(sk->type==SOCK_PACKET)
 190                 return;
 191 
 192         sk->num = num;
 193         sk->next = NULL;
 194         num = num &(SOCK_ARRAY_SIZE -1);
 195 
 196         /* 
 197          *      We can't have an interrupt re-enter here. 
 198          */
 199          
 200         save_flags(flags);
 201         cli();
 202 
 203         sk->prot->inuse += 1;
 204         if (sk->prot->highestinuse < sk->prot->inuse)
 205                 sk->prot->highestinuse = sk->prot->inuse;
 206 
 207         if (sk->prot->sock_array[num] == NULL) 
 208         {
 209                 sk->prot->sock_array[num] = sk;
 210                 restore_flags(flags);
 211                 return;
 212         }
 213         
 214         restore_flags(flags);
 215         for(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask) 
 216         {
 217                 if ((mask & sk->rcv_saddr) &&
 218                     (mask & sk->rcv_saddr) != (mask & 0xffffffff)) 
 219                 {
 220                         mask = mask << 8;
 221                         break;
 222                 }
 223         }
 224 
 225         /*
 226          * add the socket to the sock_array[]..
 227          */
 228         skp = sk->prot->sock_array + num;
 229         cli();
 230         while ((tmp = *skp) != NULL) {
 231                 if (!(tmp->rcv_saddr & mask))
 232                         break;
 233                 skp = &tmp->next;
 234         }
 235         sk->next = tmp;
 236         *skp = sk;
 237         sti();
 238 }
 239 
 240 /*
 241  *      Remove a socket from the socket tables.
 242  */
 243 
 244 static void remove_sock(struct sock *sk1)
     /* [previous][next][first][last][top][bottom][index][help] */
 245 {
 246         struct sock **p;
 247         unsigned long flags;
 248 
 249         if (sk1->type==SOCK_PACKET)
 250                 return;
 251                 
 252         if (!sk1->prot) 
 253         {
 254                 NETDEBUG(printk("sock.c: remove_sock: sk1->prot == NULL\n"));
 255                 return;
 256         }
 257 
 258         /* We can't have this changing out from under us. */
 259         save_flags(flags);
 260         cli();
 261         
 262         p=&(sk1->prot->sock_array[sk1->num & (SOCK_ARRAY_SIZE -1)]);
 263         
 264         while(*p!=NULL)
 265         {
 266                 if(*p==sk1)
 267                 {
 268                         sk1->prot->inuse--;
 269                         *p=sk1->next;
 270                         break;
 271                 }
 272                 p=&((*p)->next);
 273         }
 274         restore_flags(flags);
 275 }
 276 
 277 /*
 278  *      Destroy an AF_INET socket
 279  */
 280  
 281 void destroy_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283         struct sk_buff *skb;
 284 
 285         sk->inuse = 1;                  /* just to be safe. */
 286 
 287         /* 
 288          *      In case it's sleeping somewhere. 
 289          */
 290          
 291         if (!sk->dead) 
 292                 sk->write_space(sk);
 293 
 294         remove_sock(sk);
 295   
 296         /*
 297          *      Now we can no longer get new packets or once the
 298          *      timers are killed, send them.
 299          */
 300          
 301         delete_timer(sk);
 302         del_timer(&sk->retransmit_timer);
 303         
 304         /*
 305          *      Drain any partial frames
 306          */
 307          
 308         while ((skb = tcp_dequeue_partial(sk)) != NULL) 
 309         {
 310                 IS_SKB(skb);
 311                 kfree_skb(skb, FREE_WRITE);
 312         }
 313 
 314         /*
 315          *      Cleanup up the write buffer. 
 316          */
 317          
 318         while((skb = skb_dequeue(&sk->write_queue)) != NULL) {
 319                 IS_SKB(skb);
 320                 kfree_skb(skb, FREE_WRITE);
 321         }
 322         
 323         /*
 324          *      Don't discard received data until the user side kills its
 325          *      half of the socket.
 326          */
 327 
 328         if (sk->dead) 
 329         {
 330                 while((skb=skb_dequeue(&sk->receive_queue))!=NULL) 
 331                 {
 332                 /*
 333                  * This will take care of closing sockets that were
 334                  * listening and didn't accept everything.
 335                  */
 336                         if (skb->sk != NULL && skb->sk != sk) 
 337                         {
 338                                 IS_SKB(skb);
 339                                 skb->sk->dead = 1;
 340                                 skb->sk->prot->close(skb->sk, 0);
 341                         }
 342                         IS_SKB(skb);
 343                         kfree_skb(skb, FREE_READ);
 344                 }
 345         }       
 346 
 347         /*
 348          *      Now we need to clean up the send head. 
 349          */
 350          
 351         cli();
 352         for(skb = sk->send_head; skb != NULL; )
 353         {
 354                 struct sk_buff *skb2;
 355 
 356                 /*
 357                  * We need to remove skb from the transmit queue,
 358                  * or maybe the arp queue.
 359                  */
 360                 if (skb->next  && skb->prev) 
 361                 {
 362                         IS_SKB(skb);
 363                         skb_unlink(skb);
 364                 }
 365                 skb->dev = NULL;
 366                 skb2 = skb->link3;
 367                 kfree_skb(skb, FREE_WRITE);
 368                 skb = skb2;
 369         }
 370         sk->send_head = NULL;
 371         sti();
 372 
 373         /*
 374          *      Now the backlog. 
 375          */
 376          
 377         while((skb=skb_dequeue(&sk->back_log))!=NULL) 
 378         {
 379                 /* this should [almost] never happen. */
 380                 kfree_skb(skb, FREE_READ);
 381         }
 382 
 383         /*
 384          *      Now if it has a half accepted/ closed socket. 
 385          */
 386          
 387         if (sk->pair) 
 388         {
 389                 sk->pair->dead = 1;
 390                 sk->pair->prot->close(sk->pair, 0);
 391                 sk->pair = NULL;
 392         }
 393 
 394         /*
 395          * Now if everything is gone we can free the socket
 396          * structure, otherwise we need to keep it around until
 397          * everything is gone.
 398          */
 399 
 400         if (sk->dead && sk->rmem_alloc == 0 && sk->wmem_alloc == 0) 
 401         {
 402                 if(sk->opt)
 403                         kfree(sk->opt);
 404                 /*
 405                  *      This one is pure paranoia. I'll take it out
 406                  *      later once I know the bug is buried.
 407                  */
 408                 tcp_cache_zap();
 409                 kfree_s((void *)sk,sizeof(*sk));
 410         } 
 411         else 
 412         {
 413                 /* this should never happen. */
 414                 /* actually it can if an ack has just been sent. */
 415                 sk->destroy = 1;
 416                 sk->ack_backlog = 0;
 417                 sk->inuse = 0;
 418                 reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
 419         }
 420 }
 421 
 422 /*
 423  *      The routines beyond this point handle the behaviour of an AF_INET
 424  *      socket object. Mostly it punts to the subprotocols of IP to do
 425  *      the work.
 426  */
 427  
 428 static int inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 429 {
 430         struct sock *sk;
 431 
 432         sk = (struct sock *) sock->data;
 433 
 434         switch(cmd) 
 435         {
 436                 case F_SETOWN:
 437                         /*
 438                          * This is a little restrictive, but it's the only
 439                          * way to make sure that you can't send a sigurg to
 440                          * another process.
 441                          */
 442                         if (!suser() && current->pgrp != -arg &&
 443                                 current->pid != arg) return(-EPERM);
 444                         sk->proc = arg;
 445                         return(0);
 446                 case F_GETOWN:
 447                         return(sk->proc);
 448                 default:
 449                         return(-EINVAL);
 450         }
 451 }
 452 
 453 /*
 454  *      Set socket options on an inet socket.
 455  */
 456  
 457 static int inet_setsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 458                     char *optval, int optlen)
 459 {
 460         struct sock *sk = (struct sock *) sock->data;  
 461         if (level == SOL_SOCKET)
 462                 return sock_setsockopt(sk,level,optname,optval,optlen);
 463         if (sk->prot->setsockopt==NULL)
 464                 return(-EOPNOTSUPP);
 465         else
 466                 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
 467 }
 468 
 469 /*
 470  *      Get a socket option on an AF_INET socket.
 471  */
 472 
 473 static int inet_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 474                     char *optval, int *optlen)
 475 {
 476         struct sock *sk = (struct sock *) sock->data;   
 477         if (level == SOL_SOCKET) 
 478                 return sock_getsockopt(sk,level,optname,optval,optlen);
 479         if(sk->prot->getsockopt==NULL)          
 480                 return(-EOPNOTSUPP);
 481         else
 482                 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
 483 }
 484 
 485 /*
 486  *      Automatically bind an unbound socket.
 487  */
 488 
 489 static int inet_autobind(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 490 {
 491         /* We may need to bind the socket. */
 492         if (sk->num == 0) 
 493         {
 494                 sk->num = get_new_socknum(sk->prot, 0);
 495                 if (sk->num == 0) 
 496                         return(-EAGAIN);
 497                 udp_cache_zap();
 498                 tcp_cache_zap();
 499                 put_sock(sk->num, sk);
 500                 sk->dummy_th.source = ntohs(sk->num);
 501         }
 502         return 0;
 503 }
 504 
 505 /*
 506  *      Move a socket into listening state.
 507  */
 508  
 509 static int inet_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 510 {
 511         struct sock *sk = (struct sock *) sock->data;
 512 
 513         if(inet_autobind(sk)!=0)
 514                 return -EAGAIN;
 515 
 516         /* We might as well re use these. */ 
 517         /*
 518          * note that the backlog is "unsigned char", so truncate it
 519          * somewhere. We might as well truncate it to what everybody
 520          * else does..
 521          * Now truncate to 128 not 5. 
 522          */
 523         if ((unsigned) backlog == 0)    /* BSDism */
 524                 backlog = 1;
 525         if ((unsigned) backlog > SOMAXCONN)
 526                 backlog = SOMAXCONN;
 527         sk->max_ack_backlog = backlog;
 528         if (sk->state != TCP_LISTEN)
 529         {
 530                 sk->ack_backlog = 0;
 531                 sk->state = TCP_LISTEN;
 532         }
 533         return(0);
 534 }
 535 
 536 /*
 537  *      Default callbacks for user INET sockets. These just wake up
 538  *      the user owning the socket.
 539  */
 540 
 541 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 542 {
 543         if(!sk->dead)
 544                 wake_up_interruptible(sk->sleep);
 545 }
 546 
 547 static void def_callback2(struct sock *sk,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 548 {
 549         if(!sk->dead)
 550         {
 551                 wake_up_interruptible(sk->sleep);
 552                 sock_wake_async(sk->socket, 1);
 553         }
 554 }
 555 
 556 static void def_callback3(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 557 {
 558         if(!sk->dead)
 559         {
 560                 wake_up_interruptible(sk->sleep);
 561                 sock_wake_async(sk->socket, 2);
 562         }
 563 }
 564 
 565 /*
 566  *      Create an inet socket.
 567  *
 568  *      FIXME: Gcc would generate much better code if we set the parameters
 569  *      up in in-memory structure order. Gcc68K even more so
 570  */
 571 
 572 static int inet_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 573 {
 574         struct sock *sk;
 575         struct proto *prot;
 576         int err;
 577 
 578         sk = (struct sock *) kmalloc(sizeof(*sk), GFP_KERNEL);
 579         if (sk == NULL) 
 580                 return(-ENOBUFS);
 581         memset(sk,0,sizeof(*sk));       /* Efficient way to set most fields to zero */
 582 /*      sk->num = 0;
 583  *      sk->reuse = 0;*/
 584         switch(sock->type) 
 585         {
 586                 case SOCK_STREAM:
 587                 case SOCK_SEQPACKET:
 588                         if (protocol && protocol != IPPROTO_TCP) 
 589                         {
 590                                 kfree_s((void *)sk, sizeof(*sk));
 591                                 return(-EPROTONOSUPPORT);
 592                         }
 593                         protocol = IPPROTO_TCP;
 594                         sk->no_check = TCP_NO_CHECK;
 595                         prot = &tcp_prot;
 596                         break;
 597 
 598                 case SOCK_DGRAM:
 599                         if (protocol && protocol != IPPROTO_UDP) 
 600                         {
 601                                 kfree_s((void *)sk, sizeof(*sk));
 602                                 return(-EPROTONOSUPPORT);
 603                         }
 604                         protocol = IPPROTO_UDP;
 605                         sk->no_check = UDP_NO_CHECK;
 606                         prot=&udp_prot;
 607                         break;
 608       
 609                 case SOCK_RAW:
 610                         if (!suser()) 
 611                         {
 612                                 kfree_s((void *)sk, sizeof(*sk));
 613                                 return(-EPERM);
 614                         }
 615                         if (!protocol) 
 616                         {
 617                                 kfree_s((void *)sk, sizeof(*sk));
 618                                 return(-EPROTONOSUPPORT);
 619                         }
 620                         prot = &raw_prot;
 621                         sk->reuse = 1;
 622                         sk->num = protocol;
 623                         break;
 624 
 625                 case SOCK_PACKET:
 626                         if (!suser()) 
 627                         {
 628                                 kfree_s((void *)sk, sizeof(*sk));
 629                                 return(-EPERM);
 630                         }
 631                         if (!protocol) 
 632                         {
 633                                 kfree_s((void *)sk, sizeof(*sk));
 634                                 return(-EPROTONOSUPPORT);
 635                         }
 636                         prot = &packet_prot;
 637                         sk->reuse = 1;
 638                         sk->num = protocol;
 639                         break;
 640 
 641                 default:
 642                         kfree_s((void *)sk, sizeof(*sk));
 643                         return(-ESOCKTNOSUPPORT);
 644         }
 645         sk->socket = sock;
 646 #ifdef CONFIG_TCP_NAGLE_OFF
 647         sk->nonagle = 1;
 648 #else    
 649 /*      sk->nonagle = 0;*/
 650 #endif  
 651         sk->type = sock->type;
 652         sk->protocol = protocol;
 653         sk->allocation = GFP_KERNEL;
 654         sk->sndbuf = SK_WMEM_MAX;
 655         sk->rcvbuf = SK_RMEM_MAX;
 656         sk->rto = TCP_TIMEOUT_INIT;             /*TCP_WRITE_TIME*/
 657         sk->cong_window = 1; /* start with only sending one packet at a time. */
 658         sk->priority = 1;
 659         sk->state = TCP_CLOSE;
 660 #ifdef WHAT_WE_DO_THE_MEMZERO_INSTEAD_OF        
 661         sk->stamp.tv_sec=0;
 662         sk->wmem_alloc = 0;
 663         sk->rmem_alloc = 0;
 664         sk->pair = NULL;
 665         sk->opt = NULL;
 666         sk->write_seq = 0;
 667         sk->acked_seq = 0;
 668         sk->copied_seq = 0;
 669         sk->fin_seq = 0;
 670         sk->urg_seq = 0;
 671         sk->urg_data = 0;
 672         sk->proc = 0;
 673         sk->rtt = 0;                            /*TCP_WRITE_TIME << 3;*/
 674         sk->mdev = 0;
 675         sk->backoff = 0;
 676         sk->packets_out = 0;
 677         sk->cong_count = 0;
 678         sk->ssthresh = 0;
 679         sk->max_window = 0;
 680         sk->urginline = 0;
 681         sk->intr = 0;
 682         sk->linger = 0;
 683         sk->destroy = 0;
 684         sk->shutdown = 0;
 685         sk->keepopen = 0;
 686         sk->zapped = 0;
 687         sk->done = 0;
 688         sk->ack_backlog = 0;
 689         sk->window = 0;
 690         sk->bytes_rcv = 0;
 691         sk->dead = 0;
 692         sk->ack_timed = 0;
 693         sk->partial = NULL;
 694         sk->user_mss = 0;
 695         sk->debug = 0;
 696         /* how many packets we should send before forcing an ack. 
 697            if this is set to zero it is the same as sk->delay_acks = 0 */
 698         sk->max_ack_backlog = 0;
 699         sk->inuse = 0;
 700         sk->delay_acks = 0;
 701         sk->daddr = 0;
 702         sk->saddr = 0;
 703         sk->rcv_saddr = 0;
 704         sk->err = 0;
 705         sk->next = NULL;
 706         sk->pair = NULL;
 707         sk->send_tail = NULL;
 708         sk->send_head = NULL;
 709         sk->timeout = 0;
 710         sk->broadcast = 0;
 711         sk->localroute = 0;
 712         sk->blog = 0;
 713         sk->dummy_th.res1=0;
 714         sk->dummy_th.res2=0;
 715         sk->dummy_th.urg_ptr = 0;
 716         sk->dummy_th.fin = 0;
 717         sk->dummy_th.syn = 0;
 718         sk->dummy_th.rst = 0;
 719         sk->dummy_th.psh = 0;
 720         sk->dummy_th.ack = 0;
 721         sk->dummy_th.urg = 0;
 722         sk->dummy_th.dest = 0;
 723         sk->ip_tos=0;
 724         sk->ip_route_cache=NULL;
 725         sk->ip_hcache_ver= 0;
 726         sk->ip_option_len=0;
 727         sk->ip_option_flen=0;
 728         sk->ip_opt_next_hop=0;
 729         sk->ip_opt_ptr[0]=NULL;
 730         sk->ip_opt_ptr[1]=NULL;
 731 #endif  
 732 
 733         /* this is how many unacked bytes we will accept for this socket.  */
 734         sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
 735 
 736         skb_queue_head_init(&sk->write_queue);
 737         skb_queue_head_init(&sk->receive_queue);
 738         sk->mtu = 576;
 739         sk->prot = prot;
 740         sk->sleep = sock->wait;
 741         init_timer(&sk->timer);
 742         init_timer(&sk->retransmit_timer);
 743         sk->timer.data = (unsigned long)sk;
 744         sk->timer.function = &net_timer;
 745         skb_queue_head_init(&sk->back_log);
 746         sock->data =(void *) sk;
 747         sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 748         sk->ip_ttl=64;
 749         if(sk->type==SOCK_RAW && protocol==IPPROTO_RAW)
 750                 sk->ip_hdrincl=1;
 751         else
 752                 sk->ip_hdrincl=0;
 753 #ifdef CONFIG_IP_MULTICAST
 754         sk->ip_mc_loop=1;
 755         sk->ip_mc_ttl=1;
 756         *sk->ip_mc_name=0;
 757         sk->ip_mc_list=NULL;
 758 #endif
 759         
 760         sk->state_change = def_callback1;
 761         sk->data_ready = def_callback2;
 762         sk->write_space = def_callback3;
 763         sk->error_report = def_callback1;
 764 
 765         if (sk->num) 
 766         {
 767         /*
 768          * It assumes that any protocol which allows
 769          * the user to assign a number at socket
 770          * creation time automatically
 771          * shares.
 772          */
 773                 put_sock(sk->num, sk);
 774                 sk->dummy_th.source = ntohs(sk->num);
 775         }
 776 
 777         if (sk->prot->init) 
 778         {
 779                 err = sk->prot->init(sk);
 780                 if (err != 0) 
 781                 {
 782                         destroy_sock(sk);
 783                         return(err);
 784                 }
 785         }
 786         return(0);
 787 }
 788 
 789 
 790 /*
 791  *      Duplicate a socket.
 792  */
 793  
 794 static int inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 795 {
 796         return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
 797 }
 798 
 799 /*
 800  *      Return 1 if we still have things to send in our buffers.
 801  */
 802  
 803 static inline int closing(struct sock * sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 804 {
 805         switch (sk->state) {
 806                 case TCP_FIN_WAIT1:
 807                 case TCP_CLOSING:
 808                 case TCP_LAST_ACK:
 809                         return 1;
 810         }
 811         return 0;
 812 }
 813 
 814 
 815 /*
 816  *      The peer socket should always be NULL (or else). When we call this
 817  *      function we are destroying the object and from then on nobody
 818  *      should refer to it.
 819  */
 820  
 821 static int inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 822 {
 823         struct sock *sk = (struct sock *) sock->data;
 824         if (sk == NULL) 
 825                 return(0);
 826 
 827         sk->state_change(sk);
 828 
 829         /* Start closing the connection.  This may take a while. */
 830 
 831 #ifdef CONFIG_IP_MULTICAST
 832         /* Applications forget to leave groups before exiting */
 833         ip_mc_drop_socket(sk);
 834 #endif
 835         /*
 836          * If linger is set, we don't return until the close
 837          * is complete.  Otherwise we return immediately. The
 838          * actually closing is done the same either way.
 839          *
 840          * If the close is due to the process exiting, we never
 841          * linger..
 842          */
 843 
 844         if (sk->linger == 0 || (current->flags & PF_EXITING))
 845         {
 846                 sk->prot->close(sk,0);
 847                 sk->dead = 1;
 848         } 
 849         else 
 850         {
 851                 sk->prot->close(sk, 0);
 852                 cli();
 853                 if (sk->lingertime)
 854                         current->timeout = jiffies + HZ*sk->lingertime;
 855                 while(closing(sk) && current->timeout>0) 
 856                 {
 857                         interruptible_sleep_on(sk->sleep);
 858                         if (current->signal & ~current->blocked) 
 859                         {
 860                                 break;
 861 #if 0
 862                                 /* not working now - closes can't be restarted */
 863                                 sti();
 864                                 current->timeout=0;
 865                                 return(-ERESTARTSYS);
 866 #endif
 867                         }
 868                 }
 869                 current->timeout=0;
 870                 sti();
 871                 sk->dead = 1;
 872         }
 873         sk->inuse = 1;
 874 
 875         /* This will destroy it. */
 876         sock->data = NULL;
 877         /* 
 878          *      Nasty here. release_sock can cause more frames
 879          *      to be played through the socket. That can
 880          *      reinitialise the tcp cache after tcp_close();
 881          */
 882         release_sock(sk);
 883         tcp_cache_zap();        /* Kill the cache again. */
 884         sk->socket = NULL;
 885         return(0);
 886 }
 887 
 888 
 889 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 890                int addr_len)
 891 {
 892         struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
 893         struct sock *sk=(struct sock *)sock->data, *sk2;
 894         unsigned short snum = 0 /* Stoopid compiler.. this IS ok */;
 895         int chk_addr_ret;
 896 
 897         /* check this error. */
 898         if (sk->state != TCP_CLOSE)
 899                 return(-EIO);
 900         if(addr_len<sizeof(struct sockaddr_in))
 901                 return -EINVAL;
 902                 
 903         if(sock->type != SOCK_RAW)
 904         {
 905                 if (sk->num != 0) 
 906                         return(-EINVAL);
 907 
 908                 snum = ntohs(addr->sin_port);
 909                 
 910 #ifdef CONFIG_IP_MASQUERADE
 911                 /*
 912                  *      The kernel masquerader needs some ports
 913                  */             
 914                 if(snum>=PORT_MASQ_BEGIN && snum<=PORT_MASQ_END)
 915                         return -EADDRINUSE;
 916 #endif           
 917 
 918                 if (snum == 0) 
 919                         snum = get_new_socknum(sk->prot, 0);
 920                 if (snum < PROT_SOCK && !suser()) 
 921                         return(-EACCES);
 922         }
 923         
 924         chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
 925         if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR && chk_addr_ret != IS_MULTICAST && chk_addr_ret != IS_BROADCAST)
 926                 return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
 927 
 928         if (chk_addr_ret || addr->sin_addr.s_addr == 0)
 929         {
 930                 /*
 931                  *      We keep a pair of addresses. rcv_saddr is the one
 932                  *      used by get_sock_*(), and saddr is used for transmit.
 933                  *
 934                  *      In the BSD API these are the same except where it
 935                  *      would be illegal to use (multicast/broadcast) in
 936                  *      which case the sending device address is used.
 937                  */
 938                 sk->rcv_saddr = addr->sin_addr.s_addr;
 939                 if(chk_addr_ret==IS_MULTICAST||chk_addr_ret==IS_BROADCAST)
 940                         sk->saddr = 0;  /* Use device */
 941                 else
 942                         sk->saddr = addr->sin_addr.s_addr;
 943         }
 944         if(sock->type != SOCK_RAW)
 945         {
 946                 /* Make sure we are allowed to bind here. */
 947                 cli();
 948                 for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
 949                                         sk2 != NULL; sk2 = sk2->next) 
 950                 {
 951                 /* should be below! */
 952                         if (sk2->num != snum) 
 953                                 continue;
 954                         if (!sk->reuse)
 955                         {
 956                                 sti();
 957                                 return(-EADDRINUSE);
 958                         }
 959                         
 960                         if (sk2->num != snum) 
 961                                 continue;               /* more than one */
 962                         if (sk2->rcv_saddr != sk->rcv_saddr) 
 963                                 continue;       /* socket per slot ! -FB */
 964                         if (!sk2->reuse || sk2->state==TCP_LISTEN) 
 965                         {
 966                                 sti();
 967                                 return(-EADDRINUSE);
 968                         }
 969                 }
 970                 sti();
 971 
 972                 remove_sock(sk);
 973                 if(sock->type==SOCK_DGRAM)
 974                         udp_cache_zap();
 975                 if(sock->type==SOCK_STREAM)
 976                         tcp_cache_zap();
 977                 put_sock(snum, sk);
 978                 sk->dummy_th.source = ntohs(sk->num);
 979                 sk->daddr = 0;
 980                 sk->dummy_th.dest = 0;
 981         }
 982         sk->ip_route_cache=NULL;
 983         return(0);
 984 }
 985 
 986 /*
 987  *      Handle sk->err properly. The cli/sti matter.
 988  */
 989  
 990 static int inet_error(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 991 {
 992         unsigned long flags;
 993         int err;
 994         save_flags(flags);
 995         cli();  
 996         err=sk->err;
 997         sk->err=0;
 998         restore_flags(flags);
 999         return -err;
1000 }
1001 
1002 /*
1003  *      Connect to a remote host. There is regrettably still a little
1004  *      TCP 'magic' in here.
1005  */
1006  
1007 static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1008                   int addr_len, int flags)
1009 {
1010         struct sock *sk=(struct sock *)sock->data;
1011         int err;
1012         sock->conn = NULL;
1013 
1014         if (sock->state == SS_CONNECTING && tcp_connected(sk->state))
1015         {
1016                 sock->state = SS_CONNECTED;
1017                 /* Connection completing after a connect/EINPROGRESS/select/connect */
1018                 return 0;       /* Rock and roll */
1019         }
1020 
1021         if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK))
1022         {
1023                 if(sk->err!=0)
1024                         return inet_error(sk);
1025                 return -EALREADY;       /* Connecting is currently in progress */
1026         }
1027         if (sock->state != SS_CONNECTING) 
1028         {
1029                 /* We may need to bind the socket. */
1030                 if(inet_autobind(sk)!=0)
1031                         return(-EAGAIN);
1032                 if (sk->prot->connect == NULL) 
1033                         return(-EOPNOTSUPP);
1034                 err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
1035                 if (err < 0) 
1036                         return(err);
1037                 sock->state = SS_CONNECTING;
1038         }
1039         
1040         if (sk->state > TCP_FIN_WAIT2 && sock->state==SS_CONNECTING)
1041         {
1042                 sock->state=SS_UNCONNECTED;
1043                 return inet_error(sk);
1044         }
1045 
1046         if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
1047                 return(-EINPROGRESS);
1048 
1049         cli(); /* avoid the race condition */
1050         while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
1051         {
1052                 interruptible_sleep_on(sk->sleep);
1053                 if (current->signal & ~current->blocked) 
1054                 {
1055                         sti();
1056                         return(-ERESTARTSYS);
1057                 }
1058                 /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
1059                    icmp error packets wanting to close a tcp or udp socket. */
1060                 if(sk->err && sk->protocol == IPPROTO_TCP)
1061                 {
1062                         sock->state = SS_UNCONNECTED;
1063                         sti();
1064                         return inet_error(sk); /* set by tcp_err() */
1065                 }
1066         }
1067         sti();
1068         sock->state = SS_CONNECTED;
1069 
1070         if (sk->state != TCP_ESTABLISHED && sk->err) 
1071         {
1072                 sock->state = SS_UNCONNECTED;
1073                 return inet_error(sk);
1074         }
1075         return(0);
1076 }
1077 
1078 
1079 static int inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1080 {
1081          return(-EOPNOTSUPP);
1082 }
1083 
1084 
1085 /*
1086  *      Accept a pending connection. The TCP layer now gives BSD semantics.
1087  */
1088 
1089 static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1090 {
1091         struct sock *sk1, *sk2;
1092         int err;
1093 
1094         sk1 = (struct sock *) sock->data;
1095 
1096         /*
1097          * We've been passed an extra socket.
1098          * We need to free it up because the tcp module creates
1099          * its own when it accepts one.
1100          */
1101         if (newsock->data)
1102         {
1103                 struct sock *sk=(struct sock *)newsock->data;
1104                 newsock->data=NULL;
1105                 sk->dead = 1;
1106                 destroy_sock(sk);
1107         }
1108   
1109         if (sk1->prot->accept == NULL) 
1110                 return(-EOPNOTSUPP);
1111 
1112         /* Restore the state if we have been interrupted, and then returned. */
1113         if (sk1->pair != NULL ) 
1114         {
1115                 sk2 = sk1->pair;
1116                 sk1->pair = NULL;
1117         } 
1118         else
1119         {
1120                 sk2 = sk1->prot->accept(sk1,flags);
1121                 if (sk2 == NULL) 
1122                 {
1123                         return inet_error(sk1);
1124                 }
1125         }
1126         newsock->data = (void *)sk2;
1127         sk2->sleep = newsock->wait;
1128         sk2->socket = newsock;
1129         newsock->conn = NULL;
1130         if (flags & O_NONBLOCK) 
1131                 return(0);
1132 
1133         cli(); /* avoid the race. */
1134         while(sk2->state == TCP_SYN_RECV) 
1135         {
1136                 interruptible_sleep_on(sk2->sleep);
1137                 if (current->signal & ~current->blocked) 
1138                 {
1139                         sti();
1140                         sk1->pair = sk2;
1141                         sk2->sleep = NULL;
1142                         sk2->socket=NULL;
1143                         newsock->data = NULL;
1144                         return(-ERESTARTSYS);
1145                 }
1146         }
1147         sti();
1148 
1149         if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) 
1150         {
1151                 err = inet_error(sk2);
1152                 sk2->dead=1;
1153                 destroy_sock(sk2);
1154                 newsock->data = NULL;
1155                 return err;
1156         }
1157         newsock->state = SS_CONNECTED;
1158         return(0);
1159 }
1160 
1161 
1162 /*
1163  *      This does both peername and sockname.
1164  */
1165  
1166 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1167                  int *uaddr_len, int peer)
1168 {
1169         struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
1170         struct sock *sk;
1171   
1172         sin->sin_family = AF_INET;
1173         sk = (struct sock *) sock->data;
1174         if (peer) 
1175         {
1176                 if (!tcp_connected(sk->state)) 
1177                         return(-ENOTCONN);
1178                 sin->sin_port = sk->dummy_th.dest;
1179                 sin->sin_addr.s_addr = sk->daddr;
1180         } 
1181         else 
1182         {
1183                 __u32 addr = sk->rcv_saddr;
1184                 if (!addr) {
1185                         addr = sk->saddr;
1186                         if (!addr)
1187                                 addr = ip_my_addr();
1188                 }
1189                 sin->sin_port = sk->dummy_th.source;
1190                 sin->sin_addr.s_addr = addr;
1191         }
1192         *uaddr_len = sizeof(*sin);
1193         return(0);
1194 }
1195 
1196 
1197 /*
1198  *      The assorted BSD I/O operations
1199  */
1200 
1201 static int inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1202                    unsigned flags, struct sockaddr *sin, int *addr_len )
1203 {
1204         struct sock *sk = (struct sock *) sock->data;
1205         
1206         if (sk->prot->recvfrom == NULL) 
1207                 return(-EOPNOTSUPP);
1208         if(sk->err)
1209                 return inet_error(sk);
1210         /* We may need to bind the socket. */
1211         if(inet_autobind(sk)!=0)
1212                 return(-EAGAIN);
1213         return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1214                              (struct sockaddr_in*)sin, addr_len));
1215 }
1216 
1217 static int inet_recvmsg(struct socket *sock, struct msghdr *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1218                    int flags, int *addr_len )
1219 {
1220         struct sock *sk = (struct sock *) sock->data;
1221         
1222         if (sk->prot->recvmsg == NULL) 
1223                 return(-EOPNOTSUPP);
1224         if(sk->err)
1225                 return inet_error(sk);
1226         /* We may need to bind the socket. */
1227         if(inet_autobind(sk)!=0)
1228                 return(-EAGAIN);
1229         return(sk->prot->recvmsg(sk, ubuf, size, noblock, flags,addr_len));
1230 }
1231 
1232 
1233 static int inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1234           unsigned flags)
1235 {
1236         /* BSD explicitly states these are the same - so we do it this way to be sure */
1237         return inet_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
1238 }
1239 
1240 static int inet_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1241 {
1242         struct sock *sk = (struct sock *) sock->data;
1243         
1244         if(sk->err)
1245                 return inet_error(sk);
1246         /* We may need to bind the socket. */
1247         if(inet_autobind(sk))
1248                 return(-EAGAIN);        
1249         return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, 0));
1250 }
1251 
1252 static int inet_send(struct socket *sock, const void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1253                unsigned flags)
1254 {
1255         struct sock *sk = (struct sock *) sock->data;
1256         if (sk->shutdown & SEND_SHUTDOWN) 
1257         {
1258                 send_sig(SIGPIPE, current, 1);
1259                 return(-EPIPE);
1260         }
1261         if(sk->err)
1262                 return inet_error(sk);
1263         /* We may need to bind the socket. */
1264         if(inet_autobind(sk)!=0)
1265                 return(-EAGAIN);
1266         return(sk->prot->write(sk, (const unsigned char *) ubuf, size, noblock, flags));
1267 }
1268 
1269 static int inet_write(struct socket *sock, const char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1270 {
1271         return inet_send(sock,ubuf,size,noblock,0);
1272 }
1273 
1274 static int inet_sendto(struct socket *sock, const void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1275             unsigned flags, struct sockaddr *sin, int addr_len)
1276 {
1277         struct sock *sk = (struct sock *) sock->data;
1278         if (sk->shutdown & SEND_SHUTDOWN) 
1279         {
1280                 send_sig(SIGPIPE, current, 1);
1281                 return(-EPIPE);
1282         }
1283         if (sk->prot->sendto == NULL) 
1284                 return(-EOPNOTSUPP);
1285         if(sk->err)
1286                 return inet_error(sk);
1287         /* We may need to bind the socket. */
1288         if(inet_autobind(sk)!=0)
1289                 return -EAGAIN;
1290         return(sk->prot->sendto(sk, (const unsigned char *) ubuf, size, noblock, flags, 
1291                            (struct sockaddr_in *)sin, addr_len));
1292 }
1293 
1294 static int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1295            int flags)
1296 {
1297         struct sock *sk = (struct sock *) sock->data;
1298         if (sk->shutdown & SEND_SHUTDOWN) 
1299         {
1300                 send_sig(SIGPIPE, current, 1);
1301                 return(-EPIPE);
1302         }
1303         if (sk->prot->sendmsg == NULL) 
1304                 return(-EOPNOTSUPP);
1305         if(sk->err)
1306                 return inet_error(sk);
1307         /* We may need to bind the socket. */
1308         if(inet_autobind(sk)!=0)
1309                 return -EAGAIN;
1310         return(sk->prot->sendmsg(sk, msg, size, noblock, flags));
1311                            
1312 }
1313 
1314 
1315 static int inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1316 {
1317         struct sock *sk=(struct sock*)sock->data;
1318 
1319         /*
1320          * This should really check to make sure
1321          * the socket is a TCP socket. (WHY AC...)
1322          */
1323         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1324                        1->2 bit 2 snds.
1325                        2->3 */
1326         if ((how & ~SHUTDOWN_MASK) || how==0)   /* MAXINT->0 */
1327                 return(-EINVAL);
1328         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1329                 sock->state = SS_CONNECTED;
1330         if (!tcp_connected(sk->state)) 
1331                 return(-ENOTCONN);
1332         sk->shutdown |= how;
1333         if (sk->prot->shutdown)
1334                 sk->prot->shutdown(sk, how);
1335         return(0);
1336 }
1337 
1338 
1339 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1340 {
1341         struct sock *sk=(struct sock *) sock->data;
1342         if (sk->prot->select == NULL) 
1343         {
1344                 return(0);
1345         }
1346         return(sk->prot->select(sk, sel_type, wait));
1347 }
1348 
1349 /*
1350  *      ioctl() calls you can issue on an INET socket. Most of these are
1351  *      device configuration and stuff and very rarely used. Some ioctls
1352  *      pass on to the socket itself.
1353  *
1354  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
1355  *      loads the devconfigure module does its configuring and unloads it.
1356  *      There's a good 20K of config code hanging around the kernel.
1357  */
1358 
1359 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1360 {
1361         struct sock *sk=(struct sock *)sock->data;
1362         int err;
1363         int pid;
1364 
1365         switch(cmd) 
1366         {
1367                 case FIOSETOWN:
1368                 case SIOCSPGRP:
1369                         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1370                         if(err)
1371                                 return err;
1372                         pid = get_user((int *) arg);
1373                         /* see inet_fcntl */
1374                         if (current->pid != pid && current->pgrp != -pid && !suser())
1375                                 return -EPERM;
1376                         sk->proc = pid;
1377                         return(0);
1378                 case FIOGETOWN:
1379                 case SIOCGPGRP:
1380                         err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1381                         if(err)
1382                                 return err;
1383                         put_fs_long(sk->proc,(int *)arg);
1384                         return(0);                      
1385                 case SIOCGSTAMP:
1386                         if(sk->stamp.tv_sec==0)
1387                                 return -ENOENT;
1388                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1389                         if(err)
1390                                 return err;
1391                         memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1392                         return 0;
1393                 case SIOCADDRT:
1394                 case SIOCDELRT:
1395                         return(ip_rt_ioctl(cmd,(void *) arg));
1396                 case SIOCDARP:
1397                 case SIOCGARP:
1398                 case SIOCSARP:
1399                         return(arp_ioctl(cmd,(void *) arg));
1400                 case SIOCDRARP:
1401                 case SIOCGRARP:
1402                 case SIOCSRARP:
1403                         if (rarp_ioctl_hook != NULL)
1404                                 return(rarp_ioctl_hook(cmd,(void *) arg));
1405                 case SIOCGIFCONF:
1406                 case SIOCGIFFLAGS:
1407                 case SIOCSIFFLAGS:
1408                 case SIOCGIFADDR:
1409                 case SIOCSIFADDR:
1410                 case SIOCADDMULTI:
1411                 case SIOCDELMULTI:
1412                 case SIOCGIFDSTADDR:
1413                 case SIOCSIFDSTADDR:
1414                 case SIOCGIFBRDADDR:
1415                 case SIOCSIFBRDADDR:
1416                 case SIOCGIFNETMASK:
1417                 case SIOCSIFNETMASK:
1418                 case SIOCGIFMETRIC:
1419                 case SIOCSIFMETRIC:
1420                 case SIOCGIFMEM:
1421                 case SIOCSIFMEM:
1422                 case SIOCGIFMTU:
1423                 case SIOCSIFMTU:
1424                 case SIOCSIFLINK:
1425                 case SIOCGIFHWADDR:
1426                 case SIOCSIFHWADDR:
1427                 case OLD_SIOCGIFHWADDR:
1428                 case SIOCSIFMAP:
1429                 case SIOCGIFMAP:
1430                 case SIOCSIFSLAVE:
1431                 case SIOCGIFSLAVE:
1432                         return(dev_ioctl(cmd,(void *) arg));
1433 
1434                 default:
1435                         if ((cmd >= SIOCDEVPRIVATE) &&
1436                            (cmd <= (SIOCDEVPRIVATE + 15)))
1437                                 return(dev_ioctl(cmd,(void *) arg));
1438 
1439                         if (sk->prot->ioctl==NULL) 
1440                                 return(-EINVAL);
1441                         return(sk->prot->ioctl(sk, cmd, arg));
1442         }
1443         /*NOTREACHED*/
1444         return(0);
1445 }
1446 
1447 /*
1448  * This routine must find a socket given a TCP or UDP header.
1449  * Everything is assumed to be in net order.
1450  *
1451  * We give priority to more closely bound ports: if some socket
1452  * is bound to a particular foreign address, it will get the packet
1453  * rather than somebody listening to any address..
1454  */
1455 
1456 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1457                                 unsigned long raddr,
1458                                 unsigned short rnum, unsigned long laddr)
1459 {
1460         struct sock *s;
1461         struct sock *result = NULL;
1462         int badness = -1;
1463         unsigned short hnum;
1464 
1465         hnum = ntohs(num);
1466 
1467         /*
1468          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1469          * than a prime unless 3 or more sockets end up using the same
1470          * array entry.  This should not be a problem because most
1471          * well known sockets don't overlap that much, and for
1472          * the other ones, we can just be careful about picking our
1473          * socket number when we choose an arbitrary one.
1474          */
1475 
1476         for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1477                         s != NULL; s = s->next) 
1478         {
1479                 int score = 0;
1480 
1481                 if (s->num != hnum) 
1482                         continue;
1483 
1484                 if(s->dead && (s->state == TCP_CLOSE))
1485                         continue;
1486                 /* local address matches? */
1487                 if (s->rcv_saddr) {
1488                         if (s->rcv_saddr != laddr)
1489                                 continue;
1490                         score++;
1491                 }
1492                 /* remote address matches? */
1493                 if (s->daddr) {
1494                         if (s->daddr != raddr)
1495                                 continue;
1496                         score++;
1497                 }
1498                 /* remote port matches? */
1499                 if (s->dummy_th.dest) {
1500                         if (s->dummy_th.dest != rnum)
1501                                 continue;
1502                         score++;
1503                 }
1504                 /* perfect match? */
1505                 if (score == 3)
1506                         return s;
1507                 /* no, check if this is the best so far.. */
1508                 if (score <= badness)
1509                         continue;
1510                 result = s;
1511                 badness = score;
1512         }
1513         return result;
1514 }
1515 
1516 /*
1517  *      Deliver a datagram to raw sockets.
1518  */
1519  
1520 struct sock *get_sock_raw(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1521                                 unsigned short num,
1522                                 unsigned long raddr,
1523                                 unsigned long laddr)
1524 {
1525         struct sock *s;
1526 
1527         s=sk;
1528 
1529         for(; s != NULL; s = s->next) 
1530         {
1531                 if (s->num != num) 
1532                         continue;
1533                 if(s->dead && (s->state == TCP_CLOSE))
1534                         continue;
1535                 if(s->daddr && s->daddr!=raddr)
1536                         continue;
1537                 if(s->rcv_saddr && s->rcv_saddr != laddr)
1538                         continue;
1539                 return(s);
1540         }
1541         return(NULL);
1542 }
1543 
1544 #ifdef CONFIG_IP_MULTICAST
1545 /*
1546  *      Deliver a datagram to broadcast/multicast sockets.
1547  */
1548  
1549 struct sock *get_sock_mcast(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1550                                 unsigned short num,
1551                                 unsigned long raddr,
1552                                 unsigned short rnum, unsigned long laddr)
1553 {
1554         struct sock *s;
1555         unsigned short hnum;
1556 
1557         hnum = ntohs(num);
1558 
1559         /*
1560          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1561          * than a prime unless 3 or more sockets end up using the same
1562          * array entry.  This should not be a problem because most
1563          * well known sockets don't overlap that much, and for
1564          * the other ones, we can just be careful about picking our
1565          * socket number when we choose an arbitrary one.
1566          */
1567         
1568         s=sk;
1569 
1570         for(; s != NULL; s = s->next) 
1571         {
1572                 if (s->num != hnum) 
1573                         continue;
1574                 if(s->dead && (s->state == TCP_CLOSE))
1575                         continue;
1576                 if(s->daddr && s->daddr!=raddr)
1577                         continue;
1578                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1579                         continue;
1580                 if(s->rcv_saddr  && s->rcv_saddr != laddr)
1581                         continue;
1582                 return(s);
1583         }
1584         return(NULL);
1585 }
1586 
1587 #endif
1588 
1589 static struct proto_ops inet_proto_ops = {
1590         AF_INET,
1591 
1592         inet_create,
1593         inet_dup,
1594         inet_release,
1595         inet_bind,
1596         inet_connect,
1597         inet_socketpair,
1598         inet_accept,
1599         inet_getname, 
1600         inet_read,
1601         inet_write,
1602         inet_select,
1603         inet_ioctl,
1604         inet_listen,
1605         inet_send,
1606         inet_recv,
1607         inet_sendto,
1608         inet_recvfrom,
1609         inet_shutdown,
1610         inet_setsockopt,
1611         inet_getsockopt,
1612         inet_fcntl,
1613         inet_sendmsg,
1614         inet_recvmsg
1615 };
1616 
1617 extern unsigned long seq_offset;
1618 
1619 /*
1620  *      Called by socket.c on kernel startup.  
1621  */
1622  
1623 void inet_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1624 {
1625         struct inet_protocol *p;
1626         int i;
1627 
1628 
1629         printk("Swansea University Computer Society TCP/IP for NET3.032\n");
1630 
1631         /*
1632          *      Tell SOCKET that we are alive... 
1633          */
1634    
1635         (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1636 
1637         seq_offset = CURRENT_TIME*250;
1638 
1639         /*
1640          *      Add all the protocols. 
1641          */
1642          
1643         for(i = 0; i < SOCK_ARRAY_SIZE; i++) 
1644         {
1645                 tcp_prot.sock_array[i] = NULL;
1646                 udp_prot.sock_array[i] = NULL;
1647                 raw_prot.sock_array[i] = NULL;
1648         }
1649         tcp_prot.inuse = 0;
1650         tcp_prot.highestinuse = 0;
1651         udp_prot.inuse = 0;
1652         udp_prot.highestinuse = 0;
1653         raw_prot.inuse = 0;
1654         raw_prot.highestinuse = 0;
1655 
1656         printk("IP Protocols: ");
1657         for(p = inet_protocol_base; p != NULL;) 
1658         {
1659                 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1660                 inet_add_protocol(p);
1661                 printk("%s%s",p->name,tmp?", ":"\n");
1662                 p = tmp;
1663         }
1664 
1665         /*
1666          *      Set the ARP module up
1667          */
1668         arp_init();
1669         /*
1670          *      Set the IP module up
1671          */
1672         ip_init();
1673         /*
1674          *      Set the ICMP layer up
1675          */
1676         icmp_init(&inet_proto_ops);
1677         /*
1678          *      Set the firewalling up
1679          */
1680 #if defined(CONFIG_IP_ACCT)||defined(CONFIG_IP_FIREWALL)|| \
1681     defined(CONFIG_IP_MASQUERADE)
1682         ip_fw_init();
1683 #endif
1684         /*
1685          *      Initialise the multicast router
1686          */
1687 #if defined(CONFIG_IP_MROUTE)
1688         ip_mr_init();
1689 #endif
1690 
1691         /*
1692          *      Create all the /proc entries.
1693          */
1694 
1695 #ifdef CONFIG_INET_RARP
1696         proc_net_register(&(struct proc_dir_entry) {
1697                 PROC_NET_RARP, 4, "rarp",
1698                 S_IFREG | S_IRUGO, 1, 0, 0,
1699                 0, &proc_net_inode_operations,
1700                 rarp_get_info
1701         });
1702         rarp_ioctl_hook = rarp_ioctl;
1703 #endif
1704         proc_net_register(&(struct proc_dir_entry) {
1705                 PROC_NET_RAW, 3, "raw",
1706                 S_IFREG | S_IRUGO, 1, 0, 0,
1707                 0, &proc_net_inode_operations,
1708                 raw_get_info
1709         });
1710         proc_net_register(&(struct proc_dir_entry) {
1711                 PROC_NET_SNMP, 4, "snmp",
1712                 S_IFREG | S_IRUGO, 1, 0, 0,
1713                 0, &proc_net_inode_operations,
1714                 snmp_get_info
1715         });
1716         proc_net_register(&(struct proc_dir_entry) {
1717                 PROC_NET_SOCKSTAT, 8, "sockstat",
1718                 S_IFREG | S_IRUGO, 1, 0, 0,
1719                 0, &proc_net_inode_operations,
1720                 afinet_get_info
1721         });
1722         proc_net_register(&(struct proc_dir_entry) {
1723                 PROC_NET_TCP, 3, "tcp",
1724                 S_IFREG | S_IRUGO, 1, 0, 0,
1725                 0, &proc_net_inode_operations,
1726                 tcp_get_info
1727         });
1728         proc_net_register(&(struct proc_dir_entry) {
1729                 PROC_NET_UDP, 3, "udp",
1730                 S_IFREG | S_IRUGO, 1, 0, 0,
1731                 0, &proc_net_inode_operations,
1732                 udp_get_info
1733         });
1734         proc_net_register(&(struct proc_dir_entry) {
1735                 PROC_NET_ROUTE, 5, "route",
1736                 S_IFREG | S_IRUGO, 1, 0, 0,
1737                 0, &proc_net_inode_operations,
1738                 rt_get_info
1739         });
1740 }

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