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

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