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


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

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

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

    1 /*-
    2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
   30  * $FreeBSD: releng/6.1/sys/netinet/tcp_input.c 156186 2006-03-01 21:13:29Z andre $
   31  */
   32 
   33 #include "opt_ipfw.h"           /* for ipfw_fwd         */
   34 #include "opt_inet.h"
   35 #include "opt_inet6.h"
   36 #include "opt_ipsec.h"
   37 #include "opt_mac.h"
   38 #include "opt_tcpdebug.h"
   39 #include "opt_tcp_input.h"
   40 #include "opt_tcp_sack.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/kernel.h>
   44 #include <sys/mac.h>
   45 #include <sys/malloc.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/proc.h>           /* for proc0 declaration */
   48 #include <sys/protosw.h>
   49 #include <sys/signalvar.h>
   50 #include <sys/socket.h>
   51 #include <sys/socketvar.h>
   52 #include <sys/sysctl.h>
   53 #include <sys/syslog.h>
   54 #include <sys/systm.h>
   55 
   56 #include <machine/cpu.h>        /* before tcp_seq.h, for tcp_random18() */
   57 
   58 #include <vm/uma.h>
   59 
   60 #include <net/if.h>
   61 #include <net/route.h>
   62 
   63 #include <netinet/in.h>
   64 #include <netinet/in_pcb.h>
   65 #include <netinet/in_systm.h>
   66 #include <netinet/in_var.h>
   67 #include <netinet/ip.h>
   68 #include <netinet/ip_icmp.h>    /* for ICMP_BANDLIM             */
   69 #include <netinet/icmp_var.h>   /* for ICMP_BANDLIM             */
   70 #include <netinet/ip_var.h>
   71 #include <netinet/ip6.h>
   72 #include <netinet/icmp6.h>
   73 #include <netinet6/in6_pcb.h>
   74 #include <netinet6/ip6_var.h>
   75 #include <netinet6/nd6.h>
   76 #include <netinet/tcp.h>
   77 #include <netinet/tcp_fsm.h>
   78 #include <netinet/tcp_seq.h>
   79 #include <netinet/tcp_timer.h>
   80 #include <netinet/tcp_var.h>
   81 #include <netinet6/tcp6_var.h>
   82 #include <netinet/tcpip.h>
   83 #ifdef TCPDEBUG
   84 #include <netinet/tcp_debug.h>
   85 #endif /* TCPDEBUG */
   86 
   87 #ifdef FAST_IPSEC
   88 #include <netipsec/ipsec.h>
   89 #include <netipsec/ipsec6.h>
   90 #endif /*FAST_IPSEC*/
   91 
   92 #ifdef IPSEC
   93 #include <netinet6/ipsec.h>
   94 #include <netinet6/ipsec6.h>
   95 #include <netkey/key.h>
   96 #endif /*IPSEC*/
   97 
   98 #include <machine/in_cksum.h>
   99 
  100 static const int tcprexmtthresh = 3;
  101 
  102 struct  tcpstat tcpstat;
  103 SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
  104     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
  105 
  106 static int log_in_vain = 0;
  107 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
  108     &log_in_vain, 0, "Log all incoming TCP connections");
  109 
  110 static int blackhole = 0;
  111 SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
  112         &blackhole, 0, "Do not send RST when dropping refused connections");
  113 
  114 int tcp_delack_enabled = 1;
  115 SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
  116     &tcp_delack_enabled, 0,
  117     "Delay ACK to try and piggyback it onto a data packet");
  118 
  119 #ifdef TCP_DROP_SYNFIN
  120 static int drop_synfin = 0;
  121 SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
  122     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
  123 #endif
  124 
  125 static int tcp_do_rfc3042 = 1;
  126 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
  127     &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
  128 
  129 static int tcp_do_rfc3390 = 1;
  130 SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
  131     &tcp_do_rfc3390, 0,
  132     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
  133 
  134 static int tcp_insecure_rst = 0;
  135 SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
  136     &tcp_insecure_rst, 0,
  137     "Follow the old (insecure) criteria for accepting RST packets.");
  138 
  139 SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
  140             "TCP Segment Reassembly Queue");
  141 
  142 static int tcp_reass_maxseg = 0;
  143 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
  144            &tcp_reass_maxseg, 0,
  145            "Global maximum number of TCP Segments in Reassembly Queue");
  146 
  147 int tcp_reass_qsize = 0;
  148 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
  149            &tcp_reass_qsize, 0,
  150            "Global number of TCP Segments currently in Reassembly Queue");
  151 
  152 static int tcp_reass_maxqlen = 48;
  153 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW,
  154            &tcp_reass_maxqlen, 0,
  155            "Maximum number of TCP Segments per individual Reassembly Queue");
  156 
  157 static int tcp_reass_overflows = 0;
  158 SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
  159            &tcp_reass_overflows, 0,
  160            "Global number of TCP Segment Reassembly Queue Overflows");
  161 
  162 struct inpcbhead tcb;
  163 #define tcb6    tcb  /* for KAME src sync over BSD*'s */
  164 struct inpcbinfo tcbinfo;
  165 struct mtx      *tcbinfo_mtx;
  166 
  167 static void      tcp_dooptions(struct tcpopt *, u_char *, int, int);
  168 
  169 static void      tcp_pulloutofband(struct socket *,
  170                      struct tcphdr *, struct mbuf *, int);
  171 static int       tcp_reass(struct tcpcb *, struct tcphdr *, int *,
  172                      struct mbuf *);
  173 static void      tcp_xmit_timer(struct tcpcb *, int);
  174 static void      tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
  175 static int       tcp_timewait(struct tcptw *, struct tcpopt *,
  176                      struct tcphdr *, struct mbuf *, int);
  177 
  178 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
  179 #ifdef INET6
  180 #define ND6_HINT(tp) \
  181 do { \
  182         if ((tp) && (tp)->t_inpcb && \
  183             ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
  184                 nd6_nud_hint(NULL, NULL, 0); \
  185 } while (0)
  186 #else
  187 #define ND6_HINT(tp)
  188 #endif
  189 
  190 /*
  191  * Indicate whether this ack should be delayed.  We can delay the ack if
  192  *      - there is no delayed ack timer in progress and
  193  *      - our last ack wasn't a 0-sized window.  We never want to delay
  194  *        the ack that opens up a 0-sized window and
  195  *              - delayed acks are enabled or
  196  *              - this is a half-synchronized T/TCP connection.
  197  */
  198 #define DELAY_ACK(tp)                                                   \
  199         ((!callout_active(tp->tt_delack) &&                             \
  200             (tp->t_flags & TF_RXWIN0SENT) == 0) &&                      \
  201             (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
  202 
  203 /* Initialize TCP reassembly queue */
  204 uma_zone_t      tcp_reass_zone;
  205 void
  206 tcp_reass_init()
  207 {
  208         tcp_reass_maxseg = nmbclusters / 16;
  209         TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
  210             &tcp_reass_maxseg);
  211         tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
  212             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  213         uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
  214 }
  215 
  216 static int
  217 tcp_reass(tp, th, tlenp, m)
  218         register struct tcpcb *tp;
  219         register struct tcphdr *th;
  220         int *tlenp;
  221         struct mbuf *m;
  222 {
  223         struct tseg_qent *q;
  224         struct tseg_qent *p = NULL;
  225         struct tseg_qent *nq;
  226         struct tseg_qent *te = NULL;
  227         struct socket *so = tp->t_inpcb->inp_socket;
  228         int flags;
  229 
  230         INP_LOCK_ASSERT(tp->t_inpcb);
  231 
  232         /*
  233          * XXX: tcp_reass() is rather inefficient with its data structures
  234          * and should be rewritten (see NetBSD for optimizations).  While
  235          * doing that it should move to its own file tcp_reass.c.
  236          */
  237 
  238         /*
  239          * Call with th==NULL after become established to
  240          * force pre-ESTABLISHED data up to user socket.
  241          */
  242         if (th == NULL)
  243                 goto present;
  244 
  245         /*
  246          * Limit the number of segments in the reassembly queue to prevent
  247          * holding on to too many segments (and thus running out of mbufs).
  248          * Make sure to let the missing segment through which caused this
  249          * queue.  Always keep one global queue entry spare to be able to
  250          * process the missing segment.
  251          */
  252         if (th->th_seq != tp->rcv_nxt &&
  253             (tcp_reass_qsize + 1 >= tcp_reass_maxseg ||
  254              tp->t_segqlen >= tcp_reass_maxqlen)) {
  255                 tcp_reass_overflows++;
  256                 tcpstat.tcps_rcvmemdrop++;
  257                 m_freem(m);
  258                 *tlenp = 0;
  259                 return (0);
  260         }
  261 
  262         /*
  263          * Allocate a new queue entry. If we can't, or hit the zone limit
  264          * just drop the pkt.
  265          */
  266         te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
  267         if (te == NULL) {
  268                 tcpstat.tcps_rcvmemdrop++;
  269                 m_freem(m);
  270                 *tlenp = 0;
  271                 return (0);
  272         }
  273         tp->t_segqlen++;
  274         tcp_reass_qsize++;
  275 
  276         /*
  277          * Find a segment which begins after this one does.
  278          */
  279         LIST_FOREACH(q, &tp->t_segq, tqe_q) {
  280                 if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
  281                         break;
  282                 p = q;
  283         }
  284 
  285         /*
  286          * If there is a preceding segment, it may provide some of
  287          * our data already.  If so, drop the data from the incoming
  288          * segment.  If it provides all of our data, drop us.
  289          */
  290         if (p != NULL) {
  291                 register int i;
  292                 /* conversion to int (in i) handles seq wraparound */
  293                 i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
  294                 if (i > 0) {
  295                         if (i >= *tlenp) {
  296                                 tcpstat.tcps_rcvduppack++;
  297                                 tcpstat.tcps_rcvdupbyte += *tlenp;
  298                                 m_freem(m);
  299                                 uma_zfree(tcp_reass_zone, te);
  300                                 tp->t_segqlen--;
  301                                 tcp_reass_qsize--;
  302                                 /*
  303                                  * Try to present any queued data
  304                                  * at the left window edge to the user.
  305                                  * This is needed after the 3-WHS
  306                                  * completes.
  307                                  */
  308                                 goto present;   /* ??? */
  309                         }
  310                         m_adj(m, i);
  311                         *tlenp -= i;
  312                         th->th_seq += i;
  313                 }
  314         }
  315         tcpstat.tcps_rcvoopack++;
  316         tcpstat.tcps_rcvoobyte += *tlenp;
  317 
  318         /*
  319          * While we overlap succeeding segments trim them or,
  320          * if they are completely covered, dequeue them.
  321          */
  322         while (q) {
  323                 register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
  324                 if (i <= 0)
  325                         break;
  326                 if (i < q->tqe_len) {
  327                         q->tqe_th->th_seq += i;
  328                         q->tqe_len -= i;
  329                         m_adj(q->tqe_m, i);
  330                         break;
  331                 }
  332 
  333                 nq = LIST_NEXT(q, tqe_q);
  334                 LIST_REMOVE(q, tqe_q);
  335                 m_freem(q->tqe_m);
  336                 uma_zfree(tcp_reass_zone, q);
  337                 tp->t_segqlen--;
  338                 tcp_reass_qsize--;
  339                 q = nq;
  340         }
  341 
  342         /* Insert the new segment queue entry into place. */
  343         te->tqe_m = m;
  344         te->tqe_th = th;
  345         te->tqe_len = *tlenp;
  346 
  347         if (p == NULL) {
  348                 LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
  349         } else {
  350                 LIST_INSERT_AFTER(p, te, tqe_q);
  351         }
  352 
  353 present:
  354         /*
  355          * Present data to user, advancing rcv_nxt through
  356          * completed sequence space.
  357          */
  358         if (!TCPS_HAVEESTABLISHED(tp->t_state))
  359                 return (0);
  360         q = LIST_FIRST(&tp->t_segq);
  361         if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
  362                 return (0);
  363         SOCKBUF_LOCK(&so->so_rcv);
  364         do {
  365                 tp->rcv_nxt += q->tqe_len;
  366                 flags = q->tqe_th->th_flags & TH_FIN;
  367                 nq = LIST_NEXT(q, tqe_q);
  368                 LIST_REMOVE(q, tqe_q);
  369                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
  370                         m_freem(q->tqe_m);
  371                 else
  372                         sbappendstream_locked(&so->so_rcv, q->tqe_m);
  373                 uma_zfree(tcp_reass_zone, q);
  374                 tp->t_segqlen--;
  375                 tcp_reass_qsize--;
  376                 q = nq;
  377         } while (q && q->tqe_th->th_seq == tp->rcv_nxt);
  378         ND6_HINT(tp);
  379         sorwakeup_locked(so);
  380         return (flags);
  381 }
  382 
  383 /*
  384  * TCP input routine, follows pages 65-76 of the
  385  * protocol specification dated September, 1981 very closely.
  386  */
  387 #ifdef INET6
  388 int
  389 tcp6_input(mp, offp, proto)
  390         struct mbuf **mp;
  391         int *offp, proto;
  392 {
  393         register struct mbuf *m = *mp;
  394         struct in6_ifaddr *ia6;
  395 
  396         IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
  397 
  398         /*
  399          * draft-itojun-ipv6-tcp-to-anycast
  400          * better place to put this in?
  401          */
  402         ia6 = ip6_getdstifaddr(m);
  403         if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
  404                 struct ip6_hdr *ip6;
  405 
  406                 ip6 = mtod(m, struct ip6_hdr *);
  407                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
  408                             (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
  409                 return IPPROTO_DONE;
  410         }
  411 
  412         tcp_input(m, *offp);
  413         return IPPROTO_DONE;
  414 }
  415 #endif
  416 
  417 void
  418 tcp_input(m, off0)
  419         register struct mbuf *m;
  420         int off0;
  421 {
  422         register struct tcphdr *th;
  423         register struct ip *ip = NULL;
  424         register struct ipovly *ipov;
  425         register struct inpcb *inp = NULL;
  426         u_char *optp = NULL;
  427         int optlen = 0;
  428         int len, tlen, off;
  429         int drop_hdrlen;
  430         register struct tcpcb *tp = 0;
  431         register int thflags;
  432         struct socket *so = 0;
  433         int todrop, acked, ourfinisacked, needoutput = 0;
  434         u_long tiwin;
  435         struct tcpopt to;               /* options in this segment */
  436         int headlocked = 0;
  437 #ifdef IPFIREWALL_FORWARD
  438         struct m_tag *fwd_tag;
  439 #endif
  440         int rstreason; /* For badport_bandlim accounting purposes */
  441 
  442         struct ip6_hdr *ip6 = NULL;
  443 #ifdef INET6
  444         int isipv6;
  445 #else
  446         const int isipv6 = 0;
  447 #endif
  448 
  449 #ifdef TCPDEBUG
  450         /*
  451          * The size of tcp_saveipgen must be the size of the max ip header,
  452          * now IPv6.
  453          */
  454         u_char tcp_saveipgen[40];
  455         struct tcphdr tcp_savetcp;
  456         short ostate = 0;
  457 #endif
  458 
  459 #ifdef INET6
  460         isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
  461 #endif
  462         bzero((char *)&to, sizeof(to));
  463 
  464         tcpstat.tcps_rcvtotal++;
  465 
  466         if (isipv6) {
  467 #ifdef INET6
  468                 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
  469                 ip6 = mtod(m, struct ip6_hdr *);
  470                 tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
  471                 if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
  472                         tcpstat.tcps_rcvbadsum++;
  473                         goto drop;
  474                 }
  475                 th = (struct tcphdr *)((caddr_t)ip6 + off0);
  476 
  477                 /*
  478                  * Be proactive about unspecified IPv6 address in source.
  479                  * As we use all-zero to indicate unbounded/unconnected pcb,
  480                  * unspecified IPv6 address can be used to confuse us.
  481                  *
  482                  * Note that packets with unspecified IPv6 destination is
  483                  * already dropped in ip6_input.
  484                  */
  485                 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
  486                         /* XXX stat */
  487                         goto drop;
  488                 }
  489 #else
  490                 th = NULL;              /* XXX: avoid compiler warning */
  491 #endif
  492         } else {
  493                 /*
  494                  * Get IP and TCP header together in first mbuf.
  495                  * Note: IP leaves IP header in first mbuf.
  496                  */
  497                 if (off0 > sizeof (struct ip)) {
  498                         ip_stripoptions(m, (struct mbuf *)0);
  499                         off0 = sizeof(struct ip);
  500                 }
  501                 if (m->m_len < sizeof (struct tcpiphdr)) {
  502                         if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
  503                                 tcpstat.tcps_rcvshort++;
  504                                 return;
  505                         }
  506                 }
  507                 ip = mtod(m, struct ip *);
  508                 ipov = (struct ipovly *)ip;
  509                 th = (struct tcphdr *)((caddr_t)ip + off0);
  510                 tlen = ip->ip_len;
  511 
  512                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
  513                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  514                                 th->th_sum = m->m_pkthdr.csum_data;
  515                         else
  516                                 th->th_sum = in_pseudo(ip->ip_src.s_addr,
  517                                                 ip->ip_dst.s_addr,
  518                                                 htonl(m->m_pkthdr.csum_data +
  519                                                         ip->ip_len +
  520                                                         IPPROTO_TCP));
  521                         th->th_sum ^= 0xffff;
  522 #ifdef TCPDEBUG
  523                         ipov->ih_len = (u_short)tlen;
  524                         ipov->ih_len = htons(ipov->ih_len);
  525 #endif
  526                 } else {
  527                         /*
  528                          * Checksum extended TCP header and data.
  529                          */
  530                         len = sizeof (struct ip) + tlen;
  531                         bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
  532                         ipov->ih_len = (u_short)tlen;
  533                         ipov->ih_len = htons(ipov->ih_len);
  534                         th->th_sum = in_cksum(m, len);
  535                 }
  536                 if (th->th_sum) {
  537                         tcpstat.tcps_rcvbadsum++;
  538                         goto drop;
  539                 }
  540 #ifdef INET6
  541                 /* Re-initialization for later version check */
  542                 ip->ip_v = IPVERSION;
  543 #endif
  544         }
  545 
  546         /*
  547          * Check that TCP offset makes sense,
  548          * pull out TCP options and adjust length.              XXX
  549          */
  550         off = th->th_off << 2;
  551         if (off < sizeof (struct tcphdr) || off > tlen) {
  552                 tcpstat.tcps_rcvbadoff++;
  553                 goto drop;
  554         }
  555         tlen -= off;    /* tlen is used instead of ti->ti_len */
  556         if (off > sizeof (struct tcphdr)) {
  557                 if (isipv6) {
  558 #ifdef INET6
  559                         IP6_EXTHDR_CHECK(m, off0, off, );
  560                         ip6 = mtod(m, struct ip6_hdr *);
  561                         th = (struct tcphdr *)((caddr_t)ip6 + off0);
  562 #endif
  563                 } else {
  564                         if (m->m_len < sizeof(struct ip) + off) {
  565                                 if ((m = m_pullup(m, sizeof (struct ip) + off))
  566                                                 == 0) {
  567                                         tcpstat.tcps_rcvshort++;
  568                                         return;
  569                                 }
  570                                 ip = mtod(m, struct ip *);
  571                                 ipov = (struct ipovly *)ip;
  572                                 th = (struct tcphdr *)((caddr_t)ip + off0);
  573                         }
  574                 }
  575                 optlen = off - sizeof (struct tcphdr);
  576                 optp = (u_char *)(th + 1);
  577         }
  578         thflags = th->th_flags;
  579 
  580 #ifdef TCP_DROP_SYNFIN
  581         /*
  582          * If the drop_synfin option is enabled, drop all packets with
  583          * both the SYN and FIN bits set. This prevents e.g. nmap from
  584          * identifying the TCP/IP stack.
  585          *
  586          * This is a violation of the TCP specification.
  587          */
  588         if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
  589                 goto drop;
  590 #endif
  591 
  592         /*
  593          * Convert TCP protocol specific fields to host format.
  594          */
  595         th->th_seq = ntohl(th->th_seq);
  596         th->th_ack = ntohl(th->th_ack);
  597         th->th_win = ntohs(th->th_win);
  598         th->th_urp = ntohs(th->th_urp);
  599 
  600         /*
  601          * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
  602          * until after ip6_savecontrol() is called and before other functions
  603          * which don't want those proto headers.
  604          * Because ip6_savecontrol() is going to parse the mbuf to
  605          * search for data to be passed up to user-land, it wants mbuf
  606          * parameters to be unchanged.
  607          * XXX: the call of ip6_savecontrol() has been obsoleted based on
  608          * latest version of the advanced API (20020110).
  609          */
  610         drop_hdrlen = off0 + off;
  611 
  612         /*
  613          * Locate pcb for segment.
  614          */
  615         INP_INFO_WLOCK(&tcbinfo);
  616         headlocked = 1;
  617 findpcb:
  618         KASSERT(headlocked, ("tcp_input: findpcb: head not locked"));
  619 #ifdef IPFIREWALL_FORWARD
  620         /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
  621         fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
  622 
  623         if (fwd_tag != NULL && isipv6 == 0) {   /* IPv6 support is not yet */
  624                 struct sockaddr_in *next_hop;
  625 
  626                 next_hop = (struct sockaddr_in *)(fwd_tag+1);
  627                 /*
  628                  * Transparently forwarded. Pretend to be the destination.
  629                  * already got one like this?
  630                  */
  631                 inp = in_pcblookup_hash(&tcbinfo,
  632                                         ip->ip_src, th->th_sport,
  633                                         ip->ip_dst, th->th_dport,
  634                                         0, m->m_pkthdr.rcvif);
  635                 if (!inp) {
  636                         /* It's new.  Try to find the ambushing socket. */
  637                         inp = in_pcblookup_hash(&tcbinfo,
  638                                                 ip->ip_src, th->th_sport,
  639                                                 next_hop->sin_addr,
  640                                                 next_hop->sin_port ?
  641                                                     ntohs(next_hop->sin_port) :
  642                                                     th->th_dport,
  643                                                 1, m->m_pkthdr.rcvif);
  644                 }
  645                 /* Remove the tag from the packet.  We don't need it anymore. */
  646                 m_tag_delete(m, fwd_tag);
  647         } else {
  648 #endif /* IPFIREWALL_FORWARD */
  649                 if (isipv6) {
  650 #ifdef INET6
  651                         inp = in6_pcblookup_hash(&tcbinfo,
  652                                                  &ip6->ip6_src, th->th_sport,
  653                                                  &ip6->ip6_dst, th->th_dport,
  654                                                  1, m->m_pkthdr.rcvif);
  655 #endif
  656                 } else
  657                         inp = in_pcblookup_hash(&tcbinfo,
  658                                                 ip->ip_src, th->th_sport,
  659                                                 ip->ip_dst, th->th_dport,
  660                                                 1, m->m_pkthdr.rcvif);
  661 #ifdef IPFIREWALL_FORWARD
  662         }
  663 #endif /* IPFIREWALL_FORWARD */
  664 
  665 #if defined(IPSEC) || defined(FAST_IPSEC)
  666 #ifdef INET6
  667         if (isipv6) {
  668                 if (inp != NULL && ipsec6_in_reject(m, inp)) {
  669 #ifdef IPSEC
  670                         ipsec6stat.in_polvio++;
  671 #endif
  672                         goto drop;
  673                 }
  674         } else
  675 #endif /* INET6 */
  676         if (inp != NULL && ipsec4_in_reject(m, inp)) {
  677 #ifdef IPSEC
  678                 ipsecstat.in_polvio++;
  679 #endif
  680                 goto drop;
  681         }
  682 #endif /*IPSEC || FAST_IPSEC*/
  683 
  684         /*
  685          * If the state is CLOSED (i.e., TCB does not exist) then
  686          * all data in the incoming segment is discarded.
  687          * If the TCB exists but is in CLOSED state, it is embryonic,
  688          * but should either do a listen or a connect soon.
  689          */
  690         if (inp == NULL) {
  691                 if (log_in_vain) {
  692 #ifdef INET6
  693                         char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
  694 #else
  695                         char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
  696 #endif
  697 
  698                         if (isipv6) {
  699 #ifdef INET6
  700                                 strcpy(dbuf, "[");
  701                                 strcpy(sbuf, "[");
  702                                 strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
  703                                 strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
  704                                 strcat(dbuf, "]");
  705                                 strcat(sbuf, "]");
  706 #endif
  707                         } else {
  708                                 strcpy(dbuf, inet_ntoa(ip->ip_dst));
  709                                 strcpy(sbuf, inet_ntoa(ip->ip_src));
  710                         }
  711                         switch (log_in_vain) {
  712                         case 1:
  713                                 if ((thflags & TH_SYN) == 0)
  714                                         break;
  715                                 /* FALLTHROUGH */
  716                         case 2:
  717                                 log(LOG_INFO,
  718                                     "Connection attempt to TCP %s:%d "
  719                                     "from %s:%d flags:0x%02x\n",
  720                                     dbuf, ntohs(th->th_dport), sbuf,
  721                                     ntohs(th->th_sport), thflags);
  722                                 break;
  723                         default:
  724                                 break;
  725                         }
  726                 }
  727                 if (blackhole) {
  728                         switch (blackhole) {
  729                         case 1:
  730                                 if (thflags & TH_SYN)
  731                                         goto drop;
  732                                 break;
  733                         case 2:
  734                                 goto drop;
  735                         default:
  736                                 goto drop;
  737                         }
  738                 }
  739                 rstreason = BANDLIM_RST_CLOSEDPORT;
  740                 goto dropwithreset;
  741         }
  742         INP_LOCK(inp);
  743 
  744         /* Check the minimum TTL for socket. */
  745         if (inp->inp_ip_minttl != 0) {
  746 #ifdef INET6
  747                 if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
  748                         goto drop;
  749                 else
  750 #endif
  751                 if (inp->inp_ip_minttl > ip->ip_ttl)
  752                         goto drop;
  753         }
  754 
  755         if (inp->inp_vflag & INP_TIMEWAIT) {
  756                 /*
  757                  * The only option of relevance is TOF_CC, and only if
  758                  * present in a SYN segment.  See tcp_timewait().
  759                  */
  760                 if (thflags & TH_SYN)
  761                         tcp_dooptions(&to, optp, optlen, 1);
  762                 if (tcp_timewait((struct tcptw *)inp->inp_ppcb,
  763                     &to, th, m, tlen))
  764                         goto findpcb;
  765                 /*
  766                  * tcp_timewait unlocks inp.
  767                  */
  768                 INP_INFO_WUNLOCK(&tcbinfo);
  769                 return;
  770         }
  771         tp = intotcpcb(inp);
  772         if (tp == 0) {
  773                 INP_UNLOCK(inp);
  774                 rstreason = BANDLIM_RST_CLOSEDPORT;
  775                 goto dropwithreset;
  776         }
  777         if (tp->t_state == TCPS_CLOSED)
  778                 goto drop;
  779 
  780         /* Unscale the window into a 32-bit value. */
  781         if ((thflags & TH_SYN) == 0)
  782                 tiwin = th->th_win << tp->snd_scale;
  783         else
  784                 tiwin = th->th_win;
  785 
  786 #ifdef MAC
  787         INP_LOCK_ASSERT(inp);
  788         if (mac_check_inpcb_deliver(inp, m))
  789                 goto drop;
  790 #endif
  791         so = inp->inp_socket;
  792 #ifdef TCPDEBUG
  793         if (so->so_options & SO_DEBUG) {
  794                 ostate = tp->t_state;
  795                 if (isipv6)
  796                         bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
  797                 else
  798                         bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
  799                 tcp_savetcp = *th;
  800         }
  801 #endif
  802         if (so->so_options & SO_ACCEPTCONN) {
  803                 struct in_conninfo inc;
  804 
  805 #ifdef INET6
  806                 inc.inc_isipv6 = isipv6;
  807 #endif
  808                 if (isipv6) {
  809                         inc.inc6_faddr = ip6->ip6_src;
  810                         inc.inc6_laddr = ip6->ip6_dst;
  811                 } else {
  812                         inc.inc_faddr = ip->ip_src;
  813                         inc.inc_laddr = ip->ip_dst;
  814                 }
  815                 inc.inc_fport = th->th_sport;
  816                 inc.inc_lport = th->th_dport;
  817 
  818                 /*
  819                  * If the state is LISTEN then ignore segment if it contains
  820                  * a RST.  If the segment contains an ACK then it is bad and
  821                  * send a RST.  If it does not contain a SYN then it is not
  822                  * interesting; drop it.
  823                  *
  824                  * If the state is SYN_RECEIVED (syncache) and seg contains
  825                  * an ACK, but not for our SYN/ACK, send a RST.  If the seg
  826                  * contains a RST, check the sequence number to see if it
  827                  * is a valid reset segment.
  828                  */
  829                 if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
  830                         if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
  831                                 if (!syncache_expand(&inc, th, &so, m)) {
  832                                         /*
  833                                          * No syncache entry, or ACK was not
  834                                          * for our SYN/ACK.  Send a RST.
  835                                          */
  836                                         tcpstat.tcps_badsyn++;
  837                                         rstreason = BANDLIM_RST_OPENPORT;
  838                                         goto dropwithreset;
  839                                 }
  840                                 if (so == NULL) {
  841                                         /*
  842                                          * Could not complete 3-way handshake,
  843                                          * connection is being closed down, and
  844                                          * syncache will free mbuf.
  845                                          */
  846                                         INP_UNLOCK(inp);
  847                                         INP_INFO_WUNLOCK(&tcbinfo);
  848                                         return;
  849                                 }
  850                                 /*
  851                                  * Socket is created in state SYN_RECEIVED.
  852                                  * Continue processing segment.
  853                                  */
  854                                 INP_UNLOCK(inp);
  855                                 inp = sotoinpcb(so);
  856                                 INP_LOCK(inp);
  857                                 tp = intotcpcb(inp);
  858                                 /*
  859                                  * This is what would have happened in
  860                                  * tcp_output() when the SYN,ACK was sent.
  861                                  */
  862                                 tp->snd_up = tp->snd_una;
  863                                 tp->snd_max = tp->snd_nxt = tp->iss + 1;
  864                                 tp->last_ack_sent = tp->rcv_nxt;
  865                                 /*
  866                                  * RFC1323: The window in SYN & SYN/ACK
  867                                  * segments is never scaled.
  868                                  */
  869                                 tp->snd_wnd = tiwin;    /* unscaled */
  870                                 goto after_listen;
  871                         }
  872                         if (thflags & TH_RST) {
  873                                 syncache_chkrst(&inc, th);
  874                                 goto drop;
  875                         }
  876                         if (thflags & TH_ACK) {
  877                                 syncache_badack(&inc);
  878                                 tcpstat.tcps_badsyn++;
  879                                 rstreason = BANDLIM_RST_OPENPORT;
  880                                 goto dropwithreset;
  881                         }
  882                         goto drop;
  883                 }
  884 
  885                 /*
  886                  * Segment's flags are (SYN) or (SYN|FIN).
  887                  */
  888 #ifdef INET6
  889                 /*
  890                  * If deprecated address is forbidden,
  891                  * we do not accept SYN to deprecated interface
  892                  * address to prevent any new inbound connection from
  893                  * getting established.
  894                  * When we do not accept SYN, we send a TCP RST,
  895                  * with deprecated source address (instead of dropping
  896                  * it).  We compromise it as it is much better for peer
  897                  * to send a RST, and RST will be the final packet
  898                  * for the exchange.
  899                  *
  900                  * If we do not forbid deprecated addresses, we accept
  901                  * the SYN packet.  RFC2462 does not suggest dropping
  902                  * SYN in this case.
  903                  * If we decipher RFC2462 5.5.4, it says like this:
  904                  * 1. use of deprecated addr with existing
  905                  *    communication is okay - "SHOULD continue to be
  906                  *    used"
  907                  * 2. use of it with new communication:
  908                  *   (2a) "SHOULD NOT be used if alternate address
  909                  *        with sufficient scope is available"
  910                  *   (2b) nothing mentioned otherwise.
  911                  * Here we fall into (2b) case as we have no choice in
  912                  * our source address selection - we must obey the peer.
  913                  *
  914                  * The wording in RFC2462 is confusing, and there are
  915                  * multiple description text for deprecated address
  916                  * handling - worse, they are not exactly the same.
  917                  * I believe 5.5.4 is the best one, so we follow 5.5.4.
  918                  */
  919                 if (isipv6 && !ip6_use_deprecated) {
  920                         struct in6_ifaddr *ia6;
  921 
  922                         if ((ia6 = ip6_getdstifaddr(m)) &&
  923                             (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
  924                                 INP_UNLOCK(inp);
  925                                 tp = NULL;
  926                                 rstreason = BANDLIM_RST_OPENPORT;
  927                                 goto dropwithreset;
  928                         }
  929                 }
  930 #endif
  931                 /*
  932                  * If it is from this socket, drop it, it must be forged.
  933                  * Don't bother responding if the destination was a broadcast.
  934                  */
  935                 if (th->th_dport == th->th_sport) {
  936                         if (isipv6) {
  937                                 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
  938                                                        &ip6->ip6_src))
  939                                         goto drop;
  940                         } else {
  941                                 if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
  942                                         goto drop;
  943                         }
  944                 }
  945                 /*
  946                  * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
  947                  *
  948                  * Note that it is quite possible to receive unicast
  949                  * link-layer packets with a broadcast IP address. Use
  950                  * in_broadcast() to find them.
  951                  */
  952                 if (m->m_flags & (M_BCAST|M_MCAST))
  953                         goto drop;
  954                 if (isipv6) {
  955                         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
  956                             IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
  957                                 goto drop;
  958                 } else {
  959                         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  960                             IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
  961                             ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
  962                             in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
  963                                 goto drop;
  964                 }
  965                 /*
  966                  * SYN appears to be valid; create compressed TCP state
  967                  * for syncache, or perform t/tcp connection.
  968                  */
  969                 if (so->so_qlen <= so->so_qlimit) {
  970 #ifdef TCPDEBUG
  971                         if (so->so_options & SO_DEBUG)
  972                                 tcp_trace(TA_INPUT, ostate, tp,
  973                                     (void *)tcp_saveipgen, &tcp_savetcp, 0);
  974 #endif
  975                         tcp_dooptions(&to, optp, optlen, 1);
  976                         if (!syncache_add(&inc, &to, th, &so, m))
  977                                 goto drop;
  978                         if (so == NULL) {
  979                                 /*
  980                                  * Entry added to syncache, mbuf used to
  981                                  * send SYN,ACK packet.
  982                                  */
  983                                 KASSERT(headlocked, ("headlocked"));
  984                                 INP_UNLOCK(inp);
  985                                 INP_INFO_WUNLOCK(&tcbinfo);
  986                                 return;
  987                         }
  988                         /*
  989                          * Segment passed TAO tests.
  990                          */
  991                         INP_UNLOCK(inp);
  992                         inp = sotoinpcb(so);
  993                         INP_LOCK(inp);
  994                         tp = intotcpcb(inp);
  995                         tp->snd_wnd = tiwin;
  996                         tp->t_starttime = ticks;
  997                         tp->t_state = TCPS_ESTABLISHED;
  998 
  999                         /*
 1000                          * T/TCP logic:
 1001                          * If there is a FIN or if there is data, then
 1002                          * delay SYN,ACK(SYN) in the hope of piggy-backing
 1003                          * it on a response segment.  Otherwise must send
 1004                          * ACK now in case the other side is slow starting.
 1005                          */
 1006                         if (thflags & TH_FIN || tlen != 0)
 1007                                 tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
 1008                         else
 1009                                 tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
 1010                         tcpstat.tcps_connects++;
 1011                         soisconnected(so);
 1012                         goto trimthenstep6;
 1013                 }
 1014                 goto drop;
 1015         }
 1016 after_listen:
 1017         KASSERT(headlocked, ("tcp_input: after_listen: head not locked"));
 1018         INP_LOCK_ASSERT(inp);
 1019 
 1020         /* Syncache takes care of sockets in the listen state. */
 1021         KASSERT(tp->t_state != TCPS_LISTEN, ("tcp_input: TCPS_LISTEN"));
 1022 
 1023         /*
 1024          * This is the second part of the MSS DoS prevention code (after
 1025          * minmss on the sending side) and it deals with too many too small
 1026          * tcp packets in a too short timeframe (1 second).
 1027          *
 1028          * For every full second we count the number of received packets
 1029          * and bytes. If we get a lot of packets per second for this connection
 1030          * (tcp_minmssoverload) we take a closer look at it and compute the
 1031          * average packet size for the past second. If that is less than
 1032          * tcp_minmss we get too many packets with very small payload which
 1033          * is not good and burdens our system (and every packet generates
 1034          * a wakeup to the process connected to our socket). We can reasonable
 1035          * expect this to be small packet DoS attack to exhaust our CPU
 1036          * cycles.
 1037          *
 1038          * Care has to be taken for the minimum packet overload value. This
 1039          * value defines the minimum number of packets per second before we
 1040          * start to worry. This must not be too low to avoid killing for
 1041          * example interactive connections with many small packets like
 1042          * telnet or SSH.
 1043          *
 1044          * Setting either tcp_minmssoverload or tcp_minmss to "" disables
 1045          * this check.
 1046          *
 1047          * Account for packet if payload packet, skip over ACK, etc.
 1048          */
 1049         if (tcp_minmss && tcp_minmssoverload &&
 1050             tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
 1051                 if ((unsigned int)(tp->rcv_second - ticks) < hz) {
 1052                         tp->rcv_pps++;
 1053                         tp->rcv_byps += tlen + off;
 1054                         if (tp->rcv_pps > tcp_minmssoverload) {
 1055                                 if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) {
 1056                                         printf("too many small tcp packets from "
 1057                                                "%s:%u, av. %lubyte/packet, "
 1058                                                "dropping connection\n",
 1059 #ifdef INET6
 1060                                                 isipv6 ?
 1061                                                 ip6_sprintf(&inp->inp_inc.inc6_faddr) :
 1062 #endif
 1063                                                 inet_ntoa(inp->inp_inc.inc_faddr),
 1064                                                 inp->inp_inc.inc_fport,
 1065                                                 tp->rcv_byps / tp->rcv_pps);
 1066                                         KASSERT(headlocked, ("tcp_input: "
 1067                                             "after_listen: tcp_drop: head "
 1068                                             "not locked"));
 1069                                         tp = tcp_drop(tp, ECONNRESET);
 1070                                         tcpstat.tcps_minmssdrops++;
 1071                                         goto drop;
 1072                                 }
 1073                         }
 1074                 } else {
 1075                         tp->rcv_second = ticks + hz;
 1076                         tp->rcv_pps = 1;
 1077                         tp->rcv_byps = tlen + off;
 1078                 }
 1079         }
 1080 
 1081         /*
 1082          * Segment received on connection.
 1083          * Reset idle time and keep-alive timer.
 1084          */
 1085         tp->t_rcvtime = ticks;
 1086         if (TCPS_HAVEESTABLISHED(tp->t_state))
 1087                 callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
 1088 
 1089         /*
 1090          * Process options only when we get SYN/ACK back. The SYN case
 1091          * for incoming connections is handled in tcp_syncache.
 1092          * XXX this is traditional behavior, may need to be cleaned up.
 1093          */
 1094         tcp_dooptions(&to, optp, optlen, thflags & TH_SYN);
 1095         if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
 1096                 if (to.to_flags & TOF_SCALE) {
 1097                         tp->t_flags |= TF_RCVD_SCALE;
 1098                         tp->requested_s_scale = to.to_requested_s_scale;
 1099                 }
 1100                 if (to.to_flags & TOF_TS) {
 1101                         tp->t_flags |= TF_RCVD_TSTMP;
 1102                         tp->ts_recent = to.to_tsval;
 1103                         tp->ts_recent_age = ticks;
 1104                 }
 1105                 if (to.to_flags & TOF_MSS)
 1106                         tcp_mss(tp, to.to_mss);
 1107                 if (tp->sack_enable) {
 1108                         if (!(to.to_flags & TOF_SACK))
 1109                                 tp->sack_enable = 0;
 1110                         else
 1111                                 tp->t_flags |= TF_SACK_PERMIT;
 1112                 }
 1113 
 1114         }
 1115 
 1116         /*
 1117          * Header prediction: check for the two common cases
 1118          * of a uni-directional data xfer.  If the packet has
 1119          * no control flags, is in-sequence, the window didn't
 1120          * change and we're not retransmitting, it's a
 1121          * candidate.  If the length is zero and the ack moved
 1122          * forward, we're the sender side of the xfer.  Just
 1123          * free the data acked & wake any higher level process
 1124          * that was blocked waiting for space.  If the length
 1125          * is non-zero and the ack didn't move, we're the
 1126          * receiver side.  If we're getting packets in-order
 1127          * (the reassembly queue is empty), add the data to
 1128          * the socket buffer and note that we need a delayed ack.
 1129          * Make sure that the hidden state-flags are also off.
 1130          * Since we check for TCPS_ESTABLISHED above, it can only
 1131          * be TH_NEEDSYN.
 1132          */
 1133         if (tp->t_state == TCPS_ESTABLISHED &&
 1134             (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
 1135             ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
 1136             ((to.to_flags & TOF_TS) == 0 ||
 1137              TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
 1138              th->th_seq == tp->rcv_nxt && tiwin && tiwin == tp->snd_wnd &&
 1139              tp->snd_nxt == tp->snd_max) {
 1140 
 1141                 /*
 1142                  * If last ACK falls within this segment's sequence numbers,
 1143                  * record the timestamp.
 1144                  * NOTE that the test is modified according to the latest
 1145                  * proposal of the tcplw@cray.com list (Braden 1993/04/26).
 1146                  */
 1147                 if ((to.to_flags & TOF_TS) != 0 &&
 1148                     SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
 1149                         tp->ts_recent_age = ticks;
 1150                         tp->ts_recent = to.to_tsval;
 1151                 }
 1152 
 1153                 if (tlen == 0) {
 1154                         if (SEQ_GT(th->th_ack, tp->snd_una) &&
 1155                             SEQ_LEQ(th->th_ack, tp->snd_max) &&
 1156                             tp->snd_cwnd >= tp->snd_wnd &&
 1157                             ((!tcp_do_newreno && !tp->sack_enable &&
 1158                               tp->t_dupacks < tcprexmtthresh) ||
 1159                              ((tcp_do_newreno || tp->sack_enable) &&
 1160                               !IN_FASTRECOVERY(tp) && to.to_nsacks == 0 &&
 1161                               TAILQ_EMPTY(&tp->snd_holes)))) {
 1162                                 KASSERT(headlocked, ("headlocked"));
 1163                                 INP_INFO_WUNLOCK(&tcbinfo);
 1164                                 headlocked = 0;
 1165                                 /*
 1166                                  * this is a pure ack for outstanding data.
 1167                                  */
 1168                                 ++tcpstat.tcps_predack;
 1169                                 /*
 1170                                  * "bad retransmit" recovery
 1171                                  */
 1172                                 if (tp->t_rxtshift == 1 &&
 1173                                     ticks < tp->t_badrxtwin) {
 1174                                         ++tcpstat.tcps_sndrexmitbad;
 1175                                         tp->snd_cwnd = tp->snd_cwnd_prev;
 1176                                         tp->snd_ssthresh =
 1177                                             tp->snd_ssthresh_prev;
 1178                                         tp->snd_recover = tp->snd_recover_prev;
 1179                                         if (tp->t_flags & TF_WASFRECOVERY)
 1180                                             ENTER_FASTRECOVERY(tp);
 1181                                         tp->snd_nxt = tp->snd_max;
 1182                                         tp->t_badrxtwin = 0;
 1183                                 }
 1184 
 1185                                 /*
 1186                                  * Recalculate the transmit timer / rtt.
 1187                                  *
 1188                                  * Some boxes send broken timestamp replies
 1189                                  * during the SYN+ACK phase, ignore
 1190                                  * timestamps of 0 or we could calculate a
 1191                                  * huge RTT and blow up the retransmit timer.
 1192                                  */
 1193                                 if ((to.to_flags & TOF_TS) != 0 &&
 1194                                     to.to_tsecr) {
 1195                                         if (!tp->t_rttlow ||
 1196                                             tp->t_rttlow > ticks - to.to_tsecr)
 1197                                                 tp->t_rttlow = ticks - to.to_tsecr;
 1198                                         tcp_xmit_timer(tp,
 1199                                             ticks - to.to_tsecr + 1);
 1200                                 } else if (tp->t_rtttime &&
 1201                                             SEQ_GT(th->th_ack, tp->t_rtseq)) {
 1202                                         if (!tp->t_rttlow ||
 1203                                             tp->t_rttlow > ticks - tp->t_rtttime)
 1204                                                 tp->t_rttlow = ticks - tp->t_rtttime;
 1205                                         tcp_xmit_timer(tp,
 1206                                                         ticks - tp->t_rtttime);
 1207                                 }
 1208                                 tcp_xmit_bandwidth_limit(tp, th->th_ack);
 1209                                 acked = th->th_ack - tp->snd_una;
 1210                                 tcpstat.tcps_rcvackpack++;
 1211                                 tcpstat.tcps_rcvackbyte += acked;
 1212                                 sbdrop(&so->so_snd, acked);
 1213                                 if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
 1214                                     SEQ_LEQ(th->th_ack, tp->snd_recover))
 1215                                         tp->snd_recover = th->th_ack - 1;
 1216                                 tp->snd_una = th->th_ack;
 1217                                 /*
 1218                                  * pull snd_wl2 up to prevent seq wrap relative
 1219                                  * to th_ack.
 1220                                  */
 1221                                 tp->snd_wl2 = th->th_ack;
 1222                                 tp->t_dupacks = 0;
 1223                                 m_freem(m);
 1224                                 ND6_HINT(tp); /* some progress has been done */
 1225 
 1226                                 /*
 1227                                  * If all outstanding data are acked, stop
 1228                                  * retransmit timer, otherwise restart timer
 1229                                  * using current (possibly backed-off) value.
 1230                                  * If process is waiting for space,
 1231                                  * wakeup/selwakeup/signal.  If data
 1232                                  * are ready to send, let tcp_output
 1233                                  * decide between more output or persist.
 1234 
 1235 #ifdef TCPDEBUG
 1236                                 if (so->so_options & SO_DEBUG)
 1237                                         tcp_trace(TA_INPUT, ostate, tp,
 1238                                             (void *)tcp_saveipgen,
 1239                                             &tcp_savetcp, 0);
 1240 #endif
 1241                                  */
 1242                                 if (tp->snd_una == tp->snd_max)
 1243                                         callout_stop(tp->tt_rexmt);
 1244                                 else if (!callout_active(tp->tt_persist))
 1245                                         callout_reset(tp->tt_rexmt,
 1246                                                       tp->t_rxtcur,
 1247                                                       tcp_timer_rexmt, tp);
 1248 
 1249                                 sowwakeup(so);
 1250                                 if (so->so_snd.sb_cc)
 1251                                         (void) tcp_output(tp);
 1252                                 goto check_delack;
 1253                         }
 1254                 } else if (th->th_ack == tp->snd_una &&
 1255                     LIST_EMPTY(&tp->t_segq) &&
 1256                     tlen <= sbspace(&so->so_rcv)) {
 1257                         KASSERT(headlocked, ("headlocked"));
 1258                         INP_INFO_WUNLOCK(&tcbinfo);
 1259                         headlocked = 0;
 1260                         /*
 1261                          * this is a pure, in-sequence data packet
 1262                          * with nothing on the reassembly queue and
 1263                          * we have enough buffer space to take it.
 1264                          */
 1265                         /* Clean receiver SACK report if present */
 1266                         if (tp->sack_enable && tp->rcv_numsacks)
 1267                                 tcp_clean_sackreport(tp);
 1268                         ++tcpstat.tcps_preddat;
 1269                         tp->rcv_nxt += tlen;
 1270                         /*
 1271                          * Pull snd_wl1 up to prevent seq wrap relative to
 1272                          * th_seq.
 1273                          */
 1274                         tp->snd_wl1 = th->th_seq;
 1275                         /*
 1276                          * Pull rcv_up up to prevent seq wrap relative to
 1277                          * rcv_nxt.
 1278                          */
 1279                         tp->rcv_up = tp->rcv_nxt;
 1280                         tcpstat.tcps_rcvpack++;
 1281                         tcpstat.tcps_rcvbyte += tlen;
 1282                         ND6_HINT(tp);   /* some progress has been done */
 1283                         /*
 1284 #ifdef TCPDEBUG
 1285                         if (so->so_options & SO_DEBUG)
 1286                                 tcp_trace(TA_INPUT, ostate, tp,
 1287                                     (void *)tcp_saveipgen, &tcp_savetcp, 0);
 1288 #endif
 1289                          * Add data to socket buffer.
 1290                          */
 1291                         SOCKBUF_LOCK(&so->so_rcv);
 1292                         if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
 1293                                 m_freem(m);
 1294                         } else {
 1295                                 m_adj(m, drop_hdrlen);  /* delayed header drop */
 1296                                 sbappendstream_locked(&so->so_rcv, m);
 1297                         }
 1298                         sorwakeup_locked(so);
 1299                         if (DELAY_ACK(tp)) {
 1300                                 tp->t_flags |= TF_DELACK;
 1301                         } else {
 1302                                 tp->t_flags |= TF_ACKNOW;
 1303                                 tcp_output(tp);
 1304                         }
 1305                         goto check_delack;
 1306                 }
 1307         }
 1308 
 1309         /*
 1310          * Calculate amount of space in receive window,
 1311          * and then do TCP input processing.
 1312          * Receive window is amount of space in rcv queue,
 1313          * but not less than advertised window.
 1314          */
 1315         { int win;
 1316 
 1317         win = sbspace(&so->so_rcv);
 1318         if (win < 0)
 1319                 win = 0;
 1320         tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
 1321         }
 1322 
 1323         switch (tp->t_state) {
 1324 
 1325         /*
 1326          * If the state is SYN_RECEIVED:
 1327          *      if seg contains an ACK, but not for our SYN/ACK, send a RST.
 1328          */
 1329         case TCPS_SYN_RECEIVED:
 1330                 if ((thflags & TH_ACK) &&
 1331                     (SEQ_LEQ(th->th_ack, tp->snd_una) ||
 1332                      SEQ_GT(th->th_ack, tp->snd_max))) {
 1333                                 rstreason = BANDLIM_RST_OPENPORT;
 1334                                 goto dropwithreset;
 1335                 }
 1336                 break;
 1337 
 1338         /*
 1339          * If the state is SYN_SENT:
 1340          *      if seg contains an ACK, but not for our SYN, drop the input.
 1341          *      if seg contains a RST, then drop the connection.
 1342          *      if seg does not contain SYN, then drop it.
 1343          * Otherwise this is an acceptable SYN segment
 1344          *      initialize tp->rcv_nxt and tp->irs
 1345          *      if seg contains ack then advance tp->snd_una
 1346          *      if SYN has been acked change to ESTABLISHED else SYN_RCVD state
 1347          *      arrange for segment to be acked (eventually)
 1348          *      continue processing rest of data/controls, beginning with URG
 1349          */
 1350         case TCPS_SYN_SENT:
 1351                 if ((thflags & TH_ACK) &&
 1352                     (SEQ_LEQ(th->th_ack, tp->iss) ||
 1353                      SEQ_GT(th->th_ack, tp->snd_max))) {
 1354                         rstreason = BANDLIM_UNLIMITED;
 1355                         goto dropwithreset;
 1356                 }
 1357                 if (thflags & TH_RST) {
 1358                         if (thflags & TH_ACK) {
 1359                                 KASSERT(headlocked, ("tcp_input: after_listen"
 1360                                     ": tcp_drop.2: head not locked"));
 1361                                 tp = tcp_drop(tp, ECONNREFUSED);
 1362                         }
 1363                         goto drop;
 1364                 }
 1365                 if ((thflags & TH_SYN) == 0)
 1366                         goto drop;
 1367                 tp->snd_wnd = th->th_win;       /* initial send window */
 1368 
 1369                 tp->irs = th->th_seq;
 1370                 tcp_rcvseqinit(tp);
 1371                 if (thflags & TH_ACK) {
 1372                         tcpstat.tcps_connects++;
 1373                         soisconnected(so);
 1374 #ifdef MAC
 1375                         SOCK_LOCK(so);
 1376                         mac_set_socket_peer_from_mbuf(m, so);
 1377                         SOCK_UNLOCK(so);
 1378 #endif
 1379                         /* Do window scaling on this connection? */
 1380                         if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
 1381                                 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
 1382                                 tp->snd_scale = tp->requested_s_scale;
 1383                                 tp->rcv_scale = tp->request_r_scale;
 1384                         }
 1385                         tp->rcv_adv += tp->rcv_wnd;
 1386                         tp->snd_una++;          /* SYN is acked */
 1387                         /*
 1388                          * If there's data, delay ACK; if there's also a FIN
 1389                          * ACKNOW will be turned on later.
 1390                          */
 1391                         if (DELAY_ACK(tp) && tlen != 0)
 1392                                 callout_reset(tp->tt_delack, tcp_delacktime,
 1393                                     tcp_timer_delack, tp);
 1394                         else
 1395                                 tp->t_flags |= TF_ACKNOW;
 1396                         /*
 1397                          * Received <SYN,ACK> in SYN_SENT[*] state.
 1398                          * Transitions:
 1399                          *      SYN_SENT  --> ESTABLISHED
 1400                          *      SYN_SENT* --> FIN_WAIT_1
 1401                          */
 1402                         tp->t_starttime = ticks;
 1403                         if (tp->t_flags & TF_NEEDFIN) {
 1404                                 tp->t_state = TCPS_FIN_WAIT_1;
 1405                                 tp->t_flags &= ~TF_NEEDFIN;
 1406                                 thflags &= ~TH_SYN;
 1407                         } else {
 1408                                 tp->t_state = TCPS_ESTABLISHED;
 1409                                 callout_reset(tp->tt_keep, tcp_keepidle,
 1410                                               tcp_timer_keep, tp);
 1411                         }
 1412                 } else {
 1413                         /*
 1414                          * Received initial SYN in SYN-SENT[*] state =>
 1415                          * simultaneous open.  If segment contains CC option
 1416                          * and there is a cached CC, apply TAO test.
 1417                          * If it succeeds, connection is * half-synchronized.
 1418                          * Otherwise, do 3-way handshake:
 1419                          *        SYN-SENT -> SYN-RECEIVED
 1420                          *        SYN-SENT* -> SYN-RECEIVED*
 1421                          * If there was no CC option, clear cached CC value.
 1422                          */
 1423                         tp->t_flags |= TF_ACKNOW;
 1424                         callout_stop(tp->tt_rexmt);
 1425                         tp->t_state = TCPS_SYN_RECEIVED;
 1426                 }
 1427 
 1428 trimthenstep6:
 1429                 KASSERT(headlocked, ("tcp_input: trimthenstep6: head not "
 1430                     "locked"));
 1431                 INP_LOCK_ASSERT(inp);
 1432 
 1433                 /*
 1434                  * Advance th->th_seq to correspond to first data byte.
 1435                  * If data, trim to stay within window,
 1436                  * dropping FIN if necessary.
 1437                  */
 1438                 th->th_seq++;
 1439                 if (tlen > tp->rcv_wnd) {
 1440                         todrop = tlen - tp->rcv_wnd;
 1441                         m_adj(m, -todrop);
 1442                         tlen = tp->rcv_wnd;
 1443                         thflags &= ~TH_FIN;
 1444                         tcpstat.tcps_rcvpackafterwin++;
 1445                         tcpstat.tcps_rcvbyteafterwin += todrop;
 1446                 }
 1447                 tp->snd_wl1 = th->th_seq - 1;
 1448                 tp->rcv_up = th->th_seq;
 1449                 /*
 1450                  * Client side of transaction: already sent SYN and data.
 1451                  * If the remote host used T/TCP to validate the SYN,
 1452                  * our data will be ACK'd; if so, enter normal data segment
 1453                  * processing in the middle of step 5, ack processing.
 1454                  * Otherwise, goto step 6.
 1455                  */
 1456                 if (thflags & TH_ACK)
 1457                         goto process_ACK;
 1458 
 1459                 goto step6;
 1460 
 1461         /*
 1462          * If the state is LAST_ACK or CLOSING or TIME_WAIT:
 1463          *      do normal processing.
 1464          *
 1465          * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
 1466          */
 1467         case TCPS_LAST_ACK:
 1468         case TCPS_CLOSING:
 1469         case TCPS_TIME_WAIT:
 1470                 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
 1471                 break;  /* continue normal processing */
 1472         }
 1473 
 1474         /*
 1475          * States other than LISTEN or SYN_SENT.
 1476          * First check the RST flag and sequence number since reset segments
 1477          * are exempt from the timestamp and connection count tests.  This
 1478          * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
 1479          * below which allowed reset segments in half the sequence space
 1480          * to fall though and be processed (which gives forged reset
 1481          * segments with a random sequence number a 50 percent chance of
 1482          * killing a connection).
 1483          * Then check timestamp, if present.
 1484          * Then check the connection count, if present.
 1485          * Then check that at least some bytes of segment are within
 1486          * receive window.  If segment begins before rcv_nxt,
 1487          * drop leading data (and SYN); if nothing left, just ack.
 1488          *
 1489          *
 1490          * If the RST bit is set, check the sequence number to see
 1491          * if this is a valid reset segment.
 1492          * RFC 793 page 37:
 1493          *   In all states except SYN-SENT, all reset (RST) segments
 1494          *   are validated by checking their SEQ-fields.  A reset is
 1495          *   valid if its sequence number is in the window.
 1496          * Note: this does not take into account delayed ACKs, so
 1497          *   we should test against last_ack_sent instead of rcv_nxt.
 1498          *   The sequence number in the reset segment is normally an
 1499          *   echo of our outgoing acknowlegement numbers, but some hosts
 1500          *   send a reset with the sequence number at the rightmost edge
 1501          *   of our receive window, and we have to handle this case.
 1502          * Note 2: Paul Watson's paper "Slipping in the Window" has shown
 1503          *   that brute force RST attacks are possible.  To combat this,
 1504          *   we use a much stricter check while in the ESTABLISHED state,
 1505          *   only accepting RSTs where the sequence number is equal to
 1506          *   last_ack_sent.  In all other states (the states in which a
 1507          *   RST is more likely), the more permissive check is used.
 1508          * If we have multiple segments in flight, the intial reset
 1509          * segment sequence numbers will be to the left of last_ack_sent,
 1510          * but they will eventually catch up.
 1511          * In any case, it never made sense to trim reset segments to
 1512          * fit the receive window since RFC 1122 says:
 1513          *   4.2.2.12  RST Segment: RFC-793 Section 3.4
 1514          *
 1515          *    A TCP SHOULD allow a received RST segment to include data.
 1516          *
 1517          *    DISCUSSION
 1518          *         It has been suggested that a RST segment could contain
 1519          *         ASCII text that encoded and explained the cause of the
 1520          *         RST.  No standard has yet been established for such
 1521          *         data.
 1522          *
 1523          * If the reset segment passes the sequence number test examine
 1524          * the state:
 1525          *    SYN_RECEIVED STATE:
 1526          *      If passive open, return to LISTEN state.
 1527          *      If active open, inform user that connection was refused.
 1528          *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
 1529          *      Inform user that connection was reset, and close tcb.
 1530          *    CLOSING, LAST_ACK STATES:
 1531          *      Close the tcb.
 1532          *    TIME_WAIT STATE:
 1533          *      Drop the segment - see Stevens, vol. 2, p. 964 and
 1534          *      RFC 1337.
 1535          */
 1536         if (thflags & TH_RST) {
 1537                 if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
 1538                     SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
 1539                     (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
 1540                         switch (tp->t_state) {
 1541 
 1542                         case TCPS_SYN_RECEIVED:
 1543                                 so->so_error = ECONNREFUSED;
 1544                                 goto close;
 1545 
 1546                         case TCPS_ESTABLISHED:
 1547                                 if (tp->last_ack_sent != th->th_seq &&
 1548                                     tcp_insecure_rst == 0) {
 1549                                         tcpstat.tcps_badrst++;
 1550                                         goto drop;
 1551                                 }
 1552                         case TCPS_FIN_WAIT_1:
 1553                         case TCPS_FIN_WAIT_2:
 1554                         case TCPS_CLOSE_WAIT:
 1555                                 so->so_error = ECONNRESET;
 1556                         close:
 1557                                 tp->t_state = TCPS_CLOSED;
 1558                                 tcpstat.tcps_drops++;
 1559                                 KASSERT(headlocked, ("tcp_input: "
 1560                                     "trimthenstep6: tcp_close: head not "
 1561                                     "locked"));
 1562                                 tp = tcp_close(tp);
 1563                                 break;
 1564 
 1565                         case TCPS_CLOSING:
 1566                         case TCPS_LAST_ACK:
 1567                                 KASSERT(headlocked, ("trimthenstep6: "
 1568                                     "tcp_close.2: head not locked"));
 1569                                 tp = tcp_close(tp);
 1570                                 break;
 1571 
 1572                         case TCPS_TIME_WAIT:
 1573                                 KASSERT(tp->t_state != TCPS_TIME_WAIT,
 1574                                     ("timewait"));
 1575                                 break;
 1576                         }
 1577                 }
 1578                 goto drop;
 1579         }
 1580 
 1581         /*
 1582          * RFC 1323 PAWS: If we have a timestamp reply on this segment
 1583          * and it's less than ts_recent, drop it.
 1584          */
 1585         if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
 1586             TSTMP_LT(to.to_tsval, tp->ts_recent)) {
 1587 
 1588                 /* Check to see if ts_recent is over 24 days old.  */
 1589                 if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
 1590                         /*
 1591                          * Invalidate ts_recent.  If this segment updates
 1592                          * ts_recent, the age will be reset later and ts_recent
 1593                          * will get a valid value.  If it does not, setting
 1594                          * ts_recent to zero will at least satisfy the
 1595                          * requirement that zero be placed in the timestamp
 1596                          * echo reply when ts_recent isn't valid.  The
 1597                          * age isn't reset until we get a valid ts_recent
 1598                          * because we don't want out-of-order segments to be
 1599                          * dropped when ts_recent is old.
 1600                          */
 1601                         tp->ts_recent = 0;
 1602                 } else {
 1603                         tcpstat.tcps_rcvduppack++;
 1604                         tcpstat.tcps_rcvdupbyte += tlen;
 1605                         tcpstat.tcps_pawsdrop++;
 1606                         if (tlen)
 1607                                 goto dropafterack;
 1608                         goto drop;
 1609                 }
 1610         }
 1611 
 1612         /*
 1613          * In the SYN-RECEIVED state, validate that the packet belongs to
 1614          * this connection before trimming the data to fit the receive
 1615          * window.  Check the sequence number versus IRS since we know
 1616          * the sequence numbers haven't wrapped.  This is a partial fix
 1617          * for the "LAND" DoS attack.
 1618          */
 1619         if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
 1620                 rstreason = BANDLIM_RST_OPENPORT;
 1621                 goto dropwithreset;
 1622         }
 1623 
 1624         todrop = tp->rcv_nxt - th->th_seq;
 1625         if (todrop > 0) {
 1626                 if (thflags & TH_SYN) {
 1627                         thflags &= ~TH_SYN;
 1628                         th->th_seq++;
 1629                         if (th->th_urp > 1)
 1630                                 th->th_urp--;
 1631                         else
 1632                                 thflags &= ~TH_URG;
 1633                         todrop--;
 1634                 }
 1635                 /*
 1636                  * Following if statement from Stevens, vol. 2, p. 960.
 1637                  */
 1638                 if (todrop > tlen
 1639                     || (todrop == tlen && (thflags & TH_FIN) == 0)) {
 1640                         /*
 1641                          * Any valid FIN must be to the left of the window.
 1642                          * At this point the FIN must be a duplicate or out
 1643                          * of sequence; drop it.
 1644                          */
 1645                         thflags &= ~TH_FIN;
 1646 
 1647                         /*
 1648                          * Send an ACK to resynchronize and drop any data.
 1649                          * But keep on processing for RST or ACK.
 1650                          */
 1651                         tp->t_flags |= TF_ACKNOW;
 1652                         todrop = tlen;
 1653                         tcpstat.tcps_rcvduppack++;
 1654                         tcpstat.tcps_rcvdupbyte += todrop;
 1655                 } else {
 1656                         tcpstat.tcps_rcvpartduppack++;
 1657                         tcpstat.tcps_rcvpartdupbyte += todrop;
 1658                 }
 1659                 drop_hdrlen += todrop;  /* drop from the top afterwards */
 1660                 th->th_seq += todrop;
 1661                 tlen -= todrop;
 1662                 if (th->th_urp > todrop)
 1663                         th->th_urp -= todrop;
 1664                 else {
 1665                         thflags &= ~TH_URG;
 1666                         th->th_urp = 0;
 1667                 }
 1668         }
 1669 
 1670         /*
 1671          * If new data are received on a connection after the
 1672          * user processes are gone, then RST the other end.
 1673          */
 1674         if ((so->so_state & SS_NOFDREF) &&
 1675             tp->t_state > TCPS_CLOSE_WAIT && tlen) {
 1676                 KASSERT(headlocked, ("trimthenstep6: tcp_close.3: head not "
 1677                     "locked"));
 1678                 tp = tcp_close(tp);
 1679                 tcpstat.tcps_rcvafterclose++;
 1680                 rstreason = BANDLIM_UNLIMITED;
 1681                 goto dropwithreset;
 1682         }
 1683 
 1684         /*
 1685          * If segment ends after window, drop trailing data
 1686          * (and PUSH and FIN); if nothing left, just ACK.
 1687          */
 1688         todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
 1689         if (todrop > 0) {
 1690                 tcpstat.tcps_rcvpackafterwin++;
 1691                 if (todrop >= tlen) {
 1692                         tcpstat.tcps_rcvbyteafterwin += tlen;
 1693                         /*
 1694                          * If a new connection request is received
 1695                          * while in TIME_WAIT, drop the old connection
 1696                          * and start over if the sequence numbers
 1697                          * are above the previous ones.
 1698                          */
 1699                         KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
 1700                         if (thflags & TH_SYN &&
 1701                             tp->t_state == TCPS_TIME_WAIT &&
 1702                             SEQ_GT(th->th_seq, tp->rcv_nxt)) {
 1703                                 KASSERT(headlocked, ("trimthenstep6: "
 1704                                     "tcp_close.4: head not locked"));
 1705                                 tp = tcp_close(tp);
 1706                                 goto findpcb;
 1707                         }
 1708                         /*
 1709                          * If window is closed can only take segments at
 1710                          * window edge, and have to drop data and PUSH from
 1711                          * incoming segments.  Continue processing, but
 1712                          * remember to ack.  Otherwise, drop segment
 1713                          * and ack.
 1714                          */
 1715                         if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
 1716                                 tp->t_flags |= TF_ACKNOW;
 1717                                 tcpstat.tcps_rcvwinprobe++;
 1718                         } else
 1719                                 goto dropafterack;
 1720                 } else
 1721                         tcpstat.tcps_rcvbyteafterwin += todrop;
 1722                 m_adj(m, -todrop);
 1723                 tlen -= todrop;
 1724                 thflags &= ~(TH_PUSH|TH_FIN);
 1725         }
 1726 
 1727         /*
 1728          * If last ACK falls within this segment's sequence numbers,
 1729          * record its timestamp.
 1730          * NOTE: 
 1731          * 1) That the test incorporates suggestions from the latest
 1732          *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
 1733          * 2) That updating only on newer timestamps interferes with
 1734          *    our earlier PAWS tests, so this check should be solely
 1735          *    predicated on the sequence space of this segment.
 1736          * 3) That we modify the segment boundary check to be 
 1737          *        Last.ACK.Sent <= SEG.SEQ + SEG.Len  
 1738          *    instead of RFC1323's
 1739          *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
 1740          *    This modified check allows us to overcome RFC1323's
 1741          *    limitations as described in Stevens TCP/IP Illustrated
 1742          *    Vol. 2 p.869. In such cases, we can still calculate the
 1743          *    RTT correctly when RCV.NXT == Last.ACK.Sent.
 1744          */
 1745         if ((to.to_flags & TOF_TS) != 0 &&
 1746             SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
 1747             SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
 1748                 ((thflags & (TH_SYN|TH_FIN)) != 0))) {
 1749                 tp->ts_recent_age = ticks;
 1750                 tp->ts_recent = to.to_tsval;
 1751         }
 1752 
 1753         /*
 1754          * If a SYN is in the window, then this is an
 1755          * error and we send an RST and drop the connection.
 1756          */
 1757         if (thflags & TH_SYN) {
 1758                 KASSERT(headlocked, ("tcp_input: tcp_drop: trimthenstep6: "
 1759                     "head not locked"));
 1760                 tp = tcp_drop(tp, ECONNRESET);
 1761                 rstreason = BANDLIM_UNLIMITED;
 1762                 goto drop;
 1763         }
 1764 
 1765         /*
 1766          * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
 1767          * flag is on (half-synchronized state), then queue data for
 1768          * later processing; else drop segment and return.
 1769          */
 1770         if ((thflags & TH_ACK) == 0) {
 1771                 if (tp->t_state == TCPS_SYN_RECEIVED ||
 1772                     (tp->t_flags & TF_NEEDSYN))
 1773                         goto step6;
 1774                 else
 1775                         goto drop;
 1776         }
 1777 
 1778         /*
 1779          * Ack processing.
 1780          */
 1781         switch (tp->t_state) {
 1782 
 1783         /*
 1784          * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
 1785          * ESTABLISHED state and continue processing.
 1786          * The ACK was checked above.
 1787          */
 1788         case TCPS_SYN_RECEIVED:
 1789 
 1790                 tcpstat.tcps_connects++;
 1791                 soisconnected(so);
 1792                 /* Do window scaling? */
 1793                 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
 1794                         (TF_RCVD_SCALE|TF_REQ_SCALE)) {
 1795                         tp->snd_scale = tp->requested_s_scale;
 1796                         tp->rcv_scale = tp->request_r_scale;
 1797                 }
 1798                 /*
 1799                  * Make transitions:
 1800                  *      SYN-RECEIVED  -> ESTABLISHED
 1801                  *      SYN-RECEIVED* -> FIN-WAIT-1
 1802                  */
 1803                 tp->t_starttime = ticks;
 1804                 if (tp->t_flags & TF_NEEDFIN) {
 1805                         tp->t_state = TCPS_FIN_WAIT_1;
 1806                         tp->t_flags &= ~TF_NEEDFIN;
 1807                 } else {
 1808                         tp->t_state = TCPS_ESTABLISHED;
 1809                         callout_reset(tp->tt_keep, tcp_keepidle,
 1810                                       tcp_timer_keep, tp);
 1811                 }
 1812                 /*
 1813                  * If segment contains data or ACK, will call tcp_reass()
 1814                  * later; if not, do so now to pass queued data to user.
 1815                  */
 1816                 if (tlen == 0 && (thflags & TH_FIN) == 0)
 1817                         (void) tcp_reass(tp, (struct tcphdr *)0, 0,
 1818                             (struct mbuf *)0);
 1819                 tp->snd_wl1 = th->th_seq - 1;
 1820                 /* FALLTHROUGH */
 1821 
 1822         /*
 1823          * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
 1824          * ACKs.  If the ack is in the range
 1825          *      tp->snd_una < th->th_ack <= tp->snd_max
 1826          * then advance tp->snd_una to th->th_ack and drop
 1827          * data from the retransmission queue.  If this ACK reflects
 1828          * more up to date window information we update our window information.
 1829          */
 1830         case TCPS_ESTABLISHED:
 1831         case TCPS_FIN_WAIT_1:
 1832         case TCPS_FIN_WAIT_2:
 1833         case TCPS_CLOSE_WAIT:
 1834         case TCPS_CLOSING:
 1835         case TCPS_LAST_ACK:
 1836         case TCPS_TIME_WAIT:
 1837                 KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
 1838                 if (SEQ_GT(th->th_ack, tp->snd_max)) {
 1839                         tcpstat.tcps_rcvacktoomuch++;
 1840                         goto dropafterack;
 1841                 }
 1842                 if (tp->sack_enable &&
 1843                     (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes)))
 1844                         tcp_sack_doack(tp, &to, th->th_ack);
 1845                 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
 1846                         if (tlen == 0 && tiwin == tp->snd_wnd) {
 1847                                 tcpstat.tcps_rcvdupack++;
 1848                                 /*
 1849                                  * If we have outstanding data (other than
 1850                                  * a window probe), this is a completely
 1851                                  * duplicate ack (ie, window info didn't
 1852                                  * change), the ack is the biggest we've
 1853                                  * seen and we've seen exactly our rexmt
 1854                                  * threshhold of them, assume a packet
 1855                                  * has been dropped and retransmit it.
 1856                                  * Kludge snd_nxt & the congestion
 1857                                  * window so we send only this one
 1858                                  * packet.
 1859                                  *
 1860                                  * We know we're losing at the current
 1861                                  * window size so do congestion avoidance
 1862                                  * (set ssthresh to half the current window
 1863                                  * and pull our congestion window back to
 1864                                  * the new ssthresh).
 1865                                  *
 1866                                  * Dup acks mean that packets have left the
 1867                                  * network (they're now cached at the receiver)
 1868                                  * so bump cwnd by the amount in the receiver
 1869                                  * to keep a constant cwnd packets in the
 1870                                  * network.
 1871                                  */
 1872                                 if (!callout_active(tp->tt_rexmt) ||
 1873                                     th->th_ack != tp->snd_una)
 1874                                         tp->t_dupacks = 0;
 1875                                 else if (++tp->t_dupacks > tcprexmtthresh ||
 1876                                          ((tcp_do_newreno || tp->sack_enable) &&
 1877                                           IN_FASTRECOVERY(tp))) {
 1878                                         if (tp->sack_enable && IN_FASTRECOVERY(tp)) {
 1879                                                 int awnd;
 1880                                                 
 1881                                                 /*
 1882                                                  * Compute the amount of data in flight first.
 1883                                                  * We can inject new data into the pipe iff 
 1884                                                  * we have less than 1/2 the original window's  
 1885                                                  * worth of data in flight.
 1886                                                  */
 1887                                                 awnd = (tp->snd_nxt - tp->snd_fack) +
 1888                                                         tp->sackhint.sack_bytes_rexmit;
 1889                                                 if (awnd < tp->snd_ssthresh) {
 1890                                                         tp->snd_cwnd += tp->t_maxseg;
 1891                                                         if (tp->snd_cwnd > tp->snd_ssthresh)
 1892                                                                 tp->snd_cwnd = tp->snd_ssthresh;
 1893                                                 }
 1894                                         } else
 1895                                                 tp->snd_cwnd += tp->t_maxseg;
 1896                                         (void) tcp_output(tp);
 1897                                         goto drop;
 1898                                 } else if (tp->t_dupacks == tcprexmtthresh) {
 1899                                         tcp_seq onxt = tp->snd_nxt;
 1900                                         u_int win;
 1901 
 1902                                         /*
 1903                                          * If we're doing sack, check to
 1904                                          * see if we're already in sack
 1905                                          * recovery. If we're not doing sack,
 1906                                          * check to see if we're in newreno
 1907                                          * recovery.
 1908                                          */
 1909                                         if (tp->sack_enable) {
 1910                                                 if (IN_FASTRECOVERY(tp)) {
 1911                                                         tp->t_dupacks = 0;
 1912                                                         break;
 1913                                                 }
 1914                                         } else if (tcp_do_newreno) {
 1915                                                 if (SEQ_LEQ(th->th_ack,
 1916                                                     tp->snd_recover)) {
 1917                                                         tp->t_dupacks = 0;
 1918                                                         break;
 1919                                                 }
 1920                                         }
 1921                                         win = min(tp->snd_wnd, tp->snd_cwnd) /
 1922                                             2 / tp->t_maxseg;
 1923                                         if (win < 2)
 1924                                                 win = 2;
 1925                                         tp->snd_ssthresh = win * tp->t_maxseg;
 1926                                         ENTER_FASTRECOVERY(tp);
 1927                                         tp->snd_recover = tp->snd_max;
 1928                                         callout_stop(tp->tt_rexmt);
 1929                                         tp->t_rtttime = 0;
 1930                                         if (tp->sack_enable) {
 1931                                                 tcpstat.tcps_sack_recovery_episode++;
 1932                                                 tp->sack_newdata = tp->snd_nxt;
 1933                                                 tp->snd_cwnd = tp->t_maxseg;
 1934                                                 (void) tcp_output(tp);
 1935                                                 goto drop;
 1936                                         }
 1937                                         tp->snd_nxt = th->th_ack;
 1938                                         tp->snd_cwnd = tp->t_maxseg;
 1939                                         (void) tcp_output(tp);
 1940                                         KASSERT(tp->snd_limited <= 2,
 1941                                             ("tp->snd_limited too big"));
 1942                                         tp->snd_cwnd = tp->snd_ssthresh +
 1943                                              tp->t_maxseg *
 1944                                              (tp->t_dupacks - tp->snd_limited);
 1945                                         if (SEQ_GT(onxt, tp->snd_nxt))
 1946                                                 tp->snd_nxt = onxt;
 1947                                         goto drop;
 1948                                 } else if (tcp_do_rfc3042) {
 1949                                         u_long oldcwnd = tp->snd_cwnd;
 1950                                         tcp_seq oldsndmax = tp->snd_max;
 1951                                         u_int sent;
 1952 
 1953                                         KASSERT(tp->t_dupacks == 1 ||
 1954                                             tp->t_dupacks == 2,
 1955                                             ("dupacks not 1 or 2"));
 1956                                         if (tp->t_dupacks == 1)
 1957                                                 tp->snd_limited = 0;
 1958                                         tp->snd_cwnd =
 1959                                             (tp->snd_nxt - tp->snd_una) +
 1960                                             (tp->t_dupacks - tp->snd_limited) *
 1961                                             tp->t_maxseg;
 1962                                         (void) tcp_output(tp);
 1963                                         sent = tp->snd_max - oldsndmax;
 1964                                         if (sent > tp->t_maxseg) {
 1965                                                 KASSERT((tp->t_dupacks == 2 &&
 1966                                                     tp->snd_limited == 0) ||
 1967                                                    (sent == tp->t_maxseg + 1 &&
 1968                                                     tp->t_flags & TF_SENTFIN),
 1969                                                     ("sent too much"));
 1970                                                 tp->snd_limited = 2;
 1971                                         } else if (sent > 0)
 1972                                                 ++tp->snd_limited;
 1973                                         tp->snd_cwnd = oldcwnd;
 1974                                         goto drop;
 1975                                 }
 1976                         } else
 1977                                 tp->t_dupacks = 0;
 1978                         break;
 1979                 }
 1980 
 1981                 KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
 1982 
 1983                 /*
 1984                  * If the congestion window was inflated to account
 1985                  * for the other side's cached packets, retract it.
 1986                  */
 1987                 if (tcp_do_newreno || tp->sack_enable) {
 1988                         if (IN_FASTRECOVERY(tp)) {
 1989                                 if (SEQ_LT(th->th_ack, tp->snd_recover)) {
 1990                                         if (tp->sack_enable)
 1991                                                 tcp_sack_partialack(tp, th);
 1992                                         else
 1993                                                 tcp_newreno_partial_ack(tp, th);
 1994                                 } else {
 1995                                         /*
 1996                                          * Out of fast recovery.
 1997                                          * Window inflation should have left us
 1998                                          * with approximately snd_ssthresh
 1999                                          * outstanding data.
 2000                                          * But in case we would be inclined to
 2001                                          * send a burst, better to do it via
 2002                                          * the slow start mechanism.
 2003                                          */
 2004                                         if (SEQ_GT(th->th_ack +
 2005                                                         tp->snd_ssthresh,
 2006                                                    tp->snd_max))
 2007                                                 tp->snd_cwnd = tp->snd_max -
 2008                                                                 th->th_ack +
 2009                                                                 tp->t_maxseg;
 2010                                         else
 2011                                                 tp->snd_cwnd = tp->snd_ssthresh;
 2012                                 }
 2013                         }
 2014                 } else {
 2015                         if (tp->t_dupacks >= tcprexmtthresh &&
 2016                             tp->snd_cwnd > tp->snd_ssthresh)
 2017                                 tp->snd_cwnd = tp->snd_ssthresh;
 2018                 }
 2019                 tp->t_dupacks = 0;
 2020                 /*
 2021                  * If we reach this point, ACK is not a duplicate,
 2022                  *     i.e., it ACKs something we sent.
 2023                  */
 2024                 if (tp->t_flags & TF_NEEDSYN) {
 2025                         /*
 2026                          * T/TCP: Connection was half-synchronized, and our
 2027                          * SYN has been ACK'd (so connection is now fully
 2028                          * synchronized).  Go to non-starred state,
 2029                          * increment snd_una for ACK of SYN, and check if
 2030                          * we can do window scaling.
 2031                          */
 2032                         tp->t_flags &= ~TF_NEEDSYN;
 2033                         tp->snd_una++;
 2034                         /* Do window scaling? */
 2035                         if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
 2036                                 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
 2037                                 tp->snd_scale = tp->requested_s_scale;
 2038                                 tp->rcv_scale = tp->request_r_scale;
 2039                         }
 2040                 }
 2041 
 2042 process_ACK:
 2043                 KASSERT(headlocked, ("tcp_input: process_ACK: head not "
 2044                     "locked"));
 2045                 INP_LOCK_ASSERT(inp);
 2046 
 2047                 acked = th->th_ack - tp->snd_una;
 2048                 tcpstat.tcps_rcvackpack++;
 2049                 tcpstat.tcps_rcvackbyte += acked;
 2050 
 2051                 /*
 2052                  * If we just performed our first retransmit, and the ACK
 2053                  * arrives within our recovery window, then it was a mistake
 2054                  * to do the retransmit in the first place.  Recover our
 2055                  * original cwnd and ssthresh, and proceed to transmit where
 2056                  * we left off.
 2057                  */
 2058                 if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
 2059                         ++tcpstat.tcps_sndrexmitbad;
 2060                         tp->snd_cwnd = tp->snd_cwnd_prev;
 2061                         tp->snd_ssthresh = tp->snd_ssthresh_prev;
 2062                         tp->snd_recover = tp->snd_recover_prev;
 2063                         if (tp->t_flags & TF_WASFRECOVERY)
 2064                                 ENTER_FASTRECOVERY(tp);
 2065                         tp->snd_nxt = tp->snd_max;
 2066                         tp->t_badrxtwin = 0;    /* XXX probably not required */
 2067                 }
 2068 
 2069                 /*
 2070                  * If we have a timestamp reply, update smoothed
 2071                  * round trip time.  If no timestamp is present but
 2072                  * transmit timer is running and timed sequence
 2073                  * number was acked, update smoothed round trip time.
 2074                  * Since we now have an rtt measurement, cancel the
 2075                  * timer backoff (cf., Phil Karn's retransmit alg.).
 2076                  * Recompute the initial retransmit timer.
 2077                  *
 2078                  * Some boxes send broken timestamp replies
 2079                  * during the SYN+ACK phase, ignore
 2080                  * timestamps of 0 or we could calculate a
 2081                  * huge RTT and blow up the retransmit timer.
 2082                  */
 2083                 if ((to.to_flags & TOF_TS) != 0 &&
 2084                     to.to_tsecr) {
 2085                         if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr)
 2086                                 tp->t_rttlow = ticks - to.to_tsecr;
 2087                         tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
 2088                 } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
 2089                         if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
 2090                                 tp->t_rttlow = ticks - tp->t_rtttime;
 2091                         tcp_xmit_timer(tp, ticks - tp->t_rtttime);
 2092                 }
 2093                 tcp_xmit_bandwidth_limit(tp, th->th_ack);
 2094 
 2095                 /*
 2096                  * If all outstanding data is acked, stop retransmit
 2097                  * timer and remember to restart (more output or persist).
 2098                  * If there is more data to be acked, restart retransmit
 2099                  * timer, using current (possibly backed-off) value.
 2100                  */
 2101                 if (th->th_ack == tp->snd_max) {
 2102                         callout_stop(tp->tt_rexmt);
 2103                         needoutput = 1;
 2104                 } else if (!callout_active(tp->tt_persist))
 2105                         callout_reset(tp->tt_rexmt, tp->t_rxtcur,
 2106                                       tcp_timer_rexmt, tp);
 2107 
 2108                 /*
 2109                  * If no data (only SYN) was ACK'd,
 2110                  *    skip rest of ACK processing.
 2111                  */
 2112                 if (acked == 0)
 2113                         goto step6;
 2114 
 2115                 /*
 2116                  * When new data is acked, open the congestion window.
 2117                  * If the window gives us less than ssthresh packets
 2118                  * in flight, open exponentially (maxseg per packet).
 2119                  * Otherwise open linearly: maxseg per window
 2120                  * (maxseg^2 / cwnd per packet).
 2121                  */
 2122                 if ((!tcp_do_newreno && !tp->sack_enable) ||
 2123                     !IN_FASTRECOVERY(tp)) {
 2124                         register u_int cw = tp->snd_cwnd;
 2125                         register u_int incr = tp->t_maxseg;
 2126                         if (cw > tp->snd_ssthresh)
 2127                                 incr = incr * incr / cw;
 2128                         tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
 2129                 }
 2130                 SOCKBUF_LOCK(&so->so_snd);
 2131                 if (acked > so->so_snd.sb_cc) {
 2132                         tp->snd_wnd -= so->so_snd.sb_cc;
 2133                         sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
 2134                         ourfinisacked = 1;
 2135                 } else {
 2136                         sbdrop_locked(&so->so_snd, acked);
 2137                         tp->snd_wnd -= acked;
 2138                         ourfinisacked = 0;
 2139                 }
 2140                 sowwakeup_locked(so);
 2141                 /* detect una wraparound */
 2142                 if ((tcp_do_newreno || tp->sack_enable) &&
 2143                     !IN_FASTRECOVERY(tp) &&
 2144                     SEQ_GT(tp->snd_una, tp->snd_recover) &&
 2145                     SEQ_LEQ(th->th_ack, tp->snd_recover))
 2146                         tp->snd_recover = th->th_ack - 1;
 2147                 if ((tcp_do_newreno || tp->sack_enable) &&
 2148                     IN_FASTRECOVERY(tp) &&
 2149                     SEQ_GEQ(th->th_ack, tp->snd_recover))
 2150                         EXIT_FASTRECOVERY(tp);
 2151                 tp->snd_una = th->th_ack;
 2152                 if (tp->sack_enable) {
 2153                         if (SEQ_GT(tp->snd_una, tp->snd_recover))
 2154                                 tp->snd_recover = tp->snd_una;
 2155                 }
 2156                 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
 2157                         tp->snd_nxt = tp->snd_una;
 2158 
 2159                 switch (tp->t_state) {
 2160 
 2161                 /*
 2162                  * In FIN_WAIT_1 STATE in addition to the processing
 2163                  * for the ESTABLISHED state if our FIN is now acknowledged
 2164                  * then enter FIN_WAIT_2.
 2165                  */
 2166                 case TCPS_FIN_WAIT_1:
 2167                         if (ourfinisacked) {
 2168                                 /*
 2169                                  * If we can't receive any more
 2170                                  * data, then closing user can proceed.
 2171                                  * Starting the timer is contrary to the
 2172                                  * specification, but if we don't get a FIN
 2173                                  * we'll hang forever.
 2174                                  */
 2175                 /* XXXjl
 2176                  * we should release the tp also, and use a
 2177                  * compressed state.
 2178                  */
 2179                                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
 2180                                         soisdisconnected(so);
 2181                                         callout_reset(tp->tt_2msl, tcp_maxidle,
 2182                                                       tcp_timer_2msl, tp);
 2183                                 }
 2184                                 tp->t_state = TCPS_FIN_WAIT_2;
 2185                         }
 2186                         break;
 2187 
 2188                 /*
 2189                  * In CLOSING STATE in addition to the processing for
 2190                  * the ESTABLISHED state if the ACK acknowledges our FIN
 2191                  * then enter the TIME-WAIT state, otherwise ignore
 2192                  * the segment.
 2193                  */
 2194                 case TCPS_CLOSING:
 2195                         if (ourfinisacked) {
 2196                                 KASSERT(headlocked, ("tcp_input: process_ACK: "
 2197                                     "head not locked"));
 2198                                 tcp_twstart(tp);
 2199                                 INP_INFO_WUNLOCK(&tcbinfo);
 2200                                 m_freem(m);
 2201                                 return;
 2202                         }
 2203                         break;
 2204 
 2205                 /*
 2206                  * In LAST_ACK, we may still be waiting for data to drain
 2207                  * and/or to be acked, as well as for the ack of our FIN.
 2208                  * If our FIN is now acknowledged, delete the TCB,
 2209                  * enter the closed state and return.
 2210                  */
 2211                 case TCPS_LAST_ACK:
 2212                         if (ourfinisacked) {
 2213                                 KASSERT(headlocked, ("tcp_input: process_ACK:"
 2214                                     " tcp_close: head not locked"));
 2215                                 tp = tcp_close(tp);
 2216                                 goto drop;
 2217                         }
 2218                         break;
 2219 
 2220                 /*
 2221                  * In TIME_WAIT state the only thing that should arrive
 2222                  * is a retransmission of the remote FIN.  Acknowledge
 2223                  * it and restart the finack timer.
 2224                  */
 2225                 case TCPS_TIME_WAIT:
 2226                         KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
 2227                         callout_reset(tp->tt_2msl, 2 * tcp_msl,
 2228                                       tcp_timer_2msl, tp);
 2229                         goto dropafterack;
 2230                 }
 2231         }
 2232 
 2233 step6:
 2234         KASSERT(headlocked, ("tcp_input: step6: head not locked"));
 2235         INP_LOCK_ASSERT(inp);
 2236 
 2237         /*
 2238          * Update window information.
 2239          * Don't look at window if no ACK: TAC's send garbage on first SYN.
 2240          */
 2241         if ((thflags & TH_ACK) &&
 2242             (SEQ_LT(tp->snd_wl1, th->th_seq) ||
 2243             (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
 2244              (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
 2245                 /* keep track of pure window updates */
 2246                 if (tlen == 0 &&
 2247                     tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
 2248                         tcpstat.tcps_rcvwinupd++;
 2249                 tp->snd_wnd = tiwin;
 2250                 tp->snd_wl1 = th->th_seq;
 2251                 tp->snd_wl2 = th->th_ack;
 2252                 if (tp->snd_wnd > tp->max_sndwnd)
 2253                         tp->max_sndwnd = tp->snd_wnd;
 2254                 needoutput = 1;
 2255         }
 2256 
 2257         /*
 2258          * Process segments with URG.
 2259          */
 2260         if ((thflags & TH_URG) && th->th_urp &&
 2261             TCPS_HAVERCVDFIN(tp->t_state) == 0) {
 2262                 /*
 2263                  * This is a kludge, but if we receive and accept
 2264                  * random urgent pointers, we'll crash in
 2265                  * soreceive.  It's hard to imagine someone
 2266                  * actually wanting to send this much urgent data.
 2267                  */
 2268                 SOCKBUF_LOCK(&so->so_rcv);
 2269                 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
 2270                         th->th_urp = 0;                 /* XXX */
 2271                         thflags &= ~TH_URG;             /* XXX */
 2272                         SOCKBUF_UNLOCK(&so->so_rcv);    /* XXX */
 2273                         goto dodata;                    /* XXX */
 2274                 }
 2275                 /*
 2276                  * If this segment advances the known urgent pointer,
 2277                  * then mark the data stream.  This should not happen
 2278                  * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
 2279                  * a FIN has been received from the remote side.
 2280                  * In these states we ignore the URG.
 2281                  *
 2282                  * According to RFC961 (Assigned Protocols),
 2283                  * the urgent pointer points to the last octet
 2284                  * of urgent data.  We continue, however,
 2285                  * to consider it to indicate the first octet
 2286                  * of data past the urgent section as the original
 2287                  * spec states (in one of two places).
 2288                  */
 2289                 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
 2290                         tp->rcv_up = th->th_seq + th->th_urp;
 2291                         so->so_oobmark = so->so_rcv.sb_cc +
 2292                             (tp->rcv_up - tp->rcv_nxt) - 1;
 2293                         if (so->so_oobmark == 0)
 2294                                 so->so_rcv.sb_state |= SBS_RCVATMARK;
 2295                         sohasoutofband(so);
 2296                         tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
 2297                 }
 2298                 SOCKBUF_UNLOCK(&so->so_rcv);
 2299                 /*
 2300                  * Remove out of band data so doesn't get presented to user.
 2301                  * This can happen independent of advancing the URG pointer,
 2302                  * but if two URG's are pending at once, some out-of-band
 2303                  * data may creep in... ick.
 2304                  */
 2305                 if (th->th_urp <= (u_long)tlen &&
 2306                     !(so->so_options & SO_OOBINLINE)) {
 2307                         /* hdr drop is delayed */
 2308                         tcp_pulloutofband(so, th, m, drop_hdrlen);
 2309                 }
 2310         } else {
 2311                 /*
 2312                  * If no out of band data is expected,
 2313                  * pull receive urgent pointer along
 2314                  * with the receive window.
 2315                  */
 2316                 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
 2317                         tp->rcv_up = tp->rcv_nxt;
 2318         }
 2319 dodata:                                                 /* XXX */
 2320         KASSERT(headlocked, ("tcp_input: dodata: head not locked"));
 2321         INP_LOCK_ASSERT(inp);
 2322 
 2323         /*
 2324          * Process the segment text, merging it into the TCP sequencing queue,
 2325          * and arranging for acknowledgment of receipt if necessary.
 2326          * This process logically involves adjusting tp->rcv_wnd as data
 2327          * is presented to the user (this happens in tcp_usrreq.c,
 2328          * case PRU_RCVD).  If a FIN has already been received on this
 2329          * connection then we just ignore the text.
 2330          */
 2331         if ((tlen || (thflags & TH_FIN)) &&
 2332             TCPS_HAVERCVDFIN(tp->t_state) == 0) {
 2333                 tcp_seq save_start = th->th_seq;
 2334                 tcp_seq save_end = th->th_seq + tlen;
 2335                 m_adj(m, drop_hdrlen);  /* delayed header drop */
 2336                 /*
 2337                  * Insert segment which includes th into TCP reassembly queue
 2338                  * with control block tp.  Set thflags to whether reassembly now
 2339                  * includes a segment with FIN.  This handles the common case
 2340                  * inline (segment is the next to be received on an established
 2341                  * connection, and the queue is empty), avoiding linkage into
 2342                  * and removal from the queue and repetition of various
 2343                  * conversions.
 2344                  * Set DELACK for segments received in order, but ack
 2345                  * immediately when segments are out of order (so
 2346                  * fast retransmit can work).
 2347                  */
 2348                 if (th->th_seq == tp->rcv_nxt &&
 2349                     LIST_EMPTY(&tp->t_segq) &&
 2350                     TCPS_HAVEESTABLISHED(tp->t_state)) {
 2351                         if (DELAY_ACK(tp))
 2352                                 tp->t_flags |= TF_DELACK;
 2353                         else
 2354                                 tp->t_flags |= TF_ACKNOW;
 2355                         tp->rcv_nxt += tlen;
 2356                         thflags = th->th_flags & TH_FIN;
 2357                         tcpstat.tcps_rcvpack++;
 2358                         tcpstat.tcps_rcvbyte += tlen;
 2359                         ND6_HINT(tp);
 2360                         SOCKBUF_LOCK(&so->so_rcv);
 2361                         if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
 2362                                 m_freem(m);
 2363                         else
 2364                                 sbappendstream_locked(&so->so_rcv, m);
 2365                         sorwakeup_locked(so);
 2366                 } else {
 2367                         thflags = tcp_reass(tp, th, &tlen, m);
 2368                         tp->t_flags |= TF_ACKNOW;
 2369                 }
 2370                 if (tlen > 0 && tp->sack_enable)
 2371                         tcp_update_sack_list(tp, save_start, save_end);
 2372                 /*
 2373                  * Note the amount of data that peer has sent into
 2374                  * our window, in order to estimate the sender's
 2375                  * buffer size.
 2376                  */
 2377                 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
 2378         } else {
 2379                 m_freem(m);
 2380                 thflags &= ~TH_FIN;
 2381         }
 2382 
 2383         /*
 2384          * If FIN is received ACK the FIN and let the user know
 2385          * that the connection is closing.
 2386          */
 2387         if (thflags & TH_FIN) {
 2388                 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
 2389                         socantrcvmore(so);
 2390                         /*
 2391                          * If connection is half-synchronized
 2392                          * (ie NEEDSYN flag on) then delay ACK,
 2393                          * so it may be piggybacked when SYN is sent.
 2394                          * Otherwise, since we received a FIN then no
 2395                          * more input can be expected, send ACK now.
 2396                          */
 2397                         if (tp->t_flags & TF_NEEDSYN)
 2398                                 tp->t_flags |= TF_DELACK;
 2399                         else
 2400                                 tp->t_flags |= TF_ACKNOW;
 2401                         tp->rcv_nxt++;
 2402                 }
 2403                 switch (tp->t_state) {
 2404 
 2405                 /*
 2406                  * In SYN_RECEIVED and ESTABLISHED STATES
 2407                  * enter the CLOSE_WAIT state.
 2408                  */
 2409                 case TCPS_SYN_RECEIVED:
 2410                         tp->t_starttime = ticks;
 2411                         /*FALLTHROUGH*/
 2412                 case TCPS_ESTABLISHED:
 2413                         tp->t_state = TCPS_CLOSE_WAIT;
 2414                         break;
 2415 
 2416                 /*
 2417                  * If still in FIN_WAIT_1 STATE FIN has not been acked so
 2418                  * enter the CLOSING state.
 2419                  */
 2420                 case TCPS_FIN_WAIT_1:
 2421                         tp->t_state = TCPS_CLOSING;
 2422                         break;
 2423 
 2424                 /*
 2425                  * In FIN_WAIT_2 state enter the TIME_WAIT state,
 2426                  * starting the time-wait timer, turning off the other
 2427                  * standard timers.
 2428                  */
 2429                 case TCPS_FIN_WAIT_2:
 2430                         KASSERT(headlocked == 1, ("tcp_input: dodata: "
 2431                             "TCP_FIN_WAIT_2: head not locked"));
 2432                         tcp_twstart(tp);
 2433                         INP_INFO_WUNLOCK(&tcbinfo);
 2434                         return;
 2435 
 2436                 /*
 2437                  * In TIME_WAIT state restart the 2 MSL time_wait timer.
 2438                  */
 2439                 case TCPS_TIME_WAIT:
 2440                         KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
 2441                         callout_reset(tp->tt_2msl, 2 * tcp_msl,
 2442                                       tcp_timer_2msl, tp);
 2443                         break;
 2444                 }
 2445         }
 2446         INP_INFO_WUNLOCK(&tcbinfo);
 2447         headlocked = 0;
 2448 #ifdef TCPDEBUG
 2449         if (so->so_options & SO_DEBUG)
 2450                 tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
 2451                           &tcp_savetcp, 0);
 2452 #endif
 2453 
 2454         /*
 2455          * Return any desired output.
 2456          */
 2457         if (needoutput || (tp->t_flags & TF_ACKNOW))
 2458                 (void) tcp_output(tp);
 2459 
 2460 check_delack:
 2461         KASSERT(headlocked == 0, ("tcp_input: check_delack: head locked"));
 2462         INP_LOCK_ASSERT(inp);
 2463         if (tp->t_flags & TF_DELACK) {
 2464                 tp->t_flags &= ~TF_DELACK;
 2465                 callout_reset(tp->tt_delack, tcp_delacktime,
 2466                     tcp_timer_delack, tp);
 2467         }
 2468         INP_UNLOCK(inp);
 2469         return;
 2470 
 2471 dropafterack:
 2472         KASSERT(headlocked, ("tcp_input: dropafterack: head not locked"));
 2473         /*
 2474          * Generate an ACK dropping incoming segment if it occupies
 2475          * sequence space, where the ACK reflects our state.
 2476          *
 2477          * We can now skip the test for the RST flag since all
 2478          * paths to this code happen after packets containing
 2479          * RST have been dropped.
 2480          *
 2481          * In the SYN-RECEIVED state, don't send an ACK unless the
 2482          * segment we received passes the SYN-RECEIVED ACK test.
 2483          * If it fails send a RST.  This breaks the loop in the
 2484          * "LAND" DoS attack, and also prevents an ACK storm
 2485          * between two listening ports that have been sent forged
 2486          * SYN segments, each with the source address of the other.
 2487          */
 2488         if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
 2489             (SEQ_GT(tp->snd_una, th->th_ack) ||
 2490              SEQ_GT(th->th_ack, tp->snd_max)) ) {
 2491                 rstreason = BANDLIM_RST_OPENPORT;
 2492                 goto dropwithreset;
 2493         }
 2494 #ifdef TCPDEBUG
 2495         if (so->so_options & SO_DEBUG)
 2496                 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
 2497                           &tcp_savetcp, 0);
 2498 #endif
 2499         KASSERT(headlocked, ("headlocked should be 1"));
 2500         INP_INFO_WUNLOCK(&tcbinfo);
 2501         tp->t_flags |= TF_ACKNOW;
 2502         (void) tcp_output(tp);
 2503         INP_UNLOCK(inp);
 2504         m_freem(m);
 2505         return;
 2506 
 2507 dropwithreset:
 2508         KASSERT(headlocked, ("tcp_input: dropwithreset: head not locked"));
 2509         /*
 2510          * Generate a RST, dropping incoming segment.
 2511          * Make ACK acceptable to originator of segment.
 2512          * Don't bother to respond if destination was broadcast/multicast.
 2513          */
 2514         if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
 2515                 goto drop;
 2516         if (isipv6) {
 2517                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
 2518                     IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
 2519                         goto drop;
 2520         } else {
 2521                 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
 2522                     IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
 2523                     ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
 2524                     in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
 2525                         goto drop;
 2526         }
 2527         /* IPv6 anycast check is done at tcp6_input() */
 2528 
 2529         /*
 2530          * Perform bandwidth limiting.
 2531          */
 2532         if (badport_bandlim(rstreason) < 0)
 2533                 goto drop;
 2534 
 2535 #ifdef TCPDEBUG
 2536         if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
 2537                 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
 2538                           &tcp_savetcp, 0);
 2539 #endif
 2540 
 2541         if (thflags & TH_ACK)
 2542                 /* mtod() below is safe as long as hdr dropping is delayed */
 2543                 tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
 2544                             TH_RST);
 2545         else {
 2546                 if (thflags & TH_SYN)
 2547                         tlen++;
 2548                 /* mtod() below is safe as long as hdr dropping is delayed */
 2549                 tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
 2550                             (tcp_seq)0, TH_RST|TH_ACK);
 2551         }
 2552 
 2553         if (tp)
 2554                 INP_UNLOCK(inp);
 2555         if (headlocked)
 2556                 INP_INFO_WUNLOCK(&tcbinfo);
 2557         return;
 2558 
 2559 drop:
 2560         /*
 2561          * Drop space held by incoming segment and return.
 2562          */
 2563 #ifdef TCPDEBUG
 2564         if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
 2565                 tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
 2566                           &tcp_savetcp, 0);
 2567 #endif
 2568         if (tp)
 2569                 INP_UNLOCK(inp);
 2570         if (headlocked)
 2571                 INP_INFO_WUNLOCK(&tcbinfo);
 2572         m_freem(m);
 2573         return;
 2574 }
 2575 
 2576 /*
 2577  * Parse TCP options and place in tcpopt.
 2578  */
 2579 static void
 2580 tcp_dooptions(to, cp, cnt, is_syn)
 2581         struct tcpopt *to;
 2582         u_char *cp;
 2583         int cnt;
 2584         int is_syn;
 2585 {
 2586         int opt, optlen;
 2587 
 2588         to->to_flags = 0;
 2589         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 2590                 opt = cp[0];
 2591                 if (opt == TCPOPT_EOL)
 2592                         break;
 2593                 if (opt == TCPOPT_NOP)
 2594                         optlen = 1;
 2595                 else {
 2596                         if (cnt < 2)
 2597                                 break;
 2598                         optlen = cp[1];
 2599                         if (optlen < 2 || optlen > cnt)
 2600                                 break;
 2601                 }
 2602                 switch (opt) {
 2603                 case TCPOPT_MAXSEG:
 2604                         if (optlen != TCPOLEN_MAXSEG)
 2605                                 continue;
 2606                         if (!is_syn)
 2607                                 continue;
 2608                         to->to_flags |= TOF_MSS;
 2609                         bcopy((char *)cp + 2,
 2610                             (char *)&to->to_mss, sizeof(to->to_mss));
 2611                         to->to_mss = ntohs(to->to_mss);
 2612                         break;
 2613                 case TCPOPT_WINDOW:
 2614                         if (optlen != TCPOLEN_WINDOW)
 2615                                 continue;
 2616                         if (! is_syn)
 2617                                 continue;
 2618                         to->to_flags |= TOF_SCALE;
 2619                         to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
 2620                         break;
 2621                 case TCPOPT_TIMESTAMP:
 2622                         if (optlen != TCPOLEN_TIMESTAMP)
 2623                                 continue;
 2624                         to->to_flags |= TOF_TS;
 2625                         bcopy((char *)cp + 2,
 2626                             (char *)&to->to_tsval, sizeof(to->to_tsval));
 2627                         to->to_tsval = ntohl(to->to_tsval);
 2628                         bcopy((char *)cp + 6,
 2629                             (char *)&to->to_tsecr, sizeof(to->to_tsecr));
 2630                         to->to_tsecr = ntohl(to->to_tsecr);
 2631                         /*
 2632                          * If echoed timestamp is later than the current time,
 2633                          * fall back to non RFC1323 RTT calculation.
 2634                          */
 2635                         if ((to->to_tsecr != 0) && TSTMP_GT(to->to_tsecr, ticks))
 2636                                 to->to_tsecr = 0;
 2637                         break;
 2638 #ifdef TCP_SIGNATURE
 2639                 /*
 2640                  * XXX In order to reply to a host which has set the
 2641                  * TCP_SIGNATURE option in its initial SYN, we have to
 2642                  * record the fact that the option was observed here
 2643                  * for the syncache code to perform the correct response.
 2644                  */
 2645                 case TCPOPT_SIGNATURE:
 2646                         if (optlen != TCPOLEN_SIGNATURE)
 2647                                 continue;
 2648                         to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
 2649                         break;
 2650 #endif
 2651                 case TCPOPT_SACK_PERMITTED:
 2652                         if (!tcp_do_sack ||
 2653                             optlen != TCPOLEN_SACK_PERMITTED)
 2654                                 continue;
 2655                         if (is_syn) {
 2656                                 /* MUST only be set on SYN */
 2657                                 to->to_flags |= TOF_SACK;
 2658                         }
 2659                         break;
 2660                 case TCPOPT_SACK:
 2661                         if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
 2662                                 continue;
 2663                         to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
 2664                         to->to_sacks = cp + 2;
 2665                         tcpstat.tcps_sack_rcv_blocks++;
 2666                         break;
 2667                 default:
 2668                         continue;
 2669                 }
 2670         }
 2671 }
 2672 
 2673 /*
 2674  * Pull out of band byte out of a segment so
 2675  * it doesn't appear in the user's data queue.
 2676  * It is still reflected in the segment length for
 2677  * sequencing purposes.
 2678  */
 2679 static void
 2680 tcp_pulloutofband(so, th, m, off)
 2681         struct socket *so;
 2682         struct tcphdr *th;
 2683         register struct mbuf *m;
 2684         int off;                /* delayed to be droped hdrlen */
 2685 {
 2686         int cnt = off + th->th_urp - 1;
 2687 
 2688         while (cnt >= 0) {
 2689                 if (m->m_len > cnt) {
 2690                         char *cp = mtod(m, caddr_t) + cnt;
 2691                         struct tcpcb *tp = sototcpcb(so);
 2692 
 2693                         tp->t_iobc = *cp;
 2694                         tp->t_oobflags |= TCPOOB_HAVEDATA;
 2695                         bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
 2696                         m->m_len--;
 2697                         if (m->m_flags & M_PKTHDR)
 2698                                 m->m_pkthdr.len--;
 2699                         return;
 2700                 }
 2701                 cnt -= m->m_len;
 2702                 m = m->m_next;
 2703                 if (m == 0)
 2704                         break;
 2705         }
 2706         panic("tcp_pulloutofband");
 2707 }
 2708 
 2709 /*
 2710  * Collect new round-trip time estimate
 2711  * and update averages and current timeout.
 2712  */
 2713 static void
 2714 tcp_xmit_timer(tp, rtt)
 2715         register struct tcpcb *tp;
 2716         int rtt;
 2717 {
 2718         register int delta;
 2719 
 2720         INP_LOCK_ASSERT(tp->t_inpcb);
 2721 
 2722         tcpstat.tcps_rttupdated++;
 2723         tp->t_rttupdated++;
 2724         if (tp->t_srtt != 0) {
 2725                 /*
 2726                  * srtt is stored as fixed point with 5 bits after the
 2727                  * binary point (i.e., scaled by 8).  The following magic
 2728                  * is equivalent to the smoothing algorithm in rfc793 with
 2729                  * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
 2730                  * point).  Adjust rtt to origin 0.
 2731                  */
 2732                 delta = ((rtt - 1) << TCP_DELTA_SHIFT)
 2733                         - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
 2734 
 2735                 if ((tp->t_srtt += delta) <= 0)
 2736                         tp->t_srtt = 1;
 2737 
 2738                 /*
 2739                  * We accumulate a smoothed rtt variance (actually, a
 2740                  * smoothed mean difference), then set the retransmit
 2741                  * timer to smoothed rtt + 4 times the smoothed variance.
 2742                  * rttvar is stored as fixed point with 4 bits after the
 2743                  * binary point (scaled by 16).  The following is
 2744                  * equivalent to rfc793 smoothing with an alpha of .75
 2745                  * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
 2746                  * rfc793's wired-in beta.
 2747                  */
 2748                 if (delta < 0)
 2749                         delta = -delta;
 2750                 delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
 2751                 if ((tp->t_rttvar += delta) <= 0)
 2752                         tp->t_rttvar = 1;
 2753                 if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
 2754                     tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
 2755         } else {
 2756                 /*
 2757                  * No rtt measurement yet - use the unsmoothed rtt.
 2758                  * Set the variance to half the rtt (so our first
 2759                  * retransmit happens at 3*rtt).
 2760                  */
 2761                 tp->t_srtt = rtt << TCP_RTT_SHIFT;
 2762                 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
 2763                 tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
 2764         }
 2765         tp->t_rtttime = 0;
 2766         tp->t_rxtshift = 0;
 2767 
 2768         /*
 2769          * the retransmit should happen at rtt + 4 * rttvar.
 2770          * Because of the way we do the smoothing, srtt and rttvar
 2771          * will each average +1/2 tick of bias.  When we compute
 2772          * the retransmit timer, we want 1/2 tick of rounding and
 2773          * 1 extra tick because of +-1/2 tick uncertainty in the
 2774          * firing of the timer.  The bias will give us exactly the
 2775          * 1.5 tick we need.  But, because the bias is
 2776          * statistical, we have to test that we don't drop below
 2777          * the minimum feasible timer (which is 2 ticks).
 2778          */
 2779         TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
 2780                       max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
 2781 
 2782         /*
 2783          * We received an ack for a packet that wasn't retransmitted;
 2784          * it is probably safe to discard any error indications we've
 2785          * received recently.  This isn't quite right, but close enough
 2786          * for now (a route might have failed after we sent a segment,
 2787          * and the return path might not be symmetrical).
 2788          */
 2789         tp->t_softerror = 0;
 2790 }
 2791 
 2792 /*
 2793  * Determine a reasonable value for maxseg size.
 2794  * If the route is known, check route for mtu.
 2795  * If none, use an mss that can be handled on the outgoing
 2796  * interface without forcing IP to fragment; if bigger than
 2797  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
 2798  * to utilize large mbufs.  If no route is found, route has no mtu,
 2799  * or the destination isn't local, use a default, hopefully conservative
 2800  * size (usually 512 or the default IP max size, but no more than the mtu
 2801  * of the interface), as we can't discover anything about intervening
 2802  * gateways or networks.  We also initialize the congestion/slow start
 2803  * window to be a single segment if the destination isn't local.
 2804  * While looking at the routing entry, we also initialize other path-dependent
 2805  * parameters from pre-set or cached values in the routing entry.
 2806  *
 2807  * Also take into account the space needed for options that we
 2808  * send regularly.  Make maxseg shorter by that amount to assure
 2809  * that we can send maxseg amount of data even when the options
 2810  * are present.  Store the upper limit of the length of options plus
 2811  * data in maxopd.
 2812  *
 2813  *
 2814  * In case of T/TCP, we call this routine during implicit connection
 2815  * setup as well (offer = -1), to initialize maxseg from the cached
 2816  * MSS of our peer.
 2817  *
 2818  * NOTE that this routine is only called when we process an incoming
 2819  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
 2820  */
 2821 void
 2822 tcp_mss(tp, offer)
 2823         struct tcpcb *tp;
 2824         int offer;
 2825 {
 2826         int rtt, mss;
 2827         u_long bufsize;
 2828         u_long maxmtu;
 2829         struct inpcb *inp = tp->t_inpcb;
 2830         struct socket *so;
 2831         struct hc_metrics_lite metrics;
 2832         int origoffer = offer;
 2833 #ifdef INET6
 2834         int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
 2835         size_t min_protoh = isipv6 ?
 2836                             sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
 2837                             sizeof (struct tcpiphdr);
 2838 #else
 2839         const size_t min_protoh = sizeof(struct tcpiphdr);
 2840 #endif
 2841 
 2842         /* initialize */
 2843 #ifdef INET6
 2844         if (isipv6) {
 2845                 maxmtu = tcp_maxmtu6(&inp->inp_inc);
 2846                 tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
 2847         } else
 2848 #endif
 2849         {
 2850                 maxmtu = tcp_maxmtu(&inp->inp_inc);
 2851                 tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
 2852         }
 2853         so = inp->inp_socket;
 2854 
 2855         /*
 2856          * no route to sender, stay with default mss and return
 2857          */
 2858         if (maxmtu == 0)
 2859                 return;
 2860 
 2861         /* what have we got? */
 2862         switch (offer) {
 2863                 case 0:
 2864                         /*
 2865                          * Offer == 0 means that there was no MSS on the SYN
 2866                          * segment, in this case we use tcp_mssdflt.
 2867                          */
 2868                         offer =
 2869 #ifdef INET6
 2870                                 isipv6 ? tcp_v6mssdflt :
 2871 #endif
 2872                                 tcp_mssdflt;
 2873                         break;
 2874 
 2875                 case -1:
 2876                         /*
 2877                          * Offer == -1 means that we didn't receive SYN yet.
 2878                          */
 2879                         /* FALLTHROUGH */
 2880 
 2881                 default:
 2882                         /*
 2883                          * Prevent DoS attack with too small MSS. Round up
 2884                          * to at least minmss.
 2885                          */
 2886                         offer = max(offer, tcp_minmss);
 2887                         /*
 2888                          * Sanity check: make sure that maxopd will be large
 2889                          * enough to allow some data on segments even if the
 2890                          * all the option space is used (40bytes).  Otherwise
 2891                          * funny things may happen in tcp_output.
 2892                          */
 2893                         offer = max(offer, 64);
 2894         }
 2895 
 2896         /*
 2897          * rmx information is now retrieved from tcp_hostcache
 2898          */
 2899         tcp_hc_get(&inp->inp_inc, &metrics);
 2900 
 2901         /*
 2902          * if there's a discovered mtu int tcp hostcache, use it
 2903          * else, use the link mtu.
 2904          */
 2905         if (metrics.rmx_mtu)
 2906                 mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
 2907         else {
 2908 #ifdef INET6
 2909                 if (isipv6) {
 2910                         mss = maxmtu - min_protoh;
 2911                         if (!path_mtu_discovery &&
 2912                             !in6_localaddr(&inp->in6p_faddr))
 2913                                 mss = min(mss, tcp_v6mssdflt);
 2914                 } else
 2915 #endif
 2916                 {
 2917                         mss = maxmtu - min_protoh;
 2918                         if (!path_mtu_discovery &&
 2919                             !in_localaddr(inp->inp_faddr))
 2920                                 mss = min(mss, tcp_mssdflt);
 2921                 }
 2922         }
 2923         mss = min(mss, offer);
 2924 
 2925         /*
 2926          * maxopd stores the maximum length of data AND options
 2927          * in a segment; maxseg is the amount of data in a normal
 2928          * segment.  We need to store this value (maxopd) apart
 2929          * from maxseg, because now every segment carries options
 2930          * and thus we normally have somewhat less data in segments.
 2931          */
 2932         tp->t_maxopd = mss;
 2933 
 2934         /*
 2935          * origoffer==-1 indicates, that no segments were received yet.
 2936          * In this case we just guess.
 2937          */
 2938         if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
 2939             (origoffer == -1 ||
 2940              (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
 2941                 mss -= TCPOLEN_TSTAMP_APPA;
 2942         tp->t_maxseg = mss;
 2943 
 2944 #if     (MCLBYTES & (MCLBYTES - 1)) == 0
 2945                 if (mss > MCLBYTES)
 2946                         mss &= ~(MCLBYTES-1);
 2947 #else
 2948                 if (mss > MCLBYTES)
 2949                         mss = mss / MCLBYTES * MCLBYTES;
 2950 #endif
 2951         tp->t_maxseg = mss;
 2952 
 2953         /*
 2954          * If there's a pipesize, change the socket buffer to that size,
 2955          * don't change if sb_hiwat is different than default (then it
 2956          * has been changed on purpose with setsockopt).
 2957          * Make the socket buffers an integral number of mss units;
 2958          * if the mss is larger than the socket buffer, decrease the mss.
 2959          */
 2960         SOCKBUF_LOCK(&so->so_snd);
 2961         if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
 2962                 bufsize = metrics.rmx_sendpipe;
 2963         else
 2964                 bufsize = so->so_snd.sb_hiwat;
 2965         if (bufsize < mss)
 2966                 mss = bufsize;
 2967         else {
 2968                 bufsize = roundup(bufsize, mss);
 2969                 if (bufsize > sb_max)
 2970                         bufsize = sb_max;
 2971                 if (bufsize > so->so_snd.sb_hiwat)
 2972                         (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
 2973         }
 2974         SOCKBUF_UNLOCK(&so->so_snd);
 2975         tp->t_maxseg = mss;
 2976 
 2977         SOCKBUF_LOCK(&so->so_rcv);
 2978         if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
 2979                 bufsize = metrics.rmx_recvpipe;
 2980         else
 2981                 bufsize = so->so_rcv.sb_hiwat;
 2982         if (bufsize > mss) {
 2983                 bufsize = roundup(bufsize, mss);
 2984                 if (bufsize > sb_max)
 2985                         bufsize = sb_max;
 2986                 if (bufsize > so->so_rcv.sb_hiwat)
 2987                         (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
 2988         }
 2989         SOCKBUF_UNLOCK(&so->so_rcv);
 2990         /*
 2991          * While we're here, check the others too
 2992          */
 2993         if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
 2994                 tp->t_srtt = rtt;
 2995                 tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
 2996                 tcpstat.tcps_usedrtt++;
 2997                 if (metrics.rmx_rttvar) {
 2998                         tp->t_rttvar = metrics.rmx_rttvar;
 2999                         tcpstat.tcps_usedrttvar++;
 3000                 } else {
 3001                         /* default variation is +- 1 rtt */
 3002                         tp->t_rttvar =
 3003                             tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
 3004                 }
 3005                 TCPT_RANGESET(tp->t_rxtcur,
 3006                               ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
 3007                               tp->t_rttmin, TCPTV_REXMTMAX);
 3008         }
 3009         if (metrics.rmx_ssthresh) {
 3010                 /*
 3011                  * There's some sort of gateway or interface
 3012                  * buffer limit on the path.  Use this to set
 3013                  * the slow start threshhold, but set the
 3014                  * threshold to no less than 2*mss.
 3015                  */
 3016                 tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
 3017                 tcpstat.tcps_usedssthresh++;
 3018         }
 3019         if (metrics.rmx_bandwidth)
 3020                 tp->snd_bandwidth = metrics.rmx_bandwidth;
 3021 
 3022         /*
 3023          * Set the slow-start flight size depending on whether this
 3024          * is a local network or not.
 3025          *
 3026          * Extend this so we cache the cwnd too and retrieve it here.
 3027          * Make cwnd even bigger than RFC3390 suggests but only if we
 3028          * have previous experience with the remote host. Be careful
 3029          * not make cwnd bigger than remote receive window or our own
 3030          * send socket buffer. Maybe put some additional upper bound
 3031          * on the retrieved cwnd. Should do incremental updates to
 3032          * hostcache when cwnd collapses so next connection doesn't
 3033          * overloads the path again.
 3034          *
 3035          * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
 3036          * We currently check only in syncache_socket for that.
 3037          */
 3038 #define TCP_METRICS_CWND
 3039 #ifdef TCP_METRICS_CWND
 3040         if (metrics.rmx_cwnd)
 3041                 tp->snd_cwnd = max(mss,
 3042                                 min(metrics.rmx_cwnd / 2,
 3043                                  min(tp->snd_wnd, so->so_snd.sb_hiwat)));
 3044         else
 3045 #endif
 3046         if (tcp_do_rfc3390)
 3047                 tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
 3048 #ifdef INET6
 3049         else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
 3050                  (!isipv6 && in_localaddr(inp->inp_faddr)))
 3051 #else
 3052         else if (in_localaddr(inp->inp_faddr))
 3053 #endif
 3054                 tp->snd_cwnd = mss * ss_fltsz_local;
 3055         else
 3056                 tp->snd_cwnd = mss * ss_fltsz;
 3057 }
 3058 
 3059 /*
 3060  * Determine the MSS option to send on an outgoing SYN.
 3061  */
 3062 int
 3063 tcp_mssopt(inc)
 3064         struct in_conninfo *inc;
 3065 {
 3066         int mss = 0;
 3067         u_long maxmtu = 0;
 3068         u_long thcmtu = 0;
 3069         size_t min_protoh;
 3070 #ifdef INET6
 3071         int isipv6 = inc->inc_isipv6 ? 1 : 0;
 3072 #endif
 3073 
 3074         KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
 3075 
 3076 #ifdef INET6
 3077         if (isipv6) {
 3078                 mss = tcp_v6mssdflt;
 3079                 maxmtu = tcp_maxmtu6(inc);
 3080                 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
 3081                 min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
 3082         } else
 3083 #endif
 3084         {
 3085                 mss = tcp_mssdflt;
 3086                 maxmtu = tcp_maxmtu(inc);
 3087                 thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
 3088                 min_protoh = sizeof(struct tcpiphdr);
 3089         }
 3090         if (maxmtu && thcmtu)
 3091                 mss = min(maxmtu, thcmtu) - min_protoh;
 3092         else if (maxmtu || thcmtu)
 3093                 mss = max(maxmtu, thcmtu) - min_protoh;
 3094 
 3095         return (mss);
 3096 }
 3097 
 3098 
 3099 /*
 3100  * On a partial ack arrives, force the retransmission of the
 3101  * next unacknowledged segment.  Do not clear tp->t_dupacks.
 3102  * By setting snd_nxt to ti_ack, this forces retransmission timer to
 3103  * be started again.
 3104  */
 3105 static void
 3106 tcp_newreno_partial_ack(tp, th)
 3107         struct tcpcb *tp;
 3108         struct tcphdr *th;
 3109 {
 3110         tcp_seq onxt = tp->snd_nxt;
 3111         u_long  ocwnd = tp->snd_cwnd;
 3112 
 3113         callout_stop(tp->tt_rexmt);
 3114         tp->t_rtttime = 0;
 3115         tp->snd_nxt = th->th_ack;
 3116         /*
 3117          * Set snd_cwnd to one segment beyond acknowledged offset.
 3118          * (tp->snd_una has not yet been updated when this function is called.)
 3119          */
 3120         tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
 3121         tp->t_flags |= TF_ACKNOW;
 3122         (void) tcp_output(tp);
 3123         tp->snd_cwnd = ocwnd;
 3124         if (SEQ_GT(onxt, tp->snd_nxt))
 3125                 tp->snd_nxt = onxt;
 3126         /*
 3127          * Partial window deflation.  Relies on fact that tp->snd_una
 3128          * not updated yet.
 3129          */
 3130         if (tp->snd_cwnd > th->th_ack - tp->snd_una)
 3131                 tp->snd_cwnd -= th->th_ack - tp->snd_una;
 3132         else
 3133                 tp->snd_cwnd = 0;
 3134         tp->snd_cwnd += tp->t_maxseg;
 3135 }
 3136 
 3137 /*
 3138  * Returns 1 if the TIME_WAIT state was killed and we should start over,
 3139  * looking for a pcb in the listen state.  Returns 0 otherwise.
 3140  */
 3141 static int
 3142 tcp_timewait(tw, to, th, m, tlen)
 3143         struct tcptw *tw;
 3144         struct tcpopt *to;
 3145         struct tcphdr *th;
 3146         struct mbuf *m;
 3147         int tlen;
 3148 {
 3149         int thflags;
 3150         tcp_seq seq;
 3151 #ifdef INET6
 3152         int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
 3153 #else
 3154         const int isipv6 = 0;
 3155 #endif
 3156 
 3157         /* tcbinfo lock required for tcp_twclose(), tcp_2msl_reset. */
 3158         INP_INFO_WLOCK_ASSERT(&tcbinfo);
 3159         INP_LOCK_ASSERT(tw->tw_inpcb);
 3160 
 3161         thflags = th->th_flags;
 3162 
 3163         /*
 3164          * NOTE: for FIN_WAIT_2 (to be added later),
 3165          * must validate sequence number before accepting RST
 3166          */
 3167 
 3168         /*
 3169          * If the segment contains RST:
 3170          *      Drop the segment - see Stevens, vol. 2, p. 964 and
 3171          *      RFC 1337.
 3172          */
 3173         if (thflags & TH_RST)
 3174                 goto drop;
 3175 
 3176 #if 0
 3177 /* PAWS not needed at the moment */
 3178         /*
 3179          * RFC 1323 PAWS: If we have a timestamp reply on this segment
 3180          * and it's less than ts_recent, drop it.
 3181          */
 3182         if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
 3183             TSTMP_LT(to.to_tsval, tp->ts_recent)) {
 3184                 if ((thflags & TH_ACK) == 0)
 3185                         goto drop;
 3186                 goto ack;
 3187         }
 3188         /*
 3189          * ts_recent is never updated because we never accept new segments.
 3190          */
 3191 #endif
 3192 
 3193         /*
 3194          * If a new connection request is received
 3195          * while in TIME_WAIT, drop the old connection
 3196          * and start over if the sequence numbers
 3197          * are above the previous ones.
 3198          */
 3199         if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
 3200                 (void) tcp_twclose(tw, 0);
 3201                 return (1);
 3202         }
 3203 
 3204         /*
 3205          * Drop the the segment if it does not contain an ACK.
 3206          */
 3207         if ((thflags & TH_ACK) == 0)
 3208                 goto drop;
 3209 
 3210         /*
 3211          * Reset the 2MSL timer if this is a duplicate FIN.
 3212          */
 3213         if (thflags & TH_FIN) {
 3214                 seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
 3215                 if (seq + 1 == tw->rcv_nxt)
 3216                         tcp_timer_2msl_reset(tw, 2 * tcp_msl);
 3217         }
 3218 
 3219         /*
 3220          * Acknowledge the segment if it has data or is not a duplicate ACK.
 3221          */
 3222         if (thflags != TH_ACK || tlen != 0 ||
 3223             th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
 3224                 tcp_twrespond(tw, TH_ACK);
 3225         goto drop;
 3226 
 3227         /*
 3228          * Generate a RST, dropping incoming segment.
 3229          * Make ACK acceptable to originator of segment.
 3230          * Don't bother to respond if destination was broadcast/multicast.
 3231          */
 3232         if (m->m_flags & (M_BCAST|M_MCAST))
 3233                 goto drop;
 3234         if (isipv6) {
 3235                 struct ip6_hdr *ip6;
 3236 
 3237                 /* IPv6 anycast check is done at tcp6_input() */
 3238                 ip6 = mtod(m, struct ip6_hdr *);
 3239                 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
 3240                     IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
 3241                         goto drop;
 3242         } else {
 3243                 struct ip *ip;
 3244 
 3245                 ip = mtod(m, struct ip *);
 3246                 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
 3247                     IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
 3248                     ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
 3249                     in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
 3250                         goto drop;
 3251         }
 3252         if (thflags & TH_ACK) {
 3253                 tcp_respond(NULL,
 3254                     mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
 3255         } else {
 3256                 seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
 3257                 tcp_respond(NULL,
 3258                     mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
 3259         }
 3260         INP_UNLOCK(tw->tw_inpcb);
 3261         return (0);
 3262 
 3263 drop:
 3264         INP_UNLOCK(tw->tw_inpcb);
 3265         m_freem(m);
 3266         return (0);
 3267 }

Cache object: cbedebaad36bc6676f7f286831b53954


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