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 / tcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  92.6 KB  |  3,726 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.  *        Implementation of the Transmission Control Protocol(TCP).
  7.  *
  8.  * Version:    @(#)tcp.c    1.0.16    05/25/93
  9.  *
  10.  * Authors:    Ross Biro, <bir7@leland.Stanford.Edu>
  11.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12.  *        Mark Evans, <evansmp@uhura.aston.ac.uk>
  13.  *        Corey Minyard <wf-rch!minyard@relay.EU.net>
  14.  *        Florian La Roche, <flla@stud.uni-sb.de>
  15.  *
  16.  * Fixes:    
  17.  *        Alan Cox    :    Numerous verify_area() calls
  18.  *        Alan Cox    :    Set the ACK bit on a reset
  19.  *        Alan Cox    :    Stopped it crashing if it closed while sk->inuse=1
  20.  *                    and was trying to connect (tcp_err()).
  21.  *        Alan Cox    :    All icmp error handling was broken
  22.  *                    pointers passed where wrong and the
  23.  *                    socket was looked up backwards. Nobody
  24.  *                    tested any icmp error code obviously.
  25.  *        Alan Cox    :    tcp_err() now handled properly. It wakes people
  26.  *                    on errors. select behaves and the icmp error race
  27.  *                    has gone by moving it into sock.c
  28.  *        Alan Cox    :    tcp_reset() fixed to work for everything not just
  29.  *                    packets for unknown sockets.
  30.  *        Alan Cox    :    tcp option processing.
  31.  *        Alan Cox    :    Reset tweaked (still not 100%) [Had syn rule wrong]
  32.  *        Herp Rosmanith  :    More reset fixes
  33.  *        Alan Cox    :    No longer acks invalid rst frames. Acking
  34.  *                    any kind of RST is right out.
  35.  *        Alan Cox    :    Sets an ignore me flag on an rst receive
  36.  *                    otherwise odd bits of prattle escape still
  37.  *        Alan Cox    :    Fixed another acking RST frame bug. Should stop
  38.  *                    LAN workplace lockups.
  39.  *        Alan Cox    :     Some tidyups using the new skb list facilities
  40.  *        Alan Cox    :    sk->keepopen now seems to work
  41.  *        Alan Cox    :    Pulls options out correctly on accepts
  42.  *        Alan Cox    :    Fixed assorted sk->rqueue->next errors
  43.  *        Alan Cox    :    PSH doesn't end a TCP read. Switched a bit to skb ops.
  44.  *        Alan Cox    :    Tidied tcp_data to avoid a potential nasty.
  45.  *        Alan Cox    :    Added some beter commenting, as the tcp is hard to follow
  46.  *        Alan Cox    :    Removed incorrect check for 20 * psh
  47.  *    Michael O'Reilly    :    ack < copied bug fix.
  48.  *    Johannes Stille        :    Misc tcp fixes (not all in yet).
  49.  *        Alan Cox    :    FIN with no memory -> CRASH
  50.  *        Alan Cox    :    Added socket option proto entries. Also added awareness of them to accept.
  51.  *        Alan Cox    :    Added TCP options (SOL_TCP)
  52.  *        Alan Cox    :    Switched wakeup calls to callbacks, so the kernel can layer network sockets.
  53.  *        Alan Cox    :    Use ip_tos/ip_ttl settings.
  54.  *        Alan Cox    :    Handle FIN (more) properly (we hope).
  55.  *        Alan Cox    :    RST frames sent on unsynchronised state ack error/
  56.  *        Alan Cox    :    Put in missing check for SYN bit.
  57.  *        Alan Cox    :    Added tcp_select_window() aka NET2E 
  58.  *                    window non shrink trick.
  59.  *        Alan Cox    :    Added a couple of small NET2E timer fixes
  60.  *        Charles Hedrick :    TCP fixes
  61.  *        Toomas Tamm    :    TCP window fixes
  62.  *        Alan Cox    :    Small URG fix to rlogin ^C ack fight
  63.  *        Charles Hedrick    :    Window fix
  64.  *        Linus        :    Rewrote tcp_read() and URG handling
  65.  *                    completely
  66.  *
  67.  *
  68.  * To Fix:
  69.  *            Possibly a problem with accept(). BSD accept never fails after
  70.  *        it causes a select. Linux can - given the official select semantics I
  71.  *        feel that _really_ its the BSD network programs that are bust (notably
  72.  *        inetd, which hangs occasionally because of this).
  73.  *            Add VJ Fastrecovery algorithm ?
  74.  *            Protocol closedown badly messed up.
  75.  *            Incompatiblity with spider ports (tcp hangs on that 
  76.  *            socket occasionally).
  77.  *        MSG_PEEK and read on same socket at once can cause crashes.
  78.  *
  79.  *        This program is free software; you can redistribute it and/or
  80.  *        modify it under the terms of the GNU General Public License
  81.  *        as published by the Free Software Foundation; either version
  82.  *        2 of the License, or(at your option) any later version.
  83.  */
  84. #include <linux/types.h>
  85. #include <linux/sched.h>
  86. #include <linux/mm.h>
  87. #include <linux/string.h>
  88. #include <linux/socket.h>
  89. #include <linux/sockios.h>
  90. #include <linux/termios.h>
  91. #include <linux/in.h>
  92. #include <linux/fcntl.h>
  93. #include "inet.h"
  94. #include "dev.h"
  95. #include "ip.h"
  96. #include "protocol.h"
  97. #include "icmp.h"
  98. #include "tcp.h"
  99. #include "skbuff.h"
  100. #include "sock.h"
  101. #include "arp.h"
  102. #include <linux/errno.h>
  103. #include <linux/timer.h>
  104. #include <asm/system.h>
  105. #include <asm/segment.h>
  106. #include <linux/mm.h>
  107.  
  108. #define SEQ_TICK 3
  109. unsigned long seq_offset;
  110. #define SUBNETSARELOCAL
  111.  
  112. static __inline__ int 
  113. min(unsigned int a, unsigned int b)
  114. {
  115.   if (a < b) return(a);
  116.   return(b);
  117. }
  118.  
  119.  
  120. static void __print_th(struct tcphdr *th)
  121. {
  122.     unsigned char *ptr;
  123.  
  124.     printk("TCP header:\n");
  125.     printk("    source=%d, dest=%d, seq =%ld, ack_seq = %ld\n",
  126.         ntohs(th->source), ntohs(th->dest),
  127.         ntohl(th->seq), ntohl(th->ack_seq));
  128.     printk("    fin=%d, syn=%d, rst=%d, psh=%d, ack=%d, urg=%d res1=%d res2=%d\n",
  129.         th->fin, th->syn, th->rst, th->psh, th->ack,
  130.         th->urg, th->res1, th->res2);
  131.     printk("    window = %d, check = %d urg_ptr = %d\n",
  132.         ntohs(th->window), ntohs(th->check), ntohs(th->urg_ptr));
  133.     printk("    doff = %d\n", th->doff);
  134.     ptr =(unsigned char *)(th + 1);
  135.     printk("    options = %d %d %d %d\n", ptr[0], ptr[1], ptr[2], ptr[3]);
  136. }
  137.  
  138. static inline void print_th(struct tcphdr *th)
  139. {
  140.     if (inet_debug == DBG_TCP)
  141.         __print_th(th);
  142. }
  143.  
  144. /* This routine grabs the first thing off of a rcv queue. */
  145. static struct sk_buff *
  146. get_firstr(struct sock *sk)
  147. {
  148.   return skb_dequeue(&sk->rqueue);
  149. }
  150.  
  151. /*
  152.  *    Difference between two values in tcp ack terms.
  153.  */
  154.  
  155. static long
  156. diff(unsigned long seq1, unsigned long seq2)
  157. {
  158.   long d;
  159.  
  160.   d = seq1 - seq2;
  161.   if (d > 0) return(d);
  162.  
  163.   /* I hope this returns what I want. */
  164.   return(~d+1);
  165. }
  166.  
  167. /* This routine picks a TCP windows for a socket based on
  168.    the following constraints
  169.    
  170.    1. The window can never be shrunk once it is offered (RFC 793)
  171.    2. We limit memory per socket
  172.    
  173.    For now we use NET2E3's heuristic of offering half the memory
  174.    we have handy. All is not as bad as this seems however because
  175.    of two things. Firstly we will bin packets even within the window
  176.    in order to get the data we are waiting for into the memory limit.
  177.    Secondly we bin common duplicate forms at receive time
  178.  
  179.    Better heuristics welcome
  180. */
  181.    
  182. static int tcp_select_window(struct sock *sk)
  183. {
  184.     int new_window = sk->prot->rspace(sk);
  185.  
  186. /*
  187.  * two things are going on here.  First, we don't ever offer a
  188.  * window less than min(sk->mss, MAX_WINDOW/2).  This is the
  189.  * receiver side of SWS as specified in RFC1122.
  190.  * Second, we always give them at least the window they
  191.  * had before, in order to avoid retracting window.  This
  192.  * is technically allowed, but RFC1122 advises against it and
  193.  * in practice it causes trouble.
  194.  */
  195.     if (new_window < min(sk->mss, MAX_WINDOW/2) ||
  196.         new_window < sk->window)
  197.       return(sk->window);
  198.     return(new_window);
  199. }
  200.  
  201. /* Enter the time wait state. */
  202.  
  203. static void tcp_time_wait(struct sock *sk)
  204. {
  205.   sk->state = TCP_TIME_WAIT;
  206.   sk->shutdown = SHUTDOWN_MASK;
  207.   if (!sk->dead)
  208.     sk->state_change(sk);
  209.   reset_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
  210. }
  211.  
  212. /*
  213.  *    A timer event has trigger a tcp retransmit timeout. The
  214.  *    socket xmit queue is ready and set up to send. Because
  215.  *    the ack receive code keeps the queue straight we do
  216.  *    nothing clever here.
  217.  */
  218.  
  219. static void
  220. tcp_retransmit(struct sock *sk, int all)
  221. {
  222.   if (all) {
  223.     ip_retransmit(sk, all);
  224.     return;
  225.   }
  226.  
  227.   sk->ssthresh = sk->cong_window >> 1; /* remember window where we lost */
  228.   /* sk->ssthresh in theory can be zero.  I guess that's OK */
  229.   sk->cong_count = 0;
  230.  
  231.   sk->cong_window = 1;
  232.  
  233.   /* Do the actual retransmit. */
  234.   ip_retransmit(sk, all);
  235. }
  236.  
  237.  
  238. /*
  239.  * This routine is called by the ICMP module when it gets some
  240.  * sort of error condition.  If err < 0 then the socket should
  241.  * be closed and the error returned to the user.  If err > 0
  242.  * it's just the icmp type << 8 | icmp code.  After adjustment
  243.  * header points to the first 8 bytes of the tcp header.  We need
  244.  * to find the appropriate port.
  245.  */
  246. void
  247. tcp_err(int err, unsigned char *header, unsigned long daddr,
  248.     unsigned long saddr, struct inet_protocol *protocol)
  249. {
  250.   struct tcphdr *th;
  251.   struct sock *sk;
  252.   struct iphdr *iph=(struct iphdr *)header;
  253.   
  254.   header+=4*iph->ihl;
  255.    
  256.   DPRINTF((DBG_TCP, "TCP: tcp_err(%d, hdr=%X, daddr=%X saddr=%X, protocol=%X)\n",
  257.                     err, header, daddr, saddr, protocol));
  258.  
  259.   th =(struct tcphdr *)header;
  260.   sk = get_sock(&tcp_prot, th->source/*dest*/, daddr, th->dest/*source*/, saddr);
  261.   print_th(th);
  262.  
  263.   if (sk == NULL) return;
  264.   
  265.   if(err<0)
  266.   {
  267.       sk->err = -err;
  268.       sk->error_report(sk);
  269.       return;
  270.   }
  271.  
  272.   if ((err & 0xff00) == (ICMP_SOURCE_QUENCH << 8)) {
  273.     /*
  274.      * FIXME:
  275.      * For now we will just trigger a linear backoff.
  276.      * The slow start code should cause a real backoff here.
  277.      */
  278.     if (sk->cong_window > 4) sk->cong_window--;
  279.     return;
  280.   }
  281.  
  282.   DPRINTF((DBG_TCP, "TCP: icmp_err got error\n"));
  283.   sk->err = icmp_err_convert[err & 0xff].errno;
  284.  
  285.   /*
  286.    * If we've already connected we will keep trying
  287.    * until we time out, or the user gives up.
  288.    */
  289.   if (icmp_err_convert[err & 0xff].fatal) {
  290.     if (sk->state == TCP_SYN_SENT) {
  291.         sk->state = TCP_CLOSE;
  292.         sk->error_report(sk);        /* Wake people up to see the error (see connect in sock.c) */
  293.     }
  294.   }
  295.   return;
  296. }
  297.  
  298.  
  299. /*
  300.  *    Walk down the receive queue counting readable data until we hit the end or we find a gap
  301.  *    in the received data queue (ie a frame missing that needs sending to us)
  302.  */
  303.  
  304. static int
  305. tcp_readable(struct sock *sk)
  306. {
  307.   unsigned long counted;
  308.   unsigned long amount;
  309.   struct sk_buff *skb;
  310.   int count=0;
  311.   int sum;
  312.   unsigned long flags;
  313.  
  314.   DPRINTF((DBG_TCP, "tcp_readable(sk=%X)\n", sk));
  315.   if(sk && sk->debug)
  316.       printk("tcp_readable: %p - ",sk);
  317.  
  318.   if (sk == NULL || skb_peek(&sk->rqueue) == NULL)     /* Empty sockets are easy! */
  319.   {
  320.       if(sk && sk->debug) 
  321.           printk("empty\n");
  322.       return(0);
  323.   }
  324.   
  325.   counted = sk->copied_seq+1;    /* Where we are at the moment */
  326.   amount = 0;
  327.   
  328.   save_flags(flags);        /* So nobody adds things at the wrong moment */
  329.   cli();
  330.   skb =(struct sk_buff *)sk->rqueue;
  331.  
  332.   /* Do until a push or until we are out of data. */
  333.   do {
  334.     count++;
  335. #ifdef OLD    
  336.     /* This is wrong: It breaks Chameleon amongst other stacks */
  337.     if (count > 20) {
  338.         restore_flags(flags);
  339.         DPRINTF((DBG_TCP, "tcp_readable, more than 20 packets without a psh\n"));
  340.         printk("tcp_read: possible read_queue corruption.\n");
  341.         return(amount);
  342.     }
  343. #endif    
  344.     if (before(counted, skb->h.th->seq))     /* Found a hole so stops here */
  345.         break;
  346.     sum = skb->len -(counted - skb->h.th->seq);    /* Length - header but start from where we are up to (avoid overlaps) */
  347.     if (skb->h.th->syn)
  348.         sum++;
  349.     if (sum >= 0) {                    /* Add it up, move on */
  350.         amount += sum;
  351.         if (skb->h.th->syn) amount--;
  352.         counted += sum;
  353.     }
  354.     if (amount && skb->h.th->psh) break;
  355.     skb =(struct sk_buff *)skb->next;        /* Move along */
  356.   } while(skb != sk->rqueue);
  357.   if (amount && !sk->urginline && sk->urg_data &&
  358.       (sk->urg_seq - sk->copied_seq) <= (counted - sk->copied_seq))
  359.     amount--;        /* don't count urg data */
  360.   restore_flags(flags);
  361.   DPRINTF((DBG_TCP, "tcp readable returning %d bytes\n", amount));
  362.   if(sk->debug)
  363.       printk("got %lu bytes.\n",amount);
  364.   return(amount);
  365. }
  366.  
  367.  
  368. /*
  369.  *    Wait for a TCP event. Note the oddity with SEL_IN and reading. The
  370.  *    listening socket has a receive queue of sockets to accept.
  371.  */
  372.  
  373. static int
  374. tcp_select(struct sock *sk, int sel_type, select_table *wait)
  375. {
  376.   DPRINTF((DBG_TCP, "tcp_select(sk=%X, sel_type = %d, wait = %X)\n",
  377.                           sk, sel_type, wait));
  378.  
  379.   sk->inuse = 1;
  380.   switch(sel_type) {
  381.     case SEL_IN:
  382.         if(sk->debug)
  383.             printk("select in");
  384.         select_wait(sk->sleep, wait);
  385.         if(sk->debug)
  386.             printk("-select out");
  387.         if (skb_peek(&sk->rqueue) != NULL) {
  388.             if (sk->state == TCP_LISTEN || tcp_readable(sk)) {
  389.                 release_sock(sk);
  390.                 if(sk->debug)
  391.                     printk("-select ok data\n");
  392.                 return(1);
  393.             }
  394.         }
  395.         if (sk->err != 0)    /* Receiver error */
  396.         {
  397.             release_sock(sk);
  398.             if(sk->debug)
  399.                 printk("-select ok error");
  400.             return(1);
  401.         }
  402.         if (sk->shutdown & RCV_SHUTDOWN) {
  403.             release_sock(sk);
  404.             if(sk->debug)
  405.                 printk("-select ok down\n");
  406.             return(1);
  407.         } else {
  408.             release_sock(sk);
  409.             if(sk->debug)
  410.                 printk("-select fail\n");
  411.             return(0);
  412.         }
  413.     case SEL_OUT:
  414.         select_wait(sk->sleep, wait);
  415.         if (sk->shutdown & SEND_SHUTDOWN) {
  416.             DPRINTF((DBG_TCP,
  417.                 "write select on shutdown socket.\n"));
  418.  
  419.             /* FIXME: should this return an error? */
  420.             release_sock(sk);
  421.             return(0);
  422.         }
  423.  
  424.         /*
  425.          * FIXME:
  426.          * Hack so it will probably be able to write
  427.          * something if it says it's ok to write.
  428.          */
  429.         if (sk->prot->wspace(sk) >= sk->mss) {
  430.             release_sock(sk);
  431.             /* This should cause connect to work ok. */
  432.             if (sk->state == TCP_SYN_RECV ||
  433.                 sk->state == TCP_SYN_SENT) return(0);
  434.             return(1);
  435.         }
  436.         DPRINTF((DBG_TCP,
  437.             "tcp_select: sleeping on write sk->wmem_alloc = %d, "
  438.             "sk->packets_out = %d\n"
  439.             "sk->wback = %X, sk->wfront = %X\n"
  440.             "sk->write_seq = %u, sk->window_seq=%u\n", 
  441.                 sk->wmem_alloc, sk->packets_out,
  442.                 sk->wback, sk->wfront,
  443.                 sk->write_seq, sk->window_seq));
  444.  
  445.         release_sock(sk);
  446.         return(0);
  447.     case SEL_EX:
  448.         select_wait(sk->sleep,wait);
  449.         if (sk->err || sk->urg_data) {
  450.             release_sock(sk);
  451.             return(1);
  452.         }
  453.         release_sock(sk);
  454.         return(0);
  455.   }
  456.  
  457.   release_sock(sk);
  458.   return(0);
  459. }
  460.  
  461.  
  462. int
  463. tcp_ioctl(struct sock *sk, int cmd, unsigned long arg)
  464. {
  465.   int err;
  466.   DPRINTF((DBG_TCP, "tcp_ioctl(sk=%X, cmd = %d, arg=%X)\n", sk, cmd, arg));
  467.   switch(cmd) {
  468.     case DDIOCSDBG:
  469.         return(dbg_ioctl((void *) arg, DBG_TCP));
  470.  
  471.     case TIOCINQ:
  472. #ifdef FIXME    /* FIXME: */
  473.     case FIONREAD:
  474. #endif
  475.         {
  476.             unsigned long amount;
  477.  
  478.             if (sk->state == TCP_LISTEN) return(-EINVAL);
  479.  
  480.             sk->inuse = 1;
  481.             amount = tcp_readable(sk);
  482.             release_sock(sk);
  483.             DPRINTF((DBG_TCP, "returning %d\n", amount));
  484.             err=verify_area(VERIFY_WRITE,(void *)arg,
  485.                            sizeof(unsigned long));
  486.             if(err)
  487.                 return err;
  488.             put_fs_long(amount,(unsigned long *)arg);
  489.             return(0);
  490.         }
  491.     case SIOCATMARK:
  492.         {
  493.             int answ = sk->urg_data && sk->urg_seq == sk->copied_seq+1;
  494.  
  495.             err = verify_area(VERIFY_WRITE,(void *) arg,
  496.                           sizeof(unsigned long));
  497.             if (err)
  498.                 return err;
  499.             put_fs_long(answ,(int *) arg);
  500.             return(0);
  501.         }
  502.     case TIOCOUTQ:
  503.         {
  504.             unsigned long amount;
  505.  
  506.             if (sk->state == TCP_LISTEN) return(-EINVAL);
  507.             amount = sk->prot->wspace(sk);
  508.             err=verify_area(VERIFY_WRITE,(void *)arg,
  509.                            sizeof(unsigned long));
  510.             if(err)
  511.                 return err;
  512.             put_fs_long(amount,(unsigned long *)arg);
  513.             return(0);
  514.         }
  515.     default:
  516.         return(-EINVAL);
  517.   }
  518. }
  519.  
  520.  
  521. /* This routine computes a TCP checksum. */
  522. unsigned short
  523. tcp_check(struct tcphdr *th, int len,
  524.       unsigned long saddr, unsigned long daddr)
  525. {     
  526.   unsigned long sum;
  527.    
  528.   if (saddr == 0) saddr = my_addr();
  529.   print_th(th);
  530.   __asm__("\t addl %%ecx,%%ebx\n"
  531.       "\t adcl %%edx,%%ebx\n"
  532.       "\t adcl $0, %%ebx\n"
  533.       : "=b"(sum)
  534.       : "0"(daddr), "c"(saddr), "d"((ntohs(len) << 16) + IPPROTO_TCP*256)
  535.       : "cx","bx","dx" );
  536.    
  537.   if (len > 3) {
  538.     __asm__("\tclc\n"
  539.         "1:\n"
  540.         "\t lodsl\n"
  541.         "\t adcl %%eax, %%ebx\n"
  542.         "\t loop 1b\n"
  543.         "\t adcl $0, %%ebx\n"
  544.         : "=b"(sum) , "=S"(th)
  545.         : "0"(sum), "c"(len/4) ,"1"(th)
  546.         : "ax", "cx", "bx", "si" );
  547.   }
  548.    
  549.   /* Convert from 32 bits to 16 bits. */
  550.   __asm__("\t movl %%ebx, %%ecx\n"
  551.       "\t shrl $16,%%ecx\n"
  552.       "\t addw %%cx, %%bx\n"
  553.       "\t adcw $0, %%bx\n"
  554.       : "=b"(sum)
  555.       : "0"(sum)
  556.       : "bx", "cx");
  557.    
  558.   /* Check for an extra word. */
  559.   if ((len & 2) != 0) {
  560.     __asm__("\t lodsw\n"
  561.         "\t addw %%ax,%%bx\n"
  562.         "\t adcw $0, %%bx\n"
  563.         : "=b"(sum), "=S"(th)
  564.         : "0"(sum) ,"1"(th)
  565.         : "si", "ax", "bx");
  566.   }
  567.    
  568.   /* Now check for the extra byte. */
  569.   if ((len & 1) != 0) {
  570.     __asm__("\t lodsb\n"
  571.         "\t movb $0,%%ah\n"
  572.         "\t addw %%ax,%%bx\n"
  573.         "\t adcw $0, %%bx\n"
  574.         : "=b"(sum)
  575.         : "0"(sum) ,"S"(th)
  576.         : "si", "ax", "bx");
  577.   }
  578.    
  579.   /* We only want the bottom 16 bits, but we never cleared the top 16. */
  580.   return((~sum) & 0xffff);
  581. }
  582.  
  583.  
  584. void tcp_send_check(struct tcphdr *th, unsigned long saddr, 
  585.         unsigned long daddr, int len, struct sock *sk)
  586. {
  587.     th->check = 0;
  588.     th->check = tcp_check(th, len, saddr, daddr);
  589.     return;
  590. }
  591.  
  592. static void tcp_send_skb(struct sock *sk, struct sk_buff *skb)
  593. {
  594.     int size;
  595.     struct tcphdr * th = skb->h.th;
  596.  
  597.     /* length of packet (not counting length of pre-tcp headers) */
  598.     size = skb->len - ((unsigned char *) th - skb->data);
  599.  
  600.     /* sanity check it.. */
  601.     if (size < sizeof(struct tcphdr) || size > skb->len) {
  602.         printk("tcp_send_skb: bad skb (skb = %p, data = %p, th = %p, len = %lu)\n",
  603.             skb, skb->data, th, skb->len);
  604.         kfree_skb(skb, FREE_WRITE);
  605.         return;
  606.     }
  607.  
  608.     /* If we have queued a header size packet.. */
  609.     if (size == sizeof(struct tcphdr)) {
  610.         /* If its got a syn or fin its notionally included in the size..*/
  611.         if(!th->syn && !th->fin) {
  612.             printk("tcp_send_skb: attempt to queue a bogon.\n");
  613.             kfree_skb(skb,FREE_WRITE);
  614.             return;
  615.         }
  616.     }
  617.   
  618.     /* We need to complete and send the packet. */
  619.     tcp_send_check(th, sk->saddr, sk->daddr, size, sk);
  620.  
  621.     skb->h.seq = ntohl(th->seq) + size - 4*th->doff;
  622.     if (after(skb->h.seq, sk->window_seq) ||
  623.         (sk->retransmits && sk->timeout == TIME_WRITE) ||
  624.          sk->packets_out >= sk->cong_window) {
  625.         DPRINTF((DBG_TCP, "sk->cong_window = %d, sk->packets_out = %d\n",
  626.                     sk->cong_window, sk->packets_out));
  627.         DPRINTF((DBG_TCP, "sk->write_seq = %d, sk->window_seq = %d\n",
  628.                     sk->write_seq, sk->window_seq));
  629.         skb->next = NULL;
  630.         skb->magic = TCP_WRITE_QUEUE_MAGIC;
  631.         if (sk->wback == NULL) {
  632.             sk->wfront = skb;
  633.         } else {
  634.             sk->wback->next = skb;
  635.         }
  636.         sk->wback = skb;
  637.         if (before(sk->window_seq, sk->wfront->h.seq) &&
  638.             sk->send_head == NULL &&
  639.             sk->ack_backlog == 0)
  640.           reset_timer(sk, TIME_PROBE0, sk->rto);
  641.     } else {
  642.         sk->sent_seq = sk->write_seq;
  643.         sk->prot->queue_xmit(sk, skb->dev, skb, 0);
  644.     }
  645. }
  646.  
  647. struct sk_buff * tcp_dequeue_partial(struct sock * sk)
  648. {
  649.     struct sk_buff * skb;
  650.     unsigned long flags;
  651.  
  652.     save_flags(flags);
  653.     cli();
  654.     skb = sk->partial;
  655.     if (skb) {
  656.         sk->partial = NULL;
  657.         del_timer(&sk->partial_timer);
  658.     }
  659.     restore_flags(flags);
  660.     return skb;
  661. }
  662.  
  663. static void tcp_send_partial(struct sock *sk)
  664. {
  665.     struct sk_buff *skb;
  666.  
  667.     if (sk == NULL)
  668.         return;
  669.     while ((skb = tcp_dequeue_partial(sk)) != NULL)
  670.         tcp_send_skb(sk, skb);
  671. }
  672.  
  673. void tcp_enqueue_partial(struct sk_buff * skb, struct sock * sk)
  674. {
  675.     struct sk_buff * tmp;
  676.     unsigned long flags;
  677.  
  678.     save_flags(flags);
  679.     cli();
  680.     tmp = sk->partial;
  681.     if (tmp)
  682.         del_timer(&sk->partial_timer);
  683.     sk->partial = skb;
  684.     sk->partial_timer.expires = HZ;
  685.     sk->partial_timer.function = (void (*)(unsigned long)) tcp_send_partial;
  686.     sk->partial_timer.data = (unsigned long) sk;
  687.     add_timer(&sk->partial_timer);
  688.     restore_flags(flags);
  689.     if (tmp)
  690.         tcp_send_skb(sk, tmp);
  691. }
  692.  
  693.  
  694. /* This routine sends an ack and also updates the window. */
  695. static void
  696. tcp_send_ack(unsigned long sequence, unsigned long ack,
  697.          struct sock *sk,
  698.          struct tcphdr *th, unsigned long daddr)
  699. {
  700.   struct sk_buff *buff;
  701.   struct tcphdr *t1;
  702.   struct device *dev = NULL;
  703.   int tmp;
  704.  
  705.   if(sk->zapped)
  706.     return;        /* We have been reset, we may not send again */
  707.   /*
  708.    * We need to grab some memory, and put together an ack,
  709.    * and then put it into the queue to be sent.
  710.    */
  711.   buff = sk->prot->wmalloc(sk, MAX_ACK_SIZE, 1, GFP_ATOMIC);
  712.   if (buff == NULL) {
  713.     /* Force it to send an ack. */
  714.     sk->ack_backlog++;
  715.     if (sk->timeout != TIME_WRITE && tcp_connected(sk->state)) {
  716.         reset_timer(sk, TIME_WRITE, 10);
  717.     }
  718. if (inet_debug == DBG_SLIP) printk("\rtcp_ack: malloc failed\n");
  719.     return;
  720.   }
  721.  
  722.   buff->mem_addr = buff;
  723.   buff->mem_len = MAX_ACK_SIZE;
  724.   buff->len = sizeof(struct tcphdr);
  725.   buff->sk = sk;
  726.   t1 =(struct tcphdr *) buff->data;
  727.  
  728.   /* Put in the IP header and routing stuff. */
  729.   tmp = sk->prot->build_header(buff, sk->saddr, daddr, &dev,
  730.                 IPPROTO_TCP, sk->opt, MAX_ACK_SIZE,sk->ip_tos,sk->ip_ttl);
  731.   if (tmp < 0) {
  732.       buff->free=1;
  733.     sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
  734. if (inet_debug == DBG_SLIP) printk("\rtcp_ack: build_header failed\n");
  735.     return;
  736.   }
  737.   buff->len += tmp;
  738.   t1 =(struct tcphdr *)((char *)t1 +tmp);
  739.  
  740.   /* FIXME: */
  741.   memcpy(t1, th, sizeof(*t1)); /* this should probably be removed */
  742.  
  743.   /* swap the send and the receive. */
  744.   t1->dest = th->source;
  745.   t1->source = th->dest;
  746.   t1->seq = ntohl(sequence);
  747.   t1->ack = 1;
  748.   sk->window = tcp_select_window(sk);/*sk->prot->rspace(sk);*/
  749.   t1->window = ntohs(sk->window);
  750.   t1->res1 = 0;
  751.   t1->res2 = 0;
  752.   t1->rst = 0;
  753.   t1->urg = 0;
  754.   t1->syn = 0;
  755.   t1->psh = 0;
  756.   t1->fin = 0;
  757.   if (ack == sk->acked_seq) {
  758.     sk->ack_backlog = 0;
  759.     sk->bytes_rcv = 0;
  760.     sk->ack_timed = 0;
  761.     if (sk->send_head == NULL && sk->wfront == NULL && sk->timeout == TIME_WRITE) 
  762.     {
  763.         if(sk->keepopen)
  764.             reset_timer(sk,TIME_KEEPOPEN,TCP_TIMEOUT_LEN);
  765.         else
  766.             delete_timer(sk);
  767.     }
  768.   }
  769.   t1->ack_seq = ntohl(ack);
  770.   t1->doff = sizeof(*t1)/4;
  771.   tcp_send_check(t1, sk->saddr, daddr, sizeof(*t1), sk);
  772.   if (sk->debug)
  773.        printk("\rtcp_ack: seq %lx ack %lx\n", sequence, ack);
  774.   sk->prot->queue_xmit(sk, dev, buff, 1);
  775. }
  776.  
  777.  
  778. /* This routine builds a generic TCP header. */
  779. static int
  780. tcp_build_header(struct tcphdr *th, struct sock *sk, int push)
  781. {
  782.  
  783.   /* FIXME: want to get rid of this. */
  784.   memcpy(th,(void *) &(sk->dummy_th), sizeof(*th));
  785.   th->seq = htonl(sk->write_seq);
  786.   th->psh =(push == 0) ? 1 : 0;
  787.   th->doff = sizeof(*th)/4;
  788.   th->ack = 1;
  789.   th->fin = 0;
  790.   sk->ack_backlog = 0;
  791.   sk->bytes_rcv = 0;
  792.   sk->ack_timed = 0;
  793.   th->ack_seq = htonl(sk->acked_seq);
  794.   sk->window = tcp_select_window(sk)/*sk->prot->rspace(sk)*/;
  795.   th->window = htons(sk->window);
  796.  
  797.   return(sizeof(*th));
  798. }
  799.  
  800. /*
  801.  * This routine copies from a user buffer into a socket,
  802.  * and starts the transmit system.
  803.  */
  804. static int
  805. tcp_write(struct sock *sk, unsigned char *from,
  806.       int len, int nonblock, unsigned flags)
  807. {
  808.   int copied = 0;
  809.   int copy;
  810.   int tmp;
  811.   struct sk_buff *skb;
  812.   struct sk_buff *send_tmp;
  813.   unsigned char *buff;
  814.   struct proto *prot;
  815.   struct device *dev = NULL;
  816.  
  817.   DPRINTF((DBG_TCP, "tcp_write(sk=%X, from=%X, len=%d, nonblock=%d, flags=%X)\n",
  818.                     sk, from, len, nonblock, flags));
  819.  
  820.   sk->inuse=1;
  821.   prot = sk->prot;
  822.   while(len > 0) {
  823.     if (sk->err) {            /* Stop on an error */
  824.         release_sock(sk);
  825.         if (copied) return(copied);
  826.         tmp = -sk->err;
  827.         sk->err = 0;
  828.         return(tmp);
  829.     }
  830.  
  831.     /* First thing we do is make sure that we are established. */     
  832.     if (sk->shutdown & SEND_SHUTDOWN) {
  833.         release_sock(sk);
  834.         sk->err = EPIPE;
  835.         if (copied) return(copied);
  836.         sk->err = 0;
  837.         return(-EPIPE);
  838.     }
  839.  
  840.  
  841.     /* Wait for a connection to finish. */
  842.     
  843.     while(sk->state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT) {
  844.         if (sk->err) {
  845.             release_sock(sk);
  846.             if (copied) return(copied);
  847.             tmp = -sk->err;
  848.             sk->err = 0;
  849.             return(tmp);
  850.         }
  851.  
  852.         if (sk->state != TCP_SYN_SENT && sk->state != TCP_SYN_RECV) {
  853.             release_sock(sk);
  854.             DPRINTF((DBG_TCP, "tcp_write: return 1\n"));
  855.             if (copied) return(copied);
  856.  
  857.             if (sk->err) {
  858.                 tmp = -sk->err;
  859.                 sk->err = 0;
  860.                 return(tmp);
  861.             }
  862.  
  863.             if (sk->keepopen) {
  864.                 send_sig(SIGPIPE, current, 0);
  865.             }
  866.             return(-EPIPE);
  867.         }
  868.  
  869.         if (nonblock || copied) {
  870.             release_sock(sk);
  871.             DPRINTF((DBG_TCP, "tcp_write: return 2\n"));
  872.             if (copied) return(copied);
  873.             return(-EAGAIN);
  874.         }
  875.  
  876.         release_sock(sk);
  877.         cli();
  878.         if (sk->state != TCP_ESTABLISHED &&
  879.             sk->state != TCP_CLOSE_WAIT && sk->err == 0) {
  880.             interruptible_sleep_on(sk->sleep);
  881.             if (current->signal & ~current->blocked) {
  882.                 sti();
  883.                 DPRINTF((DBG_TCP, "tcp_write: return 3\n"));
  884.                 if (copied) return(copied);
  885.                 return(-ERESTARTSYS);
  886.             }
  887.         }
  888.         sk->inuse = 1;
  889.         sti();
  890.     }
  891.  
  892. /*
  893.  * The following code can result in copy <= if sk->mss is ever
  894.  * decreased.  It shouldn't be.  sk->mss is min(sk->mtu, sk->max_window).
  895.  * sk->mtu is constant once SYN processing is finished.  I.e. we
  896.  * had better not get here until we've seen his SYN and at least one
  897.  * valid ack.  (The SYN sets sk->mtu and the ack sets sk->max_window.)
  898.  * But ESTABLISHED should guarantee that.  sk->max_window is by definition
  899.  * non-decreasing.  Note that any ioctl to set user_mss must be done
  900.  * before the exchange of SYN's.  If the initial ack from the other
  901.  * end has a window of 0, max_window and thus mss will both be 0.
  902.  */
  903.  
  904.     /* Now we need to check if we have a half built packet. */
  905.     if ((skb = tcp_dequeue_partial(sk)) != NULL) {
  906.             int hdrlen;
  907.  
  908.              /* IP header + TCP header */
  909.         hdrlen = ((unsigned long)skb->h.th - (unsigned long)skb->data)
  910.                  + sizeof(struct tcphdr);
  911.  
  912.         /* Add more stuff to the end of skb->len */
  913.         if (!(flags & MSG_OOB)) {
  914.             copy = min(sk->mss - (skb->len - hdrlen), len);
  915.             /* FIXME: this is really a bug. */
  916.             if (copy <= 0) {
  917.               printk("TCP: **bug**: \"copy\" <= 0!!\n");
  918.               copy = 0;
  919.             }
  920.       
  921.             memcpy_fromfs(skb->data + skb->len, from, copy);
  922.             skb->len += copy;
  923.             from += copy;
  924.             copied += copy;
  925.             len -= copy;
  926.             sk->write_seq += copy;
  927.               }
  928.         if ((skb->len - hdrlen) >= sk->mss ||
  929.             (flags & MSG_OOB) ||
  930.             !sk->packets_out)
  931.             tcp_send_skb(sk, skb);
  932.         else
  933.             tcp_enqueue_partial(skb, sk);
  934.         continue;
  935.     }
  936.  
  937.     /*
  938.      * We also need to worry about the window.
  939.       * If window < 1/2 the maximum window we've seen from this
  940.       *   host, don't use it.  This is sender side
  941.       *   silly window prevention, as specified in RFC1122.
  942.       *   (Note that this is diffferent than earlier versions of
  943.       *   SWS prevention, e.g. RFC813.).  What we actually do is 
  944.      *   use the whole MSS.  Since the results in the right
  945.      *   edge of the packet being outside the window, it will
  946.      *   be queued for later rather than sent.
  947.      */
  948.  
  949.     copy = sk->window_seq - sk->write_seq;
  950.     if (copy <= 0 || copy < (sk->max_window >> 1) || copy > sk->mss)
  951.         copy = sk->mss;
  952.     if (copy > len)
  953.         copy = len;
  954.  
  955.   /* We should really check the window here also. */
  956.     send_tmp = NULL;
  957.     if (copy < sk->mss && !(flags & MSG_OOB)) {
  958.     /* We will release the socket incase we sleep here. */
  959.       release_sock(sk);
  960.       /* NB: following must be mtu, because mss can be increased.
  961.        * mss is always <= mtu */
  962.       skb = prot->wmalloc(sk, sk->mtu + 128 + prot->max_header + sizeof(*skb), 0, GFP_KERNEL);
  963.       sk->inuse = 1;
  964.       send_tmp = skb;
  965.     } else {
  966.         /* We will release the socket incase we sleep here. */
  967.       release_sock(sk);
  968.       skb = prot->wmalloc(sk, copy + prot->max_header + sizeof(*skb), 0, GFP_KERNEL);
  969.       sk->inuse = 1;
  970.     }
  971.  
  972.     /* If we didn't get any memory, we need to sleep. */
  973.     if (skb == NULL) {
  974.         if (nonblock /* || copied */) {
  975.             release_sock(sk);
  976.             DPRINTF((DBG_TCP, "tcp_write: return 4\n"));
  977.             if (copied) return(copied);
  978.             return(-EAGAIN);
  979.         }
  980.  
  981.         /* FIXME: here is another race condition. */
  982.         tmp = sk->wmem_alloc;
  983.         release_sock(sk);
  984.         cli();
  985.         /* Again we will try to avoid it. */
  986.         if (tmp <= sk->wmem_alloc &&
  987.           (sk->state == TCP_ESTABLISHED||sk->state == TCP_CLOSE_WAIT)
  988.                 && sk->err == 0) {
  989.             interruptible_sleep_on(sk->sleep);
  990.             if (current->signal & ~current->blocked) {
  991.                 sti();
  992.                 DPRINTF((DBG_TCP, "tcp_write: return 5\n"));
  993.                 if (copied) return(copied);
  994.                 return(-ERESTARTSYS);
  995.             }
  996.         }
  997.         sk->inuse = 1;
  998.         sti();
  999.         continue;
  1000.     }
  1001.  
  1002.     skb->len = 0;
  1003.     skb->sk = sk;
  1004.     skb->free = 0;
  1005.  
  1006.     buff = skb->data;
  1007.  
  1008.     /*
  1009.      * FIXME: we need to optimize this.
  1010.      * Perhaps some hints here would be good.
  1011.      */
  1012.     tmp = prot->build_header(skb, sk->saddr, sk->daddr, &dev,
  1013.                  IPPROTO_TCP, sk->opt, skb->mem_len,sk->ip_tos,sk->ip_ttl);
  1014.     if (tmp < 0 ) {
  1015.         prot->wfree(sk, skb->mem_addr, skb->mem_len);
  1016.         release_sock(sk);
  1017.         DPRINTF((DBG_TCP, "tcp_write: return 6\n"));
  1018.         if (copied) return(copied);
  1019.         return(tmp);
  1020.     }
  1021.     skb->len += tmp;
  1022.     skb->dev = dev;
  1023.     buff += tmp;
  1024.     skb->h.th =(struct tcphdr *) buff;
  1025.     tmp = tcp_build_header((struct tcphdr *)buff, sk, len-copy);
  1026.     if (tmp < 0) {
  1027.         prot->wfree(sk, skb->mem_addr, skb->mem_len);
  1028.         release_sock(sk);
  1029.         DPRINTF((DBG_TCP, "tcp_write: return 7\n"));
  1030.         if (copied) return(copied);
  1031.         return(tmp);
  1032.     }
  1033.  
  1034.     if (flags & MSG_OOB) {
  1035.         ((struct tcphdr *)buff)->urg = 1;
  1036.         ((struct tcphdr *)buff)->urg_ptr = ntohs(copy);
  1037.     }
  1038.     skb->len += tmp;
  1039.     memcpy_fromfs(buff+tmp, from, copy);
  1040.  
  1041.     from += copy;
  1042.     copied += copy;
  1043.     len -= copy;
  1044.     skb->len += copy;
  1045.     skb->free = 0;
  1046.     sk->write_seq += copy;
  1047.  
  1048.     if (send_tmp != NULL && sk->packets_out) {
  1049.         tcp_enqueue_partial(send_tmp, sk);
  1050.         continue;
  1051.     }
  1052.     tcp_send_skb(sk, skb);
  1053.   }
  1054.   sk->err = 0;
  1055.  
  1056. /*
  1057.  *    Nagles rule. Turn Nagle off with TCP_NODELAY for highly
  1058.  *    interactive fast network servers. It's meant to be on and
  1059.  *    it really improves the throughput though not the echo time
  1060.  *    on my slow slip link - Alan
  1061.  */
  1062.  
  1063.   /* Avoid possible race on send_tmp - c/o Johannes Stille */
  1064.   if(sk->partial && 
  1065.      ((!sk->packets_out) 
  1066.      /* If not nagling we can send on the before case too.. */
  1067.       || (sk->nonagle && before(sk->write_seq , sk->window_seq))
  1068.       ))
  1069.       tcp_send_partial(sk);
  1070.   /* -- */
  1071.   release_sock(sk);
  1072.   DPRINTF((DBG_TCP, "tcp_write: return 8\n"));
  1073.   return(copied);
  1074. }
  1075.  
  1076.  
  1077. static int
  1078. tcp_sendto(struct sock *sk, unsigned char *from,
  1079.        int len, int nonblock, unsigned flags,
  1080.        struct sockaddr_in *addr, int addr_len)
  1081. {
  1082.   struct sockaddr_in sin;
  1083.  
  1084.   if (addr_len < sizeof(sin)) return(-EINVAL);
  1085.   memcpy_fromfs(&sin, addr, sizeof(sin));
  1086.   if (sin.sin_family && sin.sin_family != AF_INET) return(-EINVAL);
  1087.   if (sin.sin_port != sk->dummy_th.dest) return(-EINVAL);
  1088.   if (sin.sin_addr.s_addr != sk->daddr) return(-EINVAL);
  1089.   return(tcp_write(sk, from, len, nonblock, flags));
  1090. }
  1091.  
  1092.  
  1093. static void
  1094. tcp_read_wakeup(struct sock *sk)
  1095. {
  1096.   int tmp;
  1097.   struct device *dev = NULL;
  1098.   struct tcphdr *t1;
  1099.   struct sk_buff *buff;
  1100.  
  1101.   DPRINTF((DBG_TCP, "in tcp read wakeup\n"));
  1102.   if (!sk->ack_backlog) return;
  1103.  
  1104.   /*
  1105.    * FIXME: we need to put code here to prevent this routine from
  1106.    * being called.  Being called once in a while is ok, so only check
  1107.    * if this is the second time in a row.
  1108.    */
  1109.  
  1110.   /*
  1111.    * We need to grab some memory, and put together an ack,
  1112.    * and then put it into the queue to be sent.
  1113.    */
  1114.   buff = sk->prot->wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
  1115.   if (buff == NULL) {
  1116.     /* Try again real soon. */
  1117.     reset_timer(sk, TIME_WRITE, 10);
  1118.     return;
  1119.   }
  1120.  
  1121.   buff->mem_addr = buff;
  1122.   buff->mem_len = MAX_ACK_SIZE;
  1123.   buff->len = sizeof(struct tcphdr);
  1124.   buff->sk = sk;
  1125.  
  1126.   /* Put in the IP header and routing stuff. */
  1127.   tmp = sk->prot->build_header(buff, sk->saddr, sk->daddr, &dev,
  1128.                    IPPROTO_TCP, sk->opt, MAX_ACK_SIZE,sk->ip_tos,sk->ip_ttl);
  1129.   if (tmp < 0) {
  1130.       buff->free=1;
  1131.     sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
  1132.     return;
  1133.   }
  1134.  
  1135.   buff->len += tmp;
  1136.   t1 =(struct tcphdr *)(buff->data +tmp);
  1137.  
  1138.   memcpy(t1,(void *) &sk->dummy_th, sizeof(*t1));
  1139.   t1->seq = htonl(sk->sent_seq);
  1140.   t1->ack = 1;
  1141.   t1->res1 = 0;
  1142.   t1->res2 = 0;
  1143.   t1->rst = 0;
  1144.   t1->urg = 0;
  1145.   t1->syn = 0;
  1146.   t1->psh = 0;
  1147.   sk->ack_backlog = 0;
  1148.   sk->bytes_rcv = 0;
  1149.   sk->window = tcp_select_window(sk);/*sk->prot->rspace(sk);*/
  1150.   t1->window = ntohs(sk->window);
  1151.   t1->ack_seq = ntohl(sk->acked_seq);
  1152.   t1->doff = sizeof(*t1)/4;
  1153.   tcp_send_check(t1, sk->saddr, sk->daddr, sizeof(*t1), sk);
  1154.   sk->prot->queue_xmit(sk, dev, buff, 1);
  1155. }
  1156.  
  1157.  
  1158. /*
  1159.  * FIXME:
  1160.  * This routine frees used buffers.
  1161.  * It should consider sending an ACK to let the
  1162.  * other end know we now have a bigger window.
  1163.  */
  1164. static void
  1165. cleanup_rbuf(struct sock *sk)
  1166. {
  1167.   unsigned long flags;
  1168.   int left;
  1169.   struct sk_buff *skb;
  1170.  
  1171.   if(sk->debug)
  1172.       printk("cleaning rbuf for sk=%p\n", sk);
  1173.   
  1174.   save_flags(flags);
  1175.   cli();
  1176.   
  1177.   left = sk->prot->rspace(sk);
  1178.  
  1179.   /*
  1180.    * We have to loop through all the buffer headers,
  1181.    * and try to free up all the space we can.
  1182.    */
  1183.   while((skb=skb_peek(&sk->rqueue)) != NULL ) 
  1184.   {
  1185.     if (!skb->used) 
  1186.         break;
  1187.     skb_unlink(skb);
  1188.     skb->sk = sk;
  1189.     kfree_skb(skb, FREE_READ);
  1190.   }
  1191.  
  1192.   restore_flags(flags);
  1193.  
  1194.   /*
  1195.    * FIXME:
  1196.    * At this point we should send an ack if the difference
  1197.    * in the window, and the amount of space is bigger than
  1198.    * TCP_WINDOW_DIFF.
  1199.    */
  1200.   DPRINTF((DBG_TCP, "sk->window left = %d, sk->prot->rspace(sk)=%d\n",
  1201.             sk->window - sk->bytes_rcv, sk->prot->rspace(sk)));
  1202.  
  1203.   if(sk->debug)
  1204.       printk("sk->rspace = %lu, was %d\n", sk->prot->rspace(sk),
  1205.                           left);
  1206.   if (sk->prot->rspace(sk) != left) 
  1207.   {
  1208.     /*
  1209.      * This area has caused the most trouble.  The current strategy
  1210.      * is to simply do nothing if the other end has room to send at
  1211.      * least 3 full packets, because the ack from those will auto-
  1212.      * matically update the window.  If the other end doesn't think
  1213.      * we have much space left, but we have room for atleast 1 more
  1214.      * complete packet than it thinks we do, we will send an ack
  1215.      * immediatedly.  Otherwise we will wait up to .5 seconds in case
  1216.      * the user reads some more.
  1217.      */
  1218.     sk->ack_backlog++;
  1219. /*
  1220.  * It's unclear whether to use sk->mtu or sk->mss here.  They differ only
  1221.  * if the other end is offering a window smaller than the agreed on MSS
  1222.  * (called sk->mtu here).  In theory there's no connection between send
  1223.  * and receive, and so no reason to think that they're going to send
  1224.  * small packets.  For the moment I'm using the hack of reducing the mss
  1225.  * only on the send side, so I'm putting mtu here.
  1226.  */
  1227.     if ((sk->prot->rspace(sk) > (sk->window - sk->bytes_rcv + sk->mtu))) {
  1228.         /* Send an ack right now. */
  1229.         tcp_read_wakeup(sk);
  1230.     } else {
  1231.         /* Force it to send an ack soon. */
  1232.         int was_active = del_timer(&sk->timer);
  1233.         if (!was_active || TCP_ACK_TIME < sk->timer.expires) {
  1234.             reset_timer(sk, TIME_WRITE, TCP_ACK_TIME);
  1235.         } else
  1236.             add_timer(&sk->timer);
  1237.     }
  1238.   }
  1239.  
  1240.  
  1241. /* Handle reading urgent data. */
  1242. static int
  1243. tcp_read_urg(struct sock * sk, int nonblock,
  1244.          unsigned char *to, int len, unsigned flags)
  1245. {
  1246.     struct wait_queue wait = { current, NULL };
  1247.  
  1248.     while (len > 0) {
  1249.         if (sk->urginline || !sk->urg_data || sk->urg_data == URG_READ)
  1250.             return -EINVAL;
  1251.         if (sk->urg_data & URG_VALID) {
  1252.             char c = sk->urg_data;
  1253.             if (!(flags & MSG_PEEK))
  1254.                 sk->urg_data = URG_READ;
  1255.             put_fs_byte(c, to);
  1256.             return 1;
  1257.         }
  1258.  
  1259.         if (sk->err) {
  1260.             int tmp = -sk->err;
  1261.             sk->err = 0;
  1262.             return tmp;
  1263.         }
  1264.  
  1265.         if (sk->state == TCP_CLOSE || sk->done) {
  1266.             if (!sk->done) {
  1267.                 sk->done = 1;
  1268.                 return 0;
  1269.             }
  1270.             return -ENOTCONN;
  1271.         }
  1272.  
  1273.         if (sk->shutdown & RCV_SHUTDOWN) {
  1274.             sk->done = 1;
  1275.             return 0;
  1276.         }
  1277.  
  1278.         if (nonblock)
  1279.             return -EAGAIN;
  1280.  
  1281.         if (current->signal & ~current->blocked)
  1282.             return -ERESTARTSYS;
  1283.  
  1284.         current->state = TASK_INTERRUPTIBLE;
  1285.         add_wait_queue(sk->sleep, &wait);
  1286.         if ((sk->urg_data & URG_NOTYET) && sk->err == 0 &&
  1287.             !(sk->shutdown & RCV_SHUTDOWN))
  1288.             schedule();
  1289.         remove_wait_queue(sk->sleep, &wait);
  1290.         current->state = TASK_RUNNING;
  1291.     }
  1292.     return 0;
  1293. }
  1294.  
  1295.  
  1296. /* This routine copies from a sock struct into the user buffer. */
  1297. static int tcp_read(struct sock *sk, unsigned char *to,
  1298.     int len, int nonblock, unsigned flags)
  1299. {
  1300.     struct wait_queue wait = { current, NULL };
  1301.     int copied = 0;
  1302.     unsigned long peek_seq;
  1303.     unsigned long *seq;
  1304.     unsigned long used;
  1305.     int err;
  1306.  
  1307.     if (len == 0)
  1308.         return 0;
  1309.  
  1310.     if (len < 0)
  1311.         return -EINVAL;
  1312.  
  1313.     err = verify_area(VERIFY_WRITE, to, len);
  1314.     if (err)
  1315.         return err;
  1316.  
  1317.     /* This error should be checked. */
  1318.     if (sk->state == TCP_LISTEN)
  1319.         return -ENOTCONN;
  1320.  
  1321.     /* Urgent data needs to be handled specially. */
  1322.     if (flags & MSG_OOB)
  1323.         return tcp_read_urg(sk, nonblock, to, len, flags);
  1324.  
  1325.     peek_seq = sk->copied_seq;
  1326.     seq = &sk->copied_seq;
  1327.     if (flags & MSG_PEEK)
  1328.         seq = &peek_seq;
  1329.  
  1330.     add_wait_queue(sk->sleep, &wait);
  1331.     sk->inuse = 1;
  1332.     while (len > 0) {
  1333.         struct sk_buff * skb;
  1334.         unsigned long offset;
  1335.     
  1336.         /*
  1337.          * are we at urgent data? Stop if we have read anything.
  1338.          */
  1339.         if (copied && sk->urg_data && sk->urg_seq == 1+*seq)
  1340.             break;
  1341.  
  1342.         current->state = TASK_INTERRUPTIBLE;
  1343.  
  1344.         skb = sk->rqueue;
  1345.         do {
  1346.             if (!skb)
  1347.                 break;
  1348.             if (before(1+*seq, skb->h.th->seq))
  1349.                 break;
  1350.             offset = 1 + *seq - skb->h.th->seq;
  1351.             if (skb->h.th->syn)
  1352.                 offset--;
  1353.             if (offset < skb->len)
  1354.                 goto found_ok_skb;
  1355.             if (!(flags & MSG_PEEK))
  1356.                 skb->used = 1;
  1357.             skb = (struct sk_buff *)skb->next;
  1358.         } while (skb != sk->rqueue);
  1359.  
  1360.         if (copied)
  1361.             break;
  1362.  
  1363.         if (sk->err) {
  1364.             copied = -sk->err;
  1365.             sk->err = 0;
  1366.             break;
  1367.         }
  1368.  
  1369.         if (sk->state == TCP_CLOSE) {
  1370.             if (!sk->done) {
  1371.                 sk->done = 1;
  1372.                 break;
  1373.             }
  1374.             copied = -ENOTCONN;
  1375.             break;
  1376.         }
  1377.  
  1378.         if (sk->shutdown & RCV_SHUTDOWN) {
  1379.             sk->done = 1;
  1380.             break;
  1381.         }
  1382.             
  1383.         if (nonblock) {
  1384.             copied = -EAGAIN;
  1385.             break;
  1386.         }
  1387.  
  1388.         cleanup_rbuf(sk);
  1389.         release_sock(sk);
  1390.         schedule();
  1391.         sk->inuse = 1;
  1392.  
  1393.         if (current->signal & ~current->blocked) {
  1394.             copied = -ERESTARTSYS;
  1395.             break;
  1396.         }
  1397.         continue;
  1398.  
  1399.     found_ok_skb:
  1400.         /* Ok so how much can we use ? */
  1401.         used = skb->len - offset;
  1402.         if (len < used)
  1403.             used = len;
  1404.         /* do we have urgent data here? */
  1405.         if (sk->urg_data) {
  1406.             unsigned long urg_offset = sk->urg_seq - (1 + *seq);
  1407.             if (urg_offset < used) {
  1408.                 if (!urg_offset) {
  1409.                     if (!sk->urginline) {
  1410.                         ++*seq;
  1411.                         offset++;
  1412.                         used--;
  1413.                     }
  1414.                 } else
  1415.                     used = urg_offset;
  1416.             }
  1417.         }
  1418.         /* Copy it */
  1419.         memcpy_tofs(to,((unsigned char *)skb->h.th) +
  1420.             skb->h.th->doff*4 + offset, used);
  1421.         copied += used;
  1422.         len -= used;
  1423.         to += used;
  1424.         *seq += used;
  1425.         if (after(sk->copied_seq+1,sk->urg_seq))
  1426.             sk->urg_data = 0;
  1427.         if (!(flags & MSG_PEEK) && (used + offset >= skb->len))
  1428.             skb->used = 1;
  1429.     }
  1430.     remove_wait_queue(sk->sleep, &wait);
  1431.     current->state = TASK_RUNNING;
  1432.  
  1433.     /* Clean up data we have read: This will do ACK frames */
  1434.     cleanup_rbuf(sk);
  1435.     release_sock(sk);
  1436.     DPRINTF((DBG_TCP, "tcp_read: returning %d\n", copied));
  1437.     return copied;
  1438. }
  1439.  
  1440.  
  1441. /*
  1442.  * Send a FIN without closing the connection.
  1443.  * Not called at interrupt time.
  1444.  */
  1445. void
  1446. tcp_shutdown(struct sock *sk, int how)
  1447. {
  1448.   struct sk_buff *buff;
  1449.   struct tcphdr *t1, *th;
  1450.   struct proto *prot;
  1451.   int tmp;
  1452.   struct device *dev = NULL;
  1453.  
  1454.   /*
  1455.    * We need to grab some memory, and put together a FIN,
  1456.    * and then put it into the queue to be sent.
  1457.    * FIXME:
  1458.    *    Tim MacKenzie(tym@dibbler.cs.monash.edu.au) 4 Dec '92.
  1459.    *    Most of this is guesswork, so maybe it will work...
  1460.    */
  1461.   /* If we've already sent a FIN, return. */
  1462.   if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2) return;
  1463.   if (!(how & SEND_SHUTDOWN)) return;
  1464.   sk->inuse = 1;
  1465.  
  1466.   /* Clear out any half completed packets. */
  1467.   if (sk->partial)
  1468.     tcp_send_partial(sk);
  1469.  
  1470.   prot =(struct proto *)sk->prot;
  1471.   th =(struct tcphdr *)&sk->dummy_th;
  1472.   release_sock(sk); /* incase the malloc sleeps. */
  1473.   buff = prot->wmalloc(sk, MAX_RESET_SIZE,1 , GFP_KERNEL);
  1474.   if (buff == NULL) return;
  1475.   sk->inuse = 1;
  1476.  
  1477.   DPRINTF((DBG_TCP, "tcp_shutdown_send buff = %X\n", buff));
  1478.   buff->mem_addr = buff;
  1479.   buff->mem_len = MAX_RESET_SIZE;
  1480.   buff->sk = sk;
  1481.   buff->len = sizeof(*t1);
  1482.   t1 =(struct tcphdr *) buff->data;
  1483.  
  1484.   /* Put in the IP header and routing stuff. */
  1485.   tmp = prot->build_header(buff,sk->saddr, sk->daddr, &dev,
  1486.                IPPROTO_TCP, sk->opt,
  1487.                sizeof(struct tcphdr),sk->ip_tos,sk->ip_ttl);
  1488.   if (tmp < 0) {
  1489.       buff->free=1;
  1490.     prot->wfree(sk,buff->mem_addr, buff->mem_len);
  1491.     release_sock(sk);
  1492.     DPRINTF((DBG_TCP, "Unable to build header for fin.\n"));
  1493.     return;
  1494.   }
  1495.  
  1496.   t1 =(struct tcphdr *)((char *)t1 +tmp);
  1497.   buff->len += tmp;
  1498.   buff->dev = dev;
  1499.   memcpy(t1, th, sizeof(*t1));
  1500.   t1->seq = ntohl(sk->write_seq);
  1501.   sk->write_seq++;
  1502.   buff->h.seq = sk->write_seq;
  1503.   t1->ack = 1;
  1504.   t1->ack_seq = ntohl(sk->acked_seq);
  1505.   t1->window = ntohs(sk->window=tcp_select_window(sk)/*sk->prot->rspace(sk)*/);
  1506.   t1->fin = 1;
  1507.   t1->rst = 0;
  1508.   t1->doff = sizeof(*t1)/4;
  1509.   tcp_send_check(t1, sk->saddr, sk->daddr, sizeof(*t1), sk);
  1510.  
  1511.   /*
  1512.    * Can't just queue this up.
  1513.    * It should go at the end of the write queue.
  1514.    */
  1515.   if (sk->wback != NULL) {
  1516.       buff->free=0;    
  1517.     buff->next = NULL;
  1518.     sk->wback->next = buff;
  1519.     sk->wback = buff;
  1520.     buff->magic = TCP_WRITE_QUEUE_MAGIC;
  1521.   } else {
  1522.         sk->sent_seq = sk->write_seq;
  1523.     sk->prot->queue_xmit(sk, dev, buff, 0);
  1524.   }
  1525.  
  1526.   if (sk->state == TCP_ESTABLISHED) sk->state = TCP_FIN_WAIT1;
  1527.     else sk->state = TCP_FIN_WAIT2;
  1528.  
  1529.   release_sock(sk);
  1530. }
  1531.  
  1532.  
  1533. static int
  1534. tcp_recvfrom(struct sock *sk, unsigned char *to,
  1535.          int to_len, int nonblock, unsigned flags,
  1536.          struct sockaddr_in *addr, int *addr_len)
  1537. {
  1538.   struct sockaddr_in sin;
  1539.   int len;
  1540.   int err;
  1541.   int result;
  1542.   
  1543.   /* Have to check these first unlike the old code. If 
  1544.      we check them after we lose data on an error
  1545.      which is wrong */
  1546.   err = verify_area(VERIFY_WRITE,addr_len,sizeof(long));
  1547.   if(err)
  1548.       return err;
  1549.   len = get_fs_long(addr_len);
  1550.   if(len > sizeof(sin))
  1551.       len = sizeof(sin);
  1552.   err=verify_area(VERIFY_WRITE, addr, len);  
  1553.   if(err)
  1554.       return err;
  1555.       
  1556.   result=tcp_read(sk, to, to_len, nonblock, flags);
  1557.  
  1558.   if (result < 0) return(result);
  1559.   
  1560.   sin.sin_family = AF_INET;
  1561.   sin.sin_port = sk->dummy_th.dest;
  1562.   sin.sin_addr.s_addr = sk->daddr;
  1563.  
  1564.   memcpy_tofs(addr, &sin, len);
  1565.   put_fs_long(len, addr_len);
  1566.   return(result);
  1567. }
  1568.  
  1569.  
  1570. /* This routine will send an RST to the other tcp. */
  1571. static void
  1572. tcp_reset(unsigned long saddr, unsigned long daddr, struct tcphdr *th,
  1573.       struct proto *prot, struct options *opt, struct device *dev, int tos, int ttl)
  1574. {
  1575.   struct sk_buff *buff;
  1576.   struct tcphdr *t1;
  1577.   int tmp;
  1578.  
  1579.   /*
  1580.    * We need to grab some memory, and put together an RST,
  1581.    * and then put it into the queue to be sent.
  1582.    */
  1583.   buff = prot->wmalloc(NULL, MAX_RESET_SIZE, 1, GFP_ATOMIC);
  1584.   if (buff == NULL) 
  1585.       return;
  1586.  
  1587.   DPRINTF((DBG_TCP, "tcp_reset buff = %X\n", buff));
  1588.   buff->mem_addr = buff;
  1589.   buff->mem_len = MAX_RESET_SIZE;
  1590.   buff->len = sizeof(*t1);
  1591.   buff->sk = NULL;
  1592.   buff->dev = dev;
  1593.  
  1594.   t1 =(struct tcphdr *) buff->data;
  1595.  
  1596.   /* Put in the IP header and routing stuff. */
  1597.   tmp = prot->build_header(buff, saddr, daddr, &dev, IPPROTO_TCP, opt,
  1598.                sizeof(struct tcphdr),tos,ttl);
  1599.   if (tmp < 0) {
  1600.       buff->free = 1;
  1601.     prot->wfree(NULL, buff->mem_addr, buff->mem_len);
  1602.     return;
  1603.   }
  1604.   t1 =(struct tcphdr *)((char *)t1 +tmp);
  1605.   buff->len += tmp;
  1606.   memcpy(t1, th, sizeof(*t1));
  1607.  
  1608.   /* Swap the send and the receive. */
  1609.   t1->dest = th->source;
  1610.   t1->source = th->dest;
  1611.   t1->rst = 1;  
  1612.   t1->window = 0;
  1613.   
  1614.   if(th->ack)
  1615.   {
  1616.       t1->ack = 0;
  1617.       t1->seq = th->ack_seq;
  1618.       t1->ack_seq = 0;
  1619.   }
  1620.   else
  1621.   {
  1622.       t1->ack = 1;
  1623.       if(!th->syn)
  1624.           t1->ack_seq=htonl(th->seq);
  1625.       else
  1626.           t1->ack_seq=htonl(th->seq+1);
  1627.       t1->seq=0;
  1628.   }
  1629.  
  1630.   t1->syn = 0;
  1631.   t1->urg = 0;
  1632.   t1->fin = 0;
  1633.   t1->psh = 0;
  1634.   t1->doff = sizeof(*t1)/4;
  1635.   tcp_send_check(t1, saddr, daddr, sizeof(*t1), NULL);
  1636.   prot->queue_xmit(NULL, dev, buff, 1);
  1637. }
  1638.  
  1639.  
  1640. /*
  1641.  *    Look for tcp options. Parses everything but only knows about MSS.
  1642.  *      This routine is always called with the packet containing the SYN.
  1643.  *      However it may also be called with the ack to the SYN.  So you
  1644.  *      can't assume this is always the SYN.  It's always called after
  1645.  *      we have set up sk->mtu to our own MTU.
  1646.  */
  1647.  
  1648. static void
  1649. tcp_options(struct sock *sk, struct tcphdr *th)
  1650. {
  1651.   unsigned char *ptr;
  1652.   int length=(th->doff*4)-sizeof(struct tcphdr);
  1653.   int mss_seen = 0;
  1654.     
  1655.   ptr = (unsigned char *)(th + 1);
  1656.   
  1657.   while(length>0)
  1658.   {
  1659.       int opcode=*ptr++;
  1660.       int opsize=*ptr++;
  1661.       switch(opcode)
  1662.       {
  1663.           case TCPOPT_EOL:
  1664.               return;
  1665.           case TCPOPT_NOP:
  1666.               length-=2;
  1667.               continue;
  1668.           
  1669.           default:
  1670.               if(opsize<=2)    /* Avoid silly options looping forever */
  1671.                   return;
  1672.               switch(opcode)
  1673.               {
  1674.                   case TCPOPT_MSS:
  1675.                       if(opsize==4 && th->syn)
  1676.                       {
  1677.                           sk->mtu=min(sk->mtu,ntohs(*(unsigned short *)ptr));
  1678.                         mss_seen = 1;
  1679.                       }
  1680.                       break;
  1681.                   /* Add other options here as people feel the urge to implement stuff like large windows */
  1682.               }
  1683.               ptr+=opsize-2;
  1684.               length-=opsize;
  1685.       }
  1686.   }
  1687.   if (th->syn) {
  1688.     if (! mss_seen)
  1689.       sk->mtu=min(sk->mtu, 536);  /* default MSS if none sent */
  1690.   }
  1691.   sk->mss = min(sk->max_window, sk->mtu);
  1692. }
  1693.  
  1694. static inline unsigned long default_mask(unsigned long dst)
  1695. {
  1696.     dst = ntohl(dst);
  1697.     if (IN_CLASSA(dst))
  1698.         return htonl(IN_CLASSA_NET);
  1699.     if (IN_CLASSB(dst))
  1700.         return htonl(IN_CLASSB_NET);
  1701.     return htonl(IN_CLASSC_NET);
  1702. }
  1703.  
  1704. /*
  1705.  * This routine handles a connection request.
  1706.  * It should make sure we haven't already responded.
  1707.  * Because of the way BSD works, we have to send a syn/ack now.
  1708.  * This also means it will be harder to close a socket which is
  1709.  * listening.
  1710.  */
  1711. static void
  1712. tcp_conn_request(struct sock *sk, struct sk_buff *skb,
  1713.          unsigned long daddr, unsigned long saddr,
  1714.          struct options *opt, struct device *dev)
  1715. {
  1716.   struct sk_buff *buff;
  1717.   struct tcphdr *t1;
  1718.   unsigned char *ptr;
  1719.   struct sock *newsk;
  1720.   struct tcphdr *th;
  1721.   int tmp;
  1722.  
  1723.   DPRINTF((DBG_TCP, "tcp_conn_request(sk = %X, skb = %X, daddr = %X, sadd4= %X, \n"
  1724.       "                  opt = %X, dev = %X)\n",
  1725.       sk, skb, daddr, saddr, opt, dev));
  1726.   
  1727.   th = skb->h.th;
  1728.  
  1729.   /* If the socket is dead, don't accept the connection. */
  1730.   if (!sk->dead) {
  1731.       sk->data_ready(sk,0);
  1732.   } else {
  1733.     DPRINTF((DBG_TCP, "tcp_conn_request on dead socket\n"));
  1734.     tcp_reset(daddr, saddr, th, sk->prot, opt, dev, sk->ip_tos,sk->ip_ttl);
  1735.     kfree_skb(skb, FREE_READ);
  1736.     return;
  1737.   }
  1738.  
  1739.   /*
  1740.    * Make sure we can accept more.  This will prevent a
  1741.    * flurry of syns from eating up all our memory.
  1742.    */
  1743.   if (sk->ack_backlog >= sk->max_ack_backlog) {
  1744.     kfree_skb(skb, FREE_READ);
  1745.     return;
  1746.   }
  1747.  
  1748.   /*
  1749.    * We need to build a new sock struct.
  1750.    * It is sort of bad to have a socket without an inode attached
  1751.    * to it, but the wake_up's will just wake up the listening socket,
  1752.    * and if the listening socket is destroyed before this is taken
  1753.    * off of the queue, this will take care of it.
  1754.    */
  1755.   newsk = (struct sock *) kmalloc(sizeof(struct sock), GFP_ATOMIC);
  1756.   if (newsk == NULL) {
  1757.     /* just ignore the syn.  It will get retransmitted. */
  1758.     kfree_skb(skb, FREE_READ);
  1759.     return;
  1760.   }
  1761.  
  1762.   DPRINTF((DBG_TCP, "newsk = %X\n", newsk));
  1763.   memcpy((void *)newsk,(void *)sk, sizeof(*newsk));
  1764.   newsk->wback = NULL;
  1765.   newsk->wfront = NULL;
  1766.   newsk->rqueue = NULL;
  1767.   newsk->send_head = NULL;
  1768.   newsk->send_tail = NULL;
  1769.   newsk->back_log = NULL;
  1770.   newsk->rtt = TCP_CONNECT_TIME << 3;
  1771.   newsk->rto = TCP_CONNECT_TIME;
  1772.   newsk->mdev = 0;
  1773.   newsk->max_window = 0;
  1774.   newsk->cong_window = 1;
  1775.   newsk->cong_count = 0;
  1776.   newsk->ssthresh = 0;
  1777.   newsk->backoff = 0;
  1778.   newsk->blog = 0;
  1779.   newsk->intr = 0;
  1780.   newsk->proc = 0;
  1781.   newsk->done = 0;
  1782.   newsk->partial = NULL;
  1783.   newsk->pair = NULL;
  1784.   newsk->wmem_alloc = 0;
  1785.   newsk->rmem_alloc = 0;
  1786.  
  1787.   newsk->max_unacked = MAX_WINDOW - TCP_WINDOW_DIFF;
  1788.  
  1789.   newsk->err = 0;
  1790.   newsk->shutdown = 0;
  1791.   newsk->ack_backlog = 0;
  1792.   newsk->acked_seq = skb->h.th->seq+1;
  1793.   newsk->fin_seq = skb->h.th->seq;
  1794.   newsk->copied_seq = skb->h.th->seq;
  1795.   newsk->state = TCP_SYN_RECV;
  1796.   newsk->timeout = 0;
  1797.   newsk->write_seq = jiffies * SEQ_TICK - seq_offset;
  1798.   newsk->window_seq = newsk->write_seq;
  1799.   newsk->rcv_ack_seq = newsk->write_seq;
  1800.   newsk->urg_data = 0;
  1801.   newsk->retransmits = 0;
  1802.   newsk->destroy = 0;
  1803.   newsk->timer.data = (unsigned long)newsk;
  1804.   newsk->timer.function = &net_timer;
  1805.   newsk->dummy_th.source = skb->h.th->dest;
  1806.   newsk->dummy_th.dest = skb->h.th->source;
  1807.  
  1808.   /* Swap these two, they are from our point of view. */
  1809.   newsk->daddr = saddr;
  1810.   newsk->saddr = daddr;
  1811.  
  1812.   put_sock(newsk->num,newsk);
  1813.   newsk->dummy_th.res1 = 0;
  1814.   newsk->dummy_th.doff = 6;
  1815.   newsk->dummy_th.fin = 0;
  1816.   newsk->dummy_th.syn = 0;
  1817.   newsk->dummy_th.rst = 0;
  1818.   newsk->dummy_th.psh = 0;
  1819.   newsk->dummy_th.ack = 0;
  1820.   newsk->dummy_th.urg = 0;
  1821.   newsk->dummy_th.res2 = 0;
  1822.   newsk->acked_seq = skb->h.th->seq + 1;
  1823.   newsk->copied_seq = skb->h.th->seq;
  1824.  
  1825.   /* Grab the ttl and tos values and use them */
  1826.   newsk->ip_ttl=sk->ip_ttl;
  1827.   newsk->ip_tos=skb->ip_hdr->tos;
  1828.  
  1829. /* use 512 or whatever user asked for */
  1830. /* note use of sk->user_mss, since user has no direct access to newsk */
  1831.   if (sk->user_mss)
  1832.     newsk->mtu = sk->user_mss;
  1833.   else {
  1834. #ifdef SUBNETSARELOCAL
  1835.     if ((saddr ^ daddr) & default_mask(saddr))
  1836. #else
  1837.     if ((saddr ^ daddr) & dev->pa_mask)
  1838. #endif
  1839.       newsk->mtu = 576 - HEADER_SIZE;
  1840.     else
  1841.       newsk->mtu = MAX_WINDOW;
  1842.   }
  1843. /* but not bigger than device MTU */
  1844.   newsk->mtu = min(newsk->mtu, dev->mtu - HEADER_SIZE);
  1845.  
  1846. /* this will min with what arrived in the packet */
  1847.   tcp_options(newsk,skb->h.th);
  1848.  
  1849.   buff = newsk->prot->wmalloc(newsk, MAX_SYN_SIZE, 1, GFP_ATOMIC);
  1850.   if (buff == NULL) {
  1851.     sk->err = -ENOMEM;
  1852.     newsk->dead = 1;
  1853.     release_sock(newsk);
  1854.     kfree_skb(skb, FREE_READ);
  1855.     return;
  1856.   }
  1857.   
  1858.   buff->mem_addr = buff;
  1859.   buff->mem_len = MAX_SYN_SIZE;
  1860.   buff->len = sizeof(struct tcphdr)+4;
  1861.   buff->sk = newsk;
  1862.   
  1863.   t1 =(struct tcphdr *) buff->data;
  1864.  
  1865.   /* Put in the IP header and routing stuff. */
  1866.   tmp = sk->prot->build_header(buff, newsk->saddr, newsk->daddr, &dev,
  1867.                    IPPROTO_TCP, NULL, MAX_SYN_SIZE,sk->ip_tos,sk->ip_ttl);
  1868.  
  1869.   /* Something went wrong. */
  1870.   if (tmp < 0) {
  1871.     sk->err = tmp;
  1872.     buff->free=1;
  1873.     kfree_skb(buff,FREE_WRITE);
  1874.     newsk->dead = 1;
  1875.     release_sock(newsk);
  1876.     skb->sk = sk;
  1877.     kfree_skb(skb, FREE_READ);
  1878.     return;
  1879.   }
  1880.  
  1881.   buff->len += tmp;
  1882.   t1 =(struct tcphdr *)((char *)t1 +tmp);
  1883.   
  1884.   memcpy(t1, skb->h.th, sizeof(*t1));
  1885.   buff->h.seq = newsk->write_seq;
  1886.  
  1887.   /* Swap the send and the receive. */
  1888.   t1->dest = skb->h.th->source;
  1889.   t1->source = newsk->dummy_th.source;
  1890.   t1->seq = ntohl(newsk->write_seq++);
  1891.   t1->ack = 1;
  1892.   newsk->window = tcp_select_window(newsk);/*newsk->prot->rspace(newsk);*/
  1893.   newsk->sent_seq = newsk->write_seq;
  1894.   t1->window = ntohs(newsk->window);
  1895.   t1->res1 = 0;
  1896.   t1->res2 = 0;
  1897.   t1->rst = 0;
  1898.   t1->urg = 0;
  1899.   t1->psh = 0;
  1900.   t1->syn = 1;
  1901.   t1->ack_seq = ntohl(skb->h.th->seq+1);
  1902.   t1->doff = sizeof(*t1)/4+1;
  1903.  
  1904.   ptr =(unsigned char *)(t1+1);
  1905.   ptr[0] = 2;
  1906.   ptr[1] = 4;
  1907.   ptr[2] = ((newsk->mtu) >> 8) & 0xff;
  1908.   ptr[3] =(newsk->mtu) & 0xff;
  1909.  
  1910.   tcp_send_check(t1, daddr, saddr, sizeof(*t1)+4, newsk);
  1911.   newsk->prot->queue_xmit(newsk, dev, buff, 0);
  1912.  
  1913.   reset_timer(newsk, TIME_WRITE /* -1 ? FIXME ??? */, TCP_CONNECT_TIME);
  1914.   skb->sk = newsk;
  1915.  
  1916.   /* Charge the sock_buff to newsk. */
  1917.   sk->rmem_alloc -= skb->mem_len;
  1918.   newsk->rmem_alloc += skb->mem_len;
  1919.  
  1920.   skb_queue_tail(&sk->rqueue,skb);
  1921.   sk->ack_backlog++;
  1922.   release_sock(newsk);
  1923. }
  1924.  
  1925.  
  1926. static void
  1927. tcp_close(struct sock *sk, int timeout)
  1928. {
  1929.   struct sk_buff *buff;
  1930.   int need_reset = 0;
  1931.   struct tcphdr *t1, *th;
  1932.   struct proto *prot;
  1933.   struct device *dev=NULL;
  1934.   int tmp;
  1935.  
  1936.   /*
  1937.    * We need to grab some memory, and put together a FIN,
  1938.    * and then put it into the queue to be sent.
  1939.    */
  1940.   DPRINTF((DBG_TCP, "tcp_close((struct sock *)%X, %d)\n",sk, timeout));
  1941.   sk->inuse = 1;
  1942.   sk->keepopen = 1;
  1943.   sk->shutdown = SHUTDOWN_MASK;
  1944.  
  1945.   if (!sk->dead) 
  1946.       sk->state_change(sk);
  1947.  
  1948.   /* We need to flush the recv. buffs. */
  1949.   if (skb_peek(&sk->rqueue) != NULL) 
  1950.   {
  1951.     struct sk_buff *skb;
  1952.     if(sk->debug)
  1953.         printk("Clean rcv queue\n");
  1954.     while((skb=skb_dequeue(&sk->rqueue))!=NULL)
  1955.     {
  1956.         if(skb->len > 0 && after(skb->h.th->seq + skb->len + 1 , sk->copied_seq))
  1957.                 need_reset = 1;
  1958.         kfree_skb(skb, FREE_READ);
  1959.     }
  1960.     if(sk->debug)
  1961.         printk("Cleaned.\n");
  1962.   }
  1963.   sk->rqueue = NULL;
  1964.  
  1965.   /* Get rid off any half-completed packets. */
  1966.   if (sk->partial) {
  1967.     tcp_send_partial(sk);
  1968.   }
  1969.  
  1970.   switch(sk->state) {
  1971.     case TCP_FIN_WAIT1:
  1972.     case TCP_FIN_WAIT2:
  1973.     case TCP_LAST_ACK:
  1974.         /* start a timer. */
  1975.                 /* original code was 4 * sk->rtt.  In converting to the
  1976.          * new rtt representation, we can't quite use that.
  1977.          * it seems to make most sense to  use the backed off value
  1978.          */
  1979.         reset_timer(sk, TIME_CLOSE, 4 * sk->rto);
  1980.         if (timeout) tcp_time_wait(sk);
  1981.         release_sock(sk);
  1982.         return;    /* break causes a double release - messy */
  1983.     case TCP_TIME_WAIT:
  1984.         if (timeout) {
  1985.           sk->state = TCP_CLOSE;
  1986.         }
  1987.         release_sock(sk);
  1988.         return;
  1989.     case TCP_LISTEN:
  1990.         sk->state = TCP_CLOSE;
  1991.         release_sock(sk);
  1992.         return;
  1993.     case TCP_CLOSE:
  1994.         release_sock(sk);
  1995.         return;
  1996.     case TCP_CLOSE_WAIT:
  1997.     case TCP_ESTABLISHED:
  1998.     case TCP_SYN_SENT:
  1999.     case TCP_SYN_RECV:
  2000.         prot =(struct proto *)sk->prot;
  2001.         th =(struct tcphdr *)&sk->dummy_th;
  2002.         buff = prot->wmalloc(sk, MAX_FIN_SIZE, 1, GFP_ATOMIC);
  2003.         if (buff == NULL) {
  2004.             /* This will force it to try again later. */
  2005.             /* Or it would have if someone released the socket
  2006.                first. Anyway it might work now */
  2007.             release_sock(sk);
  2008.             if (sk->state != TCP_CLOSE_WAIT)
  2009.                     sk->state = TCP_ESTABLISHED;
  2010.             reset_timer(sk, TIME_CLOSE, 100);
  2011.             return;
  2012.         }
  2013.         buff->mem_addr = buff;
  2014.         buff->mem_len = MAX_FIN_SIZE;
  2015.         buff->sk = sk;
  2016.         buff->free = 1;
  2017.         buff->len = sizeof(*t1);
  2018.         t1 =(struct tcphdr *) buff->data;
  2019.  
  2020.         /* Put in the IP header and routing stuff. */
  2021.         tmp = prot->build_header(buff,sk->saddr, sk->daddr, &dev,
  2022.                      IPPROTO_TCP, sk->opt,
  2023.                          sizeof(struct tcphdr),sk->ip_tos,sk->ip_ttl);
  2024.         if (tmp < 0) {
  2025.             kfree_skb(buff,FREE_WRITE);
  2026.             DPRINTF((DBG_TCP, "Unable to build header for fin.\n"));
  2027.             release_sock(sk);
  2028.             return;
  2029.         }
  2030.  
  2031.         t1 =(struct tcphdr *)((char *)t1 +tmp);
  2032.         buff->len += tmp;
  2033.         buff->dev = dev;
  2034.         memcpy(t1, th, sizeof(*t1));
  2035.         t1->seq = ntohl(sk->write_seq);
  2036.         sk->write_seq++;
  2037.         buff->h.seq = sk->write_seq;
  2038.         t1->ack = 1;
  2039.  
  2040.         /* Ack everything immediately from now on. */
  2041.         sk->delay_acks = 0;
  2042.         t1->ack_seq = ntohl(sk->acked_seq);
  2043.         t1->window = ntohs(sk->window=tcp_select_window(sk)/*sk->prot->rspace(sk)*/);
  2044.         t1->fin = 1;
  2045.         t1->rst = need_reset;
  2046.         t1->doff = sizeof(*t1)/4;
  2047.         tcp_send_check(t1, sk->saddr, sk->daddr, sizeof(*t1), sk);
  2048.  
  2049.         if (sk->wfront == NULL) {
  2050.             sk->sent_seq = sk->write_seq;
  2051.             prot->queue_xmit(sk, dev, buff, 0);
  2052.         } else {
  2053.             reset_timer(sk, TIME_WRITE, sk->rto);
  2054.             buff->next = NULL;
  2055.             if (sk->wback == NULL) {
  2056.                 sk->wfront = buff;
  2057.             } else {
  2058.                 sk->wback->next = buff;
  2059.             }
  2060.             sk->wback = buff;
  2061.             buff->magic = TCP_WRITE_QUEUE_MAGIC;
  2062.         }
  2063.  
  2064.         if (sk->state == TCP_CLOSE_WAIT) {
  2065.             sk->state = TCP_FIN_WAIT2;
  2066.         } else {
  2067.             sk->state = TCP_FIN_WAIT1;
  2068.     }
  2069.   }
  2070.   release_sock(sk);
  2071. }
  2072.  
  2073.  
  2074. /*
  2075.  * This routine takes stuff off of the write queue,
  2076.  * and puts it in the xmit queue.
  2077.  */
  2078. static void
  2079. tcp_write_xmit(struct sock *sk)
  2080. {
  2081.   struct sk_buff *skb;
  2082.  
  2083.   DPRINTF((DBG_TCP, "tcp_write_xmit(sk=%X)\n", sk));
  2084.  
  2085.   /* The bytes will have to remain here. In time closedown will
  2086.      empty the write queue and all will be happy */
  2087.   if(sk->zapped)
  2088.     return;
  2089.  
  2090.   while(sk->wfront != NULL &&
  2091.         before(sk->wfront->h.seq, sk->window_seq +1) &&
  2092.     (sk->retransmits == 0 ||
  2093.      sk->timeout != TIME_WRITE ||
  2094.      before(sk->wfront->h.seq, sk->rcv_ack_seq +1))
  2095.         && sk->packets_out < sk->cong_window) {
  2096.         skb = sk->wfront;
  2097.         IS_SKB(skb);
  2098.         sk->wfront = skb->next;
  2099.         if (sk->wfront == NULL) sk->wback = NULL;
  2100.         skb->next = NULL;
  2101.         if (skb->magic != TCP_WRITE_QUEUE_MAGIC) {
  2102.             printk("tcp.c skb with bad magic(%X) on write queue. Squashing "
  2103.                 "queue\n", skb->magic);
  2104.             sk->wfront = NULL;
  2105.             sk->wback = NULL;
  2106.             return;
  2107.         }
  2108.         skb->magic = 0;
  2109.         DPRINTF((DBG_TCP, "Sending a packet.\n"));
  2110.  
  2111.         /* See if we really need to send the packet. */
  2112.         if (before(skb->h.seq, sk->rcv_ack_seq +1)) {
  2113.             sk->retransmits = 0;
  2114.             kfree_skb(skb, FREE_WRITE);
  2115.             if (!sk->dead) sk->write_space(sk);
  2116.         } else {
  2117.             sk->sent_seq = skb->h.seq;
  2118.             sk->prot->queue_xmit(sk, skb->dev, skb, skb->free);
  2119.         }
  2120.     }
  2121. }
  2122.  
  2123.  
  2124. /*
  2125.  * This routine sorts the send list, and resets the
  2126.  * sk->send_head and sk->send_tail pointers.
  2127.  */
  2128. void
  2129. sort_send(struct sock *sk)
  2130. {
  2131.   struct sk_buff *list = NULL;
  2132.   struct sk_buff *skb,*skb2,*skb3;
  2133.  
  2134.   for (skb = sk->send_head; skb != NULL; skb = skb2) {
  2135.     skb2 = (struct sk_buff *)skb->link3;
  2136.     if (list == NULL || before (skb2->h.seq, list->h.seq)) {
  2137.         skb->link3 = list;
  2138.         sk->send_tail = skb;
  2139.         list = skb;
  2140.     } else {
  2141.         for (skb3 = list; ; skb3 = (struct sk_buff *)skb3->link3) {
  2142.             if (skb3->link3 == NULL ||
  2143.                 before(skb->h.seq, skb3->link3->h.seq)) {
  2144.                 skb->link3 = skb3->link3;
  2145.                 skb3->link3 = skb;
  2146.                 if (skb->link3 == NULL) sk->send_tail = skb;
  2147.                 break;
  2148.             }
  2149.         }
  2150.     }
  2151.   }
  2152.   sk->send_head = list;
  2153. }
  2154.   
  2155.  
  2156. /* This routine deals with incoming acks, but not outgoing ones. */
  2157. static int
  2158. tcp_ack(struct sock *sk, struct tcphdr *th, unsigned long saddr, int len)
  2159. {
  2160.   unsigned long ack;
  2161.   int flag = 0;
  2162.   /* 
  2163.    * 1 - there was data in packet as well as ack or new data is sent or 
  2164.    *     in shutdown state
  2165.    * 2 - data from retransmit queue was acked and removed
  2166.    * 4 - window shrunk or data from retransmit queue was acked and removed
  2167.    */
  2168.  
  2169.   if(sk->zapped)
  2170.     return(1);    /* Dead, cant ack any more so why bother */
  2171.  
  2172.   ack = ntohl(th->ack_seq);
  2173.   DPRINTF((DBG_TCP, "tcp_ack ack=%d, window=%d, "
  2174.       "sk->rcv_ack_seq=%d, sk->window_seq = %d\n",
  2175.       ack, ntohs(th->window), sk->rcv_ack_seq, sk->window_seq));
  2176.  
  2177.   if (ntohs(th->window) > sk->max_window) {
  2178.       sk->max_window = ntohs(th->window);
  2179.     sk->mss = min(sk->max_window, sk->mtu);
  2180.   }
  2181.  
  2182.   if (sk->retransmits && sk->timeout == TIME_KEEPOPEN)
  2183.       sk->retransmits = 0;
  2184.  
  2185. /* not quite clear why the +1 and -1 here, and why not +1 in next line */
  2186.   if (after(ack, sk->sent_seq+1) || before(ack, sk->rcv_ack_seq-1)) {
  2187.     if (after(ack, sk->sent_seq) ||
  2188.        (sk->state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT)) {
  2189.         return(0);
  2190.     }
  2191.     if (sk->keepopen) {
  2192.         reset_timer(sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
  2193.     }
  2194.     return(1);
  2195.   }
  2196.  
  2197.   if (len != th->doff*4) flag |= 1;
  2198.  
  2199.   /* See if our window has been shrunk. */
  2200.   if (after(sk->window_seq, ack+ntohs(th->window))) {
  2201.     /*
  2202.      * We may need to move packets from the send queue
  2203.      * to the write queue, if the window has been shrunk on us.
  2204.      * The RFC says you are not allowed to shrink your window
  2205.      * like this, but if the other end does, you must be able
  2206.      * to deal with it.
  2207.      */
  2208.     struct sk_buff *skb;
  2209.     struct sk_buff *skb2;
  2210.     struct sk_buff *wskb = NULL;
  2211.   
  2212.     skb2 = sk->send_head;
  2213.     sk->send_head = NULL;
  2214.     sk->send_tail = NULL;
  2215.  
  2216.     flag |= 4;
  2217.  
  2218.     sk->window_seq = ack + ntohs(th->window);
  2219.     cli();
  2220.     while (skb2 != NULL) {
  2221.         skb = skb2;
  2222.         skb2 = (struct sk_buff *)skb->link3;
  2223.         skb->link3 = NULL;
  2224.         if (after(skb->h.seq, sk->window_seq)) {
  2225.             if (sk->packets_out > 0) sk->packets_out--;
  2226.             /* We may need to remove this from the dev send list. */
  2227.             if (skb->next != NULL) {
  2228.                 skb_unlink(skb);                
  2229.             }
  2230.             /* Now add it to the write_queue. */
  2231.             skb->magic = TCP_WRITE_QUEUE_MAGIC;
  2232.             if (wskb == NULL) {
  2233.                 skb->next = sk->wfront;
  2234.                 sk->wfront = skb;
  2235.             } else {
  2236.                 skb->next = wskb->next;
  2237.                 wskb->next = skb;
  2238.             }
  2239.             if (sk->wback == wskb) sk->wback = skb;
  2240.             wskb = skb;
  2241.         } else {
  2242.             if (sk->send_head == NULL) {
  2243.                 sk->send_head = skb;
  2244.                 sk->send_tail = skb;
  2245.             } else {
  2246.                 sk->send_tail->link3 = skb;
  2247.                 sk->send_tail = skb;
  2248.             }
  2249.             skb->link3 = NULL;
  2250.         }
  2251.     }
  2252.     sti();
  2253.   }
  2254.  
  2255.   if (sk->send_tail == NULL || sk->send_head == NULL) {
  2256.     sk->send_head = NULL;
  2257.     sk->send_tail = NULL;
  2258.     sk->packets_out= 0;
  2259.   }
  2260.  
  2261.   sk->window_seq = ack + ntohs(th->window);
  2262.  
  2263.   /* We don't want too many packets out there. */
  2264.   if (sk->timeout == TIME_WRITE && 
  2265.       sk->cong_window < 2048 && after(ack, sk->rcv_ack_seq)) {
  2266. /* 
  2267.  * This is Jacobson's slow start and congestion avoidance. 
  2268.  * SIGCOMM '88, p. 328.  Because we keep cong_window in integral
  2269.  * mss's, we can't do cwnd += 1 / cwnd.  Instead, maintain a 
  2270.  * counter and increment it once every cwnd times.  It's possible
  2271.  * that this should be done only if sk->retransmits == 0.  I'm
  2272.  * interpreting "new data is acked" as including data that has
  2273.  * been retransmitted but is just now being acked.
  2274.  */
  2275.     if (sk->cong_window < sk->ssthresh)  
  2276.       /* in "safe" area, increase */
  2277.       sk->cong_window++;
  2278.     else {
  2279.       /* in dangerous area, increase slowly.  In theory this is
  2280.          sk->cong_window += 1 / sk->cong_window
  2281.        */
  2282.       if (sk->cong_count >= sk->cong_window) {
  2283.         sk->cong_window++;
  2284.         sk->cong_count = 0;
  2285.       } else 
  2286.         sk->cong_count++;
  2287.     }
  2288.   }
  2289.  
  2290.   DPRINTF((DBG_TCP, "tcp_ack: Updating rcv ack sequence.\n"));
  2291.   sk->rcv_ack_seq = ack;
  2292.  
  2293.   /*
  2294.    * if this ack opens up a zero window, clear backoff.  It was
  2295.    * being used to time the probes, and is probably far higher than
  2296.    * it needs to be for normal retransmission
  2297.    */
  2298.   if (sk->timeout == TIME_PROBE0) {
  2299.       if (sk->wfront != NULL &&   /* should always be non-null */
  2300.         ! before (sk->window_seq, sk->wfront->h.seq)) {
  2301.       sk->retransmits = 0;
  2302.       sk->backoff = 0;
  2303.       /* recompute rto from rtt.  this eliminates any backoff */
  2304.       sk->rto = ((sk->rtt >> 2) + sk->mdev) >> 1;
  2305.       if (sk->rto > 120*HZ)
  2306.         sk->rto = 120*HZ;
  2307.       if (sk->rto < 1*HZ)
  2308.         sk->rto = 1*HZ;
  2309.     }
  2310.   }
  2311.  
  2312.   /* See if we can take anything off of the retransmit queue. */
  2313.   while(sk->send_head != NULL) {
  2314.     /* Check for a bug. */
  2315.     if (sk->send_head->link3 &&
  2316.         after(sk->send_head->h.seq, sk->send_head->link3->h.seq)) {
  2317.         printk("INET: tcp.c: *** bug send_list out of order.\n");
  2318.         sort_send(sk);
  2319.     }
  2320.  
  2321.     if (before(sk->send_head->h.seq, ack+1)) {
  2322.         struct sk_buff *oskb;
  2323.  
  2324.         if (sk->retransmits) {
  2325.  
  2326.           /* we were retransmitting.  don't count this in RTT est */
  2327.           flag |= 2;
  2328.  
  2329.           /*
  2330.            * even though we've gotten an ack, we're still
  2331.            * retransmitting as long as we're sending from
  2332.            * the retransmit queue.  Keeping retransmits non-zero
  2333.            * prevents us from getting new data interspersed with
  2334.            * retransmissions.
  2335.            */
  2336.  
  2337.           if (sk->send_head->link3)
  2338.             sk->retransmits = 1;
  2339.           else
  2340.             sk->retransmits = 0;
  2341.  
  2342.         }
  2343.  
  2344.           /*
  2345.          * Note that we only reset backoff and rto in the
  2346.          * rtt recomputation code.  And that doesn't happen
  2347.          * if there were retransmissions in effect.  So the
  2348.          * first new packet after the retransmissions is
  2349.          * sent with the backoff still in effect.  Not until
  2350.          * we get an ack from a non-retransmitted packet do
  2351.          * we reset the backoff and rto.  This allows us to deal
  2352.          * with a situation where the network delay has increased
  2353.          * suddenly.  I.e. Karn's algorithm. (SIGCOMM '87, p5.)
  2354.          */
  2355.  
  2356.         /* We have one less packet out there. */
  2357.         if (sk->packets_out > 0) sk->packets_out --;
  2358.         DPRINTF((DBG_TCP, "skb=%X skb->h.seq = %d acked ack=%d\n",
  2359.                 sk->send_head, sk->send_head->h.seq, ack));
  2360.  
  2361.         /* Wake up the process, it can probably write more. */
  2362.         if (!sk->dead) sk->write_space(sk);
  2363.  
  2364.         oskb = sk->send_head;
  2365.  
  2366.         if (!(flag&2)) {
  2367.           long m;
  2368.  
  2369.           /* The following amusing code comes from Jacobson's
  2370.            * article in SIGCOMM '88.  Note that rtt and mdev
  2371.            * are scaled versions of rtt and mean deviation.
  2372.            * This is designed to be as fast as possible 
  2373.            * m stands for "measurement".
  2374.            */
  2375.  
  2376.           m = jiffies - oskb->when;  /* RTT */
  2377.           m -= (sk->rtt >> 3);       /* m is now error in rtt est */
  2378.           sk->rtt += m;              /* rtt = 7/8 rtt + 1/8 new */
  2379.           if (m < 0)
  2380.             m = -m;             /* m is now abs(error) */
  2381.           m -= (sk->mdev >> 2);      /* similar update on mdev */
  2382.           sk->mdev += m;         /* mdev = 3/4 mdev + 1/4 new */
  2383.  
  2384.           /* now update timeout.  Note that this removes any backoff */
  2385.           sk->rto = ((sk->rtt >> 2) + sk->mdev) >> 1;
  2386.           if (sk->rto > 120*HZ)
  2387.             sk->rto = 120*HZ;
  2388.           if (sk->rto < 1*HZ)
  2389.             sk->rto = 1*HZ;
  2390.           sk->backoff = 0;
  2391.  
  2392.         }
  2393.         flag |= (2|4);
  2394.  
  2395.         cli();
  2396.  
  2397.         oskb = sk->send_head;
  2398.         IS_SKB(oskb);
  2399.         sk->send_head =(struct sk_buff *)oskb->link3;
  2400.         if (sk->send_head == NULL) {
  2401.             sk->send_tail = NULL;
  2402.         }
  2403.  
  2404.         /* We may need to remove this from the dev send list. */        
  2405.         skb_unlink(oskb);    /* Much easier! */
  2406.         sti();
  2407.         oskb->magic = 0;
  2408.         kfree_skb(oskb, FREE_WRITE); /* write. */
  2409.         if (!sk->dead) sk->write_space(sk);
  2410.     } else {
  2411.         break;
  2412.     }
  2413.   }
  2414.  
  2415.   /*
  2416.    * Maybe we can take some stuff off of the write queue,
  2417.    * and put it onto the xmit queue.
  2418.    */
  2419.   if (sk->wfront != NULL) {
  2420.     if (after (sk->window_seq+1, sk->wfront->h.seq) &&
  2421.             (sk->retransmits == 0 || 
  2422.          sk->timeout != TIME_WRITE ||
  2423.          before(sk->wfront->h.seq, sk->rcv_ack_seq +1))
  2424.         && sk->packets_out < sk->cong_window) {
  2425.         flag |= 1;
  2426.         tcp_write_xmit(sk);
  2427.      } else if (before(sk->window_seq, sk->wfront->h.seq) &&
  2428.             sk->send_head == NULL &&
  2429.             sk->ack_backlog == 0 &&
  2430.             sk->state != TCP_TIME_WAIT) {
  2431.              reset_timer(sk, TIME_PROBE0, sk->rto);
  2432.      }        
  2433.   } else {
  2434.     if (sk->send_head == NULL && sk->ack_backlog == 0 &&
  2435.         sk->state != TCP_TIME_WAIT && !sk->keepopen) {
  2436.         DPRINTF((DBG_TCP, "Nothing to do, going to sleep.\n")); 
  2437.         if (!sk->dead) sk->write_space(sk);
  2438.  
  2439.         if (sk->keepopen)
  2440.             reset_timer(sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
  2441.         else
  2442.             delete_timer(sk);
  2443.     } else {
  2444.         if (sk->state != (unsigned char) sk->keepopen) {
  2445.             reset_timer(sk, TIME_WRITE, sk->rto);
  2446.         }
  2447.         if (sk->state == TCP_TIME_WAIT) {
  2448.             reset_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
  2449.         }
  2450.     }
  2451.   }
  2452.  
  2453.   if (sk->packets_out == 0 && sk->partial != NULL &&
  2454.       sk->wfront == NULL && sk->send_head == NULL) {
  2455.     flag |= 1;
  2456.     tcp_send_partial(sk);
  2457.   }
  2458.  
  2459.   /* See if we are done. */
  2460.   if (sk->state == TCP_TIME_WAIT) {
  2461.     if (!sk->dead)
  2462.         sk->state_change(sk);
  2463.     if (sk->rcv_ack_seq == sk->write_seq && sk->acked_seq == sk->fin_seq) {
  2464.         flag |= 1;
  2465.         sk->state = TCP_CLOSE;
  2466.         sk->shutdown = SHUTDOWN_MASK;
  2467.     }
  2468.   }
  2469.  
  2470.   if (sk->state == TCP_LAST_ACK || sk->state == TCP_FIN_WAIT2) {
  2471.     if (!sk->dead) sk->state_change(sk);
  2472.     if (sk->rcv_ack_seq == sk->write_seq) {
  2473.         flag |= 1;
  2474.         if (sk->acked_seq != sk->fin_seq) {
  2475.             tcp_time_wait(sk);
  2476.         } else {
  2477.             DPRINTF((DBG_TCP, "tcp_ack closing socket - %X\n", sk));
  2478.             tcp_send_ack(sk->sent_seq, sk->acked_seq, sk,
  2479.                      th, sk->daddr);
  2480.             sk->shutdown = SHUTDOWN_MASK;
  2481.             sk->state = TCP_CLOSE;
  2482.         }
  2483.     }
  2484.   }
  2485.  
  2486. /*
  2487.  * I make no guarantees about the first clause in the following
  2488.  * test, i.e. "(!flag) || (flag&4)".  I'm not entirely sure under
  2489.  * what conditions "!flag" would be true.  However I think the rest
  2490.  * of the conditions would prevent that from causing any
  2491.  * unnecessary retransmission. 
  2492.  *   Clearly if the first packet has expired it should be 
  2493.  * retransmitted.  The other alternative, "flag&2 && retransmits", is
  2494.  * harder to explain:  You have to look carefully at how and when the
  2495.  * timer is set and with what timeout.  The most recent transmission always
  2496.  * sets the timer.  So in general if the most recent thing has timed
  2497.  * out, everything before it has as well.  So we want to go ahead and
  2498.  * retransmit some more.  If we didn't explicitly test for this
  2499.  * condition with "flag&2 && retransmits", chances are "when + rto < jiffies"
  2500.  * would not be true.  If you look at the pattern of timing, you can
  2501.  * show that rto is increased fast enough that the next packet would
  2502.  * almost never be retransmitted immediately.  Then you'd end up
  2503.  * waiting for a timeout to send each packet on the retranmission
  2504.  * queue.  With my implementation of the Karn sampling algorithm,
  2505.  * the timeout would double each time.  The net result is that it would
  2506.  * take a hideous amount of time to recover from a single dropped packet.
  2507.  * It's possible that there should also be a test for TIME_WRITE, but
  2508.  * I think as long as "send_head != NULL" and "retransmit" is on, we've
  2509.  * got to be in real retransmission mode.
  2510.  *   Note that ip_do_retransmit is called with all==1.  Setting cong_window
  2511.  * back to 1 at the timeout will cause us to send 1, then 2, etc. packets.
  2512.  * As long as no further losses occur, this seems reasonable.
  2513.  */
  2514.  
  2515.   if (((!flag) || (flag&4)) && sk->send_head != NULL &&
  2516.       (((flag&2) && sk->retransmits) ||
  2517.        (sk->send_head->when + sk->rto < jiffies))) {
  2518.     ip_do_retransmit(sk, 1);
  2519.     reset_timer(sk, TIME_WRITE, sk->rto);
  2520.       }
  2521.  
  2522.   DPRINTF((DBG_TCP, "leaving tcp_ack\n"));
  2523.   return(1);
  2524. }
  2525.  
  2526.  
  2527. /*
  2528.  * This routine handles the data.  If there is room in the buffer,
  2529.  * it will be have already been moved into it.  If there is no
  2530.  * room, then we will just have to discard the packet.
  2531.  */
  2532. static int
  2533. tcp_data(struct sk_buff *skb, struct sock *sk, 
  2534.      unsigned long saddr, unsigned short len)
  2535. {
  2536.   struct sk_buff *skb1, *skb2;
  2537.   struct tcphdr *th;
  2538.   int dup_dumped=0;
  2539.  
  2540.   th = skb->h.th;
  2541.   print_th(th);
  2542.   skb->len = len -(th->doff*4);
  2543.  
  2544.   DPRINTF((DBG_TCP, "tcp_data len = %d sk = %X:\n", skb->len, sk));
  2545.  
  2546.   sk->bytes_rcv += skb->len;
  2547.   if (skb->len == 0 && !th->fin && !th->urg && !th->psh) {
  2548.     /* Don't want to keep passing ack's back and forth. */
  2549.     if (!th->ack) tcp_send_ack(sk->sent_seq, sk->acked_seq,sk, th, saddr);
  2550.     kfree_skb(skb, FREE_READ);
  2551.     return(0);
  2552.   }
  2553.  
  2554.   if (sk->shutdown & RCV_SHUTDOWN) {
  2555.     sk->acked_seq = th->seq + skb->len + th->syn + th->fin;
  2556.     tcp_reset(sk->saddr, sk->daddr, skb->h.th,
  2557.     sk->prot, NULL, skb->dev, sk->ip_tos, sk->ip_ttl);
  2558.     sk->state = TCP_CLOSE;
  2559.     sk->err = EPIPE;
  2560.     sk->shutdown = SHUTDOWN_MASK;
  2561.     DPRINTF((DBG_TCP, "tcp_data: closing socket - %X\n", sk));
  2562.     kfree_skb(skb, FREE_READ);
  2563.     if (!sk->dead) sk->state_change(sk);
  2564.     return(0);
  2565.   }
  2566.  
  2567.   /*
  2568.    * Now we have to walk the chain, and figure out where this one
  2569.    * goes into it.  This is set up so that the last packet we received
  2570.    * will be the first one we look at, that way if everything comes
  2571.    * in order, there will be no performance loss, and if they come
  2572.    * out of order we will be able to fit things in nicely.
  2573.    */
  2574.  
  2575.   /* This should start at the last one, and then go around forwards. */
  2576.   if (sk->rqueue == NULL) {
  2577.     DPRINTF((DBG_TCP, "tcp_data: skb = %X:\n", skb));
  2578. #ifdef OLDWAY
  2579.     sk->rqueue = skb;
  2580.     skb->next = skb;
  2581.     skb->prev = skb;
  2582.     skb->list = &sk->rqueue;
  2583. #else
  2584.     skb_queue_head(&sk->rqueue,skb);
  2585. #endif        
  2586.     skb1= NULL;
  2587.   } else {
  2588.     DPRINTF((DBG_TCP, "tcp_data adding to chain sk = %X:\n", sk));
  2589.     for(skb1=sk->rqueue->prev; ; skb1 =(struct sk_buff *)skb1->prev) {
  2590.         if(sk->debug)
  2591.         {
  2592.             printk("skb1=%p :", skb1);
  2593.             printk("skb1->h.th->seq = %ld: ", skb1->h.th->seq);
  2594.             printk("skb->h.th->seq = %ld\n",skb->h.th->seq);
  2595.             printk("copied_seq = %ld acked_seq = %ld\n", sk->copied_seq,
  2596.                     sk->acked_seq);
  2597.         }
  2598. #ifdef OLD        
  2599.         if (after(th->seq+1, skb1->h.th->seq)) {
  2600.             skb->prev = skb1;
  2601.             skb->next = skb1->next;
  2602.             skb->next->prev = skb;
  2603.             skb1->next = skb;
  2604.             if (skb1 == sk->rqueue) sk->rqueue = skb;
  2605.             break;
  2606.         }
  2607.         if (skb1->prev == sk->rqueue) {
  2608.             skb->next= skb1;
  2609.             skb->prev = skb1->prev;
  2610.             skb->prev->next = skb;
  2611.             skb1->prev = skb;
  2612.             skb1 = NULL; /* so we know we might be able
  2613.                     to ack stuff. */
  2614.             break;
  2615.         }
  2616. #else
  2617.         if (th->seq==skb1->h.th->seq && skb->len>= skb1->len)
  2618.         {
  2619.             skb_append(skb1,skb);
  2620.             skb_unlink(skb1);
  2621.             kfree_skb(skb1,FREE_READ);
  2622.             dup_dumped=1;
  2623.             skb1=NULL;
  2624.             break;
  2625.         }
  2626.         if (after(th->seq+1, skb1->h.th->seq))
  2627.         {
  2628.             skb_append(skb1,skb);
  2629.             break;
  2630.         }
  2631.         if (skb1 == sk->rqueue)
  2632.         {
  2633.             skb_queue_head(&sk->rqueue, skb);        
  2634.             break;
  2635.         }
  2636. #endif        
  2637.     }
  2638.     DPRINTF((DBG_TCP, "skb = %X:\n", skb));
  2639.   }
  2640.  
  2641.   th->ack_seq = th->seq + skb->len;
  2642.   if (th->syn) th->ack_seq++;
  2643.   if (th->fin) th->ack_seq++;
  2644.  
  2645.   if (before(sk->acked_seq, sk->copied_seq)) {
  2646.     printk("*** tcp.c:tcp_data bug acked < copied\n");
  2647.     sk->acked_seq = sk->copied_seq;
  2648.   }
  2649.  
  2650.   /* Now figure out if we can ack anything. */
  2651.   if ((!dup_dumped && (skb1 == NULL || skb1->acked)) || before(th->seq, sk->acked_seq+1)) {
  2652.       if (before(th->seq, sk->acked_seq+1)) {
  2653.         int newwindow;
  2654.  
  2655.         if (after(th->ack_seq, sk->acked_seq)) {
  2656.             newwindow = sk->window -
  2657.                        (th->ack_seq - sk->acked_seq);
  2658.             if (newwindow < 0)
  2659.                 newwindow = 0;    
  2660.             sk->window = newwindow;
  2661.             sk->acked_seq = th->ack_seq;
  2662.         }
  2663.         skb->acked = 1;
  2664.  
  2665.         /* When we ack the fin, we turn on the RCV_SHUTDOWN flag. */
  2666.         if (skb->h.th->fin) {
  2667.             if (!sk->dead) sk->state_change(sk);
  2668.             sk->shutdown |= RCV_SHUTDOWN;
  2669.         }
  2670.       
  2671.         for(skb2 = (struct sk_buff *)skb->next;
  2672.             skb2 !=(struct sk_buff *) sk->rqueue;
  2673.             skb2 = (struct sk_buff *)skb2->next) {
  2674.             if (before(skb2->h.th->seq, sk->acked_seq+1)) {
  2675.                 if (after(skb2->h.th->ack_seq, sk->acked_seq))
  2676.                 {
  2677.                     newwindow = sk->window -
  2678.                      (skb2->h.th->ack_seq - sk->acked_seq);
  2679.                     if (newwindow < 0)
  2680.                         newwindow = 0;    
  2681.                     sk->window = newwindow;
  2682.                     sk->acked_seq = skb2->h.th->ack_seq;
  2683.                 }
  2684.                 skb2->acked = 1;
  2685.  
  2686.                 /*
  2687.                  * When we ack the fin, we turn on
  2688.                  * the RCV_SHUTDOWN flag.
  2689.                  */
  2690.                 if (skb2->h.th->fin) {
  2691.                     sk->shutdown |= RCV_SHUTDOWN;
  2692.                     if (!sk->dead) sk->state_change(sk);
  2693.                 }
  2694.  
  2695.                 /* Force an immediate ack. */
  2696.                 sk->ack_backlog = sk->max_ack_backlog;
  2697.             } else {
  2698.                 break;
  2699.             }
  2700.         }
  2701.  
  2702.         /*
  2703.          * This also takes care of updating the window.
  2704.          * This if statement needs to be simplified.
  2705.          */
  2706.         if (!sk->delay_acks ||
  2707.             sk->ack_backlog >= sk->max_ack_backlog || 
  2708.             sk->bytes_rcv > sk->max_unacked || th->fin) {
  2709. /*            tcp_send_ack(sk->sent_seq, sk->acked_seq,sk,th, saddr); */
  2710.         } else {
  2711.             sk->ack_backlog++;
  2712.             if(sk->debug)
  2713.                 printk("Ack queued.\n");
  2714.             reset_timer(sk, TIME_WRITE, TCP_ACK_TIME);
  2715.         }
  2716.     }
  2717.   }
  2718.  
  2719.   /*
  2720.    * If we've missed a packet, send an ack.
  2721.    * Also start a timer to send another.
  2722.    */
  2723.   if (!skb->acked) {
  2724.     /*
  2725.      * This is important.  If we don't have much room left,
  2726.      * we need to throw out a few packets so we have a good
  2727.      * window.  Note that mtu is used, not mss, because mss is really
  2728.      * for the send side.  He could be sending us stuff as large as mtu.
  2729.      */
  2730.     while (sk->prot->rspace(sk) < sk->mtu) {
  2731.         skb1 = skb_peek(&sk->rqueue);
  2732.         if (skb1 == NULL) {
  2733.             printk("INET: tcp.c:tcp_data memory leak detected.\n");
  2734.             break;
  2735.         }
  2736.  
  2737.         /* Don't throw out something that has been acked. */
  2738.         if (skb1->acked) {
  2739.             break;
  2740.         }
  2741.         
  2742.         skb_unlink(skb1);
  2743. #ifdef OLDWAY        
  2744.         if (skb1->prev == skb1) {
  2745.             sk->rqueue = NULL;
  2746.         } else {
  2747.             sk->rqueue = (struct sk_buff *)skb1->prev;
  2748.             skb1->next->prev = skb1->prev;
  2749.             skb1->prev->next = skb1->next;
  2750.         }
  2751. #endif        
  2752.         kfree_skb(skb1, FREE_READ);
  2753.     }
  2754.     tcp_send_ack(sk->sent_seq, sk->acked_seq, sk, th, saddr);
  2755.     sk->ack_backlog++;
  2756.     reset_timer(sk, TIME_WRITE, TCP_ACK_TIME);
  2757.   } else {
  2758.     /* We missed a packet.  Send an ack to try to resync things. */
  2759.     tcp_send_ack(sk->sent_seq, sk->acked_seq, sk, th, saddr);
  2760.   }
  2761.  
  2762.   /* Now tell the user we may have some data. */
  2763.   if (!sk->dead) {
  2764.         if(sk->debug)
  2765.             printk("Data wakeup.\n");
  2766.     sk->data_ready(sk,0);
  2767.   } else {
  2768.     DPRINTF((DBG_TCP, "data received on dead socket.\n"));
  2769.   }
  2770.  
  2771.   if (sk->state == TCP_FIN_WAIT2 &&
  2772.       sk->acked_seq == sk->fin_seq && sk->rcv_ack_seq == sk->write_seq) {
  2773.     DPRINTF((DBG_TCP, "tcp_data: entering last_ack state sk = %X\n", sk));
  2774.  
  2775. /*    tcp_send_ack(sk->sent_seq, sk->acked_seq, sk, th, saddr); */
  2776.     sk->shutdown = SHUTDOWN_MASK;
  2777.     sk->state = TCP_LAST_ACK;
  2778.     if (!sk->dead) sk->state_change(sk);
  2779.   }
  2780.  
  2781.   return(0);
  2782. }
  2783.  
  2784.  
  2785. static void tcp_check_urg(struct sock * sk, struct tcphdr * th)
  2786. {
  2787.     unsigned long ptr = ntohs(th->urg_ptr);
  2788.  
  2789.     if (ptr)
  2790.         ptr--;
  2791.     ptr += th->seq;
  2792.  
  2793.     /* ignore urgent data that we've already seen and read */
  2794.     if (after(sk->copied_seq+1, ptr))
  2795.         return;
  2796.  
  2797.     /* do we already have a newer (or duplicate) urgent pointer? */
  2798.     if (sk->urg_data && !after(ptr, sk->urg_seq))
  2799.         return;
  2800.  
  2801.     /* tell the world about our new urgent pointer */
  2802.     if (sk->proc != 0) {
  2803.         if (sk->proc > 0) {
  2804.             kill_proc(sk->proc, SIGURG, 1);
  2805.         } else {
  2806.             kill_pg(-sk->proc, SIGURG, 1);
  2807.         }
  2808.     }
  2809.     sk->urg_data = URG_NOTYET;
  2810.     sk->urg_seq = ptr;
  2811. }
  2812.  
  2813. static inline int tcp_urg(struct sock *sk, struct tcphdr *th,
  2814.     unsigned long saddr, unsigned long len)
  2815. {
  2816.     unsigned long ptr;
  2817.  
  2818.     /* check if we get a new urgent pointer */
  2819.     if (th->urg)
  2820.         tcp_check_urg(sk,th);
  2821.  
  2822.     /* do we wait for any urgent data? */
  2823.     if (sk->urg_data != URG_NOTYET)
  2824.         return 0;
  2825.  
  2826.     /* is the urgent pointer pointing into this packet? */
  2827.     ptr = sk->urg_seq - th->seq + th->doff*4;
  2828.     if (ptr >= len)
  2829.         return 0;
  2830.  
  2831.     /* ok, got the correct packet, update info */
  2832.     sk->urg_data = URG_VALID | *(ptr + (unsigned char *) th);
  2833.     if (!sk->dead)
  2834.         wake_up_interruptible(sk->sleep);
  2835.     return 0;
  2836. }
  2837.  
  2838.  
  2839. /* This deals with incoming fins. 'Linus at 9 O'clock' 8-) */
  2840. static int
  2841. tcp_fin(struct sock *sk, struct tcphdr *th, 
  2842.      unsigned long saddr, struct device *dev)
  2843. {
  2844.   DPRINTF((DBG_TCP, "tcp_fin(sk=%X, th=%X, saddr=%X, dev=%X)\n",
  2845.                         sk, th, saddr, dev));
  2846.   
  2847.   if (!sk->dead) {
  2848.     sk->state_change(sk);
  2849.   }
  2850.  
  2851.   switch(sk->state) {
  2852.     case TCP_SYN_RECV:
  2853.     case TCP_SYN_SENT:
  2854.     case TCP_ESTABLISHED:
  2855.         /* Contains the one that needs to be acked */
  2856.         sk->fin_seq = th->seq+1;
  2857.         sk->state = TCP_CLOSE_WAIT;
  2858.         if (th->rst) sk->shutdown = SHUTDOWN_MASK;
  2859.         break;
  2860.  
  2861.     case TCP_CLOSE_WAIT:
  2862.     case TCP_FIN_WAIT2:
  2863.         break; /* we got a retransmit of the fin. */
  2864.  
  2865.     case TCP_FIN_WAIT1:
  2866.         /* Contains the one that needs to be acked */
  2867.         sk->fin_seq = th->seq+1;
  2868.         sk->state = TCP_FIN_WAIT2;
  2869.         break;
  2870.  
  2871.     default:
  2872.     case TCP_TIME_WAIT:
  2873.         sk->state = TCP_LAST_ACK;
  2874.  
  2875.         /* Start the timers. */
  2876.         reset_timer(sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
  2877.         return(0);
  2878.   }
  2879.   sk->ack_backlog++;
  2880.  
  2881.   return(0);
  2882. }
  2883.  
  2884.  
  2885. /* This will accept the next outstanding connection. */
  2886. static struct sock *
  2887. tcp_accept(struct sock *sk, int flags)
  2888. {
  2889.   struct sock *newsk;
  2890.   struct sk_buff *skb;
  2891.   
  2892.   DPRINTF((DBG_TCP, "tcp_accept(sk=%X, flags=%X, addr=%s)\n",
  2893.                 sk, flags, in_ntoa(sk->saddr)));
  2894.  
  2895.   /*
  2896.    * We need to make sure that this socket is listening,
  2897.    * and that it has something pending.
  2898.    */
  2899.   if (sk->state != TCP_LISTEN) {
  2900.     sk->err = EINVAL;
  2901.     return(NULL); 
  2902.   }
  2903.  
  2904.   /* avoid the race. */
  2905.   cli();
  2906.   sk->inuse = 1;
  2907.   while((skb = get_firstr(sk)) == NULL) {
  2908.     if (flags & O_NONBLOCK) {
  2909.         sti();
  2910.         release_sock(sk);
  2911.         sk->err = EAGAIN;
  2912.         return(NULL);
  2913.     }
  2914.  
  2915.     release_sock(sk);
  2916.     interruptible_sleep_on(sk->sleep);
  2917.     if (current->signal & ~current->blocked) {
  2918.         sti();
  2919.         sk->err = ERESTARTSYS;
  2920.         return(NULL);
  2921.     }
  2922.     sk->inuse = 1;
  2923.   }
  2924.   sti();
  2925.  
  2926.   /* Now all we need to do is return skb->sk. */
  2927.   newsk = skb->sk;
  2928.  
  2929.   kfree_skb(skb, FREE_READ);
  2930.   sk->ack_backlog--;
  2931.   release_sock(sk);
  2932.   return(newsk);
  2933. }
  2934.  
  2935.  
  2936. /* This will initiate an outgoing connection. */
  2937. static int
  2938. tcp_connect(struct sock *sk, struct sockaddr_in *usin, int addr_len)
  2939. {
  2940.   struct sk_buff *buff;
  2941.   struct sockaddr_in sin;
  2942.   struct device *dev=NULL;
  2943.   unsigned char *ptr;
  2944.   int tmp;
  2945.   struct tcphdr *t1;
  2946.   int err;
  2947.  
  2948.   if (sk->state != TCP_CLOSE) return(-EISCONN);
  2949.   if (addr_len < 8) return(-EINVAL);
  2950.  
  2951.   err=verify_area(VERIFY_READ, usin, addr_len);
  2952.   if(err)
  2953.       return err;
  2954.       
  2955.   memcpy_fromfs(&sin,usin, min(sizeof(sin), addr_len));
  2956.  
  2957.   if (sin.sin_family && sin.sin_family != AF_INET) return(-EAFNOSUPPORT);
  2958.  
  2959.   DPRINTF((DBG_TCP, "TCP connect daddr=%s\n", in_ntoa(sin.sin_addr.s_addr)));
  2960.   
  2961.   /* Don't want a TCP connection going to a broadcast address */
  2962.   if (chk_addr(sin.sin_addr.s_addr) == IS_BROADCAST) { 
  2963.     DPRINTF((DBG_TCP, "TCP connection to broadcast address not allowed\n"));
  2964.     return(-ENETUNREACH);
  2965.   }
  2966.   
  2967.   /* Connect back to the same socket: Blows up so disallow it */
  2968.   if(sk->saddr == sin.sin_addr.s_addr && sk->num==ntohs(sin.sin_port))
  2969.     return -EBUSY;
  2970.  
  2971.   sk->inuse = 1;
  2972.   sk->daddr = sin.sin_addr.s_addr;
  2973.   sk->write_seq = jiffies * SEQ_TICK - seq_offset;
  2974.   sk->window_seq = sk->write_seq;
  2975.   sk->rcv_ack_seq = sk->write_seq -1;
  2976.   sk->err = 0;
  2977.   sk->dummy_th.dest = sin.sin_port;
  2978.   release_sock(sk);
  2979.  
  2980.   buff = sk->prot->wmalloc(sk,MAX_SYN_SIZE,0, GFP_KERNEL);
  2981.   if (buff == NULL) {
  2982.     return(-ENOMEM);
  2983.   }
  2984.   sk->inuse = 1;
  2985.   buff->mem_addr = buff;
  2986.   buff->mem_len = MAX_SYN_SIZE;
  2987.   buff->len = 24;
  2988.   buff->sk = sk;
  2989.   buff->free = 1;
  2990.   t1 = (struct tcphdr *) buff->data;
  2991.  
  2992.   /* Put in the IP header and routing stuff. */
  2993.   /* We need to build the routing stuff fromt the things saved in skb. */
  2994.   tmp = sk->prot->build_header(buff, sk->saddr, sk->daddr, &dev,
  2995.                     IPPROTO_TCP, NULL, MAX_SYN_SIZE,sk->ip_tos,sk->ip_ttl);
  2996.   if (tmp < 0) {
  2997.     sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
  2998.     release_sock(sk);
  2999.     return(-ENETUNREACH);
  3000.   }
  3001.   buff->len += tmp;
  3002.   t1 = (struct tcphdr *)((char *)t1 +tmp);
  3003.  
  3004.   memcpy(t1,(void *)&(sk->dummy_th), sizeof(*t1));
  3005.   t1->seq = ntohl(sk->write_seq++);
  3006.   sk->sent_seq = sk->write_seq;
  3007.   buff->h.seq = sk->write_seq;
  3008.   t1->ack = 0;
  3009.   t1->window = 2;
  3010.   t1->res1=0;
  3011.   t1->res2=0;
  3012.   t1->rst = 0;
  3013.   t1->urg = 0;
  3014.   t1->psh = 0;
  3015.   t1->syn = 1;
  3016.   t1->urg_ptr = 0;
  3017.   t1->doff = 6;
  3018.  
  3019. /* use 512 or whatever user asked for */
  3020.   if (sk->user_mss)
  3021.     sk->mtu = sk->user_mss;
  3022.   else {
  3023. #ifdef SUBNETSARELOCAL
  3024.     if ((sk->saddr ^ sk->daddr) & default_mask(sk->saddr))
  3025. #else
  3026.     if ((sk->saddr ^ sk->daddr) & dev->pa_mask)
  3027. #endif
  3028.       sk->mtu = 576 - HEADER_SIZE;
  3029.     else
  3030.       sk->mtu = MAX_WINDOW;
  3031.   }
  3032. /* but not bigger than device MTU */
  3033.   sk->mtu = min(sk->mtu, dev->mtu - HEADER_SIZE);
  3034.  
  3035.   /* Put in the TCP options to say MTU. */
  3036.   ptr = (unsigned char *)(t1+1);
  3037.   ptr[0] = 2;
  3038.   ptr[1] = 4;
  3039.   ptr[2] = (sk->mtu) >> 8;
  3040.   ptr[3] = (sk->mtu) & 0xff;
  3041.   tcp_send_check(t1, sk->saddr, sk->daddr,
  3042.           sizeof(struct tcphdr) + 4, sk);
  3043.  
  3044.   /* This must go first otherwise a really quick response will get reset. */
  3045.   sk->state = TCP_SYN_SENT;
  3046.   sk->rtt = TCP_CONNECT_TIME;
  3047.   reset_timer(sk, TIME_WRITE, TCP_CONNECT_TIME);    /* Timer for repeating the SYN until an answer */
  3048.   sk->retransmits = TCP_RETR2 - TCP_SYN_RETRIES;
  3049.  
  3050.   sk->prot->queue_xmit(sk, dev, buff, 0);  
  3051.   
  3052.   release_sock(sk);
  3053.   return(0);
  3054. }
  3055.  
  3056.  
  3057. /* This functions checks to see if the tcp header is actually acceptable. */
  3058. static int
  3059. tcp_sequence(struct sock *sk, struct tcphdr *th, short len,
  3060.          struct options *opt, unsigned long saddr, struct device *dev)
  3061. {
  3062.     unsigned long next_seq;
  3063.  
  3064.     next_seq = len - 4*th->doff;
  3065.     if (th->fin)
  3066.         next_seq++;
  3067.     /* if we have a zero window, we can't have any data in the packet.. */
  3068.     if (next_seq && !sk->window)
  3069.         goto ignore_it;
  3070.     next_seq += th->seq;
  3071.  
  3072.     /*
  3073.      * This isn't quite right.  sk->acked_seq could be more recent
  3074.      * than sk->window.  This is however close enough.  We will accept
  3075.      * slightly more packets than we should, but it should not cause
  3076.      * problems unless someone is trying to forge packets.
  3077.      */
  3078.  
  3079.     /* have we already seen all of this packet? */
  3080.     if (!after(next_seq+1, sk->acked_seq))
  3081.         goto ignore_it;
  3082.     /* or does it start beyond the window? */
  3083.     if (!before(th->seq, sk->acked_seq + sk->window + 1))
  3084.         goto ignore_it;
  3085.  
  3086.     /* ok, at least part of this packet would seem interesting.. */
  3087.     return 1;
  3088.  
  3089. ignore_it:
  3090.     DPRINTF((DBG_TCP, "tcp_sequence: rejecting packet.\n"));
  3091.  
  3092.     /*
  3093.      *    Send a reset if we get something not ours and we are
  3094.      *    unsynchronized. Note: We don't do anything to our end. We
  3095.      *    are just killing the bogus remote connection then we will
  3096.      *    connect again and it will work (with luck).
  3097.      */
  3098.        
  3099.     if (sk->state==TCP_SYN_SENT || sk->state==TCP_SYN_RECV) {
  3100.         tcp_reset(sk->saddr,sk->daddr,th,sk->prot,NULL,dev, sk->ip_tos,sk->ip_ttl);
  3101.         return 1;
  3102.     }
  3103.  
  3104.     if (th->rst)
  3105.         return 0;
  3106.  
  3107.     /* Try to resync things. */
  3108.     tcp_send_ack(sk->sent_seq, sk->acked_seq, sk, th, saddr);
  3109.     return 0;
  3110. }
  3111.  
  3112.  
  3113. int
  3114. tcp_rcv(struct sk_buff *skb, struct device *dev, struct options *opt,
  3115.     unsigned long daddr, unsigned short len,
  3116.     unsigned long saddr, int redo, struct inet_protocol * protocol)
  3117. {
  3118.   struct tcphdr *th;
  3119.   struct sock *sk;
  3120.  
  3121.   if (!skb) {
  3122.     DPRINTF((DBG_TCP, "tcp.c: tcp_rcv skb = NULL\n"));
  3123.     return(0);
  3124.   }
  3125. #if 0    /* FIXME: it's ok for protocol to be NULL */
  3126.   if (!protocol) {
  3127.     DPRINTF((DBG_TCP, "tcp.c: tcp_rcv protocol = NULL\n"));
  3128.     return(0);
  3129.   }
  3130.  
  3131.   if (!opt) {    /* FIXME: it's ok for opt to be NULL */
  3132.     DPRINTF((DBG_TCP, "tcp.c: tcp_rcv opt = NULL\n"));
  3133.   }
  3134. #endif
  3135.   if (!dev) {
  3136.     DPRINTF((DBG_TCP, "tcp.c: tcp_rcv dev = NULL\n"));
  3137.     return(0);
  3138.   }
  3139.   th = skb->h.th;
  3140.  
  3141.   /* Find the socket. */
  3142.   sk = get_sock(&tcp_prot, th->dest, saddr, th->source, daddr);
  3143.   DPRINTF((DBG_TCP, "<<\n"));
  3144.   DPRINTF((DBG_TCP, "len = %d, redo = %d, skb=%X\n", len, redo, skb));
  3145.   
  3146.   /* If this socket has got a reset its to all intents and purposes 
  3147.      really dead */
  3148.   if (sk!=NULL && sk->zapped)
  3149.     sk=NULL;
  3150.  
  3151.   if (sk) {
  3152.      DPRINTF((DBG_TCP, "sk = %X:\n", sk));
  3153.   }
  3154.  
  3155.   if (!redo) {
  3156.     if (tcp_check(th, len, saddr, daddr )) {
  3157.         skb->sk = NULL;
  3158.         DPRINTF((DBG_TCP, "packet dropped with bad checksum.\n"));
  3159. if (inet_debug == DBG_SLIP) printk("\rtcp_rcv: bad checksum\n");
  3160.         kfree_skb(skb,FREE_READ);
  3161.         /*
  3162.          * We don't release the socket because it was
  3163.          * never marked in use.
  3164.          */
  3165.         return(0);
  3166.     }
  3167.  
  3168.     th->seq = ntohl(th->seq);
  3169.  
  3170.     /* See if we know about the socket. */
  3171.     if (sk == NULL) {
  3172.         if (!th->rst)
  3173.             tcp_reset(daddr, saddr, th, &tcp_prot, opt,dev,skb->ip_hdr->tos,255);
  3174.         skb->sk = NULL;
  3175.         kfree_skb(skb, FREE_READ);
  3176.         return(0);
  3177.     }
  3178.  
  3179.     skb->len = len;
  3180.     skb->sk = sk;
  3181.     skb->acked = 0;
  3182.     skb->used = 0;
  3183.     skb->free = 0;
  3184.     skb->saddr = daddr;
  3185.     skb->daddr = saddr;
  3186.  
  3187.     /* We may need to add it to the backlog here. */
  3188.     cli();
  3189.     if (sk->inuse) {
  3190.         if (sk->back_log == NULL) {
  3191.             sk->back_log = skb;
  3192.             skb->next = skb;
  3193.             skb->prev = skb;
  3194.         } else {
  3195.             skb->next = sk->back_log;
  3196.             skb->prev = sk->back_log->prev;
  3197.             skb->prev->next = skb;
  3198.             skb->next->prev = skb;
  3199.         }
  3200.         sti();
  3201.         return(0);
  3202.     }
  3203.     sk->inuse = 1;
  3204.     sti();
  3205.   } else {
  3206.     if (!sk) {
  3207.         DPRINTF((DBG_TCP, "tcp.c: tcp_rcv bug sk=NULL redo = 1\n"));
  3208.         return(0);
  3209.     }
  3210.   }
  3211.  
  3212.   if (!sk->prot) {
  3213.     DPRINTF((DBG_TCP, "tcp.c: tcp_rcv sk->prot = NULL \n"));
  3214.     return(0);
  3215.   }
  3216.  
  3217.   /* Charge the memory to the socket. */
  3218.   if (sk->rmem_alloc + skb->mem_len >= sk->rcvbuf) {
  3219.     skb->sk = NULL;
  3220.     DPRINTF((DBG_TCP, "dropping packet due to lack of buffer space.\n"));
  3221.     kfree_skb(skb, FREE_READ);
  3222.     release_sock(sk);
  3223.     return(0);
  3224.   }
  3225.   sk->rmem_alloc += skb->mem_len;
  3226.  
  3227.   DPRINTF((DBG_TCP, "About to do switch.\n"));
  3228.  
  3229.   /* Now deal with it. */
  3230.   switch(sk->state) {
  3231.     /*
  3232.      * This should close the system down if it's waiting
  3233.      * for an ack that is never going to be sent.
  3234.      */
  3235.     case TCP_LAST_ACK:
  3236.         if (th->rst) {
  3237.             sk->zapped=1;
  3238.             sk->err = ECONNRESET;
  3239.              sk->state = TCP_CLOSE;
  3240.             sk->shutdown = SHUTDOWN_MASK;
  3241.             if (!sk->dead) {
  3242.                 sk->state_change(sk);
  3243.             }
  3244.             kfree_skb(skb, FREE_READ);
  3245.             release_sock(sk);
  3246.             return(0);
  3247.         }
  3248.  
  3249.     case TCP_ESTABLISHED:
  3250.     case TCP_CLOSE_WAIT:
  3251.     case TCP_FIN_WAIT1:
  3252.     case TCP_FIN_WAIT2:
  3253.     case TCP_TIME_WAIT:
  3254.         if (!tcp_sequence(sk, th, len, opt, saddr,dev)) {
  3255. if (inet_debug == DBG_SLIP) printk("\rtcp_rcv: not in seq\n");
  3256. #ifdef undef
  3257. /* nice idea, but tcp_sequence already does this.  Maybe it shouldn't?? */
  3258.             if(!th->rst)
  3259.                 tcp_send_ack(sk->sent_seq, sk->acked_seq, 
  3260.                      sk, th, saddr);
  3261. #endif
  3262.             kfree_skb(skb, FREE_READ);
  3263.             release_sock(sk);
  3264.             return(0);
  3265.         }
  3266.  
  3267.         if (th->rst) {
  3268.             sk->zapped=1;
  3269.             /* This means the thing should really be closed. */
  3270.             sk->err = ECONNRESET;
  3271.  
  3272.             if (sk->state == TCP_CLOSE_WAIT) {
  3273.                 sk->err = EPIPE;
  3274.             }
  3275.  
  3276.             /*
  3277.              * A reset with a fin just means that
  3278.              * the data was not all read.
  3279.              */
  3280.             sk->state = TCP_CLOSE;
  3281.             sk->shutdown = SHUTDOWN_MASK;
  3282.             if (!sk->dead) {
  3283.                 sk->state_change(sk);
  3284.             }
  3285.             kfree_skb(skb, FREE_READ);
  3286.             release_sock(sk);
  3287.             return(0);
  3288.         }
  3289.         if (
  3290. #if 0
  3291.         if ((opt && (opt->security != 0 ||
  3292.                 opt->compartment != 0)) || 
  3293. #endif
  3294.                  th->syn) {
  3295.             sk->err = ECONNRESET;
  3296.             sk->state = TCP_CLOSE;
  3297.             sk->shutdown = SHUTDOWN_MASK;
  3298.             tcp_reset(daddr, saddr,  th, sk->prot, opt,dev, sk->ip_tos,sk->ip_ttl);
  3299.             if (!sk->dead) {
  3300.                 sk->state_change(sk);
  3301.             }
  3302.             kfree_skb(skb, FREE_READ);
  3303.             release_sock(sk);
  3304.             return(0);
  3305.         }
  3306.  
  3307.         if (th->ack && !tcp_ack(sk, th, saddr, len)) {
  3308.             kfree_skb(skb, FREE_READ);
  3309.             release_sock(sk);
  3310.             return(0);
  3311.         }
  3312.  
  3313.         if (tcp_urg(sk, th, saddr, len)) {
  3314.             kfree_skb(skb, FREE_READ);
  3315.             release_sock(sk);
  3316.             return(0);
  3317.         }
  3318.  
  3319.         if (tcp_data(skb, sk, saddr, len)) {
  3320.             kfree_skb(skb, FREE_READ);
  3321.             release_sock(sk);
  3322.             return(0);
  3323.         }
  3324.  
  3325.         /* Moved: you must do data then fin bit */
  3326.         if (th->fin && tcp_fin(sk, th, saddr, dev)) {
  3327.             kfree_skb(skb, FREE_READ);
  3328.             release_sock(sk);
  3329.             return(0);
  3330.         }
  3331.  
  3332.         release_sock(sk);
  3333.         return(0);
  3334.  
  3335.     case TCP_CLOSE:
  3336.         if (sk->dead || sk->daddr) {
  3337.             DPRINTF((DBG_TCP, "packet received for closed,dead socket\n"));
  3338.             kfree_skb(skb, FREE_READ);
  3339.             release_sock(sk);
  3340.             return(0);
  3341.         }
  3342.  
  3343.         if (!th->rst) {
  3344.             if (!th->ack)
  3345.                 th->ack_seq = 0;
  3346.             tcp_reset(daddr, saddr, th, sk->prot, opt,dev,sk->ip_tos,sk->ip_ttl);
  3347.         }
  3348.         kfree_skb(skb, FREE_READ);
  3349.         release_sock(sk);
  3350.         return(0);
  3351.  
  3352.     case TCP_LISTEN:
  3353.         if (th->rst) {
  3354.             kfree_skb(skb, FREE_READ);
  3355.             release_sock(sk);
  3356.             return(0);
  3357.         }
  3358.         if (th->ack) {
  3359.             tcp_reset(daddr, saddr, th, sk->prot, opt,dev,sk->ip_tos,sk->ip_ttl);
  3360.             kfree_skb(skb, FREE_READ);
  3361.             release_sock(sk);
  3362.             return(0);
  3363.         }
  3364.  
  3365.         if (th->syn) {
  3366. #if 0
  3367.             if (opt->security != 0 || opt->compartment != 0) {
  3368.                 tcp_reset(daddr, saddr, th, prot, opt,dev);
  3369.                 release_sock(sk);
  3370.                 return(0);
  3371.             }
  3372. #endif
  3373.  
  3374.             /*
  3375.              * Now we just put the whole thing including
  3376.              * the header and saddr, and protocol pointer
  3377.              * into the buffer.  We can't respond until the
  3378.              * user tells us to accept the connection.
  3379.              */
  3380.             tcp_conn_request(sk, skb, daddr, saddr, opt, dev);
  3381.             release_sock(sk);
  3382.             return(0);
  3383.         }
  3384.  
  3385.         kfree_skb(skb, FREE_READ);
  3386.         release_sock(sk);
  3387.         return(0);
  3388.  
  3389.     default:
  3390.         if (!tcp_sequence(sk, th, len, opt, saddr,dev)) {
  3391.             kfree_skb(skb, FREE_READ);
  3392.             release_sock(sk);
  3393.             return(0);
  3394.         }
  3395.  
  3396.     case TCP_SYN_SENT:
  3397.         if (th->rst) {
  3398.             sk->err = ECONNREFUSED;
  3399.             sk->state = TCP_CLOSE;
  3400.             sk->shutdown = SHUTDOWN_MASK;
  3401.             sk->zapped = 1;
  3402.             if (!sk->dead) {
  3403.                 sk->state_change(sk);
  3404.             }
  3405.             kfree_skb(skb, FREE_READ);
  3406.             release_sock(sk);
  3407.             return(0);
  3408.         }
  3409. #if 0
  3410.         if (opt->security != 0 || opt->compartment != 0) {
  3411.             sk->err = ECONNRESET;
  3412.             sk->state = TCP_CLOSE;
  3413.             sk->shutdown = SHUTDOWN_MASK;
  3414.             tcp_reset(daddr, saddr,  th, sk->prot, opt, dev);
  3415.             if (!sk->dead) {
  3416.                 wake_up_interruptible(sk->sleep);
  3417.             }
  3418.             kfree_skb(skb, FREE_READ);
  3419.             release_sock(sk);
  3420.             return(0);
  3421.         }
  3422. #endif
  3423.         if (!th->ack) {
  3424.             if (th->syn) {
  3425.                 sk->state = TCP_SYN_RECV;
  3426.             }
  3427.  
  3428.             kfree_skb(skb, FREE_READ);
  3429.             release_sock(sk);
  3430.             return(0);
  3431.         }
  3432.  
  3433.         switch(sk->state) {
  3434.             case TCP_SYN_SENT:
  3435.                 if (!tcp_ack(sk, th, saddr, len)) {
  3436.                     tcp_reset(daddr, saddr, th,
  3437.                             sk->prot, opt,dev,sk->ip_tos,sk->ip_ttl);
  3438.                     kfree_skb(skb, FREE_READ);
  3439.                     release_sock(sk);
  3440.                     return(0);
  3441.                 }
  3442.  
  3443.                 /*
  3444.                  * If the syn bit is also set, switch to
  3445.                  * tcp_syn_recv, and then to established.
  3446.                  */
  3447.                 if (!th->syn) {
  3448.                     kfree_skb(skb, FREE_READ);
  3449.                     release_sock(sk);
  3450.                     return(0);
  3451.                 }
  3452.  
  3453.                 /* Ack the syn and fall through. */
  3454.                 sk->acked_seq = th->seq+1;
  3455.                 sk->fin_seq = th->seq;
  3456.                 tcp_send_ack(sk->sent_seq, th->seq+1,
  3457.                             sk, th, sk->daddr);
  3458.     
  3459.             case TCP_SYN_RECV:
  3460.                 if (!tcp_ack(sk, th, saddr, len)) {
  3461.                     tcp_reset(daddr, saddr, th,
  3462.                             sk->prot, opt, dev,sk->ip_tos,sk->ip_ttl);
  3463.                     kfree_skb(skb, FREE_READ);
  3464.                     release_sock(sk);
  3465.                     return(0);
  3466.                 }
  3467.                 sk->state = TCP_ESTABLISHED;
  3468.  
  3469.                 /*
  3470.                  * Now we need to finish filling out
  3471.                  * some of the tcp header.
  3472.                  */
  3473.                 /* We need to check for mtu info. */
  3474.                 tcp_options(sk, th);
  3475.                 sk->dummy_th.dest = th->source;
  3476.                 sk->copied_seq = sk->acked_seq-1;
  3477.                 if (!sk->dead) {
  3478.                     sk->state_change(sk);
  3479.                 }
  3480.  
  3481.                 /*
  3482.                  * We've already processed his first
  3483.                  * ack.  In just about all cases that
  3484.                  * will have set max_window.  This is
  3485.                  * to protect us against the possibility
  3486.                  * that the initial window he sent was 0.
  3487.                  * This must occur after tcp_options, which
  3488.                  * sets sk->mtu.
  3489.                  */
  3490.                 if (sk->max_window == 0) {
  3491.                   sk->max_window = 32;
  3492.                   sk->mss = min(sk->max_window, sk->mtu);
  3493.                 }
  3494.  
  3495.                 /*
  3496.                  * Now process the rest like we were
  3497.                  * already in the established state.
  3498.                  */
  3499.                 if (th->urg) {
  3500.                     if (tcp_urg(sk, th, saddr, len)) { 
  3501.                         kfree_skb(skb, FREE_READ);
  3502.                         release_sock(sk);
  3503.                         return(0);
  3504.                     }
  3505.             }
  3506.             if (tcp_data(skb, sk, saddr, len))
  3507.                         kfree_skb(skb, FREE_READ);
  3508.  
  3509.             if (th->fin) tcp_fin(sk, th, saddr, dev);
  3510.             release_sock(sk);
  3511.             return(0);
  3512.         }
  3513.  
  3514.         if (th->urg) {
  3515.             if (tcp_urg(sk, th, saddr, len)) {
  3516.                 kfree_skb(skb, FREE_READ);
  3517.                 release_sock(sk);
  3518.                 return(0);
  3519.             }
  3520.         }
  3521.  
  3522.         if (tcp_data(skb, sk, saddr, len)) {
  3523.             kfree_skb(skb, FREE_READ);
  3524.             release_sock(sk);
  3525.             return(0);
  3526.         }
  3527.  
  3528.         if (!th->fin) {
  3529.             release_sock(sk);
  3530.             return(0);
  3531.         }
  3532.         tcp_fin(sk, th, saddr, dev);
  3533.         release_sock(sk);
  3534.         return(0);
  3535.     }
  3536. }
  3537.  
  3538.  
  3539. /*
  3540.   * This routine sends a packet with an out of date sequence
  3541.   * number. It assumes the other end will try to ack it.
  3542.   */
  3543. static void
  3544. tcp_write_wakeup(struct sock *sk)
  3545. {
  3546.   struct sk_buff *buff;
  3547.   struct tcphdr *t1;
  3548.   struct device *dev=NULL;
  3549.   int tmp;
  3550.  
  3551.   if (sk->zapped)
  3552.     return;    /* Afer a valid reset we can send no more */
  3553.  
  3554.   if (sk -> state != TCP_ESTABLISHED && sk->state != TCP_CLOSE_WAIT &&
  3555.       sk -> state != TCP_FIN_WAIT1 && sk->state != TCP_FIN_WAIT2)
  3556.     return;
  3557.  
  3558.   buff = sk->prot->wmalloc(sk,MAX_ACK_SIZE,1, GFP_ATOMIC);
  3559.   if (buff == NULL) return;
  3560.  
  3561.   buff->mem_addr = buff;
  3562.   buff->mem_len = MAX_ACK_SIZE;
  3563.   buff->len = sizeof(struct tcphdr);
  3564.   buff->free = 1;
  3565.   buff->sk = sk;
  3566.   DPRINTF((DBG_TCP, "in tcp_write_wakeup\n"));
  3567.   t1 = (struct tcphdr *) buff->data;
  3568.  
  3569.   /* Put in the IP header and routing stuff. */
  3570.   tmp = sk->prot->build_header(buff, sk->saddr, sk->daddr, &dev,
  3571.                 IPPROTO_TCP, sk->opt, MAX_ACK_SIZE,sk->ip_tos,sk->ip_ttl);
  3572.   if (tmp < 0) {
  3573.     sk->prot->wfree(sk, buff->mem_addr, buff->mem_len);
  3574.     return;
  3575.   }
  3576.  
  3577.   buff->len += tmp;
  3578.   t1 = (struct tcphdr *)((char *)t1 +tmp);
  3579.  
  3580.   memcpy(t1,(void *) &sk->dummy_th, sizeof(*t1));
  3581.  
  3582.   /*
  3583.    * Use a previous sequence.
  3584.    * This should cause the other end to send an ack.
  3585.    */
  3586.   t1->seq = htonl(sk->sent_seq-1);
  3587.   t1->ack = 1; 
  3588.   t1->res1= 0;
  3589.   t1->res2= 0;
  3590.   t1->rst = 0;
  3591.   t1->urg = 0;
  3592.   t1->psh = 0;
  3593.   t1->fin = 0;
  3594.   t1->syn = 0;
  3595.   t1->ack_seq = ntohl(sk->acked_seq);
  3596.   t1->window = ntohs(tcp_select_window(sk)/*sk->prot->rspace(sk)*/);
  3597.   t1->doff = sizeof(*t1)/4;
  3598.   tcp_send_check(t1, sk->saddr, sk->daddr, sizeof(*t1), sk);
  3599.  
  3600.   /* Send it and free it.
  3601.    * This will prevent the timer from automatically being restarted.
  3602.   */
  3603.   sk->prot->queue_xmit(sk, dev, buff, 1);
  3604. }
  3605.  
  3606. void
  3607. tcp_send_probe0(struct sock *sk)
  3608. {
  3609.     if (sk->zapped)
  3610.         return;        /* Afer a valid reset we can send no more */
  3611.  
  3612.     tcp_write_wakeup(sk);
  3613.  
  3614.     sk->backoff++;
  3615.     sk->rto = min(sk->rto << 1, 120*HZ);
  3616.     reset_timer (sk, TIME_PROBE0, sk->rto);
  3617.     sk->retransmits++;
  3618.     sk->prot->retransmits ++;
  3619. }
  3620.  
  3621.  
  3622. /*
  3623.  *    Socket option code for TCP. 
  3624.  */  
  3625. int tcp_setsockopt(struct sock *sk, int level, int optname, char *optval, int optlen)
  3626. {
  3627.     int val,err;
  3628.  
  3629.     if(level!=SOL_TCP)
  3630.         return ip_setsockopt(sk,level,optname,optval,optlen);
  3631.  
  3632.       if (optval == NULL) 
  3633.           return(-EINVAL);
  3634.  
  3635.       err=verify_area(VERIFY_READ, optval, sizeof(int));
  3636.       if(err)
  3637.           return err;
  3638.       
  3639.       val = get_fs_long((unsigned long *)optval);
  3640.  
  3641.     switch(optname)
  3642.     {
  3643.         case TCP_MAXSEG:
  3644. /*            if(val<200||val>2048 || val>sk->mtu) */
  3645. /*
  3646.  * values greater than interface MTU won't take effect.  however at
  3647.  * the point when this call is done we typically don't yet know
  3648.  * which interface is going to be used
  3649.  */
  3650.               if(val<1||val>MAX_WINDOW)
  3651.                 return -EINVAL;
  3652.             sk->user_mss=val;
  3653.             return 0;
  3654.         case TCP_NODELAY:
  3655.             sk->nonagle=(val==0)?0:1;
  3656.             return 0;
  3657.         default:
  3658.             return(-ENOPROTOOPT);
  3659.     }
  3660. }
  3661.  
  3662. int tcp_getsockopt(struct sock *sk, int level, int optname, char *optval, int *optlen)
  3663. {
  3664.     int val,err;
  3665.  
  3666.     if(level!=SOL_TCP)
  3667.         return ip_getsockopt(sk,level,optname,optval,optlen);
  3668.             
  3669.     switch(optname)
  3670.     {
  3671.         case TCP_MAXSEG:
  3672.             val=sk->user_mss;
  3673.             break;
  3674.         case TCP_NODELAY:
  3675.             val=sk->nonagle;    /* Until Johannes stuff is in */
  3676.             break;
  3677.         default:
  3678.             return(-ENOPROTOOPT);
  3679.     }
  3680.     err=verify_area(VERIFY_WRITE, optlen, sizeof(int));
  3681.     if(err)
  3682.           return err;
  3683.       put_fs_long(sizeof(int),(unsigned long *) optlen);
  3684.  
  3685.       err=verify_area(VERIFY_WRITE, optval, sizeof(int));
  3686.       if(err)
  3687.           return err;
  3688.       put_fs_long(val,(unsigned long *)optval);
  3689.  
  3690.       return(0);
  3691. }    
  3692.  
  3693.  
  3694. struct proto tcp_prot = {
  3695.   sock_wmalloc,
  3696.   sock_rmalloc,
  3697.   sock_wfree,
  3698.   sock_rfree,
  3699.   sock_rspace,
  3700.   sock_wspace,
  3701.   tcp_close,
  3702.   tcp_read,
  3703.   tcp_write,
  3704.   tcp_sendto,
  3705.   tcp_recvfrom,
  3706.   ip_build_header,
  3707.   tcp_connect,
  3708.   tcp_accept,
  3709.   ip_queue_xmit,
  3710.   tcp_retransmit,
  3711.   tcp_write_wakeup,
  3712.   tcp_read_wakeup,
  3713.   tcp_rcv,
  3714.   tcp_select,
  3715.   tcp_ioctl,
  3716.   NULL,
  3717.   tcp_shutdown,
  3718.   tcp_setsockopt,
  3719.   tcp_getsockopt,
  3720.   128,
  3721.   0,
  3722.   {NULL,},
  3723.   "TCP"
  3724. };
  3725.