root/net/ipv4/af_inet.c

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

DEFINITIONS

This source file includes following definitions.
  1. sk_inuse
  2. get_new_socknum
  3. put_sock
  4. remove_sock
  5. destroy_sock
  6. inet_fcntl
  7. inet_setsockopt
  8. inet_getsockopt
  9. inet_autobind
  10. inet_listen
  11. def_callback1
  12. def_callback2
  13. def_callback3
  14. inet_create
  15. inet_dup
  16. closing
  17. inet_release
  18. inet_bind
  19. inet_error
  20. inet_connect
  21. inet_socketpair
  22. inet_accept
  23. inet_getname
  24. inet_recvfrom
  25. inet_recv
  26. inet_read
  27. inet_send
  28. inet_write
  29. inet_sendto
  30. inet_shutdown
  31. inet_select
  32. inet_ioctl
  33. get_sock
  34. get_sock_raw
  35. get_sock_mcast
  36. inet_proto_init

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

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