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

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