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         /* Incase 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 (backlog > 5)
 478                 backlog = 5;
 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 
 783         if (sk->linger == 0) 
 784         {
 785                 sk->prot->close(sk,0);
 786                 sk->dead = 1;
 787         } 
 788         else 
 789         {
 790                 sk->prot->close(sk, 0);
 791                 cli();
 792                 if (sk->lingertime)
 793                         current->timeout = jiffies + HZ*sk->lingertime;
 794                 while(closing(sk) && current->timeout>0) 
 795                 {
 796                         interruptible_sleep_on(sk->sleep);
 797                         if (current->signal & ~current->blocked) 
 798                         {
 799                                 break;
 800 #if 0
 801                                 /* not working now - closes can't be restarted */
 802                                 sti();
 803                                 current->timeout=0;
 804                                 return(-ERESTARTSYS);
 805 #endif
 806                         }
 807                 }
 808                 current->timeout=0;
 809                 sti();
 810                 sk->dead = 1;
 811         }
 812         sk->inuse = 1;
 813 
 814         /* This will destroy it. */
 815         release_sock(sk);
 816         sock->data = NULL;
 817         sk->socket = NULL;
 818         return(0);
 819 }
 820 
 821 
 822 /* this needs to be changed to disallow
 823    the rebinding of sockets.   What error
 824    should it return? */
 825 
 826 static int inet_bind(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 827                int addr_len)
 828 {
 829         struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
 830         struct sock *sk=(struct sock *)sock->data, *sk2;
 831         unsigned short snum = 0 /* Stoopid compiler.. this IS ok */;
 832         int chk_addr_ret;
 833 
 834         /* check this error. */
 835         if (sk->state != TCP_CLOSE)
 836                 return(-EIO);
 837         if(addr_len<sizeof(struct sockaddr_in))
 838                 return -EINVAL;
 839                 
 840         if(sock->type != SOCK_RAW)
 841         {
 842                 if (sk->num != 0) 
 843                         return(-EINVAL);
 844 
 845                 snum = ntohs(addr->sin_port);
 846 
 847                 /*
 848                  * We can't just leave the socket bound wherever it is, it might
 849                  * be bound to a privileged port. However, since there seems to
 850                  * be a bug here, we will leave it if the port is not privileged.
 851                  */
 852                 if (snum == 0) 
 853                 {
 854                         snum = get_new_socknum(sk->prot, 0);
 855                 }
 856                 if (snum < PROT_SOCK && !suser()) 
 857                         return(-EACCES);
 858         }
 859         
 860         chk_addr_ret = ip_chk_addr(addr->sin_addr.s_addr);
 861         if (addr->sin_addr.s_addr != 0 && chk_addr_ret != IS_MYADDR && chk_addr_ret != IS_MULTICAST)
 862                 return(-EADDRNOTAVAIL); /* Source address MUST be ours! */
 863                 
 864         if (chk_addr_ret || addr->sin_addr.s_addr == 0)
 865                 sk->saddr = addr->sin_addr.s_addr;
 866         
 867         if(sock->type != SOCK_RAW)
 868         {
 869                 /* Make sure we are allowed to bind here. */
 870                 cli();
 871                 for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
 872                                         sk2 != NULL; sk2 = sk2->next) 
 873                 {
 874                 /* should be below! */
 875                         if (sk2->num != snum) 
 876                                 continue;
 877                         if (!sk->reuse)
 878                         {
 879                                 sti();
 880                                 return(-EADDRINUSE);
 881                         }
 882                         
 883                         if (sk2->num != snum) 
 884                                 continue;               /* more than one */
 885                         if (sk2->saddr != sk->saddr) 
 886                                 continue;       /* socket per slot ! -FB */
 887                         if (!sk2->reuse || sk2->state==TCP_LISTEN) 
 888                         {
 889                                 sti();
 890                                 return(-EADDRINUSE);
 891                         }
 892                 }
 893                 sti();
 894 
 895                 remove_sock(sk);
 896                 put_sock(snum, sk);
 897                 sk->dummy_th.source = ntohs(sk->num);
 898                 sk->daddr = 0;
 899                 sk->dummy_th.dest = 0;
 900         }
 901         return(0);
 902 }
 903 
 904 /*
 905  *      Handle sk->err properly. The cli/sti matter.
 906  */
 907  
 908 static int inet_error(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 909 {
 910         unsigned long flags;
 911         int err;
 912         save_flags(flags);
 913         cli();  
 914         err=sk->err;
 915         sk->err=0;
 916         restore_flags(flags);
 917         return -err;
 918 }
 919 
 920 /*
 921  *      Connect to a remote host. There is regrettably still a little
 922  *      TCP 'magic' in here.
 923  */
 924  
 925 static int inet_connect(struct socket *sock, struct sockaddr * uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 926                   int addr_len, int flags)
 927 {
 928         struct sock *sk=(struct sock *)sock->data;
 929         int err;
 930         sock->conn = NULL;
 931 
 932         if (sock->state == SS_CONNECTING && tcp_connected(sk->state))
 933         {
 934                 sock->state = SS_CONNECTED;
 935                 /* Connection completing after a connect/EINPROGRESS/select/connect */
 936                 return 0;       /* Rock and roll */
 937         }
 938 
 939         if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK))
 940                 return -EALREADY;       /* Connecting is currently in progress */
 941         
 942         if (sock->state != SS_CONNECTING) 
 943         {
 944                 /* We may need to bind the socket. */
 945                 if(inet_autobind(sk)!=0)
 946                         return(-EAGAIN);
 947                 if (sk->prot->connect == NULL) 
 948                         return(-EOPNOTSUPP);
 949                 err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
 950                 if (err < 0) 
 951                         return(err);
 952                 sock->state = SS_CONNECTING;
 953         }
 954         
 955         if (sk->state > TCP_FIN_WAIT2 && sock->state==SS_CONNECTING)
 956         {
 957                 sock->state=SS_UNCONNECTED;
 958                 cli();
 959                 err=sk->err;
 960                 sk->err=0;
 961                 sti();
 962                 return -err;
 963         }
 964 
 965         if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
 966                 return(-EINPROGRESS);
 967 
 968         cli(); /* avoid the race condition */
 969         while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
 970         {
 971                 interruptible_sleep_on(sk->sleep);
 972                 if (current->signal & ~current->blocked) 
 973                 {
 974                         sti();
 975                         return(-ERESTARTSYS);
 976                 }
 977                 /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
 978                    icmp error packets wanting to close a tcp or udp socket. */
 979                 if(sk->err && sk->protocol == IPPROTO_TCP)
 980                 {
 981                         sti();
 982                         sock->state = SS_UNCONNECTED;
 983                         err = -sk->err;
 984                         sk->err=0;
 985                         return err; /* set by tcp_err() */
 986                 }
 987         }
 988         sti();
 989         sock->state = SS_CONNECTED;
 990 
 991         if (sk->state != TCP_ESTABLISHED && sk->err) 
 992         {
 993                 sock->state = SS_UNCONNECTED;
 994                 err=sk->err;
 995                 sk->err=0;
 996                 return(-err);
 997         }
 998         return(0);
 999 }
1000 
1001 
1002 static int inet_socketpair(struct socket *sock1, struct socket *sock2)
     /* [previous][next][first][last][top][bottom][index][help] */
1003 {
1004          return(-EOPNOTSUPP);
1005 }
1006 
1007 
1008 /*
1009  *      Accept a pending connection. The TCP layer now gives BSD semantics.
1010  */
1011 
1012 static int inet_accept(struct socket *sock, struct socket *newsock, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1013 {
1014         struct sock *sk1, *sk2;
1015         int err;
1016 
1017         sk1 = (struct sock *) sock->data;
1018 
1019         /*
1020          * We've been passed an extra socket.
1021          * We need to free it up because the tcp module creates
1022          * it's own when it accepts one.
1023          */
1024         if (newsock->data)
1025         {
1026                 struct sock *sk=(struct sock *)newsock->data;
1027                 newsock->data=NULL;
1028                 sk->dead = 1;
1029                 destroy_sock(sk);
1030         }
1031   
1032         if (sk1->prot->accept == NULL) 
1033                 return(-EOPNOTSUPP);
1034 
1035         /* Restore the state if we have been interrupted, and then returned. */
1036         if (sk1->pair != NULL ) 
1037         {
1038                 sk2 = sk1->pair;
1039                 sk1->pair = NULL;
1040         } 
1041         else
1042         {
1043                 sk2 = sk1->prot->accept(sk1,flags);
1044                 if (sk2 == NULL) 
1045                 {
1046                         if (sk1->err <= 0)
1047                                 printk("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
1048                         err=sk1->err;
1049                         sk1->err=0;
1050                         return(-err);
1051                 }
1052         }
1053         newsock->data = (void *)sk2;
1054         sk2->sleep = newsock->wait;
1055         sk2->socket = newsock;
1056         newsock->conn = NULL;
1057         if (flags & O_NONBLOCK) 
1058                 return(0);
1059 
1060         cli(); /* avoid the race. */
1061         while(sk2->state == TCP_SYN_RECV) 
1062         {
1063                 interruptible_sleep_on(sk2->sleep);
1064                 if (current->signal & ~current->blocked) 
1065                 {
1066                         sti();
1067                         sk1->pair = sk2;
1068                         sk2->sleep = NULL;
1069                         sk2->socket=NULL;
1070                         newsock->data = NULL;
1071                         return(-ERESTARTSYS);
1072                 }
1073         }
1074         sti();
1075 
1076         if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) 
1077         {
1078                 err = -sk2->err;
1079                 sk2->err=0;
1080                 sk2->dead=1;    /* ANK */
1081                 destroy_sock(sk2);
1082                 newsock->data = NULL;
1083                 return(err);
1084         }
1085         newsock->state = SS_CONNECTED;
1086         return(0);
1087 }
1088 
1089 
1090 /*
1091  *      This does both peername and sockname.
1092  */
1093  
1094 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1095                  int *uaddr_len, int peer)
1096 {
1097         struct sockaddr_in *sin=(struct sockaddr_in *)uaddr;
1098         struct sock *sk;
1099   
1100         sin->sin_family = AF_INET;
1101         sk = (struct sock *) sock->data;
1102         if (peer) 
1103         {
1104                 if (!tcp_connected(sk->state)) 
1105                         return(-ENOTCONN);
1106                 sin->sin_port = sk->dummy_th.dest;
1107                 sin->sin_addr.s_addr = sk->daddr;
1108         } 
1109         else 
1110         {
1111                 sin->sin_port = sk->dummy_th.source;
1112                 if (sk->saddr == 0) 
1113                         sin->sin_addr.s_addr = ip_my_addr();
1114                 else 
1115                         sin->sin_addr.s_addr = sk->saddr;
1116         }
1117         *uaddr_len = sizeof(*sin);
1118         return(0);
1119 }
1120 
1121 
1122 /*
1123  *      The assorted BSD I/O operations
1124  */
1125 
1126 static int inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1127                    unsigned flags, struct sockaddr *sin, int *addr_len )
1128 {
1129         struct sock *sk = (struct sock *) sock->data;
1130         
1131         if (sk->prot->recvfrom == NULL) 
1132                 return(-EOPNOTSUPP);
1133         if(sk->err)
1134                 return inet_error(sk);
1135         /* We may need to bind the socket. */
1136         if(inet_autobind(sk)!=0)
1137                 return(-EAGAIN);
1138         return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
1139                              (struct sockaddr_in*)sin, addr_len));
1140 }
1141 
1142 
1143 static int inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
     /* [previous][next][first][last][top][bottom][index][help] */
1144           unsigned flags)
1145 {
1146         /* BSD explicitly states these are the same - so we do it this way to be sure */
1147         return inet_recvfrom(sock,ubuf,size,noblock,flags,NULL,NULL);
1148 }
1149 
1150 static int inet_read(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1151 {
1152         struct sock *sk = (struct sock *) sock->data;
1153         
1154         if(sk->err)
1155                 return inet_error(sk);
1156         /* We may need to bind the socket. */
1157         if(inet_autobind(sk))
1158                 return(-EAGAIN);        
1159         return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, 0));
1160 }
1161 
1162 static int inet_send(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1163                unsigned flags)
1164 {
1165         struct sock *sk = (struct sock *) sock->data;
1166         if (sk->shutdown & SEND_SHUTDOWN) 
1167         {
1168                 send_sig(SIGPIPE, current, 1);
1169                 return(-EPIPE);
1170         }
1171         if(sk->err)
1172                 return inet_error(sk);
1173         /* We may need to bind the socket. */
1174         if(inet_autobind(sk)!=0)
1175                 return(-EAGAIN);
1176         return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
1177 }
1178 
1179 static int inet_write(struct socket *sock, char *ubuf, int size, int noblock)
     /* [previous][next][first][last][top][bottom][index][help] */
1180 {
1181         return inet_send(sock,ubuf,size,noblock,0);
1182 }
1183 
1184 static int inet_sendto(struct socket *sock, void *ubuf, int size, int noblock, 
     /* [previous][next][first][last][top][bottom][index][help] */
1185             unsigned flags, struct sockaddr *sin, int addr_len)
1186 {
1187         struct sock *sk = (struct sock *) sock->data;
1188         if (sk->shutdown & SEND_SHUTDOWN) 
1189         {
1190                 send_sig(SIGPIPE, current, 1);
1191                 return(-EPIPE);
1192         }
1193         if (sk->prot->sendto == NULL) 
1194                 return(-EOPNOTSUPP);
1195         if(sk->err)
1196                 return inet_error(sk);
1197         /* We may need to bind the socket. */
1198         if(inet_autobind(sk)!=0)
1199                 return -EAGAIN;
1200         return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags, 
1201                            (struct sockaddr_in *)sin, addr_len));
1202 }
1203 
1204 
1205 static int inet_shutdown(struct socket *sock, int how)
     /* [previous][next][first][last][top][bottom][index][help] */
1206 {
1207         struct sock *sk=(struct sock*)sock->data;
1208 
1209         /*
1210          * This should really check to make sure
1211          * the socket is a TCP socket. (WHY AC...)
1212          */
1213         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
1214                        1->2 bit 2 snds.
1215                        2->3 */
1216         if ((how & ~SHUTDOWN_MASK) || how==0)   /* MAXINT->0 */
1217                 return(-EINVAL);
1218         if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
1219                 sock->state = SS_CONNECTED;
1220         if (!tcp_connected(sk->state)) 
1221                 return(-ENOTCONN);
1222         sk->shutdown |= how;
1223         if (sk->prot->shutdown)
1224                 sk->prot->shutdown(sk, how);
1225         return(0);
1226 }
1227 
1228 
1229 static int inet_select(struct socket *sock, int sel_type, select_table *wait )
     /* [previous][next][first][last][top][bottom][index][help] */
1230 {
1231         struct sock *sk=(struct sock *) sock->data;
1232         if (sk->prot->select == NULL) 
1233         {
1234                 return(0);
1235         }
1236         return(sk->prot->select(sk, sel_type, wait));
1237 }
1238 
1239 /*
1240  *      ioctl() calls you can issue on an INET socket. Most of these are
1241  *      device configuration and stuff and very rarely used. Some ioctls
1242  *      pass on to the socket itself.
1243  *
1244  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
1245  *      loads the devconfigure module does its configuring and unloads it.
1246  *      There's a good 20K of config code hanging around the kernel.
1247  */
1248 
1249 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1250 {
1251         struct sock *sk=(struct sock *)sock->data;
1252         int err;
1253 
1254         switch(cmd) 
1255         {
1256                 case FIOSETOWN:
1257                 case SIOCSPGRP:
1258                         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
1259                         if(err)
1260                                 return err;
1261                         sk->proc = get_fs_long((int *) arg);
1262                         return(0);
1263                 case FIOGETOWN:
1264                 case SIOCGPGRP:
1265                         err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
1266                         if(err)
1267                                 return err;
1268                         put_fs_long(sk->proc,(int *)arg);
1269                         return(0);                      
1270                 case SIOCGSTAMP:
1271                         if(sk->stamp.tv_sec==0)
1272                                 return -ENOENT;
1273                         err=verify_area(VERIFY_WRITE,(void *)arg,sizeof(struct timeval));
1274                         if(err)
1275                                 return err;
1276                         memcpy_tofs((void *)arg,&sk->stamp,sizeof(struct timeval));
1277                         return 0;
1278                 case SIOCADDRT: case SIOCADDRTOLD:
1279                 case SIOCDELRT: case SIOCDELRTOLD:
1280                         return(ip_rt_ioctl(cmd,(void *) arg));
1281                 case SIOCDARP:
1282                 case SIOCGARP:
1283                 case SIOCSARP:
1284                         return(arp_ioctl(cmd,(void *) arg));
1285 #ifdef CONFIG_INET_RARP                 
1286                 case SIOCDRARP:
1287                 case SIOCGRARP:
1288                 case SIOCSRARP:
1289                         return(rarp_ioctl(cmd,(void *) arg));
1290 #endif
1291                 case SIOCGIFCONF:
1292                 case SIOCGIFFLAGS:
1293                 case SIOCSIFFLAGS:
1294                 case SIOCGIFADDR:
1295                 case SIOCSIFADDR:
1296 
1297 /* begin multicast support change */
1298                 case SIOCADDMULTI:
1299                 case SIOCDELMULTI:
1300 /* end multicast support change */
1301                 
1302                 case SIOCGIFDSTADDR:
1303                 case SIOCSIFDSTADDR:
1304                 case SIOCGIFBRDADDR:
1305                 case SIOCSIFBRDADDR:
1306                 case SIOCGIFNETMASK:
1307                 case SIOCSIFNETMASK:
1308                 case SIOCGIFMETRIC:
1309                 case SIOCSIFMETRIC:
1310                 case SIOCGIFMEM:
1311                 case SIOCSIFMEM:
1312                 case SIOCGIFMTU:
1313                 case SIOCSIFMTU:
1314                 case SIOCSIFLINK:
1315                 case SIOCGIFHWADDR:
1316                 case SIOCSIFHWADDR:
1317                 case OLD_SIOCGIFHWADDR:
1318                 case SIOCSIFMAP:
1319                 case SIOCGIFMAP:
1320                 case SIOCSIFSLAVE:
1321                 case SIOCGIFSLAVE:
1322                         return(dev_ioctl(cmd,(void *) arg));
1323 
1324                 default:
1325                         if ((cmd >= SIOCDEVPRIVATE) &&
1326                            (cmd <= (SIOCDEVPRIVATE + 15)))
1327                                 return(dev_ioctl(cmd,(void *) arg));
1328 
1329                         if (sk->prot->ioctl==NULL) 
1330                                 return(-EINVAL);
1331                         return(sk->prot->ioctl(sk, cmd, arg));
1332         }
1333         /*NOTREACHED*/
1334         return(0);
1335 }
1336 
1337 /*
1338  * This routine must find a socket given a TCP or UDP header.
1339  * Everything is assumed to be in net order.
1340  *
1341  * We give priority to more closely bound ports: if some socket
1342  * is bound to a particular foreign address, it will get the packet
1343  * rather than somebody listening to any address..
1344  */
1345 
1346 struct sock *get_sock(struct proto *prot, unsigned short num,
     /* [previous][next][first][last][top][bottom][index][help] */
1347                                 unsigned long raddr,
1348                                 unsigned short rnum, unsigned long laddr)
1349 {
1350         struct sock *s;
1351         struct sock *result = NULL;
1352         int badness = -1;
1353         unsigned short hnum;
1354 
1355         hnum = ntohs(num);
1356 
1357         /*
1358          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1359          * than a prime unless 3 or more sockets end up using the same
1360          * array entry.  This should not be a problem because most
1361          * well known sockets don't overlap that much, and for
1362          * the other ones, we can just be careful about picking our
1363          * socket number when we choose an arbitrary one.
1364          */
1365 
1366         for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
1367                         s != NULL; s = s->next) 
1368         {
1369                 int score = 0;
1370 
1371                 if (s->num != hnum) 
1372                         continue;
1373 
1374                 if(s->dead && (s->state == TCP_CLOSE))
1375                         continue;
1376                 /* local address matches? */
1377                 if (s->saddr) {
1378                         if (s->saddr != laddr)
1379                                 continue;
1380                         score++;
1381                 }
1382                 /* remote address matches? */
1383                 if (s->daddr) {
1384                         if (s->daddr != raddr)
1385                                 continue;
1386                         score++;
1387                 }
1388                 /* remote port matches? */
1389                 if (s->dummy_th.dest) {
1390                         if (s->dummy_th.dest != rnum)
1391                                 continue;
1392                         score++;
1393                 }
1394                 /* perfect match? */
1395                 if (score == 3)
1396                         return s;
1397                 /* no, check if this is the best so far.. */
1398                 if (score <= badness)
1399                         continue;
1400                 result = s;
1401                 badness = score;
1402         }
1403         return result;
1404 }
1405 
1406 /*
1407  *      Deliver a datagram to raw sockets.
1408  */
1409  
1410 struct sock *get_sock_raw(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1411                                 unsigned short num,
1412                                 unsigned long raddr,
1413                                 unsigned long laddr)
1414 {
1415         struct sock *s;
1416 
1417         s=sk;
1418 
1419         for(; s != NULL; s = s->next) 
1420         {
1421                 if (s->num != num) 
1422                         continue;
1423                 if(s->dead && (s->state == TCP_CLOSE))
1424                         continue;
1425                 if(s->daddr && s->daddr!=raddr)
1426                         continue;
1427                 if(s->saddr  && s->saddr!=laddr)
1428                         continue;
1429                 return(s);
1430         }
1431         return(NULL);
1432 }
1433 
1434 #ifdef CONFIG_IP_MULTICAST
1435 /*
1436  *      Deliver a datagram to broadcast/multicast sockets.
1437  */
1438  
1439 struct sock *get_sock_mcast(struct sock *sk, 
     /* [previous][next][first][last][top][bottom][index][help] */
1440                                 unsigned short num,
1441                                 unsigned long raddr,
1442                                 unsigned short rnum, unsigned long laddr)
1443 {
1444         struct sock *s;
1445         unsigned short hnum;
1446 
1447         hnum = ntohs(num);
1448 
1449         /*
1450          * SOCK_ARRAY_SIZE must be a power of two.  This will work better
1451          * than a prime unless 3 or more sockets end up using the same
1452          * array entry.  This should not be a problem because most
1453          * well known sockets don't overlap that much, and for
1454          * the other ones, we can just be careful about picking our
1455          * socket number when we choose an arbitrary one.
1456          */
1457         
1458         s=sk;
1459 
1460         for(; s != NULL; s = s->next) 
1461         {
1462                 if (s->num != hnum) 
1463                         continue;
1464                 if(s->dead && (s->state == TCP_CLOSE))
1465                         continue;
1466                 if(s->daddr && s->daddr!=raddr)
1467                         continue;
1468                 if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
1469                         continue;
1470                 if(s->saddr  && s->saddr!=laddr)
1471                         continue;
1472                 return(s);
1473         }
1474         return(NULL);
1475 }
1476 
1477 #endif
1478 
1479 static struct proto_ops inet_proto_ops = {
1480         AF_INET,
1481 
1482         inet_create,
1483         inet_dup,
1484         inet_release,
1485         inet_bind,
1486         inet_connect,
1487         inet_socketpair,
1488         inet_accept,
1489         inet_getname, 
1490         inet_read,
1491         inet_write,
1492         inet_select,
1493         inet_ioctl,
1494         inet_listen,
1495         inet_send,
1496         inet_recv,
1497         inet_sendto,
1498         inet_recvfrom,
1499         inet_shutdown,
1500         inet_setsockopt,
1501         inet_getsockopt,
1502         inet_fcntl,
1503 };
1504 
1505 extern unsigned long seq_offset;
1506 
1507 /*
1508  *      Called by socket.c on kernel startup.  
1509  */
1510  
1511 void inet_proto_init(struct net_proto *pro)
     /* [previous][next][first][last][top][bottom][index][help] */
1512 {
1513         struct inet_protocol *p;
1514         int i;
1515 
1516 
1517         printk("Swansea University Computer Society TCP/IP for NET3.019\n");
1518 
1519         /*
1520          *      Tell SOCKET that we are alive... 
1521          */
1522    
1523         (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
1524 
1525         seq_offset = CURRENT_TIME*250;
1526 
1527         /*
1528          *      Add all the protocols. 
1529          */
1530          
1531         for(i = 0; i < SOCK_ARRAY_SIZE; i++) 
1532         {
1533                 tcp_prot.sock_array[i] = NULL;
1534                 udp_prot.sock_array[i] = NULL;
1535                 raw_prot.sock_array[i] = NULL;
1536         }
1537         tcp_prot.inuse = 0;
1538         tcp_prot.highestinuse = 0;
1539         udp_prot.inuse = 0;
1540         udp_prot.highestinuse = 0;
1541         raw_prot.inuse = 0;
1542         raw_prot.highestinuse = 0;
1543 
1544         printk("IP Protocols: ");
1545         for(p = inet_protocol_base; p != NULL;) 
1546         {
1547                 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1548                 inet_add_protocol(p);
1549                 printk("%s%s",p->name,tmp?", ":"\n");
1550                 p = tmp;
1551         }
1552         /*
1553          *      Set the ARP module up
1554          */
1555         arp_init();
1556         /*
1557          *      Set the IP module up
1558          */
1559         ip_init();
1560 }
1561 

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