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

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