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. def_callback3
  14. inet_create
  15. inet_dup
  16. inet_release
  17. inet_bind
  18. inet_error
  19. inet_connect
  20. inet_socketpair
  21. inet_accept
  22. inet_getname
  23. inet_recvfrom
  24. inet_recv
  25. inet_read
  26. inet_send
  27. inet_write
  28. inet_sendto
  29. inet_shutdown
  30. inet_select
  31. inet_ioctl
  32. get_sock
  33. get_sock_raw
  34. get_sock_mcast
  35. inet_proto_init

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

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