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

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