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

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