home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / net / inet / af_inet.c next >
Encoding:
C/C++ Source or Header  |  1995-03-07  |  32.9 KB  |  1,565 lines

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