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

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