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. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29e79adb8eSGarrett Wollman * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 35f9e354dfSJulian Elischer #include "opt_ipfw.h" /* for ipfw_fwd */ 361cfd4b53SBruce M Simpson #include "opt_inet.h" 37fb59c426SYoshinobu Inoue #include "opt_inet6.h" 383a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 400cc12cc5SJoerg Wunsch 41df8bae1dSRodney W. Grimes #include <sys/param.h> 4298163b98SPoul-Henning Kamp #include <sys/kernel.h> 43df8bae1dSRodney W. Grimes #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 46df8bae1dSRodney W. Grimes #include <sys/protosw.h> 47960ed29cSSeigo Tanimura #include <sys/signalvar.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 50960ed29cSSeigo Tanimura #include <sys/sysctl.h> 51816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 52960ed29cSSeigo Tanimura #include <sys/systm.h> 53df8bae1dSRodney W. Grimes 54e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 55e79adb8eSGarrett Wollman 5612e2e970SAndre Oppermann #include <vm/uma.h> 5712e2e970SAndre Oppermann 58df8bae1dSRodney W. Grimes #include <net/if.h> 59df8bae1dSRodney W. Grimes #include <net/route.h> 60530c0060SRobert Watson #include <net/vnet.h> 61df8bae1dSRodney W. Grimes 62773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 63773673c1SAndre Oppermann 64df8bae1dSRodney W. Grimes #include <netinet/in.h> 65960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 66df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 67960ed29cSSeigo Tanimura #include <netinet/in_var.h> 68df8bae1dSRodney W. Grimes #include <netinet/ip.h> 698e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 70686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 72ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 73686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 74686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 75686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 76960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 77960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 78df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 83fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 84df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 85c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 86610ee2f9SDavid Greenman #ifdef TCPDEBUG 87df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 88fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 89fb59c426SYoshinobu Inoue 90b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 91b9234fafSSam Leffler #include <netipsec/ipsec.h> 92b9234fafSSam Leffler #include <netipsec/ipsec6.h> 93b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 94b9234fafSSam Leffler 95db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 96db4f9cc7SJonathan Lemon 97aed55708SRobert Watson #include <security/mac/mac_framework.h> 98aed55708SRobert Watson 99c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1000312fbe9SPoul-Henning Kamp 101eddfbb76SRobert Watson VNET_DEFINE(struct tcpstat, tcpstat); 102eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 103eddfbb76SRobert Watson &VNET_NAME(tcpstat), tcpstat, 1048b615593SMarko Zec "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1050312fbe9SPoul-Henning Kamp 106773673c1SAndre Oppermann int tcp_log_in_vain = 0; 107816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 108eddfbb76SRobert Watson &tcp_log_in_vain, 0, 109eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 110816a3d83SPoul-Henning Kamp 11182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0; 11282cea7e6SBjoern A. Zeeb #define V_blackhole VNET(blackhole) 113eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 114eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 115eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 11616f7f31fSGeoff Rehmet 11782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1; 118eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 119eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1203d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 121f498eeeeSDavid Greenman 12282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0; 12382cea7e6SBjoern A. Zeeb #define V_drop_synfin VNET(drop_synfin) 124eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 125eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 126eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 127f8613305SDag-Erling Smørgrav 12882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1; 12982cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3042 VNET(tcp_do_rfc3042) 130eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 131eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 132eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 133582a954bSJeffrey Hsu 13482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1; 13582cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3390 VNET(tcp_do_rfc3390) 136eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 137eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 138da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 139da3a8a1aSJeffrey Hsu 14082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1; 14182cea7e6SBjoern A. Zeeb #define V_tcp_do_rfc3465 VNET(tcp_do_rfc3465) 142eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 143eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 14424cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 145eddfbb76SRobert Watson 14682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2; 14782cea7e6SBjoern A. Zeeb #define V_tcp_abc_l_var VNET(tcp_abc_l_var) 148eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 149eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 15024cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 15124cb0f22SLawrence Stewart 152f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 153f2512ba1SRui Paulo 15482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0; 155eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_RW, 156eddfbb76SRobert Watson &VNET_NAME(tcp_do_ecn), 0, 157eddfbb76SRobert Watson "TCP ECN support"); 158eddfbb76SRobert Watson 15982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1; 160eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW, 161eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 162eddfbb76SRobert Watson "Max retries before giving up on ECN"); 163eddfbb76SRobert Watson 16482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0; 16582cea7e6SBjoern A. Zeeb #define V_tcp_insecure_rst VNET(tcp_insecure_rst) 166eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 167eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 1686489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 169a69968eeSMike Silbersack 17082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1; 17182cea7e6SBjoern A. Zeeb #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf) 172eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 173eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 1748b615593SMarko Zec "Enable automatic receive buffer sizing"); 1756741ecf5SAndre Oppermann 17682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024; 17782cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc) 178eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 179eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 1806489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1816741ecf5SAndre Oppermann 18282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 256*1024; 18382cea7e6SBjoern A. Zeeb #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max) 184eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 185eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 1868b615593SMarko Zec "Max size of automatic receive buffer"); 1876741ecf5SAndre Oppermann 188252ca428SRobert Watson int tcp_read_locking = 1; 189252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW, 190252ca428SRobert Watson &tcp_read_locking, 0, "Enable read locking strategy"); 191252ca428SRobert Watson 192eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 19344e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 19482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo); 195df8bae1dSRodney W. Grimes 1965a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 197679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 198252ca428SRobert Watson struct socket *, struct tcpcb *, int, int, uint8_t, 199252ca428SRobert Watson int); 200302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 201302ce8d6SAndre Oppermann struct tcpcb *, int, int); 2024d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 2034d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 2044d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 205c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 206f2512ba1SRui Paulo static void inline 207f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *); 208f2512ba1SRui Paulo 209315e3e38SRobert Watson /* 210315e3e38SRobert Watson * Kernel module interface for updating tcpstat. The argument is an index 211315e3e38SRobert Watson * into tcpstat treated as an array of u_long. While this encodes the 212315e3e38SRobert Watson * general layout of tcpstat into the caller, it doesn't encode its location, 213315e3e38SRobert Watson * so that future changes to add, for example, per-CPU stats support won't 214315e3e38SRobert Watson * cause binary compatibility problems for kernel modules. 215315e3e38SRobert Watson */ 216315e3e38SRobert Watson void 217315e3e38SRobert Watson kmod_tcpstat_inc(int statnum) 218315e3e38SRobert Watson { 219315e3e38SRobert Watson 220315e3e38SRobert Watson (*((u_long *)&V_tcpstat + statnum))++; 221315e3e38SRobert Watson } 222315e3e38SRobert Watson 223f2512ba1SRui Paulo static void inline 224f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *tp) 225f2512ba1SRui Paulo { 226f2512ba1SRui Paulo u_int win; 227f2512ba1SRui Paulo 228f2512ba1SRui Paulo win = min(tp->snd_wnd, tp->snd_cwnd) / 229f2512ba1SRui Paulo 2 / tp->t_maxseg; 230f2512ba1SRui Paulo if (win < 2) 231f2512ba1SRui Paulo win = 2; 232f2512ba1SRui Paulo tp->snd_ssthresh = win * tp->t_maxseg; 233f2512ba1SRui Paulo ENTER_FASTRECOVERY(tp); 234f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 235f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 236f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 237f2512ba1SRui Paulo } 2380312fbe9SPoul-Henning Kamp 239fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 240fb59c426SYoshinobu Inoue #ifdef INET6 241fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 242fb59c426SYoshinobu Inoue do { \ 243fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 24497d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 24597d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 246fb59c426SYoshinobu Inoue } while (0) 247fb59c426SYoshinobu Inoue #else 248fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 249fb59c426SYoshinobu Inoue #endif 250df8bae1dSRodney W. Grimes 251df8bae1dSRodney W. Grimes /* 252262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 253262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 254262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 2553bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 2563bfd6421SJonathan Lemon * - delayed acks are enabled or 2573bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 258d8c85a26SJonathan Lemon */ 259d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 260b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 261a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 262603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 263d8c85a26SJonathan Lemon 264df8bae1dSRodney W. Grimes /* 2658d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 2668d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 2678d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 2688d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 2698d573cc1SAndre Oppermann * SYN processing on listen sockets 2708d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 2718d573cc1SAndre Oppermann * establishing, established and closing connections 272df8bae1dSRodney W. Grimes */ 273fb59c426SYoshinobu Inoue #ifdef INET6 274fb59c426SYoshinobu Inoue int 275ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 276fb59c426SYoshinobu Inoue { 277ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 27833841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 279fb59c426SYoshinobu Inoue 280fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 281fb59c426SYoshinobu Inoue 282fb59c426SYoshinobu Inoue /* 283fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 284fb59c426SYoshinobu Inoue * better place to put this in? 285fb59c426SYoshinobu Inoue */ 28633841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 28733841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 288fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 289fb59c426SYoshinobu Inoue 2908c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 291fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 292fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 293fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 294fb59c426SYoshinobu Inoue return IPPROTO_DONE; 295fb59c426SYoshinobu Inoue } 296fb59c426SYoshinobu Inoue 297f0ffb944SJulian Elischer tcp_input(m, *offp); 298fb59c426SYoshinobu Inoue return IPPROTO_DONE; 299fb59c426SYoshinobu Inoue } 300fb59c426SYoshinobu Inoue #endif 301fb59c426SYoshinobu Inoue 302df8bae1dSRodney W. Grimes void 303ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 304df8bae1dSRodney W. Grimes { 305ad3f9ab3SAndre Oppermann struct tcphdr *th; 306ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 307ad3f9ab3SAndre Oppermann struct ipovly *ipov; 308ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 309302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 310302ce8d6SAndre Oppermann struct socket *so = NULL; 311e79adb8eSGarrett Wollman u_char *optp = NULL; 31226f9a767SRodney W. Grimes int optlen = 0; 313a0194ef1SBruce M Simpson int len, tlen, off; 314fb59c426SYoshinobu Inoue int drop_hdrlen; 315ad3f9ab3SAndre Oppermann int thflags; 316302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 317f2512ba1SRui Paulo uint8_t iptos; 3189b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 3199b932e9eSAndre Oppermann struct m_tag *fwd_tag; 3209b932e9eSAndre Oppermann #endif 321c068736aSJeffrey Hsu #ifdef INET6 322302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 323c068736aSJeffrey Hsu int isipv6; 324c068736aSJeffrey Hsu #else 325a160e630SAndre Oppermann const void *ip6 = NULL; 326c068736aSJeffrey Hsu const int isipv6 = 0; 327c068736aSJeffrey Hsu #endif 328302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 329a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 330252ca428SRobert Watson int ti_locked; 331252ca428SRobert Watson #define TI_UNLOCKED 1 332252ca428SRobert Watson #define TI_RLOCKED 2 333252ca428SRobert Watson #define TI_WLOCKED 3 334f76fcf6dSJeffrey Hsu 335610ee2f9SDavid Greenman #ifdef TCPDEBUG 336c068736aSJeffrey Hsu /* 337c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 338c068736aSJeffrey Hsu * now IPv6. 339c068736aSJeffrey Hsu */ 34014739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 341410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 342610ee2f9SDavid Greenman short ostate = 0; 343610ee2f9SDavid Greenman #endif 344c068736aSJeffrey Hsu 345fb59c426SYoshinobu Inoue #ifdef INET6 346fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 347fb59c426SYoshinobu Inoue #endif 348a0292f23SGarrett Wollman 349302ce8d6SAndre Oppermann to.to_flags = 0; 35078b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 351fb59c426SYoshinobu Inoue 352fb59c426SYoshinobu Inoue if (isipv6) { 35304d3a452SHajimu UMEMOTO #ifdef INET6 3548d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 355fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 356fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 357fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 35878b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 359fb59c426SYoshinobu Inoue goto drop; 360fb59c426SYoshinobu Inoue } 361fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 36233841545SHajimu UMEMOTO 36333841545SHajimu UMEMOTO /* 36433841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 36533841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 36633841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 36733841545SHajimu UMEMOTO * 36833841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 36933841545SHajimu UMEMOTO * already dropped in ip6_input. 37033841545SHajimu UMEMOTO */ 37133841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 37233841545SHajimu UMEMOTO /* XXX stat */ 37333841545SHajimu UMEMOTO goto drop; 37433841545SHajimu UMEMOTO } 37504d3a452SHajimu UMEMOTO #else 3768d573cc1SAndre Oppermann th = NULL; /* XXX: Avoid compiler warning. */ 37704d3a452SHajimu UMEMOTO #endif 378c068736aSJeffrey Hsu } else { 379df8bae1dSRodney W. Grimes /* 380df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 381df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 382df8bae1dSRodney W. Grimes */ 383fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 384df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 385fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 386fb59c426SYoshinobu Inoue } 387df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3884dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3894dfdffe9SAndre Oppermann == NULL) { 39078b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 391df8bae1dSRodney W. Grimes return; 392df8bae1dSRodney W. Grimes } 393df8bae1dSRodney W. Grimes } 394fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 395fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 396db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 397db4f9cc7SJonathan Lemon tlen = ip->ip_len; 398df8bae1dSRodney W. Grimes 399db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 400db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 401db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 402db4f9cc7SJonathan Lemon else 403db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 404c068736aSJeffrey Hsu ip->ip_dst.s_addr, 405c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 406c068736aSJeffrey Hsu ip->ip_len + 407c068736aSJeffrey Hsu IPPROTO_TCP)); 408db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 4093c653157SHartmut Brandt #ifdef TCPDEBUG 4103c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 4113c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 4123c653157SHartmut Brandt #endif 413db4f9cc7SJonathan Lemon } else { 414df8bae1dSRodney W. Grimes /* 415df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 416df8bae1dSRodney W. Grimes */ 417df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 418fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 419fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 420fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 421fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 422db4f9cc7SJonathan Lemon } 423fb59c426SYoshinobu Inoue if (th->th_sum) { 42478b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 425df8bae1dSRodney W. Grimes goto drop; 426df8bae1dSRodney W. Grimes } 427fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 428fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 429fb59c426SYoshinobu Inoue } 430df8bae1dSRodney W. Grimes 431f2512ba1SRui Paulo #ifdef INET6 432f2512ba1SRui Paulo if (isipv6) 433f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 434f2512ba1SRui Paulo else 435f2512ba1SRui Paulo #endif 436f2512ba1SRui Paulo iptos = ip->ip_tos; 437f2512ba1SRui Paulo 438df8bae1dSRodney W. Grimes /* 439df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 440df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 441df8bae1dSRodney W. Grimes */ 442fb59c426SYoshinobu Inoue off = th->th_off << 2; 443df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 44478b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 445df8bae1dSRodney W. Grimes goto drop; 446df8bae1dSRodney W. Grimes } 447fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 448df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 449fb59c426SYoshinobu Inoue if (isipv6) { 45004d3a452SHajimu UMEMOTO #ifdef INET6 451fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 452fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 453fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 45404d3a452SHajimu UMEMOTO #endif 455c068736aSJeffrey Hsu } else { 456df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 457c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 4584dfdffe9SAndre Oppermann == NULL) { 45978b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 460df8bae1dSRodney W. Grimes return; 461df8bae1dSRodney W. Grimes } 462fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 463fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 464fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 465fb59c426SYoshinobu Inoue } 466df8bae1dSRodney W. Grimes } 467df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 468fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 469df8bae1dSRodney W. Grimes } 470fb59c426SYoshinobu Inoue thflags = th->th_flags; 471df8bae1dSRodney W. Grimes 472e46cd3d4SDag-Erling Smørgrav /* 473df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 474df8bae1dSRodney W. Grimes */ 475fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 476fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 477fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 478fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 479df8bae1dSRodney W. Grimes 480df8bae1dSRodney W. Grimes /* 481794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 482755c1f07SAndras Olah */ 483fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 484755c1f07SAndras Olah 485755c1f07SAndras Olah /* 486252ca428SRobert Watson * Locate pcb for segment, which requires a lock on tcbinfo. 487d15fb965SRobert Watson * Optimisticaly acquire a global read lock rather than a write lock 488d15fb965SRobert Watson * unless header flags necessarily imply a state change. There are 489d15fb965SRobert Watson * two cases where we might discover later we need a write lock 490d15fb965SRobert Watson * despite the flags: ACKs moving a connection out of the syncache, 491d15fb965SRobert Watson * and ACKs for a connection in TIMEWAIT. 492df8bae1dSRodney W. Grimes */ 493252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 494252ca428SRobert Watson tcp_read_locking == 0) { 495603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 496252ca428SRobert Watson ti_locked = TI_WLOCKED; 497252ca428SRobert Watson } else { 498252ca428SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 499252ca428SRobert Watson ti_locked = TI_RLOCKED; 500252ca428SRobert Watson } 501252ca428SRobert Watson 502df8bae1dSRodney W. Grimes findpcb: 503252ca428SRobert Watson #ifdef INVARIANTS 504252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 505252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 506252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 507603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 508252ca428SRobert Watson else 509252ca428SRobert Watson panic("%s: findpcb ti_locked %d\n", __func__, ti_locked); 510252ca428SRobert Watson #endif 511252ca428SRobert Watson 5129b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5138d573cc1SAndre Oppermann /* 5148d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 5158d573cc1SAndre Oppermann */ 5169b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 5179b932e9eSAndre Oppermann 5189b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 5199b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 5209b932e9eSAndre Oppermann 5219b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 522f9e354dfSJulian Elischer /* 5232b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 524f9e354dfSJulian Elischer * already got one like this? 525f9e354dfSJulian Elischer */ 526603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 5279b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 528c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 529c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 530f9e354dfSJulian Elischer if (!inp) { 5319b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 532603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 533fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 5342b25acc1SLuigi Rizzo next_hop->sin_addr, 535c068736aSJeffrey Hsu next_hop->sin_port ? 536c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 537c068736aSJeffrey Hsu th->th_dport, 538421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 539421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 540f9e354dfSJulian Elischer } 5419b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 5429b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 543b10fbdeaSAndre Oppermann } else 5449b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 545b10fbdeaSAndre Oppermann { 54604d3a452SHajimu UMEMOTO if (isipv6) { 54704d3a452SHajimu UMEMOTO #ifdef INET6 548603724d3SBjoern A. Zeeb inp = in6_pcblookup_hash(&V_tcbinfo, 549c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 550c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 551421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 552421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 55304d3a452SHajimu UMEMOTO #endif 55404d3a452SHajimu UMEMOTO } else 555603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 556c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 557c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 558421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 559421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 560fb59c426SYoshinobu Inoue } 561f9e354dfSJulian Elischer 562df8bae1dSRodney W. Grimes /* 563574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 564574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 5658b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 566df8bae1dSRodney W. Grimes */ 567816a3d83SPoul-Henning Kamp if (inp == NULL) { 568574b6964SAndre Oppermann /* 569574b6964SAndre Oppermann * Log communication attempts to ports that are not 570574b6964SAndre Oppermann * in use. 571574b6964SAndre Oppermann */ 572574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 573574b6964SAndre Oppermann tcp_log_in_vain == 2) { 574*b7d747ecSAndre Oppermann if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6))) 575df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 576df541e5fSAndre Oppermann "to closed port\n", s, __func__); 5772e4e1b4cSGeoff Rehmet } 578574b6964SAndre Oppermann /* 579574b6964SAndre Oppermann * When blackholing do not respond with a RST but 580574b6964SAndre Oppermann * completely ignore the segment and drop it. 581574b6964SAndre Oppermann */ 582603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 583603724d3SBjoern A. Zeeb V_blackhole == 2) 5841929eae1SAndre Oppermann goto dropunlock; 585574b6964SAndre Oppermann 586a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 587a57815efSBosko Milekic goto dropwithreset; 588816a3d83SPoul-Henning Kamp } 5898501a69cSRobert Watson INP_WLOCK(inp); 59080cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 59180cb9f21SKip Macy && (m->m_flags & M_FLOWID) 59280cb9f21SKip Macy && ((inp->inp_socket == NULL) 59380cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 59480cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 59580cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 59680cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 59780cb9f21SKip Macy } 598a2589465SKen Smith #ifdef IPSEC 599a2589465SKen Smith #ifdef INET6 600a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 601603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 602a2589465SKen Smith goto dropunlock; 603a2589465SKen Smith } else 604a2589465SKen Smith #endif /* INET6 */ 605a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 606603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 607a2589465SKen Smith goto dropunlock; 608a2589465SKen Smith } 609a2589465SKen Smith #endif /* IPSEC */ 610a2589465SKen Smith 6118d573cc1SAndre Oppermann /* 6128d573cc1SAndre Oppermann * Check the minimum TTL for socket. 6138d573cc1SAndre Oppermann */ 61434f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 61534f83c52SGeorge V. Neville-Neil #ifdef INET6 61634f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 617302ce8d6SAndre Oppermann goto dropunlock; 61802707462SAndre Oppermann else 61934f83c52SGeorge V. Neville-Neil #endif 62034f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 621302ce8d6SAndre Oppermann goto dropunlock; 62234f83c52SGeorge V. Neville-Neil } 623936cd18dSAndre Oppermann 624340c35deSJonathan Lemon /* 625252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 626252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 627252ca428SRobert Watson * legitimate new connection attempt the old INPCB gets removed and 628252ca428SRobert Watson * we can try again to find a listening socket. 629252ca428SRobert Watson * 630252ca428SRobert Watson * At this point, due to earlier optimism, we may hold a read lock on 631252ca428SRobert Watson * the inpcbinfo, rather than a write lock. If so, we need to 632252ca428SRobert Watson * upgrade, or if that fails, acquire a reference on the inpcb, drop 633252ca428SRobert Watson * all locks, acquire a global write lock, and then re-acquire the 634252ca428SRobert Watson * inpcb lock. We may at that point discover that another thread has 635252ca428SRobert Watson * tried to free the inpcb, in which case we need to loop back and 636252ca428SRobert Watson * try to find a new inpcb to deliver to. 637340c35deSJonathan Lemon */ 638883e9bc4SRobert Watson relocked: 639ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 640252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 641252ca428SRobert Watson ("%s: INP_TIMEWAIT ti_locked %d", __func__, ti_locked)); 642252ca428SRobert Watson 643252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 64466f80e90SRobert Watson if (INP_INFO_TRY_UPGRADE(&V_tcbinfo) == 0) { 645252ca428SRobert Watson in_pcbref(inp); 646252ca428SRobert Watson INP_WUNLOCK(inp); 647252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 648252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 649252ca428SRobert Watson ti_locked = TI_WLOCKED; 650252ca428SRobert Watson INP_WLOCK(inp); 651252ca428SRobert Watson if (in_pcbrele(inp)) { 652252ca428SRobert Watson inp = NULL; 653252ca428SRobert Watson goto findpcb; 654252ca428SRobert Watson } 655f681a5fdSRobert Watson } else 656252ca428SRobert Watson ti_locked = TI_WLOCKED; 657252ca428SRobert Watson } 658252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 659252ca428SRobert Watson 660340c35deSJonathan Lemon if (thflags & TH_SYN) 661f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 6628d573cc1SAndre Oppermann /* 6638d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 6648d573cc1SAndre Oppermann */ 6652104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 666340c35deSJonathan Lemon goto findpcb; 667603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 668340c35deSJonathan Lemon return; 669340c35deSJonathan Lemon } 670794235b7SAndre Oppermann /* 671794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 672794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 673794235b7SAndre Oppermann * segment and send an appropriate response. 674794235b7SAndre Oppermann */ 675df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 676a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 677a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 678a57815efSBosko Milekic goto dropwithreset; 67909f81a46SBosko Milekic } 680df8bae1dSRodney W. Grimes 681252ca428SRobert Watson /* 682252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 683252ca428SRobert Watson * inpcbinfo write lock and have only a read lock. In this case, 684252ca428SRobert Watson * attempt to upgrade/relock using the same strategy as the TIMEWAIT 685883e9bc4SRobert Watson * case above. If we relock, we have to jump back to 'relocked' as 686883e9bc4SRobert Watson * the connection might now be in TIMEWAIT. 687252ca428SRobert Watson */ 688252ca428SRobert Watson if (tp->t_state != TCPS_ESTABLISHED || 689252ca428SRobert Watson (thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 690252ca428SRobert Watson tcp_read_locking == 0) { 691252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 692252ca428SRobert Watson ("%s: upgrade check ti_locked %d", __func__, ti_locked)); 693252ca428SRobert Watson 694252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 69566f80e90SRobert Watson if (INP_INFO_TRY_UPGRADE(&V_tcbinfo) == 0) { 696252ca428SRobert Watson in_pcbref(inp); 697252ca428SRobert Watson INP_WUNLOCK(inp); 698252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 699252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 700252ca428SRobert Watson ti_locked = TI_WLOCKED; 701252ca428SRobert Watson INP_WLOCK(inp); 702252ca428SRobert Watson if (in_pcbrele(inp)) { 703252ca428SRobert Watson inp = NULL; 704252ca428SRobert Watson goto findpcb; 705252ca428SRobert Watson } 706883e9bc4SRobert Watson goto relocked; 707f681a5fdSRobert Watson } else 708252ca428SRobert Watson ti_locked = TI_WLOCKED; 709252ca428SRobert Watson } 710252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 711252ca428SRobert Watson } 712252ca428SRobert Watson 713c488362eSRobert Watson #ifdef MAC 7148501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 71530d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 716302ce8d6SAndre Oppermann goto dropunlock; 717c488362eSRobert Watson #endif 718a557af22SRobert Watson so = inp->inp_socket; 719302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 720610ee2f9SDavid Greenman #ifdef TCPDEBUG 721df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 722df8bae1dSRodney W. Grimes ostate = tp->t_state; 723d30d90dcSMaxim Konovalov if (isipv6) { 72410fe523eSMaxim Konovalov #ifdef INET6 725540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 726d30d90dcSMaxim Konovalov #endif 727d30d90dcSMaxim Konovalov } else 728540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 729fb59c426SYoshinobu Inoue tcp_savetcp = *th; 730df8bae1dSRodney W. Grimes } 731610ee2f9SDavid Greenman #endif 732db33b3e6SAndre Oppermann /* 733db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 734db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 735db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 736db33b3e6SAndre Oppermann */ 737540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 738540e8b7eSJeffrey Hsu struct in_conninfo inc; 739540e8b7eSJeffrey Hsu 7407824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 7417824d002SAndre Oppermann "tp not listening", __func__)); 7427824d002SAndre Oppermann 7438bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 744302ce8d6SAndre Oppermann #ifdef INET6 745be2ac88cSJonathan Lemon if (isipv6) { 746dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 747be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 748be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 749302ce8d6SAndre Oppermann } else 750302ce8d6SAndre Oppermann #endif 751302ce8d6SAndre Oppermann { 752be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 753be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 754be2ac88cSJonathan Lemon } 755be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 756be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 7577973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 758be2ac88cSJonathan Lemon 759fb59c426SYoshinobu Inoue /* 7608d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 7618d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 7628d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 763fb59c426SYoshinobu Inoue */ 764be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 765bf6d304aSAndre Oppermann /* 766bf6d304aSAndre Oppermann * Parse the TCP options here because 767bf6d304aSAndre Oppermann * syncookies need access to the reflected 768bf6d304aSAndre Oppermann * timestamp. 769bf6d304aSAndre Oppermann */ 770bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 7719fa198beSAndre Oppermann /* 7729fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 7739fa198beSAndre Oppermann * inp and tcpinfo locks. 7749fa198beSAndre Oppermann */ 775bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 7764195b4afSPaul Traina /* 777e207f800SAndre Oppermann * No syncache entry or ACK was not 778be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 7798d573cc1SAndre Oppermann * NB: syncache did its own logging 7808d573cc1SAndre Oppermann * of the failure cause. 7814195b4afSPaul Traina */ 782be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 783be2ac88cSJonathan Lemon goto dropwithreset; 784be2ac88cSJonathan Lemon } 785f76fcf6dSJeffrey Hsu if (so == NULL) { 786be2ac88cSJonathan Lemon /* 787e207f800SAndre Oppermann * We completed the 3-way handshake 788e207f800SAndre Oppermann * but could not allocate a socket 789e207f800SAndre Oppermann * either due to memory shortage, 790e207f800SAndre Oppermann * listen queue length limits or 791a160e630SAndre Oppermann * global socket limits. Send RST 792a160e630SAndre Oppermann * or wait and have the remote end 793a160e630SAndre Oppermann * retransmit the ACK for another 794a160e630SAndre Oppermann * try. 795be2ac88cSJonathan Lemon */ 796a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 797a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 7988d573cc1SAndre Oppermann "Socket allocation failed due to " 7998d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 800ac957cd2SJulian Elischer s, __func__, 801ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 802ac957cd2SJulian Elischer "sending RST" : "try again"); 803603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 804e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 805e207f800SAndre Oppermann goto dropwithreset; 806a160e630SAndre Oppermann } else 807a160e630SAndre Oppermann goto dropunlock; 808f76fcf6dSJeffrey Hsu } 809be2ac88cSJonathan Lemon /* 810be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 8118d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 8128d573cc1SAndre Oppermann * created socket and update the tp variable. 813be2ac88cSJonathan Lemon */ 8148501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 815be2ac88cSJonathan Lemon inp = sotoinpcb(so); 8168501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 817be2ac88cSJonathan Lemon tp = intotcpcb(inp); 8188d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 8198d573cc1SAndre Oppermann ("%s: ", __func__)); 820be2ac88cSJonathan Lemon /* 821302ce8d6SAndre Oppermann * Process the segment and the data it 822302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 823302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 824302ce8d6SAndre Oppermann */ 825f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 826252ca428SRobert Watson iptos, ti_locked); 827603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 828302ce8d6SAndre Oppermann return; 829be2ac88cSJonathan Lemon } 830a160e630SAndre Oppermann /* 8318d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 8328d573cc1SAndre Oppermann * 833a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 834a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 835a160e630SAndre Oppermann * retransmits. 83619bc77c5SAndre Oppermann * 83719bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 83819bc77c5SAndre Oppermann * causes. 839a160e630SAndre Oppermann */ 840a160e630SAndre Oppermann if (thflags & TH_RST) { 84119bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 842a160e630SAndre Oppermann goto dropunlock; 843a160e630SAndre Oppermann } 844a160e630SAndre Oppermann /* 845a160e630SAndre Oppermann * We can't do anything without SYN. 846a160e630SAndre Oppermann */ 847a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 848a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 849a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 850773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 8518d573cc1SAndre Oppermann s, __func__); 85278b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 853a160e630SAndre Oppermann goto dropunlock; 854a160e630SAndre Oppermann } 855a160e630SAndre Oppermann /* 856a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 857a160e630SAndre Oppermann */ 858fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 859a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 860a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8618d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 8628d573cc1SAndre Oppermann s, __func__); 863a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 86478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 865a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 866a57815efSBosko Milekic goto dropwithreset; 8674195b4afSPaul Traina } 868a160e630SAndre Oppermann /* 869a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 870a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 871a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 872a160e630SAndre Oppermann * TCP/IP stack. 873a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 874a160e630SAndre Oppermann * and is constantly refining its stack detection 875a160e630SAndre Oppermann * strategies. 8768d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 877a160e630SAndre Oppermann * and was used by RFC1644. 878a160e630SAndre Oppermann */ 879603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 880a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 881a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 882773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 8838d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 88478b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 885302ce8d6SAndre Oppermann goto dropunlock; 886ebb0cbeaSPaul Traina } 887be2ac88cSJonathan Lemon /* 888be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 889a160e630SAndre Oppermann * 890a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 891a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 892a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 893be2ac88cSJonathan Lemon */ 894a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 895a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 896a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 897a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 89833841545SHajimu UMEMOTO #ifdef INET6 89933841545SHajimu UMEMOTO /* 90033841545SHajimu UMEMOTO * If deprecated address is forbidden, 90133841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 90233841545SHajimu UMEMOTO * address to prevent any new inbound connection from 90333841545SHajimu UMEMOTO * getting established. 90433841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 90533841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 90633841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 90733841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 90833841545SHajimu UMEMOTO * for the exchange. 90933841545SHajimu UMEMOTO * 91033841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 91133841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 91233841545SHajimu UMEMOTO * SYN in this case. 91333841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 91433841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 91533841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 91633841545SHajimu UMEMOTO * used" 91733841545SHajimu UMEMOTO * 2. use of it with new communication: 91833841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 91933841545SHajimu UMEMOTO * with sufficient scope is available" 92033841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 92133841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 92233841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 92333841545SHajimu UMEMOTO * 92433841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 92533841545SHajimu UMEMOTO * multiple description text for deprecated address 92633841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 92733841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 92833841545SHajimu UMEMOTO */ 929603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 93033841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 93133841545SHajimu UMEMOTO 9328c0fec80SRobert Watson ia6 = ip6_getdstifaddr(m); 9338c0fec80SRobert Watson if (ia6 != NULL && 93433841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 9358c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 936a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 937a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9388d573cc1SAndre Oppermann "Connection attempt to deprecated " 9398d573cc1SAndre Oppermann "IPv6 address rejected\n", 940a160e630SAndre Oppermann s, __func__); 94133841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 94233841545SHajimu UMEMOTO goto dropwithreset; 94333841545SHajimu UMEMOTO } 9448c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 94533841545SHajimu UMEMOTO } 94633841545SHajimu UMEMOTO #endif 94765f28919SJesper Skriver /* 948db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 9498d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 950db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 9518d573cc1SAndre Oppermann * If it is from this socket it must be forged. 9528d573cc1SAndre Oppermann * Don't respond if the source or destination is a 9538d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 95493ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 95593ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 95693ec91baSCrist J. Clark * in_broadcast() to find them. 957be2ac88cSJonathan Lemon */ 9588d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 9598d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 9608d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9618d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 962773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 963302ce8d6SAndre Oppermann goto dropunlock; 9648d573cc1SAndre Oppermann } 965be2ac88cSJonathan Lemon if (isipv6) { 966db33b3e6SAndre Oppermann #ifdef INET6 967db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 968a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 969a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 970a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9718d573cc1SAndre Oppermann "Connection attempt to/from self " 972773673c1SAndre Oppermann "ignored\n", s, __func__); 973302ce8d6SAndre Oppermann goto dropunlock; 974a160e630SAndre Oppermann } 975be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 976a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 977a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 978a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9798d573cc1SAndre Oppermann "Connection attempt from/to multicast " 980773673c1SAndre Oppermann "address ignored\n", s, __func__); 981302ce8d6SAndre Oppermann goto dropunlock; 982a160e630SAndre Oppermann } 983db33b3e6SAndre Oppermann #endif 984c068736aSJeffrey Hsu } else { 985db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 986a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 987a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 988a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9898d573cc1SAndre Oppermann "Connection attempt from/to self " 990773673c1SAndre Oppermann "ignored\n", s, __func__); 991302ce8d6SAndre Oppermann goto dropunlock; 992a160e630SAndre Oppermann } 993be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 994be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 9952ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 996a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 997a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 998a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9998d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1000773673c1SAndre Oppermann "or multicast address ignored\n", 1001a160e630SAndre Oppermann s, __func__); 1002302ce8d6SAndre Oppermann goto dropunlock; 1003c068736aSJeffrey Hsu } 1004a160e630SAndre Oppermann } 1005be2ac88cSJonathan Lemon /* 1006db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1007db33b3e6SAndre Oppermann * for syncache. 1008be2ac88cSJonathan Lemon */ 10093c653157SHartmut Brandt #ifdef TCPDEBUG 10103c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10113c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10123c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 10133c653157SHartmut Brandt #endif 1014f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 10154d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 1016be2ac88cSJonathan Lemon /* 10174d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1018a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1019be2ac88cSJonathan Lemon */ 1020603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1021be2ac88cSJonathan Lemon return; 1022f76fcf6dSJeffrey Hsu } 1023db33b3e6SAndre Oppermann 1024302ce8d6SAndre Oppermann /* 10258d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 10268d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 10278d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1028302ce8d6SAndre Oppermann */ 1029252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1030603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1031302ce8d6SAndre Oppermann return; 1032be2ac88cSJonathan Lemon 1033302ce8d6SAndre Oppermann dropwithreset: 1034252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1035252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1036252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1037dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1038252ca428SRobert Watson else 1039252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 1040252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1041014ea782SRobert Watson 1042014ea782SRobert Watson if (inp != NULL) { 1043302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1044014ea782SRobert Watson INP_WUNLOCK(inp); 1045dd8ac7f9SRobert Watson } else 1046014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1047302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1048014ea782SRobert Watson goto drop; 1049014ea782SRobert Watson 1050302ce8d6SAndre Oppermann dropunlock: 1051252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1052252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1053252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1054252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1055252ca428SRobert Watson else 1056252ca428SRobert Watson panic("%s: dropunlock ti_locked %d", __func__, ti_locked); 1057252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1058252ca428SRobert Watson 10599fa198beSAndre Oppermann if (inp != NULL) 10608501a69cSRobert Watson INP_WUNLOCK(inp); 1061014ea782SRobert Watson 1062302ce8d6SAndre Oppermann drop: 1063603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1064a160e630SAndre Oppermann if (s != NULL) 1065a160e630SAndre Oppermann free(s, M_TCPLOG); 1066302ce8d6SAndre Oppermann if (m != NULL) 1067302ce8d6SAndre Oppermann m_freem(m); 1068302ce8d6SAndre Oppermann } 1069302ce8d6SAndre Oppermann 1070679d9708SAndre Oppermann static void 1071302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1072252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1073252ca428SRobert Watson int ti_locked) 1074302ce8d6SAndre Oppermann { 1075302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1076302ce8d6SAndre Oppermann int rstreason, todrop, win; 1077302ce8d6SAndre Oppermann u_long tiwin; 1078302ce8d6SAndre Oppermann struct tcpopt to; 1079302ce8d6SAndre Oppermann 108014739780SMaxim Konovalov #ifdef TCPDEBUG 108114739780SMaxim Konovalov /* 108214739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 108314739780SMaxim Konovalov * now IPv6. 108414739780SMaxim Konovalov */ 108514739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 108614739780SMaxim Konovalov struct tcphdr tcp_savetcp; 108714739780SMaxim Konovalov short ostate = 0; 108814739780SMaxim Konovalov #endif 1089302ce8d6SAndre Oppermann thflags = th->th_flags; 1090302ce8d6SAndre Oppermann 1091252ca428SRobert Watson /* 1092252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1093252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 1094252ca428SRobert Watson * allow either a read lock or a write lock, as we may have acquired 1095252ca428SRobert Watson * a write lock due to a race. 1096252ca428SRobert Watson * 1097d15fb965SRobert Watson * Require a global write lock for SYN/FIN/RST segments or 1098252ca428SRobert Watson * non-established connections; otherwise accept either a read or 1099252ca428SRobert Watson * write lock, as we may have conservatively acquired a write lock in 1100252ca428SRobert Watson * certain cases in tcp_input() (is this still true?). Currently we 1101252ca428SRobert Watson * will never enter with no lock, so we try to drop it quickly in the 1102252ca428SRobert Watson * common pure ack/pure data cases. 1103252ca428SRobert Watson */ 1104252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1105252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1106252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1107252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1108603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1109252ca428SRobert Watson } else { 1110252ca428SRobert Watson #ifdef INVARIANTS 1111252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1112252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1113252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1114252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1115252ca428SRobert Watson else 1116252ca428SRobert Watson panic("%s: ti_locked %d for EST", __func__, 1117252ca428SRobert Watson ti_locked); 1118252ca428SRobert Watson #endif 1119252ca428SRobert Watson } 11208501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1121679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1122679d9708SAndre Oppermann __func__)); 1123679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1124679d9708SAndre Oppermann __func__)); 1125df8bae1dSRodney W. Grimes 1126df8bae1dSRodney W. Grimes /* 1127df8bae1dSRodney W. Grimes * Segment received on connection. 1128df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1129e8949f74SAndre Oppermann * XXX: This should be done after segment 1130e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1131df8bae1dSRodney W. Grimes */ 11329b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 11337ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1134b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1135df8bae1dSRodney W. Grimes 1136df8bae1dSRodney W. Grimes /* 1137464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1138e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1139464fcfbcSAndre Oppermann */ 1140464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1141464fcfbcSAndre Oppermann 1142464fcfbcSAndre Oppermann /* 1143f2512ba1SRui Paulo * TCP ECN processing. 1144f2512ba1SRui Paulo */ 1145f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 11469c251892SRui Paulo if (thflags & TH_CWR) 11479c251892SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1148f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1149f2512ba1SRui Paulo case IPTOS_ECN_CE: 1150f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 115178b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1152f2512ba1SRui Paulo break; 1153f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 115478b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1155f2512ba1SRui Paulo break; 1156f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 115778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1158f2512ba1SRui Paulo break; 1159f2512ba1SRui Paulo } 1160f2512ba1SRui Paulo /* 1161f2512ba1SRui Paulo * Congestion experienced. 1162f2512ba1SRui Paulo * Ignore if we are already trying to recover. 1163f2512ba1SRui Paulo */ 1164f2512ba1SRui Paulo if ((thflags & TH_ECE) && 1165f2512ba1SRui Paulo SEQ_LEQ(th->th_ack, tp->snd_recover)) { 116678b50714SRobert Watson TCPSTAT_INC(tcps_ecn_rcwnd); 1167f2512ba1SRui Paulo tcp_congestion_exp(tp); 1168f2512ba1SRui Paulo } 1169f2512ba1SRui Paulo } 1170f2512ba1SRui Paulo 1171f2512ba1SRui Paulo /* 1172f72167f4SAndre Oppermann * Parse options on any incoming segment. 1173f72167f4SAndre Oppermann */ 1174302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1175302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1176302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1177f72167f4SAndre Oppermann 1178f72167f4SAndre Oppermann /* 1179f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1180bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1181bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1182bf6d304aSAndre Oppermann * was established. 1183f72167f4SAndre Oppermann */ 1184bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1185e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1186bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1187f72167f4SAndre Oppermann to.to_tsecr = 0; 1188bf6d304aSAndre Oppermann } 1189f72167f4SAndre Oppermann 1190f72167f4SAndre Oppermann /* 119197d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 119297d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 11935396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 11945396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 119597d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1196df8bae1dSRodney W. Grimes */ 11970a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1198464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1199464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1200be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 120102a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1202be2ac88cSJonathan Lemon } 12035396d0f8SAndre Oppermann /* 12045396d0f8SAndre Oppermann * Initial send window. It will be updated with 12055396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 12065396d0f8SAndre Oppermann */ 12075396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1208be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1209be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1210be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1211be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1212be2ac88cSJonathan Lemon } 1213be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1214be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 12153529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 12163529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 12173529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 12186d90faf3SPaul Saab } 12196d90faf3SPaul Saab 1220df8bae1dSRodney W. Grimes /* 1221df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1222df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1223df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1224df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1225df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1226df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1227df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1228df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1229df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1230df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1231df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1232df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1233a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1234c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1235a0292f23SGarrett Wollman * be TH_NEEDSYN. 1236df8bae1dSRodney W. Grimes */ 1237df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1238c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1239fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1240c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1241c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1242a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1243c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1244be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1245c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1246df8bae1dSRodney W. Grimes 1247df8bae1dSRodney W. Grimes /* 1248df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1249df8bae1dSRodney W. Grimes * record the timestamp. 1250a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1251a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1252df8bae1dSRodney W. Grimes */ 1253be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1254fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 12559b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1256a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1257df8bae1dSRodney W. Grimes } 1258df8bae1dSRodney W. Grimes 1259fb59c426SYoshinobu Inoue if (tlen == 0) { 1260fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1261fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1262233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 1263603724d3SBjoern A. Zeeb ((!V_tcp_do_newreno && 12643529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 1265cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 1266603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 12673529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 1268fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 1269fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1270482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1271df8bae1dSRodney W. Grimes /* 1272e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1273df8bae1dSRodney W. Grimes */ 1274252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1275252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1276252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1277252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1278252ca428SRobert Watson else 1279252ca428SRobert Watson panic("%s: ti_locked %d on pure ACK", 1280252ca428SRobert Watson __func__, ti_locked); 1281252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1282252ca428SRobert Watson 128378b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1284252ca428SRobert Watson 12859b8b58e0SJonathan Lemon /* 1286e8949f74SAndre Oppermann * "bad retransmit" recovery. 12879b8b58e0SJonathan Lemon */ 12889b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 12896dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 129078b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitbad); 12919b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 12929b8b58e0SJonathan Lemon tp->snd_ssthresh = 12939b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 12949d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 12959d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 12969d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 12979b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 12989b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 12999b8b58e0SJonathan Lemon } 1300fa55172bSMatthew Dillon 1301fa55172bSMatthew Dillon /* 1302fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1303fa55172bSMatthew Dillon * 1304fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1305fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1306fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1307fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1308fa55172bSMatthew Dillon */ 1309fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1310fa55172bSMatthew Dillon to.to_tsecr) { 1311eaf80179SAndre Oppermann if (!tp->t_rttlow || 1312eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1313eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1314a0292f23SGarrett Wollman tcp_xmit_timer(tp, 13159b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1316fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1317fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1318eaf80179SAndre Oppermann if (!tp->t_rttlow || 1319eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1320eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1321c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1322c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1323fa55172bSMatthew Dillon } 13241fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1325fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 132678b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 132778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1328df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 13299d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 13309d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 13319d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 13329d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 13331645d090SJeff Roberson /* 1334e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 13351645d090SJeff Roberson * to th_ack. 13361645d090SJeff Roberson */ 1337cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1338b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1339df8bae1dSRodney W. Grimes m_freem(m); 1340e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1341df8bae1dSRodney W. Grimes 1342df8bae1dSRodney W. Grimes /* 1343df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1344df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1345df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1346df8bae1dSRodney W. Grimes * If process is waiting for space, 1347df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1348df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1349df8bae1dSRodney W. Grimes * decide between more output or persist. 1350e8949f74SAndre Oppermann */ 13513c653157SHartmut Brandt #ifdef TCPDEBUG 13523c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13533c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13543c653157SHartmut Brandt (void *)tcp_saveipgen, 13553c653157SHartmut Brandt &tcp_savetcp, 0); 13563c653157SHartmut Brandt #endif 1357df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1358b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1359b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1360b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1361b8152ba7SAndre Oppermann tp->t_rxtcur); 1362df8bae1dSRodney W. Grimes sowwakeup(so); 1363df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1364df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1365a14c749fSJonathan Lemon goto check_delack; 1366df8bae1dSRodney W. Grimes } 1367fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1368fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 13696741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 13706741ecf5SAndre Oppermann 1371df8bae1dSRodney W. Grimes /* 1372252ca428SRobert Watson * This is a pure, in-sequence data packet with 1373252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1374252ca428SRobert Watson * buffer space to take it. 1375df8bae1dSRodney W. Grimes */ 1376252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1377252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1378252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1379252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1380252ca428SRobert Watson else 1381252ca428SRobert Watson panic("%s: ti_locked %d on pure data " 1382252ca428SRobert Watson "segment", __func__, ti_locked); 1383252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1384252ca428SRobert Watson 13856d90faf3SPaul Saab /* Clean receiver SACK report if present */ 13863529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 13876d90faf3SPaul Saab tcp_clean_sackreport(tp); 138878b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1389fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 13901645d090SJeff Roberson /* 13911645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 13921645d090SJeff Roberson * th_seq. 13931645d090SJeff Roberson */ 13941645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 13951645d090SJeff Roberson /* 13961645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 13971645d090SJeff Roberson * rcv_nxt. 13981645d090SJeff Roberson */ 13991645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 140078b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 140178b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 1402e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 14033c653157SHartmut Brandt #ifdef TCPDEBUG 14043c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 14053c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 14063c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 14073c653157SHartmut Brandt #endif 14086741ecf5SAndre Oppermann /* 14096741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 14106741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 14116741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 14126741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 14136741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 14146741ecf5SAndre Oppermann * 14156741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 14166741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 14176741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 14186741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 14196741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 14206741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 14216741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 14226741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 14236741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 14246741ecf5SAndre Oppermann * reassembly queue. 14256741ecf5SAndre Oppermann * 14266741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 14276741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 14286741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 14296741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 14306741ecf5SAndre Oppermann * current socket buffer size; 14316741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 14326741ecf5SAndre Oppermann * 14336741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 14346741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 14356741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 14366741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 14376741ecf5SAndre Oppermann * 14386741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 14396741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1440df8bae1dSRodney W. Grimes */ 1441603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 14426741ecf5SAndre Oppermann to.to_tsecr && 14436741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 14446741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 14456741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 14466741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 14476741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 14486741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1449603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 14506741ecf5SAndre Oppermann newsize = 14516741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1452603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1453603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 14546741ecf5SAndre Oppermann } 14556741ecf5SAndre Oppermann /* Start over with next RTT. */ 14566741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 14576741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 14586741ecf5SAndre Oppermann } else 14596741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 14606741ecf5SAndre Oppermann } 14616741ecf5SAndre Oppermann 14626741ecf5SAndre Oppermann /* Add data to socket buffer. */ 14631e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1464c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1465c1c36a2cSMike Silbersack m_freem(m); 1466c1c36a2cSMike Silbersack } else { 14676741ecf5SAndre Oppermann /* 14686741ecf5SAndre Oppermann * Set new socket buffer size. 14696741ecf5SAndre Oppermann * Give up when limit is reached. 14706741ecf5SAndre Oppermann */ 14716741ecf5SAndre Oppermann if (newsize) 14726741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 14736c8286e4SRobert Watson newsize, so, NULL)) 14746741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1475fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 14761e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1477c1c36a2cSMike Silbersack } 1478e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 14791e4d7da7SRobert Watson sorwakeup_locked(so); 1480d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 14813bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1482f498eeeeSDavid Greenman } else { 1483e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1484e612a582SDavid Greenman tcp_output(tp); 1485e612a582SDavid Greenman } 1486a14c749fSJonathan Lemon goto check_delack; 1487df8bae1dSRodney W. Grimes } 1488df8bae1dSRodney W. Grimes } 1489df8bae1dSRodney W. Grimes 1490df8bae1dSRodney W. Grimes /* 1491df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1492df8bae1dSRodney W. Grimes * and then do TCP input processing. 1493df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1494df8bae1dSRodney W. Grimes * but not less than advertised window. 1495df8bae1dSRodney W. Grimes */ 1496df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1497df8bae1dSRodney W. Grimes if (win < 0) 1498df8bae1dSRodney W. Grimes win = 0; 149966e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1500df8bae1dSRodney W. Grimes 15016741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 15026741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 15036741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 15046741ecf5SAndre Oppermann 1505df8bae1dSRodney W. Grimes switch (tp->t_state) { 1506df8bae1dSRodney W. Grimes 1507df8bae1dSRodney W. Grimes /* 1508764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1509764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1510764d8cefSBill Fenner */ 1511764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1512fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1513fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1514a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1515a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1516a57815efSBosko Milekic goto dropwithreset; 1517a57815efSBosko Milekic } 1518764d8cefSBill Fenner break; 1519764d8cefSBill Fenner 1520764d8cefSBill Fenner /* 1521df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1522df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1523df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1524df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1525df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1526df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1527df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1528f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1529f2512ba1SRui Paulo * is ECN capable. 1530df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1531df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1532df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1533df8bae1dSRodney W. Grimes */ 1534df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1535fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1536fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1537fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1538a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1539a0292f23SGarrett Wollman goto dropwithreset; 1540a0292f23SGarrett Wollman } 15410ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1542df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 15430ca3f933SAndre Oppermann if (thflags & TH_RST) 1544df8bae1dSRodney W. Grimes goto drop; 15450ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1546df8bae1dSRodney W. Grimes goto drop; 1547464fcfbcSAndre Oppermann 1548fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1549df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1550fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 155178b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1552845799c1SAndras Olah soisconnected(so); 1553c488362eSRobert Watson #ifdef MAC 155430d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1555c488362eSRobert Watson #endif 1556845799c1SAndras Olah /* Do window scaling on this connection? */ 1557845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1558845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1559845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1560845799c1SAndras Olah } 1561a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1562a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1563a0292f23SGarrett Wollman /* 1564a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1565a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1566a0292f23SGarrett Wollman */ 1567d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1568b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1569b8152ba7SAndre Oppermann tcp_delacktime); 1570a0292f23SGarrett Wollman else 1571a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1572f2512ba1SRui Paulo 1573603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1574f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 157578b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1576f2512ba1SRui Paulo } 1577f2512ba1SRui Paulo 1578a0292f23SGarrett Wollman /* 1579a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1580a0292f23SGarrett Wollman * Transitions: 1581a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1582a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1583a0292f23SGarrett Wollman */ 15849b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1585a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1586a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1587a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1588fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 15897ff19458SPaul Traina } else { 1590a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1591b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 15927ff19458SPaul Traina } 1593a0292f23SGarrett Wollman } else { 1594a0292f23SGarrett Wollman /* 1595c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1596c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1597c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1598c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1599c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1600a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1601a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1602a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1603a0292f23SGarrett Wollman */ 16044b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1605b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1606df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1607a0292f23SGarrett Wollman } 1608df8bae1dSRodney W. Grimes 1609252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1610252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1611252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 16128501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 16137cfc6904SRobert Watson 1614df8bae1dSRodney W. Grimes /* 1615fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1616df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1617df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1618df8bae1dSRodney W. Grimes */ 1619fb59c426SYoshinobu Inoue th->th_seq++; 1620fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1621fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1622df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1623fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1624fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 162578b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 162678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1627df8bae1dSRodney W. Grimes } 1628fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1629fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1630a0292f23SGarrett Wollman /* 1631a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1632a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1633a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1634a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1635a0292f23SGarrett Wollman * Otherwise, goto step 6. 1636a0292f23SGarrett Wollman */ 1637fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1638a0292f23SGarrett Wollman goto process_ACK; 1639c068736aSJeffrey Hsu 1640df8bae1dSRodney W. Grimes goto step6; 1641c068736aSJeffrey Hsu 1642a0292f23SGarrett Wollman /* 1643a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1644c94c54e4SAndre Oppermann * do normal processing. 1645a0292f23SGarrett Wollman * 1646c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1647a0292f23SGarrett Wollman */ 1648a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1649a0292f23SGarrett Wollman case TCPS_CLOSING: 1650a0292f23SGarrett Wollman break; /* continue normal processing */ 1651df8bae1dSRodney W. Grimes } 1652df8bae1dSRodney W. Grimes 1653df8bae1dSRodney W. Grimes /* 1654df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 165580ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 165680ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 165780ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 165880ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 165980ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 166080ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 166180ab7c0eSGarrett Wollman * killing a connection). 166280ab7c0eSGarrett Wollman * Then check timestamp, if present. 1663a0292f23SGarrett Wollman * Then check the connection count, if present. 1664df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1665df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1666df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1667df8bae1dSRodney W. Grimes * 166880ab7c0eSGarrett Wollman * 166980ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 167080ab7c0eSGarrett Wollman * if this is a valid reset segment. 167180ab7c0eSGarrett Wollman * RFC 793 page 37: 167280ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 167380ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 167480ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 167580ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 167680ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 16771a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 16781a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 16791a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 16801a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 168180dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 168280dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 168380dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 168480dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 168580dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 168680dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 16878e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 168880ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 168980ab7c0eSGarrett Wollman * but they will eventually catch up. 169080ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 169180ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 169280ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 169380ab7c0eSGarrett Wollman * 169480ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 169580ab7c0eSGarrett Wollman * 169680ab7c0eSGarrett Wollman * DISCUSSION 169780ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 169880ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 169980ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 170080ab7c0eSGarrett Wollman * data. 170180ab7c0eSGarrett Wollman * 170280ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 170380ab7c0eSGarrett Wollman * the state: 170480ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 170580ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 170680ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1707745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 170880ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1709e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 171080ab7c0eSGarrett Wollman * Close the tcb. 1711e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 171280ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 171380ab7c0eSGarrett Wollman * RFC 1337. 171480ab7c0eSGarrett Wollman */ 1715fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 171695ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 171795ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 171880ab7c0eSGarrett Wollman switch (tp->t_state) { 171980ab7c0eSGarrett Wollman 172080ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 172180ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 172280ab7c0eSGarrett Wollman goto close; 172380ab7c0eSGarrett Wollman 172480ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 1725603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 172695ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 172795ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 172895ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 172995ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 173078b50714SRobert Watson TCPSTAT_INC(tcps_badrst); 173180dd2a81SMike Silbersack goto drop; 173280dd2a81SMike Silbersack } 1733564aab1fSAndre Oppermann /* FALLTHROUGH */ 173480ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 173580ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 173680ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 173780ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 173880ab7c0eSGarrett Wollman close: 1739252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1740252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 1741252ca428SRobert Watson ti_locked)); 1742252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1743252ca428SRobert Watson 174480ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 174578b50714SRobert Watson TCPSTAT_INC(tcps_drops); 174680ab7c0eSGarrett Wollman tp = tcp_close(tp); 174780ab7c0eSGarrett Wollman break; 174880ab7c0eSGarrett Wollman 174980ab7c0eSGarrett Wollman case TCPS_CLOSING: 175080ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1751252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1752252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 1753252ca428SRobert Watson ti_locked)); 1754252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1755252ca428SRobert Watson 175680ab7c0eSGarrett Wollman tp = tcp_close(tp); 175780ab7c0eSGarrett Wollman break; 175880ab7c0eSGarrett Wollman } 175980ab7c0eSGarrett Wollman } 176080ab7c0eSGarrett Wollman goto drop; 176180ab7c0eSGarrett Wollman } 176280ab7c0eSGarrett Wollman 176380ab7c0eSGarrett Wollman /* 1764df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1765df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1766df8bae1dSRodney W. Grimes */ 1767be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 176880ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1769df8bae1dSRodney W. Grimes 1770df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 17711a0e7cfcSJohn Baldwin if (ticks - tp->ts_recent_age > TCP_PAWS_IDLE) { 1772df8bae1dSRodney W. Grimes /* 1773df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1774df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1775df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1776df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1777df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1778df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1779df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1780df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1781df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1782df8bae1dSRodney W. Grimes */ 1783df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1784df8bae1dSRodney W. Grimes } else { 178578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 178678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 178778b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 178807fd333dSMatthew Dillon if (tlen) 1789df8bae1dSRodney W. Grimes goto dropafterack; 17901ab4789dSMatthew Dillon goto drop; 17911ab4789dSMatthew Dillon } 1792df8bae1dSRodney W. Grimes } 1793df8bae1dSRodney W. Grimes 1794a0292f23SGarrett Wollman /* 179580ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 179680ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 179780ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 179880ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 179980ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 180080ab7c0eSGarrett Wollman */ 1801a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1802a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1803a57815efSBosko Milekic goto dropwithreset; 1804a57815efSBosko Milekic } 180580ab7c0eSGarrett Wollman 1806fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1807df8bae1dSRodney W. Grimes if (todrop > 0) { 180881ad7eb0SZachary Loafman /* 180981ad7eb0SZachary Loafman * If this is a duplicate SYN for our current connection, 181081ad7eb0SZachary Loafman * advance over it and pretend and it's not a SYN. 181181ad7eb0SZachary Loafman */ 181281ad7eb0SZachary Loafman if (thflags & TH_SYN && th->th_seq == tp->irs) { 1813fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1814fb59c426SYoshinobu Inoue th->th_seq++; 1815fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1816fb59c426SYoshinobu Inoue th->th_urp--; 1817df8bae1dSRodney W. Grimes else 1818fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1819df8bae1dSRodney W. Grimes todrop--; 1820df8bae1dSRodney W. Grimes } 1821df8bae1dSRodney W. Grimes /* 1822dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1823df8bae1dSRodney W. Grimes */ 1824fb59c426SYoshinobu Inoue if (todrop > tlen 1825fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1826dac20301SGarrett Wollman /* 1827dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1828dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1829dac20301SGarrett Wollman * of sequence; drop it. 1830dac20301SGarrett Wollman */ 1831fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1832dac20301SGarrett Wollman 1833df8bae1dSRodney W. Grimes /* 1834dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1835dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1836df8bae1dSRodney W. Grimes */ 1837dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1838fb59c426SYoshinobu Inoue todrop = tlen; 183978b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 184078b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 1841df8bae1dSRodney W. Grimes } else { 184278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 184378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 1844df8bae1dSRodney W. Grimes } 1845fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1846fb59c426SYoshinobu Inoue th->th_seq += todrop; 1847fb59c426SYoshinobu Inoue tlen -= todrop; 1848fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1849fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1850df8bae1dSRodney W. Grimes else { 1851fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1852fb59c426SYoshinobu Inoue th->th_urp = 0; 1853df8bae1dSRodney W. Grimes } 1854df8bae1dSRodney W. Grimes } 1855df8bae1dSRodney W. Grimes 1856df8bae1dSRodney W. Grimes /* 1857df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1858df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1859df8bae1dSRodney W. Grimes */ 1860df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1861fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1862773673c1SAndre Oppermann char *s; 1863773673c1SAndre Oppermann 1864252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 1865252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 1866252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1867252ca428SRobert Watson 1868773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 1869e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 1870773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 1871e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 1872773673c1SAndre Oppermann free(s, M_TCPLOG); 1873773673c1SAndre Oppermann } 1874df8bae1dSRodney W. Grimes tp = tcp_close(tp); 187578b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 1876a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1877df8bae1dSRodney W. Grimes goto dropwithreset; 1878df8bae1dSRodney W. Grimes } 1879df8bae1dSRodney W. Grimes 1880df8bae1dSRodney W. Grimes /* 1881df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1882df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1883df8bae1dSRodney W. Grimes */ 1884fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1885df8bae1dSRodney W. Grimes if (todrop > 0) { 188678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 1887fb59c426SYoshinobu Inoue if (todrop >= tlen) { 188878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 1889df8bae1dSRodney W. Grimes /* 1890df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1891df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1892df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1893df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1894df8bae1dSRodney W. Grimes * and ack. 1895df8bae1dSRodney W. Grimes */ 1896fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1897df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 189878b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 1899df8bae1dSRodney W. Grimes } else 1900df8bae1dSRodney W. Grimes goto dropafterack; 1901df8bae1dSRodney W. Grimes } else 190278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1903df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1904fb59c426SYoshinobu Inoue tlen -= todrop; 1905fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1906df8bae1dSRodney W. Grimes } 1907df8bae1dSRodney W. Grimes 1908df8bae1dSRodney W. Grimes /* 1909df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1910df8bae1dSRodney W. Grimes * record its timestamp. 1911cf09195bSPaul Saab * NOTE: 1912cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1913a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1914cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1915cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1916cf09195bSPaul Saab * predicated on the sequence space of this segment. 1917cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1918cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1919cf09195bSPaul Saab * instead of RFC1323's 1920cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1921cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1922cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1923cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1924cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1925df8bae1dSRodney W. Grimes */ 1926be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1927cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1928cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1929cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 19309b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1931a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1932df8bae1dSRodney W. Grimes } 1933df8bae1dSRodney W. Grimes 1934df8bae1dSRodney W. Grimes /* 1935df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1936df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1937df8bae1dSRodney W. Grimes */ 1938fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1939252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1940252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 1941252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1942252ca428SRobert Watson 1943df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1944a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1945122aad88SAndre Oppermann goto drop; 1946df8bae1dSRodney W. Grimes } 1947df8bae1dSRodney W. Grimes 1948a0292f23SGarrett Wollman /* 1949a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1950a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1951a0292f23SGarrett Wollman * later processing; else drop segment and return. 1952a0292f23SGarrett Wollman */ 1953fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1954a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1955a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1956a0292f23SGarrett Wollman goto step6; 19575e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 19585e1aa279SDavid Malone goto dropafterack; 1959a0292f23SGarrett Wollman else 1960a0292f23SGarrett Wollman goto drop; 1961a0292f23SGarrett Wollman } 1962df8bae1dSRodney W. Grimes 1963df8bae1dSRodney W. Grimes /* 1964df8bae1dSRodney W. Grimes * Ack processing. 1965df8bae1dSRodney W. Grimes */ 1966df8bae1dSRodney W. Grimes switch (tp->t_state) { 1967df8bae1dSRodney W. Grimes 1968df8bae1dSRodney W. Grimes /* 1969764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1970764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1971764d8cefSBill Fenner * The ACK was checked above. 1972df8bae1dSRodney W. Grimes */ 1973df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1974a0292f23SGarrett Wollman 197578b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1976df8bae1dSRodney W. Grimes soisconnected(so); 1977df8bae1dSRodney W. Grimes /* Do window scaling? */ 1978df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1979df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1980df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1981464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1982df8bae1dSRodney W. Grimes } 1983a0292f23SGarrett Wollman /* 1984a0292f23SGarrett Wollman * Make transitions: 1985a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1986a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1987a0292f23SGarrett Wollman */ 19889b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1989a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1990a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1991a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 19927ff19458SPaul Traina } else { 1993a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1994b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 19957ff19458SPaul Traina } 1996a0292f23SGarrett Wollman /* 1997a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1998a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1999a0292f23SGarrett Wollman */ 2000fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2001fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2002a0292f23SGarrett Wollman (struct mbuf *)0); 2003fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 200493b0017fSPhilippe Charnier /* FALLTHROUGH */ 2005df8bae1dSRodney W. Grimes 2006df8bae1dSRodney W. Grimes /* 2007df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2008df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2009fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2010fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2011df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2012df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2013df8bae1dSRodney W. Grimes */ 2014df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2015df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2016df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2017df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2018df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2019df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 20205a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 202178b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 20225a53ca16SPaul Saab goto dropafterack; 20235a53ca16SPaul Saab } 20243529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2025fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2026fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 20275a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 2028fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2029fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 203078b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2031df8bae1dSRodney W. Grimes /* 2032df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2033df8bae1dSRodney W. Grimes * a window probe), this is a completely 2034df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2035df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2036df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2037df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2038df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2039df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2040df8bae1dSRodney W. Grimes * window so we send only this one 2041df8bae1dSRodney W. Grimes * packet. 2042df8bae1dSRodney W. Grimes * 2043df8bae1dSRodney W. Grimes * We know we're losing at the current 2044df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2045df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2046df8bae1dSRodney W. Grimes * and pull our congestion window back to 2047df8bae1dSRodney W. Grimes * the new ssthresh). 2048df8bae1dSRodney W. Grimes * 2049df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2050df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2051df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2052df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2053df8bae1dSRodney W. Grimes * network. 2054f2512ba1SRui Paulo * 2055f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2056f2512ba1SRui Paulo * we reduced the cwnd. 2057df8bae1dSRodney W. Grimes */ 2058b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2059fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2060df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2061cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2062603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 20633529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 20649d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 20653529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 20663529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 2067808f11b7SPaul Saab int awnd; 206825e6f9edSPaul Saab 206925e6f9edSPaul Saab /* 207025e6f9edSPaul Saab * Compute the amount of data in flight first. 207125e6f9edSPaul Saab * We can inject new data into the pipe iff 207225e6f9edSPaul Saab * we have less than 1/2 the original window's 207325e6f9edSPaul Saab * worth of data in flight. 207425e6f9edSPaul Saab */ 2075808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 20760077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2077808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 207825e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 207925e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 208025e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 208125e6f9edSPaul Saab } 208225e6f9edSPaul Saab } else 208346f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 208446f58482SJonathan Lemon (void) tcp_output(tp); 208546f58482SJonathan Lemon goto drop; 2086cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2087cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2088a0445c2eSJayanth Vijayaraghavan 2089a0445c2eSJayanth Vijayaraghavan /* 2090a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2091a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2092a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2093a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2094a0445c2eSJayanth Vijayaraghavan * recovery. 2095a0445c2eSJayanth Vijayaraghavan */ 20963529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2097a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 2098a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2099a0445c2eSJayanth Vijayaraghavan break; 2100a0445c2eSJayanth Vijayaraghavan } 2101603724d3SBjoern A. Zeeb } else if (V_tcp_do_newreno || 2102603724d3SBjoern A. Zeeb V_tcp_do_ecn) { 2103a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 21049d11646dSJeffrey Hsu tp->snd_recover)) { 2105cb942153SJeffrey Hsu tp->t_dupacks = 0; 2106cb942153SJeffrey Hsu break; 210746f58482SJonathan Lemon } 2108a0445c2eSJayanth Vijayaraghavan } 2109f2512ba1SRui Paulo tcp_congestion_exp(tp); 2110b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 21119b8b58e0SJonathan Lemon tp->t_rtttime = 0; 21123529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 211378b50714SRobert Watson TCPSTAT_INC( 211478b50714SRobert Watson tcps_sack_recovery_episode); 2115a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 21168db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 21176d90faf3SPaul Saab (void) tcp_output(tp); 21186d90faf3SPaul Saab goto drop; 21196d90faf3SPaul Saab } 2120fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2121df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2122df8bae1dSRodney W. Grimes (void) tcp_output(tp); 212348d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2124302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2125302ce8d6SAndre Oppermann __func__)); 2126df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 212748d2549cSJeffrey Hsu tp->t_maxseg * 212848d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2129df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2130df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2131df8bae1dSRodney W. Grimes goto drop; 2132603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2133582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 213448d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 213548d2549cSJeffrey Hsu u_int sent; 213689c02376SJeffrey Hsu 2137582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2138582a954bSJeffrey Hsu tp->t_dupacks == 2, 2139302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2140302ce8d6SAndre Oppermann __func__)); 214161a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 214248d2549cSJeffrey Hsu tp->snd_limited = 0; 214361a36e3dSJeffrey Hsu tp->snd_cwnd = 214461a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 214561a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 214661a36e3dSJeffrey Hsu tp->t_maxseg; 2147582a954bSJeffrey Hsu (void) tcp_output(tp); 214848d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 214948d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 215089c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 215189c02376SJeffrey Hsu tp->snd_limited == 0) || 215289c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 215389c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2154302ce8d6SAndre Oppermann ("%s: sent too much", 2155302ce8d6SAndre Oppermann __func__)); 215648d2549cSJeffrey Hsu tp->snd_limited = 2; 215748d2549cSJeffrey Hsu } else if (sent > 0) 215848d2549cSJeffrey Hsu ++tp->snd_limited; 2159582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2160582a954bSJeffrey Hsu goto drop; 2161df8bae1dSRodney W. Grimes } 2162df8bae1dSRodney W. Grimes } else 2163df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2164df8bae1dSRodney W. Grimes break; 2165df8bae1dSRodney W. Grimes } 2166cb942153SJeffrey Hsu 2167302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2168302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2169cb942153SJeffrey Hsu 2170df8bae1dSRodney W. Grimes /* 2171df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2172df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2173df8bae1dSRodney W. Grimes */ 2174603724d3SBjoern A. Zeeb if (V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 21759d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 2176cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 21773529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 21786d90faf3SPaul Saab tcp_sack_partialack(tp, th); 21796d90faf3SPaul Saab else 2180c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2181c068736aSJeffrey Hsu } else { 2182c068736aSJeffrey Hsu /* 21836d90faf3SPaul Saab * Out of fast recovery. 2184c068736aSJeffrey Hsu * Window inflation should have left us 2185c068736aSJeffrey Hsu * with approximately snd_ssthresh 2186c068736aSJeffrey Hsu * outstanding data. 2187c068736aSJeffrey Hsu * But in case we would be inclined to 2188c068736aSJeffrey Hsu * send a burst, better to do it via 2189c068736aSJeffrey Hsu * the slow start mechanism. 2190c068736aSJeffrey Hsu */ 2191c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 2192c068736aSJeffrey Hsu tp->snd_ssthresh, 2193c068736aSJeffrey Hsu tp->snd_max)) 2194c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 2195c068736aSJeffrey Hsu th->th_ack + 2196c068736aSJeffrey Hsu tp->t_maxseg; 2197c068736aSJeffrey Hsu else 2198c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 2199c068736aSJeffrey Hsu } 2200c068736aSJeffrey Hsu } 2201c068736aSJeffrey Hsu } else { 2202233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 2203df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 2204df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 220546f58482SJonathan Lemon } 2206cb942153SJeffrey Hsu tp->t_dupacks = 0; 2207a0292f23SGarrett Wollman /* 2208a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2209a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2210a0292f23SGarrett Wollman */ 2211a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2212a0292f23SGarrett Wollman /* 2213a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2214a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 221507e43e10SAndras Olah * synchronized). Go to non-starred state, 221607e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 221707e43e10SAndras Olah * we can do window scaling. 2218a0292f23SGarrett Wollman */ 2219a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2220a0292f23SGarrett Wollman tp->snd_una++; 222107e43e10SAndras Olah /* Do window scaling? */ 222207e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 222307e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 222407e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2225464fcfbcSAndre Oppermann /* Send window already scaled. */ 222607e43e10SAndras Olah } 2227a0292f23SGarrett Wollman } 2228a0292f23SGarrett Wollman 2229a0292f23SGarrett Wollman process_ACK: 2230252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2231252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2232252ca428SRobert Watson ("tcp_input: process_ACK ti_locked %d", ti_locked)); 22338501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 22347cfc6904SRobert Watson 2235fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 223678b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 223778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2238df8bae1dSRodney W. Grimes 2239df8bae1dSRodney W. Grimes /* 22409b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 22419b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 22429b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 22439b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 22449b8b58e0SJonathan Lemon * we left off. 22459b8b58e0SJonathan Lemon */ 22466dfb8b31SJohn Baldwin if (tp->t_rxtshift == 1 && (int)(ticks - tp->t_badrxtwin) < 0) { 224778b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitbad); 22489b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 22499b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 22509d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 22519d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 22529d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 22539b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 22549b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 22559b8b58e0SJonathan Lemon } 22569b8b58e0SJonathan Lemon 22579b8b58e0SJonathan Lemon /* 2258df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2259df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2260df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2261df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2262df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2263df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2264df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2265fa55172bSMatthew Dillon * 2266fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2267fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2268fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2269fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2270df8bae1dSRodney W. Grimes */ 2271fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2272fa55172bSMatthew Dillon to.to_tsecr) { 2273eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2274eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 22759b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2276fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2277eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2278eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 22799b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2280fa55172bSMatthew Dillon } 22811fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2282df8bae1dSRodney W. Grimes 2283df8bae1dSRodney W. Grimes /* 2284df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2285df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2286df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2287df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2288df8bae1dSRodney W. Grimes */ 2289fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2290b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2291df8bae1dSRodney W. Grimes needoutput = 1; 2292b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2293b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2294a0292f23SGarrett Wollman 2295a0292f23SGarrett Wollman /* 2296a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2297a0292f23SGarrett Wollman * skip rest of ACK processing. 2298a0292f23SGarrett Wollman */ 2299a0292f23SGarrett Wollman if (acked == 0) 2300a0292f23SGarrett Wollman goto step6; 2301a0292f23SGarrett Wollman 2302df8bae1dSRodney W. Grimes /* 2303df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 230424cb0f22SLawrence Stewart * Method depends on which congestion control state we're 230524cb0f22SLawrence Stewart * in (slow start or cong avoid) and if ABC (RFC 3465) is 230624cb0f22SLawrence Stewart * enabled. 230724cb0f22SLawrence Stewart * 230824cb0f22SLawrence Stewart * slow start: cwnd <= ssthresh 230924cb0f22SLawrence Stewart * cong avoid: cwnd > ssthresh 231024cb0f22SLawrence Stewart * 231124cb0f22SLawrence Stewart * slow start and ABC (RFC 3465): 231224cb0f22SLawrence Stewart * Grow cwnd exponentially by the amount of data 231324cb0f22SLawrence Stewart * ACKed capping the max increment per ACK to 231424cb0f22SLawrence Stewart * (abc_l_var * maxseg) bytes. 231524cb0f22SLawrence Stewart * 231624cb0f22SLawrence Stewart * slow start without ABC (RFC 2581): 231724cb0f22SLawrence Stewart * Grow cwnd exponentially by maxseg per ACK. 231824cb0f22SLawrence Stewart * 231924cb0f22SLawrence Stewart * cong avoid and ABC (RFC 3465): 232024cb0f22SLawrence Stewart * Grow cwnd linearly by maxseg per RTT for each 232124cb0f22SLawrence Stewart * cwnd worth of ACKed data. 232224cb0f22SLawrence Stewart * 232324cb0f22SLawrence Stewart * cong avoid without ABC (RFC 2581): 232424cb0f22SLawrence Stewart * Grow cwnd linearly by approximately maxseg per RTT using 232524cb0f22SLawrence Stewart * maxseg^2 / cwnd per ACK as the increment. 232624cb0f22SLawrence Stewart * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to 232724cb0f22SLawrence Stewart * avoid capping cwnd. 2328df8bae1dSRodney W. Grimes */ 2329603724d3SBjoern A. Zeeb if ((!V_tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 23306d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2331ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2332ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 233324cb0f22SLawrence Stewart /* In congestion avoidance? */ 233424cb0f22SLawrence Stewart if (cw > tp->snd_ssthresh) { 233524cb0f22SLawrence Stewart if (V_tcp_do_rfc3465) { 233624cb0f22SLawrence Stewart tp->t_bytes_acked += acked; 233724cb0f22SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) 233824cb0f22SLawrence Stewart tp->t_bytes_acked -= cw; 233924cb0f22SLawrence Stewart else 234024cb0f22SLawrence Stewart incr = 0; 234124cb0f22SLawrence Stewart } 234224cb0f22SLawrence Stewart else 2343c10eb6d1SBjoern A. Zeeb incr = max((incr * incr / cw), 1); 234424cb0f22SLawrence Stewart /* 234524cb0f22SLawrence Stewart * In slow-start with ABC enabled and no RTO in sight? 234624cb0f22SLawrence Stewart * (Must not use abc_l_var > 1 if slow starting after an 234724cb0f22SLawrence Stewart * RTO. On RTO, snd_nxt = snd_una, so the snd_nxt == 234824cb0f22SLawrence Stewart * snd_max check is sufficient to handle this). 234924cb0f22SLawrence Stewart */ 235024cb0f22SLawrence Stewart } else if (V_tcp_do_rfc3465 && 235124cb0f22SLawrence Stewart tp->snd_nxt == tp->snd_max) 235224cb0f22SLawrence Stewart incr = min(acked, 235324cb0f22SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 235424cb0f22SLawrence Stewart /* ABC is on by default, so (incr == 0) frequently. */ 235524cb0f22SLawrence Stewart if (incr > 0) 2356df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2357df8bae1dSRodney W. Grimes } 23585905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2359df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2360df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 23615905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2362df8bae1dSRodney W. Grimes ourfinisacked = 1; 2363df8bae1dSRodney W. Grimes } else { 23645905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2365df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2366df8bae1dSRodney W. Grimes ourfinisacked = 0; 2367df8bae1dSRodney W. Grimes } 2368564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 23691e4d7da7SRobert Watson sowwakeup_locked(so); 2370e8949f74SAndre Oppermann /* Detect una wraparound. */ 2371603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23726d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 23739d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 23749d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 23759d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2376603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23776d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 237824cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 23799d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 238024cb0f22SLawrence Stewart tp->t_bytes_acked = 0; 238124cb0f22SLawrence Stewart } 2382fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 23833529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 23846d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 23856d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 23866d90faf3SPaul Saab } 2387df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2388df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2389df8bae1dSRodney W. Grimes 2390df8bae1dSRodney W. Grimes switch (tp->t_state) { 2391df8bae1dSRodney W. Grimes 2392df8bae1dSRodney W. Grimes /* 2393df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2394df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2395df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2396df8bae1dSRodney W. Grimes */ 2397df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2398df8bae1dSRodney W. Grimes if (ourfinisacked) { 2399df8bae1dSRodney W. Grimes /* 2400df8bae1dSRodney W. Grimes * If we can't receive any more 2401df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2402df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2403df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2404df8bae1dSRodney W. Grimes * we'll hang forever. 2405e8949f74SAndre Oppermann * 2406e8949f74SAndre Oppermann * XXXjl: 2407340c35deSJonathan Lemon * we should release the tp also, and use a 2408340c35deSJonathan Lemon * compressed state. 2409340c35deSJonathan Lemon */ 2410c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 24117c72af87SMohan Srinivasan int timeout; 24127c72af87SMohan Srinivasan 241303e49181SSeigo Tanimura soisdisconnected(so); 24147c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 24157c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2416b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 24174cc20ab1SSeigo Tanimura } 2418df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2419df8bae1dSRodney W. Grimes } 2420df8bae1dSRodney W. Grimes break; 2421df8bae1dSRodney W. Grimes 2422df8bae1dSRodney W. Grimes /* 2423df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2424df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2425df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2426df8bae1dSRodney W. Grimes * the segment. 2427df8bae1dSRodney W. Grimes */ 2428df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2429df8bae1dSRodney W. Grimes if (ourfinisacked) { 2430252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 243111a20fb8SJeffrey Hsu tcp_twstart(tp); 2432603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2433340c35deSJonathan Lemon m_freem(m); 2434679d9708SAndre Oppermann return; 2435df8bae1dSRodney W. Grimes } 2436df8bae1dSRodney W. Grimes break; 2437df8bae1dSRodney W. Grimes 2438df8bae1dSRodney W. Grimes /* 2439df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2440df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2441df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2442df8bae1dSRodney W. Grimes * enter the closed state and return. 2443df8bae1dSRodney W. Grimes */ 2444df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2445df8bae1dSRodney W. Grimes if (ourfinisacked) { 2446252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2447df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2448df8bae1dSRodney W. Grimes goto drop; 2449df8bae1dSRodney W. Grimes } 2450df8bae1dSRodney W. Grimes break; 2451df8bae1dSRodney W. Grimes } 2452df8bae1dSRodney W. Grimes } 2453df8bae1dSRodney W. Grimes 2454df8bae1dSRodney W. Grimes step6: 2455252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2456252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2457252ca428SRobert Watson ("tcp_do_segment: step6 ti_locked %d", ti_locked)); 24588501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 24597cfc6904SRobert Watson 2460df8bae1dSRodney W. Grimes /* 2461df8bae1dSRodney W. Grimes * Update window information. 2462df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2463df8bae1dSRodney W. Grimes */ 2464fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2465fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2466fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2467fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2468df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2469fb59c426SYoshinobu Inoue if (tlen == 0 && 2470fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 247178b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2472df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2473fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2474fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2475df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2476df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2477df8bae1dSRodney W. Grimes needoutput = 1; 2478df8bae1dSRodney W. Grimes } 2479df8bae1dSRodney W. Grimes 2480df8bae1dSRodney W. Grimes /* 2481df8bae1dSRodney W. Grimes * Process segments with URG. 2482df8bae1dSRodney W. Grimes */ 2483fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2484df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2485df8bae1dSRodney W. Grimes /* 2486df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2487df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2488df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2489df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2490df8bae1dSRodney W. Grimes */ 249118ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2492fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2493fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2494fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 249518ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2496df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2497df8bae1dSRodney W. Grimes } 2498df8bae1dSRodney W. Grimes /* 2499df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2500df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2501df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2502df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2503df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2504df8bae1dSRodney W. Grimes * 2505df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2506df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2507df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2508df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2509df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2510df8bae1dSRodney W. Grimes * spec states (in one of two places). 2511df8bae1dSRodney W. Grimes */ 2512fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2513fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2514df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2515df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2516927c5ceaSRobert Watson if (so->so_oobmark == 0) 2517c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2518df8bae1dSRodney W. Grimes sohasoutofband(so); 2519df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2520df8bae1dSRodney W. Grimes } 252118ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2522df8bae1dSRodney W. Grimes /* 2523df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2524df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2525df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2526df8bae1dSRodney W. Grimes * data may creep in... ick. 2527df8bae1dSRodney W. Grimes */ 252830613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 252930613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 253030613f56SJeffrey Hsu /* hdr drop is delayed */ 253130613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 253230613f56SJeffrey Hsu } 2533c068736aSJeffrey Hsu } else { 2534df8bae1dSRodney W. Grimes /* 2535df8bae1dSRodney W. Grimes * If no out of band data is expected, 2536df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2537df8bae1dSRodney W. Grimes * with the receive window. 2538df8bae1dSRodney W. Grimes */ 2539df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2540df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2541c068736aSJeffrey Hsu } 2542df8bae1dSRodney W. Grimes dodata: /* XXX */ 2543252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2544252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2545252ca428SRobert Watson ("tcp_do_segment: dodata ti_locked %d", ti_locked)); 25468501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25477cfc6904SRobert Watson 2548df8bae1dSRodney W. Grimes /* 2549df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2550df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2551df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2552df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2553df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2554df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2555df8bae1dSRodney W. Grimes */ 2556fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2557df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 255869e03620SPaul Saab tcp_seq save_start = th->th_seq; 2559fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2560e4b64281SJesper Skriver /* 2561c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2562c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2563c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2564c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2565c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2566c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2567c068736aSJeffrey Hsu * conversions. 2568c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2569c068736aSJeffrey Hsu * immediately when segments are out of order (so 2570c068736aSJeffrey Hsu * fast retransmit can work). 2571e4b64281SJesper Skriver */ 2572e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2573e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2574e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2575e4b64281SJesper Skriver if (DELAY_ACK(tp)) 25763bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2577e4b64281SJesper Skriver else 2578e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2579e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2580e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 258178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 258278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 2583e4b64281SJesper Skriver ND6_HINT(tp); 25841e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2585c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2586c1c36a2cSMike Silbersack m_freem(m); 2587c1c36a2cSMike Silbersack else 25881e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2589e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 25901e4d7da7SRobert Watson sorwakeup_locked(so); 2591e4b64281SJesper Skriver } else { 2592e8949f74SAndre Oppermann /* 2593e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2594e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2595e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2596e8949f74SAndre Oppermann * when trimming from the head. 2597e8949f74SAndre Oppermann */ 2598e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2599e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2600e4b64281SJesper Skriver } 26013529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2602f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2603302ce8d6SAndre Oppermann #if 0 2604df8bae1dSRodney W. Grimes /* 2605df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2606df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2607df8bae1dSRodney W. Grimes * buffer size. 2608302ce8d6SAndre Oppermann * XXX: Unused. 2609df8bae1dSRodney W. Grimes */ 2610df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2611302ce8d6SAndre Oppermann #endif 2612df8bae1dSRodney W. Grimes } else { 2613df8bae1dSRodney W. Grimes m_freem(m); 2614fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2615df8bae1dSRodney W. Grimes } 2616df8bae1dSRodney W. Grimes 2617df8bae1dSRodney W. Grimes /* 2618df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2619df8bae1dSRodney W. Grimes * that the connection is closing. 2620df8bae1dSRodney W. Grimes */ 2621fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2622df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2623df8bae1dSRodney W. Grimes socantrcvmore(so); 2624a0292f23SGarrett Wollman /* 2625a0292f23SGarrett Wollman * If connection is half-synchronized 2626d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2627a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2628a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2629a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2630a0292f23SGarrett Wollman */ 26313bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 26323bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2633a0292f23SGarrett Wollman else 2634df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2635df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2636df8bae1dSRodney W. Grimes } 2637df8bae1dSRodney W. Grimes switch (tp->t_state) { 2638df8bae1dSRodney W. Grimes 2639df8bae1dSRodney W. Grimes /* 2640df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2641df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2642df8bae1dSRodney W. Grimes */ 2643df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 26449b8b58e0SJonathan Lemon tp->t_starttime = ticks; 26459b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2646df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2647df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2648df8bae1dSRodney W. Grimes break; 2649df8bae1dSRodney W. Grimes 2650df8bae1dSRodney W. Grimes /* 2651df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2652df8bae1dSRodney W. Grimes * enter the CLOSING state. 2653df8bae1dSRodney W. Grimes */ 2654df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2655df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2656df8bae1dSRodney W. Grimes break; 2657df8bae1dSRodney W. Grimes 2658df8bae1dSRodney W. Grimes /* 2659df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2660df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2661df8bae1dSRodney W. Grimes * standard timers. 2662df8bae1dSRodney W. Grimes */ 2663df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2664252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2665252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2666252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2667252ca428SRobert Watson ti_locked)); 2668252ca428SRobert Watson 2669340c35deSJonathan Lemon tcp_twstart(tp); 2670603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2671679d9708SAndre Oppermann return; 2672df8bae1dSRodney W. Grimes } 2673df8bae1dSRodney W. Grimes } 2674252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2675252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2676252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2677603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2678252ca428SRobert Watson else 2679252ca428SRobert Watson panic("%s: dodata epilogue ti_locked %d", __func__, 2680252ca428SRobert Watson ti_locked); 2681252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2682252ca428SRobert Watson 2683610ee2f9SDavid Greenman #ifdef TCPDEBUG 26844cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2685fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2686fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2687610ee2f9SDavid Greenman #endif 2688df8bae1dSRodney W. Grimes 2689df8bae1dSRodney W. Grimes /* 2690df8bae1dSRodney W. Grimes * Return any desired output. 2691df8bae1dSRodney W. Grimes */ 2692df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2693df8bae1dSRodney W. Grimes (void) tcp_output(tp); 26947792ea27SJeffrey Hsu 2695a14c749fSJonathan Lemon check_delack: 2696252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2697252ca428SRobert Watson __func__, ti_locked)); 2698603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 26998501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2700252ca428SRobert Watson 2701a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 27023bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2703b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 27043bfd6421SJonathan Lemon } 27058501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2706679d9708SAndre Oppermann return; 2707df8bae1dSRodney W. Grimes 2708df8bae1dSRodney W. Grimes dropafterack: 2709252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2710252ca428SRobert Watson ("tcp_do_segment: dropafterack ti_locked %d", ti_locked)); 2711252ca428SRobert Watson 2712df8bae1dSRodney W. Grimes /* 2713df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2714df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 271580ab7c0eSGarrett Wollman * 271680ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 271780ab7c0eSGarrett Wollman * paths to this code happen after packets containing 271880ab7c0eSGarrett Wollman * RST have been dropped. 271980ab7c0eSGarrett Wollman * 272080ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 272180ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 272280ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 272380ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 272480ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 272580ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2726df8bae1dSRodney W. Grimes */ 2727fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2728fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2729a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2730a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2731a57815efSBosko Milekic goto dropwithreset; 2732a57815efSBosko Milekic } 2733a0292f23SGarrett Wollman #ifdef TCPDEBUG 27344cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2735fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2736fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2737a0292f23SGarrett Wollman #endif 2738252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2739252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2740252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2741603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2742252ca428SRobert Watson else 2743252ca428SRobert Watson panic("%s: dropafterack epilogue ti_locked %d", __func__, 2744252ca428SRobert Watson ti_locked); 2745252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2746252ca428SRobert Watson 2747df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2748df8bae1dSRodney W. Grimes (void) tcp_output(tp); 27498501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2750d6915262SRobert Watson m_freem(m); 2751679d9708SAndre Oppermann return; 2752df8bae1dSRodney W. Grimes 2753df8bae1dSRodney W. Grimes dropwithreset: 2754252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2755252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2756252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2757dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2758252ca428SRobert Watson else 2759252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 2760252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2761a57815efSBosko Milekic 2762a0ca0871SRobert Watson if (tp != NULL) { 2763302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 27648501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2765dd8ac7f9SRobert Watson } else 2766a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2767679d9708SAndre Oppermann return; 2768df8bae1dSRodney W. Grimes 2769df8bae1dSRodney W. Grimes drop: 2770252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2771252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2772252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2773252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2774252ca428SRobert Watson #ifdef INVARIANTS 2775252ca428SRobert Watson else 2776252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2777252ca428SRobert Watson #endif 2778252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2779252ca428SRobert Watson 2780df8bae1dSRodney W. Grimes /* 2781df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2782df8bae1dSRodney W. Grimes */ 2783610ee2f9SDavid Greenman #ifdef TCPDEBUG 27841c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2785fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2786fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2787a0292f23SGarrett Wollman #endif 27881c53f806SRobert Watson if (tp != NULL) 27898501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2790d6915262SRobert Watson m_freem(m); 2791302ce8d6SAndre Oppermann } 2792302ce8d6SAndre Oppermann 2793302ce8d6SAndre Oppermann /* 27940ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 27950ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 27960ca3f933SAndre Oppermann * tp may be NULL. 2797302ce8d6SAndre Oppermann */ 2798302ce8d6SAndre Oppermann static void 2799302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2800302ce8d6SAndre Oppermann int tlen, int rstreason) 2801302ce8d6SAndre Oppermann { 2802302ce8d6SAndre Oppermann struct ip *ip; 2803302ce8d6SAndre Oppermann #ifdef INET6 2804302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2805302ce8d6SAndre Oppermann #endif 28067a3244ccSRobert Watson 28077a3244ccSRobert Watson if (tp != NULL) { 28088501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28097a3244ccSRobert Watson } 28107a3244ccSRobert Watson 28110ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2812302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2813302ce8d6SAndre Oppermann goto drop; 2814302ce8d6SAndre Oppermann #ifdef INET6 2815302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2816302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2817302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2818302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2819302ce8d6SAndre Oppermann goto drop; 2820302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2821302ce8d6SAndre Oppermann } else 2822302ce8d6SAndre Oppermann #endif 2823302ce8d6SAndre Oppermann { 2824302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2825302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2826302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2827302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2828302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2829302ce8d6SAndre Oppermann goto drop; 2830302ce8d6SAndre Oppermann } 2831302ce8d6SAndre Oppermann 2832302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2833302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2834302ce8d6SAndre Oppermann goto drop; 2835302ce8d6SAndre Oppermann 2836302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2837302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2838302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2839302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2840302ce8d6SAndre Oppermann } else { 2841302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2842302ce8d6SAndre Oppermann tlen++; 2843302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2844302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2845302ce8d6SAndre Oppermann } 2846302ce8d6SAndre Oppermann return; 2847302ce8d6SAndre Oppermann drop: 2848302ce8d6SAndre Oppermann m_freem(m); 2849df8bae1dSRodney W. Grimes } 2850df8bae1dSRodney W. Grimes 2851be2ac88cSJonathan Lemon /* 2852be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2853be2ac88cSJonathan Lemon */ 28540312fbe9SPoul-Henning Kamp static void 2855ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2856df8bae1dSRodney W. Grimes { 2857df8bae1dSRodney W. Grimes int opt, optlen; 2858df8bae1dSRodney W. Grimes 2859be2ac88cSJonathan Lemon to->to_flags = 0; 2860df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2861df8bae1dSRodney W. Grimes opt = cp[0]; 2862df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2863df8bae1dSRodney W. Grimes break; 2864df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2865df8bae1dSRodney W. Grimes optlen = 1; 2866df8bae1dSRodney W. Grimes else { 2867b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2868b474779fSJun-ichiro itojun Hagino break; 2869df8bae1dSRodney W. Grimes optlen = cp[1]; 2870b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2871df8bae1dSRodney W. Grimes break; 2872df8bae1dSRodney W. Grimes } 2873df8bae1dSRodney W. Grimes switch (opt) { 2874df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2875df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2876df8bae1dSRodney W. Grimes continue; 2877f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2878df8bae1dSRodney W. Grimes continue; 2879be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2880be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2881be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2882fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2883df8bae1dSRodney W. Grimes break; 2884df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2885df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2886df8bae1dSRodney W. Grimes continue; 2887f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2888df8bae1dSRodney W. Grimes continue; 2889be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 289002a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2891df8bae1dSRodney W. Grimes break; 2892df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2893df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2894df8bae1dSRodney W. Grimes continue; 2895be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2896a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2897a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2898fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2899a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2900a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2901fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2902df8bae1dSRodney W. Grimes break; 29031cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 29041cfd4b53SBruce M Simpson /* 29051cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 29061cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 29071cfd4b53SBruce M Simpson * record the fact that the option was observed here 29081cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 29091cfd4b53SBruce M Simpson */ 29101cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 29111cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 29121cfd4b53SBruce M Simpson continue; 2913df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2914df47e437SAndre Oppermann to->to_signature = cp + 2; 29151cfd4b53SBruce M Simpson break; 2916265ed012SBruce M Simpson #endif 29176d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2918f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 29196d90faf3SPaul Saab continue; 2920f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2921f72167f4SAndre Oppermann continue; 2922603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 2923f72167f4SAndre Oppermann continue; 2924fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 29256d90faf3SPaul Saab break; 29266d90faf3SPaul Saab case TCPOPT_SACK: 29275a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 29286d90faf3SPaul Saab continue; 2929b728e902SAndre Oppermann if (flags & TO_SYN) 2930b728e902SAndre Oppermann continue; 2931fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 29325a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 29335a53ca16SPaul Saab to->to_sacks = cp + 2; 293478b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 29356d90faf3SPaul Saab break; 2936be2ac88cSJonathan Lemon default: 2937be2ac88cSJonathan Lemon continue; 2938df8bae1dSRodney W. Grimes } 2939df8bae1dSRodney W. Grimes } 2940df8bae1dSRodney W. Grimes } 2941df8bae1dSRodney W. Grimes 2942df8bae1dSRodney W. Grimes /* 2943df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2944df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2945df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2946df8bae1dSRodney W. Grimes * sequencing purposes. 2947df8bae1dSRodney W. Grimes */ 29480312fbe9SPoul-Henning Kamp static void 2949ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2950ad3f9ab3SAndre Oppermann int off) 2951df8bae1dSRodney W. Grimes { 2952fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2953df8bae1dSRodney W. Grimes 2954df8bae1dSRodney W. Grimes while (cnt >= 0) { 2955df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2956df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2957df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2958df8bae1dSRodney W. Grimes 29598501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29607a3244ccSRobert Watson 2961df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2962df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2963df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2964df8bae1dSRodney W. Grimes m->m_len--; 296569a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 296669a34685SYoshinobu Inoue m->m_pkthdr.len--; 2967df8bae1dSRodney W. Grimes return; 2968df8bae1dSRodney W. Grimes } 2969df8bae1dSRodney W. Grimes cnt -= m->m_len; 2970df8bae1dSRodney W. Grimes m = m->m_next; 29714dfdffe9SAndre Oppermann if (m == NULL) 2972df8bae1dSRodney W. Grimes break; 2973df8bae1dSRodney W. Grimes } 2974df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2975df8bae1dSRodney W. Grimes } 2976df8bae1dSRodney W. Grimes 2977df8bae1dSRodney W. Grimes /* 2978df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2979df8bae1dSRodney W. Grimes * and update averages and current timeout. 2980df8bae1dSRodney W. Grimes */ 29810312fbe9SPoul-Henning Kamp static void 2982ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2983df8bae1dSRodney W. Grimes { 2984ad3f9ab3SAndre Oppermann int delta; 2985233e8c18SGarrett Wollman 29868501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29872be3bf22SRobert Watson 298878b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 2989233e8c18SGarrett Wollman tp->t_rttupdated++; 2990233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2991233e8c18SGarrett Wollman /* 2992233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2993233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2994233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2995233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2996233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2997233e8c18SGarrett Wollman */ 2998233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2999233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3000233e8c18SGarrett Wollman 3001233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3002233e8c18SGarrett Wollman tp->t_srtt = 1; 3003233e8c18SGarrett Wollman 3004233e8c18SGarrett Wollman /* 3005233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3006233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3007233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3008233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3009233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3010233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3011233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3012233e8c18SGarrett Wollman * rfc793's wired-in beta. 3013233e8c18SGarrett Wollman */ 3014233e8c18SGarrett Wollman if (delta < 0) 3015233e8c18SGarrett Wollman delta = -delta; 3016233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3017233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3018233e8c18SGarrett Wollman tp->t_rttvar = 1; 30191fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 30201fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3021233e8c18SGarrett Wollman } else { 3022233e8c18SGarrett Wollman /* 3023233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3024233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3025233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3026233e8c18SGarrett Wollman */ 3027233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3028233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 30291fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3030233e8c18SGarrett Wollman } 30319b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3032df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3033df8bae1dSRodney W. Grimes 3034df8bae1dSRodney W. Grimes /* 3035df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3036df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3037df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3038df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3039df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3040df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3041df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3042df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3043df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3044df8bae1dSRodney W. Grimes */ 3045233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 30469e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3047df8bae1dSRodney W. Grimes 3048df8bae1dSRodney W. Grimes /* 3049df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3050df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3051df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3052df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3053df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3054df8bae1dSRodney W. Grimes */ 3055df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3056df8bae1dSRodney W. Grimes } 3057df8bae1dSRodney W. Grimes 3058df8bae1dSRodney W. Grimes /* 3059df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3060df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 3061df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 3062df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 3063df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3064df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 3065df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3066df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3067df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3068df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3069df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3070df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3071df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3072a0292f23SGarrett Wollman * 3073a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3074a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3075a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3076a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3077a0292f23SGarrett Wollman * data in maxopd. 3078a0292f23SGarrett Wollman * 3079a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 3080a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 3081a0292f23SGarrett Wollman * MSS of our peer. 308297d8d152SAndre Oppermann * 308397d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 308497d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3085df8bae1dSRodney W. Grimes */ 3086a0292f23SGarrett Wollman void 308791d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, 308891d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3089df8bae1dSRodney W. Grimes { 30903cee92e0SBjoern A. Zeeb int mss; 309197d8d152SAndre Oppermann u_long maxmtu; 3092c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 309397d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3094a0292f23SGarrett Wollman int origoffer = offer; 3095fb59c426SYoshinobu Inoue #ifdef INET6 3096c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3097c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3098c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3099c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3100c068736aSJeffrey Hsu #else 3101c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3102fb59c426SYoshinobu Inoue #endif 3103df8bae1dSRodney W. Grimes 31048501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31057a3244ccSRobert Watson 3106e8949f74SAndre Oppermann /* Initialize. */ 310797d8d152SAndre Oppermann #ifdef INET6 310897d8d152SAndre Oppermann if (isipv6) { 310991d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3110603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 311197d8d152SAndre Oppermann } else 311297d8d152SAndre Oppermann #endif 311397d8d152SAndre Oppermann { 311491d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3115603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3116df8bae1dSRodney W. Grimes } 3117df8bae1dSRodney W. Grimes 311897d8d152SAndre Oppermann /* 3119e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 312097d8d152SAndre Oppermann */ 31216f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 31226f01cac6SBjoern A. Zeeb /* 31238e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 31246f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 31256f01cac6SBjoern A. Zeeb * if there was no cache hit. 31266f01cac6SBjoern A. Zeeb */ 31276f01cac6SBjoern A. Zeeb if (metricptr != NULL) 31286f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 312997d8d152SAndre Oppermann return; 31306f01cac6SBjoern A. Zeeb } 313197d8d152SAndre Oppermann 3132e8949f74SAndre Oppermann /* What have we got? */ 313397d8d152SAndre Oppermann switch (offer) { 313497d8d152SAndre Oppermann case 0: 313597d8d152SAndre Oppermann /* 313697d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3137c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3138c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 313997d8d152SAndre Oppermann */ 3140c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 314197d8d152SAndre Oppermann break; 314297d8d152SAndre Oppermann 314397d8d152SAndre Oppermann case -1: 3144a0292f23SGarrett Wollman /* 3145c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3146a0292f23SGarrett Wollman */ 314797d8d152SAndre Oppermann /* FALLTHROUGH */ 314897d8d152SAndre Oppermann 314997d8d152SAndre Oppermann default: 3150a0292f23SGarrett Wollman /* 315153369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 315253369ac9SAndre Oppermann * to at least minmss. 315353369ac9SAndre Oppermann */ 3154603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 315597d8d152SAndre Oppermann } 3156a0292f23SGarrett Wollman 3157df8bae1dSRodney W. Grimes /* 3158e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3159df8bae1dSRodney W. Grimes */ 316097d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 31613cee92e0SBjoern A. Zeeb if (metricptr != NULL) 31623cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 316397d8d152SAndre Oppermann 3164df8bae1dSRodney W. Grimes /* 3165e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3166fb59c426SYoshinobu Inoue * else, use the link mtu. 3167df8bae1dSRodney W. Grimes */ 316897d8d152SAndre Oppermann if (metrics.rmx_mtu) 31692d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3170c068736aSJeffrey Hsu else { 317131b3783cSHajimu UMEMOTO #ifdef INET6 3172fb59c426SYoshinobu Inoue if (isipv6) { 317397d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3174603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 317597d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3176603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3177b3399803SHajimu UMEMOTO } else 3178b3399803SHajimu UMEMOTO #endif 317997d8d152SAndre Oppermann { 318097d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3181603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 318297d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3183603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3184a0292f23SGarrett Wollman } 31853cee92e0SBjoern A. Zeeb /* 31863cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 31873cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 31883cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 31893cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 31903cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 31913cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 31923cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 31933cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 31943cee92e0SBjoern A. Zeeb * this under the carpet. 31953cee92e0SBjoern A. Zeeb * 31963cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 31973cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 31983cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 31993cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 32003cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 32013cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 32023cee92e0SBjoern A. Zeeb */ 320397d8d152SAndre Oppermann } 3204a0292f23SGarrett Wollman mss = min(mss, offer); 320597d8d152SAndre Oppermann 3206a0292f23SGarrett Wollman /* 32073cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 32083cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 32093cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 32103cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 32113cee92e0SBjoern A. Zeeb */ 32123cee92e0SBjoern A. Zeeb mss = max(mss, 64); 32133cee92e0SBjoern A. Zeeb 32143cee92e0SBjoern A. Zeeb /* 3215a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3216a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3217a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3218a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3219a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3220a0292f23SGarrett Wollman */ 3221a0292f23SGarrett Wollman tp->t_maxopd = mss; 3222a0292f23SGarrett Wollman 3223a0292f23SGarrett Wollman /* 3224e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3225c94c54e4SAndre Oppermann * In this case we just guess. 3226a0292f23SGarrett Wollman */ 3227a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3228a0292f23SGarrett Wollman (origoffer == -1 || 3229a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3230a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3231a0292f23SGarrett Wollman 3232df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3233df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3234df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3235df8bae1dSRodney W. Grimes #else 3236df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3237df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3238df8bae1dSRodney W. Grimes #endif 323997d8d152SAndre Oppermann tp->t_maxseg = mss; 32403cee92e0SBjoern A. Zeeb } 32413cee92e0SBjoern A. Zeeb 32423cee92e0SBjoern A. Zeeb void 32433cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 32443cee92e0SBjoern A. Zeeb { 32453cee92e0SBjoern A. Zeeb int rtt, mss; 32463cee92e0SBjoern A. Zeeb u_long bufsize; 32473cee92e0SBjoern A. Zeeb struct inpcb *inp; 32483cee92e0SBjoern A. Zeeb struct socket *so; 32493cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 325091d6cfa6SBjoern A. Zeeb int mtuflags = 0; 32513cee92e0SBjoern A. Zeeb #ifdef INET6 32523cee92e0SBjoern A. Zeeb int isipv6; 32533cee92e0SBjoern A. Zeeb #endif 32543cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 32553cee92e0SBjoern A. Zeeb 325691d6cfa6SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics, &mtuflags); 32573cee92e0SBjoern A. Zeeb 32583cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 32593cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 32603cee92e0SBjoern A. Zeeb #ifdef INET6 32613cee92e0SBjoern A. Zeeb isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 32623cee92e0SBjoern A. Zeeb #endif 326397d8d152SAndre Oppermann 3264df8bae1dSRodney W. Grimes /* 326597d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 326697d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 326797d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 326897d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 326997d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3270df8bae1dSRodney W. Grimes */ 3271c3b02504SBjoern A. Zeeb so = inp->inp_socket; 32723f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 327397d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 327497d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 327597d8d152SAndre Oppermann else 3276df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3277df8bae1dSRodney W. Grimes if (bufsize < mss) 3278df8bae1dSRodney W. Grimes mss = bufsize; 3279df8bae1dSRodney W. Grimes else { 3280df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3281df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3282df8bae1dSRodney W. Grimes bufsize = sb_max; 328388c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 32843f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3285df8bae1dSRodney W. Grimes } 32863f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3287df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3288df8bae1dSRodney W. Grimes 32893f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 329097d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 329197d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 329297d8d152SAndre Oppermann else 3293df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3294df8bae1dSRodney W. Grimes if (bufsize > mss) { 3295df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3296df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3297df8bae1dSRodney W. Grimes bufsize = sb_max; 329888c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 32993f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3300df8bae1dSRodney W. Grimes } 33013f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3302a0292f23SGarrett Wollman /* 3303e8949f74SAndre Oppermann * While we're here, check the others too. 3304a0292f23SGarrett Wollman */ 330597d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 330697d8d152SAndre Oppermann tp->t_srtt = rtt; 330797d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 330878b50714SRobert Watson TCPSTAT_INC(tcps_usedrtt); 330997d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 331097d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 331178b50714SRobert Watson TCPSTAT_INC(tcps_usedrttvar); 331297d8d152SAndre Oppermann } else { 331397d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 331497d8d152SAndre Oppermann tp->t_rttvar = 331597d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 331697d8d152SAndre Oppermann } 331797d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 331897d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 331997d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 332097d8d152SAndre Oppermann } 332197d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 3322df8bae1dSRodney W. Grimes /* 3323df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 3324df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 3325df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 3326df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 3327df8bae1dSRodney W. Grimes */ 332897d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 332978b50714SRobert Watson TCPSTAT_INC(tcps_usedssthresh); 3330df8bae1dSRodney W. Grimes } 333197d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 333297d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 333397d8d152SAndre Oppermann 333497d8d152SAndre Oppermann /* 333597d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 333697d8d152SAndre Oppermann * is a local network or not. 333797d8d152SAndre Oppermann * 333897d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 333997d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 334097d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 334197d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 334297d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 334397d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 334497d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 334597d8d152SAndre Oppermann * overloads the path again. 334697d8d152SAndre Oppermann * 334797d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 334897d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 334997d8d152SAndre Oppermann */ 335097d8d152SAndre Oppermann #define TCP_METRICS_CWND 335197d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 335297d8d152SAndre Oppermann if (metrics.rmx_cwnd) 335397d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 335497d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 335597d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 335697d8d152SAndre Oppermann else 335797d8d152SAndre Oppermann #endif 3358603724d3SBjoern A. Zeeb if (V_tcp_do_rfc3390) 335997d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 336097d8d152SAndre Oppermann #ifdef INET6 336197d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 336297d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3363943ae302SAndre Oppermann #else 3364943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 336597d8d152SAndre Oppermann #endif 3366603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz_local; 336797d8d152SAndre Oppermann else 3368603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz; 336991d6cfa6SBjoern A. Zeeb 337091d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 337191d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 337291d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3373a0292f23SGarrett Wollman } 3374a0292f23SGarrett Wollman 3375a0292f23SGarrett Wollman /* 3376a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3377a0292f23SGarrett Wollman */ 3378a0292f23SGarrett Wollman int 3379ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3380a0292f23SGarrett Wollman { 338197d8d152SAndre Oppermann int mss = 0; 338297d8d152SAndre Oppermann u_long maxmtu = 0; 338397d8d152SAndre Oppermann u_long thcmtu = 0; 338497d8d152SAndre Oppermann size_t min_protoh; 3385a0292f23SGarrett Wollman 338697d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3387a0292f23SGarrett Wollman 338831b3783cSHajimu UMEMOTO #ifdef INET6 3389dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3390603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3391233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 339297d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 339397d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 339497d8d152SAndre Oppermann } else 339531b3783cSHajimu UMEMOTO #endif 339697d8d152SAndre Oppermann { 3397603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3398233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 339997d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 340097d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 340197d8d152SAndre Oppermann } 340297d8d152SAndre Oppermann if (maxmtu && thcmtu) 340397d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 340497d8d152SAndre Oppermann else if (maxmtu || thcmtu) 340597d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 340697d8d152SAndre Oppermann 340797d8d152SAndre Oppermann return (mss); 3408df8bae1dSRodney W. Grimes } 340946f58482SJonathan Lemon 341046f58482SJonathan Lemon 341146f58482SJonathan Lemon /* 3412c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3413c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3414c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3415c068736aSJeffrey Hsu * be started again. 341646f58482SJonathan Lemon */ 3417c068736aSJeffrey Hsu static void 3418ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 341946f58482SJonathan Lemon { 342046f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 342146f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 342246f58482SJonathan Lemon 34238501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34247a3244ccSRobert Watson 3425b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 342646f58482SJonathan Lemon tp->t_rtttime = 0; 342746f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 34286b2a5f92SJayanth Vijayaraghavan /* 3429c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3430c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 34316b2a5f92SJayanth Vijayaraghavan */ 34326b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3433a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 34346b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 343546f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 343646f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 343746f58482SJonathan Lemon tp->snd_nxt = onxt; 343846f58482SJonathan Lemon /* 343946f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 344046f58482SJonathan Lemon * not updated yet. 344146f58482SJonathan Lemon */ 3442d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 3443d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 3444d7587117SPaul Saab else 3445d7587117SPaul Saab tp->snd_cwnd = 0; 3446d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 344746f58482SJonathan Lemon } 3448