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

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