home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / TCPIN.C < prev    next >
C/C++ Source or Header  |  1991-12-18  |  25KB  |  889 lines

  1. /* Process incoming TCP segments. Page number references are to ARPA RFC-793,
  2.  * the TCP specification.
  3.  *
  4.  * Copyright 1991 Phil Karn, KA9Q
  5.  */
  6. #include "global.h"
  7. #include "timer.h"
  8. #include "mbuf.h"
  9. #include "netuser.h"
  10. #include "internet.h"
  11. #include "tcp.h"
  12. #include "icmp.h"
  13. #include "iface.h"
  14. #include "ip.h"
  15.  
  16. static void update __ARGS((struct tcb *tcb,struct tcp *seg,int16 length));
  17. static void proc_syn __ARGS((struct tcb *tcb,char tos,struct tcp *seg));
  18. static void add_reseq __ARGS((struct tcb *tcb,char tos,struct tcp *seg,
  19.     struct mbuf *bp,int16 length));
  20. static void get_reseq __ARGS((struct tcb *tcb,char *tos,struct tcp *seq,
  21.     struct mbuf **bp,int16 *length));
  22. static int trim __ARGS((struct tcb *tcb,struct tcp *seg,struct mbuf **bpp,
  23.     int16 *length));
  24. static int in_window __ARGS((struct tcb *tcb,int32 seq));
  25.  
  26. /* This function is called from IP with the IP header in machine byte order,
  27.  * along with a mbuf chain pointing to the TCP header.
  28.  */
  29. void
  30. tcp_input(iface,ip,bp,rxbroadcast)
  31. struct iface *iface;    /* Incoming interface (ignored) */
  32. struct mbuf *bp;    /* Data field, if any */
  33. struct ip *ip;        /* IP header */
  34. int rxbroadcast;    /* Incoming broadcast - discard if true */
  35. {
  36.     struct tcb *ntcb;
  37.     register struct tcb *tcb;    /* TCP Protocol control block */
  38.     struct tcp seg;            /* Local copy of segment header */
  39.     struct connection conn;        /* Local copy of addresses */
  40.     struct pseudo_header ph;    /* Pseudo-header for checksumming */
  41.     int hdrlen;            /* Length of TCP header */
  42.     int16 length;
  43.  
  44.     if(bp == NULLBUF)
  45.         return;
  46.  
  47.     tcpInSegs++;
  48.     if(rxbroadcast){
  49.         /* Any TCP packet arriving as a broadcast is
  50.          * to be completely IGNORED!!
  51.          */
  52.         free_p(bp);
  53.         return;
  54.     }
  55.     length = ip->length - IPLEN - ip->optlen;
  56.     ph.source = ip->source;
  57.     ph.dest = ip->dest;
  58.     ph.protocol = ip->protocol;
  59.     ph.length = length;
  60.     if(cksum(&ph,bp,length) != 0){
  61.         /* Checksum failed, ignore segment completely */
  62.         tcpInErrs++;
  63.         free_p(bp);
  64.         return;
  65.     }
  66.     /* Form local copy of TCP header in host byte order */
  67.     if((hdrlen = ntohtcp(&seg,&bp)) < 0){
  68.         /* TCP header is too small */
  69.         free_p(bp);
  70.         return;
  71.     }
  72.     length -= hdrlen;
  73.  
  74.     /* Fill in connection structure and find TCB */
  75.     conn.local.address = ip->dest;
  76.     conn.local.port = seg.dest;
  77.     conn.remote.address = ip->source;
  78.     conn.remote.port = seg.source;
  79.     
  80.     if((tcb = lookup_tcb(&conn)) == NULLTCB){
  81.         /* If this segment doesn't carry a SYN, reject it */
  82.         if(!seg.flags.syn){
  83.             free_p(bp);
  84.             reset(ip,&seg);
  85.             return;
  86.         }
  87.         /* See if there's a TCP_LISTEN on this socket with
  88.          * unspecified remote address and port
  89.          */
  90.         conn.remote.address = 0;
  91.         conn.remote.port = 0;
  92.         if((tcb = lookup_tcb(&conn)) == NULLTCB){
  93.             /* Nope, try unspecified local address too */
  94.             conn.local.address = 0;
  95.             if((tcb = lookup_tcb(&conn)) == NULLTCB){
  96.                 /* No LISTENs, so reject */
  97.                 free_p(bp);
  98.                 reset(ip,&seg);
  99.                 return;
  100.             }
  101.         }
  102.         /* We've found an server listen socket, so clone the TCB */
  103.         if(tcb->flags.clone){
  104.             ntcb = (struct tcb *)mallocw(sizeof (struct tcb));
  105.             ASSIGN(*ntcb,*tcb);
  106.             tcb = ntcb;
  107.             tcb->timer.arg = tcb;
  108.             /* Put on list */
  109.             tcb->next = Tcbs;
  110.             Tcbs = tcb;
  111.         }
  112.         /* Put all the socket info into the TCB */
  113.         tcb->conn.local.address = ip->dest;
  114.         tcb->conn.remote.address = ip->source;
  115.         tcb->conn.remote.port = seg.source;
  116.     }
  117.     tcb->flags.congest = ip->flags.congest;
  118.     /* Do unsynchronized-state processing (p. 65-68) */
  119.     switch(tcb->state){
  120.     case TCP_CLOSED:
  121.         free_p(bp);
  122.         reset(ip,&seg);
  123.         return;
  124.     case TCP_LISTEN:
  125.         if(seg.flags.rst){
  126.             free_p(bp);
  127.             return;
  128.         }
  129.         if(seg.flags.ack){
  130.             free_p(bp);
  131.             reset(ip,&seg);
  132.             return;
  133.         }
  134.         if(seg.flags.syn){
  135.             /* (Security check is bypassed) */
  136.             /* page 66 */
  137.             proc_syn(tcb,ip->tos,&seg);
  138.             send_syn(tcb);
  139.             setstate(tcb,TCP_SYN_RECEIVED);        
  140.             if(length != 0 || seg.flags.fin) {
  141.                 /* Continue processing if there's more */
  142.                 break;
  143.             }
  144.             tcp_output(tcb);
  145.         }
  146.         free_p(bp);    /* Unlikely to get here directly */
  147.         return;
  148.     case TCP_SYN_SENT:
  149.         if(seg.flags.ack){
  150.             if(!seq_within(seg.ack,tcb->iss+1,tcb->snd.nxt)){
  151.                 free_p(bp);
  152.                 reset(ip,&seg);
  153.                 return;
  154.             }
  155.         }
  156.         if(seg.flags.rst){    /* p 67 */
  157.             if(seg.flags.ack){
  158.                 /* The ack must be acceptable since we just checked it.
  159.                  * This is how the remote side refuses connect requests.
  160.                  */
  161.                 close_self(tcb,RESET);
  162.             }
  163.             free_p(bp);
  164.             return;
  165.         }
  166.         /* (Security check skipped here) */
  167.         /* Check incoming precedence; it must match if there's an ACK */
  168.         if(seg.flags.ack && PREC(ip->tos) != PREC(tcb->tos)){
  169.             free_p(bp);
  170.             reset(ip,&seg);
  171.             return;
  172.         }
  173.         if(seg.flags.syn){
  174.             proc_syn(tcb,ip->tos,&seg);
  175.             if(seg.flags.ack){
  176.                 /* Our SYN has been acked, otherwise the ACK
  177.                  * wouldn't have been valid.
  178.                  */
  179.                 update(tcb,&seg,length);
  180.                 setstate(tcb,TCP_ESTABLISHED);
  181.             } else {
  182.                 setstate(tcb,TCP_SYN_RECEIVED);
  183.             }
  184.             if(length != 0 || seg.flags.fin) {
  185.                 break;        /* Continue processing if there's more */
  186.             }
  187.             tcp_output(tcb);
  188.         } else {
  189.             free_p(bp);    /* Ignore if neither SYN or RST is set */
  190.         }
  191.         return;
  192.     }
  193.     /* We reach this point directly in any synchronized state. Note that
  194.      * if we fell through from LISTEN or SYN_SENT processing because of a
  195.      * data-bearing SYN, window trimming and sequence testing "cannot fail".
  196.      */
  197.  
  198.     /* Trim segment to fit receive window. */
  199.     if(trim(tcb,&seg,&bp,&length) == -1){
  200.         /* Segment is unacceptable */
  201.         if(!seg.flags.rst){    /* NEVER answer RSTs */
  202.             /* In SYN_RECEIVED state, answer a retransmitted SYN 
  203.              * with a retransmitted SYN/ACK.
  204.              */
  205.             if(tcb->state == TCP_SYN_RECEIVED)
  206.                 tcb->snd.ptr = tcb->snd.una;
  207.             tcb->flags.force = 1;
  208.             tcp_output(tcb);
  209.         }
  210.         return;
  211.     }
  212.     /* If segment isn't the next one expected, and there's data
  213.      * or flags associated with it, put it on the resequencing
  214.      * queue, ACK it and return.
  215.      *
  216.      * Processing the ACK in an out-of-sequence segment without
  217.      * flags or data should be safe, however.
  218.      */
  219.     if(seg.seq != tcb->rcv.nxt
  220.      && (length != 0 || seg.flags.syn || seg.flags.fin)){
  221.         add_reseq(tcb,ip->tos,&seg,bp,length);
  222.         tcb->flags.force = 1;
  223.         tcp_output(tcb);
  224.         return;
  225.     }
  226.     /* This loop first processes the current segment, and then
  227.      * repeats if it can process the resequencing queue.
  228.      */
  229.     for(;;){
  230.         /* We reach this point with an acceptable segment; all data and flags
  231.          * are in the window, and the starting sequence number equals rcv.nxt
  232.          * (p. 70)
  233.          */    
  234.         if(seg.flags.rst){
  235.             if(tcb->state == TCP_SYN_RECEIVED
  236.              && !tcb->flags.clone && !tcb->flags.active){
  237.                 /* Go back to listen state only if this was
  238.                  * not a cloned or active server TCB
  239.                  */
  240.                 setstate(tcb,TCP_LISTEN);
  241.             } else {
  242.                 close_self(tcb,RESET);
  243.             }
  244.             free_p(bp);
  245.             return;
  246.         }
  247.         /* (Security check skipped here) p. 71 */
  248.         /* Check for precedence mismatch or erroneous extra SYN */
  249.         if(PREC(ip->tos) != PREC(tcb->tos) || seg.flags.syn){
  250.             free_p(bp);
  251.             reset(ip,&seg);
  252.             return;
  253.         }
  254.         /* Check ack field p. 72 */
  255.         if(!seg.flags.ack){
  256.             free_p(bp);    /* All segments after synchronization must have ACK */
  257.             return;
  258.         }
  259.         /* Process ACK */
  260.         switch(tcb->state){
  261.         case TCP_SYN_RECEIVED:
  262.             if(seq_within(seg.ack,tcb->snd.una+1,tcb->snd.nxt)){
  263.                 update(tcb,&seg,length);
  264.                 setstate(tcb,TCP_ESTABLISHED);
  265.             } else {
  266.                 free_p(bp);
  267.                 reset(ip,&seg);
  268.                 return;
  269.             }
  270.             break;
  271.         case TCP_ESTABLISHED:
  272.         case TCP_CLOSE_WAIT:
  273.             update(tcb,&seg,length);
  274.             break;
  275.         case TCP_FINWAIT1:    /* p. 73 */
  276.             update(tcb,&seg,length);
  277.             if(tcb->sndcnt == 0){
  278.                 /* Our FIN is acknowledged */
  279.                 setstate(tcb,TCP_FINWAIT2);
  280.             }
  281.             break;
  282.         case TCP_FINWAIT2:
  283.             update(tcb,&seg,length);
  284.             break;
  285.         case TCP_CLOSING:
  286.             update(tcb,&seg,length);
  287.             if(tcb->sndcnt == 0){
  288.                 /* Our FIN is acknowledged */
  289.                 setstate(tcb,TCP_TIME_WAIT);
  290.                 set_timer(&tcb->timer,MSL2*1000L);
  291.                 start_timer(&tcb->timer);
  292.             }
  293.             break;
  294.         case TCP_LAST_ACK:
  295.             update(tcb,&seg,length);
  296.             if(tcb->sndcnt == 0){
  297.                 /* Our FIN is acknowledged, close connection */
  298.                 close_self(tcb,NORMAL);
  299.                 return;
  300.             }            
  301.             break;
  302.         case TCP_TIME_WAIT:
  303.             start_timer(&tcb->timer);
  304.             break;
  305.         }
  306.  
  307.         /* (URGent bit processing skipped here) */
  308.  
  309.         /* Process the segment text, if any, beginning at rcv.nxt (p. 74) */
  310.         if(length != 0){
  311.             switch(tcb->state){
  312.             case TCP_SYN_RECEIVED:
  313.             case TCP_ESTABLISHED:
  314.             case TCP_FINWAIT1:
  315.             case TCP_FINWAIT2:
  316.                 /* Place on receive queue */
  317.                 append(&tcb->rcvq,bp);
  318.                 tcb->rcvcnt += length;
  319.                 tcb->rcv.nxt += length;
  320.                 tcb->rcv.wnd -= length;
  321.                 tcb->flags.force = 1;
  322.                 /* Notify user */
  323.                 if(tcb->r_upcall)
  324.                     (*tcb->r_upcall)(tcb,tcb->rcvcnt);
  325.                 break;
  326.             default:
  327.                 /* Ignore segment text */
  328.                 free_p(bp);
  329.                 break;
  330.             }
  331.         }
  332.         /* process FIN bit (p 75) */
  333.         if(seg.flags.fin){
  334.             tcb->flags.force = 1;    /* Always respond with an ACK */
  335.  
  336.             switch(tcb->state){
  337.             case TCP_SYN_RECEIVED:
  338.             case TCP_ESTABLISHED:
  339.                 tcb->rcv.nxt++;
  340.                 setstate(tcb,TCP_CLOSE_WAIT);
  341.                 break;
  342.             case TCP_FINWAIT1:
  343.                 tcb->rcv.nxt++;
  344.                 if(tcb->sndcnt == 0){
  345.                     /* Our FIN has been acked; bypass TCP_CLOSING state */
  346.                     setstate(tcb,TCP_TIME_WAIT);
  347.                     set_timer(&tcb->timer,MSL2*1000L);
  348.                     start_timer(&tcb->timer);
  349.                 } else {
  350.                     setstate(tcb,TCP_CLOSING);
  351.                 }
  352.                 break;
  353.             case TCP_FINWAIT2:
  354.                 tcb->rcv.nxt++;
  355.                 setstate(tcb,TCP_TIME_WAIT);
  356.                 set_timer(&tcb->timer,MSL2*1000L);
  357.                 start_timer(&tcb->timer);
  358.                 break;
  359.             case TCP_CLOSE_WAIT:
  360.             case TCP_CLOSING:
  361.             case TCP_LAST_ACK:
  362.                 break;        /* Ignore */
  363.             case TCP_TIME_WAIT:    /* p 76 */
  364.                 start_timer(&tcb->timer);
  365.                 break;
  366.             }
  367.             /* Call the client again so he can see EOF */
  368.             if(tcb->r_upcall)
  369.                 (*tcb->r_upcall)(tcb,tcb->rcvcnt);
  370.         }
  371.         /* Scan the resequencing queue, looking for a segment we can handle,
  372.          * and freeing all those that are now obsolete.
  373.          */
  374.         while(tcb->reseq != NULLRESEQ && seq_ge(tcb->rcv.nxt,tcb->reseq->seg.seq)){
  375.             get_reseq(tcb,&ip->tos,&seg,&bp,&length);
  376.             if(trim(tcb,&seg,&bp,&length) == 0)
  377.                 goto gotone;
  378.             /* Segment is an old one; trim has freed it */
  379.         }
  380.         break;
  381. gotone:    ;
  382.     }
  383.     tcp_output(tcb);    /* Send any necessary ack */
  384. }
  385.  
  386. /* Process an incoming ICMP response */
  387. void
  388. tcp_icmp(icsource,source,dest,type,code,bpp)
  389. int32 icsource;            /* Sender of ICMP message (not used) */
  390. int32 source;            /* Original IP datagram source (i.e. us) */
  391. int32 dest;            /* Original IP datagram dest (i.e., them) */
  392. char type,code;            /* ICMP error codes */
  393. struct mbuf **bpp;        /* First 8 bytes of TCP header */
  394. {
  395.     struct tcp seg;
  396.     struct connection conn;
  397.     register struct tcb *tcb;
  398.  
  399.     /* Extract the socket info from the returned TCP header fragment
  400.      * Note that since this is a datagram we sent, the source fields
  401.      * refer to the local side.
  402.      */
  403.     ntohtcp(&seg,bpp);
  404.     conn.local.port = seg.source;
  405.     conn.remote.port = seg.dest;
  406.     conn.local.address = source;
  407.     conn.remote.address = dest;
  408.     if((tcb = lookup_tcb(&conn)) == NULLTCB)
  409.         return;    /* Unknown connection, ignore */
  410.  
  411.     /* Verify that the sequence number in the returned segment corresponds
  412.      * to something currently unacknowledged. If not, it can safely
  413.      * be ignored.
  414.      */
  415.     if(!seq_within(seg.seq,tcb->snd.una,tcb->snd.nxt))
  416.         return;
  417.  
  418.     /* Destination Unreachable and Time Exceeded messages never kill a
  419.      * connection; the info is merely saved for future reference.
  420.      */
  421.     switch(uchar(type)){
  422.     case ICMP_DEST_UNREACH:
  423.     case ICMP_TIME_EXCEED:
  424.         tcb->type = type;
  425.         tcb->code = code;
  426.         break;
  427.     case ICMP_QUENCH:
  428.         /* Source quench; cut the congestion window in half,
  429.          * but don't let it go below one packet
  430.          */
  431.         tcb->cwind /= 2;
  432.         tcb->cwind = max(tcb->mss,tcb->cwind);
  433.         break;
  434.     }
  435. }
  436. /* Send an acceptable reset (RST) response for this segment
  437.  * The RST reply is composed in place on the input segment
  438.  */
  439. void
  440. reset(ip,seg)
  441. struct ip *ip;            /* Offending IP header */
  442. register struct tcp *seg;    /* Offending TCP header */
  443. {
  444.     struct mbuf *hbp;
  445.     struct pseudo_header ph;
  446.     int16 tmp;
  447.  
  448.     if(seg->flags.rst)
  449.         return;    /* Never send an RST in response to an RST */
  450.  
  451.     /* Compose the RST IP pseudo-header, swapping addresses */
  452.     ph.source = ip->dest;
  453.     ph.dest = ip->source;
  454.     ph.protocol = TCP_PTCL;
  455.     ph.length = TCPLEN;
  456.  
  457.     /* Swap port numbers */
  458.     tmp = seg->source;
  459.     seg->source = seg->dest;
  460.     seg->dest = tmp;
  461.  
  462.     if(seg->flags.ack){
  463.         /* This reset is being sent to clear a half-open connection.
  464.          * Set the sequence number of the RST to the incoming ACK
  465.          * so it will be acceptable.
  466.          */
  467.         seg->flags.ack = 0;
  468.         seg->seq = seg->ack;
  469.         seg->ack = 0;
  470.     } else {
  471.         /* We're rejecting a connect request (SYN) from TCP_LISTEN state
  472.          * so we have to "acknowledge" their SYN.
  473.          */
  474.         seg->flags.ack = 1;
  475.         seg->ack = seg->seq;
  476.         seg->seq = 0;
  477.         if(seg->flags.syn)
  478.             seg->ack++;
  479.     }
  480.     /* Set remaining parts of packet */
  481.     seg->flags.urg = 0;
  482.     seg->flags.psh = 0;
  483.     seg->flags.rst = 1;
  484.     seg->flags.syn = 0;
  485.     seg->flags.fin = 0;
  486.     seg->wnd = 0;
  487.     seg->up = 0;
  488.     seg->mss = 0;
  489.     seg->optlen = 0;
  490.     if((hbp = htontcp(seg,NULLBUF,&ph)) == NULLBUF)
  491.         return;
  492.     /* Ship it out (note swap of addresses) */
  493.     ip_send(ip->dest,ip->source,TCP_PTCL,ip->tos,0,hbp,ph.length,0,0);
  494.     tcpOutRsts++;
  495. }
  496.  
  497. /* Process an incoming acknowledgement and window indication.
  498.  * From page 72.
  499.  */
  500. static void
  501. update(tcb,seg,length)
  502. register struct tcb *tcb;
  503. register struct tcp *seg;
  504. int16 length;
  505. {
  506.     int16 acked;
  507.     int16 expand;
  508.  
  509.     acked = 0;
  510.     if(seq_gt(seg->ack,tcb->snd.nxt)){
  511.         tcb->flags.force = 1;    /* Acks something not yet sent */
  512.         return;
  513.     }
  514.     /* Decide if we need to do a window update.
  515.      * This is always checked whenever a legal ACK is received,
  516.      * even if it doesn't actually acknowledge anything,
  517.      * because it might be a spontaneous window reopening.
  518.      */
  519.     if(seq_gt(seg->seq,tcb->snd.wl1) || ((seg->seq == tcb->snd.wl1) 
  520.      && seq_ge(seg->ack,tcb->snd.wl2))){
  521.         /* If the window had been closed, crank back the
  522.          * send pointer so we'll immediately resume transmission.
  523.          * Otherwise we'd have to wait until the next probe.
  524.          */
  525.         if(tcb->snd.wnd == 0 && seg->wnd != 0)
  526.             tcb->snd.ptr = tcb->snd.una;
  527.         tcb->snd.wnd = seg->wnd;
  528.         tcb->snd.wl1 = seg->seq;
  529.         tcb->snd.wl2 = seg->ack;
  530.     }
  531.     /* See if anything new is being acknowledged */
  532.     if(!seq_gt(seg->ack,tcb->snd.una)){
  533.         if(seg->ack != tcb->snd.una)
  534.             return;    /* Old ack, ignore */
  535.  
  536.         if(length != 0 || seg->flags.syn || seg->flags.fin)
  537.             return;    /* Nothing acked, but there is data */
  538.  
  539.         /* Van Jacobson "fast recovery" code */
  540.         if(++tcb->dupacks == TCPDUPACKS){
  541.             /* We've had a burst of do-nothing acks, so
  542.              * we almost certainly lost a packet.
  543.              * Resend it now to avoid a timeout. (This is
  544.              * Van Jacobson's 'quick recovery' algorithm.)
  545.              */
  546.             int32 ptrsave;
  547.  
  548.             /* Knock the threshold down just as though
  549.              * this were a timeout, since we've had
  550.              * network congestion.
  551.              */
  552.             tcb->ssthresh = tcb->cwind/2;
  553.             tcb->ssthresh = max(tcb->ssthresh,tcb->mss);
  554.  
  555.             /* Manipulate the machinery in tcp_output() to
  556.              * retransmit just the missing packet
  557.              */
  558.             ptrsave = tcb->snd.ptr;
  559.             tcb->snd.ptr = tcb->snd.una;
  560.             tcb->cwind = tcb->mss;
  561.             tcp_output(tcb);
  562.             tcb->snd.ptr = ptrsave;
  563.  
  564.             /* "Inflate" the congestion window, pretending as
  565.              * though the duplicate acks were normally acking
  566.              * the packets beyond the one that was lost.
  567.              */
  568.             tcb->cwind = tcb->ssthresh + TCPDUPACKS*tcb->mss;
  569.         } else if(tcb->dupacks > TCPDUPACKS){
  570.             /* Continue to inflate the congestion window
  571.              * until the acks finally get "unstuck".
  572.              */
  573.             tcb->cwind += tcb->mss;
  574.         }
  575.         return;
  576.     }
  577.     if(tcb->dupacks >= TCPDUPACKS && tcb->cwind > tcb->ssthresh){
  578.         /* The acks have finally gotten "unstuck". So now we
  579.          * can "deflate" the congestion window, i.e. take it
  580.          * back down to where it would be after slow start
  581.          * finishes.
  582.          */
  583.         tcb->cwind = tcb->ssthresh;
  584.     }
  585.     tcb->dupacks = 0;
  586.  
  587.     /* We're here, so the ACK must have actually acked something */
  588.     acked = seg->ack - tcb->snd.una;
  589.  
  590.     /* Expand congestion window if not already at limit and if
  591.      * this packet wasn't retransmitted
  592.      */
  593.     if(tcb->cwind < tcb->snd.wnd && !tcb->flags.retran){
  594.         if(tcb->cwind < tcb->ssthresh){
  595.             /* Still doing slow start/CUTE, expand by amount acked */
  596.             expand = min(acked,tcb->mss);
  597.         } else {
  598.             /* Steady-state test of extra path capacity */
  599.             expand = ((long)tcb->mss * tcb->mss) / tcb->cwind;
  600.         }
  601.         /* Guard against arithmetic overflow */
  602.         if(tcb->cwind + expand < tcb->cwind)
  603.             expand = MAXINT16 - tcb->cwind;
  604.  
  605.         /* Don't expand beyond the offered window */
  606.         if(tcb->cwind + expand > tcb->snd.wnd)
  607.             expand = tcb->snd.wnd - tcb->cwind;
  608.  
  609.         if(expand != 0){
  610. #ifdef    notdef
  611.             /* Kick up the mean deviation estimate to prevent
  612.              * unnecessary retransmission should we already be
  613.              * bandwidth limited
  614.              */
  615.             tcb->mdev += ((long)tcb->srtt * expand) / tcb->cwind;
  616. #endif
  617.             tcb->cwind += expand;
  618.         }
  619.     }
  620.     /* Round trip time estimation */
  621.     if(tcb->flags.rtt_run && seq_ge(seg->ack,tcb->rttseq)){
  622.         /* A timed sequence number has been acked */
  623.         tcb->flags.rtt_run = 0;
  624.         if(!(tcb->flags.retran)){
  625.             int32 rtt;    /* measured round trip time */
  626.             int32 abserr;    /* abs(rtt - srtt) */
  627.  
  628.             /* This packet was sent only once and now
  629.              * it's been acked, so process the round trip time
  630.              */
  631.             rtt = msclock() - tcb->rtt_time;
  632.  
  633.             abserr = (rtt > tcb->srtt) ? rtt - tcb->srtt : tcb->srtt - rtt;
  634.             /* Run SRTT and MDEV integrators, with rounding */
  635.             tcb->srtt = ((AGAIN-1)*tcb->srtt + rtt + (AGAIN/2)) >> LAGAIN;
  636.             tcb->mdev = ((DGAIN-1)*tcb->mdev + abserr + (DGAIN/2)) >> LDGAIN;
  637.  
  638.             rtt_add(tcb->conn.remote.address,rtt);
  639.             /* Reset the backoff level */
  640.             tcb->backoff = 0;
  641.         }
  642.     }
  643.     tcb->sndcnt -= acked;    /* Update virtual byte count on snd queue */
  644.     tcb->snd.una = seg->ack;
  645.  
  646.     /* If we're waiting for an ack of our SYN, note it and adjust count */
  647.     if(!(tcb->flags.synack)){
  648.         tcb->flags.synack = 1;
  649.         acked--;    /* One less byte to pull from real snd queue */
  650.     }
  651.     /* Remove acknowledged bytes from the send queue and update the
  652.      * unacknowledged pointer. If a FIN is being acked,
  653.      * pullup won't be able to remove it from the queue, but that
  654.      * causes no harm.
  655.      */
  656.     pullup(&tcb->sndq,NULLCHAR,acked);
  657.  
  658.     /* Stop retransmission timer, but restart it if there is still
  659.      * unacknowledged data. If there is no more unacked data,
  660.      * the transmitter has gone at least momentarily idle, so
  661.      * record the time for the VJ restart-slowstart rule.
  662.      */    
  663.     stop_timer(&tcb->timer);
  664.     if(tcb->snd.una != tcb->snd.nxt)
  665.         start_timer(&tcb->timer);
  666.     else
  667.         tcb->lastactive = msclock();
  668.  
  669.     /* If retransmissions have been occurring, make sure the
  670.      * send pointer doesn't repeat ancient history
  671.      */
  672.     if(seq_lt(tcb->snd.ptr,tcb->snd.una))
  673.         tcb->snd.ptr = tcb->snd.una;
  674.  
  675.     /* Clear the retransmission flag since the oldest
  676.      * unacknowledged segment (the only one that is ever retransmitted)
  677.      * has now been acked.
  678.      */
  679.     tcb->flags.retran = 0;
  680.  
  681.     /* If outgoing data was acked, notify the user so he can send more
  682.      * unless we've already sent a FIN.
  683.      */
  684.     if(acked != 0 && tcb->t_upcall
  685.      && (tcb->state == TCP_ESTABLISHED || tcb->state == TCP_CLOSE_WAIT)){
  686.         (*tcb->t_upcall)(tcb,tcb->window - tcb->sndcnt);
  687.     }
  688. }
  689.  
  690. /* Determine if the given sequence number is in our receiver window.
  691.  * NB: must not be used when window is closed!
  692.  */
  693. static
  694. int
  695. in_window(tcb,seq)
  696. struct tcb *tcb;
  697. int32 seq;
  698. {
  699.     return seq_within(seq,tcb->rcv.nxt,(int32)(tcb->rcv.nxt+tcb->rcv.wnd-1));
  700. }
  701.  
  702. /* Process an incoming SYN */
  703. static void
  704. proc_syn(tcb,tos,seg)
  705. register struct tcb *tcb;
  706. char tos;
  707. struct tcp *seg;
  708. {
  709.     int16 mtu;
  710.     struct tcp_rtt *tp;
  711.  
  712.     tcb->flags.force = 1;    /* Always send a response */
  713.  
  714.     /* Note: It's not specified in RFC 793, but SND.WL1 and
  715.      * SND.WND are initialized here since it's possible for the
  716.      * window update routine in update() to fail depending on the
  717.      * IRS if they are left unitialized.
  718.      */
  719.     /* Check incoming precedence and increase if higher */
  720.     if(PREC(tos) > PREC(tcb->tos))
  721.         tcb->tos = tos;
  722.     tcb->rcv.nxt = seg->seq + 1;    /* p 68 */
  723.     tcb->snd.wl1 = tcb->irs = seg->seq;
  724.     tcb->snd.wnd = seg->wnd;
  725.     if(seg->mss != 0)
  726.         tcb->mss = seg->mss;
  727.     /* Check the MTU of the interface we'll use to reach this guy
  728.      * and lower the MSS so that unnecessary fragmentation won't occur
  729.      */
  730.     if((mtu = ip_mtu(tcb->conn.remote.address)) != 0){
  731.         /* Allow space for the TCP and IP headers */
  732.         mtu -= TCPLEN + IPLEN;
  733.         tcb->cwind = tcb->mss = min(mtu,tcb->mss);
  734.     }
  735.     /* See if there's round-trip time experience */
  736.     if((tp = rtt_get(tcb->conn.remote.address)) != NULLRTT){
  737.         tcb->srtt = tp->srtt;
  738.         tcb->mdev = tp->mdev;
  739.     }
  740. }
  741.  
  742. /* Generate an initial sequence number and put a SYN on the send queue */
  743. void
  744. send_syn(tcb)
  745. register struct tcb *tcb;
  746. {
  747.     tcb->iss = geniss();
  748.     tcb->rttseq = tcb->snd.wl2 = tcb->snd.una = tcb->iss;
  749.     tcb->snd.ptr = tcb->snd.nxt = tcb->rttseq;
  750.     tcb->sndcnt++;
  751.     tcb->flags.force = 1;
  752. }
  753.  
  754. /* Add an entry to the resequencing queue in the proper place */
  755. static void
  756. add_reseq(tcb,tos,seg,bp,length)
  757. struct tcb *tcb;
  758. char tos;
  759. struct tcp *seg;
  760. struct mbuf *bp;
  761. int16 length;
  762. {
  763.     register struct reseq *rp,*rp1;
  764.  
  765.     /* Allocate reassembly descriptor */
  766.     if((rp = (struct reseq *)malloc(sizeof (struct reseq))) == NULLRESEQ){
  767.         /* No space, toss on floor */
  768.         free_p(bp);
  769.         return;
  770.     }
  771.     ASSIGN(rp->seg,*seg);
  772.     rp->tos = tos;
  773.     rp->bp = bp;
  774.     rp->length = length;
  775.  
  776.     /* Place on reassembly list sorting by starting seq number */
  777.     rp1 = tcb->reseq;
  778.     if(rp1 == NULLRESEQ || seq_lt(seg->seq,rp1->seg.seq)){
  779.         /* Either the list is empty, or we're less than all other
  780.          * entries; insert at beginning.
  781.          */
  782.         rp->next = rp1;
  783.         tcb->reseq = rp;
  784.     } else {
  785.         /* Find the last entry less than us */
  786.         for(;;){
  787.             if(rp1->next == NULLRESEQ || seq_lt(seg->seq,rp1->next->seg.seq)){
  788.                 /* We belong just after this one */
  789.                 rp->next = rp1->next;
  790.                 rp1->next = rp;
  791.                 break;
  792.             }
  793.             rp1 = rp1->next;
  794.         }
  795.     }
  796. }
  797.  
  798. /* Fetch the first entry off the resequencing queue */
  799. static void
  800. get_reseq(tcb,tos,seg,bp,length)
  801. register struct tcb *tcb;
  802. char *tos;
  803. struct tcp *seg;
  804. struct mbuf **bp;
  805. int16 *length;
  806. {
  807.     register struct reseq *rp;
  808.  
  809.     if((rp = tcb->reseq) == NULLRESEQ)
  810.         return;
  811.  
  812.     tcb->reseq = rp->next;
  813.  
  814.     *tos = rp->tos;
  815.     ASSIGN(*seg,rp->seg);
  816.     *bp = rp->bp;
  817.     *length = rp->length;
  818.     free((char *)rp);
  819. }
  820.  
  821. /* Trim segment to fit window. Return 0 if OK, -1 if segment is
  822.  * unacceptable.
  823.  */
  824. static int
  825. trim(tcb,seg,bpp,length)
  826. register struct tcb *tcb;
  827. register struct tcp *seg;
  828. struct mbuf **bpp;
  829. int16 *length;
  830. {
  831.     long dupcnt,excess;
  832.     int16 len;        /* Segment length including flags */
  833.     char accept = 0;
  834.  
  835.     len = *length;
  836.     if(seg->flags.syn)
  837.         len++;
  838.     if(seg->flags.fin)
  839.         len++;
  840.  
  841.     /* Acceptability tests */
  842.     if(tcb->rcv.wnd == 0){
  843.         /* Only in-order, zero-length segments are acceptable when
  844.          * our window is closed.
  845.          */
  846.         if(seg->seq == tcb->rcv.nxt && len == 0){
  847.             return 0;    /* Acceptable, no trimming needed */
  848.         }
  849.     } else {
  850.         /* Some part of the segment must be in the window */
  851.         if(in_window(tcb,seg->seq)){
  852.             accept++;    /* Beginning is */
  853.         } else if(len != 0){
  854.             if(in_window(tcb,(int32)(seg->seq+len-1)) || /* End is */
  855.              seq_within(tcb->rcv.nxt,seg->seq,(int32)(seg->seq+len-1))){ /* Straddles */
  856.                 accept++;
  857.             }
  858.         }
  859.     }
  860.     if(!accept){
  861.         tcb->rerecv += len;    /* Assume all of it was a duplicate */
  862.         free_p(*bpp);
  863.         return -1;
  864.     }
  865.     if((dupcnt = tcb->rcv.nxt - seg->seq) > 0){
  866.         tcb->rerecv += dupcnt;
  867.         /* Trim off SYN if present */
  868.         if(seg->flags.syn){
  869.             /* SYN is before first data byte */
  870.             seg->flags.syn = 0;
  871.             seg->seq++;
  872.             dupcnt--;
  873.         }
  874.         if(dupcnt > 0){
  875.             pullup(bpp,NULLCHAR,(int16)dupcnt);
  876.             seg->seq += dupcnt;
  877.             *length -= dupcnt;
  878.         }
  879.     }
  880.     if((excess = seg->seq + *length - (tcb->rcv.nxt + tcb->rcv.wnd)) > 0){
  881.         tcb->rerecv += excess;
  882.         /* Trim right edge */
  883.         *length -= excess;
  884.         trim_mbuf(bpp,*length);
  885.         seg->flags.fin = 0;    /* FIN follows last data byte */
  886.     }
  887.     return 0;
  888. }
  889.