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

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