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

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