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 VNET_DEFINE(int, blackhole); 103eddfbb76SRobert Watson VNET_DEFINE(int, tcp_delack_enabled); 104eddfbb76SRobert Watson VNET_DEFINE(int, drop_synfin); 105eddfbb76SRobert Watson VNET_DEFINE(int, tcp_do_rfc3042); 106eddfbb76SRobert Watson VNET_DEFINE(int, tcp_do_rfc3390); 107eddfbb76SRobert Watson VNET_DEFINE(int, tcp_do_ecn); 108eddfbb76SRobert Watson VNET_DEFINE(int, tcp_ecn_maxretries); 109eddfbb76SRobert Watson VNET_DEFINE(int, tcp_insecure_rst); 110eddfbb76SRobert Watson VNET_DEFINE(int, tcp_do_autorcvbuf); 111eddfbb76SRobert Watson VNET_DEFINE(int, tcp_autorcvbuf_inc); 112eddfbb76SRobert Watson VNET_DEFINE(int, tcp_autorcvbuf_max); 113eddfbb76SRobert Watson VNET_DEFINE(int, tcp_do_rfc3465); 114eddfbb76SRobert Watson VNET_DEFINE(int, tcp_abc_l_var); 11544e33a07SMarko Zec 116eddfbb76SRobert Watson SYSCTL_VNET_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW, 117eddfbb76SRobert Watson &VNET_NAME(tcpstat), tcpstat, 1188b615593SMarko Zec "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1190312fbe9SPoul-Henning Kamp 120773673c1SAndre Oppermann int tcp_log_in_vain = 0; 121816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 122eddfbb76SRobert Watson &tcp_log_in_vain, 0, 123eddfbb76SRobert Watson "Log all incoming TCP segments to closed ports"); 124816a3d83SPoul-Henning Kamp 125eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 126eddfbb76SRobert Watson &VNET_NAME(blackhole), 0, 127eddfbb76SRobert Watson "Do not send RST on segments to closed ports"); 12816f7f31fSGeoff Rehmet 129eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, 130eddfbb76SRobert Watson &VNET_NAME(tcp_delack_enabled), 0, 1313d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 132f498eeeeSDavid Greenman 133eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW, 134eddfbb76SRobert Watson &VNET_NAME(drop_synfin), 0, 135eddfbb76SRobert Watson "Drop TCP packets with SYN+FIN set"); 136f8613305SDag-Erling Smørgrav 137eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 138eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3042), 0, 139eddfbb76SRobert Watson "Enable RFC 3042 (Limited Transmit)"); 140582a954bSJeffrey Hsu 141eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 142eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3390), 0, 143da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 144da3a8a1aSJeffrey Hsu 145eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW, 146eddfbb76SRobert Watson &VNET_NAME(tcp_do_rfc3465), 0, 14724cb0f22SLawrence Stewart "Enable RFC 3465 (Appropriate Byte Counting)"); 148eddfbb76SRobert Watson 149eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_RW, 150eddfbb76SRobert Watson &VNET_NAME(tcp_abc_l_var), 2, 15124cb0f22SLawrence Stewart "Cap the max cwnd increment during slow-start to this number of segments"); 15224cb0f22SLawrence Stewart 153f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 154f2512ba1SRui Paulo 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 159eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_RW, 160eddfbb76SRobert Watson &VNET_NAME(tcp_ecn_maxretries), 0, 161eddfbb76SRobert Watson "Max retries before giving up on ECN"); 162eddfbb76SRobert Watson 163eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW, 164eddfbb76SRobert Watson &VNET_NAME(tcp_insecure_rst), 0, 1656489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 166a69968eeSMike Silbersack 167eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW, 168eddfbb76SRobert Watson &VNET_NAME(tcp_do_autorcvbuf), 0, 1698b615593SMarko Zec "Enable automatic receive buffer sizing"); 1706741ecf5SAndre Oppermann 171eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW, 172eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_inc), 0, 1736489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1746741ecf5SAndre Oppermann 175eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW, 176eddfbb76SRobert Watson &VNET_NAME(tcp_autorcvbuf_max), 0, 1778b615593SMarko Zec "Max size of automatic receive buffer"); 1786741ecf5SAndre Oppermann 179252ca428SRobert Watson int tcp_read_locking = 1; 180252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, read_locking, CTLFLAG_RW, 181252ca428SRobert Watson &tcp_read_locking, 0, "Enable read locking strategy"); 182252ca428SRobert Watson 183252ca428SRobert Watson int tcp_rlock_atfirst; 184252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, rlock_atfirst, CTLFLAG_RD, 185252ca428SRobert Watson &tcp_rlock_atfirst, 0, ""); 186252ca428SRobert Watson 187252ca428SRobert Watson int tcp_wlock_atfirst; 188252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_wlock_atfirst, CTLFLAG_RD, 189252ca428SRobert Watson &tcp_wlock_atfirst, 0, ""); 190252ca428SRobert Watson 191252ca428SRobert Watson int tcp_wlock_upgraded; 192252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_upgraded, CTLFLAG_RD, 193252ca428SRobert Watson &tcp_wlock_upgraded, 0, ""); 194252ca428SRobert Watson 195252ca428SRobert Watson int tcp_wlock_relocked; 196252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_relocked, CTLFLAG_RD, 197252ca428SRobert Watson &tcp_wlock_relocked, 0, ""); 198252ca428SRobert Watson 199252ca428SRobert Watson int tcp_wlock_looped; 200252ca428SRobert Watson SYSCTL_INT(_net_inet_tcp, OID_AUTO, wlock_looped, CTLFLAG_RD, 201252ca428SRobert Watson &tcp_wlock_looped, 0, ""); 202252ca428SRobert Watson 203eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb); 204eddfbb76SRobert Watson VNET_DEFINE(struct inpcbinfo, tcbinfo); 20544e33a07SMarko Zec #define tcb6 tcb /* for KAME src sync over BSD*'s */ 206df8bae1dSRodney W. Grimes 2075a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 208679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 209252ca428SRobert Watson struct socket *, struct tcpcb *, int, int, uint8_t, 210252ca428SRobert Watson int); 211302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 212302ce8d6SAndre Oppermann struct tcpcb *, int, int); 2134d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 2144d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 2154d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 216c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 217f2512ba1SRui Paulo static void inline 218f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *); 219f2512ba1SRui Paulo 220f2512ba1SRui Paulo static void inline 221f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *tp) 222f2512ba1SRui Paulo { 223f2512ba1SRui Paulo u_int win; 224f2512ba1SRui Paulo 225f2512ba1SRui Paulo win = min(tp->snd_wnd, tp->snd_cwnd) / 226f2512ba1SRui Paulo 2 / tp->t_maxseg; 227f2512ba1SRui Paulo if (win < 2) 228f2512ba1SRui Paulo win = 2; 229f2512ba1SRui Paulo tp->snd_ssthresh = win * tp->t_maxseg; 230f2512ba1SRui Paulo ENTER_FASTRECOVERY(tp); 231f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 232f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 233f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 234f2512ba1SRui Paulo } 2350312fbe9SPoul-Henning Kamp 236fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 237fb59c426SYoshinobu Inoue #ifdef INET6 238fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 239fb59c426SYoshinobu Inoue do { \ 240fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 24197d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 24297d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 243fb59c426SYoshinobu Inoue } while (0) 244fb59c426SYoshinobu Inoue #else 245fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 246fb59c426SYoshinobu Inoue #endif 247df8bae1dSRodney W. Grimes 248df8bae1dSRodney W. Grimes /* 249262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 250262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 251262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 2523bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 2533bfd6421SJonathan Lemon * - delayed acks are enabled or 2543bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 255d8c85a26SJonathan Lemon */ 256d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 257b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 258a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 259603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 260d8c85a26SJonathan Lemon 261df8bae1dSRodney W. Grimes /* 2628d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 2638d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 2648d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 2658d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 2668d573cc1SAndre Oppermann * SYN processing on listen sockets 2678d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 2688d573cc1SAndre Oppermann * establishing, established and closing connections 269df8bae1dSRodney W. Grimes */ 270fb59c426SYoshinobu Inoue #ifdef INET6 271fb59c426SYoshinobu Inoue int 272ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 273fb59c426SYoshinobu Inoue { 274ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 27533841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 276fb59c426SYoshinobu Inoue 277fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 278fb59c426SYoshinobu Inoue 279fb59c426SYoshinobu Inoue /* 280fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 281fb59c426SYoshinobu Inoue * better place to put this in? 282fb59c426SYoshinobu Inoue */ 28333841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 28433841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 285fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 286fb59c426SYoshinobu Inoue 2878c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 288fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 289fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 290fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 291fb59c426SYoshinobu Inoue return IPPROTO_DONE; 292fb59c426SYoshinobu Inoue } 293fb59c426SYoshinobu Inoue 294f0ffb944SJulian Elischer tcp_input(m, *offp); 295fb59c426SYoshinobu Inoue return IPPROTO_DONE; 296fb59c426SYoshinobu Inoue } 297fb59c426SYoshinobu Inoue #endif 298fb59c426SYoshinobu Inoue 299df8bae1dSRodney W. Grimes void 300ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 301df8bae1dSRodney W. Grimes { 302ad3f9ab3SAndre Oppermann struct tcphdr *th; 303ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 304ad3f9ab3SAndre Oppermann struct ipovly *ipov; 305ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 306302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 307302ce8d6SAndre Oppermann struct socket *so = NULL; 308e79adb8eSGarrett Wollman u_char *optp = NULL; 30926f9a767SRodney W. Grimes int optlen = 0; 310a0194ef1SBruce M Simpson int len, tlen, off; 311fb59c426SYoshinobu Inoue int drop_hdrlen; 312ad3f9ab3SAndre Oppermann int thflags; 313302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 314f2512ba1SRui Paulo uint8_t iptos; 3159b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 3169b932e9eSAndre Oppermann struct m_tag *fwd_tag; 3179b932e9eSAndre Oppermann #endif 318c068736aSJeffrey Hsu #ifdef INET6 319302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 320c068736aSJeffrey Hsu int isipv6; 321c068736aSJeffrey Hsu #else 322a160e630SAndre Oppermann const void *ip6 = NULL; 323c068736aSJeffrey Hsu const int isipv6 = 0; 324c068736aSJeffrey Hsu #endif 325302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 326a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 327252ca428SRobert Watson int ti_locked; 328252ca428SRobert Watson #define TI_UNLOCKED 1 329252ca428SRobert Watson #define TI_RLOCKED 2 330252ca428SRobert Watson #define TI_WLOCKED 3 331f76fcf6dSJeffrey Hsu 332610ee2f9SDavid Greenman #ifdef TCPDEBUG 333c068736aSJeffrey Hsu /* 334c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 335c068736aSJeffrey Hsu * now IPv6. 336c068736aSJeffrey Hsu */ 33714739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 338410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 339610ee2f9SDavid Greenman short ostate = 0; 340610ee2f9SDavid Greenman #endif 341c068736aSJeffrey Hsu 342fb59c426SYoshinobu Inoue #ifdef INET6 343fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 344fb59c426SYoshinobu Inoue #endif 345a0292f23SGarrett Wollman 346302ce8d6SAndre Oppermann to.to_flags = 0; 34778b50714SRobert Watson TCPSTAT_INC(tcps_rcvtotal); 348fb59c426SYoshinobu Inoue 349fb59c426SYoshinobu Inoue if (isipv6) { 35004d3a452SHajimu UMEMOTO #ifdef INET6 3518d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 352fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 353fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 354fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 35578b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 356fb59c426SYoshinobu Inoue goto drop; 357fb59c426SYoshinobu Inoue } 358fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 35933841545SHajimu UMEMOTO 36033841545SHajimu UMEMOTO /* 36133841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 36233841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 36333841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 36433841545SHajimu UMEMOTO * 36533841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 36633841545SHajimu UMEMOTO * already dropped in ip6_input. 36733841545SHajimu UMEMOTO */ 36833841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 36933841545SHajimu UMEMOTO /* XXX stat */ 37033841545SHajimu UMEMOTO goto drop; 37133841545SHajimu UMEMOTO } 37204d3a452SHajimu UMEMOTO #else 3738d573cc1SAndre Oppermann th = NULL; /* XXX: Avoid compiler warning. */ 37404d3a452SHajimu UMEMOTO #endif 375c068736aSJeffrey Hsu } else { 376df8bae1dSRodney W. Grimes /* 377df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 378df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 379df8bae1dSRodney W. Grimes */ 380fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 381df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 382fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 383fb59c426SYoshinobu Inoue } 384df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3854dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3864dfdffe9SAndre Oppermann == NULL) { 38778b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 388df8bae1dSRodney W. Grimes return; 389df8bae1dSRodney W. Grimes } 390df8bae1dSRodney W. Grimes } 391fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 392fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 393db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 394db4f9cc7SJonathan Lemon tlen = ip->ip_len; 395df8bae1dSRodney W. Grimes 396db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 397db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 398db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 399db4f9cc7SJonathan Lemon else 400db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 401c068736aSJeffrey Hsu ip->ip_dst.s_addr, 402c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 403c068736aSJeffrey Hsu ip->ip_len + 404c068736aSJeffrey Hsu IPPROTO_TCP)); 405db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 4063c653157SHartmut Brandt #ifdef TCPDEBUG 4073c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 4083c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 4093c653157SHartmut Brandt #endif 410db4f9cc7SJonathan Lemon } else { 411df8bae1dSRodney W. Grimes /* 412df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 413df8bae1dSRodney W. Grimes */ 414df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 415fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 416fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 417fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 418fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 419db4f9cc7SJonathan Lemon } 420fb59c426SYoshinobu Inoue if (th->th_sum) { 42178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadsum); 422df8bae1dSRodney W. Grimes goto drop; 423df8bae1dSRodney W. Grimes } 424fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 425fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 426fb59c426SYoshinobu Inoue } 427df8bae1dSRodney W. Grimes 428f2512ba1SRui Paulo #ifdef INET6 429f2512ba1SRui Paulo if (isipv6) 430f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 431f2512ba1SRui Paulo else 432f2512ba1SRui Paulo #endif 433f2512ba1SRui Paulo iptos = ip->ip_tos; 434f2512ba1SRui Paulo 435df8bae1dSRodney W. Grimes /* 436df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 437df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 438df8bae1dSRodney W. Grimes */ 439fb59c426SYoshinobu Inoue off = th->th_off << 2; 440df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 44178b50714SRobert Watson TCPSTAT_INC(tcps_rcvbadoff); 442df8bae1dSRodney W. Grimes goto drop; 443df8bae1dSRodney W. Grimes } 444fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 445df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 446fb59c426SYoshinobu Inoue if (isipv6) { 44704d3a452SHajimu UMEMOTO #ifdef INET6 448fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 449fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 450fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 45104d3a452SHajimu UMEMOTO #endif 452c068736aSJeffrey Hsu } else { 453df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 454c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 4554dfdffe9SAndre Oppermann == NULL) { 45678b50714SRobert Watson TCPSTAT_INC(tcps_rcvshort); 457df8bae1dSRodney W. Grimes return; 458df8bae1dSRodney W. Grimes } 459fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 460fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 461fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 462fb59c426SYoshinobu Inoue } 463df8bae1dSRodney W. Grimes } 464df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 465fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 466df8bae1dSRodney W. Grimes } 467fb59c426SYoshinobu Inoue thflags = th->th_flags; 468df8bae1dSRodney W. Grimes 469e46cd3d4SDag-Erling Smørgrav /* 470df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 471df8bae1dSRodney W. Grimes */ 472fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 473fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 474fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 475fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 476df8bae1dSRodney W. Grimes 477df8bae1dSRodney W. Grimes /* 478794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 479755c1f07SAndras Olah */ 480fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 481755c1f07SAndras Olah 482755c1f07SAndras Olah /* 483252ca428SRobert Watson * Locate pcb for segment, which requires a lock on tcbinfo. 484d15fb965SRobert Watson * Optimisticaly acquire a global read lock rather than a write lock 485d15fb965SRobert Watson * unless header flags necessarily imply a state change. There are 486d15fb965SRobert Watson * two cases where we might discover later we need a write lock 487d15fb965SRobert Watson * despite the flags: ACKs moving a connection out of the syncache, 488d15fb965SRobert Watson * and ACKs for a connection in TIMEWAIT. 489df8bae1dSRodney W. Grimes */ 490252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 491252ca428SRobert Watson tcp_read_locking == 0) { 492603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 493252ca428SRobert Watson ti_locked = TI_WLOCKED; 494252ca428SRobert Watson tcp_wlock_atfirst++; 495252ca428SRobert Watson } else { 496252ca428SRobert Watson INP_INFO_RLOCK(&V_tcbinfo); 497252ca428SRobert Watson ti_locked = TI_RLOCKED; 498252ca428SRobert Watson tcp_rlock_atfirst++; 499252ca428SRobert Watson } 500252ca428SRobert Watson 501df8bae1dSRodney W. Grimes findpcb: 502252ca428SRobert Watson #ifdef INVARIANTS 503252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 504252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 505252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 506603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 507252ca428SRobert Watson else 508252ca428SRobert Watson panic("%s: findpcb ti_locked %d\n", __func__, ti_locked); 509252ca428SRobert Watson #endif 510252ca428SRobert Watson 5119b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 5128d573cc1SAndre Oppermann /* 5138d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 5148d573cc1SAndre Oppermann */ 5159b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 5169b932e9eSAndre Oppermann 5179b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 5189b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 5199b932e9eSAndre Oppermann 5209b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 521f9e354dfSJulian Elischer /* 5222b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 523f9e354dfSJulian Elischer * already got one like this? 524f9e354dfSJulian Elischer */ 525603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 5269b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 527c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 528c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 529f9e354dfSJulian Elischer if (!inp) { 5309b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 531603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 532fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 5332b25acc1SLuigi Rizzo next_hop->sin_addr, 534c068736aSJeffrey Hsu next_hop->sin_port ? 535c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 536c068736aSJeffrey Hsu th->th_dport, 537421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 538421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 539f9e354dfSJulian Elischer } 5409b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 5419b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 542b10fbdeaSAndre Oppermann } else 5439b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 544b10fbdeaSAndre Oppermann { 54504d3a452SHajimu UMEMOTO if (isipv6) { 54604d3a452SHajimu UMEMOTO #ifdef INET6 547603724d3SBjoern A. Zeeb inp = in6_pcblookup_hash(&V_tcbinfo, 548c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 549c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 550421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 551421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 55204d3a452SHajimu UMEMOTO #endif 55304d3a452SHajimu UMEMOTO } else 554603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 555c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 556c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 557421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 558421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 559fb59c426SYoshinobu Inoue } 560f9e354dfSJulian Elischer 561df8bae1dSRodney W. Grimes /* 562574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 563574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 5648b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 565df8bae1dSRodney W. Grimes */ 566816a3d83SPoul-Henning Kamp if (inp == NULL) { 567574b6964SAndre Oppermann /* 568574b6964SAndre Oppermann * Log communication attempts to ports that are not 569574b6964SAndre Oppermann * in use. 570574b6964SAndre Oppermann */ 571574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 572574b6964SAndre Oppermann tcp_log_in_vain == 2) { 5739fb5d4c0SPeter Wemm if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6))) 574df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 575df541e5fSAndre Oppermann "to closed port\n", s, __func__); 5762e4e1b4cSGeoff Rehmet } 577574b6964SAndre Oppermann /* 578574b6964SAndre Oppermann * When blackholing do not respond with a RST but 579574b6964SAndre Oppermann * completely ignore the segment and drop it. 580574b6964SAndre Oppermann */ 581603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 582603724d3SBjoern A. Zeeb V_blackhole == 2) 5831929eae1SAndre Oppermann goto dropunlock; 584574b6964SAndre Oppermann 585a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 586a57815efSBosko Milekic goto dropwithreset; 587816a3d83SPoul-Henning Kamp } 5888501a69cSRobert Watson INP_WLOCK(inp); 58980cb9f21SKip Macy if (!(inp->inp_flags & INP_HW_FLOWID) 59080cb9f21SKip Macy && (m->m_flags & M_FLOWID) 59180cb9f21SKip Macy && ((inp->inp_socket == NULL) 59280cb9f21SKip Macy || !(inp->inp_socket->so_options & SO_ACCEPTCONN))) { 59380cb9f21SKip Macy inp->inp_flags |= INP_HW_FLOWID; 59480cb9f21SKip Macy inp->inp_flags &= ~INP_SW_FLOWID; 59580cb9f21SKip Macy inp->inp_flowid = m->m_pkthdr.flowid; 59680cb9f21SKip Macy } 597a2589465SKen Smith #ifdef IPSEC 598a2589465SKen Smith #ifdef INET6 599a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 600603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 601a2589465SKen Smith goto dropunlock; 602a2589465SKen Smith } else 603a2589465SKen Smith #endif /* INET6 */ 604a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 605603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 606a2589465SKen Smith goto dropunlock; 607a2589465SKen Smith } 608a2589465SKen Smith #endif /* IPSEC */ 609a2589465SKen Smith 6108d573cc1SAndre Oppermann /* 6118d573cc1SAndre Oppermann * Check the minimum TTL for socket. 6128d573cc1SAndre Oppermann */ 61334f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 61434f83c52SGeorge V. Neville-Neil #ifdef INET6 61534f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 616302ce8d6SAndre Oppermann goto dropunlock; 61702707462SAndre Oppermann else 61834f83c52SGeorge V. Neville-Neil #endif 61934f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 620302ce8d6SAndre Oppermann goto dropunlock; 62134f83c52SGeorge V. Neville-Neil } 622936cd18dSAndre Oppermann 623340c35deSJonathan Lemon /* 624252ca428SRobert Watson * A previous connection in TIMEWAIT state is supposed to catch stray 625252ca428SRobert Watson * or duplicate segments arriving late. If this segment was a 626252ca428SRobert Watson * legitimate new connection attempt the old INPCB gets removed and 627252ca428SRobert Watson * we can try again to find a listening socket. 628252ca428SRobert Watson * 629252ca428SRobert Watson * At this point, due to earlier optimism, we may hold a read lock on 630252ca428SRobert Watson * the inpcbinfo, rather than a write lock. If so, we need to 631252ca428SRobert Watson * upgrade, or if that fails, acquire a reference on the inpcb, drop 632252ca428SRobert Watson * all locks, acquire a global write lock, and then re-acquire the 633252ca428SRobert Watson * inpcb lock. We may at that point discover that another thread has 634252ca428SRobert Watson * tried to free the inpcb, in which case we need to loop back and 635252ca428SRobert Watson * try to find a new inpcb to deliver to. 636340c35deSJonathan Lemon */ 637ad71fe3cSRobert Watson if (inp->inp_flags & INP_TIMEWAIT) { 638252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 639252ca428SRobert Watson ("%s: INP_TIMEWAIT ti_locked %d", __func__, ti_locked)); 640252ca428SRobert Watson 641252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 642252ca428SRobert Watson if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) { 643252ca428SRobert Watson in_pcbref(inp); 644252ca428SRobert Watson INP_WUNLOCK(inp); 645252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 646252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 647252ca428SRobert Watson ti_locked = TI_WLOCKED; 648252ca428SRobert Watson INP_WLOCK(inp); 649252ca428SRobert Watson if (in_pcbrele(inp)) { 650252ca428SRobert Watson tcp_wlock_looped++; 651252ca428SRobert Watson inp = NULL; 652252ca428SRobert Watson goto findpcb; 653252ca428SRobert Watson } 654252ca428SRobert Watson tcp_wlock_relocked++; 655252ca428SRobert Watson } else { 656252ca428SRobert Watson ti_locked = TI_WLOCKED; 657252ca428SRobert Watson tcp_wlock_upgraded++; 658252ca428SRobert Watson } 659252ca428SRobert Watson } 660252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 661252ca428SRobert Watson 662340c35deSJonathan Lemon if (thflags & TH_SYN) 663f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 6648d573cc1SAndre Oppermann /* 6658d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 6668d573cc1SAndre Oppermann */ 6672104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 668340c35deSJonathan Lemon goto findpcb; 669603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 670340c35deSJonathan Lemon return; 671340c35deSJonathan Lemon } 672794235b7SAndre Oppermann /* 673794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 674794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 675794235b7SAndre Oppermann * segment and send an appropriate response. 676794235b7SAndre Oppermann */ 677df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 678a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 679a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 680a57815efSBosko Milekic goto dropwithreset; 68109f81a46SBosko Milekic } 682df8bae1dSRodney W. Grimes 683252ca428SRobert Watson /* 684252ca428SRobert Watson * We've identified a valid inpcb, but it could be that we need an 685252ca428SRobert Watson * inpcbinfo write lock and have only a read lock. In this case, 686252ca428SRobert Watson * attempt to upgrade/relock using the same strategy as the TIMEWAIT 687252ca428SRobert Watson * case above. 688252ca428SRobert Watson */ 689252ca428SRobert Watson if (tp->t_state != TCPS_ESTABLISHED || 690252ca428SRobert Watson (thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 691252ca428SRobert Watson tcp_read_locking == 0) { 692252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 693252ca428SRobert Watson ("%s: upgrade check ti_locked %d", __func__, ti_locked)); 694252ca428SRobert Watson 695252ca428SRobert Watson if (ti_locked == TI_RLOCKED) { 696252ca428SRobert Watson if (rw_try_upgrade(&V_tcbinfo.ipi_lock) == 0) { 697252ca428SRobert Watson in_pcbref(inp); 698252ca428SRobert Watson INP_WUNLOCK(inp); 699252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 700252ca428SRobert Watson INP_INFO_WLOCK(&V_tcbinfo); 701252ca428SRobert Watson ti_locked = TI_WLOCKED; 702252ca428SRobert Watson INP_WLOCK(inp); 703252ca428SRobert Watson if (in_pcbrele(inp)) { 704252ca428SRobert Watson tcp_wlock_looped++; 705252ca428SRobert Watson inp = NULL; 706252ca428SRobert Watson goto findpcb; 707252ca428SRobert Watson } 708252ca428SRobert Watson tcp_wlock_relocked++; 709252ca428SRobert Watson } else { 710252ca428SRobert Watson ti_locked = TI_WLOCKED; 711252ca428SRobert Watson tcp_wlock_upgraded++; 712252ca428SRobert Watson } 713252ca428SRobert Watson } 714252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 715252ca428SRobert Watson } 716252ca428SRobert Watson 717c488362eSRobert Watson #ifdef MAC 7188501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 71930d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 720302ce8d6SAndre Oppermann goto dropunlock; 721c488362eSRobert Watson #endif 722a557af22SRobert Watson so = inp->inp_socket; 723302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 724610ee2f9SDavid Greenman #ifdef TCPDEBUG 725df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 726df8bae1dSRodney W. Grimes ostate = tp->t_state; 727d30d90dcSMaxim Konovalov if (isipv6) { 72810fe523eSMaxim Konovalov #ifdef INET6 729540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 730d30d90dcSMaxim Konovalov #endif 731d30d90dcSMaxim Konovalov } else 732540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 733fb59c426SYoshinobu Inoue tcp_savetcp = *th; 734df8bae1dSRodney W. Grimes } 735610ee2f9SDavid Greenman #endif 736db33b3e6SAndre Oppermann /* 737db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 738db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 739db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 740db33b3e6SAndre Oppermann */ 741540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 742540e8b7eSJeffrey Hsu struct in_conninfo inc; 743540e8b7eSJeffrey Hsu 7447824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 7457824d002SAndre Oppermann "tp not listening", __func__)); 7467824d002SAndre Oppermann 7478bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 748302ce8d6SAndre Oppermann #ifdef INET6 749be2ac88cSJonathan Lemon if (isipv6) { 750dcdb4371SBjoern A. Zeeb inc.inc_flags |= INC_ISIPV6; 751be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 752be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 753302ce8d6SAndre Oppermann } else 754302ce8d6SAndre Oppermann #endif 755302ce8d6SAndre Oppermann { 756be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 757be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 758be2ac88cSJonathan Lemon } 759be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 760be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 7617973fba3SJulian Elischer inc.inc_fibnum = so->so_fibnum; 762be2ac88cSJonathan Lemon 763fb59c426SYoshinobu Inoue /* 7648d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 7658d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 7668d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 767fb59c426SYoshinobu Inoue */ 768be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 769bf6d304aSAndre Oppermann /* 770bf6d304aSAndre Oppermann * Parse the TCP options here because 771bf6d304aSAndre Oppermann * syncookies need access to the reflected 772bf6d304aSAndre Oppermann * timestamp. 773bf6d304aSAndre Oppermann */ 774bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 7759fa198beSAndre Oppermann /* 7769fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 7779fa198beSAndre Oppermann * inp and tcpinfo locks. 7789fa198beSAndre Oppermann */ 779bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 7804195b4afSPaul Traina /* 781e207f800SAndre Oppermann * No syncache entry or ACK was not 782be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 7838d573cc1SAndre Oppermann * NB: syncache did its own logging 7848d573cc1SAndre Oppermann * of the failure cause. 7854195b4afSPaul Traina */ 786be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 787be2ac88cSJonathan Lemon goto dropwithreset; 788be2ac88cSJonathan Lemon } 789f76fcf6dSJeffrey Hsu if (so == NULL) { 790be2ac88cSJonathan Lemon /* 791e207f800SAndre Oppermann * We completed the 3-way handshake 792e207f800SAndre Oppermann * but could not allocate a socket 793e207f800SAndre Oppermann * either due to memory shortage, 794e207f800SAndre Oppermann * listen queue length limits or 795a160e630SAndre Oppermann * global socket limits. Send RST 796a160e630SAndre Oppermann * or wait and have the remote end 797a160e630SAndre Oppermann * retransmit the ACK for another 798a160e630SAndre Oppermann * try. 799be2ac88cSJonathan Lemon */ 800a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 801a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8028d573cc1SAndre Oppermann "Socket allocation failed due to " 8038d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 804ac957cd2SJulian Elischer s, __func__, 805ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 806ac957cd2SJulian Elischer "sending RST" : "try again"); 807603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 808e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 809e207f800SAndre Oppermann goto dropwithreset; 810a160e630SAndre Oppermann } else 811a160e630SAndre Oppermann goto dropunlock; 812f76fcf6dSJeffrey Hsu } 813be2ac88cSJonathan Lemon /* 814be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 8158d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 8168d573cc1SAndre Oppermann * created socket and update the tp variable. 817be2ac88cSJonathan Lemon */ 8188501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 819be2ac88cSJonathan Lemon inp = sotoinpcb(so); 8208501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 821be2ac88cSJonathan Lemon tp = intotcpcb(inp); 8228d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 8238d573cc1SAndre Oppermann ("%s: ", __func__)); 824be2ac88cSJonathan Lemon /* 825302ce8d6SAndre Oppermann * Process the segment and the data it 826302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 827302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 828302ce8d6SAndre Oppermann */ 829f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 830252ca428SRobert Watson iptos, ti_locked); 831603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 832302ce8d6SAndre Oppermann return; 833be2ac88cSJonathan Lemon } 834a160e630SAndre Oppermann /* 8358d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 8368d573cc1SAndre Oppermann * 837a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 838a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 839a160e630SAndre Oppermann * retransmits. 84019bc77c5SAndre Oppermann * 84119bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 84219bc77c5SAndre Oppermann * causes. 843a160e630SAndre Oppermann */ 844a160e630SAndre Oppermann if (thflags & TH_RST) { 84519bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 846a160e630SAndre Oppermann goto dropunlock; 847a160e630SAndre Oppermann } 848a160e630SAndre Oppermann /* 849a160e630SAndre Oppermann * We can't do anything without SYN. 850a160e630SAndre Oppermann */ 851a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 852a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 853a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 854773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 8558d573cc1SAndre Oppermann s, __func__); 85678b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 857a160e630SAndre Oppermann goto dropunlock; 858a160e630SAndre Oppermann } 859a160e630SAndre Oppermann /* 860a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 861a160e630SAndre Oppermann */ 862fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 863a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 864a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8658d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 8668d573cc1SAndre Oppermann s, __func__); 867a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 86878b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 869a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 870a57815efSBosko Milekic goto dropwithreset; 8714195b4afSPaul Traina } 872a160e630SAndre Oppermann /* 873a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 874a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 875a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 876a160e630SAndre Oppermann * TCP/IP stack. 877a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 878a160e630SAndre Oppermann * and is constantly refining its stack detection 879a160e630SAndre Oppermann * strategies. 8808d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 881a160e630SAndre Oppermann * and was used by RFC1644. 882a160e630SAndre Oppermann */ 883603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 884a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 885a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 886773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 8878d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 88878b50714SRobert Watson TCPSTAT_INC(tcps_badsyn); 889302ce8d6SAndre Oppermann goto dropunlock; 890ebb0cbeaSPaul Traina } 891be2ac88cSJonathan Lemon /* 892be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 893a160e630SAndre Oppermann * 894a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 895a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 896a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 897be2ac88cSJonathan Lemon */ 898a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 899a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 900a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 901a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 90233841545SHajimu UMEMOTO #ifdef INET6 90333841545SHajimu UMEMOTO /* 90433841545SHajimu UMEMOTO * If deprecated address is forbidden, 90533841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 90633841545SHajimu UMEMOTO * address to prevent any new inbound connection from 90733841545SHajimu UMEMOTO * getting established. 90833841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 90933841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 91033841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 91133841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 91233841545SHajimu UMEMOTO * for the exchange. 91333841545SHajimu UMEMOTO * 91433841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 91533841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 91633841545SHajimu UMEMOTO * SYN in this case. 91733841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 91833841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 91933841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 92033841545SHajimu UMEMOTO * used" 92133841545SHajimu UMEMOTO * 2. use of it with new communication: 92233841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 92333841545SHajimu UMEMOTO * with sufficient scope is available" 92433841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 92533841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 92633841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 92733841545SHajimu UMEMOTO * 92833841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 92933841545SHajimu UMEMOTO * multiple description text for deprecated address 93033841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 93133841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 93233841545SHajimu UMEMOTO */ 933603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 93433841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 93533841545SHajimu UMEMOTO 9368c0fec80SRobert Watson ia6 = ip6_getdstifaddr(m); 9378c0fec80SRobert Watson if (ia6 != NULL && 93833841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 9398c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 940a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 941a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9428d573cc1SAndre Oppermann "Connection attempt to deprecated " 9438d573cc1SAndre Oppermann "IPv6 address rejected\n", 944a160e630SAndre Oppermann s, __func__); 94533841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 94633841545SHajimu UMEMOTO goto dropwithreset; 94733841545SHajimu UMEMOTO } 9488c0fec80SRobert Watson ifa_free(&ia6->ia_ifa); 94933841545SHajimu UMEMOTO } 95033841545SHajimu UMEMOTO #endif 95165f28919SJesper Skriver /* 952db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 9538d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 954db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 9558d573cc1SAndre Oppermann * If it is from this socket it must be forged. 9568d573cc1SAndre Oppermann * Don't respond if the source or destination is a 9578d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 95893ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 95993ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 96093ec91baSCrist J. Clark * in_broadcast() to find them. 961be2ac88cSJonathan Lemon */ 9628d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 9638d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 9648d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9658d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 966773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 967302ce8d6SAndre Oppermann goto dropunlock; 9688d573cc1SAndre Oppermann } 969be2ac88cSJonathan Lemon if (isipv6) { 970db33b3e6SAndre Oppermann #ifdef INET6 971db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 972a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 973a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 974a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9758d573cc1SAndre Oppermann "Connection attempt to/from self " 976773673c1SAndre Oppermann "ignored\n", s, __func__); 977302ce8d6SAndre Oppermann goto dropunlock; 978a160e630SAndre Oppermann } 979be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 980a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 981a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 982a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9838d573cc1SAndre Oppermann "Connection attempt from/to multicast " 984773673c1SAndre Oppermann "address ignored\n", s, __func__); 985302ce8d6SAndre Oppermann goto dropunlock; 986a160e630SAndre Oppermann } 987db33b3e6SAndre Oppermann #endif 988c068736aSJeffrey Hsu } else { 989db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 990a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 991a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 992a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 9938d573cc1SAndre Oppermann "Connection attempt from/to self " 994773673c1SAndre Oppermann "ignored\n", s, __func__); 995302ce8d6SAndre Oppermann goto dropunlock; 996a160e630SAndre Oppermann } 997be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 998be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 9992ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 1000a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 1001a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 1002a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 10038d573cc1SAndre Oppermann "Connection attempt from/to broad- " 1004773673c1SAndre Oppermann "or multicast address ignored\n", 1005a160e630SAndre Oppermann s, __func__); 1006302ce8d6SAndre Oppermann goto dropunlock; 1007c068736aSJeffrey Hsu } 1008a160e630SAndre Oppermann } 1009be2ac88cSJonathan Lemon /* 1010db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 1011db33b3e6SAndre Oppermann * for syncache. 1012be2ac88cSJonathan Lemon */ 10133c653157SHartmut Brandt #ifdef TCPDEBUG 10143c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 10153c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 10163c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 10173c653157SHartmut Brandt #endif 1018f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 10194d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 1020be2ac88cSJonathan Lemon /* 10214d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 1022a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 1023be2ac88cSJonathan Lemon */ 1024603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1025be2ac88cSJonathan Lemon return; 1026f76fcf6dSJeffrey Hsu } 1027db33b3e6SAndre Oppermann 1028302ce8d6SAndre Oppermann /* 10298d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 10308d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 10318d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 1032302ce8d6SAndre Oppermann */ 1033252ca428SRobert Watson tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked); 1034603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1035302ce8d6SAndre Oppermann return; 1036be2ac88cSJonathan Lemon 1037302ce8d6SAndre Oppermann dropwithreset: 1038252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1039252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1040252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1041dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1042252ca428SRobert Watson else 1043252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 1044252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1045014ea782SRobert Watson 1046014ea782SRobert Watson if (inp != NULL) { 1047302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 1048014ea782SRobert Watson INP_WUNLOCK(inp); 1049dd8ac7f9SRobert Watson } else 1050014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 1051302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 1052014ea782SRobert Watson goto drop; 1053014ea782SRobert Watson 1054302ce8d6SAndre Oppermann dropunlock: 1055252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1056252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1057252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1058252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1059252ca428SRobert Watson else 1060252ca428SRobert Watson panic("%s: dropunlock ti_locked %d", __func__, ti_locked); 1061252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1062252ca428SRobert Watson 10639fa198beSAndre Oppermann if (inp != NULL) 10648501a69cSRobert Watson INP_WUNLOCK(inp); 1065014ea782SRobert Watson 1066302ce8d6SAndre Oppermann drop: 1067603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 1068a160e630SAndre Oppermann if (s != NULL) 1069a160e630SAndre Oppermann free(s, M_TCPLOG); 1070302ce8d6SAndre Oppermann if (m != NULL) 1071302ce8d6SAndre Oppermann m_freem(m); 1072302ce8d6SAndre Oppermann } 1073302ce8d6SAndre Oppermann 1074679d9708SAndre Oppermann static void 1075302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 1076252ca428SRobert Watson struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos, 1077252ca428SRobert Watson int ti_locked) 1078302ce8d6SAndre Oppermann { 1079302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 1080302ce8d6SAndre Oppermann int rstreason, todrop, win; 1081302ce8d6SAndre Oppermann u_long tiwin; 1082302ce8d6SAndre Oppermann struct tcpopt to; 1083302ce8d6SAndre Oppermann 108414739780SMaxim Konovalov #ifdef TCPDEBUG 108514739780SMaxim Konovalov /* 108614739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 108714739780SMaxim Konovalov * now IPv6. 108814739780SMaxim Konovalov */ 108914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 109014739780SMaxim Konovalov struct tcphdr tcp_savetcp; 109114739780SMaxim Konovalov short ostate = 0; 109214739780SMaxim Konovalov #endif 1093302ce8d6SAndre Oppermann thflags = th->th_flags; 1094302ce8d6SAndre Oppermann 1095252ca428SRobert Watson /* 1096252ca428SRobert Watson * If this is either a state-changing packet or current state isn't 1097252ca428SRobert Watson * established, we require a write lock on tcbinfo. Otherwise, we 1098252ca428SRobert Watson * allow either a read lock or a write lock, as we may have acquired 1099252ca428SRobert Watson * a write lock due to a race. 1100252ca428SRobert Watson * 1101d15fb965SRobert Watson * Require a global write lock for SYN/FIN/RST segments or 1102252ca428SRobert Watson * non-established connections; otherwise accept either a read or 1103252ca428SRobert Watson * write lock, as we may have conservatively acquired a write lock in 1104252ca428SRobert Watson * certain cases in tcp_input() (is this still true?). Currently we 1105252ca428SRobert Watson * will never enter with no lock, so we try to drop it quickly in the 1106252ca428SRobert Watson * common pure ack/pure data cases. 1107252ca428SRobert Watson */ 1108252ca428SRobert Watson if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 || 1109252ca428SRobert Watson tp->t_state != TCPS_ESTABLISHED) { 1110252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s ti_locked %d for " 1111252ca428SRobert Watson "SYN/FIN/RST/!EST", __func__, ti_locked)); 1112603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1113252ca428SRobert Watson } else { 1114252ca428SRobert Watson #ifdef INVARIANTS 1115252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1116252ca428SRobert Watson INP_INFO_RLOCK_ASSERT(&V_tcbinfo); 1117252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1118252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1119252ca428SRobert Watson else 1120252ca428SRobert Watson panic("%s: ti_locked %d for EST", __func__, 1121252ca428SRobert Watson ti_locked); 1122252ca428SRobert Watson #endif 1123252ca428SRobert Watson } 11248501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1125679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 1126679d9708SAndre Oppermann __func__)); 1127679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 1128679d9708SAndre Oppermann __func__)); 1129df8bae1dSRodney W. Grimes 1130df8bae1dSRodney W. Grimes /* 1131df8bae1dSRodney W. Grimes * Segment received on connection. 1132df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 1133e8949f74SAndre Oppermann * XXX: This should be done after segment 1134e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 1135df8bae1dSRodney W. Grimes */ 11369b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 11377ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 1138b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 1139df8bae1dSRodney W. Grimes 1140df8bae1dSRodney W. Grimes /* 1141464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 1142e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 1143464fcfbcSAndre Oppermann */ 1144464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 1145464fcfbcSAndre Oppermann 1146464fcfbcSAndre Oppermann /* 1147f2512ba1SRui Paulo * TCP ECN processing. 1148f2512ba1SRui Paulo */ 1149f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 1150f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 1151f2512ba1SRui Paulo case IPTOS_ECN_CE: 1152f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 115378b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ce); 1154f2512ba1SRui Paulo break; 1155f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 115678b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1157f2512ba1SRui Paulo break; 1158f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 115978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect1); 1160f2512ba1SRui Paulo break; 1161f2512ba1SRui Paulo } 1162f2512ba1SRui Paulo 1163f2512ba1SRui Paulo if (thflags & TH_CWR) 1164f2512ba1SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 1165f2512ba1SRui Paulo 1166f2512ba1SRui Paulo /* 1167f2512ba1SRui Paulo * Congestion experienced. 1168f2512ba1SRui Paulo * Ignore if we are already trying to recover. 1169f2512ba1SRui Paulo */ 1170f2512ba1SRui Paulo if ((thflags & TH_ECE) && 1171f2512ba1SRui Paulo SEQ_LEQ(th->th_ack, tp->snd_recover)) { 117278b50714SRobert Watson TCPSTAT_INC(tcps_ecn_rcwnd); 1173f2512ba1SRui Paulo tcp_congestion_exp(tp); 1174f2512ba1SRui Paulo } 1175f2512ba1SRui Paulo } 1176f2512ba1SRui Paulo 1177f2512ba1SRui Paulo /* 1178f72167f4SAndre Oppermann * Parse options on any incoming segment. 1179f72167f4SAndre Oppermann */ 1180302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1181302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1182302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1183f72167f4SAndre Oppermann 1184f72167f4SAndre Oppermann /* 1185f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1186bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1187bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1188bf6d304aSAndre Oppermann * was established. 1189f72167f4SAndre Oppermann */ 1190bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1191e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1192bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1193f72167f4SAndre Oppermann to.to_tsecr = 0; 1194bf6d304aSAndre Oppermann } 1195f72167f4SAndre Oppermann 1196f72167f4SAndre Oppermann /* 119797d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 119897d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 11995396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 12005396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 120197d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1202df8bae1dSRodney W. Grimes */ 12030a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1204464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1205464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1206be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 120702a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1208be2ac88cSJonathan Lemon } 12095396d0f8SAndre Oppermann /* 12105396d0f8SAndre Oppermann * Initial send window. It will be updated with 12115396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 12125396d0f8SAndre Oppermann */ 12135396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1214be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1215be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1216be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1217be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1218be2ac88cSJonathan Lemon } 1219be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1220be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 12213529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 12223529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 12233529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 12246d90faf3SPaul Saab } 12256d90faf3SPaul Saab 1226df8bae1dSRodney W. Grimes /* 1227df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1228df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1229df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1230df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1231df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1232df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1233df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1234df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1235df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1236df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1237df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1238df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1239a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1240c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1241a0292f23SGarrett Wollman * be TH_NEEDSYN. 1242df8bae1dSRodney W. Grimes */ 1243df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1244c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1245fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1246c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1247c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1248a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1249c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1250be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1251c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1252df8bae1dSRodney W. Grimes 1253df8bae1dSRodney W. Grimes /* 1254df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1255df8bae1dSRodney W. Grimes * record the timestamp. 1256a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1257a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1258df8bae1dSRodney W. Grimes */ 1259be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1260fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 12619b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1262a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1263df8bae1dSRodney W. Grimes } 1264df8bae1dSRodney W. Grimes 1265fb59c426SYoshinobu Inoue if (tlen == 0) { 1266fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1267fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1268233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 1269603724d3SBjoern A. Zeeb ((!V_tcp_do_newreno && 12703529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 1271cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 1272603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 12733529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 1274fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 1275fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1276482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1277df8bae1dSRodney W. Grimes /* 1278e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1279df8bae1dSRodney W. Grimes */ 1280252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1281252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1282252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1283252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1284252ca428SRobert Watson else 1285252ca428SRobert Watson panic("%s: ti_locked %d on pure ACK", 1286252ca428SRobert Watson __func__, ti_locked); 1287252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1288252ca428SRobert Watson 128978b50714SRobert Watson TCPSTAT_INC(tcps_predack); 1290252ca428SRobert Watson 12919b8b58e0SJonathan Lemon /* 1292e8949f74SAndre Oppermann * "bad retransmit" recovery. 12939b8b58e0SJonathan Lemon */ 12949b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 12956dfb8b31SJohn Baldwin (int)(ticks - tp->t_badrxtwin) < 0) { 129678b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitbad); 12979b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 12989b8b58e0SJonathan Lemon tp->snd_ssthresh = 12999b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 13009d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 13019d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 13029d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 13039b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 13049b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 13059b8b58e0SJonathan Lemon } 1306fa55172bSMatthew Dillon 1307fa55172bSMatthew Dillon /* 1308fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1309fa55172bSMatthew Dillon * 1310fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1311fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1312fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1313fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1314fa55172bSMatthew Dillon */ 1315fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1316fa55172bSMatthew Dillon to.to_tsecr) { 1317eaf80179SAndre Oppermann if (!tp->t_rttlow || 1318eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1319eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1320a0292f23SGarrett Wollman tcp_xmit_timer(tp, 13219b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1322fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1323fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1324eaf80179SAndre Oppermann if (!tp->t_rttlow || 1325eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1326eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1327c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1328c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1329fa55172bSMatthew Dillon } 13301fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1331fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 133278b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 133378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 1334df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 13359d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 13369d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 13379d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 13389d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 13391645d090SJeff Roberson /* 1340e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 13411645d090SJeff Roberson * to th_ack. 13421645d090SJeff Roberson */ 1343cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1344b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1345df8bae1dSRodney W. Grimes m_freem(m); 1346e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1347df8bae1dSRodney W. Grimes 1348df8bae1dSRodney W. Grimes /* 1349df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1350df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1351df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1352df8bae1dSRodney W. Grimes * If process is waiting for space, 1353df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1354df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1355df8bae1dSRodney W. Grimes * decide between more output or persist. 1356e8949f74SAndre Oppermann */ 13573c653157SHartmut Brandt #ifdef TCPDEBUG 13583c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 13593c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 13603c653157SHartmut Brandt (void *)tcp_saveipgen, 13613c653157SHartmut Brandt &tcp_savetcp, 0); 13623c653157SHartmut Brandt #endif 1363df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1364b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1365b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1366b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1367b8152ba7SAndre Oppermann tp->t_rxtcur); 1368df8bae1dSRodney W. Grimes sowwakeup(so); 1369df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1370df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1371a14c749fSJonathan Lemon goto check_delack; 1372df8bae1dSRodney W. Grimes } 1373fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1374fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 13756741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 13766741ecf5SAndre Oppermann 1377df8bae1dSRodney W. Grimes /* 1378252ca428SRobert Watson * This is a pure, in-sequence data packet with 1379252ca428SRobert Watson * nothing on the reassembly queue and we have enough 1380252ca428SRobert Watson * buffer space to take it. 1381df8bae1dSRodney W. Grimes */ 1382252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 1383252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 1384252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 1385252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 1386252ca428SRobert Watson else 1387252ca428SRobert Watson panic("%s: ti_locked %d on pure data " 1388252ca428SRobert Watson "segment", __func__, ti_locked); 1389252ca428SRobert Watson ti_locked = TI_UNLOCKED; 1390252ca428SRobert Watson 13916d90faf3SPaul Saab /* Clean receiver SACK report if present */ 13923529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 13936d90faf3SPaul Saab tcp_clean_sackreport(tp); 139478b50714SRobert Watson TCPSTAT_INC(tcps_preddat); 1395fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 13961645d090SJeff Roberson /* 13971645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 13981645d090SJeff Roberson * th_seq. 13991645d090SJeff Roberson */ 14001645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 14011645d090SJeff Roberson /* 14021645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 14031645d090SJeff Roberson * rcv_nxt. 14041645d090SJeff Roberson */ 14051645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 140678b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 140778b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 1408e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 14093c653157SHartmut Brandt #ifdef TCPDEBUG 14103c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 14113c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 14123c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 14133c653157SHartmut Brandt #endif 14146741ecf5SAndre Oppermann /* 14156741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 14166741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 14176741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 14186741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 14196741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 14206741ecf5SAndre Oppermann * 14216741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 14226741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 14236741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 14246741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 14256741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 14266741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 14276741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 14286741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 14296741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 14306741ecf5SAndre Oppermann * reassembly queue. 14316741ecf5SAndre Oppermann * 14326741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 14336741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 14346741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 14356741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 14366741ecf5SAndre Oppermann * current socket buffer size; 14376741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 14386741ecf5SAndre Oppermann * 14396741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 14406741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 14416741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 14426741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 14436741ecf5SAndre Oppermann * 14446741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 14456741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1446df8bae1dSRodney W. Grimes */ 1447603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 14486741ecf5SAndre Oppermann to.to_tsecr && 14496741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 14506741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 14516741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 14526741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 14536741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 14546741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1455603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 14566741ecf5SAndre Oppermann newsize = 14576741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1458603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1459603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 14606741ecf5SAndre Oppermann } 14616741ecf5SAndre Oppermann /* Start over with next RTT. */ 14626741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 14636741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 14646741ecf5SAndre Oppermann } else 14656741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 14666741ecf5SAndre Oppermann } 14676741ecf5SAndre Oppermann 14686741ecf5SAndre Oppermann /* Add data to socket buffer. */ 14691e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1470c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1471c1c36a2cSMike Silbersack m_freem(m); 1472c1c36a2cSMike Silbersack } else { 14736741ecf5SAndre Oppermann /* 14746741ecf5SAndre Oppermann * Set new socket buffer size. 14756741ecf5SAndre Oppermann * Give up when limit is reached. 14766741ecf5SAndre Oppermann */ 14776741ecf5SAndre Oppermann if (newsize) 14786741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 14796c8286e4SRobert Watson newsize, so, NULL)) 14806741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1481fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 14821e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1483c1c36a2cSMike Silbersack } 1484e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 14851e4d7da7SRobert Watson sorwakeup_locked(so); 1486d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 14873bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1488f498eeeeSDavid Greenman } else { 1489e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1490e612a582SDavid Greenman tcp_output(tp); 1491e612a582SDavid Greenman } 1492a14c749fSJonathan Lemon goto check_delack; 1493df8bae1dSRodney W. Grimes } 1494df8bae1dSRodney W. Grimes } 1495df8bae1dSRodney W. Grimes 1496df8bae1dSRodney W. Grimes /* 1497df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1498df8bae1dSRodney W. Grimes * and then do TCP input processing. 1499df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1500df8bae1dSRodney W. Grimes * but not less than advertised window. 1501df8bae1dSRodney W. Grimes */ 1502df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1503df8bae1dSRodney W. Grimes if (win < 0) 1504df8bae1dSRodney W. Grimes win = 0; 150566e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1506df8bae1dSRodney W. Grimes 15076741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 15086741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 15096741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 15106741ecf5SAndre Oppermann 1511df8bae1dSRodney W. Grimes switch (tp->t_state) { 1512df8bae1dSRodney W. Grimes 1513df8bae1dSRodney W. Grimes /* 1514764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1515764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1516764d8cefSBill Fenner */ 1517764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1518fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1519fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1520a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1521a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1522a57815efSBosko Milekic goto dropwithreset; 1523a57815efSBosko Milekic } 1524764d8cefSBill Fenner break; 1525764d8cefSBill Fenner 1526764d8cefSBill Fenner /* 1527df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1528df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1529df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1530df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1531df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1532df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1533df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1534f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1535f2512ba1SRui Paulo * is ECN capable. 1536df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1537df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1538df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1539df8bae1dSRodney W. Grimes */ 1540df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1541fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1542fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1543fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1544a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1545a0292f23SGarrett Wollman goto dropwithreset; 1546a0292f23SGarrett Wollman } 15470ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1548df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 15490ca3f933SAndre Oppermann if (thflags & TH_RST) 1550df8bae1dSRodney W. Grimes goto drop; 15510ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1552df8bae1dSRodney W. Grimes goto drop; 1553464fcfbcSAndre Oppermann 1554fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1555df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1556fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 155778b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1558845799c1SAndras Olah soisconnected(so); 1559c488362eSRobert Watson #ifdef MAC 156030d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1561c488362eSRobert Watson #endif 1562845799c1SAndras Olah /* Do window scaling on this connection? */ 1563845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1564845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1565845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1566845799c1SAndras Olah } 1567a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1568a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1569a0292f23SGarrett Wollman /* 1570a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1571a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1572a0292f23SGarrett Wollman */ 1573d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1574b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1575b8152ba7SAndre Oppermann tcp_delacktime); 1576a0292f23SGarrett Wollman else 1577a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1578f2512ba1SRui Paulo 1579603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1580f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 158178b50714SRobert Watson TCPSTAT_INC(tcps_ecn_shs); 1582f2512ba1SRui Paulo } 1583f2512ba1SRui Paulo 1584a0292f23SGarrett Wollman /* 1585a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1586a0292f23SGarrett Wollman * Transitions: 1587a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1588a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1589a0292f23SGarrett Wollman */ 15909b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1591a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1592a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1593a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1594fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 15957ff19458SPaul Traina } else { 1596a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1597b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 15987ff19458SPaul Traina } 1599a0292f23SGarrett Wollman } else { 1600a0292f23SGarrett Wollman /* 1601c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1602c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1603c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1604c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1605c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1606a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1607a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1608a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1609a0292f23SGarrett Wollman */ 16104b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1611b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1612df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1613a0292f23SGarrett Wollman } 1614df8bae1dSRodney W. Grimes 1615252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: trimthenstep6: " 1616252ca428SRobert Watson "ti_locked %d", __func__, ti_locked)); 1617252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 16188501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 16197cfc6904SRobert Watson 1620df8bae1dSRodney W. Grimes /* 1621fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1622df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1623df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1624df8bae1dSRodney W. Grimes */ 1625fb59c426SYoshinobu Inoue th->th_seq++; 1626fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1627fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1628df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1629fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1630fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 163178b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 163278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1633df8bae1dSRodney W. Grimes } 1634fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1635fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1636a0292f23SGarrett Wollman /* 1637a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1638a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1639a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1640a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1641a0292f23SGarrett Wollman * Otherwise, goto step 6. 1642a0292f23SGarrett Wollman */ 1643fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1644a0292f23SGarrett Wollman goto process_ACK; 1645c068736aSJeffrey Hsu 1646df8bae1dSRodney W. Grimes goto step6; 1647c068736aSJeffrey Hsu 1648a0292f23SGarrett Wollman /* 1649a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1650c94c54e4SAndre Oppermann * do normal processing. 1651a0292f23SGarrett Wollman * 1652c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1653a0292f23SGarrett Wollman */ 1654a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1655a0292f23SGarrett Wollman case TCPS_CLOSING: 1656a0292f23SGarrett Wollman break; /* continue normal processing */ 1657df8bae1dSRodney W. Grimes } 1658df8bae1dSRodney W. Grimes 1659df8bae1dSRodney W. Grimes /* 1660df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 166180ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 166280ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 166380ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 166480ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 166580ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 166680ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 166780ab7c0eSGarrett Wollman * killing a connection). 166880ab7c0eSGarrett Wollman * Then check timestamp, if present. 1669a0292f23SGarrett Wollman * Then check the connection count, if present. 1670df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1671df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1672df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1673df8bae1dSRodney W. Grimes * 167480ab7c0eSGarrett Wollman * 167580ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 167680ab7c0eSGarrett Wollman * if this is a valid reset segment. 167780ab7c0eSGarrett Wollman * RFC 793 page 37: 167880ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 167980ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 168080ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 168180ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 168280ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 16831a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 16841a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 16851a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 16861a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 168780dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 168880dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 168980dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 169080dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 169180dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 169280dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 16938e5c87f4SBjoern A. Zeeb * If we have multiple segments in flight, the initial reset 169480ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 169580ab7c0eSGarrett Wollman * but they will eventually catch up. 169680ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 169780ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 169880ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 169980ab7c0eSGarrett Wollman * 170080ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 170180ab7c0eSGarrett Wollman * 170280ab7c0eSGarrett Wollman * DISCUSSION 170380ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 170480ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 170580ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 170680ab7c0eSGarrett Wollman * data. 170780ab7c0eSGarrett Wollman * 170880ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 170980ab7c0eSGarrett Wollman * the state: 171080ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 171180ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 171280ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1713745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 171480ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1715e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 171680ab7c0eSGarrett Wollman * Close the tcb. 1717e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 171880ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 171980ab7c0eSGarrett Wollman * RFC 1337. 172080ab7c0eSGarrett Wollman */ 1721fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 172295ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 172395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 172480ab7c0eSGarrett Wollman switch (tp->t_state) { 172580ab7c0eSGarrett Wollman 172680ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 172780ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 172880ab7c0eSGarrett Wollman goto close; 172980ab7c0eSGarrett Wollman 173080ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 1731603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 173295ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 173395ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 173495ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 173595ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 173678b50714SRobert Watson TCPSTAT_INC(tcps_badrst); 173780dd2a81SMike Silbersack goto drop; 173880dd2a81SMike Silbersack } 1739564aab1fSAndre Oppermann /* FALLTHROUGH */ 174080ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 174180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 174280ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 174380ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 174480ab7c0eSGarrett Wollman close: 1745252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1746252ca428SRobert Watson ("tcp_do_segment: TH_RST 1 ti_locked %d", 1747252ca428SRobert Watson ti_locked)); 1748252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1749252ca428SRobert Watson 175080ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 175178b50714SRobert Watson TCPSTAT_INC(tcps_drops); 175280ab7c0eSGarrett Wollman tp = tcp_close(tp); 175380ab7c0eSGarrett Wollman break; 175480ab7c0eSGarrett Wollman 175580ab7c0eSGarrett Wollman case TCPS_CLOSING: 175680ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1757252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1758252ca428SRobert Watson ("tcp_do_segment: TH_RST 2 ti_locked %d", 1759252ca428SRobert Watson ti_locked)); 1760252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1761252ca428SRobert Watson 176280ab7c0eSGarrett Wollman tp = tcp_close(tp); 176380ab7c0eSGarrett Wollman break; 176480ab7c0eSGarrett Wollman } 176580ab7c0eSGarrett Wollman } 176680ab7c0eSGarrett Wollman goto drop; 176780ab7c0eSGarrett Wollman } 176880ab7c0eSGarrett Wollman 176980ab7c0eSGarrett Wollman /* 1770df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1771df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1772df8bae1dSRodney W. Grimes */ 1773be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 177480ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1775df8bae1dSRodney W. Grimes 1776df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 17771a0e7cfcSJohn Baldwin if (ticks - tp->ts_recent_age > TCP_PAWS_IDLE) { 1778df8bae1dSRodney W. Grimes /* 1779df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1780df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1781df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1782df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1783df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1784df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1785df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1786df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1787df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1788df8bae1dSRodney W. Grimes */ 1789df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1790df8bae1dSRodney W. Grimes } else { 179178b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 179278b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, tlen); 179378b50714SRobert Watson TCPSTAT_INC(tcps_pawsdrop); 179407fd333dSMatthew Dillon if (tlen) 1795df8bae1dSRodney W. Grimes goto dropafterack; 17961ab4789dSMatthew Dillon goto drop; 17971ab4789dSMatthew Dillon } 1798df8bae1dSRodney W. Grimes } 1799df8bae1dSRodney W. Grimes 1800a0292f23SGarrett Wollman /* 180180ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 180280ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 180380ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 180480ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 180580ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 180680ab7c0eSGarrett Wollman */ 1807a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1808a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1809a57815efSBosko Milekic goto dropwithreset; 1810a57815efSBosko Milekic } 181180ab7c0eSGarrett Wollman 1812fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1813df8bae1dSRodney W. Grimes if (todrop > 0) { 181481ad7eb0SZachary Loafman /* 181581ad7eb0SZachary Loafman * If this is a duplicate SYN for our current connection, 181681ad7eb0SZachary Loafman * advance over it and pretend and it's not a SYN. 181781ad7eb0SZachary Loafman */ 181881ad7eb0SZachary Loafman if (thflags & TH_SYN && th->th_seq == tp->irs) { 1819fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1820fb59c426SYoshinobu Inoue th->th_seq++; 1821fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1822fb59c426SYoshinobu Inoue th->th_urp--; 1823df8bae1dSRodney W. Grimes else 1824fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1825df8bae1dSRodney W. Grimes todrop--; 1826df8bae1dSRodney W. Grimes } 1827df8bae1dSRodney W. Grimes /* 1828dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1829df8bae1dSRodney W. Grimes */ 1830fb59c426SYoshinobu Inoue if (todrop > tlen 1831fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1832dac20301SGarrett Wollman /* 1833dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1834dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1835dac20301SGarrett Wollman * of sequence; drop it. 1836dac20301SGarrett Wollman */ 1837fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1838dac20301SGarrett Wollman 1839df8bae1dSRodney W. Grimes /* 1840dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1841dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1842df8bae1dSRodney W. Grimes */ 1843dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1844fb59c426SYoshinobu Inoue todrop = tlen; 184578b50714SRobert Watson TCPSTAT_INC(tcps_rcvduppack); 184678b50714SRobert Watson TCPSTAT_ADD(tcps_rcvdupbyte, todrop); 1847df8bae1dSRodney W. Grimes } else { 184878b50714SRobert Watson TCPSTAT_INC(tcps_rcvpartduppack); 184978b50714SRobert Watson TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop); 1850df8bae1dSRodney W. Grimes } 1851fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1852fb59c426SYoshinobu Inoue th->th_seq += todrop; 1853fb59c426SYoshinobu Inoue tlen -= todrop; 1854fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1855fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1856df8bae1dSRodney W. Grimes else { 1857fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1858fb59c426SYoshinobu Inoue th->th_urp = 0; 1859df8bae1dSRodney W. Grimes } 1860df8bae1dSRodney W. Grimes } 1861df8bae1dSRodney W. Grimes 1862df8bae1dSRodney W. Grimes /* 1863df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1864df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1865df8bae1dSRodney W. Grimes */ 1866df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1867fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1868773673c1SAndre Oppermann char *s; 1869773673c1SAndre Oppermann 1870252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: SS_NOFDEREF && " 1871252ca428SRobert Watson "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked)); 1872252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1873252ca428SRobert Watson 1874773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 1875e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 1876773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 1877e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 1878773673c1SAndre Oppermann free(s, M_TCPLOG); 1879773673c1SAndre Oppermann } 1880df8bae1dSRodney W. Grimes tp = tcp_close(tp); 188178b50714SRobert Watson TCPSTAT_INC(tcps_rcvafterclose); 1882a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1883df8bae1dSRodney W. Grimes goto dropwithreset; 1884df8bae1dSRodney W. Grimes } 1885df8bae1dSRodney W. Grimes 1886df8bae1dSRodney W. Grimes /* 1887df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1888df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1889df8bae1dSRodney W. Grimes */ 1890fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1891df8bae1dSRodney W. Grimes if (todrop > 0) { 189278b50714SRobert Watson TCPSTAT_INC(tcps_rcvpackafterwin); 1893fb59c426SYoshinobu Inoue if (todrop >= tlen) { 189478b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen); 1895df8bae1dSRodney W. Grimes /* 1896df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1897df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1898df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1899df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1900df8bae1dSRodney W. Grimes * and ack. 1901df8bae1dSRodney W. Grimes */ 1902fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1903df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 190478b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinprobe); 1905df8bae1dSRodney W. Grimes } else 1906df8bae1dSRodney W. Grimes goto dropafterack; 1907df8bae1dSRodney W. Grimes } else 190878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop); 1909df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1910fb59c426SYoshinobu Inoue tlen -= todrop; 1911fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1912df8bae1dSRodney W. Grimes } 1913df8bae1dSRodney W. Grimes 1914df8bae1dSRodney W. Grimes /* 1915df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1916df8bae1dSRodney W. Grimes * record its timestamp. 1917cf09195bSPaul Saab * NOTE: 1918cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1919a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1920cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1921cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1922cf09195bSPaul Saab * predicated on the sequence space of this segment. 1923cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1924cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1925cf09195bSPaul Saab * instead of RFC1323's 1926cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1927cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1928cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1929cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1930cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1931df8bae1dSRodney W. Grimes */ 1932be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1933cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1934cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1935cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 19369b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1937a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1938df8bae1dSRodney W. Grimes } 1939df8bae1dSRodney W. Grimes 1940df8bae1dSRodney W. Grimes /* 1941df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1942df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1943df8bae1dSRodney W. Grimes */ 1944fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1945252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, 1946252ca428SRobert Watson ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked)); 1947252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 1948252ca428SRobert Watson 1949df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1950a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1951122aad88SAndre Oppermann goto drop; 1952df8bae1dSRodney W. Grimes } 1953df8bae1dSRodney W. Grimes 1954a0292f23SGarrett Wollman /* 1955a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1956a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1957a0292f23SGarrett Wollman * later processing; else drop segment and return. 1958a0292f23SGarrett Wollman */ 1959fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1960a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1961a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1962a0292f23SGarrett Wollman goto step6; 19635e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 19645e1aa279SDavid Malone goto dropafterack; 1965a0292f23SGarrett Wollman else 1966a0292f23SGarrett Wollman goto drop; 1967a0292f23SGarrett Wollman } 1968df8bae1dSRodney W. Grimes 1969df8bae1dSRodney W. Grimes /* 1970df8bae1dSRodney W. Grimes * Ack processing. 1971df8bae1dSRodney W. Grimes */ 1972df8bae1dSRodney W. Grimes switch (tp->t_state) { 1973df8bae1dSRodney W. Grimes 1974df8bae1dSRodney W. Grimes /* 1975764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1976764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1977764d8cefSBill Fenner * The ACK was checked above. 1978df8bae1dSRodney W. Grimes */ 1979df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1980a0292f23SGarrett Wollman 198178b50714SRobert Watson TCPSTAT_INC(tcps_connects); 1982df8bae1dSRodney W. Grimes soisconnected(so); 1983df8bae1dSRodney W. Grimes /* Do window scaling? */ 1984df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1985df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1986df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1987464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1988df8bae1dSRodney W. Grimes } 1989a0292f23SGarrett Wollman /* 1990a0292f23SGarrett Wollman * Make transitions: 1991a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1992a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1993a0292f23SGarrett Wollman */ 19949b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1995a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1996a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1997a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 19987ff19458SPaul Traina } else { 1999a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 2000b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 20017ff19458SPaul Traina } 2002a0292f23SGarrett Wollman /* 2003a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 2004a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 2005a0292f23SGarrett Wollman */ 2006fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 2007fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 2008a0292f23SGarrett Wollman (struct mbuf *)0); 2009fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 201093b0017fSPhilippe Charnier /* FALLTHROUGH */ 2011df8bae1dSRodney W. Grimes 2012df8bae1dSRodney W. Grimes /* 2013df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 2014df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 2015fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 2016fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 2017df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 2018df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 2019df8bae1dSRodney W. Grimes */ 2020df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2021df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2022df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2023df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 2024df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2025df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 20265a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 202778b50714SRobert Watson TCPSTAT_INC(tcps_rcvacktoomuch); 20285a53ca16SPaul Saab goto dropafterack; 20295a53ca16SPaul Saab } 20303529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2031fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 2032fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 20335a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 2034fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 2035fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 203678b50714SRobert Watson TCPSTAT_INC(tcps_rcvdupack); 2037df8bae1dSRodney W. Grimes /* 2038df8bae1dSRodney W. Grimes * If we have outstanding data (other than 2039df8bae1dSRodney W. Grimes * a window probe), this is a completely 2040df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 2041df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 2042df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 2043df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 2044df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 2045df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 2046df8bae1dSRodney W. Grimes * window so we send only this one 2047df8bae1dSRodney W. Grimes * packet. 2048df8bae1dSRodney W. Grimes * 2049df8bae1dSRodney W. Grimes * We know we're losing at the current 2050df8bae1dSRodney W. Grimes * window size so do congestion avoidance 2051df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 2052df8bae1dSRodney W. Grimes * and pull our congestion window back to 2053df8bae1dSRodney W. Grimes * the new ssthresh). 2054df8bae1dSRodney W. Grimes * 2055df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 2056df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 2057df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 2058df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 2059df8bae1dSRodney W. Grimes * network. 2060f2512ba1SRui Paulo * 2061f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 2062f2512ba1SRui Paulo * we reduced the cwnd. 2063df8bae1dSRodney W. Grimes */ 2064b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 2065fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 2066df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2067cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 2068603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 20693529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 20709d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 20713529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 20723529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 2073808f11b7SPaul Saab int awnd; 207425e6f9edSPaul Saab 207525e6f9edSPaul Saab /* 207625e6f9edSPaul Saab * Compute the amount of data in flight first. 207725e6f9edSPaul Saab * We can inject new data into the pipe iff 207825e6f9edSPaul Saab * we have less than 1/2 the original window's 207925e6f9edSPaul Saab * worth of data in flight. 208025e6f9edSPaul Saab */ 2081808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 20820077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 2083808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 208425e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 208525e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 208625e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 208725e6f9edSPaul Saab } 208825e6f9edSPaul Saab } else 208946f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 209046f58482SJonathan Lemon (void) tcp_output(tp); 209146f58482SJonathan Lemon goto drop; 2092cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 2093cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 2094a0445c2eSJayanth Vijayaraghavan 2095a0445c2eSJayanth Vijayaraghavan /* 2096a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 2097a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 2098a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 2099a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 2100a0445c2eSJayanth Vijayaraghavan * recovery. 2101a0445c2eSJayanth Vijayaraghavan */ 21023529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 2103a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 2104a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 2105a0445c2eSJayanth Vijayaraghavan break; 2106a0445c2eSJayanth Vijayaraghavan } 2107603724d3SBjoern A. Zeeb } else if (V_tcp_do_newreno || 2108603724d3SBjoern A. Zeeb V_tcp_do_ecn) { 2109a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 21109d11646dSJeffrey Hsu tp->snd_recover)) { 2111cb942153SJeffrey Hsu tp->t_dupacks = 0; 2112cb942153SJeffrey Hsu break; 211346f58482SJonathan Lemon } 2114a0445c2eSJayanth Vijayaraghavan } 2115f2512ba1SRui Paulo tcp_congestion_exp(tp); 2116b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 21179b8b58e0SJonathan Lemon tp->t_rtttime = 0; 21183529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 211978b50714SRobert Watson TCPSTAT_INC( 212078b50714SRobert Watson tcps_sack_recovery_episode); 2121a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 21228db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 21236d90faf3SPaul Saab (void) tcp_output(tp); 21246d90faf3SPaul Saab goto drop; 21256d90faf3SPaul Saab } 2126fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 2127df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 2128df8bae1dSRodney W. Grimes (void) tcp_output(tp); 212948d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 2130302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 2131302ce8d6SAndre Oppermann __func__)); 2132df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 213348d2549cSJeffrey Hsu tp->t_maxseg * 213448d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 2135df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 2136df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 2137df8bae1dSRodney W. Grimes goto drop; 2138603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 2139582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 214048d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 214148d2549cSJeffrey Hsu u_int sent; 214289c02376SJeffrey Hsu 2143582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 2144582a954bSJeffrey Hsu tp->t_dupacks == 2, 2145302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 2146302ce8d6SAndre Oppermann __func__)); 214761a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 214848d2549cSJeffrey Hsu tp->snd_limited = 0; 214961a36e3dSJeffrey Hsu tp->snd_cwnd = 215061a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 215161a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 215261a36e3dSJeffrey Hsu tp->t_maxseg; 2153582a954bSJeffrey Hsu (void) tcp_output(tp); 215448d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 215548d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 215689c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 215789c02376SJeffrey Hsu tp->snd_limited == 0) || 215889c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 215989c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 2160302ce8d6SAndre Oppermann ("%s: sent too much", 2161302ce8d6SAndre Oppermann __func__)); 216248d2549cSJeffrey Hsu tp->snd_limited = 2; 216348d2549cSJeffrey Hsu } else if (sent > 0) 216448d2549cSJeffrey Hsu ++tp->snd_limited; 2165582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 2166582a954bSJeffrey Hsu goto drop; 2167df8bae1dSRodney W. Grimes } 2168df8bae1dSRodney W. Grimes } else 2169df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 2170df8bae1dSRodney W. Grimes break; 2171df8bae1dSRodney W. Grimes } 2172cb942153SJeffrey Hsu 2173302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 2174302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 2175cb942153SJeffrey Hsu 2176df8bae1dSRodney W. Grimes /* 2177df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 2178df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 2179df8bae1dSRodney W. Grimes */ 2180603724d3SBjoern A. Zeeb if (V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 21819d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 2182cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 21833529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 21846d90faf3SPaul Saab tcp_sack_partialack(tp, th); 21856d90faf3SPaul Saab else 2186c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 2187c068736aSJeffrey Hsu } else { 2188c068736aSJeffrey Hsu /* 21896d90faf3SPaul Saab * Out of fast recovery. 2190c068736aSJeffrey Hsu * Window inflation should have left us 2191c068736aSJeffrey Hsu * with approximately snd_ssthresh 2192c068736aSJeffrey Hsu * outstanding data. 2193c068736aSJeffrey Hsu * But in case we would be inclined to 2194c068736aSJeffrey Hsu * send a burst, better to do it via 2195c068736aSJeffrey Hsu * the slow start mechanism. 2196c068736aSJeffrey Hsu */ 2197c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 2198c068736aSJeffrey Hsu tp->snd_ssthresh, 2199c068736aSJeffrey Hsu tp->snd_max)) 2200c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 2201c068736aSJeffrey Hsu th->th_ack + 2202c068736aSJeffrey Hsu tp->t_maxseg; 2203c068736aSJeffrey Hsu else 2204c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 2205c068736aSJeffrey Hsu } 2206c068736aSJeffrey Hsu } 2207c068736aSJeffrey Hsu } else { 2208233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 2209df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 2210df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 221146f58482SJonathan Lemon } 2212cb942153SJeffrey Hsu tp->t_dupacks = 0; 2213a0292f23SGarrett Wollman /* 2214a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2215a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2216a0292f23SGarrett Wollman */ 2217a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2218a0292f23SGarrett Wollman /* 2219a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2220a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 222107e43e10SAndras Olah * synchronized). Go to non-starred state, 222207e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 222307e43e10SAndras Olah * we can do window scaling. 2224a0292f23SGarrett Wollman */ 2225a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2226a0292f23SGarrett Wollman tp->snd_una++; 222707e43e10SAndras Olah /* Do window scaling? */ 222807e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 222907e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 223007e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2231464fcfbcSAndre Oppermann /* Send window already scaled. */ 223207e43e10SAndras Olah } 2233a0292f23SGarrett Wollman } 2234a0292f23SGarrett Wollman 2235a0292f23SGarrett Wollman process_ACK: 2236252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2237252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2238252ca428SRobert Watson ("tcp_input: process_ACK ti_locked %d", ti_locked)); 22398501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 22407cfc6904SRobert Watson 2241fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 224278b50714SRobert Watson TCPSTAT_INC(tcps_rcvackpack); 224378b50714SRobert Watson TCPSTAT_ADD(tcps_rcvackbyte, acked); 2244df8bae1dSRodney W. Grimes 2245df8bae1dSRodney W. Grimes /* 22469b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 22479b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 22489b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 22499b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 22509b8b58e0SJonathan Lemon * we left off. 22519b8b58e0SJonathan Lemon */ 22526dfb8b31SJohn Baldwin if (tp->t_rxtshift == 1 && (int)(ticks - tp->t_badrxtwin) < 0) { 225378b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitbad); 22549b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 22559b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 22569d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 22579d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 22589d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 22599b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 22609b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 22619b8b58e0SJonathan Lemon } 22629b8b58e0SJonathan Lemon 22639b8b58e0SJonathan Lemon /* 2264df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2265df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2266df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2267df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2268df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2269df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2270df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2271fa55172bSMatthew Dillon * 2272fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2273fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2274fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2275fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2276df8bae1dSRodney W. Grimes */ 2277fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2278fa55172bSMatthew Dillon to.to_tsecr) { 2279eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2280eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 22819b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2282fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2283eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2284eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 22859b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2286fa55172bSMatthew Dillon } 22871fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2288df8bae1dSRodney W. Grimes 2289df8bae1dSRodney W. Grimes /* 2290df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2291df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2292df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2293df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2294df8bae1dSRodney W. Grimes */ 2295fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2296b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2297df8bae1dSRodney W. Grimes needoutput = 1; 2298b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2299b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2300a0292f23SGarrett Wollman 2301a0292f23SGarrett Wollman /* 2302a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2303a0292f23SGarrett Wollman * skip rest of ACK processing. 2304a0292f23SGarrett Wollman */ 2305a0292f23SGarrett Wollman if (acked == 0) 2306a0292f23SGarrett Wollman goto step6; 2307a0292f23SGarrett Wollman 2308df8bae1dSRodney W. Grimes /* 2309df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 231024cb0f22SLawrence Stewart * Method depends on which congestion control state we're 231124cb0f22SLawrence Stewart * in (slow start or cong avoid) and if ABC (RFC 3465) is 231224cb0f22SLawrence Stewart * enabled. 231324cb0f22SLawrence Stewart * 231424cb0f22SLawrence Stewart * slow start: cwnd <= ssthresh 231524cb0f22SLawrence Stewart * cong avoid: cwnd > ssthresh 231624cb0f22SLawrence Stewart * 231724cb0f22SLawrence Stewart * slow start and ABC (RFC 3465): 231824cb0f22SLawrence Stewart * Grow cwnd exponentially by the amount of data 231924cb0f22SLawrence Stewart * ACKed capping the max increment per ACK to 232024cb0f22SLawrence Stewart * (abc_l_var * maxseg) bytes. 232124cb0f22SLawrence Stewart * 232224cb0f22SLawrence Stewart * slow start without ABC (RFC 2581): 232324cb0f22SLawrence Stewart * Grow cwnd exponentially by maxseg per ACK. 232424cb0f22SLawrence Stewart * 232524cb0f22SLawrence Stewart * cong avoid and ABC (RFC 3465): 232624cb0f22SLawrence Stewart * Grow cwnd linearly by maxseg per RTT for each 232724cb0f22SLawrence Stewart * cwnd worth of ACKed data. 232824cb0f22SLawrence Stewart * 232924cb0f22SLawrence Stewart * cong avoid without ABC (RFC 2581): 233024cb0f22SLawrence Stewart * Grow cwnd linearly by approximately maxseg per RTT using 233124cb0f22SLawrence Stewart * maxseg^2 / cwnd per ACK as the increment. 233224cb0f22SLawrence Stewart * If cwnd > maxseg^2, fix the cwnd increment at 1 byte to 233324cb0f22SLawrence Stewart * avoid capping cwnd. 2334df8bae1dSRodney W. Grimes */ 2335603724d3SBjoern A. Zeeb if ((!V_tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 23366d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2337ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2338ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 233924cb0f22SLawrence Stewart /* In congestion avoidance? */ 234024cb0f22SLawrence Stewart if (cw > tp->snd_ssthresh) { 234124cb0f22SLawrence Stewart if (V_tcp_do_rfc3465) { 234224cb0f22SLawrence Stewart tp->t_bytes_acked += acked; 234324cb0f22SLawrence Stewart if (tp->t_bytes_acked >= tp->snd_cwnd) 234424cb0f22SLawrence Stewart tp->t_bytes_acked -= cw; 234524cb0f22SLawrence Stewart else 234624cb0f22SLawrence Stewart incr = 0; 234724cb0f22SLawrence Stewart } 234824cb0f22SLawrence Stewart else 2349c10eb6d1SBjoern A. Zeeb incr = max((incr * incr / cw), 1); 235024cb0f22SLawrence Stewart /* 235124cb0f22SLawrence Stewart * In slow-start with ABC enabled and no RTO in sight? 235224cb0f22SLawrence Stewart * (Must not use abc_l_var > 1 if slow starting after an 235324cb0f22SLawrence Stewart * RTO. On RTO, snd_nxt = snd_una, so the snd_nxt == 235424cb0f22SLawrence Stewart * snd_max check is sufficient to handle this). 235524cb0f22SLawrence Stewart */ 235624cb0f22SLawrence Stewart } else if (V_tcp_do_rfc3465 && 235724cb0f22SLawrence Stewart tp->snd_nxt == tp->snd_max) 235824cb0f22SLawrence Stewart incr = min(acked, 235924cb0f22SLawrence Stewart V_tcp_abc_l_var * tp->t_maxseg); 236024cb0f22SLawrence Stewart /* ABC is on by default, so (incr == 0) frequently. */ 236124cb0f22SLawrence Stewart if (incr > 0) 2362df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2363df8bae1dSRodney W. Grimes } 23645905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2365df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2366df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 23675905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2368df8bae1dSRodney W. Grimes ourfinisacked = 1; 2369df8bae1dSRodney W. Grimes } else { 23705905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2371df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2372df8bae1dSRodney W. Grimes ourfinisacked = 0; 2373df8bae1dSRodney W. Grimes } 2374564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 23751e4d7da7SRobert Watson sowwakeup_locked(so); 2376e8949f74SAndre Oppermann /* Detect una wraparound. */ 2377603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23786d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 23799d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 23809d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 23819d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2382603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 23836d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 238424cb0f22SLawrence Stewart SEQ_GEQ(th->th_ack, tp->snd_recover)) { 23859d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 238624cb0f22SLawrence Stewart tp->t_bytes_acked = 0; 238724cb0f22SLawrence Stewart } 2388fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 23893529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 23906d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 23916d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 23926d90faf3SPaul Saab } 2393df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2394df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2395df8bae1dSRodney W. Grimes 2396df8bae1dSRodney W. Grimes switch (tp->t_state) { 2397df8bae1dSRodney W. Grimes 2398df8bae1dSRodney W. Grimes /* 2399df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2400df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2401df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2402df8bae1dSRodney W. Grimes */ 2403df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2404df8bae1dSRodney W. Grimes if (ourfinisacked) { 2405df8bae1dSRodney W. Grimes /* 2406df8bae1dSRodney W. Grimes * If we can't receive any more 2407df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2408df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2409df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2410df8bae1dSRodney W. Grimes * we'll hang forever. 2411e8949f74SAndre Oppermann * 2412e8949f74SAndre Oppermann * XXXjl: 2413340c35deSJonathan Lemon * we should release the tp also, and use a 2414340c35deSJonathan Lemon * compressed state. 2415340c35deSJonathan Lemon */ 2416c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 24177c72af87SMohan Srinivasan int timeout; 24187c72af87SMohan Srinivasan 241903e49181SSeigo Tanimura soisdisconnected(so); 24207c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 24217c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2422b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 24234cc20ab1SSeigo Tanimura } 2424df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2425df8bae1dSRodney W. Grimes } 2426df8bae1dSRodney W. Grimes break; 2427df8bae1dSRodney W. Grimes 2428df8bae1dSRodney W. Grimes /* 2429df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2430df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2431df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2432df8bae1dSRodney W. Grimes * the segment. 2433df8bae1dSRodney W. Grimes */ 2434df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2435df8bae1dSRodney W. Grimes if (ourfinisacked) { 2436252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 243711a20fb8SJeffrey Hsu tcp_twstart(tp); 2438603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2439340c35deSJonathan Lemon m_freem(m); 2440679d9708SAndre Oppermann return; 2441df8bae1dSRodney W. Grimes } 2442df8bae1dSRodney W. Grimes break; 2443df8bae1dSRodney W. Grimes 2444df8bae1dSRodney W. Grimes /* 2445df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2446df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2447df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2448df8bae1dSRodney W. Grimes * enter the closed state and return. 2449df8bae1dSRodney W. Grimes */ 2450df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2451df8bae1dSRodney W. Grimes if (ourfinisacked) { 2452252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2453df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2454df8bae1dSRodney W. Grimes goto drop; 2455df8bae1dSRodney W. Grimes } 2456df8bae1dSRodney W. Grimes break; 2457df8bae1dSRodney W. Grimes } 2458df8bae1dSRodney W. Grimes } 2459df8bae1dSRodney W. Grimes 2460df8bae1dSRodney W. Grimes step6: 2461252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2462252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2463252ca428SRobert Watson ("tcp_do_segment: step6 ti_locked %d", ti_locked)); 24648501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 24657cfc6904SRobert Watson 2466df8bae1dSRodney W. Grimes /* 2467df8bae1dSRodney W. Grimes * Update window information. 2468df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2469df8bae1dSRodney W. Grimes */ 2470fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2471fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2472fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2473fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2474df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2475fb59c426SYoshinobu Inoue if (tlen == 0 && 2476fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 247778b50714SRobert Watson TCPSTAT_INC(tcps_rcvwinupd); 2478df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2479fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2480fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2481df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2482df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2483df8bae1dSRodney W. Grimes needoutput = 1; 2484df8bae1dSRodney W. Grimes } 2485df8bae1dSRodney W. Grimes 2486df8bae1dSRodney W. Grimes /* 2487df8bae1dSRodney W. Grimes * Process segments with URG. 2488df8bae1dSRodney W. Grimes */ 2489fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2490df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2491df8bae1dSRodney W. Grimes /* 2492df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2493df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2494df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2495df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2496df8bae1dSRodney W. Grimes */ 249718ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2498fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2499fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2500fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 250118ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2502df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2503df8bae1dSRodney W. Grimes } 2504df8bae1dSRodney W. Grimes /* 2505df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2506df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2507df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2508df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2509df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2510df8bae1dSRodney W. Grimes * 2511df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2512df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2513df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2514df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2515df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2516df8bae1dSRodney W. Grimes * spec states (in one of two places). 2517df8bae1dSRodney W. Grimes */ 2518fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2519fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2520df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2521df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2522927c5ceaSRobert Watson if (so->so_oobmark == 0) 2523c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2524df8bae1dSRodney W. Grimes sohasoutofband(so); 2525df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2526df8bae1dSRodney W. Grimes } 252718ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2528df8bae1dSRodney W. Grimes /* 2529df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2530df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2531df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2532df8bae1dSRodney W. Grimes * data may creep in... ick. 2533df8bae1dSRodney W. Grimes */ 253430613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 253530613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 253630613f56SJeffrey Hsu /* hdr drop is delayed */ 253730613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 253830613f56SJeffrey Hsu } 2539c068736aSJeffrey Hsu } else { 2540df8bae1dSRodney W. Grimes /* 2541df8bae1dSRodney W. Grimes * If no out of band data is expected, 2542df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2543df8bae1dSRodney W. Grimes * with the receive window. 2544df8bae1dSRodney W. Grimes */ 2545df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2546df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2547c068736aSJeffrey Hsu } 2548df8bae1dSRodney W. Grimes dodata: /* XXX */ 2549252ca428SRobert Watson INP_INFO_LOCK_ASSERT(&V_tcbinfo); 2550252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2551252ca428SRobert Watson ("tcp_do_segment: dodata ti_locked %d", ti_locked)); 25528501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25537cfc6904SRobert Watson 2554df8bae1dSRodney W. Grimes /* 2555df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2556df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2557df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2558df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2559df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2560df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2561df8bae1dSRodney W. Grimes */ 2562fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2563df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 256469e03620SPaul Saab tcp_seq save_start = th->th_seq; 2565fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2566e4b64281SJesper Skriver /* 2567c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2568c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2569c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2570c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2571c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2572c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2573c068736aSJeffrey Hsu * conversions. 2574c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2575c068736aSJeffrey Hsu * immediately when segments are out of order (so 2576c068736aSJeffrey Hsu * fast retransmit can work). 2577e4b64281SJesper Skriver */ 2578e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2579e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2580e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2581e4b64281SJesper Skriver if (DELAY_ACK(tp)) 25823bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2583e4b64281SJesper Skriver else 2584e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2585e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2586e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 258778b50714SRobert Watson TCPSTAT_INC(tcps_rcvpack); 258878b50714SRobert Watson TCPSTAT_ADD(tcps_rcvbyte, tlen); 2589e4b64281SJesper Skriver ND6_HINT(tp); 25901e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2591c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2592c1c36a2cSMike Silbersack m_freem(m); 2593c1c36a2cSMike Silbersack else 25941e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2595e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 25961e4d7da7SRobert Watson sorwakeup_locked(so); 2597e4b64281SJesper Skriver } else { 2598e8949f74SAndre Oppermann /* 2599e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2600e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2601e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2602e8949f74SAndre Oppermann * when trimming from the head. 2603e8949f74SAndre Oppermann */ 2604e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2605e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2606e4b64281SJesper Skriver } 26073529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2608f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2609302ce8d6SAndre Oppermann #if 0 2610df8bae1dSRodney W. Grimes /* 2611df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2612df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2613df8bae1dSRodney W. Grimes * buffer size. 2614302ce8d6SAndre Oppermann * XXX: Unused. 2615df8bae1dSRodney W. Grimes */ 2616df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2617302ce8d6SAndre Oppermann #endif 2618df8bae1dSRodney W. Grimes } else { 2619df8bae1dSRodney W. Grimes m_freem(m); 2620fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2621df8bae1dSRodney W. Grimes } 2622df8bae1dSRodney W. Grimes 2623df8bae1dSRodney W. Grimes /* 2624df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2625df8bae1dSRodney W. Grimes * that the connection is closing. 2626df8bae1dSRodney W. Grimes */ 2627fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2628df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2629df8bae1dSRodney W. Grimes socantrcvmore(so); 2630a0292f23SGarrett Wollman /* 2631a0292f23SGarrett Wollman * If connection is half-synchronized 2632d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2633a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2634a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2635a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2636a0292f23SGarrett Wollman */ 26373bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 26383bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2639a0292f23SGarrett Wollman else 2640df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2641df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2642df8bae1dSRodney W. Grimes } 2643df8bae1dSRodney W. Grimes switch (tp->t_state) { 2644df8bae1dSRodney W. Grimes 2645df8bae1dSRodney W. Grimes /* 2646df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2647df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2648df8bae1dSRodney W. Grimes */ 2649df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 26509b8b58e0SJonathan Lemon tp->t_starttime = ticks; 26519b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2652df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2653df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2654df8bae1dSRodney W. Grimes break; 2655df8bae1dSRodney W. Grimes 2656df8bae1dSRodney W. Grimes /* 2657df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2658df8bae1dSRodney W. Grimes * enter the CLOSING state. 2659df8bae1dSRodney W. Grimes */ 2660df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2661df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2662df8bae1dSRodney W. Grimes break; 2663df8bae1dSRodney W. Grimes 2664df8bae1dSRodney W. Grimes /* 2665df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2666df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2667df8bae1dSRodney W. Grimes * standard timers. 2668df8bae1dSRodney W. Grimes */ 2669df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2670252ca428SRobert Watson INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 2671252ca428SRobert Watson KASSERT(ti_locked == TI_WLOCKED, ("%s: dodata " 2672252ca428SRobert Watson "TCP_FIN_WAIT_2 ti_locked: %d", __func__, 2673252ca428SRobert Watson ti_locked)); 2674252ca428SRobert Watson 2675340c35deSJonathan Lemon tcp_twstart(tp); 2676603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2677679d9708SAndre Oppermann return; 2678df8bae1dSRodney W. Grimes } 2679df8bae1dSRodney W. Grimes } 2680252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2681252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2682252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2683603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2684252ca428SRobert Watson else 2685252ca428SRobert Watson panic("%s: dodata epilogue ti_locked %d", __func__, 2686252ca428SRobert Watson ti_locked); 2687252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2688252ca428SRobert Watson 2689610ee2f9SDavid Greenman #ifdef TCPDEBUG 26904cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2691fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2692fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2693610ee2f9SDavid Greenman #endif 2694df8bae1dSRodney W. Grimes 2695df8bae1dSRodney W. Grimes /* 2696df8bae1dSRodney W. Grimes * Return any desired output. 2697df8bae1dSRodney W. Grimes */ 2698df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2699df8bae1dSRodney W. Grimes (void) tcp_output(tp); 27007792ea27SJeffrey Hsu 2701a14c749fSJonathan Lemon check_delack: 2702252ca428SRobert Watson KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d", 2703252ca428SRobert Watson __func__, ti_locked)); 2704603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 27058501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2706252ca428SRobert Watson 2707a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 27083bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2709b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 27103bfd6421SJonathan Lemon } 27118501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2712679d9708SAndre Oppermann return; 2713df8bae1dSRodney W. Grimes 2714df8bae1dSRodney W. Grimes dropafterack: 2715252ca428SRobert Watson KASSERT(ti_locked == TI_RLOCKED || ti_locked == TI_WLOCKED, 2716252ca428SRobert Watson ("tcp_do_segment: dropafterack ti_locked %d", ti_locked)); 2717252ca428SRobert Watson 2718df8bae1dSRodney W. Grimes /* 2719df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2720df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 272180ab7c0eSGarrett Wollman * 272280ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 272380ab7c0eSGarrett Wollman * paths to this code happen after packets containing 272480ab7c0eSGarrett Wollman * RST have been dropped. 272580ab7c0eSGarrett Wollman * 272680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 272780ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 272880ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 272980ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 273080ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 273180ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2732df8bae1dSRodney W. Grimes */ 2733fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2734fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2735a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2736a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2737a57815efSBosko Milekic goto dropwithreset; 2738a57815efSBosko Milekic } 2739a0292f23SGarrett Wollman #ifdef TCPDEBUG 27404cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2741fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2742fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2743a0292f23SGarrett Wollman #endif 2744252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2745252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2746252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2747603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2748252ca428SRobert Watson else 2749252ca428SRobert Watson panic("%s: dropafterack epilogue ti_locked %d", __func__, 2750252ca428SRobert Watson ti_locked); 2751252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2752252ca428SRobert Watson 2753df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2754df8bae1dSRodney W. Grimes (void) tcp_output(tp); 27558501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2756d6915262SRobert Watson m_freem(m); 2757679d9708SAndre Oppermann return; 2758df8bae1dSRodney W. Grimes 2759df8bae1dSRodney W. Grimes dropwithreset: 2760252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2761252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2762252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2763dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2764252ca428SRobert Watson else 2765252ca428SRobert Watson panic("%s: dropwithreset ti_locked %d", __func__, ti_locked); 2766252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2767a57815efSBosko Milekic 2768a0ca0871SRobert Watson if (tp != NULL) { 2769302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 27708501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2771dd8ac7f9SRobert Watson } else 2772a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2773679d9708SAndre Oppermann return; 2774df8bae1dSRodney W. Grimes 2775df8bae1dSRodney W. Grimes drop: 2776252ca428SRobert Watson if (ti_locked == TI_RLOCKED) 2777252ca428SRobert Watson INP_INFO_RUNLOCK(&V_tcbinfo); 2778252ca428SRobert Watson else if (ti_locked == TI_WLOCKED) 2779252ca428SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2780252ca428SRobert Watson #ifdef INVARIANTS 2781252ca428SRobert Watson else 2782252ca428SRobert Watson INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 2783252ca428SRobert Watson #endif 2784252ca428SRobert Watson ti_locked = TI_UNLOCKED; 2785252ca428SRobert Watson 2786df8bae1dSRodney W. Grimes /* 2787df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2788df8bae1dSRodney W. Grimes */ 2789610ee2f9SDavid Greenman #ifdef TCPDEBUG 27901c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2791fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2792fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2793a0292f23SGarrett Wollman #endif 27941c53f806SRobert Watson if (tp != NULL) 27958501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2796d6915262SRobert Watson m_freem(m); 2797302ce8d6SAndre Oppermann } 2798302ce8d6SAndre Oppermann 2799302ce8d6SAndre Oppermann /* 28000ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 28010ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 28020ca3f933SAndre Oppermann * tp may be NULL. 2803302ce8d6SAndre Oppermann */ 2804302ce8d6SAndre Oppermann static void 2805302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2806302ce8d6SAndre Oppermann int tlen, int rstreason) 2807302ce8d6SAndre Oppermann { 2808302ce8d6SAndre Oppermann struct ip *ip; 2809302ce8d6SAndre Oppermann #ifdef INET6 2810302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2811302ce8d6SAndre Oppermann #endif 28127a3244ccSRobert Watson 28137a3244ccSRobert Watson if (tp != NULL) { 28148501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28157a3244ccSRobert Watson } 28167a3244ccSRobert Watson 28170ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2818302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2819302ce8d6SAndre Oppermann goto drop; 2820302ce8d6SAndre Oppermann #ifdef INET6 2821302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2822302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2823302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2824302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2825302ce8d6SAndre Oppermann goto drop; 2826302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2827302ce8d6SAndre Oppermann } else 2828302ce8d6SAndre Oppermann #endif 2829302ce8d6SAndre Oppermann { 2830302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2831302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2832302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2833302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2834302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2835302ce8d6SAndre Oppermann goto drop; 2836302ce8d6SAndre Oppermann } 2837302ce8d6SAndre Oppermann 2838302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2839302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2840302ce8d6SAndre Oppermann goto drop; 2841302ce8d6SAndre Oppermann 2842302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2843302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2844302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2845302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2846302ce8d6SAndre Oppermann } else { 2847302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2848302ce8d6SAndre Oppermann tlen++; 2849302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2850302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2851302ce8d6SAndre Oppermann } 2852302ce8d6SAndre Oppermann return; 2853302ce8d6SAndre Oppermann drop: 2854302ce8d6SAndre Oppermann m_freem(m); 2855df8bae1dSRodney W. Grimes } 2856df8bae1dSRodney W. Grimes 2857be2ac88cSJonathan Lemon /* 2858be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2859be2ac88cSJonathan Lemon */ 28600312fbe9SPoul-Henning Kamp static void 2861ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2862df8bae1dSRodney W. Grimes { 2863df8bae1dSRodney W. Grimes int opt, optlen; 2864df8bae1dSRodney W. Grimes 2865be2ac88cSJonathan Lemon to->to_flags = 0; 2866df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2867df8bae1dSRodney W. Grimes opt = cp[0]; 2868df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2869df8bae1dSRodney W. Grimes break; 2870df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2871df8bae1dSRodney W. Grimes optlen = 1; 2872df8bae1dSRodney W. Grimes else { 2873b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2874b474779fSJun-ichiro itojun Hagino break; 2875df8bae1dSRodney W. Grimes optlen = cp[1]; 2876b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2877df8bae1dSRodney W. Grimes break; 2878df8bae1dSRodney W. Grimes } 2879df8bae1dSRodney W. Grimes switch (opt) { 2880df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2881df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2882df8bae1dSRodney W. Grimes continue; 2883f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2884df8bae1dSRodney W. Grimes continue; 2885be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2886be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2887be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2888fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2889df8bae1dSRodney W. Grimes break; 2890df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2891df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2892df8bae1dSRodney W. Grimes continue; 2893f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2894df8bae1dSRodney W. Grimes continue; 2895be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 289602a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2897df8bae1dSRodney W. Grimes break; 2898df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2899df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2900df8bae1dSRodney W. Grimes continue; 2901be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2902a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2903a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2904fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2905a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2906a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2907fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2908df8bae1dSRodney W. Grimes break; 29091cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 29101cfd4b53SBruce M Simpson /* 29111cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 29121cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 29131cfd4b53SBruce M Simpson * record the fact that the option was observed here 29141cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 29151cfd4b53SBruce M Simpson */ 29161cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 29171cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 29181cfd4b53SBruce M Simpson continue; 2919df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2920df47e437SAndre Oppermann to->to_signature = cp + 2; 29211cfd4b53SBruce M Simpson break; 2922265ed012SBruce M Simpson #endif 29236d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2924f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 29256d90faf3SPaul Saab continue; 2926f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2927f72167f4SAndre Oppermann continue; 2928603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 2929f72167f4SAndre Oppermann continue; 2930fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 29316d90faf3SPaul Saab break; 29326d90faf3SPaul Saab case TCPOPT_SACK: 29335a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 29346d90faf3SPaul Saab continue; 2935b728e902SAndre Oppermann if (flags & TO_SYN) 2936b728e902SAndre Oppermann continue; 2937fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 29385a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 29395a53ca16SPaul Saab to->to_sacks = cp + 2; 294078b50714SRobert Watson TCPSTAT_INC(tcps_sack_rcv_blocks); 29416d90faf3SPaul Saab break; 2942be2ac88cSJonathan Lemon default: 2943be2ac88cSJonathan Lemon continue; 2944df8bae1dSRodney W. Grimes } 2945df8bae1dSRodney W. Grimes } 2946df8bae1dSRodney W. Grimes } 2947df8bae1dSRodney W. Grimes 2948df8bae1dSRodney W. Grimes /* 2949df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2950df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2951df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2952df8bae1dSRodney W. Grimes * sequencing purposes. 2953df8bae1dSRodney W. Grimes */ 29540312fbe9SPoul-Henning Kamp static void 2955ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2956ad3f9ab3SAndre Oppermann int off) 2957df8bae1dSRodney W. Grimes { 2958fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2959df8bae1dSRodney W. Grimes 2960df8bae1dSRodney W. Grimes while (cnt >= 0) { 2961df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2962df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2963df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2964df8bae1dSRodney W. Grimes 29658501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29667a3244ccSRobert Watson 2967df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2968df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2969df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2970df8bae1dSRodney W. Grimes m->m_len--; 297169a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 297269a34685SYoshinobu Inoue m->m_pkthdr.len--; 2973df8bae1dSRodney W. Grimes return; 2974df8bae1dSRodney W. Grimes } 2975df8bae1dSRodney W. Grimes cnt -= m->m_len; 2976df8bae1dSRodney W. Grimes m = m->m_next; 29774dfdffe9SAndre Oppermann if (m == NULL) 2978df8bae1dSRodney W. Grimes break; 2979df8bae1dSRodney W. Grimes } 2980df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2981df8bae1dSRodney W. Grimes } 2982df8bae1dSRodney W. Grimes 2983df8bae1dSRodney W. Grimes /* 2984df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2985df8bae1dSRodney W. Grimes * and update averages and current timeout. 2986df8bae1dSRodney W. Grimes */ 29870312fbe9SPoul-Henning Kamp static void 2988ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2989df8bae1dSRodney W. Grimes { 2990ad3f9ab3SAndre Oppermann int delta; 2991233e8c18SGarrett Wollman 29928501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 29932be3bf22SRobert Watson 299478b50714SRobert Watson TCPSTAT_INC(tcps_rttupdated); 2995233e8c18SGarrett Wollman tp->t_rttupdated++; 2996233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2997233e8c18SGarrett Wollman /* 2998233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2999233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 3000233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 3001233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 3002233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 3003233e8c18SGarrett Wollman */ 3004233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 3005233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 3006233e8c18SGarrett Wollman 3007233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 3008233e8c18SGarrett Wollman tp->t_srtt = 1; 3009233e8c18SGarrett Wollman 3010233e8c18SGarrett Wollman /* 3011233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 3012233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 3013233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 3014233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 3015233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 3016233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 3017233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 3018233e8c18SGarrett Wollman * rfc793's wired-in beta. 3019233e8c18SGarrett Wollman */ 3020233e8c18SGarrett Wollman if (delta < 0) 3021233e8c18SGarrett Wollman delta = -delta; 3022233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 3023233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 3024233e8c18SGarrett Wollman tp->t_rttvar = 1; 30251fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 30261fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3027233e8c18SGarrett Wollman } else { 3028233e8c18SGarrett Wollman /* 3029233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 3030233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 3031233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 3032233e8c18SGarrett Wollman */ 3033233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 3034233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 30351fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 3036233e8c18SGarrett Wollman } 30379b8b58e0SJonathan Lemon tp->t_rtttime = 0; 3038df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 3039df8bae1dSRodney W. Grimes 3040df8bae1dSRodney W. Grimes /* 3041df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 3042df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 3043df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 3044df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 3045df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 3046df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 3047df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 3048df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 3049df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 3050df8bae1dSRodney W. Grimes */ 3051233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 30529e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 3053df8bae1dSRodney W. Grimes 3054df8bae1dSRodney W. Grimes /* 3055df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 3056df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 3057df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 3058df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 3059df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 3060df8bae1dSRodney W. Grimes */ 3061df8bae1dSRodney W. Grimes tp->t_softerror = 0; 3062df8bae1dSRodney W. Grimes } 3063df8bae1dSRodney W. Grimes 3064df8bae1dSRodney W. Grimes /* 3065df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 3066df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 3067df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 3068df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 3069df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 3070df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 3071df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 3072df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 3073df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 3074df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 3075df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 3076df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 3077df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 3078a0292f23SGarrett Wollman * 3079a0292f23SGarrett Wollman * Also take into account the space needed for options that we 3080a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 3081a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 3082a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 3083a0292f23SGarrett Wollman * data in maxopd. 3084a0292f23SGarrett Wollman * 3085a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 3086a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 3087a0292f23SGarrett Wollman * MSS of our peer. 308897d8d152SAndre Oppermann * 308997d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 309097d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 3091df8bae1dSRodney W. Grimes */ 3092a0292f23SGarrett Wollman void 309391d6cfa6SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, 309491d6cfa6SBjoern A. Zeeb struct hc_metrics_lite *metricptr, int *mtuflags) 3095df8bae1dSRodney W. Grimes { 30963cee92e0SBjoern A. Zeeb int mss; 309797d8d152SAndre Oppermann u_long maxmtu; 3098c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 309997d8d152SAndre Oppermann struct hc_metrics_lite metrics; 3100a0292f23SGarrett Wollman int origoffer = offer; 3101fb59c426SYoshinobu Inoue #ifdef INET6 3102c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 3103c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 3104c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 3105c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 3106c068736aSJeffrey Hsu #else 3107c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 3108fb59c426SYoshinobu Inoue #endif 3109df8bae1dSRodney W. Grimes 31108501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31117a3244ccSRobert Watson 3112e8949f74SAndre Oppermann /* Initialize. */ 311397d8d152SAndre Oppermann #ifdef INET6 311497d8d152SAndre Oppermann if (isipv6) { 311591d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu6(&inp->inp_inc, mtuflags); 3116603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 311797d8d152SAndre Oppermann } else 311897d8d152SAndre Oppermann #endif 311997d8d152SAndre Oppermann { 312091d6cfa6SBjoern A. Zeeb maxmtu = tcp_maxmtu(&inp->inp_inc, mtuflags); 3121603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 3122df8bae1dSRodney W. Grimes } 3123df8bae1dSRodney W. Grimes 312497d8d152SAndre Oppermann /* 3125e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 312697d8d152SAndre Oppermann */ 31276f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 31286f01cac6SBjoern A. Zeeb /* 31298e5c87f4SBjoern A. Zeeb * In case we return early we need to initialize metrics 31306f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 31316f01cac6SBjoern A. Zeeb * if there was no cache hit. 31326f01cac6SBjoern A. Zeeb */ 31336f01cac6SBjoern A. Zeeb if (metricptr != NULL) 31346f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 313597d8d152SAndre Oppermann return; 31366f01cac6SBjoern A. Zeeb } 313797d8d152SAndre Oppermann 3138e8949f74SAndre Oppermann /* What have we got? */ 313997d8d152SAndre Oppermann switch (offer) { 314097d8d152SAndre Oppermann case 0: 314197d8d152SAndre Oppermann /* 314297d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 3143c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 3144c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 314597d8d152SAndre Oppermann */ 3146c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 314797d8d152SAndre Oppermann break; 314897d8d152SAndre Oppermann 314997d8d152SAndre Oppermann case -1: 3150a0292f23SGarrett Wollman /* 3151c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 3152a0292f23SGarrett Wollman */ 315397d8d152SAndre Oppermann /* FALLTHROUGH */ 315497d8d152SAndre Oppermann 315597d8d152SAndre Oppermann default: 3156a0292f23SGarrett Wollman /* 315753369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 315853369ac9SAndre Oppermann * to at least minmss. 315953369ac9SAndre Oppermann */ 3160603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 316197d8d152SAndre Oppermann } 3162a0292f23SGarrett Wollman 3163df8bae1dSRodney W. Grimes /* 3164e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 3165df8bae1dSRodney W. Grimes */ 316697d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 31673cee92e0SBjoern A. Zeeb if (metricptr != NULL) 31683cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 316997d8d152SAndre Oppermann 3170df8bae1dSRodney W. Grimes /* 3171e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 3172fb59c426SYoshinobu Inoue * else, use the link mtu. 3173df8bae1dSRodney W. Grimes */ 317497d8d152SAndre Oppermann if (metrics.rmx_mtu) 31752d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 3176c068736aSJeffrey Hsu else { 317731b3783cSHajimu UMEMOTO #ifdef INET6 3178fb59c426SYoshinobu Inoue if (isipv6) { 317997d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3180603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 318197d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 3182603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 3183b3399803SHajimu UMEMOTO } else 3184b3399803SHajimu UMEMOTO #endif 318597d8d152SAndre Oppermann { 318697d8d152SAndre Oppermann mss = maxmtu - min_protoh; 3187603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 318897d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 3189603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 3190a0292f23SGarrett Wollman } 31913cee92e0SBjoern A. Zeeb /* 31923cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 31933cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 31943cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 31953cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 31963cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 31973cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 31983cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 31993cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 32003cee92e0SBjoern A. Zeeb * this under the carpet. 32013cee92e0SBjoern A. Zeeb * 32023cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 32033cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 32043cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 32053cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 32063cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 32073cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 32083cee92e0SBjoern A. Zeeb */ 320997d8d152SAndre Oppermann } 3210a0292f23SGarrett Wollman mss = min(mss, offer); 321197d8d152SAndre Oppermann 3212a0292f23SGarrett Wollman /* 32133cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 32143cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 32153cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 32163cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 32173cee92e0SBjoern A. Zeeb */ 32183cee92e0SBjoern A. Zeeb mss = max(mss, 64); 32193cee92e0SBjoern A. Zeeb 32203cee92e0SBjoern A. Zeeb /* 3221a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 3222a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 3223a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 3224a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 3225a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 3226a0292f23SGarrett Wollman */ 3227a0292f23SGarrett Wollman tp->t_maxopd = mss; 3228a0292f23SGarrett Wollman 3229a0292f23SGarrett Wollman /* 3230e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 3231c94c54e4SAndre Oppermann * In this case we just guess. 3232a0292f23SGarrett Wollman */ 3233a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 3234a0292f23SGarrett Wollman (origoffer == -1 || 3235a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 3236a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 3237a0292f23SGarrett Wollman 3238df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 3239df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3240df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 3241df8bae1dSRodney W. Grimes #else 3242df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 3243df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 3244df8bae1dSRodney W. Grimes #endif 324597d8d152SAndre Oppermann tp->t_maxseg = mss; 32463cee92e0SBjoern A. Zeeb } 32473cee92e0SBjoern A. Zeeb 32483cee92e0SBjoern A. Zeeb void 32493cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 32503cee92e0SBjoern A. Zeeb { 32513cee92e0SBjoern A. Zeeb int rtt, mss; 32523cee92e0SBjoern A. Zeeb u_long bufsize; 32533cee92e0SBjoern A. Zeeb struct inpcb *inp; 32543cee92e0SBjoern A. Zeeb struct socket *so; 32553cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 325691d6cfa6SBjoern A. Zeeb int mtuflags = 0; 32573cee92e0SBjoern A. Zeeb #ifdef INET6 32583cee92e0SBjoern A. Zeeb int isipv6; 32593cee92e0SBjoern A. Zeeb #endif 32603cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 32613cee92e0SBjoern A. Zeeb 326291d6cfa6SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics, &mtuflags); 32633cee92e0SBjoern A. Zeeb 32643cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 32653cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 32663cee92e0SBjoern A. Zeeb #ifdef INET6 32673cee92e0SBjoern A. Zeeb isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 32683cee92e0SBjoern A. Zeeb #endif 326997d8d152SAndre Oppermann 3270df8bae1dSRodney W. Grimes /* 327197d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 327297d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 327397d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 327497d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 327597d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3276df8bae1dSRodney W. Grimes */ 3277c3b02504SBjoern A. Zeeb so = inp->inp_socket; 32783f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 327997d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 328097d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 328197d8d152SAndre Oppermann else 3282df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3283df8bae1dSRodney W. Grimes if (bufsize < mss) 3284df8bae1dSRodney W. Grimes mss = bufsize; 3285df8bae1dSRodney W. Grimes else { 3286df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3287df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3288df8bae1dSRodney W. Grimes bufsize = sb_max; 328988c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 32903f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3291df8bae1dSRodney W. Grimes } 32923f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3293df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3294df8bae1dSRodney W. Grimes 32953f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 329697d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 329797d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 329897d8d152SAndre Oppermann else 3299df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3300df8bae1dSRodney W. Grimes if (bufsize > mss) { 3301df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3302df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3303df8bae1dSRodney W. Grimes bufsize = sb_max; 330488c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 33053f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3306df8bae1dSRodney W. Grimes } 33073f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3308a0292f23SGarrett Wollman /* 3309e8949f74SAndre Oppermann * While we're here, check the others too. 3310a0292f23SGarrett Wollman */ 331197d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 331297d8d152SAndre Oppermann tp->t_srtt = rtt; 331397d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 331478b50714SRobert Watson TCPSTAT_INC(tcps_usedrtt); 331597d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 331697d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 331778b50714SRobert Watson TCPSTAT_INC(tcps_usedrttvar); 331897d8d152SAndre Oppermann } else { 331997d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 332097d8d152SAndre Oppermann tp->t_rttvar = 332197d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 332297d8d152SAndre Oppermann } 332397d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 332497d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 332597d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 332697d8d152SAndre Oppermann } 332797d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 3328df8bae1dSRodney W. Grimes /* 3329df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 3330df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 3331df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 3332df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 3333df8bae1dSRodney W. Grimes */ 333497d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 333578b50714SRobert Watson TCPSTAT_INC(tcps_usedssthresh); 3336df8bae1dSRodney W. Grimes } 333797d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 333897d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 333997d8d152SAndre Oppermann 334097d8d152SAndre Oppermann /* 334197d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 334297d8d152SAndre Oppermann * is a local network or not. 334397d8d152SAndre Oppermann * 334497d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 334597d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 334697d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 334797d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 334897d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 334997d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 335097d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 335197d8d152SAndre Oppermann * overloads the path again. 335297d8d152SAndre Oppermann * 335397d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 335497d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 335597d8d152SAndre Oppermann */ 335697d8d152SAndre Oppermann #define TCP_METRICS_CWND 335797d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 335897d8d152SAndre Oppermann if (metrics.rmx_cwnd) 335997d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 336097d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 336197d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 336297d8d152SAndre Oppermann else 336397d8d152SAndre Oppermann #endif 3364603724d3SBjoern A. Zeeb if (V_tcp_do_rfc3390) 336597d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 336697d8d152SAndre Oppermann #ifdef INET6 336797d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 336897d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3369943ae302SAndre Oppermann #else 3370943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 337197d8d152SAndre Oppermann #endif 3372603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz_local; 337397d8d152SAndre Oppermann else 3374603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz; 337591d6cfa6SBjoern A. Zeeb 337691d6cfa6SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 337791d6cfa6SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 337891d6cfa6SBjoern A. Zeeb tp->t_flags |= TF_TSO; 3379a0292f23SGarrett Wollman } 3380a0292f23SGarrett Wollman 3381a0292f23SGarrett Wollman /* 3382a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3383a0292f23SGarrett Wollman */ 3384a0292f23SGarrett Wollman int 3385ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3386a0292f23SGarrett Wollman { 338797d8d152SAndre Oppermann int mss = 0; 338897d8d152SAndre Oppermann u_long maxmtu = 0; 338997d8d152SAndre Oppermann u_long thcmtu = 0; 339097d8d152SAndre Oppermann size_t min_protoh; 3391a0292f23SGarrett Wollman 339297d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3393a0292f23SGarrett Wollman 339431b3783cSHajimu UMEMOTO #ifdef INET6 3395dcdb4371SBjoern A. Zeeb if (inc->inc_flags & INC_ISIPV6) { 3396603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3397233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 339897d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 339997d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 340097d8d152SAndre Oppermann } else 340131b3783cSHajimu UMEMOTO #endif 340297d8d152SAndre Oppermann { 3403603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3404233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 340597d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 340697d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 340797d8d152SAndre Oppermann } 340897d8d152SAndre Oppermann if (maxmtu && thcmtu) 340997d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 341097d8d152SAndre Oppermann else if (maxmtu || thcmtu) 341197d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 341297d8d152SAndre Oppermann 341397d8d152SAndre Oppermann return (mss); 3414df8bae1dSRodney W. Grimes } 341546f58482SJonathan Lemon 341646f58482SJonathan Lemon 341746f58482SJonathan Lemon /* 3418c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3419c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3420c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3421c068736aSJeffrey Hsu * be started again. 342246f58482SJonathan Lemon */ 3423c068736aSJeffrey Hsu static void 3424ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 342546f58482SJonathan Lemon { 342646f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 342746f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 342846f58482SJonathan Lemon 34298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 34307a3244ccSRobert Watson 3431b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 343246f58482SJonathan Lemon tp->t_rtttime = 0; 343346f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 34346b2a5f92SJayanth Vijayaraghavan /* 3435c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3436c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 34376b2a5f92SJayanth Vijayaraghavan */ 34386b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3439a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 34406b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 344146f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 344246f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 344346f58482SJonathan Lemon tp->snd_nxt = onxt; 344446f58482SJonathan Lemon /* 344546f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 344646f58482SJonathan Lemon * not updated yet. 344746f58482SJonathan Lemon */ 3448d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 3449d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 3450d7587117SPaul Saab else 3451d7587117SPaul Saab tp->snd_cwnd = 0; 3452d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 345346f58482SJonathan Lemon } 3454