root/net/inet/af_inet.c

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

DEFINITIONS

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

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