root/net/ipv4/af_inet.c

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

DEFINITIONS

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

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

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