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 8fa046d87SRobert Watson * Copyright (c) 2010-2011 Juniper Networks, Inc. 9dbc42409SLawrence Stewart * All rights reserved. 10dbc42409SLawrence Stewart * 11dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced Internet 12891b8ed4SLawrence Stewart * Architectures, Swinburne University of Technology, by Lawrence Stewart, 13891b8ed4SLawrence Stewart * James Healy and David Hayes, made possible in part by a grant from the Cisco 14891b8ed4SLawrence Stewart * University Research Program Fund at Community Foundation Silicon Valley. 15dbc42409SLawrence Stewart * 16dbc42409SLawrence Stewart * Portions of this software were developed at the Centre for Advanced 17dbc42409SLawrence Stewart * Internet Architectures, Swinburne University of Technology, Melbourne, 18dbc42409SLawrence Stewart * Australia by David Hayes under sponsorship from the FreeBSD Foundation. 19df8bae1dSRodney W. Grimes * 20fa046d87SRobert Watson * Portions of this software were developed by Robert N. M. Watson under 21fa046d87SRobert Watson * contract to Juniper Networks, Inc. 22fa046d87SRobert Watson * 23df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 24df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 25df8bae1dSRodney W. Grimes * are met: 26df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 27df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 28df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 29df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 30df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 31df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 32df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 33df8bae1dSRodney W. Grimes * without specific prior written permission. 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 36df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 38df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 39df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 40df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 41df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 43df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 44df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 45df8bae1dSRodney W. Grimes * SUCH DAMAGE. 46df8bae1dSRodney W. Grimes * 47e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 48df8bae1dSRodney W. Grimes */ 49df8bae1dSRodney W. Grimes 504b421e2dSMike Silbersack #include <sys/cdefs.h> 514b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 524b421e2dSMike Silbersack 53f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 541cfd4b53SBruce M Simpson #include "opt_inet.h" 55fb59c426SYoshinobu Inoue #include "opt_inet6.h" 563a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 570cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 580cc12cc5SJoerg Wunsch 59df8bae1dSRodney W. Grimes #include <sys/param.h> 6098163b98SPoul-Henning Kamp #include <sys/kernel.h> 6139bc9de5SLawrence Stewart #include <sys/hhook.h> 62df8bae1dSRodney W. Grimes #include <sys/malloc.h> 63df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 64a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 65df8bae1dSRodney W. Grimes #include <sys/protosw.h> 66960ed29cSSeigo Tanimura #include <sys/signalvar.h> 67df8bae1dSRodney W. Grimes #include <sys/socket.h> 68df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 69960ed29cSSeigo Tanimura #include <sys/sysctl.h> 70816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 71960ed29cSSeigo Tanimura #include <sys/systm.h> 72df8bae1dSRodney W. Grimes 73e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 74e79adb8eSGarrett Wollman 7512e2e970SAndre Oppermann #include <vm/uma.h> 7612e2e970SAndre Oppermann 77df8bae1dSRodney W. Grimes #include <net/if.h> 78df8bae1dSRodney W. Grimes #include <net/route.h> 79530c0060SRobert Watson #include <net/vnet.h> 80df8bae1dSRodney W. Grimes 81773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 82773673c1SAndre Oppermann 83dbc42409SLawrence Stewart #include <netinet/cc.h> 84df8bae1dSRodney W. Grimes #include <netinet/in.h> 85960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 86df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 87960ed29cSSeigo Tanimura #include <netinet/in_var.h> 88df8bae1dSRodney W. Grimes #include <netinet/ip.h> 898e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 90686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 91df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 92ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 93686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 95686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 96960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 97960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 98df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 99df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 100df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 101df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 102fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 103df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 104c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 105610ee2f9SDavid Greenman #ifdef TCPDEBUG 106df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 107fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 108fb59c426SYoshinobu Inoue 109b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 110b9234fafSSam Leffler #include <netipsec/ipsec.h> 111b9234fafSSam Leffler #include <netipsec/ipsec6.h> 112b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 113b9234fafSSam Leffler 114db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 115db4f9cc7SJonathan Lemon 116aed55708SRobert Watson #include <security/mac/mac_framework.h> 117aed55708SRobert Watson 118dbc42409SLawrence Stewart const int tcprexmtthresh = 3; 1190312fbe9SPoul-Henning Kamp 120eddfbb76SRobert Watson VNET_DEFINE(struct tcpstat, tcpstat); 121eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 122eddfbb76SRobert Watson &VNET_NAME(tcpstat), tcpstat, 1238b615593SMarko Zec "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1240312fbe9SPoul-Henning Kamp 125773673c1SAndre Oppermann int tcp_log_in_vain = 0; 126816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 127eddfbb76SRobert Watson &tcp_log_in_vain, 0, 128eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 129816a3d83SPoul-Henning Kamp 13082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 13182cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 132eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 133eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 134eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 13516f7f31fSGeoff Rehmet 13682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 137eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 138eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1393d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 140f498eeeeSDavid Greenman 14182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 14282cea7e6SBjoern A. Zeeb #define V_drop_synfin VNET(drop_synfin) 143eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 144eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 145eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 146f8613305SDag-Erling Smørgrav 14782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 14882cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 149eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 150eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 151eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 152582a954bSJeffrey Hsu 15382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 154eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 155eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 156da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 157da3a8a1aSJeffrey Hsu 15882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 159eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 160eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 16124cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 162eddfbb76SRobert Watson 16382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 164eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 165eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 16624cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 16724cb0f22SLawrence Stewart 168f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 169f2512ba1SRui Paulo 17082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0; 171eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW, 172eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 173eddfbb76SRobert Watson "TCP ECN support"); 174eddfbb76SRobert Watson 17582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 176eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW, 177eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 178eddfbb76SRobert Watson "Max retries before giving up on ECN"); 179eddfbb76SRobert Watson 18082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 18182cea7e6SBjoern A. Zeeb #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 182eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 183eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 1846489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 185a69968eeSMike Silbersack 18682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 18782cea7e6SBjoern A. Zeeb #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 188eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 189eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 1908b615593SMarko Zec "Enable automatic receive buffer sizing"); 1916741ecf5SAndre Oppermann 19282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 19382cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 194eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 195eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 1966489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1976741ecf5SAndre Oppermann 198b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024; 19982cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 200eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 201eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 2028b615593SMarko Zec "Max size of automatic receive buffer"); 2036741ecf5SAndre Oppermann 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 *); 2182903309aSAttilio Rao static void inline tcp_fields_to_host(struct tcphdr *); 2192903309aSAttilio Rao #ifdef TCP_SIGNATURE 2202903309aSAttilio Rao static void inline tcp_fields_to_net(struct tcphdr *); 2212903309aSAttilio Rao static int inline tcp_signature_verify_input(struct mbuf *, int, int, 2222903309aSAttilio Rao int, struct tcpopt *, struct tcphdr *, u_int); 2232903309aSAttilio Rao #endif 224dbc42409SLawrence Stewart static void inline cc_ack_received(struct tcpcb *tp, struct tcphdr *th, 225dbc42409SLawrence Stewart uint16_t type); 226dbc42409SLawrence Stewart static void inline cc_conn_init(struct tcpcb *tp); 227dbc42409SLawrence Stewart static void inline cc_post_recovery(struct tcpcb *tp, struct tcphdr *th); 22839bc9de5SLawrence Stewart static void inline hhook_run_tcp_est_in(struct tcpcb *tp, 22939bc9de5SLawrence Stewart struct tcphdr *th, struct tcpopt *to); 230f2512ba1SRui Paulo 231315e3e38SRobert Watson /* 232315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 233315e3e38SRobert Watson * into tcpstat treated as an array of u_long. While this encodes the 234315e3e38SRobert Watson * general layout of tcpstat into the caller, it doesn't encode its location, 235315e3e38SRobert Watson * so that future changes to add, for example, per-CPU stats support won't 236315e3e38SRobert Watson * cause binary compatibility problems for kernel modules. 237315e3e38SRobert Watson */ 238315e3e38SRobert Watson void 239315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 240315e3e38SRobert Watson { 241315e3e38SRobert Watson 242315e3e38SRobert Watson (*((u_long *)&V_tcpstat + statnum))++; 243315e3e38SRobert Watson } 244315e3e38SRobert Watson 245dbc42409SLawrence Stewart /* 24639bc9de5SLawrence Stewart * Wrapper for the TCP established input helper hook. 24739bc9de5SLawrence Stewart */ 24839bc9de5SLawrence Stewart static void inline 24939bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) 25039bc9de5SLawrence Stewart { 25139bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 25239bc9de5SLawrence Stewart 25339bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) { 25439bc9de5SLawrence Stewart hhook_data.tp = tp; 25539bc9de5SLawrence Stewart hhook_data.th = th; 25639bc9de5SLawrence Stewart hhook_data.to = to; 25739bc9de5SLawrence Stewart 25839bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data, 25939bc9de5SLawrence Stewart tp->osd); 26039bc9de5SLawrence Stewart } 26139bc9de5SLawrence Stewart } 26239bc9de5SLawrence Stewart 26339bc9de5SLawrence Stewart /* 264dbc42409SLawrence Stewart * CC wrapper hook functions 265dbc42409SLawrence Stewart */ 266f2512ba1SRui Paulo static void inline 267dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type) 268f2512ba1SRui Paulo { 269dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 270f2512ba1SRui Paulo 271dbc42409SLawrence Stewart tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th); 272dbc42409SLawrence Stewart if (tp->snd_cwnd == min(tp->snd_cwnd, tp->snd_wnd)) 273dbc42409SLawrence Stewart tp->ccv->flags |= CCF_CWND_LIMITED; 274dbc42409SLawrence Stewart else 275dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_CWND_LIMITED; 276dbc42409SLawrence Stewart 277dbc42409SLawrence Stewart if (type == CC_ACK) { 278dbc42409SLawrence Stewart if (tp->snd_cwnd > tp->snd_ssthresh) { 279dbc42409SLawrence Stewart tp->t_bytes_acked += min(tp->ccv->bytes_this_ack, 280dbc42409SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 281dbc42409SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) { 282dbc42409SLawrence Stewart tp->t_bytes_acked -= tp->snd_cwnd; 283dbc42409SLawrence Stewart tp->ccv->flags |= CCF_ABC_SENTAWND; 284dbc42409SLawrence Stewart } 285dbc42409SLawrence Stewart } else { 286dbc42409SLawrence Stewart tp->ccv->flags &= ~CCF_ABC_SENTAWND; 287dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 288dbc42409SLawrence Stewart } 289dbc42409SLawrence Stewart } 290dbc42409SLawrence Stewart 291dbc42409SLawrence Stewart if (CC_ALGO(tp)->ack_received != NULL) { 292dbc42409SLawrence Stewart /* XXXLAS: Find a way to live without this */ 293dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 294dbc42409SLawrence Stewart CC_ALGO(tp)->ack_received(tp->ccv, type); 295dbc42409SLawrence Stewart } 296dbc42409SLawrence Stewart } 297dbc42409SLawrence Stewart 298dbc42409SLawrence Stewart static void inline 299dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp) 300dbc42409SLawrence Stewart { 301dbc42409SLawrence Stewart struct hc_metrics_lite metrics; 302dbc42409SLawrence Stewart struct inpcb *inp = tp->t_inpcb; 303dbc42409SLawrence Stewart int rtt; 304dbc42409SLawrence Stewart 305dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 306dbc42409SLawrence Stewart 307dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 308dbc42409SLawrence Stewart 309dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 310dbc42409SLawrence Stewart tp->t_srtt = rtt; 311dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 312dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 313dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 314dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 315dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 316dbc42409SLawrence Stewart } else { 317dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 318dbc42409SLawrence Stewart tp->t_rttvar = 319dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 320dbc42409SLawrence Stewart } 321dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 322dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 323dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 324dbc42409SLawrence Stewart } 325dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 326dbc42409SLawrence Stewart /* 327dbc42409SLawrence Stewart * There's some sort of gateway or interface 328dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 329dbc42409SLawrence Stewart * the slow start threshhold, but set the 330dbc42409SLawrence Stewart * threshold to no less than 2*mss. 331dbc42409SLawrence Stewart */ 332dbc42409SLawrence Stewart tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 333dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 334dbc42409SLawrence Stewart } 335dbc42409SLawrence Stewart 336dbc42409SLawrence Stewart /* 337*9ec4a4ccSAndre Oppermann * Set the initial slow-start flight size. 338dbc42409SLawrence Stewart * 339dbc42409SLawrence Stewart * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 340*9ec4a4ccSAndre Oppermann * XXX: We currently check only in syncache_socket for that. 341dbc42409SLawrence Stewart */ 342dbc42409SLawrence Stewart if (V_tcp_do_rfc3390) 343dbc42409SLawrence Stewart tp->snd_cwnd = min(4 * tp->t_maxseg, 344dbc42409SLawrence Stewart max(2 * tp->t_maxseg, 4380)); 345dbc42409SLawrence Stewart else 346*9ec4a4ccSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 347dbc42409SLawrence Stewart 348dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 349dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 350dbc42409SLawrence Stewart } 351dbc42409SLawrence Stewart 352dbc42409SLawrence Stewart void inline 353dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 354dbc42409SLawrence Stewart { 355dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 356dbc42409SLawrence Stewart 357dbc42409SLawrence Stewart switch(type) { 358dbc42409SLawrence Stewart case CC_NDUPACK: 359dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 360f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 361f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 362f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 363f2512ba1SRui Paulo } 364dbc42409SLawrence Stewart break; 365dbc42409SLawrence Stewart case CC_ECN: 366dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 367dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 368dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 369dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 370dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 371dbc42409SLawrence Stewart } 372dbc42409SLawrence Stewart break; 373dbc42409SLawrence Stewart case CC_RTO: 374dbc42409SLawrence Stewart tp->t_dupacks = 0; 375dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 376dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 3776157935fSLawrence Stewart tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 3786157935fSLawrence Stewart tp->t_maxseg) * tp->t_maxseg; 379dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg; 380dbc42409SLawrence Stewart break; 381dbc42409SLawrence Stewart case CC_RTO_ERR: 382dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 383dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 384dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 385dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 386dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 387dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 388dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 389dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 390dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 391dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 392672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 393dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 394dbc42409SLawrence Stewart break; 395dbc42409SLawrence Stewart } 396dbc42409SLawrence Stewart 397dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 398dbc42409SLawrence Stewart if (th != NULL) 399dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 400dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 401dbc42409SLawrence Stewart } 402dbc42409SLawrence Stewart } 403dbc42409SLawrence Stewart 404dbc42409SLawrence Stewart static void inline 405dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 406dbc42409SLawrence Stewart { 407dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 408dbc42409SLawrence Stewart 409dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 410dbc42409SLawrence Stewart 411dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 412dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 413dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 414dbc42409SLawrence Stewart } 415dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 416dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 417dbc42409SLawrence Stewart } 4180312fbe9SPoul-Henning Kamp 4192903309aSAttilio Rao static inline void 4202903309aSAttilio Rao tcp_fields_to_host(struct tcphdr *th) 4212903309aSAttilio Rao { 4222903309aSAttilio Rao 4232903309aSAttilio Rao th->th_seq = ntohl(th->th_seq); 4242903309aSAttilio Rao th->th_ack = ntohl(th->th_ack); 4252903309aSAttilio Rao th->th_win = ntohs(th->th_win); 4262903309aSAttilio Rao th->th_urp = ntohs(th->th_urp); 4272903309aSAttilio Rao } 4282903309aSAttilio Rao 4292903309aSAttilio Rao #ifdef TCP_SIGNATURE 4302903309aSAttilio Rao static inline void 4312903309aSAttilio Rao tcp_fields_to_net(struct tcphdr *th) 4322903309aSAttilio Rao { 4332903309aSAttilio Rao 4342903309aSAttilio Rao th->th_seq = htonl(th->th_seq); 4352903309aSAttilio Rao th->th_ack = htonl(th->th_ack); 4362903309aSAttilio Rao th->th_win = htons(th->th_win); 4372903309aSAttilio Rao th->th_urp = htons(th->th_urp); 4382903309aSAttilio Rao } 4392903309aSAttilio Rao 4402903309aSAttilio Rao static inline int 4412903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 4422903309aSAttilio Rao struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 4432903309aSAttilio Rao { 4442903309aSAttilio Rao int ret; 4452903309aSAttilio Rao 4462903309aSAttilio Rao tcp_fields_to_net(th); 4472903309aSAttilio Rao ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 4482903309aSAttilio Rao tcp_fields_to_host(th); 4492903309aSAttilio Rao return (ret); 4502903309aSAttilio Rao } 4512903309aSAttilio Rao #endif 4522903309aSAttilio Rao 453fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 454fb59c426SYoshinobu Inoue #ifdef INET6 455fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 456fb59c426SYoshinobu Inoue do { \ 457fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 45897d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 45997d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 460fb59c426SYoshinobu Inoue } while (0) 461fb59c426SYoshinobu Inoue #else 462fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 463fb59c426SYoshinobu Inoue #endif 464df8bae1dSRodney W. Grimes 465df8bae1dSRodney W. Grimes /* 466262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 467262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 468262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 4693bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 4703bfd6421SJonathan Lemon * - delayed acks are enabled or 4713bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 472d8c85a26SJonathan Lemon */ 473d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 474b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 475a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 476603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 477d8c85a26SJonathan Lemon 478df8bae1dSRodney W. Grimes /* 4798d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 4808d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 4818d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 4828d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 4838d573cc1SAndre Oppermann * SYN processing on listen sockets 4848d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 4858d573cc1SAndre Oppermann * establishing, established and closing connections 486df8bae1dSRodney W. Grimes */ 487fb59c426SYoshinobu Inoue #ifdef INET6 488fb59c426SYoshinobu Inoue int 489ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 490fb59c426SYoshinobu Inoue { 491ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 49233841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 493fb59c426SYoshinobu Inoue 494fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 495fb59c426SYoshinobu Inoue 496fb59c426SYoshinobu Inoue /* 497fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 498fb59c426SYoshinobu Inoue * better place to put this in? 499fb59c426SYoshinobu Inoue */ 50033841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 50133841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 502fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 503fb59c426SYoshinobu Inoue 5048c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 505fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 506fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 507fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 508fb59c426SYoshinobu Inoue return IPPROTO_DONE; 509fb59c426SYoshinobu Inoue } 510fb59c426SYoshinobu Inoue 511f0ffb944SJulian Elischer tcp_input(m, *offp); 512fb59c426SYoshinobu Inoue return IPPROTO_DONE; 513fb59c426SYoshinobu Inoue } 514b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 515fb59c426SYoshinobu Inoue 516df8bae1dSRodney W. Grimes void 517ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 518df8bae1dSRodney W. Grimes { 519b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 520ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 521b287c6c7SBjoern A. Zeeb #ifdef INET 522ad3f9ab3SAndre Oppermann struct ipovly *ipov; 523b287c6c7SBjoern A. Zeeb #endif 524ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 525302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 526302ce8d6SAndre Oppermann struct socket *so = NULL; 527e79adb8eSGarrett Wollman u_char *optp = NULL; 52826f9a767SRodney W. Grimes int optlen = 0; 529b287c6c7SBjoern A. Zeeb #ifdef INET 530b287c6c7SBjoern A. Zeeb int len; 531b287c6c7SBjoern A. Zeeb #endif 532b287c6c7SBjoern A. Zeeb int tlen = 0, off; 533fb59c426SYoshinobu Inoue int drop_hdrlen; 534ad3f9ab3SAndre Oppermann int thflags; 535302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5362903309aSAttilio Rao #ifdef TCP_SIGNATURE 5372903309aSAttilio Rao uint8_t sig_checked = 0; 5382903309aSAttilio Rao #endif 539b287c6c7SBjoern A. Zeeb uint8_t iptos = 0; 5409b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5419b932e9eSAndre Oppermann struct m_tag *fwd_tag; 5429b932e9eSAndre Oppermann #endif 543c068736aSJeffrey Hsu #ifdef INET6 544302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 545c068736aSJeffrey Hsu int isipv6; 546c068736aSJeffrey Hsu #else 547a160e630SAndre Oppermann const void *ip6 = NULL; 548b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 549302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 550a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 551252ca428SRobert Watson int ti_locked; 552252ca428SRobert Watson #define TI_UNLOCKED 1 553fa046d87SRobert Watson #define TI_WLOCKED 2 554f76fcf6dSJeffrey Hsu 555610ee2f9SDavid Greenman #ifdef TCPDEBUG 556c068736aSJeffrey Hsu /* 557c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 558c068736aSJeffrey Hsu * now IPv6. 559c068736aSJeffrey Hsu */ 56014739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 561410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 562610ee2f9SDavid Greenman short ostate = 0; 563610ee2f9SDavid Greenman #endif 564c068736aSJeffrey Hsu 565fb59c426SYoshinobu Inoue #ifdef INET6 566fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 567fb59c426SYoshinobu Inoue #endif 568a0292f23SGarrett Wollman 569302ce8d6SAndre Oppermann to.to_flags = 0; 57078b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 571fb59c426SYoshinobu Inoue 57204d3a452SHajimu UMEMOTO #ifdef INET6 573b287c6c7SBjoern A. Zeeb if (isipv6) { 5748d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 575fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 576fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 577fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 57878b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 579fb59c426SYoshinobu Inoue goto drop; 580fb59c426SYoshinobu Inoue } 581fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 58233841545SHajimu UMEMOTO 58333841545SHajimu UMEMOTO /* 58433841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 58533841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 58633841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 58733841545SHajimu UMEMOTO * 58833841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 58933841545SHajimu UMEMOTO * already dropped in ip6_input. 59033841545SHajimu UMEMOTO */ 59133841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 59233841545SHajimu UMEMOTO /* XXX stat */ 59333841545SHajimu UMEMOTO goto drop; 59433841545SHajimu UMEMOTO } 595b287c6c7SBjoern A. Zeeb } 59604d3a452SHajimu UMEMOTO #endif 597b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 598b287c6c7SBjoern A. Zeeb else 599b287c6c7SBjoern A. Zeeb #endif 600b287c6c7SBjoern A. Zeeb #ifdef INET 601b287c6c7SBjoern A. Zeeb { 602df8bae1dSRodney W. Grimes /* 603df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 604df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 605df8bae1dSRodney W. Grimes */ 606fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 607df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 608fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 609fb59c426SYoshinobu Inoue } 610df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6114dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6124dfdffe9SAndre Oppermann == NULL) { 61378b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 614df8bae1dSRodney W. Grimes return; 615df8bae1dSRodney W. Grimes } 616df8bae1dSRodney W. Grimes } 617fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 618fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 619db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 620db4f9cc7SJonathan Lemon tlen = ip->ip_len; 621df8bae1dSRodney W. Grimes 622db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 623db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 624db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 625db4f9cc7SJonathan Lemon else 626db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 627c068736aSJeffrey Hsu ip->ip_dst.s_addr, 628c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 629c068736aSJeffrey Hsu ip->ip_len + 630c068736aSJeffrey Hsu IPPROTO_TCP)); 631db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 6323c653157SHartmut Brandt #ifdef TCPDEBUG 6333c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 6343c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 6353c653157SHartmut Brandt #endif 636db4f9cc7SJonathan Lemon } else { 637df8bae1dSRodney W. Grimes /* 638df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 639df8bae1dSRodney W. Grimes */ 640df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 641fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 642fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 643fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 644fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 645db4f9cc7SJonathan Lemon } 646fb59c426SYoshinobu Inoue if (th->th_sum) { 64778b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 648df8bae1dSRodney W. Grimes goto drop; 649df8bae1dSRodney W. Grimes } 650fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 651fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 652fb59c426SYoshinobu Inoue } 653b287c6c7SBjoern A. Zeeb #endif /* INET */ 654df8bae1dSRodney W. Grimes 655f2512ba1SRui Paulo #ifdef INET6 656f2512ba1SRui Paulo if (isipv6) 657f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 658b287c6c7SBjoern A. Zeeb #endif 659b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 660f2512ba1SRui Paulo else 661f2512ba1SRui Paulo #endif 662b287c6c7SBjoern A. Zeeb #ifdef INET 663f2512ba1SRui Paulo iptos = ip->ip_tos; 664b287c6c7SBjoern A. Zeeb #endif 665f2512ba1SRui Paulo 666df8bae1dSRodney W. Grimes /* 667df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 668df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 669df8bae1dSRodney W. Grimes */ 670fb59c426SYoshinobu Inoue off = th->th_off << 2; 671df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 67278b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 673df8bae1dSRodney W. Grimes goto drop; 674df8bae1dSRodney W. Grimes } 675fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 676df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 67704d3a452SHajimu UMEMOTO #ifdef INET6 678b287c6c7SBjoern A. Zeeb if (isipv6) { 679fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 680fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 681fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 682b287c6c7SBjoern A. Zeeb } 68304d3a452SHajimu UMEMOTO #endif 684b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 685b287c6c7SBjoern A. Zeeb else 686b287c6c7SBjoern A. Zeeb #endif 687b287c6c7SBjoern A. Zeeb #ifdef INET 688b287c6c7SBjoern A. Zeeb { 689df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 690c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 6914dfdffe9SAndre Oppermann == NULL) { 69278b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 693df8bae1dSRodney W. Grimes return; 694df8bae1dSRodney W. Grimes } 695fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 696fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 697fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 698fb59c426SYoshinobu Inoue } 699df8bae1dSRodney W. Grimes } 700b287c6c7SBjoern A. Zeeb #endif 701df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 702fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 703df8bae1dSRodney W. Grimes } 704fb59c426SYoshinobu Inoue thflags = th->th_flags; 705df8bae1dSRodney W. Grimes 706e46cd3d4SDag-Erling Smørgrav /* 707df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 708df8bae1dSRodney W. Grimes */ 7092903309aSAttilio Rao tcp_fields_to_host(th); 710df8bae1dSRodney W. Grimes 711df8bae1dSRodney W. Grimes /* 712794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 713755c1f07SAndras Olah */ 714fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 715755c1f07SAndras Olah 716755c1f07SAndras Olah /* 717fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 718fa046d87SRobert Watson * connection then first acquire pcbinfo lock. There are two cases 719fa046d87SRobert Watson * where we might discover later we need a write lock despite the 720fa046d87SRobert Watson * flags: ACKs moving a connection out of the syncache, and ACKs for 721fa046d87SRobert Watson * a connection in TIMEWAIT. 722df8bae1dSRodney W. Grimes */ 723fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) { 724603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 725252ca428SRobert Watson ti_locked = TI_WLOCKED; 726fa046d87SRobert Watson } else 727fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 728252ca428SRobert Watson 729df8bae1dSRodney W. Grimes findpcb: 730252ca428SRobert Watson #ifdef INVARIANTS 731fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 732603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 733fa046d87SRobert Watson } else { 734fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 735fa046d87SRobert Watson } 736252ca428SRobert Watson #endif 737252ca428SRobert Watson 7389b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 7398d573cc1SAndre Oppermann /* 7408d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7418d573cc1SAndre Oppermann */ 7429b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 7438a006adbSBjoern A. Zeeb #endif /* IPFIREWALL_FORWARD */ 7449b932e9eSAndre Oppermann 7458a006adbSBjoern A. Zeeb #ifdef INET6 7468a006adbSBjoern A. Zeeb #ifdef IPFIREWALL_FORWARD 7478a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 7488a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 7498a006adbSBjoern A. Zeeb 7508a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 7518a006adbSBjoern A. Zeeb /* 7528a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 7538a006adbSBjoern A. Zeeb * Already got one like this? 7548a006adbSBjoern A. Zeeb */ 7558a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 7568a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 7578a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 7588a006adbSBjoern A. Zeeb if (!inp) { 7598a006adbSBjoern A. Zeeb /* 7608a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 7618a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 7628a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 7638a006adbSBjoern A. Zeeb */ 7648a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 7658a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 7668a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 7678a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 7688a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 7698a006adbSBjoern A. Zeeb } 7708a006adbSBjoern A. Zeeb /* Remove the tag from the packet. We don't need it anymore. */ 7718a006adbSBjoern A. Zeeb m_tag_delete(m, fwd_tag); 7728a006adbSBjoern A. Zeeb } else 7738a006adbSBjoern A. Zeeb #endif /* IPFIREWALL_FORWARD */ 7748a006adbSBjoern A. Zeeb if (isipv6) { 7758a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 7768a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 7778a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 7788a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 7798a006adbSBjoern A. Zeeb } 7808a006adbSBjoern A. Zeeb #endif /* INET6 */ 7818a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 7828a006adbSBjoern A. Zeeb else 7838a006adbSBjoern A. Zeeb #endif 7848a006adbSBjoern A. Zeeb #ifdef INET 7858a006adbSBjoern A. Zeeb #ifdef IPFIREWALL_FORWARD 7868a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 7879b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 7889b932e9eSAndre Oppermann 7899b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 790f9e354dfSJulian Elischer /* 7912b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 792f9e354dfSJulian Elischer * already got one like this? 793f9e354dfSJulian Elischer */ 794d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 795fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 796d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 797fa046d87SRobert Watson if (!inp) { 798fa046d87SRobert Watson /* 799fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 800d3c1f003SRobert Watson * Because we've rewritten the destination address, 801d3c1f003SRobert Watson * any hardware-generated hash is ignored. 802fa046d87SRobert Watson */ 803fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 804fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 805fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 806fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 807fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 808f9e354dfSJulian Elischer } 8099b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 8109b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 811b10fbdeaSAndre Oppermann } else 8129b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 813d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 814fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 815fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 816d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 8178a006adbSBjoern A. Zeeb #endif /* INET */ 818f9e354dfSJulian Elischer 819df8bae1dSRodney W. Grimes /* 820574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 821574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8228b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 823df8bae1dSRodney W. Grimes */ 824816a3d83SPoul-Henning Kamp if (inp == NULL) { 825574b6964SAndre Oppermann /* 826574b6964SAndre Oppermann * Log communication attempts to ports that are not 827574b6964SAndre Oppermann * in use. 828574b6964SAndre Oppermann */ 829574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 830574b6964SAndre Oppermann tcp_log_in_vain == 2) { 831b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 832df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 833df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8342e4e1b4cSGeoff Rehmet } 835574b6964SAndre Oppermann /* 836574b6964SAndre Oppermann * When blackholing do not respond with a RST but 837574b6964SAndre Oppermann * completely ignore the segment and drop it. 838574b6964SAndre Oppermann */ 839603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 840603724d3SBjoern A. Zeeb V_blackhole == 2) 8411929eae1SAndre Oppermann goto dropunlock; 842574b6964SAndre Oppermann 843a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 844a57815efSBosko Milekic goto dropwithreset; 845816a3d83SPoul-Henning Kamp } 846fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 84780cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 84880cb9f21SKip Macy && (m->m_flags & M_FLOWID) 84980cb9f21SKip Macy && ((inp->inp_socket == NULL) 85080cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 85180cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 85280cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 85380cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 85480cb9f21SKip Macy } 855a2589465SKen Smith #ifdef IPSEC 856a2589465SKen Smith #ifdef INET6 857a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 858603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 859a2589465SKen Smith goto dropunlock; 860a2589465SKen Smith } else 861a2589465SKen Smith #endif /* INET6 */ 862a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 863603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 864a2589465SKen Smith goto dropunlock; 865a2589465SKen Smith } 866a2589465SKen Smith #endif /* IPSEC */ 867a2589465SKen Smith 8688d573cc1SAndre Oppermann /* 8698d573cc1SAndre Oppermann * Check the minimum TTL for socket. 8708d573cc1SAndre Oppermann */ 87134f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 87234f83c52SGeorge V. Neville-Neil #ifdef INET6 87334f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 874302ce8d6SAndre Oppermann goto dropunlock; 87502707462SAndre Oppermann else 87634f83c52SGeorge V. Neville-Neil #endif 87734f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 878302ce8d6SAndre Oppermann goto dropunlock; 87934f83c52SGeorge V. Neville-Neil } 880936cd18dSAndre Oppermann 881340c35deSJonathan Lemon /* 882252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 883252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 884252ca428SRobert Watson * legitimate new connection attempt the old INPCB gets removed and 885252ca428SRobert Watson * we can try again to find a listening socket. 886252ca428SRobert Watson * 887fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 888fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 889fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 890fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 891fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 892fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 893fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 894fa046d87SRobert Watson * 895fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 896340c35deSJonathan Lemon */ 897883e9bc4SRobert Watson relocked: 898ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 899fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 900fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 901252ca428SRobert Watson in_pcbref(inp); 902252ca428SRobert Watson INP_WUNLOCK(inp); 903252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 904252ca428SRobert Watson ti_locked = TI_WLOCKED; 905252ca428SRobert Watson INP_WLOCK(inp); 906fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 907252ca428SRobert Watson inp = NULL; 908252ca428SRobert Watson goto findpcb; 909252ca428SRobert Watson } 910f681a5fdSRobert Watson } else 911252ca428SRobert Watson ti_locked = TI_WLOCKED; 912252ca428SRobert Watson } 913252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 914252ca428SRobert Watson 915340c35deSJonathan Lemon if (thflags & TH_SYN) 916f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9178d573cc1SAndre Oppermann /* 9188d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9198d573cc1SAndre Oppermann */ 9202104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 921340c35deSJonathan Lemon goto findpcb; 922603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 923340c35deSJonathan Lemon return; 924340c35deSJonathan Lemon } 925794235b7SAndre Oppermann /* 926794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 927794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 928794235b7SAndre Oppermann * segment and send an appropriate response. 929794235b7SAndre Oppermann */ 930df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 931a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 932a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 933a57815efSBosko Milekic goto dropwithreset; 93409f81a46SBosko Milekic } 935df8bae1dSRodney W. Grimes 936252ca428SRobert Watson /* 937252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 938fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 939fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 940fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 941fa046d87SRobert Watson * now be in TIMEWAIT. 942252ca428SRobert Watson */ 943fa046d87SRobert Watson #ifdef INVARIANTS 944fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) 945fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 946fa046d87SRobert Watson #endif 947fa046d87SRobert Watson if (tp->t_state != TCPS_ESTABLISHED) { 948fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 949fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 950252ca428SRobert Watson in_pcbref(inp); 951252ca428SRobert Watson INP_WUNLOCK(inp); 952252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 953252ca428SRobert Watson ti_locked = TI_WLOCKED; 954252ca428SRobert Watson INP_WLOCK(inp); 955fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 956252ca428SRobert Watson inp = NULL; 957252ca428SRobert Watson goto findpcb; 958252ca428SRobert Watson } 959883e9bc4SRobert Watson goto relocked; 960f681a5fdSRobert Watson } else 961252ca428SRobert Watson ti_locked = TI_WLOCKED; 962252ca428SRobert Watson } 963252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 964252ca428SRobert Watson } 965252ca428SRobert Watson 966c488362eSRobert Watson #ifdef MAC 9678501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 96830d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 969302ce8d6SAndre Oppermann goto dropunlock; 970c488362eSRobert Watson #endif 971a557af22SRobert Watson so = inp->inp_socket; 972302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 973610ee2f9SDavid Greenman #ifdef TCPDEBUG 974df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 975df8bae1dSRodney W. Grimes ostate = tp->t_state; 97610fe523eSMaxim Konovalov #ifdef INET6 9776f697424SBjoern A. Zeeb if (isipv6) { 978540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 979d30d90dcSMaxim Konovalov } else 9806f697424SBjoern A. Zeeb #endif 981540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 982fb59c426SYoshinobu Inoue tcp_savetcp = *th; 983df8bae1dSRodney W. Grimes } 984b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 985db33b3e6SAndre Oppermann /* 986db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 987db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 988fa046d87SRobert Watson * attempt or the completion of a previous one. Because listen 989fa046d87SRobert Watson * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be 990fa046d87SRobert Watson * held in this case. 991db33b3e6SAndre Oppermann */ 992540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 993540e8b7eSJeffrey Hsu struct in_conninfo inc; 994540e8b7eSJeffrey Hsu 9957824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 9967824d002SAndre Oppermann "tp not listening", __func__)); 997fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 9987824d002SAndre Oppermann 9998bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1000302ce8d6SAndre Oppermann #ifdef INET6 1001be2ac88cSJonathan Lemon if (isipv6) { 1002dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1003be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1004be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1005302ce8d6SAndre Oppermann } else 1006302ce8d6SAndre Oppermann #endif 1007302ce8d6SAndre Oppermann { 1008be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1009be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1010be2ac88cSJonathan Lemon } 1011be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1012be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10137973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1014be2ac88cSJonathan Lemon 1015fb59c426SYoshinobu Inoue /* 10168d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10178d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10188d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1019fb59c426SYoshinobu Inoue */ 1020be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1021bf6d304aSAndre Oppermann /* 1022bf6d304aSAndre Oppermann * Parse the TCP options here because 1023bf6d304aSAndre Oppermann * syncookies need access to the reflected 1024bf6d304aSAndre Oppermann * timestamp. 1025bf6d304aSAndre Oppermann */ 1026bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10279fa198beSAndre Oppermann /* 10289fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10299fa198beSAndre Oppermann * inp and tcpinfo locks. 10309fa198beSAndre Oppermann */ 1031bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 10324195b4afSPaul Traina /* 1033e207f800SAndre Oppermann * No syncache entry or ACK was not 1034be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10358d573cc1SAndre Oppermann * NB: syncache did its own logging 10368d573cc1SAndre Oppermann * of the failure cause. 10374195b4afSPaul Traina */ 1038be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1039be2ac88cSJonathan Lemon goto dropwithreset; 1040be2ac88cSJonathan Lemon } 1041f76fcf6dSJeffrey Hsu if (so == NULL) { 1042be2ac88cSJonathan Lemon /* 1043e207f800SAndre Oppermann * We completed the 3-way handshake 1044e207f800SAndre Oppermann * but could not allocate a socket 1045e207f800SAndre Oppermann * either due to memory shortage, 1046e207f800SAndre Oppermann * listen queue length limits or 1047a160e630SAndre Oppermann * global socket limits. Send RST 1048a160e630SAndre Oppermann * or wait and have the remote end 1049a160e630SAndre Oppermann * retransmit the ACK for another 1050a160e630SAndre Oppermann * try. 1051be2ac88cSJonathan Lemon */ 1052a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1053a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10548d573cc1SAndre Oppermann "Socket allocation failed due to " 10558d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1056ac957cd2SJulian Elischer s, __func__, 1057ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1058ac957cd2SJulian Elischer "sending RST" : "try again"); 1059603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1060e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1061e207f800SAndre Oppermann goto dropwithreset; 1062a160e630SAndre Oppermann } else 1063a160e630SAndre Oppermann goto dropunlock; 1064f76fcf6dSJeffrey Hsu } 1065be2ac88cSJonathan Lemon /* 1066be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 10678d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 10688d573cc1SAndre Oppermann * created socket and update the tp variable. 1069be2ac88cSJonathan Lemon */ 10708501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1071be2ac88cSJonathan Lemon inp = sotoinpcb(so); 10728501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 1073be2ac88cSJonathan Lemon tp = intotcpcb(inp); 10748d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 10758d573cc1SAndre Oppermann ("%s: ", __func__)); 10762903309aSAttilio Rao #ifdef TCP_SIGNATURE 10772903309aSAttilio Rao if (sig_checked == 0) { 10782903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 10792903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 10802903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, 10812903309aSAttilio Rao optlen, &to, th, tp->t_flags)) { 10822903309aSAttilio Rao 10832903309aSAttilio Rao /* 10842903309aSAttilio Rao * In SYN_SENT state if it receives an 10852903309aSAttilio Rao * RST, it is allowed for further 10862903309aSAttilio Rao * processing. 10872903309aSAttilio Rao */ 10882903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 10892903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 10902903309aSAttilio Rao goto dropunlock; 10912903309aSAttilio Rao } 10922903309aSAttilio Rao sig_checked = 1; 10932903309aSAttilio Rao } 10942903309aSAttilio Rao #endif 10952903309aSAttilio Rao 1096be2ac88cSJonathan Lemon /* 1097302ce8d6SAndre Oppermann * Process the segment and the data it 1098302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1099302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1100302ce8d6SAndre Oppermann */ 1101f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1102252ca428SRobert Watson iptos, ti_locked); 1103603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1104302ce8d6SAndre Oppermann return; 1105be2ac88cSJonathan Lemon } 1106a160e630SAndre Oppermann /* 11078d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11088d573cc1SAndre Oppermann * 1109a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1110a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1111a160e630SAndre Oppermann * retransmits. 111219bc77c5SAndre Oppermann * 111319bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 111419bc77c5SAndre Oppermann * causes. 1115a160e630SAndre Oppermann */ 1116a160e630SAndre Oppermann if (thflags & TH_RST) { 111719bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1118a160e630SAndre Oppermann goto dropunlock; 1119a160e630SAndre Oppermann } 1120a160e630SAndre Oppermann /* 1121a160e630SAndre Oppermann * We can't do anything without SYN. 1122a160e630SAndre Oppermann */ 1123a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1124a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1125a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1126773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11278d573cc1SAndre Oppermann s, __func__); 112878b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1129a160e630SAndre Oppermann goto dropunlock; 1130a160e630SAndre Oppermann } 1131a160e630SAndre Oppermann /* 1132a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1133a160e630SAndre Oppermann */ 1134fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1135a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1136a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11378d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11388d573cc1SAndre Oppermann s, __func__); 1139a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 114078b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1141a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1142a57815efSBosko Milekic goto dropwithreset; 11434195b4afSPaul Traina } 1144a160e630SAndre Oppermann /* 1145a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1146a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1147a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1148a160e630SAndre Oppermann * TCP/IP stack. 1149a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1150a160e630SAndre Oppermann * and is constantly refining its stack detection 1151a160e630SAndre Oppermann * strategies. 11528d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1153a160e630SAndre Oppermann * and was used by RFC1644. 1154a160e630SAndre Oppermann */ 1155603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1156a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1157a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1158773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 11598d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 116078b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1161302ce8d6SAndre Oppermann goto dropunlock; 1162ebb0cbeaSPaul Traina } 1163be2ac88cSJonathan Lemon /* 1164be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1165a160e630SAndre Oppermann * 1166a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1167a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1168a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1169be2ac88cSJonathan Lemon */ 1170a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1171a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1172a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1173a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 117433841545SHajimu UMEMOTO #ifdef INET6 117533841545SHajimu UMEMOTO /* 117633841545SHajimu UMEMOTO * If deprecated address is forbidden, 117733841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 117833841545SHajimu UMEMOTO * address to prevent any new inbound connection from 117933841545SHajimu UMEMOTO * getting established. 118033841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 118133841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 118233841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 118333841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 118433841545SHajimu UMEMOTO * for the exchange. 118533841545SHajimu UMEMOTO * 118633841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 118733841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 118833841545SHajimu UMEMOTO * SYN in this case. 118933841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 119033841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 119133841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 119233841545SHajimu UMEMOTO * used" 119333841545SHajimu UMEMOTO * 2. use of it with new communication: 119433841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 119533841545SHajimu UMEMOTO * with sufficient scope is available" 119633841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 119733841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 119833841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 119933841545SHajimu UMEMOTO * 120033841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 120133841545SHajimu UMEMOTO * multiple description text for deprecated address 120233841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 120333841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 120433841545SHajimu UMEMOTO */ 1205603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 120633841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 120733841545SHajimu UMEMOTO 12088c0fec80SRobert Watson ia6 = ip6_getdstifaddr(m); 12098c0fec80SRobert Watson if (ia6 != NULL && 121033841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12118c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1212a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1213a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12148d573cc1SAndre Oppermann "Connection attempt to deprecated " 12158d573cc1SAndre Oppermann "IPv6 address rejected\n", 1216a160e630SAndre Oppermann s, __func__); 121733841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 121833841545SHajimu UMEMOTO goto dropwithreset; 121933841545SHajimu UMEMOTO } 12208c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 122133841545SHajimu UMEMOTO } 1222b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 122365f28919SJesper Skriver /* 1224db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12258d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1226db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12278d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12288d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12298d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 123093ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 123193ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 123293ec91baSCrist J. Clark * in_broadcast() to find them. 1233be2ac88cSJonathan Lemon */ 12348d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12358d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12368d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12378d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1238773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1239302ce8d6SAndre Oppermann goto dropunlock; 12408d573cc1SAndre Oppermann } 1241db33b3e6SAndre Oppermann #ifdef INET6 1242b287c6c7SBjoern A. Zeeb if (isipv6) { 1243db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1244a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1245a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1246a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12478d573cc1SAndre Oppermann "Connection attempt to/from self " 1248773673c1SAndre Oppermann "ignored\n", s, __func__); 1249302ce8d6SAndre Oppermann goto dropunlock; 1250a160e630SAndre Oppermann } 1251be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1252a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1253a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1254a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12558d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1256773673c1SAndre Oppermann "address ignored\n", s, __func__); 1257302ce8d6SAndre Oppermann goto dropunlock; 1258a160e630SAndre Oppermann } 1259b287c6c7SBjoern A. Zeeb } 1260db33b3e6SAndre Oppermann #endif 1261b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1262b287c6c7SBjoern A. Zeeb else 1263b287c6c7SBjoern A. Zeeb #endif 1264b287c6c7SBjoern A. Zeeb #ifdef INET 1265b287c6c7SBjoern A. Zeeb { 1266db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1267a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1268a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1269a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12708d573cc1SAndre Oppermann "Connection attempt from/to self " 1271773673c1SAndre Oppermann "ignored\n", s, __func__); 1272302ce8d6SAndre Oppermann goto dropunlock; 1273a160e630SAndre Oppermann } 1274be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1275be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 12762ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1277a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1278a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1279a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12808d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1281773673c1SAndre Oppermann "or multicast address ignored\n", 1282a160e630SAndre Oppermann s, __func__); 1283302ce8d6SAndre Oppermann goto dropunlock; 1284c068736aSJeffrey Hsu } 1285a160e630SAndre Oppermann } 1286b287c6c7SBjoern A. Zeeb #endif 1287be2ac88cSJonathan Lemon /* 1288db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1289db33b3e6SAndre Oppermann * for syncache. 1290be2ac88cSJonathan Lemon */ 12913c653157SHartmut Brandt #ifdef TCPDEBUG 12923c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12933c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12943c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12953c653157SHartmut Brandt #endif 1296f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 12974d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 1298be2ac88cSJonathan Lemon /* 12994d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1300a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1301be2ac88cSJonathan Lemon */ 1302603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1303be2ac88cSJonathan Lemon return; 1304f76fcf6dSJeffrey Hsu } 1305db33b3e6SAndre Oppermann 13062903309aSAttilio Rao #ifdef TCP_SIGNATURE 13072903309aSAttilio Rao if (sig_checked == 0) { 13082903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 13092903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 13102903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 13112903309aSAttilio Rao th, tp->t_flags)) { 13122903309aSAttilio Rao 13132903309aSAttilio Rao /* 13142903309aSAttilio Rao * In SYN_SENT state if it receives an RST, it is 13152903309aSAttilio Rao * allowed for further processing. 13162903309aSAttilio Rao */ 13172903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 13182903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 13192903309aSAttilio Rao goto dropunlock; 13202903309aSAttilio Rao } 13212903309aSAttilio Rao sig_checked = 1; 13222903309aSAttilio Rao } 13232903309aSAttilio Rao #endif 13242903309aSAttilio Rao 1325302ce8d6SAndre Oppermann /* 13268d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13278d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13288d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1329302ce8d6SAndre Oppermann */ 1330252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1331603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1332302ce8d6SAndre Oppermann return; 1333be2ac88cSJonathan Lemon 1334302ce8d6SAndre Oppermann dropwithreset: 1335fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1336dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1337252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1338fa046d87SRobert Watson } 1339fa046d87SRobert Watson #ifdef INVARIANTS 1340fa046d87SRobert Watson else { 1341fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1342fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1343fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1344fa046d87SRobert Watson } 1345fa046d87SRobert Watson #endif 1346014ea782SRobert Watson 1347014ea782SRobert Watson if (inp != NULL) { 1348302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1349014ea782SRobert Watson INP_WUNLOCK(inp); 1350dd8ac7f9SRobert Watson } else 1351014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1352302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1353014ea782SRobert Watson goto drop; 1354014ea782SRobert Watson 1355302ce8d6SAndre Oppermann dropunlock: 1356fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1357252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1358252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1359fa046d87SRobert Watson } 1360fa046d87SRobert Watson #ifdef INVARIANTS 1361fa046d87SRobert Watson else { 1362fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1363fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1364fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1365fa046d87SRobert Watson } 1366fa046d87SRobert Watson #endif 1367252ca428SRobert Watson 13689fa198beSAndre Oppermann if (inp != NULL) 13698501a69cSRobert Watson INP_WUNLOCK(inp); 1370014ea782SRobert Watson 1371302ce8d6SAndre Oppermann drop: 1372603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1373a160e630SAndre Oppermann if (s != NULL) 1374a160e630SAndre Oppermann free(s, M_TCPLOG); 1375302ce8d6SAndre Oppermann if (m != NULL) 1376302ce8d6SAndre Oppermann m_freem(m); 1377302ce8d6SAndre Oppermann } 1378302ce8d6SAndre Oppermann 1379679d9708SAndre Oppermann static void 1380302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1381252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1382252ca428SRobert Watson int ti_locked) 1383302ce8d6SAndre Oppermann { 1384302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1385302ce8d6SAndre Oppermann int rstreason, todrop, win; 1386302ce8d6SAndre Oppermann u_long tiwin; 1387302ce8d6SAndre Oppermann struct tcpopt to; 1388302ce8d6SAndre Oppermann 138914739780SMaxim Konovalov #ifdef TCPDEBUG 139014739780SMaxim Konovalov /* 139114739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 139214739780SMaxim Konovalov * now IPv6. 139314739780SMaxim Konovalov */ 139414739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 139514739780SMaxim Konovalov struct tcphdr tcp_savetcp; 139614739780SMaxim Konovalov short ostate = 0; 139714739780SMaxim Konovalov #endif 1398302ce8d6SAndre Oppermann thflags = th->th_flags; 1399d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1400302ce8d6SAndre Oppermann 1401252ca428SRobert Watson /* 1402252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1403252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 1404252ca428SRobert Watson * allow either a read lock or a write lock, as we may have acquired 1405252ca428SRobert Watson * a write lock due to a race. 1406252ca428SRobert Watson * 1407d15fb965SRobert Watson * Require a global write lock for SYN/FIN/RST segments or 1408252ca428SRobert Watson * non-established connections; otherwise accept either a read or 1409252ca428SRobert Watson * write lock, as we may have conservatively acquired a write lock in 1410252ca428SRobert Watson * certain cases in tcp_input() (is this still true?). Currently we 1411252ca428SRobert Watson * will never enter with no lock, so we try to drop it quickly in the 1412252ca428SRobert Watson * common pure ack/pure data cases. 1413252ca428SRobert Watson */ 1414252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1415252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1416252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1417252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1418603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1419252ca428SRobert Watson } else { 1420252ca428SRobert Watson #ifdef INVARIANTS 1421fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1422252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1423fa046d87SRobert Watson else { 1424fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1425fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1426fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1427fa046d87SRobert Watson } 1428252ca428SRobert Watson #endif 1429252ca428SRobert Watson } 14308501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1431679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1432679d9708SAndre Oppermann __func__)); 1433679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1434679d9708SAndre Oppermann __func__)); 1435df8bae1dSRodney W. Grimes 1436df8bae1dSRodney W. Grimes /* 1437df8bae1dSRodney W. Grimes * Segment received on connection. 1438df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1439e8949f74SAndre Oppermann * XXX: This should be done after segment 1440e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1441df8bae1dSRodney W. Grimes */ 14429b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 14437ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1444b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1445df8bae1dSRodney W. Grimes 1446df8bae1dSRodney W. Grimes /* 1447464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1448e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1449464fcfbcSAndre Oppermann */ 1450464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1451464fcfbcSAndre Oppermann 1452464fcfbcSAndre Oppermann /* 1453f2512ba1SRui Paulo * TCP ECN processing. 1454f2512ba1SRui Paulo */ 1455f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 14569c251892SRui Paulo if (thflags & TH_CWR) 14579c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1458f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1459f2512ba1SRui Paulo case IPTOS_ECN_CE: 1460f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 146178b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1462f2512ba1SRui Paulo break; 1463f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 146478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1465f2512ba1SRui Paulo break; 1466f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 146778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1468f2512ba1SRui Paulo break; 1469f2512ba1SRui Paulo } 1470dbc42409SLawrence Stewart /* Congestion experienced. */ 1471dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1472dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1473f2512ba1SRui Paulo } 1474f2512ba1SRui Paulo } 1475f2512ba1SRui Paulo 1476f2512ba1SRui Paulo /* 1477f72167f4SAndre Oppermann * Parse options on any incoming segment. 1478f72167f4SAndre Oppermann */ 1479302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1480302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1481302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1482f72167f4SAndre Oppermann 1483f72167f4SAndre Oppermann /* 1484f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1485bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1486bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1487bf6d304aSAndre Oppermann * was established. 1488f72167f4SAndre Oppermann */ 1489bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1490e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1491bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1492f72167f4SAndre Oppermann to.to_tsecr = 0; 1493bf6d304aSAndre Oppermann } 1494f72167f4SAndre Oppermann 1495f72167f4SAndre Oppermann /* 149697d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 149797d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 14985396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 14995396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 150097d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1501df8bae1dSRodney W. Grimes */ 15020a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1503464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1504464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1505be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 150602a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1507be2ac88cSJonathan Lemon } 15085396d0f8SAndre Oppermann /* 15095396d0f8SAndre Oppermann * Initial send window. It will be updated with 15105396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 15115396d0f8SAndre Oppermann */ 15125396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1513be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1514be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1515be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1516be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1517be2ac88cSJonathan Lemon } 1518be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1519be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 15203529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 15213529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 15223529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 15236d90faf3SPaul Saab } 15246d90faf3SPaul Saab 1525df8bae1dSRodney W. Grimes /* 1526df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1527df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1528df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1529df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1530df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1531df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1532df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1533df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1534df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1535df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1536df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1537df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1538a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1539c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1540a0292f23SGarrett Wollman * be TH_NEEDSYN. 1541df8bae1dSRodney W. Grimes */ 1542df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1543c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1544fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1545c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1546c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1547a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1548c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1549be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1550c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1551df8bae1dSRodney W. Grimes 1552df8bae1dSRodney W. Grimes /* 1553df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1554df8bae1dSRodney W. Grimes * record the timestamp. 1555a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1556a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1557df8bae1dSRodney W. Grimes */ 1558be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1559fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 15609b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1561a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1562df8bae1dSRodney W. Grimes } 1563df8bae1dSRodney W. Grimes 1564fb59c426SYoshinobu Inoue if (tlen == 0) { 1565fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1566fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1567dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1568fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1569dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1570df8bae1dSRodney W. Grimes /* 1571e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1572df8bae1dSRodney W. Grimes */ 1573fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1574252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1575252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1576252ca428SRobert Watson 157778b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1578252ca428SRobert Watson 15799b8b58e0SJonathan Lemon /* 1580e8949f74SAndre Oppermann * "bad retransmit" recovery. 15819b8b58e0SJonathan Lemon */ 15829b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1583672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 15846dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1585dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 15869b8b58e0SJonathan Lemon } 1587fa55172bSMatthew Dillon 1588fa55172bSMatthew Dillon /* 1589fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1590fa55172bSMatthew Dillon * 1591fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1592fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1593fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1594fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1595fa55172bSMatthew Dillon */ 1596fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1597fa55172bSMatthew Dillon to.to_tsecr) { 1598eaf80179SAndre Oppermann if (!tp->t_rttlow || 1599eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1600eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1601a0292f23SGarrett Wollman tcp_xmit_timer(tp, 16029b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1603fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1604fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1605eaf80179SAndre Oppermann if (!tp->t_rttlow || 1606eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1607eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1608c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1609c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1610fa55172bSMatthew Dillon } 1611dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 161239bc9de5SLawrence Stewart 161339bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 161439bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 161539bc9de5SLawrence Stewart 161678b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 161778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1618df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 16199d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 16209d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 16219d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1622dbc42409SLawrence Stewart 1623dbc42409SLawrence Stewart /* 1624dbc42409SLawrence Stewart * Let the congestion control algorithm update 1625dbc42409SLawrence Stewart * congestion control related information. This 1626dbc42409SLawrence Stewart * typically means increasing the congestion 1627dbc42409SLawrence Stewart * window. 1628dbc42409SLawrence Stewart */ 1629dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 1630dbc42409SLawrence Stewart 16319d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 16321645d090SJeff Roberson /* 1633e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 16341645d090SJeff Roberson * to th_ack. 16351645d090SJeff Roberson */ 1636cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1637b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1638df8bae1dSRodney W. Grimes m_freem(m); 1639e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1640df8bae1dSRodney W. Grimes 1641df8bae1dSRodney W. Grimes /* 1642df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1643df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1644df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1645df8bae1dSRodney W. Grimes * If process is waiting for space, 1646df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1647df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1648df8bae1dSRodney W. Grimes * decide between more output or persist. 1649e8949f74SAndre Oppermann */ 16503c653157SHartmut Brandt #ifdef TCPDEBUG 16513c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 16523c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 16533c653157SHartmut Brandt (void *)tcp_saveipgen, 16543c653157SHartmut Brandt &tcp_savetcp, 0); 16553c653157SHartmut Brandt #endif 1656df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1657b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1658b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1659b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1660b8152ba7SAndre Oppermann tp->t_rxtcur); 1661df8bae1dSRodney W. Grimes sowwakeup(so); 1662df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1663df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1664a14c749fSJonathan Lemon goto check_delack; 1665df8bae1dSRodney W. Grimes } 1666fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1667fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 16686741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 16696741ecf5SAndre Oppermann 1670df8bae1dSRodney W. Grimes /* 1671252ca428SRobert Watson * This is a pure, in-sequence data packet with 1672252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1673252ca428SRobert Watson * buffer space to take it. 1674df8bae1dSRodney W. Grimes */ 1675fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1676252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1677252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1678252ca428SRobert Watson 16796d90faf3SPaul Saab /* Clean receiver SACK report if present */ 16803529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 16816d90faf3SPaul Saab tcp_clean_sackreport(tp); 168278b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1683fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 16841645d090SJeff Roberson /* 16851645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 16861645d090SJeff Roberson * th_seq. 16871645d090SJeff Roberson */ 16881645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 16891645d090SJeff Roberson /* 16901645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 16911645d090SJeff Roberson * rcv_nxt. 16921645d090SJeff Roberson */ 16931645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 169478b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 169578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 1696e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 16973c653157SHartmut Brandt #ifdef TCPDEBUG 16983c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 16993c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17003c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 17013c653157SHartmut Brandt #endif 17026741ecf5SAndre Oppermann /* 17036741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 17046741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 17056741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 17066741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 17076741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 17086741ecf5SAndre Oppermann * 17096741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 17106741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 17116741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 17126741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 17136741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 17146741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 17156741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 17166741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 17176741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 17186741ecf5SAndre Oppermann * reassembly queue. 17196741ecf5SAndre Oppermann * 17206741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 17216741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 17226741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 17236741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 17246741ecf5SAndre Oppermann * current socket buffer size; 17256741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 17266741ecf5SAndre Oppermann * 17276741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 17286741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 17296741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 17306741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 17316741ecf5SAndre Oppermann * 17326741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 17336741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1734df8bae1dSRodney W. Grimes */ 1735603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 17366741ecf5SAndre Oppermann to.to_tsecr && 17376741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 17388502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 17396741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 17406741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 17416741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 17426741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1743603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 17446741ecf5SAndre Oppermann newsize = 17456741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1746603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1747603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 17486741ecf5SAndre Oppermann } 17496741ecf5SAndre Oppermann /* Start over with next RTT. */ 17506741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 17516741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 17526741ecf5SAndre Oppermann } else 17536741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 17546741ecf5SAndre Oppermann } 17556741ecf5SAndre Oppermann 17566741ecf5SAndre Oppermann /* Add data to socket buffer. */ 17571e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1758c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1759c1c36a2cSMike Silbersack m_freem(m); 1760c1c36a2cSMike Silbersack } else { 17616741ecf5SAndre Oppermann /* 17626741ecf5SAndre Oppermann * Set new socket buffer size. 17636741ecf5SAndre Oppermann * Give up when limit is reached. 17646741ecf5SAndre Oppermann */ 17656741ecf5SAndre Oppermann if (newsize) 17666741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 17676c8286e4SRobert Watson newsize, so, NULL)) 17686741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1769fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 17701e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1771c1c36a2cSMike Silbersack } 1772e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 17731e4d7da7SRobert Watson sorwakeup_locked(so); 1774d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 17753bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1776f498eeeeSDavid Greenman } else { 1777e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1778e612a582SDavid Greenman tcp_output(tp); 1779e612a582SDavid Greenman } 1780a14c749fSJonathan Lemon goto check_delack; 1781df8bae1dSRodney W. Grimes } 1782df8bae1dSRodney W. Grimes } 1783df8bae1dSRodney W. Grimes 1784df8bae1dSRodney W. Grimes /* 1785df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1786df8bae1dSRodney W. Grimes * and then do TCP input processing. 1787df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1788df8bae1dSRodney W. Grimes * but not less than advertised window. 1789df8bae1dSRodney W. Grimes */ 1790df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1791df8bae1dSRodney W. Grimes if (win < 0) 1792df8bae1dSRodney W. Grimes win = 0; 1793f701e30dSJohn Baldwin KASSERT(SEQ_GEQ(tp->rcv_adv, tp->rcv_nxt), 1794f701e30dSJohn Baldwin ("tcp_input negative window: tp %p rcv_nxt %u rcv_adv %u", tp, 17955891ebd6SJohn Baldwin tp->rcv_nxt, tp->rcv_adv)); 179666e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1797df8bae1dSRodney W. Grimes 17986741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 17996741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18006741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18016741ecf5SAndre Oppermann 1802df8bae1dSRodney W. Grimes switch (tp->t_state) { 1803df8bae1dSRodney W. Grimes 1804df8bae1dSRodney W. Grimes /* 1805764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1806764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1807764d8cefSBill Fenner */ 1808764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1809fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1810fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1811a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1812a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1813a57815efSBosko Milekic goto dropwithreset; 1814a57815efSBosko Milekic } 1815764d8cefSBill Fenner break; 1816764d8cefSBill Fenner 1817764d8cefSBill Fenner /* 1818df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1819df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1820df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1821df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1822df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1823df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1824df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1825f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1826f2512ba1SRui Paulo * is ECN capable. 1827df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1828df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1829df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1830df8bae1dSRodney W. Grimes */ 1831df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1832fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1833fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1834fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1835a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1836a0292f23SGarrett Wollman goto dropwithreset; 1837a0292f23SGarrett Wollman } 18380ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1839df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 18400ca3f933SAndre Oppermann if (thflags & TH_RST) 1841df8bae1dSRodney W. Grimes goto drop; 18420ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1843df8bae1dSRodney W. Grimes goto drop; 1844464fcfbcSAndre Oppermann 1845fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1846df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1847fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 184878b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1849845799c1SAndras Olah soisconnected(so); 1850c488362eSRobert Watson #ifdef MAC 185130d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1852c488362eSRobert Watson #endif 1853845799c1SAndras Olah /* Do window scaling on this connection? */ 1854845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1855845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1856845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1857845799c1SAndras Olah } 1858766282cbSJohn Baldwin tp->rcv_adv += imin(tp->rcv_wnd, 1859766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1860a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1861a0292f23SGarrett Wollman /* 1862a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1863a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1864a0292f23SGarrett Wollman */ 1865d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1866b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1867b8152ba7SAndre Oppermann tcp_delacktime); 1868a0292f23SGarrett Wollman else 1869a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1870f2512ba1SRui Paulo 1871603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1872f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 187378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1874f2512ba1SRui Paulo } 1875f2512ba1SRui Paulo 1876a0292f23SGarrett Wollman /* 1877a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1878a0292f23SGarrett Wollman * Transitions: 1879a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1880a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1881a0292f23SGarrett Wollman */ 18829b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1883a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1884a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1885a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1886fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 18877ff19458SPaul Traina } else { 1888a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1889dbc42409SLawrence Stewart cc_conn_init(tp); 1890b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 18917ff19458SPaul Traina } 1892a0292f23SGarrett Wollman } else { 1893a0292f23SGarrett Wollman /* 1894c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1895c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1896c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1897c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1898c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1899a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1900a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1901a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1902a0292f23SGarrett Wollman */ 19034b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1904b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1905df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1906a0292f23SGarrett Wollman } 1907df8bae1dSRodney W. Grimes 1908252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1909252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1910252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 19118501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 19127cfc6904SRobert Watson 1913df8bae1dSRodney W. Grimes /* 1914fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1915df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1916df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1917df8bae1dSRodney W. Grimes */ 1918fb59c426SYoshinobu Inoue th->th_seq++; 1919fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1920fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1921df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1922fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1923fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 192478b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 192578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1926df8bae1dSRodney W. Grimes } 1927fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1928fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1929a0292f23SGarrett Wollman /* 1930a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1931a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1932a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1933a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1934a0292f23SGarrett Wollman * Otherwise, goto step 6. 1935a0292f23SGarrett Wollman */ 1936fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1937a0292f23SGarrett Wollman goto process_ACK; 1938c068736aSJeffrey Hsu 1939df8bae1dSRodney W. Grimes goto step6; 1940c068736aSJeffrey Hsu 1941a0292f23SGarrett Wollman /* 1942a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1943c94c54e4SAndre Oppermann * do normal processing. 1944a0292f23SGarrett Wollman * 1945c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1946a0292f23SGarrett Wollman */ 1947a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1948a0292f23SGarrett Wollman case TCPS_CLOSING: 1949a0292f23SGarrett Wollman break; /* continue normal processing */ 1950df8bae1dSRodney W. Grimes } 1951df8bae1dSRodney W. Grimes 1952df8bae1dSRodney W. Grimes /* 1953df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 195480ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 195580ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 195680ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 195780ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 195880ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 195980ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 196080ab7c0eSGarrett Wollman * killing a connection). 196180ab7c0eSGarrett Wollman * Then check timestamp, if present. 1962a0292f23SGarrett Wollman * Then check the connection count, if present. 1963df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1964df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1965df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1966df8bae1dSRodney W. Grimes * 196780ab7c0eSGarrett Wollman * 196880ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 196980ab7c0eSGarrett Wollman * if this is a valid reset segment. 197080ab7c0eSGarrett Wollman * RFC 793 page 37: 197180ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 197280ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 197380ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 197480ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 197580ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 19761a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 19771a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 19781a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 19791a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 198080dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 198180dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 198280dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 198380dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 198480dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 198580dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 19868e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 198780ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 198880ab7c0eSGarrett Wollman * but they will eventually catch up. 198980ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 199080ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 199180ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 199280ab7c0eSGarrett Wollman * 199380ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 199480ab7c0eSGarrett Wollman * 199580ab7c0eSGarrett Wollman * DISCUSSION 199680ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 199780ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 199880ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 199980ab7c0eSGarrett Wollman * data. 200080ab7c0eSGarrett Wollman * 200180ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 200280ab7c0eSGarrett Wollman * the state: 200380ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 200480ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 200580ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 2006745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 200780ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 2008e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 200980ab7c0eSGarrett Wollman * Close the tcb. 2010e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 201180ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 201280ab7c0eSGarrett Wollman * RFC 1337. 201380ab7c0eSGarrett Wollman */ 2014fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 201595ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 201695ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 201780ab7c0eSGarrett Wollman switch (tp->t_state) { 201880ab7c0eSGarrett Wollman 201980ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 202080ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 202180ab7c0eSGarrett Wollman goto close; 202280ab7c0eSGarrett Wollman 202380ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 2024603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 202595ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 202695ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 202795ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 202895ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 202978b50714SRobert Watson TCPSTAT_INC(tcps_badrst); 203080dd2a81SMike Silbersack goto drop; 203180dd2a81SMike Silbersack } 2032564aab1fSAndre Oppermann /* FALLTHROUGH */ 203380ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 203480ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 203580ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 203680ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 203780ab7c0eSGarrett Wollman close: 2038252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2039252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 2040252ca428SRobert Watson ti_locked)); 2041252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2042252ca428SRobert Watson 204380ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 204478b50714SRobert Watson TCPSTAT_INC(tcps_drops); 204580ab7c0eSGarrett Wollman tp = tcp_close(tp); 204680ab7c0eSGarrett Wollman break; 204780ab7c0eSGarrett Wollman 204880ab7c0eSGarrett Wollman case TCPS_CLOSING: 204980ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 2050252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2051252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 2052252ca428SRobert Watson ti_locked)); 2053252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2054252ca428SRobert Watson 205580ab7c0eSGarrett Wollman tp = tcp_close(tp); 205680ab7c0eSGarrett Wollman break; 205780ab7c0eSGarrett Wollman } 205880ab7c0eSGarrett Wollman } 205980ab7c0eSGarrett Wollman goto drop; 206080ab7c0eSGarrett Wollman } 206180ab7c0eSGarrett Wollman 206280ab7c0eSGarrett Wollman /* 2063df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2064df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2065df8bae1dSRodney W. Grimes */ 2066be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 206780ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2068df8bae1dSRodney W. Grimes 2069df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 20701a0e7cfcSJohn Baldwin if (ticks - tp->ts_recent_age > TCP_PAWS_IDLE) { 2071df8bae1dSRodney W. Grimes /* 2072df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2073df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2074df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2075df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2076df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2077df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2078df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2079df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2080df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2081df8bae1dSRodney W. Grimes */ 2082df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2083df8bae1dSRodney W. Grimes } else { 208478b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 208578b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 208678b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 208707fd333dSMatthew Dillon if (tlen) 2088df8bae1dSRodney W. Grimes goto dropafterack; 20891ab4789dSMatthew Dillon goto drop; 20901ab4789dSMatthew Dillon } 2091df8bae1dSRodney W. Grimes } 2092df8bae1dSRodney W. Grimes 2093a0292f23SGarrett Wollman /* 209480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 209580ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 209680ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 209780ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 209880ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 209980ab7c0eSGarrett Wollman */ 2100a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2101a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2102a57815efSBosko Milekic goto dropwithreset; 2103a57815efSBosko Milekic } 210480ab7c0eSGarrett Wollman 2105fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2106df8bae1dSRodney W. Grimes if (todrop > 0) { 210781ad7eb0SZachary Loafman /* 210881ad7eb0SZachary Loafman * If this is a duplicate SYN for our current connection, 210981ad7eb0SZachary Loafman * advance over it and pretend and it's not a SYN. 211081ad7eb0SZachary Loafman */ 211181ad7eb0SZachary Loafman if (thflags & TH_SYN && th->th_seq == tp->irs) { 2112fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2113fb59c426SYoshinobu Inoue th->th_seq++; 2114fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2115fb59c426SYoshinobu Inoue th->th_urp--; 2116df8bae1dSRodney W. Grimes else 2117fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2118df8bae1dSRodney W. Grimes todrop--; 2119df8bae1dSRodney W. Grimes } 2120df8bae1dSRodney W. Grimes /* 2121dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2122df8bae1dSRodney W. Grimes */ 2123fb59c426SYoshinobu Inoue if (todrop > tlen 2124fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2125dac20301SGarrett Wollman /* 2126dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2127dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2128dac20301SGarrett Wollman * of sequence; drop it. 2129dac20301SGarrett Wollman */ 2130fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2131dac20301SGarrett Wollman 2132df8bae1dSRodney W. Grimes /* 2133dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2134dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2135df8bae1dSRodney W. Grimes */ 2136dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2137fb59c426SYoshinobu Inoue todrop = tlen; 213878b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 213978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2140df8bae1dSRodney W. Grimes } else { 214178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 214278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2143df8bae1dSRodney W. Grimes } 2144fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2145fb59c426SYoshinobu Inoue th->th_seq += todrop; 2146fb59c426SYoshinobu Inoue tlen -= todrop; 2147fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2148fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2149df8bae1dSRodney W. Grimes else { 2150fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2151fb59c426SYoshinobu Inoue th->th_urp = 0; 2152df8bae1dSRodney W. Grimes } 2153df8bae1dSRodney W. Grimes } 2154df8bae1dSRodney W. Grimes 2155df8bae1dSRodney W. Grimes /* 2156df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2157df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2158df8bae1dSRodney W. Grimes */ 2159df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2160fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2161773673c1SAndre Oppermann char *s; 2162773673c1SAndre Oppermann 2163252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 2164252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2165252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2166252ca428SRobert Watson 2167773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 2168e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 2169773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 2170e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2171773673c1SAndre Oppermann free(s, M_TCPLOG); 2172773673c1SAndre Oppermann } 2173df8bae1dSRodney W. Grimes tp = tcp_close(tp); 217478b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2175a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2176df8bae1dSRodney W. Grimes goto dropwithreset; 2177df8bae1dSRodney W. Grimes } 2178df8bae1dSRodney W. Grimes 2179df8bae1dSRodney W. Grimes /* 2180df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2181df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2182df8bae1dSRodney W. Grimes */ 2183fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2184df8bae1dSRodney W. Grimes if (todrop > 0) { 218578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2186fb59c426SYoshinobu Inoue if (todrop >= tlen) { 218778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2188df8bae1dSRodney W. Grimes /* 2189df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2190df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2191df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2192df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2193df8bae1dSRodney W. Grimes * and ack. 2194df8bae1dSRodney W. Grimes */ 2195fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2196df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 219778b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2198df8bae1dSRodney W. Grimes } else 2199df8bae1dSRodney W. Grimes goto dropafterack; 2200df8bae1dSRodney W. Grimes } else 220178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2202df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2203fb59c426SYoshinobu Inoue tlen -= todrop; 2204fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2205df8bae1dSRodney W. Grimes } 2206df8bae1dSRodney W. Grimes 2207df8bae1dSRodney W. Grimes /* 2208df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2209df8bae1dSRodney W. Grimes * record its timestamp. 2210cf09195bSPaul Saab * NOTE: 2211cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2212a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2213cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2214cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2215cf09195bSPaul Saab * predicated on the sequence space of this segment. 2216cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2217cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2218cf09195bSPaul Saab * instead of RFC1323's 2219cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2220cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2221cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2222cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2223cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2224df8bae1dSRodney W. Grimes */ 2225be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2226cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2227cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2228cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 22299b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 2230a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2231df8bae1dSRodney W. Grimes } 2232df8bae1dSRodney W. Grimes 2233df8bae1dSRodney W. Grimes /* 2234df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 2235df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 2236df8bae1dSRodney W. Grimes */ 2237fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 2238252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2239252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2240252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2241252ca428SRobert Watson 2242df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 2243a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2244122aad88SAndre Oppermann goto drop; 2245df8bae1dSRodney W. Grimes } 2246df8bae1dSRodney W. Grimes 2247a0292f23SGarrett Wollman /* 2248a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2249a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2250a0292f23SGarrett Wollman * later processing; else drop segment and return. 2251a0292f23SGarrett Wollman */ 2252fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2253a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2254a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 2255a0292f23SGarrett Wollman goto step6; 22565e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 22575e1aa279SDavid Malone goto dropafterack; 2258a0292f23SGarrett Wollman else 2259a0292f23SGarrett Wollman goto drop; 2260a0292f23SGarrett Wollman } 2261df8bae1dSRodney W. Grimes 2262df8bae1dSRodney W. Grimes /* 2263df8bae1dSRodney W. Grimes * Ack processing. 2264df8bae1dSRodney W. Grimes */ 2265df8bae1dSRodney W. Grimes switch (tp->t_state) { 2266df8bae1dSRodney W. Grimes 2267df8bae1dSRodney W. Grimes /* 2268764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2269764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2270764d8cefSBill Fenner * The ACK was checked above. 2271df8bae1dSRodney W. Grimes */ 2272df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2273a0292f23SGarrett Wollman 227478b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2275df8bae1dSRodney W. Grimes soisconnected(so); 2276df8bae1dSRodney W. Grimes /* Do window scaling? */ 2277df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2278df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2279df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2280464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2281df8bae1dSRodney W. Grimes } 2282a0292f23SGarrett Wollman /* 2283a0292f23SGarrett Wollman * Make transitions: 2284a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2285a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2286a0292f23SGarrett Wollman */ 22879b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2288a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 2289a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 2290a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 22917ff19458SPaul Traina } else { 2292a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 2293dbc42409SLawrence Stewart cc_conn_init(tp); 2294b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 22957ff19458SPaul Traina } 2296a0292f23SGarrett Wollman /* 2297a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2298a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2299a0292f23SGarrett Wollman */ 2300fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2301fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2302a0292f23SGarrett Wollman (struct mbuf *)0); 2303fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 230493b0017fSPhilippe Charnier /* FALLTHROUGH */ 2305df8bae1dSRodney W. Grimes 2306df8bae1dSRodney W. Grimes /* 2307df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2308df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2309fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2310fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2311df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2312df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2313df8bae1dSRodney W. Grimes */ 2314df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2315df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2316df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2317df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2318df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2319df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 23205a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 232178b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 23225a53ca16SPaul Saab goto dropafterack; 23235a53ca16SPaul Saab } 23243529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2325fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2326fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 23275a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 232839bc9de5SLawrence Stewart 232939bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 233039bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 233139bc9de5SLawrence Stewart 2332fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2333fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 233478b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2335df8bae1dSRodney W. Grimes /* 2336df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2337df8bae1dSRodney W. Grimes * a window probe), this is a completely 2338df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2339df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2340df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2341df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2342df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2343df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2344df8bae1dSRodney W. Grimes * window so we send only this one 2345df8bae1dSRodney W. Grimes * packet. 2346df8bae1dSRodney W. Grimes * 2347df8bae1dSRodney W. Grimes * We know we're losing at the current 2348df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2349df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2350df8bae1dSRodney W. Grimes * and pull our congestion window back to 2351df8bae1dSRodney W. Grimes * the new ssthresh). 2352df8bae1dSRodney W. Grimes * 2353df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2354df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2355df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2356df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2357df8bae1dSRodney W. Grimes * network. 2358f2512ba1SRui Paulo * 2359f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2360f2512ba1SRui Paulo * we reduced the cwnd. 2361df8bae1dSRodney W. Grimes */ 2362b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2363fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2364df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2365cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2366dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2367dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 23683529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2369dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2370808f11b7SPaul Saab int awnd; 237125e6f9edSPaul Saab 237225e6f9edSPaul Saab /* 237325e6f9edSPaul Saab * Compute the amount of data in flight first. 237425e6f9edSPaul Saab * We can inject new data into the pipe iff 237525e6f9edSPaul Saab * we have less than 1/2 the original window's 237625e6f9edSPaul Saab * worth of data in flight. 237725e6f9edSPaul Saab */ 2378808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 23790077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2380808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 238125e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 238225e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 238325e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 238425e6f9edSPaul Saab } 238525e6f9edSPaul Saab } else 238646f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 238746f58482SJonathan Lemon (void) tcp_output(tp); 238846f58482SJonathan Lemon goto drop; 2389cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2390cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2391a0445c2eSJayanth Vijayaraghavan 2392a0445c2eSJayanth Vijayaraghavan /* 2393a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2394a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2395a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2396a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2397a0445c2eSJayanth Vijayaraghavan * recovery. 2398a0445c2eSJayanth Vijayaraghavan */ 23993529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2400dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2401a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2402a0445c2eSJayanth Vijayaraghavan break; 2403a0445c2eSJayanth Vijayaraghavan } 2404dbc42409SLawrence Stewart } else { 2405a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 24069d11646dSJeffrey Hsu tp->snd_recover)) { 2407cb942153SJeffrey Hsu tp->t_dupacks = 0; 2408cb942153SJeffrey Hsu break; 240946f58482SJonathan Lemon } 2410a0445c2eSJayanth Vijayaraghavan } 2411dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2412dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 2413dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2414b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 24159b8b58e0SJonathan Lemon tp->t_rtttime = 0; 24163529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 241778b50714SRobert Watson TCPSTAT_INC( 241878b50714SRobert Watson tcps_sack_recovery_episode); 2419a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 24208db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 24216d90faf3SPaul Saab (void) tcp_output(tp); 24226d90faf3SPaul Saab goto drop; 24236d90faf3SPaul Saab } 2424fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2425df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2426df8bae1dSRodney W. Grimes (void) tcp_output(tp); 242748d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2428302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2429302ce8d6SAndre Oppermann __func__)); 2430df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 243148d2549cSJeffrey Hsu tp->t_maxseg * 243248d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2433df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2434df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2435df8bae1dSRodney W. Grimes goto drop; 2436603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2437dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2438582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 243948d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 244048d2549cSJeffrey Hsu u_int sent; 244189c02376SJeffrey Hsu 2442582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2443582a954bSJeffrey Hsu tp->t_dupacks == 2, 2444302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2445302ce8d6SAndre Oppermann __func__)); 244661a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 244748d2549cSJeffrey Hsu tp->snd_limited = 0; 244861a36e3dSJeffrey Hsu tp->snd_cwnd = 244961a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 245061a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 245161a36e3dSJeffrey Hsu tp->t_maxseg; 2452582a954bSJeffrey Hsu (void) tcp_output(tp); 245348d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 245448d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 245589c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 245689c02376SJeffrey Hsu tp->snd_limited == 0) || 245789c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 245889c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2459302ce8d6SAndre Oppermann ("%s: sent too much", 2460302ce8d6SAndre Oppermann __func__)); 246148d2549cSJeffrey Hsu tp->snd_limited = 2; 246248d2549cSJeffrey Hsu } else if (sent > 0) 246348d2549cSJeffrey Hsu ++tp->snd_limited; 2464582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2465582a954bSJeffrey Hsu goto drop; 2466df8bae1dSRodney W. Grimes } 2467df8bae1dSRodney W. Grimes } else 2468df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2469df8bae1dSRodney W. Grimes break; 2470df8bae1dSRodney W. Grimes } 2471cb942153SJeffrey Hsu 2472302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2473302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2474cb942153SJeffrey Hsu 2475df8bae1dSRodney W. Grimes /* 2476df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2477df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2478df8bae1dSRodney W. Grimes */ 2479dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2480cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 24813529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 24826d90faf3SPaul Saab tcp_sack_partialack(tp, th); 24836d90faf3SPaul Saab else 2484c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2485dbc42409SLawrence Stewart } else 2486dbc42409SLawrence Stewart cc_post_recovery(tp, th); 248746f58482SJonathan Lemon } 2488cb942153SJeffrey Hsu tp->t_dupacks = 0; 2489a0292f23SGarrett Wollman /* 2490a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2491a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2492a0292f23SGarrett Wollman */ 2493a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2494a0292f23SGarrett Wollman /* 2495a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2496a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 249707e43e10SAndras Olah * synchronized). Go to non-starred state, 249807e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 249907e43e10SAndras Olah * we can do window scaling. 2500a0292f23SGarrett Wollman */ 2501a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2502a0292f23SGarrett Wollman tp->snd_una++; 250307e43e10SAndras Olah /* Do window scaling? */ 250407e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 250507e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 250607e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2507464fcfbcSAndre Oppermann /* Send window already scaled. */ 250807e43e10SAndras Olah } 2509a0292f23SGarrett Wollman } 2510a0292f23SGarrett Wollman 2511a0292f23SGarrett Wollman process_ACK: 25128501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25137cfc6904SRobert Watson 2514dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 251578b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 251678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2517df8bae1dSRodney W. Grimes 2518df8bae1dSRodney W. Grimes /* 25199b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 25209b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 25219b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 25229b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 25239b8b58e0SJonathan Lemon * we left off. 25249b8b58e0SJonathan Lemon */ 2525672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2526672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2527dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 25289b8b58e0SJonathan Lemon 25299b8b58e0SJonathan Lemon /* 2530df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2531df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2532df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2533df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2534df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2535df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2536df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2537fa55172bSMatthew Dillon * 2538fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2539fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2540fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2541fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2542df8bae1dSRodney W. Grimes */ 2543fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2544fa55172bSMatthew Dillon to.to_tsecr) { 2545eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2546eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 25479b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2548fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2549eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2550eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 25519b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2552fa55172bSMatthew Dillon } 2553df8bae1dSRodney W. Grimes 2554df8bae1dSRodney W. Grimes /* 2555df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2556df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2557df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2558df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2559df8bae1dSRodney W. Grimes */ 2560fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2561b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2562df8bae1dSRodney W. Grimes needoutput = 1; 2563b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2564b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2565a0292f23SGarrett Wollman 2566a0292f23SGarrett Wollman /* 2567a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2568a0292f23SGarrett Wollman * skip rest of ACK processing. 2569a0292f23SGarrett Wollman */ 2570a0292f23SGarrett Wollman if (acked == 0) 2571a0292f23SGarrett Wollman goto step6; 2572a0292f23SGarrett Wollman 2573df8bae1dSRodney W. Grimes /* 2574dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2575dbc42409SLawrence Stewart * control related information. This typically means increasing 2576dbc42409SLawrence Stewart * the congestion window. 2577df8bae1dSRodney W. Grimes */ 2578dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 2579dbc42409SLawrence Stewart 25805905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2581df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2582df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 25835905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2584df8bae1dSRodney W. Grimes ourfinisacked = 1; 2585df8bae1dSRodney W. Grimes } else { 25865905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2587df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2588df8bae1dSRodney W. Grimes ourfinisacked = 0; 2589df8bae1dSRodney W. Grimes } 2590564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 25911e4d7da7SRobert Watson sowwakeup_locked(so); 2592e8949f74SAndre Oppermann /* Detect una wraparound. */ 2593dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 25949d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 25959d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 25969d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2597dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2598dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 259924cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2600dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 260124cb0f22SLawrence Stewart } 2602fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 26033529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 26046d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 26056d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 26066d90faf3SPaul Saab } 2607df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2608df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2609df8bae1dSRodney W. Grimes 2610df8bae1dSRodney W. Grimes switch (tp->t_state) { 2611df8bae1dSRodney W. Grimes 2612df8bae1dSRodney W. Grimes /* 2613df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2614df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2615df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2616df8bae1dSRodney W. Grimes */ 2617df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2618df8bae1dSRodney W. Grimes if (ourfinisacked) { 2619df8bae1dSRodney W. Grimes /* 2620df8bae1dSRodney W. Grimes * If we can't receive any more 2621df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2622df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2623df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2624df8bae1dSRodney W. Grimes * we'll hang forever. 2625e8949f74SAndre Oppermann * 2626e8949f74SAndre Oppermann * XXXjl: 2627340c35deSJonathan Lemon * we should release the tp also, and use a 2628340c35deSJonathan Lemon * compressed state. 2629340c35deSJonathan Lemon */ 2630c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 26317c72af87SMohan Srinivasan int timeout; 26327c72af87SMohan Srinivasan 263303e49181SSeigo Tanimura soisdisconnected(so); 26347c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 26357c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2636b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 26374cc20ab1SSeigo Tanimura } 2638df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2639df8bae1dSRodney W. Grimes } 2640df8bae1dSRodney W. Grimes break; 2641df8bae1dSRodney W. Grimes 2642df8bae1dSRodney W. Grimes /* 2643df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2644df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2645df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2646df8bae1dSRodney W. Grimes * the segment. 2647df8bae1dSRodney W. Grimes */ 2648df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2649df8bae1dSRodney W. Grimes if (ourfinisacked) { 2650252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 265111a20fb8SJeffrey Hsu tcp_twstart(tp); 2652603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2653340c35deSJonathan Lemon m_freem(m); 2654679d9708SAndre Oppermann return; 2655df8bae1dSRodney W. Grimes } 2656df8bae1dSRodney W. Grimes break; 2657df8bae1dSRodney W. Grimes 2658df8bae1dSRodney W. Grimes /* 2659df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2660df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2661df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2662df8bae1dSRodney W. Grimes * enter the closed state and return. 2663df8bae1dSRodney W. Grimes */ 2664df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2665df8bae1dSRodney W. Grimes if (ourfinisacked) { 2666252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2667df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2668df8bae1dSRodney W. Grimes goto drop; 2669df8bae1dSRodney W. Grimes } 2670df8bae1dSRodney W. Grimes break; 2671df8bae1dSRodney W. Grimes } 2672df8bae1dSRodney W. Grimes } 2673df8bae1dSRodney W. Grimes 2674df8bae1dSRodney W. Grimes step6: 26758501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26767cfc6904SRobert Watson 2677df8bae1dSRodney W. Grimes /* 2678df8bae1dSRodney W. Grimes * Update window information. 2679df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2680df8bae1dSRodney W. Grimes */ 2681fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2682fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2683fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2684fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2685df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2686fb59c426SYoshinobu Inoue if (tlen == 0 && 2687fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 268878b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2689df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2690fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2691fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2692df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2693df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2694df8bae1dSRodney W. Grimes needoutput = 1; 2695df8bae1dSRodney W. Grimes } 2696df8bae1dSRodney W. Grimes 2697df8bae1dSRodney W. Grimes /* 2698df8bae1dSRodney W. Grimes * Process segments with URG. 2699df8bae1dSRodney W. Grimes */ 2700fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2701df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2702df8bae1dSRodney W. Grimes /* 2703df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2704df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2705df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2706df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2707df8bae1dSRodney W. Grimes */ 270818ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2709fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2710fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2711fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 271218ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2713df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2714df8bae1dSRodney W. Grimes } 2715df8bae1dSRodney W. Grimes /* 2716df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2717df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2718df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2719df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2720df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2721df8bae1dSRodney W. Grimes * 2722df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2723df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2724df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2725df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2726df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2727df8bae1dSRodney W. Grimes * spec states (in one of two places). 2728df8bae1dSRodney W. Grimes */ 2729fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2730fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2731df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2732df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2733927c5ceaSRobert Watson if (so->so_oobmark == 0) 2734c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2735df8bae1dSRodney W. Grimes sohasoutofband(so); 2736df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2737df8bae1dSRodney W. Grimes } 273818ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2739df8bae1dSRodney W. Grimes /* 2740df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2741df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2742df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2743df8bae1dSRodney W. Grimes * data may creep in... ick. 2744df8bae1dSRodney W. Grimes */ 274530613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 274630613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 274730613f56SJeffrey Hsu /* hdr drop is delayed */ 274830613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 274930613f56SJeffrey Hsu } 2750c068736aSJeffrey Hsu } else { 2751df8bae1dSRodney W. Grimes /* 2752df8bae1dSRodney W. Grimes * If no out of band data is expected, 2753df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2754df8bae1dSRodney W. Grimes * with the receive window. 2755df8bae1dSRodney W. Grimes */ 2756df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2757df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2758c068736aSJeffrey Hsu } 2759df8bae1dSRodney W. Grimes dodata: /* XXX */ 27608501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27617cfc6904SRobert Watson 2762df8bae1dSRodney W. Grimes /* 2763df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2764df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2765df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2766df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2767df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2768df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2769df8bae1dSRodney W. Grimes */ 2770fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2771df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 277269e03620SPaul Saab tcp_seq save_start = th->th_seq; 2773fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2774e4b64281SJesper Skriver /* 2775c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2776c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2777c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2778c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2779c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2780c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2781c068736aSJeffrey Hsu * conversions. 2782c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2783c068736aSJeffrey Hsu * immediately when segments are out of order (so 2784c068736aSJeffrey Hsu * fast retransmit can work). 2785e4b64281SJesper Skriver */ 2786e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2787e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2788e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2789e4b64281SJesper Skriver if (DELAY_ACK(tp)) 27903bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2791e4b64281SJesper Skriver else 2792e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2793e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2794e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 279578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 279678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 2797e4b64281SJesper Skriver ND6_HINT(tp); 27981e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2799c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2800c1c36a2cSMike Silbersack m_freem(m); 2801c1c36a2cSMike Silbersack else 28021e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2803e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 28041e4d7da7SRobert Watson sorwakeup_locked(so); 2805e4b64281SJesper Skriver } else { 2806e8949f74SAndre Oppermann /* 2807e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2808e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2809e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2810e8949f74SAndre Oppermann * when trimming from the head. 2811e8949f74SAndre Oppermann */ 2812e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2813e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2814e4b64281SJesper Skriver } 28153529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2816f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2817302ce8d6SAndre Oppermann #if 0 2818df8bae1dSRodney W. Grimes /* 2819df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2820df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2821df8bae1dSRodney W. Grimes * buffer size. 2822302ce8d6SAndre Oppermann * XXX: Unused. 2823df8bae1dSRodney W. Grimes */ 2824f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 2825df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2826f701e30dSJohn Baldwin else 2827f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 2828302ce8d6SAndre Oppermann #endif 2829df8bae1dSRodney W. Grimes } else { 2830df8bae1dSRodney W. Grimes m_freem(m); 2831fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2832df8bae1dSRodney W. Grimes } 2833df8bae1dSRodney W. Grimes 2834df8bae1dSRodney W. Grimes /* 2835df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2836df8bae1dSRodney W. Grimes * that the connection is closing. 2837df8bae1dSRodney W. Grimes */ 2838fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2839df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2840df8bae1dSRodney W. Grimes socantrcvmore(so); 2841a0292f23SGarrett Wollman /* 2842a0292f23SGarrett Wollman * If connection is half-synchronized 2843d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2844a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2845a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2846a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2847a0292f23SGarrett Wollman */ 28483bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 28493bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2850a0292f23SGarrett Wollman else 2851df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2852df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2853df8bae1dSRodney W. Grimes } 2854df8bae1dSRodney W. Grimes switch (tp->t_state) { 2855df8bae1dSRodney W. Grimes 2856df8bae1dSRodney W. Grimes /* 2857df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2858df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2859df8bae1dSRodney W. Grimes */ 2860df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 28619b8b58e0SJonathan Lemon tp->t_starttime = ticks; 28629b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2863df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2864df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2865df8bae1dSRodney W. Grimes break; 2866df8bae1dSRodney W. Grimes 2867df8bae1dSRodney W. Grimes /* 2868df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2869df8bae1dSRodney W. Grimes * enter the CLOSING state. 2870df8bae1dSRodney W. Grimes */ 2871df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2872df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2873df8bae1dSRodney W. Grimes break; 2874df8bae1dSRodney W. Grimes 2875df8bae1dSRodney W. Grimes /* 2876df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2877df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2878df8bae1dSRodney W. Grimes * standard timers. 2879df8bae1dSRodney W. Grimes */ 2880df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2881252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2882252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2883252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2884252ca428SRobert Watson ti_locked)); 2885252ca428SRobert Watson 2886340c35deSJonathan Lemon tcp_twstart(tp); 2887603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2888679d9708SAndre Oppermann return; 2889df8bae1dSRodney W. Grimes } 2890df8bae1dSRodney W. Grimes } 2891fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 2892603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2893252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2894252ca428SRobert Watson 2895610ee2f9SDavid Greenman #ifdef TCPDEBUG 28964cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2897fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2898fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2899610ee2f9SDavid Greenman #endif 2900df8bae1dSRodney W. Grimes 2901df8bae1dSRodney W. Grimes /* 2902df8bae1dSRodney W. Grimes * Return any desired output. 2903df8bae1dSRodney W. Grimes */ 2904df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2905df8bae1dSRodney W. Grimes (void) tcp_output(tp); 29067792ea27SJeffrey Hsu 2907a14c749fSJonathan Lemon check_delack: 2908252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2909252ca428SRobert Watson __func__, ti_locked)); 2910603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 29118501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2912252ca428SRobert Watson 2913a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 29143bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2915b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 29163bfd6421SJonathan Lemon } 29178501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2918679d9708SAndre Oppermann return; 2919df8bae1dSRodney W. Grimes 2920df8bae1dSRodney W. Grimes dropafterack: 2921df8bae1dSRodney W. Grimes /* 2922df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2923df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 292480ab7c0eSGarrett Wollman * 292580ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 292680ab7c0eSGarrett Wollman * paths to this code happen after packets containing 292780ab7c0eSGarrett Wollman * RST have been dropped. 292880ab7c0eSGarrett Wollman * 292980ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 293080ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 293180ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 293280ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 293380ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 293480ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2935df8bae1dSRodney W. Grimes */ 2936fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2937fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2938a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2939a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2940a57815efSBosko Milekic goto dropwithreset; 2941a57815efSBosko Milekic } 2942a0292f23SGarrett Wollman #ifdef TCPDEBUG 29434cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2944fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2945fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2946a0292f23SGarrett Wollman #endif 2947fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 2948603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2949252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2950252ca428SRobert Watson 2951df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2952df8bae1dSRodney W. Grimes (void) tcp_output(tp); 29538501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2954d6915262SRobert Watson m_freem(m); 2955679d9708SAndre Oppermann return; 2956df8bae1dSRodney W. Grimes 2957df8bae1dSRodney W. Grimes dropwithreset: 2958fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 2959dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2960252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2961a57815efSBosko Milekic 2962a0ca0871SRobert Watson if (tp != NULL) { 2963302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 29648501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2965dd8ac7f9SRobert Watson } else 2966a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2967679d9708SAndre Oppermann return; 2968df8bae1dSRodney W. Grimes 2969df8bae1dSRodney W. Grimes drop: 2970fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 2971252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2972fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 2973fa046d87SRobert Watson } 2974252ca428SRobert Watson #ifdef INVARIANTS 2975252ca428SRobert Watson else 2976252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2977252ca428SRobert Watson #endif 2978252ca428SRobert Watson 2979df8bae1dSRodney W. Grimes /* 2980df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2981df8bae1dSRodney W. Grimes */ 2982610ee2f9SDavid Greenman #ifdef TCPDEBUG 29831c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2984fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2985fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2986a0292f23SGarrett Wollman #endif 29871c53f806SRobert Watson if (tp != NULL) 29888501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2989d6915262SRobert Watson m_freem(m); 2990302ce8d6SAndre Oppermann } 2991302ce8d6SAndre Oppermann 2992302ce8d6SAndre Oppermann /* 29930ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 29940ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 29950ca3f933SAndre Oppermann * tp may be NULL. 2996302ce8d6SAndre Oppermann */ 2997302ce8d6SAndre Oppermann static void 2998302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2999302ce8d6SAndre Oppermann int tlen, int rstreason) 3000302ce8d6SAndre Oppermann { 3001b287c6c7SBjoern A. Zeeb #ifdef INET 3002302ce8d6SAndre Oppermann struct ip *ip; 3003b287c6c7SBjoern A. Zeeb #endif 3004302ce8d6SAndre Oppermann #ifdef INET6 3005302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3006302ce8d6SAndre Oppermann #endif 30077a3244ccSRobert Watson 30087a3244ccSRobert Watson if (tp != NULL) { 30098501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30107a3244ccSRobert Watson } 30117a3244ccSRobert Watson 30120ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3013302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3014302ce8d6SAndre Oppermann goto drop; 3015302ce8d6SAndre Oppermann #ifdef INET6 3016302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3017302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3018302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3019302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3020302ce8d6SAndre Oppermann goto drop; 3021302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3022b287c6c7SBjoern A. Zeeb } 3023302ce8d6SAndre Oppermann #endif 3024b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3025b287c6c7SBjoern A. Zeeb else 3026b287c6c7SBjoern A. Zeeb #endif 3027b287c6c7SBjoern A. Zeeb #ifdef INET 3028302ce8d6SAndre Oppermann { 3029302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3030302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3031302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3032302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3033302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3034302ce8d6SAndre Oppermann goto drop; 3035302ce8d6SAndre Oppermann } 3036b287c6c7SBjoern A. Zeeb #endif 3037302ce8d6SAndre Oppermann 3038302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3039302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3040302ce8d6SAndre Oppermann goto drop; 3041302ce8d6SAndre Oppermann 3042302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3043302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3044302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3045302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3046302ce8d6SAndre Oppermann } else { 3047302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3048302ce8d6SAndre Oppermann tlen++; 3049302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3050302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3051302ce8d6SAndre Oppermann } 3052302ce8d6SAndre Oppermann return; 3053302ce8d6SAndre Oppermann drop: 3054302ce8d6SAndre Oppermann m_freem(m); 3055df8bae1dSRodney W. Grimes } 3056df8bae1dSRodney W. Grimes 3057be2ac88cSJonathan Lemon /* 3058be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3059be2ac88cSJonathan Lemon */ 30600312fbe9SPoul-Henning Kamp static void 3061ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3062df8bae1dSRodney W. Grimes { 3063df8bae1dSRodney W. Grimes int opt, optlen; 3064df8bae1dSRodney W. Grimes 3065be2ac88cSJonathan Lemon to->to_flags = 0; 3066df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3067df8bae1dSRodney W. Grimes opt = cp[0]; 3068df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3069df8bae1dSRodney W. Grimes break; 3070df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3071df8bae1dSRodney W. Grimes optlen = 1; 3072df8bae1dSRodney W. Grimes else { 3073b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3074b474779fSJun-ichiro itojun Hagino break; 3075df8bae1dSRodney W. Grimes optlen = cp[1]; 3076b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3077df8bae1dSRodney W. Grimes break; 3078df8bae1dSRodney W. Grimes } 3079df8bae1dSRodney W. Grimes switch (opt) { 3080df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3081df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3082df8bae1dSRodney W. Grimes continue; 3083f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3084df8bae1dSRodney W. Grimes continue; 3085be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3086be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3087be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3088fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3089df8bae1dSRodney W. Grimes break; 3090df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3091df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3092df8bae1dSRodney W. Grimes continue; 3093f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3094df8bae1dSRodney W. Grimes continue; 3095be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 309602a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3097df8bae1dSRodney W. Grimes break; 3098df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3099df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3100df8bae1dSRodney W. Grimes continue; 3101be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3102a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3103a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3104fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3105a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3106a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3107fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3108df8bae1dSRodney W. Grimes break; 31091cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 31101cfd4b53SBruce M Simpson /* 31111cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 31121cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 31131cfd4b53SBruce M Simpson * record the fact that the option was observed here 31141cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 31151cfd4b53SBruce M Simpson */ 31161cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 31171cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 31181cfd4b53SBruce M Simpson continue; 3119df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3120df47e437SAndre Oppermann to->to_signature = cp + 2; 31211cfd4b53SBruce M Simpson break; 3122265ed012SBruce M Simpson #endif 31236d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3124f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 31256d90faf3SPaul Saab continue; 3126f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3127f72167f4SAndre Oppermann continue; 3128603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3129f72167f4SAndre Oppermann continue; 3130fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 31316d90faf3SPaul Saab break; 31326d90faf3SPaul Saab case TCPOPT_SACK: 31335a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 31346d90faf3SPaul Saab continue; 3135b728e902SAndre Oppermann if (flags & TO_SYN) 3136b728e902SAndre Oppermann continue; 3137fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 31385a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 31395a53ca16SPaul Saab to->to_sacks = cp + 2; 314078b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 31416d90faf3SPaul Saab break; 3142be2ac88cSJonathan Lemon default: 3143be2ac88cSJonathan Lemon continue; 3144df8bae1dSRodney W. Grimes } 3145df8bae1dSRodney W. Grimes } 3146df8bae1dSRodney W. Grimes } 3147df8bae1dSRodney W. Grimes 3148df8bae1dSRodney W. Grimes /* 3149df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3150df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3151df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3152df8bae1dSRodney W. Grimes * sequencing purposes. 3153df8bae1dSRodney W. Grimes */ 31540312fbe9SPoul-Henning Kamp static void 3155ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3156ad3f9ab3SAndre Oppermann int off) 3157df8bae1dSRodney W. Grimes { 3158fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3159df8bae1dSRodney W. Grimes 3160df8bae1dSRodney W. Grimes while (cnt >= 0) { 3161df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3162df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3163df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3164df8bae1dSRodney W. Grimes 31658501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31667a3244ccSRobert Watson 3167df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3168df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3169df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3170df8bae1dSRodney W. Grimes m->m_len--; 317169a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 317269a34685SYoshinobu Inoue m->m_pkthdr.len--; 3173df8bae1dSRodney W. Grimes return; 3174df8bae1dSRodney W. Grimes } 3175df8bae1dSRodney W. Grimes cnt -= m->m_len; 3176df8bae1dSRodney W. Grimes m = m->m_next; 31774dfdffe9SAndre Oppermann if (m == NULL) 3178df8bae1dSRodney W. Grimes break; 3179df8bae1dSRodney W. Grimes } 3180df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3181df8bae1dSRodney W. Grimes } 3182df8bae1dSRodney W. Grimes 3183df8bae1dSRodney W. Grimes /* 3184df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3185df8bae1dSRodney W. Grimes * and update averages and current timeout. 3186df8bae1dSRodney W. Grimes */ 31870312fbe9SPoul-Henning Kamp static void 3188ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3189df8bae1dSRodney W. Grimes { 3190ad3f9ab3SAndre Oppermann int delta; 3191233e8c18SGarrett Wollman 31928501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31932be3bf22SRobert Watson 319478b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3195233e8c18SGarrett Wollman tp->t_rttupdated++; 3196233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3197233e8c18SGarrett Wollman /* 3198233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3199233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3200233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3201233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3202233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3203233e8c18SGarrett Wollman */ 3204233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3205233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3206233e8c18SGarrett Wollman 3207233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3208233e8c18SGarrett Wollman tp->t_srtt = 1; 3209233e8c18SGarrett Wollman 3210233e8c18SGarrett Wollman /* 3211233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3212233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3213233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3214233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3215233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3216233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3217233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3218233e8c18SGarrett Wollman * rfc793's wired-in beta. 3219233e8c18SGarrett Wollman */ 3220233e8c18SGarrett Wollman if (delta < 0) 3221233e8c18SGarrett Wollman delta = -delta; 3222233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3223233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3224233e8c18SGarrett Wollman tp->t_rttvar = 1; 32251fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 32261fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3227233e8c18SGarrett Wollman } else { 3228233e8c18SGarrett Wollman /* 3229233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3230233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3231233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3232233e8c18SGarrett Wollman */ 3233233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3234233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 32351fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3236233e8c18SGarrett Wollman } 32379b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3238df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3239df8bae1dSRodney W. Grimes 3240df8bae1dSRodney W. Grimes /* 3241df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3242df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3243df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3244df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3245df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3246df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3247df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3248df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3249df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3250df8bae1dSRodney W. Grimes */ 3251233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 32529e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3253df8bae1dSRodney W. Grimes 3254df8bae1dSRodney W. Grimes /* 3255df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3256df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3257df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3258df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3259df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3260df8bae1dSRodney W. Grimes */ 3261df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3262df8bae1dSRodney W. Grimes } 3263df8bae1dSRodney W. Grimes 3264df8bae1dSRodney W. Grimes /* 3265df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3266df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 3267df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 3268df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 3269df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3270df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 3271df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3272df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3273df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3274df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3275df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3276df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3277df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3278a0292f23SGarrett Wollman * 3279a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3280a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3281a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3282a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3283a0292f23SGarrett Wollman * data in maxopd. 3284a0292f23SGarrett Wollman * 3285a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 3286a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 3287a0292f23SGarrett Wollman * MSS of our peer. 328897d8d152SAndre Oppermann * 328997d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 329097d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3291df8bae1dSRodney W. Grimes */ 3292a0292f23SGarrett Wollman void 329391d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, 329491d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3295df8bae1dSRodney W. Grimes { 3296b287c6c7SBjoern A. Zeeb int mss = 0; 3297b287c6c7SBjoern A. Zeeb u_long maxmtu = 0; 3298c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 329997d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3300a0292f23SGarrett Wollman int origoffer = offer; 3301fb59c426SYoshinobu Inoue #ifdef INET6 3302c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3303c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3304c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3305c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3306c068736aSJeffrey Hsu #else 3307c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3308fb59c426SYoshinobu Inoue #endif 3309df8bae1dSRodney W. Grimes 33108501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33117a3244ccSRobert Watson 3312e8949f74SAndre Oppermann /* Initialize. */ 331397d8d152SAndre Oppermann #ifdef INET6 331497d8d152SAndre Oppermann if (isipv6) { 331591d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3316603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3317b287c6c7SBjoern A. Zeeb } 331897d8d152SAndre Oppermann #endif 3319b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3320b287c6c7SBjoern A. Zeeb else 3321b287c6c7SBjoern A. Zeeb #endif 3322b287c6c7SBjoern A. Zeeb #ifdef INET 332397d8d152SAndre Oppermann { 332491d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3325603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3326df8bae1dSRodney W. Grimes } 3327b287c6c7SBjoern A. Zeeb #endif 3328df8bae1dSRodney W. Grimes 332997d8d152SAndre Oppermann /* 3330e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 333197d8d152SAndre Oppermann */ 33326f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 33336f01cac6SBjoern A. Zeeb /* 33348e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 33356f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 33366f01cac6SBjoern A. Zeeb * if there was no cache hit. 33376f01cac6SBjoern A. Zeeb */ 33386f01cac6SBjoern A. Zeeb if (metricptr != NULL) 33396f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 334097d8d152SAndre Oppermann return; 33416f01cac6SBjoern A. Zeeb } 334297d8d152SAndre Oppermann 3343e8949f74SAndre Oppermann /* What have we got? */ 334497d8d152SAndre Oppermann switch (offer) { 334597d8d152SAndre Oppermann case 0: 334697d8d152SAndre Oppermann /* 334797d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3348c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3349c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 335097d8d152SAndre Oppermann */ 3351c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 335297d8d152SAndre Oppermann break; 335397d8d152SAndre Oppermann 335497d8d152SAndre Oppermann case -1: 3355a0292f23SGarrett Wollman /* 3356c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3357a0292f23SGarrett Wollman */ 335897d8d152SAndre Oppermann /* FALLTHROUGH */ 335997d8d152SAndre Oppermann 336097d8d152SAndre Oppermann default: 3361a0292f23SGarrett Wollman /* 336253369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 336353369ac9SAndre Oppermann * to at least minmss. 336453369ac9SAndre Oppermann */ 3365603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 336697d8d152SAndre Oppermann } 3367a0292f23SGarrett Wollman 3368df8bae1dSRodney W. Grimes /* 3369e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3370df8bae1dSRodney W. Grimes */ 337197d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 33723cee92e0SBjoern A. Zeeb if (metricptr != NULL) 33733cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 337497d8d152SAndre Oppermann 3375df8bae1dSRodney W. Grimes /* 3376e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3377fb59c426SYoshinobu Inoue * else, use the link mtu. 3378df8bae1dSRodney W. Grimes */ 337997d8d152SAndre Oppermann if (metrics.rmx_mtu) 33802d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3381c068736aSJeffrey Hsu else { 338231b3783cSHajimu UMEMOTO #ifdef INET6 3383fb59c426SYoshinobu Inoue if (isipv6) { 338497d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3385603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 338697d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3387603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3388b287c6c7SBjoern A. Zeeb } 3389b3399803SHajimu UMEMOTO #endif 3390b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3391b287c6c7SBjoern A. Zeeb else 3392b287c6c7SBjoern A. Zeeb #endif 3393b287c6c7SBjoern A. Zeeb #ifdef INET 339497d8d152SAndre Oppermann { 339597d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3396603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 339797d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3398603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3399a0292f23SGarrett Wollman } 3400b287c6c7SBjoern A. Zeeb #endif 34013cee92e0SBjoern A. Zeeb /* 34023cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 34033cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 34043cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 34053cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 34063cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 34073cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 34083cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 34093cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 34103cee92e0SBjoern A. Zeeb * this under the carpet. 34113cee92e0SBjoern A. Zeeb * 34123cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 34133cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 34143cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 34153cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 34163cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 34173cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 34183cee92e0SBjoern A. Zeeb */ 341997d8d152SAndre Oppermann } 3420a0292f23SGarrett Wollman mss = min(mss, offer); 342197d8d152SAndre Oppermann 3422a0292f23SGarrett Wollman /* 34233cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 34243cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 34253cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 34263cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 34273cee92e0SBjoern A. Zeeb */ 34283cee92e0SBjoern A. Zeeb mss = max(mss, 64); 34293cee92e0SBjoern A. Zeeb 34303cee92e0SBjoern A. Zeeb /* 3431a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3432a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3433a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3434a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3435a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3436a0292f23SGarrett Wollman */ 3437a0292f23SGarrett Wollman tp->t_maxopd = mss; 3438a0292f23SGarrett Wollman 3439a0292f23SGarrett Wollman /* 3440e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3441c94c54e4SAndre Oppermann * In this case we just guess. 3442a0292f23SGarrett Wollman */ 3443a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3444a0292f23SGarrett Wollman (origoffer == -1 || 3445a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3446a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3447a0292f23SGarrett Wollman 3448df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3449df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3450df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3451df8bae1dSRodney W. Grimes #else 3452df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3453df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3454df8bae1dSRodney W. Grimes #endif 345597d8d152SAndre Oppermann tp->t_maxseg = mss; 34563cee92e0SBjoern A. Zeeb } 34573cee92e0SBjoern A. Zeeb 34583cee92e0SBjoern A. Zeeb void 34593cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 34603cee92e0SBjoern A. Zeeb { 3461dbc42409SLawrence Stewart int mss; 34623cee92e0SBjoern A. Zeeb u_long bufsize; 34633cee92e0SBjoern A. Zeeb struct inpcb *inp; 34643cee92e0SBjoern A. Zeeb struct socket *so; 34653cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 346691d6cfa6SBjoern A. Zeeb int mtuflags = 0; 3467dbc42409SLawrence Stewart 34683cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 34693cee92e0SBjoern A. Zeeb 347091d6cfa6SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics, &mtuflags); 34713cee92e0SBjoern A. Zeeb 34723cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 34733cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 347497d8d152SAndre Oppermann 3475df8bae1dSRodney W. Grimes /* 347697d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 347797d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 347897d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 347997d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 348097d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3481df8bae1dSRodney W. Grimes */ 3482c3b02504SBjoern A. Zeeb so = inp->inp_socket; 34833f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 3484e233e2acSAndre Oppermann if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe) 348597d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 348697d8d152SAndre Oppermann else 3487df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3488df8bae1dSRodney W. Grimes if (bufsize < mss) 3489df8bae1dSRodney W. Grimes mss = bufsize; 3490df8bae1dSRodney W. Grimes else { 3491df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3492df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3493df8bae1dSRodney W. Grimes bufsize = sb_max; 349488c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 34953f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3496df8bae1dSRodney W. Grimes } 34973f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3498df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3499df8bae1dSRodney W. Grimes 35003f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 3501e233e2acSAndre Oppermann if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe) 350297d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 350397d8d152SAndre Oppermann else 3504df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3505df8bae1dSRodney W. Grimes if (bufsize > mss) { 3506df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3507df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3508df8bae1dSRodney W. Grimes bufsize = sb_max; 350988c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 35103f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3511df8bae1dSRodney W. Grimes } 35123f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 351391d6cfa6SBjoern A. Zeeb 351491d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 351591d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 351691d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3517a0292f23SGarrett Wollman } 3518a0292f23SGarrett Wollman 3519a0292f23SGarrett Wollman /* 3520a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3521a0292f23SGarrett Wollman */ 3522a0292f23SGarrett Wollman int 3523ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3524a0292f23SGarrett Wollman { 352597d8d152SAndre Oppermann int mss = 0; 352697d8d152SAndre Oppermann u_long maxmtu = 0; 352797d8d152SAndre Oppermann u_long thcmtu = 0; 352897d8d152SAndre Oppermann size_t min_protoh; 3529a0292f23SGarrett Wollman 353097d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3531a0292f23SGarrett Wollman 353231b3783cSHajimu UMEMOTO #ifdef INET6 3533dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3534603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3535233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 353697d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 353797d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3538b287c6c7SBjoern A. Zeeb } 353931b3783cSHajimu UMEMOTO #endif 3540b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3541b287c6c7SBjoern A. Zeeb else 3542b287c6c7SBjoern A. Zeeb #endif 3543b287c6c7SBjoern A. Zeeb #ifdef INET 354497d8d152SAndre Oppermann { 3545603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3546233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 354797d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 354897d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 354997d8d152SAndre Oppermann } 3550b287c6c7SBjoern A. Zeeb #endif 355197d8d152SAndre Oppermann if (maxmtu && thcmtu) 355297d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 355397d8d152SAndre Oppermann else if (maxmtu || thcmtu) 355497d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 355597d8d152SAndre Oppermann 355697d8d152SAndre Oppermann return (mss); 3557df8bae1dSRodney W. Grimes } 355846f58482SJonathan Lemon 355946f58482SJonathan Lemon 356046f58482SJonathan Lemon /* 3561c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3562c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3563c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3564c068736aSJeffrey Hsu * be started again. 356546f58482SJonathan Lemon */ 3566c068736aSJeffrey Hsu static void 3567ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 356846f58482SJonathan Lemon { 356946f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 357046f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 357146f58482SJonathan Lemon 35728501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 35737a3244ccSRobert Watson 3574b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 357546f58482SJonathan Lemon tp->t_rtttime = 0; 357646f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 35776b2a5f92SJayanth Vijayaraghavan /* 3578c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3579c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 35806b2a5f92SJayanth Vijayaraghavan */ 3581dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3582a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 35836b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 358446f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 358546f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 358646f58482SJonathan Lemon tp->snd_nxt = onxt; 358746f58482SJonathan Lemon /* 358846f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 358946f58482SJonathan Lemon * not updated yet. 359046f58482SJonathan Lemon */ 3591dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3592dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3593d7587117SPaul Saab else 3594d7587117SPaul Saab tp->snd_cwnd = 0; 3595d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 359646f58482SJonathan Lemon } 3597