root/net/ipv4/af_inet.c

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

DEFINITIONS

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

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