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

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