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

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