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

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