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

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