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

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