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_connect
  20. inet_socketpair
  21. inet_accept
  22. inet_getname
  23. inet_recvmsg
  24. inet_sendmsg
  25. inet_shutdown
  26. inet_select
  27. inet_ioctl
  28. get_sock
  29. get_sock_raw
  30. get_sock_mcast
  31. inet_proto_init

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

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