The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/tcp_input.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
   34  * $FreeBSD$
   35  */
   36 
   37 #include "opt_ipfw.h"           /* for ipfw_fwd         */
   38 #include "opt_tcpdebug.h"
   39 #include "opt_tcp_input.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/kernel.h>
   44 #include <sys/sysctl.h>
   45 #include <sys/malloc.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/proc.h>           /* for proc0 declaration */
   48 #include <sys/protosw.h>
   49 #include <sys/socket.h>
   50 #include <sys/socketvar.h>
   51 #include <sys/syslog.h>
   52 
   53 #include <machine/cpu.h>        /* before tcp_seq.h, for tcp_random18() */
   54 
   55 #include <net/if.h>
   56 #include <net/route.h>
   57 
   58 #include <netinet/in.h>
   59 #include <netinet/in_systm.h>
   60 #include <netinet/ip.h>
   61 #include <netinet/ip_icmp.h>    /* for ICMP_BANDLIM             */
   62 #include <netinet/in_pcb.h>
   63 #include <netinet/ip_var.h>
   64 #include <netinet/icmp_var.h>   /* for ICMP_BANDLIM             */
   65 #include <netinet/tcp.h>
   66 #include <netinet/tcp_fsm.h>
   67 #include <netinet/tcp_seq.h>
   68 #include <netinet/tcp_timer.h>
   69 #include <netinet/tcp_var.h>
   70 #include <netinet/tcpip.h>
   71 #ifdef TCPDEBUG
   72 #include <netinet/tcp_debug.h>
   73 static struct   tcpiphdr tcp_saveti;
   74 #endif
   75 
   76 static int      tcprexmtthresh = 3;
   77 tcp_seq tcp_iss;
   78 tcp_cc  tcp_ccgen;
   79 
   80 struct  tcpstat tcpstat;
   81 SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats,
   82         CTLFLAG_RD, &tcpstat , tcpstat, "");
   83 
   84 static int log_in_vain = 0;
   85 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 
   86         &log_in_vain, 0, "");
   87 
   88 int tcp_delack_enabled = 1;
   89 SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 
   90         &tcp_delack_enabled, 0, "");
   91 
   92 #ifdef TCP_DROP_SYNFIN
   93 static int drop_synfin = 0;
   94 SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
   95     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
   96 #endif
   97 
   98 #ifdef TCP_RESTRICT_RST
   99 static int restrict_rst = 0;
  100 SYSCTL_INT(_net_inet_tcp, OID_AUTO, restrict_rst, CTLFLAG_RW,
  101     &restrict_rst, 0, "Restrict RST emission");
  102 #endif
  103 
  104 u_long  tcp_now;
  105 struct inpcbhead tcb;
  106 struct inpcbinfo tcbinfo;
  107 
  108 static void      tcp_dooptions __P((struct tcpcb *,
  109             u_char *, int, struct tcpiphdr *, struct tcpopt *));
  110 static void      tcp_pulloutofband __P((struct socket *,
  111             struct tcpiphdr *, struct mbuf *));
  112 static int       tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *));
  113 static void      tcp_xmit_timer __P((struct tcpcb *, int));
  114 
  115 
  116 /*
  117  * Insert segment ti into reassembly queue of tcp with
  118  * control block tp.  Return TH_FIN if reassembly now includes
  119  * a segment with FIN.  The macro form does the common case inline
  120  * (segment is the next to be received on an established connection,
  121  * and the queue is empty), avoiding linkage into and removal
  122  * from the queue and repetition of various conversions.
  123  * Set DELACK for segments received in order, but ack immediately
  124  * when segments are out of order (so fast retransmit can work).
  125  */
  126 #define TCP_REASS(tp, ti, m, so, flags) { \
  127         if ((ti)->ti_seq == (tp)->rcv_nxt && \
  128             (tp)->t_segq == NULL && \
  129             (tp)->t_state == TCPS_ESTABLISHED) { \
  130                 if (tcp_delack_enabled) \
  131                         tp->t_flags |= TF_DELACK; \
  132                 else \
  133                         tp->t_flags |= TF_ACKNOW; \
  134                 (tp)->rcv_nxt += (ti)->ti_len; \
  135                 flags = (ti)->ti_flags & TH_FIN; \
  136                 tcpstat.tcps_rcvpack++;\
  137                 tcpstat.tcps_rcvbyte += (ti)->ti_len;\
  138                 sbappend(&(so)->so_rcv, (m)); \
  139                 sorwakeup(so); \
  140         } else { \
  141                 (flags) = tcp_reass((tp), (ti), (m)); \
  142                 tp->t_flags |= TF_ACKNOW; \
  143         } \
  144 }
  145 
  146 static int
  147 tcp_reass(tp, ti, m)
  148         register struct tcpcb *tp;
  149         register struct tcpiphdr *ti;
  150         struct mbuf *m;
  151 {
  152         struct mbuf *q;
  153         struct mbuf *p;
  154         struct mbuf *nq;
  155         struct socket *so = tp->t_inpcb->inp_socket;
  156         int flags;
  157 
  158 #define GETTCP(m)       ((struct tcpiphdr *)m->m_pkthdr.header)
  159 
  160         /*
  161          * Call with ti==0 after become established to
  162          * force pre-ESTABLISHED data up to user socket.
  163          */
  164         if (ti == 0)
  165                 goto present;
  166 
  167         m->m_pkthdr.header = ti;
  168 
  169         /*
  170          * Find a segment which begins after this one does.
  171          */
  172         for (q = tp->t_segq, p = NULL; q; p = q, q = q->m_nextpkt)
  173                 if (SEQ_GT(GETTCP(q)->ti_seq, ti->ti_seq))
  174                         break;
  175 
  176         /*
  177          * If there is a preceding segment, it may provide some of
  178          * our data already.  If so, drop the data from the incoming
  179          * segment.  If it provides all of our data, drop us.
  180          */
  181         if (p != NULL) {
  182                 register int i;
  183                 /* conversion to int (in i) handles seq wraparound */
  184                 i = GETTCP(p)->ti_seq + GETTCP(p)->ti_len - ti->ti_seq;
  185                 if (i > 0) {
  186                         if (i >= ti->ti_len) {
  187                                 tcpstat.tcps_rcvduppack++;
  188                                 tcpstat.tcps_rcvdupbyte += ti->ti_len;
  189                                 m_freem(m);
  190                                 /*
  191                                  * Try to present any queued data
  192                                  * at the left window edge to the user.
  193                                  * This is needed after the 3-WHS
  194                                  * completes.
  195                                  */
  196                                 goto present;   /* ??? */
  197                         }
  198                         m_adj(m, i);
  199                         ti->ti_len -= i;
  200                         ti->ti_seq += i;
  201                 }
  202         }
  203         tcpstat.tcps_rcvoopack++;
  204         tcpstat.tcps_rcvoobyte += ti->ti_len;
  205 
  206         /*
  207          * While we overlap succeeding segments trim them or,
  208          * if they are completely covered, dequeue them.
  209          */
  210         while (q) {
  211                 register int i = (ti->ti_seq + ti->ti_len) - GETTCP(q)->ti_seq;
  212                 if (i <= 0)
  213                         break;
  214                 if (i < GETTCP(q)->ti_len) {
  215                         GETTCP(q)->ti_seq += i;
  216                         GETTCP(q)->ti_len -= i;
  217                         m_adj(q, i);
  218                         break;
  219                 }
  220 
  221                 nq = q->m_nextpkt;
  222                 if (p)
  223                         p->m_nextpkt = nq;
  224                 else
  225                         tp->t_segq = nq;
  226                 m_freem(q);
  227                 q = nq;
  228         }
  229 
  230         if (p == NULL) {
  231                 m->m_nextpkt = tp->t_segq;
  232                 tp->t_segq = m;
  233         } else {
  234                 m->m_nextpkt = p->m_nextpkt;
  235                 p->m_nextpkt = m;
  236         }
  237 
  238 present:
  239         /*
  240          * Present data to user, advancing rcv_nxt through
  241          * completed sequence space.
  242          */
  243         if (!TCPS_HAVEESTABLISHED(tp->t_state))
  244                 return (0);
  245         q = tp->t_segq;
  246         if (!q || GETTCP(q)->ti_seq != tp->rcv_nxt)
  247                 return (0);
  248         do {
  249                 tp->rcv_nxt += GETTCP(q)->ti_len;
  250                 flags = GETTCP(q)->ti_flags & TH_FIN;
  251                 nq = q->m_nextpkt;
  252                 tp->t_segq = nq;
  253                 q->m_nextpkt = NULL;
  254                 if (so->so_state & SS_CANTRCVMORE)
  255                         m_freem(q);
  256                 else
  257                         sbappend(&so->so_rcv, q);
  258                 q = nq;
  259         } while (q && GETTCP(q)->ti_seq == tp->rcv_nxt);
  260         sorwakeup(so);
  261         return (flags);
  262 
  263 #undef GETTCP
  264 }
  265 
  266 /*
  267  * TCP input routine, follows pages 65-76 of the
  268  * protocol specification dated September, 1981 very closely.
  269  */
  270 void
  271 tcp_input(m, iphlen)
  272         register struct mbuf *m;
  273         int iphlen;
  274 {
  275         register struct tcpiphdr *ti;
  276         register struct inpcb *inp;
  277         u_char *optp = NULL;
  278         int optlen = 0;
  279         int len, tlen, off;
  280         register struct tcpcb *tp = 0;
  281         register int tiflags;
  282         struct socket *so = 0;
  283         int todrop, acked, ourfinisacked, needoutput = 0;
  284         struct in_addr laddr;
  285         int dropsocket = 0;
  286         int iss = 0;
  287         u_long tiwin;
  288         struct tcpopt to;               /* options in this segment */
  289         struct rmxp_tao *taop;          /* pointer to our TAO cache entry */
  290         struct rmxp_tao tao_noncached;  /* in case there's no cached entry */
  291 #ifdef TCPDEBUG
  292         short ostate = 0;
  293 #endif
  294 
  295         bzero((char *)&to, sizeof(to));
  296 
  297         tcpstat.tcps_rcvtotal++;
  298         /*
  299          * Get IP and TCP header together in first mbuf.
  300          * Note: IP leaves IP header in first mbuf.
  301          */
  302         ti = mtod(m, struct tcpiphdr *);
  303         if (iphlen > sizeof (struct ip))
  304                 ip_stripoptions(m, (struct mbuf *)0);
  305         if (m->m_len < sizeof (struct tcpiphdr)) {
  306                 if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
  307                         tcpstat.tcps_rcvshort++;
  308                         return;
  309                 }
  310                 ti = mtod(m, struct tcpiphdr *);
  311         }
  312 
  313         /*
  314          * Checksum extended TCP header and data.
  315          */
  316         tlen = ((struct ip *)ti)->ip_len;
  317         len = sizeof (struct ip) + tlen;
  318         bzero(ti->ti_x1, sizeof(ti->ti_x1));
  319         ti->ti_len = (u_short)tlen;
  320         HTONS(ti->ti_len);
  321         ti->ti_sum = in_cksum(m, len);
  322         if (ti->ti_sum) {
  323                 tcpstat.tcps_rcvbadsum++;
  324                 goto drop;
  325         }
  326 
  327         /*
  328          * Check that TCP offset makes sense,
  329          * pull out TCP options and adjust length.              XXX
  330          */
  331         off = ti->ti_off << 2;
  332         if (off < sizeof (struct tcphdr) || off > tlen) {
  333                 tcpstat.tcps_rcvbadoff++;
  334                 goto drop;
  335         }
  336         tlen -= off;
  337         ti->ti_len = tlen;
  338         if (off > sizeof (struct tcphdr)) {
  339                 if (m->m_len < sizeof(struct ip) + off) {
  340                         if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
  341                                 tcpstat.tcps_rcvshort++;
  342                                 return;
  343                         }
  344                         ti = mtod(m, struct tcpiphdr *);
  345                 }
  346                 optlen = off - sizeof (struct tcphdr);
  347                 optp = mtod(m, u_char *) + sizeof (struct tcpiphdr);
  348         }
  349         tiflags = ti->ti_flags;
  350 
  351 #ifdef TCP_DROP_SYNFIN
  352         /*
  353          * If the drop_synfin option is enabled, drop all packets with
  354          * both the SYN and FIN bits set. This prevents e.g. nmap from
  355          * identifying the TCP/IP stack.
  356          *
  357          * This is incompatible with RFC1644 extensions (T/TCP).
  358          */
  359         if (drop_synfin && (tiflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
  360                 goto drop;
  361 #endif
  362 
  363         /*
  364          * Convert TCP protocol specific fields to host format.
  365          */
  366         NTOHL(ti->ti_seq);
  367         NTOHL(ti->ti_ack);
  368         NTOHS(ti->ti_win);
  369         NTOHS(ti->ti_urp);
  370 
  371         /*
  372          * Drop TCP, IP headers and TCP options.
  373          */
  374         m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
  375         m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
  376 
  377         /*
  378          * Locate pcb for segment.
  379          */
  380 findpcb:
  381 #ifdef IPFIREWALL_FORWARD
  382         if (ip_fw_fwd_addr != NULL) {
  383                 /*
  384                  * Diverted. Pretend to be the destination.
  385                  * already got one like this? 
  386                  */
  387                 inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport,
  388                         ti->ti_dst, ti->ti_dport, 0);
  389                 if (!inp) {
  390                         /* 
  391                          * No, then it's new. Try find the ambushing socket
  392                          */
  393                         if (!ip_fw_fwd_addr->sin_port) {
  394                                 inp = in_pcblookup_hash(&tcbinfo, ti->ti_src,
  395                                     ti->ti_sport, ip_fw_fwd_addr->sin_addr,
  396                                     ti->ti_dport, 1);
  397                         } else {
  398                                 inp = in_pcblookup_hash(&tcbinfo,
  399                                     ti->ti_src, ti->ti_sport,
  400                                     ip_fw_fwd_addr->sin_addr,
  401                                     ntohs(ip_fw_fwd_addr->sin_port), 1);
  402                         }
  403                 }
  404                 ip_fw_fwd_addr = NULL;
  405         } else
  406 #endif  /* IPFIREWALL_FORWARD */
  407 
  408         inp = in_pcblookup_hash(&tcbinfo, ti->ti_src, ti->ti_sport,
  409             ti->ti_dst, ti->ti_dport, 1);
  410 
  411         /*
  412          * If the state is CLOSED (i.e., TCB does not exist) then
  413          * all data in the incoming segment is discarded.
  414          * If the TCB exists but is in CLOSED state, it is embryonic,
  415          * but should either do a listen or a connect soon.
  416          */
  417         if (inp == NULL) {
  418                 if (log_in_vain && tiflags & TH_SYN) {
  419                         char buf[4*sizeof "123"];
  420 
  421                         strcpy(buf, inet_ntoa(ti->ti_dst));
  422                         log(LOG_INFO,
  423                             "Connection attempt to TCP %s:%d from %s:%d\n",
  424                             buf, ntohs(ti->ti_dport), inet_ntoa(ti->ti_src),
  425                             ntohs(ti->ti_sport));
  426                 }
  427 #ifdef ICMP_BANDLIM
  428                 if (badport_bandlim(1) < 0)
  429                         goto drop;
  430 #endif
  431                 goto dropwithreset;
  432         }
  433         tp = intotcpcb(inp);
  434         if (tp == 0)
  435                 goto maybedropwithreset;
  436         if (tp->t_state == TCPS_CLOSED)
  437                 goto drop;
  438 
  439         /* Unscale the window into a 32-bit value. */
  440         if ((tiflags & TH_SYN) == 0)
  441                 tiwin = ti->ti_win << tp->snd_scale;
  442         else
  443                 tiwin = ti->ti_win;
  444 
  445         so = inp->inp_socket;
  446         if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
  447 #ifdef TCPDEBUG
  448                 if (so->so_options & SO_DEBUG) {
  449                         ostate = tp->t_state;
  450                         tcp_saveti = *ti;
  451                 }
  452 #endif
  453                 if (so->so_options & SO_ACCEPTCONN) {
  454                         register struct tcpcb *tp0 = tp;
  455                         struct socket *so2;
  456                         if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
  457                                 /*
  458                                  * Note: dropwithreset makes sure we don't
  459                                  * send a RST in response to a RST.
  460                                  */
  461                                 if (tiflags & TH_ACK) {
  462                                         tcpstat.tcps_badsyn++;
  463                                         goto maybedropwithreset;
  464                                 }
  465                                 goto drop;
  466                         }
  467                         so2 = sonewconn(so, 0);
  468                         if (so2 == 0) {
  469                                 tcpstat.tcps_listendrop++;
  470                                 so2 = sodropablereq(so);
  471                                 if (so2) {
  472                                         tcp_drop(sototcpcb(so2), ETIMEDOUT);
  473                                         so2 = sonewconn(so, 0);
  474                                 }
  475                                 if (!so2)
  476                                         goto drop;
  477                         }
  478                         so = so2;
  479                         /*
  480                          * This is ugly, but ....
  481                          *
  482                          * Mark socket as temporary until we're
  483                          * committed to keeping it.  The code at
  484                          * ``drop'' and ``dropwithreset'' check the
  485                          * flag dropsocket to see if the temporary
  486                          * socket created here should be discarded.
  487                          * We mark the socket as discardable until
  488                          * we're committed to it below in TCPS_LISTEN.
  489                          */
  490                         dropsocket++;
  491                         inp = (struct inpcb *)so->so_pcb;
  492                         inp->inp_laddr = ti->ti_dst;
  493                         inp->inp_lport = ti->ti_dport;
  494                         if (in_pcbinshash(inp) != 0) {
  495                                 /*
  496                                  * Undo the assignments above if we failed to put
  497                                  * the PCB on the hash lists.
  498                                  */
  499                                 inp->inp_laddr.s_addr = INADDR_ANY;
  500                                 inp->inp_lport = 0;
  501                                 goto drop;
  502                         }
  503                         inp->inp_options = ip_srcroute();
  504                         tp = intotcpcb(inp);
  505                         tp->t_state = TCPS_LISTEN;
  506                         tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
  507 
  508                         /* Compute proper scaling value from buffer space */
  509                         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
  510                            TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
  511                                 tp->request_r_scale++;
  512                 }
  513         }
  514 
  515         /*
  516          * Segment received on connection.
  517          * Reset idle time and keep-alive timer.
  518          */
  519         tp->t_idle = 0;
  520         if (TCPS_HAVEESTABLISHED(tp->t_state))
  521                 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
  522 
  523         /*
  524          * Process options if not in LISTEN state,
  525          * else do it below (after getting remote address).
  526          */
  527         if (tp->t_state != TCPS_LISTEN)
  528                 tcp_dooptions(tp, optp, optlen, ti, &to);
  529 
  530         /*
  531          * Header prediction: check for the two common cases
  532          * of a uni-directional data xfer.  If the packet has
  533          * no control flags, is in-sequence, the window didn't
  534          * change and we're not retransmitting, it's a
  535          * candidate.  If the length is zero and the ack moved
  536          * forward, we're the sender side of the xfer.  Just
  537          * free the data acked & wake any higher level process
  538          * that was blocked waiting for space.  If the length
  539          * is non-zero and the ack didn't move, we're the
  540          * receiver side.  If we're getting packets in-order
  541          * (the reassembly queue is empty), add the data to
  542          * the socket buffer and note that we need a delayed ack.
  543          * Make sure that the hidden state-flags are also off.
  544          * Since we check for TCPS_ESTABLISHED above, it can only
  545          * be TH_NEEDSYN.
  546          */
  547         if (tp->t_state == TCPS_ESTABLISHED &&
  548             (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
  549             ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
  550             ((to.to_flag & TOF_TS) == 0 ||
  551              TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
  552             /*
  553              * Using the CC option is compulsory if once started:
  554              *   the segment is OK if no T/TCP was negotiated or
  555              *   if the segment has a CC option equal to CCrecv
  556              */
  557             ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
  558              ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
  559             ti->ti_seq == tp->rcv_nxt &&
  560             tiwin && tiwin == tp->snd_wnd &&
  561             tp->snd_nxt == tp->snd_max) {
  562 
  563                 /*
  564                  * If last ACK falls within this segment's sequence numbers,
  565                  * record the timestamp.
  566                  * NOTE that the test is modified according to the latest
  567                  * proposal of the tcplw@cray.com list (Braden 1993/04/26).
  568                  */
  569                 if ((to.to_flag & TOF_TS) != 0 &&
  570                    SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
  571                         tp->ts_recent_age = tcp_now;
  572                         tp->ts_recent = to.to_tsval;
  573                 }
  574 
  575                 if (ti->ti_len == 0) {
  576                         if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
  577                             SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
  578                             tp->snd_cwnd >= tp->snd_wnd &&
  579                             tp->t_dupacks < tcprexmtthresh) {
  580                                 /*
  581                                  * this is a pure ack for outstanding data.
  582                                  */
  583                                 ++tcpstat.tcps_predack;
  584                                 if ((to.to_flag & TOF_TS) != 0)
  585                                         tcp_xmit_timer(tp,
  586                                             tcp_now - to.to_tsecr + 1);
  587                                 else if (tp->t_rtt &&
  588                                             SEQ_GT(ti->ti_ack, tp->t_rtseq))
  589                                         tcp_xmit_timer(tp, tp->t_rtt);
  590                                 acked = ti->ti_ack - tp->snd_una;
  591                                 tcpstat.tcps_rcvackpack++;
  592                                 tcpstat.tcps_rcvackbyte += acked;
  593                                 sbdrop(&so->so_snd, acked);
  594                                 tp->snd_una = ti->ti_ack;
  595                                 m_freem(m);
  596 
  597                                 /*
  598                                  * If all outstanding data are acked, stop
  599                                  * retransmit timer, otherwise restart timer
  600                                  * using current (possibly backed-off) value.
  601                                  * If process is waiting for space,
  602                                  * wakeup/selwakeup/signal.  If data
  603                                  * are ready to send, let tcp_output
  604                                  * decide between more output or persist.
  605                                  */
  606                                 if (tp->snd_una == tp->snd_max)
  607                                         tp->t_timer[TCPT_REXMT] = 0;
  608                                 else if (tp->t_timer[TCPT_PERSIST] == 0)
  609                                         tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
  610 
  611                                 sowwakeup(so);
  612                                 if (so->so_snd.sb_cc)
  613                                         (void) tcp_output(tp);
  614                                 return;
  615                         }
  616                 } else if (ti->ti_ack == tp->snd_una &&
  617                     tp->t_segq == NULL &&
  618                     ti->ti_len <= sbspace(&so->so_rcv)) {
  619                         /*
  620                          * this is a pure, in-sequence data packet
  621                          * with nothing on the reassembly queue and
  622                          * we have enough buffer space to take it.
  623                          */
  624                         ++tcpstat.tcps_preddat;
  625                         tp->rcv_nxt += ti->ti_len;
  626                         tcpstat.tcps_rcvpack++;
  627                         tcpstat.tcps_rcvbyte += ti->ti_len;
  628                         /*
  629                          * Add data to socket buffer.
  630                          */
  631                         sbappend(&so->so_rcv, m);
  632                         sorwakeup(so);
  633                         if (tcp_delack_enabled) {
  634                                 tp->t_flags |= TF_DELACK;
  635                         } else {
  636                                 tp->t_flags |= TF_ACKNOW;
  637                                 tcp_output(tp);
  638                         }
  639                         return;
  640                 }
  641         }
  642 
  643         /*
  644          * Calculate amount of space in receive window,
  645          * and then do TCP input processing.
  646          * Receive window is amount of space in rcv queue,
  647          * but not less than advertised window.
  648          */
  649         { int win;
  650 
  651         win = sbspace(&so->so_rcv);
  652         if (win < 0)
  653                 win = 0;
  654         tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
  655         }
  656 
  657         switch (tp->t_state) {
  658 
  659         /*
  660          * If the state is LISTEN then ignore segment if it contains an RST.
  661          * If the segment contains an ACK then it is bad and send a RST.
  662          * If it does not contain a SYN then it is not interesting; drop it.
  663          * If it is from this socket, drop it, it must be forged.
  664          * Don't bother responding if the destination was a broadcast.
  665          * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
  666          * tp->iss, and send a segment:
  667          *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
  668          * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
  669          * Fill in remote peer address fields if not previously specified.
  670          * Enter SYN_RECEIVED state, and process any other fields of this
  671          * segment in this state.
  672          */
  673         case TCPS_LISTEN: {
  674                 register struct sockaddr_in *sin;
  675 
  676                 if (tiflags & TH_RST)
  677                         goto drop;
  678                 if (tiflags & TH_ACK)
  679                         goto maybedropwithreset;
  680                 if ((tiflags & TH_SYN) == 0)
  681                         goto drop;
  682                 if ((ti->ti_dport == ti->ti_sport) &&
  683                     (ti->ti_dst.s_addr == ti->ti_src.s_addr))
  684                         goto drop;
  685                 /*
  686                  * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
  687                  * in_broadcast() should never return true on a received
  688                  * packet with M_BCAST not set.
  689                  */
  690                 if (m->m_flags & (M_BCAST|M_MCAST) ||
  691                     IN_MULTICAST(ntohl(ti->ti_src.s_addr)) ||
  692                     IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
  693                         goto drop;
  694                 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
  695                        M_NOWAIT);
  696                 if (sin == NULL)
  697                         goto drop;
  698                 sin->sin_family = AF_INET;
  699                 sin->sin_len = sizeof(*sin);
  700                 sin->sin_addr = ti->ti_src;
  701                 sin->sin_port = ti->ti_sport;
  702                 bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
  703                 laddr = inp->inp_laddr;
  704                 if (inp->inp_laddr.s_addr == INADDR_ANY)
  705                         inp->inp_laddr = ti->ti_dst;
  706                 if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) {
  707                         inp->inp_laddr = laddr;
  708                         FREE(sin, M_SONAME);
  709                         goto drop;
  710                 }
  711                 FREE(sin, M_SONAME);
  712                 tp->t_template = tcp_template(tp);
  713                 if (tp->t_template == 0) {
  714                         tp = tcp_drop(tp, ENOBUFS);
  715                         dropsocket = 0;         /* socket is already gone */
  716                         goto drop;
  717                 }
  718                 if ((taop = tcp_gettaocache(inp)) == NULL) {
  719                         taop = &tao_noncached;
  720                         bzero(taop, sizeof(*taop));
  721                 }
  722                 tcp_dooptions(tp, optp, optlen, ti, &to);
  723                 if (iss)
  724                         tp->iss = iss;
  725                 else {
  726 #ifdef TCP_COMPAT_42
  727                         tcp_iss += TCP_ISSINCR/2;
  728                         tp->iss = tcp_iss;
  729 #else
  730                         tp->iss = tcp_rndiss_next();
  731 #endif /* TCP_COMPAT_42 */
  732                 }
  733                 tp->irs = ti->ti_seq;
  734                 tcp_sendseqinit(tp);
  735                 tcp_rcvseqinit(tp);
  736                 /*
  737                  * Initialization of the tcpcb for transaction;
  738                  *   set SND.WND = SEG.WND,
  739                  *   initialize CCsend and CCrecv.
  740                  */
  741                 tp->snd_wnd = tiwin;    /* initial send-window */
  742                 tp->cc_send = CC_INC(tcp_ccgen);
  743                 tp->cc_recv = to.to_cc;
  744                 /*
  745                  * Perform TAO test on incoming CC (SEG.CC) option, if any.
  746                  * - compare SEG.CC against cached CC from the same host,
  747                  *      if any.
  748                  * - if SEG.CC > chached value, SYN must be new and is accepted
  749                  *      immediately: save new CC in the cache, mark the socket
  750                  *      connected, enter ESTABLISHED state, turn on flag to
  751                  *      send a SYN in the next segment.
  752                  *      A virtual advertised window is set in rcv_adv to
  753                  *      initialize SWS prevention.  Then enter normal segment
  754                  *      processing: drop SYN, process data and FIN.
  755                  * - otherwise do a normal 3-way handshake.
  756                  */
  757                 if ((to.to_flag & TOF_CC) != 0) {
  758                     if (((tp->t_flags & TF_NOPUSH) != 0) &&
  759                         taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
  760 
  761                         taop->tao_cc = to.to_cc;
  762                         tp->t_state = TCPS_ESTABLISHED;
  763 
  764                         /*
  765                          * If there is a FIN, or if there is data and the
  766                          * connection is local, then delay SYN,ACK(SYN) in
  767                          * the hope of piggy-backing it on a response
  768                          * segment.  Otherwise must send ACK now in case
  769                          * the other side is slow starting.
  770                          */
  771                         if (tcp_delack_enabled && ((tiflags & TH_FIN) || (ti->ti_len != 0 &&
  772                             in_localaddr(inp->inp_faddr))))
  773                                 tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
  774                         else
  775                                 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
  776 
  777                         /*
  778                          * Limit the `virtual advertised window' to TCP_MAXWIN
  779                          * here.  Even if we requested window scaling, it will
  780                          * become effective only later when our SYN is acked.
  781                          */
  782                         tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
  783                         tcpstat.tcps_connects++;
  784                         soisconnected(so);
  785                         tp->t_timer[TCPT_KEEP] = tcp_keepinit;
  786                         dropsocket = 0;         /* committed to socket */
  787                         tcpstat.tcps_accepts++;
  788                         goto trimthenstep6;
  789                     }
  790                 /* else do standard 3-way handshake */
  791                 } else {
  792                     /*
  793                      * No CC option, but maybe CC.NEW:
  794                      *   invalidate cached value.
  795                      */
  796                      taop->tao_cc = 0;
  797                 }
  798                 /*
  799                  * TAO test failed or there was no CC option,
  800                  *    do a standard 3-way handshake.
  801                  */
  802                 tp->t_flags |= TF_ACKNOW;
  803                 tp->t_state = TCPS_SYN_RECEIVED;
  804                 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
  805                 dropsocket = 0;         /* committed to socket */
  806                 tcpstat.tcps_accepts++;
  807                 goto trimthenstep6;
  808                 }
  809 
  810         /*
  811          * If the state is SYN_RECEIVED:
  812          *      if seg contains an ACK, but not for our SYN/ACK, send a RST.
  813          */
  814         case TCPS_SYN_RECEIVED:
  815                 if ((tiflags & TH_ACK) &&
  816                     (SEQ_LEQ(ti->ti_ack, tp->snd_una) ||
  817                      SEQ_GT(ti->ti_ack, tp->snd_max)))
  818                                 goto maybedropwithreset;
  819                 break;
  820 
  821         /*
  822          * If the state is SYN_SENT:
  823          *      if seg contains an ACK, but not for our SYN, drop the input.
  824          *      if seg contains a RST, then drop the connection.
  825          *      if seg does not contain SYN, then drop it.
  826          * Otherwise this is an acceptable SYN segment
  827          *      initialize tp->rcv_nxt and tp->irs
  828          *      if seg contains ack then advance tp->snd_una
  829          *      if SYN has been acked change to ESTABLISHED else SYN_RCVD state
  830          *      arrange for segment to be acked (eventually)
  831          *      continue processing rest of data/controls, beginning with URG
  832          */
  833         case TCPS_SYN_SENT:
  834                 if ((taop = tcp_gettaocache(inp)) == NULL) {
  835                         taop = &tao_noncached;
  836                         bzero(taop, sizeof(*taop));
  837                 }
  838 
  839                 if ((tiflags & TH_ACK) &&
  840                     (SEQ_LEQ(ti->ti_ack, tp->iss) ||
  841                      SEQ_GT(ti->ti_ack, tp->snd_max))) {
  842                         /*
  843                          * If we have a cached CCsent for the remote host,
  844                          * hence we haven't just crashed and restarted,
  845                          * do not send a RST.  This may be a retransmission
  846                          * from the other side after our earlier ACK was lost.
  847                          * Our new SYN, when it arrives, will serve as the
  848                          * needed ACK.
  849                          */
  850                         if (taop->tao_ccsent != 0)
  851                                 goto drop;
  852                         else
  853                                 goto dropwithreset;
  854                 }
  855                 if (tiflags & TH_RST) {
  856                         if (tiflags & TH_ACK)
  857                                 tp = tcp_drop(tp, ECONNREFUSED);
  858                         goto drop;
  859                 }
  860                 if ((tiflags & TH_SYN) == 0)
  861                         goto drop;
  862                 tp->snd_wnd = ti->ti_win;       /* initial send window */
  863                 tp->cc_recv = to.to_cc;         /* foreign CC */
  864 
  865                 tp->irs = ti->ti_seq;
  866                 tcp_rcvseqinit(tp);
  867                 if (tiflags & TH_ACK) {
  868                         /*
  869                          * Our SYN was acked.  If segment contains CC.ECHO
  870                          * option, check it to make sure this segment really
  871                          * matches our SYN.  If not, just drop it as old
  872                          * duplicate, but send an RST if we're still playing
  873                          * by the old rules.  If no CC.ECHO option, make sure
  874                          * we don't get fooled into using T/TCP.
  875                          */
  876                         if (to.to_flag & TOF_CCECHO) {
  877                                 if (tp->cc_send != to.to_ccecho)
  878                                         if (taop->tao_ccsent != 0)
  879                                                 goto drop;
  880                                         else
  881                                                 goto dropwithreset;
  882                         } else
  883                                 tp->t_flags &= ~TF_RCVD_CC;
  884                         tcpstat.tcps_connects++;
  885                         soisconnected(so);
  886                         /* Do window scaling on this connection? */
  887                         if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
  888                                 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
  889                                 tp->snd_scale = tp->requested_s_scale;
  890                                 tp->rcv_scale = tp->request_r_scale;
  891                         }
  892                         /* Segment is acceptable, update cache if undefined. */
  893                         if (taop->tao_ccsent == 0)
  894                                 taop->tao_ccsent = to.to_ccecho;
  895 
  896                         tp->rcv_adv += tp->rcv_wnd;
  897                         tp->snd_una++;          /* SYN is acked */
  898                         /*
  899                          * If there's data, delay ACK; if there's also a FIN
  900                          * ACKNOW will be turned on later.
  901                          */
  902                         if (tcp_delack_enabled && ti->ti_len != 0)
  903                                 tp->t_flags |= TF_DELACK;
  904                         else
  905                                 tp->t_flags |= TF_ACKNOW;
  906                         /*
  907                          * Received <SYN,ACK> in SYN_SENT[*] state.
  908                          * Transitions:
  909                          *      SYN_SENT  --> ESTABLISHED
  910                          *      SYN_SENT* --> FIN_WAIT_1
  911                          */
  912                         if (tp->t_flags & TF_NEEDFIN) {
  913                                 tp->t_state = TCPS_FIN_WAIT_1;
  914                                 tp->t_flags &= ~TF_NEEDFIN;
  915                                 tiflags &= ~TH_SYN;
  916                         } else {
  917                                 tp->t_state = TCPS_ESTABLISHED;
  918                                 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
  919                         }
  920                 } else {
  921                 /*
  922                  *  Received initial SYN in SYN-SENT[*] state => simul-
  923                  *  taneous open.  If segment contains CC option and there is
  924                  *  a cached CC, apply TAO test; if it succeeds, connection is
  925                  *  half-synchronized.  Otherwise, do 3-way handshake:
  926                  *        SYN-SENT -> SYN-RECEIVED
  927                  *        SYN-SENT* -> SYN-RECEIVED*
  928                  *  If there was no CC option, clear cached CC value.
  929                  */
  930                         tp->t_flags |= TF_ACKNOW;
  931                         tp->t_timer[TCPT_REXMT] = 0;
  932                         if (to.to_flag & TOF_CC) {
  933                                 if (taop->tao_cc != 0 &&
  934                                     CC_GT(to.to_cc, taop->tao_cc)) {
  935                                         /*
  936                                          * update cache and make transition:
  937                                          *        SYN-SENT -> ESTABLISHED*
  938                                          *        SYN-SENT* -> FIN-WAIT-1*
  939                                          */
  940                                         taop->tao_cc = to.to_cc;
  941                                         if (tp->t_flags & TF_NEEDFIN) {
  942                                                 tp->t_state = TCPS_FIN_WAIT_1;
  943                                                 tp->t_flags &= ~TF_NEEDFIN;
  944                                         } else {
  945                                                 tp->t_state = TCPS_ESTABLISHED;
  946                                                 tp->t_timer[TCPT_KEEP] = tcp_keepidle;
  947                                         }
  948                                         tp->t_flags |= TF_NEEDSYN;
  949                                 } else
  950                                         tp->t_state = TCPS_SYN_RECEIVED;
  951                         } else {
  952                                 /* CC.NEW or no option => invalidate cache */
  953                                 taop->tao_cc = 0;
  954                                 tp->t_state = TCPS_SYN_RECEIVED;
  955                         }
  956                 }
  957 
  958 trimthenstep6:
  959                 /*
  960                  * Advance ti->ti_seq to correspond to first data byte.
  961                  * If data, trim to stay within window,
  962                  * dropping FIN if necessary.
  963                  */
  964                 ti->ti_seq++;
  965                 if (ti->ti_len > tp->rcv_wnd) {
  966                         todrop = ti->ti_len - tp->rcv_wnd;
  967                         m_adj(m, -todrop);
  968                         ti->ti_len = tp->rcv_wnd;
  969                         tiflags &= ~TH_FIN;
  970                         tcpstat.tcps_rcvpackafterwin++;
  971                         tcpstat.tcps_rcvbyteafterwin += todrop;
  972                 }
  973                 tp->snd_wl1 = ti->ti_seq - 1;
  974                 tp->rcv_up = ti->ti_seq;
  975                 /*
  976                  *  Client side of transaction: already sent SYN and data.
  977                  *  If the remote host used T/TCP to validate the SYN,
  978                  *  our data will be ACK'd; if so, enter normal data segment
  979                  *  processing in the middle of step 5, ack processing.
  980                  *  Otherwise, goto step 6.
  981                  */
  982                 if (tiflags & TH_ACK)
  983                         goto process_ACK;
  984                 goto step6;
  985         /*
  986          * If the state is LAST_ACK or CLOSING or TIME_WAIT:
  987          *      if segment contains a SYN and CC [not CC.NEW] option:
  988          *              if state == TIME_WAIT and connection duration > MSL,
  989          *                  drop packet and send RST;
  990          *
  991          *              if SEG.CC > CCrecv then is new SYN, and can implicitly
  992          *                  ack the FIN (and data) in retransmission queue.
  993          *                  Complete close and delete TCPCB.  Then reprocess
  994          *                  segment, hoping to find new TCPCB in LISTEN state;
  995          *
  996          *              else must be old SYN; drop it.
  997          *      else do normal processing.
  998          */
  999         case TCPS_LAST_ACK:
 1000         case TCPS_CLOSING:
 1001         case TCPS_TIME_WAIT:
 1002                 if ((tiflags & TH_SYN) &&
 1003                     (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
 1004                         if (tp->t_state == TCPS_TIME_WAIT &&
 1005                                         tp->t_duration > TCPTV_MSL)
 1006                                 goto dropwithreset;
 1007                         if (CC_GT(to.to_cc, tp->cc_recv)) {
 1008                                 tp = tcp_close(tp);
 1009                                 goto findpcb;
 1010                         }
 1011                         else
 1012                                 goto drop;
 1013                 }
 1014                 break;  /* continue normal processing */
 1015         }
 1016 
 1017         /*
 1018          * States other than LISTEN or SYN_SENT.
 1019          * First check the RST flag and sequence number since reset segments
 1020          * are exempt from the timestamp and connection count tests.  This
 1021          * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
 1022          * below which allowed reset segments in half the sequence space
 1023          * to fall though and be processed (which gives forged reset
 1024          * segments with a random sequence number a 50 percent chance of
 1025          * killing a connection).
 1026          * Then check timestamp, if present.
 1027          * Then check the connection count, if present.
 1028          * Then check that at least some bytes of segment are within
 1029          * receive window.  If segment begins before rcv_nxt,
 1030          * drop leading data (and SYN); if nothing left, just ack.
 1031          *
 1032          *
 1033          * If the RST bit is set, check the sequence number to see
 1034          * if this is a valid reset segment.
 1035          * RFC 793 page 37:
 1036          *   In all states except SYN-SENT, all reset (RST) segments
 1037          *   are validated by checking their SEQ-fields.  A reset is
 1038          *   valid if its sequence number is in the window.
 1039          * Note: this does not take into account delayed ACKs, so
 1040          *   we should test against last_ack_sent instead of rcv_nxt.
 1041          *   Also, it does not make sense to allow reset segments with
 1042          *   sequence numbers greater than last_ack_sent to be processed
 1043          *   since these sequence numbers are just the acknowledgement
 1044          *   numbers in our outgoing packets being echoed back at us,
 1045          *   and these acknowledgement numbers are monotonically
 1046          *   increasing.
 1047          * If we have multiple segments in flight, the intial reset
 1048          * segment sequence numbers will be to the left of last_ack_sent,
 1049          * but they will eventually catch up.
 1050          * In any case, it never made sense to trim reset segments to
 1051          * fit the receive window since RFC 1122 says:
 1052          *   4.2.2.12  RST Segment: RFC-793 Section 3.4
 1053          *
 1054          *    A TCP SHOULD allow a received RST segment to include data.
 1055          *
 1056          *    DISCUSSION
 1057          *         It has been suggested that a RST segment could contain
 1058          *         ASCII text that encoded and explained the cause of the
 1059          *         RST.  No standard has yet been established for such
 1060          *         data.
 1061          *
 1062          * If the reset segment passes the sequence number test examine
 1063          * the state:
 1064          *    SYN_RECEIVED STATE:
 1065          *      If passive open, return to LISTEN state.
 1066          *      If active open, inform user that connection was refused.
 1067          *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
 1068          *      Inform user that connection was reset, and close tcb.
 1069          *    CLOSING, LAST_ACK, TIME_WAIT STATES
 1070          *      Close the tcb.
 1071          *    TIME_WAIT state:
 1072          *      Drop the segment - see Stevens, vol. 2, p. 964 and
 1073          *      RFC 1337.
 1074          */
 1075         if (tiflags & TH_RST) {
 1076                 if (tp->last_ack_sent == ti->ti_seq) {
 1077                         switch (tp->t_state) {
 1078 
 1079                         case TCPS_SYN_RECEIVED:
 1080                                 so->so_error = ECONNREFUSED;
 1081                                 goto close;
 1082 
 1083                         case TCPS_ESTABLISHED:
 1084                         case TCPS_FIN_WAIT_1:
 1085                         case TCPS_FIN_WAIT_2:
 1086                         case TCPS_CLOSE_WAIT:
 1087                                 so->so_error = ECONNRESET;
 1088                         close:
 1089                                 tp->t_state = TCPS_CLOSED;
 1090                                 tcpstat.tcps_drops++;
 1091                                 tp = tcp_close(tp);
 1092                                 break;
 1093 
 1094                         case TCPS_CLOSING:
 1095                         case TCPS_LAST_ACK:
 1096                                 tp = tcp_close(tp);
 1097                                 break;
 1098 
 1099                         case TCPS_TIME_WAIT:
 1100                                 break;
 1101                         }
 1102                 }
 1103                 goto drop;
 1104         }
 1105 
 1106         /*
 1107          * RFC 1323 PAWS: If we have a timestamp reply on this segment
 1108          * and it's less than ts_recent, drop it.
 1109          */
 1110         if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
 1111             TSTMP_LT(to.to_tsval, tp->ts_recent)) {
 1112 
 1113                 /* Check to see if ts_recent is over 24 days old.  */
 1114                 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
 1115                         /*
 1116                          * Invalidate ts_recent.  If this segment updates
 1117                          * ts_recent, the age will be reset later and ts_recent
 1118                          * will get a valid value.  If it does not, setting
 1119                          * ts_recent to zero will at least satisfy the
 1120                          * requirement that zero be placed in the timestamp
 1121                          * echo reply when ts_recent isn't valid.  The
 1122                          * age isn't reset until we get a valid ts_recent
 1123                          * because we don't want out-of-order segments to be
 1124                          * dropped when ts_recent is old.
 1125                          */
 1126                         tp->ts_recent = 0;
 1127                 } else {
 1128                         tcpstat.tcps_rcvduppack++;
 1129                         tcpstat.tcps_rcvdupbyte += ti->ti_len;
 1130                         tcpstat.tcps_pawsdrop++;
 1131                         goto dropafterack;
 1132                 }
 1133         }
 1134 
 1135         /*
 1136          * T/TCP mechanism
 1137          *   If T/TCP was negotiated and the segment doesn't have CC,
 1138          *   or if its CC is wrong then drop the segment.
 1139          *   RST segments do not have to comply with this.
 1140          */
 1141         if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
 1142             ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
 1143                 goto dropafterack;
 1144 
 1145         /*
 1146          * In the SYN-RECEIVED state, validate that the packet belongs to
 1147          * this connection before trimming the data to fit the receive
 1148          * window.  Check the sequence number versus IRS since we know
 1149          * the sequence numbers haven't wrapped.  This is a partial fix
 1150          * for the "LAND" DoS attack.
 1151          */
 1152         if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(ti->ti_seq, tp->irs))
 1153                 goto dropwithreset;
 1154 
 1155         todrop = tp->rcv_nxt - ti->ti_seq;
 1156         if (todrop > 0) {
 1157                 if (tiflags & TH_SYN) {
 1158                         tiflags &= ~TH_SYN;
 1159                         ti->ti_seq++;
 1160                         if (ti->ti_urp > 1)
 1161                                 ti->ti_urp--;
 1162                         else
 1163                                 tiflags &= ~TH_URG;
 1164                         todrop--;
 1165                 }
 1166                 /*
 1167                  * Following if statement from Stevens, vol. 2, p. 960.
 1168                  */
 1169                 if (todrop > ti->ti_len
 1170                     || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
 1171                         /*
 1172                          * Any valid FIN must be to the left of the window.
 1173                          * At this point the FIN must be a duplicate or out
 1174                          * of sequence; drop it.
 1175                          */
 1176                         tiflags &= ~TH_FIN;
 1177 
 1178                         /*
 1179                          * Send an ACK to resynchronize and drop any data.
 1180                          * But keep on processing for RST or ACK.
 1181                          */
 1182                         tp->t_flags |= TF_ACKNOW;
 1183                         todrop = ti->ti_len;
 1184                         tcpstat.tcps_rcvduppack++;
 1185                         tcpstat.tcps_rcvdupbyte += todrop;
 1186                 } else {
 1187                         tcpstat.tcps_rcvpartduppack++;
 1188                         tcpstat.tcps_rcvpartdupbyte += todrop;
 1189                 }
 1190                 m_adj(m, todrop);
 1191                 ti->ti_seq += todrop;
 1192                 ti->ti_len -= todrop;
 1193                 if (ti->ti_urp > todrop)
 1194                         ti->ti_urp -= todrop;
 1195                 else {
 1196                         tiflags &= ~TH_URG;
 1197                         ti->ti_urp = 0;
 1198                 }
 1199         }
 1200 
 1201         /*
 1202          * If new data are received on a connection after the
 1203          * user processes are gone, then RST the other end.
 1204          */
 1205         if ((so->so_state & SS_NOFDREF) &&
 1206             tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
 1207                 tp = tcp_close(tp);
 1208                 tcpstat.tcps_rcvafterclose++;
 1209                 goto dropwithreset;
 1210         }
 1211 
 1212         /*
 1213          * If segment ends after window, drop trailing data
 1214          * (and PUSH and FIN); if nothing left, just ACK.
 1215          */
 1216         todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
 1217         if (todrop > 0) {
 1218                 tcpstat.tcps_rcvpackafterwin++;
 1219                 if (todrop >= ti->ti_len) {
 1220                         tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
 1221                         /*
 1222                          * If a new connection request is received
 1223                          * while in TIME_WAIT, drop the old connection
 1224                          * and start over if the sequence numbers
 1225                          * are above the previous ones.
 1226                          */
 1227                         if (tiflags & TH_SYN &&
 1228                             tp->t_state == TCPS_TIME_WAIT &&
 1229                             SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
 1230 #ifdef TCP_COMPAT_42
 1231                                 iss = tp->snd_nxt + TCP_ISSINCR;
 1232 #else
 1233                                 iss = tcp_rndiss_next();
 1234 #endif /* TCP_COMPAT_42 */
 1235                                 tp = tcp_close(tp);
 1236                                 goto findpcb;
 1237                         }
 1238                         /*
 1239                          * If window is closed can only take segments at
 1240                          * window edge, and have to drop data and PUSH from
 1241                          * incoming segments.  Continue processing, but
 1242                          * remember to ack.  Otherwise, drop segment
 1243                          * and ack.
 1244                          */
 1245                         if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
 1246                                 tp->t_flags |= TF_ACKNOW;
 1247                                 tcpstat.tcps_rcvwinprobe++;
 1248                         } else
 1249                                 goto dropafterack;
 1250                 } else
 1251                         tcpstat.tcps_rcvbyteafterwin += todrop;
 1252                 m_adj(m, -todrop);
 1253                 ti->ti_len -= todrop;
 1254                 tiflags &= ~(TH_PUSH|TH_FIN);
 1255         }
 1256 
 1257         /*
 1258          * If last ACK falls within this segment's sequence numbers,
 1259          * record its timestamp.
 1260          * NOTE that the test is modified according to the latest
 1261          * proposal of the tcplw@cray.com list (Braden 1993/04/26).
 1262          */
 1263         if ((to.to_flag & TOF_TS) != 0 &&
 1264             SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
 1265                 tp->ts_recent_age = tcp_now;
 1266                 tp->ts_recent = to.to_tsval;
 1267         }
 1268 
 1269         /*
 1270          * If a SYN is in the window, then this is an
 1271          * error and we send an RST and drop the connection.
 1272          */
 1273         if (tiflags & TH_SYN) {
 1274                 tp = tcp_drop(tp, ECONNRESET);
 1275                 goto dropwithreset;
 1276         }
 1277 
 1278         /*
 1279          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
 1280          * flag is on (half-synchronized state), then queue data for
 1281          * later processing; else drop segment and return.
 1282          */
 1283         if ((tiflags & TH_ACK) == 0) {
 1284                 if (tp->t_state == TCPS_SYN_RECEIVED ||
 1285                     (tp->t_flags & TF_NEEDSYN))
 1286                         goto step6;
 1287                 else
 1288                         goto drop;
 1289         }
 1290 
 1291         /*
 1292          * Ack processing.
 1293          */
 1294         switch (tp->t_state) {
 1295 
 1296         /*
 1297          * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
 1298          * ESTABLISHED state and continue processing.
 1299          * The ACK was checked above.
 1300          */
 1301         case TCPS_SYN_RECEIVED:
 1302 
 1303                 tcpstat.tcps_connects++;
 1304                 soisconnected(so);
 1305                 /* Do window scaling? */
 1306                 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
 1307                         (TF_RCVD_SCALE|TF_REQ_SCALE)) {
 1308                         tp->snd_scale = tp->requested_s_scale;
 1309                         tp->rcv_scale = tp->request_r_scale;
 1310                 }
 1311                 /*
 1312                  * Upon successful completion of 3-way handshake,
 1313                  * update cache.CC if it was undefined, pass any queued
 1314                  * data to the user, and advance state appropriately.
 1315                  */
 1316                 if ((taop = tcp_gettaocache(inp)) != NULL &&
 1317                     taop->tao_cc == 0)
 1318                         taop->tao_cc = tp->cc_recv;
 1319 
 1320                 /*
 1321                  * Make transitions:
 1322                  *      SYN-RECEIVED  -> ESTABLISHED
 1323                  *      SYN-RECEIVED* -> FIN-WAIT-1
 1324                  */
 1325                 if (tp->t_flags & TF_NEEDFIN) {
 1326                         tp->t_state = TCPS_FIN_WAIT_1;
 1327                         tp->t_flags &= ~TF_NEEDFIN;
 1328                 } else {
 1329                         tp->t_state = TCPS_ESTABLISHED;
 1330                         tp->t_timer[TCPT_KEEP] = tcp_keepidle;
 1331                 }
 1332                 /*
 1333                  * If segment contains data or ACK, will call tcp_reass()
 1334                  * later; if not, do so now to pass queued data to user.
 1335                  */
 1336                 if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0)
 1337                         (void) tcp_reass(tp, (struct tcpiphdr *)0,
 1338                             (struct mbuf *)0);
 1339                 tp->snd_wl1 = ti->ti_seq - 1;
 1340                 /* fall into ... */
 1341 
 1342         /*
 1343          * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
 1344          * ACKs.  If the ack is in the range
 1345          *      tp->snd_una < ti->ti_ack <= tp->snd_max
 1346          * then advance tp->snd_una to ti->ti_ack and drop
 1347          * data from the retransmission queue.  If this ACK reflects
 1348          * more up to date window information we update our window information.
 1349          */
 1350         case TCPS_ESTABLISHED:
 1351         case TCPS_FIN_WAIT_1:
 1352         case TCPS_FIN_WAIT_2:
 1353         case TCPS_CLOSE_WAIT:
 1354         case TCPS_CLOSING:
 1355         case TCPS_LAST_ACK:
 1356         case TCPS_TIME_WAIT:
 1357 
 1358                 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
 1359                         if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
 1360                                 tcpstat.tcps_rcvdupack++;
 1361                                 /*
 1362                                  * If we have outstanding data (other than
 1363                                  * a window probe), this is a completely
 1364                                  * duplicate ack (ie, window info didn't
 1365                                  * change), the ack is the biggest we've
 1366                                  * seen and we've seen exactly our rexmt
 1367                                  * threshhold of them, assume a packet
 1368                                  * has been dropped and retransmit it.
 1369                                  * Kludge snd_nxt & the congestion
 1370                                  * window so we send only this one
 1371                                  * packet.
 1372                                  *
 1373                                  * We know we're losing at the current
 1374                                  * window size so do congestion avoidance
 1375                                  * (set ssthresh to half the current window
 1376                                  * and pull our congestion window back to
 1377                                  * the new ssthresh).
 1378                                  *
 1379                                  * Dup acks mean that packets have left the
 1380                                  * network (they're now cached at the receiver)
 1381                                  * so bump cwnd by the amount in the receiver
 1382                                  * to keep a constant cwnd packets in the
 1383                                  * network.
 1384                                  */
 1385                                 if (tp->t_timer[TCPT_REXMT] == 0 ||
 1386                                     ti->ti_ack != tp->snd_una)
 1387                                         tp->t_dupacks = 0;
 1388                                 else if (++tp->t_dupacks == tcprexmtthresh) {
 1389                                         tcp_seq onxt = tp->snd_nxt;
 1390                                         u_int win =
 1391                                             min(tp->snd_wnd, tp->snd_cwnd) / 2 /
 1392                                                 tp->t_maxseg;
 1393 
 1394                                         if (win < 2)
 1395                                                 win = 2;
 1396                                         tp->snd_ssthresh = win * tp->t_maxseg;
 1397                                         tp->t_timer[TCPT_REXMT] = 0;
 1398                                         tp->t_rtt = 0;
 1399                                         tp->snd_nxt = ti->ti_ack;
 1400                                         tp->snd_cwnd = tp->t_maxseg;
 1401                                         (void) tcp_output(tp);
 1402                                         tp->snd_cwnd = tp->snd_ssthresh +
 1403                                                tp->t_maxseg * tp->t_dupacks;
 1404                                         if (SEQ_GT(onxt, tp->snd_nxt))
 1405                                                 tp->snd_nxt = onxt;
 1406                                         goto drop;
 1407                                 } else if (tp->t_dupacks > tcprexmtthresh) {
 1408                                         tp->snd_cwnd += tp->t_maxseg;
 1409                                         (void) tcp_output(tp);
 1410                                         goto drop;
 1411                                 }
 1412                         } else
 1413                                 tp->t_dupacks = 0;
 1414                         break;
 1415                 }
 1416                 /*
 1417                  * If the congestion window was inflated to account
 1418                  * for the other side's cached packets, retract it.
 1419                  */
 1420                 if (tp->t_dupacks >= tcprexmtthresh &&
 1421                     tp->snd_cwnd > tp->snd_ssthresh)
 1422                         tp->snd_cwnd = tp->snd_ssthresh;
 1423                 tp->t_dupacks = 0;
 1424                 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
 1425                         tcpstat.tcps_rcvacktoomuch++;
 1426                         goto dropafterack;
 1427                 }
 1428                 /*
 1429                  *  If we reach this point, ACK is not a duplicate,
 1430                  *     i.e., it ACKs something we sent.
 1431                  */
 1432                 if (tp->t_flags & TF_NEEDSYN) {
 1433                         /*
 1434                          * T/TCP: Connection was half-synchronized, and our
 1435                          * SYN has been ACK'd (so connection is now fully
 1436                          * synchronized).  Go to non-starred state,
 1437                          * increment snd_una for ACK of SYN, and check if
 1438                          * we can do window scaling.
 1439                          */
 1440                         tp->t_flags &= ~TF_NEEDSYN;
 1441                         tp->snd_una++;
 1442                         /* Do window scaling? */
 1443                         if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
 1444                                 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
 1445                                 tp->snd_scale = tp->requested_s_scale;
 1446                                 tp->rcv_scale = tp->request_r_scale;
 1447                         }
 1448                 }
 1449 
 1450 process_ACK:
 1451                 acked = ti->ti_ack - tp->snd_una;
 1452                 tcpstat.tcps_rcvackpack++;
 1453                 tcpstat.tcps_rcvackbyte += acked;
 1454 
 1455                 /*
 1456                  * If we have a timestamp reply, update smoothed
 1457                  * round trip time.  If no timestamp is present but
 1458                  * transmit timer is running and timed sequence
 1459                  * number was acked, update smoothed round trip time.
 1460                  * Since we now have an rtt measurement, cancel the
 1461                  * timer backoff (cf., Phil Karn's retransmit alg.).
 1462                  * Recompute the initial retransmit timer.
 1463                  */
 1464                 if (to.to_flag & TOF_TS)
 1465                         tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
 1466                 else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
 1467                         tcp_xmit_timer(tp,tp->t_rtt);
 1468 
 1469                 /*
 1470                  * If all outstanding data is acked, stop retransmit
 1471                  * timer and remember to restart (more output or persist).
 1472                  * If there is more data to be acked, restart retransmit
 1473                  * timer, using current (possibly backed-off) value.
 1474                  */
 1475                 if (ti->ti_ack == tp->snd_max) {
 1476                         tp->t_timer[TCPT_REXMT] = 0;
 1477                         needoutput = 1;
 1478                 } else if (tp->t_timer[TCPT_PERSIST] == 0)
 1479                         tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
 1480 
 1481                 /*
 1482                  * If no data (only SYN) was ACK'd,
 1483                  *    skip rest of ACK processing.
 1484                  */
 1485                 if (acked == 0)
 1486                         goto step6;
 1487 
 1488                 /*
 1489                  * When new data is acked, open the congestion window.
 1490                  * If the window gives us less than ssthresh packets
 1491                  * in flight, open exponentially (maxseg per packet).
 1492                  * Otherwise open linearly: maxseg per window
 1493                  * (maxseg^2 / cwnd per packet).
 1494                  */
 1495                 {
 1496                 register u_int cw = tp->snd_cwnd;
 1497                 register u_int incr = tp->t_maxseg;
 1498 
 1499                 if (cw > tp->snd_ssthresh)
 1500                         incr = incr * incr / cw;
 1501                 tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
 1502                 }
 1503                 if (acked > so->so_snd.sb_cc) {
 1504                         tp->snd_wnd -= so->so_snd.sb_cc;
 1505                         sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
 1506                         ourfinisacked = 1;
 1507                 } else {
 1508                         sbdrop(&so->so_snd, acked);
 1509                         tp->snd_wnd -= acked;
 1510                         ourfinisacked = 0;
 1511                 }
 1512                 sowwakeup(so);
 1513                 tp->snd_una = ti->ti_ack;
 1514                 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
 1515                         tp->snd_nxt = tp->snd_una;
 1516 
 1517                 switch (tp->t_state) {
 1518 
 1519                 /*
 1520                  * In FIN_WAIT_1 STATE in addition to the processing
 1521                  * for the ESTABLISHED state if our FIN is now acknowledged
 1522                  * then enter FIN_WAIT_2.
 1523                  */
 1524                 case TCPS_FIN_WAIT_1:
 1525                         if (ourfinisacked) {
 1526                                 /*
 1527                                  * If we can't receive any more
 1528                                  * data, then closing user can proceed.
 1529                                  * Starting the timer is contrary to the
 1530                                  * specification, but if we don't get a FIN
 1531                                  * we'll hang forever.
 1532                                  */
 1533                                 if (so->so_state & SS_CANTRCVMORE) {
 1534                                         soisdisconnected(so);
 1535                                         tp->t_timer[TCPT_2MSL] = tcp_maxidle;
 1536                                 }
 1537                                 tp->t_state = TCPS_FIN_WAIT_2;
 1538                         }
 1539                         break;
 1540 
 1541                 /*
 1542                  * In CLOSING STATE in addition to the processing for
 1543                  * the ESTABLISHED state if the ACK acknowledges our FIN
 1544                  * then enter the TIME-WAIT state, otherwise ignore
 1545                  * the segment.
 1546                  */
 1547                 case TCPS_CLOSING:
 1548                         if (ourfinisacked) {
 1549                                 tp->t_state = TCPS_TIME_WAIT;
 1550                                 tcp_canceltimers(tp);
 1551                                 /* Shorten TIME_WAIT [RFC-1644, p.28] */
 1552                                 if (tp->cc_recv != 0 &&
 1553                                     tp->t_duration < TCPTV_MSL)
 1554                                         tp->t_timer[TCPT_2MSL] =
 1555                                             tp->t_rxtcur * TCPTV_TWTRUNC;
 1556                                 else
 1557                                         tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
 1558                                 soisdisconnected(so);
 1559                         }
 1560                         break;
 1561 
 1562                 /*
 1563                  * In LAST_ACK, we may still be waiting for data to drain
 1564                  * and/or to be acked, as well as for the ack of our FIN.
 1565                  * If our FIN is now acknowledged, delete the TCB,
 1566                  * enter the closed state and return.
 1567                  */
 1568                 case TCPS_LAST_ACK:
 1569                         if (ourfinisacked) {
 1570                                 tp = tcp_close(tp);
 1571                                 goto drop;
 1572                         }
 1573                         break;
 1574 
 1575                 /*
 1576                  * In TIME_WAIT state the only thing that should arrive
 1577                  * is a retransmission of the remote FIN.  Acknowledge
 1578                  * it and restart the finack timer.
 1579                  */
 1580                 case TCPS_TIME_WAIT:
 1581                         tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
 1582                         goto dropafterack;
 1583                 }
 1584         }
 1585 
 1586 step6:
 1587         /*
 1588          * Update window information.
 1589          * Don't look at window if no ACK: TAC's send garbage on first SYN.
 1590          */
 1591         if ((tiflags & TH_ACK) &&
 1592             (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
 1593             (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
 1594              (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
 1595                 /* keep track of pure window updates */
 1596                 if (ti->ti_len == 0 &&
 1597                     tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
 1598                         tcpstat.tcps_rcvwinupd++;
 1599                 tp->snd_wnd = tiwin;
 1600                 tp->snd_wl1 = ti->ti_seq;
 1601                 tp->snd_wl2 = ti->ti_ack;
 1602                 if (tp->snd_wnd > tp->max_sndwnd)
 1603                         tp->max_sndwnd = tp->snd_wnd;
 1604                 needoutput = 1;
 1605         }
 1606 
 1607         /*
 1608          * Process segments with URG.
 1609          */
 1610         if ((tiflags & TH_URG) && ti->ti_urp &&
 1611             TCPS_HAVERCVDFIN(tp->t_state) == 0) {
 1612                 /*
 1613                  * This is a kludge, but if we receive and accept
 1614                  * random urgent pointers, we'll crash in
 1615                  * soreceive.  It's hard to imagine someone
 1616                  * actually wanting to send this much urgent data.
 1617                  */
 1618                 if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
 1619                         ti->ti_urp = 0;                 /* XXX */
 1620                         tiflags &= ~TH_URG;             /* XXX */
 1621                         goto dodata;                    /* XXX */
 1622                 }
 1623                 /*
 1624                  * If this segment advances the known urgent pointer,
 1625                  * then mark the data stream.  This should not happen
 1626                  * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
 1627                  * a FIN has been received from the remote side.
 1628                  * In these states we ignore the URG.
 1629                  *
 1630                  * According to RFC961 (Assigned Protocols),
 1631                  * the urgent pointer points to the last octet
 1632                  * of urgent data.  We continue, however,
 1633                  * to consider it to indicate the first octet
 1634                  * of data past the urgent section as the original
 1635                  * spec states (in one of two places).
 1636                  */
 1637                 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
 1638                         tp->rcv_up = ti->ti_seq + ti->ti_urp;
 1639                         so->so_oobmark = so->so_rcv.sb_cc +
 1640                             (tp->rcv_up - tp->rcv_nxt) - 1;
 1641                         if (so->so_oobmark == 0)
 1642                                 so->so_state |= SS_RCVATMARK;
 1643                         sohasoutofband(so);
 1644                         tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
 1645                 }
 1646                 /*
 1647                  * Remove out of band data so doesn't get presented to user.
 1648                  * This can happen independent of advancing the URG pointer,
 1649                  * but if two URG's are pending at once, some out-of-band
 1650                  * data may creep in... ick.
 1651                  */
 1652                 if (ti->ti_urp <= (u_long)ti->ti_len
 1653 #ifdef SO_OOBINLINE
 1654                      && (so->so_options & SO_OOBINLINE) == 0
 1655 #endif
 1656                      )
 1657                         tcp_pulloutofband(so, ti, m);
 1658         } else
 1659                 /*
 1660                  * If no out of band data is expected,
 1661                  * pull receive urgent pointer along
 1662                  * with the receive window.
 1663                  */
 1664                 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
 1665                         tp->rcv_up = tp->rcv_nxt;
 1666 dodata:                                                 /* XXX */
 1667 
 1668         /*
 1669          * Process the segment text, merging it into the TCP sequencing queue,
 1670          * and arranging for acknowledgment of receipt if necessary.
 1671          * This process logically involves adjusting tp->rcv_wnd as data
 1672          * is presented to the user (this happens in tcp_usrreq.c,
 1673          * case PRU_RCVD).  If a FIN has already been received on this
 1674          * connection then we just ignore the text.
 1675          */
 1676         if ((ti->ti_len || (tiflags&TH_FIN)) &&
 1677             TCPS_HAVERCVDFIN(tp->t_state) == 0) {
 1678                 TCP_REASS(tp, ti, m, so, tiflags);
 1679                 /*
 1680                  * Note the amount of data that peer has sent into
 1681                  * our window, in order to estimate the sender's
 1682                  * buffer size.
 1683                  */
 1684                 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
 1685         } else {
 1686                 m_freem(m);
 1687                 tiflags &= ~TH_FIN;
 1688         }
 1689 
 1690         /*
 1691          * If FIN is received ACK the FIN and let the user know
 1692          * that the connection is closing.
 1693          */
 1694         if (tiflags & TH_FIN) {
 1695                 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
 1696                         socantrcvmore(so);
 1697                         /*
 1698                          *  If connection is half-synchronized
 1699                          *  (ie NEEDSYN flag on) then delay ACK,
 1700                          *  so it may be piggybacked when SYN is sent.
 1701                          *  Otherwise, since we received a FIN then no
 1702                          *  more input can be expected, send ACK now.
 1703                          */
 1704                         if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN))
 1705                                 tp->t_flags |= TF_DELACK;
 1706                         else
 1707                                 tp->t_flags |= TF_ACKNOW;
 1708                         tp->rcv_nxt++;
 1709                 }
 1710                 switch (tp->t_state) {
 1711 
 1712                 /*
 1713                  * In SYN_RECEIVED and ESTABLISHED STATES
 1714                  * enter the CLOSE_WAIT state.
 1715                  */
 1716                 case TCPS_SYN_RECEIVED:
 1717                 case TCPS_ESTABLISHED:
 1718                         tp->t_state = TCPS_CLOSE_WAIT;
 1719                         break;
 1720 
 1721                 /*
 1722                  * If still in FIN_WAIT_1 STATE FIN has not been acked so
 1723                  * enter the CLOSING state.
 1724                  */
 1725                 case TCPS_FIN_WAIT_1:
 1726                         tp->t_state = TCPS_CLOSING;
 1727                         break;
 1728 
 1729                 /*
 1730                  * In FIN_WAIT_2 state enter the TIME_WAIT state,
 1731                  * starting the time-wait timer, turning off the other
 1732                  * standard timers.
 1733                  */
 1734                 case TCPS_FIN_WAIT_2:
 1735                         tp->t_state = TCPS_TIME_WAIT;
 1736                         tcp_canceltimers(tp);
 1737                         /* Shorten TIME_WAIT [RFC-1644, p.28] */
 1738                         if (tp->cc_recv != 0 &&
 1739                             tp->t_duration < TCPTV_MSL) {
 1740                                 tp->t_timer[TCPT_2MSL] =
 1741                                     tp->t_rxtcur * TCPTV_TWTRUNC;
 1742                                 /* For transaction client, force ACK now. */
 1743                                 tp->t_flags |= TF_ACKNOW;
 1744                         }
 1745                         else
 1746                                 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
 1747                         soisdisconnected(so);
 1748                         break;
 1749 
 1750                 /*
 1751                  * In TIME_WAIT state restart the 2 MSL time_wait timer.
 1752                  */
 1753                 case TCPS_TIME_WAIT:
 1754                         tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
 1755                         break;
 1756                 }
 1757         }
 1758 #ifdef TCPDEBUG
 1759         if (so->so_options & SO_DEBUG)
 1760                 tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
 1761 #endif
 1762 
 1763         /*
 1764          * Return any desired output.
 1765          */
 1766         if (needoutput || (tp->t_flags & TF_ACKNOW))
 1767                 (void) tcp_output(tp);
 1768         return;
 1769 
 1770 dropafterack:
 1771         /*
 1772          * Generate an ACK dropping incoming segment if it occupies
 1773          * sequence space, where the ACK reflects our state.
 1774          *
 1775          * We can now skip the test for the RST flag since all
 1776          * paths to this code happen after packets containing
 1777          * RST have been dropped.
 1778          *
 1779          * In the SYN-RECEIVED state, don't send an ACK unless the
 1780          * segment we received passes the SYN-RECEIVED ACK test.
 1781          * If it fails send a RST.  This breaks the loop in the
 1782          * "LAND" DoS attack, and also prevents an ACK storm
 1783          * between two listening ports that have been sent forged
 1784          * SYN segments, each with the source address of the other.
 1785          */
 1786         if (tp->t_state == TCPS_SYN_RECEIVED && (tiflags & TH_ACK) &&
 1787             (SEQ_GT(tp->snd_una, ti->ti_ack) ||
 1788              SEQ_GT(ti->ti_ack, tp->snd_max)) )
 1789                 goto maybedropwithreset;
 1790 #ifdef TCPDEBUG
 1791         if (so->so_options & SO_DEBUG)
 1792                 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
 1793 #endif
 1794         m_freem(m);
 1795         tp->t_flags |= TF_ACKNOW;
 1796         (void) tcp_output(tp);
 1797         return;
 1798 
 1799         /*
 1800          * Conditionally drop with reset or just drop depending on whether
 1801          * we think we are under attack or not.
 1802          */
 1803 maybedropwithreset:
 1804 #ifdef ICMP_BANDLIM
 1805         if (badport_bandlim(1) < 0)
 1806             goto drop;
 1807 #endif
 1808         /* fall through */
 1809 dropwithreset:
 1810 #ifdef TCP_RESTRICT_RST
 1811         if (restrict_rst)
 1812                 goto drop;
 1813 #endif
 1814         /*
 1815          * Generate a RST, dropping incoming segment.
 1816          * Make ACK acceptable to originator of segment.
 1817          * Don't bother to respond if destination was broadcast/multicast.
 1818          */
 1819         if ((tiflags & TH_RST) ||
 1820             m->m_flags & (M_BCAST|M_MCAST) ||
 1821             IN_MULTICAST(ntohl(ti->ti_src.s_addr)) ||
 1822             IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
 1823                 goto drop;
 1824 #ifdef TCPDEBUG
 1825         if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
 1826                 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
 1827 #endif
 1828         if (tiflags & TH_ACK)
 1829                 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
 1830         else {
 1831                 if (tiflags & TH_SYN)
 1832                         ti->ti_len++;
 1833                 tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
 1834                     TH_RST|TH_ACK);
 1835         }
 1836         /* destroy temporarily created socket */
 1837         if (dropsocket)
 1838                 (void) soabort(so);
 1839         return;
 1840 
 1841 drop:
 1842         /*
 1843          * Drop space held by incoming segment and return.
 1844          */
 1845 #ifdef TCPDEBUG
 1846         if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
 1847                 tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
 1848 #endif
 1849         m_freem(m);
 1850         /* destroy temporarily created socket */
 1851         if (dropsocket)
 1852                 (void) soabort(so);
 1853         return;
 1854 }
 1855 
 1856 static void
 1857 tcp_dooptions(tp, cp, cnt, ti, to)
 1858         struct tcpcb *tp;
 1859         u_char *cp;
 1860         int cnt;
 1861         struct tcpiphdr *ti;
 1862         struct tcpopt *to;
 1863 {
 1864         u_short mss = 0;
 1865         int opt, optlen;
 1866 
 1867         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 1868                 opt = cp[0];
 1869                 if (opt == TCPOPT_EOL)
 1870                         break;
 1871                 if (opt == TCPOPT_NOP)
 1872                         optlen = 1;
 1873                 else {
 1874                         optlen = cp[1];
 1875                         if (optlen <= 0)
 1876                                 break;
 1877                 }
 1878                 switch (opt) {
 1879 
 1880                 default:
 1881                         continue;
 1882 
 1883                 case TCPOPT_MAXSEG:
 1884                         if (optlen != TCPOLEN_MAXSEG)
 1885                                 continue;
 1886                         if (!(ti->ti_flags & TH_SYN))
 1887                                 continue;
 1888                         bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
 1889                         NTOHS(mss);
 1890                         break;
 1891 
 1892                 case TCPOPT_WINDOW:
 1893                         if (optlen != TCPOLEN_WINDOW)
 1894                                 continue;
 1895                         if (!(ti->ti_flags & TH_SYN))
 1896                                 continue;
 1897                         tp->t_flags |= TF_RCVD_SCALE;
 1898                         tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
 1899                         break;
 1900 
 1901                 case TCPOPT_TIMESTAMP:
 1902                         if (optlen != TCPOLEN_TIMESTAMP)
 1903                                 continue;
 1904                         to->to_flag |= TOF_TS;
 1905                         bcopy((char *)cp + 2,
 1906                             (char *)&to->to_tsval, sizeof(to->to_tsval));
 1907                         NTOHL(to->to_tsval);
 1908                         bcopy((char *)cp + 6,
 1909                             (char *)&to->to_tsecr, sizeof(to->to_tsecr));
 1910                         NTOHL(to->to_tsecr);
 1911 
 1912                         /*
 1913                          * A timestamp received in a SYN makes
 1914                          * it ok to send timestamp requests and replies.
 1915                          */
 1916                         if (ti->ti_flags & TH_SYN) {
 1917                                 tp->t_flags |= TF_RCVD_TSTMP;
 1918                                 tp->ts_recent = to->to_tsval;
 1919                                 tp->ts_recent_age = tcp_now;
 1920                         }
 1921                         break;
 1922                 case TCPOPT_CC:
 1923                         if (optlen != TCPOLEN_CC)
 1924                                 continue;
 1925                         to->to_flag |= TOF_CC;
 1926                         bcopy((char *)cp + 2,
 1927                             (char *)&to->to_cc, sizeof(to->to_cc));
 1928                         NTOHL(to->to_cc);
 1929                         /*
 1930                          * A CC or CC.new option received in a SYN makes
 1931                          * it ok to send CC in subsequent segments.
 1932                          */
 1933                         if (ti->ti_flags & TH_SYN)
 1934                                 tp->t_flags |= TF_RCVD_CC;
 1935                         break;
 1936                 case TCPOPT_CCNEW:
 1937                         if (optlen != TCPOLEN_CC)
 1938                                 continue;
 1939                         if (!(ti->ti_flags & TH_SYN))
 1940                                 continue;
 1941                         to->to_flag |= TOF_CCNEW;
 1942                         bcopy((char *)cp + 2,
 1943                             (char *)&to->to_cc, sizeof(to->to_cc));
 1944                         NTOHL(to->to_cc);
 1945                         /*
 1946                          * A CC or CC.new option received in a SYN makes
 1947                          * it ok to send CC in subsequent segments.
 1948                          */
 1949                         tp->t_flags |= TF_RCVD_CC;
 1950                         break;
 1951                 case TCPOPT_CCECHO:
 1952                         if (optlen != TCPOLEN_CC)
 1953                                 continue;
 1954                         if (!(ti->ti_flags & TH_SYN))
 1955                                 continue;
 1956                         to->to_flag |= TOF_CCECHO;
 1957                         bcopy((char *)cp + 2,
 1958                             (char *)&to->to_ccecho, sizeof(to->to_ccecho));
 1959                         NTOHL(to->to_ccecho);
 1960                         break;
 1961                 }
 1962         }
 1963         if (ti->ti_flags & TH_SYN)
 1964                 tcp_mss(tp, mss);       /* sets t_maxseg */
 1965 }
 1966 
 1967 /*
 1968  * Pull out of band byte out of a segment so
 1969  * it doesn't appear in the user's data queue.
 1970  * It is still reflected in the segment length for
 1971  * sequencing purposes.
 1972  */
 1973 static void
 1974 tcp_pulloutofband(so, ti, m)
 1975         struct socket *so;
 1976         struct tcpiphdr *ti;
 1977         register struct mbuf *m;
 1978 {
 1979         int cnt = ti->ti_urp - 1;
 1980 
 1981         while (cnt >= 0) {
 1982                 if (m->m_len > cnt) {
 1983                         char *cp = mtod(m, caddr_t) + cnt;
 1984                         struct tcpcb *tp = sototcpcb(so);
 1985 
 1986                         tp->t_iobc = *cp;
 1987                         tp->t_oobflags |= TCPOOB_HAVEDATA;
 1988                         bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
 1989                         m->m_len--;
 1990                         return;
 1991                 }
 1992                 cnt -= m->m_len;
 1993                 m = m->m_next;
 1994                 if (m == 0)
 1995                         break;
 1996         }
 1997         panic("tcp_pulloutofband");
 1998 }
 1999 
 2000 /*
 2001  * Collect new round-trip time estimate
 2002  * and update averages and current timeout.
 2003  */
 2004 static void
 2005 tcp_xmit_timer(tp, rtt)
 2006         register struct tcpcb *tp;
 2007         short rtt;
 2008 {
 2009         register int delta;
 2010 
 2011         tcpstat.tcps_rttupdated++;
 2012         tp->t_rttupdated++;
 2013         if (tp->t_srtt != 0) {
 2014                 /*
 2015                  * srtt is stored as fixed point with 5 bits after the
 2016                  * binary point (i.e., scaled by 8).  The following magic
 2017                  * is equivalent to the smoothing algorithm in rfc793 with
 2018                  * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
 2019                  * point).  Adjust rtt to origin 0.
 2020                  */
 2021                 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
 2022                         - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
 2023 
 2024                 if ((tp->t_srtt += delta) <= 0)
 2025                         tp->t_srtt = 1;
 2026 
 2027                 /*
 2028                  * We accumulate a smoothed rtt variance (actually, a
 2029                  * smoothed mean difference), then set the retransmit
 2030                  * timer to smoothed rtt + 4 times the smoothed variance.
 2031                  * rttvar is stored as fixed point with 4 bits after the
 2032                  * binary point (scaled by 16).  The following is
 2033                  * equivalent to rfc793 smoothing with an alpha of .75
 2034                  * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
 2035                  * rfc793's wired-in beta.
 2036                  */
 2037                 if (delta < 0)
 2038                         delta = -delta;
 2039                 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
 2040                 if ((tp->t_rttvar += delta) <= 0)
 2041                         tp->t_rttvar = 1;
 2042         } else {
 2043                 /*
 2044                  * No rtt measurement yet - use the unsmoothed rtt.
 2045                  * Set the variance to half the rtt (so our first
 2046                  * retransmit happens at 3*rtt).
 2047                  */
 2048                 tp->t_srtt = rtt << TCP_RTT_SHIFT;
 2049                 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
 2050         }
 2051         tp->t_rtt = 0;
 2052         tp->t_rxtshift = 0;
 2053 
 2054         /*
 2055          * the retransmit should happen at rtt + 4 * rttvar.
 2056          * Because of the way we do the smoothing, srtt and rttvar
 2057          * will each average +1/2 tick of bias.  When we compute
 2058          * the retransmit timer, we want 1/2 tick of rounding and
 2059          * 1 extra tick because of +-1/2 tick uncertainty in the
 2060          * firing of the timer.  The bias will give us exactly the
 2061          * 1.5 tick we need.  But, because the bias is
 2062          * statistical, we have to test that we don't drop below
 2063          * the minimum feasible timer (which is 2 ticks).
 2064          */
 2065         TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
 2066                       max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
 2067 
 2068         /*
 2069          * We received an ack for a packet that wasn't retransmitted;
 2070          * it is probably safe to discard any error indications we've
 2071          * received recently.  This isn't quite right, but close enough
 2072          * for now (a route might have failed after we sent a segment,
 2073          * and the return path might not be symmetrical).
 2074          */
 2075         tp->t_softerror = 0;
 2076 }
 2077 
 2078 /*
 2079  * Determine a reasonable value for maxseg size.
 2080  * If the route is known, check route for mtu.
 2081  * If none, use an mss that can be handled on the outgoing
 2082  * interface without forcing IP to fragment; if bigger than
 2083  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
 2084  * to utilize large mbufs.  If no route is found, route has no mtu,
 2085  * or the destination isn't local, use a default, hopefully conservative
 2086  * size (usually 512 or the default IP max size, but no more than the mtu
 2087  * of the interface), as we can't discover anything about intervening
 2088  * gateways or networks.  We also initialize the congestion/slow start
 2089  * window to be a single segment if the destination isn't local.
 2090  * While looking at the routing entry, we also initialize other path-dependent
 2091  * parameters from pre-set or cached values in the routing entry.
 2092  *
 2093  * Also take into account the space needed for options that we
 2094  * send regularly.  Make maxseg shorter by that amount to assure
 2095  * that we can send maxseg amount of data even when the options
 2096  * are present.  Store the upper limit of the length of options plus
 2097  * data in maxopd.
 2098  *
 2099  * NOTE that this routine is only called when we process an incoming
 2100  * segment, for outgoing segments only tcp_mssopt is called.
 2101  *
 2102  * In case of T/TCP, we call this routine during implicit connection
 2103  * setup as well (offer = -1), to initialize maxseg from the cached
 2104  * MSS of our peer.
 2105  */
 2106 void
 2107 tcp_mss(tp, offer)
 2108         struct tcpcb *tp;
 2109         int offer;
 2110 {
 2111         register struct rtentry *rt;
 2112         struct ifnet *ifp;
 2113         register int rtt, mss;
 2114         u_long bufsize;
 2115         struct inpcb *inp;
 2116         struct socket *so;
 2117         struct rmxp_tao *taop;
 2118         int origoffer = offer;
 2119 
 2120         inp = tp->t_inpcb;
 2121         if ((rt = tcp_rtlookup(inp)) == NULL) {
 2122                 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
 2123                 return;
 2124         }
 2125         ifp = rt->rt_ifp;
 2126         so = inp->inp_socket;
 2127 
 2128         taop = rmx_taop(rt->rt_rmx);
 2129         /*
 2130          * Offer == -1 means that we didn't receive SYN yet,
 2131          * use cached value in that case;
 2132          */
 2133         if (offer == -1)
 2134                 offer = taop->tao_mssopt;
 2135         /*
 2136          * Offer == 0 means that there was no MSS on the SYN segment,
 2137          * in this case we use tcp_mssdflt.
 2138          */
 2139         if (offer == 0)
 2140                 offer = tcp_mssdflt;
 2141         else
 2142                 /*
 2143                  * Sanity check: make sure that maxopd will be large
 2144                  * enough to allow some data on segments even is the
 2145                  * all the option space is used (40bytes).  Otherwise
 2146                  * funny things may happen in tcp_output.
 2147                  */
 2148                 offer = max(offer, 64);
 2149         taop->tao_mssopt = offer;
 2150 
 2151         /*
 2152          * While we're here, check if there's an initial rtt
 2153          * or rttvar.  Convert from the route-table units
 2154          * to scaled multiples of the slow timeout timer.
 2155          */
 2156         if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
 2157                 /*
 2158                  * XXX the lock bit for RTT indicates that the value
 2159                  * is also a minimum value; this is subject to time.
 2160                  */
 2161                 if (rt->rt_rmx.rmx_locks & RTV_RTT)
 2162                         tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
 2163                 tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
 2164                 tcpstat.tcps_usedrtt++;
 2165                 if (rt->rt_rmx.rmx_rttvar) {
 2166                         tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
 2167                             (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
 2168                         tcpstat.tcps_usedrttvar++;
 2169                 } else {
 2170                         /* default variation is +- 1 rtt */
 2171                         tp->t_rttvar =
 2172                             tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
 2173                 }
 2174                 TCPT_RANGESET(tp->t_rxtcur,
 2175                     ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
 2176                     tp->t_rttmin, TCPTV_REXMTMAX);
 2177         }
 2178         /*
 2179          * if there's an mtu associated with the route, use it
 2180          */
 2181         if (rt->rt_rmx.rmx_mtu)
 2182                 mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
 2183         else
 2184         {
 2185                 mss = ifp->if_mtu - sizeof(struct tcpiphdr);
 2186                 if (!in_localaddr(inp->inp_faddr))
 2187                         mss = min(mss, tcp_mssdflt);
 2188         }
 2189         mss = min(mss, offer);
 2190         /*
 2191          * maxopd stores the maximum length of data AND options
 2192          * in a segment; maxseg is the amount of data in a normal
 2193          * segment.  We need to store this value (maxopd) apart
 2194          * from maxseg, because now every segment carries options
 2195          * and thus we normally have somewhat less data in segments.
 2196          */
 2197         tp->t_maxopd = mss;
 2198 
 2199         /*
 2200          * In case of T/TCP, origoffer==-1 indicates, that no segments
 2201          * were received yet.  In this case we just guess, otherwise
 2202          * we do the same as before T/TCP.
 2203          */
 2204         if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
 2205             (origoffer == -1 ||
 2206              (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
 2207                 mss -= TCPOLEN_TSTAMP_APPA;
 2208         if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
 2209             (origoffer == -1 ||
 2210              (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
 2211                 mss -= TCPOLEN_CC_APPA;
 2212 
 2213 #if     (MCLBYTES & (MCLBYTES - 1)) == 0
 2214                 if (mss > MCLBYTES)
 2215                         mss &= ~(MCLBYTES-1);
 2216 #else
 2217                 if (mss > MCLBYTES)
 2218                         mss = mss / MCLBYTES * MCLBYTES;
 2219 #endif
 2220         /*
 2221          * If there's a pipesize, change the socket buffer
 2222          * to that size.  Make the socket buffers an integral
 2223          * number of mss units; if the mss is larger than
 2224          * the socket buffer, decrease the mss.
 2225          */
 2226 #ifdef RTV_SPIPE
 2227         if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
 2228 #endif
 2229                 bufsize = so->so_snd.sb_hiwat;
 2230         if (bufsize < mss)
 2231                 mss = bufsize;
 2232         else {
 2233                 bufsize = roundup(bufsize, mss);
 2234                 if (bufsize > sb_max)
 2235                         bufsize = sb_max;
 2236                 (void)sbreserve(&so->so_snd, bufsize);
 2237         }
 2238         tp->t_maxseg = mss;
 2239 
 2240 #ifdef RTV_RPIPE
 2241         if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
 2242 #endif
 2243                 bufsize = so->so_rcv.sb_hiwat;
 2244         if (bufsize > mss) {
 2245                 bufsize = roundup(bufsize, mss);
 2246                 if (bufsize > sb_max)
 2247                         bufsize = sb_max;
 2248                 (void)sbreserve(&so->so_rcv, bufsize);
 2249         }
 2250         /*
 2251          * Don't force slow-start on local network.
 2252          */
 2253         if (!in_localaddr(inp->inp_faddr))
 2254                 tp->snd_cwnd = mss;
 2255 
 2256         if (rt->rt_rmx.rmx_ssthresh) {
 2257                 /*
 2258                  * There's some sort of gateway or interface
 2259                  * buffer limit on the path.  Use this to set
 2260                  * the slow start threshhold, but set the
 2261                  * threshold to no less than 2*mss.
 2262                  */
 2263                 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
 2264                 tcpstat.tcps_usedssthresh++;
 2265         }
 2266 }
 2267 
 2268 /*
 2269  * Determine the MSS option to send on an outgoing SYN.
 2270  */
 2271 int
 2272 tcp_mssopt(tp)
 2273         struct tcpcb *tp;
 2274 {
 2275         struct rtentry *rt;
 2276 
 2277         rt = tcp_rtlookup(tp->t_inpcb);
 2278         if (rt == NULL)
 2279                 return tcp_mssdflt;
 2280 
 2281         return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr);
 2282 }

Cache object: 6bd523d143759947cefff63027f9777c


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.