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

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