root/net/ipv4/af_inet.c

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

DEFINITIONS

This source file includes following definitions.
  1. sk_inuse
  2. get_new_socknum
  3. put_sock
  4. remove_sock
  5. destroy_sock
  6. inet_fcntl
  7. inet_setsockopt
  8. inet_getsockopt
  9. inet_autobind
  10. inet_listen
  11. def_callback1
  12. def_callback2
  13. def_callback3
  14. inet_create
  15. inet_dup
  16. inet_release
  17. inet_bind
  18. inet_connect
  19. inet_socketpair
  20. inet_accept
  21. inet_getname
  22. inet_recvmsg
  23. inet_sendmsg
  24. inet_shutdown
  25. inet_select
  26. inet_ioctl
  27. get_sock
  28. get_sock_raw
  29. get_sock_mcast
  30. inet_proto_init

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              AF_INET protocol family socket handler.
   7  *
   8  * Version:     @(#)af_inet.c   (from sock.c) 1.0.17    06/02/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Florian La Roche, <flla@stud.uni-sb.de>
  13  *              Alan Cox, <A.Cox@swansea.ac.uk>
  14  *
  15  * Changes (see also sock.c)
  16  *
  17  *              A.N.Kuznetsov   :       Socket death error in accept().
  18  *              John Richardson :       Fix non blocking error in connect()
  19  *                                      so sockets that fail to connect
  20  *                                      don't return -EINPROGRESS.
  21  *              Alan Cox        :       Asynchronous I/O support
  22  *              Alan Cox        :       Keep correct socket pointer on sock structures
  23  *                                      when accept() ed
  24  *              Alan Cox        :       Semantics of SO_LINGER aren't state moved
  25  *                                      to close when you look carefully. With
  26  *                                      this fixed and the accept bug fixed 
  27  *                                      some RPC stuff seems happier.
  28  *              Niibe Yutaka    :       4.4BSD style write async I/O
  29  *              Alan Cox, 
  30  *              Tony Gale       :       Fixed reuse semantics.
  31  *              Alan Cox        :       bind() shouldn't abort existing but dead
  32  *                                      sockets. Stops FTP netin:.. I hope.
  33  *              Alan Cox        :       bind() works correctly for RAW sockets. Note
  34  *                                      that FreeBSD at least was broken in this respect
  35  *                                      so be careful with compatibility tests...
  36  *              Alan Cox        :       routing cache support
  37  *              Alan Cox        :       memzero the socket structure for compactness.
  38  *              Matt Day        :       nonblock connect error handler
  39  *              Alan Cox        :       Allow large numbers of pending sockets
  40  *                                      (eg for big web sites), but only if
  41  *                                      specifically application requested.
  42  *              Alan Cox        :       New buffering throughout IP. Used dumbly.
  43  *              Alan Cox        :       New buffering now used smartly.
  44  *              Alan Cox        :       BSD rather than common sense interpretation of
  45  *                                      listen.
  46  *              Germano Caronni :       Assorted small races.
  47  *              Alan Cox        :       sendmsg/recvmsg basic support.
  48  *              Alan Cox        :       Only sendmsg/recvmsg now supported.
  49  *              Alan Cox        :       Locked down bind (see security list).
  50  *              Alan Cox        :       Loosened bind a little.
  51  *
  52  *              This program is free software; you can redistribute it and/or
  53  *              modify it under the terms of the GNU General Public License
  54  *              as published by the Free Software Foundation; either version
  55  *              2 of the License, or (at your option) any later version.
  56  */
  57 
  58 #include <linux/config.h>
  59 #include <linux/errno.h>
  60 #include <linux/types.h>
  61 #include <linux/socket.h>
  62 #include <linux/in.h>
  63 #include <linux/kernel.h>
  64 #include <linux/major.h>
  65 #include <linux/sched.h>
  66 #include <linux/timer.h>
  67 #include <linux/string.h>
  68 #include <linux/sockios.h>
  69 #include <linux/net.h>
  70 #include <linux/fcntl.h>
  71 #include <linux/mm.h>
  72 #include <linux/interrupt.h>
  73 #include <linux/proc_fs.h>
  74 #include <linux/stat.h>
  75 
  76 #include <asm/segment.h>
  77 #include <asm/system.h>
  78 
  79 #include <linux/inet.h>
  80 #include <linux/netdevice.h>
  81 #include <net/ip.h>
  82 #include <net/protocol.h>
  83 #include <net/arp.h>
  84 #include <net/rarp.h>
  85 #include <net/route.h>
  86 #include <net/tcp.h>
  87 #include <net/udp.h>
  88 #include <linux/skbuff.h>
  89 #include <net/sock.h>
  90 #include <net/raw.h>
  91 #include <net/icmp.h>
  92 #include <linux/ip_fw.h>
  93 #ifdef CONFIG_IP_MASQUERADE
  94 #include <net/ip_masq.h>
  95 #endif
  96 #ifdef CONFIG_IP_ALIAS
  97 #include <net/ip_alias.h>
  98 #endif
  99 #ifdef CONFIG_BRIDGE
 100 #include <net/br.h>
 101 #endif
 102 #ifdef CONFIG_KERNELD
 103 #include <linux/kerneld.h>
 104 #endif
 105 
 106 #define min(a,b)        ((a)<(b)?(a):(b))
 107 
 108 extern struct proto packet_prot;
 109 extern int raw_get_info(char *, char **, off_t, int, int);
 110 extern int snmp_get_info(char *, char **, off_t, int, int);
 111 extern int afinet_get_info(char *, char **, off_t, int, int);
 112 extern int tcp_get_info(char *, char **, off_t, int, int);
 113 extern int udp_get_info(char *, char **, off_t, int, int);
 114 
 115 int (*rarp_ioctl_hook)(unsigned int,void*) = NULL;
 116 
 117 /*
 118  *      See if a socket number is in use.
 119  */
 120  
 121 static int sk_inuse(struct proto *prot, int num)
     /* [previous][next][first][last][top][bottom][index][help] */
 122 {
 123         struct sock *sk;
 124 
 125         for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];
 126                 sk != NULL;  sk=sk->next) 
 127         {
 128                 if (sk->num == num) 
 129                         return(1);
 130         }
 131         return(0);
 132 }
 133 
 134 
 135 /*
 136  *      Pick a new socket number
 137  */
 138 
 139 unsigned short get_new_socknum(struct proto *prot, unsigned short base)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141         static int start=0;
 142 
 143         /*
 144          * Used to cycle through the port numbers so the
 145          * chances of a confused connection drop.
 146          */
 147          
 148         int i, j;
 149         int best = 0;
 150         int size = 32767; /* a big num. */
 151         struct sock *sk;
 152 
 153         if (base == 0) 
 154                 base = PROT_SOCK+1+(start % 1024);
 155         if (base <= PROT_SOCK) 
 156         {
 157                 base += PROT_SOCK+(start % 1024);
 158         }
 159 
 160         /*
 161          *      Now look through the entire array and try to find an empty ptr. 
 162          */
 163          
 164         for(i=0; i < SOCK_ARRAY_SIZE; i++) 
 165         {
 166                 j = 0;
 167                 sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
 168                 while(sk != NULL) 
 169                 {
 170                         sk = sk->next;
 171                         j++;
 172                 }
 173                 if (j == 0) 
 174                 {
 175                         start =(i+1+start )%1024;
 176                         return(i+base+1);
 177                 }
 178                 if (j < size) 
 179                 {
 180                         best = i;
 181                         size = j;
 182                 }
 183         }
 184 
 185         /* Now make sure the one we want is not in use. */
 186 
 187         while(sk_inuse(prot, base +best+1)) 
 188         {
 189                 best += SOCK_ARRAY_SIZE;
 190         }
 191         return(best+base+1);
 192 }
 193 
 194 /*
 195  *      Add a socket into the socket tables by number.
 196  */
 197 
 198 void put_sock(unsigned short num, struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 199 {
 200         struct sock **skp, *tmp;
 201         int mask;
 202         unsigned long flags;
 203         
 204         if(sk->type==SOCK_PACKET)
 205                 return;
 206 
 207         sk->num = num;
 208         sk->next = NULL;
 209         num = num &(SOCK_ARRAY_SIZE -1);
 210 
 211         /* 
 212          *      We can't have an interrupt re-enter here. 
 213          */
 214          
 215         save_flags(flags);
 216         cli();
 217 
 218         sk->prot->inuse += 1;
 219         if (sk->prot->highestinuse < sk->prot->inuse)
 220                 sk->prot->highestinuse = sk->prot->inuse;
 221 
 222         if (sk->prot->sock_array[num] == NULL) 
 223         {
 224                 sk->prot->sock_array[num] = sk;
 225                 restore_flags(flags);
 226                 return;
 227         }
 228         
 229         restore_flags(flags);
 230         for(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask) 
 231         {
 232                 if ((mask & sk->rcv_saddr) &&
 233                     (mask & sk->rcv_saddr) != (mask & 0xffffffff)) 
 234                 {
 235                         mask = mask << 8;
 236                         break;
 237                 }
 238         }
 239 
 240         /*
 241          * add the socket to the sock_array[]..
 242          */
 243         skp = sk->prot->sock_array + num;
 244         cli();
 245         while ((tmp = *skp) != NULL) {
 246                 if (!(tmp->rcv_saddr & mask))
 247                         break;
 248                 skp = &tmp->next;
 249         }
 250         sk->next = tmp;
 251         *skp = sk;
 252         sti();
 253 }
 254 
 255 /*
 256  *      Remove a socket from the socket tables.
 257  */
 258 
 259 static void remove_sock(struct sock *sk1)
     /* [previous][next][first][last][top][bottom][index][help] */
 260 {
 261         struct sock **p;
 262         unsigned long flags;
 263 
 264         if (sk1->type==SOCK_PACKET)
 265                 return;
 266                 
 267         if (!sk1->prot) 
 268         {
 269                 NETDEBUG(printk("sock.c: remove_sock: sk1->prot == NULL\n"));
 270                 return;
 271         }
 272 
 273         /* We can't have this changing out from under us. */
 274         save_flags(flags);
 275         cli();
 276         
 277         p=&(sk1->prot->sock_array[sk1->num & (SOCK_ARRAY_SIZE -1)]);
 278         
 279         while(*p!=NULL)
 280         {
 281                 if(*p==sk1)
 282                 {
 283                         sk1->prot->inuse--;
 284                         *p=sk1->next;
 285                         break;
 286                 }
 287                 p=&((*p)->next);
 288         }
 289         restore_flags(flags);
 290 }
 291 
 292 /*
 293  *      Destroy an AF_INET socket
 294  */
 295  
 296 void destroy_sock(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 297 {
 298         struct sk_buff *skb;
 299 
 300         lock_sock(sk);                  /* just to be safe. */
 301 
 302         remove_sock(sk);
 303   
 304         /*
 305          *      Now we can no longer get new packets or once the
 306          *      timers are killed, send them.
 307          */
 308          
 309         delete_timer(sk);
 310         del_timer(&sk->delack_timer);
 311         del_timer(&sk->retransmit_timer);
 312         
 313         /*
 314          *      Drain any partial frames
 315          */
 316          
 317         while ((skb = tcp_dequeue_partial(sk)) != NULL) 
 318         {
 319                 IS_SKB(skb);
 320                 kfree_skb(skb, FREE_WRITE);
 321         }
 322 
 323         /*
 324          *      Cleanup up the write buffer. 
 325          */
 326          
 327         while((skb = skb_dequeue(&sk->write_queue)) != NULL) {
 328                 IS_SKB(skb);
 329                 kfree_skb(skb, FREE_WRITE);
 330         }
 331         
 332         /*
 333          *      Clean up the read buffer.
 334          */
 335 
 336         while((skb=skb_dequeue(&sk->receive_queue))!=NULL) 
 337         {
 338                 /*
 339                  * This will take care of closing sockets that were
 340                  * listening and didn't accept everything.
 341                  */
 342                 if (skb->sk != NULL && skb->sk != sk) 
 343                 {
 344                         IS_SKB(skb);
 345                         skb->sk->prot->close(skb->sk, 0);
 346                 }
 347                 IS_SKB(skb);
 348                 kfree_skb(skb, FREE_READ);
 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                 skb->sk = NULL;
 385                 kfree_skb(skb, FREE_READ);
 386         }
 387 
 388         /*
 389          *      Now if it has a half accepted/ closed socket. 
 390          */
 391          
 392         if (sk->pair) 
 393         {
 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->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                 sk_free(sk);
 415         } 
 416         else 
 417         {
 418                 /* this should never happen. */
 419                 /* actually it can if an ack has just been sent. */
 420                 printk("Socket destroy delayed (r=%d w=%d)\n",
 421                         sk->rmem_alloc, sk->wmem_alloc);
 422                 sk->destroy = 1;
 423                 sk->ack_backlog = 0;
 424                 release_sock(sk);
 425                 reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
 426         }
 427 }
 428 
 429 /*
 430  *      The routines beyond this point handle the behaviour of an AF_INET
 431  *      socket object. Mostly it punts to the subprotocols of IP to do
 432  *      the work.
 433  */
 434  
 435 static int inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 436 {
 437         struct sock *sk;
 438 
 439         sk = (struct sock *) sock->data;
 440 
 441         switch(cmd) 
 442         {
 443                 case F_SETOWN:
 444                         /*
 445                          * This is a little restrictive, but it's the only
 446                          * way to make sure that you can't send a sigurg to
 447                          * another process.
 448                          */
 449                         if (!suser() && current->pgrp != -arg &&
 450                                 current->pid != arg) return(-EPERM);
 451                         sk->proc = arg;
 452                         return(0);
 453                 case F_GETOWN:
 454                         return(sk->proc);
 455                 default:
 456                         return(-EINVAL);
 457         }
 458 }
 459 
 460 /*
 461  *      Set socket options on an inet socket.
 462  */
 463  
 464 static int inet_setsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 465                     char *optval, int optlen)
 466 {
 467         struct sock *sk = (struct sock *) sock->data;  
 468         if (level == SOL_SOCKET)
 469                 return sock_setsockopt(sk,level,optname,optval,optlen);
 470         if (sk->prot->setsockopt==NULL)
 471                 return(-EOPNOTSUPP);
 472         else
 473                 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
 474 }
 475 
 476 /*
 477  *      Get a socket option on an AF_INET socket.
 478  */
 479 
 480 static int inet_getsockopt(struct socket *sock, int level, int optname,
     /* [previous][next][first][last][top][bottom][index][help] */
 481                     char *optval, int *optlen)
 482 {
 483         struct sock *sk = (struct sock *) sock->data;   
 484         if (level == SOL_SOCKET) 
 485                 return sock_getsockopt(sk,level,optname,optval,optlen);
 486         if(sk->prot->getsockopt==NULL)          
 487                 return(-EOPNOTSUPP);
 488         else
 489                 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
 490 }
 491 
 492 /*
 493  *      Automatically bind an unbound socket.
 494  */
 495 
 496 static int inet_autobind(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 497 {
 498         /* We may need to bind the socket. */
 499         if (sk->num == 0) 
 500         {
 501                 sk->num = get_new_socknum(sk->prot, 0);
 502                 if (sk->num == 0) 
 503                         return(-EAGAIN);
 504                 udp_cache_zap();
 505                 tcp_cache_zap();
 506                 put_sock(sk->num, sk);
 507                 sk->dummy_th.source = ntohs(sk->num);
 508         }
 509         return 0;
 510 }
 511 
 512 /*
 513  *      Move a socket into listening state.
 514  */
 515  
 516 static int inet_listen(struct socket *sock, int backlog)
     /* [previous][next][first][last][top][bottom][index][help] */
 517 {
 518         struct sock *sk = (struct sock *) sock->data;
 519 
 520         if(inet_autobind(sk)!=0)
 521                 return -EAGAIN;
 522 
 523         /* We might as well re use these. */ 
 524         /*
 525          * note that the backlog is "unsigned char", so truncate it
 526          * somewhere. We might as well truncate it to what everybody
 527          * else does..
 528          * Now truncate to 128 not 5. 
 529          */
 530         if ((unsigned) backlog == 0)    /* BSDism */
 531                 backlog = 1;
 532         if ((unsigned) backlog > SOMAXCONN)
 533                 backlog = SOMAXCONN;
 534         sk->max_ack_backlog = backlog;
 535         if (sk->state != TCP_LISTEN)
 536         {
 537                 sk->ack_backlog = 0;
 538                 sk->state = TCP_LISTEN;
 539         }
 540         return(0);
 541 }
 542 
 543 /*
 544  *      Default callbacks for user INET sockets. These just wake up
 545  *      the user owning the socket.
 546  */
 547 
 548 static void def_callback1(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 549 {
 550         if(!sk->dead)
 551                 wake_up_interruptible(sk->sleep);
 552 }
 553 
 554 static void def_callback2(struct sock *sk,int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 555 {
 556         if(!sk->dead)
 557         {
 558                 wake_up_interruptible(sk->sleep);
 559                 sock_wake_async(sk->socket, 1);
 560         }
 561 }
 562 
 563 static void def_callback3(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 564 {
 565         if(!sk->dead && sk->wmem_alloc*2 <= sk->sndbuf)
 566         {
 567                 wake_up_interruptible(sk->sleep);
 568                 sock_wake_async(sk->socket, 2);
 569         }
 570 }
 571 
 572 /*
 573  *      Create an inet socket.
 574  *
 575  *      FIXME: Gcc would generate much better code if we set the parameters
 576  *      up in in-memory structure order. Gcc68K even more so
 577  */
 578 
 579 static int inet_create(struct socket *sock, int protocol)
     /* [previous][next][first][last][top][bottom][index][help] */
 580 {
 581         struct sock *sk;
 582         struct proto *prot;
 583         int err;
 584 
 585         sk = sk_alloc(GFP_KERNEL);
 586         if (sk == NULL) 
 587                 return(-ENOBUFS);
 588         memset(sk,0,sizeof(*sk));       /* Efficient way to set most fields to zero */
 589         /*
 590          *      Note for tcp that also wiped the dummy_th block for us.
 591          */
 592         switch(sock->type) 
 593         {
 594                 case SOCK_STREAM:
 595                 case SOCK_SEQPACKET:
 596                         if (protocol && protocol != IPPROTO_TCP) 
 597                         {
 598                                 sk_free(sk);
 599                                 return(-EPROTONOSUPPORT);
 600                         }
 601                         protocol = IPPROTO_TCP;
 602                         sk->no_check = TCP_NO_CHECK;
 603                         prot = &tcp_prot;
 604                         break;
 605 
 606                 case SOCK_DGRAM:
 607                         if (protocol && protocol != IPPROTO_UDP) 
 608                         {
 609                                 sk_free(sk);
 610                                 return(-EPROTONOSUPPORT);
 611                         }
 612                         protocol = IPPROTO_UDP;
 613                         sk->no_check = UDP_NO_CHECK;
 614                         prot=&udp_prot;
 615                         break;
 616       
 617                 case SOCK_RAW:
 618                         if (!suser()) 
 619                         {
 620                                 sk_free(sk);
 621                                 return(-EPERM);
 622                         }
 623                         if (!protocol) 
 624                         {
 625                                 sk_free(sk);
 626                                 return(-EPROTONOSUPPORT);
 627                         }
 628                         prot = &raw_prot;
 629                         sk->reuse = 1;
 630                         sk->num = protocol;
 631                         break;
 632 
 633                 case SOCK_PACKET:
 634                         if (!suser()) 
 635                         {
 636                                 sk_free(sk);
 637                                 return(-EPERM);
 638                         }
 639                         if (!protocol) 
 640                         {
 641                                 sk_free(sk);
 642                                 return(-EPROTONOSUPPORT);
 643                         }
 644                         prot = &packet_prot;
 645                         sk->reuse = 1;
 646                         sk->num = protocol;
 647                         break;
 648 
 649                 default:
 650                         sk_free(sk);
 651                         return(-ESOCKTNOSUPPORT);
 652         }
 653         sk->socket = sock;
 654 #ifdef CONFIG_TCP_NAGLE_OFF
 655         sk->nonagle = 1;
 656 #endif  
 657         sk->type = sock->type;
 658         sk->protocol = protocol;
 659         sk->allocation = GFP_KERNEL;
 660         sk->sndbuf = SK_WMEM_MAX;
 661         sk->rcvbuf = SK_RMEM_MAX;
 662         sk->rto = TCP_TIMEOUT_INIT;             /*TCP_WRITE_TIME*/
 663         sk->cong_window = 1; /* start with only sending one packet at a time. */
 664         sk->priority = 1;
 665         sk->state = TCP_CLOSE;
 666 
 667         /* this is how many unacked bytes we will accept for this socket.  */
 668         sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
 669         sk->delay_acks = 1;
 670         sk->max_ack_backlog = SOMAXCONN;
 671         skb_queue_head_init(&sk->write_queue);
 672         skb_queue_head_init(&sk->receive_queue);
 673         sk->mtu = 576;
 674         sk->prot = prot;
 675         sk->sleep = sock->wait;
 676         init_timer(&sk->timer);
 677         init_timer(&sk->delack_timer);
 678         init_timer(&sk->retransmit_timer);
 679         sk->timer.data = (unsigned long)sk;
 680         sk->timer.function = &net_timer;
 681         skb_queue_head_init(&sk->back_log);
 682         sock->data =(void *) sk;
 683         sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
 684         sk->ip_ttl=ip_statistics.IpDefaultTTL;
 685         if(sk->type==SOCK_RAW && protocol==IPPROTO_RAW)
 686                 sk->ip_hdrincl=1;
 687         else
 688                 sk->ip_hdrincl=0;
 689 #ifdef CONFIG_IP_MULTICAST
 690         sk->ip_mc_loop=1;
 691         sk->ip_mc_ttl=1;
 692         *sk->ip_mc_name=0;
 693         sk->ip_mc_list=NULL;
 694 #endif
 695         /*
 696          *      Speed up by setting some standard state for the dummy_th
 697          *      if TCP uses it (maybe move to tcp_init later)
 698          */
 699         
 700         sk->dummy_th.ack=1;     
 701         sk->dummy_th.doff=sizeof(struct tcphdr)>>2;
 702         
 703         sk->state_change = def_callback1;
 704         sk->data_ready = def_callback2;
 705         sk->write_space = def_callback3;
 706         sk->error_report = def_callback1;
 707 
 708         if (sk->num) 
 709         {
 710         /*
 711          * It assumes that any protocol which allows
 712          * the user to assign a number at socket
 713          * creation time automatically
 714          * shares.
 715          */
 716                 put_sock(sk->num, sk);
 717                 sk->dummy_th.source = ntohs(sk->num);
 718         }
 719 
 720         if (sk->prot->init) 
 721         {
 722                 err = sk->prot->init(sk);
 723                 if (err != 0) 
 724                 {
 725                         destroy_sock(sk);
 726                         return(err);
 727                 }
 728         }
 729         return(0);
 730 }
 731 
 732 
 733 /*
 734  *      Duplicate a socket.
 735  */
 736  
 737 static int inet_dup(struct socket *newsock, struct socket *oldsock)
     /* [previous][next][first][last][top][bottom][index][help] */
 738 {
 739         return(inet_create(newsock,((struct sock *)(oldsock->data))->protocol));
 740 }
 741 
 742 /*
 743  *      The peer socket should always be NULL (or else). When we call this
 744  *      function we are destroying the object and from then on nobody
 745  *      should refer to it.
 746  */
 747  
 748 static int inet_release(struct socket *sock, struct socket *peer)
     /* [previous][next][first][last][top][bottom][index][help] */
 749 {
 750         unsigned long timeout;
 751         struct sock *sk = (struct sock *) sock->data;
 752 
 753         if (sk == NULL) 
 754                 return(0);
 755 
 756         sk->state_change(sk);
 757 
 758         /* Start closing the connection.  This may take a while. */
 759 
 760 #ifdef CONFIG_IP_MULTICAST
 761         /* Applications forget to leave groups before exiting */
 762         ip_mc_drop_socket(sk);
 763 #endif
 764         /*
 765          * If linger is set, we don't return until the close
 766          * is complete.  Otherwise we return immediately. The
 767          * actually closing is done the same either way.
 768          *
 769          * If the close is due to the process exiting, we never
 770          * linger..
 771          */
 772         timeout = 0;
 773         if (sk->linger) {
 774                 timeout = ~0UL;
 775                 if (!sk->lingertime)
 776                         timeout = jiffies + HZ*sk->lingertime;
 777         }
 778         if (current->flags & PF_EXITING)
 779                 timeout = 0;
 780 
 781         sock->data = NULL;
 782         sk->socket = NULL;
 783 
 784         sk->prot->close(sk, timeout);
 785         return(0);
 786 }
 787 
 788 
 789 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 790                int addr_len)
 791 {
 792         struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
 793         struct sock *sk=(struct sock *)sock->data, *sk2;
 794         unsigned short snum = 0 /* Stoopid compiler.. this IS ok */;
 795         int chk_addr_ret;
 796 
 797         /*
 798          *      If the socket has its own bind function then use it.
 799          */
 800          
 801         if(sk->prot->bind)
 802                 return sk->prot->bind(sk,uaddr, addr_len);
 803                 
 804         /* check this error. */
 805         if (sk->state != TCP_CLOSE)
 806                 return(-EIO);
 807         if(addr_len<sizeof(struct sockaddr_in))
 808                 return -EINVAL;
 809                 
 810         if(sock->type != SOCK_RAW)
 811         {
 812                 if (sk->num != 0) 
 813                         return(-EINVAL);
 814 
 815                 snum = ntohs(addr->sin_port);
 816                 
 817 #ifdef CONFIG_IP_MASQUERADE
 818                 /*
 819                  *      The kernel masquerader needs some ports
 820                  */             
 821                 if(snum>=PORT_MASQ_BEGIN && snum<=PORT_MASQ_END)
 822                         return -EADDRINUSE;
 823 #endif           
 824 
 825                 if (snum == 0) 
 826                         snum = get_new_socknum(sk->prot, 0);
 827                 if (snum < PROT_SOCK && !suser()) 
 828                         return(-EACCES);
 829         }
 830         
 831         chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
 832         if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR && chk_addr_ret != IS_MULTICAST && chk_addr_ret != IS_BROADCAST)
 833                 return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
 834 
 835         if (chk_addr_ret || addr->sin_addr.s_addr == 0)
 836         {
 837                 /*
 838                  *      We keep a pair of addresses. rcv_saddr is the one
 839                  *      used by get_sock_*(), and saddr is used for transmit.
 840                  *
 841                  *      In the BSD API these are the same except where it
 842                  *      would be illegal to use them (multicast/broadcast) in
 843                  *      which case the sending device address is used.
 844                  */
 845                 sk->rcv_saddr = addr->sin_addr.s_addr;
 846                 if(chk_addr_ret==IS_MULTICAST||chk_addr_ret==IS_BROADCAST)
 847                         sk->saddr = 0;  /* Use device */
 848                 else
 849                         sk->saddr = addr->sin_addr.s_addr;
 850         }
 851         if(sock->type != SOCK_RAW)
 852         {
 853                 /* Make sure we are allowed to bind here. */
 854                 cli();
 855                 for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
 856                                         sk2 != NULL; sk2 = sk2->next) 
 857                 {
 858                         /*
 859                          *      Hash collision or real match ?
 860                          */
 861                          
 862                         if (sk2->num != snum) 
 863                                 continue;
 864                                 
 865                         /*
 866                          *      Either bind on the port is wildcard means
 867                          *      they will overlap and thus be in error
 868                          */                     
 869                          
 870                         if (!sk2->rcv_saddr || !sk->rcv_saddr)
 871                         {
 872                                 /*
 873                                  *      Allow only if both are setting reuse.
 874                                  */
 875                                 if(sk2->reuse && sk->reuse && sk2->state!=TCP_LISTEN)
 876                                         continue;
 877                                 sti();
 878                                 return(-EADDRINUSE);
 879                         }
 880 
 881                         /*
 882                          *      Two binds match ?
 883                          */
 884 
 885                         if (sk2->rcv_saddr != sk->rcv_saddr) 
 886                                 continue;
 887                         /*
 888                          *      Reusable port ?
 889                          */
 890 
 891                         if (!sk->reuse)
 892                         {
 893                                 sti();
 894                                 return(-EADDRINUSE);
 895                         }
 896                         
 897                         /*
 898                          *      Reuse ?
 899                          */
 900                          
 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          
1024         if (newsock->data)
1025         {
1026                 struct sock *sk=(struct sock *)newsock->data;
1027                 newsock->data=NULL;
1028                 destroy_sock(sk);
1029         }
1030   
1031         if (sk1->prot->accept == NULL) 
1032                 return(-EOPNOTSUPP);
1033 
1034         /*
1035          *      Restore the state if we have been interrupted, and then returned. 
1036          */
1037          
1038         if (sk1->pair != NULL ) 
1039         {
1040                 sk2 = sk1->pair;
1041                 sk1->pair = NULL;
1042         } 
1043         else
1044         {
1045                 sk2 = sk1->prot->accept(sk1,flags);
1046                 if (sk2 == NULL) 
1047                 {
1048                         return sock_error(sk1);
1049                 }
1050         }
1051         newsock->data = (void *)sk2;
1052         sk2->sleep = newsock->wait;
1053         sk2->socket = newsock;
1054         newsock->conn = NULL;
1055         if (flags & O_NONBLOCK) 
1056                 return(0);
1057 
1058         cli(); /* avoid the race. */
1059         while(sk2->state == TCP_SYN_RECV) 
1060         {
1061                 interruptible_sleep_on(sk2->sleep);
1062                 if (current->signal & ~current->blocked) 
1063                 {
1064                         sti();
1065                         sk1->pair = sk2;
1066                         sk2->sleep = NULL;
1067                         sk2->socket=NULL;
1068                         newsock->data = NULL;
1069                         return(-ERESTARTSYS);
1070                 }
1071         }
1072         sti();
1073 
1074         if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) 
1075         {
1076                 err = sock_error(sk2);
1077                 destroy_sock(sk2);
1078                 newsock->data = NULL;
1079                 return err;
1080         }
1081         newsock->state = SS_CONNECTED;
1082         return(0);
1083 }
1084 
1085 
1086 /*
1087  *      This does both peername and sockname.
1088  */
1089  
1090 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1091                  int *uaddr_len, int peer)
1092 {
1093         struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
1094         struct sock *sk;
1095   
1096         sin->sin_family = AF_INET;
1097         sk = (struct sock *) sock->data;
1098         if (peer) 
1099         {
1100                 if (!tcp_connected(sk->state)) 
1101                         return(-ENOTCONN);
1102                 sin->sin_port = sk->dummy_th.dest;
1103                 sin->sin_addr.s_addr = sk->daddr;
1104         } 
1105         else 
1106         {
1107                 __u32 addr = sk->rcv_saddr;
1108                 if (!addr) {
1109                         addr = sk->saddr;
1110                         if (!addr)
1111                                 addr = ip_my_addr();
1112                 }
1113                 sin->sin_port = sk->dummy_th.source;
1114                 sin->sin_addr.s_addr = addr;
1115         }
1116         *uaddr_len = sizeof(*sin);
1117         return(0);
1118 }
1119 
1120 
1121 
1122 static int inet_recvmsg(struct socket *sock, struct msghdr *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1123                    int flags, int *addr_len )
1124 {
1125         struct sock *sk = (struct sock *) sock->data;
1126         
1127         if (sk->prot->recvmsg == NULL) 
1128                 return(-EOPNOTSUPP);
1129         if(sk->err)
1130                 return sock_error(sk);
1131         /* We may need to bind the socket. */
1132         if(inet_autobind(sk)!=0)
1133                 return(-EAGAIN);
1134         return(sk->prot->recvmsg(sk, ubuf, size, noblock, flags,addr_len));
1135 }
1136 
1137 
1138 static int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1139            int flags)
1140 {
1141         struct sock *sk = (struct sock *) sock->data;
1142         if (sk->shutdown & SEND_SHUTDOWN) 
1143         {
1144                 send_sig(SIGPIPE, current, 1);
1145                 return(-EPIPE);
1146         }
1147         if (sk->prot->sendmsg == NULL) 
1148                 return(-EOPNOTSUPP);
1149         if(sk->err)
1150                 return sock_error(sk);
1151         /* We may need to bind the socket. */
1152         if(inet_autobind(sk)!=0)
1153                 return -EAGAIN;
1154         return(sk->prot->sendmsg(sk, msg, size, noblock, flags));
1155                            
1156 }
1157 
1158 
1159 static int inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1160 {
1161         struct sock *sk=(struct sock*)sock->data;
1162 
1163         /*
1164          * This should really check to make sure
1165          * the socket is a TCP socket. (WHY AC...)
1166          */
1167         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1168                        1->2 bit 2 snds.
1169                        2->3 */
1170         if ((how & ~SHUTDOWN_MASK) || how==0)   /* MAXINT->0 */
1171                 return(-EINVAL);
1172         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1173                 sock->state = SS_CONNECTED;
1174         if (!sk || !tcp_connected(sk->state)) 
1175                 return(-ENOTCONN);
1176         sk->shutdown |= how;
1177         if (sk->prot->shutdown)
1178                 sk->prot->shutdown(sk, how);
1179         return(0);
1180 }
1181 
1182 
1183 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1184 {
1185         struct sock *sk=(struct sock *) sock->data;
1186         if (sk->prot->select == NULL) 
1187         {
1188                 return(0);
1189         }
1190         return(sk->prot->select(sk, sel_type, wait));
1191 }
1192 
1193 /*
1194  *      ioctl() calls you can issue on an INET socket. Most of these are
1195  *      device configuration and stuff and very rarely used. Some ioctls
1196  *      pass on to the socket itself.
1197  *
1198  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
1199  *      loads the devconfigure module does its configuring and unloads it.
1200  *      There's a good 20K of config code hanging around the kernel.
1201  */
1202 
1203 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1204 {
1205         struct sock *sk=(struct sock *)sock->data;
1206         int err;
1207         int pid;
1208 
1209         switch(cmd) 
1210         {
1211                 case FIOSETOWN:
1212                 case SIOCSPGRP:
1213                         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1214                         if(err)
1215                                 return err;
1216                         pid = get_user((int *) arg);
1217                         /* see inet_fcntl */
1218                         if (current->pid != pid && current->pgrp != -pid && !suser())
1219                                 return -EPERM;
1220                         sk->proc = pid;
1221                         return(0);
1222                 case FIOGETOWN:
1223                 case SIOCGPGRP:
1224                         err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1225                         if(err)
1226                                 return err;
1227                         put_fs_long(sk->proc,(int *)arg);
1228                         return(0);                      
1229                 case SIOCGSTAMP:
1230                         if(sk->stamp.tv_sec==0)
1231                                 return -ENOENT;
1232                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1233                         if(err)
1234                                 return err;
1235                         memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1236                         return 0;
1237                 case SIOCADDRT:
1238                 case SIOCDELRT:
1239                         return(ip_rt_ioctl(cmd,(void *) arg));
1240                 case SIOCDARP:
1241                 case SIOCGARP:
1242                 case SIOCSARP:
1243                 case OLD_SIOCDARP:
1244                 case OLD_SIOCGARP:
1245                 case OLD_SIOCSARP:
1246                         return(arp_ioctl(cmd,(void *) arg));
1247                 case SIOCDRARP:
1248                 case SIOCGRARP:
1249                 case SIOCSRARP:
1250 #ifdef CONFIG_KERNELD
1251                         if (rarp_ioctl_hook == NULL)
1252                                 request_module("rarp");
1253 #endif
1254                         if (rarp_ioctl_hook != NULL)
1255                                 return(rarp_ioctl_hook(cmd,(void *) arg));
1256                 case SIOCGIFCONF:
1257                 case SIOCGIFFLAGS:
1258                 case SIOCSIFFLAGS:
1259                 case SIOCGIFADDR:
1260                 case SIOCSIFADDR:
1261                 case SIOCADDMULTI:
1262                 case SIOCDELMULTI:
1263                 case SIOCGIFDSTADDR:
1264                 case SIOCSIFDSTADDR:
1265                 case SIOCGIFBRDADDR:
1266                 case SIOCSIFBRDADDR:
1267                 case SIOCGIFNETMASK:
1268                 case SIOCSIFNETMASK:
1269                 case SIOCGIFMETRIC:
1270                 case SIOCSIFMETRIC:
1271                 case SIOCGIFMEM:
1272                 case SIOCSIFMEM:
1273                 case SIOCGIFMTU:
1274                 case SIOCSIFMTU:
1275                 case SIOCSIFLINK:
1276                 case SIOCGIFHWADDR:
1277                 case SIOCSIFHWADDR:
1278                 case SIOCSIFMAP:
1279                 case SIOCGIFMAP:
1280                 case SIOCSIFSLAVE:
1281                 case SIOCGIFSLAVE:
1282                         return(dev_ioctl(cmd,(void *) arg));
1283 
1284                 case SIOCGIFBR:
1285                 case SIOCSIFBR:
1286 #ifdef CONFIG_BRIDGE            
1287                         return(br_ioctl(cmd,(void *) arg));
1288 #else
1289                         return -ENOPKG;
1290 #endif                                          
1291                         
1292                 default:
1293                         if ((cmd >= SIOCDEVPRIVATE) &&
1294                            (cmd <= (SIOCDEVPRIVATE + 15)))
1295                                 return(dev_ioctl(cmd,(void *) arg));
1296 
1297                         if (sk->prot->ioctl==NULL) 
1298                                 return(-EINVAL);
1299                         return(sk->prot->ioctl(sk, cmd, arg));
1300         }
1301         /*NOTREACHED*/
1302         return(0);
1303 }
1304 
1305 /*
1306  * This routine must find a socket given a TCP or UDP header.
1307  * Everything is assumed to be in net order.
1308  *
1309  * We give priority to more closely bound ports: if some socket
1310  * is bound to a particular foreign address, it will get the packet
1311  * rather than somebody listening to any address..
1312  */
1313 
1314 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1315                                 unsigned long raddr,
1316                                 unsigned short rnum, unsigned long laddr)
1317 {
1318         struct sock *s;
1319         struct sock *result = NULL;
1320         int badness = -1;
1321         unsigned short hnum;
1322 
1323         hnum = ntohs(num);
1324 
1325         /*
1326          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1327          * than a prime unless 3 or more sockets end up using the same
1328          * array entry.  This should not be a problem because most
1329          * well known sockets don't overlap that much, and for
1330          * the other ones, we can just be careful about picking our
1331          * socket number when we choose an arbitrary one.
1332          */
1333 
1334         for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1335                         s != NULL; s = s->next) 
1336         {
1337                 int score = 0;
1338 
1339                 if (s->num != hnum) 
1340                         continue;
1341 
1342                 if(s->dead && (s->state == TCP_CLOSE))
1343                         continue;
1344                 /* local address matches? */
1345                 if (s->rcv_saddr) {
1346                         if (s->rcv_saddr != laddr)
1347                                 continue;
1348                         score++;
1349                 }
1350                 /* remote address matches? */
1351                 if (s->daddr) {
1352                         if (s->daddr != raddr)
1353                                 continue;
1354                         score++;
1355                 }
1356                 /* remote port matches? */
1357                 if (s->dummy_th.dest) {
1358                         if (s->dummy_th.dest != rnum)
1359                                 continue;
1360                         score++;
1361                 }
1362                 /* perfect match? */
1363                 if (score == 3)
1364                         return s;
1365                 /* no, check if this is the best so far.. */
1366                 if (score <= badness)
1367                         continue;
1368                 result = s;
1369                 badness = score;
1370         }
1371         return result;
1372 }
1373 
1374 /*
1375  *      Deliver a datagram to raw sockets.
1376  */
1377  
1378 struct sock *get_sock_raw(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1379                                 unsigned short num,
1380                                 unsigned long raddr,
1381                                 unsigned long laddr)
1382 {
1383         struct sock *s;
1384 
1385         s=sk;
1386 
1387         for(; s != NULL; s = s->next) 
1388         {
1389                 if (s->num != num) 
1390                         continue;
1391                 if(s->dead && (s->state == TCP_CLOSE))
1392                         continue;
1393                 if(s->daddr && s->daddr!=raddr)
1394                         continue;
1395                 if(s->rcv_saddr && s->rcv_saddr != laddr)
1396                         continue;
1397                 return(s);
1398         }
1399         return(NULL);
1400 }
1401 
1402 #ifdef CONFIG_IP_MULTICAST
1403 /*
1404  *      Deliver a datagram to broadcast/multicast sockets.
1405  */
1406  
1407 struct sock *get_sock_mcast(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1408                                 unsigned short num,
1409                                 unsigned long raddr,
1410                                 unsigned short rnum, unsigned long laddr)
1411 {
1412         struct sock *s;
1413         unsigned short hnum;
1414 
1415         hnum = ntohs(num);
1416 
1417         /*
1418          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1419          * than a prime unless 3 or more sockets end up using the same
1420          * array entry.  This should not be a problem because most
1421          * well known sockets don't overlap that much, and for
1422          * the other ones, we can just be careful about picking our
1423          * socket number when we choose an arbitrary one.
1424          */
1425         
1426         s=sk;
1427 
1428         for(; s != NULL; s = s->next) 
1429         {
1430                 if (s->num != hnum) 
1431                         continue;
1432                 if(s->dead && (s->state == TCP_CLOSE))
1433                         continue;
1434                 if(s->daddr && s->daddr!=raddr)
1435                         continue;
1436                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1437                         continue;
1438                 if(s->rcv_saddr  && s->rcv_saddr != laddr)
1439                         continue;
1440                 return(s);
1441         }
1442         return(NULL);
1443 }
1444 
1445 #endif
1446 
1447 static struct proto_ops inet_proto_ops = {
1448         AF_INET,
1449 
1450         inet_create,
1451         inet_dup,
1452         inet_release,
1453         inet_bind,
1454         inet_connect,
1455         inet_socketpair,
1456         inet_accept,
1457         inet_getname, 
1458         inet_select,
1459         inet_ioctl,
1460         inet_listen,
1461         inet_shutdown,
1462         inet_setsockopt,
1463         inet_getsockopt,
1464         inet_fcntl,
1465         inet_sendmsg,
1466         inet_recvmsg
1467 };
1468 
1469 extern unsigned long seq_offset;
1470 
1471 /*
1472  *      Called by socket.c on kernel startup.  
1473  */
1474  
1475 void inet_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1476 {
1477         struct inet_protocol *p;
1478         int i;
1479 
1480 
1481         printk("Swansea University Computer Society TCP/IP for NET3.034\n");
1482 
1483         /*
1484          *      Tell SOCKET that we are alive... 
1485          */
1486    
1487         (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1488 
1489         seq_offset = CURRENT_TIME*250;
1490 
1491         /*
1492          *      Add all the protocols. 
1493          */
1494          
1495         for(i = 0; i < SOCK_ARRAY_SIZE; i++) 
1496         {
1497                 tcp_prot.sock_array[i] = NULL;
1498                 udp_prot.sock_array[i] = NULL;
1499                 raw_prot.sock_array[i] = NULL;
1500         }
1501         tcp_prot.inuse = 0;
1502         tcp_prot.highestinuse = 0;
1503         udp_prot.inuse = 0;
1504         udp_prot.highestinuse = 0;
1505         raw_prot.inuse = 0;
1506         raw_prot.highestinuse = 0;
1507 
1508         printk("IP Protocols: ");
1509         for(p = inet_protocol_base; p != NULL;) 
1510         {
1511                 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1512                 inet_add_protocol(p);
1513                 printk("%s%s",p->name,tmp?", ":"\n");
1514                 p = tmp;
1515         }
1516 
1517         /*
1518          *      Set the ARP module up
1519          */
1520         arp_init();
1521         /*
1522          *      Set the IP module up
1523          */
1524         ip_init();
1525         /*
1526          *      Set the ICMP layer up
1527          */
1528         icmp_init(&inet_proto_ops);
1529         /*
1530          *      Set the firewalling up
1531          */
1532 #if defined(CONFIG_IP_ACCT)||defined(CONFIG_IP_FIREWALL)|| \
1533     defined(CONFIG_IP_MASQUERADE)
1534         ip_fw_init();
1535 #endif
1536         /*
1537          *      Initialise the multicast router
1538          */
1539 #if defined(CONFIG_IP_MROUTE)
1540         ip_mr_init();
1541 #endif
1542 
1543         /*
1544          *  Initialise AF_INET alias type (register net_alias_type)
1545          */
1546 
1547 #if defined(CONFIG_IP_ALIAS)
1548         ip_alias_init();
1549 #endif
1550 
1551 #ifdef CONFIG_INET_RARP
1552         rarp_ioctl_hook = rarp_ioctl;
1553 #endif
1554         /*
1555          *      Create all the /proc entries.
1556          */
1557 
1558 #ifdef CONFIG_PROC_FS
1559 
1560 #ifdef CONFIG_INET_RARP
1561         proc_net_register(&(struct proc_dir_entry) {
1562                 PROC_NET_RARP, 4, "rarp",
1563                 S_IFREG | S_IRUGO, 1, 0, 0,
1564                 0, &proc_net_inode_operations,
1565                 rarp_get_info
1566         });
1567 #endif          /* RARP */
1568 
1569         proc_net_register(&(struct proc_dir_entry) {
1570                 PROC_NET_RAW, 3, "raw",
1571                 S_IFREG | S_IRUGO, 1, 0, 0,
1572                 0, &proc_net_inode_operations,
1573                 raw_get_info
1574         });
1575         proc_net_register(&(struct proc_dir_entry) {
1576                 PROC_NET_SNMP, 4, "snmp",
1577                 S_IFREG | S_IRUGO, 1, 0, 0,
1578                 0, &proc_net_inode_operations,
1579                 snmp_get_info
1580         });
1581         proc_net_register(&(struct proc_dir_entry) {
1582                 PROC_NET_SOCKSTAT, 8, "sockstat",
1583                 S_IFREG | S_IRUGO, 1, 0, 0,
1584                 0, &proc_net_inode_operations,
1585                 afinet_get_info
1586         });
1587         proc_net_register(&(struct proc_dir_entry) {
1588                 PROC_NET_TCP, 3, "tcp",
1589                 S_IFREG | S_IRUGO, 1, 0, 0,
1590                 0, &proc_net_inode_operations,
1591                 tcp_get_info
1592         });
1593         proc_net_register(&(struct proc_dir_entry) {
1594                 PROC_NET_UDP, 3, "udp",
1595                 S_IFREG | S_IRUGO, 1, 0, 0,
1596                 0, &proc_net_inode_operations,
1597                 udp_get_info
1598         });
1599         proc_net_register(&(struct proc_dir_entry) {
1600                 PROC_NET_ROUTE, 5, "route",
1601                 S_IFREG | S_IRUGO, 1, 0, 0,
1602                 0, &proc_net_inode_operations,
1603                 rt_get_info
1604         });
1605         proc_net_register(&(struct proc_dir_entry) {
1606                 PROC_NET_RTCACHE, 8, "rt_cache",
1607                 S_IFREG | S_IRUGO, 1, 0, 0,
1608                 0, &proc_net_inode_operations,
1609                 rt_cache_get_info
1610         });
1611 #endif          /* CONFIG_PROC_FS */
1612 }

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