home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / net / inet / sock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-11  |  41.5 KB  |  1,897 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.  *        SOCK - AF_INET protocol family socket handler.
  7.  *
  8.  * Version:    @(#)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.  *
  14.  * Fixes:
  15.  *        Alan Cox    :     Numerous verify_area() problems
  16.  *        Alan Cox    :    Connecting on a connecting socket
  17.  *                    now returns an error for tcp.
  18.  *        Alan Cox    :    sock->protocol is set correctly.
  19.  *                    and is not sometimes left as 0.
  20.  *        Alan Cox    :    connect handles icmp errors on a
  21.  *                    connect properly. Unfortunately there
  22.  *                    is a restart syscall nasty there. I
  23.  *                    can't match BSD without hacking the C
  24.  *                    library. Ideas urgently sought!
  25.  *        Alan Cox    :    Disallow bind() to addresses that are
  26.  *                    not ours - especially broadcast ones!!
  27.  *        Alan Cox    :    Socket 1024 _IS_ ok for users. (fencepost)
  28.  *        Alan Cox    :    sock_wfree/sock_rfree don't destroy sockets,
  29.  *                    instead they leave that for the DESTROY timer.
  30.  *        Alan Cox    :    Clean up error flag in accept
  31.  *        Alan Cox    :    TCP ack handling is buggy, the DESTROY timer
  32.  *                    was buggy. Put a remove_sock() in the handler
  33.  *                    for memory when we hit 0. Also altered the timer
  34.  *                    code. The ACK stuff can wait and needs major 
  35.  *                    TCP layer surgery.
  36.  *        Alan Cox    :    Fixed TCP ack bug, removed remove sock
  37.  *                    and fixed timer/inet_bh race.
  38.  *        Alan Cox    :    Added zapped flag for TCP
  39.  *        Alan Cox    :    Move kfree_skb into skbuff.c and tidied up surplus code
  40.  *        Alan Cox    :    for new sk_buff allocations wmalloc/rmalloc now call alloc_skb
  41.  *        Alan Cox    :    kfree_s calls now are kfree_skbmem so we can track skb resources
  42.  *        Alan Cox    :    Supports socket option broadcast now as does udp. Packet and raw need fixing.
  43.  *        Alan Cox    :    Added RCVBUF,SNDBUF size setting. It suddenely occured to me how easy it was so...
  44.  *        Rick Sladkey    :    Relaxed UDP rules for matching packets.
  45.  *        C.E.Hawkins    :    IFF_PROMISC/SIOCGHWADDR support
  46.  *    Pauline Middelink    :    Pidentd support
  47.  *        Alan Cox    :    Fixed connect() taking signals I think.
  48.  *        Alan Cox    :    SO_LINGER supported
  49.  *        Alan Cox    :    Error reporting fixes
  50.  *        Anonymous    :    inet_create tidied up (sk->reuse setting)
  51.  *        Alan Cox    :    inet sockets don't set sk->type!
  52.  *        Alan Cox    :    Split socket option code
  53.  *        Alan Cox    :    Callbacks
  54.  *        Alan Cox    :    Nagle flag for Charles & Johannes stuff
  55.  *
  56.  * To Fix:
  57.  *
  58.  *
  59.  *        This program is free software; you can redistribute it and/or
  60.  *        modify it under the terms of the GNU General Public License
  61.  *        as published by the Free Software Foundation; either version
  62.  *        2 of the License, or (at your option) any later version.
  63.  */
  64.  
  65. #include <linux/config.h>
  66. #include <linux/errno.h>
  67. #include <linux/types.h>
  68. #include <linux/socket.h>
  69. #include <linux/in.h>
  70. #include <linux/kernel.h>
  71. #include <linux/major.h>
  72. #include <linux/sched.h>
  73. #include <linux/timer.h>
  74. #include <linux/string.h>
  75. #include <linux/sockios.h>
  76. #include <linux/net.h>
  77. #include <linux/fcntl.h>
  78. #include <linux/mm.h>
  79. #include <linux/interrupt.h>
  80.  
  81. #include <asm/segment.h>
  82. #include <asm/system.h>
  83.  
  84. #include "inet.h"
  85. #include "dev.h"
  86. #include "ip.h"
  87. #include "protocol.h"
  88. #include "arp.h"
  89. #include "route.h"
  90. #include "tcp.h"
  91. #include "udp.h"
  92. #include "skbuff.h"
  93. #include "sock.h"
  94. #include "raw.h"
  95. #include "icmp.h"
  96.  
  97.  
  98. int inet_debug = DBG_OFF;        /* INET module debug flag    */
  99.  
  100.  
  101. #define min(a,b)    ((a)<(b)?(a):(b))
  102.  
  103. extern struct proto packet_prot;
  104.  
  105.  
  106. void
  107. print_sk(struct sock *sk)
  108. {
  109.   if (!sk) {
  110.     printk("  print_sk(NULL)\n");
  111.     return;
  112.   }
  113.   printk("  wmem_alloc = %lu\n", sk->wmem_alloc);
  114.   printk("  rmem_alloc = %lu\n", sk->rmem_alloc);
  115.   printk("  send_head = %p\n", sk->send_head);
  116.   printk("  state = %d\n",sk->state);
  117.   printk("  wback = %p, rqueue = %p\n", sk->wback, sk->rqueue);
  118.   printk("  wfront = %p\n", sk->wfront);
  119.   printk("  daddr = %lX, saddr = %lX\n", sk->daddr,sk->saddr);
  120.   printk("  num = %d", sk->num);
  121.   printk(" next = %p\n", sk->next);
  122.   printk("  write_seq = %ld, acked_seq = %ld, copied_seq = %ld\n",
  123.       sk->write_seq, sk->acked_seq, sk->copied_seq);
  124.   printk("  rcv_ack_seq = %ld, window_seq = %ld, fin_seq = %ld\n",
  125.       sk->rcv_ack_seq, sk->window_seq, sk->fin_seq);
  126.   printk("  prot = %p\n", sk->prot);
  127.   printk("  pair = %p, back_log = %p\n", sk->pair,sk->back_log);
  128.   printk("  inuse = %d , blog = %d\n", sk->inuse, sk->blog);
  129.   printk("  dead = %d delay_acks=%d\n", sk->dead, sk->delay_acks);
  130.   printk("  retransmits = %ld, timeout = %d\n", sk->retransmits, sk->timeout);
  131.   printk("  cong_window = %d, packets_out = %d\n", sk->cong_window,
  132.       sk->packets_out);
  133.   printk("  shutdown=%d\n", sk->shutdown);
  134. }
  135.  
  136.  
  137. void
  138. print_skb(struct sk_buff *skb)
  139. {
  140.   if (!skb) {
  141.     printk("  print_skb(NULL)\n");
  142.     return;
  143.   }
  144.   printk("  prev = %p, next = %p\n", skb->prev, skb->next);
  145.   printk("  sk = %p link3 = %p\n", skb->sk, skb->link3);
  146.   printk("  mem_addr = %p, mem_len = %lu\n", skb->mem_addr, skb->mem_len);
  147.   printk("  used = %d free = %d\n", skb->used,skb->free);
  148. }
  149.  
  150.  
  151.  
  152. static int
  153. sk_inuse(struct proto *prot, int num)
  154. {
  155.   struct sock *sk;
  156.  
  157.   for(sk = prot->sock_array[num & (SOCK_ARRAY_SIZE -1 )];
  158.       sk != NULL;
  159.       sk=sk->next) {
  160.     if (sk->num == num) return(1);
  161.   }
  162.   return(0);
  163. }
  164.  
  165.  
  166. unsigned short
  167. get_new_socknum(struct proto *prot, unsigned short base)
  168. {
  169.   static int start=0;
  170.  
  171.   /*
  172.    * Used to cycle through the port numbers so the
  173.    * chances of a confused connection drop.
  174.    */
  175.   int i, j;
  176.   int best = 0;
  177.   int size = 32767; /* a big num. */
  178.   struct sock *sk;
  179.  
  180.   if (base == 0) base = PROT_SOCK+1+(start % 1024);
  181.   if (base <= PROT_SOCK) {
  182.     base += PROT_SOCK+(start % 1024);
  183.   }
  184.  
  185.   /* Now look through the entire array and try to find an empty ptr. */
  186.   for(i=0; i < SOCK_ARRAY_SIZE; i++) {
  187.     j = 0;
  188.     sk = prot->sock_array[(i+base+1) &(SOCK_ARRAY_SIZE -1)];
  189.     while(sk != NULL) {
  190.         sk = sk->next;
  191.         j++;
  192.     }
  193.     if (j == 0) {
  194.         start =(i+1+start )%1024;
  195.         DPRINTF((DBG_INET, "get_new_socknum returning %d, start = %d\n",
  196.                             i + base + 1, start));
  197.         return(i+base+1);
  198.     }
  199.     if (j < size) {
  200.         best = i;
  201.         size = j;
  202.     }
  203.   }
  204.  
  205.   /* Now make sure the one we want is not in use. */
  206.   while(sk_inuse(prot, base +best+1)) {
  207.     best += SOCK_ARRAY_SIZE;
  208.   }
  209.   DPRINTF((DBG_INET, "get_new_socknum returning %d, start = %d\n",
  210.                         best + base + 1, start));
  211.   return(best+base+1);
  212. }
  213.  
  214.  
  215. void
  216. put_sock(unsigned short num, struct sock *sk)
  217. {
  218.   struct sock *sk1;
  219.   struct sock *sk2;
  220.   int mask;
  221.  
  222.   DPRINTF((DBG_INET, "put_sock(num = %d, sk = %X\n", num, sk));
  223.   sk->num = num;
  224.   sk->next = NULL;
  225.   num = num &(SOCK_ARRAY_SIZE -1);
  226.  
  227.   /* We can't have an interupt re-enter here. */
  228.   cli();
  229.   if (sk->prot->sock_array[num] == NULL) {
  230.     sk->prot->sock_array[num] = sk;
  231.     sti();
  232.     return;
  233.   }
  234.   sti();
  235.   for(mask = 0xff000000; mask != 0xffffffff; mask = (mask >> 8) | mask) {
  236.     if ((mask & sk->saddr) &&
  237.         (mask & sk->saddr) != (mask & 0xffffffff)) {
  238.         mask = mask << 8;
  239.         break;
  240.     }
  241.   }
  242.   DPRINTF((DBG_INET, "mask = %X\n", mask));
  243.  
  244.   cli();
  245.   sk1 = sk->prot->sock_array[num];
  246.   for(sk2 = sk1; sk2 != NULL; sk2=sk2->next) {
  247.     if (!(sk2->saddr & mask)) {
  248.         if (sk2 == sk1) {
  249.             sk->next = sk->prot->sock_array[num];
  250.             sk->prot->sock_array[num] = sk;
  251.             sti();
  252.             return;
  253.         }
  254.         sk->next = sk2;
  255.         sk1->next= sk;
  256.         sti();
  257.         return;
  258.     }
  259.     sk1 = sk2;
  260.   }
  261.  
  262.   /* Goes at the end. */
  263.   sk->next = NULL;
  264.   sk1->next = sk;
  265.   sti();
  266. }
  267.  
  268.  
  269. static void
  270. remove_sock(struct sock *sk1)
  271. {
  272.   struct sock *sk2;
  273.  
  274.   DPRINTF((DBG_INET, "remove_sock(sk1=%X)\n", sk1));
  275.   if (!sk1) {
  276.     printk("sock.c: remove_sock: sk1 == NULL\n");
  277.     return;
  278.   }
  279.  
  280.   if (!sk1->prot) {
  281.     printk("sock.c: remove_sock: sk1->prot == NULL\n");
  282.     return;
  283.   }
  284.  
  285.   /* We can't have this changing out from under us. */
  286.   cli();
  287.   sk2 = sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)];
  288.   if (sk2 == sk1) {
  289.     sk1->prot->sock_array[sk1->num &(SOCK_ARRAY_SIZE -1)] = sk1->next;
  290.     sti();
  291.     return;
  292.   }
  293.  
  294.   while(sk2 && sk2->next != sk1) {
  295.     sk2 = sk2->next;
  296.   }
  297.  
  298.   if (sk2) {
  299.     sk2->next = sk1->next;
  300.     sti();
  301.     return;
  302.   }
  303.   sti();
  304.  
  305.   if (sk1->num != 0) DPRINTF((DBG_INET, "remove_sock: sock not found.\n"));
  306. }
  307.  
  308.  
  309. void
  310. destroy_sock(struct sock *sk)
  311. {
  312.     struct sk_buff *skb;
  313.  
  314.       DPRINTF((DBG_INET, "destroying socket %X\n", sk));
  315.       sk->inuse = 1;            /* just to be safe. */
  316.  
  317.       /* Incase it's sleeping somewhere. */
  318.       if (!sk->dead) 
  319.           sk->write_space(sk);
  320.  
  321.       remove_sock(sk);
  322.   
  323.       /* Now we can no longer get new packets. */
  324.       delete_timer(sk);
  325.  
  326.  
  327.     while ((skb = tcp_dequeue_partial(sk)) != NULL) 
  328.       {
  329.           IS_SKB(skb);
  330.           kfree_skb(skb, FREE_WRITE);
  331.       }
  332.  
  333.   /* Cleanup up the write buffer. */
  334.       for(skb = sk->wfront; skb != NULL; ) 
  335.       {
  336.         struct sk_buff *skb2;
  337.  
  338.         skb2=(struct sk_buff *)skb->next;
  339.         if (skb->magic != TCP_WRITE_QUEUE_MAGIC) {
  340.             printk("sock.c:destroy_sock write queue with bad magic(%X)\n",
  341.                                 skb->magic);
  342.             break;
  343.         }
  344.         IS_SKB(skb);
  345.         kfree_skb(skb, FREE_WRITE);
  346.         skb = skb2;
  347.       }
  348.  
  349.       sk->wfront = NULL;
  350.       sk->wback = NULL;
  351.  
  352.       if (sk->rqueue != NULL) 
  353.       {
  354.           while((skb=skb_dequeue(&sk->rqueue))!=NULL)
  355.           {
  356.         /*
  357.          * This will take care of closing sockets that were
  358.          * listening and didn't accept everything.
  359.          */
  360.             if (skb->sk != NULL && skb->sk != sk) 
  361.             {
  362.                 IS_SKB(skb);
  363.                 skb->sk->dead = 1;
  364.                 skb->sk->prot->close(skb->sk, 0);
  365.             }
  366.             IS_SKB(skb);
  367.             kfree_skb(skb, FREE_READ);
  368.         }
  369.       }
  370.       sk->rqueue = NULL;
  371.  
  372.   /* Now we need to clean up the send head. */
  373.       for(skb = sk->send_head; skb != NULL; ) 
  374.       {
  375.         struct sk_buff *skb2;
  376.  
  377.         /*
  378.          * We need to remove skb from the transmit queue,
  379.          * or maybe the arp queue.
  380.          */
  381.         cli();
  382.         /* see if it's in a transmit queue. */
  383.         /* this can be simplified quite a bit.  Look */
  384.         /* at tcp.c:tcp_ack to see how. */
  385.         if (skb->next != NULL) 
  386.         {
  387.             IS_SKB(skb);
  388.             skb_unlink(skb);
  389.         }
  390.         skb->dev = NULL;
  391.         sti();
  392.         skb2 = (struct sk_buff *)skb->link3;
  393.         kfree_skb(skb, FREE_WRITE);
  394.         skb = skb2;
  395.       }    
  396.       sk->send_head = NULL;
  397.  
  398.       /* And now the backlog. */
  399.       if (sk->back_log != NULL) 
  400.       {
  401.         /* this should never happen. */
  402.         printk("cleaning back_log. \n");
  403.         cli();
  404.         skb = (struct sk_buff *)sk->back_log;
  405.         do 
  406.         {
  407.             struct sk_buff *skb2;
  408.     
  409.             skb2 = (struct sk_buff *)skb->next;
  410.             kfree_skb(skb, FREE_READ);
  411.             skb = skb2;
  412.         }
  413.         while(skb != sk->back_log);
  414.         sti();
  415.     }
  416.     sk->back_log = NULL;
  417.  
  418.   /* Now if it has a half accepted/ closed socket. */
  419.     if (sk->pair) 
  420.     {
  421.         sk->pair->dead = 1;
  422.         sk->pair->prot->close(sk->pair, 0);
  423.         sk->pair = NULL;
  424.       }
  425.  
  426.   /*
  427.    * Now if everything is gone we can free the socket
  428.    * structure, otherwise we need to keep it around until
  429.    * everything is gone.
  430.    */
  431.       if (sk->rmem_alloc == 0 && sk->wmem_alloc == 0) 
  432.       {
  433.         kfree_s((void *)sk,sizeof(*sk));
  434.       } 
  435.       else 
  436.       {
  437.         /* this should never happen. */
  438.         /* actually it can if an ack has just been sent. */
  439.         DPRINTF((DBG_INET, "possible memory leak in socket = %X\n", sk));
  440.         sk->destroy = 1;
  441.         sk->ack_backlog = 0;
  442.         sk->inuse = 0;
  443.         reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
  444.       }
  445.       DPRINTF((DBG_INET, "leaving destroy_sock\n"));
  446. }
  447.  
  448.  
  449. static int
  450. inet_fcntl(struct socket *sock, unsigned int cmd, unsigned long arg)
  451. {
  452.   struct sock *sk;
  453.  
  454.   sk = (struct sock *) sock->data;
  455.   if (sk == NULL) {
  456.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  457.     return(0);
  458.   }
  459.  
  460.   switch(cmd) {
  461.     case F_SETOWN:
  462.         /*
  463.          * This is a little restrictive, but it's the only
  464.          * way to make sure that you can't send a sigurg to
  465.          * another process.
  466.          */
  467.         if (!suser() && current->pgrp != -arg &&
  468.                 current->pid != arg) return(-EPERM);
  469.         sk->proc = arg;
  470.         return(0);
  471.     case F_GETOWN:
  472.         return(sk->proc);
  473.     default:
  474.         return(-EINVAL);
  475.   }
  476. }
  477.  
  478. /*
  479.  *    Set socket options on an inet socket.
  480.  */
  481.  
  482. static int inet_setsockopt(struct socket *sock, int level, int optname,
  483.             char *optval, int optlen)
  484. {
  485.       struct sock *sk = (struct sock *) sock->data;  
  486.     if (level == SOL_SOCKET)
  487.         return sock_setsockopt(sk,level,optname,optval,optlen);
  488.     if (sk->prot->setsockopt==NULL)
  489.         return(-EOPNOTSUPP);
  490.     else
  491.         return sk->prot->setsockopt(sk,level,optname,optval,optlen);
  492. }
  493.  
  494.  
  495.  
  496.  
  497. static int inet_getsockopt(struct socket *sock, int level, int optname,
  498.             char *optval, int *optlen)
  499. {
  500.       struct sock *sk = (struct sock *) sock->data;      
  501.       if (level == SOL_SOCKET) 
  502.           return sock_getsockopt(sk,level,optname,optval,optlen);
  503.       if(sk->prot->getsockopt==NULL)      
  504.           return(-EOPNOTSUPP);
  505.       else
  506.           return sk->prot->getsockopt(sk,level,optname,optval,optlen);
  507. }
  508.  
  509. /*
  510.  *    This is meant for all protocols to use and covers goings on
  511.  *    at the socket level. Everything here is generic.
  512.  */
  513.  
  514. int sock_setsockopt(struct sock *sk, int level, int optname,
  515.         char *optval, int optlen)
  516. {
  517.     int val;
  518.     int err;
  519.     struct linger ling;
  520.  
  521.       if (optval == NULL) 
  522.           return(-EINVAL);
  523.  
  524.       err=verify_area(VERIFY_READ, optval, sizeof(int));
  525.       if(err)
  526.           return err;
  527.       
  528.       val = get_fs_long((unsigned long *)optval);
  529.       switch(optname) 
  530.       {
  531.         case SO_TYPE:
  532.         case SO_ERROR:
  533.               return(-ENOPROTOOPT);
  534.  
  535.         case SO_DEBUG:    
  536.             sk->debug=val?1:0;
  537.         case SO_DONTROUTE:    /* Still to be implemented */
  538.             return(0);
  539.         case SO_BROADCAST:
  540.             sk->broadcast=val?1:0;
  541.             return 0;
  542.         case SO_SNDBUF:
  543.             if(val>32767)
  544.                 val=32767;
  545.             if(val<256)
  546.                 val=256;
  547.             sk->sndbuf=val;
  548.             return 0;
  549.         case SO_LINGER:
  550.             err=verify_area(VERIFY_READ,optval,sizeof(ling));
  551.             if(err)
  552.                 return err;
  553.             memcpy_fromfs(&ling,optval,sizeof(ling));
  554.             if(ling.l_onoff==0)
  555.                 sk->linger=0;
  556.             else
  557.             {
  558.                 sk->lingertime=ling.l_linger;
  559.                 sk->linger=1;
  560.             }
  561.             return 0;
  562.         case SO_RCVBUF:
  563.             if(val>32767)
  564.                 val=32767;
  565.             if(val<256)
  566.                 val=256;
  567.             sk->rcvbuf=val;
  568.             return(0);
  569.  
  570.         case SO_REUSEADDR:
  571.             if (val) 
  572.                 sk->reuse = 1;
  573.             else 
  574.                 sk->reuse = 0;
  575.             return(0);
  576.  
  577.         case SO_KEEPALIVE:
  578.             if (val)
  579.                 sk->keepopen = 1;
  580.             else 
  581.                 sk->keepopen = 0;
  582.             return(0);
  583.  
  584.          case SO_OOBINLINE:
  585.             if (val) 
  586.                 sk->urginline = 1;
  587.             else 
  588.                 sk->urginline = 0;
  589.             return(0);
  590.  
  591.          case SO_NO_CHECK:
  592.             if (val) 
  593.                 sk->no_check = 1;
  594.             else 
  595.                 sk->no_check = 0;
  596.             return(0);
  597.  
  598.          case SO_PRIORITY:
  599.             if (val >= 0 && val < DEV_NUMBUFFS) 
  600.             {
  601.                 sk->priority = val;
  602.             } 
  603.             else 
  604.             {
  605.                 return(-EINVAL);
  606.             }
  607.             return(0);
  608.  
  609.         default:
  610.               return(-ENOPROTOOPT);
  611.       }
  612. }
  613.  
  614.  
  615. int sock_getsockopt(struct sock *sk, int level, int optname,
  616.            char *optval, int *optlen)
  617. {        
  618.       int val;
  619.       int err;
  620.       struct linger ling;
  621.  
  622.       switch(optname) 
  623.       {
  624.         case SO_DEBUG:        
  625.             val = sk->debug;
  626.             break;
  627.         
  628.         case SO_DONTROUTE:    /* One last option to implement */
  629.             val = 0;
  630.             break;
  631.         
  632.         case SO_BROADCAST:
  633.             val= sk->broadcast;
  634.             break;
  635.         
  636.         case SO_LINGER:    
  637.             err=verify_area(VERIFY_WRITE,optval,sizeof(ling));
  638.             if(err)
  639.                 return err;
  640.             err=verify_area(VERIFY_WRITE,optlen,sizeof(int));
  641.             if(err)
  642.                 return err;
  643.             put_fs_long(sizeof(ling),(unsigned long *)optlen);
  644.             ling.l_onoff=sk->linger;
  645.             ling.l_linger=sk->lingertime;
  646.             memcpy_tofs(optval,&ling,sizeof(ling));
  647.             return 0;
  648.         
  649.         case SO_SNDBUF:
  650.             val=sk->sndbuf;
  651.             break;
  652.         
  653.         case SO_RCVBUF:
  654.             val =sk->rcvbuf;
  655.             break;
  656.  
  657.         case SO_REUSEADDR:
  658.             val = sk->reuse;
  659.             break;
  660.  
  661.         case SO_KEEPALIVE:
  662.             val = sk->keepopen;
  663.             break;
  664.  
  665.         case SO_TYPE:
  666.             if (sk->prot == &tcp_prot) 
  667.                 val = SOCK_STREAM;
  668.               else 
  669.                   val = SOCK_DGRAM;
  670.             break;
  671.  
  672.         case SO_ERROR:
  673.             val = sk->err;
  674.             sk->err = 0;
  675.             break;
  676.  
  677.         case SO_OOBINLINE:
  678.             val = sk->urginline;
  679.             break;
  680.     
  681.         case SO_NO_CHECK:
  682.             val = sk->no_check;
  683.             break;
  684.  
  685.         case SO_PRIORITY:
  686.             val = sk->priority;
  687.             break;
  688.  
  689.         default:
  690.             return(-ENOPROTOOPT);
  691.     }
  692.     err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
  693.     if(err)
  694.           return err;
  695.       put_fs_long(sizeof(int),(unsigned long *) optlen);
  696.  
  697.       err=verify_area(VERIFY_WRITE, optval, sizeof(int));
  698.       if(err)
  699.           return err;
  700.       put_fs_long(val,(unsigned long *)optval);
  701.  
  702.       return(0);
  703. }
  704.  
  705.  
  706.  
  707.  
  708. static int
  709. inet_listen(struct socket *sock, int backlog)
  710. {
  711.   struct sock *sk;
  712.  
  713.   sk = (struct sock *) sock->data;
  714.   if (sk == NULL) {
  715.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  716.     return(0);
  717.   }
  718.  
  719.   /* We may need to bind the socket. */
  720.   if (sk->num == 0) {
  721.     sk->num = get_new_socknum(sk->prot, 0);
  722.     if (sk->num == 0) return(-EAGAIN);
  723.     put_sock(sk->num, sk);
  724.     sk->dummy_th.source = ntohs(sk->num);
  725.   }
  726.  
  727.   /* We might as well re use these. */ 
  728.   sk->max_ack_backlog = backlog;
  729.   if (sk->state != TCP_LISTEN) {
  730.     sk->ack_backlog = 0;
  731.     sk->state = TCP_LISTEN;
  732.   }
  733.   return(0);
  734. }
  735.  
  736. /*
  737.  *    Default callbacks for user INET sockets. These just wake up
  738.  *    the user owning the socket.
  739.  */
  740.  
  741. static void def_callback1(struct sock *sk)
  742. {
  743.     if(!sk->dead)
  744.         wake_up_interruptible(sk->sleep);
  745. }
  746.  
  747. static void def_callback2(struct sock *sk,int len)
  748. {
  749.     if(!sk->dead)
  750.         wake_up_interruptible(sk->sleep);
  751. }
  752.  
  753.  
  754. static int
  755. inet_create(struct socket *sock, int protocol)
  756. {
  757.   struct sock *sk;
  758.   struct proto *prot;
  759.   int err;
  760.  
  761.   sk = (struct sock *) kmalloc(sizeof(*sk), GFP_KERNEL);
  762.   if (sk == NULL) 
  763.       return(-ENOMEM);
  764.   sk->num = 0;
  765.   sk->reuse = 0;
  766.   switch(sock->type) {
  767.     case SOCK_STREAM:
  768.     case SOCK_SEQPACKET:
  769.         if (protocol && protocol != IPPROTO_TCP) {
  770.             kfree_s((void *)sk, sizeof(*sk));
  771.             return(-EPROTONOSUPPORT);
  772.         }
  773.         protocol = IPPROTO_TCP;
  774.         sk->no_check = TCP_NO_CHECK;
  775.         prot = &tcp_prot;
  776.         break;
  777.  
  778.     case SOCK_DGRAM:
  779.         if (protocol && protocol != IPPROTO_UDP) {
  780.             kfree_s((void *)sk, sizeof(*sk));
  781.             return(-EPROTONOSUPPORT);
  782.         }
  783.         protocol = IPPROTO_UDP;
  784.         sk->no_check = UDP_NO_CHECK;
  785.         prot=&udp_prot;
  786.         break;
  787.       
  788.     case SOCK_RAW:
  789.         if (!suser()) {
  790.             kfree_s((void *)sk, sizeof(*sk));
  791.             return(-EPERM);
  792.         }
  793.         if (!protocol) {
  794.             kfree_s((void *)sk, sizeof(*sk));
  795.             return(-EPROTONOSUPPORT);
  796.         }
  797.         prot = &raw_prot;
  798.         sk->reuse = 1;
  799.         sk->no_check = 0;    /*
  800.                      * Doesn't matter no checksum is
  801.                      * preformed anyway.
  802.                      */
  803.         sk->num = protocol;
  804.         break;
  805.  
  806.     case SOCK_PACKET:
  807.         if (!suser()) {
  808.             kfree_s((void *)sk, sizeof(*sk));
  809.             return(-EPERM);
  810.         }
  811.         if (!protocol) {
  812.             kfree_s((void *)sk, sizeof(*sk));
  813.             return(-EPROTONOSUPPORT);
  814.         }
  815.         prot = &packet_prot;
  816.         sk->reuse = 1;
  817.         sk->no_check = 0;    /* Doesn't matter no checksum is
  818.                      * preformed anyway.
  819.                      */
  820.         sk->num = protocol;
  821.         break;
  822.  
  823.     default:
  824.         kfree_s((void *)sk, sizeof(*sk));
  825.         return(-ESOCKTNOSUPPORT);
  826.   }
  827.   sk->socket = sock;
  828. #ifdef CONFIG_TCP_NAGLE_OFF
  829.   sk->nonagle = 1;
  830. #else    
  831.   sk->nonagle = 0;
  832. #endif  
  833.   sk->type = sock->type;
  834.   sk->protocol = protocol;
  835.   sk->wmem_alloc = 0;
  836.   sk->rmem_alloc = 0;
  837.   sk->sndbuf = SK_WMEM_MAX;
  838.   sk->rcvbuf = SK_RMEM_MAX;
  839.   sk->pair = NULL;
  840.   sk->opt = NULL;
  841.   sk->write_seq = 0;
  842.   sk->acked_seq = 0;
  843.   sk->copied_seq = 0;
  844.   sk->fin_seq = 0;
  845.   sk->urg_seq = 0;
  846.   sk->urg_data = 0;
  847.   sk->proc = 0;
  848.   sk->rtt = TCP_WRITE_TIME << 3;
  849.   sk->rto = TCP_WRITE_TIME;
  850.   sk->mdev = 0;
  851.   sk->backoff = 0;
  852.   sk->packets_out = 0;
  853.   sk->cong_window = 1; /* start with only sending one packet at a time. */
  854.   sk->cong_count = 0;
  855.   sk->ssthresh = 0;
  856.   sk->max_window = 0;
  857.   sk->urginline = 0;
  858.   sk->intr = 0;
  859.   sk->linger = 0;
  860.   sk->destroy = 0;
  861.  
  862.   sk->priority = 1;
  863.   sk->shutdown = 0;
  864.   sk->keepopen = 0;
  865.   sk->zapped = 0;
  866.   sk->done = 0;
  867.   sk->ack_backlog = 0;
  868.   sk->window = 0;
  869.   sk->bytes_rcv = 0;
  870.   sk->state = TCP_CLOSE;
  871.   sk->dead = 0;
  872.   sk->ack_timed = 0;
  873.   sk->partial = NULL;
  874.   sk->user_mss = 0;
  875.   sk->debug = 0;
  876.  
  877.   /* this is how many unacked bytes we will accept for this socket.  */
  878.   sk->max_unacked = 2048; /* needs to be at most 2 full packets. */
  879.  
  880.   /* how many packets we should send before forcing an ack. 
  881.      if this is set to zero it is the same as sk->delay_acks = 0 */
  882.   sk->max_ack_backlog = 0;
  883.   sk->inuse = 0;
  884.   sk->delay_acks = 0;
  885.   sk->wback = NULL;
  886.   sk->wfront = NULL;
  887.   sk->rqueue = NULL;
  888.   sk->mtu = 576;
  889.   sk->prot = prot;
  890.   sk->sleep = sock->wait;
  891.   sk->daddr = 0;
  892.   sk->saddr = my_addr();
  893.   sk->err = 0;
  894.   sk->next = NULL;
  895.   sk->pair = NULL;
  896.   sk->send_tail = NULL;
  897.   sk->send_head = NULL;
  898.   sk->timeout = 0;
  899.   sk->broadcast = 0;
  900.   sk->timer.data = (unsigned long)sk;
  901.   sk->timer.function = &net_timer;
  902.   sk->back_log = NULL;
  903.   sk->blog = 0;
  904.   sock->data =(void *) sk;
  905.   sk->dummy_th.doff = sizeof(sk->dummy_th)/4;
  906.   sk->dummy_th.res1=0;
  907.   sk->dummy_th.res2=0;
  908.   sk->dummy_th.urg_ptr = 0;
  909.   sk->dummy_th.fin = 0;
  910.   sk->dummy_th.syn = 0;
  911.   sk->dummy_th.rst = 0;
  912.   sk->dummy_th.psh = 0;
  913.   sk->dummy_th.ack = 0;
  914.   sk->dummy_th.urg = 0;
  915.   sk->dummy_th.dest = 0;
  916.  
  917.   sk->ip_tos=0;
  918.   sk->ip_ttl=64;
  919.       
  920.   sk->state_change = def_callback1;
  921.   sk->data_ready = def_callback2;
  922.   sk->write_space = def_callback1;
  923.   sk->error_report = def_callback1;
  924.  
  925.   if (sk->num) {
  926.     /*
  927.      * It assumes that any protocol which allows
  928.      * the user to assign a number at socket
  929.      * creation time automatically
  930.      * shares.
  931.      */
  932.     put_sock(sk->num, sk);
  933.     sk->dummy_th.source = ntohs(sk->num);
  934.   }
  935.  
  936.   if (sk->prot->init) {
  937.     err = sk->prot->init(sk);
  938.     if (err != 0) {
  939.         destroy_sock(sk);
  940.         return(err);
  941.     }
  942.   }
  943.   return(0);
  944. }
  945.  
  946.  
  947. static int
  948. inet_dup(struct socket *newsock, struct socket *oldsock)
  949. {
  950.   return(inet_create(newsock,
  951.            ((struct sock *)(oldsock->data))->protocol));
  952. }
  953.  
  954.  
  955. /* The peer socket should always be NULL. */
  956. static int
  957. inet_release(struct socket *sock, struct socket *peer)
  958. {
  959.   struct sock *sk;
  960.  
  961.   sk = (struct sock *) sock->data;
  962.   if (sk == NULL) return(0);
  963.  
  964.   DPRINTF((DBG_INET, "inet_release(sock = %X, peer = %X)\n", sock, peer));
  965.   sk->state_change(sk);
  966.  
  967.   /* Start closing the connection.  This may take a while. */
  968.   /*
  969.    * If linger is set, we don't return until the close
  970.    * is complete.  Other wise we return immediately. The
  971.    * actually closing is done the same either way.
  972.    */
  973.   if (sk->linger == 0) {
  974.     sk->prot->close(sk,0);
  975.     sk->dead = 1;
  976.   } else {
  977.     DPRINTF((DBG_INET, "sk->linger set.\n"));
  978.     sk->prot->close(sk, 0);
  979.     cli();
  980.     if (sk->lingertime)
  981.         current->timeout = jiffies + HZ*sk->lingertime;
  982.     while(sk->state != TCP_CLOSE && current->timeout>0) {
  983.         interruptible_sleep_on(sk->sleep);
  984.         if (current->signal & ~current->blocked) {
  985.             break;
  986. #if 0
  987.             /* not working now - closes can't be restarted */
  988.             sti();
  989.             current->timeout=0;
  990.             return(-ERESTARTSYS);
  991. #endif
  992.         }
  993.     }
  994.     current->timeout=0;
  995.     sti();
  996.     sk->dead = 1;
  997.   }
  998.   sk->inuse = 1;
  999.  
  1000.   /* This will destroy it. */
  1001.   release_sock(sk);
  1002.   sock->data = NULL;
  1003.   DPRINTF((DBG_INET, "inet_release returning\n"));
  1004.   return(0);
  1005. }
  1006.  
  1007.  
  1008. /* this needs to be changed to dissallow
  1009.    the rebinding of sockets.   What error
  1010.    should it return? */
  1011.  
  1012. static int
  1013. inet_bind(struct socket *sock, struct sockaddr *uaddr,
  1014.            int addr_len)
  1015. {
  1016.   struct sockaddr_in addr;
  1017.   struct sock *sk, *sk2;
  1018.   unsigned short snum;
  1019.   int err;
  1020.  
  1021.   sk = (struct sock *) sock->data;
  1022.   if (sk == NULL) {
  1023.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1024.     return(0);
  1025.   }
  1026.  
  1027.   /* check this error. */
  1028.   if (sk->state != TCP_CLOSE) return(-EIO);
  1029.   if (sk->num != 0) return(-EINVAL);
  1030.  
  1031.   err=verify_area(VERIFY_READ, uaddr, addr_len);
  1032.   if(err)
  1033.       return err;
  1034.   memcpy_fromfs(&addr, uaddr, min(sizeof(addr), addr_len));
  1035.  
  1036.   snum = ntohs(addr.sin_port);
  1037.   DPRINTF((DBG_INET, "bind sk =%X to port = %d\n", sk, snum));
  1038.   sk = (struct sock *) sock->data;
  1039.  
  1040.   /*
  1041.    * We can't just leave the socket bound wherever it is, it might
  1042.    * be bound to a privileged port. However, since there seems to
  1043.    * be a bug here, we will leave it if the port is not privileged.
  1044.    */
  1045.   if (snum == 0) {
  1046.     snum = get_new_socknum(sk->prot, 0);
  1047.   }
  1048.   if (snum < PROT_SOCK && !suser()) return(-EACCES);
  1049.  
  1050.   if (addr.sin_addr.s_addr!=0 && chk_addr(addr.sin_addr.s_addr)!=IS_MYADDR)
  1051.       return(-EADDRNOTAVAIL);    /* Source address MUST be ours! */
  1052.       
  1053.   if (chk_addr(addr.sin_addr.s_addr) || addr.sin_addr.s_addr == 0)
  1054.                     sk->saddr = addr.sin_addr.s_addr;
  1055.  
  1056.   DPRINTF((DBG_INET, "sock_array[%d] = %X:\n", snum &(SOCK_ARRAY_SIZE -1),
  1057.               sk->prot->sock_array[snum &(SOCK_ARRAY_SIZE -1)]));
  1058.  
  1059.   /* Make sure we are allowed to bind here. */
  1060.   cli();
  1061. outside_loop:
  1062.   for(sk2 = sk->prot->sock_array[snum & (SOCK_ARRAY_SIZE -1)];
  1063.                     sk2 != NULL; sk2 = sk2->next) {
  1064. #if     1    /* should be below! */
  1065.     if (sk2->num != snum) continue;
  1066. /*    if (sk2->saddr != sk->saddr) continue; */
  1067. #endif
  1068.     if (sk2->dead) {
  1069.         destroy_sock(sk2);
  1070.         goto outside_loop;
  1071.     }
  1072.     if (!sk->reuse) {
  1073.         sti();
  1074.         return(-EADDRINUSE);
  1075.     }
  1076.     if (sk2->num != snum) continue;        /* more than one */
  1077.     if (sk2->saddr != sk->saddr) continue;    /* socket per slot ! -FB */
  1078.     if (!sk2->reuse) {
  1079.         sti();
  1080.         return(-EADDRINUSE);
  1081.     }
  1082.   }
  1083.   sti();
  1084.  
  1085.   remove_sock(sk);
  1086.   put_sock(snum, sk);
  1087.   sk->dummy_th.source = ntohs(sk->num);
  1088.   sk->daddr = 0;
  1089.   sk->dummy_th.dest = 0;
  1090.   return(0);
  1091. }
  1092.  
  1093.  
  1094. static int
  1095. inet_connect(struct socket *sock, struct sockaddr * uaddr,
  1096.           int addr_len, int flags)
  1097. {
  1098.   struct sock *sk;
  1099.   int err;
  1100.  
  1101.   sock->conn = NULL;
  1102.   sk = (struct sock *) sock->data;
  1103.   if (sk == NULL) {
  1104.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1105.     return(0);
  1106.   }
  1107.  
  1108.   if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
  1109.   {
  1110.     sock->state = SS_CONNECTED;
  1111.   /* Connection completing after a connect/EINPROGRESS/select/connect */
  1112.     return 0;    /* Rock and roll */
  1113.   }
  1114.  
  1115.   if (sock->state == SS_CONNECTING && sk->protocol == IPPROTO_TCP &&
  1116.       (flags & O_NONBLOCK))
  1117.       return -EALREADY;    /* Connecting is currently in progress */
  1118.       
  1119.   if (sock->state != SS_CONNECTING) {
  1120.     /* We may need to bind the socket. */
  1121.     if (sk->num == 0) {
  1122.         sk->num = get_new_socknum(sk->prot, 0);
  1123.         if (sk->num == 0) 
  1124.             return(-EAGAIN);
  1125.         put_sock(sk->num, sk);
  1126.         sk->dummy_th.source = htons(sk->num);
  1127.     }
  1128.  
  1129.     if (sk->prot->connect == NULL) 
  1130.         return(-EOPNOTSUPP);
  1131.   
  1132.     err = sk->prot->connect(sk, (struct sockaddr_in *)uaddr, addr_len);
  1133.     if (err < 0) return(err);
  1134.   
  1135.     sock->state = SS_CONNECTING;
  1136.   }
  1137.  
  1138.   if (sk->state != TCP_ESTABLISHED &&(flags & O_NONBLOCK)) 
  1139.       return(-EINPROGRESS);
  1140.  
  1141.   cli(); /* avoid the race condition */
  1142.   while(sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) 
  1143.   {
  1144.     interruptible_sleep_on(sk->sleep);
  1145.     if (current->signal & ~current->blocked) {
  1146.         sti();
  1147.         return(-ERESTARTSYS);
  1148.     }
  1149.     /* This fixes a nasty in the tcp/ip code. There is a hideous hassle with
  1150.        icmp error packets wanting to close a tcp or udp socket. */
  1151.     if(sk->err && sk->protocol == IPPROTO_TCP)
  1152.     {
  1153.         sti();
  1154.         sock->state = SS_UNCONNECTED;
  1155.         err = -sk->err;
  1156.         sk->err=0;
  1157.         return err; /* set by tcp_err() */
  1158.     }
  1159.   }
  1160.   sti();
  1161.   sock->state = SS_CONNECTED;
  1162.  
  1163.   if (sk->state != TCP_ESTABLISHED && sk->err) {
  1164.     sock->state = SS_UNCONNECTED;
  1165.     err=sk->err;
  1166.     sk->err=0;
  1167.     return(-err);
  1168.   }
  1169.   return(0);
  1170. }
  1171.  
  1172.  
  1173. static int
  1174. inet_socketpair(struct socket *sock1, struct socket *sock2)
  1175. {
  1176.   return(-EOPNOTSUPP);
  1177. }
  1178.  
  1179.  
  1180. static int
  1181. inet_accept(struct socket *sock, struct socket *newsock, int flags)
  1182. {
  1183.   struct sock *sk1, *sk2;
  1184.   int err;
  1185.  
  1186.   sk1 = (struct sock *) sock->data;
  1187.   if (sk1 == NULL) {
  1188.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1189.     return(0);
  1190.   }
  1191.  
  1192.   /*
  1193.    * We've been passed an extra socket.
  1194.    * We need to free it up because the tcp module creates
  1195.    * it's own when it accepts one.
  1196.    */
  1197.   if (newsock->data) kfree_s(newsock->data, sizeof(struct sock));
  1198.   newsock->data = NULL;
  1199.  
  1200.   if (sk1->prot->accept == NULL) return(-EOPNOTSUPP);
  1201.  
  1202.   /* Restore the state if we have been interrupted, and then returned. */
  1203.   if (sk1->pair != NULL ) {
  1204.     sk2 = sk1->pair;
  1205.     sk1->pair = NULL;
  1206.   } else {
  1207.     sk2 = sk1->prot->accept(sk1,flags);
  1208.     if (sk2 == NULL) {
  1209.         if (sk1->err <= 0)
  1210.             printk("Warning sock.c:sk1->err <= 0.  Returning non-error.\n");
  1211.         err=sk1->err;
  1212.         sk1->err=0;
  1213.         return(-err);
  1214.     }
  1215.   }
  1216.   newsock->data = (void *)sk2;
  1217.   sk2->sleep = newsock->wait;
  1218.   newsock->conn = NULL;
  1219.   if (flags & O_NONBLOCK) return(0);
  1220.  
  1221.   cli(); /* avoid the race. */
  1222.   while(sk2->state == TCP_SYN_RECV) {
  1223.     interruptible_sleep_on(sk2->sleep);
  1224.     if (current->signal & ~current->blocked) {
  1225.         sti();
  1226.         sk1->pair = sk2;
  1227.         sk2->sleep = NULL;
  1228.         newsock->data = NULL;
  1229.         return(-ERESTARTSYS);
  1230.     }
  1231.   }
  1232.   sti();
  1233.  
  1234.   if (sk2->state != TCP_ESTABLISHED && sk2->err > 0) {
  1235.  
  1236.     err = -sk2->err;
  1237.     sk2->err=0;
  1238.     destroy_sock(sk2);
  1239.     newsock->data = NULL;
  1240.     return(err);
  1241.   }
  1242.   newsock->state = SS_CONNECTED;
  1243.   return(0);
  1244. }
  1245.  
  1246.  
  1247. static int
  1248. inet_getname(struct socket *sock, struct sockaddr *uaddr,
  1249.          int *uaddr_len, int peer)
  1250. {
  1251.   struct sockaddr_in sin;
  1252.   struct sock *sk;
  1253.   int len;
  1254.   int err;
  1255.   
  1256.   
  1257.   err = verify_area(VERIFY_WRITE,uaddr_len,sizeof(long));
  1258.   if(err)
  1259.       return err;
  1260.       
  1261.   len=get_fs_long(uaddr_len);
  1262.   
  1263.   err = verify_area(VERIFY_WRITE, uaddr, len);
  1264.   if(err)
  1265.       return err;
  1266.       
  1267.   /* Check this error. */
  1268.   if (len < sizeof(sin)) return(-EINVAL);
  1269.  
  1270.   sin.sin_family = AF_INET;
  1271.   sk = (struct sock *) sock->data;
  1272.   if (sk == NULL) {
  1273.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1274.     return(0);
  1275.   }
  1276.   if (peer) {
  1277.     if (!tcp_connected(sk->state)) return(-ENOTCONN);
  1278.     sin.sin_port = sk->dummy_th.dest;
  1279.     sin.sin_addr.s_addr = sk->daddr;
  1280.   } else {
  1281.     sin.sin_port = sk->dummy_th.source;
  1282.     if (sk->saddr == 0) sin.sin_addr.s_addr = my_addr();
  1283.       else sin.sin_addr.s_addr = sk->saddr;
  1284.   }
  1285.   len = sizeof(sin);
  1286. /*  verify_area(VERIFY_WRITE, uaddr, len); NOW DONE ABOVE */
  1287.   memcpy_tofs(uaddr, &sin, sizeof(sin));
  1288. /*  verify_area(VERIFY_WRITE, uaddr_len, sizeof(len)); NOW DONE ABOVE */
  1289.   put_fs_long(len, uaddr_len);
  1290.   return(0);
  1291. }
  1292.  
  1293.  
  1294. static int
  1295. inet_read(struct socket *sock, char *ubuf, int size, int noblock)
  1296. {
  1297.   struct sock *sk;
  1298.  
  1299.   sk = (struct sock *) sock->data;
  1300.   if (sk == NULL) {
  1301.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1302.     return(0);
  1303.   }
  1304.  
  1305.   /* We may need to bind the socket. */
  1306.   if (sk->num == 0) {
  1307.     sk->num = get_new_socknum(sk->prot, 0);
  1308.     if (sk->num == 0) return(-EAGAIN);
  1309.     put_sock(sk->num, sk);
  1310.     sk->dummy_th.source = ntohs(sk->num);
  1311.   }
  1312.   return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock,0));
  1313. }
  1314.  
  1315.  
  1316. static int
  1317. inet_recv(struct socket *sock, void *ubuf, int size, int noblock,
  1318.       unsigned flags)
  1319. {
  1320.   struct sock *sk;
  1321.  
  1322.   sk = (struct sock *) sock->data;
  1323.   if (sk == NULL) {
  1324.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1325.     return(0);
  1326.   }
  1327.  
  1328.   /* We may need to bind the socket. */
  1329.   if (sk->num == 0) {
  1330.     sk->num = get_new_socknum(sk->prot, 0);
  1331.     if (sk->num == 0) return(-EAGAIN);
  1332.     put_sock(sk->num, sk);
  1333.     sk->dummy_th.source = ntohs(sk->num);
  1334.   }
  1335.   return(sk->prot->read(sk, (unsigned char *) ubuf, size, noblock, flags));
  1336. }
  1337.  
  1338.  
  1339. static int
  1340. inet_write(struct socket *sock, char *ubuf, int size, int noblock)
  1341. {
  1342.   struct sock *sk;
  1343.  
  1344.   sk = (struct sock *) sock->data;
  1345.   if (sk == NULL) {
  1346.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1347.     return(0);
  1348.   }
  1349.   if (sk->shutdown & SEND_SHUTDOWN) {
  1350.     send_sig(SIGPIPE, current, 1);
  1351.     return(-EPIPE);
  1352.   }
  1353.  
  1354.   /* We may need to bind the socket. */
  1355.   if (sk->num == 0) {
  1356.     sk->num = get_new_socknum(sk->prot, 0);
  1357.     if (sk->num == 0) return(-EAGAIN);
  1358.     put_sock(sk->num, sk);
  1359.     sk->dummy_th.source = ntohs(sk->num);
  1360.   }
  1361.  
  1362.   return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, 0));
  1363. }
  1364.  
  1365.  
  1366. static int
  1367. inet_send(struct socket *sock, void *ubuf, int size, int noblock, 
  1368.            unsigned flags)
  1369. {
  1370.   struct sock *sk;
  1371.  
  1372.   sk = (struct sock *) sock->data;
  1373.   if (sk == NULL) {
  1374.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1375.     return(0);
  1376.   }
  1377.   if (sk->shutdown & SEND_SHUTDOWN) {
  1378.     send_sig(SIGPIPE, current, 1);
  1379.     return(-EPIPE);
  1380.   }
  1381.  
  1382.   /* We may need to bind the socket. */
  1383.   if (sk->num == 0) {
  1384.     sk->num = get_new_socknum(sk->prot, 0);
  1385.     if (sk->num == 0) return(-EAGAIN);
  1386.     put_sock(sk->num, sk);
  1387.     sk->dummy_th.source = ntohs(sk->num);
  1388.   }
  1389.  
  1390.   return(sk->prot->write(sk, (unsigned char *) ubuf, size, noblock, flags));
  1391. }
  1392.  
  1393.  
  1394. static int
  1395. inet_sendto(struct socket *sock, void *ubuf, int size, int noblock, 
  1396.         unsigned flags, struct sockaddr *sin, int addr_len)
  1397. {
  1398.   struct sock *sk;
  1399.  
  1400.   sk = (struct sock *) sock->data;
  1401.   if (sk == NULL) {
  1402.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1403.     return(0);
  1404.   }
  1405.   if (sk->shutdown & SEND_SHUTDOWN) {
  1406.     send_sig(SIGPIPE, current, 1);
  1407.     return(-EPIPE);
  1408.   }
  1409.  
  1410.   if (sk->prot->sendto == NULL) return(-EOPNOTSUPP);
  1411.  
  1412.   /* We may need to bind the socket. */
  1413.   if (sk->num == 0) {
  1414.     sk->num = get_new_socknum(sk->prot, 0);
  1415.     if (sk->num == 0) return(-EAGAIN);
  1416.     put_sock(sk->num, sk);
  1417.     sk->dummy_th.source = ntohs(sk->num);
  1418.   }
  1419.  
  1420.   return(sk->prot->sendto(sk, (unsigned char *) ubuf, size, noblock, flags, 
  1421.                (struct sockaddr_in *)sin, addr_len));
  1422. }
  1423.  
  1424.  
  1425. static int
  1426. inet_recvfrom(struct socket *sock, void *ubuf, int size, int noblock, 
  1427.            unsigned flags, struct sockaddr *sin, int *addr_len )
  1428. {
  1429.   struct sock *sk;
  1430.  
  1431.   sk = (struct sock *) sock->data;
  1432.   if (sk == NULL) {
  1433.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1434.     return(0);
  1435.   }
  1436.  
  1437.   if (sk->prot->recvfrom == NULL) return(-EOPNOTSUPP);
  1438.  
  1439.   /* We may need to bind the socket. */
  1440.   if (sk->num == 0) {
  1441.     sk->num = get_new_socknum(sk->prot, 0);
  1442.     if (sk->num == 0) return(-EAGAIN);
  1443.     put_sock(sk->num, sk);
  1444.     sk->dummy_th.source = ntohs(sk->num);
  1445.   }
  1446.  
  1447.   return(sk->prot->recvfrom(sk, (unsigned char *) ubuf, size, noblock, flags,
  1448.                  (struct sockaddr_in*)sin, addr_len));
  1449. }
  1450.  
  1451.  
  1452. static int
  1453. inet_shutdown(struct socket *sock, int how)
  1454. {
  1455.   struct sock *sk;
  1456.  
  1457.   /*
  1458.    * This should really check to make sure
  1459.    * the socket is a TCP socket.
  1460.    */
  1461.   how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
  1462.                1->2 bit 2 snds.
  1463.                2->3 */
  1464.   if (how & ~SHUTDOWN_MASK) return(-EINVAL);
  1465.   sk = (struct sock *) sock->data;
  1466.   if (sk == NULL) {
  1467.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1468.     return(0);
  1469.   }
  1470.   if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
  1471.                         sock->state = SS_CONNECTED;
  1472.  
  1473.   if (!tcp_connected(sk->state)) return(-ENOTCONN);
  1474.   sk->shutdown |= how;
  1475.   if (sk->prot->shutdown) sk->prot->shutdown(sk, how);
  1476.   return(0);
  1477. }
  1478.  
  1479.  
  1480. static int
  1481. inet_select(struct socket *sock, int sel_type, select_table *wait )
  1482. {
  1483.   struct sock *sk;
  1484.  
  1485.   sk = (struct sock *) sock->data;
  1486.   if (sk == NULL) {
  1487.     printk("Warning: sock->data = NULL: %d\n" ,__LINE__);
  1488.     return(0);
  1489.   }
  1490.  
  1491.   if (sk->prot->select == NULL) {
  1492.     DPRINTF((DBG_INET, "select on non-selectable socket.\n"));
  1493.     return(0);
  1494.   }
  1495.   return(sk->prot->select(sk, sel_type, wait));
  1496. }
  1497.  
  1498.  
  1499. static int
  1500. inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1501. {
  1502.   struct sock *sk;
  1503.   int err;
  1504.  
  1505.   DPRINTF((DBG_INET, "INET: in inet_ioctl\n"));
  1506.   sk = NULL;
  1507.   if (sock && (sk = (struct sock *) sock->data) == NULL) {
  1508.     printk("AF_INET: Warning: sock->data = NULL: %d\n" , __LINE__);
  1509.     return(0);
  1510.   }
  1511.  
  1512.   switch(cmd) {
  1513.     case FIOSETOWN:
  1514.     case SIOCSPGRP:
  1515.         err=verify_area(VERIFY_READ,(int *)arg,sizeof(long));
  1516.         if(err)
  1517.             return err;
  1518.         if (sk)
  1519.             sk->proc = get_fs_long((int *) arg);
  1520.         return(0);
  1521.     case FIOGETOWN:
  1522.     case SIOCGPGRP:
  1523.         if (sk) {
  1524.             err=verify_area(VERIFY_WRITE,(void *) arg, sizeof(long));
  1525.             if(err)
  1526.                 return err;
  1527.             put_fs_long(sk->proc,(int *)arg);
  1528.         }
  1529.         return(0);
  1530. #if 0    /* FIXME: */
  1531.     case SIOCATMARK:
  1532.         printk("AF_INET: ioctl(SIOCATMARK, 0x%08X)\n",(void *) arg);
  1533.         return(-EINVAL);
  1534. #endif
  1535.  
  1536.     case DDIOCSDBG:
  1537.         return(dbg_ioctl((void *) arg, DBG_INET));
  1538.  
  1539.     case SIOCADDRT: case SIOCADDRTOLD:
  1540.     case SIOCDELRT: case SIOCDELRTOLD:
  1541.         return(rt_ioctl(cmd,(void *) arg));
  1542.  
  1543.     case SIOCDARP:
  1544.     case SIOCGARP:
  1545.     case SIOCSARP:
  1546.         return(arp_ioctl(cmd,(void *) arg));
  1547.  
  1548.     case IP_SET_DEV:
  1549.     case SIOCGIFCONF:
  1550.     case SIOCGIFFLAGS:
  1551.     case SIOCSIFFLAGS:
  1552.     case SIOCGIFADDR:
  1553.     case SIOCSIFADDR:
  1554.     case SIOCGIFDSTADDR:
  1555.     case SIOCSIFDSTADDR:
  1556.     case SIOCGIFBRDADDR:
  1557.     case SIOCSIFBRDADDR:
  1558.     case SIOCGIFNETMASK:
  1559.     case SIOCSIFNETMASK:
  1560.     case SIOCGIFMETRIC:
  1561.     case SIOCSIFMETRIC:
  1562.     case SIOCGIFMEM:
  1563.     case SIOCSIFMEM:
  1564.     case SIOCGIFMTU:
  1565.     case SIOCSIFMTU:
  1566.     case SIOCSIFLINK:
  1567.     case SIOCGIFHWADDR:
  1568.         return(dev_ioctl(cmd,(void *) arg));
  1569.  
  1570.     default:
  1571.         if (!sk || !sk->prot->ioctl) return(-EINVAL);
  1572.         return(sk->prot->ioctl(sk, cmd, arg));
  1573.   }
  1574.   /*NOTREACHED*/
  1575.   return(0);
  1576. }
  1577.  
  1578.  
  1579. struct sk_buff *
  1580. sock_wmalloc(struct sock *sk, unsigned long size, int force,
  1581.          int priority)
  1582. {
  1583.   if (sk) {
  1584.     if (sk->wmem_alloc + size < sk->sndbuf || force) {
  1585.         struct sk_buff * c = alloc_skb(size, priority);
  1586.         if (c) {
  1587.             cli();
  1588.             sk->wmem_alloc+= size;
  1589.             sti();
  1590.         }
  1591.         return c;
  1592.     }
  1593.     DPRINTF((DBG_INET, "sock_wmalloc(%X,%d,%d,%d) returning NULL\n",
  1594.                         sk, size, force, priority));
  1595.     return(NULL);
  1596.   }
  1597.   return(alloc_skb(size, priority));
  1598. }
  1599.  
  1600.  
  1601. struct sk_buff *
  1602. sock_rmalloc(struct sock *sk, unsigned long size, int force, int priority)
  1603. {
  1604.   if (sk) {
  1605.     if (sk->rmem_alloc + size < sk->rcvbuf || force) {
  1606.         struct sk_buff *c = alloc_skb(size, priority);
  1607.         if (c) {
  1608.             cli();
  1609.             sk->rmem_alloc += size;
  1610.             sti();
  1611.         }
  1612.         return(c);
  1613.     }
  1614.     DPRINTF((DBG_INET, "sock_rmalloc(%X,%d,%d,%d) returning NULL\n",
  1615.                         sk,size,force, priority));
  1616.     return(NULL);
  1617.   }
  1618.   return(alloc_skb(size, priority));
  1619. }
  1620.  
  1621.  
  1622. unsigned long
  1623. sock_rspace(struct sock *sk)
  1624. {
  1625.   int amt;
  1626.  
  1627.   if (sk != NULL) {
  1628.     if (sk->rmem_alloc >= sk->rcvbuf-2*MIN_WINDOW) return(0);
  1629.     amt = min((sk->rcvbuf-sk->rmem_alloc)/2-MIN_WINDOW, MAX_WINDOW);
  1630.     if (amt < 0) return(0);
  1631.     return(amt);
  1632.   }
  1633.   return(0);
  1634. }
  1635.  
  1636.  
  1637. unsigned long
  1638. sock_wspace(struct sock *sk)
  1639. {
  1640.   if (sk != NULL) {
  1641.     if (sk->shutdown & SEND_SHUTDOWN) return(0);
  1642.     if (sk->wmem_alloc >= sk->sndbuf) return(0);
  1643.     return(sk->sndbuf-sk->wmem_alloc );
  1644.   }
  1645.   return(0);
  1646. }
  1647.  
  1648.  
  1649. void
  1650. sock_wfree(struct sock *sk, void *mem, unsigned long size)
  1651. {
  1652.   DPRINTF((DBG_INET, "sock_wfree(sk=%X, mem=%X, size=%d)\n", sk, mem, size));
  1653.  
  1654.   IS_SKB(mem);
  1655.   kfree_skbmem(mem, size);
  1656.   if (sk) {
  1657.     sk->wmem_alloc -= size;
  1658.  
  1659.     /* In case it might be waiting for more memory. */
  1660.     if (!sk->dead) sk->write_space(sk);
  1661.     if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0) {
  1662.         DPRINTF((DBG_INET,
  1663.             "recovered lost memory, sock = %X\n", sk));
  1664.     }
  1665.     return;
  1666.   }
  1667. }
  1668.  
  1669.  
  1670. void
  1671. sock_rfree(struct sock *sk, void *mem, unsigned long size)
  1672. {
  1673.   DPRINTF((DBG_INET, "sock_rfree(sk=%X, mem=%X, size=%d)\n", sk, mem, size));
  1674.   IS_SKB(mem);
  1675.   kfree_skbmem(mem, size);
  1676.   if (sk) {
  1677.     sk->rmem_alloc -= size;
  1678.     if (sk->destroy && sk->wmem_alloc == 0 && sk->rmem_alloc == 0) {
  1679.         DPRINTF((DBG_INET,
  1680.             "recovered lot memory, sock = %X\n", sk));
  1681.     }
  1682.   }
  1683. }
  1684.  
  1685.  
  1686. /*
  1687.  * This routine must find a socket given a TCP or UDP header.
  1688.  * Everyhting is assumed to be in net order.
  1689.  */
  1690. struct sock *get_sock(struct proto *prot, unsigned short num,
  1691.                 unsigned long raddr,
  1692.                 unsigned short rnum, unsigned long laddr)
  1693. {
  1694.   struct sock *s;
  1695.   unsigned short hnum;
  1696.  
  1697.   hnum = ntohs(num);
  1698.   DPRINTF((DBG_INET, "get_sock(prot=%X, num=%d, raddr=%X, rnum=%d, laddr=%X)\n",
  1699.       prot, num, raddr, rnum, laddr));
  1700.  
  1701.   /*
  1702.    * SOCK_ARRAY_SIZE must be a power of two.  This will work better
  1703.    * than a prime unless 3 or more sockets end up using the same
  1704.    * array entry.  This should not be a problem because most
  1705.    * well known sockets don't overlap that much, and for
  1706.    * the other ones, we can just be careful about picking our
  1707.    * socket number when we choose an arbitrary one.
  1708.    */
  1709.   for(s = prot->sock_array[hnum & (SOCK_ARRAY_SIZE - 1)];
  1710.       s != NULL; s = s->next) 
  1711.   {
  1712.     if (s->num != hnum) 
  1713.         continue;
  1714.     if(s->dead && (s->state == TCP_CLOSE))
  1715.         continue;
  1716.     if(prot == &udp_prot)
  1717.         return s;
  1718.     if(ip_addr_match(s->daddr,raddr)==0)
  1719.         continue;
  1720.     if (s->dummy_th.dest != rnum && s->dummy_th.dest != 0) 
  1721.         continue;
  1722.     if(ip_addr_match(s->saddr,laddr) == 0)
  1723.         continue;
  1724.     return(s);
  1725.   }
  1726.   return(NULL);
  1727. }
  1728.  
  1729.  
  1730. void release_sock(struct sock *sk)
  1731. {
  1732.   if (!sk) {
  1733.     printk("sock.c: release_sock sk == NULL\n");
  1734.     return;
  1735.   }
  1736.   if (!sk->prot) {
  1737. /*    printk("sock.c: release_sock sk->prot == NULL\n"); */
  1738.     return;
  1739.   }
  1740.  
  1741.   if (sk->blog) return;
  1742.  
  1743.   /* See if we have any packets built up. */
  1744.   cli();
  1745.   sk->inuse = 1;
  1746.   while(sk->back_log != NULL) {
  1747.     struct sk_buff *skb;
  1748.  
  1749.     sk->blog = 1;
  1750.     skb =(struct sk_buff *)sk->back_log;
  1751.     DPRINTF((DBG_INET, "release_sock: skb = %X:\n", skb));
  1752.     if (skb->next != skb) {
  1753.         sk->back_log = skb->next;
  1754.         skb->prev->next = skb->next;
  1755.         skb->next->prev = skb->prev;
  1756.     } else {
  1757.         sk->back_log = NULL;
  1758.     }
  1759.     sti();
  1760.     DPRINTF((DBG_INET, "sk->back_log = %X\n", sk->back_log));
  1761.     if (sk->prot->rcv) sk->prot->rcv(skb, skb->dev, sk->opt,
  1762.                      skb->saddr, skb->len, skb->daddr, 1,
  1763.  
  1764.     /* Only used for/by raw sockets. */
  1765.     (struct inet_protocol *)sk->pair); 
  1766.     cli();
  1767.   }
  1768.   sk->blog = 0;
  1769.   sk->inuse = 0;
  1770.   sti();
  1771.   if (sk->dead && sk->state == TCP_CLOSE) {
  1772.     /* Should be about 2 rtt's */
  1773.     reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
  1774.   }
  1775. }
  1776.  
  1777.  
  1778. static int
  1779. inet_fioctl(struct inode *inode, struct file *file,
  1780.      unsigned int cmd, unsigned long arg)
  1781. {
  1782.   int minor, ret;
  1783.  
  1784.   /* Extract the minor number on which we work. */
  1785.   minor = MINOR(inode->i_rdev);
  1786.   if (minor != 0) return(-ENODEV);
  1787.  
  1788.   /* Now dispatch on the minor device. */
  1789.   switch(minor) {
  1790.     case 0:        /* INET */
  1791.         ret = inet_ioctl(NULL, cmd, arg);
  1792.         break;
  1793.     case 1:        /* IP */
  1794.         ret = ip_ioctl(NULL, cmd, arg);
  1795.         break;
  1796.     case 2:        /* ICMP */
  1797.         ret = icmp_ioctl(NULL, cmd, arg);
  1798.         break;
  1799.     case 3:        /* TCP */
  1800.         ret = tcp_ioctl(NULL, cmd, arg);
  1801.         break;
  1802.     case 4:        /* UDP */
  1803.         ret = udp_ioctl(NULL, cmd, arg);
  1804.         break;
  1805.     default:
  1806.         ret = -ENODEV;
  1807.   }
  1808.  
  1809.   return(ret);
  1810. }
  1811.  
  1812.  
  1813.  
  1814.  
  1815. static struct file_operations inet_fops = {
  1816.   NULL,        /* LSEEK    */
  1817.   NULL,        /* READ        */
  1818.   NULL,        /* WRITE    */
  1819.   NULL,        /* READDIR    */
  1820.   NULL,        /* SELECT    */
  1821.   inet_fioctl,    /* IOCTL    */
  1822.   NULL,        /* MMAP        */
  1823.   NULL,        /* OPEN        */
  1824.   NULL        /* CLOSE    */
  1825. };
  1826.  
  1827.  
  1828. static struct proto_ops inet_proto_ops = {
  1829.   AF_INET,
  1830.  
  1831.   inet_create,
  1832.   inet_dup,
  1833.   inet_release,
  1834.   inet_bind,
  1835.   inet_connect,
  1836.   inet_socketpair,
  1837.   inet_accept,
  1838.   inet_getname, 
  1839.   inet_read,
  1840.   inet_write,
  1841.   inet_select,
  1842.   inet_ioctl,
  1843.   inet_listen,
  1844.   inet_send,
  1845.   inet_recv,
  1846.   inet_sendto,
  1847.   inet_recvfrom,
  1848.   inet_shutdown,
  1849.   inet_setsockopt,
  1850.   inet_getsockopt,
  1851.   inet_fcntl,
  1852. };
  1853.  
  1854. extern unsigned long seq_offset;
  1855.  
  1856. /* Called by ddi.c on kernel startup.  */
  1857. void inet_proto_init(struct ddi_proto *pro)
  1858. {
  1859.   struct inet_protocol *p;
  1860.   int i;
  1861.  
  1862.   printk("Swansea University Computer Society Net2Debugged [1.30]\n");
  1863.   /* Set up our UNIX VFS major device. */
  1864.   if (register_chrdev(AF_INET_MAJOR, "af_inet", &inet_fops) < 0) {
  1865.     printk("%s: cannot register major device %d!\n",
  1866.                     pro->name, AF_INET_MAJOR);
  1867.     return;
  1868.   }
  1869.  
  1870.   /* Tell SOCKET that we are alive... */
  1871.   (void) sock_register(inet_proto_ops.family, &inet_proto_ops);
  1872.  
  1873.   seq_offset = CURRENT_TIME*250;
  1874.  
  1875.   /* Add all the protocols. */
  1876.   for(i = 0; i < SOCK_ARRAY_SIZE; i++) {
  1877.     tcp_prot.sock_array[i] = NULL;
  1878.     udp_prot.sock_array[i] = NULL;
  1879.     raw_prot.sock_array[i] = NULL;
  1880.   }
  1881.   printk("IP Protocols: ");
  1882.   for(p = inet_protocol_base; p != NULL;) {
  1883.     struct inet_protocol *tmp;
  1884.  
  1885.     tmp = (struct inet_protocol *) p->next;
  1886.     inet_add_protocol(p);
  1887.     printk("%s%s",p->name,tmp?", ":"\n");
  1888.     p = tmp;
  1889.   }
  1890.  
  1891.   /* Initialize the DEV module. */
  1892.   dev_init();
  1893.  
  1894.   /* Initialize the "Buffer Head" pointers. */
  1895.   bh_base[INET_BH].routine = inet_bh;
  1896. }
  1897.