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

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