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

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