1c398230bSWarner Losh /*- 2e79adb8eSGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4dbc42409SLawrence Stewart * Copyright (c) 2007-2008,2010 5dbc42409SLawrence Stewart * Swinburne University of Technology, Melbourne, Australia. 6dbc42409SLawrence Stewart * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org> 7dbc42409SLawrence Stewart * Copyright (c) 2010 The FreeBSD Foundation 8dbc42409SLawrence Stewart * All rights reserved. 9dbc42409SLawrence Stewart * 10dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced Internet 11*891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart, 12*891b8ed4SLawrence Stewart * James Healy and David Hayes, made possible in part by a grant from the Cisco 13*891b8ed4SLawrence Stewart * University Research Program Fund at Community Foundation Silicon Valley. 14dbc42409SLawrence Stewart * 15dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced 16dbc42409SLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 17dbc42409SLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 20df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 21df8bae1dSRodney W. Grimes * are met: 22df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 23df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 24df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 25df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 26df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 27df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 28df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 29df8bae1dSRodney W. Grimes * without specific prior written permission. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41df8bae1dSRodney W. Grimes * SUCH DAMAGE. 42df8bae1dSRodney W. Grimes * 43e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 44df8bae1dSRodney W. Grimes */ 45df8bae1dSRodney W. Grimes 464b421e2dSMike Silbersack #include <sys/cdefs.h> 474b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 484b421e2dSMike Silbersack 49f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 501cfd4b53SBruce M Simpson #include "opt_inet.h" 51fb59c426SYoshinobu Inoue #include "opt_inet6.h" 523a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 530cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 540cc12cc5SJoerg Wunsch 55df8bae1dSRodney W. Grimes #include <sys/param.h> 5698163b98SPoul-Henning Kamp #include <sys/kernel.h> 5739bc9de5SLawrence Stewart #include <sys/hhook.h> 58df8bae1dSRodney W. Grimes #include <sys/malloc.h> 59df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 60a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 61df8bae1dSRodney W. Grimes #include <sys/protosw.h> 62960ed29cSSeigo Tanimura #include <sys/signalvar.h> 63df8bae1dSRodney W. Grimes #include <sys/socket.h> 64df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 65960ed29cSSeigo Tanimura #include <sys/sysctl.h> 66816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 67960ed29cSSeigo Tanimura #include <sys/systm.h> 68df8bae1dSRodney W. Grimes 69e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 70e79adb8eSGarrett Wollman 7112e2e970SAndre Oppermann #include <vm/uma.h> 7212e2e970SAndre Oppermann 73df8bae1dSRodney W. Grimes #include <net/if.h> 74df8bae1dSRodney W. Grimes #include <net/route.h> 75530c0060SRobert Watson #include <net/vnet.h> 76df8bae1dSRodney W. Grimes 77773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 78773673c1SAndre Oppermann 79dbc42409SLawrence Stewart #include <netinet/cc.h> 80df8bae1dSRodney W. Grimes #include <netinet/in.h> 81960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 82df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 83960ed29cSSeigo Tanimura #include <netinet/in_var.h> 84df8bae1dSRodney W. Grimes #include <netinet/ip.h> 858e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 86686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 87df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 88ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 89686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 90686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 91686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 92960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 93960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 94df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 95df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 96df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 97df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 98fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 99df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 100c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 101610ee2f9SDavid Greenman #ifdef TCPDEBUG 102df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 103fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 104fb59c426SYoshinobu Inoue 105b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 106b9234fafSSam Leffler #include <netipsec/ipsec.h> 107b9234fafSSam Leffler #include <netipsec/ipsec6.h> 108b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 109b9234fafSSam Leffler 110db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 111db4f9cc7SJonathan Lemon 112aed55708SRobert Watson #include <security/mac/mac_framework.h> 113aed55708SRobert Watson 114dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1150312fbe9SPoul-Henning Kamp 116eddfbb76SRobert Watson VNET_DEFINE(struct tcpstat, tcpstat); 117eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 118eddfbb76SRobert Watson &VNET_NAME(tcpstat), tcpstat, 1198b615593SMarko Zec "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1200312fbe9SPoul-Henning Kamp 121773673c1SAndre Oppermann int tcp_log_in_vain = 0; 122816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 123eddfbb76SRobert Watson &tcp_log_in_vain, 0, 124eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 125816a3d83SPoul-Henning Kamp 12682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 12782cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 128eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 129eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 130eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 13116f7f31fSGeoff Rehmet 13282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 133eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 134eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1353d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 136f498eeeeSDavid Greenman 13782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 13882cea7e6SBjoern A. Zeeb #define V_drop_synfin VNET(drop_synfin) 139eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 140eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 141eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 142f8613305SDag-Erling Smørgrav 14382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 14482cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 145eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 146eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 147eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 148582a954bSJeffrey Hsu 14982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 150eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 151eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 152da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 153da3a8a1aSJeffrey Hsu 15482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 155eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 156eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 15724cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 158eddfbb76SRobert Watson 15982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 160eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 161eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 16224cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 16324cb0f22SLawrence Stewart 164f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 165f2512ba1SRui Paulo 16682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0; 167eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW, 168eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 169eddfbb76SRobert Watson "TCP ECN support"); 170eddfbb76SRobert Watson 17182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 172eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW, 173eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 174eddfbb76SRobert Watson "Max retries before giving up on ECN"); 175eddfbb76SRobert Watson 17682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 17782cea7e6SBjoern A. Zeeb #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 178eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 179eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 1806489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 181a69968eeSMike Silbersack 18282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 18382cea7e6SBjoern A. Zeeb #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 184eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 185eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 1868b615593SMarko Zec "Enable automatic receive buffer sizing"); 1876741ecf5SAndre Oppermann 18882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 18982cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 190eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 191eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 1926489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1936741ecf5SAndre Oppermann 19482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 256*1024; 19582cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 196eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 197eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 1988b615593SMarko Zec "Max size of automatic receive buffer"); 1996741ecf5SAndre Oppermann 200252ca428SRobert Watson int tcp_read_locking = 1; 201252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW, 202252ca428SRobert Watson &tcp_read_locking, 0, "Enable read locking strategy"); 203252ca428SRobert Watson 204eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 20544e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 20682cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 207df8bae1dSRodney W. Grimes 2085a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 209679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 210252ca428SRobert Watson struct socket *, struct tcpcb *, int, int, uint8_t, 211252ca428SRobert Watson int); 212302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 213302ce8d6SAndre Oppermann struct tcpcb *, int, int); 2144d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 2154d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 2164d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 217c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 218dbc42409SLawrence Stewart static void inline cc_ack_received(struct tcpcb *tp, struct tcphdr *th, 219dbc42409SLawrence Stewart uint16_t type); 220dbc42409SLawrence Stewart static void inline cc_conn_init(struct tcpcb *tp); 221dbc42409SLawrence Stewart static void inline cc_post_recovery(struct tcpcb *tp, struct tcphdr *th); 22239bc9de5SLawrence Stewart static void inline hhook_run_tcp_est_in(struct tcpcb *tp, 22339bc9de5SLawrence Stewart struct tcphdr *th, struct tcpopt *to); 224f2512ba1SRui Paulo 225315e3e38SRobert Watson /* 226315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 227315e3e38SRobert Watson * into tcpstat treated as an array of u_long. While this encodes the 228315e3e38SRobert Watson * general layout of tcpstat into the caller, it doesn't encode its location, 229315e3e38SRobert Watson * so that future changes to add, for example, per-CPU stats support won't 230315e3e38SRobert Watson * cause binary compatibility problems for kernel modules. 231315e3e38SRobert Watson */ 232315e3e38SRobert Watson void 233315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 234315e3e38SRobert Watson { 235315e3e38SRobert Watson 236315e3e38SRobert Watson (*((u_long *)&V_tcpstat + statnum))++; 237315e3e38SRobert Watson } 238315e3e38SRobert Watson 239dbc42409SLawrence Stewart /* 24039bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 24139bc9de5SLawrence Stewart */ 24239bc9de5SLawrence Stewart static void inline 24339bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 24439bc9de5SLawrence Stewart { 24539bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 24639bc9de5SLawrence Stewart 24739bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 24839bc9de5SLawrence Stewart hhook_data.tp = tp; 24939bc9de5SLawrence Stewart hhook_data.th = th; 25039bc9de5SLawrence Stewart hhook_data.to = to; 25139bc9de5SLawrence Stewart 25239bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 25339bc9de5SLawrence Stewart tp->osd); 25439bc9de5SLawrence Stewart } 25539bc9de5SLawrence Stewart } 25639bc9de5SLawrence Stewart 25739bc9de5SLawrence Stewart /* 258dbc42409SLawrence Stewart * CC wrapper hook functions 259dbc42409SLawrence Stewart */ 260f2512ba1SRui Paulo static void inline 261dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 262f2512ba1SRui Paulo { 263dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 264f2512ba1SRui Paulo 265dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 266dbc42409SLawrence Stewart if (tp->snd_cwnd == min(tp->snd_cwnd, tp->snd_wnd)) 267dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 268dbc42409SLawrence Stewart else 269dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 270dbc42409SLawrence Stewart 271dbc42409SLawrence Stewart if (type == CC_ACK) { 272dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 273dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 274dbc42409SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 275dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 276dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 277dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 278dbc42409SLawrence Stewart } 279dbc42409SLawrence Stewart } else { 280dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 281dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 282dbc42409SLawrence Stewart } 283dbc42409SLawrence Stewart } 284dbc42409SLawrence Stewart 285dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 286dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 287dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 288dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 289dbc42409SLawrence Stewart } 290dbc42409SLawrence Stewart } 291dbc42409SLawrence Stewart 292dbc42409SLawrence Stewart static void inline 293dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 294dbc42409SLawrence Stewart { 295dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 296dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 297dbc42409SLawrence Stewart int rtt; 298dbc42409SLawrence Stewart #ifdef INET6 299dbc42409SLawrence Stewart int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 300dbc42409SLawrence Stewart #endif 301dbc42409SLawrence Stewart 302dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 303dbc42409SLawrence Stewart 304dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 305dbc42409SLawrence Stewart 306dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 307dbc42409SLawrence Stewart tp->t_srtt = rtt; 308dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 309dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 310dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 311dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 312dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 313dbc42409SLawrence Stewart } else { 314dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 315dbc42409SLawrence Stewart tp->t_rttvar = 316dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 317dbc42409SLawrence Stewart } 318dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 319dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 320dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 321dbc42409SLawrence Stewart } 322dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 323dbc42409SLawrence Stewart /* 324dbc42409SLawrence Stewart * There's some sort of gateway or interface 325dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 326dbc42409SLawrence Stewart * the slow start threshhold, but set the 327dbc42409SLawrence Stewart * threshold to no less than 2*mss. 328dbc42409SLawrence Stewart */ 329dbc42409SLawrence Stewart tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 330dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 331dbc42409SLawrence Stewart } 332dbc42409SLawrence Stewart 333dbc42409SLawrence Stewart /* 334dbc42409SLawrence Stewart * Set the slow-start flight size depending on whether this 335dbc42409SLawrence Stewart * is a local network or not. 336dbc42409SLawrence Stewart * 337dbc42409SLawrence Stewart * Extend this so we cache the cwnd too and retrieve it here. 338dbc42409SLawrence Stewart * Make cwnd even bigger than RFC3390 suggests but only if we 339dbc42409SLawrence Stewart * have previous experience with the remote host. Be careful 340dbc42409SLawrence Stewart * not make cwnd bigger than remote receive window or our own 341dbc42409SLawrence Stewart * send socket buffer. Maybe put some additional upper bound 342dbc42409SLawrence Stewart * on the retrieved cwnd. Should do incremental updates to 343dbc42409SLawrence Stewart * hostcache when cwnd collapses so next connection doesn't 344dbc42409SLawrence Stewart * overloads the path again. 345dbc42409SLawrence Stewart * 346dbc42409SLawrence Stewart * XXXAO: Initializing the CWND from the hostcache is broken 347dbc42409SLawrence Stewart * and in its current form not RFC conformant. It is disabled 348dbc42409SLawrence Stewart * until fixed or removed entirely. 349dbc42409SLawrence Stewart * 350dbc42409SLawrence Stewart * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 351dbc42409SLawrence Stewart * We currently check only in syncache_socket for that. 352dbc42409SLawrence Stewart */ 353dbc42409SLawrence Stewart /* #define TCP_METRICS_CWND */ 354dbc42409SLawrence Stewart #ifdef TCP_METRICS_CWND 355dbc42409SLawrence Stewart if (metrics.rmx_cwnd) 356dbc42409SLawrence Stewart tp->snd_cwnd = max(tp->t_maxseg, min(metrics.rmx_cwnd / 2, 357dbc42409SLawrence Stewart min(tp->snd_wnd, so->so_snd.sb_hiwat))); 358dbc42409SLawrence Stewart else 359dbc42409SLawrence Stewart #endif 360dbc42409SLawrence Stewart if (V_tcp_do_rfc3390) 361dbc42409SLawrence Stewart tp->snd_cwnd = min(4 * tp->t_maxseg, 362dbc42409SLawrence Stewart max(2 * tp->t_maxseg, 4380)); 363dbc42409SLawrence Stewart #ifdef INET6 364dbc42409SLawrence Stewart else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 365dbc42409SLawrence Stewart (!isipv6 && in_localaddr(inp->inp_faddr))) 366dbc42409SLawrence Stewart #else 367dbc42409SLawrence Stewart else if (in_localaddr(inp->inp_faddr)) 368dbc42409SLawrence Stewart #endif 369dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg * V_ss_fltsz_local; 370dbc42409SLawrence Stewart else 371dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg * V_ss_fltsz; 372dbc42409SLawrence Stewart 373dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 374dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 375dbc42409SLawrence Stewart } 376dbc42409SLawrence Stewart 377dbc42409SLawrence Stewart void inline 378dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 379dbc42409SLawrence Stewart { 380dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 381dbc42409SLawrence Stewart 382dbc42409SLawrence Stewart switch(type) { 383dbc42409SLawrence Stewart case CC_NDUPACK: 384dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 385f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 386f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 387f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 388f2512ba1SRui Paulo } 389dbc42409SLawrence Stewart break; 390dbc42409SLawrence Stewart case CC_ECN: 391dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 392dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 393dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 394dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 395dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 396dbc42409SLawrence Stewart } 397dbc42409SLawrence Stewart break; 398dbc42409SLawrence Stewart case CC_RTO: 399dbc42409SLawrence Stewart tp->t_dupacks = 0; 400dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 401dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4026157935fSLawrence Stewart tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 4036157935fSLawrence Stewart tp->t_maxseg) * tp->t_maxseg; 404dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg; 405dbc42409SLawrence Stewart break; 406dbc42409SLawrence Stewart case CC_RTO_ERR: 407dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 408dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 409dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 410dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 411dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 412dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 413dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 414dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 415dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 416dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 417dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 418dbc42409SLawrence Stewart break; 419dbc42409SLawrence Stewart } 420dbc42409SLawrence Stewart 421dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 422dbc42409SLawrence Stewart if (th != NULL) 423dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 424dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 425dbc42409SLawrence Stewart } 426dbc42409SLawrence Stewart } 427dbc42409SLawrence Stewart 428dbc42409SLawrence Stewart static void inline 429dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 430dbc42409SLawrence Stewart { 431dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 432dbc42409SLawrence Stewart 433dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 434dbc42409SLawrence Stewart 435dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 436dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 437dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 438dbc42409SLawrence Stewart } 439dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 440dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 441dbc42409SLawrence Stewart } 4420312fbe9SPoul-Henning Kamp 443fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 444fb59c426SYoshinobu Inoue #ifdef INET6 445fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 446fb59c426SYoshinobu Inoue do { \ 447fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 44897d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 44997d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 450fb59c426SYoshinobu Inoue } while (0) 451fb59c426SYoshinobu Inoue #else 452fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 453fb59c426SYoshinobu Inoue #endif 454df8bae1dSRodney W. Grimes 455df8bae1dSRodney W. Grimes /* 456262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 457262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 458262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 4593bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 4603bfd6421SJonathan Lemon * - delayed acks are enabled or 4613bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 462d8c85a26SJonathan Lemon */ 463d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 464b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 465a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 466603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 467d8c85a26SJonathan Lemon 468df8bae1dSRodney W. Grimes /* 4698d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 4708d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 4718d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 4728d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 4738d573cc1SAndre Oppermann * SYN processing on listen sockets 4748d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 4758d573cc1SAndre Oppermann * establishing, established and closing connections 476df8bae1dSRodney W. Grimes */ 477fb59c426SYoshinobu Inoue #ifdef INET6 478fb59c426SYoshinobu Inoue int 479ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 480fb59c426SYoshinobu Inoue { 481ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 48233841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 483fb59c426SYoshinobu Inoue 484fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 485fb59c426SYoshinobu Inoue 486fb59c426SYoshinobu Inoue /* 487fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 488fb59c426SYoshinobu Inoue * better place to put this in? 489fb59c426SYoshinobu Inoue */ 49033841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 49133841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 492fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 493fb59c426SYoshinobu Inoue 4948c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 495fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 496fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 497fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 498fb59c426SYoshinobu Inoue return IPPROTO_DONE; 499fb59c426SYoshinobu Inoue } 500fb59c426SYoshinobu Inoue 501f0ffb944SJulian Elischer tcp_input(m, *offp); 502fb59c426SYoshinobu Inoue return IPPROTO_DONE; 503fb59c426SYoshinobu Inoue } 504fb59c426SYoshinobu Inoue #endif 505fb59c426SYoshinobu Inoue 506df8bae1dSRodney W. Grimes void 507ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 508df8bae1dSRodney W. Grimes { 509ad3f9ab3SAndre Oppermann struct tcphdr *th; 510ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 511ad3f9ab3SAndre Oppermann struct ipovly *ipov; 512ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 513302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 514302ce8d6SAndre Oppermann struct socket *so = NULL; 515e79adb8eSGarrett Wollman u_char *optp = NULL; 51626f9a767SRodney W. Grimes int optlen = 0; 517a0194ef1SBruce M Simpson int len, tlen, off; 518fb59c426SYoshinobu Inoue int drop_hdrlen; 519ad3f9ab3SAndre Oppermann int thflags; 520302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 521f2512ba1SRui Paulo uint8_t iptos; 5229b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5239b932e9eSAndre Oppermann struct m_tag *fwd_tag; 5249b932e9eSAndre Oppermann #endif 525c068736aSJeffrey Hsu #ifdef INET6 526302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 527c068736aSJeffrey Hsu int isipv6; 528c068736aSJeffrey Hsu #else 529a160e630SAndre Oppermann const void *ip6 = NULL; 530c068736aSJeffrey Hsu const int isipv6 = 0; 531c068736aSJeffrey Hsu #endif 532302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 533a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 534252ca428SRobert Watson int ti_locked; 535252ca428SRobert Watson #define TI_UNLOCKED 1 536252ca428SRobert Watson #define TI_RLOCKED 2 537252ca428SRobert Watson #define TI_WLOCKED 3 538f76fcf6dSJeffrey Hsu 539610ee2f9SDavid Greenman #ifdef TCPDEBUG 540c068736aSJeffrey Hsu /* 541c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 542c068736aSJeffrey Hsu * now IPv6. 543c068736aSJeffrey Hsu */ 54414739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 545410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 546610ee2f9SDavid Greenman short ostate = 0; 547610ee2f9SDavid Greenman #endif 548c068736aSJeffrey Hsu 549fb59c426SYoshinobu Inoue #ifdef INET6 550fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 551fb59c426SYoshinobu Inoue #endif 552a0292f23SGarrett Wollman 553302ce8d6SAndre Oppermann to.to_flags = 0; 55478b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 555fb59c426SYoshinobu Inoue 556fb59c426SYoshinobu Inoue if (isipv6) { 55704d3a452SHajimu UMEMOTO #ifdef INET6 5588d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 559fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 560fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 561fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 56278b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 563fb59c426SYoshinobu Inoue goto drop; 564fb59c426SYoshinobu Inoue } 565fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 56633841545SHajimu UMEMOTO 56733841545SHajimu UMEMOTO /* 56833841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 56933841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 57033841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 57133841545SHajimu UMEMOTO * 57233841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 57333841545SHajimu UMEMOTO * already dropped in ip6_input. 57433841545SHajimu UMEMOTO */ 57533841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 57633841545SHajimu UMEMOTO /* XXX stat */ 57733841545SHajimu UMEMOTO goto drop; 57833841545SHajimu UMEMOTO } 57904d3a452SHajimu UMEMOTO #else 5808d573cc1SAndre Oppermann th = NULL; /* XXX: Avoid compiler warning. */ 58104d3a452SHajimu UMEMOTO #endif 582c068736aSJeffrey Hsu } else { 583df8bae1dSRodney W. Grimes /* 584df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 585df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 586df8bae1dSRodney W. Grimes */ 587fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 588df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 589fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 590fb59c426SYoshinobu Inoue } 591df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 5924dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 5934dfdffe9SAndre Oppermann == NULL) { 59478b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 595df8bae1dSRodney W. Grimes return; 596df8bae1dSRodney W. Grimes } 597df8bae1dSRodney W. Grimes } 598fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 599fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 600db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 601db4f9cc7SJonathan Lemon tlen = ip->ip_len; 602df8bae1dSRodney W. Grimes 603db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 604db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 605db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 606db4f9cc7SJonathan Lemon else 607db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 608c068736aSJeffrey Hsu ip->ip_dst.s_addr, 609c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 610c068736aSJeffrey Hsu ip->ip_len + 611c068736aSJeffrey Hsu IPPROTO_TCP)); 612db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 6133c653157SHartmut Brandt #ifdef TCPDEBUG 6143c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 6153c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 6163c653157SHartmut Brandt #endif 617db4f9cc7SJonathan Lemon } else { 618df8bae1dSRodney W. Grimes /* 619df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 620df8bae1dSRodney W. Grimes */ 621df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 622fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 623fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 624fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 625fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 626db4f9cc7SJonathan Lemon } 627fb59c426SYoshinobu Inoue if (th->th_sum) { 62878b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 629df8bae1dSRodney W. Grimes goto drop; 630df8bae1dSRodney W. Grimes } 631fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 632fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 633fb59c426SYoshinobu Inoue } 634df8bae1dSRodney W. Grimes 635f2512ba1SRui Paulo #ifdef INET6 636f2512ba1SRui Paulo if (isipv6) 637f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 638f2512ba1SRui Paulo else 639f2512ba1SRui Paulo #endif 640f2512ba1SRui Paulo iptos = ip->ip_tos; 641f2512ba1SRui Paulo 642df8bae1dSRodney W. Grimes /* 643df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 644df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 645df8bae1dSRodney W. Grimes */ 646fb59c426SYoshinobu Inoue off = th->th_off << 2; 647df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 64878b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 649df8bae1dSRodney W. Grimes goto drop; 650df8bae1dSRodney W. Grimes } 651fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 652df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 653fb59c426SYoshinobu Inoue if (isipv6) { 65404d3a452SHajimu UMEMOTO #ifdef INET6 655fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 656fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 657fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 65804d3a452SHajimu UMEMOTO #endif 659c068736aSJeffrey Hsu } else { 660df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 661c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 6624dfdffe9SAndre Oppermann == NULL) { 66378b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 664df8bae1dSRodney W. Grimes return; 665df8bae1dSRodney W. Grimes } 666fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 667fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 668fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 669fb59c426SYoshinobu Inoue } 670df8bae1dSRodney W. Grimes } 671df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 672fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 673df8bae1dSRodney W. Grimes } 674fb59c426SYoshinobu Inoue thflags = th->th_flags; 675df8bae1dSRodney W. Grimes 676e46cd3d4SDag-Erling Smørgrav /* 677df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 678df8bae1dSRodney W. Grimes */ 679fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 680fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 681fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 682fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 683df8bae1dSRodney W. Grimes 684df8bae1dSRodney W. Grimes /* 685794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 686755c1f07SAndras Olah */ 687fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 688755c1f07SAndras Olah 689755c1f07SAndras Olah /* 690252ca428SRobert Watson * Locate pcb for segment, which requires a lock on tcbinfo. 691d15fb965SRobert Watson * Optimisticaly acquire a global read lock rather than a write lock 692d15fb965SRobert Watson * unless header flags necessarily imply a state change. There are 693d15fb965SRobert Watson * two cases where we might discover later we need a write lock 694d15fb965SRobert Watson * despite the flags: ACKs moving a connection out of the syncache, 695d15fb965SRobert Watson * and ACKs for a connection in TIMEWAIT. 696df8bae1dSRodney W. Grimes */ 697252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 698252ca428SRobert Watson tcp_read_locking == 0) { 699603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 700252ca428SRobert Watson ti_locked = TI_WLOCKED; 701252ca428SRobert Watson } else { 702252ca428SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 703252ca428SRobert Watson ti_locked = TI_RLOCKED; 704252ca428SRobert Watson } 705252ca428SRobert Watson 706df8bae1dSRodney W. Grimes findpcb: 707252ca428SRobert Watson #ifdef INVARIANTS 708252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 709252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 710252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 711603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 712252ca428SRobert Watson else 713252ca428SRobert Watson panic("%s: findpcb ti_locked %d\n", __func__, ti_locked); 714252ca428SRobert Watson #endif 715252ca428SRobert Watson 7169b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 7178d573cc1SAndre Oppermann /* 7188d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7198d573cc1SAndre Oppermann */ 7209b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7219b932e9eSAndre Oppermann 7229b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 7239b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 7249b932e9eSAndre Oppermann 7259b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 726f9e354dfSJulian Elischer /* 7272b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 728f9e354dfSJulian Elischer * already got one like this? 729f9e354dfSJulian Elischer */ 730603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 7319b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 732c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 733c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 734f9e354dfSJulian Elischer if (!inp) { 7359b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 736603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 737fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 7382b25acc1SLuigi Rizzo next_hop->sin_addr, 739c068736aSJeffrey Hsu next_hop->sin_port ? 740c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 741c068736aSJeffrey Hsu th->th_dport, 742421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 743421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 744f9e354dfSJulian Elischer } 7459b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 7469b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 747b10fbdeaSAndre Oppermann } else 7489b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 749b10fbdeaSAndre Oppermann { 75004d3a452SHajimu UMEMOTO if (isipv6) { 75104d3a452SHajimu UMEMOTO #ifdef INET6 752603724d3SBjoern A. Zeeb inp = in6_pcblookup_hash(&V_tcbinfo, 753c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 754c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 755421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 756421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 75704d3a452SHajimu UMEMOTO #endif 75804d3a452SHajimu UMEMOTO } else 759603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 760c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 761c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 762421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 763421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 764fb59c426SYoshinobu Inoue } 765f9e354dfSJulian Elischer 766df8bae1dSRodney W. Grimes /* 767574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 768574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 7698b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 770df8bae1dSRodney W. Grimes */ 771816a3d83SPoul-Henning Kamp if (inp == NULL) { 772574b6964SAndre Oppermann /* 773574b6964SAndre Oppermann * Log communication attempts to ports that are not 774574b6964SAndre Oppermann * in use. 775574b6964SAndre Oppermann */ 776574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 777574b6964SAndre Oppermann tcp_log_in_vain == 2) { 778b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 779df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 780df541e5fSAndre Oppermann "to closed port\n", s, __func__); 7812e4e1b4cSGeoff Rehmet } 782574b6964SAndre Oppermann /* 783574b6964SAndre Oppermann * When blackholing do not respond with a RST but 784574b6964SAndre Oppermann * completely ignore the segment and drop it. 785574b6964SAndre Oppermann */ 786603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 787603724d3SBjoern A. Zeeb V_blackhole == 2) 7881929eae1SAndre Oppermann goto dropunlock; 789574b6964SAndre Oppermann 790a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 791a57815efSBosko Milekic goto dropwithreset; 792816a3d83SPoul-Henning Kamp } 7938501a69cSRobert Watson INP_WLOCK(inp); 79480cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 79580cb9f21SKip Macy && (m->m_flags & M_FLOWID) 79680cb9f21SKip Macy && ((inp->inp_socket == NULL) 79780cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 79880cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 79980cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 80080cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 80180cb9f21SKip Macy } 802a2589465SKen Smith #ifdef IPSEC 803a2589465SKen Smith #ifdef INET6 804a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 805603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 806a2589465SKen Smith goto dropunlock; 807a2589465SKen Smith } else 808a2589465SKen Smith #endif /* INET6 */ 809a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 810603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 811a2589465SKen Smith goto dropunlock; 812a2589465SKen Smith } 813a2589465SKen Smith #endif /* IPSEC */ 814a2589465SKen Smith 8158d573cc1SAndre Oppermann /* 8168d573cc1SAndre Oppermann * Check the minimum TTL for socket. 8178d573cc1SAndre Oppermann */ 81834f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 81934f83c52SGeorge V. Neville-Neil #ifdef INET6 82034f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 821302ce8d6SAndre Oppermann goto dropunlock; 82202707462SAndre Oppermann else 82334f83c52SGeorge V. Neville-Neil #endif 82434f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 825302ce8d6SAndre Oppermann goto dropunlock; 82634f83c52SGeorge V. Neville-Neil } 827936cd18dSAndre Oppermann 828340c35deSJonathan Lemon /* 829252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 830252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 831252ca428SRobert Watson * legitimate new connection attempt the old INPCB gets removed and 832252ca428SRobert Watson * we can try again to find a listening socket. 833252ca428SRobert Watson * 834252ca428SRobert Watson * At this point, due to earlier optimism, we may hold a read lock on 835252ca428SRobert Watson * the inpcbinfo, rather than a write lock. If so, we need to 836252ca428SRobert Watson * upgrade, or if that fails, acquire a reference on the inpcb, drop 837252ca428SRobert Watson * all locks, acquire a global write lock, and then re-acquire the 838252ca428SRobert Watson * inpcb lock. We may at that point discover that another thread has 839252ca428SRobert Watson * tried to free the inpcb, in which case we need to loop back and 840252ca428SRobert Watson * try to find a new inpcb to deliver to. 841340c35deSJonathan Lemon */ 842883e9bc4SRobert Watson relocked: 843ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 844252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 845252ca428SRobert Watson ("%s: INP_TIMEWAIT ti_locked %d", __func__, ti_locked)); 846252ca428SRobert Watson 847252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 84866f80e90SRobert Watson if (INP_INFO_TRY_UPGRADE(&V_tcbinfo) == 0) { 849252ca428SRobert Watson in_pcbref(inp); 850252ca428SRobert Watson INP_WUNLOCK(inp); 851252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 852252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 853252ca428SRobert Watson ti_locked = TI_WLOCKED; 854252ca428SRobert Watson INP_WLOCK(inp); 855252ca428SRobert Watson if (in_pcbrele(inp)) { 856252ca428SRobert Watson inp = NULL; 857252ca428SRobert Watson goto findpcb; 858252ca428SRobert Watson } 859f681a5fdSRobert Watson } else 860252ca428SRobert Watson ti_locked = TI_WLOCKED; 861252ca428SRobert Watson } 862252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 863252ca428SRobert Watson 864340c35deSJonathan Lemon if (thflags & TH_SYN) 865f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 8668d573cc1SAndre Oppermann /* 8678d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 8688d573cc1SAndre Oppermann */ 8692104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 870340c35deSJonathan Lemon goto findpcb; 871603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 872340c35deSJonathan Lemon return; 873340c35deSJonathan Lemon } 874794235b7SAndre Oppermann /* 875794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 876794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 877794235b7SAndre Oppermann * segment and send an appropriate response. 878794235b7SAndre Oppermann */ 879df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 880a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 881a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 882a57815efSBosko Milekic goto dropwithreset; 88309f81a46SBosko Milekic } 884df8bae1dSRodney W. Grimes 885252ca428SRobert Watson /* 886252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 887252ca428SRobert Watson * inpcbinfo write lock and have only a read lock. In this case, 888252ca428SRobert Watson * attempt to upgrade/relock using the same strategy as the TIMEWAIT 889883e9bc4SRobert Watson * case above. If we relock, we have to jump back to 'relocked' as 890883e9bc4SRobert Watson * the connection might now be in TIMEWAIT. 891252ca428SRobert Watson */ 892252ca428SRobert Watson if (tp->t_state != TCPS_ESTABLISHED || 893252ca428SRobert Watson (thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 894252ca428SRobert Watson tcp_read_locking == 0) { 895252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 896252ca428SRobert Watson ("%s: upgrade check ti_locked %d", __func__, ti_locked)); 897252ca428SRobert Watson 898252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 89966f80e90SRobert Watson if (INP_INFO_TRY_UPGRADE(&V_tcbinfo) == 0) { 900252ca428SRobert Watson in_pcbref(inp); 901252ca428SRobert Watson INP_WUNLOCK(inp); 902252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 903252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 904252ca428SRobert Watson ti_locked = TI_WLOCKED; 905252ca428SRobert Watson INP_WLOCK(inp); 906252ca428SRobert Watson if (in_pcbrele(inp)) { 907252ca428SRobert Watson inp = NULL; 908252ca428SRobert Watson goto findpcb; 909252ca428SRobert Watson } 910883e9bc4SRobert Watson goto relocked; 911f681a5fdSRobert Watson } else 912252ca428SRobert Watson ti_locked = TI_WLOCKED; 913252ca428SRobert Watson } 914252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 915252ca428SRobert Watson } 916252ca428SRobert Watson 917c488362eSRobert Watson #ifdef MAC 9188501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 91930d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 920302ce8d6SAndre Oppermann goto dropunlock; 921c488362eSRobert Watson #endif 922a557af22SRobert Watson so = inp->inp_socket; 923302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 924610ee2f9SDavid Greenman #ifdef TCPDEBUG 925df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 926df8bae1dSRodney W. Grimes ostate = tp->t_state; 927d30d90dcSMaxim Konovalov if (isipv6) { 92810fe523eSMaxim Konovalov #ifdef INET6 929540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 930d30d90dcSMaxim Konovalov #endif 931d30d90dcSMaxim Konovalov } else 932540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 933fb59c426SYoshinobu Inoue tcp_savetcp = *th; 934df8bae1dSRodney W. Grimes } 935610ee2f9SDavid Greenman #endif 936db33b3e6SAndre Oppermann /* 937db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 938db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 939db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 940db33b3e6SAndre Oppermann */ 941540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 942540e8b7eSJeffrey Hsu struct in_conninfo inc; 943540e8b7eSJeffrey Hsu 9447824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 9457824d002SAndre Oppermann "tp not listening", __func__)); 9467824d002SAndre Oppermann 9478bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 948302ce8d6SAndre Oppermann #ifdef INET6 949be2ac88cSJonathan Lemon if (isipv6) { 950dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 951be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 952be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 953302ce8d6SAndre Oppermann } else 954302ce8d6SAndre Oppermann #endif 955302ce8d6SAndre Oppermann { 956be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 957be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 958be2ac88cSJonathan Lemon } 959be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 960be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 9617973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 962be2ac88cSJonathan Lemon 963fb59c426SYoshinobu Inoue /* 9648d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 9658d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 9668d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 967fb59c426SYoshinobu Inoue */ 968be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 969bf6d304aSAndre Oppermann /* 970bf6d304aSAndre Oppermann * Parse the TCP options here because 971bf6d304aSAndre Oppermann * syncookies need access to the reflected 972bf6d304aSAndre Oppermann * timestamp. 973bf6d304aSAndre Oppermann */ 974bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 9759fa198beSAndre Oppermann /* 9769fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 9779fa198beSAndre Oppermann * inp and tcpinfo locks. 9789fa198beSAndre Oppermann */ 979bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 9804195b4afSPaul Traina /* 981e207f800SAndre Oppermann * No syncache entry or ACK was not 982be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 9838d573cc1SAndre Oppermann * NB: syncache did its own logging 9848d573cc1SAndre Oppermann * of the failure cause. 9854195b4afSPaul Traina */ 986be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 987be2ac88cSJonathan Lemon goto dropwithreset; 988be2ac88cSJonathan Lemon } 989f76fcf6dSJeffrey Hsu if (so == NULL) { 990be2ac88cSJonathan Lemon /* 991e207f800SAndre Oppermann * We completed the 3-way handshake 992e207f800SAndre Oppermann * but could not allocate a socket 993e207f800SAndre Oppermann * either due to memory shortage, 994e207f800SAndre Oppermann * listen queue length limits or 995a160e630SAndre Oppermann * global socket limits. Send RST 996a160e630SAndre Oppermann * or wait and have the remote end 997a160e630SAndre Oppermann * retransmit the ACK for another 998a160e630SAndre Oppermann * try. 999be2ac88cSJonathan Lemon */ 1000a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1001a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10028d573cc1SAndre Oppermann "Socket allocation failed due to " 10038d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1004ac957cd2SJulian Elischer s, __func__, 1005ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1006ac957cd2SJulian Elischer "sending RST" : "try again"); 1007603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1008e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1009e207f800SAndre Oppermann goto dropwithreset; 1010a160e630SAndre Oppermann } else 1011a160e630SAndre Oppermann goto dropunlock; 1012f76fcf6dSJeffrey Hsu } 1013be2ac88cSJonathan Lemon /* 1014be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 10158d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 10168d573cc1SAndre Oppermann * created socket and update the tp variable. 1017be2ac88cSJonathan Lemon */ 10188501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1019be2ac88cSJonathan Lemon inp = sotoinpcb(so); 10208501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 1021be2ac88cSJonathan Lemon tp = intotcpcb(inp); 10228d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 10238d573cc1SAndre Oppermann ("%s: ", __func__)); 1024be2ac88cSJonathan Lemon /* 1025302ce8d6SAndre Oppermann * Process the segment and the data it 1026302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1027302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1028302ce8d6SAndre Oppermann */ 1029f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1030252ca428SRobert Watson iptos, ti_locked); 1031603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1032302ce8d6SAndre Oppermann return; 1033be2ac88cSJonathan Lemon } 1034a160e630SAndre Oppermann /* 10358d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 10368d573cc1SAndre Oppermann * 1037a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1038a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1039a160e630SAndre Oppermann * retransmits. 104019bc77c5SAndre Oppermann * 104119bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 104219bc77c5SAndre Oppermann * causes. 1043a160e630SAndre Oppermann */ 1044a160e630SAndre Oppermann if (thflags & TH_RST) { 104519bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1046a160e630SAndre Oppermann goto dropunlock; 1047a160e630SAndre Oppermann } 1048a160e630SAndre Oppermann /* 1049a160e630SAndre Oppermann * We can't do anything without SYN. 1050a160e630SAndre Oppermann */ 1051a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1052a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1053a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1054773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 10558d573cc1SAndre Oppermann s, __func__); 105678b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1057a160e630SAndre Oppermann goto dropunlock; 1058a160e630SAndre Oppermann } 1059a160e630SAndre Oppermann /* 1060a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1061a160e630SAndre Oppermann */ 1062fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1063a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1064a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10658d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 10668d573cc1SAndre Oppermann s, __func__); 1067a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 106878b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1069a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1070a57815efSBosko Milekic goto dropwithreset; 10714195b4afSPaul Traina } 1072a160e630SAndre Oppermann /* 1073a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1074a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1075a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1076a160e630SAndre Oppermann * TCP/IP stack. 1077a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1078a160e630SAndre Oppermann * and is constantly refining its stack detection 1079a160e630SAndre Oppermann * strategies. 10808d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1081a160e630SAndre Oppermann * and was used by RFC1644. 1082a160e630SAndre Oppermann */ 1083603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1084a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1085a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1086773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 10878d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 108878b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1089302ce8d6SAndre Oppermann goto dropunlock; 1090ebb0cbeaSPaul Traina } 1091be2ac88cSJonathan Lemon /* 1092be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1093a160e630SAndre Oppermann * 1094a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1095a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1096a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1097be2ac88cSJonathan Lemon */ 1098a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1099a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1100a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1101a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 110233841545SHajimu UMEMOTO #ifdef INET6 110333841545SHajimu UMEMOTO /* 110433841545SHajimu UMEMOTO * If deprecated address is forbidden, 110533841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 110633841545SHajimu UMEMOTO * address to prevent any new inbound connection from 110733841545SHajimu UMEMOTO * getting established. 110833841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 110933841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 111033841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 111133841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 111233841545SHajimu UMEMOTO * for the exchange. 111333841545SHajimu UMEMOTO * 111433841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 111533841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 111633841545SHajimu UMEMOTO * SYN in this case. 111733841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 111833841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 111933841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 112033841545SHajimu UMEMOTO * used" 112133841545SHajimu UMEMOTO * 2. use of it with new communication: 112233841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 112333841545SHajimu UMEMOTO * with sufficient scope is available" 112433841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 112533841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 112633841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 112733841545SHajimu UMEMOTO * 112833841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 112933841545SHajimu UMEMOTO * multiple description text for deprecated address 113033841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 113133841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 113233841545SHajimu UMEMOTO */ 1133603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 113433841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 113533841545SHajimu UMEMOTO 11368c0fec80SRobert Watson ia6 = ip6_getdstifaddr(m); 11378c0fec80SRobert Watson if (ia6 != NULL && 113833841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 11398c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1140a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1141a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11428d573cc1SAndre Oppermann "Connection attempt to deprecated " 11438d573cc1SAndre Oppermann "IPv6 address rejected\n", 1144a160e630SAndre Oppermann s, __func__); 114533841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 114633841545SHajimu UMEMOTO goto dropwithreset; 114733841545SHajimu UMEMOTO } 11488c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 114933841545SHajimu UMEMOTO } 115033841545SHajimu UMEMOTO #endif 115165f28919SJesper Skriver /* 1152db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 11538d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1154db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 11558d573cc1SAndre Oppermann * If it is from this socket it must be forged. 11568d573cc1SAndre Oppermann * Don't respond if the source or destination is a 11578d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 115893ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 115993ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 116093ec91baSCrist J. Clark * in_broadcast() to find them. 1161be2ac88cSJonathan Lemon */ 11628d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 11638d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 11648d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11658d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1166773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1167302ce8d6SAndre Oppermann goto dropunlock; 11688d573cc1SAndre Oppermann } 1169be2ac88cSJonathan Lemon if (isipv6) { 1170db33b3e6SAndre Oppermann #ifdef INET6 1171db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1172a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1173a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1174a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11758d573cc1SAndre Oppermann "Connection attempt to/from self " 1176773673c1SAndre Oppermann "ignored\n", s, __func__); 1177302ce8d6SAndre Oppermann goto dropunlock; 1178a160e630SAndre Oppermann } 1179be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1180a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1181a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1182a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11838d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1184773673c1SAndre Oppermann "address ignored\n", s, __func__); 1185302ce8d6SAndre Oppermann goto dropunlock; 1186a160e630SAndre Oppermann } 1187db33b3e6SAndre Oppermann #endif 1188c068736aSJeffrey Hsu } else { 1189db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1190a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1191a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1192a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11938d573cc1SAndre Oppermann "Connection attempt from/to self " 1194773673c1SAndre Oppermann "ignored\n", s, __func__); 1195302ce8d6SAndre Oppermann goto dropunlock; 1196a160e630SAndre Oppermann } 1197be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1198be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 11992ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1200a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1201a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1202a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12038d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1204773673c1SAndre Oppermann "or multicast address ignored\n", 1205a160e630SAndre Oppermann s, __func__); 1206302ce8d6SAndre Oppermann goto dropunlock; 1207c068736aSJeffrey Hsu } 1208a160e630SAndre Oppermann } 1209be2ac88cSJonathan Lemon /* 1210db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1211db33b3e6SAndre Oppermann * for syncache. 1212be2ac88cSJonathan Lemon */ 12133c653157SHartmut Brandt #ifdef TCPDEBUG 12143c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12153c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12163c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12173c653157SHartmut Brandt #endif 1218f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 12194d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 1220be2ac88cSJonathan Lemon /* 12214d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1222a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1223be2ac88cSJonathan Lemon */ 1224603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1225be2ac88cSJonathan Lemon return; 1226f76fcf6dSJeffrey Hsu } 1227db33b3e6SAndre Oppermann 1228302ce8d6SAndre Oppermann /* 12298d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 12308d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 12318d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1232302ce8d6SAndre Oppermann */ 1233252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1234603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1235302ce8d6SAndre Oppermann return; 1236be2ac88cSJonathan Lemon 1237302ce8d6SAndre Oppermann dropwithreset: 1238252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1239252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1240252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1241dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1242252ca428SRobert Watson else 1243252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 1244252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1245014ea782SRobert Watson 1246014ea782SRobert Watson if (inp != NULL) { 1247302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1248014ea782SRobert Watson INP_WUNLOCK(inp); 1249dd8ac7f9SRobert Watson } else 1250014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1251302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1252014ea782SRobert Watson goto drop; 1253014ea782SRobert Watson 1254302ce8d6SAndre Oppermann dropunlock: 1255252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1256252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1257252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1258252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1259252ca428SRobert Watson else 1260252ca428SRobert Watson panic("%s: dropunlock ti_locked %d", __func__, ti_locked); 1261252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1262252ca428SRobert Watson 12639fa198beSAndre Oppermann if (inp != NULL) 12648501a69cSRobert Watson INP_WUNLOCK(inp); 1265014ea782SRobert Watson 1266302ce8d6SAndre Oppermann drop: 1267603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1268a160e630SAndre Oppermann if (s != NULL) 1269a160e630SAndre Oppermann free(s, M_TCPLOG); 1270302ce8d6SAndre Oppermann if (m != NULL) 1271302ce8d6SAndre Oppermann m_freem(m); 1272302ce8d6SAndre Oppermann } 1273302ce8d6SAndre Oppermann 1274679d9708SAndre Oppermann static void 1275302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1276252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1277252ca428SRobert Watson int ti_locked) 1278302ce8d6SAndre Oppermann { 1279302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1280302ce8d6SAndre Oppermann int rstreason, todrop, win; 1281302ce8d6SAndre Oppermann u_long tiwin; 1282302ce8d6SAndre Oppermann struct tcpopt to; 1283302ce8d6SAndre Oppermann 128414739780SMaxim Konovalov #ifdef TCPDEBUG 128514739780SMaxim Konovalov /* 128614739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 128714739780SMaxim Konovalov * now IPv6. 128814739780SMaxim Konovalov */ 128914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 129014739780SMaxim Konovalov struct tcphdr tcp_savetcp; 129114739780SMaxim Konovalov short ostate = 0; 129214739780SMaxim Konovalov #endif 1293302ce8d6SAndre Oppermann thflags = th->th_flags; 1294d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1295302ce8d6SAndre Oppermann 1296252ca428SRobert Watson /* 1297252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1298252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 1299252ca428SRobert Watson * allow either a read lock or a write lock, as we may have acquired 1300252ca428SRobert Watson * a write lock due to a race. 1301252ca428SRobert Watson * 1302d15fb965SRobert Watson * Require a global write lock for SYN/FIN/RST segments or 1303252ca428SRobert Watson * non-established connections; otherwise accept either a read or 1304252ca428SRobert Watson * write lock, as we may have conservatively acquired a write lock in 1305252ca428SRobert Watson * certain cases in tcp_input() (is this still true?). Currently we 1306252ca428SRobert Watson * will never enter with no lock, so we try to drop it quickly in the 1307252ca428SRobert Watson * common pure ack/pure data cases. 1308252ca428SRobert Watson */ 1309252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1310252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1311252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1312252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1313603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1314252ca428SRobert Watson } else { 1315252ca428SRobert Watson #ifdef INVARIANTS 1316252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1317252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1318252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1319252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1320252ca428SRobert Watson else 1321252ca428SRobert Watson panic("%s: ti_locked %d for EST", __func__, 1322252ca428SRobert Watson ti_locked); 1323252ca428SRobert Watson #endif 1324252ca428SRobert Watson } 13258501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1326679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1327679d9708SAndre Oppermann __func__)); 1328679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1329679d9708SAndre Oppermann __func__)); 1330df8bae1dSRodney W. Grimes 1331df8bae1dSRodney W. Grimes /* 1332df8bae1dSRodney W. Grimes * Segment received on connection. 1333df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1334e8949f74SAndre Oppermann * XXX: This should be done after segment 1335e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1336df8bae1dSRodney W. Grimes */ 13379b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 13387ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1339b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1340df8bae1dSRodney W. Grimes 1341df8bae1dSRodney W. Grimes /* 1342464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1343e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1344464fcfbcSAndre Oppermann */ 1345464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1346464fcfbcSAndre Oppermann 1347464fcfbcSAndre Oppermann /* 1348f2512ba1SRui Paulo * TCP ECN processing. 1349f2512ba1SRui Paulo */ 1350f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 13519c251892SRui Paulo if (thflags & TH_CWR) 13529c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1353f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1354f2512ba1SRui Paulo case IPTOS_ECN_CE: 1355f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 135678b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1357f2512ba1SRui Paulo break; 1358f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 135978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1360f2512ba1SRui Paulo break; 1361f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 136278b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1363f2512ba1SRui Paulo break; 1364f2512ba1SRui Paulo } 1365dbc42409SLawrence Stewart /* Congestion experienced. */ 1366dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1367dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1368f2512ba1SRui Paulo } 1369f2512ba1SRui Paulo } 1370f2512ba1SRui Paulo 1371f2512ba1SRui Paulo /* 1372f72167f4SAndre Oppermann * Parse options on any incoming segment. 1373f72167f4SAndre Oppermann */ 1374302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1375302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1376302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1377f72167f4SAndre Oppermann 1378f72167f4SAndre Oppermann /* 1379f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1380bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1381bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1382bf6d304aSAndre Oppermann * was established. 1383f72167f4SAndre Oppermann */ 1384bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1385e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1386bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1387f72167f4SAndre Oppermann to.to_tsecr = 0; 1388bf6d304aSAndre Oppermann } 1389f72167f4SAndre Oppermann 1390f72167f4SAndre Oppermann /* 139197d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 139297d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 13935396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 13945396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 139597d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1396df8bae1dSRodney W. Grimes */ 13970a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1398464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1399464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1400be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 140102a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1402be2ac88cSJonathan Lemon } 14035396d0f8SAndre Oppermann /* 14045396d0f8SAndre Oppermann * Initial send window. It will be updated with 14055396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 14065396d0f8SAndre Oppermann */ 14075396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1408be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1409be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1410be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1411be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1412be2ac88cSJonathan Lemon } 1413be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1414be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 14153529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 14163529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 14173529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 14186d90faf3SPaul Saab } 14196d90faf3SPaul Saab 1420df8bae1dSRodney W. Grimes /* 1421df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1422df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1423df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1424df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1425df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1426df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1427df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1428df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1429df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1430df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1431df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1432df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1433a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1434c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1435a0292f23SGarrett Wollman * be TH_NEEDSYN. 1436df8bae1dSRodney W. Grimes */ 1437df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1438c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1439fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1440c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1441c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1442a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1443c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1444be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1445c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1446df8bae1dSRodney W. Grimes 1447df8bae1dSRodney W. Grimes /* 1448df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1449df8bae1dSRodney W. Grimes * record the timestamp. 1450a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1451a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1452df8bae1dSRodney W. Grimes */ 1453be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1454fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 14559b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1456a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1457df8bae1dSRodney W. Grimes } 1458df8bae1dSRodney W. Grimes 1459fb59c426SYoshinobu Inoue if (tlen == 0) { 1460fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1461fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1462dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1463fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1464dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1465df8bae1dSRodney W. Grimes /* 1466e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1467df8bae1dSRodney W. Grimes */ 1468252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1469252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1470252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1471252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1472252ca428SRobert Watson else 1473252ca428SRobert Watson panic("%s: ti_locked %d on pure ACK", 1474252ca428SRobert Watson __func__, ti_locked); 1475252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1476252ca428SRobert Watson 147778b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1478252ca428SRobert Watson 14799b8b58e0SJonathan Lemon /* 1480e8949f74SAndre Oppermann * "bad retransmit" recovery. 14819b8b58e0SJonathan Lemon */ 14829b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 14836dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1484dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 14859b8b58e0SJonathan Lemon } 1486fa55172bSMatthew Dillon 1487fa55172bSMatthew Dillon /* 1488fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1489fa55172bSMatthew Dillon * 1490fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1491fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1492fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1493fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1494fa55172bSMatthew Dillon */ 1495fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1496fa55172bSMatthew Dillon to.to_tsecr) { 1497eaf80179SAndre Oppermann if (!tp->t_rttlow || 1498eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1499eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1500a0292f23SGarrett Wollman tcp_xmit_timer(tp, 15019b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1502fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1503fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1504eaf80179SAndre Oppermann if (!tp->t_rttlow || 1505eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1506eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1507c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1508c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1509fa55172bSMatthew Dillon } 1510dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 151139bc9de5SLawrence Stewart 151239bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 151339bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 151439bc9de5SLawrence Stewart 151578b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 151678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1517df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 15189d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 15199d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 15209d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1521dbc42409SLawrence Stewart 1522dbc42409SLawrence Stewart /* 1523dbc42409SLawrence Stewart * Let the congestion control algorithm update 1524dbc42409SLawrence Stewart * congestion control related information. This 1525dbc42409SLawrence Stewart * typically means increasing the congestion 1526dbc42409SLawrence Stewart * window. 1527dbc42409SLawrence Stewart */ 1528dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 1529dbc42409SLawrence Stewart 15309d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 15311645d090SJeff Roberson /* 1532e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 15331645d090SJeff Roberson * to th_ack. 15341645d090SJeff Roberson */ 1535cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1536b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1537df8bae1dSRodney W. Grimes m_freem(m); 1538e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1539df8bae1dSRodney W. Grimes 1540df8bae1dSRodney W. Grimes /* 1541df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1542df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1543df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1544df8bae1dSRodney W. Grimes * If process is waiting for space, 1545df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1546df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1547df8bae1dSRodney W. Grimes * decide between more output or persist. 1548e8949f74SAndre Oppermann */ 15493c653157SHartmut Brandt #ifdef TCPDEBUG 15503c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 15513c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 15523c653157SHartmut Brandt (void *)tcp_saveipgen, 15533c653157SHartmut Brandt &tcp_savetcp, 0); 15543c653157SHartmut Brandt #endif 1555df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1556b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1557b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1558b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1559b8152ba7SAndre Oppermann tp->t_rxtcur); 1560df8bae1dSRodney W. Grimes sowwakeup(so); 1561df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1562df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1563a14c749fSJonathan Lemon goto check_delack; 1564df8bae1dSRodney W. Grimes } 1565fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1566fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 15676741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 15686741ecf5SAndre Oppermann 1569df8bae1dSRodney W. Grimes /* 1570252ca428SRobert Watson * This is a pure, in-sequence data packet with 1571252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1572252ca428SRobert Watson * buffer space to take it. 1573df8bae1dSRodney W. Grimes */ 1574252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1575252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1576252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1577252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1578252ca428SRobert Watson else 1579252ca428SRobert Watson panic("%s: ti_locked %d on pure data " 1580252ca428SRobert Watson "segment", __func__, ti_locked); 1581252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1582252ca428SRobert Watson 15836d90faf3SPaul Saab /* Clean receiver SACK report if present */ 15843529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 15856d90faf3SPaul Saab tcp_clean_sackreport(tp); 158678b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1587fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 15881645d090SJeff Roberson /* 15891645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 15901645d090SJeff Roberson * th_seq. 15911645d090SJeff Roberson */ 15921645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 15931645d090SJeff Roberson /* 15941645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 15951645d090SJeff Roberson * rcv_nxt. 15961645d090SJeff Roberson */ 15971645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 159878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 159978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 1600e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 16013c653157SHartmut Brandt #ifdef TCPDEBUG 16023c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 16033c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 16043c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 16053c653157SHartmut Brandt #endif 16066741ecf5SAndre Oppermann /* 16076741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 16086741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 16096741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 16106741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 16116741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 16126741ecf5SAndre Oppermann * 16136741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 16146741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 16156741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 16166741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 16176741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 16186741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 16196741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 16206741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 16216741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 16226741ecf5SAndre Oppermann * reassembly queue. 16236741ecf5SAndre Oppermann * 16246741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 16256741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 16266741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 16276741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 16286741ecf5SAndre Oppermann * current socket buffer size; 16296741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 16306741ecf5SAndre Oppermann * 16316741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 16326741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 16336741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 16346741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 16356741ecf5SAndre Oppermann * 16366741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 16376741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1638df8bae1dSRodney W. Grimes */ 1639603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 16406741ecf5SAndre Oppermann to.to_tsecr && 16416741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 16428502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 16436741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 16446741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 16456741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 16466741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1647603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 16486741ecf5SAndre Oppermann newsize = 16496741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1650603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1651603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 16526741ecf5SAndre Oppermann } 16536741ecf5SAndre Oppermann /* Start over with next RTT. */ 16546741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 16556741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 16566741ecf5SAndre Oppermann } else 16576741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 16586741ecf5SAndre Oppermann } 16596741ecf5SAndre Oppermann 16606741ecf5SAndre Oppermann /* Add data to socket buffer. */ 16611e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1662c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1663c1c36a2cSMike Silbersack m_freem(m); 1664c1c36a2cSMike Silbersack } else { 16656741ecf5SAndre Oppermann /* 16666741ecf5SAndre Oppermann * Set new socket buffer size. 16676741ecf5SAndre Oppermann * Give up when limit is reached. 16686741ecf5SAndre Oppermann */ 16696741ecf5SAndre Oppermann if (newsize) 16706741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 16716c8286e4SRobert Watson newsize, so, NULL)) 16726741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1673fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 16741e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1675c1c36a2cSMike Silbersack } 1676e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 16771e4d7da7SRobert Watson sorwakeup_locked(so); 1678d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 16793bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1680f498eeeeSDavid Greenman } else { 1681e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1682e612a582SDavid Greenman tcp_output(tp); 1683e612a582SDavid Greenman } 1684a14c749fSJonathan Lemon goto check_delack; 1685df8bae1dSRodney W. Grimes } 1686df8bae1dSRodney W. Grimes } 1687df8bae1dSRodney W. Grimes 1688df8bae1dSRodney W. Grimes /* 1689df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1690df8bae1dSRodney W. Grimes * and then do TCP input processing. 1691df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1692df8bae1dSRodney W. Grimes * but not less than advertised window. 1693df8bae1dSRodney W. Grimes */ 1694df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1695df8bae1dSRodney W. Grimes if (win < 0) 1696df8bae1dSRodney W. Grimes win = 0; 169766e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1698df8bae1dSRodney W. Grimes 16996741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 17006741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 17016741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 17026741ecf5SAndre Oppermann 1703df8bae1dSRodney W. Grimes switch (tp->t_state) { 1704df8bae1dSRodney W. Grimes 1705df8bae1dSRodney W. Grimes /* 1706764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1707764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1708764d8cefSBill Fenner */ 1709764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1710fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1711fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1712a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1713a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1714a57815efSBosko Milekic goto dropwithreset; 1715a57815efSBosko Milekic } 1716764d8cefSBill Fenner break; 1717764d8cefSBill Fenner 1718764d8cefSBill Fenner /* 1719df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1720df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1721df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1722df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1723df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1724df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1725df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1726f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1727f2512ba1SRui Paulo * is ECN capable. 1728df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1729df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1730df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1731df8bae1dSRodney W. Grimes */ 1732df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1733fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1734fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1735fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1736a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1737a0292f23SGarrett Wollman goto dropwithreset; 1738a0292f23SGarrett Wollman } 17390ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1740df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 17410ca3f933SAndre Oppermann if (thflags & TH_RST) 1742df8bae1dSRodney W. Grimes goto drop; 17430ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1744df8bae1dSRodney W. Grimes goto drop; 1745464fcfbcSAndre Oppermann 1746fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1747df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1748fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 174978b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1750845799c1SAndras Olah soisconnected(so); 1751c488362eSRobert Watson #ifdef MAC 175230d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1753c488362eSRobert Watson #endif 1754845799c1SAndras Olah /* Do window scaling on this connection? */ 1755845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1756845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1757845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1758845799c1SAndras Olah } 1759766282cbSJohn Baldwin tp->rcv_adv += imin(tp->rcv_wnd, 1760766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1761a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1762a0292f23SGarrett Wollman /* 1763a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1764a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1765a0292f23SGarrett Wollman */ 1766d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1767b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1768b8152ba7SAndre Oppermann tcp_delacktime); 1769a0292f23SGarrett Wollman else 1770a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1771f2512ba1SRui Paulo 1772603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1773f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 177478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1775f2512ba1SRui Paulo } 1776f2512ba1SRui Paulo 1777a0292f23SGarrett Wollman /* 1778a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1779a0292f23SGarrett Wollman * Transitions: 1780a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1781a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1782a0292f23SGarrett Wollman */ 17839b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1784a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1785a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1786a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1787fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 17887ff19458SPaul Traina } else { 1789a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1790dbc42409SLawrence Stewart cc_conn_init(tp); 1791b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 17927ff19458SPaul Traina } 1793a0292f23SGarrett Wollman } else { 1794a0292f23SGarrett Wollman /* 1795c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1796c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1797c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1798c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1799c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1800a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1801a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1802a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1803a0292f23SGarrett Wollman */ 18044b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1805b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1806df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1807a0292f23SGarrett Wollman } 1808df8bae1dSRodney W. Grimes 1809252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1810252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1811252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 18128501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 18137cfc6904SRobert Watson 1814df8bae1dSRodney W. Grimes /* 1815fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1816df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1817df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1818df8bae1dSRodney W. Grimes */ 1819fb59c426SYoshinobu Inoue th->th_seq++; 1820fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1821fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1822df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1823fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1824fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 182578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 182678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1827df8bae1dSRodney W. Grimes } 1828fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1829fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1830a0292f23SGarrett Wollman /* 1831a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1832a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1833a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1834a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1835a0292f23SGarrett Wollman * Otherwise, goto step 6. 1836a0292f23SGarrett Wollman */ 1837fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1838a0292f23SGarrett Wollman goto process_ACK; 1839c068736aSJeffrey Hsu 1840df8bae1dSRodney W. Grimes goto step6; 1841c068736aSJeffrey Hsu 1842a0292f23SGarrett Wollman /* 1843a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1844c94c54e4SAndre Oppermann * do normal processing. 1845a0292f23SGarrett Wollman * 1846c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1847a0292f23SGarrett Wollman */ 1848a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1849a0292f23SGarrett Wollman case TCPS_CLOSING: 1850a0292f23SGarrett Wollman break; /* continue normal processing */ 1851df8bae1dSRodney W. Grimes } 1852df8bae1dSRodney W. Grimes 1853df8bae1dSRodney W. Grimes /* 1854df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 185580ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 185680ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 185780ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 185880ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 185980ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 186080ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 186180ab7c0eSGarrett Wollman * killing a connection). 186280ab7c0eSGarrett Wollman * Then check timestamp, if present. 1863a0292f23SGarrett Wollman * Then check the connection count, if present. 1864df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1865df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1866df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1867df8bae1dSRodney W. Grimes * 186880ab7c0eSGarrett Wollman * 186980ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 187080ab7c0eSGarrett Wollman * if this is a valid reset segment. 187180ab7c0eSGarrett Wollman * RFC 793 page 37: 187280ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 187380ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 187480ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 187580ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 187680ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 18771a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 18781a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 18791a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 18801a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 188180dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 188280dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 188380dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 188480dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 188580dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 188680dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 18878e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 188880ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 188980ab7c0eSGarrett Wollman * but they will eventually catch up. 189080ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 189180ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 189280ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 189380ab7c0eSGarrett Wollman * 189480ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 189580ab7c0eSGarrett Wollman * 189680ab7c0eSGarrett Wollman * DISCUSSION 189780ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 189880ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 189980ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 190080ab7c0eSGarrett Wollman * data. 190180ab7c0eSGarrett Wollman * 190280ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 190380ab7c0eSGarrett Wollman * the state: 190480ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 190580ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 190680ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1907745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 190880ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1909e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 191080ab7c0eSGarrett Wollman * Close the tcb. 1911e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 191280ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 191380ab7c0eSGarrett Wollman * RFC 1337. 191480ab7c0eSGarrett Wollman */ 1915fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 191695ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 191795ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 191880ab7c0eSGarrett Wollman switch (tp->t_state) { 191980ab7c0eSGarrett Wollman 192080ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 192180ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 192280ab7c0eSGarrett Wollman goto close; 192380ab7c0eSGarrett Wollman 192480ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 1925603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 192695ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 192795ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 192895ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 192995ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 193078b50714SRobert Watson TCPSTAT_INC(tcps_badrst); 193180dd2a81SMike Silbersack goto drop; 193280dd2a81SMike Silbersack } 1933564aab1fSAndre Oppermann /* FALLTHROUGH */ 193480ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 193580ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 193680ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 193780ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 193880ab7c0eSGarrett Wollman close: 1939252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1940252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 1941252ca428SRobert Watson ti_locked)); 1942252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1943252ca428SRobert Watson 194480ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 194578b50714SRobert Watson TCPSTAT_INC(tcps_drops); 194680ab7c0eSGarrett Wollman tp = tcp_close(tp); 194780ab7c0eSGarrett Wollman break; 194880ab7c0eSGarrett Wollman 194980ab7c0eSGarrett Wollman case TCPS_CLOSING: 195080ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1951252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1952252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 1953252ca428SRobert Watson ti_locked)); 1954252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1955252ca428SRobert Watson 195680ab7c0eSGarrett Wollman tp = tcp_close(tp); 195780ab7c0eSGarrett Wollman break; 195880ab7c0eSGarrett Wollman } 195980ab7c0eSGarrett Wollman } 196080ab7c0eSGarrett Wollman goto drop; 196180ab7c0eSGarrett Wollman } 196280ab7c0eSGarrett Wollman 196380ab7c0eSGarrett Wollman /* 1964df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1965df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1966df8bae1dSRodney W. Grimes */ 1967be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 196880ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1969df8bae1dSRodney W. Grimes 1970df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 19711a0e7cfcSJohn Baldwin if (ticks - tp->ts_recent_age > TCP_PAWS_IDLE) { 1972df8bae1dSRodney W. Grimes /* 1973df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1974df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1975df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1976df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1977df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1978df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1979df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1980df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1981df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1982df8bae1dSRodney W. Grimes */ 1983df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1984df8bae1dSRodney W. Grimes } else { 198578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 198678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 198778b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 198807fd333dSMatthew Dillon if (tlen) 1989df8bae1dSRodney W. Grimes goto dropafterack; 19901ab4789dSMatthew Dillon goto drop; 19911ab4789dSMatthew Dillon } 1992df8bae1dSRodney W. Grimes } 1993df8bae1dSRodney W. Grimes 1994a0292f23SGarrett Wollman /* 199580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 199680ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 199780ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 199880ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 199980ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 200080ab7c0eSGarrett Wollman */ 2001a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2002a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2003a57815efSBosko Milekic goto dropwithreset; 2004a57815efSBosko Milekic } 200580ab7c0eSGarrett Wollman 2006fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2007df8bae1dSRodney W. Grimes if (todrop > 0) { 200881ad7eb0SZachary Loafman /* 200981ad7eb0SZachary Loafman * If this is a duplicate SYN for our current connection, 201081ad7eb0SZachary Loafman * advance over it and pretend and it's not a SYN. 201181ad7eb0SZachary Loafman */ 201281ad7eb0SZachary Loafman if (thflags & TH_SYN && th->th_seq == tp->irs) { 2013fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2014fb59c426SYoshinobu Inoue th->th_seq++; 2015fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2016fb59c426SYoshinobu Inoue th->th_urp--; 2017df8bae1dSRodney W. Grimes else 2018fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2019df8bae1dSRodney W. Grimes todrop--; 2020df8bae1dSRodney W. Grimes } 2021df8bae1dSRodney W. Grimes /* 2022dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2023df8bae1dSRodney W. Grimes */ 2024fb59c426SYoshinobu Inoue if (todrop > tlen 2025fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2026dac20301SGarrett Wollman /* 2027dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2028dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2029dac20301SGarrett Wollman * of sequence; drop it. 2030dac20301SGarrett Wollman */ 2031fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2032dac20301SGarrett Wollman 2033df8bae1dSRodney W. Grimes /* 2034dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2035dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2036df8bae1dSRodney W. Grimes */ 2037dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2038fb59c426SYoshinobu Inoue todrop = tlen; 203978b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 204078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2041df8bae1dSRodney W. Grimes } else { 204278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 204378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2044df8bae1dSRodney W. Grimes } 2045fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2046fb59c426SYoshinobu Inoue th->th_seq += todrop; 2047fb59c426SYoshinobu Inoue tlen -= todrop; 2048fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2049fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2050df8bae1dSRodney W. Grimes else { 2051fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2052fb59c426SYoshinobu Inoue th->th_urp = 0; 2053df8bae1dSRodney W. Grimes } 2054df8bae1dSRodney W. Grimes } 2055df8bae1dSRodney W. Grimes 2056df8bae1dSRodney W. Grimes /* 2057df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2058df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2059df8bae1dSRodney W. Grimes */ 2060df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2061fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2062773673c1SAndre Oppermann char *s; 2063773673c1SAndre Oppermann 2064252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 2065252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2066252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2067252ca428SRobert Watson 2068773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 2069e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 2070773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 2071e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2072773673c1SAndre Oppermann free(s, M_TCPLOG); 2073773673c1SAndre Oppermann } 2074df8bae1dSRodney W. Grimes tp = tcp_close(tp); 207578b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2076a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2077df8bae1dSRodney W. Grimes goto dropwithreset; 2078df8bae1dSRodney W. Grimes } 2079df8bae1dSRodney W. Grimes 2080df8bae1dSRodney W. Grimes /* 2081df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2082df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2083df8bae1dSRodney W. Grimes */ 2084fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2085df8bae1dSRodney W. Grimes if (todrop > 0) { 208678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2087fb59c426SYoshinobu Inoue if (todrop >= tlen) { 208878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2089df8bae1dSRodney W. Grimes /* 2090df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2091df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2092df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2093df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2094df8bae1dSRodney W. Grimes * and ack. 2095df8bae1dSRodney W. Grimes */ 2096fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2097df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 209878b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2099df8bae1dSRodney W. Grimes } else 2100df8bae1dSRodney W. Grimes goto dropafterack; 2101df8bae1dSRodney W. Grimes } else 210278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2103df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2104fb59c426SYoshinobu Inoue tlen -= todrop; 2105fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2106df8bae1dSRodney W. Grimes } 2107df8bae1dSRodney W. Grimes 2108df8bae1dSRodney W. Grimes /* 2109df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2110df8bae1dSRodney W. Grimes * record its timestamp. 2111cf09195bSPaul Saab * NOTE: 2112cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2113a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2114cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2115cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2116cf09195bSPaul Saab * predicated on the sequence space of this segment. 2117cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2118cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2119cf09195bSPaul Saab * instead of RFC1323's 2120cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2121cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2122cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2123cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2124cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2125df8bae1dSRodney W. Grimes */ 2126be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2127cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2128cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2129cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 21309b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 2131a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2132df8bae1dSRodney W. Grimes } 2133df8bae1dSRodney W. Grimes 2134df8bae1dSRodney W. Grimes /* 2135df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 2136df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 2137df8bae1dSRodney W. Grimes */ 2138fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 2139252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2140252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2141252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2142252ca428SRobert Watson 2143df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 2144a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2145122aad88SAndre Oppermann goto drop; 2146df8bae1dSRodney W. Grimes } 2147df8bae1dSRodney W. Grimes 2148a0292f23SGarrett Wollman /* 2149a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2150a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2151a0292f23SGarrett Wollman * later processing; else drop segment and return. 2152a0292f23SGarrett Wollman */ 2153fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2154a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2155a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 2156a0292f23SGarrett Wollman goto step6; 21575e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 21585e1aa279SDavid Malone goto dropafterack; 2159a0292f23SGarrett Wollman else 2160a0292f23SGarrett Wollman goto drop; 2161a0292f23SGarrett Wollman } 2162df8bae1dSRodney W. Grimes 2163df8bae1dSRodney W. Grimes /* 2164df8bae1dSRodney W. Grimes * Ack processing. 2165df8bae1dSRodney W. Grimes */ 2166df8bae1dSRodney W. Grimes switch (tp->t_state) { 2167df8bae1dSRodney W. Grimes 2168df8bae1dSRodney W. Grimes /* 2169764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2170764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2171764d8cefSBill Fenner * The ACK was checked above. 2172df8bae1dSRodney W. Grimes */ 2173df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2174a0292f23SGarrett Wollman 217578b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2176df8bae1dSRodney W. Grimes soisconnected(so); 2177df8bae1dSRodney W. Grimes /* Do window scaling? */ 2178df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2179df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2180df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2181464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2182df8bae1dSRodney W. Grimes } 2183a0292f23SGarrett Wollman /* 2184a0292f23SGarrett Wollman * Make transitions: 2185a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2186a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2187a0292f23SGarrett Wollman */ 21889b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2189a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 2190a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 2191a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 21927ff19458SPaul Traina } else { 2193a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 2194dbc42409SLawrence Stewart cc_conn_init(tp); 2195b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 21967ff19458SPaul Traina } 2197a0292f23SGarrett Wollman /* 2198a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2199a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2200a0292f23SGarrett Wollman */ 2201fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2202fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2203a0292f23SGarrett Wollman (struct mbuf *)0); 2204fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 220593b0017fSPhilippe Charnier /* FALLTHROUGH */ 2206df8bae1dSRodney W. Grimes 2207df8bae1dSRodney W. Grimes /* 2208df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2209df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2210fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2211fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2212df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2213df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2214df8bae1dSRodney W. Grimes */ 2215df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2216df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2217df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2218df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2219df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2220df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 22215a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 222278b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 22235a53ca16SPaul Saab goto dropafterack; 22245a53ca16SPaul Saab } 22253529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2226fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2227fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 22285a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 222939bc9de5SLawrence Stewart 223039bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 223139bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 223239bc9de5SLawrence Stewart 2233fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2234fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 223578b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2236df8bae1dSRodney W. Grimes /* 2237df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2238df8bae1dSRodney W. Grimes * a window probe), this is a completely 2239df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2240df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2241df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2242df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2243df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2244df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2245df8bae1dSRodney W. Grimes * window so we send only this one 2246df8bae1dSRodney W. Grimes * packet. 2247df8bae1dSRodney W. Grimes * 2248df8bae1dSRodney W. Grimes * We know we're losing at the current 2249df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2250df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2251df8bae1dSRodney W. Grimes * and pull our congestion window back to 2252df8bae1dSRodney W. Grimes * the new ssthresh). 2253df8bae1dSRodney W. Grimes * 2254df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2255df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2256df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2257df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2258df8bae1dSRodney W. Grimes * network. 2259f2512ba1SRui Paulo * 2260f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2261f2512ba1SRui Paulo * we reduced the cwnd. 2262df8bae1dSRodney W. Grimes */ 2263b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2264fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2265df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2266cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2267dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2268dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 22693529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2270dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2271808f11b7SPaul Saab int awnd; 227225e6f9edSPaul Saab 227325e6f9edSPaul Saab /* 227425e6f9edSPaul Saab * Compute the amount of data in flight first. 227525e6f9edSPaul Saab * We can inject new data into the pipe iff 227625e6f9edSPaul Saab * we have less than 1/2 the original window's 227725e6f9edSPaul Saab * worth of data in flight. 227825e6f9edSPaul Saab */ 2279808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 22800077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2281808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 228225e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 228325e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 228425e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 228525e6f9edSPaul Saab } 228625e6f9edSPaul Saab } else 228746f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 228846f58482SJonathan Lemon (void) tcp_output(tp); 228946f58482SJonathan Lemon goto drop; 2290cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2291cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2292a0445c2eSJayanth Vijayaraghavan 2293a0445c2eSJayanth Vijayaraghavan /* 2294a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2295a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2296a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2297a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2298a0445c2eSJayanth Vijayaraghavan * recovery. 2299a0445c2eSJayanth Vijayaraghavan */ 23003529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2301dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2302a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2303a0445c2eSJayanth Vijayaraghavan break; 2304a0445c2eSJayanth Vijayaraghavan } 2305dbc42409SLawrence Stewart } else { 2306a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 23079d11646dSJeffrey Hsu tp->snd_recover)) { 2308cb942153SJeffrey Hsu tp->t_dupacks = 0; 2309cb942153SJeffrey Hsu break; 231046f58482SJonathan Lemon } 2311a0445c2eSJayanth Vijayaraghavan } 2312dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2313dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 2314dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2315b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 23169b8b58e0SJonathan Lemon tp->t_rtttime = 0; 23173529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 231878b50714SRobert Watson TCPSTAT_INC( 231978b50714SRobert Watson tcps_sack_recovery_episode); 2320a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 23218db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 23226d90faf3SPaul Saab (void) tcp_output(tp); 23236d90faf3SPaul Saab goto drop; 23246d90faf3SPaul Saab } 2325fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2326df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2327df8bae1dSRodney W. Grimes (void) tcp_output(tp); 232848d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2329302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2330302ce8d6SAndre Oppermann __func__)); 2331df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 233248d2549cSJeffrey Hsu tp->t_maxseg * 233348d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2334df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2335df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2336df8bae1dSRodney W. Grimes goto drop; 2337603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2338dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2339582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 234048d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 234148d2549cSJeffrey Hsu u_int sent; 234289c02376SJeffrey Hsu 2343582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2344582a954bSJeffrey Hsu tp->t_dupacks == 2, 2345302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2346302ce8d6SAndre Oppermann __func__)); 234761a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 234848d2549cSJeffrey Hsu tp->snd_limited = 0; 234961a36e3dSJeffrey Hsu tp->snd_cwnd = 235061a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 235161a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 235261a36e3dSJeffrey Hsu tp->t_maxseg; 2353582a954bSJeffrey Hsu (void) tcp_output(tp); 235448d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 235548d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 235689c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 235789c02376SJeffrey Hsu tp->snd_limited == 0) || 235889c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 235989c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2360302ce8d6SAndre Oppermann ("%s: sent too much", 2361302ce8d6SAndre Oppermann __func__)); 236248d2549cSJeffrey Hsu tp->snd_limited = 2; 236348d2549cSJeffrey Hsu } else if (sent > 0) 236448d2549cSJeffrey Hsu ++tp->snd_limited; 2365582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2366582a954bSJeffrey Hsu goto drop; 2367df8bae1dSRodney W. Grimes } 2368df8bae1dSRodney W. Grimes } else 2369df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2370df8bae1dSRodney W. Grimes break; 2371df8bae1dSRodney W. Grimes } 2372cb942153SJeffrey Hsu 2373302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2374302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2375cb942153SJeffrey Hsu 2376df8bae1dSRodney W. Grimes /* 2377df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2378df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2379df8bae1dSRodney W. Grimes */ 2380dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2381cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 23823529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 23836d90faf3SPaul Saab tcp_sack_partialack(tp, th); 23846d90faf3SPaul Saab else 2385c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2386dbc42409SLawrence Stewart } else 2387dbc42409SLawrence Stewart cc_post_recovery(tp, th); 238846f58482SJonathan Lemon } 2389cb942153SJeffrey Hsu tp->t_dupacks = 0; 2390a0292f23SGarrett Wollman /* 2391a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2392a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2393a0292f23SGarrett Wollman */ 2394a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2395a0292f23SGarrett Wollman /* 2396a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2397a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 239807e43e10SAndras Olah * synchronized). Go to non-starred state, 239907e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 240007e43e10SAndras Olah * we can do window scaling. 2401a0292f23SGarrett Wollman */ 2402a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2403a0292f23SGarrett Wollman tp->snd_una++; 240407e43e10SAndras Olah /* Do window scaling? */ 240507e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 240607e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 240707e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2408464fcfbcSAndre Oppermann /* Send window already scaled. */ 240907e43e10SAndras Olah } 2410a0292f23SGarrett Wollman } 2411a0292f23SGarrett Wollman 2412a0292f23SGarrett Wollman process_ACK: 2413252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2414252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2415252ca428SRobert Watson ("tcp_input: process_ACK ti_locked %d", ti_locked)); 24168501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 24177cfc6904SRobert Watson 2418dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 241978b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 242078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2421df8bae1dSRodney W. Grimes 2422df8bae1dSRodney W. Grimes /* 24239b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 24249b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 24259b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 24269b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 24279b8b58e0SJonathan Lemon * we left off. 24289b8b58e0SJonathan Lemon */ 2429dbc42409SLawrence Stewart if (tp->t_rxtshift == 1 && (int)(ticks - tp->t_badrxtwin) < 0) 2430dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 24319b8b58e0SJonathan Lemon 24329b8b58e0SJonathan Lemon /* 2433df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2434df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2435df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2436df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2437df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2438df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2439df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2440fa55172bSMatthew Dillon * 2441fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2442fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2443fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2444fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2445df8bae1dSRodney W. Grimes */ 2446fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2447fa55172bSMatthew Dillon to.to_tsecr) { 2448eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2449eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 24509b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2451fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2452eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2453eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 24549b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2455fa55172bSMatthew Dillon } 2456df8bae1dSRodney W. Grimes 2457df8bae1dSRodney W. Grimes /* 2458df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2459df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2460df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2461df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2462df8bae1dSRodney W. Grimes */ 2463fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2464b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2465df8bae1dSRodney W. Grimes needoutput = 1; 2466b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2467b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2468a0292f23SGarrett Wollman 2469a0292f23SGarrett Wollman /* 2470a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2471a0292f23SGarrett Wollman * skip rest of ACK processing. 2472a0292f23SGarrett Wollman */ 2473a0292f23SGarrett Wollman if (acked == 0) 2474a0292f23SGarrett Wollman goto step6; 2475a0292f23SGarrett Wollman 2476df8bae1dSRodney W. Grimes /* 2477dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2478dbc42409SLawrence Stewart * control related information. This typically means increasing 2479dbc42409SLawrence Stewart * the congestion window. 2480df8bae1dSRodney W. Grimes */ 2481dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 2482dbc42409SLawrence Stewart 24835905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2484df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2485df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 24865905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2487df8bae1dSRodney W. Grimes ourfinisacked = 1; 2488df8bae1dSRodney W. Grimes } else { 24895905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2490df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2491df8bae1dSRodney W. Grimes ourfinisacked = 0; 2492df8bae1dSRodney W. Grimes } 2493564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 24941e4d7da7SRobert Watson sowwakeup_locked(so); 2495e8949f74SAndre Oppermann /* Detect una wraparound. */ 2496dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 24979d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 24989d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 24999d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2500dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2501dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 250224cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2503dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 250424cb0f22SLawrence Stewart } 2505fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 25063529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 25076d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 25086d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 25096d90faf3SPaul Saab } 2510df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2511df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2512df8bae1dSRodney W. Grimes 2513df8bae1dSRodney W. Grimes switch (tp->t_state) { 2514df8bae1dSRodney W. Grimes 2515df8bae1dSRodney W. Grimes /* 2516df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2517df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2518df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2519df8bae1dSRodney W. Grimes */ 2520df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2521df8bae1dSRodney W. Grimes if (ourfinisacked) { 2522df8bae1dSRodney W. Grimes /* 2523df8bae1dSRodney W. Grimes * If we can't receive any more 2524df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2525df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2526df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2527df8bae1dSRodney W. Grimes * we'll hang forever. 2528e8949f74SAndre Oppermann * 2529e8949f74SAndre Oppermann * XXXjl: 2530340c35deSJonathan Lemon * we should release the tp also, and use a 2531340c35deSJonathan Lemon * compressed state. 2532340c35deSJonathan Lemon */ 2533c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 25347c72af87SMohan Srinivasan int timeout; 25357c72af87SMohan Srinivasan 253603e49181SSeigo Tanimura soisdisconnected(so); 25377c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 25387c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2539b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 25404cc20ab1SSeigo Tanimura } 2541df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2542df8bae1dSRodney W. Grimes } 2543df8bae1dSRodney W. Grimes break; 2544df8bae1dSRodney W. Grimes 2545df8bae1dSRodney W. Grimes /* 2546df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2547df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2548df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2549df8bae1dSRodney W. Grimes * the segment. 2550df8bae1dSRodney W. Grimes */ 2551df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2552df8bae1dSRodney W. Grimes if (ourfinisacked) { 2553252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 255411a20fb8SJeffrey Hsu tcp_twstart(tp); 2555603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2556340c35deSJonathan Lemon m_freem(m); 2557679d9708SAndre Oppermann return; 2558df8bae1dSRodney W. Grimes } 2559df8bae1dSRodney W. Grimes break; 2560df8bae1dSRodney W. Grimes 2561df8bae1dSRodney W. Grimes /* 2562df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2563df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2564df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2565df8bae1dSRodney W. Grimes * enter the closed state and return. 2566df8bae1dSRodney W. Grimes */ 2567df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2568df8bae1dSRodney W. Grimes if (ourfinisacked) { 2569252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2570df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2571df8bae1dSRodney W. Grimes goto drop; 2572df8bae1dSRodney W. Grimes } 2573df8bae1dSRodney W. Grimes break; 2574df8bae1dSRodney W. Grimes } 2575df8bae1dSRodney W. Grimes } 2576df8bae1dSRodney W. Grimes 2577df8bae1dSRodney W. Grimes step6: 2578252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2579252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2580252ca428SRobert Watson ("tcp_do_segment: step6 ti_locked %d", ti_locked)); 25818501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25827cfc6904SRobert Watson 2583df8bae1dSRodney W. Grimes /* 2584df8bae1dSRodney W. Grimes * Update window information. 2585df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2586df8bae1dSRodney W. Grimes */ 2587fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2588fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2589fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2590fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2591df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2592fb59c426SYoshinobu Inoue if (tlen == 0 && 2593fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 259478b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2595df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2596fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2597fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2598df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2599df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2600df8bae1dSRodney W. Grimes needoutput = 1; 2601df8bae1dSRodney W. Grimes } 2602df8bae1dSRodney W. Grimes 2603df8bae1dSRodney W. Grimes /* 2604df8bae1dSRodney W. Grimes * Process segments with URG. 2605df8bae1dSRodney W. Grimes */ 2606fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2607df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2608df8bae1dSRodney W. Grimes /* 2609df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2610df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2611df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2612df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2613df8bae1dSRodney W. Grimes */ 261418ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2615fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2616fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2617fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 261818ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2619df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2620df8bae1dSRodney W. Grimes } 2621df8bae1dSRodney W. Grimes /* 2622df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2623df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2624df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2625df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2626df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2627df8bae1dSRodney W. Grimes * 2628df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2629df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2630df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2631df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2632df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2633df8bae1dSRodney W. Grimes * spec states (in one of two places). 2634df8bae1dSRodney W. Grimes */ 2635fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2636fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2637df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2638df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2639927c5ceaSRobert Watson if (so->so_oobmark == 0) 2640c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2641df8bae1dSRodney W. Grimes sohasoutofband(so); 2642df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2643df8bae1dSRodney W. Grimes } 264418ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2645df8bae1dSRodney W. Grimes /* 2646df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2647df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2648df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2649df8bae1dSRodney W. Grimes * data may creep in... ick. 2650df8bae1dSRodney W. Grimes */ 265130613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 265230613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 265330613f56SJeffrey Hsu /* hdr drop is delayed */ 265430613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 265530613f56SJeffrey Hsu } 2656c068736aSJeffrey Hsu } else { 2657df8bae1dSRodney W. Grimes /* 2658df8bae1dSRodney W. Grimes * If no out of band data is expected, 2659df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2660df8bae1dSRodney W. Grimes * with the receive window. 2661df8bae1dSRodney W. Grimes */ 2662df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2663df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2664c068736aSJeffrey Hsu } 2665df8bae1dSRodney W. Grimes dodata: /* XXX */ 2666252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2667252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2668252ca428SRobert Watson ("tcp_do_segment: dodata ti_locked %d", ti_locked)); 26698501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26707cfc6904SRobert Watson 2671df8bae1dSRodney W. Grimes /* 2672df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2673df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2674df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2675df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2676df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2677df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2678df8bae1dSRodney W. Grimes */ 2679fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2680df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 268169e03620SPaul Saab tcp_seq save_start = th->th_seq; 2682fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2683e4b64281SJesper Skriver /* 2684c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2685c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2686c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2687c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2688c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2689c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2690c068736aSJeffrey Hsu * conversions. 2691c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2692c068736aSJeffrey Hsu * immediately when segments are out of order (so 2693c068736aSJeffrey Hsu * fast retransmit can work). 2694e4b64281SJesper Skriver */ 2695e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2696e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2697e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2698e4b64281SJesper Skriver if (DELAY_ACK(tp)) 26993bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2700e4b64281SJesper Skriver else 2701e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2702e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2703e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 270478b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 270578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 2706e4b64281SJesper Skriver ND6_HINT(tp); 27071e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2708c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2709c1c36a2cSMike Silbersack m_freem(m); 2710c1c36a2cSMike Silbersack else 27111e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2712e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 27131e4d7da7SRobert Watson sorwakeup_locked(so); 2714e4b64281SJesper Skriver } else { 2715e8949f74SAndre Oppermann /* 2716e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2717e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2718e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2719e8949f74SAndre Oppermann * when trimming from the head. 2720e8949f74SAndre Oppermann */ 2721e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2722e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2723e4b64281SJesper Skriver } 27243529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2725f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2726302ce8d6SAndre Oppermann #if 0 2727df8bae1dSRodney W. Grimes /* 2728df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2729df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2730df8bae1dSRodney W. Grimes * buffer size. 2731302ce8d6SAndre Oppermann * XXX: Unused. 2732df8bae1dSRodney W. Grimes */ 2733df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2734302ce8d6SAndre Oppermann #endif 2735df8bae1dSRodney W. Grimes } else { 2736df8bae1dSRodney W. Grimes m_freem(m); 2737fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2738df8bae1dSRodney W. Grimes } 2739df8bae1dSRodney W. Grimes 2740df8bae1dSRodney W. Grimes /* 2741df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2742df8bae1dSRodney W. Grimes * that the connection is closing. 2743df8bae1dSRodney W. Grimes */ 2744fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2745df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2746df8bae1dSRodney W. Grimes socantrcvmore(so); 2747a0292f23SGarrett Wollman /* 2748a0292f23SGarrett Wollman * If connection is half-synchronized 2749d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2750a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2751a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2752a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2753a0292f23SGarrett Wollman */ 27543bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 27553bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2756a0292f23SGarrett Wollman else 2757df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2758df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2759df8bae1dSRodney W. Grimes } 2760df8bae1dSRodney W. Grimes switch (tp->t_state) { 2761df8bae1dSRodney W. Grimes 2762df8bae1dSRodney W. Grimes /* 2763df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2764df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2765df8bae1dSRodney W. Grimes */ 2766df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 27679b8b58e0SJonathan Lemon tp->t_starttime = ticks; 27689b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2769df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2770df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2771df8bae1dSRodney W. Grimes break; 2772df8bae1dSRodney W. Grimes 2773df8bae1dSRodney W. Grimes /* 2774df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2775df8bae1dSRodney W. Grimes * enter the CLOSING state. 2776df8bae1dSRodney W. Grimes */ 2777df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2778df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2779df8bae1dSRodney W. Grimes break; 2780df8bae1dSRodney W. Grimes 2781df8bae1dSRodney W. Grimes /* 2782df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2783df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2784df8bae1dSRodney W. Grimes * standard timers. 2785df8bae1dSRodney W. Grimes */ 2786df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2787252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2788252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2789252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2790252ca428SRobert Watson ti_locked)); 2791252ca428SRobert Watson 2792340c35deSJonathan Lemon tcp_twstart(tp); 2793603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2794679d9708SAndre Oppermann return; 2795df8bae1dSRodney W. Grimes } 2796df8bae1dSRodney W. Grimes } 2797252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2798252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2799252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2800603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2801252ca428SRobert Watson else 2802252ca428SRobert Watson panic("%s: dodata epilogue ti_locked %d", __func__, 2803252ca428SRobert Watson ti_locked); 2804252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2805252ca428SRobert Watson 2806610ee2f9SDavid Greenman #ifdef TCPDEBUG 28074cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2808fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2809fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2810610ee2f9SDavid Greenman #endif 2811df8bae1dSRodney W. Grimes 2812df8bae1dSRodney W. Grimes /* 2813df8bae1dSRodney W. Grimes * Return any desired output. 2814df8bae1dSRodney W. Grimes */ 2815df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2816df8bae1dSRodney W. Grimes (void) tcp_output(tp); 28177792ea27SJeffrey Hsu 2818a14c749fSJonathan Lemon check_delack: 2819252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2820252ca428SRobert Watson __func__, ti_locked)); 2821603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 28228501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2823252ca428SRobert Watson 2824a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 28253bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2826b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 28273bfd6421SJonathan Lemon } 28288501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2829679d9708SAndre Oppermann return; 2830df8bae1dSRodney W. Grimes 2831df8bae1dSRodney W. Grimes dropafterack: 2832252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2833252ca428SRobert Watson ("tcp_do_segment: dropafterack ti_locked %d", ti_locked)); 2834252ca428SRobert Watson 2835df8bae1dSRodney W. Grimes /* 2836df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2837df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 283880ab7c0eSGarrett Wollman * 283980ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 284080ab7c0eSGarrett Wollman * paths to this code happen after packets containing 284180ab7c0eSGarrett Wollman * RST have been dropped. 284280ab7c0eSGarrett Wollman * 284380ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 284480ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 284580ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 284680ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 284780ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 284880ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2849df8bae1dSRodney W. Grimes */ 2850fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2851fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2852a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2853a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2854a57815efSBosko Milekic goto dropwithreset; 2855a57815efSBosko Milekic } 2856a0292f23SGarrett Wollman #ifdef TCPDEBUG 28574cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2858fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2859fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2860a0292f23SGarrett Wollman #endif 2861252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2862252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2863252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2864603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2865252ca428SRobert Watson else 2866252ca428SRobert Watson panic("%s: dropafterack epilogue ti_locked %d", __func__, 2867252ca428SRobert Watson ti_locked); 2868252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2869252ca428SRobert Watson 2870df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2871df8bae1dSRodney W. Grimes (void) tcp_output(tp); 28728501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2873d6915262SRobert Watson m_freem(m); 2874679d9708SAndre Oppermann return; 2875df8bae1dSRodney W. Grimes 2876df8bae1dSRodney W. Grimes dropwithreset: 2877252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2878252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2879252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2880dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2881252ca428SRobert Watson else 2882252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 2883252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2884a57815efSBosko Milekic 2885a0ca0871SRobert Watson if (tp != NULL) { 2886302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 28878501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2888dd8ac7f9SRobert Watson } else 2889a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2890679d9708SAndre Oppermann return; 2891df8bae1dSRodney W. Grimes 2892df8bae1dSRodney W. Grimes drop: 2893252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2894252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2895252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2896252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2897252ca428SRobert Watson #ifdef INVARIANTS 2898252ca428SRobert Watson else 2899252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2900252ca428SRobert Watson #endif 2901252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2902252ca428SRobert Watson 2903df8bae1dSRodney W. Grimes /* 2904df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2905df8bae1dSRodney W. Grimes */ 2906610ee2f9SDavid Greenman #ifdef TCPDEBUG 29071c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2908fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2909fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2910a0292f23SGarrett Wollman #endif 29111c53f806SRobert Watson if (tp != NULL) 29128501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2913d6915262SRobert Watson m_freem(m); 2914302ce8d6SAndre Oppermann } 2915302ce8d6SAndre Oppermann 2916302ce8d6SAndre Oppermann /* 29170ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 29180ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 29190ca3f933SAndre Oppermann * tp may be NULL. 2920302ce8d6SAndre Oppermann */ 2921302ce8d6SAndre Oppermann static void 2922302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2923302ce8d6SAndre Oppermann int tlen, int rstreason) 2924302ce8d6SAndre Oppermann { 2925302ce8d6SAndre Oppermann struct ip *ip; 2926302ce8d6SAndre Oppermann #ifdef INET6 2927302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2928302ce8d6SAndre Oppermann #endif 29297a3244ccSRobert Watson 29307a3244ccSRobert Watson if (tp != NULL) { 29318501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29327a3244ccSRobert Watson } 29337a3244ccSRobert Watson 29340ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2935302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2936302ce8d6SAndre Oppermann goto drop; 2937302ce8d6SAndre Oppermann #ifdef INET6 2938302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2939302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2940302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2941302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2942302ce8d6SAndre Oppermann goto drop; 2943302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2944302ce8d6SAndre Oppermann } else 2945302ce8d6SAndre Oppermann #endif 2946302ce8d6SAndre Oppermann { 2947302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2948302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2949302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2950302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2951302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2952302ce8d6SAndre Oppermann goto drop; 2953302ce8d6SAndre Oppermann } 2954302ce8d6SAndre Oppermann 2955302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2956302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2957302ce8d6SAndre Oppermann goto drop; 2958302ce8d6SAndre Oppermann 2959302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2960302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2961302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2962302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2963302ce8d6SAndre Oppermann } else { 2964302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2965302ce8d6SAndre Oppermann tlen++; 2966302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2967302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2968302ce8d6SAndre Oppermann } 2969302ce8d6SAndre Oppermann return; 2970302ce8d6SAndre Oppermann drop: 2971302ce8d6SAndre Oppermann m_freem(m); 2972df8bae1dSRodney W. Grimes } 2973df8bae1dSRodney W. Grimes 2974be2ac88cSJonathan Lemon /* 2975be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2976be2ac88cSJonathan Lemon */ 29770312fbe9SPoul-Henning Kamp static void 2978ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2979df8bae1dSRodney W. Grimes { 2980df8bae1dSRodney W. Grimes int opt, optlen; 2981df8bae1dSRodney W. Grimes 2982be2ac88cSJonathan Lemon to->to_flags = 0; 2983df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2984df8bae1dSRodney W. Grimes opt = cp[0]; 2985df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2986df8bae1dSRodney W. Grimes break; 2987df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2988df8bae1dSRodney W. Grimes optlen = 1; 2989df8bae1dSRodney W. Grimes else { 2990b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2991b474779fSJun-ichiro itojun Hagino break; 2992df8bae1dSRodney W. Grimes optlen = cp[1]; 2993b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2994df8bae1dSRodney W. Grimes break; 2995df8bae1dSRodney W. Grimes } 2996df8bae1dSRodney W. Grimes switch (opt) { 2997df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2998df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2999df8bae1dSRodney W. Grimes continue; 3000f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3001df8bae1dSRodney W. Grimes continue; 3002be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3003be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3004be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3005fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3006df8bae1dSRodney W. Grimes break; 3007df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3008df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3009df8bae1dSRodney W. Grimes continue; 3010f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3011df8bae1dSRodney W. Grimes continue; 3012be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 301302a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3014df8bae1dSRodney W. Grimes break; 3015df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3016df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3017df8bae1dSRodney W. Grimes continue; 3018be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3019a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3020a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3021fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3022a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3023a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3024fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3025df8bae1dSRodney W. Grimes break; 30261cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 30271cfd4b53SBruce M Simpson /* 30281cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 30291cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 30301cfd4b53SBruce M Simpson * record the fact that the option was observed here 30311cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 30321cfd4b53SBruce M Simpson */ 30331cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 30341cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 30351cfd4b53SBruce M Simpson continue; 3036df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3037df47e437SAndre Oppermann to->to_signature = cp + 2; 30381cfd4b53SBruce M Simpson break; 3039265ed012SBruce M Simpson #endif 30406d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3041f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 30426d90faf3SPaul Saab continue; 3043f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3044f72167f4SAndre Oppermann continue; 3045603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3046f72167f4SAndre Oppermann continue; 3047fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 30486d90faf3SPaul Saab break; 30496d90faf3SPaul Saab case TCPOPT_SACK: 30505a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 30516d90faf3SPaul Saab continue; 3052b728e902SAndre Oppermann if (flags & TO_SYN) 3053b728e902SAndre Oppermann continue; 3054fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 30555a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 30565a53ca16SPaul Saab to->to_sacks = cp + 2; 305778b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 30586d90faf3SPaul Saab break; 3059be2ac88cSJonathan Lemon default: 3060be2ac88cSJonathan Lemon continue; 3061df8bae1dSRodney W. Grimes } 3062df8bae1dSRodney W. Grimes } 3063df8bae1dSRodney W. Grimes } 3064df8bae1dSRodney W. Grimes 3065df8bae1dSRodney W. Grimes /* 3066df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3067df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3068df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3069df8bae1dSRodney W. Grimes * sequencing purposes. 3070df8bae1dSRodney W. Grimes */ 30710312fbe9SPoul-Henning Kamp static void 3072ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3073ad3f9ab3SAndre Oppermann int off) 3074df8bae1dSRodney W. Grimes { 3075fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3076df8bae1dSRodney W. Grimes 3077df8bae1dSRodney W. Grimes while (cnt >= 0) { 3078df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3079df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3080df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3081df8bae1dSRodney W. Grimes 30828501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30837a3244ccSRobert Watson 3084df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3085df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3086df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3087df8bae1dSRodney W. Grimes m->m_len--; 308869a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 308969a34685SYoshinobu Inoue m->m_pkthdr.len--; 3090df8bae1dSRodney W. Grimes return; 3091df8bae1dSRodney W. Grimes } 3092df8bae1dSRodney W. Grimes cnt -= m->m_len; 3093df8bae1dSRodney W. Grimes m = m->m_next; 30944dfdffe9SAndre Oppermann if (m == NULL) 3095df8bae1dSRodney W. Grimes break; 3096df8bae1dSRodney W. Grimes } 3097df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3098df8bae1dSRodney W. Grimes } 3099df8bae1dSRodney W. Grimes 3100df8bae1dSRodney W. Grimes /* 3101df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3102df8bae1dSRodney W. Grimes * and update averages and current timeout. 3103df8bae1dSRodney W. Grimes */ 31040312fbe9SPoul-Henning Kamp static void 3105ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3106df8bae1dSRodney W. Grimes { 3107ad3f9ab3SAndre Oppermann int delta; 3108233e8c18SGarrett Wollman 31098501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31102be3bf22SRobert Watson 311178b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3112233e8c18SGarrett Wollman tp->t_rttupdated++; 3113233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3114233e8c18SGarrett Wollman /* 3115233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3116233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3117233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3118233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3119233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3120233e8c18SGarrett Wollman */ 3121233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3122233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3123233e8c18SGarrett Wollman 3124233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3125233e8c18SGarrett Wollman tp->t_srtt = 1; 3126233e8c18SGarrett Wollman 3127233e8c18SGarrett Wollman /* 3128233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3129233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3130233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3131233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3132233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3133233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3134233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3135233e8c18SGarrett Wollman * rfc793's wired-in beta. 3136233e8c18SGarrett Wollman */ 3137233e8c18SGarrett Wollman if (delta < 0) 3138233e8c18SGarrett Wollman delta = -delta; 3139233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3140233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3141233e8c18SGarrett Wollman tp->t_rttvar = 1; 31421fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 31431fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3144233e8c18SGarrett Wollman } else { 3145233e8c18SGarrett Wollman /* 3146233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3147233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3148233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3149233e8c18SGarrett Wollman */ 3150233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3151233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 31521fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3153233e8c18SGarrett Wollman } 31549b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3155df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3156df8bae1dSRodney W. Grimes 3157df8bae1dSRodney W. Grimes /* 3158df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3159df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3160df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3161df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3162df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3163df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3164df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3165df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3166df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3167df8bae1dSRodney W. Grimes */ 3168233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 31699e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3170df8bae1dSRodney W. Grimes 3171df8bae1dSRodney W. Grimes /* 3172df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3173df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3174df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3175df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3176df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3177df8bae1dSRodney W. Grimes */ 3178df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3179df8bae1dSRodney W. Grimes } 3180df8bae1dSRodney W. Grimes 3181df8bae1dSRodney W. Grimes /* 3182df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3183df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 3184df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 3185df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 3186df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3187df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 3188df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3189df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3190df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3191df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3192df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3193df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3194df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3195a0292f23SGarrett Wollman * 3196a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3197a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3198a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3199a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3200a0292f23SGarrett Wollman * data in maxopd. 3201a0292f23SGarrett Wollman * 3202a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 3203a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 3204a0292f23SGarrett Wollman * MSS of our peer. 320597d8d152SAndre Oppermann * 320697d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 320797d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3208df8bae1dSRodney W. Grimes */ 3209a0292f23SGarrett Wollman void 321091d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, 321191d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3212df8bae1dSRodney W. Grimes { 32133cee92e0SBjoern A. Zeeb int mss; 321497d8d152SAndre Oppermann u_long maxmtu; 3215c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 321697d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3217a0292f23SGarrett Wollman int origoffer = offer; 3218fb59c426SYoshinobu Inoue #ifdef INET6 3219c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3220c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3221c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3222c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3223c068736aSJeffrey Hsu #else 3224c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3225fb59c426SYoshinobu Inoue #endif 3226df8bae1dSRodney W. Grimes 32278501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32287a3244ccSRobert Watson 3229e8949f74SAndre Oppermann /* Initialize. */ 323097d8d152SAndre Oppermann #ifdef INET6 323197d8d152SAndre Oppermann if (isipv6) { 323291d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3233603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 323497d8d152SAndre Oppermann } else 323597d8d152SAndre Oppermann #endif 323697d8d152SAndre Oppermann { 323791d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3238603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3239df8bae1dSRodney W. Grimes } 3240df8bae1dSRodney W. Grimes 324197d8d152SAndre Oppermann /* 3242e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 324397d8d152SAndre Oppermann */ 32446f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 32456f01cac6SBjoern A. Zeeb /* 32468e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 32476f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 32486f01cac6SBjoern A. Zeeb * if there was no cache hit. 32496f01cac6SBjoern A. Zeeb */ 32506f01cac6SBjoern A. Zeeb if (metricptr != NULL) 32516f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 325297d8d152SAndre Oppermann return; 32536f01cac6SBjoern A. Zeeb } 325497d8d152SAndre Oppermann 3255e8949f74SAndre Oppermann /* What have we got? */ 325697d8d152SAndre Oppermann switch (offer) { 325797d8d152SAndre Oppermann case 0: 325897d8d152SAndre Oppermann /* 325997d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3260c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3261c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 326297d8d152SAndre Oppermann */ 3263c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 326497d8d152SAndre Oppermann break; 326597d8d152SAndre Oppermann 326697d8d152SAndre Oppermann case -1: 3267a0292f23SGarrett Wollman /* 3268c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3269a0292f23SGarrett Wollman */ 327097d8d152SAndre Oppermann /* FALLTHROUGH */ 327197d8d152SAndre Oppermann 327297d8d152SAndre Oppermann default: 3273a0292f23SGarrett Wollman /* 327453369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 327553369ac9SAndre Oppermann * to at least minmss. 327653369ac9SAndre Oppermann */ 3277603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 327897d8d152SAndre Oppermann } 3279a0292f23SGarrett Wollman 3280df8bae1dSRodney W. Grimes /* 3281e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3282df8bae1dSRodney W. Grimes */ 328397d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 32843cee92e0SBjoern A. Zeeb if (metricptr != NULL) 32853cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 328697d8d152SAndre Oppermann 3287df8bae1dSRodney W. Grimes /* 3288e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3289fb59c426SYoshinobu Inoue * else, use the link mtu. 3290df8bae1dSRodney W. Grimes */ 329197d8d152SAndre Oppermann if (metrics.rmx_mtu) 32922d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3293c068736aSJeffrey Hsu else { 329431b3783cSHajimu UMEMOTO #ifdef INET6 3295fb59c426SYoshinobu Inoue if (isipv6) { 329697d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3297603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 329897d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3299603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3300b3399803SHajimu UMEMOTO } else 3301b3399803SHajimu UMEMOTO #endif 330297d8d152SAndre Oppermann { 330397d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3304603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 330597d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3306603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3307a0292f23SGarrett Wollman } 33083cee92e0SBjoern A. Zeeb /* 33093cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 33103cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 33113cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 33123cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 33133cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 33143cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 33153cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 33163cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 33173cee92e0SBjoern A. Zeeb * this under the carpet. 33183cee92e0SBjoern A. Zeeb * 33193cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 33203cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 33213cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 33223cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 33233cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 33243cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 33253cee92e0SBjoern A. Zeeb */ 332697d8d152SAndre Oppermann } 3327a0292f23SGarrett Wollman mss = min(mss, offer); 332897d8d152SAndre Oppermann 3329a0292f23SGarrett Wollman /* 33303cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 33313cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 33323cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 33333cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 33343cee92e0SBjoern A. Zeeb */ 33353cee92e0SBjoern A. Zeeb mss = max(mss, 64); 33363cee92e0SBjoern A. Zeeb 33373cee92e0SBjoern A. Zeeb /* 3338a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3339a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3340a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3341a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3342a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3343a0292f23SGarrett Wollman */ 3344a0292f23SGarrett Wollman tp->t_maxopd = mss; 3345a0292f23SGarrett Wollman 3346a0292f23SGarrett Wollman /* 3347e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3348c94c54e4SAndre Oppermann * In this case we just guess. 3349a0292f23SGarrett Wollman */ 3350a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3351a0292f23SGarrett Wollman (origoffer == -1 || 3352a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3353a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3354a0292f23SGarrett Wollman 3355df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3356df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3357df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3358df8bae1dSRodney W. Grimes #else 3359df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3360df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3361df8bae1dSRodney W. Grimes #endif 336297d8d152SAndre Oppermann tp->t_maxseg = mss; 33633cee92e0SBjoern A. Zeeb } 33643cee92e0SBjoern A. Zeeb 33653cee92e0SBjoern A. Zeeb void 33663cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 33673cee92e0SBjoern A. Zeeb { 3368dbc42409SLawrence Stewart int mss; 33693cee92e0SBjoern A. Zeeb u_long bufsize; 33703cee92e0SBjoern A. Zeeb struct inpcb *inp; 33713cee92e0SBjoern A. Zeeb struct socket *so; 33723cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 337391d6cfa6SBjoern A. Zeeb int mtuflags = 0; 3374dbc42409SLawrence Stewart 33753cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 33763cee92e0SBjoern A. Zeeb 337791d6cfa6SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics, &mtuflags); 33783cee92e0SBjoern A. Zeeb 33793cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 33803cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 338197d8d152SAndre Oppermann 3382df8bae1dSRodney W. Grimes /* 338397d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 338497d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 338597d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 338697d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 338797d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3388df8bae1dSRodney W. Grimes */ 3389c3b02504SBjoern A. Zeeb so = inp->inp_socket; 33903f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 339197d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 339297d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 339397d8d152SAndre Oppermann else 3394df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3395df8bae1dSRodney W. Grimes if (bufsize < mss) 3396df8bae1dSRodney W. Grimes mss = bufsize; 3397df8bae1dSRodney W. Grimes else { 3398df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3399df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3400df8bae1dSRodney W. Grimes bufsize = sb_max; 340188c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 34023f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3403df8bae1dSRodney W. Grimes } 34043f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3405df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3406df8bae1dSRodney W. Grimes 34073f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 340897d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 340997d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 341097d8d152SAndre Oppermann else 3411df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3412df8bae1dSRodney W. Grimes if (bufsize > mss) { 3413df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3414df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3415df8bae1dSRodney W. Grimes bufsize = sb_max; 341688c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 34173f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3418df8bae1dSRodney W. Grimes } 34193f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 342091d6cfa6SBjoern A. Zeeb 342191d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 342291d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 342391d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3424a0292f23SGarrett Wollman } 3425a0292f23SGarrett Wollman 3426a0292f23SGarrett Wollman /* 3427a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3428a0292f23SGarrett Wollman */ 3429a0292f23SGarrett Wollman int 3430ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3431a0292f23SGarrett Wollman { 343297d8d152SAndre Oppermann int mss = 0; 343397d8d152SAndre Oppermann u_long maxmtu = 0; 343497d8d152SAndre Oppermann u_long thcmtu = 0; 343597d8d152SAndre Oppermann size_t min_protoh; 3436a0292f23SGarrett Wollman 343797d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3438a0292f23SGarrett Wollman 343931b3783cSHajimu UMEMOTO #ifdef INET6 3440dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3441603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3442233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 344397d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 344497d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 344597d8d152SAndre Oppermann } else 344631b3783cSHajimu UMEMOTO #endif 344797d8d152SAndre Oppermann { 3448603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3449233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 345097d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 345197d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 345297d8d152SAndre Oppermann } 345397d8d152SAndre Oppermann if (maxmtu && thcmtu) 345497d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 345597d8d152SAndre Oppermann else if (maxmtu || thcmtu) 345697d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 345797d8d152SAndre Oppermann 345897d8d152SAndre Oppermann return (mss); 3459df8bae1dSRodney W. Grimes } 346046f58482SJonathan Lemon 346146f58482SJonathan Lemon 346246f58482SJonathan Lemon /* 3463c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3464c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3465c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3466c068736aSJeffrey Hsu * be started again. 346746f58482SJonathan Lemon */ 3468c068736aSJeffrey Hsu static void 3469ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 347046f58482SJonathan Lemon { 347146f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 347246f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 347346f58482SJonathan Lemon 34748501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34757a3244ccSRobert Watson 3476b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 347746f58482SJonathan Lemon tp->t_rtttime = 0; 347846f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 34796b2a5f92SJayanth Vijayaraghavan /* 3480c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3481c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 34826b2a5f92SJayanth Vijayaraghavan */ 3483dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3484a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 34856b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 348646f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 348746f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 348846f58482SJonathan Lemon tp->snd_nxt = onxt; 348946f58482SJonathan Lemon /* 349046f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 349146f58482SJonathan Lemon * not updated yet. 349246f58482SJonathan Lemon */ 3493dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3494dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3495d7587117SPaul Saab else 3496d7587117SPaul Saab tp->snd_cwnd = 0; 3497d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 349846f58482SJonathan Lemon } 3499