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                 sk_free(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 = sk_alloc(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                                 sk_free(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                                 sk_free(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                                 sk_free(sk);
 617                                 return(-EPERM);
 618                         }
 619                         if (!protocol) 
 620                         {
 621                                 sk_free(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                                 sk_free(sk);
 633                                 return(-EPERM);
 634                         }
 635                         if (!protocol) 
 636                         {
 637                                 sk_free(sk);
 638                                 return(-EPROTONOSUPPORT);
 639                         }
 640                         prot = &packet_prot;
 641                         sk->reuse = 1;
 642                         sk->num = protocol;
 643                         break;
 644 
 645                 default:
 646                         sk_free(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->rto = TCP_TIMEOUT_INIT;             /*TCP_WRITE_TIME*/
 659         sk->cong_window = 1; /* start with only sending one packet at a time. */
 660         sk->priority = 1;
 661         sk->state = TCP_CLOSE;
 662 
 663         /* this is how many unacked bytes we will accept for this socket.  */
 664         sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
 665         sk->delay_acks = 1;
 666         sk->max_ack_backlog = SOMAXCONN;
 667         skb_queue_head_init(&sk->write_queue);
 668         skb_queue_head_init(&sk->receive_queue);
 669         sk->mtu = 576;
 670         sk->prot = prot;
 671         sk->sleep = sock->wait;
 672         init_timer(&sk->timer);
 673         init_timer(&sk->retransmit_timer);
 674         sk->timer.data = (unsigned long)sk;
 675         sk->timer.function = &net_timer;
 676         skb_queue_head_init(&sk->back_log);
 677         sock->data =(void *) sk;
 678         sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 679         sk->ip_ttl=ip_statistics.IpDefaultTTL;
 680         if(sk->type==SOCK_RAW && protocol==IPPROTO_RAW)
 681                 sk->ip_hdrincl=1;
 682         else
 683                 sk->ip_hdrincl=0;
 684 #ifdef CONFIG_IP_MULTICAST
 685         sk->ip_mc_loop=1;
 686         sk->ip_mc_ttl=1;
 687         *sk->ip_mc_name=0;
 688         sk->ip_mc_list=NULL;
 689 #endif
 690         /*
 691          *      Speed up by setting some standard state for the dummy_th
 692          *      if TCP uses it (maybe move to tcp_init later)
 693          */
 694         
 695         sk->dummy_th.ack=1;     
 696         sk->dummy_th.doff=sizeof(struct tcphdr)>>2;
 697         
 698         sk->state_change = def_callback1;
 699         sk->data_ready = def_callback2;
 700         sk->write_space = def_callback3;
 701         sk->error_report = def_callback1;
 702 
 703         if (sk->num) 
 704         {
 705         /*
 706          * It assumes that any protocol which allows
 707          * the user to assign a number at socket
 708          * creation time automatically
 709          * shares.
 710          */
 711                 put_sock(sk->num, sk);
 712                 sk->dummy_th.source = ntohs(sk->num);
 713         }
 714 
 715         if (sk->prot->init) 
 716         {
 717                 err = sk->prot->init(sk);
 718                 if (err != 0) 
 719                 {
 720                         destroy_sock(sk);
 721                         return(err);
 722                 }
 723         }
 724         return(0);
 725 }
 726 
 727 
 728 /*
 729  *      Duplicate a socket.
 730  */
 731  
 732 static int inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 733 {
 734         return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
 735 }
 736 
 737 /*
 738  *      The peer socket should always be NULL (or else). When we call this
 739  *      function we are destroying the object and from then on nobody
 740  *      should refer to it.
 741  */
 742  
 743 static int inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 744 {
 745         unsigned long timeout;
 746         struct sock *sk = (struct sock *) sock->data;
 747 
 748         if (sk == NULL) 
 749                 return(0);
 750 
 751         sk->state_change(sk);
 752 
 753         /* Start closing the connection.  This may take a while. */
 754 
 755 #ifdef CONFIG_IP_MULTICAST
 756         /* Applications forget to leave groups before exiting */
 757         ip_mc_drop_socket(sk);
 758 #endif
 759         /*
 760          * If linger is set, we don't return until the close
 761          * is complete.  Otherwise we return immediately. The
 762          * actually closing is done the same either way.
 763          *
 764          * If the close is due to the process exiting, we never
 765          * linger..
 766          */
 767         timeout = 0;
 768         if (sk->linger) {
 769                 timeout = ~0UL;
 770                 if (!sk->lingertime)
 771                         timeout = jiffies + HZ*sk->lingertime;
 772         }
 773         if (current->flags & PF_EXITING)
 774                 timeout = 0;
 775 
 776         sock->data = NULL;
 777         sk->socket = NULL;
 778 
 779         sk->prot->close(sk, timeout);
 780         return(0);
 781 }
 782 
 783 
 784 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 785                int addr_len)
 786 {
 787         struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
 788         struct sock *sk=(struct sock *)sock->data, *sk2;
 789         unsigned short snum = 0 /* Stoopid compiler.. this IS ok */;
 790         int chk_addr_ret;
 791 
 792         /*
 793          *      If the socket has its own bind function then use it.
 794          */
 795          
 796         if(sk->prot->bind)
 797                 return sk->prot->bind(sk,uaddr, addr_len);
 798                 
 799         /* check this error. */
 800         if (sk->state != TCP_CLOSE)
 801                 return(-EIO);
 802         if(addr_len<sizeof(struct sockaddr_in))
 803                 return -EINVAL;
 804                 
 805         if(sock->type != SOCK_RAW)
 806         {
 807                 if (sk->num != 0) 
 808                         return(-EINVAL);
 809 
 810                 snum = ntohs(addr->sin_port);
 811                 
 812 #ifdef CONFIG_IP_MASQUERADE
 813                 /*
 814                  *      The kernel masquerader needs some ports
 815                  */             
 816                 if(snum>=PORT_MASQ_BEGIN && snum<=PORT_MASQ_END)
 817                         return -EADDRINUSE;
 818 #endif           
 819 
 820                 if (snum == 0) 
 821                         snum = get_new_socknum(sk->prot, 0);
 822                 if (snum < PROT_SOCK && !suser()) 
 823                         return(-EACCES);
 824         }
 825         
 826         chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
 827         if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR && chk_addr_ret != IS_MULTICAST && chk_addr_ret != IS_BROADCAST)
 828                 return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
 829 
 830         if (chk_addr_ret || addr->sin_addr.s_addr == 0)
 831         {
 832                 /*
 833                  *      We keep a pair of addresses. rcv_saddr is the one
 834                  *      used by get_sock_*(), and saddr is used for transmit.
 835                  *
 836                  *      In the BSD API these are the same except where it
 837                  *      would be illegal to use them (multicast/broadcast) in
 838                  *      which case the sending device address is used.
 839                  */
 840                 sk->rcv_saddr = addr->sin_addr.s_addr;
 841                 if(chk_addr_ret==IS_MULTICAST||chk_addr_ret==IS_BROADCAST)
 842                         sk->saddr = 0;  /* Use device */
 843                 else
 844                         sk->saddr = addr->sin_addr.s_addr;
 845         }
 846         if(sock->type != SOCK_RAW)
 847         {
 848                 /* Make sure we are allowed to bind here. */
 849                 cli();
 850                 for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
 851                                         sk2 != NULL; sk2 = sk2->next) 
 852                 {
 853                         /*
 854                          *      Hash collision or real match ?
 855                          */
 856                          
 857                         if (sk2->num != snum) 
 858                                 continue;
 859                                 
 860                         /*
 861                          *      Either bind on the port is wildcard means
 862                          *      they will overlap and thus be in error
 863                          */                     
 864                          
 865                         if (!sk2->rcv_saddr || !sk->rcv_saddr)
 866                         {
 867                                 /*
 868                                  *      Allow only if both are setting reuse.
 869                                  */
 870                                 if(sk2->reuse && sk->reuse && sk2->state!=TCP_LISTEN)
 871                                         continue;
 872                                 sti();
 873                                 return(-EADDRINUSE);
 874                         }
 875 
 876                         /*
 877                          *      Two binds match ?
 878                          */
 879 
 880                         if (sk2->rcv_saddr != sk->rcv_saddr) 
 881                                 continue;
 882                         /*
 883                          *      Reusable port ?
 884                          */
 885 
 886                         if (!sk->reuse)
 887                         {
 888                                 sti();
 889                                 return(-EADDRINUSE);
 890                         }
 891                         
 892                         /*
 893                          *      Reuse ?
 894                          */
 895                          
 896                         if (!sk2->reuse || sk2->state==TCP_LISTEN) 
 897                         {
 898                                 sti();
 899                                 return(-EADDRINUSE);
 900                         }
 901                 }
 902                 sti();
 903 
 904                 remove_sock(sk);
 905                 if(sock->type==SOCK_DGRAM)
 906                         udp_cache_zap();
 907                 if(sock->type==SOCK_STREAM)
 908                         tcp_cache_zap();
 909                 put_sock(snum, sk);
 910                 sk->dummy_th.source = ntohs(sk->num);
 911                 sk->daddr = 0;
 912                 sk->dummy_th.dest = 0;
 913         }
 914         ip_rt_put(sk->ip_route_cache);
 915         sk->ip_route_cache=NULL;
 916         return(0);
 917 }
 918 
 919 /*
 920  *      Connect to a remote host. There is regrettably still a little
 921  *      TCP 'magic' in here.
 922  */
 923  
 924 static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 925                   int addr_len, int flags)
 926 {
 927         struct sock *sk=(struct sock *)sock->data;
 928         int err;
 929         sock->conn = NULL;
 930 
 931         if (sock->state == SS_CONNECTING && tcp_connected(sk->state))
 932         {
 933                 sock->state = SS_CONNECTED;
 934                 /* Connection completing after a connect/EINPROGRESS/select/connect */
 935                 return 0;       /* Rock and roll */
 936         }
 937 
 938         if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK))
 939         {
 940                 if(sk->err!=0)
 941                         return sock_error(sk);
 942                 return -EALREADY;       /* Connecting is currently in progress */
 943         }
 944         if (sock->state != SS_CONNECTING) 
 945         {
 946                 /* We may need to bind the socket. */
 947                 if(inet_autobind(sk)!=0)
 948                         return(-EAGAIN);
 949                 if (sk->prot->connect == NULL) 
 950                         return(-EOPNOTSUPP);
 951                 err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
 952                 if (err < 0) 
 953                         return(err);
 954                 sock->state = SS_CONNECTING;
 955         }
 956         
 957         if (sk->state > TCP_FIN_WAIT2 && sock->state==SS_CONNECTING)
 958         {
 959                 sock->state=SS_UNCONNECTED;
 960                 return sock_error(sk);
 961         }
 962 
 963         if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
 964                 return(-EINPROGRESS);
 965 
 966         cli(); /* avoid the race condition */
 967         while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
 968         {
 969                 interruptible_sleep_on(sk->sleep);
 970                 if (current->signal & ~current->blocked) 
 971                 {
 972                         sti();
 973                         return(-ERESTARTSYS);
 974                 }
 975                 /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
 976                    icmp error packets wanting to close a tcp or udp socket. */
 977                 if(sk->err && sk->protocol == IPPROTO_TCP)
 978                 {
 979                         sock->state = SS_UNCONNECTED;
 980                         sti();
 981                         return sock_error(sk); /* set by tcp_err() */
 982                 }
 983         }
 984         sti();
 985         sock->state = SS_CONNECTED;
 986 
 987         if (sk->state != TCP_ESTABLISHED && sk->err) 
 988         {
 989                 sock->state = SS_UNCONNECTED;
 990                 return sock_error(sk);
 991         }
 992         return(0);
 993 }
 994 
 995 
 996 static int inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
 997 {
 998          return(-EOPNOTSUPP);
 999 }
1000 
1001 
1002 /*
1003  *      Accept a pending connection. The TCP layer now gives BSD semantics.
1004  */
1005 
1006 static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1007 {
1008         struct sock *sk1, *sk2;
1009         int err;
1010 
1011         sk1 = (struct sock *) sock->data;
1012 
1013         /*
1014          *      We've been passed an extra socket.
1015          *      We need to free it up because the tcp module creates
1016          *      its own when it accepts one.
1017          */
1018          
1019         if (newsock->data)
1020         {
1021                 struct sock *sk=(struct sock *)newsock->data;
1022                 newsock->data=NULL;
1023                 destroy_sock(sk);
1024         }
1025   
1026         if (sk1->prot->accept == NULL) 
1027                 return(-EOPNOTSUPP);
1028 
1029         /*
1030          *      Restore the state if we have been interrupted, and then returned. 
1031          */
1032          
1033         if (sk1->pair != NULL ) 
1034         {
1035                 sk2 = sk1->pair;
1036                 sk1->pair = NULL;
1037         } 
1038         else
1039         {
1040                 sk2 = sk1->prot->accept(sk1,flags);
1041                 if (sk2 == NULL) 
1042                 {
1043                         return sock_error(sk1);
1044                 }
1045         }
1046         newsock->data = (void *)sk2;
1047         sk2->sleep = newsock->wait;
1048         sk2->socket = newsock;
1049         newsock->conn = NULL;
1050         if (flags & O_NONBLOCK) 
1051                 return(0);
1052 
1053         cli(); /* avoid the race. */
1054         while(sk2->state == TCP_SYN_RECV) 
1055         {
1056                 interruptible_sleep_on(sk2->sleep);
1057                 if (current->signal & ~current->blocked) 
1058                 {
1059                         sti();
1060                         sk1->pair = sk2;
1061                         sk2->sleep = NULL;
1062                         sk2->socket=NULL;
1063                         newsock->data = NULL;
1064                         return(-ERESTARTSYS);
1065                 }
1066         }
1067         sti();
1068 
1069         if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) 
1070         {
1071                 err = sock_error(sk2);
1072                 destroy_sock(sk2);
1073                 newsock->data = NULL;
1074                 return err;
1075         }
1076         newsock->state = SS_CONNECTED;
1077         return(0);
1078 }
1079 
1080 
1081 /*
1082  *      This does both peername and sockname.
1083  */
1084  
1085 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1086                  int *uaddr_len, int peer)
1087 {
1088         struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
1089         struct sock *sk;
1090   
1091         sin->sin_family = AF_INET;
1092         sk = (struct sock *) sock->data;
1093         if (peer) 
1094         {
1095                 if (!tcp_connected(sk->state)) 
1096                         return(-ENOTCONN);
1097                 sin->sin_port = sk->dummy_th.dest;
1098                 sin->sin_addr.s_addr = sk->daddr;
1099         } 
1100         else 
1101         {
1102                 __u32 addr = sk->rcv_saddr;
1103                 if (!addr) {
1104                         addr = sk->saddr;
1105                         if (!addr)
1106                                 addr = ip_my_addr();
1107                 }
1108                 sin->sin_port = sk->dummy_th.source;
1109                 sin->sin_addr.s_addr = addr;
1110         }
1111         *uaddr_len = sizeof(*sin);
1112         return(0);
1113 }
1114 
1115 
1116 
1117 static int inet_recvmsg(struct socket *sock, struct msghdr *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1118                    int flags, int *addr_len )
1119 {
1120         struct sock *sk = (struct sock *) sock->data;
1121         
1122         if (sk->prot->recvmsg == NULL) 
1123                 return(-EOPNOTSUPP);
1124         if(sk->err)
1125                 return sock_error(sk);
1126         /* We may need to bind the socket. */
1127         if(inet_autobind(sk)!=0)
1128                 return(-EAGAIN);
1129         return(sk->prot->recvmsg(sk, ubuf, size, noblock, flags,addr_len));
1130 }
1131 
1132 
1133 static int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1134            int flags)
1135 {
1136         struct sock *sk = (struct sock *) sock->data;
1137         if (sk->shutdown & SEND_SHUTDOWN) 
1138         {
1139                 send_sig(SIGPIPE, current, 1);
1140                 return(-EPIPE);
1141         }
1142         if (sk->prot->sendmsg == NULL) 
1143                 return(-EOPNOTSUPP);
1144         if(sk->err)
1145                 return sock_error(sk);
1146         /* We may need to bind the socket. */
1147         if(inet_autobind(sk)!=0)
1148                 return -EAGAIN;
1149         return(sk->prot->sendmsg(sk, msg, size, noblock, flags));
1150                            
1151 }
1152 
1153 
1154 static int inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1155 {
1156         struct sock *sk=(struct sock*)sock->data;
1157 
1158         /*
1159          * This should really check to make sure
1160          * the socket is a TCP socket. (WHY AC...)
1161          */
1162         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1163                        1->2 bit 2 snds.
1164                        2->3 */
1165         if ((how & ~SHUTDOWN_MASK) || how==0)   /* MAXINT->0 */
1166                 return(-EINVAL);
1167         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1168                 sock->state = SS_CONNECTED;
1169         if (!sk || !tcp_connected(sk->state)) 
1170                 return(-ENOTCONN);
1171         sk->shutdown |= how;
1172         if (sk->prot->shutdown)
1173                 sk->prot->shutdown(sk, how);
1174         return(0);
1175 }
1176 
1177 
1178 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1179 {
1180         struct sock *sk=(struct sock *) sock->data;
1181         if (sk->prot->select == NULL) 
1182         {
1183                 return(0);
1184         }
1185         return(sk->prot->select(sk, sel_type, wait));
1186 }
1187 
1188 /*
1189  *      ioctl() calls you can issue on an INET socket. Most of these are
1190  *      device configuration and stuff and very rarely used. Some ioctls
1191  *      pass on to the socket itself.
1192  *
1193  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
1194  *      loads the devconfigure module does its configuring and unloads it.
1195  *      There's a good 20K of config code hanging around the kernel.
1196  */
1197 
1198 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1199 {
1200         struct sock *sk=(struct sock *)sock->data;
1201         int err;
1202         int pid;
1203 
1204         switch(cmd) 
1205         {
1206                 case FIOSETOWN:
1207                 case SIOCSPGRP:
1208                         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1209                         if(err)
1210                                 return err;
1211                         pid = get_user((int *) arg);
1212                         /* see inet_fcntl */
1213                         if (current->pid != pid && current->pgrp != -pid && !suser())
1214                                 return -EPERM;
1215                         sk->proc = pid;
1216                         return(0);
1217                 case FIOGETOWN:
1218                 case SIOCGPGRP:
1219                         err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1220                         if(err)
1221                                 return err;
1222                         put_fs_long(sk->proc,(int *)arg);
1223                         return(0);                      
1224                 case SIOCGSTAMP:
1225                         if(sk->stamp.tv_sec==0)
1226                                 return -ENOENT;
1227                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1228                         if(err)
1229                                 return err;
1230                         memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1231                         return 0;
1232                 case SIOCADDRT:
1233                 case SIOCDELRT:
1234                         return(ip_rt_ioctl(cmd,(void *) arg));
1235                 case SIOCDARP:
1236                 case SIOCGARP:
1237                 case SIOCSARP:
1238                 case OLD_SIOCDARP:
1239                 case OLD_SIOCGARP:
1240                 case OLD_SIOCSARP:
1241                         return(arp_ioctl(cmd,(void *) arg));
1242                 case SIOCDRARP:
1243                 case SIOCGRARP:
1244                 case SIOCSRARP:
1245 #ifdef CONFIG_KERNELD
1246                         if (rarp_ioctl_hook == NULL)
1247                                 request_module("rarp");
1248 #endif
1249                         if (rarp_ioctl_hook != NULL)
1250                                 return(rarp_ioctl_hook(cmd,(void *) arg));
1251                 case SIOCGIFCONF:
1252                 case SIOCGIFFLAGS:
1253                 case SIOCSIFFLAGS:
1254                 case SIOCGIFADDR:
1255                 case SIOCSIFADDR:
1256                 case SIOCADDMULTI:
1257                 case SIOCDELMULTI:
1258                 case SIOCGIFDSTADDR:
1259                 case SIOCSIFDSTADDR:
1260                 case SIOCGIFBRDADDR:
1261                 case SIOCSIFBRDADDR:
1262                 case SIOCGIFNETMASK:
1263                 case SIOCSIFNETMASK:
1264                 case SIOCGIFMETRIC:
1265                 case SIOCSIFMETRIC:
1266                 case SIOCGIFMEM:
1267                 case SIOCSIFMEM:
1268                 case SIOCGIFMTU:
1269                 case SIOCSIFMTU:
1270                 case SIOCSIFLINK:
1271                 case SIOCGIFHWADDR:
1272                 case SIOCSIFHWADDR:
1273                 case SIOCSIFMAP:
1274                 case SIOCGIFMAP:
1275                 case SIOCSIFSLAVE:
1276                 case SIOCGIFSLAVE:
1277                         return(dev_ioctl(cmd,(void *) arg));
1278 
1279                 default:
1280                         if ((cmd >= SIOCDEVPRIVATE) &&
1281                            (cmd <= (SIOCDEVPRIVATE + 15)))
1282                                 return(dev_ioctl(cmd,(void *) arg));
1283 
1284                         if (sk->prot->ioctl==NULL) 
1285                                 return(-EINVAL);
1286                         return(sk->prot->ioctl(sk, cmd, arg));
1287         }
1288         /*NOTREACHED*/
1289         return(0);
1290 }
1291 
1292 /*
1293  * This routine must find a socket given a TCP or UDP header.
1294  * Everything is assumed to be in net order.
1295  *
1296  * We give priority to more closely bound ports: if some socket
1297  * is bound to a particular foreign address, it will get the packet
1298  * rather than somebody listening to any address..
1299  */
1300 
1301 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1302                                 unsigned long raddr,
1303                                 unsigned short rnum, unsigned long laddr)
1304 {
1305         struct sock *s;
1306         struct sock *result = NULL;
1307         int badness = -1;
1308         unsigned short hnum;
1309 
1310         hnum = ntohs(num);
1311 
1312         /*
1313          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1314          * than a prime unless 3 or more sockets end up using the same
1315          * array entry.  This should not be a problem because most
1316          * well known sockets don't overlap that much, and for
1317          * the other ones, we can just be careful about picking our
1318          * socket number when we choose an arbitrary one.
1319          */
1320 
1321         for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1322                         s != NULL; s = s->next) 
1323         {
1324                 int score = 0;
1325 
1326                 if (s->num != hnum) 
1327                         continue;
1328 
1329                 if(s->dead && (s->state == TCP_CLOSE))
1330                         continue;
1331                 /* local address matches? */
1332                 if (s->rcv_saddr) {
1333                         if (s->rcv_saddr != laddr)
1334                                 continue;
1335                         score++;
1336                 }
1337                 /* remote address matches? */
1338                 if (s->daddr) {
1339                         if (s->daddr != raddr)
1340                                 continue;
1341                         score++;
1342                 }
1343                 /* remote port matches? */
1344                 if (s->dummy_th.dest) {
1345                         if (s->dummy_th.dest != rnum)
1346                                 continue;
1347                         score++;
1348                 }
1349                 /* perfect match? */
1350                 if (score == 3)
1351                         return s;
1352                 /* no, check if this is the best so far.. */
1353                 if (score <= badness)
1354                         continue;
1355                 result = s;
1356                 badness = score;
1357         }
1358         return result;
1359 }
1360 
1361 /*
1362  *      Deliver a datagram to raw sockets.
1363  */
1364  
1365 struct sock *get_sock_raw(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1366                                 unsigned short num,
1367                                 unsigned long raddr,
1368                                 unsigned long laddr)
1369 {
1370         struct sock *s;
1371 
1372         s=sk;
1373 
1374         for(; s != NULL; s = s->next) 
1375         {
1376                 if (s->num != num) 
1377                         continue;
1378                 if(s->dead && (s->state == TCP_CLOSE))
1379                         continue;
1380                 if(s->daddr && s->daddr!=raddr)
1381                         continue;
1382                 if(s->rcv_saddr && s->rcv_saddr != laddr)
1383                         continue;
1384                 return(s);
1385         }
1386         return(NULL);
1387 }
1388 
1389 #ifdef CONFIG_IP_MULTICAST
1390 /*
1391  *      Deliver a datagram to broadcast/multicast sockets.
1392  */
1393  
1394 struct sock *get_sock_mcast(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1395                                 unsigned short num,
1396                                 unsigned long raddr,
1397                                 unsigned short rnum, unsigned long laddr)
1398 {
1399         struct sock *s;
1400         unsigned short hnum;
1401 
1402         hnum = ntohs(num);
1403 
1404         /*
1405          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1406          * than a prime unless 3 or more sockets end up using the same
1407          * array entry.  This should not be a problem because most
1408          * well known sockets don't overlap that much, and for
1409          * the other ones, we can just be careful about picking our
1410          * socket number when we choose an arbitrary one.
1411          */
1412         
1413         s=sk;
1414 
1415         for(; s != NULL; s = s->next) 
1416         {
1417                 if (s->num != hnum) 
1418                         continue;
1419                 if(s->dead && (s->state == TCP_CLOSE))
1420                         continue;
1421                 if(s->daddr && s->daddr!=raddr)
1422                         continue;
1423                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1424                         continue;
1425                 if(s->rcv_saddr  && s->rcv_saddr != laddr)
1426                         continue;
1427                 return(s);
1428         }
1429         return(NULL);
1430 }
1431 
1432 #endif
1433 
1434 static struct proto_ops inet_proto_ops = {
1435         AF_INET,
1436 
1437         inet_create,
1438         inet_dup,
1439         inet_release,
1440         inet_bind,
1441         inet_connect,
1442         inet_socketpair,
1443         inet_accept,
1444         inet_getname, 
1445         inet_select,
1446         inet_ioctl,
1447         inet_listen,
1448         inet_shutdown,
1449         inet_setsockopt,
1450         inet_getsockopt,
1451         inet_fcntl,
1452         inet_sendmsg,
1453         inet_recvmsg
1454 };
1455 
1456 extern unsigned long seq_offset;
1457 
1458 /*
1459  *      Called by socket.c on kernel startup.  
1460  */
1461  
1462 void inet_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1463 {
1464         struct inet_protocol *p;
1465         int i;
1466 
1467 
1468         printk("Swansea University Computer Society TCP/IP for NET3.034\n");
1469 
1470         /*
1471          *      Tell SOCKET that we are alive... 
1472          */
1473    
1474         (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1475 
1476         seq_offset = CURRENT_TIME*250;
1477 
1478         /*
1479          *      Add all the protocols. 
1480          */
1481          
1482         for(i = 0; i < SOCK_ARRAY_SIZE; i++) 
1483         {
1484                 tcp_prot.sock_array[i] = NULL;
1485                 udp_prot.sock_array[i] = NULL;
1486                 raw_prot.sock_array[i] = NULL;
1487         }
1488         tcp_prot.inuse = 0;
1489         tcp_prot.highestinuse = 0;
1490         udp_prot.inuse = 0;
1491         udp_prot.highestinuse = 0;
1492         raw_prot.inuse = 0;
1493         raw_prot.highestinuse = 0;
1494 
1495         printk("IP Protocols: ");
1496         for(p = inet_protocol_base; p != NULL;) 
1497         {
1498                 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1499                 inet_add_protocol(p);
1500                 printk("%s%s",p->name,tmp?", ":"\n");
1501                 p = tmp;
1502         }
1503 
1504         /*
1505          *      Set the ARP module up
1506          */
1507         arp_init();
1508         /*
1509          *      Set the IP module up
1510          */
1511         ip_init();
1512         /*
1513          *      Set the ICMP layer up
1514          */
1515         icmp_init(&inet_proto_ops);
1516         /*
1517          *      Set the firewalling up
1518          */
1519 #if defined(CONFIG_IP_ACCT)||defined(CONFIG_IP_FIREWALL)|| \
1520     defined(CONFIG_IP_MASQUERADE)
1521         ip_fw_init();
1522 #endif
1523         /*
1524          *      Initialise the multicast router
1525          */
1526 #if defined(CONFIG_IP_MROUTE)
1527         ip_mr_init();
1528 #endif
1529 
1530         /*
1531          *  Initialise AF_INET alias type (register net_alias_type)
1532          */
1533 
1534 #if defined(CONFIG_IP_ALIAS)
1535         ip_alias_init();
1536 #endif
1537 
1538 #ifdef CONFIG_INET_RARP
1539         rarp_ioctl_hook = rarp_ioctl;
1540 #endif
1541         /*
1542          *      Create all the /proc entries.
1543          */
1544 
1545 #ifdef CONFIG_PROC_FS
1546 
1547 #ifdef CONFIG_INET_RARP
1548         proc_net_register(&(struct proc_dir_entry) {
1549                 PROC_NET_RARP, 4, "rarp",
1550                 S_IFREG | S_IRUGO, 1, 0, 0,
1551                 0, &proc_net_inode_operations,
1552                 rarp_get_info
1553         });
1554 #endif          /* RARP */
1555 
1556         proc_net_register(&(struct proc_dir_entry) {
1557                 PROC_NET_RAW, 3, "raw",
1558                 S_IFREG | S_IRUGO, 1, 0, 0,
1559                 0, &proc_net_inode_operations,
1560                 raw_get_info
1561         });
1562         proc_net_register(&(struct proc_dir_entry) {
1563                 PROC_NET_SNMP, 4, "snmp",
1564                 S_IFREG | S_IRUGO, 1, 0, 0,
1565                 0, &proc_net_inode_operations,
1566                 snmp_get_info
1567         });
1568         proc_net_register(&(struct proc_dir_entry) {
1569                 PROC_NET_SOCKSTAT, 8, "sockstat",
1570                 S_IFREG | S_IRUGO, 1, 0, 0,
1571                 0, &proc_net_inode_operations,
1572                 afinet_get_info
1573         });
1574         proc_net_register(&(struct proc_dir_entry) {
1575                 PROC_NET_TCP, 3, "tcp",
1576                 S_IFREG | S_IRUGO, 1, 0, 0,
1577                 0, &proc_net_inode_operations,
1578                 tcp_get_info
1579         });
1580         proc_net_register(&(struct proc_dir_entry) {
1581                 PROC_NET_UDP, 3, "udp",
1582                 S_IFREG | S_IRUGO, 1, 0, 0,
1583                 0, &proc_net_inode_operations,
1584                 udp_get_info
1585         });
1586         proc_net_register(&(struct proc_dir_entry) {
1587                 PROC_NET_ROUTE, 5, "route",
1588                 S_IFREG | S_IRUGO, 1, 0, 0,
1589                 0, &proc_net_inode_operations,
1590                 rt_get_info
1591         });
1592         proc_net_register(&(struct proc_dir_entry) {
1593                 PROC_NET_RTCACHE, 8, "rt_cache",
1594                 S_IFREG | S_IRUGO, 1, 0, 0,
1595                 0, &proc_net_inode_operations,
1596                 rt_cache_get_info
1597         });
1598 #endif          /* CONFIG_PROC_FS */
1599 }

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