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

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