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

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