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 19882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 256*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 #ifdef INET6 305dbc42409SLawrence Stewart int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 306dbc42409SLawrence Stewart #endif 307dbc42409SLawrence Stewart 308dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 309dbc42409SLawrence Stewart 310dbc42409SLawrence Stewart tcp_hc_get(&inp->inp_inc, &metrics); 311dbc42409SLawrence Stewart 312dbc42409SLawrence Stewart if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 313dbc42409SLawrence Stewart tp->t_srtt = rtt; 314dbc42409SLawrence Stewart tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 315dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrtt); 316dbc42409SLawrence Stewart if (metrics.rmx_rttvar) { 317dbc42409SLawrence Stewart tp->t_rttvar = metrics.rmx_rttvar; 318dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedrttvar); 319dbc42409SLawrence Stewart } else { 320dbc42409SLawrence Stewart /* default variation is +- 1 rtt */ 321dbc42409SLawrence Stewart tp->t_rttvar = 322dbc42409SLawrence Stewart tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 323dbc42409SLawrence Stewart } 324dbc42409SLawrence Stewart TCPT_RANGESET(tp->t_rxtcur, 325dbc42409SLawrence Stewart ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 326dbc42409SLawrence Stewart tp->t_rttmin, TCPTV_REXMTMAX); 327dbc42409SLawrence Stewart } 328dbc42409SLawrence Stewart if (metrics.rmx_ssthresh) { 329dbc42409SLawrence Stewart /* 330dbc42409SLawrence Stewart * There's some sort of gateway or interface 331dbc42409SLawrence Stewart * buffer limit on the path. Use this to set 332dbc42409SLawrence Stewart * the slow start threshhold, but set the 333dbc42409SLawrence Stewart * threshold to no less than 2*mss. 334dbc42409SLawrence Stewart */ 335dbc42409SLawrence Stewart tp->snd_ssthresh = max(2 * tp->t_maxseg, metrics.rmx_ssthresh); 336dbc42409SLawrence Stewart TCPSTAT_INC(tcps_usedssthresh); 337dbc42409SLawrence Stewart } 338dbc42409SLawrence Stewart 339dbc42409SLawrence Stewart /* 340dbc42409SLawrence Stewart * Set the slow-start flight size depending on whether this 341dbc42409SLawrence Stewart * is a local network or not. 342dbc42409SLawrence Stewart * 343dbc42409SLawrence Stewart * Extend this so we cache the cwnd too and retrieve it here. 344dbc42409SLawrence Stewart * Make cwnd even bigger than RFC3390 suggests but only if we 345dbc42409SLawrence Stewart * have previous experience with the remote host. Be careful 346dbc42409SLawrence Stewart * not make cwnd bigger than remote receive window or our own 347dbc42409SLawrence Stewart * send socket buffer. Maybe put some additional upper bound 348dbc42409SLawrence Stewart * on the retrieved cwnd. Should do incremental updates to 349dbc42409SLawrence Stewart * hostcache when cwnd collapses so next connection doesn't 350dbc42409SLawrence Stewart * overloads the path again. 351dbc42409SLawrence Stewart * 352dbc42409SLawrence Stewart * XXXAO: Initializing the CWND from the hostcache is broken 353dbc42409SLawrence Stewart * and in its current form not RFC conformant. It is disabled 354dbc42409SLawrence Stewart * until fixed or removed entirely. 355dbc42409SLawrence Stewart * 356dbc42409SLawrence Stewart * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 357dbc42409SLawrence Stewart * We currently check only in syncache_socket for that. 358dbc42409SLawrence Stewart */ 359dbc42409SLawrence Stewart /* #define TCP_METRICS_CWND */ 360dbc42409SLawrence Stewart #ifdef TCP_METRICS_CWND 361dbc42409SLawrence Stewart if (metrics.rmx_cwnd) 362dbc42409SLawrence Stewart tp->snd_cwnd = max(tp->t_maxseg, min(metrics.rmx_cwnd / 2, 363dbc42409SLawrence Stewart min(tp->snd_wnd, so->so_snd.sb_hiwat))); 364dbc42409SLawrence Stewart else 365dbc42409SLawrence Stewart #endif 366dbc42409SLawrence Stewart if (V_tcp_do_rfc3390) 367dbc42409SLawrence Stewart tp->snd_cwnd = min(4 * tp->t_maxseg, 368dbc42409SLawrence Stewart max(2 * tp->t_maxseg, 4380)); 369dbc42409SLawrence Stewart #ifdef INET6 370b287c6c7SBjoern A. Zeeb else if (isipv6 && in6_localaddr(&inp->in6p_faddr)) 371dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg * V_ss_fltsz_local; 372b287c6c7SBjoern A. Zeeb #endif 37329bd2010SBjoern A. Zeeb #if defined(INET) && defined(INET6) 37429bd2010SBjoern A. Zeeb else if (!isipv6 && in_localaddr(inp->inp_faddr)) 37529bd2010SBjoern A. Zeeb tp->snd_cwnd = tp->t_maxseg * V_ss_fltsz_local; 37629bd2010SBjoern A. Zeeb #endif 37729bd2010SBjoern A. Zeeb #ifdef INET 378b287c6c7SBjoern A. Zeeb else if (in_localaddr(inp->inp_faddr)) 379b287c6c7SBjoern A. Zeeb tp->snd_cwnd = tp->t_maxseg * V_ss_fltsz_local; 380b287c6c7SBjoern A. Zeeb #endif 381dbc42409SLawrence Stewart else 382dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg * V_ss_fltsz; 383dbc42409SLawrence Stewart 384dbc42409SLawrence Stewart if (CC_ALGO(tp)->conn_init != NULL) 385dbc42409SLawrence Stewart CC_ALGO(tp)->conn_init(tp->ccv); 386dbc42409SLawrence Stewart } 387dbc42409SLawrence Stewart 388dbc42409SLawrence Stewart void inline 389dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type) 390dbc42409SLawrence Stewart { 391dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 392dbc42409SLawrence Stewart 393dbc42409SLawrence Stewart switch(type) { 394dbc42409SLawrence Stewart case CC_NDUPACK: 395dbc42409SLawrence Stewart if (!IN_FASTRECOVERY(tp->t_flags)) { 396f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 397f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 398f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 399f2512ba1SRui Paulo } 400dbc42409SLawrence Stewart break; 401dbc42409SLawrence Stewart case CC_ECN: 402dbc42409SLawrence Stewart if (!IN_CONGRECOVERY(tp->t_flags)) { 403dbc42409SLawrence Stewart TCPSTAT_INC(tcps_ecn_rcwnd); 404dbc42409SLawrence Stewart tp->snd_recover = tp->snd_max; 405dbc42409SLawrence Stewart if (tp->t_flags & TF_ECN_PERMIT) 406dbc42409SLawrence Stewart tp->t_flags |= TF_ECN_SND_CWR; 407dbc42409SLawrence Stewart } 408dbc42409SLawrence Stewart break; 409dbc42409SLawrence Stewart case CC_RTO: 410dbc42409SLawrence Stewart tp->t_dupacks = 0; 411dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 412dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 4136157935fSLawrence Stewart tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 / 4146157935fSLawrence Stewart tp->t_maxseg) * tp->t_maxseg; 415dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg; 416dbc42409SLawrence Stewart break; 417dbc42409SLawrence Stewart case CC_RTO_ERR: 418dbc42409SLawrence Stewart TCPSTAT_INC(tcps_sndrexmitbad); 419dbc42409SLawrence Stewart /* RTO was unnecessary, so reset everything. */ 420dbc42409SLawrence Stewart tp->snd_cwnd = tp->snd_cwnd_prev; 421dbc42409SLawrence Stewart tp->snd_ssthresh = tp->snd_ssthresh_prev; 422dbc42409SLawrence Stewart tp->snd_recover = tp->snd_recover_prev; 423dbc42409SLawrence Stewart if (tp->t_flags & TF_WASFRECOVERY) 424dbc42409SLawrence Stewart ENTER_FASTRECOVERY(tp->t_flags); 425dbc42409SLawrence Stewart if (tp->t_flags & TF_WASCRECOVERY) 426dbc42409SLawrence Stewart ENTER_CONGRECOVERY(tp->t_flags); 427dbc42409SLawrence Stewart tp->snd_nxt = tp->snd_max; 428672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 429dbc42409SLawrence Stewart tp->t_badrxtwin = 0; 430dbc42409SLawrence Stewart break; 431dbc42409SLawrence Stewart } 432dbc42409SLawrence Stewart 433dbc42409SLawrence Stewart if (CC_ALGO(tp)->cong_signal != NULL) { 434dbc42409SLawrence Stewart if (th != NULL) 435dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 436dbc42409SLawrence Stewart CC_ALGO(tp)->cong_signal(tp->ccv, type); 437dbc42409SLawrence Stewart } 438dbc42409SLawrence Stewart } 439dbc42409SLawrence Stewart 440dbc42409SLawrence Stewart static void inline 441dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th) 442dbc42409SLawrence Stewart { 443dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 444dbc42409SLawrence Stewart 445dbc42409SLawrence Stewart /* XXXLAS: KASSERT that we're in recovery? */ 446dbc42409SLawrence Stewart 447dbc42409SLawrence Stewart if (CC_ALGO(tp)->post_recovery != NULL) { 448dbc42409SLawrence Stewart tp->ccv->curack = th->th_ack; 449dbc42409SLawrence Stewart CC_ALGO(tp)->post_recovery(tp->ccv); 450dbc42409SLawrence Stewart } 451dbc42409SLawrence Stewart /* XXXLAS: EXIT_RECOVERY ? */ 452dbc42409SLawrence Stewart tp->t_bytes_acked = 0; 453dbc42409SLawrence Stewart } 4540312fbe9SPoul-Henning Kamp 4552903309aSAttilio Rao static inline void 4562903309aSAttilio Rao tcp_fields_to_host(struct tcphdr *th) 4572903309aSAttilio Rao { 4582903309aSAttilio Rao 4592903309aSAttilio Rao th->th_seq = ntohl(th->th_seq); 4602903309aSAttilio Rao th->th_ack = ntohl(th->th_ack); 4612903309aSAttilio Rao th->th_win = ntohs(th->th_win); 4622903309aSAttilio Rao th->th_urp = ntohs(th->th_urp); 4632903309aSAttilio Rao } 4642903309aSAttilio Rao 4652903309aSAttilio Rao #ifdef TCP_SIGNATURE 4662903309aSAttilio Rao static inline void 4672903309aSAttilio Rao tcp_fields_to_net(struct tcphdr *th) 4682903309aSAttilio Rao { 4692903309aSAttilio Rao 4702903309aSAttilio Rao th->th_seq = htonl(th->th_seq); 4712903309aSAttilio Rao th->th_ack = htonl(th->th_ack); 4722903309aSAttilio Rao th->th_win = htons(th->th_win); 4732903309aSAttilio Rao th->th_urp = htons(th->th_urp); 4742903309aSAttilio Rao } 4752903309aSAttilio Rao 4762903309aSAttilio Rao static inline int 4772903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen, 4782903309aSAttilio Rao struct tcpopt *to, struct tcphdr *th, u_int tcpbflag) 4792903309aSAttilio Rao { 4802903309aSAttilio Rao int ret; 4812903309aSAttilio Rao 4822903309aSAttilio Rao tcp_fields_to_net(th); 4832903309aSAttilio Rao ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag); 4842903309aSAttilio Rao tcp_fields_to_host(th); 4852903309aSAttilio Rao return (ret); 4862903309aSAttilio Rao } 4872903309aSAttilio Rao #endif 4882903309aSAttilio Rao 489fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 490fb59c426SYoshinobu Inoue #ifdef INET6 491fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 492fb59c426SYoshinobu Inoue do { \ 493fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 49497d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 49597d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 496fb59c426SYoshinobu Inoue } while (0) 497fb59c426SYoshinobu Inoue #else 498fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 499fb59c426SYoshinobu Inoue #endif 500df8bae1dSRodney W. Grimes 501df8bae1dSRodney W. Grimes /* 502262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 503262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 504262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 5053bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 5063bfd6421SJonathan Lemon * - delayed acks are enabled or 5073bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 508d8c85a26SJonathan Lemon */ 509d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 510b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 511a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 512603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 513d8c85a26SJonathan Lemon 514df8bae1dSRodney W. Grimes /* 5158d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 5168d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 5178d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 5188d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 5198d573cc1SAndre Oppermann * SYN processing on listen sockets 5208d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 5218d573cc1SAndre Oppermann * establishing, established and closing connections 522df8bae1dSRodney W. Grimes */ 523fb59c426SYoshinobu Inoue #ifdef INET6 524fb59c426SYoshinobu Inoue int 525ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 526fb59c426SYoshinobu Inoue { 527ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 52833841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 529fb59c426SYoshinobu Inoue 530fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 531fb59c426SYoshinobu Inoue 532fb59c426SYoshinobu Inoue /* 533fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 534fb59c426SYoshinobu Inoue * better place to put this in? 535fb59c426SYoshinobu Inoue */ 53633841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 53733841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 538fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 539fb59c426SYoshinobu Inoue 5408c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 541fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 542fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 543fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 544fb59c426SYoshinobu Inoue return IPPROTO_DONE; 545fb59c426SYoshinobu Inoue } 546fb59c426SYoshinobu Inoue 547f0ffb944SJulian Elischer tcp_input(m, *offp); 548fb59c426SYoshinobu Inoue return IPPROTO_DONE; 549fb59c426SYoshinobu Inoue } 550b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 551fb59c426SYoshinobu Inoue 552df8bae1dSRodney W. Grimes void 553ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 554df8bae1dSRodney W. Grimes { 555b287c6c7SBjoern A. Zeeb struct tcphdr *th = NULL; 556ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 557b287c6c7SBjoern A. Zeeb #ifdef INET 558ad3f9ab3SAndre Oppermann struct ipovly *ipov; 559b287c6c7SBjoern A. Zeeb #endif 560ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 561302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 562302ce8d6SAndre Oppermann struct socket *so = NULL; 563e79adb8eSGarrett Wollman u_char *optp = NULL; 56426f9a767SRodney W. Grimes int optlen = 0; 565b287c6c7SBjoern A. Zeeb #ifdef INET 566b287c6c7SBjoern A. Zeeb int len; 567b287c6c7SBjoern A. Zeeb #endif 568b287c6c7SBjoern A. Zeeb int tlen = 0, off; 569fb59c426SYoshinobu Inoue int drop_hdrlen; 570ad3f9ab3SAndre Oppermann int thflags; 571302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 5722903309aSAttilio Rao #ifdef TCP_SIGNATURE 5732903309aSAttilio Rao uint8_t sig_checked = 0; 5742903309aSAttilio Rao #endif 575b287c6c7SBjoern A. Zeeb uint8_t iptos = 0; 5769b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5779b932e9eSAndre Oppermann struct m_tag *fwd_tag; 5789b932e9eSAndre Oppermann #endif 579c068736aSJeffrey Hsu #ifdef INET6 580302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 581c068736aSJeffrey Hsu int isipv6; 582c068736aSJeffrey Hsu #else 583a160e630SAndre Oppermann const void *ip6 = NULL; 584b287c6c7SBjoern A. Zeeb #if (defined(INET) && defined(IPFIREWALL_FORWARD)) || defined(TCPDEBUG) 585c068736aSJeffrey Hsu const int isipv6 = 0; 586c068736aSJeffrey Hsu #endif 587b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 588302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 589a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 590252ca428SRobert Watson int ti_locked; 591252ca428SRobert Watson #define TI_UNLOCKED 1 592fa046d87SRobert Watson #define TI_WLOCKED 2 593f76fcf6dSJeffrey Hsu 594610ee2f9SDavid Greenman #ifdef TCPDEBUG 595c068736aSJeffrey Hsu /* 596c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 597c068736aSJeffrey Hsu * now IPv6. 598c068736aSJeffrey Hsu */ 59914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 600410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 601610ee2f9SDavid Greenman short ostate = 0; 602610ee2f9SDavid Greenman #endif 603c068736aSJeffrey Hsu 604fb59c426SYoshinobu Inoue #ifdef INET6 605fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 606fb59c426SYoshinobu Inoue #endif 607a0292f23SGarrett Wollman 608302ce8d6SAndre Oppermann to.to_flags = 0; 60978b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 610fb59c426SYoshinobu Inoue 61104d3a452SHajimu UMEMOTO #ifdef INET6 612b287c6c7SBjoern A. Zeeb if (isipv6) { 6138d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 614fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 615fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 616fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 61778b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 618fb59c426SYoshinobu Inoue goto drop; 619fb59c426SYoshinobu Inoue } 620fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 62133841545SHajimu UMEMOTO 62233841545SHajimu UMEMOTO /* 62333841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 62433841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 62533841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 62633841545SHajimu UMEMOTO * 62733841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 62833841545SHajimu UMEMOTO * already dropped in ip6_input. 62933841545SHajimu UMEMOTO */ 63033841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 63133841545SHajimu UMEMOTO /* XXX stat */ 63233841545SHajimu UMEMOTO goto drop; 63333841545SHajimu UMEMOTO } 634b287c6c7SBjoern A. Zeeb } 63504d3a452SHajimu UMEMOTO #endif 636b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 637b287c6c7SBjoern A. Zeeb else 638b287c6c7SBjoern A. Zeeb #endif 639b287c6c7SBjoern A. Zeeb #ifdef INET 640b287c6c7SBjoern A. Zeeb { 641df8bae1dSRodney W. Grimes /* 642df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 643df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 644df8bae1dSRodney W. Grimes */ 645fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 646df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 647fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 648fb59c426SYoshinobu Inoue } 649df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 6504dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 6514dfdffe9SAndre Oppermann == NULL) { 65278b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 653df8bae1dSRodney W. Grimes return; 654df8bae1dSRodney W. Grimes } 655df8bae1dSRodney W. Grimes } 656fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 657fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 658db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 659db4f9cc7SJonathan Lemon tlen = ip->ip_len; 660df8bae1dSRodney W. Grimes 661db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 662db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 663db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 664db4f9cc7SJonathan Lemon else 665db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 666c068736aSJeffrey Hsu ip->ip_dst.s_addr, 667c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 668c068736aSJeffrey Hsu ip->ip_len + 669c068736aSJeffrey Hsu IPPROTO_TCP)); 670db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 6713c653157SHartmut Brandt #ifdef TCPDEBUG 6723c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 6733c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 6743c653157SHartmut Brandt #endif 675db4f9cc7SJonathan Lemon } else { 676df8bae1dSRodney W. Grimes /* 677df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 678df8bae1dSRodney W. Grimes */ 679df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 680fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 681fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 682fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 683fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 684db4f9cc7SJonathan Lemon } 685fb59c426SYoshinobu Inoue if (th->th_sum) { 68678b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 687df8bae1dSRodney W. Grimes goto drop; 688df8bae1dSRodney W. Grimes } 689fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 690fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 691fb59c426SYoshinobu Inoue } 692b287c6c7SBjoern A. Zeeb #endif /* INET */ 693df8bae1dSRodney W. Grimes 694f2512ba1SRui Paulo #ifdef INET6 695f2512ba1SRui Paulo if (isipv6) 696f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 697b287c6c7SBjoern A. Zeeb #endif 698b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 699f2512ba1SRui Paulo else 700f2512ba1SRui Paulo #endif 701b287c6c7SBjoern A. Zeeb #ifdef INET 702f2512ba1SRui Paulo iptos = ip->ip_tos; 703b287c6c7SBjoern A. Zeeb #endif 704f2512ba1SRui Paulo 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 707df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 708df8bae1dSRodney W. Grimes */ 709fb59c426SYoshinobu Inoue off = th->th_off << 2; 710df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 71178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 712df8bae1dSRodney W. Grimes goto drop; 713df8bae1dSRodney W. Grimes } 714fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 715df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 71604d3a452SHajimu UMEMOTO #ifdef INET6 717b287c6c7SBjoern A. Zeeb if (isipv6) { 718fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 719fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 720fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 721b287c6c7SBjoern A. Zeeb } 72204d3a452SHajimu UMEMOTO #endif 723b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 724b287c6c7SBjoern A. Zeeb else 725b287c6c7SBjoern A. Zeeb #endif 726b287c6c7SBjoern A. Zeeb #ifdef INET 727b287c6c7SBjoern A. Zeeb { 728df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 729c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 7304dfdffe9SAndre Oppermann == NULL) { 73178b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 732df8bae1dSRodney W. Grimes return; 733df8bae1dSRodney W. Grimes } 734fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 735fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 736fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 737fb59c426SYoshinobu Inoue } 738df8bae1dSRodney W. Grimes } 739b287c6c7SBjoern A. Zeeb #endif 740df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 741fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 742df8bae1dSRodney W. Grimes } 743fb59c426SYoshinobu Inoue thflags = th->th_flags; 744df8bae1dSRodney W. Grimes 745e46cd3d4SDag-Erling Smørgrav /* 746df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 747df8bae1dSRodney W. Grimes */ 7482903309aSAttilio Rao tcp_fields_to_host(th); 749df8bae1dSRodney W. Grimes 750df8bae1dSRodney W. Grimes /* 751794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 752755c1f07SAndras Olah */ 753fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 754755c1f07SAndras Olah 755755c1f07SAndras Olah /* 756fa046d87SRobert Watson * Locate pcb for segment; if we're likely to add or remove a 757fa046d87SRobert Watson * connection then first acquire pcbinfo lock. There are two cases 758fa046d87SRobert Watson * where we might discover later we need a write lock despite the 759fa046d87SRobert Watson * flags: ACKs moving a connection out of the syncache, and ACKs for 760fa046d87SRobert Watson * a connection in TIMEWAIT. 761df8bae1dSRodney W. Grimes */ 762fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) { 763603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 764252ca428SRobert Watson ti_locked = TI_WLOCKED; 765fa046d87SRobert Watson } else 766fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 767252ca428SRobert Watson 768df8bae1dSRodney W. Grimes findpcb: 769252ca428SRobert Watson #ifdef INVARIANTS 770fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 771603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 772fa046d87SRobert Watson } else { 773fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 774fa046d87SRobert Watson } 775252ca428SRobert Watson #endif 776252ca428SRobert Watson 7779b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 7788d573cc1SAndre Oppermann /* 7798d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 7808d573cc1SAndre Oppermann */ 7819b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 782*8a006adbSBjoern A. Zeeb #endif /* IPFIREWALL_FORWARD */ 7839b932e9eSAndre Oppermann 784*8a006adbSBjoern A. Zeeb #ifdef INET6 785*8a006adbSBjoern A. Zeeb #ifdef IPFIREWALL_FORWARD 786*8a006adbSBjoern A. Zeeb if (isipv6 && fwd_tag != NULL) { 787*8a006adbSBjoern A. Zeeb struct sockaddr_in6 *next_hop6; 788*8a006adbSBjoern A. Zeeb 789*8a006adbSBjoern A. Zeeb next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1); 790*8a006adbSBjoern A. Zeeb /* 791*8a006adbSBjoern A. Zeeb * Transparently forwarded. Pretend to be the destination. 792*8a006adbSBjoern A. Zeeb * Already got one like this? 793*8a006adbSBjoern A. Zeeb */ 794*8a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, 795*8a006adbSBjoern A. Zeeb &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport, 796*8a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m); 797*8a006adbSBjoern A. Zeeb if (!inp) { 798*8a006adbSBjoern A. Zeeb /* 799*8a006adbSBjoern A. Zeeb * It's new. Try to find the ambushing socket. 800*8a006adbSBjoern A. Zeeb * Because we've rewritten the destination address, 801*8a006adbSBjoern A. Zeeb * any hardware-generated hash is ignored. 802*8a006adbSBjoern A. Zeeb */ 803*8a006adbSBjoern A. Zeeb inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src, 804*8a006adbSBjoern A. Zeeb th->th_sport, &next_hop6->sin6_addr, 805*8a006adbSBjoern A. Zeeb next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) : 806*8a006adbSBjoern A. Zeeb th->th_dport, INPLOOKUP_WILDCARD | 807*8a006adbSBjoern A. Zeeb INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 808*8a006adbSBjoern A. Zeeb } 809*8a006adbSBjoern A. Zeeb /* Remove the tag from the packet. We don't need it anymore. */ 810*8a006adbSBjoern A. Zeeb m_tag_delete(m, fwd_tag); 811*8a006adbSBjoern A. Zeeb } else 812*8a006adbSBjoern A. Zeeb #endif /* IPFIREWALL_FORWARD */ 813*8a006adbSBjoern A. Zeeb if (isipv6) { 814*8a006adbSBjoern A. Zeeb inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src, 815*8a006adbSBjoern A. Zeeb th->th_sport, &ip6->ip6_dst, th->th_dport, 816*8a006adbSBjoern A. Zeeb INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 817*8a006adbSBjoern A. Zeeb m->m_pkthdr.rcvif, m); 818*8a006adbSBjoern A. Zeeb } 819*8a006adbSBjoern A. Zeeb #endif /* INET6 */ 820*8a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET) 821*8a006adbSBjoern A. Zeeb else 822*8a006adbSBjoern A. Zeeb #endif 823*8a006adbSBjoern A. Zeeb #ifdef INET 824*8a006adbSBjoern A. Zeeb #ifdef IPFIREWALL_FORWARD 825*8a006adbSBjoern A. Zeeb if (fwd_tag != NULL) { 8269b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 8279b932e9eSAndre Oppermann 8289b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 829f9e354dfSJulian Elischer /* 8302b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 831f9e354dfSJulian Elischer * already got one like this? 832f9e354dfSJulian Elischer */ 833d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport, 834fa046d87SRobert Watson ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB, 835d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 836fa046d87SRobert Watson if (!inp) { 837fa046d87SRobert Watson /* 838fa046d87SRobert Watson * It's new. Try to find the ambushing socket. 839d3c1f003SRobert Watson * Because we've rewritten the destination address, 840d3c1f003SRobert Watson * any hardware-generated hash is ignored. 841fa046d87SRobert Watson */ 842fa046d87SRobert Watson inp = in_pcblookup(&V_tcbinfo, ip->ip_src, 843fa046d87SRobert Watson th->th_sport, next_hop->sin_addr, 844fa046d87SRobert Watson next_hop->sin_port ? ntohs(next_hop->sin_port) : 845fa046d87SRobert Watson th->th_dport, INPLOOKUP_WILDCARD | 846fa046d87SRobert Watson INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif); 847f9e354dfSJulian Elischer } 8489b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 8499b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 850b10fbdeaSAndre Oppermann } else 8519b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 852d3c1f003SRobert Watson inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, 853fa046d87SRobert Watson th->th_sport, ip->ip_dst, th->th_dport, 854fa046d87SRobert Watson INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB, 855d3c1f003SRobert Watson m->m_pkthdr.rcvif, m); 856*8a006adbSBjoern A. Zeeb #endif /* INET */ 857f9e354dfSJulian Elischer 858df8bae1dSRodney W. Grimes /* 859574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 860574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 8618b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 862df8bae1dSRodney W. Grimes */ 863816a3d83SPoul-Henning Kamp if (inp == NULL) { 864574b6964SAndre Oppermann /* 865574b6964SAndre Oppermann * Log communication attempts to ports that are not 866574b6964SAndre Oppermann * in use. 867574b6964SAndre Oppermann */ 868574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 869574b6964SAndre Oppermann tcp_log_in_vain == 2) { 870b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 871df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 872df541e5fSAndre Oppermann "to closed port\n", s, __func__); 8732e4e1b4cSGeoff Rehmet } 874574b6964SAndre Oppermann /* 875574b6964SAndre Oppermann * When blackholing do not respond with a RST but 876574b6964SAndre Oppermann * completely ignore the segment and drop it. 877574b6964SAndre Oppermann */ 878603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 879603724d3SBjoern A. Zeeb V_blackhole == 2) 8801929eae1SAndre Oppermann goto dropunlock; 881574b6964SAndre Oppermann 882a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 883a57815efSBosko Milekic goto dropwithreset; 884816a3d83SPoul-Henning Kamp } 885fa046d87SRobert Watson INP_WLOCK_ASSERT(inp); 88680cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 88780cb9f21SKip Macy && (m->m_flags & M_FLOWID) 88880cb9f21SKip Macy && ((inp->inp_socket == NULL) 88980cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 89080cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 89180cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 89280cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 89380cb9f21SKip Macy } 894a2589465SKen Smith #ifdef IPSEC 895a2589465SKen Smith #ifdef INET6 896a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 897603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 898a2589465SKen Smith goto dropunlock; 899a2589465SKen Smith } else 900a2589465SKen Smith #endif /* INET6 */ 901a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 902603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 903a2589465SKen Smith goto dropunlock; 904a2589465SKen Smith } 905a2589465SKen Smith #endif /* IPSEC */ 906a2589465SKen Smith 9078d573cc1SAndre Oppermann /* 9088d573cc1SAndre Oppermann * Check the minimum TTL for socket. 9098d573cc1SAndre Oppermann */ 91034f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 91134f83c52SGeorge V. Neville-Neil #ifdef INET6 91234f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 913302ce8d6SAndre Oppermann goto dropunlock; 91402707462SAndre Oppermann else 91534f83c52SGeorge V. Neville-Neil #endif 91634f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 917302ce8d6SAndre Oppermann goto dropunlock; 91834f83c52SGeorge V. Neville-Neil } 919936cd18dSAndre Oppermann 920340c35deSJonathan Lemon /* 921252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 922252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 923252ca428SRobert Watson * legitimate new connection attempt the old INPCB gets removed and 924252ca428SRobert Watson * we can try again to find a listening socket. 925252ca428SRobert Watson * 926fa046d87SRobert Watson * At this point, due to earlier optimism, we may hold only an inpcb 927fa046d87SRobert Watson * lock, and not the inpcbinfo write lock. If so, we need to try to 928fa046d87SRobert Watson * acquire it, or if that fails, acquire a reference on the inpcb, 929fa046d87SRobert Watson * drop all locks, acquire a global write lock, and then re-acquire 930fa046d87SRobert Watson * the inpcb lock. We may at that point discover that another thread 931fa046d87SRobert Watson * has tried to free the inpcb, in which case we need to loop back 932fa046d87SRobert Watson * and try to find a new inpcb to deliver to. 933fa046d87SRobert Watson * 934fa046d87SRobert Watson * XXXRW: It may be time to rethink timewait locking. 935340c35deSJonathan Lemon */ 936883e9bc4SRobert Watson relocked: 937ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 938fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 939fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 940252ca428SRobert Watson in_pcbref(inp); 941252ca428SRobert Watson INP_WUNLOCK(inp); 942252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 943252ca428SRobert Watson ti_locked = TI_WLOCKED; 944252ca428SRobert Watson INP_WLOCK(inp); 945fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 946252ca428SRobert Watson inp = NULL; 947252ca428SRobert Watson goto findpcb; 948252ca428SRobert Watson } 949f681a5fdSRobert Watson } else 950252ca428SRobert Watson ti_locked = TI_WLOCKED; 951252ca428SRobert Watson } 952252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 953252ca428SRobert Watson 9542903309aSAttilio Rao #ifdef TCP_SIGNATURE 9552903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 9562903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 9572903309aSAttilio Rao if (sig_checked == 0) { 9582903309aSAttilio Rao tp = intotcpcb(inp); 9592903309aSAttilio Rao if (tp == NULL || tp->t_state == TCPS_CLOSED) { 9602903309aSAttilio Rao rstreason = BANDLIM_RST_CLOSEDPORT; 9612903309aSAttilio Rao goto dropwithreset; 9622903309aSAttilio Rao } 9632903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, 9642903309aSAttilio Rao &to, th, tp->t_flags)) 9652903309aSAttilio Rao goto dropunlock; 9662903309aSAttilio Rao sig_checked = 1; 9672903309aSAttilio Rao } 9682903309aSAttilio Rao #else 969340c35deSJonathan Lemon if (thflags & TH_SYN) 970f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 9712903309aSAttilio Rao #endif 9728d573cc1SAndre Oppermann /* 9738d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 9748d573cc1SAndre Oppermann */ 9752104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 976340c35deSJonathan Lemon goto findpcb; 977603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 978340c35deSJonathan Lemon return; 979340c35deSJonathan Lemon } 980794235b7SAndre Oppermann /* 981794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 982794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 983794235b7SAndre Oppermann * segment and send an appropriate response. 984794235b7SAndre Oppermann */ 985df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 986a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 987a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 988a57815efSBosko Milekic goto dropwithreset; 98909f81a46SBosko Milekic } 990df8bae1dSRodney W. Grimes 991252ca428SRobert Watson /* 992252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 993fa046d87SRobert Watson * inpcbinfo write lock but don't hold it. In this case, attempt to 994fa046d87SRobert Watson * acquire using the same strategy as the TIMEWAIT case above. If we 995fa046d87SRobert Watson * relock, we have to jump back to 'relocked' as the connection might 996fa046d87SRobert Watson * now be in TIMEWAIT. 997252ca428SRobert Watson */ 998fa046d87SRobert Watson #ifdef INVARIANTS 999fa046d87SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0) 1000fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1001fa046d87SRobert Watson #endif 1002fa046d87SRobert Watson if (tp->t_state != TCPS_ESTABLISHED) { 1003fa046d87SRobert Watson if (ti_locked == TI_UNLOCKED) { 1004fa046d87SRobert Watson if (INP_INFO_TRY_WLOCK(&V_tcbinfo) == 0) { 1005252ca428SRobert Watson in_pcbref(inp); 1006252ca428SRobert Watson INP_WUNLOCK(inp); 1007252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 1008252ca428SRobert Watson ti_locked = TI_WLOCKED; 1009252ca428SRobert Watson INP_WLOCK(inp); 1010fa046d87SRobert Watson if (in_pcbrele_wlocked(inp)) { 1011252ca428SRobert Watson inp = NULL; 1012252ca428SRobert Watson goto findpcb; 1013252ca428SRobert Watson } 1014883e9bc4SRobert Watson goto relocked; 1015f681a5fdSRobert Watson } else 1016252ca428SRobert Watson ti_locked = TI_WLOCKED; 1017252ca428SRobert Watson } 1018252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1019252ca428SRobert Watson } 1020252ca428SRobert Watson 1021c488362eSRobert Watson #ifdef MAC 10228501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 102330d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 1024302ce8d6SAndre Oppermann goto dropunlock; 1025c488362eSRobert Watson #endif 1026a557af22SRobert Watson so = inp->inp_socket; 1027302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 1028610ee2f9SDavid Greenman #ifdef TCPDEBUG 1029df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 1030df8bae1dSRodney W. Grimes ostate = tp->t_state; 1031d30d90dcSMaxim Konovalov if (isipv6) { 103210fe523eSMaxim Konovalov #ifdef INET6 1033540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 1034d30d90dcSMaxim Konovalov #endif 1035d30d90dcSMaxim Konovalov } else 1036540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 1037fb59c426SYoshinobu Inoue tcp_savetcp = *th; 1038df8bae1dSRodney W. Grimes } 1039b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1040db33b3e6SAndre Oppermann /* 1041db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 1042db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 1043fa046d87SRobert Watson * attempt or the completion of a previous one. Because listen 1044fa046d87SRobert Watson * sockets are never in TCPS_ESTABLISHED, the V_tcbinfo lock will be 1045fa046d87SRobert Watson * held in this case. 1046db33b3e6SAndre Oppermann */ 1047540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 1048540e8b7eSJeffrey Hsu struct in_conninfo inc; 1049540e8b7eSJeffrey Hsu 10507824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 10517824d002SAndre Oppermann "tp not listening", __func__)); 1052fa046d87SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 10537824d002SAndre Oppermann 10548bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 1055302ce8d6SAndre Oppermann #ifdef INET6 1056be2ac88cSJonathan Lemon if (isipv6) { 1057dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 1058be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 1059be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 1060302ce8d6SAndre Oppermann } else 1061302ce8d6SAndre Oppermann #endif 1062302ce8d6SAndre Oppermann { 1063be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 1064be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 1065be2ac88cSJonathan Lemon } 1066be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 1067be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 10687973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 1069be2ac88cSJonathan Lemon 1070fb59c426SYoshinobu Inoue /* 10718d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 10728d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 10738d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 1074fb59c426SYoshinobu Inoue */ 1075be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 1076bf6d304aSAndre Oppermann /* 1077bf6d304aSAndre Oppermann * Parse the TCP options here because 1078bf6d304aSAndre Oppermann * syncookies need access to the reflected 1079bf6d304aSAndre Oppermann * timestamp. 1080bf6d304aSAndre Oppermann */ 1081bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 10829fa198beSAndre Oppermann /* 10839fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 10849fa198beSAndre Oppermann * inp and tcpinfo locks. 10859fa198beSAndre Oppermann */ 1086bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 10874195b4afSPaul Traina /* 1088e207f800SAndre Oppermann * No syncache entry or ACK was not 1089be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 10908d573cc1SAndre Oppermann * NB: syncache did its own logging 10918d573cc1SAndre Oppermann * of the failure cause. 10924195b4afSPaul Traina */ 1093be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 1094be2ac88cSJonathan Lemon goto dropwithreset; 1095be2ac88cSJonathan Lemon } 1096f76fcf6dSJeffrey Hsu if (so == NULL) { 1097be2ac88cSJonathan Lemon /* 1098e207f800SAndre Oppermann * We completed the 3-way handshake 1099e207f800SAndre Oppermann * but could not allocate a socket 1100e207f800SAndre Oppermann * either due to memory shortage, 1101e207f800SAndre Oppermann * listen queue length limits or 1102a160e630SAndre Oppermann * global socket limits. Send RST 1103a160e630SAndre Oppermann * or wait and have the remote end 1104a160e630SAndre Oppermann * retransmit the ACK for another 1105a160e630SAndre Oppermann * try. 1106be2ac88cSJonathan Lemon */ 1107a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1108a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11098d573cc1SAndre Oppermann "Socket allocation failed due to " 11108d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 1111ac957cd2SJulian Elischer s, __func__, 1112ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 1113ac957cd2SJulian Elischer "sending RST" : "try again"); 1114603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 1115e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 1116e207f800SAndre Oppermann goto dropwithreset; 1117a160e630SAndre Oppermann } else 1118a160e630SAndre Oppermann goto dropunlock; 1119f76fcf6dSJeffrey Hsu } 1120be2ac88cSJonathan Lemon /* 1121be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 11228d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 11238d573cc1SAndre Oppermann * created socket and update the tp variable. 1124be2ac88cSJonathan Lemon */ 11258501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 1126be2ac88cSJonathan Lemon inp = sotoinpcb(so); 11278501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 1128be2ac88cSJonathan Lemon tp = intotcpcb(inp); 11298d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 11308d573cc1SAndre Oppermann ("%s: ", __func__)); 11312903309aSAttilio Rao #ifdef TCP_SIGNATURE 11322903309aSAttilio Rao if (sig_checked == 0) { 11332903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 11342903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 11352903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, 11362903309aSAttilio Rao optlen, &to, th, tp->t_flags)) { 11372903309aSAttilio Rao 11382903309aSAttilio Rao /* 11392903309aSAttilio Rao * In SYN_SENT state if it receives an 11402903309aSAttilio Rao * RST, it is allowed for further 11412903309aSAttilio Rao * processing. 11422903309aSAttilio Rao */ 11432903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 11442903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 11452903309aSAttilio Rao goto dropunlock; 11462903309aSAttilio Rao } 11472903309aSAttilio Rao sig_checked = 1; 11482903309aSAttilio Rao } 11492903309aSAttilio Rao #endif 11502903309aSAttilio Rao 1151be2ac88cSJonathan Lemon /* 1152302ce8d6SAndre Oppermann * Process the segment and the data it 1153302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 1154302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 1155302ce8d6SAndre Oppermann */ 1156f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 1157252ca428SRobert Watson iptos, ti_locked); 1158603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1159302ce8d6SAndre Oppermann return; 1160be2ac88cSJonathan Lemon } 1161a160e630SAndre Oppermann /* 11628d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 11638d573cc1SAndre Oppermann * 1164a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 1165a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 1166a160e630SAndre Oppermann * retransmits. 116719bc77c5SAndre Oppermann * 116819bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 116919bc77c5SAndre Oppermann * causes. 1170a160e630SAndre Oppermann */ 1171a160e630SAndre Oppermann if (thflags & TH_RST) { 117219bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 1173a160e630SAndre Oppermann goto dropunlock; 1174a160e630SAndre Oppermann } 1175a160e630SAndre Oppermann /* 1176a160e630SAndre Oppermann * We can't do anything without SYN. 1177a160e630SAndre Oppermann */ 1178a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 1179a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1180a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1181773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 11828d573cc1SAndre Oppermann s, __func__); 118378b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1184a160e630SAndre Oppermann goto dropunlock; 1185a160e630SAndre Oppermann } 1186a160e630SAndre Oppermann /* 1187a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 1188a160e630SAndre Oppermann */ 1189fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1190a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1191a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 11928d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 11938d573cc1SAndre Oppermann s, __func__); 1194a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 119578b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1196a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1197a57815efSBosko Milekic goto dropwithreset; 11984195b4afSPaul Traina } 1199a160e630SAndre Oppermann /* 1200a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 1201a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 1202a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 1203a160e630SAndre Oppermann * TCP/IP stack. 1204a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 1205a160e630SAndre Oppermann * and is constantly refining its stack detection 1206a160e630SAndre Oppermann * strategies. 12078d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 1208a160e630SAndre Oppermann * and was used by RFC1644. 1209a160e630SAndre Oppermann */ 1210603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 1211a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1212a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 1213773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 12148d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 121578b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 1216302ce8d6SAndre Oppermann goto dropunlock; 1217ebb0cbeaSPaul Traina } 1218be2ac88cSJonathan Lemon /* 1219be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 1220a160e630SAndre Oppermann * 1221a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 1222a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 1223a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 1224be2ac88cSJonathan Lemon */ 1225a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 1226a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 1227a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 1228a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 122933841545SHajimu UMEMOTO #ifdef INET6 123033841545SHajimu UMEMOTO /* 123133841545SHajimu UMEMOTO * If deprecated address is forbidden, 123233841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 123333841545SHajimu UMEMOTO * address to prevent any new inbound connection from 123433841545SHajimu UMEMOTO * getting established. 123533841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 123633841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 123733841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 123833841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 123933841545SHajimu UMEMOTO * for the exchange. 124033841545SHajimu UMEMOTO * 124133841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 124233841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 124333841545SHajimu UMEMOTO * SYN in this case. 124433841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 124533841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 124633841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 124733841545SHajimu UMEMOTO * used" 124833841545SHajimu UMEMOTO * 2. use of it with new communication: 124933841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 125033841545SHajimu UMEMOTO * with sufficient scope is available" 125133841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 125233841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 125333841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 125433841545SHajimu UMEMOTO * 125533841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 125633841545SHajimu UMEMOTO * multiple description text for deprecated address 125733841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 125833841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 125933841545SHajimu UMEMOTO */ 1260603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 126133841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 126233841545SHajimu UMEMOTO 12638c0fec80SRobert Watson ia6 = ip6_getdstifaddr(m); 12648c0fec80SRobert Watson if (ia6 != NULL && 126533841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 12668c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 1267a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1268a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12698d573cc1SAndre Oppermann "Connection attempt to deprecated " 12708d573cc1SAndre Oppermann "IPv6 address rejected\n", 1271a160e630SAndre Oppermann s, __func__); 127233841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 127333841545SHajimu UMEMOTO goto dropwithreset; 127433841545SHajimu UMEMOTO } 12758c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 127633841545SHajimu UMEMOTO } 1277b287c6c7SBjoern A. Zeeb #endif /* INET6 */ 127865f28919SJesper Skriver /* 1279db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 12808d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 1281db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 12828d573cc1SAndre Oppermann * If it is from this socket it must be forged. 12838d573cc1SAndre Oppermann * Don't respond if the source or destination is a 12848d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 128593ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 128693ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 128793ec91baSCrist J. Clark * in_broadcast() to find them. 1288be2ac88cSJonathan Lemon */ 12898d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 12908d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 12918d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 12928d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 1293773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 1294302ce8d6SAndre Oppermann goto dropunlock; 12958d573cc1SAndre Oppermann } 1296db33b3e6SAndre Oppermann #ifdef INET6 1297b287c6c7SBjoern A. Zeeb if (isipv6) { 1298db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1299a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 1300a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1301a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13028d573cc1SAndre Oppermann "Connection attempt to/from self " 1303773673c1SAndre Oppermann "ignored\n", s, __func__); 1304302ce8d6SAndre Oppermann goto dropunlock; 1305a160e630SAndre Oppermann } 1306be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 1307a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 1308a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1309a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13108d573cc1SAndre Oppermann "Connection attempt from/to multicast " 1311773673c1SAndre Oppermann "address ignored\n", s, __func__); 1312302ce8d6SAndre Oppermann goto dropunlock; 1313a160e630SAndre Oppermann } 1314b287c6c7SBjoern A. Zeeb } 1315db33b3e6SAndre Oppermann #endif 1316b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1317b287c6c7SBjoern A. Zeeb else 1318b287c6c7SBjoern A. Zeeb #endif 1319b287c6c7SBjoern A. Zeeb #ifdef INET 1320b287c6c7SBjoern A. Zeeb { 1321db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 1322a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 1323a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1324a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13258d573cc1SAndre Oppermann "Connection attempt from/to self " 1326773673c1SAndre Oppermann "ignored\n", s, __func__); 1327302ce8d6SAndre Oppermann goto dropunlock; 1328a160e630SAndre Oppermann } 1329be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 1330be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 13312ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1332a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1333a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1334a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 13358d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1336773673c1SAndre Oppermann "or multicast address ignored\n", 1337a160e630SAndre Oppermann s, __func__); 1338302ce8d6SAndre Oppermann goto dropunlock; 1339c068736aSJeffrey Hsu } 1340a160e630SAndre Oppermann } 1341b287c6c7SBjoern A. Zeeb #endif 1342be2ac88cSJonathan Lemon /* 1343db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1344db33b3e6SAndre Oppermann * for syncache. 1345be2ac88cSJonathan Lemon */ 13463c653157SHartmut Brandt #ifdef TCPDEBUG 13473c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13483c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13493c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 13503c653157SHartmut Brandt #endif 1351f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 13524d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 1353be2ac88cSJonathan Lemon /* 13544d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1355a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1356be2ac88cSJonathan Lemon */ 1357603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1358be2ac88cSJonathan Lemon return; 1359f76fcf6dSJeffrey Hsu } 1360db33b3e6SAndre Oppermann 13612903309aSAttilio Rao #ifdef TCP_SIGNATURE 13622903309aSAttilio Rao if (sig_checked == 0) { 13632903309aSAttilio Rao tcp_dooptions(&to, optp, optlen, 13642903309aSAttilio Rao (thflags & TH_SYN) ? TO_SYN : 0); 13652903309aSAttilio Rao if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to, 13662903309aSAttilio Rao th, tp->t_flags)) { 13672903309aSAttilio Rao 13682903309aSAttilio Rao /* 13692903309aSAttilio Rao * In SYN_SENT state if it receives an RST, it is 13702903309aSAttilio Rao * allowed for further processing. 13712903309aSAttilio Rao */ 13722903309aSAttilio Rao if ((thflags & TH_RST) == 0 || 13732903309aSAttilio Rao (tp->t_state == TCPS_SYN_SENT) == 0) 13742903309aSAttilio Rao goto dropunlock; 13752903309aSAttilio Rao } 13762903309aSAttilio Rao sig_checked = 1; 13772903309aSAttilio Rao } 13782903309aSAttilio Rao #endif 13792903309aSAttilio Rao 1380302ce8d6SAndre Oppermann /* 13818d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 13828d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 13838d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1384302ce8d6SAndre Oppermann */ 1385252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1386603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1387302ce8d6SAndre Oppermann return; 1388be2ac88cSJonathan Lemon 1389302ce8d6SAndre Oppermann dropwithreset: 1390fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1391dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1392252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1393fa046d87SRobert Watson } 1394fa046d87SRobert Watson #ifdef INVARIANTS 1395fa046d87SRobert Watson else { 1396fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset " 1397fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1398fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1399fa046d87SRobert Watson } 1400fa046d87SRobert Watson #endif 1401014ea782SRobert Watson 1402014ea782SRobert Watson if (inp != NULL) { 1403302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1404014ea782SRobert Watson INP_WUNLOCK(inp); 1405dd8ac7f9SRobert Watson } else 1406014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1407302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1408014ea782SRobert Watson goto drop; 1409014ea782SRobert Watson 1410302ce8d6SAndre Oppermann dropunlock: 1411fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 1412252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1413252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1414fa046d87SRobert Watson } 1415fa046d87SRobert Watson #ifdef INVARIANTS 1416fa046d87SRobert Watson else { 1417fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock " 1418fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1419fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1420fa046d87SRobert Watson } 1421fa046d87SRobert Watson #endif 1422252ca428SRobert Watson 14239fa198beSAndre Oppermann if (inp != NULL) 14248501a69cSRobert Watson INP_WUNLOCK(inp); 1425014ea782SRobert Watson 1426302ce8d6SAndre Oppermann drop: 1427603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1428a160e630SAndre Oppermann if (s != NULL) 1429a160e630SAndre Oppermann free(s, M_TCPLOG); 1430302ce8d6SAndre Oppermann if (m != NULL) 1431302ce8d6SAndre Oppermann m_freem(m); 1432302ce8d6SAndre Oppermann } 1433302ce8d6SAndre Oppermann 1434679d9708SAndre Oppermann static void 1435302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1436252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1437252ca428SRobert Watson int ti_locked) 1438302ce8d6SAndre Oppermann { 1439302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1440302ce8d6SAndre Oppermann int rstreason, todrop, win; 1441302ce8d6SAndre Oppermann u_long tiwin; 1442302ce8d6SAndre Oppermann struct tcpopt to; 1443302ce8d6SAndre Oppermann 144414739780SMaxim Konovalov #ifdef TCPDEBUG 144514739780SMaxim Konovalov /* 144614739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 144714739780SMaxim Konovalov * now IPv6. 144814739780SMaxim Konovalov */ 144914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 145014739780SMaxim Konovalov struct tcphdr tcp_savetcp; 145114739780SMaxim Konovalov short ostate = 0; 145214739780SMaxim Konovalov #endif 1453302ce8d6SAndre Oppermann thflags = th->th_flags; 1454d64a46eaSLawrence Stewart tp->sackhint.last_sack_ack = 0; 1455302ce8d6SAndre Oppermann 1456252ca428SRobert Watson /* 1457252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1458252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 1459252ca428SRobert Watson * allow either a read lock or a write lock, as we may have acquired 1460252ca428SRobert Watson * a write lock due to a race. 1461252ca428SRobert Watson * 1462d15fb965SRobert Watson * Require a global write lock for SYN/FIN/RST segments or 1463252ca428SRobert Watson * non-established connections; otherwise accept either a read or 1464252ca428SRobert Watson * write lock, as we may have conservatively acquired a write lock in 1465252ca428SRobert Watson * certain cases in tcp_input() (is this still true?). Currently we 1466252ca428SRobert Watson * will never enter with no lock, so we try to drop it quickly in the 1467252ca428SRobert Watson * common pure ack/pure data cases. 1468252ca428SRobert Watson */ 1469252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1470252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1471252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1472252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1473603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1474252ca428SRobert Watson } else { 1475252ca428SRobert Watson #ifdef INVARIANTS 1476fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1477252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1478fa046d87SRobert Watson else { 1479fa046d87SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST " 1480fa046d87SRobert Watson "ti_locked: %d", __func__, ti_locked)); 1481fa046d87SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1482fa046d87SRobert Watson } 1483252ca428SRobert Watson #endif 1484252ca428SRobert Watson } 14858501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1486679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1487679d9708SAndre Oppermann __func__)); 1488679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1489679d9708SAndre Oppermann __func__)); 1490df8bae1dSRodney W. Grimes 1491df8bae1dSRodney W. Grimes /* 1492df8bae1dSRodney W. Grimes * Segment received on connection. 1493df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1494e8949f74SAndre Oppermann * XXX: This should be done after segment 1495e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1496df8bae1dSRodney W. Grimes */ 14979b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 14987ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1499b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1500df8bae1dSRodney W. Grimes 1501df8bae1dSRodney W. Grimes /* 1502464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1503e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1504464fcfbcSAndre Oppermann */ 1505464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1506464fcfbcSAndre Oppermann 1507464fcfbcSAndre Oppermann /* 1508f2512ba1SRui Paulo * TCP ECN processing. 1509f2512ba1SRui Paulo */ 1510f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 15119c251892SRui Paulo if (thflags & TH_CWR) 15129c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1513f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1514f2512ba1SRui Paulo case IPTOS_ECN_CE: 1515f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 151678b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1517f2512ba1SRui Paulo break; 1518f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 151978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1520f2512ba1SRui Paulo break; 1521f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 152278b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1523f2512ba1SRui Paulo break; 1524f2512ba1SRui Paulo } 1525dbc42409SLawrence Stewart /* Congestion experienced. */ 1526dbc42409SLawrence Stewart if (thflags & TH_ECE) { 1527dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_ECN); 1528f2512ba1SRui Paulo } 1529f2512ba1SRui Paulo } 1530f2512ba1SRui Paulo 1531f2512ba1SRui Paulo /* 1532f72167f4SAndre Oppermann * Parse options on any incoming segment. 1533f72167f4SAndre Oppermann */ 1534302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1535302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1536302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1537f72167f4SAndre Oppermann 1538f72167f4SAndre Oppermann /* 1539f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1540bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1541bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1542bf6d304aSAndre Oppermann * was established. 1543f72167f4SAndre Oppermann */ 1544bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1545e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1546bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1547f72167f4SAndre Oppermann to.to_tsecr = 0; 1548bf6d304aSAndre Oppermann } 1549f72167f4SAndre Oppermann 1550f72167f4SAndre Oppermann /* 155197d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 155297d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 15535396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 15545396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 155597d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1556df8bae1dSRodney W. Grimes */ 15570a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1558464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1559464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1560be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 156102a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1562be2ac88cSJonathan Lemon } 15635396d0f8SAndre Oppermann /* 15645396d0f8SAndre Oppermann * Initial send window. It will be updated with 15655396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 15665396d0f8SAndre Oppermann */ 15675396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1568be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1569be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1570be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1571be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1572be2ac88cSJonathan Lemon } 1573be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1574be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 15753529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 15763529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 15773529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 15786d90faf3SPaul Saab } 15796d90faf3SPaul Saab 1580df8bae1dSRodney W. Grimes /* 1581df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1582df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1583df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1584df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1585df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1586df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1587df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1588df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1589df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1590df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1591df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1592df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1593a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1594c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1595a0292f23SGarrett Wollman * be TH_NEEDSYN. 1596df8bae1dSRodney W. Grimes */ 1597df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1598c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1599fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1600c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1601c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1602a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1603c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1604be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1605c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1606df8bae1dSRodney W. Grimes 1607df8bae1dSRodney W. Grimes /* 1608df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1609df8bae1dSRodney W. Grimes * record the timestamp. 1610a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1611a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1612df8bae1dSRodney W. Grimes */ 1613be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1614fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 16159b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1616a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1617df8bae1dSRodney W. Grimes } 1618df8bae1dSRodney W. Grimes 1619fb59c426SYoshinobu Inoue if (tlen == 0) { 1620fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1621fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1622dbc42409SLawrence Stewart !IN_RECOVERY(tp->t_flags) && 1623fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1624dbc42409SLawrence Stewart TAILQ_EMPTY(&tp->snd_holes)) { 1625df8bae1dSRodney W. Grimes /* 1626e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1627df8bae1dSRodney W. Grimes */ 1628fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1629252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1630252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1631252ca428SRobert Watson 163278b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1633252ca428SRobert Watson 16349b8b58e0SJonathan Lemon /* 1635e8949f74SAndre Oppermann * "bad retransmit" recovery. 16369b8b58e0SJonathan Lemon */ 16379b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 1638672dc4aeSJohn Baldwin tp->t_flags & TF_PREVVALID && 16396dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 1640dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 16419b8b58e0SJonathan Lemon } 1642fa55172bSMatthew Dillon 1643fa55172bSMatthew Dillon /* 1644fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1645fa55172bSMatthew Dillon * 1646fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1647fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1648fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1649fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1650fa55172bSMatthew Dillon */ 1651fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1652fa55172bSMatthew Dillon to.to_tsecr) { 1653eaf80179SAndre Oppermann if (!tp->t_rttlow || 1654eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1655eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1656a0292f23SGarrett Wollman tcp_xmit_timer(tp, 16579b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1658fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1659fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1660eaf80179SAndre Oppermann if (!tp->t_rttlow || 1661eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1662eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1663c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1664c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1665fa55172bSMatthew Dillon } 1666dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 166739bc9de5SLawrence Stewart 166839bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 166939bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 167039bc9de5SLawrence Stewart 167178b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 167278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1673df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 16749d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 16759d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 16769d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 1677dbc42409SLawrence Stewart 1678dbc42409SLawrence Stewart /* 1679dbc42409SLawrence Stewart * Let the congestion control algorithm update 1680dbc42409SLawrence Stewart * congestion control related information. This 1681dbc42409SLawrence Stewart * typically means increasing the congestion 1682dbc42409SLawrence Stewart * window. 1683dbc42409SLawrence Stewart */ 1684dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 1685dbc42409SLawrence Stewart 16869d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 16871645d090SJeff Roberson /* 1688e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 16891645d090SJeff Roberson * to th_ack. 16901645d090SJeff Roberson */ 1691cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1692b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1693df8bae1dSRodney W. Grimes m_freem(m); 1694e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1695df8bae1dSRodney W. Grimes 1696df8bae1dSRodney W. Grimes /* 1697df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1698df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1699df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1700df8bae1dSRodney W. Grimes * If process is waiting for space, 1701df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1702df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1703df8bae1dSRodney W. Grimes * decide between more output or persist. 1704e8949f74SAndre Oppermann */ 17053c653157SHartmut Brandt #ifdef TCPDEBUG 17063c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17073c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17083c653157SHartmut Brandt (void *)tcp_saveipgen, 17093c653157SHartmut Brandt &tcp_savetcp, 0); 17103c653157SHartmut Brandt #endif 1711df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1712b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1713b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1714b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1715b8152ba7SAndre Oppermann tp->t_rxtcur); 1716df8bae1dSRodney W. Grimes sowwakeup(so); 1717df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1718df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1719a14c749fSJonathan Lemon goto check_delack; 1720df8bae1dSRodney W. Grimes } 1721fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1722fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 17236741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 17246741ecf5SAndre Oppermann 1725df8bae1dSRodney W. Grimes /* 1726252ca428SRobert Watson * This is a pure, in-sequence data packet with 1727252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1728252ca428SRobert Watson * buffer space to take it. 1729df8bae1dSRodney W. Grimes */ 1730fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 1731252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1732252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1733252ca428SRobert Watson 17346d90faf3SPaul Saab /* Clean receiver SACK report if present */ 17353529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 17366d90faf3SPaul Saab tcp_clean_sackreport(tp); 173778b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1738fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 17391645d090SJeff Roberson /* 17401645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 17411645d090SJeff Roberson * th_seq. 17421645d090SJeff Roberson */ 17431645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 17441645d090SJeff Roberson /* 17451645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 17461645d090SJeff Roberson * rcv_nxt. 17471645d090SJeff Roberson */ 17481645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 174978b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 175078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 1751e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 17523c653157SHartmut Brandt #ifdef TCPDEBUG 17533c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 17543c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 17553c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 17563c653157SHartmut Brandt #endif 17576741ecf5SAndre Oppermann /* 17586741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 17596741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 17606741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 17616741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 17626741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 17636741ecf5SAndre Oppermann * 17646741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 17656741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 17666741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 17676741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 17686741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 17696741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 17706741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 17716741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 17726741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 17736741ecf5SAndre Oppermann * reassembly queue. 17746741ecf5SAndre Oppermann * 17756741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 17766741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 17776741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 17786741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 17796741ecf5SAndre Oppermann * current socket buffer size; 17806741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 17816741ecf5SAndre Oppermann * 17826741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 17836741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 17846741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 17856741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 17866741ecf5SAndre Oppermann * 17876741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 17886741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1789df8bae1dSRodney W. Grimes */ 1790603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 17916741ecf5SAndre Oppermann to.to_tsecr && 17926741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 17938502ec25SAndre Oppermann if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) && 17946741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 17956741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 17966741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 17976741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1798603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 17996741ecf5SAndre Oppermann newsize = 18006741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1801603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1802603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 18036741ecf5SAndre Oppermann } 18046741ecf5SAndre Oppermann /* Start over with next RTT. */ 18056741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18066741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18076741ecf5SAndre Oppermann } else 18086741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 18096741ecf5SAndre Oppermann } 18106741ecf5SAndre Oppermann 18116741ecf5SAndre Oppermann /* Add data to socket buffer. */ 18121e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1813c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1814c1c36a2cSMike Silbersack m_freem(m); 1815c1c36a2cSMike Silbersack } else { 18166741ecf5SAndre Oppermann /* 18176741ecf5SAndre Oppermann * Set new socket buffer size. 18186741ecf5SAndre Oppermann * Give up when limit is reached. 18196741ecf5SAndre Oppermann */ 18206741ecf5SAndre Oppermann if (newsize) 18216741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 18226c8286e4SRobert Watson newsize, so, NULL)) 18236741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1824fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 18251e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1826c1c36a2cSMike Silbersack } 1827e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 18281e4d7da7SRobert Watson sorwakeup_locked(so); 1829d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 18303bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1831f498eeeeSDavid Greenman } else { 1832e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1833e612a582SDavid Greenman tcp_output(tp); 1834e612a582SDavid Greenman } 1835a14c749fSJonathan Lemon goto check_delack; 1836df8bae1dSRodney W. Grimes } 1837df8bae1dSRodney W. Grimes } 1838df8bae1dSRodney W. Grimes 1839df8bae1dSRodney W. Grimes /* 1840df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1841df8bae1dSRodney W. Grimes * and then do TCP input processing. 1842df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1843df8bae1dSRodney W. Grimes * but not less than advertised window. 1844df8bae1dSRodney W. Grimes */ 1845df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1846df8bae1dSRodney W. Grimes if (win < 0) 1847df8bae1dSRodney W. Grimes win = 0; 1848f701e30dSJohn Baldwin KASSERT(SEQ_GEQ(tp->rcv_adv, tp->rcv_nxt), 1849f701e30dSJohn Baldwin ("tcp_input negative window: tp %p rcv_nxt %u rcv_adv %u", tp, 18505891ebd6SJohn Baldwin tp->rcv_nxt, tp->rcv_adv)); 185166e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1852df8bae1dSRodney W. Grimes 18536741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 18546741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 18556741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 18566741ecf5SAndre Oppermann 1857df8bae1dSRodney W. Grimes switch (tp->t_state) { 1858df8bae1dSRodney W. Grimes 1859df8bae1dSRodney W. Grimes /* 1860764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1861764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1862764d8cefSBill Fenner */ 1863764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1864fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1865fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1866a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1867a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1868a57815efSBosko Milekic goto dropwithreset; 1869a57815efSBosko Milekic } 1870764d8cefSBill Fenner break; 1871764d8cefSBill Fenner 1872764d8cefSBill Fenner /* 1873df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1874df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1875df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1876df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1877df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1878df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1879df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1880f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1881f2512ba1SRui Paulo * is ECN capable. 1882df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1883df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1884df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1885df8bae1dSRodney W. Grimes */ 1886df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1887fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1888fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1889fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1890a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1891a0292f23SGarrett Wollman goto dropwithreset; 1892a0292f23SGarrett Wollman } 18930ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1894df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 18950ca3f933SAndre Oppermann if (thflags & TH_RST) 1896df8bae1dSRodney W. Grimes goto drop; 18970ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1898df8bae1dSRodney W. Grimes goto drop; 1899464fcfbcSAndre Oppermann 1900fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1901df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1902fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 190378b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1904845799c1SAndras Olah soisconnected(so); 1905c488362eSRobert Watson #ifdef MAC 190630d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1907c488362eSRobert Watson #endif 1908845799c1SAndras Olah /* Do window scaling on this connection? */ 1909845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1910845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1911845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1912845799c1SAndras Olah } 1913766282cbSJohn Baldwin tp->rcv_adv += imin(tp->rcv_wnd, 1914766282cbSJohn Baldwin TCP_MAXWIN << tp->rcv_scale); 1915a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1916a0292f23SGarrett Wollman /* 1917a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1918a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1919a0292f23SGarrett Wollman */ 1920d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1921b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1922b8152ba7SAndre Oppermann tcp_delacktime); 1923a0292f23SGarrett Wollman else 1924a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1925f2512ba1SRui Paulo 1926603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1927f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 192878b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1929f2512ba1SRui Paulo } 1930f2512ba1SRui Paulo 1931a0292f23SGarrett Wollman /* 1932a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1933a0292f23SGarrett Wollman * Transitions: 1934a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1935a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1936a0292f23SGarrett Wollman */ 19379b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1938a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1939a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1940a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1941fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 19427ff19458SPaul Traina } else { 1943a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1944dbc42409SLawrence Stewart cc_conn_init(tp); 1945b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 19467ff19458SPaul Traina } 1947a0292f23SGarrett Wollman } else { 1948a0292f23SGarrett Wollman /* 1949c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1950c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1951c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1952c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1953c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1954a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1955a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1956a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1957a0292f23SGarrett Wollman */ 19584b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1959b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1960df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1961a0292f23SGarrett Wollman } 1962df8bae1dSRodney W. Grimes 1963252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1964252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1965252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 19668501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 19677cfc6904SRobert Watson 1968df8bae1dSRodney W. Grimes /* 1969fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1970df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1971df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1972df8bae1dSRodney W. Grimes */ 1973fb59c426SYoshinobu Inoue th->th_seq++; 1974fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1975fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1976df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1977fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1978fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 197978b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 198078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1981df8bae1dSRodney W. Grimes } 1982fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1983fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1984a0292f23SGarrett Wollman /* 1985a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1986a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1987a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1988a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1989a0292f23SGarrett Wollman * Otherwise, goto step 6. 1990a0292f23SGarrett Wollman */ 1991fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1992a0292f23SGarrett Wollman goto process_ACK; 1993c068736aSJeffrey Hsu 1994df8bae1dSRodney W. Grimes goto step6; 1995c068736aSJeffrey Hsu 1996a0292f23SGarrett Wollman /* 1997a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1998c94c54e4SAndre Oppermann * do normal processing. 1999a0292f23SGarrett Wollman * 2000c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 2001a0292f23SGarrett Wollman */ 2002a0292f23SGarrett Wollman case TCPS_LAST_ACK: 2003a0292f23SGarrett Wollman case TCPS_CLOSING: 2004a0292f23SGarrett Wollman break; /* continue normal processing */ 2005df8bae1dSRodney W. Grimes } 2006df8bae1dSRodney W. Grimes 2007df8bae1dSRodney W. Grimes /* 2008df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 200980ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 201080ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 201180ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 201280ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 201380ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 201480ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 201580ab7c0eSGarrett Wollman * killing a connection). 201680ab7c0eSGarrett Wollman * Then check timestamp, if present. 2017a0292f23SGarrett Wollman * Then check the connection count, if present. 2018df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 2019df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 2020df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 2021df8bae1dSRodney W. Grimes * 202280ab7c0eSGarrett Wollman * 202380ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 202480ab7c0eSGarrett Wollman * if this is a valid reset segment. 202580ab7c0eSGarrett Wollman * RFC 793 page 37: 202680ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 202780ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 202880ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 202980ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 203080ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 20311a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 20321a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 20331a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 20341a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 203580dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 203680dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 203780dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 203880dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 203980dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 204080dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 20418e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 204280ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 204380ab7c0eSGarrett Wollman * but they will eventually catch up. 204480ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 204580ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 204680ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 204780ab7c0eSGarrett Wollman * 204880ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 204980ab7c0eSGarrett Wollman * 205080ab7c0eSGarrett Wollman * DISCUSSION 205180ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 205280ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 205380ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 205480ab7c0eSGarrett Wollman * data. 205580ab7c0eSGarrett Wollman * 205680ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 205780ab7c0eSGarrett Wollman * the state: 205880ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 205980ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 206080ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 2061745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 206280ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 2063e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 206480ab7c0eSGarrett Wollman * Close the tcb. 2065e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 206680ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 206780ab7c0eSGarrett Wollman * RFC 1337. 206880ab7c0eSGarrett Wollman */ 2069fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 207095ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 207195ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 207280ab7c0eSGarrett Wollman switch (tp->t_state) { 207380ab7c0eSGarrett Wollman 207480ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 207580ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 207680ab7c0eSGarrett Wollman goto close; 207780ab7c0eSGarrett Wollman 207880ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 2079603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 208095ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 208195ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 208295ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 208395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 208478b50714SRobert Watson TCPSTAT_INC(tcps_badrst); 208580dd2a81SMike Silbersack goto drop; 208680dd2a81SMike Silbersack } 2087564aab1fSAndre Oppermann /* FALLTHROUGH */ 208880ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 208980ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 209080ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 209180ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 209280ab7c0eSGarrett Wollman close: 2093252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2094252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 2095252ca428SRobert Watson ti_locked)); 2096252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2097252ca428SRobert Watson 209880ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 209978b50714SRobert Watson TCPSTAT_INC(tcps_drops); 210080ab7c0eSGarrett Wollman tp = tcp_close(tp); 210180ab7c0eSGarrett Wollman break; 210280ab7c0eSGarrett Wollman 210380ab7c0eSGarrett Wollman case TCPS_CLOSING: 210480ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 2105252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2106252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 2107252ca428SRobert Watson ti_locked)); 2108252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2109252ca428SRobert Watson 211080ab7c0eSGarrett Wollman tp = tcp_close(tp); 211180ab7c0eSGarrett Wollman break; 211280ab7c0eSGarrett Wollman } 211380ab7c0eSGarrett Wollman } 211480ab7c0eSGarrett Wollman goto drop; 211580ab7c0eSGarrett Wollman } 211680ab7c0eSGarrett Wollman 211780ab7c0eSGarrett Wollman /* 2118df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 2119df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 2120df8bae1dSRodney W. Grimes */ 2121be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 212280ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 2123df8bae1dSRodney W. Grimes 2124df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 21251a0e7cfcSJohn Baldwin if (ticks - tp->ts_recent_age > TCP_PAWS_IDLE) { 2126df8bae1dSRodney W. Grimes /* 2127df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 2128df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 2129df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 2130df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 2131df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 2132df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 2133df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 2134df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 2135df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 2136df8bae1dSRodney W. Grimes */ 2137df8bae1dSRodney W. Grimes tp->ts_recent = 0; 2138df8bae1dSRodney W. Grimes } else { 213978b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 214078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 214178b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 214207fd333dSMatthew Dillon if (tlen) 2143df8bae1dSRodney W. Grimes goto dropafterack; 21441ab4789dSMatthew Dillon goto drop; 21451ab4789dSMatthew Dillon } 2146df8bae1dSRodney W. Grimes } 2147df8bae1dSRodney W. Grimes 2148a0292f23SGarrett Wollman /* 214980ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 215080ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 215180ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 215280ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 215380ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 215480ab7c0eSGarrett Wollman */ 2155a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 2156a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2157a57815efSBosko Milekic goto dropwithreset; 2158a57815efSBosko Milekic } 215980ab7c0eSGarrett Wollman 2160fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 2161df8bae1dSRodney W. Grimes if (todrop > 0) { 216281ad7eb0SZachary Loafman /* 216381ad7eb0SZachary Loafman * If this is a duplicate SYN for our current connection, 216481ad7eb0SZachary Loafman * advance over it and pretend and it's not a SYN. 216581ad7eb0SZachary Loafman */ 216681ad7eb0SZachary Loafman if (thflags & TH_SYN && th->th_seq == tp->irs) { 2167fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 2168fb59c426SYoshinobu Inoue th->th_seq++; 2169fb59c426SYoshinobu Inoue if (th->th_urp > 1) 2170fb59c426SYoshinobu Inoue th->th_urp--; 2171df8bae1dSRodney W. Grimes else 2172fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2173df8bae1dSRodney W. Grimes todrop--; 2174df8bae1dSRodney W. Grimes } 2175df8bae1dSRodney W. Grimes /* 2176dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 2177df8bae1dSRodney W. Grimes */ 2178fb59c426SYoshinobu Inoue if (todrop > tlen 2179fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 2180dac20301SGarrett Wollman /* 2181dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 2182dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 2183dac20301SGarrett Wollman * of sequence; drop it. 2184dac20301SGarrett Wollman */ 2185fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2186dac20301SGarrett Wollman 2187df8bae1dSRodney W. Grimes /* 2188dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 2189dac20301SGarrett Wollman * But keep on processing for RST or ACK. 2190df8bae1dSRodney W. Grimes */ 2191dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 2192fb59c426SYoshinobu Inoue todrop = tlen; 219378b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 219478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 2195df8bae1dSRodney W. Grimes } else { 219678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 219778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 2198df8bae1dSRodney W. Grimes } 2199fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 2200fb59c426SYoshinobu Inoue th->th_seq += todrop; 2201fb59c426SYoshinobu Inoue tlen -= todrop; 2202fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 2203fb59c426SYoshinobu Inoue th->th_urp -= todrop; 2204df8bae1dSRodney W. Grimes else { 2205fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 2206fb59c426SYoshinobu Inoue th->th_urp = 0; 2207df8bae1dSRodney W. Grimes } 2208df8bae1dSRodney W. Grimes } 2209df8bae1dSRodney W. Grimes 2210df8bae1dSRodney W. Grimes /* 2211df8bae1dSRodney W. Grimes * If new data are received on a connection after the 2212df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 2213df8bae1dSRodney W. Grimes */ 2214df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 2215fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 2216773673c1SAndre Oppermann char *s; 2217773673c1SAndre Oppermann 2218252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 2219252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 2220252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2221252ca428SRobert Watson 2222773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 2223e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 2224773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 2225e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 2226773673c1SAndre Oppermann free(s, M_TCPLOG); 2227773673c1SAndre Oppermann } 2228df8bae1dSRodney W. Grimes tp = tcp_close(tp); 222978b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 2230a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2231df8bae1dSRodney W. Grimes goto dropwithreset; 2232df8bae1dSRodney W. Grimes } 2233df8bae1dSRodney W. Grimes 2234df8bae1dSRodney W. Grimes /* 2235df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 2236df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 2237df8bae1dSRodney W. Grimes */ 2238fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 2239df8bae1dSRodney W. Grimes if (todrop > 0) { 224078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 2241fb59c426SYoshinobu Inoue if (todrop >= tlen) { 224278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 2243df8bae1dSRodney W. Grimes /* 2244df8bae1dSRodney W. Grimes * If window is closed can only take segments at 2245df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 2246df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 2247df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 2248df8bae1dSRodney W. Grimes * and ack. 2249df8bae1dSRodney W. Grimes */ 2250fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 2251df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 225278b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 2253df8bae1dSRodney W. Grimes } else 2254df8bae1dSRodney W. Grimes goto dropafterack; 2255df8bae1dSRodney W. Grimes } else 225678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 2257df8bae1dSRodney W. Grimes m_adj(m, -todrop); 2258fb59c426SYoshinobu Inoue tlen -= todrop; 2259fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 2260df8bae1dSRodney W. Grimes } 2261df8bae1dSRodney W. Grimes 2262df8bae1dSRodney W. Grimes /* 2263df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 2264df8bae1dSRodney W. Grimes * record its timestamp. 2265cf09195bSPaul Saab * NOTE: 2266cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 2267a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 2268cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 2269cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 2270cf09195bSPaul Saab * predicated on the sequence space of this segment. 2271cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 2272cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 2273cf09195bSPaul Saab * instead of RFC1323's 2274cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 2275cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 2276cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 2277cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 2278cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 2279df8bae1dSRodney W. Grimes */ 2280be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 2281cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 2282cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 2283cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 22849b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 2285a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 2286df8bae1dSRodney W. Grimes } 2287df8bae1dSRodney W. Grimes 2288df8bae1dSRodney W. Grimes /* 2289df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 2290df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 2291df8bae1dSRodney W. Grimes */ 2292fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 2293252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 2294252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 2295252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2296252ca428SRobert Watson 2297df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 2298a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 2299122aad88SAndre Oppermann goto drop; 2300df8bae1dSRodney W. Grimes } 2301df8bae1dSRodney W. Grimes 2302a0292f23SGarrett Wollman /* 2303a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 2304a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 2305a0292f23SGarrett Wollman * later processing; else drop segment and return. 2306a0292f23SGarrett Wollman */ 2307fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 2308a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 2309a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 2310a0292f23SGarrett Wollman goto step6; 23115e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 23125e1aa279SDavid Malone goto dropafterack; 2313a0292f23SGarrett Wollman else 2314a0292f23SGarrett Wollman goto drop; 2315a0292f23SGarrett Wollman } 2316df8bae1dSRodney W. Grimes 2317df8bae1dSRodney W. Grimes /* 2318df8bae1dSRodney W. Grimes * Ack processing. 2319df8bae1dSRodney W. Grimes */ 2320df8bae1dSRodney W. Grimes switch (tp->t_state) { 2321df8bae1dSRodney W. Grimes 2322df8bae1dSRodney W. Grimes /* 2323764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 2324764d8cefSBill Fenner * ESTABLISHED state and continue processing. 2325764d8cefSBill Fenner * The ACK was checked above. 2326df8bae1dSRodney W. Grimes */ 2327df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 2328a0292f23SGarrett Wollman 232978b50714SRobert Watson TCPSTAT_INC(tcps_connects); 2330df8bae1dSRodney W. Grimes soisconnected(so); 2331df8bae1dSRodney W. Grimes /* Do window scaling? */ 2332df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 2333df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 2334df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 2335464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 2336df8bae1dSRodney W. Grimes } 2337a0292f23SGarrett Wollman /* 2338a0292f23SGarrett Wollman * Make transitions: 2339a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 2340a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 2341a0292f23SGarrett Wollman */ 23429b8b58e0SJonathan Lemon tp->t_starttime = ticks; 2343a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 2344a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 2345a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 23467ff19458SPaul Traina } else { 2347a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 2348dbc42409SLawrence Stewart cc_conn_init(tp); 2349b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 23507ff19458SPaul Traina } 2351a0292f23SGarrett Wollman /* 2352a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2353a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2354a0292f23SGarrett Wollman */ 2355fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2356fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2357a0292f23SGarrett Wollman (struct mbuf *)0); 2358fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 235993b0017fSPhilippe Charnier /* FALLTHROUGH */ 2360df8bae1dSRodney W. Grimes 2361df8bae1dSRodney W. Grimes /* 2362df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2363df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2364fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2365fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2366df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2367df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2368df8bae1dSRodney W. Grimes */ 2369df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2370df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2371df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2372df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2373df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2374df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 23755a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 237678b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 23775a53ca16SPaul Saab goto dropafterack; 23785a53ca16SPaul Saab } 23793529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2380fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2381fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 23825a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 238339bc9de5SLawrence Stewart 238439bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */ 238539bc9de5SLawrence Stewart hhook_run_tcp_est_in(tp, th, &to); 238639bc9de5SLawrence Stewart 2387fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2388fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 238978b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2390df8bae1dSRodney W. Grimes /* 2391df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2392df8bae1dSRodney W. Grimes * a window probe), this is a completely 2393df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2394df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2395df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2396df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2397df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2398df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2399df8bae1dSRodney W. Grimes * window so we send only this one 2400df8bae1dSRodney W. Grimes * packet. 2401df8bae1dSRodney W. Grimes * 2402df8bae1dSRodney W. Grimes * We know we're losing at the current 2403df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2404df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2405df8bae1dSRodney W. Grimes * and pull our congestion window back to 2406df8bae1dSRodney W. Grimes * the new ssthresh). 2407df8bae1dSRodney W. Grimes * 2408df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2409df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2410df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2411df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2412df8bae1dSRodney W. Grimes * network. 2413f2512ba1SRui Paulo * 2414f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2415f2512ba1SRui Paulo * we reduced the cwnd. 2416df8bae1dSRodney W. Grimes */ 2417b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2418fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2419df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2420cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2421dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2422dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 24233529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2424dbc42409SLawrence Stewart IN_FASTRECOVERY(tp->t_flags)) { 2425808f11b7SPaul Saab int awnd; 242625e6f9edSPaul Saab 242725e6f9edSPaul Saab /* 242825e6f9edSPaul Saab * Compute the amount of data in flight first. 242925e6f9edSPaul Saab * We can inject new data into the pipe iff 243025e6f9edSPaul Saab * we have less than 1/2 the original window's 243125e6f9edSPaul Saab * worth of data in flight. 243225e6f9edSPaul Saab */ 2433808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 24340077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2435808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 243625e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 243725e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 243825e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 243925e6f9edSPaul Saab } 244025e6f9edSPaul Saab } else 244146f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 244246f58482SJonathan Lemon (void) tcp_output(tp); 244346f58482SJonathan Lemon goto drop; 2444cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2445cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2446a0445c2eSJayanth Vijayaraghavan 2447a0445c2eSJayanth Vijayaraghavan /* 2448a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2449a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2450a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2451a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2452a0445c2eSJayanth Vijayaraghavan * recovery. 2453a0445c2eSJayanth Vijayaraghavan */ 24543529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2455dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2456a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2457a0445c2eSJayanth Vijayaraghavan break; 2458a0445c2eSJayanth Vijayaraghavan } 2459dbc42409SLawrence Stewart } else { 2460a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 24619d11646dSJeffrey Hsu tp->snd_recover)) { 2462cb942153SJeffrey Hsu tp->t_dupacks = 0; 2463cb942153SJeffrey Hsu break; 246446f58482SJonathan Lemon } 2465a0445c2eSJayanth Vijayaraghavan } 2466dbc42409SLawrence Stewart /* Congestion signal before ack. */ 2467dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_NDUPACK); 2468dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2469b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 24709b8b58e0SJonathan Lemon tp->t_rtttime = 0; 24713529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 247278b50714SRobert Watson TCPSTAT_INC( 247378b50714SRobert Watson tcps_sack_recovery_episode); 2474a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 24758db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 24766d90faf3SPaul Saab (void) tcp_output(tp); 24776d90faf3SPaul Saab goto drop; 24786d90faf3SPaul Saab } 2479fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2480df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2481df8bae1dSRodney W. Grimes (void) tcp_output(tp); 248248d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2483302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2484302ce8d6SAndre Oppermann __func__)); 2485df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 248648d2549cSJeffrey Hsu tp->t_maxseg * 248748d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2488df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2489df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2490df8bae1dSRodney W. Grimes goto drop; 2491603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2492dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_DUPACK); 2493582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 249448d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 249548d2549cSJeffrey Hsu u_int sent; 249689c02376SJeffrey Hsu 2497582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2498582a954bSJeffrey Hsu tp->t_dupacks == 2, 2499302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2500302ce8d6SAndre Oppermann __func__)); 250161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 250248d2549cSJeffrey Hsu tp->snd_limited = 0; 250361a36e3dSJeffrey Hsu tp->snd_cwnd = 250461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 250561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 250661a36e3dSJeffrey Hsu tp->t_maxseg; 2507582a954bSJeffrey Hsu (void) tcp_output(tp); 250848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 250948d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 251089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 251189c02376SJeffrey Hsu tp->snd_limited == 0) || 251289c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 251389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2514302ce8d6SAndre Oppermann ("%s: sent too much", 2515302ce8d6SAndre Oppermann __func__)); 251648d2549cSJeffrey Hsu tp->snd_limited = 2; 251748d2549cSJeffrey Hsu } else if (sent > 0) 251848d2549cSJeffrey Hsu ++tp->snd_limited; 2519582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2520582a954bSJeffrey Hsu goto drop; 2521df8bae1dSRodney W. Grimes } 2522df8bae1dSRodney W. Grimes } else 2523df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2524df8bae1dSRodney W. Grimes break; 2525df8bae1dSRodney W. Grimes } 2526cb942153SJeffrey Hsu 2527302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2528302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2529cb942153SJeffrey Hsu 2530df8bae1dSRodney W. Grimes /* 2531df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2532df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2533df8bae1dSRodney W. Grimes */ 2534dbc42409SLawrence Stewart if (IN_FASTRECOVERY(tp->t_flags)) { 2535cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 25363529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 25376d90faf3SPaul Saab tcp_sack_partialack(tp, th); 25386d90faf3SPaul Saab else 2539c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2540dbc42409SLawrence Stewart } else 2541dbc42409SLawrence Stewart cc_post_recovery(tp, th); 254246f58482SJonathan Lemon } 2543cb942153SJeffrey Hsu tp->t_dupacks = 0; 2544a0292f23SGarrett Wollman /* 2545a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2546a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2547a0292f23SGarrett Wollman */ 2548a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2549a0292f23SGarrett Wollman /* 2550a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2551a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 255207e43e10SAndras Olah * synchronized). Go to non-starred state, 255307e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 255407e43e10SAndras Olah * we can do window scaling. 2555a0292f23SGarrett Wollman */ 2556a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2557a0292f23SGarrett Wollman tp->snd_una++; 255807e43e10SAndras Olah /* Do window scaling? */ 255907e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 256007e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 256107e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2562464fcfbcSAndre Oppermann /* Send window already scaled. */ 256307e43e10SAndras Olah } 2564a0292f23SGarrett Wollman } 2565a0292f23SGarrett Wollman 2566a0292f23SGarrett Wollman process_ACK: 25678501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25687cfc6904SRobert Watson 2569dbc42409SLawrence Stewart acked = BYTES_THIS_ACK(tp, th); 257078b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 257178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2572df8bae1dSRodney W. Grimes 2573df8bae1dSRodney W. Grimes /* 25749b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 25759b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 25769b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 25779b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 25789b8b58e0SJonathan Lemon * we left off. 25799b8b58e0SJonathan Lemon */ 2580672dc4aeSJohn Baldwin if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID && 2581672dc4aeSJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) 2582dbc42409SLawrence Stewart cc_cong_signal(tp, th, CC_RTO_ERR); 25839b8b58e0SJonathan Lemon 25849b8b58e0SJonathan Lemon /* 2585df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2586df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2587df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2588df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2589df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2590df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2591df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2592fa55172bSMatthew Dillon * 2593fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2594fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2595fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2596fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2597df8bae1dSRodney W. Grimes */ 2598fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2599fa55172bSMatthew Dillon to.to_tsecr) { 2600eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2601eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 26029b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2603fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2604eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2605eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 26069b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2607fa55172bSMatthew Dillon } 2608df8bae1dSRodney W. Grimes 2609df8bae1dSRodney W. Grimes /* 2610df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2611df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2612df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2613df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2614df8bae1dSRodney W. Grimes */ 2615fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2616b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2617df8bae1dSRodney W. Grimes needoutput = 1; 2618b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2619b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2620a0292f23SGarrett Wollman 2621a0292f23SGarrett Wollman /* 2622a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2623a0292f23SGarrett Wollman * skip rest of ACK processing. 2624a0292f23SGarrett Wollman */ 2625a0292f23SGarrett Wollman if (acked == 0) 2626a0292f23SGarrett Wollman goto step6; 2627a0292f23SGarrett Wollman 2628df8bae1dSRodney W. Grimes /* 2629dbc42409SLawrence Stewart * Let the congestion control algorithm update congestion 2630dbc42409SLawrence Stewart * control related information. This typically means increasing 2631dbc42409SLawrence Stewart * the congestion window. 2632df8bae1dSRodney W. Grimes */ 2633dbc42409SLawrence Stewart cc_ack_received(tp, th, CC_ACK); 2634dbc42409SLawrence Stewart 26355905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2636df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2637df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 26385905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2639df8bae1dSRodney W. Grimes ourfinisacked = 1; 2640df8bae1dSRodney W. Grimes } else { 26415905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2642df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2643df8bae1dSRodney W. Grimes ourfinisacked = 0; 2644df8bae1dSRodney W. Grimes } 2645564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 26461e4d7da7SRobert Watson sowwakeup_locked(so); 2647e8949f74SAndre Oppermann /* Detect una wraparound. */ 2648dbc42409SLawrence Stewart if (!IN_RECOVERY(tp->t_flags) && 26499d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 26509d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 26519d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2652dbc42409SLawrence Stewart /* XXXLAS: Can this be moved up into cc_post_recovery? */ 2653dbc42409SLawrence Stewart if (IN_RECOVERY(tp->t_flags) && 265424cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 2655dbc42409SLawrence Stewart EXIT_RECOVERY(tp->t_flags); 265624cb0f22SLawrence Stewart } 2657fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 26583529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 26596d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 26606d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 26616d90faf3SPaul Saab } 2662df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2663df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2664df8bae1dSRodney W. Grimes 2665df8bae1dSRodney W. Grimes switch (tp->t_state) { 2666df8bae1dSRodney W. Grimes 2667df8bae1dSRodney W. Grimes /* 2668df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2669df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2670df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2671df8bae1dSRodney W. Grimes */ 2672df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2673df8bae1dSRodney W. Grimes if (ourfinisacked) { 2674df8bae1dSRodney W. Grimes /* 2675df8bae1dSRodney W. Grimes * If we can't receive any more 2676df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2677df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2678df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2679df8bae1dSRodney W. Grimes * we'll hang forever. 2680e8949f74SAndre Oppermann * 2681e8949f74SAndre Oppermann * XXXjl: 2682340c35deSJonathan Lemon * we should release the tp also, and use a 2683340c35deSJonathan Lemon * compressed state. 2684340c35deSJonathan Lemon */ 2685c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 26867c72af87SMohan Srinivasan int timeout; 26877c72af87SMohan Srinivasan 268803e49181SSeigo Tanimura soisdisconnected(so); 26897c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 26907c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2691b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 26924cc20ab1SSeigo Tanimura } 2693df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2694df8bae1dSRodney W. Grimes } 2695df8bae1dSRodney W. Grimes break; 2696df8bae1dSRodney W. Grimes 2697df8bae1dSRodney W. Grimes /* 2698df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2699df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2700df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2701df8bae1dSRodney W. Grimes * the segment. 2702df8bae1dSRodney W. Grimes */ 2703df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2704df8bae1dSRodney W. Grimes if (ourfinisacked) { 2705252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 270611a20fb8SJeffrey Hsu tcp_twstart(tp); 2707603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2708340c35deSJonathan Lemon m_freem(m); 2709679d9708SAndre Oppermann return; 2710df8bae1dSRodney W. Grimes } 2711df8bae1dSRodney W. Grimes break; 2712df8bae1dSRodney W. Grimes 2713df8bae1dSRodney W. Grimes /* 2714df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2715df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2716df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2717df8bae1dSRodney W. Grimes * enter the closed state and return. 2718df8bae1dSRodney W. Grimes */ 2719df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2720df8bae1dSRodney W. Grimes if (ourfinisacked) { 2721252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2722df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2723df8bae1dSRodney W. Grimes goto drop; 2724df8bae1dSRodney W. Grimes } 2725df8bae1dSRodney W. Grimes break; 2726df8bae1dSRodney W. Grimes } 2727df8bae1dSRodney W. Grimes } 2728df8bae1dSRodney W. Grimes 2729df8bae1dSRodney W. Grimes step6: 27308501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27317cfc6904SRobert Watson 2732df8bae1dSRodney W. Grimes /* 2733df8bae1dSRodney W. Grimes * Update window information. 2734df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2735df8bae1dSRodney W. Grimes */ 2736fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2737fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2738fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2739fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2740df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2741fb59c426SYoshinobu Inoue if (tlen == 0 && 2742fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 274378b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2744df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2745fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2746fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2747df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2748df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2749df8bae1dSRodney W. Grimes needoutput = 1; 2750df8bae1dSRodney W. Grimes } 2751df8bae1dSRodney W. Grimes 2752df8bae1dSRodney W. Grimes /* 2753df8bae1dSRodney W. Grimes * Process segments with URG. 2754df8bae1dSRodney W. Grimes */ 2755fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2756df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2757df8bae1dSRodney W. Grimes /* 2758df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2759df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2760df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2761df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2762df8bae1dSRodney W. Grimes */ 276318ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2764fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2765fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2766fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 276718ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2768df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2769df8bae1dSRodney W. Grimes } 2770df8bae1dSRodney W. Grimes /* 2771df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2772df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2773df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2774df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2775df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2776df8bae1dSRodney W. Grimes * 2777df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2778df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2779df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2780df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2781df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2782df8bae1dSRodney W. Grimes * spec states (in one of two places). 2783df8bae1dSRodney W. Grimes */ 2784fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2785fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2786df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2787df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2788927c5ceaSRobert Watson if (so->so_oobmark == 0) 2789c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2790df8bae1dSRodney W. Grimes sohasoutofband(so); 2791df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2792df8bae1dSRodney W. Grimes } 279318ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2794df8bae1dSRodney W. Grimes /* 2795df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2796df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2797df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2798df8bae1dSRodney W. Grimes * data may creep in... ick. 2799df8bae1dSRodney W. Grimes */ 280030613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 280130613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 280230613f56SJeffrey Hsu /* hdr drop is delayed */ 280330613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 280430613f56SJeffrey Hsu } 2805c068736aSJeffrey Hsu } else { 2806df8bae1dSRodney W. Grimes /* 2807df8bae1dSRodney W. Grimes * If no out of band data is expected, 2808df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2809df8bae1dSRodney W. Grimes * with the receive window. 2810df8bae1dSRodney W. Grimes */ 2811df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2812df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2813c068736aSJeffrey Hsu } 2814df8bae1dSRodney W. Grimes dodata: /* XXX */ 28158501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28167cfc6904SRobert Watson 2817df8bae1dSRodney W. Grimes /* 2818df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2819df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2820df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2821df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2822df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2823df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2824df8bae1dSRodney W. Grimes */ 2825fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2826df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 282769e03620SPaul Saab tcp_seq save_start = th->th_seq; 2828fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2829e4b64281SJesper Skriver /* 2830c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2831c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2832c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2833c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2834c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2835c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2836c068736aSJeffrey Hsu * conversions. 2837c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2838c068736aSJeffrey Hsu * immediately when segments are out of order (so 2839c068736aSJeffrey Hsu * fast retransmit can work). 2840e4b64281SJesper Skriver */ 2841e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2842e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2843e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2844e4b64281SJesper Skriver if (DELAY_ACK(tp)) 28453bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2846e4b64281SJesper Skriver else 2847e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2848e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2849e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 285078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 285178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 2852e4b64281SJesper Skriver ND6_HINT(tp); 28531e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2854c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2855c1c36a2cSMike Silbersack m_freem(m); 2856c1c36a2cSMike Silbersack else 28571e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2858e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 28591e4d7da7SRobert Watson sorwakeup_locked(so); 2860e4b64281SJesper Skriver } else { 2861e8949f74SAndre Oppermann /* 2862e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2863e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2864e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2865e8949f74SAndre Oppermann * when trimming from the head. 2866e8949f74SAndre Oppermann */ 2867e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2868e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2869e4b64281SJesper Skriver } 28703529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2871f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2872302ce8d6SAndre Oppermann #if 0 2873df8bae1dSRodney W. Grimes /* 2874df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2875df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2876df8bae1dSRodney W. Grimes * buffer size. 2877302ce8d6SAndre Oppermann * XXX: Unused. 2878df8bae1dSRodney W. Grimes */ 2879f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) 2880df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2881f701e30dSJohn Baldwin else 2882f701e30dSJohn Baldwin len = so->so_rcv.sb_hiwat; 2883302ce8d6SAndre Oppermann #endif 2884df8bae1dSRodney W. Grimes } else { 2885df8bae1dSRodney W. Grimes m_freem(m); 2886fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2887df8bae1dSRodney W. Grimes } 2888df8bae1dSRodney W. Grimes 2889df8bae1dSRodney W. Grimes /* 2890df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2891df8bae1dSRodney W. Grimes * that the connection is closing. 2892df8bae1dSRodney W. Grimes */ 2893fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2894df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2895df8bae1dSRodney W. Grimes socantrcvmore(so); 2896a0292f23SGarrett Wollman /* 2897a0292f23SGarrett Wollman * If connection is half-synchronized 2898d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2899a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2900a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2901a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2902a0292f23SGarrett Wollman */ 29033bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 29043bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2905a0292f23SGarrett Wollman else 2906df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2907df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2908df8bae1dSRodney W. Grimes } 2909df8bae1dSRodney W. Grimes switch (tp->t_state) { 2910df8bae1dSRodney W. Grimes 2911df8bae1dSRodney W. Grimes /* 2912df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2913df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2914df8bae1dSRodney W. Grimes */ 2915df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 29169b8b58e0SJonathan Lemon tp->t_starttime = ticks; 29179b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2918df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2919df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2920df8bae1dSRodney W. Grimes break; 2921df8bae1dSRodney W. Grimes 2922df8bae1dSRodney W. Grimes /* 2923df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2924df8bae1dSRodney W. Grimes * enter the CLOSING state. 2925df8bae1dSRodney W. Grimes */ 2926df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2927df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2928df8bae1dSRodney W. Grimes break; 2929df8bae1dSRodney W. Grimes 2930df8bae1dSRodney W. Grimes /* 2931df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2932df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2933df8bae1dSRodney W. Grimes * standard timers. 2934df8bae1dSRodney W. Grimes */ 2935df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2936252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2937252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2938252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2939252ca428SRobert Watson ti_locked)); 2940252ca428SRobert Watson 2941340c35deSJonathan Lemon tcp_twstart(tp); 2942603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2943679d9708SAndre Oppermann return; 2944df8bae1dSRodney W. Grimes } 2945df8bae1dSRodney W. Grimes } 2946fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 2947603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2948252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2949252ca428SRobert Watson 2950610ee2f9SDavid Greenman #ifdef TCPDEBUG 29514cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2952fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2953fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2954610ee2f9SDavid Greenman #endif 2955df8bae1dSRodney W. Grimes 2956df8bae1dSRodney W. Grimes /* 2957df8bae1dSRodney W. Grimes * Return any desired output. 2958df8bae1dSRodney W. Grimes */ 2959df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2960df8bae1dSRodney W. Grimes (void) tcp_output(tp); 29617792ea27SJeffrey Hsu 2962a14c749fSJonathan Lemon check_delack: 2963252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2964252ca428SRobert Watson __func__, ti_locked)); 2965603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 29668501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2967252ca428SRobert Watson 2968a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 29693bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2970b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 29713bfd6421SJonathan Lemon } 29728501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2973679d9708SAndre Oppermann return; 2974df8bae1dSRodney W. Grimes 2975df8bae1dSRodney W. Grimes dropafterack: 2976df8bae1dSRodney W. Grimes /* 2977df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2978df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 297980ab7c0eSGarrett Wollman * 298080ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 298180ab7c0eSGarrett Wollman * paths to this code happen after packets containing 298280ab7c0eSGarrett Wollman * RST have been dropped. 298380ab7c0eSGarrett Wollman * 298480ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 298580ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 298680ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 298780ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 298880ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 298980ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2990df8bae1dSRodney W. Grimes */ 2991fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2992fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2993a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2994a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2995a57815efSBosko Milekic goto dropwithreset; 2996a57815efSBosko Milekic } 2997a0292f23SGarrett Wollman #ifdef TCPDEBUG 29984cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2999fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3000fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3001a0292f23SGarrett Wollman #endif 3002fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3003603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 3004252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3005252ca428SRobert Watson 3006df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 3007df8bae1dSRodney W. Grimes (void) tcp_output(tp); 30088501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3009d6915262SRobert Watson m_freem(m); 3010679d9708SAndre Oppermann return; 3011df8bae1dSRodney W. Grimes 3012df8bae1dSRodney W. Grimes dropwithreset: 3013fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) 3014dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 3015252ca428SRobert Watson ti_locked = TI_UNLOCKED; 3016a57815efSBosko Milekic 3017a0ca0871SRobert Watson if (tp != NULL) { 3018302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 30198501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3020dd8ac7f9SRobert Watson } else 3021a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 3022679d9708SAndre Oppermann return; 3023df8bae1dSRodney W. Grimes 3024df8bae1dSRodney W. Grimes drop: 3025fa046d87SRobert Watson if (ti_locked == TI_WLOCKED) { 3026252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 3027fa046d87SRobert Watson ti_locked = TI_UNLOCKED; 3028fa046d87SRobert Watson } 3029252ca428SRobert Watson #ifdef INVARIANTS 3030252ca428SRobert Watson else 3031252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 3032252ca428SRobert Watson #endif 3033252ca428SRobert Watson 3034df8bae1dSRodney W. Grimes /* 3035df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 3036df8bae1dSRodney W. Grimes */ 3037610ee2f9SDavid Greenman #ifdef TCPDEBUG 30381c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 3039fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 3040fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 3041a0292f23SGarrett Wollman #endif 30421c53f806SRobert Watson if (tp != NULL) 30438501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 3044d6915262SRobert Watson m_freem(m); 3045302ce8d6SAndre Oppermann } 3046302ce8d6SAndre Oppermann 3047302ce8d6SAndre Oppermann /* 30480ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 30490ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 30500ca3f933SAndre Oppermann * tp may be NULL. 3051302ce8d6SAndre Oppermann */ 3052302ce8d6SAndre Oppermann static void 3053302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 3054302ce8d6SAndre Oppermann int tlen, int rstreason) 3055302ce8d6SAndre Oppermann { 3056b287c6c7SBjoern A. Zeeb #ifdef INET 3057302ce8d6SAndre Oppermann struct ip *ip; 3058b287c6c7SBjoern A. Zeeb #endif 3059302ce8d6SAndre Oppermann #ifdef INET6 3060302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 3061302ce8d6SAndre Oppermann #endif 30627a3244ccSRobert Watson 30637a3244ccSRobert Watson if (tp != NULL) { 30648501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 30657a3244ccSRobert Watson } 30667a3244ccSRobert Watson 30670ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 3068302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 3069302ce8d6SAndre Oppermann goto drop; 3070302ce8d6SAndre Oppermann #ifdef INET6 3071302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 3072302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 3073302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 3074302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 3075302ce8d6SAndre Oppermann goto drop; 3076302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 3077b287c6c7SBjoern A. Zeeb } 3078302ce8d6SAndre Oppermann #endif 3079b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3080b287c6c7SBjoern A. Zeeb else 3081b287c6c7SBjoern A. Zeeb #endif 3082b287c6c7SBjoern A. Zeeb #ifdef INET 3083302ce8d6SAndre Oppermann { 3084302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 3085302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 3086302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 3087302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 3088302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 3089302ce8d6SAndre Oppermann goto drop; 3090302ce8d6SAndre Oppermann } 3091b287c6c7SBjoern A. Zeeb #endif 3092302ce8d6SAndre Oppermann 3093302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 3094302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 3095302ce8d6SAndre Oppermann goto drop; 3096302ce8d6SAndre Oppermann 3097302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 3098302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 3099302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 3100302ce8d6SAndre Oppermann th->th_ack, TH_RST); 3101302ce8d6SAndre Oppermann } else { 3102302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 3103302ce8d6SAndre Oppermann tlen++; 3104302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 3105302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 3106302ce8d6SAndre Oppermann } 3107302ce8d6SAndre Oppermann return; 3108302ce8d6SAndre Oppermann drop: 3109302ce8d6SAndre Oppermann m_freem(m); 3110df8bae1dSRodney W. Grimes } 3111df8bae1dSRodney W. Grimes 3112be2ac88cSJonathan Lemon /* 3113be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 3114be2ac88cSJonathan Lemon */ 31150312fbe9SPoul-Henning Kamp static void 3116ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 3117df8bae1dSRodney W. Grimes { 3118df8bae1dSRodney W. Grimes int opt, optlen; 3119df8bae1dSRodney W. Grimes 3120be2ac88cSJonathan Lemon to->to_flags = 0; 3121df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 3122df8bae1dSRodney W. Grimes opt = cp[0]; 3123df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 3124df8bae1dSRodney W. Grimes break; 3125df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 3126df8bae1dSRodney W. Grimes optlen = 1; 3127df8bae1dSRodney W. Grimes else { 3128b474779fSJun-ichiro itojun Hagino if (cnt < 2) 3129b474779fSJun-ichiro itojun Hagino break; 3130df8bae1dSRodney W. Grimes optlen = cp[1]; 3131b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 3132df8bae1dSRodney W. Grimes break; 3133df8bae1dSRodney W. Grimes } 3134df8bae1dSRodney W. Grimes switch (opt) { 3135df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 3136df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 3137df8bae1dSRodney W. Grimes continue; 3138f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3139df8bae1dSRodney W. Grimes continue; 3140be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 3141be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 3142be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 3143fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 3144df8bae1dSRodney W. Grimes break; 3145df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 3146df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 3147df8bae1dSRodney W. Grimes continue; 3148f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3149df8bae1dSRodney W. Grimes continue; 3150be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 315102a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 3152df8bae1dSRodney W. Grimes break; 3153df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 3154df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 3155df8bae1dSRodney W. Grimes continue; 3156be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 3157a0292f23SGarrett Wollman bcopy((char *)cp + 2, 3158a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 3159fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 3160a0292f23SGarrett Wollman bcopy((char *)cp + 6, 3161a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 3162fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 3163df8bae1dSRodney W. Grimes break; 31641cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 31651cfd4b53SBruce M Simpson /* 31661cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 31671cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 31681cfd4b53SBruce M Simpson * record the fact that the option was observed here 31691cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 31701cfd4b53SBruce M Simpson */ 31711cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 31721cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 31731cfd4b53SBruce M Simpson continue; 3174df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 3175df47e437SAndre Oppermann to->to_signature = cp + 2; 31761cfd4b53SBruce M Simpson break; 3177265ed012SBruce M Simpson #endif 31786d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 3179f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 31806d90faf3SPaul Saab continue; 3181f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 3182f72167f4SAndre Oppermann continue; 3183603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 3184f72167f4SAndre Oppermann continue; 3185fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 31866d90faf3SPaul Saab break; 31876d90faf3SPaul Saab case TCPOPT_SACK: 31885a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 31896d90faf3SPaul Saab continue; 3190b728e902SAndre Oppermann if (flags & TO_SYN) 3191b728e902SAndre Oppermann continue; 3192fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 31935a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 31945a53ca16SPaul Saab to->to_sacks = cp + 2; 319578b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 31966d90faf3SPaul Saab break; 3197be2ac88cSJonathan Lemon default: 3198be2ac88cSJonathan Lemon continue; 3199df8bae1dSRodney W. Grimes } 3200df8bae1dSRodney W. Grimes } 3201df8bae1dSRodney W. Grimes } 3202df8bae1dSRodney W. Grimes 3203df8bae1dSRodney W. Grimes /* 3204df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 3205df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 3206df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 3207df8bae1dSRodney W. Grimes * sequencing purposes. 3208df8bae1dSRodney W. Grimes */ 32090312fbe9SPoul-Henning Kamp static void 3210ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 3211ad3f9ab3SAndre Oppermann int off) 3212df8bae1dSRodney W. Grimes { 3213fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 3214df8bae1dSRodney W. Grimes 3215df8bae1dSRodney W. Grimes while (cnt >= 0) { 3216df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 3217df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 3218df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 3219df8bae1dSRodney W. Grimes 32208501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32217a3244ccSRobert Watson 3222df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 3223df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 3224df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 3225df8bae1dSRodney W. Grimes m->m_len--; 322669a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 322769a34685SYoshinobu Inoue m->m_pkthdr.len--; 3228df8bae1dSRodney W. Grimes return; 3229df8bae1dSRodney W. Grimes } 3230df8bae1dSRodney W. Grimes cnt -= m->m_len; 3231df8bae1dSRodney W. Grimes m = m->m_next; 32324dfdffe9SAndre Oppermann if (m == NULL) 3233df8bae1dSRodney W. Grimes break; 3234df8bae1dSRodney W. Grimes } 3235df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 3236df8bae1dSRodney W. Grimes } 3237df8bae1dSRodney W. Grimes 3238df8bae1dSRodney W. Grimes /* 3239df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 3240df8bae1dSRodney W. Grimes * and update averages and current timeout. 3241df8bae1dSRodney W. Grimes */ 32420312fbe9SPoul-Henning Kamp static void 3243ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 3244df8bae1dSRodney W. Grimes { 3245ad3f9ab3SAndre Oppermann int delta; 3246233e8c18SGarrett Wollman 32478501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 32482be3bf22SRobert Watson 324978b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 3250233e8c18SGarrett Wollman tp->t_rttupdated++; 3251233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 3252233e8c18SGarrett Wollman /* 3253233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 3254233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3255233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3256233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3257233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3258233e8c18SGarrett Wollman */ 3259233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3260233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3261233e8c18SGarrett Wollman 3262233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3263233e8c18SGarrett Wollman tp->t_srtt = 1; 3264233e8c18SGarrett Wollman 3265233e8c18SGarrett Wollman /* 3266233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3267233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3268233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3269233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3270233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3271233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3272233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3273233e8c18SGarrett Wollman * rfc793's wired-in beta. 3274233e8c18SGarrett Wollman */ 3275233e8c18SGarrett Wollman if (delta < 0) 3276233e8c18SGarrett Wollman delta = -delta; 3277233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3278233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3279233e8c18SGarrett Wollman tp->t_rttvar = 1; 32801fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 32811fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3282233e8c18SGarrett Wollman } else { 3283233e8c18SGarrett Wollman /* 3284233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3285233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3286233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3287233e8c18SGarrett Wollman */ 3288233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3289233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 32901fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3291233e8c18SGarrett Wollman } 32929b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3293df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3294df8bae1dSRodney W. Grimes 3295df8bae1dSRodney W. Grimes /* 3296df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3297df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3298df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3299df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3300df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3301df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3302df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3303df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3304df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3305df8bae1dSRodney W. Grimes */ 3306233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 33079e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3308df8bae1dSRodney W. Grimes 3309df8bae1dSRodney W. Grimes /* 3310df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3311df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3312df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3313df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3314df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3315df8bae1dSRodney W. Grimes */ 3316df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3317df8bae1dSRodney W. Grimes } 3318df8bae1dSRodney W. Grimes 3319df8bae1dSRodney W. Grimes /* 3320df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3321df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 3322df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 3323df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 3324df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3325df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 3326df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3327df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3328df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3329df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3330df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3331df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3332df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3333a0292f23SGarrett Wollman * 3334a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3335a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3336a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3337a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3338a0292f23SGarrett Wollman * data in maxopd. 3339a0292f23SGarrett Wollman * 3340a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 3341a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 3342a0292f23SGarrett Wollman * MSS of our peer. 334397d8d152SAndre Oppermann * 334497d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 334597d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3346df8bae1dSRodney W. Grimes */ 3347a0292f23SGarrett Wollman void 334891d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, 334991d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3350df8bae1dSRodney W. Grimes { 3351b287c6c7SBjoern A. Zeeb int mss = 0; 3352b287c6c7SBjoern A. Zeeb u_long maxmtu = 0; 3353c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 335497d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3355a0292f23SGarrett Wollman int origoffer = offer; 3356fb59c426SYoshinobu Inoue #ifdef INET6 3357c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3358c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3359c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3360c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3361c068736aSJeffrey Hsu #else 3362c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3363fb59c426SYoshinobu Inoue #endif 3364df8bae1dSRodney W. Grimes 33658501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 33667a3244ccSRobert Watson 3367e8949f74SAndre Oppermann /* Initialize. */ 336897d8d152SAndre Oppermann #ifdef INET6 336997d8d152SAndre Oppermann if (isipv6) { 337091d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3371603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 3372b287c6c7SBjoern A. Zeeb } 337397d8d152SAndre Oppermann #endif 3374b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3375b287c6c7SBjoern A. Zeeb else 3376b287c6c7SBjoern A. Zeeb #endif 3377b287c6c7SBjoern A. Zeeb #ifdef INET 337897d8d152SAndre Oppermann { 337991d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3380603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3381df8bae1dSRodney W. Grimes } 3382b287c6c7SBjoern A. Zeeb #endif 3383df8bae1dSRodney W. Grimes 338497d8d152SAndre Oppermann /* 3385e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 338697d8d152SAndre Oppermann */ 33876f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 33886f01cac6SBjoern A. Zeeb /* 33898e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 33906f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 33916f01cac6SBjoern A. Zeeb * if there was no cache hit. 33926f01cac6SBjoern A. Zeeb */ 33936f01cac6SBjoern A. Zeeb if (metricptr != NULL) 33946f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 339597d8d152SAndre Oppermann return; 33966f01cac6SBjoern A. Zeeb } 339797d8d152SAndre Oppermann 3398e8949f74SAndre Oppermann /* What have we got? */ 339997d8d152SAndre Oppermann switch (offer) { 340097d8d152SAndre Oppermann case 0: 340197d8d152SAndre Oppermann /* 340297d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3403c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3404c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 340597d8d152SAndre Oppermann */ 3406c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 340797d8d152SAndre Oppermann break; 340897d8d152SAndre Oppermann 340997d8d152SAndre Oppermann case -1: 3410a0292f23SGarrett Wollman /* 3411c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3412a0292f23SGarrett Wollman */ 341397d8d152SAndre Oppermann /* FALLTHROUGH */ 341497d8d152SAndre Oppermann 341597d8d152SAndre Oppermann default: 3416a0292f23SGarrett Wollman /* 341753369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 341853369ac9SAndre Oppermann * to at least minmss. 341953369ac9SAndre Oppermann */ 3420603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 342197d8d152SAndre Oppermann } 3422a0292f23SGarrett Wollman 3423df8bae1dSRodney W. Grimes /* 3424e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3425df8bae1dSRodney W. Grimes */ 342697d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 34273cee92e0SBjoern A. Zeeb if (metricptr != NULL) 34283cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 342997d8d152SAndre Oppermann 3430df8bae1dSRodney W. Grimes /* 3431e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3432fb59c426SYoshinobu Inoue * else, use the link mtu. 3433df8bae1dSRodney W. Grimes */ 343497d8d152SAndre Oppermann if (metrics.rmx_mtu) 34352d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3436c068736aSJeffrey Hsu else { 343731b3783cSHajimu UMEMOTO #ifdef INET6 3438fb59c426SYoshinobu Inoue if (isipv6) { 343997d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3440603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 344197d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3442603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3443b287c6c7SBjoern A. Zeeb } 3444b3399803SHajimu UMEMOTO #endif 3445b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3446b287c6c7SBjoern A. Zeeb else 3447b287c6c7SBjoern A. Zeeb #endif 3448b287c6c7SBjoern A. Zeeb #ifdef INET 344997d8d152SAndre Oppermann { 345097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3451603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 345297d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3453603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3454a0292f23SGarrett Wollman } 3455b287c6c7SBjoern A. Zeeb #endif 34563cee92e0SBjoern A. Zeeb /* 34573cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 34583cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 34593cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 34603cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 34613cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 34623cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 34633cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 34643cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 34653cee92e0SBjoern A. Zeeb * this under the carpet. 34663cee92e0SBjoern A. Zeeb * 34673cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 34683cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 34693cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 34703cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 34713cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 34723cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 34733cee92e0SBjoern A. Zeeb */ 347497d8d152SAndre Oppermann } 3475a0292f23SGarrett Wollman mss = min(mss, offer); 347697d8d152SAndre Oppermann 3477a0292f23SGarrett Wollman /* 34783cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 34793cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 34803cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 34813cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 34823cee92e0SBjoern A. Zeeb */ 34833cee92e0SBjoern A. Zeeb mss = max(mss, 64); 34843cee92e0SBjoern A. Zeeb 34853cee92e0SBjoern A. Zeeb /* 3486a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3487a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3488a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3489a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3490a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3491a0292f23SGarrett Wollman */ 3492a0292f23SGarrett Wollman tp->t_maxopd = mss; 3493a0292f23SGarrett Wollman 3494a0292f23SGarrett Wollman /* 3495e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3496c94c54e4SAndre Oppermann * In this case we just guess. 3497a0292f23SGarrett Wollman */ 3498a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3499a0292f23SGarrett Wollman (origoffer == -1 || 3500a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3501a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3502a0292f23SGarrett Wollman 3503df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3504df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3505df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3506df8bae1dSRodney W. Grimes #else 3507df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3508df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3509df8bae1dSRodney W. Grimes #endif 351097d8d152SAndre Oppermann tp->t_maxseg = mss; 35113cee92e0SBjoern A. Zeeb } 35123cee92e0SBjoern A. Zeeb 35133cee92e0SBjoern A. Zeeb void 35143cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 35153cee92e0SBjoern A. Zeeb { 3516dbc42409SLawrence Stewart int mss; 35173cee92e0SBjoern A. Zeeb u_long bufsize; 35183cee92e0SBjoern A. Zeeb struct inpcb *inp; 35193cee92e0SBjoern A. Zeeb struct socket *so; 35203cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 352191d6cfa6SBjoern A. Zeeb int mtuflags = 0; 3522dbc42409SLawrence Stewart 35233cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 35243cee92e0SBjoern A. Zeeb 352591d6cfa6SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics, &mtuflags); 35263cee92e0SBjoern A. Zeeb 35273cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 35283cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 352997d8d152SAndre Oppermann 3530df8bae1dSRodney W. Grimes /* 353197d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 353297d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 353397d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 353497d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 353597d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3536df8bae1dSRodney W. Grimes */ 3537c3b02504SBjoern A. Zeeb so = inp->inp_socket; 35383f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 353997d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 354097d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 354197d8d152SAndre Oppermann else 3542df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3543df8bae1dSRodney W. Grimes if (bufsize < mss) 3544df8bae1dSRodney W. Grimes mss = bufsize; 3545df8bae1dSRodney W. Grimes else { 3546df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3547df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3548df8bae1dSRodney W. Grimes bufsize = sb_max; 354988c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 35503f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3551df8bae1dSRodney W. Grimes } 35523f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3553df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3554df8bae1dSRodney W. Grimes 35553f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 355697d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 355797d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 355897d8d152SAndre Oppermann else 3559df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3560df8bae1dSRodney W. Grimes if (bufsize > mss) { 3561df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3562df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3563df8bae1dSRodney W. Grimes bufsize = sb_max; 356488c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 35653f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3566df8bae1dSRodney W. Grimes } 35673f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 356891d6cfa6SBjoern A. Zeeb 356991d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 357091d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 357191d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3572a0292f23SGarrett Wollman } 3573a0292f23SGarrett Wollman 3574a0292f23SGarrett Wollman /* 3575a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3576a0292f23SGarrett Wollman */ 3577a0292f23SGarrett Wollman int 3578ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3579a0292f23SGarrett Wollman { 358097d8d152SAndre Oppermann int mss = 0; 358197d8d152SAndre Oppermann u_long maxmtu = 0; 358297d8d152SAndre Oppermann u_long thcmtu = 0; 358397d8d152SAndre Oppermann size_t min_protoh; 3584a0292f23SGarrett Wollman 358597d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3586a0292f23SGarrett Wollman 358731b3783cSHajimu UMEMOTO #ifdef INET6 3588dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3589603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3590233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 359197d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 359297d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 3593b287c6c7SBjoern A. Zeeb } 359431b3783cSHajimu UMEMOTO #endif 3595b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 3596b287c6c7SBjoern A. Zeeb else 3597b287c6c7SBjoern A. Zeeb #endif 3598b287c6c7SBjoern A. Zeeb #ifdef INET 359997d8d152SAndre Oppermann { 3600603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3601233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 360297d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 360397d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 360497d8d152SAndre Oppermann } 3605b287c6c7SBjoern A. Zeeb #endif 360697d8d152SAndre Oppermann if (maxmtu && thcmtu) 360797d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 360897d8d152SAndre Oppermann else if (maxmtu || thcmtu) 360997d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 361097d8d152SAndre Oppermann 361197d8d152SAndre Oppermann return (mss); 3612df8bae1dSRodney W. Grimes } 361346f58482SJonathan Lemon 361446f58482SJonathan Lemon 361546f58482SJonathan Lemon /* 3616c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3617c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3618c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3619c068736aSJeffrey Hsu * be started again. 362046f58482SJonathan Lemon */ 3621c068736aSJeffrey Hsu static void 3622ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 362346f58482SJonathan Lemon { 362446f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 362546f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 362646f58482SJonathan Lemon 36278501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 36287a3244ccSRobert Watson 3629b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 363046f58482SJonathan Lemon tp->t_rtttime = 0; 363146f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 36326b2a5f92SJayanth Vijayaraghavan /* 3633c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3634c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 36356b2a5f92SJayanth Vijayaraghavan */ 3636dbc42409SLawrence Stewart tp->snd_cwnd = tp->t_maxseg + BYTES_THIS_ACK(tp, th); 3637a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 36386b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 363946f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 364046f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 364146f58482SJonathan Lemon tp->snd_nxt = onxt; 364246f58482SJonathan Lemon /* 364346f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 364446f58482SJonathan Lemon * not updated yet. 364546f58482SJonathan Lemon */ 3646dbc42409SLawrence Stewart if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th)) 3647dbc42409SLawrence Stewart tp->snd_cwnd -= BYTES_THIS_ACK(tp, th); 3648d7587117SPaul Saab else 3649d7587117SPaul Saab tp->snd_cwnd = 0; 3650d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 365146f58482SJonathan Lemon } 3652