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) 2002, 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
    3  * Copyright (c) 2002, 2003, 2004 The DragonFly Project.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to The DragonFly Project
    6  * by Jeffrey M. Hsu.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of The DragonFly Project nor the names of its
   17  *    contributors may be used to endorse or promote products derived
   18  *    from this software without specific, prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
   26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  */
   33 
   34 /*
   35  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
   36  *      The Regents of the University of California.  All rights reserved.
   37  *
   38  * Redistribution and use in source and binary forms, with or without
   39  * modification, are permitted provided that the following conditions
   40  * are met:
   41  * 1. Redistributions of source code must retain the above copyright
   42  *    notice, this list of conditions and the following disclaimer.
   43  * 2. Redistributions in binary form must reproduce the above copyright
   44  *    notice, this list of conditions and the following disclaimer in the
   45  *    documentation and/or other materials provided with the distribution.
   46  * 3. Neither the name of the University nor the names of its contributors
   47  *    may be used to endorse or promote products derived from this software
   48  *    without specific prior written permission.
   49  *
   50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   60  * SUCH DAMAGE.
   61  *
   62  *      @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
   63  * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.38 2003/05/21 04:46:41 cjc Exp $
   64  */
   65 
   66 #include "opt_inet.h"
   67 #include "opt_inet6.h"
   68 #include "opt_ipsec.h"
   69 #include "opt_tcpdebug.h"
   70 #include "opt_tcp_input.h"
   71 
   72 #include <sys/param.h>
   73 #include <sys/systm.h>
   74 #include <sys/kernel.h>
   75 #include <sys/sysctl.h>
   76 #include <sys/malloc.h>
   77 #include <sys/mbuf.h>
   78 #include <sys/proc.h>           /* for proc0 declaration */
   79 #include <sys/protosw.h>
   80 #include <sys/socket.h>
   81 #include <sys/socketvar.h>
   82 #include <sys/syslog.h>
   83 #include <sys/in_cksum.h>
   84 
   85 #include <sys/socketvar2.h>
   86 
   87 #include <machine/cpu.h>        /* before tcp_seq.h, for tcp_random18() */
   88 #include <machine/stdarg.h>
   89 
   90 #include <net/if.h>
   91 #include <net/route.h>
   92 
   93 #include <netinet/in.h>
   94 #include <netinet/in_systm.h>
   95 #include <netinet/ip.h>
   96 #include <netinet/ip_icmp.h>    /* for ICMP_BANDLIM */
   97 #include <netinet/in_var.h>
   98 #include <netinet/icmp_var.h>   /* for ICMP_BANDLIM */
   99 #include <netinet/in_pcb.h>
  100 #include <netinet/ip_var.h>
  101 #include <netinet/ip6.h>
  102 #include <netinet/icmp6.h>
  103 #include <netinet6/nd6.h>
  104 #include <netinet6/ip6_var.h>
  105 #include <netinet6/in6_pcb.h>
  106 #include <netinet/tcp.h>
  107 #include <netinet/tcp_fsm.h>
  108 #include <netinet/tcp_seq.h>
  109 #include <netinet/tcp_timer.h>
  110 #include <netinet/tcp_timer2.h>
  111 #include <netinet/tcp_var.h>
  112 #include <netinet6/tcp6_var.h>
  113 #include <netinet/tcpip.h>
  114 
  115 #ifdef TCPDEBUG
  116 #include <netinet/tcp_debug.h>
  117 
  118 u_char tcp_saveipgen[40];    /* the size must be of max ip header, now IPv6 */
  119 struct tcphdr tcp_savetcp;
  120 #endif
  121 
  122 #ifdef FAST_IPSEC
  123 #include <netproto/ipsec/ipsec.h>
  124 #include <netproto/ipsec/ipsec6.h>
  125 #endif
  126 
  127 #ifdef IPSEC
  128 #include <netinet6/ipsec.h>
  129 #include <netinet6/ipsec6.h>
  130 #include <netproto/key/key.h>
  131 #endif
  132 
  133 /*
  134  * Limit burst of new packets during SACK based fast recovery
  135  * or extended limited transmit.
  136  */
  137 #define TCP_SACK_MAXBURST       4
  138 
  139 MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
  140 
  141 static int log_in_vain = 0;
  142 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
  143     &log_in_vain, 0, "Log all incoming TCP connections");
  144 
  145 static int blackhole = 0;
  146 SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
  147     &blackhole, 0, "Do not send RST when dropping refused connections");
  148 
  149 int tcp_delack_enabled = 1;
  150 SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
  151     &tcp_delack_enabled, 0,
  152     "Delay ACK to try and piggyback it onto a data packet");
  153 
  154 #ifdef TCP_DROP_SYNFIN
  155 static int drop_synfin = 0;
  156 SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
  157     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
  158 #endif
  159 
  160 static int tcp_do_limitedtransmit = 1;
  161 SYSCTL_INT(_net_inet_tcp, OID_AUTO, limitedtransmit, CTLFLAG_RW,
  162     &tcp_do_limitedtransmit, 0, "Enable RFC 3042 (Limited Transmit)");
  163 
  164 static int tcp_do_early_retransmit = 1;
  165 SYSCTL_INT(_net_inet_tcp, OID_AUTO, earlyretransmit, CTLFLAG_RW,
  166     &tcp_do_early_retransmit, 0, "Early retransmit");
  167 
  168 int tcp_aggregate_acks = 1;
  169 SYSCTL_INT(_net_inet_tcp, OID_AUTO, aggregate_acks, CTLFLAG_RW,
  170     &tcp_aggregate_acks, 0, "Aggregate built-up acks into one ack");
  171 
  172 static int tcp_do_eifel_detect = 1;
  173 SYSCTL_INT(_net_inet_tcp, OID_AUTO, eifel, CTLFLAG_RW,
  174     &tcp_do_eifel_detect, 0, "Eifel detection algorithm (RFC 3522)");
  175 
  176 static int tcp_do_abc = 1;
  177 SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc, CTLFLAG_RW,
  178     &tcp_do_abc, 0,
  179     "TCP Appropriate Byte Counting (RFC 3465)");
  180 
  181 /*
  182  * The following value actually takes range [25ms, 250ms],
  183  * given that most modern systems use 1ms ~ 10ms as the unit
  184  * of timestamp option.
  185  */
  186 static u_int tcp_paws_tolerance = 25;
  187 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, paws_tolerance, CTLFLAG_RW,
  188     &tcp_paws_tolerance, 0, "RFC1323 PAWS tolerance");
  189 
  190 /*
  191  * Define as tunable for easy testing with SACK on and off.
  192  * Warning:  do not change setting in the middle of an existing active TCP flow,
  193  *   else strange things might happen to that flow.
  194  */
  195 int tcp_do_sack = 1;
  196 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack, CTLFLAG_RW,
  197     &tcp_do_sack, 0, "Enable SACK Algorithms");
  198 
  199 int tcp_do_smartsack = 1;
  200 SYSCTL_INT(_net_inet_tcp, OID_AUTO, smartsack, CTLFLAG_RW,
  201     &tcp_do_smartsack, 0, "Enable Smart SACK Algorithms");
  202 
  203 int tcp_do_rescuesack = 1;
  204 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rescuesack, CTLFLAG_RW,
  205     &tcp_do_rescuesack, 0, "Rescue retransmission for SACK");
  206 
  207 int tcp_aggressive_rescuesack = 0;
  208 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rescuesack_agg, CTLFLAG_RW,
  209     &tcp_aggressive_rescuesack, 0, "Aggressive rescue retransmission for SACK");
  210 
  211 static int tcp_force_sackrxt = 1;
  212 SYSCTL_INT(_net_inet_tcp, OID_AUTO, force_sackrxt, CTLFLAG_RW,
  213     &tcp_force_sackrxt, 0, "Allowed forced SACK retransmit burst");
  214 
  215 int tcp_do_rfc6675 = 1;
  216 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675, CTLFLAG_RW,
  217     &tcp_do_rfc6675, 0, "Enable RFC6675");
  218 
  219 int tcp_rfc6675_rxt = 0;
  220 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_rxt, CTLFLAG_RW,
  221     &tcp_rfc6675_rxt, 0, "Enable RFC6675 retransmit");
  222 
  223 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
  224     "TCP Segment Reassembly Queue");
  225 
  226 int tcp_reass_maxseg = 0;
  227 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RD,
  228     &tcp_reass_maxseg, 0,
  229     "Global maximum number of TCP Segments in Reassembly Queue");
  230 
  231 int tcp_reass_qsize = 0;
  232 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
  233     &tcp_reass_qsize, 0,
  234     "Global number of TCP Segments currently in Reassembly Queue");
  235 
  236 static int tcp_reass_overflows = 0;
  237 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
  238     &tcp_reass_overflows, 0,
  239     "Global number of TCP Segment Reassembly Queue Overflows");
  240 
  241 int tcp_do_autorcvbuf = 1;
  242 SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
  243     &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing");
  244 
  245 int tcp_autorcvbuf_inc = 16*1024;
  246 SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
  247     &tcp_autorcvbuf_inc, 0,
  248     "Incrementor step size of automatic receive buffer");
  249 
  250 int tcp_autorcvbuf_max = 2*1024*1024;
  251 SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
  252     &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer");
  253 
  254 int tcp_sosend_agglim = 2;
  255 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sosend_agglim, CTLFLAG_RW,
  256     &tcp_sosend_agglim, 0, "TCP sosend mbuf aggregation limit");
  257 
  258 int tcp_sosend_async = 1;
  259 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sosend_async, CTLFLAG_RW,
  260     &tcp_sosend_async, 0, "TCP asynchronized pru_send");
  261 
  262 int tcp_sosend_jcluster = 1;
  263 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sosend_jcluster, CTLFLAG_RW,
  264     &tcp_sosend_jcluster, 0, "TCP output uses jcluster");
  265 
  266 static int tcp_ignore_redun_dsack = 1;
  267 SYSCTL_INT(_net_inet_tcp, OID_AUTO, ignore_redun_dsack, CTLFLAG_RW,
  268     &tcp_ignore_redun_dsack, 0, "Ignore redundant DSACK");
  269 
  270 static int tcp_reuseport_ext = 1;
  271 SYSCTL_INT(_net_inet_tcp, OID_AUTO, reuseport_ext, CTLFLAG_RW,
  272     &tcp_reuseport_ext, 0, "SO_REUSEPORT extension");
  273 
  274 static void      tcp_dooptions(struct tcpopt *, u_char *, int, boolean_t,
  275                     tcp_seq);
  276 static void      tcp_pulloutofband(struct socket *,
  277                      struct tcphdr *, struct mbuf *, int);
  278 static int       tcp_reass(struct tcpcb *, struct tcphdr *, int *,
  279                      struct mbuf *);
  280 static void      tcp_xmit_timer(struct tcpcb *, int, tcp_seq);
  281 static void      tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *, int);
  282 static void      tcp_sack_rexmt(struct tcpcb *, boolean_t);
  283 static boolean_t tcp_sack_limitedxmit(struct tcpcb *);
  284 static int       tcp_rmx_msl(const struct tcpcb *);
  285 static void      tcp_established(struct tcpcb *);
  286 static boolean_t tcp_recv_dupack(struct tcpcb *, tcp_seq, u_int);
  287 
  288 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
  289 #ifdef INET6
  290 #define ND6_HINT(tp) \
  291 do { \
  292         if ((tp) && (tp)->t_inpcb && \
  293             ((tp)->t_inpcb->inp_vflag & INP_IPV6) && \
  294             (tp)->t_inpcb->in6p_route.ro_rt) \
  295                 nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
  296 } while (0)
  297 #else
  298 #define ND6_HINT(tp)
  299 #endif
  300 
  301 /*
  302  * Indicate whether this ack should be delayed.  We can delay the ack if
  303  *      - delayed acks are enabled and
  304  *      - there is no delayed ack timer in progress and
  305  *      - our last ack wasn't a 0-sized window.  We never want to delay
  306  *        the ack that opens up a 0-sized window.
  307  */
  308 #define DELAY_ACK(tp) \
  309         (tcp_delack_enabled && !tcp_callout_pending(tp, tp->tt_delack) && \
  310         !(tp->t_flags & TF_RXWIN0SENT))
  311 
  312 #define acceptable_window_update(tp, th, tiwin)                         \
  313     (SEQ_LT(tp->snd_wl1, th->th_seq) ||                                 \
  314      (tp->snd_wl1 == th->th_seq &&                                      \
  315       (SEQ_LT(tp->snd_wl2, th->th_ack) ||                               \
  316        (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))
  317 
  318 #define iceildiv(n, d)          (((n)+(d)-1) / (d))
  319 #define need_early_retransmit(tp, ownd) \
  320     (tcp_do_early_retransmit && \
  321      (tcp_do_eifel_detect && (tp->t_flags & TF_RCVD_TSTMP)) && \
  322      ownd < ((tp->t_rxtthresh + 1) * tp->t_maxseg) && \
  323      tp->t_dupacks + 1 >= iceildiv(ownd, tp->t_maxseg) && \
  324      (!TCP_DO_SACK(tp) || ownd <= tp->t_maxseg || \
  325       tcp_sack_has_sacked(&tp->scb, ownd - tp->t_maxseg)))
  326 
  327 /*
  328  * Returns TRUE, if this segment can be merged with the last
  329  * pending segment in the reassemble queue and this segment
  330  * does not overlap with the pending segment immediately
  331  * preceeding the last pending segment.
  332  */
  333 static __inline boolean_t
  334 tcp_paws_canreasslast(const struct tcpcb *tp, const struct tcphdr *th, int tlen)
  335 {
  336         const struct tseg_qent *last, *prev;
  337 
  338         last = TAILQ_LAST(&tp->t_segq, tsegqe_head);
  339         if (last == NULL)
  340                 return FALSE;
  341 
  342         /* This segment comes immediately after the last pending segment */
  343         if (last->tqe_th->th_seq + last->tqe_len == th->th_seq) {
  344                 if (last->tqe_th->th_flags & TH_FIN) {
  345                         /* No segments should follow segment w/ FIN */
  346                         return FALSE;
  347                 }
  348                 return TRUE;
  349         }
  350 
  351         if (th->th_seq + tlen != last->tqe_th->th_seq)
  352                 return FALSE;
  353         /* This segment comes immediately before the last pending segment */
  354 
  355         prev = TAILQ_PREV(last, tsegqe_head, tqe_q);
  356         if (prev == NULL) {
  357                 /*
  358                  * No pending preceeding segment, we assume this segment
  359                  * could be reassembled.
  360                  */
  361                 return TRUE;
  362         }
  363 
  364         /* This segment does not overlap with the preceeding segment */
  365         if (SEQ_GEQ(th->th_seq, prev->tqe_th->th_seq + prev->tqe_len))
  366                 return TRUE;
  367 
  368         return FALSE;
  369 }
  370 
  371 static __inline void
  372 tcp_ncr_update_rxtthresh(struct tcpcb *tp)
  373 {
  374         int old_rxtthresh = tp->t_rxtthresh;
  375         uint32_t ownd = tp->snd_max - tp->snd_una;
  376 
  377         tp->t_rxtthresh = max(tcprexmtthresh, ((ownd / tp->t_maxseg) >> 1));
  378         if (tp->t_rxtthresh != old_rxtthresh) {
  379                 tcp_sack_update_lostseq(&tp->scb, tp->snd_una,
  380                     tp->t_maxseg, tp->t_rxtthresh);
  381         }
  382 }
  383 
  384 static int
  385 tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m)
  386 {
  387         struct tseg_qent *q;
  388         struct tseg_qent *p = NULL;
  389         struct tseg_qent *te;
  390         struct socket *so = tp->t_inpcb->inp_socket;
  391         int flags;
  392 
  393         /*
  394          * Call with th == NULL after become established to
  395          * force pre-ESTABLISHED data up to user socket.
  396          */
  397         if (th == NULL)
  398                 goto present;
  399 
  400         /*
  401          * Limit the number of segments in the reassembly queue to prevent
  402          * holding on to too many segments (and thus running out of mbufs).
  403          * Make sure to let the missing segment through which caused this
  404          * queue.  Always keep one global queue entry spare to be able to
  405          * process the missing segment.
  406          */
  407         if (th->th_seq != tp->rcv_nxt &&
  408             tcp_reass_qsize + 1 >= tcp_reass_maxseg) {
  409                 tcp_reass_overflows++;
  410                 tcpstat.tcps_rcvmemdrop++;
  411                 m_freem(m);
  412                 /* no SACK block to report */
  413                 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
  414                 return (0);
  415         }
  416 
  417         /* Allocate a new queue entry. */
  418         te = kmalloc(sizeof(struct tseg_qent), M_TSEGQ, M_INTWAIT | M_NULLOK);
  419         if (te == NULL) {
  420                 tcpstat.tcps_rcvmemdrop++;
  421                 m_freem(m);
  422                 /* no SACK block to report */
  423                 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
  424                 return (0);
  425         }
  426         atomic_add_int(&tcp_reass_qsize, 1);
  427 
  428         if (th->th_flags & TH_FIN)
  429                 tp->t_flags |= TF_QUEDFIN;
  430 
  431         /*
  432          * Find a segment which begins after this one does.
  433          */
  434         TAILQ_FOREACH(q, &tp->t_segq, tqe_q) {
  435                 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
  436                         break;
  437                 p = q;
  438         }
  439 
  440         /*
  441          * If there is a preceding segment, it may provide some of
  442          * our data already.  If so, drop the data from the incoming
  443          * segment.  If it provides all of our data, drop us.
  444          */
  445         if (p != NULL) {
  446                 tcp_seq_diff_t i;
  447 
  448                 /* conversion to int (in i) handles seq wraparound */
  449                 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
  450                 if (i > 0) {            /* overlaps preceding segment */
  451                         tp->sack_flags |=
  452                             (TSACK_F_DUPSEG | TSACK_F_ENCLOSESEG);
  453                         /* enclosing block starts w/ preceding segment */
  454                         tp->encloseblk.rblk_start = p->tqe_th->th_seq;
  455                         if (i >= *tlenp) {
  456                                 if (th->th_flags & TH_FIN)
  457                                         p->tqe_th->th_flags |= TH_FIN;
  458 
  459                                 /* preceding encloses incoming segment */
  460                                 tp->encloseblk.rblk_end = TCP_SACK_BLKEND(
  461                                     p->tqe_th->th_seq + p->tqe_len,
  462                                     p->tqe_th->th_flags);
  463                                 tcpstat.tcps_rcvduppack++;
  464                                 tcpstat.tcps_rcvdupbyte += *tlenp;
  465                                 m_freem(m);
  466                                 kfree(te, M_TSEGQ);
  467                                 atomic_add_int(&tcp_reass_qsize, -1);
  468                                 /*
  469                                  * Try to present any queued data
  470                                  * at the left window edge to the user.
  471                                  * This is needed after the 3-WHS
  472                                  * completes.
  473                                  */
  474                                 goto present;   /* ??? */
  475                         }
  476                         m_adj(m, i);
  477                         *tlenp -= i;
  478                         th->th_seq += i;
  479                         /* incoming segment end is enclosing block end */
  480                         tp->encloseblk.rblk_end = TCP_SACK_BLKEND(
  481                             th->th_seq + *tlenp, th->th_flags);
  482                         /* trim end of reported D-SACK block */
  483                         tp->reportblk.rblk_end = th->th_seq;
  484                 }
  485         }
  486         tcpstat.tcps_rcvoopack++;
  487         tcpstat.tcps_rcvoobyte += *tlenp;
  488 
  489         /*
  490          * While we overlap succeeding segments trim them or,
  491          * if they are completely covered, dequeue them.
  492          */
  493         while (q) {
  494                 tcp_seq_diff_t i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
  495                 tcp_seq qend = q->tqe_th->th_seq + q->tqe_len;
  496                 tcp_seq qend_sack = TCP_SACK_BLKEND(qend, q->tqe_th->th_flags);
  497                 struct tseg_qent *nq;
  498 
  499                 if (i <= 0)
  500                         break;
  501                 if (!(tp->sack_flags & TSACK_F_DUPSEG)) {
  502                         /* first time through */
  503                         tp->sack_flags |= (TSACK_F_DUPSEG | TSACK_F_ENCLOSESEG);
  504                         tp->encloseblk = tp->reportblk;
  505                         /* report trailing duplicate D-SACK segment */
  506                         tp->reportblk.rblk_start = q->tqe_th->th_seq;
  507                 }
  508                 if ((tp->sack_flags & TSACK_F_ENCLOSESEG) &&
  509                     SEQ_GT(qend_sack, tp->encloseblk.rblk_end)) {
  510                         /* extend enclosing block if one exists */
  511                         tp->encloseblk.rblk_end = qend_sack;
  512                 }
  513                 if (i < q->tqe_len) {
  514                         q->tqe_th->th_seq += i;
  515                         q->tqe_len -= i;
  516                         m_adj(q->tqe_m, i);
  517                         break;
  518                 }
  519 
  520                 if (q->tqe_th->th_flags & TH_FIN)
  521                         th->th_flags |= TH_FIN;
  522 
  523                 nq = TAILQ_NEXT(q, tqe_q);
  524                 TAILQ_REMOVE(&tp->t_segq, q, tqe_q);
  525                 m_freem(q->tqe_m);
  526                 kfree(q, M_TSEGQ);
  527                 atomic_add_int(&tcp_reass_qsize, -1);
  528                 q = nq;
  529         }
  530 
  531         /* Insert the new segment queue entry into place. */
  532         te->tqe_m = m;
  533         te->tqe_th = th;
  534         te->tqe_len = *tlenp;
  535 
  536         /* check if can coalesce with following segment */
  537         if (q != NULL && (th->th_seq + *tlenp == q->tqe_th->th_seq)) {
  538                 tcp_seq tend_sack;
  539 
  540                 te->tqe_len += q->tqe_len;
  541                 if (q->tqe_th->th_flags & TH_FIN)
  542                         te->tqe_th->th_flags |= TH_FIN;
  543                 tend_sack = TCP_SACK_BLKEND(te->tqe_th->th_seq + te->tqe_len,
  544                     te->tqe_th->th_flags);
  545 
  546                 m_cat(te->tqe_m, q->tqe_m);
  547                 tp->encloseblk.rblk_end = tend_sack;
  548                 /*
  549                  * When not reporting a duplicate segment, use
  550                  * the larger enclosing block as the SACK block.
  551                  */
  552                 if (!(tp->sack_flags & TSACK_F_DUPSEG))
  553                         tp->reportblk.rblk_end = tend_sack;
  554                 TAILQ_REMOVE(&tp->t_segq, q, tqe_q);
  555                 kfree(q, M_TSEGQ);
  556                 atomic_add_int(&tcp_reass_qsize, -1);
  557         }
  558 
  559         if (p == NULL) {
  560                 TAILQ_INSERT_HEAD(&tp->t_segq, te, tqe_q);
  561         } else {
  562                 /* check if can coalesce with preceding segment */
  563                 if (p->tqe_th->th_seq + p->tqe_len == th->th_seq) {
  564                         if (te->tqe_th->th_flags & TH_FIN)
  565                                 p->tqe_th->th_flags |= TH_FIN;
  566                         p->tqe_len += te->tqe_len;
  567                         m_cat(p->tqe_m, te->tqe_m);
  568                         tp->encloseblk.rblk_start = p->tqe_th->th_seq;
  569                         /*
  570                          * When not reporting a duplicate segment, use
  571                          * the larger enclosing block as the SACK block.
  572                          */
  573                         if (!(tp->sack_flags & TSACK_F_DUPSEG))
  574                                 tp->reportblk.rblk_start = p->tqe_th->th_seq;
  575                         kfree(te, M_TSEGQ);
  576                         atomic_add_int(&tcp_reass_qsize, -1);
  577                 } else {
  578                         TAILQ_INSERT_AFTER(&tp->t_segq, p, te, tqe_q);
  579                 }
  580         }
  581 
  582 present:
  583         /*
  584          * Present data to user, advancing rcv_nxt through
  585          * completed sequence space.
  586          */
  587         if (!TCPS_HAVEESTABLISHED(tp->t_state))
  588                 return (0);
  589         q = TAILQ_FIRST(&tp->t_segq);
  590         if (q == NULL || q->tqe_th->th_seq != tp->rcv_nxt)
  591                 return (0);
  592         tp->rcv_nxt += q->tqe_len;
  593         if (!(tp->sack_flags & TSACK_F_DUPSEG)) {
  594                 /* no SACK block to report since ACK advanced */
  595                 tp->reportblk.rblk_start = tp->reportblk.rblk_end;
  596         }
  597         /* no enclosing block to report since ACK advanced */
  598         tp->sack_flags &= ~TSACK_F_ENCLOSESEG;
  599         flags = q->tqe_th->th_flags & TH_FIN;
  600         TAILQ_REMOVE(&tp->t_segq, q, tqe_q);
  601         KASSERT(TAILQ_EMPTY(&tp->t_segq) ||
  602                 TAILQ_FIRST(&tp->t_segq)->tqe_th->th_seq != tp->rcv_nxt,
  603                 ("segment not coalesced"));
  604         if (so->so_state & SS_CANTRCVMORE) {
  605                 m_freem(q->tqe_m);
  606         } else {
  607                 lwkt_gettoken(&so->so_rcv.ssb_token);
  608                 ssb_appendstream(&so->so_rcv, q->tqe_m);
  609                 lwkt_reltoken(&so->so_rcv.ssb_token);
  610         }
  611         kfree(q, M_TSEGQ);
  612         atomic_add_int(&tcp_reass_qsize, -1);
  613         ND6_HINT(tp);
  614         sorwakeup(so);
  615         return (flags);
  616 }
  617 
  618 /*
  619  * TCP input routine, follows pages 65-76 of the
  620  * protocol specification dated September, 1981 very closely.
  621  */
  622 #ifdef INET6
  623 int
  624 tcp6_input(struct mbuf **mp, int *offp, int proto)
  625 {
  626         struct mbuf *m = *mp;
  627         struct in6_ifaddr *ia6;
  628 
  629         IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
  630 
  631         /*
  632          * draft-itojun-ipv6-tcp-to-anycast
  633          * better place to put this in?
  634          */
  635         ia6 = ip6_getdstifaddr(m);
  636         if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
  637                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
  638                             offsetof(struct ip6_hdr, ip6_dst));
  639                 return (IPPROTO_DONE);
  640         }
  641 
  642         tcp_input(mp, offp, proto);
  643         return (IPPROTO_DONE);
  644 }
  645 #endif
  646 
  647 int
  648 tcp_input(struct mbuf **mp, int *offp, int proto)
  649 {
  650         int off0;
  651         struct tcphdr *th;
  652         struct ip *ip = NULL;
  653         struct ipovly *ipov;
  654         struct inpcb *inp = NULL;
  655         u_char *optp = NULL;
  656         int optlen = 0;
  657         int tlen, off;
  658         int len = 0;
  659         int drop_hdrlen;
  660         struct tcpcb *tp = NULL;
  661         int thflags;
  662         struct socket *so = NULL;
  663         int todrop, acked;
  664         boolean_t ourfinisacked, needoutput = FALSE, delayed_dupack = FALSE;
  665         tcp_seq th_dupack = 0; /* XXX gcc warning */
  666         u_int to_flags = 0; /* XXX gcc warning */
  667         u_long tiwin;
  668         int recvwin;
  669         struct tcpopt to;               /* options in this segment */
  670         struct sockaddr_in *next_hop = NULL;
  671         int rstreason; /* For badport_bandlim accounting purposes */
  672         int cpu;
  673         struct ip6_hdr *ip6 = NULL;
  674         struct mbuf *m;
  675 #ifdef INET6
  676         boolean_t isipv6;
  677 #else
  678         const boolean_t isipv6 = FALSE;
  679 #endif
  680 #ifdef TCPDEBUG
  681         short ostate = 0;
  682 #endif
  683 
  684         off0 = *offp;
  685         m = *mp;
  686         *mp = NULL;
  687 
  688         tcpstat.tcps_rcvtotal++;
  689 
  690         if (m->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
  691                 struct m_tag *mtag;
  692 
  693                 mtag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
  694                 KKASSERT(mtag != NULL);
  695                 next_hop = m_tag_data(mtag);
  696         }
  697 
  698 #ifdef INET6
  699         isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? TRUE : FALSE;
  700 #endif
  701 
  702         if (isipv6) {
  703                 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
  704                 ip6 = mtod(m, struct ip6_hdr *);
  705                 tlen = (sizeof *ip6) + ntohs(ip6->ip6_plen) - off0;
  706                 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
  707                         tcpstat.tcps_rcvbadsum++;
  708                         goto drop;
  709                 }
  710                 th = (struct tcphdr *)((caddr_t)ip6 + off0);
  711 
  712                 /*
  713                  * Be proactive about unspecified IPv6 address in source.
  714                  * As we use all-zero to indicate unbounded/unconnected pcb,
  715                  * unspecified IPv6 address can be used to confuse us.
  716                  *
  717                  * Note that packets with unspecified IPv6 destination is
  718                  * already dropped in ip6_input.
  719                  */
  720                 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
  721                         /* XXX stat */
  722                         goto drop;
  723                 }
  724         } else {
  725                 /*
  726                  * Get IP and TCP header together in first mbuf.
  727                  * Note: IP leaves IP header in first mbuf.
  728                  */
  729                 if (off0 > sizeof(struct ip)) {
  730                         ip_stripoptions(m);
  731                         off0 = sizeof(struct ip);
  732                 }
  733                 /* already checked and pulled up in ip_demux() */
  734                 KASSERT(m->m_len >= sizeof(struct tcpiphdr),
  735                     ("TCP header not in one mbuf: m->m_len %d", m->m_len));
  736                 ip = mtod(m, struct ip *);
  737                 ipov = (struct ipovly *)ip;
  738                 th = (struct tcphdr *)((caddr_t)ip + off0);
  739                 tlen = ip->ip_len;
  740 
  741                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
  742                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  743                                 th->th_sum = m->m_pkthdr.csum_data;
  744                         else
  745                                 th->th_sum = in_pseudo(ip->ip_src.s_addr,
  746                                                 ip->ip_dst.s_addr,
  747                                                 htonl(m->m_pkthdr.csum_data +
  748                                                         ip->ip_len +
  749                                                         IPPROTO_TCP));
  750                         th->th_sum ^= 0xffff;
  751                 } else {
  752                         /*
  753                          * Checksum extended TCP header and data.
  754                          */
  755                         len = sizeof(struct ip) + tlen;
  756                         bzero(ipov->ih_x1, sizeof ipov->ih_x1);
  757                         ipov->ih_len = (u_short)tlen;
  758                         ipov->ih_len = htons(ipov->ih_len);
  759                         th->th_sum = in_cksum(m, len);
  760                 }
  761                 if (th->th_sum) {
  762                         tcpstat.tcps_rcvbadsum++;
  763                         goto drop;
  764                 }
  765 #ifdef INET6
  766                 /* Re-initialization for later version check */
  767                 ip->ip_v = IPVERSION;
  768 #endif
  769         }
  770 
  771         /*
  772          * Check that TCP offset makes sense,
  773          * pull out TCP options and adjust length.              XXX
  774          */
  775         off = th->th_off << 2;
  776         /* already checked and pulled up in ip_demux() */
  777         KASSERT(off >= sizeof(struct tcphdr) && off <= tlen,
  778             ("bad TCP data offset %d (tlen %d)", off, tlen));
  779         tlen -= off;    /* tlen is used instead of ti->ti_len */
  780         if (off > sizeof(struct tcphdr)) {
  781                 if (isipv6) {
  782                         IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
  783                         ip6 = mtod(m, struct ip6_hdr *);
  784                         th = (struct tcphdr *)((caddr_t)ip6 + off0);
  785                 } else {
  786                         /* already pulled up in ip_demux() */
  787                         KASSERT(m->m_len >= sizeof(struct ip) + off,
  788                             ("TCP header and options not in one mbuf: "
  789                              "m_len %d, off %d", m->m_len, off));
  790                 }
  791                 optlen = off - sizeof(struct tcphdr);
  792                 optp = (u_char *)(th + 1);
  793         }
  794         thflags = th->th_flags;
  795 
  796 #ifdef TCP_DROP_SYNFIN
  797         /*
  798          * If the drop_synfin option is enabled, drop all packets with
  799          * both the SYN and FIN bits set. This prevents e.g. nmap from
  800          * identifying the TCP/IP stack.
  801          *
  802          * This is a violation of the TCP specification.
  803          */
  804         if (drop_synfin && (thflags & (TH_SYN | TH_FIN)) == (TH_SYN | TH_FIN))
  805                 goto drop;
  806 #endif
  807 
  808         /*
  809          * Convert TCP protocol specific fields to host format.
  810          */
  811         th->th_seq = ntohl(th->th_seq);
  812         th->th_ack = ntohl(th->th_ack);
  813         th->th_win = ntohs(th->th_win);
  814         th->th_urp = ntohs(th->th_urp);
  815 
  816         /*
  817          * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
  818          * until after ip6_savecontrol() is called and before other functions
  819          * which don't want those proto headers.
  820          * Because ip6_savecontrol() is going to parse the mbuf to
  821          * search for data to be passed up to user-land, it wants mbuf
  822          * parameters to be unchanged.
  823          * XXX: the call of ip6_savecontrol() has been obsoleted based on
  824          * latest version of the advanced API (20020110).
  825          */
  826         drop_hdrlen = off0 + off;
  827 
  828         /*
  829          * Locate pcb for segment.
  830          */
  831 findpcb:
  832         /* IPFIREWALL_FORWARD section */
  833         if (next_hop != NULL && !isipv6) {  /* IPv6 support is not there yet */
  834                 /*
  835                  * Transparently forwarded. Pretend to be the destination.
  836                  * already got one like this?
  837                  */
  838                 cpu = mycpu->gd_cpuid;
  839                 inp = in_pcblookup_hash(&tcbinfo[cpu],
  840                                         ip->ip_src, th->th_sport,
  841                                         ip->ip_dst, th->th_dport,
  842                                         0, m->m_pkthdr.rcvif);
  843                 if (!inp) {
  844                         /*
  845                          * It's new.  Try to find the ambushing socket.
  846                          */
  847 
  848                         /*
  849                          * The rest of the ipfw code stores the port in
  850                          * host order.  XXX
  851                          * (The IP address is still in network order.)
  852                          */
  853                         in_port_t dport = next_hop->sin_port ?
  854                                                 htons(next_hop->sin_port) :
  855                                                 th->th_dport;
  856 
  857                         cpu = tcp_addrcpu(ip->ip_src.s_addr, th->th_sport,
  858                                           next_hop->sin_addr.s_addr, dport);
  859                         inp = in_pcblookup_hash(&tcbinfo[cpu],
  860                                                 ip->ip_src, th->th_sport,
  861                                                 next_hop->sin_addr, dport,
  862                                                 1, m->m_pkthdr.rcvif);
  863                 }
  864         } else {
  865                 if (isipv6) {
  866                         inp = in6_pcblookup_hash(&tcbinfo[0],
  867                                                  &ip6->ip6_src, th->th_sport,
  868                                                  &ip6->ip6_dst, th->th_dport,
  869                                                  1, m->m_pkthdr.rcvif);
  870                 } else {
  871                         cpu = mycpu->gd_cpuid;
  872                         inp = in_pcblookup_pkthash(&tcbinfo[cpu],
  873                             ip->ip_src, th->th_sport,
  874                             ip->ip_dst, th->th_dport,
  875                             1, m->m_pkthdr.rcvif,
  876                             tcp_reuseport_ext ? m : NULL);
  877                 }
  878         }
  879 
  880         /*
  881          * If the state is CLOSED (i.e., TCB does not exist) then
  882          * all data in the incoming segment is discarded.
  883          * If the TCB exists but is in CLOSED state, it is embryonic,
  884          * but should either do a listen or a connect soon.
  885          */
  886         if (inp == NULL) {
  887                 if (log_in_vain) {
  888 #ifdef INET6
  889                         char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
  890 #else
  891                         char dbuf[sizeof "aaa.bbb.ccc.ddd"];
  892                         char sbuf[sizeof "aaa.bbb.ccc.ddd"];
  893 #endif
  894                         if (isipv6) {
  895                                 strcpy(dbuf, "[");
  896                                 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
  897                                 strcat(dbuf, "]");
  898                                 strcpy(sbuf, "[");
  899                                 strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
  900                                 strcat(sbuf, "]");
  901                         } else {
  902                                 strcpy(dbuf, inet_ntoa(ip->ip_dst));
  903                                 strcpy(sbuf, inet_ntoa(ip->ip_src));
  904                         }
  905                         switch (log_in_vain) {
  906                         case 1:
  907                                 if (!(thflags & TH_SYN))
  908                                         break;
  909                         case 2:
  910                                 log(LOG_INFO,
  911                                     "Connection attempt to TCP %s:%d "
  912                                     "from %s:%d flags:0x%02x\n",
  913                                     dbuf, ntohs(th->th_dport), sbuf,
  914                                     ntohs(th->th_sport), thflags);
  915                                 break;
  916                         default:
  917                                 break;
  918                         }
  919                 }
  920                 if (blackhole) {
  921                         switch (blackhole) {
  922                         case 1:
  923                                 if (thflags & TH_SYN)
  924                                         goto drop;
  925                                 break;
  926                         case 2:
  927                                 goto drop;
  928                         default:
  929                                 goto drop;
  930                         }
  931                 }
  932                 rstreason = BANDLIM_RST_CLOSEDPORT;
  933                 goto dropwithreset;
  934         }
  935 
  936 #ifdef IPSEC
  937         if (isipv6) {
  938                 if (ipsec6_in_reject_so(m, inp->inp_socket)) {
  939                         ipsec6stat.in_polvio++;
  940                         goto drop;
  941                 }
  942         } else {
  943                 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
  944                         ipsecstat.in_polvio++;
  945                         goto drop;
  946                 }
  947         }
  948 #endif
  949 #ifdef FAST_IPSEC
  950         if (isipv6) {
  951                 if (ipsec6_in_reject(m, inp))
  952                         goto drop;
  953         } else {
  954                 if (ipsec4_in_reject(m, inp))
  955                         goto drop;
  956         }
  957 #endif
  958         /* Check the minimum TTL for socket. */
  959 #ifdef INET6
  960         if ((isipv6 ? ip6->ip6_hlim : ip->ip_ttl) < inp->inp_ip_minttl)
  961                 goto drop;
  962 #endif
  963 
  964         tp = intotcpcb(inp);
  965         if (tp == NULL) {
  966                 rstreason = BANDLIM_RST_CLOSEDPORT;
  967                 goto dropwithreset;
  968         }
  969         if (tp->t_state <= TCPS_CLOSED)
  970                 goto drop;
  971 
  972         so = inp->inp_socket;
  973 
  974 #ifdef TCPDEBUG
  975         if (so->so_options & SO_DEBUG) {
  976                 ostate = tp->t_state;
  977                 if (isipv6)
  978                         bcopy(ip6, tcp_saveipgen, sizeof(*ip6));
  979                 else
  980                         bcopy(ip, tcp_saveipgen, sizeof(*ip));
  981                 tcp_savetcp = *th;
  982         }
  983 #endif
  984 
  985         bzero(&to, sizeof to);
  986 
  987         if (so->so_options & SO_ACCEPTCONN) {
  988                 struct in_conninfo inc;
  989 
  990 #ifdef INET6
  991                 inc.inc_isipv6 = (isipv6 == TRUE);
  992 #endif
  993                 if (isipv6) {
  994                         inc.inc6_faddr = ip6->ip6_src;
  995                         inc.inc6_laddr = ip6->ip6_dst;
  996                         inc.inc6_route.ro_rt = NULL;            /* XXX */
  997                 } else {
  998                         inc.inc_faddr = ip->ip_src;
  999                         inc.inc_laddr = ip->ip_dst;
 1000                         inc.inc_route.ro_rt = NULL;             /* XXX */
 1001                 }
 1002                 inc.inc_fport = th->th_sport;
 1003                 inc.inc_lport = th->th_dport;
 1004 
 1005                 /*
 1006                  * If the state is LISTEN then ignore segment if it contains
 1007                  * a RST.  If the segment contains an ACK then it is bad and
 1008                  * send a RST.  If it does not contain a SYN then it is not
 1009                  * interesting; drop it.
 1010                  *
 1011                  * If the state is SYN_RECEIVED (syncache) and seg contains
 1012                  * an ACK, but not for our SYN/ACK, send a RST.  If the seg
 1013                  * contains a RST, check the sequence number to see if it
 1014                  * is a valid reset segment.
 1015                  */
 1016                 if ((thflags & (TH_RST | TH_ACK | TH_SYN)) != TH_SYN) {
 1017                         if ((thflags & (TH_RST | TH_ACK | TH_SYN)) == TH_ACK) {
 1018                                 if (!syncache_expand(&inc, th, &so, m)) {
 1019                                         /*
 1020                                          * No syncache entry, or ACK was not
 1021                                          * for our SYN/ACK.  Send a RST.
 1022                                          */
 1023                                         tcpstat.tcps_badsyn++;
 1024                                         rstreason = BANDLIM_RST_OPENPORT;
 1025                                         goto dropwithreset;
 1026                                 }
 1027 
 1028                                 /*
 1029                                  * Could not complete 3-way handshake,
 1030                                  * connection is being closed down, and
 1031                                  * syncache will free mbuf.
 1032                                  */
 1033                                 if (so == NULL)
 1034                                         return(IPPROTO_DONE);
 1035 
 1036                                 /*
 1037                                  * We must be in the correct protocol thread
 1038                                  * for this connection.
 1039                                  */
 1040                                 KKASSERT(so->so_port == &curthread->td_msgport);
 1041 
 1042                                 /*
 1043                                  * Socket is created in state SYN_RECEIVED.
 1044                                  * Continue processing segment.
 1045                                  */
 1046                                 inp = so->so_pcb;
 1047                                 tp = intotcpcb(inp);
 1048                                 /*
 1049                                  * This is what would have happened in
 1050                                  * tcp_output() when the SYN,ACK was sent.
 1051                                  */
 1052                                 tp->snd_up = tp->snd_una;
 1053                                 tp->snd_max = tp->snd_nxt = tp->iss + 1;
 1054                                 tp->last_ack_sent = tp->rcv_nxt;
 1055 
 1056                                 goto after_listen;
 1057                         }
 1058                         if (thflags & TH_RST) {
 1059                                 syncache_chkrst(&inc, th);
 1060                                 goto drop;
 1061                         }
 1062                         if (thflags & TH_ACK) {
 1063                                 syncache_badack(&inc);
 1064                                 tcpstat.tcps_badsyn++;
 1065                                 rstreason = BANDLIM_RST_OPENPORT;
 1066                                 goto dropwithreset;
 1067                         }
 1068                         goto drop;
 1069                 }
 1070 
 1071                 /*
 1072                  * Segment's flags are (SYN) or (SYN | FIN).
 1073                  */
 1074 #ifdef INET6
 1075                 /*
 1076                  * If deprecated address is forbidden,
 1077                  * we do not accept SYN to deprecated interface
 1078                  * address to prevent any new inbound connection from
 1079                  * getting established.
 1080                  * When we do not accept SYN, we send a TCP RST,
 1081                  * with deprecated source address (instead of dropping
 1082                  * it).  We compromise it as it is much better for peer
 1083                  * to send a RST, and RST will be the final packet
 1084                  * for the exchange.
 1085                  *
 1086                  * If we do not forbid deprecated addresses, we accept
 1087                  * the SYN packet.  RFC2462 does not suggest dropping
 1088                  * SYN in this case.
 1089                  * If we decipher RFC2462 5.5.4, it says like this:
 1090                  * 1. use of deprecated addr with existing
 1091                  *    communication is okay - "SHOULD continue to be
 1092                  *    used"
 1093                  * 2. use of it with new communication:
 1094                  *   (2a) "SHOULD NOT be used if alternate address
 1095                  *        with sufficient scope is available"
 1096                  *   (2b) nothing mentioned otherwise.
 1097                  * Here we fall into (2b) case as we have no choice in
 1098                  * our source address selection - we must obey the peer.
 1099                  *
 1100                  * The wording in RFC2462 is confusing, and there are
 1101                  * multiple description text for deprecated address
 1102                  * handling - worse, they are not exactly the same.
 1103                  * I believe 5.5.4 is the best one, so we follow 5.5.4.
 1104                  */
 1105                 if (isipv6 && !ip6_use_deprecated) {
 1106                         struct in6_ifaddr *ia6;
 1107 
 1108                         if ((ia6 = ip6_getdstifaddr(m)) &&
 1109                             (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
 1110                                 tp = NULL;
 1111                                 rstreason = BANDLIM_RST_OPENPORT;
 1112                                 goto dropwithreset;
 1113                         }
 1114                 }
 1115 #endif
 1116                 /*
 1117                  * If it is from this socket, drop it, it must be forged.
 1118                  * Don't bother responding if the destination was a broadcast.
 1119                  */
 1120                 if (th->th_dport == th->th_sport) {
 1121                         if (isipv6) {
 1122                                 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
 1123                                                        &ip6->ip6_src))
 1124                                         goto drop;
 1125                         } else {
 1126                                 if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
 1127                                         goto drop;
 1128                         }
 1129                 }
 1130                 /*
 1131                  * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
 1132                  *
 1133                  * Note that it is quite possible to receive unicast
 1134                  * link-layer packets with a broadcast IP address. Use
 1135                  * in_broadcast() to find them.
 1136                  */
 1137                 if (m->m_flags & (M_BCAST | M_MCAST))
 1138                         goto drop;
 1139                 if (isipv6) {
 1140                         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
 1141                             IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
 1142                                 goto drop;
 1143                 } else {
 1144                         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
 1145                             IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
 1146                             ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
 1147                             in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
 1148                                 goto drop;
 1149                 }
 1150                 /*
 1151                  * SYN appears to be valid; create compressed TCP state
 1152                  * for syncache, or perform t/tcp connection.
 1153                  */
 1154                 if (so->so_qlen <= so->so_qlimit) {
 1155                         tcp_dooptions(&to, optp, optlen, TRUE, th->th_ack);
 1156                         if (!syncache_add(&inc, &to, th, so, m))
 1157                                 goto drop;
 1158 
 1159                         /*
 1160                          * Entry added to syncache, mbuf used to
 1161                          * send SYN,ACK packet.
 1162                          */
 1163                         return(IPPROTO_DONE);
 1164                 }
 1165                 goto drop;
 1166         }
 1167 
 1168 after_listen:
 1169         /*
 1170          * Should not happen - syncache should pick up these connections.
 1171          *
 1172          * Once we are past handling listen sockets we must be in the
 1173          * correct protocol processing thread.
 1174          */
 1175         KASSERT(tp->t_state != TCPS_LISTEN, ("tcp_input: TCPS_LISTEN state"));
 1176         KKASSERT(so->so_port == &curthread->td_msgport);
 1177 
 1178         /* Unscale the window into a 32-bit value. */
 1179         if (!(thflags & TH_SYN))
 1180                 tiwin = th->th_win << tp->snd_scale;
 1181         else
 1182                 tiwin = th->th_win;
 1183 
 1184         /*
 1185          * This is the second part of the MSS DoS prevention code (after
 1186          * minmss on the sending side) and it deals with too many too small
 1187          * tcp packets in a too short timeframe (1 second).
 1188          *
 1189          * XXX Removed.  This code was crap.  It does not scale to network
 1190          *     speed, and default values break NFS.  Gone.
 1191          */
 1192         /* REMOVED */
 1193 
 1194         /*
 1195          * Segment received on connection.
 1196          *
 1197          * Reset idle time and keep-alive timer.  Don't waste time if less
 1198          * then a second has elapsed.
 1199          */
 1200         if ((int)(ticks - tp->t_rcvtime) > hz)
 1201                 tcp_timer_keep_activity(tp, thflags);
 1202 
 1203         /*
 1204          * Process options.
 1205          * XXX this is tradtitional behavior, may need to be cleaned up.
 1206          */
 1207         tcp_dooptions(&to, optp, optlen, (thflags & TH_SYN) != 0, th->th_ack);
 1208         if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
 1209                 if ((to.to_flags & TOF_SCALE) && (tp->t_flags & TF_REQ_SCALE)) {
 1210                         tp->t_flags |= TF_RCVD_SCALE;
 1211                         tp->snd_scale = to.to_requested_s_scale;
 1212                 }
 1213 
 1214                 /*
 1215                  * Initial send window; will be updated upon next ACK
 1216                  */
 1217                 tp->snd_wnd = th->th_win;
 1218 
 1219                 if (to.to_flags & TOF_TS) {
 1220                         tp->t_flags |= TF_RCVD_TSTMP;
 1221                         tp->ts_recent = to.to_tsval;
 1222                         tp->ts_recent_age = ticks;
 1223                 }
 1224                 if (!(to.to_flags & TOF_MSS))
 1225                         to.to_mss = 0;
 1226                 tcp_mss(tp, to.to_mss);
 1227                 /*
 1228                  * Only set the TF_SACK_PERMITTED per-connection flag
 1229                  * if we got a SACK_PERMITTED option from the other side
 1230                  * and the global tcp_do_sack variable is true.
 1231                  */
 1232                 if (tcp_do_sack && (to.to_flags & TOF_SACK_PERMITTED))
 1233                         tp->t_flags |= TF_SACK_PERMITTED;
 1234         }
 1235 
 1236         /*
 1237          * Header prediction: check for the two common cases
 1238          * of a uni-directional data xfer.  If the packet has
 1239          * no control flags, is in-sequence, the window didn't
 1240          * change and we're not retransmitting, it's a
 1241          * candidate.  If the length is zero and the ack moved
 1242          * forward, we're the sender side of the xfer.  Just
 1243          * free the data acked & wake any higher level process
 1244          * that was blocked waiting for space.  If the length
 1245          * is non-zero and the ack didn't move, we're the
 1246          * receiver side.  If we're getting packets in-order
 1247          * (the reassembly queue is empty), add the data to
 1248          * the socket buffer and note that we need a delayed ack.
 1249          * Make sure that the hidden state-flags are also off.
 1250          * Since we check for TCPS_ESTABLISHED above, it can only
 1251          * be TH_NEEDSYN.
 1252          */
 1253         if (tp->t_state == TCPS_ESTABLISHED &&
 1254             (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
 1255             !(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)) &&
 1256             (!(to.to_flags & TOF_TS) ||
 1257              TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
 1258             th->th_seq == tp->rcv_nxt &&
 1259             tp->snd_nxt == tp->snd_max) {
 1260 
 1261                 /*
 1262                  * If last ACK falls within this segment's sequence numbers,
 1263                  * record the timestamp.
 1264                  * NOTE that the test is modified according to the latest
 1265                  * proposal of the tcplw@cray.com list (Braden 1993/04/26).
 1266                  */
 1267                 if ((to.to_flags & TOF_TS) &&
 1268                     SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
 1269                         tp->ts_recent_age = ticks;
 1270                         tp->ts_recent = to.to_tsval;
 1271                 }
 1272 
 1273                 if (tlen == 0) {
 1274                         if (SEQ_GT(th->th_ack, tp->snd_una) &&
 1275                             SEQ_LEQ(th->th_ack, tp->snd_max) &&
 1276                             tp->snd_cwnd >= tp->snd_wnd &&
 1277                             !IN_FASTRECOVERY(tp)) {
 1278                                 /*
 1279                                  * This is a pure ack for outstanding data.
 1280                                  */
 1281                                 ++tcpstat.tcps_predack;
 1282                                 /*
 1283                                  * "bad retransmit" recovery
 1284                                  *
 1285                                  * If Eifel detection applies, then
 1286                                  * it is deterministic, so use it
 1287                                  * unconditionally over the old heuristic.
 1288                                  * Otherwise, fall back to the old heuristic.
 1289                                  */
 1290                                 if (tcp_do_eifel_detect &&
 1291                                     (to.to_flags & TOF_TS) && to.to_tsecr &&
 1292                                     (tp->rxt_flags & TRXT_F_FIRSTACCACK)) {
 1293                                         /* Eifel detection applicable. */
 1294                                         if (to.to_tsecr < tp->t_rexmtTS) {
 1295                                                 tcp_revert_congestion_state(tp);
 1296                                                 ++tcpstat.tcps_eifeldetected;
 1297                                                 if (tp->t_rxtshift != 1 ||
 1298                                                     ticks >= tp->t_badrxtwin)
 1299                                                         ++tcpstat.tcps_rttcantdetect;
 1300                                         }
 1301                                 } else if (tp->t_rxtshift == 1 &&
 1302                                            ticks < tp->t_badrxtwin) {
 1303                                         tcp_revert_congestion_state(tp);
 1304                                         ++tcpstat.tcps_rttdetected;
 1305                                 }
 1306                                 tp->rxt_flags &= ~(TRXT_F_FIRSTACCACK |
 1307                                     TRXT_F_FASTREXMT | TRXT_F_EARLYREXMT);
 1308                                 /*
 1309                                  * Recalculate the retransmit timer / rtt.
 1310                                  *
 1311                                  * Some machines (certain windows boxes)
 1312                                  * send broken timestamp replies during the
 1313                                  * SYN+ACK phase, ignore timestamps of 0.
 1314                                  */
 1315                                 if ((to.to_flags & TOF_TS) && to.to_tsecr) {
 1316                                         tcp_xmit_timer(tp,
 1317                                             ticks - to.to_tsecr + 1,
 1318                                             th->th_ack);
 1319                                 } else if (tp->t_rtttime &&
 1320                                            SEQ_GT(th->th_ack, tp->t_rtseq)) {
 1321                                         tcp_xmit_timer(tp,
 1322                                             ticks - tp->t_rtttime,
 1323                                             th->th_ack);
 1324                                 }
 1325                                 tcp_xmit_bandwidth_limit(tp, th->th_ack);
 1326                                 acked = th->th_ack - tp->snd_una;
 1327                                 tcpstat.tcps_rcvackpack++;
 1328                                 tcpstat.tcps_rcvackbyte += acked;
 1329                                 sbdrop(&so->so_snd.sb, acked);
 1330                                 tp->snd_recover = th->th_ack - 1;
 1331                                 tp->snd_una = th->th_ack;
 1332                                 tp->t_dupacks = 0;
 1333                                 /*
 1334                                  * Update window information.
 1335                                  */
 1336                                 if (tiwin != tp->snd_wnd &&
 1337                                     acceptable_window_update(tp, th, tiwin)) {
 1338                                         /* keep track of pure window updates */
 1339                                         if (tp->snd_wl2 == th->th_ack &&
 1340                                             tiwin > tp->snd_wnd)
 1341                                                 tcpstat.tcps_rcvwinupd++;
 1342                                         tp->snd_wnd = tiwin;
 1343                                         tp->snd_wl1 = th->th_seq;
 1344                                         tp->snd_wl2 = th->th_ack;
 1345                                         if (tp->snd_wnd > tp->max_sndwnd)
 1346                                                 tp->max_sndwnd = tp->snd_wnd;
 1347                                 }
 1348                                 m_freem(m);
 1349                                 ND6_HINT(tp); /* some progress has been done */
 1350                                 /*
 1351                                  * If all outstanding data are acked, stop
 1352                                  * retransmit timer, otherwise restart timer
 1353                                  * using current (possibly backed-off) value.
 1354                                  * If process is waiting for space,
 1355                                  * wakeup/selwakeup/signal.  If data
 1356                                  * are ready to send, let tcp_output
 1357                                  * decide between more output or persist.
 1358                                  */
 1359                                 if (tp->snd_una == tp->snd_max) {
 1360                                         tcp_callout_stop(tp, tp->tt_rexmt);
 1361                                 } else if (!tcp_callout_active(tp,
 1362                                             tp->tt_persist)) {
 1363                                         tcp_callout_reset(tp, tp->tt_rexmt,
 1364                                             tp->t_rxtcur, tcp_timer_rexmt);
 1365                                 }
 1366                                 sowwakeup(so);
 1367                                 if (so->so_snd.ssb_cc > 0 &&
 1368                                     !tcp_output_pending(tp))
 1369                                         tcp_output_fair(tp);
 1370                                 return(IPPROTO_DONE);
 1371                         }
 1372                 } else if (tiwin == tp->snd_wnd &&
 1373                     th->th_ack == tp->snd_una &&
 1374                     TAILQ_EMPTY(&tp->t_segq) &&
 1375                     tlen <= ssb_space(&so->so_rcv)) {
 1376                         u_long newsize = 0;     /* automatic sockbuf scaling */
 1377                         /*
 1378                          * This is a pure, in-sequence data packet
 1379                          * with nothing on the reassembly queue and
 1380                          * we have enough buffer space to take it.
 1381                          */
 1382                         ++tcpstat.tcps_preddat;
 1383                         tp->rcv_nxt += tlen;
 1384                         tcpstat.tcps_rcvpack++;
 1385                         tcpstat.tcps_rcvbyte += tlen;
 1386                         ND6_HINT(tp);   /* some progress has been done */
 1387                 /*
 1388                  * Automatic sizing of receive socket buffer.  Often the send
 1389                  * buffer size is not optimally adjusted to the actual network
 1390                  * conditions at hand (delay bandwidth product).  Setting the
 1391                  * buffer size too small limits throughput on links with high
 1392                  * bandwidth and high delay (eg. trans-continental/oceanic links).
 1393                  *
 1394                  * On the receive side the socket buffer memory is only rarely
 1395                  * used to any significant extent.  This allows us to be much
 1396                  * more aggressive in scaling the receive socket buffer.  For
 1397                  * the case that the buffer space is actually used to a large
 1398                  * extent and we run out of kernel memory we can simply drop
 1399                  * the new segments; TCP on the sender will just retransmit it
 1400                  * later.  Setting the buffer size too big may only consume too
 1401                  * much kernel memory if the application doesn't read() from
 1402                  * the socket or packet loss or reordering makes use of the
 1403                  * reassembly queue.
 1404                  *
 1405                  * The criteria to step up the receive buffer one notch are:
 1406                  *  1. the number of bytes received during the time it takes
 1407                  *     one timestamp to be reflected back to us (the RTT);
 1408                  *  2. received bytes per RTT is within seven eighth of the
 1409                  *     current socket buffer size;
 1410                  *  3. receive buffer size has not hit maximal automatic size;
 1411                  *
 1412                  * This algorithm does one step per RTT at most and only if
 1413                  * we receive a bulk stream w/o packet losses or reorderings.
 1414                  * Shrinking the buffer during idle times is not necessary as
 1415                  * it doesn't consume any memory when idle.
 1416                  *
 1417                  * TODO: Only step up if the application is actually serving
 1418                  * the buffer to better manage the socket buffer resources.
 1419                  */
 1420                         if (tcp_do_autorcvbuf &&
 1421                             to.to_tsecr &&
 1422                             (so->so_rcv.ssb_flags & SSB_AUTOSIZE)) {
 1423                                 if (to.to_tsecr > tp->rfbuf_ts &&
 1424                                     to.to_tsecr - tp->rfbuf_ts < hz) {
 1425                                         if (tp->rfbuf_cnt >
 1426                                             (so->so_rcv.ssb_hiwat / 8 * 7) &&
 1427                                             so->so_rcv.ssb_hiwat <
 1428                                             tcp_autorcvbuf_max) {
 1429                                                 newsize =
 1430                                                     ulmin(so->so_rcv.ssb_hiwat +
 1431                                                           tcp_autorcvbuf_inc,
 1432                                                           tcp_autorcvbuf_max);
 1433                                         }
 1434                                         /* Start over with next RTT. */
 1435                                         tp->rfbuf_ts = 0;
 1436                                         tp->rfbuf_cnt = 0;
 1437                                 } else
 1438                                         tp->rfbuf_cnt += tlen;  /* add up */
 1439                         }
 1440                         /*
 1441                          * Add data to socket buffer.
 1442                          */
 1443                         if (so->so_state & SS_CANTRCVMORE) {
 1444                                 m_freem(m);
 1445                         } else {
 1446                                 /*
 1447                                  * Set new socket buffer size, give up when
 1448                                  * limit is reached.
 1449                                  *
 1450                                  * Adjusting the size can mess up ACK
 1451                                  * sequencing when pure window updates are
 1452                                  * being avoided (which is the default),
 1453                                  * so force an ack.
 1454                                  */
 1455                                 lwkt_gettoken(&so->so_rcv.ssb_token);
 1456                                 if (newsize) {
 1457                                         tp->t_flags |= TF_RXRESIZED;
 1458                                         if (!ssb_reserve(&so->so_rcv, newsize,
 1459                                                          so, NULL)) {
 1460                                                 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
 1461                                         }
 1462                                         if (newsize >=
 1463                                             (TCP_MAXWIN << tp->rcv_scale)) {
 1464                                                 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_AUTOSIZE);
 1465                                         }
 1466                                 }
 1467                                 m_adj(m, drop_hdrlen); /* delayed header drop */
 1468                                 ssb_appendstream(&so->so_rcv, m);
 1469                                 lwkt_reltoken(&so->so_rcv.ssb_token);
 1470                         }
 1471                         sorwakeup(so);
 1472                         /*
 1473                          * This code is responsible for most of the ACKs
 1474                          * the TCP stack sends back after receiving a data
 1475                          * packet.  Note that the DELAY_ACK check fails if
 1476                          * the delack timer is already running, which results
 1477                          * in an ack being sent every other packet (which is
 1478                          * what we want).
 1479                          *
 1480                          * We then further aggregate acks by not actually
 1481                          * sending one until the protocol thread has completed
 1482                          * processing the current backlog of packets.  This
 1483                          * does not delay the ack any further, but allows us
 1484                          * to take advantage of the packet aggregation that
 1485                          * high speed NICs do (usually blocks of 8-10 packets)
 1486                          * to send a single ack rather then four or five acks,
 1487                          * greatly reducing the ack rate, the return channel
 1488                          * bandwidth, and the protocol overhead on both ends.
 1489                          *
 1490                          * Since this also has the effect of slowing down
 1491                          * the exponential slow-start ramp-up, systems with 
 1492                          * very large bandwidth-delay products might want
 1493                          * to turn the feature off.
 1494                          */
 1495                         if (DELAY_ACK(tp)) {
 1496                                 tcp_callout_reset(tp, tp->tt_delack,
 1497                                     tcp_delacktime, tcp_timer_delack);
 1498                         } else if (tcp_aggregate_acks) {
 1499                                 tp->t_flags |= TF_ACKNOW;
 1500                                 if (!(tp->t_flags & TF_ONOUTPUTQ)) {
 1501                                         tp->t_flags |= TF_ONOUTPUTQ;
 1502                                         tp->tt_cpu = mycpu->gd_cpuid;
 1503                                         TAILQ_INSERT_TAIL(
 1504                                             &tcpcbackq[tp->tt_cpu],
 1505                                             tp, t_outputq);
 1506                                 }
 1507                         } else {
 1508                                 tp->t_flags |= TF_ACKNOW;
 1509                                 tcp_output(tp);
 1510                         }
 1511                         return(IPPROTO_DONE);
 1512                 }
 1513         }
 1514 
 1515         /*
 1516          * Calculate amount of space in receive window,
 1517          * and then do TCP input processing.
 1518          * Receive window is amount of space in rcv queue,
 1519          * but not less than advertised window.
 1520          */
 1521         recvwin = ssb_space(&so->so_rcv);
 1522         if (recvwin < 0)
 1523                 recvwin = 0;
 1524         tp->rcv_wnd = imax(recvwin, (int)(tp->rcv_adv - tp->rcv_nxt));
 1525 
 1526         /* Reset receive buffer auto scaling when not in bulk receive mode. */
 1527         tp->rfbuf_ts = 0;
 1528         tp->rfbuf_cnt = 0;
 1529 
 1530         switch (tp->t_state) {
 1531         /*
 1532          * If the state is SYN_RECEIVED:
 1533          *      if seg contains an ACK, but not for our SYN/ACK, send a RST.
 1534          */
 1535         case TCPS_SYN_RECEIVED:
 1536                 if ((thflags & TH_ACK) &&
 1537                     (SEQ_LEQ(th->th_ack, tp->snd_una) ||
 1538                      SEQ_GT(th->th_ack, tp->snd_max))) {
 1539                                 rstreason = BANDLIM_RST_OPENPORT;
 1540                                 goto dropwithreset;
 1541                 }
 1542                 break;
 1543 
 1544         /*
 1545          * If the state is SYN_SENT:
 1546          *      if seg contains an ACK, but not for our SYN, drop the input.
 1547          *      if seg contains a RST, then drop the connection.
 1548          *      if seg does not contain SYN, then drop it.
 1549          * Otherwise this is an acceptable SYN segment
 1550          *      initialize tp->rcv_nxt and tp->irs
 1551          *      if seg contains ack then advance tp->snd_una
 1552          *      if SYN has been acked change to ESTABLISHED else SYN_RCVD state
 1553          *      arrange for segment to be acked (eventually)
 1554          *      continue processing rest of data/controls, beginning with URG
 1555          */
 1556         case TCPS_SYN_SENT:
 1557                 if ((thflags & TH_ACK) &&
 1558                     (SEQ_LEQ(th->th_ack, tp->iss) ||
 1559                      SEQ_GT(th->th_ack, tp->snd_max))) {
 1560                         rstreason = BANDLIM_UNLIMITED;
 1561                         goto dropwithreset;
 1562                 }
 1563                 if (thflags & TH_RST) {
 1564                         if (thflags & TH_ACK)
 1565                                 tp = tcp_drop(tp, ECONNREFUSED);
 1566                         goto drop;
 1567                 }
 1568                 if (!(thflags & TH_SYN))
 1569                         goto drop;
 1570 
 1571                 tp->irs = th->th_seq;
 1572                 tcp_rcvseqinit(tp);
 1573                 if (thflags & TH_ACK) {
 1574                         /* Our SYN was acked. */
 1575                         tcpstat.tcps_connects++;
 1576                         soisconnected(so);
 1577                         /* Do window scaling on this connection? */
 1578                         if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
 1579                             (TF_RCVD_SCALE | TF_REQ_SCALE))
 1580                                 tp->rcv_scale = tp->request_r_scale;
 1581                         tp->rcv_adv += tp->rcv_wnd;
 1582                         tp->snd_una++;          /* SYN is acked */
 1583                         tcp_callout_stop(tp, tp->tt_rexmt);
 1584                         /*
 1585                          * If there's data, delay ACK; if there's also a FIN
 1586                          * ACKNOW will be turned on later.
 1587                          */
 1588                         if (DELAY_ACK(tp) && tlen != 0) {
 1589                                 tcp_callout_reset(tp, tp->tt_delack,
 1590                                     tcp_delacktime, tcp_timer_delack);
 1591                         } else {
 1592                                 tp->t_flags |= TF_ACKNOW;
 1593                         }
 1594                         /*
 1595                          * Received <SYN,ACK> in SYN_SENT[*] state.
 1596                          * Transitions:
 1597                          *      SYN_SENT  --> ESTABLISHED
 1598                          *      SYN_SENT* --> FIN_WAIT_1
 1599                          */
 1600                         tp->t_starttime = ticks;
 1601                         if (tp->t_flags & TF_NEEDFIN) {
 1602                                 tp->t_state = TCPS_FIN_WAIT_1;
 1603                                 tp->t_flags &= ~TF_NEEDFIN;
 1604                                 thflags &= ~TH_SYN;
 1605                         } else {
 1606                                 tcp_established(tp);
 1607                         }
 1608                 } else {
 1609                         /*
 1610                          * Received initial SYN in SYN-SENT[*] state =>
 1611                          * simultaneous open.
 1612                          * Do 3-way handshake:
 1613                          *        SYN-SENT -> SYN-RECEIVED
 1614                          *        SYN-SENT* -> SYN-RECEIVED*
 1615                          */
 1616                         tp->t_flags |= TF_ACKNOW;
 1617                         tcp_callout_stop(tp, tp->tt_rexmt);
 1618                         tp->t_state = TCPS_SYN_RECEIVED;
 1619                 }
 1620 
 1621                 /*
 1622                  * Advance th->th_seq to correspond to first data byte.
 1623                  * If data, trim to stay within window,
 1624                  * dropping FIN if necessary.
 1625                  */
 1626                 th->th_seq++;
 1627                 if (tlen > tp->rcv_wnd) {
 1628                         todrop = tlen - tp->rcv_wnd;
 1629                         m_adj(m, -todrop);
 1630                         tlen = tp->rcv_wnd;
 1631                         thflags &= ~TH_FIN;
 1632                         tcpstat.tcps_rcvpackafterwin++;
 1633                         tcpstat.tcps_rcvbyteafterwin += todrop;
 1634                 }
 1635                 tp->snd_wl1 = th->th_seq - 1;
 1636                 tp->rcv_up = th->th_seq;
 1637                 /*
 1638                  * Client side of transaction: already sent SYN and data.
 1639                  * If the remote host used T/TCP to validate the SYN,
 1640                  * our data will be ACK'd; if so, enter normal data segment
 1641                  * processing in the middle of step 5, ack processing.
 1642                  * Otherwise, goto step 6.
 1643                  */
 1644                 if (thflags & TH_ACK)
 1645                         goto process_ACK;
 1646 
 1647                 goto step6;
 1648 
 1649         /*
 1650          * If the state is LAST_ACK or CLOSING or TIME_WAIT:
 1651          *      do normal processing (we no longer bother with T/TCP).
 1652          */
 1653         case TCPS_LAST_ACK:
 1654         case TCPS_CLOSING:
 1655         case TCPS_TIME_WAIT:
 1656                 break;  /* continue normal processing */
 1657         }
 1658 
 1659         /*
 1660          * States other than LISTEN or SYN_SENT.
 1661          * First check the RST flag and sequence number since reset segments
 1662          * are exempt from the timestamp and connection count tests.  This
 1663          * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
 1664          * below which allowed reset segments in half the sequence space
 1665          * to fall though and be processed (which gives forged reset
 1666          * segments with a random sequence number a 50 percent chance of
 1667          * killing a connection).
 1668          * Then check timestamp, if present.
 1669          * Then check the connection count, if present.
 1670          * Then check that at least some bytes of segment are within
 1671          * receive window.  If segment begins before rcv_nxt,
 1672          * drop leading data (and SYN); if nothing left, just ack.
 1673          *
 1674          *
 1675          * If the RST bit is set, check the sequence number to see
 1676          * if this is a valid reset segment.
 1677          * RFC 793 page 37:
 1678          *   In all states except SYN-SENT, all reset (RST) segments
 1679          *   are validated by checking their SEQ-fields.  A reset is
 1680          *   valid if its sequence number is in the window.
 1681          * Note: this does not take into account delayed ACKs, so
 1682          *   we should test against last_ack_sent instead of rcv_nxt.
 1683          *   The sequence number in the reset segment is normally an
 1684          *   echo of our outgoing acknowledgement numbers, but some hosts
 1685          *   send a reset with the sequence number at the rightmost edge
 1686          *   of our receive window, and we have to handle this case.
 1687          * If we have multiple segments in flight, the intial reset
 1688          * segment sequence numbers will be to the left of last_ack_sent,
 1689          * but they will eventually catch up.
 1690          * In any case, it never made sense to trim reset segments to
 1691          * fit the receive window since RFC 1122 says:
 1692          *   4.2.2.12  RST Segment: RFC-793 Section 3.4
 1693          *
 1694          *    A TCP SHOULD allow a received RST segment to include data.
 1695          *
 1696          *    DISCUSSION
 1697          *         It has been suggested that a RST segment could contain
 1698          *         ASCII text that encoded and explained the cause of the
 1699          *         RST.  No standard has yet been established for such
 1700          *         data.
 1701          *
 1702          * If the reset segment passes the sequence number test examine
 1703          * the state:
 1704          *    SYN_RECEIVED STATE:
 1705          *      If passive open, return to LISTEN state.
 1706          *      If active open, inform user that connection was refused.
 1707          *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
 1708          *      Inform user that connection was reset, and close tcb.
 1709          *    CLOSING, LAST_ACK STATES:
 1710          *      Close the tcb.
 1711          *    TIME_WAIT STATE:
 1712          *      Drop the segment - see Stevens, vol. 2, p. 964 and
 1713          *      RFC 1337.
 1714          */
 1715         if (thflags & TH_RST) {
 1716                 if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
 1717                     SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
 1718                         switch (tp->t_state) {
 1719 
 1720                         case TCPS_SYN_RECEIVED:
 1721                                 so->so_error = ECONNREFUSED;
 1722                                 goto close;
 1723 
 1724                         case TCPS_ESTABLISHED:
 1725                         case TCPS_FIN_WAIT_1:
 1726                         case TCPS_FIN_WAIT_2:
 1727                         case TCPS_CLOSE_WAIT:
 1728                                 so->so_error = ECONNRESET;
 1729                         close:
 1730                                 tp->t_state = TCPS_CLOSED;
 1731                                 tcpstat.tcps_drops++;
 1732                                 tp = tcp_close(tp);
 1733                                 break;
 1734 
 1735                         case TCPS_CLOSING:
 1736                         case TCPS_LAST_ACK:
 1737                                 tp = tcp_close(tp);
 1738                                 break;
 1739 
 1740                         case TCPS_TIME_WAIT:
 1741                                 break;
 1742                         }
 1743                 }
 1744                 goto drop;
 1745         }
 1746 
 1747         /*
 1748          * RFC 1323 PAWS: If we have a timestamp reply on this segment
 1749          * and it's less than ts_recent, drop it.
 1750          */
 1751         if ((to.to_flags & TOF_TS) && tp->ts_recent != 0 &&
 1752             TSTMP_LT(to.to_tsval, tp->ts_recent)) {
 1753                 /* Check to see if ts_recent is over 24 days old.  */
 1754                 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
 1755                         /*
 1756                          * Invalidate ts_recent.  If this segment updates
 1757                          * ts_recent, the age will be reset later and ts_recent
 1758                          * will get a valid value.  If it does not, setting
 1759                          * ts_recent to zero will at least satisfy the
 1760                          * requirement that zero be placed in the timestamp
 1761                          * echo reply when ts_recent isn't valid.  The
 1762                          * age isn't reset until we get a valid ts_recent
 1763                          * because we don't want out-of-order segments to be
 1764                          * dropped when ts_recent is old.
 1765                          */
 1766                         tp->ts_recent = 0;
 1767                 } else if (tcp_paws_tolerance && tlen != 0 &&
 1768                     tp->t_state == TCPS_ESTABLISHED &&
 1769                     (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK&&
 1770                     !(tp->t_flags & (TF_NEEDSYN | TF_NEEDFIN)) &&
 1771                     th->th_ack == tp->snd_una &&
 1772                     tiwin == tp->snd_wnd &&
 1773                     TSTMP_GEQ(to.to_tsval + tcp_paws_tolerance, tp->ts_recent)&&
 1774                     (th->th_seq == tp->rcv_nxt ||
 1775                      (SEQ_GT(th->th_seq, tp->rcv_nxt) && 
 1776                       tcp_paws_canreasslast(tp, th, tlen)))) {
 1777                         /*
 1778                          * This tends to prevent valid new segments from being
 1779                          * dropped by the reordered segments sent by the fast
 1780                          * retransmission algorithm on the sending side, i.e.
 1781                          * the fast retransmitted segment w/ larger timestamp
 1782                          * arrives earlier than the previously sent new segments
 1783                          * w/ smaller timestamp.
 1784                          *
 1785                          * If following conditions are met, the segment is
 1786                          * accepted:
 1787                          * - The segment contains data
 1788                          * - The connection is established
 1789                          * - The header does not contain important flags
 1790                          * - SYN or FIN is not needed
 1791                          * - It does not acknowledge new data
 1792                          * - Receive window is not changed
 1793                          * - The timestamp is within "acceptable" range
 1794                          * - The new segment is what we are expecting or
 1795                          *   the new segment could be merged w/ the last
 1796                          *   pending segment on the reassemble queue
 1797                          */
 1798                         tcpstat.tcps_pawsaccept++;
 1799                         tcpstat.tcps_pawsdrop++;
 1800                 } else {
 1801                         tcpstat.tcps_rcvduppack++;
 1802                         tcpstat.tcps_rcvdupbyte += tlen;
 1803                         tcpstat.tcps_pawsdrop++;
 1804                         if (tlen)
 1805                                 goto dropafterack;
 1806                         goto drop;
 1807                 }
 1808         }
 1809 
 1810         /*
 1811          * In the SYN-RECEIVED state, validate that the packet belongs to
 1812          * this connection before trimming the data to fit the receive
 1813          * window.  Check the sequence number versus IRS since we know
 1814          * the sequence numbers haven't wrapped.  This is a partial fix
 1815          * for the "LAND" DoS attack.
 1816          */
 1817         if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
 1818                 rstreason = BANDLIM_RST_OPENPORT;
 1819                 goto dropwithreset;
 1820         }
 1821 
 1822         todrop = tp->rcv_nxt - th->th_seq;
 1823         if (todrop > 0) {
 1824                 if (TCP_DO_SACK(tp)) {
 1825                         /* Report duplicate segment at head of packet. */
 1826                         tp->reportblk.rblk_start = th->th_seq;
 1827                         tp->reportblk.rblk_end = TCP_SACK_BLKEND(
 1828                             th->th_seq + tlen, thflags);
 1829                         if (SEQ_GT(tp->reportblk.rblk_end, tp->rcv_nxt))
 1830                                 tp->reportblk.rblk_end = tp->rcv_nxt;
 1831                         tp->sack_flags |= (TSACK_F_DUPSEG | TSACK_F_SACKLEFT);
 1832                         tp->t_flags |= TF_ACKNOW;
 1833                 }
 1834                 if (thflags & TH_SYN) {
 1835                         thflags &= ~TH_SYN;
 1836                         th->th_seq++;
 1837                         if (th->th_urp > 1)
 1838                                 th->th_urp--;
 1839                         else
 1840                                 thflags &= ~TH_URG;
 1841                         todrop--;
 1842                 }
 1843                 /*
 1844                  * Following if statement from Stevens, vol. 2, p. 960.
 1845                  */
 1846                 if (todrop > tlen ||
 1847                     (todrop == tlen && !(thflags & TH_FIN))) {
 1848                         /*
 1849                          * Any valid FIN must be to the left of the window.
 1850                          * At this point the FIN must be a duplicate or out
 1851                          * of sequence; drop it.
 1852                          */
 1853                         thflags &= ~TH_FIN;
 1854 
 1855                         /*
 1856                          * Send an ACK to resynchronize and drop any data.
 1857                          * But keep on processing for RST or ACK.
 1858                          */
 1859                         tp->t_flags |= TF_ACKNOW;
 1860                         todrop = tlen;
 1861                         tcpstat.tcps_rcvduppack++;
 1862                         tcpstat.tcps_rcvdupbyte += todrop;
 1863                 } else {
 1864                         tcpstat.tcps_rcvpartduppack++;
 1865                         tcpstat.tcps_rcvpartdupbyte += todrop;
 1866                 }
 1867                 drop_hdrlen += todrop;  /* drop from the top afterwards */
 1868                 th->th_seq += todrop;
 1869                 tlen -= todrop;
 1870                 if (th->th_urp > todrop)
 1871                         th->th_urp -= todrop;
 1872                 else {
 1873                         thflags &= ~TH_URG;
 1874                         th->th_urp = 0;
 1875                 }
 1876         }
 1877 
 1878         /*
 1879          * If new data are received on a connection after the
 1880          * user processes are gone, then RST the other end.
 1881          */
 1882         if ((so->so_state & SS_NOFDREF) &&
 1883             tp->t_state > TCPS_CLOSE_WAIT && tlen) {
 1884                 tp = tcp_close(tp);
 1885                 tcpstat.tcps_rcvafterclose++;
 1886                 rstreason = BANDLIM_UNLIMITED;
 1887                 goto dropwithreset;
 1888         }
 1889 
 1890         /*
 1891          * If segment ends after window, drop trailing data
 1892          * (and PUSH and FIN); if nothing left, just ACK.
 1893          */
 1894         todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
 1895         if (todrop > 0) {
 1896                 tcpstat.tcps_rcvpackafterwin++;
 1897                 if (todrop >= tlen) {
 1898                         tcpstat.tcps_rcvbyteafterwin += tlen;
 1899                         /*
 1900                          * If a new connection request is received
 1901                          * while in TIME_WAIT, drop the old connection
 1902                          * and start over if the sequence numbers
 1903                          * are above the previous ones.
 1904                          */
 1905                         if (thflags & TH_SYN &&
 1906                             tp->t_state == TCPS_TIME_WAIT &&
 1907                             SEQ_GT(th->th_seq, tp->rcv_nxt)) {
 1908                                 tp = tcp_close(tp);
 1909                                 goto findpcb;
 1910                         }
 1911                         /*
 1912                          * If window is closed can only take segments at
 1913                          * window edge, and have to drop data and PUSH from
 1914                          * incoming segments.  Continue processing, but
 1915                          * remember to ack.  Otherwise, drop segment
 1916                          * and ack.
 1917                          */
 1918                         if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
 1919                                 tp->t_flags |= TF_ACKNOW;
 1920                                 tcpstat.tcps_rcvwinprobe++;
 1921                         } else
 1922                                 goto dropafterack;
 1923                 } else
 1924                         tcpstat.tcps_rcvbyteafterwin += todrop;
 1925                 m_adj(m, -todrop);
 1926                 tlen -= todrop;
 1927                 thflags &= ~(TH_PUSH | TH_FIN);
 1928         }
 1929 
 1930         /*
 1931          * If last ACK falls within this segment's sequence numbers,
 1932          * record its timestamp.
 1933          * NOTE:
 1934          * 1) That the test incorporates suggestions from the latest
 1935          *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
 1936          * 2) That updating only on newer timestamps interferes with
 1937          *    our earlier PAWS tests, so this check should be solely
 1938          *    predicated on the sequence space of this segment.
 1939          * 3) That we modify the segment boundary check to be
 1940          *        Last.ACK.Sent <= SEG.SEQ + SEG.LEN
 1941          *    instead of RFC1323's
 1942          *        Last.ACK.Sent < SEG.SEQ + SEG.LEN,
 1943          *    This modified check allows us to overcome RFC1323's
 1944          *    limitations as described in Stevens TCP/IP Illustrated
 1945          *    Vol. 2 p.869. In such cases, we can still calculate the
 1946          *    RTT correctly when RCV.NXT == Last.ACK.Sent.
 1947          */
 1948         if ((to.to_flags & TOF_TS) && SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
 1949             SEQ_LEQ(tp->last_ack_sent, (th->th_seq + tlen
 1950                                         + ((thflags & TH_SYN) != 0)
 1951                                         + ((thflags & TH_FIN) != 0)))) {
 1952                 tp->ts_recent_age = ticks;
 1953                 tp->ts_recent = to.to_tsval;
 1954         }
 1955 
 1956         /*
 1957          * If a SYN is in the window, then this is an
 1958          * error and we send an RST and drop the connection.
 1959          */
 1960         if (thflags & TH_SYN) {
 1961                 tp = tcp_drop(tp, ECONNRESET);
 1962                 rstreason = BANDLIM_UNLIMITED;
 1963                 goto dropwithreset;
 1964         }
 1965 
 1966         /*
 1967          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
 1968          * flag is on (half-synchronized state), then queue data for
 1969          * later processing; else drop segment and return.
 1970          */
 1971         if (!(thflags & TH_ACK)) {
 1972                 if (tp->t_state == TCPS_SYN_RECEIVED ||
 1973                     (tp->t_flags & TF_NEEDSYN))
 1974                         goto step6;
 1975                 else
 1976                         goto drop;
 1977         }
 1978 
 1979         /*
 1980          * Ack processing.
 1981          */
 1982         switch (tp->t_state) {
 1983         /*
 1984          * In SYN_RECEIVED state, the ACK acknowledges our SYN, so enter
 1985          * ESTABLISHED state and continue processing.
 1986          * The ACK was checked above.
 1987          */
 1988         case TCPS_SYN_RECEIVED:
 1989 
 1990                 tcpstat.tcps_connects++;
 1991                 soisconnected(so);
 1992                 /* Do window scaling? */
 1993                 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
 1994                     (TF_RCVD_SCALE | TF_REQ_SCALE))
 1995                         tp->rcv_scale = tp->request_r_scale;
 1996                 /*
 1997                  * Make transitions:
 1998                  *      SYN-RECEIVED  -> ESTABLISHED
 1999                  *      SYN-RECEIVED* -> FIN-WAIT-1
 2000                  */
 2001                 tp->t_starttime = ticks;
 2002                 if (tp->t_flags & TF_NEEDFIN) {
 2003                         tp->t_state = TCPS_FIN_WAIT_1;
 2004                         tp->t_flags &= ~TF_NEEDFIN;
 2005                 } else {
 2006                         tcp_established(tp);
 2007                 }
 2008                 /*
 2009                  * If segment contains data or ACK, will call tcp_reass()
 2010                  * later; if not, do so now to pass queued data to user.
 2011                  */
 2012                 if (tlen == 0 && !(thflags & TH_FIN))
 2013                         tcp_reass(tp, NULL, NULL, NULL);
 2014                 /* fall into ... */
 2015 
 2016         /*
 2017          * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
 2018          * ACKs.  If the ack is in the range
 2019          *      tp->snd_una < th->th_ack <= tp->snd_max
 2020          * then advance tp->snd_una to th->th_ack and drop
 2021          * data from the retransmission queue.  If this ACK reflects
 2022          * more up to date window information we update our window information.
 2023          */
 2024         case TCPS_ESTABLISHED:
 2025         case TCPS_FIN_WAIT_1:
 2026         case TCPS_FIN_WAIT_2:
 2027         case TCPS_CLOSE_WAIT:
 2028         case TCPS_CLOSING:
 2029         case TCPS_LAST_ACK:
 2030         case TCPS_TIME_WAIT:
 2031 
 2032                 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
 2033                         boolean_t maynotdup = FALSE;
 2034 
 2035                         if (TCP_DO_SACK(tp))
 2036                                 tcp_sack_update_scoreboard(tp, &to);
 2037 
 2038                         if (tlen != 0 || tiwin != tp->snd_wnd ||
 2039                             ((thflags & TH_FIN) && !(tp->t_flags & TF_SAWFIN)))
 2040                                 maynotdup = TRUE;
 2041 
 2042                         if (!tcp_callout_active(tp, tp->tt_rexmt) ||
 2043                             th->th_ack != tp->snd_una) {
 2044                                 if (!maynotdup)
 2045                                         tcpstat.tcps_rcvdupack++;
 2046                                 tp->t_dupacks = 0;
 2047                                 break;
 2048                         }
 2049 
 2050 #define DELAY_DUPACK \
 2051 do { \
 2052         delayed_dupack = TRUE; \
 2053         th_dupack = th->th_ack; \
 2054         to_flags = to.to_flags; \
 2055 } while (0)
 2056                         if (maynotdup) {
 2057                                 if (!tcp_do_rfc6675 ||
 2058                                     !TCP_DO_SACK(tp) ||
 2059                                     (to.to_flags &
 2060                                      (TOF_SACK | TOF_SACK_REDUNDANT))
 2061                                      != TOF_SACK) {
 2062                                         tp->t_dupacks = 0;
 2063                                 } else {
 2064                                         DELAY_DUPACK;
 2065                                 }
 2066                                 break;
 2067                         }
 2068                         if ((thflags & TH_FIN) && !(tp->t_flags & TF_QUEDFIN)) {
 2069                                 /*
 2070                                  * This could happen, if the reassemable
 2071                                  * queue overflew or was drained.  Don't
 2072                                  * drop this FIN here; defer the duplicated
 2073                                  * ACK processing until this FIN gets queued.
 2074                                  */
 2075                                 DELAY_DUPACK;
 2076                                 break;
 2077                         }
 2078 #undef DELAY_DUPACK
 2079 
 2080                         if (tcp_recv_dupack(tp, th->th_ack, to.to_flags))
 2081                                 goto drop;
 2082                         else
 2083                                 break;
 2084                 }
 2085 
 2086                 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
 2087                 tp->t_dupacks = 0;
 2088                 if (SEQ_GT(th->th_ack, tp->snd_max)) {
 2089                         /*
 2090                          * Detected optimistic ACK attack.
 2091                          * Force slow-start to de-synchronize attack.
 2092                          */
 2093                         tp->snd_cwnd = tp->t_maxseg;
 2094                         tp->snd_wacked = 0;
 2095 
 2096                         tcpstat.tcps_rcvacktoomuch++;
 2097                         goto dropafterack;
 2098                 }
 2099                 /*
 2100                  * If we reach this point, ACK is not a duplicate,
 2101                  *     i.e., it ACKs something we sent.
 2102                  */
 2103                 if (tp->t_flags & TF_NEEDSYN) {
 2104                         /*
 2105                          * T/TCP: Connection was half-synchronized, and our
 2106                          * SYN has been ACK'd (so connection is now fully
 2107                          * synchronized).  Go to non-starred state,
 2108                          * increment snd_una for ACK of SYN, and check if
 2109                          * we can do window scaling.
 2110                          */
 2111                         tp->t_flags &= ~TF_NEEDSYN;
 2112                         tp->snd_una++;
 2113                         /* Do window scaling? */
 2114                         if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
 2115                             (TF_RCVD_SCALE | TF_REQ_SCALE))
 2116                                 tp->rcv_scale = tp->request_r_scale;
 2117                 }
 2118 
 2119 process_ACK:
 2120                 acked = th->th_ack - tp->snd_una;
 2121                 tcpstat.tcps_rcvackpack++;
 2122                 tcpstat.tcps_rcvackbyte += acked;
 2123 
 2124                 if (tcp_do_eifel_detect && acked > 0 &&
 2125                     (to.to_flags & TOF_TS) && (to.to_tsecr != 0) &&
 2126                     (tp->rxt_flags & TRXT_F_FIRSTACCACK)) {
 2127                         /* Eifel detection applicable. */
 2128                         if (to.to_tsecr < tp->t_rexmtTS) {
 2129                                 ++tcpstat.tcps_eifeldetected;
 2130                                 tcp_revert_congestion_state(tp);
 2131                                 if (tp->t_rxtshift != 1 ||
 2132                                     ticks >= tp->t_badrxtwin)
 2133                                         ++tcpstat.tcps_rttcantdetect;
 2134                         }
 2135                 } else if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
 2136                         /*
 2137                          * If we just performed our first retransmit,
 2138                          * and the ACK arrives within our recovery window,
 2139                          * then it was a mistake to do the retransmit
 2140                          * in the first place.  Recover our original cwnd
 2141                          * and ssthresh, and proceed to transmit where we
 2142                          * left off.
 2143                          */
 2144                         tcp_revert_congestion_state(tp);
 2145                         ++tcpstat.tcps_rttdetected;
 2146                 }
 2147 
 2148                 /*
 2149                  * If we have a timestamp reply, update smoothed
 2150                  * round trip time.  If no timestamp is present but
 2151                  * transmit timer is running and timed sequence
 2152                  * number was acked, update smoothed round trip time.
 2153                  * Since we now have an rtt measurement, cancel the
 2154                  * timer backoff (cf., Phil Karn's retransmit alg.).
 2155                  * Recompute the initial retransmit timer.
 2156                  *
 2157                  * Some machines (certain windows boxes) send broken
 2158                  * timestamp replies during the SYN+ACK phase, ignore
 2159                  * timestamps of 0.
 2160                  */
 2161                 if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0))
 2162                         tcp_xmit_timer(tp, ticks - to.to_tsecr + 1, th->th_ack);
 2163                 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
 2164                         tcp_xmit_timer(tp, ticks - tp->t_rtttime, th->th_ack);
 2165                 tcp_xmit_bandwidth_limit(tp, th->th_ack);
 2166 
 2167                 /*
 2168                  * If no data (only SYN) was ACK'd,
 2169                  *    skip rest of ACK processing.
 2170                  */
 2171                 if (acked == 0)
 2172                         goto step6;
 2173 
 2174                 /* Stop looking for an acceptable ACK since one was received. */
 2175                 tp->rxt_flags &= ~(TRXT_F_FIRSTACCACK |
 2176                     TRXT_F_FASTREXMT | TRXT_F_EARLYREXMT);
 2177 
 2178                 if (acked > so->so_snd.ssb_cc) {
 2179                         tp->snd_wnd -= so->so_snd.ssb_cc;
 2180                         sbdrop(&so->so_snd.sb, (int)so->so_snd.ssb_cc);
 2181                         ourfinisacked = TRUE;
 2182                 } else {
 2183                         sbdrop(&so->so_snd.sb, acked);
 2184                         tp->snd_wnd -= acked;
 2185                         ourfinisacked = FALSE;
 2186                 }
 2187                 sowwakeup(so);
 2188 
 2189                 /*
 2190                  * Update window information.
 2191                  */
 2192                 if (acceptable_window_update(tp, th, tiwin)) {
 2193                         /* keep track of pure window updates */
 2194                         if (tlen == 0 && tp->snd_wl2 == th->th_ack &&
 2195                             tiwin > tp->snd_wnd)
 2196                                 tcpstat.tcps_rcvwinupd++;
 2197                         tp->snd_wnd = tiwin;
 2198                         tp->snd_wl1 = th->th_seq;
 2199                         tp->snd_wl2 = th->th_ack;
 2200                         if (tp->snd_wnd > tp->max_sndwnd)
 2201                                 tp->max_sndwnd = tp->snd_wnd;
 2202                         needoutput = TRUE;
 2203                 }
 2204 
 2205                 tp->snd_una = th->th_ack;
 2206                 if (TCP_DO_SACK(tp))
 2207                         tcp_sack_update_scoreboard(tp, &to);
 2208                 if (IN_FASTRECOVERY(tp)) {
 2209                         if (SEQ_GEQ(th->th_ack, tp->snd_recover)) {
 2210                                 EXIT_FASTRECOVERY(tp);
 2211                                 needoutput = TRUE;
 2212                                 /*
 2213                                  * If the congestion window was inflated
 2214                                  * to account for the other side's
 2215                                  * cached packets, retract it.
 2216                                  */
 2217                                 if (!TCP_DO_SACK(tp))
 2218                                         tp->snd_cwnd = tp->snd_ssthresh;
 2219 
 2220                                 /*
 2221                                  * Window inflation should have left us
 2222                                  * with approximately snd_ssthresh outstanding
 2223                                  * data.  But, in case we would be inclined
 2224                                  * to send a burst, better do it using
 2225                                  * slow start.
 2226                                  */
 2227                                 if (SEQ_GT(th->th_ack + tp->snd_cwnd,
 2228                                            tp->snd_max + 2 * tp->t_maxseg))
 2229                                         tp->snd_cwnd =
 2230                                             (tp->snd_max - tp->snd_una) +
 2231                                             2 * tp->t_maxseg;
 2232 
 2233                                 tp->snd_wacked = 0;
 2234                         } else {
 2235                                 if (TCP_DO_SACK(tp)) {
 2236                                         tp->snd_max_rexmt = tp->snd_max;
 2237                                         tcp_sack_rexmt(tp,
 2238                                             tp->snd_una == tp->rexmt_high);
 2239                                 } else {
 2240                                         tcp_newreno_partial_ack(tp, th, acked);
 2241                                 }
 2242                                 needoutput = FALSE;
 2243                         }
 2244                 } else {
 2245                         /*
 2246                          * Open the congestion window.  When in slow-start,
 2247                          * open exponentially: maxseg per packet.  Otherwise,
 2248                          * open linearly: maxseg per window.
 2249                          */
 2250                         if (tp->snd_cwnd <= tp->snd_ssthresh) {
 2251                                 u_int abc_sslimit =
 2252                                     (SEQ_LT(tp->snd_nxt, tp->snd_max) ?
 2253                                      tp->t_maxseg : 2 * tp->t_maxseg);
 2254 
 2255                                 /* slow-start */
 2256                                 tp->snd_cwnd += tcp_do_abc ?
 2257                                     min(acked, abc_sslimit) : tp->t_maxseg;
 2258                         } else {
 2259                                 /* linear increase */
 2260                                 tp->snd_wacked += tcp_do_abc ? acked :
 2261                                     tp->t_maxseg;
 2262                                 if (tp->snd_wacked >= tp->snd_cwnd) {
 2263                                         tp->snd_wacked -= tp->snd_cwnd;
 2264                                         tp->snd_cwnd += tp->t_maxseg;
 2265                                 }
 2266                         }
 2267                         tp->snd_cwnd = min(tp->snd_cwnd,
 2268                                            TCP_MAXWIN << tp->snd_scale);
 2269                         tp->snd_recover = th->th_ack - 1;
 2270                 }
 2271                 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
 2272                         tp->snd_nxt = tp->snd_una;
 2273 
 2274                 /*
 2275                  * If all outstanding data is acked, stop retransmit
 2276                  * timer and remember to restart (more output or persist).
 2277                  * If there is more data to be acked, restart retransmit
 2278                  * timer, using current (possibly backed-off) value.
 2279                  */
 2280                 if (th->th_ack == tp->snd_max) {
 2281                         tcp_callout_stop(tp, tp->tt_rexmt);
 2282                         needoutput = TRUE;
 2283                 } else if (!tcp_callout_active(tp, tp->tt_persist)) {
 2284                         tcp_callout_reset(tp, tp->tt_rexmt, tp->t_rxtcur,
 2285                             tcp_timer_rexmt);
 2286                 }
 2287 
 2288                 switch (tp->t_state) {
 2289                 /*
 2290                  * In FIN_WAIT_1 STATE in addition to the processing
 2291                  * for the ESTABLISHED state if our FIN is now acknowledged
 2292                  * then enter FIN_WAIT_2.
 2293                  */
 2294                 case TCPS_FIN_WAIT_1:
 2295                         if (ourfinisacked) {
 2296                                 /*
 2297                                  * If we can't receive any more
 2298                                  * data, then closing user can proceed.
 2299                                  * Starting the timer is contrary to the
 2300                                  * specification, but if we don't get a FIN
 2301                                  * we'll hang forever.
 2302                                  */
 2303                                 if (so->so_state & SS_CANTRCVMORE) {
 2304                                         soisdisconnected(so);
 2305                                         tcp_callout_reset(tp, tp->tt_2msl,
 2306                                             tp->t_maxidle, tcp_timer_2msl);
 2307                                 }
 2308                                 tp->t_state = TCPS_FIN_WAIT_2;
 2309                         }
 2310                         break;
 2311 
 2312                 /*
 2313                  * In CLOSING STATE in addition to the processing for
 2314                  * the ESTABLISHED state if the ACK acknowledges our FIN
 2315                  * then enter the TIME-WAIT state, otherwise ignore
 2316                  * the segment.
 2317                  */
 2318                 case TCPS_CLOSING:
 2319                         if (ourfinisacked) {
 2320                                 tp->t_state = TCPS_TIME_WAIT;
 2321                                 tcp_canceltimers(tp);
 2322                                 tcp_callout_reset(tp, tp->tt_2msl,
 2323                                             2 * tcp_rmx_msl(tp),
 2324                                             tcp_timer_2msl);
 2325                                 soisdisconnected(so);
 2326                         }
 2327                         break;
 2328 
 2329                 /*
 2330                  * In LAST_ACK, we may still be waiting for data to drain
 2331                  * and/or to be acked, as well as for the ack of our FIN.
 2332                  * If our FIN is now acknowledged, delete the TCB,
 2333                  * enter the closed state and return.
 2334                  */
 2335                 case TCPS_LAST_ACK:
 2336                         if (ourfinisacked) {
 2337                                 tp = tcp_close(tp);
 2338                                 goto drop;
 2339                         }
 2340                         break;
 2341 
 2342                 /*
 2343                  * In TIME_WAIT state the only thing that should arrive
 2344                  * is a retransmission of the remote FIN.  Acknowledge
 2345                  * it and restart the finack timer.
 2346                  */
 2347                 case TCPS_TIME_WAIT:
 2348                         tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp),
 2349                             tcp_timer_2msl);
 2350                         goto dropafterack;
 2351                 }
 2352         }
 2353 
 2354 step6:
 2355         /*
 2356          * Update window information.
 2357          * Don't look at window if no ACK: TAC's send garbage on first SYN.
 2358          */
 2359         if ((thflags & TH_ACK) &&
 2360             acceptable_window_update(tp, th, tiwin)) {
 2361                 /* keep track of pure window updates */
 2362                 if (tlen == 0 && tp->snd_wl2 == th->th_ack &&
 2363                     tiwin > tp->snd_wnd)
 2364                         tcpstat.tcps_rcvwinupd++;
 2365                 tp->snd_wnd = tiwin;
 2366                 tp->snd_wl1 = th->th_seq;
 2367                 tp->snd_wl2 = th->th_ack;
 2368                 if (tp->snd_wnd > tp->max_sndwnd)
 2369                         tp->max_sndwnd = tp->snd_wnd;
 2370                 needoutput = TRUE;
 2371         }
 2372 
 2373         /*
 2374          * Process segments with URG.
 2375          */
 2376         if ((thflags & TH_URG) && th->th_urp &&
 2377             !TCPS_HAVERCVDFIN(tp->t_state)) {
 2378                 /*
 2379                  * This is a kludge, but if we receive and accept
 2380                  * random urgent pointers, we'll crash in
 2381                  * soreceive.  It's hard to imagine someone
 2382                  * actually wanting to send this much urgent data.
 2383                  */
 2384                 if (th->th_urp + so->so_rcv.ssb_cc > sb_max) {
 2385                         th->th_urp = 0;                 /* XXX */
 2386                         thflags &= ~TH_URG;             /* XXX */
 2387                         goto dodata;                    /* XXX */
 2388                 }
 2389                 /*
 2390                  * If this segment advances the known urgent pointer,
 2391                  * then mark the data stream.  This should not happen
 2392                  * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
 2393                  * a FIN has been received from the remote side.
 2394                  * In these states we ignore the URG.
 2395                  *
 2396                  * According to RFC961 (Assigned Protocols),
 2397                  * the urgent pointer points to the last octet
 2398                  * of urgent data.  We continue, however,
 2399                  * to consider it to indicate the first octet
 2400                  * of data past the urgent section as the original
 2401                  * spec states (in one of two places).
 2402                  */
 2403                 if (SEQ_GT(th->th_seq + th->th_urp, tp->rcv_up)) {
 2404                         tp->rcv_up = th->th_seq + th->th_urp;
 2405                         so->so_oobmark = so->so_rcv.ssb_cc +
 2406                             (tp->rcv_up - tp->rcv_nxt) - 1;
 2407                         if (so->so_oobmark == 0)
 2408                                 sosetstate(so, SS_RCVATMARK);
 2409                         sohasoutofband(so);
 2410                         tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
 2411                 }
 2412                 /*
 2413                  * Remove out of band data so doesn't get presented to user.
 2414                  * This can happen independent of advancing the URG pointer,
 2415                  * but if two URG's are pending at once, some out-of-band
 2416                  * data may creep in... ick.
 2417                  */
 2418                 if (th->th_urp <= (u_long)tlen &&
 2419                     !(so->so_options & SO_OOBINLINE)) {
 2420                         /* hdr drop is delayed */
 2421                         tcp_pulloutofband(so, th, m, drop_hdrlen);
 2422                 }
 2423         } else {
 2424                 /*
 2425                  * If no out of band data is expected,
 2426                  * pull receive urgent pointer along
 2427                  * with the receive window.
 2428                  */
 2429                 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
 2430                         tp->rcv_up = tp->rcv_nxt;
 2431         }
 2432 
 2433 dodata:                                                 /* XXX */
 2434         /*
 2435          * Process the segment text, merging it into the TCP sequencing queue,
 2436          * and arranging for acknowledgment of receipt if necessary.
 2437          * This process logically involves adjusting tp->rcv_wnd as data
 2438          * is presented to the user (this happens in tcp_usrreq.c,
 2439          * case PRU_RCVD).  If a FIN has already been received on this
 2440          * connection then we just ignore the text.
 2441          */
 2442         if ((tlen || (thflags & TH_FIN)) && !TCPS_HAVERCVDFIN(tp->t_state)) {
 2443                 if (thflags & TH_FIN)
 2444                         tp->t_flags |= TF_SAWFIN;
 2445                 m_adj(m, drop_hdrlen);  /* delayed header drop */
 2446                 /*
 2447                  * Insert segment which includes th into TCP reassembly queue
 2448                  * with control block tp.  Set thflags to whether reassembly now
 2449                  * includes a segment with FIN.  This handles the common case
 2450                  * inline (segment is the next to be received on an established
 2451                  * connection, and the queue is empty), avoiding linkage into
 2452                  * and removal from the queue and repetition of various
 2453                  * conversions.
 2454                  * Set DELACK for segments received in order, but ack
 2455                  * immediately when segments are out of order (so
 2456                  * fast retransmit can work).
 2457                  */
 2458                 if (th->th_seq == tp->rcv_nxt &&
 2459                     TAILQ_EMPTY(&tp->t_segq) &&
 2460                     TCPS_HAVEESTABLISHED(tp->t_state)) {
 2461                         if (thflags & TH_FIN)
 2462                                 tp->t_flags |= TF_QUEDFIN;
 2463                         if (DELAY_ACK(tp)) {
 2464                                 tcp_callout_reset(tp, tp->tt_delack,
 2465                                     tcp_delacktime, tcp_timer_delack);
 2466                         } else {
 2467                                 tp->t_flags |= TF_ACKNOW;
 2468                         }
 2469                         tp->rcv_nxt += tlen;
 2470                         thflags = th->th_flags & TH_FIN;
 2471                         tcpstat.tcps_rcvpack++;
 2472                         tcpstat.tcps_rcvbyte += tlen;
 2473                         ND6_HINT(tp);
 2474                         if (so->so_state & SS_CANTRCVMORE) {
 2475                                 m_freem(m);
 2476                         } else {
 2477                                 lwkt_gettoken(&so->so_rcv.ssb_token);
 2478                                 ssb_appendstream(&so->so_rcv, m);
 2479                                 lwkt_reltoken(&so->so_rcv.ssb_token);
 2480                         }
 2481                         sorwakeup(so);
 2482                 } else {
 2483                         if (!(tp->sack_flags & TSACK_F_DUPSEG)) {
 2484                                 /* Initialize SACK report block. */
 2485                                 tp->reportblk.rblk_start = th->th_seq;
 2486                                 tp->reportblk.rblk_end = TCP_SACK_BLKEND(
 2487                                     th->th_seq + tlen, thflags);
 2488                         }
 2489                         thflags = tcp_reass(tp, th, &tlen, m);
 2490                         tp->t_flags |= TF_ACKNOW;
 2491                 }
 2492 
 2493                 /*
 2494                  * Note the amount of data that peer has sent into
 2495                  * our window, in order to estimate the sender's
 2496                  * buffer size.
 2497                  */
 2498                 len = so->so_rcv.ssb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
 2499         } else {
 2500                 m_freem(m);
 2501                 thflags &= ~TH_FIN;
 2502         }
 2503 
 2504         /*
 2505          * If FIN is received ACK the FIN and let the user know
 2506          * that the connection is closing.
 2507          */
 2508         if (thflags & TH_FIN) {
 2509                 if (!TCPS_HAVERCVDFIN(tp->t_state)) {
 2510                         socantrcvmore(so);
 2511                         /*
 2512                          * If connection is half-synchronized
 2513                          * (ie NEEDSYN flag on) then delay ACK,
 2514                          * so it may be piggybacked when SYN is sent.
 2515                          * Otherwise, since we received a FIN then no
 2516                          * more input can be expected, send ACK now.
 2517                          */
 2518                         if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN)) {
 2519                                 tcp_callout_reset(tp, tp->tt_delack,
 2520                                     tcp_delacktime, tcp_timer_delack);
 2521                         } else {
 2522                                 tp->t_flags |= TF_ACKNOW;
 2523                         }
 2524                         tp->rcv_nxt++;
 2525                 }
 2526 
 2527                 switch (tp->t_state) {
 2528                 /*
 2529                  * In SYN_RECEIVED and ESTABLISHED STATES
 2530                  * enter the CLOSE_WAIT state.
 2531                  */
 2532                 case TCPS_SYN_RECEIVED:
 2533                         tp->t_starttime = ticks;
 2534                         /*FALLTHROUGH*/
 2535                 case TCPS_ESTABLISHED:
 2536                         tp->t_state = TCPS_CLOSE_WAIT;
 2537                         break;
 2538 
 2539                 /*
 2540                  * If still in FIN_WAIT_1 STATE FIN has not been acked so
 2541                  * enter the CLOSING state.
 2542                  */
 2543                 case TCPS_FIN_WAIT_1:
 2544                         tp->t_state = TCPS_CLOSING;
 2545                         break;
 2546 
 2547                 /*
 2548                  * In FIN_WAIT_2 state enter the TIME_WAIT state,
 2549                  * starting the time-wait timer, turning off the other
 2550                  * standard timers.
 2551                  */
 2552                 case TCPS_FIN_WAIT_2:
 2553                         tp->t_state = TCPS_TIME_WAIT;
 2554                         tcp_canceltimers(tp);
 2555                         tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp),
 2556                                     tcp_timer_2msl);
 2557                         soisdisconnected(so);
 2558                         break;
 2559 
 2560                 /*
 2561                  * In TIME_WAIT state restart the 2 MSL time_wait timer.
 2562                  */
 2563                 case TCPS_TIME_WAIT:
 2564                         tcp_callout_reset(tp, tp->tt_2msl, 2 * tcp_rmx_msl(tp),
 2565                             tcp_timer_2msl);
 2566                         break;
 2567                 }
 2568         }
 2569 
 2570 #ifdef TCPDEBUG
 2571         if (so->so_options & SO_DEBUG)
 2572                 tcp_trace(TA_INPUT, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
 2573 #endif
 2574 
 2575         /*
 2576          * Delayed duplicated ACK processing
 2577          */
 2578         if (delayed_dupack && tcp_recv_dupack(tp, th_dupack, to_flags))
 2579                 needoutput = FALSE;
 2580 
 2581         /*
 2582          * Return any desired output.
 2583          */
 2584         if ((tp->t_flags & TF_ACKNOW) ||
 2585             (needoutput && tcp_sack_report_needed(tp))) {
 2586                 tcp_output_cancel(tp);
 2587                 tcp_output_fair(tp);
 2588         } else if (needoutput && !tcp_output_pending(tp)) {
 2589                 tcp_output_fair(tp);
 2590         }
 2591         tcp_sack_report_cleanup(tp);
 2592         return(IPPROTO_DONE);
 2593 
 2594 dropafterack:
 2595         /*
 2596          * Generate an ACK dropping incoming segment if it occupies
 2597          * sequence space, where the ACK reflects our state.
 2598          *
 2599          * We can now skip the test for the RST flag since all
 2600          * paths to this code happen after packets containing
 2601          * RST have been dropped.
 2602          *
 2603          * In the SYN-RECEIVED state, don't send an ACK unless the
 2604          * segment we received passes the SYN-RECEIVED ACK test.
 2605          * If it fails send a RST.  This breaks the loop in the
 2606          * "LAND" DoS attack, and also prevents an ACK storm
 2607          * between two listening ports that have been sent forged
 2608          * SYN segments, each with the source address of the other.
 2609          */
 2610         if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
 2611             (SEQ_GT(tp->snd_una, th->th_ack) ||
 2612              SEQ_GT(th->th_ack, tp->snd_max)) ) {
 2613                 rstreason = BANDLIM_RST_OPENPORT;
 2614                 goto dropwithreset;
 2615         }
 2616 #ifdef TCPDEBUG
 2617         if (so->so_options & SO_DEBUG)
 2618                 tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
 2619 #endif
 2620         m_freem(m);
 2621         tp->t_flags |= TF_ACKNOW;
 2622         tcp_output(tp);
 2623         tcp_sack_report_cleanup(tp);
 2624         return(IPPROTO_DONE);
 2625 
 2626 dropwithreset:
 2627         /*
 2628          * Generate a RST, dropping incoming segment.
 2629          * Make ACK acceptable to originator of segment.
 2630          * Don't bother to respond if destination was broadcast/multicast.
 2631          */
 2632         if ((thflags & TH_RST) || m->m_flags & (M_BCAST | M_MCAST))
 2633                 goto drop;
 2634         if (isipv6) {
 2635                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
 2636                     IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
 2637                         goto drop;
 2638         } else {
 2639                 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
 2640                     IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
 2641                     ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
 2642                     in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
 2643                         goto drop;
 2644         }
 2645         /* IPv6 anycast check is done at tcp6_input() */
 2646 
 2647         /*
 2648          * Perform bandwidth limiting.
 2649          */
 2650 #ifdef ICMP_BANDLIM
 2651         if (badport_bandlim(rstreason) < 0)
 2652                 goto drop;
 2653 #endif
 2654 
 2655 #ifdef TCPDEBUG
 2656         if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
 2657                 tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
 2658 #endif
 2659         if (thflags & TH_ACK)
 2660                 /* mtod() below is safe as long as hdr dropping is delayed */
 2661                 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
 2662                             TH_RST);
 2663         else {
 2664                 if (thflags & TH_SYN)
 2665                         tlen++;
 2666                 /* mtod() below is safe as long as hdr dropping is delayed */
 2667                 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq + tlen,
 2668                             (tcp_seq)0, TH_RST | TH_ACK);
 2669         }
 2670         if (tp != NULL)
 2671                 tcp_sack_report_cleanup(tp);
 2672         return(IPPROTO_DONE);
 2673 
 2674 drop:
 2675         /*
 2676          * Drop space held by incoming segment and return.
 2677          */
 2678 #ifdef TCPDEBUG
 2679         if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
 2680                 tcp_trace(TA_DROP, ostate, tp, tcp_saveipgen, &tcp_savetcp, 0);
 2681 #endif
 2682         m_freem(m);
 2683         if (tp != NULL)
 2684                 tcp_sack_report_cleanup(tp);
 2685         return(IPPROTO_DONE);
 2686 }
 2687 
 2688 /*
 2689  * Parse TCP options and place in tcpopt.
 2690  */
 2691 static void
 2692 tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, boolean_t is_syn,
 2693     tcp_seq ack)
 2694 {
 2695         int opt, optlen, i;
 2696 
 2697         to->to_flags = 0;
 2698         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 2699                 opt = cp[0];
 2700                 if (opt == TCPOPT_EOL)
 2701                         break;
 2702                 if (opt == TCPOPT_NOP)
 2703                         optlen = 1;
 2704                 else {
 2705                         if (cnt < 2)
 2706                                 break;
 2707                         optlen = cp[1];
 2708                         if (optlen < 2 || optlen > cnt)
 2709                                 break;
 2710                 }
 2711                 switch (opt) {
 2712                 case TCPOPT_MAXSEG:
 2713                         if (optlen != TCPOLEN_MAXSEG)
 2714                                 continue;
 2715                         if (!is_syn)
 2716                                 continue;
 2717                         to->to_flags |= TOF_MSS;
 2718                         bcopy(cp + 2, &to->to_mss, sizeof to->to_mss);
 2719                         to->to_mss = ntohs(to->to_mss);
 2720                         break;
 2721                 case TCPOPT_WINDOW:
 2722                         if (optlen != TCPOLEN_WINDOW)
 2723                                 continue;
 2724                         if (!is_syn)
 2725                                 continue;
 2726                         to->to_flags |= TOF_SCALE;
 2727                         to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
 2728                         break;
 2729                 case TCPOPT_TIMESTAMP:
 2730                         if (optlen != TCPOLEN_TIMESTAMP)
 2731                                 continue;
 2732                         to->to_flags |= TOF_TS;
 2733                         bcopy(cp + 2, &to->to_tsval, sizeof to->to_tsval);
 2734                         to->to_tsval = ntohl(to->to_tsval);
 2735                         bcopy(cp + 6, &to->to_tsecr, sizeof to->to_tsecr);
 2736                         to->to_tsecr = ntohl(to->to_tsecr);
 2737                         /*
 2738                          * If echoed timestamp is later than the current time,
 2739                          * fall back to non RFC1323 RTT calculation.
 2740                          */
 2741                         if (to->to_tsecr != 0 && TSTMP_GT(to->to_tsecr, ticks))
 2742                                 to->to_tsecr = 0;
 2743                         break;
 2744                 case TCPOPT_SACK_PERMITTED:
 2745                         if (optlen != TCPOLEN_SACK_PERMITTED)
 2746                                 continue;
 2747                         if (!is_syn)
 2748                                 continue;
 2749                         to->to_flags |= TOF_SACK_PERMITTED;
 2750                         break;
 2751                 case TCPOPT_SACK:
 2752                         if ((optlen - 2) & 0x07)        /* not multiple of 8 */
 2753                                 continue;
 2754                         to->to_nsackblocks = (optlen - 2) / 8;
 2755                         to->to_sackblocks = (struct raw_sackblock *) (cp + 2);
 2756                         to->to_flags |= TOF_SACK;
 2757                         for (i = 0; i < to->to_nsackblocks; i++) {
 2758                                 struct raw_sackblock *r = &to->to_sackblocks[i];
 2759 
 2760                                 r->rblk_start = ntohl(r->rblk_start);
 2761                                 r->rblk_end = ntohl(r->rblk_end);
 2762 
 2763                                 if (SEQ_LEQ(r->rblk_end, r->rblk_start)) {
 2764                                         /*
 2765                                          * Invalid SACK block; discard all
 2766                                          * SACK blocks
 2767                                          */
 2768                                         tcpstat.tcps_rcvbadsackopt++;
 2769                                         to->to_nsackblocks = 0;
 2770                                         to->to_sackblocks = NULL;
 2771                                         to->to_flags &= ~TOF_SACK;
 2772                                         break;
 2773                                 }
 2774                         }
 2775                         if ((to->to_flags & TOF_SACK) &&
 2776                             tcp_sack_ndsack_blocks(to->to_sackblocks,
 2777                             to->to_nsackblocks, ack))
 2778                                 to->to_flags |= TOF_DSACK;
 2779                         break;
 2780 #ifdef TCP_SIGNATURE
 2781                 /*
 2782                  * XXX In order to reply to a host which has set the
 2783                  * TCP_SIGNATURE option in its initial SYN, we have to
 2784                  * record the fact that the option was observed here
 2785                  * for the syncache code to perform the correct response.
 2786                  */
 2787                 case TCPOPT_SIGNATURE:
 2788                         if (optlen != TCPOLEN_SIGNATURE)
 2789                                 continue;
 2790                         to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
 2791                         break;
 2792 #endif /* TCP_SIGNATURE */
 2793                 default:
 2794                         continue;
 2795                 }
 2796         }
 2797 }
 2798 
 2799 /*
 2800  * Pull out of band byte out of a segment so
 2801  * it doesn't appear in the user's data queue.
 2802  * It is still reflected in the segment length for
 2803  * sequencing purposes.
 2804  * "off" is the delayed to be dropped hdrlen.
 2805  */
 2806 static void
 2807 tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, int off)
 2808 {
 2809         int cnt = off + th->th_urp - 1;
 2810 
 2811         while (cnt >= 0) {
 2812                 if (m->m_len > cnt) {
 2813                         char *cp = mtod(m, caddr_t) + cnt;
 2814                         struct tcpcb *tp = sototcpcb(so);
 2815 
 2816                         tp->t_iobc = *cp;
 2817                         tp->t_oobflags |= TCPOOB_HAVEDATA;
 2818                         bcopy(cp + 1, cp, m->m_len - cnt - 1);
 2819                         m->m_len--;
 2820                         if (m->m_flags & M_PKTHDR)
 2821                                 m->m_pkthdr.len--;
 2822                         return;
 2823                 }
 2824                 cnt -= m->m_len;
 2825                 m = m->m_next;
 2826                 if (m == NULL)
 2827                         break;
 2828         }
 2829         panic("tcp_pulloutofband");
 2830 }
 2831 
 2832 /*
 2833  * Collect new round-trip time estimate
 2834  * and update averages and current timeout.
 2835  */
 2836 static void
 2837 tcp_xmit_timer(struct tcpcb *tp, int rtt, tcp_seq ack)
 2838 {
 2839         int rebaserto = 0;
 2840 
 2841         tcpstat.tcps_rttupdated++;
 2842         tp->t_rttupdated++;
 2843         if ((tp->rxt_flags & TRXT_F_REBASERTO) &&
 2844             SEQ_GT(ack, tp->snd_max_prev)) {
 2845 #ifdef DEBUG_EIFEL_RESPONSE
 2846                 kprintf("srtt/rttvar, prev %d/%d, cur %d/%d, ",
 2847                     tp->t_srtt_prev, tp->t_rttvar_prev,
 2848                     tp->t_srtt, tp->t_rttvar);
 2849 #endif
 2850 
 2851                 tcpstat.tcps_eifelresponse++;
 2852                 rebaserto = 1;
 2853                 tp->rxt_flags &= ~TRXT_F_REBASERTO;
 2854                 tp->t_srtt = max(tp->t_srtt_prev, (rtt << TCP_RTT_SHIFT));
 2855                 tp->t_rttvar = max(tp->t_rttvar_prev,
 2856                     (rtt << (TCP_RTTVAR_SHIFT - 1)));
 2857                 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
 2858                         tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
 2859 
 2860 #ifdef DEBUG_EIFEL_RESPONSE
 2861                 kprintf("new %d/%d ", tp->t_srtt, tp->t_rttvar);
 2862 #endif
 2863         } else if (tp->t_srtt != 0) {
 2864                 int delta;
 2865 
 2866                 /*
 2867                  * srtt is stored as fixed point with 5 bits after the
 2868                  * binary point (i.e., scaled by 8).  The following magic
 2869                  * is equivalent to the smoothing algorithm in rfc793 with
 2870                  * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
 2871                  * point).  Adjust rtt to origin 0.
 2872                  */
 2873                 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
 2874                         - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
 2875 
 2876                 if ((tp->t_srtt += delta) <= 0)
 2877                         tp->t_srtt = 1;
 2878 
 2879                 /*
 2880                  * We accumulate a smoothed rtt variance (actually, a
 2881                  * smoothed mean difference), then set the retransmit
 2882                  * timer to smoothed rtt + 4 times the smoothed variance.
 2883                  * rttvar is stored as fixed point with 4 bits after the
 2884                  * binary point (scaled by 16).  The following is
 2885                  * equivalent to rfc793 smoothing with an alpha of .75
 2886                  * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
 2887                  * rfc793's wired-in beta.
 2888                  */
 2889                 if (delta < 0)
 2890                         delta = -delta;
 2891                 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
 2892                 if ((tp->t_rttvar += delta) <= 0)
 2893                         tp->t_rttvar = 1;
 2894                 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
 2895                         tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
 2896         } else {
 2897                 /*
 2898                  * No rtt measurement yet - use the unsmoothed rtt.
 2899                  * Set the variance to half the rtt (so our first
 2900                  * retransmit happens at 3*rtt).
 2901                  */
 2902                 tp->t_srtt = rtt << TCP_RTT_SHIFT;
 2903                 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
 2904                 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
 2905         }
 2906         tp->t_rtttime = 0;
 2907         tp->t_rxtshift = 0;
 2908 
 2909 #ifdef DEBUG_EIFEL_RESPONSE
 2910         if (rebaserto) {
 2911                 kprintf("| rxtcur prev %d, old %d, ",
 2912                     tp->t_rxtcur_prev, tp->t_rxtcur);
 2913         }
 2914 #endif
 2915 
 2916         /*
 2917          * the retransmit should happen at rtt + 4 * rttvar.
 2918          * Because of the way we do the smoothing, srtt and rttvar
 2919          * will each average +1/2 tick of bias.  When we compute
 2920          * the retransmit timer, we want 1/2 tick of rounding and
 2921          * 1 extra tick because of +-1/2 tick uncertainty in the
 2922          * firing of the timer.  The bias will give us exactly the
 2923          * 1.5 tick we need.  But, because the bias is
 2924          * statistical, we have to test that we don't drop below
 2925          * the minimum feasible timer (which is 2 ticks).
 2926          */
 2927         TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
 2928                       max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
 2929 
 2930         if (rebaserto) {
 2931                 if (tp->t_rxtcur < tp->t_rxtcur_prev + tcp_eifel_rtoinc) {
 2932                         /*
 2933                          * RFC4015 requires that the new RTO is at least
 2934                          * 2*G (tcp_eifel_rtoinc) greater then the RTO
 2935                          * (t_rxtcur_prev) when the spurious retransmit
 2936                          * timeout happens.
 2937                          *
 2938                          * The above condition could be true, if the SRTT
 2939                          * and RTTVAR used to calculate t_rxtcur_prev
 2940                          * resulted in a value less than t_rttmin.  So
 2941                          * simply increasing SRTT by tcp_eifel_rtoinc when
 2942                          * preparing for the Eifel response could not ensure
 2943                          * that the new RTO will be tcp_eifel_rtoinc greater
 2944                          * t_rxtcur_prev.
 2945                          */
 2946                         tp->t_rxtcur = tp->t_rxtcur_prev + tcp_eifel_rtoinc;
 2947                 }
 2948 #ifdef DEBUG_EIFEL_RESPONSE
 2949                 kprintf("new %d\n", tp->t_rxtcur);
 2950 #endif
 2951         }
 2952 
 2953         /*
 2954          * We received an ack for a packet that wasn't retransmitted;
 2955          * it is probably safe to discard any error indications we've
 2956          * received recently.  This isn't quite right, but close enough
 2957          * for now (a route might have failed after we sent a segment,
 2958          * and the return path might not be symmetrical).
 2959          */
 2960         tp->t_softerror = 0;
 2961 }
 2962 
 2963 /*
 2964  * Determine a reasonable value for maxseg size.
 2965  * If the route is known, check route for mtu.
 2966  * If none, use an mss that can be handled on the outgoing
 2967  * interface without forcing IP to fragment; if bigger than
 2968  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
 2969  * to utilize large mbufs.  If no route is found, route has no mtu,
 2970  * or the destination isn't local, use a default, hopefully conservative
 2971  * size (usually 512 or the default IP max size, but no more than the mtu
 2972  * of the interface), as we can't discover anything about intervening
 2973  * gateways or networks.  We also initialize the congestion/slow start
 2974  * window to be a single segment if the destination isn't local.
 2975  * While looking at the routing entry, we also initialize other path-dependent
 2976  * parameters from pre-set or cached values in the routing entry.
 2977  *
 2978  * Also take into account the space needed for options that we
 2979  * send regularly.  Make maxseg shorter by that amount to assure
 2980  * that we can send maxseg amount of data even when the options
 2981  * are present.  Store the upper limit of the length of options plus
 2982  * data in maxopd.
 2983  *
 2984  * NOTE that this routine is only called when we process an incoming
 2985  * segment, for outgoing segments only tcp_mssopt is called.
 2986  */
 2987 void
 2988 tcp_mss(struct tcpcb *tp, int offer)
 2989 {
 2990         struct rtentry *rt;
 2991         struct ifnet *ifp;
 2992         int rtt, mss;
 2993         u_long bufsize;
 2994         struct inpcb *inp = tp->t_inpcb;
 2995         struct socket *so;
 2996 #ifdef INET6
 2997         boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) ? TRUE : FALSE);
 2998         size_t min_protoh = isipv6 ?
 2999                             sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
 3000                             sizeof(struct tcpiphdr);
 3001 #else
 3002         const boolean_t isipv6 = FALSE;
 3003         const size_t min_protoh = sizeof(struct tcpiphdr);
 3004 #endif
 3005 
 3006         if (isipv6)
 3007                 rt = tcp_rtlookup6(&inp->inp_inc);
 3008         else
 3009                 rt = tcp_rtlookup(&inp->inp_inc);
 3010         if (rt == NULL) {
 3011                 tp->t_maxopd = tp->t_maxseg =
 3012                     (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
 3013                 return;
 3014         }
 3015         ifp = rt->rt_ifp;
 3016         so = inp->inp_socket;
 3017 
 3018         /*
 3019          * Offer == 0 means that there was no MSS on the SYN segment,
 3020          * in this case we use either the interface mtu or tcp_mssdflt.
 3021          *
 3022          * An offer which is too large will be cut down later.
 3023          */
 3024         if (offer == 0) {
 3025                 if (isipv6) {
 3026                         if (in6_localaddr(&inp->in6p_faddr)) {
 3027                                 offer = ND_IFINFO(rt->rt_ifp)->linkmtu -
 3028                                         min_protoh;
 3029                         } else {
 3030                                 offer = tcp_v6mssdflt;
 3031                         }
 3032                 } else {
 3033                         if (in_localaddr(inp->inp_faddr))
 3034                                 offer = ifp->if_mtu - min_protoh;
 3035                         else
 3036                                 offer = tcp_mssdflt;
 3037                 }
 3038         }
 3039 
 3040         /*
 3041          * Prevent DoS attack with too small MSS. Round up
 3042          * to at least minmss.
 3043          *
 3044          * Sanity check: make sure that maxopd will be large
 3045          * enough to allow some data on segments even is the
 3046          * all the option space is used (40bytes).  Otherwise
 3047          * funny things may happen in tcp_output.
 3048          */
 3049         offer = max(offer, tcp_minmss);
 3050         offer = max(offer, 64);
 3051 
 3052         rt->rt_rmx.rmx_mssopt = offer;
 3053 
 3054         /*
 3055          * While we're here, check if there's an initial rtt
 3056          * or rttvar.  Convert from the route-table units
 3057          * to scaled multiples of the slow timeout timer.
 3058          */
 3059         if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
 3060                 /*
 3061                  * XXX the lock bit for RTT indicates that the value
 3062                  * is also a minimum value; this is subject to time.
 3063                  */
 3064                 if (rt->rt_rmx.rmx_locks & RTV_RTT)
 3065                         tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
 3066                 tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
 3067                 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
 3068                 tcpstat.tcps_usedrtt++;
 3069                 if (rt->rt_rmx.rmx_rttvar) {
 3070                         tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
 3071                             (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
 3072                         tcpstat.tcps_usedrttvar++;
 3073                 } else {
 3074                         /* default variation is +- 1 rtt */
 3075                         tp->t_rttvar =
 3076                             tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
 3077                 }
 3078                 TCPT_RANGESET(tp->t_rxtcur,
 3079                               ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
 3080                               tp->t_rttmin, TCPTV_REXMTMAX);
 3081         }
 3082 
 3083         /*
 3084          * if there's an mtu associated with the route, use it
 3085          * else, use the link mtu.  Take the smaller of mss or offer
 3086          * as our final mss.
 3087          */
 3088         if (rt->rt_rmx.rmx_mtu) {
 3089                 mss = rt->rt_rmx.rmx_mtu - min_protoh;
 3090         } else {
 3091                 if (isipv6)
 3092                         mss = ND_IFINFO(rt->rt_ifp)->linkmtu - min_protoh;
 3093                 else
 3094                         mss = ifp->if_mtu - min_protoh;
 3095         }
 3096         mss = min(mss, offer);
 3097 
 3098         /*
 3099          * maxopd stores the maximum length of data AND options
 3100          * in a segment; maxseg is the amount of data in a normal
 3101          * segment.  We need to store this value (maxopd) apart
 3102          * from maxseg, because now every segment carries options
 3103          * and thus we normally have somewhat less data in segments.
 3104          */
 3105         tp->t_maxopd = mss;
 3106 
 3107         if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP &&
 3108             ((tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
 3109                 mss -= TCPOLEN_TSTAMP_APPA;
 3110 
 3111 #if     (MCLBYTES & (MCLBYTES - 1)) == 0
 3112         if (mss > MCLBYTES)
 3113                 mss &= ~(MCLBYTES-1);
 3114 #else
 3115         if (mss > MCLBYTES)
 3116                 mss = mss / MCLBYTES * MCLBYTES;
 3117 #endif
 3118         /*
 3119          * If there's a pipesize, change the socket buffer
 3120          * to that size.  Make the socket buffers an integral
 3121          * number of mss units; if the mss is larger than
 3122          * the socket buffer, decrease the mss.
 3123          */
 3124 #ifdef RTV_SPIPE
 3125         if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
 3126 #endif
 3127                 bufsize = so->so_snd.ssb_hiwat;
 3128         if (bufsize < mss)
 3129                 mss = bufsize;
 3130         else {
 3131                 bufsize = roundup(bufsize, mss);
 3132                 if (bufsize > sb_max)
 3133                         bufsize = sb_max;
 3134                 if (bufsize > so->so_snd.ssb_hiwat)
 3135                         ssb_reserve(&so->so_snd, bufsize, so, NULL);
 3136         }
 3137         tp->t_maxseg = mss;
 3138 
 3139 #ifdef RTV_RPIPE
 3140         if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
 3141 #endif
 3142                 bufsize = so->so_rcv.ssb_hiwat;
 3143         if (bufsize > mss) {
 3144                 bufsize = roundup(bufsize, mss);
 3145                 if (bufsize > sb_max)
 3146                         bufsize = sb_max;
 3147                 if (bufsize > so->so_rcv.ssb_hiwat) {
 3148                         lwkt_gettoken(&so->so_rcv.ssb_token);
 3149                         ssb_reserve(&so->so_rcv, bufsize, so, NULL);
 3150                         lwkt_reltoken(&so->so_rcv.ssb_token);
 3151                 }
 3152         }
 3153 
 3154         /*
 3155          * Set the slow-start flight size
 3156          *
 3157          * NOTE: t_maxseg must have been configured!
 3158          */
 3159         tp->snd_cwnd = tcp_initial_window(tp);
 3160 
 3161         if (rt->rt_rmx.rmx_ssthresh) {
 3162                 /*
 3163                  * There's some sort of gateway or interface
 3164                  * buffer limit on the path.  Use this to set
 3165                  * the slow start threshhold, but set the
 3166                  * threshold to no less than 2*mss.
 3167                  */
 3168                 tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
 3169                 tcpstat.tcps_usedssthresh++;
 3170         }
 3171 }
 3172 
 3173 /*
 3174  * Determine the MSS option to send on an outgoing SYN.
 3175  */
 3176 int
 3177 tcp_mssopt(struct tcpcb *tp)
 3178 {
 3179         struct rtentry *rt;
 3180 #ifdef INET6
 3181         boolean_t isipv6 =
 3182             ((tp->t_inpcb->inp_vflag & INP_IPV6) ? TRUE : FALSE);
 3183         int min_protoh = isipv6 ?
 3184                              sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
 3185                              sizeof(struct tcpiphdr);
 3186 #else
 3187         const boolean_t isipv6 = FALSE;
 3188         const size_t min_protoh = sizeof(struct tcpiphdr);
 3189 #endif
 3190 
 3191         if (isipv6)
 3192                 rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc);
 3193         else
 3194                 rt = tcp_rtlookup(&tp->t_inpcb->inp_inc);
 3195         if (rt == NULL)
 3196                 return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
 3197 
 3198         return (rt->rt_ifp->if_mtu - min_protoh);
 3199 }
 3200 
 3201 /*
 3202  * When a partial ack arrives, force the retransmission of the
 3203  * next unacknowledged segment.  Do not exit Fast Recovery.
 3204  *
 3205  * Implement the Slow-but-Steady variant of NewReno by restarting the
 3206  * the retransmission timer.  Turn it off here so it can be restarted
 3207  * later in tcp_output().
 3208  */
 3209 static void
 3210 tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th, int acked)
 3211 {
 3212         tcp_seq old_snd_nxt = tp->snd_nxt;
 3213         u_long ocwnd = tp->snd_cwnd;
 3214 
 3215         tcp_callout_stop(tp, tp->tt_rexmt);
 3216         tp->t_rtttime = 0;
 3217         tp->snd_nxt = th->th_ack;
 3218         /* Set snd_cwnd to one segment beyond acknowledged offset. */
 3219         tp->snd_cwnd = tp->t_maxseg;
 3220         tp->t_flags |= TF_ACKNOW;
 3221         tcp_output(tp);
 3222         if (SEQ_GT(old_snd_nxt, tp->snd_nxt))
 3223                 tp->snd_nxt = old_snd_nxt;
 3224         /* partial window deflation */
 3225         if (ocwnd > acked)
 3226                 tp->snd_cwnd = ocwnd - acked + tp->t_maxseg;
 3227         else
 3228                 tp->snd_cwnd = tp->t_maxseg;
 3229 }
 3230 
 3231 /*
 3232  * In contrast to the Slow-but-Steady NewReno variant,
 3233  * we do not reset the retransmission timer for SACK retransmissions,
 3234  * except when retransmitting snd_una.
 3235  */
 3236 static void
 3237 tcp_sack_rexmt(struct tcpcb *tp, boolean_t force)
 3238 {
 3239         tcp_seq old_snd_nxt = tp->snd_nxt;
 3240         u_long ocwnd = tp->snd_cwnd;
 3241         uint32_t pipe;
 3242         int nseg = 0;           /* consecutive new segments */
 3243         int nseg_rexmt = 0;     /* retransmitted segments */
 3244         int maxrexmt = 0;
 3245 
 3246         if (force) {
 3247                 uint32_t unsacked = tcp_sack_first_unsacked_len(tp);
 3248 
 3249                 /*
 3250                  * Try to fill the first hole in the receiver's
 3251                  * reassemble queue.
 3252                  */
 3253                 maxrexmt = howmany(unsacked, tp->t_maxseg);
 3254                 if (maxrexmt > tcp_force_sackrxt)
 3255                         maxrexmt = tcp_force_sackrxt;
 3256         }
 3257 
 3258         tp->t_rtttime = 0;
 3259         pipe = tcp_sack_compute_pipe(tp);
 3260         while (((tcp_seq_diff_t)(ocwnd - pipe) >= (tcp_seq_diff_t)tp->t_maxseg
 3261                 || (force && nseg_rexmt < maxrexmt && nseg == 0)) &&
 3262             (!tcp_do_smartsack || nseg < TCP_SACK_MAXBURST)) {
 3263                 tcp_seq old_snd_max, old_rexmt_high, nextrexmt;
 3264                 uint32_t sent, seglen;
 3265                 boolean_t rescue;
 3266                 int error;
 3267 
 3268                 old_rexmt_high = tp->rexmt_high;
 3269                 if (!tcp_sack_nextseg(tp, &nextrexmt, &seglen, &rescue)) {
 3270                         tp->rexmt_high = old_rexmt_high;
 3271                         break;
 3272                 }
 3273 
 3274                 /*
 3275                  * If the next tranmission is a rescue retranmission,
 3276                  * we check whether we have already sent some data
 3277                  * (either new segments or retransmitted segments)
 3278                  * into the the network or not.  Since the idea of rescue
 3279                  * retransmission is to sustain ACK clock, as long as
 3280                  * some segments are in the network, ACK clock will be
 3281                  * kept ticking.
 3282                  */
 3283                 if (rescue && (nseg_rexmt > 0 || nseg > 0)) {
 3284                         tp->rexmt_high = old_rexmt_high;
 3285                         break;
 3286                 }
 3287 
 3288                 if (nextrexmt == tp->snd_max)
 3289                         ++nseg;
 3290                 else
 3291                         ++nseg_rexmt;
 3292                 tp->snd_nxt = nextrexmt;
 3293                 tp->snd_cwnd = nextrexmt - tp->snd_una + seglen;
 3294                 old_snd_max = tp->snd_max;
 3295                 if (nextrexmt == tp->snd_una)
 3296                         tcp_callout_stop(tp, tp->tt_rexmt);
 3297                 tp->t_flags |= TF_XMITNOW;
 3298                 error = tcp_output(tp);
 3299                 if (error != 0) {
 3300                         tp->rexmt_high = old_rexmt_high;
 3301                         break;
 3302                 }
 3303                 sent = tp->snd_nxt - nextrexmt;
 3304                 if (sent <= 0) {
 3305                         tp->rexmt_high = old_rexmt_high;
 3306                         break;
 3307                 }
 3308                 pipe += sent;
 3309                 tcpstat.tcps_sndsackpack++;
 3310                 tcpstat.tcps_sndsackbyte += sent;
 3311 
 3312                 if (rescue) {
 3313                         tcpstat.tcps_sackrescue++;
 3314                         tp->rexmt_rescue = tp->snd_nxt;
 3315                         tp->sack_flags |= TSACK_F_SACKRESCUED;
 3316                         break;
 3317                 }
 3318                 if (SEQ_LT(nextrexmt, old_snd_max) &&
 3319                     SEQ_LT(tp->rexmt_high, tp->snd_nxt)) {
 3320                         tp->rexmt_high = seq_min(tp->snd_nxt, old_snd_max);
 3321                         if (tcp_aggressive_rescuesack &&
 3322                             (tp->sack_flags & TSACK_F_SACKRESCUED) &&
 3323                             SEQ_LT(tp->rexmt_rescue, tp->rexmt_high)) {
 3324                                 /* Drag RescueRxt along with HighRxt */
 3325                                 tp->rexmt_rescue = tp->rexmt_high;
 3326                         }
 3327                 }
 3328         }
 3329         if (SEQ_GT(old_snd_nxt, tp->snd_nxt))
 3330                 tp->snd_nxt = old_snd_nxt;
 3331         tp->snd_cwnd = ocwnd;
 3332 }
 3333 
 3334 /*
 3335  * Return TRUE, if some new segments are sent
 3336  */
 3337 static boolean_t
 3338 tcp_sack_limitedxmit(struct tcpcb *tp)
 3339 {
 3340         tcp_seq oldsndnxt = tp->snd_nxt;
 3341         tcp_seq oldsndmax = tp->snd_max;
 3342         u_long ocwnd = tp->snd_cwnd;
 3343         uint32_t pipe, sent;
 3344         boolean_t ret = FALSE;
 3345         tcp_seq_diff_t cwnd_left;
 3346         tcp_seq next;
 3347 
 3348         tp->rexmt_high = tp->snd_una - 1;
 3349         pipe = tcp_sack_compute_pipe(tp);
 3350         cwnd_left = (tcp_seq_diff_t)(ocwnd - pipe);
 3351         if (cwnd_left < (tcp_seq_diff_t)tp->t_maxseg)
 3352                 return FALSE;
 3353 
 3354         if (tcp_do_smartsack)
 3355                 cwnd_left = ulmin(cwnd_left, tp->t_maxseg * TCP_SACK_MAXBURST);
 3356 
 3357         next = tp->snd_nxt = tp->snd_max;
 3358         tp->snd_cwnd = tp->snd_nxt - tp->snd_una +
 3359             rounddown(cwnd_left, tp->t_maxseg);
 3360 
 3361         tp->t_flags |= TF_XMITNOW;
 3362         tcp_output(tp);
 3363 
 3364         sent = tp->snd_nxt - next;
 3365         if (sent > 0) {
 3366                 tcpstat.tcps_sndlimited += howmany(sent, tp->t_maxseg);
 3367                 ret = TRUE;
 3368         }
 3369 
 3370         if (SEQ_LT(oldsndnxt, oldsndmax)) {
 3371                 KASSERT(SEQ_GEQ(oldsndnxt, tp->snd_una),
 3372                     ("snd_una moved in other threads"));
 3373                 tp->snd_nxt = oldsndnxt;
 3374         }
 3375         tp->snd_cwnd = ocwnd;
 3376 
 3377         if (ret && TCP_DO_NCR(tp))
 3378                 tcp_ncr_update_rxtthresh(tp);
 3379 
 3380         return ret;
 3381 }
 3382 
 3383 /*
 3384  * Reset idle time and keep-alive timer, typically called when a valid
 3385  * tcp packet is received but may also be called when FASTKEEP is set
 3386  * to prevent the previous long-timeout from calculating to a drop.
 3387  *
 3388  * Only update t_rcvtime for non-SYN packets.
 3389  *
 3390  * Handle the case where one side thinks the connection is established
 3391  * but the other side has, say, rebooted without cleaning out the
 3392  * connection.   The SYNs could be construed as an attack and wind
 3393  * up ignored, but in case it isn't an attack we can validate the
 3394  * connection by forcing a keepalive.
 3395  */
 3396 void
 3397 tcp_timer_keep_activity(struct tcpcb *tp, int thflags)
 3398 {
 3399         if (TCPS_HAVEESTABLISHED(tp->t_state)) {
 3400                 if ((thflags & (TH_SYN | TH_ACK)) == TH_SYN) {
 3401                         tp->t_flags |= TF_KEEPALIVE;
 3402                         tcp_callout_reset(tp, tp->tt_keep, hz / 2,
 3403                                           tcp_timer_keep);
 3404                 } else {
 3405                         tp->t_rcvtime = ticks;
 3406                         tp->t_flags &= ~TF_KEEPALIVE;
 3407                         tcp_callout_reset(tp, tp->tt_keep,
 3408                                           tp->t_keepidle,
 3409                                           tcp_timer_keep);
 3410                 }
 3411         }
 3412 }
 3413 
 3414 static int
 3415 tcp_rmx_msl(const struct tcpcb *tp)
 3416 {
 3417         struct rtentry *rt;
 3418         struct inpcb *inp = tp->t_inpcb;
 3419         int msl;
 3420 #ifdef INET6
 3421         boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) ? TRUE : FALSE);
 3422 #else
 3423         const boolean_t isipv6 = FALSE;
 3424 #endif
 3425 
 3426         if (isipv6)
 3427                 rt = tcp_rtlookup6(&inp->inp_inc);
 3428         else
 3429                 rt = tcp_rtlookup(&inp->inp_inc);
 3430         if (rt == NULL || rt->rt_rmx.rmx_msl == 0)
 3431                 return tcp_msl;
 3432 
 3433         msl = (rt->rt_rmx.rmx_msl * hz) / 1000;
 3434         if (msl == 0)
 3435                 msl = 1;
 3436 
 3437         return msl;
 3438 }
 3439 
 3440 static void
 3441 tcp_established(struct tcpcb *tp)
 3442 {
 3443         tp->t_state = TCPS_ESTABLISHED;
 3444         tcp_callout_reset(tp, tp->tt_keep, tp->t_keepidle, tcp_timer_keep);
 3445 
 3446         if (tp->t_rxtsyn > 0) {
 3447                 /*
 3448                  * RFC6298:
 3449                  * "If the timer expires awaiting the ACK of a SYN segment
 3450                  *  and the TCP implementation is using an RTO less than 3
 3451                  *  seconds, the RTO MUST be re-initialized to 3 seconds
 3452                  *  when data transmission begins"
 3453                  */
 3454                 if (tp->t_rxtcur < TCPTV_RTOBASE3)
 3455                         tp->t_rxtcur = TCPTV_RTOBASE3;
 3456         }
 3457 }
 3458 
 3459 /*
 3460  * Returns TRUE, if the ACK should be dropped
 3461  */
 3462 static boolean_t
 3463 tcp_recv_dupack(struct tcpcb *tp, tcp_seq th_ack, u_int to_flags)
 3464 {
 3465         boolean_t fast_sack_rexmt = TRUE;
 3466 
 3467         tcpstat.tcps_rcvdupack++;
 3468 
 3469         /*
 3470          * We have outstanding data (other than a window probe),
 3471          * this is a completely duplicate ack (ie, window info
 3472          * didn't change), the ack is the biggest we've seen and
 3473          * we've seen exactly our rexmt threshhold of them, so
 3474          * assume a packet has been dropped and retransmit it.
 3475          * Kludge snd_nxt & the congestion window so we send only
 3476          * this one packet.
 3477          */
 3478         if (IN_FASTRECOVERY(tp)) {
 3479                 if (TCP_DO_SACK(tp)) {
 3480                         boolean_t force = FALSE;
 3481 
 3482                         if (tp->snd_una == tp->rexmt_high &&
 3483                             (to_flags & (TOF_SACK | TOF_SACK_REDUNDANT)) ==
 3484                             TOF_SACK) {
 3485                                 /*
 3486                                  * New segments got SACKed and
 3487                                  * no retransmit yet.
 3488                                  */
 3489                                 force = TRUE;
 3490                         }
 3491 
 3492                         /* No artifical cwnd inflation. */
 3493                         tcp_sack_rexmt(tp, force);
 3494                 } else {
 3495                         /*
 3496                          * Dup acks mean that packets have left
 3497                          * the network (they're now cached at the
 3498                          * receiver) so bump cwnd by the amount in
 3499                          * the receiver to keep a constant cwnd
 3500                          * packets in the network.
 3501                          */
 3502                         tp->snd_cwnd += tp->t_maxseg;
 3503                         tcp_output(tp);
 3504                 }
 3505                 return TRUE;
 3506         } else if (SEQ_LT(th_ack, tp->snd_recover)) {
 3507                 tp->t_dupacks = 0;
 3508                 return FALSE;
 3509         } else if (tcp_ignore_redun_dsack && TCP_DO_SACK(tp) &&
 3510             (to_flags & (TOF_DSACK | TOF_SACK_REDUNDANT)) ==
 3511             (TOF_DSACK | TOF_SACK_REDUNDANT)) {
 3512                 /*
 3513                  * If the ACK carries DSACK and other SACK blocks
 3514                  * carry information that we have already known,
 3515                  * don't count this ACK as duplicate ACK.  This
 3516                  * prevents spurious early retransmit and fast
 3517                  * retransmit.  This also meets the requirement of
 3518                  * RFC3042 that new segments should not be sent if
 3519                  * the SACK blocks do not contain new information
 3520                  * (XXX we actually loosen the requirment that only
 3521                  * DSACK is checked here).
 3522                  *
 3523                  * This kind of ACKs are usually sent after spurious
 3524                  * retransmit.
 3525                  */
 3526                 /* Do nothing; don't change t_dupacks */
 3527                 return TRUE;
 3528         } else if (tp->t_dupacks == 0 && TCP_DO_NCR(tp)) {
 3529                 tcp_ncr_update_rxtthresh(tp);
 3530         }
 3531 
 3532         if (++tp->t_dupacks == tp->t_rxtthresh) {
 3533                 tcp_seq old_snd_nxt;
 3534                 u_int win;
 3535 
 3536 fastretransmit:
 3537                 if (tcp_do_eifel_detect && (tp->t_flags & TF_RCVD_TSTMP)) {
 3538                         tcp_save_congestion_state(tp);
 3539                         tp->rxt_flags |= TRXT_F_FASTREXMT;
 3540                 }
 3541                 /*
 3542                  * We know we're losing at the current window size,
 3543                  * so do congestion avoidance: set ssthresh to half
 3544                  * the current window and pull our congestion window
 3545                  * back to the new ssthresh.
 3546                  */
 3547                 win = min(tp->snd_wnd, tp->snd_cwnd) / 2 / tp->t_maxseg;
 3548                 if (win < 2)
 3549                         win = 2;
 3550                 tp->snd_ssthresh = win * tp->t_maxseg;
 3551                 ENTER_FASTRECOVERY(tp);
 3552                 tp->snd_recover = tp->snd_max;
 3553                 tcp_callout_stop(tp, tp->tt_rexmt);
 3554                 tp->t_rtttime = 0;
 3555                 old_snd_nxt = tp->snd_nxt;
 3556                 tp->snd_nxt = th_ack;
 3557                 if (TCP_DO_SACK(tp)) {
 3558                         uint32_t rxtlen;
 3559 
 3560                         rxtlen = tcp_sack_first_unsacked_len(tp);
 3561                         if (rxtlen > tp->t_maxseg)
 3562                                 rxtlen = tp->t_maxseg;
 3563                         tp->snd_cwnd = rxtlen;
 3564                 } else {
 3565                         tp->snd_cwnd = tp->t_maxseg;
 3566                 }
 3567                 tcp_output(tp);
 3568                 ++tcpstat.tcps_sndfastrexmit;
 3569                 tp->snd_cwnd = tp->snd_ssthresh;
 3570                 tp->rexmt_high = tp->snd_nxt;
 3571                 tp->sack_flags &= ~TSACK_F_SACKRESCUED;
 3572                 if (SEQ_GT(old_snd_nxt, tp->snd_nxt))
 3573                         tp->snd_nxt = old_snd_nxt;
 3574                 KASSERT(tp->snd_limited <= 2, ("tp->snd_limited too big"));
 3575                 if (TCP_DO_SACK(tp)) {
 3576                         if (fast_sack_rexmt)
 3577                                 tcp_sack_rexmt(tp, FALSE);
 3578                 } else {
 3579                         tp->snd_cwnd += tp->t_maxseg *
 3580                             (tp->t_dupacks - tp->snd_limited);
 3581                 }
 3582         } else if ((tcp_do_rfc6675 && TCP_DO_SACK(tp)) || TCP_DO_NCR(tp)) {
 3583                 /*
 3584                  * The RFC6675 recommends to reduce the byte threshold,
 3585                  * and enter fast retransmit if IsLost(snd_una).  However,
 3586                  * if we use IsLost(snd_una) based fast retransmit here,
 3587                  * segments reordering will cause spurious retransmit.  So
 3588                  * we defer the IsLost(snd_una) based fast retransmit until
 3589                  * the extended limited transmit can't send any segments and
 3590                  * early retransmit can't be done.
 3591                  */
 3592                 if (tcp_rfc6675_rxt && tcp_do_rfc6675 &&
 3593                     tcp_sack_islost(&tp->scb, tp->snd_una))
 3594                         goto fastretransmit;
 3595 
 3596                 if (tcp_do_limitedtransmit || TCP_DO_NCR(tp)) {
 3597                         if (!tcp_sack_limitedxmit(tp)) {
 3598                                 /* outstanding data */
 3599                                 uint32_t ownd = tp->snd_max - tp->snd_una;
 3600 
 3601                                 if (need_early_retransmit(tp, ownd)) {
 3602                                         ++tcpstat.tcps_sndearlyrexmit;
 3603                                         tp->rxt_flags |= TRXT_F_EARLYREXMT;
 3604                                         goto fastretransmit;
 3605                                 } else if (tcp_do_rfc6675 &&
 3606                                     tcp_sack_islost(&tp->scb, tp->snd_una)) {
 3607                                         fast_sack_rexmt = FALSE;
 3608                                         goto fastretransmit;
 3609                                 }
 3610                         }
 3611                 }
 3612         } else if (tcp_do_limitedtransmit) {
 3613                 u_long oldcwnd = tp->snd_cwnd;
 3614                 tcp_seq oldsndmax = tp->snd_max;
 3615                 tcp_seq oldsndnxt = tp->snd_nxt;
 3616                 /* outstanding data */
 3617                 uint32_t ownd = tp->snd_max - tp->snd_una;
 3618                 u_int sent;
 3619 
 3620                 KASSERT(tp->t_dupacks == 1 || tp->t_dupacks == 2,
 3621                     ("dupacks not 1 or 2"));
 3622                 if (tp->t_dupacks == 1)
 3623                         tp->snd_limited = 0;
 3624                 tp->snd_nxt = tp->snd_max;
 3625                 tp->snd_cwnd = ownd +
 3626                     (tp->t_dupacks - tp->snd_limited) * tp->t_maxseg;
 3627                 tp->t_flags |= TF_XMITNOW;
 3628                 tcp_output(tp);
 3629 
 3630                 if (SEQ_LT(oldsndnxt, oldsndmax)) {
 3631                         KASSERT(SEQ_GEQ(oldsndnxt, tp->snd_una),
 3632                             ("snd_una moved in other threads"));
 3633                         tp->snd_nxt = oldsndnxt;
 3634                 }
 3635                 tp->snd_cwnd = oldcwnd;
 3636                 sent = tp->snd_max - oldsndmax;
 3637                 if (sent > tp->t_maxseg) {
 3638                         KASSERT((tp->t_dupacks == 2 && tp->snd_limited == 0) ||
 3639                             (sent == tp->t_maxseg + 1 &&
 3640                              (tp->t_flags & TF_SENTFIN)),
 3641                             ("sent too much"));
 3642                         KASSERT(sent <= tp->t_maxseg * 2,
 3643                             ("sent too many segments"));
 3644                         tp->snd_limited = 2;
 3645                         tcpstat.tcps_sndlimited += 2;
 3646                 } else if (sent > 0) {
 3647                         ++tp->snd_limited;
 3648                         ++tcpstat.tcps_sndlimited;
 3649                 } else if (need_early_retransmit(tp, ownd)) {
 3650                         ++tcpstat.tcps_sndearlyrexmit;
 3651                         tp->rxt_flags |= TRXT_F_EARLYREXMT;
 3652                         goto fastretransmit;
 3653                 }
 3654         }
 3655         return TRUE;
 3656 }

Cache object: 2364bf8c5d6128b1ff5e7a5e6784c578


[ 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.