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

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