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

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