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


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

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

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

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

Cache object: 52f57cbdcd28579bb9c0b200f569a93a


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