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" 39c488362eSRobert Watson #include "opt_mac.h" 400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 410cc12cc5SJoerg Wunsch 42df8bae1dSRodney W. Grimes #include <sys/param.h> 4398163b98SPoul-Henning Kamp #include <sys/kernel.h> 44df8bae1dSRodney W. Grimes #include <sys/malloc.h> 45df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 46a29f300eSGarrett Wollman #include <sys/proc.h> /* for proc0 declaration */ 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48960ed29cSSeigo Tanimura #include <sys/signalvar.h> 49df8bae1dSRodney W. Grimes #include <sys/socket.h> 50df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 51960ed29cSSeigo Tanimura #include <sys/sysctl.h> 52816a3d83SPoul-Henning Kamp #include <sys/syslog.h> 53960ed29cSSeigo Tanimura #include <sys/systm.h> 54603724d3SBjoern A. Zeeb #include <sys/vimage.h> 55df8bae1dSRodney W. Grimes 56e79adb8eSGarrett Wollman #include <machine/cpu.h> /* before tcp_seq.h, for tcp_random18() */ 57e79adb8eSGarrett Wollman 5812e2e970SAndre Oppermann #include <vm/uma.h> 5912e2e970SAndre Oppermann 60df8bae1dSRodney W. Grimes #include <net/if.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62df8bae1dSRodney W. Grimes 63773673c1SAndre Oppermann #define TCPSTATES /* for logging */ 64773673c1SAndre Oppermann 65df8bae1dSRodney W. Grimes #include <netinet/in.h> 66960ed29cSSeigo Tanimura #include <netinet/in_pcb.h> 67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 68960ed29cSSeigo Tanimura #include <netinet/in_var.h> 69df8bae1dSRodney W. Grimes #include <netinet/ip.h> 708e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h> /* required for icmp_var.h */ 71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ 72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 73ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 74686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 75686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 76686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 77960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 78960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 79df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 83df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 84fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 86c325962bSMike Silbersack #include <netinet/tcp_syncache.h> 87610ee2f9SDavid Greenman #ifdef TCPDEBUG 88df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 89fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */ 90fb59c426SYoshinobu Inoue 91b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 92b9234fafSSam Leffler #include <netipsec/ipsec.h> 93b9234fafSSam Leffler #include <netipsec/ipsec6.h> 94b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 95b9234fafSSam Leffler 96db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 97db4f9cc7SJonathan Lemon 98aed55708SRobert Watson #include <security/mac/mac_framework.h> 99aed55708SRobert Watson 100c068736aSJeffrey Hsu static const int tcprexmtthresh = 3; 1010312fbe9SPoul-Henning Kamp 1022f96f1f4SGarrett Wollman struct tcpstat tcpstat; 1038b615593SMarko Zec SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_tcp, TCPCTL_STATS, stats, 1048b615593SMarko Zec CTLFLAG_RW, tcpstat , tcpstat, 1058b615593SMarko Zec "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); 1060312fbe9SPoul-Henning Kamp 107773673c1SAndre Oppermann int tcp_log_in_vain = 0; 108816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, 109574b6964SAndre Oppermann &tcp_log_in_vain, 0, "Log all incoming TCP segments to closed ports"); 110816a3d83SPoul-Henning Kamp 11116f7f31fSGeoff Rehmet static int blackhole = 0; 1128b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW, 1138b615593SMarko Zec blackhole, 0, "Do not send RST on segments to closed ports"); 11416f7f31fSGeoff Rehmet 115f498eeeeSDavid Greenman int tcp_delack_enabled = 1; 1168b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, delayed_ack, 1178b615593SMarko Zec CTLFLAG_RW, tcp_delack_enabled, 0, 1183d177f46SBill Fumerola "Delay ACK to try and piggyback it onto a data packet"); 119f498eeeeSDavid Greenman 120f8613305SDag-Erling Smørgrav static int drop_synfin = 0; 1218b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, drop_synfin, 1228b615593SMarko Zec CTLFLAG_RW, drop_synfin, 0, "Drop TCP packets with SYN+FIN set"); 123f8613305SDag-Erling Smørgrav 124dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1; 1258b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW, 1268b615593SMarko Zec tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)"); 127582a954bSJeffrey Hsu 128dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1; 1298b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW, 1308b615593SMarko Zec tcp_do_rfc3390, 0, 131da3a8a1aSJeffrey Hsu "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)"); 132da3a8a1aSJeffrey Hsu 133f2512ba1SRui Paulo int tcp_do_ecn = 0; 134f2512ba1SRui Paulo int tcp_ecn_maxretries = 1; 135f2512ba1SRui Paulo SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN"); 1368b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, enable, 1378b615593SMarko Zec CTLFLAG_RW, tcp_do_ecn, 0, "TCP ECN support"); 1388b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp_ecn, OID_AUTO, maxretries, 1398b615593SMarko Zec CTLFLAG_RW, tcp_ecn_maxretries, 0, "Max retries before giving up on ECN"); 140f2512ba1SRui Paulo 141a69968eeSMike Silbersack static int tcp_insecure_rst = 0; 1428b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, insecure_rst, 1438b615593SMarko Zec CTLFLAG_RW, tcp_insecure_rst, 0, 1446489fe65SAndre Oppermann "Follow the old (insecure) criteria for accepting RST packets"); 145a69968eeSMike Silbersack 1466741ecf5SAndre Oppermann int tcp_do_autorcvbuf = 1; 1478b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_auto, 1488b615593SMarko Zec CTLFLAG_RW, tcp_do_autorcvbuf, 0, 1498b615593SMarko Zec "Enable automatic receive buffer sizing"); 1506741ecf5SAndre Oppermann 1516741ecf5SAndre Oppermann int tcp_autorcvbuf_inc = 16*1024; 1528b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_inc, 1538b615593SMarko Zec CTLFLAG_RW, tcp_autorcvbuf_inc, 0, 1546489fe65SAndre Oppermann "Incrementor step size of automatic receive buffer"); 1556741ecf5SAndre Oppermann 1566741ecf5SAndre Oppermann int tcp_autorcvbuf_max = 256*1024; 1578b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_tcp, OID_AUTO, recvbuf_max, 1588b615593SMarko Zec CTLFLAG_RW, tcp_autorcvbuf_max, 0, 1598b615593SMarko Zec "Max size of automatic receive buffer"); 1606741ecf5SAndre Oppermann 16115bd2b43SDavid Greenman struct inpcbhead tcb; 162fb59c426SYoshinobu Inoue #define tcb6 tcb /* for KAME src sync over BSD*'s */ 16315bd2b43SDavid Greenman struct inpcbinfo tcbinfo; 164df8bae1dSRodney W. Grimes 1655a53ca16SPaul Saab static void tcp_dooptions(struct tcpopt *, u_char *, int, int); 166679d9708SAndre Oppermann static void tcp_do_segment(struct mbuf *, struct tcphdr *, 167f2512ba1SRui Paulo struct socket *, struct tcpcb *, int, int, uint8_t); 168302ce8d6SAndre Oppermann static void tcp_dropwithreset(struct mbuf *, struct tcphdr *, 169302ce8d6SAndre Oppermann struct tcpcb *, int, int); 1704d77a549SAlfred Perlstein static void tcp_pulloutofband(struct socket *, 1714d77a549SAlfred Perlstein struct tcphdr *, struct mbuf *, int); 1724d77a549SAlfred Perlstein static void tcp_xmit_timer(struct tcpcb *, int); 173c068736aSJeffrey Hsu static void tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *); 174f2512ba1SRui Paulo static void inline 175f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *); 176f2512ba1SRui Paulo 177f2512ba1SRui Paulo static void inline 178f2512ba1SRui Paulo tcp_congestion_exp(struct tcpcb *tp) 179f2512ba1SRui Paulo { 180f2512ba1SRui Paulo u_int win; 181f2512ba1SRui Paulo 182f2512ba1SRui Paulo win = min(tp->snd_wnd, tp->snd_cwnd) / 183f2512ba1SRui Paulo 2 / tp->t_maxseg; 184f2512ba1SRui Paulo if (win < 2) 185f2512ba1SRui Paulo win = 2; 186f2512ba1SRui Paulo tp->snd_ssthresh = win * tp->t_maxseg; 187f2512ba1SRui Paulo ENTER_FASTRECOVERY(tp); 188f2512ba1SRui Paulo tp->snd_recover = tp->snd_max; 189f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) 190f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_CWR; 191f2512ba1SRui Paulo } 1920312fbe9SPoul-Henning Kamp 193fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */ 194fb59c426SYoshinobu Inoue #ifdef INET6 195fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \ 196fb59c426SYoshinobu Inoue do { \ 197fb59c426SYoshinobu Inoue if ((tp) && (tp)->t_inpcb && \ 19897d8d152SAndre Oppermann ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \ 19997d8d152SAndre Oppermann nd6_nud_hint(NULL, NULL, 0); \ 200fb59c426SYoshinobu Inoue } while (0) 201fb59c426SYoshinobu Inoue #else 202fb59c426SYoshinobu Inoue #define ND6_HINT(tp) 203fb59c426SYoshinobu Inoue #endif 204df8bae1dSRodney W. Grimes 205df8bae1dSRodney W. Grimes /* 206262c1c1aSMatthew Dillon * Indicate whether this ack should be delayed. We can delay the ack if 207262c1c1aSMatthew Dillon * - there is no delayed ack timer in progress and 208262c1c1aSMatthew Dillon * - our last ack wasn't a 0-sized window. We never want to delay 2093bfd6421SJonathan Lemon * the ack that opens up a 0-sized window and 2103bfd6421SJonathan Lemon * - delayed acks are enabled or 2113bfd6421SJonathan Lemon * - this is a half-synchronized T/TCP connection. 212d8c85a26SJonathan Lemon */ 213d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \ 214b8152ba7SAndre Oppermann ((!tcp_timer_active(tp, TT_DELACK) && \ 215a14c749fSJonathan Lemon (tp->t_flags & TF_RXWIN0SENT) == 0) && \ 216603724d3SBjoern A. Zeeb (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN))) 217d8c85a26SJonathan Lemon 218df8bae1dSRodney W. Grimes /* 2198d573cc1SAndre Oppermann * TCP input handling is split into multiple parts: 2208d573cc1SAndre Oppermann * tcp6_input is a thin wrapper around tcp_input for the extended 2218d573cc1SAndre Oppermann * ip6_protox[] call format in ip6_input 2228d573cc1SAndre Oppermann * tcp_input handles primary segment validation, inpcb lookup and 2238d573cc1SAndre Oppermann * SYN processing on listen sockets 2248d573cc1SAndre Oppermann * tcp_do_segment processes the ACK and text of the segment for 2258d573cc1SAndre Oppermann * establishing, established and closing connections 226df8bae1dSRodney W. Grimes */ 227fb59c426SYoshinobu Inoue #ifdef INET6 228fb59c426SYoshinobu Inoue int 229ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto) 230fb59c426SYoshinobu Inoue { 2318b615593SMarko Zec INIT_VNET_INET6(curvnet); 232ad3f9ab3SAndre Oppermann struct mbuf *m = *mp; 23333841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 234fb59c426SYoshinobu Inoue 235fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE); 236fb59c426SYoshinobu Inoue 237fb59c426SYoshinobu Inoue /* 238fb59c426SYoshinobu Inoue * draft-itojun-ipv6-tcp-to-anycast 239fb59c426SYoshinobu Inoue * better place to put this in? 240fb59c426SYoshinobu Inoue */ 24133841545SHajimu UMEMOTO ia6 = ip6_getdstifaddr(m); 24233841545SHajimu UMEMOTO if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) { 243fb59c426SYoshinobu Inoue struct ip6_hdr *ip6; 244fb59c426SYoshinobu Inoue 245fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 246fb59c426SYoshinobu Inoue icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, 247fb59c426SYoshinobu Inoue (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); 248fb59c426SYoshinobu Inoue return IPPROTO_DONE; 249fb59c426SYoshinobu Inoue } 250fb59c426SYoshinobu Inoue 251f0ffb944SJulian Elischer tcp_input(m, *offp); 252fb59c426SYoshinobu Inoue return IPPROTO_DONE; 253fb59c426SYoshinobu Inoue } 254fb59c426SYoshinobu Inoue #endif 255fb59c426SYoshinobu Inoue 256df8bae1dSRodney W. Grimes void 257ad3f9ab3SAndre Oppermann tcp_input(struct mbuf *m, int off0) 258df8bae1dSRodney W. Grimes { 2598b615593SMarko Zec INIT_VNET_INET(curvnet); 2608b615593SMarko Zec #ifdef INET6 2618b615593SMarko Zec INIT_VNET_INET6(curvnet); 2628b615593SMarko Zec #endif 2638b615593SMarko Zec #ifdef IPSEC 2648b615593SMarko Zec INIT_VNET_IPSEC(curvnet); 2658b615593SMarko Zec #endif 266ad3f9ab3SAndre Oppermann struct tcphdr *th; 267ad3f9ab3SAndre Oppermann struct ip *ip = NULL; 268ad3f9ab3SAndre Oppermann struct ipovly *ipov; 269ad3f9ab3SAndre Oppermann struct inpcb *inp = NULL; 270302ce8d6SAndre Oppermann struct tcpcb *tp = NULL; 271302ce8d6SAndre Oppermann struct socket *so = NULL; 272e79adb8eSGarrett Wollman u_char *optp = NULL; 27326f9a767SRodney W. Grimes int optlen = 0; 274a0194ef1SBruce M Simpson int len, tlen, off; 275fb59c426SYoshinobu Inoue int drop_hdrlen; 276ad3f9ab3SAndre Oppermann int thflags; 277302ce8d6SAndre Oppermann int rstreason = 0; /* For badport_bandlim accounting purposes */ 278f2512ba1SRui Paulo uint8_t iptos; 2799b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 2809b932e9eSAndre Oppermann struct m_tag *fwd_tag; 2819b932e9eSAndre Oppermann #endif 282c068736aSJeffrey Hsu #ifdef INET6 283302ce8d6SAndre Oppermann struct ip6_hdr *ip6 = NULL; 284c068736aSJeffrey Hsu int isipv6; 285c068736aSJeffrey Hsu #else 286a160e630SAndre Oppermann const void *ip6 = NULL; 287c068736aSJeffrey Hsu const int isipv6 = 0; 288c068736aSJeffrey Hsu #endif 289302ce8d6SAndre Oppermann struct tcpopt to; /* options in this segment */ 290a160e630SAndre Oppermann char *s = NULL; /* address and port logging */ 291f76fcf6dSJeffrey Hsu 292610ee2f9SDavid Greenman #ifdef TCPDEBUG 293c068736aSJeffrey Hsu /* 294c068736aSJeffrey Hsu * The size of tcp_saveipgen must be the size of the max ip header, 295c068736aSJeffrey Hsu * now IPv6. 296c068736aSJeffrey Hsu */ 29714739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 298410bb1bfSLuigi Rizzo struct tcphdr tcp_savetcp; 299610ee2f9SDavid Greenman short ostate = 0; 300610ee2f9SDavid Greenman #endif 301c068736aSJeffrey Hsu 302fb59c426SYoshinobu Inoue #ifdef INET6 303fb59c426SYoshinobu Inoue isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 304fb59c426SYoshinobu Inoue #endif 305a0292f23SGarrett Wollman 306302ce8d6SAndre Oppermann to.to_flags = 0; 307603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvtotal++; 308fb59c426SYoshinobu Inoue 309fb59c426SYoshinobu Inoue if (isipv6) { 31004d3a452SHajimu UMEMOTO #ifdef INET6 3118d573cc1SAndre Oppermann /* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */ 312fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 313fb59c426SYoshinobu Inoue tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; 314fb59c426SYoshinobu Inoue if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) { 315603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadsum++; 316fb59c426SYoshinobu Inoue goto drop; 317fb59c426SYoshinobu Inoue } 318fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 31933841545SHajimu UMEMOTO 32033841545SHajimu UMEMOTO /* 32133841545SHajimu UMEMOTO * Be proactive about unspecified IPv6 address in source. 32233841545SHajimu UMEMOTO * As we use all-zero to indicate unbounded/unconnected pcb, 32333841545SHajimu UMEMOTO * unspecified IPv6 address can be used to confuse us. 32433841545SHajimu UMEMOTO * 32533841545SHajimu UMEMOTO * Note that packets with unspecified IPv6 destination is 32633841545SHajimu UMEMOTO * already dropped in ip6_input. 32733841545SHajimu UMEMOTO */ 32833841545SHajimu UMEMOTO if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { 32933841545SHajimu UMEMOTO /* XXX stat */ 33033841545SHajimu UMEMOTO goto drop; 33133841545SHajimu UMEMOTO } 33204d3a452SHajimu UMEMOTO #else 3338d573cc1SAndre Oppermann th = NULL; /* XXX: Avoid compiler warning. */ 33404d3a452SHajimu UMEMOTO #endif 335c068736aSJeffrey Hsu } else { 336df8bae1dSRodney W. Grimes /* 337df8bae1dSRodney W. Grimes * Get IP and TCP header together in first mbuf. 338df8bae1dSRodney W. Grimes * Note: IP leaves IP header in first mbuf. 339df8bae1dSRodney W. Grimes */ 340fb59c426SYoshinobu Inoue if (off0 > sizeof (struct ip)) { 341df8bae1dSRodney W. Grimes ip_stripoptions(m, (struct mbuf *)0); 342fb59c426SYoshinobu Inoue off0 = sizeof(struct ip); 343fb59c426SYoshinobu Inoue } 344df8bae1dSRodney W. Grimes if (m->m_len < sizeof (struct tcpiphdr)) { 3454dfdffe9SAndre Oppermann if ((m = m_pullup(m, sizeof (struct tcpiphdr))) 3464dfdffe9SAndre Oppermann == NULL) { 347603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvshort++; 348df8bae1dSRodney W. Grimes return; 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes } 351fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 352fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 353db4f9cc7SJonathan Lemon th = (struct tcphdr *)((caddr_t)ip + off0); 354db4f9cc7SJonathan Lemon tlen = ip->ip_len; 355df8bae1dSRodney W. Grimes 356db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 357db4f9cc7SJonathan Lemon if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 358db4f9cc7SJonathan Lemon th->th_sum = m->m_pkthdr.csum_data; 359db4f9cc7SJonathan Lemon else 360db4f9cc7SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, 361c068736aSJeffrey Hsu ip->ip_dst.s_addr, 362c068736aSJeffrey Hsu htonl(m->m_pkthdr.csum_data + 363c068736aSJeffrey Hsu ip->ip_len + 364c068736aSJeffrey Hsu IPPROTO_TCP)); 365db4f9cc7SJonathan Lemon th->th_sum ^= 0xffff; 3663c653157SHartmut Brandt #ifdef TCPDEBUG 3673c653157SHartmut Brandt ipov->ih_len = (u_short)tlen; 3683c653157SHartmut Brandt ipov->ih_len = htons(ipov->ih_len); 3693c653157SHartmut Brandt #endif 370db4f9cc7SJonathan Lemon } else { 371df8bae1dSRodney W. Grimes /* 372df8bae1dSRodney W. Grimes * Checksum extended TCP header and data. 373df8bae1dSRodney W. Grimes */ 374df8bae1dSRodney W. Grimes len = sizeof (struct ip) + tlen; 375fb59c426SYoshinobu Inoue bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); 376fb59c426SYoshinobu Inoue ipov->ih_len = (u_short)tlen; 377fd8e4ebcSMike Barcroft ipov->ih_len = htons(ipov->ih_len); 378fb59c426SYoshinobu Inoue th->th_sum = in_cksum(m, len); 379db4f9cc7SJonathan Lemon } 380fb59c426SYoshinobu Inoue if (th->th_sum) { 381603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadsum++; 382df8bae1dSRodney W. Grimes goto drop; 383df8bae1dSRodney W. Grimes } 384fb59c426SYoshinobu Inoue /* Re-initialization for later version check */ 385fb59c426SYoshinobu Inoue ip->ip_v = IPVERSION; 386fb59c426SYoshinobu Inoue } 387df8bae1dSRodney W. Grimes 388f2512ba1SRui Paulo #ifdef INET6 389f2512ba1SRui Paulo if (isipv6) 390f2512ba1SRui Paulo iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 391f2512ba1SRui Paulo else 392f2512ba1SRui Paulo #endif 393f2512ba1SRui Paulo iptos = ip->ip_tos; 394f2512ba1SRui Paulo 395df8bae1dSRodney W. Grimes /* 396df8bae1dSRodney W. Grimes * Check that TCP offset makes sense, 397df8bae1dSRodney W. Grimes * pull out TCP options and adjust length. XXX 398df8bae1dSRodney W. Grimes */ 399fb59c426SYoshinobu Inoue off = th->th_off << 2; 400df8bae1dSRodney W. Grimes if (off < sizeof (struct tcphdr) || off > tlen) { 401603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbadoff++; 402df8bae1dSRodney W. Grimes goto drop; 403df8bae1dSRodney W. Grimes } 404fb59c426SYoshinobu Inoue tlen -= off; /* tlen is used instead of ti->ti_len */ 405df8bae1dSRodney W. Grimes if (off > sizeof (struct tcphdr)) { 406fb59c426SYoshinobu Inoue if (isipv6) { 40704d3a452SHajimu UMEMOTO #ifdef INET6 408fb59c426SYoshinobu Inoue IP6_EXTHDR_CHECK(m, off0, off, ); 409fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 410fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip6 + off0); 41104d3a452SHajimu UMEMOTO #endif 412c068736aSJeffrey Hsu } else { 413df8bae1dSRodney W. Grimes if (m->m_len < sizeof(struct ip) + off) { 414c068736aSJeffrey Hsu if ((m = m_pullup(m, sizeof (struct ip) + off)) 4154dfdffe9SAndre Oppermann == NULL) { 416603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvshort++; 417df8bae1dSRodney W. Grimes return; 418df8bae1dSRodney W. Grimes } 419fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 420fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 421fb59c426SYoshinobu Inoue th = (struct tcphdr *)((caddr_t)ip + off0); 422fb59c426SYoshinobu Inoue } 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes optlen = off - sizeof (struct tcphdr); 425fb59c426SYoshinobu Inoue optp = (u_char *)(th + 1); 426df8bae1dSRodney W. Grimes } 427fb59c426SYoshinobu Inoue thflags = th->th_flags; 428df8bae1dSRodney W. Grimes 429e46cd3d4SDag-Erling Smørgrav /* 430df8bae1dSRodney W. Grimes * Convert TCP protocol specific fields to host format. 431df8bae1dSRodney W. Grimes */ 432fd8e4ebcSMike Barcroft th->th_seq = ntohl(th->th_seq); 433fd8e4ebcSMike Barcroft th->th_ack = ntohl(th->th_ack); 434fd8e4ebcSMike Barcroft th->th_win = ntohs(th->th_win); 435fd8e4ebcSMike Barcroft th->th_urp = ntohs(th->th_urp); 436df8bae1dSRodney W. Grimes 437df8bae1dSRodney W. Grimes /* 438794235b7SAndre Oppermann * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options. 439755c1f07SAndras Olah */ 440fb59c426SYoshinobu Inoue drop_hdrlen = off0 + off; 441755c1f07SAndras Olah 442755c1f07SAndras Olah /* 443df8bae1dSRodney W. Grimes * Locate pcb for segment. 444df8bae1dSRodney W. Grimes */ 445603724d3SBjoern A. Zeeb INP_INFO_WLOCK(&V_tcbinfo); 446df8bae1dSRodney W. Grimes findpcb: 447603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 4489b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD 4498d573cc1SAndre Oppermann /* 4508d573cc1SAndre Oppermann * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. 4518d573cc1SAndre Oppermann */ 4529b932e9eSAndre Oppermann fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 4539b932e9eSAndre Oppermann 4549b932e9eSAndre Oppermann if (fwd_tag != NULL && isipv6 == 0) { /* IPv6 support is not yet */ 4559b932e9eSAndre Oppermann struct sockaddr_in *next_hop; 4569b932e9eSAndre Oppermann 4579b932e9eSAndre Oppermann next_hop = (struct sockaddr_in *)(fwd_tag+1); 458f9e354dfSJulian Elischer /* 4592b25acc1SLuigi Rizzo * Transparently forwarded. Pretend to be the destination. 460f9e354dfSJulian Elischer * already got one like this? 461f9e354dfSJulian Elischer */ 462603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 4639b932e9eSAndre Oppermann ip->ip_src, th->th_sport, 464c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 465c068736aSJeffrey Hsu 0, m->m_pkthdr.rcvif); 466f9e354dfSJulian Elischer if (!inp) { 4679b932e9eSAndre Oppermann /* It's new. Try to find the ambushing socket. */ 468603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 469fb59c426SYoshinobu Inoue ip->ip_src, th->th_sport, 4702b25acc1SLuigi Rizzo next_hop->sin_addr, 471c068736aSJeffrey Hsu next_hop->sin_port ? 472c068736aSJeffrey Hsu ntohs(next_hop->sin_port) : 473c068736aSJeffrey Hsu th->th_dport, 474421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 475421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 476f9e354dfSJulian Elischer } 4779b932e9eSAndre Oppermann /* Remove the tag from the packet. We don't need it anymore. */ 4789b932e9eSAndre Oppermann m_tag_delete(m, fwd_tag); 479b10fbdeaSAndre Oppermann } else 4809b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */ 481b10fbdeaSAndre Oppermann { 48204d3a452SHajimu UMEMOTO if (isipv6) { 48304d3a452SHajimu UMEMOTO #ifdef INET6 484603724d3SBjoern A. Zeeb inp = in6_pcblookup_hash(&V_tcbinfo, 485c068736aSJeffrey Hsu &ip6->ip6_src, th->th_sport, 486c068736aSJeffrey Hsu &ip6->ip6_dst, th->th_dport, 487421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 488421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 48904d3a452SHajimu UMEMOTO #endif 49004d3a452SHajimu UMEMOTO } else 491603724d3SBjoern A. Zeeb inp = in_pcblookup_hash(&V_tcbinfo, 492c068736aSJeffrey Hsu ip->ip_src, th->th_sport, 493c068736aSJeffrey Hsu ip->ip_dst, th->th_dport, 494421d8aa6SBjoern A. Zeeb INPLOOKUP_WILDCARD, 495421d8aa6SBjoern A. Zeeb m->m_pkthdr.rcvif); 496fb59c426SYoshinobu Inoue } 497f9e354dfSJulian Elischer 498df8bae1dSRodney W. Grimes /* 499574b6964SAndre Oppermann * If the INPCB does not exist then all data in the incoming 500574b6964SAndre Oppermann * segment is discarded and an appropriate RST is sent back. 5018b07e49aSJulian Elischer * XXX MRT Send RST using which routing table? 502df8bae1dSRodney W. Grimes */ 503816a3d83SPoul-Henning Kamp if (inp == NULL) { 504574b6964SAndre Oppermann /* 505574b6964SAndre Oppermann * Log communication attempts to ports that are not 506574b6964SAndre Oppermann * in use. 507574b6964SAndre Oppermann */ 508574b6964SAndre Oppermann if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) || 509574b6964SAndre Oppermann tcp_log_in_vain == 2) { 5109fb5d4c0SPeter Wemm if ((s = tcp_log_addrs(NULL, th, (void *)ip, ip6))) 511df541e5fSAndre Oppermann log(LOG_INFO, "%s; %s: Connection attempt " 512df541e5fSAndre Oppermann "to closed port\n", s, __func__); 5132e4e1b4cSGeoff Rehmet } 514574b6964SAndre Oppermann /* 515574b6964SAndre Oppermann * When blackholing do not respond with a RST but 516574b6964SAndre Oppermann * completely ignore the segment and drop it. 517574b6964SAndre Oppermann */ 518603724d3SBjoern A. Zeeb if ((V_blackhole == 1 && (thflags & TH_SYN)) || 519603724d3SBjoern A. Zeeb V_blackhole == 2) 5201929eae1SAndre Oppermann goto dropunlock; 521574b6964SAndre Oppermann 522a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 523a57815efSBosko Milekic goto dropwithreset; 524816a3d83SPoul-Henning Kamp } 5258501a69cSRobert Watson INP_WLOCK(inp); 526936cd18dSAndre Oppermann 527a2589465SKen Smith #ifdef IPSEC 528a2589465SKen Smith #ifdef INET6 529a2589465SKen Smith if (isipv6 && ipsec6_in_reject(m, inp)) { 530603724d3SBjoern A. Zeeb V_ipsec6stat.in_polvio++; 531a2589465SKen Smith goto dropunlock; 532a2589465SKen Smith } else 533a2589465SKen Smith #endif /* INET6 */ 534a2589465SKen Smith if (ipsec4_in_reject(m, inp) != 0) { 535603724d3SBjoern A. Zeeb V_ipsec4stat.in_polvio++; 536a2589465SKen Smith goto dropunlock; 537a2589465SKen Smith } 538a2589465SKen Smith #endif /* IPSEC */ 539a2589465SKen Smith 5408d573cc1SAndre Oppermann /* 5418d573cc1SAndre Oppermann * Check the minimum TTL for socket. 5428d573cc1SAndre Oppermann */ 54334f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl != 0) { 54434f83c52SGeorge V. Neville-Neil #ifdef INET6 54534f83c52SGeorge V. Neville-Neil if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim) 546302ce8d6SAndre Oppermann goto dropunlock; 54702707462SAndre Oppermann else 54834f83c52SGeorge V. Neville-Neil #endif 54934f83c52SGeorge V. Neville-Neil if (inp->inp_ip_minttl > ip->ip_ttl) 550302ce8d6SAndre Oppermann goto dropunlock; 55134f83c52SGeorge V. Neville-Neil } 552936cd18dSAndre Oppermann 553340c35deSJonathan Lemon /* 554794235b7SAndre Oppermann * A previous connection in TIMEWAIT state is supposed to catch 555794235b7SAndre Oppermann * stray or duplicate segments arriving late. If this segment 556794235b7SAndre Oppermann * was a legitimate new connection attempt the old INPCB gets 557794235b7SAndre Oppermann * removed and we can try again to find a listening socket. 558340c35deSJonathan Lemon */ 559794235b7SAndre Oppermann if (inp->inp_vflag & INP_TIMEWAIT) { 560340c35deSJonathan Lemon if (thflags & TH_SYN) 561f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 5628d573cc1SAndre Oppermann /* 5638d573cc1SAndre Oppermann * NB: tcp_twcheck unlocks the INP and frees the mbuf. 5648d573cc1SAndre Oppermann */ 5652104448fSAndre Oppermann if (tcp_twcheck(inp, &to, th, m, tlen)) 566340c35deSJonathan Lemon goto findpcb; 567603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 568340c35deSJonathan Lemon return; 569340c35deSJonathan Lemon } 570794235b7SAndre Oppermann /* 571794235b7SAndre Oppermann * The TCPCB may no longer exist if the connection is winding 572794235b7SAndre Oppermann * down or it is in the CLOSED state. Either way we drop the 573794235b7SAndre Oppermann * segment and send an appropriate response. 574794235b7SAndre Oppermann */ 575df8bae1dSRodney W. Grimes tp = intotcpcb(inp); 576a160e630SAndre Oppermann if (tp == NULL || tp->t_state == TCPS_CLOSED) { 577a57815efSBosko Milekic rstreason = BANDLIM_RST_CLOSEDPORT; 578a57815efSBosko Milekic goto dropwithreset; 57909f81a46SBosko Milekic } 580df8bae1dSRodney W. Grimes 581c488362eSRobert Watson #ifdef MAC 5828501a69cSRobert Watson INP_WLOCK_ASSERT(inp); 58330d239bcSRobert Watson if (mac_inpcb_check_deliver(inp, m)) 584302ce8d6SAndre Oppermann goto dropunlock; 585c488362eSRobert Watson #endif 586a557af22SRobert Watson so = inp->inp_socket; 587302ce8d6SAndre Oppermann KASSERT(so != NULL, ("%s: so == NULL", __func__)); 588610ee2f9SDavid Greenman #ifdef TCPDEBUG 589df8bae1dSRodney W. Grimes if (so->so_options & SO_DEBUG) { 590df8bae1dSRodney W. Grimes ostate = tp->t_state; 591d30d90dcSMaxim Konovalov if (isipv6) { 59210fe523eSMaxim Konovalov #ifdef INET6 593540e8b7eSJeffrey Hsu bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6)); 594d30d90dcSMaxim Konovalov #endif 595d30d90dcSMaxim Konovalov } else 596540e8b7eSJeffrey Hsu bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); 597fb59c426SYoshinobu Inoue tcp_savetcp = *th; 598df8bae1dSRodney W. Grimes } 599610ee2f9SDavid Greenman #endif 600db33b3e6SAndre Oppermann /* 601db33b3e6SAndre Oppermann * When the socket is accepting connections (the INPCB is in LISTEN 602db33b3e6SAndre Oppermann * state) we look into the SYN cache if this is a new connection 603db33b3e6SAndre Oppermann * attempt or the completion of a previous one. 604db33b3e6SAndre Oppermann */ 605540e8b7eSJeffrey Hsu if (so->so_options & SO_ACCEPTCONN) { 606540e8b7eSJeffrey Hsu struct in_conninfo inc; 607540e8b7eSJeffrey Hsu 6087824d002SAndre Oppermann KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but " 6097824d002SAndre Oppermann "tp not listening", __func__)); 6107824d002SAndre Oppermann 6118bfb1918SAndre Oppermann bzero(&inc, sizeof(inc)); 612be2ac88cSJonathan Lemon inc.inc_isipv6 = isipv6; 613302ce8d6SAndre Oppermann #ifdef INET6 614be2ac88cSJonathan Lemon if (isipv6) { 615be2ac88cSJonathan Lemon inc.inc6_faddr = ip6->ip6_src; 616be2ac88cSJonathan Lemon inc.inc6_laddr = ip6->ip6_dst; 617302ce8d6SAndre Oppermann } else 618302ce8d6SAndre Oppermann #endif 619302ce8d6SAndre Oppermann { 620be2ac88cSJonathan Lemon inc.inc_faddr = ip->ip_src; 621be2ac88cSJonathan Lemon inc.inc_laddr = ip->ip_dst; 622be2ac88cSJonathan Lemon } 623be2ac88cSJonathan Lemon inc.inc_fport = th->th_sport; 624be2ac88cSJonathan Lemon inc.inc_lport = th->th_dport; 625be2ac88cSJonathan Lemon 626fb59c426SYoshinobu Inoue /* 6278d573cc1SAndre Oppermann * Check for an existing connection attempt in syncache if 6288d573cc1SAndre Oppermann * the flag is only ACK. A successful lookup creates a new 6298d573cc1SAndre Oppermann * socket appended to the listen queue in SYN_RECEIVED state. 630fb59c426SYoshinobu Inoue */ 631be2ac88cSJonathan Lemon if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) { 632bf6d304aSAndre Oppermann /* 633bf6d304aSAndre Oppermann * Parse the TCP options here because 634bf6d304aSAndre Oppermann * syncookies need access to the reflected 635bf6d304aSAndre Oppermann * timestamp. 636bf6d304aSAndre Oppermann */ 637bf6d304aSAndre Oppermann tcp_dooptions(&to, optp, optlen, 0); 6389fa198beSAndre Oppermann /* 6399fa198beSAndre Oppermann * NB: syncache_expand() doesn't unlock 6409fa198beSAndre Oppermann * inp and tcpinfo locks. 6419fa198beSAndre Oppermann */ 642bf6d304aSAndre Oppermann if (!syncache_expand(&inc, &to, th, &so, m)) { 6434195b4afSPaul Traina /* 644e207f800SAndre Oppermann * No syncache entry or ACK was not 645be2ac88cSJonathan Lemon * for our SYN/ACK. Send a RST. 6468d573cc1SAndre Oppermann * NB: syncache did its own logging 6478d573cc1SAndre Oppermann * of the failure cause. 6484195b4afSPaul Traina */ 649be2ac88cSJonathan Lemon rstreason = BANDLIM_RST_OPENPORT; 650be2ac88cSJonathan Lemon goto dropwithreset; 651be2ac88cSJonathan Lemon } 652f76fcf6dSJeffrey Hsu if (so == NULL) { 653be2ac88cSJonathan Lemon /* 654e207f800SAndre Oppermann * We completed the 3-way handshake 655e207f800SAndre Oppermann * but could not allocate a socket 656e207f800SAndre Oppermann * either due to memory shortage, 657e207f800SAndre Oppermann * listen queue length limits or 658a160e630SAndre Oppermann * global socket limits. Send RST 659a160e630SAndre Oppermann * or wait and have the remote end 660a160e630SAndre Oppermann * retransmit the ACK for another 661a160e630SAndre Oppermann * try. 662be2ac88cSJonathan Lemon */ 663a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 664a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 6658d573cc1SAndre Oppermann "Socket allocation failed due to " 6668d573cc1SAndre Oppermann "limits or memory shortage, %s\n", 667ac957cd2SJulian Elischer s, __func__, 668ac957cd2SJulian Elischer V_tcp_sc_rst_sock_fail ? 669ac957cd2SJulian Elischer "sending RST" : "try again"); 670603724d3SBjoern A. Zeeb if (V_tcp_sc_rst_sock_fail) { 671e207f800SAndre Oppermann rstreason = BANDLIM_UNLIMITED; 672e207f800SAndre Oppermann goto dropwithreset; 673a160e630SAndre Oppermann } else 674a160e630SAndre Oppermann goto dropunlock; 675f76fcf6dSJeffrey Hsu } 676be2ac88cSJonathan Lemon /* 677be2ac88cSJonathan Lemon * Socket is created in state SYN_RECEIVED. 6788d573cc1SAndre Oppermann * Unlock the listen socket, lock the newly 6798d573cc1SAndre Oppermann * created socket and update the tp variable. 680be2ac88cSJonathan Lemon */ 6818501a69cSRobert Watson INP_WUNLOCK(inp); /* listen socket */ 682be2ac88cSJonathan Lemon inp = sotoinpcb(so); 6838501a69cSRobert Watson INP_WLOCK(inp); /* new connection */ 684be2ac88cSJonathan Lemon tp = intotcpcb(inp); 6858d573cc1SAndre Oppermann KASSERT(tp->t_state == TCPS_SYN_RECEIVED, 6868d573cc1SAndre Oppermann ("%s: ", __func__)); 687be2ac88cSJonathan Lemon /* 688302ce8d6SAndre Oppermann * Process the segment and the data it 689302ce8d6SAndre Oppermann * contains. tcp_do_segment() consumes 690302ce8d6SAndre Oppermann * the mbuf chain and unlocks the inpcb. 691302ce8d6SAndre Oppermann */ 692f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, 693f2512ba1SRui Paulo iptos); 694603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 695302ce8d6SAndre Oppermann return; 696be2ac88cSJonathan Lemon } 697a160e630SAndre Oppermann /* 6988d573cc1SAndre Oppermann * Segment flag validation for new connection attempts: 6998d573cc1SAndre Oppermann * 700a160e630SAndre Oppermann * Our (SYN|ACK) response was rejected. 701a160e630SAndre Oppermann * Check with syncache and remove entry to prevent 702a160e630SAndre Oppermann * retransmits. 70319bc77c5SAndre Oppermann * 70419bc77c5SAndre Oppermann * NB: syncache_chkrst does its own logging of failure 70519bc77c5SAndre Oppermann * causes. 706a160e630SAndre Oppermann */ 707a160e630SAndre Oppermann if (thflags & TH_RST) { 70819bc77c5SAndre Oppermann syncache_chkrst(&inc, th); 709a160e630SAndre Oppermann goto dropunlock; 710a160e630SAndre Oppermann } 711a160e630SAndre Oppermann /* 712a160e630SAndre Oppermann * We can't do anything without SYN. 713a160e630SAndre Oppermann */ 714a160e630SAndre Oppermann if ((thflags & TH_SYN) == 0) { 715a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 716a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 717773673c1SAndre Oppermann "SYN is missing, segment ignored\n", 7188d573cc1SAndre Oppermann s, __func__); 719603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 720a160e630SAndre Oppermann goto dropunlock; 721a160e630SAndre Oppermann } 722a160e630SAndre Oppermann /* 723a160e630SAndre Oppermann * (SYN|ACK) is bogus on a listen socket. 724a160e630SAndre Oppermann */ 725fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 726a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 727a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 7288d573cc1SAndre Oppermann "SYN|ACK invalid, segment rejected\n", 7298d573cc1SAndre Oppermann s, __func__); 730a160e630SAndre Oppermann syncache_badack(&inc); /* XXX: Not needed! */ 731603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 732a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 733a57815efSBosko Milekic goto dropwithreset; 7344195b4afSPaul Traina } 735a160e630SAndre Oppermann /* 736a160e630SAndre Oppermann * If the drop_synfin option is enabled, drop all 737a160e630SAndre Oppermann * segments with both the SYN and FIN bits set. 738a160e630SAndre Oppermann * This prevents e.g. nmap from identifying the 739a160e630SAndre Oppermann * TCP/IP stack. 740a160e630SAndre Oppermann * XXX: Poor reasoning. nmap has other methods 741a160e630SAndre Oppermann * and is constantly refining its stack detection 742a160e630SAndre Oppermann * strategies. 7438d573cc1SAndre Oppermann * XXX: This is a violation of the TCP specification 744a160e630SAndre Oppermann * and was used by RFC1644. 745a160e630SAndre Oppermann */ 746603724d3SBjoern A. Zeeb if ((thflags & TH_FIN) && V_drop_synfin) { 747a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 748a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 749773673c1SAndre Oppermann "SYN|FIN segment ignored (based on " 7508d573cc1SAndre Oppermann "sysctl setting)\n", s, __func__); 751603724d3SBjoern A. Zeeb V_tcpstat.tcps_badsyn++; 752302ce8d6SAndre Oppermann goto dropunlock; 753ebb0cbeaSPaul Traina } 754be2ac88cSJonathan Lemon /* 755be2ac88cSJonathan Lemon * Segment's flags are (SYN) or (SYN|FIN). 756a160e630SAndre Oppermann * 757a160e630SAndre Oppermann * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored 758a160e630SAndre Oppermann * as they do not affect the state of the TCP FSM. 759a160e630SAndre Oppermann * The data pointed to by TH_URG and th_urp is ignored. 760be2ac88cSJonathan Lemon */ 761a160e630SAndre Oppermann KASSERT((thflags & (TH_RST|TH_ACK)) == 0, 762a160e630SAndre Oppermann ("%s: Listen socket: TH_RST or TH_ACK set", __func__)); 763a160e630SAndre Oppermann KASSERT(thflags & (TH_SYN), 764a160e630SAndre Oppermann ("%s: Listen socket: TH_SYN not set", __func__)); 76533841545SHajimu UMEMOTO #ifdef INET6 76633841545SHajimu UMEMOTO /* 76733841545SHajimu UMEMOTO * If deprecated address is forbidden, 76833841545SHajimu UMEMOTO * we do not accept SYN to deprecated interface 76933841545SHajimu UMEMOTO * address to prevent any new inbound connection from 77033841545SHajimu UMEMOTO * getting established. 77133841545SHajimu UMEMOTO * When we do not accept SYN, we send a TCP RST, 77233841545SHajimu UMEMOTO * with deprecated source address (instead of dropping 77333841545SHajimu UMEMOTO * it). We compromise it as it is much better for peer 77433841545SHajimu UMEMOTO * to send a RST, and RST will be the final packet 77533841545SHajimu UMEMOTO * for the exchange. 77633841545SHajimu UMEMOTO * 77733841545SHajimu UMEMOTO * If we do not forbid deprecated addresses, we accept 77833841545SHajimu UMEMOTO * the SYN packet. RFC2462 does not suggest dropping 77933841545SHajimu UMEMOTO * SYN in this case. 78033841545SHajimu UMEMOTO * If we decipher RFC2462 5.5.4, it says like this: 78133841545SHajimu UMEMOTO * 1. use of deprecated addr with existing 78233841545SHajimu UMEMOTO * communication is okay - "SHOULD continue to be 78333841545SHajimu UMEMOTO * used" 78433841545SHajimu UMEMOTO * 2. use of it with new communication: 78533841545SHajimu UMEMOTO * (2a) "SHOULD NOT be used if alternate address 78633841545SHajimu UMEMOTO * with sufficient scope is available" 78733841545SHajimu UMEMOTO * (2b) nothing mentioned otherwise. 78833841545SHajimu UMEMOTO * Here we fall into (2b) case as we have no choice in 78933841545SHajimu UMEMOTO * our source address selection - we must obey the peer. 79033841545SHajimu UMEMOTO * 79133841545SHajimu UMEMOTO * The wording in RFC2462 is confusing, and there are 79233841545SHajimu UMEMOTO * multiple description text for deprecated address 79333841545SHajimu UMEMOTO * handling - worse, they are not exactly the same. 79433841545SHajimu UMEMOTO * I believe 5.5.4 is the best one, so we follow 5.5.4. 79533841545SHajimu UMEMOTO */ 796603724d3SBjoern A. Zeeb if (isipv6 && !V_ip6_use_deprecated) { 79733841545SHajimu UMEMOTO struct in6_ifaddr *ia6; 79833841545SHajimu UMEMOTO 79933841545SHajimu UMEMOTO if ((ia6 = ip6_getdstifaddr(m)) && 80033841545SHajimu UMEMOTO (ia6->ia6_flags & IN6_IFF_DEPRECATED)) { 801a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 802a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8038d573cc1SAndre Oppermann "Connection attempt to deprecated " 8048d573cc1SAndre Oppermann "IPv6 address rejected\n", 805a160e630SAndre Oppermann s, __func__); 80633841545SHajimu UMEMOTO rstreason = BANDLIM_RST_OPENPORT; 80733841545SHajimu UMEMOTO goto dropwithreset; 80833841545SHajimu UMEMOTO } 80933841545SHajimu UMEMOTO } 81033841545SHajimu UMEMOTO #endif 81165f28919SJesper Skriver /* 812db33b3e6SAndre Oppermann * Basic sanity checks on incoming SYN requests: 8138d573cc1SAndre Oppermann * Don't respond if the destination is a link layer 814db33b3e6SAndre Oppermann * broadcast according to RFC1122 4.2.3.10, p. 104. 8158d573cc1SAndre Oppermann * If it is from this socket it must be forged. 8168d573cc1SAndre Oppermann * Don't respond if the source or destination is a 8178d573cc1SAndre Oppermann * global or subnet broad- or multicast address. 81893ec91baSCrist J. Clark * Note that it is quite possible to receive unicast 81993ec91baSCrist J. Clark * link-layer packets with a broadcast IP address. Use 82093ec91baSCrist J. Clark * in_broadcast() to find them. 821be2ac88cSJonathan Lemon */ 8228d573cc1SAndre Oppermann if (m->m_flags & (M_BCAST|M_MCAST)) { 8238d573cc1SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 8248d573cc1SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8258d573cc1SAndre Oppermann "Connection attempt from broad- or multicast " 826773673c1SAndre Oppermann "link layer address ignored\n", s, __func__); 827302ce8d6SAndre Oppermann goto dropunlock; 8288d573cc1SAndre Oppermann } 829be2ac88cSJonathan Lemon if (isipv6) { 830db33b3e6SAndre Oppermann #ifdef INET6 831db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 832a160e630SAndre Oppermann IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { 833a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 834a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8358d573cc1SAndre Oppermann "Connection attempt to/from self " 836773673c1SAndre Oppermann "ignored\n", s, __func__); 837302ce8d6SAndre Oppermann goto dropunlock; 838a160e630SAndre Oppermann } 839be2ac88cSJonathan Lemon if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 840a160e630SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) { 841a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 842a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8438d573cc1SAndre Oppermann "Connection attempt from/to multicast " 844773673c1SAndre Oppermann "address ignored\n", s, __func__); 845302ce8d6SAndre Oppermann goto dropunlock; 846a160e630SAndre Oppermann } 847db33b3e6SAndre Oppermann #endif 848c068736aSJeffrey Hsu } else { 849db33b3e6SAndre Oppermann if (th->th_dport == th->th_sport && 850a160e630SAndre Oppermann ip->ip_dst.s_addr == ip->ip_src.s_addr) { 851a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 852a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8538d573cc1SAndre Oppermann "Connection attempt from/to self " 854773673c1SAndre Oppermann "ignored\n", s, __func__); 855302ce8d6SAndre Oppermann goto dropunlock; 856a160e630SAndre Oppermann } 857be2ac88cSJonathan Lemon if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 858be2ac88cSJonathan Lemon IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 8592ca2159fSCrist J. Clark ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 860a160e630SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 861a160e630SAndre Oppermann if ((s = tcp_log_addrs(&inc, th, NULL, NULL))) 862a160e630SAndre Oppermann log(LOG_DEBUG, "%s; %s: Listen socket: " 8638d573cc1SAndre Oppermann "Connection attempt from/to broad- " 864773673c1SAndre Oppermann "or multicast address ignored\n", 865a160e630SAndre Oppermann s, __func__); 866302ce8d6SAndre Oppermann goto dropunlock; 867c068736aSJeffrey Hsu } 868a160e630SAndre Oppermann } 869be2ac88cSJonathan Lemon /* 870db33b3e6SAndre Oppermann * SYN appears to be valid. Create compressed TCP state 871db33b3e6SAndre Oppermann * for syncache. 872be2ac88cSJonathan Lemon */ 8733c653157SHartmut Brandt #ifdef TCPDEBUG 8743c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 8753c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 8763c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 8773c653157SHartmut Brandt #endif 878f72167f4SAndre Oppermann tcp_dooptions(&to, optp, optlen, TO_SYN); 8794d6e7130SAndre Oppermann syncache_add(&inc, &to, th, inp, &so, m); 880be2ac88cSJonathan Lemon /* 8814d6e7130SAndre Oppermann * Entry added to syncache and mbuf consumed. 882a160e630SAndre Oppermann * Everything already unlocked by syncache_add(). 883be2ac88cSJonathan Lemon */ 884603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 885be2ac88cSJonathan Lemon return; 886f76fcf6dSJeffrey Hsu } 887db33b3e6SAndre Oppermann 888302ce8d6SAndre Oppermann /* 8898d573cc1SAndre Oppermann * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later 8908d573cc1SAndre Oppermann * state. tcp_do_segment() always consumes the mbuf chain, unlocks 8918d573cc1SAndre Oppermann * the inpcb, and unlocks pcbinfo. 892302ce8d6SAndre Oppermann */ 893f2512ba1SRui Paulo tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos); 894603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 895302ce8d6SAndre Oppermann return; 896be2ac88cSJonathan Lemon 897302ce8d6SAndre Oppermann dropwithreset: 898603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 899dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 900014ea782SRobert Watson 901014ea782SRobert Watson if (inp != NULL) { 902302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 903014ea782SRobert Watson INP_WUNLOCK(inp); 904dd8ac7f9SRobert Watson } else 905014ea782SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 906302ce8d6SAndre Oppermann m = NULL; /* mbuf chain got consumed. */ 907014ea782SRobert Watson goto drop; 908014ea782SRobert Watson 909302ce8d6SAndre Oppermann dropunlock: 910603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 9119fa198beSAndre Oppermann if (inp != NULL) 9128501a69cSRobert Watson INP_WUNLOCK(inp); 913603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 914014ea782SRobert Watson 915302ce8d6SAndre Oppermann drop: 916603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 917a160e630SAndre Oppermann if (s != NULL) 918a160e630SAndre Oppermann free(s, M_TCPLOG); 919302ce8d6SAndre Oppermann if (m != NULL) 920302ce8d6SAndre Oppermann m_freem(m); 921302ce8d6SAndre Oppermann } 922302ce8d6SAndre Oppermann 923679d9708SAndre Oppermann static void 924302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so, 925f2512ba1SRui Paulo struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos) 926302ce8d6SAndre Oppermann { 9278b615593SMarko Zec INIT_VNET_INET(tp->t_vnet); 928302ce8d6SAndre Oppermann int thflags, acked, ourfinisacked, needoutput = 0; 929302ce8d6SAndre Oppermann int headlocked = 1; 930302ce8d6SAndre Oppermann int rstreason, todrop, win; 931302ce8d6SAndre Oppermann u_long tiwin; 932302ce8d6SAndre Oppermann struct tcpopt to; 933302ce8d6SAndre Oppermann 93414739780SMaxim Konovalov #ifdef TCPDEBUG 93514739780SMaxim Konovalov /* 93614739780SMaxim Konovalov * The size of tcp_saveipgen must be the size of the max ip header, 93714739780SMaxim Konovalov * now IPv6. 93814739780SMaxim Konovalov */ 93914739780SMaxim Konovalov u_char tcp_saveipgen[IP6_HDR_LEN]; 94014739780SMaxim Konovalov struct tcphdr tcp_savetcp; 94114739780SMaxim Konovalov short ostate = 0; 94214739780SMaxim Konovalov #endif 943302ce8d6SAndre Oppermann thflags = th->th_flags; 944302ce8d6SAndre Oppermann 945603724d3SBjoern A. Zeeb INP_INFO_WLOCK_ASSERT(&V_tcbinfo); 9468501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 947679d9708SAndre Oppermann KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN", 948679d9708SAndre Oppermann __func__)); 949679d9708SAndre Oppermann KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT", 950679d9708SAndre Oppermann __func__)); 951df8bae1dSRodney W. Grimes 952df8bae1dSRodney W. Grimes /* 953df8bae1dSRodney W. Grimes * Segment received on connection. 954df8bae1dSRodney W. Grimes * Reset idle time and keep-alive timer. 955e8949f74SAndre Oppermann * XXX: This should be done after segment 956e8949f74SAndre Oppermann * validation to ignore broken/spoofed segs. 957df8bae1dSRodney W. Grimes */ 9589b8b58e0SJonathan Lemon tp->t_rcvtime = ticks; 9597ff19458SPaul Traina if (TCPS_HAVEESTABLISHED(tp->t_state)) 960b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 961df8bae1dSRodney W. Grimes 962df8bae1dSRodney W. Grimes /* 963464fcfbcSAndre Oppermann * Unscale the window into a 32-bit value. 964e8949f74SAndre Oppermann * For the SYN_SENT state the scale is zero. 965464fcfbcSAndre Oppermann */ 966464fcfbcSAndre Oppermann tiwin = th->th_win << tp->snd_scale; 967464fcfbcSAndre Oppermann 968464fcfbcSAndre Oppermann /* 969f2512ba1SRui Paulo * TCP ECN processing. 970f2512ba1SRui Paulo */ 971f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_PERMIT) { 972f2512ba1SRui Paulo switch (iptos & IPTOS_ECN_MASK) { 973f2512ba1SRui Paulo case IPTOS_ECN_CE: 974f2512ba1SRui Paulo tp->t_flags |= TF_ECN_SND_ECE; 975603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ce++; 976f2512ba1SRui Paulo break; 977f2512ba1SRui Paulo case IPTOS_ECN_ECT0: 978603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ect0++; 979f2512ba1SRui Paulo break; 980f2512ba1SRui Paulo case IPTOS_ECN_ECT1: 981603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_ect1++; 982f2512ba1SRui Paulo break; 983f2512ba1SRui Paulo } 984f2512ba1SRui Paulo 985f2512ba1SRui Paulo if (thflags & TH_CWR) 986f2512ba1SRui Paulo tp->t_flags &= ~TF_ECN_SND_ECE; 987f2512ba1SRui Paulo 988f2512ba1SRui Paulo /* 989f2512ba1SRui Paulo * Congestion experienced. 990f2512ba1SRui Paulo * Ignore if we are already trying to recover. 991f2512ba1SRui Paulo */ 992f2512ba1SRui Paulo if ((thflags & TH_ECE) && 993f2512ba1SRui Paulo SEQ_LEQ(th->th_ack, tp->snd_recover)) { 994603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_rcwnd++; 995f2512ba1SRui Paulo tcp_congestion_exp(tp); 996f2512ba1SRui Paulo } 997f2512ba1SRui Paulo } 998f2512ba1SRui Paulo 999f2512ba1SRui Paulo /* 1000f72167f4SAndre Oppermann * Parse options on any incoming segment. 1001f72167f4SAndre Oppermann */ 1002302ce8d6SAndre Oppermann tcp_dooptions(&to, (u_char *)(th + 1), 1003302ce8d6SAndre Oppermann (th->th_off << 2) - sizeof(struct tcphdr), 1004302ce8d6SAndre Oppermann (thflags & TH_SYN) ? TO_SYN : 0); 1005f72167f4SAndre Oppermann 1006f72167f4SAndre Oppermann /* 1007f72167f4SAndre Oppermann * If echoed timestamp is later than the current time, 1008bf6d304aSAndre Oppermann * fall back to non RFC1323 RTT calculation. Normalize 1009bf6d304aSAndre Oppermann * timestamp if syncookies were used when this connection 1010bf6d304aSAndre Oppermann * was established. 1011f72167f4SAndre Oppermann */ 1012bf6d304aSAndre Oppermann if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) { 1013e16fa5caSJohn-Mark Gurney to.to_tsecr -= tp->ts_offset; 1014bf6d304aSAndre Oppermann if (TSTMP_GT(to.to_tsecr, ticks)) 1015f72167f4SAndre Oppermann to.to_tsecr = 0; 1016bf6d304aSAndre Oppermann } 1017f72167f4SAndre Oppermann 1018f72167f4SAndre Oppermann /* 101997d8d152SAndre Oppermann * Process options only when we get SYN/ACK back. The SYN case 102097d8d152SAndre Oppermann * for incoming connections is handled in tcp_syncache. 10215396d0f8SAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 10225396d0f8SAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. 102397d8d152SAndre Oppermann * XXX this is traditional behavior, may need to be cleaned up. 1024df8bae1dSRodney W. Grimes */ 10250a389eabSSimon L. B. Nielsen if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { 1026464fcfbcSAndre Oppermann if ((to.to_flags & TOF_SCALE) && 1027464fcfbcSAndre Oppermann (tp->t_flags & TF_REQ_SCALE)) { 1028be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_SCALE; 102902a1a643SAndre Oppermann tp->snd_scale = to.to_wscale; 1030be2ac88cSJonathan Lemon } 10315396d0f8SAndre Oppermann /* 10325396d0f8SAndre Oppermann * Initial send window. It will be updated with 10335396d0f8SAndre Oppermann * the next incoming segment to the scaled value. 10345396d0f8SAndre Oppermann */ 10355396d0f8SAndre Oppermann tp->snd_wnd = th->th_win; 1036be2ac88cSJonathan Lemon if (to.to_flags & TOF_TS) { 1037be2ac88cSJonathan Lemon tp->t_flags |= TF_RCVD_TSTMP; 1038be2ac88cSJonathan Lemon tp->ts_recent = to.to_tsval; 1039be2ac88cSJonathan Lemon tp->ts_recent_age = ticks; 1040be2ac88cSJonathan Lemon } 1041be2ac88cSJonathan Lemon if (to.to_flags & TOF_MSS) 1042be2ac88cSJonathan Lemon tcp_mss(tp, to.to_mss); 10433529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 10443529149eSAndre Oppermann (to.to_flags & TOF_SACKPERM) == 0) 10453529149eSAndre Oppermann tp->t_flags &= ~TF_SACK_PERMIT; 10466d90faf3SPaul Saab } 10476d90faf3SPaul Saab 1048df8bae1dSRodney W. Grimes /* 1049df8bae1dSRodney W. Grimes * Header prediction: check for the two common cases 1050df8bae1dSRodney W. Grimes * of a uni-directional data xfer. If the packet has 1051df8bae1dSRodney W. Grimes * no control flags, is in-sequence, the window didn't 1052df8bae1dSRodney W. Grimes * change and we're not retransmitting, it's a 1053df8bae1dSRodney W. Grimes * candidate. If the length is zero and the ack moved 1054df8bae1dSRodney W. Grimes * forward, we're the sender side of the xfer. Just 1055df8bae1dSRodney W. Grimes * free the data acked & wake any higher level process 1056df8bae1dSRodney W. Grimes * that was blocked waiting for space. If the length 1057df8bae1dSRodney W. Grimes * is non-zero and the ack didn't move, we're the 1058df8bae1dSRodney W. Grimes * receiver side. If we're getting packets in-order 1059df8bae1dSRodney W. Grimes * (the reassembly queue is empty), add the data to 1060df8bae1dSRodney W. Grimes * the socket buffer and note that we need a delayed ack. 1061a0292f23SGarrett Wollman * Make sure that the hidden state-flags are also off. 1062c5ad39b9SAndre Oppermann * Since we check for TCPS_ESTABLISHED first, it can only 1063a0292f23SGarrett Wollman * be TH_NEEDSYN. 1064df8bae1dSRodney W. Grimes */ 1065df8bae1dSRodney W. Grimes if (tp->t_state == TCPS_ESTABLISHED && 1066c5ad39b9SAndre Oppermann th->th_seq == tp->rcv_nxt && 1067fb59c426SYoshinobu Inoue (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && 1068c5ad39b9SAndre Oppermann tp->snd_nxt == tp->snd_max && 1069c5ad39b9SAndre Oppermann tiwin && tiwin == tp->snd_wnd && 1070a0292f23SGarrett Wollman ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && 1071c5ad39b9SAndre Oppermann LIST_EMPTY(&tp->t_segq) && 1072be2ac88cSJonathan Lemon ((to.to_flags & TOF_TS) == 0 || 1073c5ad39b9SAndre Oppermann TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) { 1074df8bae1dSRodney W. Grimes 1075df8bae1dSRodney W. Grimes /* 1076df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1077df8bae1dSRodney W. Grimes * record the timestamp. 1078a0292f23SGarrett Wollman * NOTE that the test is modified according to the latest 1079a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1080df8bae1dSRodney W. Grimes */ 1081be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1082fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { 10839b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1084a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1085df8bae1dSRodney W. Grimes } 1086df8bae1dSRodney W. Grimes 1087fb59c426SYoshinobu Inoue if (tlen == 0) { 1088fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_ack, tp->snd_una) && 1089fb59c426SYoshinobu Inoue SEQ_LEQ(th->th_ack, tp->snd_max) && 1090233e8c18SGarrett Wollman tp->snd_cwnd >= tp->snd_wnd && 1091603724d3SBjoern A. Zeeb ((!V_tcp_do_newreno && 10923529149eSAndre Oppermann !(tp->t_flags & TF_SACK_PERMIT) && 1093cb942153SJeffrey Hsu tp->t_dupacks < tcprexmtthresh) || 1094603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 10953529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 1096fc30a251SAndre Oppermann !IN_FASTRECOVERY(tp) && 1097fc30a251SAndre Oppermann (to.to_flags & TOF_SACK) == 0 && 1098482ac968SPaul Saab TAILQ_EMPTY(&tp->snd_holes)))) { 1099302ce8d6SAndre Oppermann KASSERT(headlocked, 1100302ce8d6SAndre Oppermann ("%s: headlocked", __func__)); 1101603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1102e0bef1cbSRobert Watson headlocked = 0; 1103df8bae1dSRodney W. Grimes /* 1104e8949f74SAndre Oppermann * This is a pure ack for outstanding data. 1105df8bae1dSRodney W. Grimes */ 1106603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_predack; 11079b8b58e0SJonathan Lemon /* 1108e8949f74SAndre Oppermann * "bad retransmit" recovery. 11099b8b58e0SJonathan Lemon */ 11109b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && 11119b8b58e0SJonathan Lemon ticks < tp->t_badrxtwin) { 1112603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_sndrexmitbad; 11139b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 11149b8b58e0SJonathan Lemon tp->snd_ssthresh = 11159b8b58e0SJonathan Lemon tp->snd_ssthresh_prev; 11169d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 11179d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 11189d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 11199b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 11209b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; 11219b8b58e0SJonathan Lemon } 1122fa55172bSMatthew Dillon 1123fa55172bSMatthew Dillon /* 1124fa55172bSMatthew Dillon * Recalculate the transmit timer / rtt. 1125fa55172bSMatthew Dillon * 1126fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 1127fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 1128fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 1129fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 1130fa55172bSMatthew Dillon */ 1131fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 1132fa55172bSMatthew Dillon to.to_tsecr) { 1133eaf80179SAndre Oppermann if (!tp->t_rttlow || 1134eaf80179SAndre Oppermann tp->t_rttlow > ticks - to.to_tsecr) 1135eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 1136a0292f23SGarrett Wollman tcp_xmit_timer(tp, 11379b8b58e0SJonathan Lemon ticks - to.to_tsecr + 1); 1138fa55172bSMatthew Dillon } else if (tp->t_rtttime && 1139fa55172bSMatthew Dillon SEQ_GT(th->th_ack, tp->t_rtseq)) { 1140eaf80179SAndre Oppermann if (!tp->t_rttlow || 1141eaf80179SAndre Oppermann tp->t_rttlow > ticks - tp->t_rtttime) 1142eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 1143c068736aSJeffrey Hsu tcp_xmit_timer(tp, 1144c068736aSJeffrey Hsu ticks - tp->t_rtttime); 1145fa55172bSMatthew Dillon } 11461fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 1147fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 1148603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackpack++; 1149603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackbyte += acked; 1150df8bae1dSRodney W. Grimes sbdrop(&so->so_snd, acked); 11519d11646dSJeffrey Hsu if (SEQ_GT(tp->snd_una, tp->snd_recover) && 11529d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 11539d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 11549d11646dSJeffrey Hsu tp->snd_una = th->th_ack; 11551645d090SJeff Roberson /* 1156e8949f74SAndre Oppermann * Pull snd_wl2 up to prevent seq wrap relative 11571645d090SJeff Roberson * to th_ack. 11581645d090SJeff Roberson */ 1159cb942153SJeffrey Hsu tp->snd_wl2 = th->th_ack; 1160b5addd85SJeffrey Hsu tp->t_dupacks = 0; 1161df8bae1dSRodney W. Grimes m_freem(m); 1162e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made. */ 1163df8bae1dSRodney W. Grimes 1164df8bae1dSRodney W. Grimes /* 1165df8bae1dSRodney W. Grimes * If all outstanding data are acked, stop 1166df8bae1dSRodney W. Grimes * retransmit timer, otherwise restart timer 1167df8bae1dSRodney W. Grimes * using current (possibly backed-off) value. 1168df8bae1dSRodney W. Grimes * If process is waiting for space, 1169df8bae1dSRodney W. Grimes * wakeup/selwakeup/signal. If data 1170df8bae1dSRodney W. Grimes * are ready to send, let tcp_output 1171df8bae1dSRodney W. Grimes * decide between more output or persist. 1172e8949f74SAndre Oppermann */ 11733c653157SHartmut Brandt #ifdef TCPDEBUG 11743c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 11753c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 11763c653157SHartmut Brandt (void *)tcp_saveipgen, 11773c653157SHartmut Brandt &tcp_savetcp, 0); 11783c653157SHartmut Brandt #endif 1179df8bae1dSRodney W. Grimes if (tp->snd_una == tp->snd_max) 1180b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1181b8152ba7SAndre Oppermann else if (!tcp_timer_active(tp, TT_PERSIST)) 1182b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 1183b8152ba7SAndre Oppermann tp->t_rxtcur); 1184df8bae1dSRodney W. Grimes sowwakeup(so); 1185df8bae1dSRodney W. Grimes if (so->so_snd.sb_cc) 1186df8bae1dSRodney W. Grimes (void) tcp_output(tp); 1187a14c749fSJonathan Lemon goto check_delack; 1188df8bae1dSRodney W. Grimes } 1189fb59c426SYoshinobu Inoue } else if (th->th_ack == tp->snd_una && 1190fb59c426SYoshinobu Inoue tlen <= sbspace(&so->so_rcv)) { 11916741ecf5SAndre Oppermann int newsize = 0; /* automatic sockbuf scaling */ 11926741ecf5SAndre Oppermann 1193302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked", __func__)); 1194603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 1195e0bef1cbSRobert Watson headlocked = 0; 1196df8bae1dSRodney W. Grimes /* 1197e8949f74SAndre Oppermann * This is a pure, in-sequence data packet 1198df8bae1dSRodney W. Grimes * with nothing on the reassembly queue and 1199df8bae1dSRodney W. Grimes * we have enough buffer space to take it. 1200df8bae1dSRodney W. Grimes */ 12016d90faf3SPaul Saab /* Clean receiver SACK report if present */ 12023529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks) 12036d90faf3SPaul Saab tcp_clean_sackreport(tp); 1204603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_preddat; 1205fb59c426SYoshinobu Inoue tp->rcv_nxt += tlen; 12061645d090SJeff Roberson /* 12071645d090SJeff Roberson * Pull snd_wl1 up to prevent seq wrap relative to 12081645d090SJeff Roberson * th_seq. 12091645d090SJeff Roberson */ 12101645d090SJeff Roberson tp->snd_wl1 = th->th_seq; 12111645d090SJeff Roberson /* 12121645d090SJeff Roberson * Pull rcv_up up to prevent seq wrap relative to 12131645d090SJeff Roberson * rcv_nxt. 12141645d090SJeff Roberson */ 12151645d090SJeff Roberson tp->rcv_up = tp->rcv_nxt; 1216603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpack++; 1217603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyte += tlen; 1218e8949f74SAndre Oppermann ND6_HINT(tp); /* Some progress has been made */ 12193c653157SHartmut Brandt #ifdef TCPDEBUG 12203c653157SHartmut Brandt if (so->so_options & SO_DEBUG) 12213c653157SHartmut Brandt tcp_trace(TA_INPUT, ostate, tp, 12223c653157SHartmut Brandt (void *)tcp_saveipgen, &tcp_savetcp, 0); 12233c653157SHartmut Brandt #endif 12246741ecf5SAndre Oppermann /* 12256741ecf5SAndre Oppermann * Automatic sizing of receive socket buffer. Often the send 12266741ecf5SAndre Oppermann * buffer size is not optimally adjusted to the actual network 12276741ecf5SAndre Oppermann * conditions at hand (delay bandwidth product). Setting the 12286741ecf5SAndre Oppermann * buffer size too small limits throughput on links with high 12296741ecf5SAndre Oppermann * bandwidth and high delay (eg. trans-continental/oceanic links). 12306741ecf5SAndre Oppermann * 12316741ecf5SAndre Oppermann * On the receive side the socket buffer memory is only rarely 12326741ecf5SAndre Oppermann * used to any significant extent. This allows us to be much 12336741ecf5SAndre Oppermann * more aggressive in scaling the receive socket buffer. For 12346741ecf5SAndre Oppermann * the case that the buffer space is actually used to a large 12356741ecf5SAndre Oppermann * extent and we run out of kernel memory we can simply drop 12366741ecf5SAndre Oppermann * the new segments; TCP on the sender will just retransmit it 12376741ecf5SAndre Oppermann * later. Setting the buffer size too big may only consume too 12386741ecf5SAndre Oppermann * much kernel memory if the application doesn't read() from 12396741ecf5SAndre Oppermann * the socket or packet loss or reordering makes use of the 12406741ecf5SAndre Oppermann * reassembly queue. 12416741ecf5SAndre Oppermann * 12426741ecf5SAndre Oppermann * The criteria to step up the receive buffer one notch are: 12436741ecf5SAndre Oppermann * 1. the number of bytes received during the time it takes 12446741ecf5SAndre Oppermann * one timestamp to be reflected back to us (the RTT); 12456741ecf5SAndre Oppermann * 2. received bytes per RTT is within seven eighth of the 12466741ecf5SAndre Oppermann * current socket buffer size; 12476741ecf5SAndre Oppermann * 3. receive buffer size has not hit maximal automatic size; 12486741ecf5SAndre Oppermann * 12496741ecf5SAndre Oppermann * This algorithm does one step per RTT at most and only if 12506741ecf5SAndre Oppermann * we receive a bulk stream w/o packet losses or reorderings. 12516741ecf5SAndre Oppermann * Shrinking the buffer during idle times is not necessary as 12526741ecf5SAndre Oppermann * it doesn't consume any memory when idle. 12536741ecf5SAndre Oppermann * 12546741ecf5SAndre Oppermann * TODO: Only step up if the application is actually serving 12556741ecf5SAndre Oppermann * the buffer to better manage the socket buffer resources. 1256df8bae1dSRodney W. Grimes */ 1257603724d3SBjoern A. Zeeb if (V_tcp_do_autorcvbuf && 12586741ecf5SAndre Oppermann to.to_tsecr && 12596741ecf5SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) { 12606741ecf5SAndre Oppermann if (to.to_tsecr > tp->rfbuf_ts && 12616741ecf5SAndre Oppermann to.to_tsecr - tp->rfbuf_ts < hz) { 12626741ecf5SAndre Oppermann if (tp->rfbuf_cnt > 12636741ecf5SAndre Oppermann (so->so_rcv.sb_hiwat / 8 * 7) && 12646741ecf5SAndre Oppermann so->so_rcv.sb_hiwat < 1265603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max) { 12666741ecf5SAndre Oppermann newsize = 12676741ecf5SAndre Oppermann min(so->so_rcv.sb_hiwat + 1268603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_inc, 1269603724d3SBjoern A. Zeeb V_tcp_autorcvbuf_max); 12706741ecf5SAndre Oppermann } 12716741ecf5SAndre Oppermann /* Start over with next RTT. */ 12726741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 12736741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 12746741ecf5SAndre Oppermann } else 12756741ecf5SAndre Oppermann tp->rfbuf_cnt += tlen; /* add up */ 12766741ecf5SAndre Oppermann } 12776741ecf5SAndre Oppermann 12786741ecf5SAndre Oppermann /* Add data to socket buffer. */ 12791e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1280c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1281c1c36a2cSMike Silbersack m_freem(m); 1282c1c36a2cSMike Silbersack } else { 12836741ecf5SAndre Oppermann /* 12846741ecf5SAndre Oppermann * Set new socket buffer size. 12856741ecf5SAndre Oppermann * Give up when limit is reached. 12866741ecf5SAndre Oppermann */ 12876741ecf5SAndre Oppermann if (newsize) 12886741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_rcv, 12896c8286e4SRobert Watson newsize, so, NULL)) 12906741ecf5SAndre Oppermann so->so_rcv.sb_flags &= ~SB_AUTOSIZE; 1291fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 12921e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 1293c1c36a2cSMike Silbersack } 1294e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 12951e4d7da7SRobert Watson sorwakeup_locked(so); 1296d8c85a26SJonathan Lemon if (DELAY_ACK(tp)) { 12973bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 1298f498eeeeSDavid Greenman } else { 1299e612a582SDavid Greenman tp->t_flags |= TF_ACKNOW; 1300e612a582SDavid Greenman tcp_output(tp); 1301e612a582SDavid Greenman } 1302a14c749fSJonathan Lemon goto check_delack; 1303df8bae1dSRodney W. Grimes } 1304df8bae1dSRodney W. Grimes } 1305df8bae1dSRodney W. Grimes 1306df8bae1dSRodney W. Grimes /* 1307df8bae1dSRodney W. Grimes * Calculate amount of space in receive window, 1308df8bae1dSRodney W. Grimes * and then do TCP input processing. 1309df8bae1dSRodney W. Grimes * Receive window is amount of space in rcv queue, 1310df8bae1dSRodney W. Grimes * but not less than advertised window. 1311df8bae1dSRodney W. Grimes */ 1312df8bae1dSRodney W. Grimes win = sbspace(&so->so_rcv); 1313df8bae1dSRodney W. Grimes if (win < 0) 1314df8bae1dSRodney W. Grimes win = 0; 131566e39adcSJohn Polstra tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 1316df8bae1dSRodney W. Grimes 13176741ecf5SAndre Oppermann /* Reset receive buffer auto scaling when not in bulk receive mode. */ 13186741ecf5SAndre Oppermann tp->rfbuf_ts = 0; 13196741ecf5SAndre Oppermann tp->rfbuf_cnt = 0; 13206741ecf5SAndre Oppermann 1321df8bae1dSRodney W. Grimes switch (tp->t_state) { 1322df8bae1dSRodney W. Grimes 1323df8bae1dSRodney W. Grimes /* 1324764d8cefSBill Fenner * If the state is SYN_RECEIVED: 1325764d8cefSBill Fenner * if seg contains an ACK, but not for our SYN/ACK, send a RST. 1326764d8cefSBill Fenner */ 1327764d8cefSBill Fenner case TCPS_SYN_RECEIVED: 1328fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1329fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->snd_una) || 1330a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max))) { 1331a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1332a57815efSBosko Milekic goto dropwithreset; 1333a57815efSBosko Milekic } 1334764d8cefSBill Fenner break; 1335764d8cefSBill Fenner 1336764d8cefSBill Fenner /* 1337df8bae1dSRodney W. Grimes * If the state is SYN_SENT: 1338df8bae1dSRodney W. Grimes * if seg contains an ACK, but not for our SYN, drop the input. 1339df8bae1dSRodney W. Grimes * if seg contains a RST, then drop the connection. 1340df8bae1dSRodney W. Grimes * if seg does not contain SYN, then drop it. 1341df8bae1dSRodney W. Grimes * Otherwise this is an acceptable SYN segment 1342df8bae1dSRodney W. Grimes * initialize tp->rcv_nxt and tp->irs 1343df8bae1dSRodney W. Grimes * if seg contains ack then advance tp->snd_una 1344f2512ba1SRui Paulo * if seg contains an ECE and ECN support is enabled, the stream 1345f2512ba1SRui Paulo * is ECN capable. 1346df8bae1dSRodney W. Grimes * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 1347df8bae1dSRodney W. Grimes * arrange for segment to be acked (eventually) 1348df8bae1dSRodney W. Grimes * continue processing rest of data/controls, beginning with URG 1349df8bae1dSRodney W. Grimes */ 1350df8bae1dSRodney W. Grimes case TCPS_SYN_SENT: 1351fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 1352fb59c426SYoshinobu Inoue (SEQ_LEQ(th->th_ack, tp->iss) || 1353fb59c426SYoshinobu Inoue SEQ_GT(th->th_ack, tp->snd_max))) { 1354a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1355a0292f23SGarrett Wollman goto dropwithreset; 1356a0292f23SGarrett Wollman } 13570ca3f933SAndre Oppermann if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) 1358df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNREFUSED); 13590ca3f933SAndre Oppermann if (thflags & TH_RST) 1360df8bae1dSRodney W. Grimes goto drop; 13610ca3f933SAndre Oppermann if (!(thflags & TH_SYN)) 1362df8bae1dSRodney W. Grimes goto drop; 1363464fcfbcSAndre Oppermann 1364fb59c426SYoshinobu Inoue tp->irs = th->th_seq; 1365df8bae1dSRodney W. Grimes tcp_rcvseqinit(tp); 1366fb59c426SYoshinobu Inoue if (thflags & TH_ACK) { 1367603724d3SBjoern A. Zeeb V_tcpstat.tcps_connects++; 1368845799c1SAndras Olah soisconnected(so); 1369c488362eSRobert Watson #ifdef MAC 1370310e7cebSRobert Watson SOCK_LOCK(so); 137130d239bcSRobert Watson mac_socketpeer_set_from_mbuf(m, so); 1372310e7cebSRobert Watson SOCK_UNLOCK(so); 1373c488362eSRobert Watson #endif 1374845799c1SAndras Olah /* Do window scaling on this connection? */ 1375845799c1SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1376845799c1SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1377845799c1SAndras Olah tp->rcv_scale = tp->request_r_scale; 1378845799c1SAndras Olah } 1379a0292f23SGarrett Wollman tp->rcv_adv += tp->rcv_wnd; 1380a0292f23SGarrett Wollman tp->snd_una++; /* SYN is acked */ 1381a0292f23SGarrett Wollman /* 1382a0292f23SGarrett Wollman * If there's data, delay ACK; if there's also a FIN 1383a0292f23SGarrett Wollman * ACKNOW will be turned on later. 1384a0292f23SGarrett Wollman */ 1385d8c85a26SJonathan Lemon if (DELAY_ACK(tp) && tlen != 0) 1386b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 1387b8152ba7SAndre Oppermann tcp_delacktime); 1388a0292f23SGarrett Wollman else 1389a0292f23SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1390f2512ba1SRui Paulo 1391603724d3SBjoern A. Zeeb if ((thflags & TH_ECE) && V_tcp_do_ecn) { 1392f2512ba1SRui Paulo tp->t_flags |= TF_ECN_PERMIT; 1393603724d3SBjoern A. Zeeb V_tcpstat.tcps_ecn_shs++; 1394f2512ba1SRui Paulo } 1395f2512ba1SRui Paulo 1396a0292f23SGarrett Wollman /* 1397a0292f23SGarrett Wollman * Received <SYN,ACK> in SYN_SENT[*] state. 1398a0292f23SGarrett Wollman * Transitions: 1399a0292f23SGarrett Wollman * SYN_SENT --> ESTABLISHED 1400a0292f23SGarrett Wollman * SYN_SENT* --> FIN_WAIT_1 1401a0292f23SGarrett Wollman */ 14029b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1403a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1404a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1405a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 1406fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 14077ff19458SPaul Traina } else { 1408a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1409b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 14107ff19458SPaul Traina } 1411a0292f23SGarrett Wollman } else { 1412a0292f23SGarrett Wollman /* 1413c068736aSJeffrey Hsu * Received initial SYN in SYN-SENT[*] state => 1414c068736aSJeffrey Hsu * simultaneous open. If segment contains CC option 1415c068736aSJeffrey Hsu * and there is a cached CC, apply TAO test. 1416c068736aSJeffrey Hsu * If it succeeds, connection is * half-synchronized. 1417c068736aSJeffrey Hsu * Otherwise, do 3-way handshake: 1418a0292f23SGarrett Wollman * SYN-SENT -> SYN-RECEIVED 1419a0292f23SGarrett Wollman * SYN-SENT* -> SYN-RECEIVED* 1420a0292f23SGarrett Wollman * If there was no CC option, clear cached CC value. 1421a0292f23SGarrett Wollman */ 14224b8e98d6SQing Li tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); 1423b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 1424df8bae1dSRodney W. Grimes tp->t_state = TCPS_SYN_RECEIVED; 1425a0292f23SGarrett Wollman } 1426df8bae1dSRodney W. Grimes 1427302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: head not locked", 1428302ce8d6SAndre Oppermann __func__)); 14298501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 14307cfc6904SRobert Watson 1431df8bae1dSRodney W. Grimes /* 1432fb59c426SYoshinobu Inoue * Advance th->th_seq to correspond to first data byte. 1433df8bae1dSRodney W. Grimes * If data, trim to stay within window, 1434df8bae1dSRodney W. Grimes * dropping FIN if necessary. 1435df8bae1dSRodney W. Grimes */ 1436fb59c426SYoshinobu Inoue th->th_seq++; 1437fb59c426SYoshinobu Inoue if (tlen > tp->rcv_wnd) { 1438fb59c426SYoshinobu Inoue todrop = tlen - tp->rcv_wnd; 1439df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1440fb59c426SYoshinobu Inoue tlen = tp->rcv_wnd; 1441fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1442603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpackafterwin++; 1443603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += todrop; 1444df8bae1dSRodney W. Grimes } 1445fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 1446fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq; 1447a0292f23SGarrett Wollman /* 1448a0292f23SGarrett Wollman * Client side of transaction: already sent SYN and data. 1449a0292f23SGarrett Wollman * If the remote host used T/TCP to validate the SYN, 1450a0292f23SGarrett Wollman * our data will be ACK'd; if so, enter normal data segment 1451a0292f23SGarrett Wollman * processing in the middle of step 5, ack processing. 1452a0292f23SGarrett Wollman * Otherwise, goto step 6. 1453a0292f23SGarrett Wollman */ 1454fb59c426SYoshinobu Inoue if (thflags & TH_ACK) 1455a0292f23SGarrett Wollman goto process_ACK; 1456c068736aSJeffrey Hsu 1457df8bae1dSRodney W. Grimes goto step6; 1458c068736aSJeffrey Hsu 1459a0292f23SGarrett Wollman /* 1460a0292f23SGarrett Wollman * If the state is LAST_ACK or CLOSING or TIME_WAIT: 1461c94c54e4SAndre Oppermann * do normal processing. 1462a0292f23SGarrett Wollman * 1463c94c54e4SAndre Oppermann * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. 1464a0292f23SGarrett Wollman */ 1465a0292f23SGarrett Wollman case TCPS_LAST_ACK: 1466a0292f23SGarrett Wollman case TCPS_CLOSING: 1467a0292f23SGarrett Wollman break; /* continue normal processing */ 1468df8bae1dSRodney W. Grimes } 1469df8bae1dSRodney W. Grimes 1470df8bae1dSRodney W. Grimes /* 1471df8bae1dSRodney W. Grimes * States other than LISTEN or SYN_SENT. 147280ab7c0eSGarrett Wollman * First check the RST flag and sequence number since reset segments 147380ab7c0eSGarrett Wollman * are exempt from the timestamp and connection count tests. This 147480ab7c0eSGarrett Wollman * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix 147580ab7c0eSGarrett Wollman * below which allowed reset segments in half the sequence space 147680ab7c0eSGarrett Wollman * to fall though and be processed (which gives forged reset 147780ab7c0eSGarrett Wollman * segments with a random sequence number a 50 percent chance of 147880ab7c0eSGarrett Wollman * killing a connection). 147980ab7c0eSGarrett Wollman * Then check timestamp, if present. 1480a0292f23SGarrett Wollman * Then check the connection count, if present. 1481df8bae1dSRodney W. Grimes * Then check that at least some bytes of segment are within 1482df8bae1dSRodney W. Grimes * receive window. If segment begins before rcv_nxt, 1483df8bae1dSRodney W. Grimes * drop leading data (and SYN); if nothing left, just ack. 1484df8bae1dSRodney W. Grimes * 148580ab7c0eSGarrett Wollman * 148680ab7c0eSGarrett Wollman * If the RST bit is set, check the sequence number to see 148780ab7c0eSGarrett Wollman * if this is a valid reset segment. 148880ab7c0eSGarrett Wollman * RFC 793 page 37: 148980ab7c0eSGarrett Wollman * In all states except SYN-SENT, all reset (RST) segments 149080ab7c0eSGarrett Wollman * are validated by checking their SEQ-fields. A reset is 149180ab7c0eSGarrett Wollman * valid if its sequence number is in the window. 149280ab7c0eSGarrett Wollman * Note: this does not take into account delayed ACKs, so 149380ab7c0eSGarrett Wollman * we should test against last_ack_sent instead of rcv_nxt. 14941a244a61SJonathan Lemon * The sequence number in the reset segment is normally an 14951a244a61SJonathan Lemon * echo of our outgoing acknowlegement numbers, but some hosts 14961a244a61SJonathan Lemon * send a reset with the sequence number at the rightmost edge 14971a244a61SJonathan Lemon * of our receive window, and we have to handle this case. 149880dd2a81SMike Silbersack * Note 2: Paul Watson's paper "Slipping in the Window" has shown 149980dd2a81SMike Silbersack * that brute force RST attacks are possible. To combat this, 150080dd2a81SMike Silbersack * we use a much stricter check while in the ESTABLISHED state, 150180dd2a81SMike Silbersack * only accepting RSTs where the sequence number is equal to 150280dd2a81SMike Silbersack * last_ack_sent. In all other states (the states in which a 150380dd2a81SMike Silbersack * RST is more likely), the more permissive check is used. 150480ab7c0eSGarrett Wollman * If we have multiple segments in flight, the intial reset 150580ab7c0eSGarrett Wollman * segment sequence numbers will be to the left of last_ack_sent, 150680ab7c0eSGarrett Wollman * but they will eventually catch up. 150780ab7c0eSGarrett Wollman * In any case, it never made sense to trim reset segments to 150880ab7c0eSGarrett Wollman * fit the receive window since RFC 1122 says: 150980ab7c0eSGarrett Wollman * 4.2.2.12 RST Segment: RFC-793 Section 3.4 151080ab7c0eSGarrett Wollman * 151180ab7c0eSGarrett Wollman * A TCP SHOULD allow a received RST segment to include data. 151280ab7c0eSGarrett Wollman * 151380ab7c0eSGarrett Wollman * DISCUSSION 151480ab7c0eSGarrett Wollman * It has been suggested that a RST segment could contain 151580ab7c0eSGarrett Wollman * ASCII text that encoded and explained the cause of the 151680ab7c0eSGarrett Wollman * RST. No standard has yet been established for such 151780ab7c0eSGarrett Wollman * data. 151880ab7c0eSGarrett Wollman * 151980ab7c0eSGarrett Wollman * If the reset segment passes the sequence number test examine 152080ab7c0eSGarrett Wollman * the state: 152180ab7c0eSGarrett Wollman * SYN_RECEIVED STATE: 152280ab7c0eSGarrett Wollman * If passive open, return to LISTEN state. 152380ab7c0eSGarrett Wollman * If active open, inform user that connection was refused. 1524745bab7fSDima Dorfman * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: 152580ab7c0eSGarrett Wollman * Inform user that connection was reset, and close tcb. 1526e9bd3a37SJonathan M. Bresler * CLOSING, LAST_ACK STATES: 152780ab7c0eSGarrett Wollman * Close the tcb. 1528e9bd3a37SJonathan M. Bresler * TIME_WAIT STATE: 152980ab7c0eSGarrett Wollman * Drop the segment - see Stevens, vol. 2, p. 964 and 153080ab7c0eSGarrett Wollman * RFC 1337. 153180ab7c0eSGarrett Wollman */ 1532fb59c426SYoshinobu Inoue if (thflags & TH_RST) { 153395ad8418SQing Li if (SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 153495ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { 153580ab7c0eSGarrett Wollman switch (tp->t_state) { 153680ab7c0eSGarrett Wollman 153780ab7c0eSGarrett Wollman case TCPS_SYN_RECEIVED: 153880ab7c0eSGarrett Wollman so->so_error = ECONNREFUSED; 153980ab7c0eSGarrett Wollman goto close; 154080ab7c0eSGarrett Wollman 154180ab7c0eSGarrett Wollman case TCPS_ESTABLISHED: 1542603724d3SBjoern A. Zeeb if (V_tcp_insecure_rst == 0 && 154395ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->rcv_nxt - 1) && 154495ad8418SQing Li SEQ_LEQ(th->th_seq, tp->rcv_nxt + 1)) && 154595ad8418SQing Li !(SEQ_GEQ(th->th_seq, tp->last_ack_sent - 1) && 154695ad8418SQing Li SEQ_LEQ(th->th_seq, tp->last_ack_sent + 1))) { 1547603724d3SBjoern A. Zeeb V_tcpstat.tcps_badrst++; 154880dd2a81SMike Silbersack goto drop; 154980dd2a81SMike Silbersack } 1550564aab1fSAndre Oppermann /* FALLTHROUGH */ 155180ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_1: 155280ab7c0eSGarrett Wollman case TCPS_FIN_WAIT_2: 155380ab7c0eSGarrett Wollman case TCPS_CLOSE_WAIT: 155480ab7c0eSGarrett Wollman so->so_error = ECONNRESET; 155580ab7c0eSGarrett Wollman close: 155680ab7c0eSGarrett Wollman tp->t_state = TCPS_CLOSED; 1557603724d3SBjoern A. Zeeb V_tcpstat.tcps_drops++; 1558302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1559302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 156080ab7c0eSGarrett Wollman tp = tcp_close(tp); 156180ab7c0eSGarrett Wollman break; 156280ab7c0eSGarrett Wollman 156380ab7c0eSGarrett Wollman case TCPS_CLOSING: 156480ab7c0eSGarrett Wollman case TCPS_LAST_ACK: 1565302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: " 1566302ce8d6SAndre Oppermann "tcp_close.2: head not locked", __func__)); 156780ab7c0eSGarrett Wollman tp = tcp_close(tp); 156880ab7c0eSGarrett Wollman break; 156980ab7c0eSGarrett Wollman } 157080ab7c0eSGarrett Wollman } 157180ab7c0eSGarrett Wollman goto drop; 157280ab7c0eSGarrett Wollman } 157380ab7c0eSGarrett Wollman 157480ab7c0eSGarrett Wollman /* 1575df8bae1dSRodney W. Grimes * RFC 1323 PAWS: If we have a timestamp reply on this segment 1576df8bae1dSRodney W. Grimes * and it's less than ts_recent, drop it. 1577df8bae1dSRodney W. Grimes */ 1578be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && 157980ab7c0eSGarrett Wollman TSTMP_LT(to.to_tsval, tp->ts_recent)) { 1580df8bae1dSRodney W. Grimes 1581df8bae1dSRodney W. Grimes /* Check to see if ts_recent is over 24 days old. */ 15829b8b58e0SJonathan Lemon if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) { 1583df8bae1dSRodney W. Grimes /* 1584df8bae1dSRodney W. Grimes * Invalidate ts_recent. If this segment updates 1585df8bae1dSRodney W. Grimes * ts_recent, the age will be reset later and ts_recent 1586df8bae1dSRodney W. Grimes * will get a valid value. If it does not, setting 1587df8bae1dSRodney W. Grimes * ts_recent to zero will at least satisfy the 1588df8bae1dSRodney W. Grimes * requirement that zero be placed in the timestamp 1589df8bae1dSRodney W. Grimes * echo reply when ts_recent isn't valid. The 1590df8bae1dSRodney W. Grimes * age isn't reset until we get a valid ts_recent 1591df8bae1dSRodney W. Grimes * because we don't want out-of-order segments to be 1592df8bae1dSRodney W. Grimes * dropped when ts_recent is old. 1593df8bae1dSRodney W. Grimes */ 1594df8bae1dSRodney W. Grimes tp->ts_recent = 0; 1595df8bae1dSRodney W. Grimes } else { 1596603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvduppack++; 1597603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupbyte += tlen; 1598603724d3SBjoern A. Zeeb V_tcpstat.tcps_pawsdrop++; 159907fd333dSMatthew Dillon if (tlen) 1600df8bae1dSRodney W. Grimes goto dropafterack; 16011ab4789dSMatthew Dillon goto drop; 16021ab4789dSMatthew Dillon } 1603df8bae1dSRodney W. Grimes } 1604df8bae1dSRodney W. Grimes 1605a0292f23SGarrett Wollman /* 160680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, validate that the packet belongs to 160780ab7c0eSGarrett Wollman * this connection before trimming the data to fit the receive 160880ab7c0eSGarrett Wollman * window. Check the sequence number versus IRS since we know 160980ab7c0eSGarrett Wollman * the sequence numbers haven't wrapped. This is a partial fix 161080ab7c0eSGarrett Wollman * for the "LAND" DoS attack. 161180ab7c0eSGarrett Wollman */ 1612a57815efSBosko Milekic if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { 1613a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 1614a57815efSBosko Milekic goto dropwithreset; 1615a57815efSBosko Milekic } 161680ab7c0eSGarrett Wollman 1617fb59c426SYoshinobu Inoue todrop = tp->rcv_nxt - th->th_seq; 1618df8bae1dSRodney W. Grimes if (todrop > 0) { 1619fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1620fb59c426SYoshinobu Inoue thflags &= ~TH_SYN; 1621fb59c426SYoshinobu Inoue th->th_seq++; 1622fb59c426SYoshinobu Inoue if (th->th_urp > 1) 1623fb59c426SYoshinobu Inoue th->th_urp--; 1624df8bae1dSRodney W. Grimes else 1625fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1626df8bae1dSRodney W. Grimes todrop--; 1627df8bae1dSRodney W. Grimes } 1628df8bae1dSRodney W. Grimes /* 1629dac20301SGarrett Wollman * Following if statement from Stevens, vol. 2, p. 960. 1630df8bae1dSRodney W. Grimes */ 1631fb59c426SYoshinobu Inoue if (todrop > tlen 1632fb59c426SYoshinobu Inoue || (todrop == tlen && (thflags & TH_FIN) == 0)) { 1633dac20301SGarrett Wollman /* 1634dac20301SGarrett Wollman * Any valid FIN must be to the left of the window. 1635dac20301SGarrett Wollman * At this point the FIN must be a duplicate or out 1636dac20301SGarrett Wollman * of sequence; drop it. 1637dac20301SGarrett Wollman */ 1638fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 1639dac20301SGarrett Wollman 1640df8bae1dSRodney W. Grimes /* 1641dac20301SGarrett Wollman * Send an ACK to resynchronize and drop any data. 1642dac20301SGarrett Wollman * But keep on processing for RST or ACK. 1643df8bae1dSRodney W. Grimes */ 1644dac20301SGarrett Wollman tp->t_flags |= TF_ACKNOW; 1645fb59c426SYoshinobu Inoue todrop = tlen; 1646603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvduppack++; 1647603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupbyte += todrop; 1648df8bae1dSRodney W. Grimes } else { 1649603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpartduppack++; 1650603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpartdupbyte += todrop; 1651df8bae1dSRodney W. Grimes } 1652fb59c426SYoshinobu Inoue drop_hdrlen += todrop; /* drop from the top afterwards */ 1653fb59c426SYoshinobu Inoue th->th_seq += todrop; 1654fb59c426SYoshinobu Inoue tlen -= todrop; 1655fb59c426SYoshinobu Inoue if (th->th_urp > todrop) 1656fb59c426SYoshinobu Inoue th->th_urp -= todrop; 1657df8bae1dSRodney W. Grimes else { 1658fb59c426SYoshinobu Inoue thflags &= ~TH_URG; 1659fb59c426SYoshinobu Inoue th->th_urp = 0; 1660df8bae1dSRodney W. Grimes } 1661df8bae1dSRodney W. Grimes } 1662df8bae1dSRodney W. Grimes 1663df8bae1dSRodney W. Grimes /* 1664df8bae1dSRodney W. Grimes * If new data are received on a connection after the 1665df8bae1dSRodney W. Grimes * user processes are gone, then RST the other end. 1666df8bae1dSRodney W. Grimes */ 1667df8bae1dSRodney W. Grimes if ((so->so_state & SS_NOFDREF) && 1668fb59c426SYoshinobu Inoue tp->t_state > TCPS_CLOSE_WAIT && tlen) { 1669773673c1SAndre Oppermann char *s; 1670773673c1SAndre Oppermann 1671302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: trimthenstep6: tcp_close.3: head " 1672302ce8d6SAndre Oppermann "not locked", __func__)); 1673773673c1SAndre Oppermann if ((s = tcp_log_addrs(&tp->t_inpcb->inp_inc, th, NULL, NULL))) { 1674e31d8aa3SMike Silbersack log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data after socket " 1675773673c1SAndre Oppermann "was closed, sending RST and removing tcpcb\n", 1676e31d8aa3SMike Silbersack s, __func__, tcpstates[tp->t_state], tlen); 1677773673c1SAndre Oppermann free(s, M_TCPLOG); 1678773673c1SAndre Oppermann } 1679df8bae1dSRodney W. Grimes tp = tcp_close(tp); 1680603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvafterclose++; 1681a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1682df8bae1dSRodney W. Grimes goto dropwithreset; 1683df8bae1dSRodney W. Grimes } 1684df8bae1dSRodney W. Grimes 1685df8bae1dSRodney W. Grimes /* 1686df8bae1dSRodney W. Grimes * If segment ends after window, drop trailing data 1687df8bae1dSRodney W. Grimes * (and PUSH and FIN); if nothing left, just ACK. 1688df8bae1dSRodney W. Grimes */ 1689fb59c426SYoshinobu Inoue todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd); 1690df8bae1dSRodney W. Grimes if (todrop > 0) { 1691603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpackafterwin++; 1692fb59c426SYoshinobu Inoue if (todrop >= tlen) { 1693603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += tlen; 1694df8bae1dSRodney W. Grimes /* 1695df8bae1dSRodney W. Grimes * If window is closed can only take segments at 1696df8bae1dSRodney W. Grimes * window edge, and have to drop data and PUSH from 1697df8bae1dSRodney W. Grimes * incoming segments. Continue processing, but 1698df8bae1dSRodney W. Grimes * remember to ack. Otherwise, drop segment 1699df8bae1dSRodney W. Grimes * and ack. 1700df8bae1dSRodney W. Grimes */ 1701fb59c426SYoshinobu Inoue if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { 1702df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 1703603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvwinprobe++; 1704df8bae1dSRodney W. Grimes } else 1705df8bae1dSRodney W. Grimes goto dropafterack; 1706df8bae1dSRodney W. Grimes } else 1707603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyteafterwin += todrop; 1708df8bae1dSRodney W. Grimes m_adj(m, -todrop); 1709fb59c426SYoshinobu Inoue tlen -= todrop; 1710fb59c426SYoshinobu Inoue thflags &= ~(TH_PUSH|TH_FIN); 1711df8bae1dSRodney W. Grimes } 1712df8bae1dSRodney W. Grimes 1713df8bae1dSRodney W. Grimes /* 1714df8bae1dSRodney W. Grimes * If last ACK falls within this segment's sequence numbers, 1715df8bae1dSRodney W. Grimes * record its timestamp. 1716cf09195bSPaul Saab * NOTE: 1717cf09195bSPaul Saab * 1) That the test incorporates suggestions from the latest 1718a0292f23SGarrett Wollman * proposal of the tcplw@cray.com list (Braden 1993/04/26). 1719cf09195bSPaul Saab * 2) That updating only on newer timestamps interferes with 1720cf09195bSPaul Saab * our earlier PAWS tests, so this check should be solely 1721cf09195bSPaul Saab * predicated on the sequence space of this segment. 1722cf09195bSPaul Saab * 3) That we modify the segment boundary check to be 1723cf09195bSPaul Saab * Last.ACK.Sent <= SEG.SEQ + SEG.Len 1724cf09195bSPaul Saab * instead of RFC1323's 1725cf09195bSPaul Saab * Last.ACK.Sent < SEG.SEQ + SEG.Len, 1726cf09195bSPaul Saab * This modified check allows us to overcome RFC1323's 1727cf09195bSPaul Saab * limitations as described in Stevens TCP/IP Illustrated 1728cf09195bSPaul Saab * Vol. 2 p.869. In such cases, we can still calculate the 1729cf09195bSPaul Saab * RTT correctly when RCV.NXT == Last.ACK.Sent. 1730df8bae1dSRodney W. Grimes */ 1731be2ac88cSJonathan Lemon if ((to.to_flags & TOF_TS) != 0 && 1732cf09195bSPaul Saab SEQ_LEQ(th->th_seq, tp->last_ack_sent) && 1733cf09195bSPaul Saab SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + 1734cf09195bSPaul Saab ((thflags & (TH_SYN|TH_FIN)) != 0))) { 17359b8b58e0SJonathan Lemon tp->ts_recent_age = ticks; 1736a0292f23SGarrett Wollman tp->ts_recent = to.to_tsval; 1737df8bae1dSRodney W. Grimes } 1738df8bae1dSRodney W. Grimes 1739df8bae1dSRodney W. Grimes /* 1740df8bae1dSRodney W. Grimes * If a SYN is in the window, then this is an 1741df8bae1dSRodney W. Grimes * error and we send an RST and drop the connection. 1742df8bae1dSRodney W. Grimes */ 1743fb59c426SYoshinobu Inoue if (thflags & TH_SYN) { 1744302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: tcp_drop: trimthenstep6: " 1745302ce8d6SAndre Oppermann "head not locked", __func__)); 1746df8bae1dSRodney W. Grimes tp = tcp_drop(tp, ECONNRESET); 1747a57815efSBosko Milekic rstreason = BANDLIM_UNLIMITED; 1748122aad88SAndre Oppermann goto drop; 1749df8bae1dSRodney W. Grimes } 1750df8bae1dSRodney W. Grimes 1751a0292f23SGarrett Wollman /* 1752a0292f23SGarrett Wollman * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN 1753a0292f23SGarrett Wollman * flag is on (half-synchronized state), then queue data for 1754a0292f23SGarrett Wollman * later processing; else drop segment and return. 1755a0292f23SGarrett Wollman */ 1756fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) == 0) { 1757a0292f23SGarrett Wollman if (tp->t_state == TCPS_SYN_RECEIVED || 1758a0292f23SGarrett Wollman (tp->t_flags & TF_NEEDSYN)) 1759a0292f23SGarrett Wollman goto step6; 17605e1aa279SDavid Malone else if (tp->t_flags & TF_ACKNOW) 17615e1aa279SDavid Malone goto dropafterack; 1762a0292f23SGarrett Wollman else 1763a0292f23SGarrett Wollman goto drop; 1764a0292f23SGarrett Wollman } 1765df8bae1dSRodney W. Grimes 1766df8bae1dSRodney W. Grimes /* 1767df8bae1dSRodney W. Grimes * Ack processing. 1768df8bae1dSRodney W. Grimes */ 1769df8bae1dSRodney W. Grimes switch (tp->t_state) { 1770df8bae1dSRodney W. Grimes 1771df8bae1dSRodney W. Grimes /* 1772764d8cefSBill Fenner * In SYN_RECEIVED state, the ack ACKs our SYN, so enter 1773764d8cefSBill Fenner * ESTABLISHED state and continue processing. 1774764d8cefSBill Fenner * The ACK was checked above. 1775df8bae1dSRodney W. Grimes */ 1776df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 1777a0292f23SGarrett Wollman 1778603724d3SBjoern A. Zeeb V_tcpstat.tcps_connects++; 1779df8bae1dSRodney W. Grimes soisconnected(so); 1780df8bae1dSRodney W. Grimes /* Do window scaling? */ 1781df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 1782df8bae1dSRodney W. Grimes (TF_RCVD_SCALE|TF_REQ_SCALE)) { 1783df8bae1dSRodney W. Grimes tp->rcv_scale = tp->request_r_scale; 1784464fcfbcSAndre Oppermann tp->snd_wnd = tiwin; 1785df8bae1dSRodney W. Grimes } 1786a0292f23SGarrett Wollman /* 1787a0292f23SGarrett Wollman * Make transitions: 1788a0292f23SGarrett Wollman * SYN-RECEIVED -> ESTABLISHED 1789a0292f23SGarrett Wollman * SYN-RECEIVED* -> FIN-WAIT-1 1790a0292f23SGarrett Wollman */ 17919b8b58e0SJonathan Lemon tp->t_starttime = ticks; 1792a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) { 1793a0292f23SGarrett Wollman tp->t_state = TCPS_FIN_WAIT_1; 1794a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDFIN; 17957ff19458SPaul Traina } else { 1796a0292f23SGarrett Wollman tp->t_state = TCPS_ESTABLISHED; 1797b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_KEEP, tcp_keepidle); 17987ff19458SPaul Traina } 1799a0292f23SGarrett Wollman /* 1800a0292f23SGarrett Wollman * If segment contains data or ACK, will call tcp_reass() 1801a0292f23SGarrett Wollman * later; if not, do so now to pass queued data to user. 1802a0292f23SGarrett Wollman */ 1803fb59c426SYoshinobu Inoue if (tlen == 0 && (thflags & TH_FIN) == 0) 1804fb59c426SYoshinobu Inoue (void) tcp_reass(tp, (struct tcphdr *)0, 0, 1805a0292f23SGarrett Wollman (struct mbuf *)0); 1806fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq - 1; 180793b0017fSPhilippe Charnier /* FALLTHROUGH */ 1808df8bae1dSRodney W. Grimes 1809df8bae1dSRodney W. Grimes /* 1810df8bae1dSRodney W. Grimes * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 1811df8bae1dSRodney W. Grimes * ACKs. If the ack is in the range 1812fb59c426SYoshinobu Inoue * tp->snd_una < th->th_ack <= tp->snd_max 1813fb59c426SYoshinobu Inoue * then advance tp->snd_una to th->th_ack and drop 1814df8bae1dSRodney W. Grimes * data from the retransmission queue. If this ACK reflects 1815df8bae1dSRodney W. Grimes * more up to date window information we update our window information. 1816df8bae1dSRodney W. Grimes */ 1817df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 1818df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 1819df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 1820df8bae1dSRodney W. Grimes case TCPS_CLOSE_WAIT: 1821df8bae1dSRodney W. Grimes case TCPS_CLOSING: 1822df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 18235a53ca16SPaul Saab if (SEQ_GT(th->th_ack, tp->snd_max)) { 1824603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvacktoomuch++; 18255a53ca16SPaul Saab goto dropafterack; 18265a53ca16SPaul Saab } 18273529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 1828fc30a251SAndre Oppermann ((to.to_flags & TOF_SACK) || 1829fc30a251SAndre Oppermann !TAILQ_EMPTY(&tp->snd_holes))) 18305a53ca16SPaul Saab tcp_sack_doack(tp, &to, th->th_ack); 1831fb59c426SYoshinobu Inoue if (SEQ_LEQ(th->th_ack, tp->snd_una)) { 1832fb59c426SYoshinobu Inoue if (tlen == 0 && tiwin == tp->snd_wnd) { 1833603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvdupack++; 1834df8bae1dSRodney W. Grimes /* 1835df8bae1dSRodney W. Grimes * If we have outstanding data (other than 1836df8bae1dSRodney W. Grimes * a window probe), this is a completely 1837df8bae1dSRodney W. Grimes * duplicate ack (ie, window info didn't 1838df8bae1dSRodney W. Grimes * change), the ack is the biggest we've 1839df8bae1dSRodney W. Grimes * seen and we've seen exactly our rexmt 1840df8bae1dSRodney W. Grimes * threshhold of them, assume a packet 1841df8bae1dSRodney W. Grimes * has been dropped and retransmit it. 1842df8bae1dSRodney W. Grimes * Kludge snd_nxt & the congestion 1843df8bae1dSRodney W. Grimes * window so we send only this one 1844df8bae1dSRodney W. Grimes * packet. 1845df8bae1dSRodney W. Grimes * 1846df8bae1dSRodney W. Grimes * We know we're losing at the current 1847df8bae1dSRodney W. Grimes * window size so do congestion avoidance 1848df8bae1dSRodney W. Grimes * (set ssthresh to half the current window 1849df8bae1dSRodney W. Grimes * and pull our congestion window back to 1850df8bae1dSRodney W. Grimes * the new ssthresh). 1851df8bae1dSRodney W. Grimes * 1852df8bae1dSRodney W. Grimes * Dup acks mean that packets have left the 1853df8bae1dSRodney W. Grimes * network (they're now cached at the receiver) 1854df8bae1dSRodney W. Grimes * so bump cwnd by the amount in the receiver 1855df8bae1dSRodney W. Grimes * to keep a constant cwnd packets in the 1856df8bae1dSRodney W. Grimes * network. 1857f2512ba1SRui Paulo * 1858f2512ba1SRui Paulo * When using TCP ECN, notify the peer that 1859f2512ba1SRui Paulo * we reduced the cwnd. 1860df8bae1dSRodney W. Grimes */ 1861b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) || 1862fb59c426SYoshinobu Inoue th->th_ack != tp->snd_una) 1863df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1864cb942153SJeffrey Hsu else if (++tp->t_dupacks > tcprexmtthresh || 1865603724d3SBjoern A. Zeeb ((V_tcp_do_newreno || 18663529149eSAndre Oppermann (tp->t_flags & TF_SACK_PERMIT)) && 18679d11646dSJeffrey Hsu IN_FASTRECOVERY(tp))) { 18683529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 18693529149eSAndre Oppermann IN_FASTRECOVERY(tp)) { 1870808f11b7SPaul Saab int awnd; 187125e6f9edSPaul Saab 187225e6f9edSPaul Saab /* 187325e6f9edSPaul Saab * Compute the amount of data in flight first. 187425e6f9edSPaul Saab * We can inject new data into the pipe iff 187525e6f9edSPaul Saab * we have less than 1/2 the original window's 187625e6f9edSPaul Saab * worth of data in flight. 187725e6f9edSPaul Saab */ 1878808f11b7SPaul Saab awnd = (tp->snd_nxt - tp->snd_fack) + 18790077b016SPaul Saab tp->sackhint.sack_bytes_rexmit; 1880808f11b7SPaul Saab if (awnd < tp->snd_ssthresh) { 188125e6f9edSPaul Saab tp->snd_cwnd += tp->t_maxseg; 188225e6f9edSPaul Saab if (tp->snd_cwnd > tp->snd_ssthresh) 188325e6f9edSPaul Saab tp->snd_cwnd = tp->snd_ssthresh; 188425e6f9edSPaul Saab } 188525e6f9edSPaul Saab } else 188646f58482SJonathan Lemon tp->snd_cwnd += tp->t_maxseg; 188746f58482SJonathan Lemon (void) tcp_output(tp); 188846f58482SJonathan Lemon goto drop; 1889cb942153SJeffrey Hsu } else if (tp->t_dupacks == tcprexmtthresh) { 1890cb942153SJeffrey Hsu tcp_seq onxt = tp->snd_nxt; 1891a0445c2eSJayanth Vijayaraghavan 1892a0445c2eSJayanth Vijayaraghavan /* 1893a0445c2eSJayanth Vijayaraghavan * If we're doing sack, check to 1894a0445c2eSJayanth Vijayaraghavan * see if we're already in sack 1895a0445c2eSJayanth Vijayaraghavan * recovery. If we're not doing sack, 1896a0445c2eSJayanth Vijayaraghavan * check to see if we're in newreno 1897a0445c2eSJayanth Vijayaraghavan * recovery. 1898a0445c2eSJayanth Vijayaraghavan */ 18993529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 1900a0445c2eSJayanth Vijayaraghavan if (IN_FASTRECOVERY(tp)) { 1901a0445c2eSJayanth Vijayaraghavan tp->t_dupacks = 0; 1902a0445c2eSJayanth Vijayaraghavan break; 1903a0445c2eSJayanth Vijayaraghavan } 1904603724d3SBjoern A. Zeeb } else if (V_tcp_do_newreno || 1905603724d3SBjoern A. Zeeb V_tcp_do_ecn) { 1906a0445c2eSJayanth Vijayaraghavan if (SEQ_LEQ(th->th_ack, 19079d11646dSJeffrey Hsu tp->snd_recover)) { 1908cb942153SJeffrey Hsu tp->t_dupacks = 0; 1909cb942153SJeffrey Hsu break; 191046f58482SJonathan Lemon } 1911a0445c2eSJayanth Vijayaraghavan } 1912f2512ba1SRui Paulo tcp_congestion_exp(tp); 1913b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 19149b8b58e0SJonathan Lemon tp->t_rtttime = 0; 19153529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 1916603724d3SBjoern A. Zeeb V_tcpstat.tcps_sack_recovery_episode++; 1917a55db2b6SPaul Saab tp->sack_newdata = tp->snd_nxt; 19188db456bfSPaul Saab tp->snd_cwnd = tp->t_maxseg; 19196d90faf3SPaul Saab (void) tcp_output(tp); 19206d90faf3SPaul Saab goto drop; 19216d90faf3SPaul Saab } 1922fb59c426SYoshinobu Inoue tp->snd_nxt = th->th_ack; 1923df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->t_maxseg; 1924df8bae1dSRodney W. Grimes (void) tcp_output(tp); 192548d2549cSJeffrey Hsu KASSERT(tp->snd_limited <= 2, 1926302ce8d6SAndre Oppermann ("%s: tp->snd_limited too big", 1927302ce8d6SAndre Oppermann __func__)); 1928df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh + 192948d2549cSJeffrey Hsu tp->t_maxseg * 193048d2549cSJeffrey Hsu (tp->t_dupacks - tp->snd_limited); 1931df8bae1dSRodney W. Grimes if (SEQ_GT(onxt, tp->snd_nxt)) 1932df8bae1dSRodney W. Grimes tp->snd_nxt = onxt; 1933df8bae1dSRodney W. Grimes goto drop; 1934603724d3SBjoern A. Zeeb } else if (V_tcp_do_rfc3042) { 1935582a954bSJeffrey Hsu u_long oldcwnd = tp->snd_cwnd; 193648d2549cSJeffrey Hsu tcp_seq oldsndmax = tp->snd_max; 193748d2549cSJeffrey Hsu u_int sent; 193889c02376SJeffrey Hsu 1939582a954bSJeffrey Hsu KASSERT(tp->t_dupacks == 1 || 1940582a954bSJeffrey Hsu tp->t_dupacks == 2, 1941302ce8d6SAndre Oppermann ("%s: dupacks not 1 or 2", 1942302ce8d6SAndre Oppermann __func__)); 194361a36e3dSJeffrey Hsu if (tp->t_dupacks == 1) 194448d2549cSJeffrey Hsu tp->snd_limited = 0; 194561a36e3dSJeffrey Hsu tp->snd_cwnd = 194661a36e3dSJeffrey Hsu (tp->snd_nxt - tp->snd_una) + 194761a36e3dSJeffrey Hsu (tp->t_dupacks - tp->snd_limited) * 194861a36e3dSJeffrey Hsu tp->t_maxseg; 1949582a954bSJeffrey Hsu (void) tcp_output(tp); 195048d2549cSJeffrey Hsu sent = tp->snd_max - oldsndmax; 195148d2549cSJeffrey Hsu if (sent > tp->t_maxseg) { 195289c02376SJeffrey Hsu KASSERT((tp->t_dupacks == 2 && 195389c02376SJeffrey Hsu tp->snd_limited == 0) || 195489c02376SJeffrey Hsu (sent == tp->t_maxseg + 1 && 195589c02376SJeffrey Hsu tp->t_flags & TF_SENTFIN), 1956302ce8d6SAndre Oppermann ("%s: sent too much", 1957302ce8d6SAndre Oppermann __func__)); 195848d2549cSJeffrey Hsu tp->snd_limited = 2; 195948d2549cSJeffrey Hsu } else if (sent > 0) 196048d2549cSJeffrey Hsu ++tp->snd_limited; 1961582a954bSJeffrey Hsu tp->snd_cwnd = oldcwnd; 1962582a954bSJeffrey Hsu goto drop; 1963df8bae1dSRodney W. Grimes } 1964df8bae1dSRodney W. Grimes } else 1965df8bae1dSRodney W. Grimes tp->t_dupacks = 0; 1966df8bae1dSRodney W. Grimes break; 1967df8bae1dSRodney W. Grimes } 1968cb942153SJeffrey Hsu 1969302ce8d6SAndre Oppermann KASSERT(SEQ_GT(th->th_ack, tp->snd_una), 1970302ce8d6SAndre Oppermann ("%s: th_ack <= snd_una", __func__)); 1971cb942153SJeffrey Hsu 1972df8bae1dSRodney W. Grimes /* 1973df8bae1dSRodney W. Grimes * If the congestion window was inflated to account 1974df8bae1dSRodney W. Grimes * for the other side's cached packets, retract it. 1975df8bae1dSRodney W. Grimes */ 1976603724d3SBjoern A. Zeeb if (V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) { 19779d11646dSJeffrey Hsu if (IN_FASTRECOVERY(tp)) { 1978cb942153SJeffrey Hsu if (SEQ_LT(th->th_ack, tp->snd_recover)) { 19793529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) 19806d90faf3SPaul Saab tcp_sack_partialack(tp, th); 19816d90faf3SPaul Saab else 1982c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th); 1983c068736aSJeffrey Hsu } else { 1984c068736aSJeffrey Hsu /* 19856d90faf3SPaul Saab * Out of fast recovery. 1986c068736aSJeffrey Hsu * Window inflation should have left us 1987c068736aSJeffrey Hsu * with approximately snd_ssthresh 1988c068736aSJeffrey Hsu * outstanding data. 1989c068736aSJeffrey Hsu * But in case we would be inclined to 1990c068736aSJeffrey Hsu * send a burst, better to do it via 1991c068736aSJeffrey Hsu * the slow start mechanism. 1992c068736aSJeffrey Hsu */ 1993c068736aSJeffrey Hsu if (SEQ_GT(th->th_ack + 1994c068736aSJeffrey Hsu tp->snd_ssthresh, 1995c068736aSJeffrey Hsu tp->snd_max)) 1996c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_max - 1997c068736aSJeffrey Hsu th->th_ack + 1998c068736aSJeffrey Hsu tp->t_maxseg; 1999c068736aSJeffrey Hsu else 2000c068736aSJeffrey Hsu tp->snd_cwnd = tp->snd_ssthresh; 2001c068736aSJeffrey Hsu } 2002c068736aSJeffrey Hsu } 2003c068736aSJeffrey Hsu } else { 2004233e8c18SGarrett Wollman if (tp->t_dupacks >= tcprexmtthresh && 2005df8bae1dSRodney W. Grimes tp->snd_cwnd > tp->snd_ssthresh) 2006df8bae1dSRodney W. Grimes tp->snd_cwnd = tp->snd_ssthresh; 200746f58482SJonathan Lemon } 2008cb942153SJeffrey Hsu tp->t_dupacks = 0; 2009a0292f23SGarrett Wollman /* 2010a0292f23SGarrett Wollman * If we reach this point, ACK is not a duplicate, 2011a0292f23SGarrett Wollman * i.e., it ACKs something we sent. 2012a0292f23SGarrett Wollman */ 2013a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) { 2014a0292f23SGarrett Wollman /* 2015a0292f23SGarrett Wollman * T/TCP: Connection was half-synchronized, and our 2016a0292f23SGarrett Wollman * SYN has been ACK'd (so connection is now fully 201707e43e10SAndras Olah * synchronized). Go to non-starred state, 201807e43e10SAndras Olah * increment snd_una for ACK of SYN, and check if 201907e43e10SAndras Olah * we can do window scaling. 2020a0292f23SGarrett Wollman */ 2021a0292f23SGarrett Wollman tp->t_flags &= ~TF_NEEDSYN; 2022a0292f23SGarrett Wollman tp->snd_una++; 202307e43e10SAndras Olah /* Do window scaling? */ 202407e43e10SAndras Olah if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == 202507e43e10SAndras Olah (TF_RCVD_SCALE|TF_REQ_SCALE)) { 202607e43e10SAndras Olah tp->rcv_scale = tp->request_r_scale; 2027464fcfbcSAndre Oppermann /* Send window already scaled. */ 202807e43e10SAndras Olah } 2029a0292f23SGarrett Wollman } 2030a0292f23SGarrett Wollman 2031a0292f23SGarrett Wollman process_ACK: 2032302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: head not locked", 2033302ce8d6SAndre Oppermann __func__)); 20348501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 20357cfc6904SRobert Watson 2036fb59c426SYoshinobu Inoue acked = th->th_ack - tp->snd_una; 2037603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackpack++; 2038603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvackbyte += acked; 2039df8bae1dSRodney W. Grimes 2040df8bae1dSRodney W. Grimes /* 20419b8b58e0SJonathan Lemon * If we just performed our first retransmit, and the ACK 20429b8b58e0SJonathan Lemon * arrives within our recovery window, then it was a mistake 20439b8b58e0SJonathan Lemon * to do the retransmit in the first place. Recover our 20449b8b58e0SJonathan Lemon * original cwnd and ssthresh, and proceed to transmit where 20459b8b58e0SJonathan Lemon * we left off. 20469b8b58e0SJonathan Lemon */ 20479b8b58e0SJonathan Lemon if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) { 2048603724d3SBjoern A. Zeeb ++V_tcpstat.tcps_sndrexmitbad; 20499b8b58e0SJonathan Lemon tp->snd_cwnd = tp->snd_cwnd_prev; 20509b8b58e0SJonathan Lemon tp->snd_ssthresh = tp->snd_ssthresh_prev; 20519d11646dSJeffrey Hsu tp->snd_recover = tp->snd_recover_prev; 20529d11646dSJeffrey Hsu if (tp->t_flags & TF_WASFRECOVERY) 20539d11646dSJeffrey Hsu ENTER_FASTRECOVERY(tp); 20549b8b58e0SJonathan Lemon tp->snd_nxt = tp->snd_max; 20559b8b58e0SJonathan Lemon tp->t_badrxtwin = 0; /* XXX probably not required */ 20569b8b58e0SJonathan Lemon } 20579b8b58e0SJonathan Lemon 20589b8b58e0SJonathan Lemon /* 2059df8bae1dSRodney W. Grimes * If we have a timestamp reply, update smoothed 2060df8bae1dSRodney W. Grimes * round trip time. If no timestamp is present but 2061df8bae1dSRodney W. Grimes * transmit timer is running and timed sequence 2062df8bae1dSRodney W. Grimes * number was acked, update smoothed round trip time. 2063df8bae1dSRodney W. Grimes * Since we now have an rtt measurement, cancel the 2064df8bae1dSRodney W. Grimes * timer backoff (cf., Phil Karn's retransmit alg.). 2065df8bae1dSRodney W. Grimes * Recompute the initial retransmit timer. 2066fa55172bSMatthew Dillon * 2067fa55172bSMatthew Dillon * Some boxes send broken timestamp replies 2068fa55172bSMatthew Dillon * during the SYN+ACK phase, ignore 2069fa55172bSMatthew Dillon * timestamps of 0 or we could calculate a 2070fa55172bSMatthew Dillon * huge RTT and blow up the retransmit timer. 2071df8bae1dSRodney W. Grimes */ 2072fa55172bSMatthew Dillon if ((to.to_flags & TOF_TS) != 0 && 2073fa55172bSMatthew Dillon to.to_tsecr) { 2074eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr) 2075eaf80179SAndre Oppermann tp->t_rttlow = ticks - to.to_tsecr; 20769b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - to.to_tsecr + 1); 2077fa55172bSMatthew Dillon } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) { 2078eaf80179SAndre Oppermann if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime) 2079eaf80179SAndre Oppermann tp->t_rttlow = ticks - tp->t_rtttime; 20809b8b58e0SJonathan Lemon tcp_xmit_timer(tp, ticks - tp->t_rtttime); 2081fa55172bSMatthew Dillon } 20821fcc99b5SMatthew Dillon tcp_xmit_bandwidth_limit(tp, th->th_ack); 2083df8bae1dSRodney W. Grimes 2084df8bae1dSRodney W. Grimes /* 2085df8bae1dSRodney W. Grimes * If all outstanding data is acked, stop retransmit 2086df8bae1dSRodney W. Grimes * timer and remember to restart (more output or persist). 2087df8bae1dSRodney W. Grimes * If there is more data to be acked, restart retransmit 2088df8bae1dSRodney W. Grimes * timer, using current (possibly backed-off) value. 2089df8bae1dSRodney W. Grimes */ 2090fb59c426SYoshinobu Inoue if (th->th_ack == tp->snd_max) { 2091b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 2092df8bae1dSRodney W. Grimes needoutput = 1; 2093b8152ba7SAndre Oppermann } else if (!tcp_timer_active(tp, TT_PERSIST)) 2094b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 2095a0292f23SGarrett Wollman 2096a0292f23SGarrett Wollman /* 2097a0292f23SGarrett Wollman * If no data (only SYN) was ACK'd, 2098a0292f23SGarrett Wollman * skip rest of ACK processing. 2099a0292f23SGarrett Wollman */ 2100a0292f23SGarrett Wollman if (acked == 0) 2101a0292f23SGarrett Wollman goto step6; 2102a0292f23SGarrett Wollman 2103df8bae1dSRodney W. Grimes /* 2104df8bae1dSRodney W. Grimes * When new data is acked, open the congestion window. 2105df8bae1dSRodney W. Grimes * If the window gives us less than ssthresh packets 2106df8bae1dSRodney W. Grimes * in flight, open exponentially (maxseg per packet). 2107df8bae1dSRodney W. Grimes * Otherwise open linearly: maxseg per window 210810be5648SGarrett Wollman * (maxseg^2 / cwnd per packet). 2109c10eb6d1SBjoern A. Zeeb * If cwnd > maxseg^2, fix the cwnd increment at 1 byte 2110c10eb6d1SBjoern A. Zeeb * to avoid capping cwnd (as suggested in RFC2581). 2111df8bae1dSRodney W. Grimes */ 2112603724d3SBjoern A. Zeeb if ((!V_tcp_do_newreno && !(tp->t_flags & TF_SACK_PERMIT)) || 21136d90faf3SPaul Saab !IN_FASTRECOVERY(tp)) { 2114ad3f9ab3SAndre Oppermann u_int cw = tp->snd_cwnd; 2115ad3f9ab3SAndre Oppermann u_int incr = tp->t_maxseg; 2116df8bae1dSRodney W. Grimes if (cw > tp->snd_ssthresh) 2117c10eb6d1SBjoern A. Zeeb incr = max((incr * incr / cw), 1); 2118df8bae1dSRodney W. Grimes tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale); 2119df8bae1dSRodney W. Grimes } 21205905999bSRobert Watson SOCKBUF_LOCK(&so->so_snd); 2121df8bae1dSRodney W. Grimes if (acked > so->so_snd.sb_cc) { 2122df8bae1dSRodney W. Grimes tp->snd_wnd -= so->so_snd.sb_cc; 21235905999bSRobert Watson sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc); 2124df8bae1dSRodney W. Grimes ourfinisacked = 1; 2125df8bae1dSRodney W. Grimes } else { 21265905999bSRobert Watson sbdrop_locked(&so->so_snd, acked); 2127df8bae1dSRodney W. Grimes tp->snd_wnd -= acked; 2128df8bae1dSRodney W. Grimes ourfinisacked = 0; 2129df8bae1dSRodney W. Grimes } 2130564aab1fSAndre Oppermann /* NB: sowwakeup_locked() does an implicit unlock. */ 21311e4d7da7SRobert Watson sowwakeup_locked(so); 2132e8949f74SAndre Oppermann /* Detect una wraparound. */ 2133603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 21346d90faf3SPaul Saab !IN_FASTRECOVERY(tp) && 21359d11646dSJeffrey Hsu SEQ_GT(tp->snd_una, tp->snd_recover) && 21369d11646dSJeffrey Hsu SEQ_LEQ(th->th_ack, tp->snd_recover)) 21379d11646dSJeffrey Hsu tp->snd_recover = th->th_ack - 1; 2138603724d3SBjoern A. Zeeb if ((V_tcp_do_newreno || (tp->t_flags & TF_SACK_PERMIT)) && 21396d90faf3SPaul Saab IN_FASTRECOVERY(tp) && 21409d11646dSJeffrey Hsu SEQ_GEQ(th->th_ack, tp->snd_recover)) 21419d11646dSJeffrey Hsu EXIT_FASTRECOVERY(tp); 2142fb59c426SYoshinobu Inoue tp->snd_una = th->th_ack; 21433529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 21446d90faf3SPaul Saab if (SEQ_GT(tp->snd_una, tp->snd_recover)) 21456d90faf3SPaul Saab tp->snd_recover = tp->snd_una; 21466d90faf3SPaul Saab } 2147df8bae1dSRodney W. Grimes if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 2148df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 2149df8bae1dSRodney W. Grimes 2150df8bae1dSRodney W. Grimes switch (tp->t_state) { 2151df8bae1dSRodney W. Grimes 2152df8bae1dSRodney W. Grimes /* 2153df8bae1dSRodney W. Grimes * In FIN_WAIT_1 STATE in addition to the processing 2154df8bae1dSRodney W. Grimes * for the ESTABLISHED state if our FIN is now acknowledged 2155df8bae1dSRodney W. Grimes * then enter FIN_WAIT_2. 2156df8bae1dSRodney W. Grimes */ 2157df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2158df8bae1dSRodney W. Grimes if (ourfinisacked) { 2159df8bae1dSRodney W. Grimes /* 2160df8bae1dSRodney W. Grimes * If we can't receive any more 2161df8bae1dSRodney W. Grimes * data, then closing user can proceed. 2162df8bae1dSRodney W. Grimes * Starting the timer is contrary to the 2163df8bae1dSRodney W. Grimes * specification, but if we don't get a FIN 2164df8bae1dSRodney W. Grimes * we'll hang forever. 2165e8949f74SAndre Oppermann * 2166e8949f74SAndre Oppermann * XXXjl: 2167340c35deSJonathan Lemon * we should release the tp also, and use a 2168340c35deSJonathan Lemon * compressed state. 2169340c35deSJonathan Lemon */ 2170c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 21717c72af87SMohan Srinivasan int timeout; 21727c72af87SMohan Srinivasan 217303e49181SSeigo Tanimura soisdisconnected(so); 21747c72af87SMohan Srinivasan timeout = (tcp_fast_finwait2_recycle) ? 21757c72af87SMohan Srinivasan tcp_finwait2_timeout : tcp_maxidle; 2176b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_2MSL, timeout); 21774cc20ab1SSeigo Tanimura } 2178df8bae1dSRodney W. Grimes tp->t_state = TCPS_FIN_WAIT_2; 2179df8bae1dSRodney W. Grimes } 2180df8bae1dSRodney W. Grimes break; 2181df8bae1dSRodney W. Grimes 2182df8bae1dSRodney W. Grimes /* 2183df8bae1dSRodney W. Grimes * In CLOSING STATE in addition to the processing for 2184df8bae1dSRodney W. Grimes * the ESTABLISHED state if the ACK acknowledges our FIN 2185df8bae1dSRodney W. Grimes * then enter the TIME-WAIT state, otherwise ignore 2186df8bae1dSRodney W. Grimes * the segment. 2187df8bae1dSRodney W. Grimes */ 2188df8bae1dSRodney W. Grimes case TCPS_CLOSING: 2189df8bae1dSRodney W. Grimes if (ourfinisacked) { 2190302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2191302ce8d6SAndre Oppermann "head not locked", __func__)); 219211a20fb8SJeffrey Hsu tcp_twstart(tp); 2193603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2194302ce8d6SAndre Oppermann headlocked = 0; 2195340c35deSJonathan Lemon m_freem(m); 2196679d9708SAndre Oppermann return; 2197df8bae1dSRodney W. Grimes } 2198df8bae1dSRodney W. Grimes break; 2199df8bae1dSRodney W. Grimes 2200df8bae1dSRodney W. Grimes /* 2201df8bae1dSRodney W. Grimes * In LAST_ACK, we may still be waiting for data to drain 2202df8bae1dSRodney W. Grimes * and/or to be acked, as well as for the ack of our FIN. 2203df8bae1dSRodney W. Grimes * If our FIN is now acknowledged, delete the TCB, 2204df8bae1dSRodney W. Grimes * enter the closed state and return. 2205df8bae1dSRodney W. Grimes */ 2206df8bae1dSRodney W. Grimes case TCPS_LAST_ACK: 2207df8bae1dSRodney W. Grimes if (ourfinisacked) { 2208302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: process_ACK: " 2209302ce8d6SAndre Oppermann "tcp_close: head not locked", __func__)); 2210df8bae1dSRodney W. Grimes tp = tcp_close(tp); 2211df8bae1dSRodney W. Grimes goto drop; 2212df8bae1dSRodney W. Grimes } 2213df8bae1dSRodney W. Grimes break; 2214df8bae1dSRodney W. Grimes } 2215df8bae1dSRodney W. Grimes } 2216df8bae1dSRodney W. Grimes 2217df8bae1dSRodney W. Grimes step6: 2218302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: step6: head not locked", __func__)); 22198501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 22207cfc6904SRobert Watson 2221df8bae1dSRodney W. Grimes /* 2222df8bae1dSRodney W. Grimes * Update window information. 2223df8bae1dSRodney W. Grimes * Don't look at window if no ACK: TAC's send garbage on first SYN. 2224df8bae1dSRodney W. Grimes */ 2225fb59c426SYoshinobu Inoue if ((thflags & TH_ACK) && 2226fb59c426SYoshinobu Inoue (SEQ_LT(tp->snd_wl1, th->th_seq) || 2227fb59c426SYoshinobu Inoue (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || 2228fb59c426SYoshinobu Inoue (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { 2229df8bae1dSRodney W. Grimes /* keep track of pure window updates */ 2230fb59c426SYoshinobu Inoue if (tlen == 0 && 2231fb59c426SYoshinobu Inoue tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) 2232603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvwinupd++; 2233df8bae1dSRodney W. Grimes tp->snd_wnd = tiwin; 2234fb59c426SYoshinobu Inoue tp->snd_wl1 = th->th_seq; 2235fb59c426SYoshinobu Inoue tp->snd_wl2 = th->th_ack; 2236df8bae1dSRodney W. Grimes if (tp->snd_wnd > tp->max_sndwnd) 2237df8bae1dSRodney W. Grimes tp->max_sndwnd = tp->snd_wnd; 2238df8bae1dSRodney W. Grimes needoutput = 1; 2239df8bae1dSRodney W. Grimes } 2240df8bae1dSRodney W. Grimes 2241df8bae1dSRodney W. Grimes /* 2242df8bae1dSRodney W. Grimes * Process segments with URG. 2243df8bae1dSRodney W. Grimes */ 2244fb59c426SYoshinobu Inoue if ((thflags & TH_URG) && th->th_urp && 2245df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2246df8bae1dSRodney W. Grimes /* 2247df8bae1dSRodney W. Grimes * This is a kludge, but if we receive and accept 2248df8bae1dSRodney W. Grimes * random urgent pointers, we'll crash in 2249df8bae1dSRodney W. Grimes * soreceive. It's hard to imagine someone 2250df8bae1dSRodney W. Grimes * actually wanting to send this much urgent data. 2251df8bae1dSRodney W. Grimes */ 225218ad5842SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2253fb59c426SYoshinobu Inoue if (th->th_urp + so->so_rcv.sb_cc > sb_max) { 2254fb59c426SYoshinobu Inoue th->th_urp = 0; /* XXX */ 2255fb59c426SYoshinobu Inoue thflags &= ~TH_URG; /* XXX */ 225618ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); /* XXX */ 2257df8bae1dSRodney W. Grimes goto dodata; /* XXX */ 2258df8bae1dSRodney W. Grimes } 2259df8bae1dSRodney W. Grimes /* 2260df8bae1dSRodney W. Grimes * If this segment advances the known urgent pointer, 2261df8bae1dSRodney W. Grimes * then mark the data stream. This should not happen 2262df8bae1dSRodney W. Grimes * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 2263df8bae1dSRodney W. Grimes * a FIN has been received from the remote side. 2264df8bae1dSRodney W. Grimes * In these states we ignore the URG. 2265df8bae1dSRodney W. Grimes * 2266df8bae1dSRodney W. Grimes * According to RFC961 (Assigned Protocols), 2267df8bae1dSRodney W. Grimes * the urgent pointer points to the last octet 2268df8bae1dSRodney W. Grimes * of urgent data. We continue, however, 2269df8bae1dSRodney W. Grimes * to consider it to indicate the first octet 2270df8bae1dSRodney W. Grimes * of data past the urgent section as the original 2271df8bae1dSRodney W. Grimes * spec states (in one of two places). 2272df8bae1dSRodney W. Grimes */ 2273fb59c426SYoshinobu Inoue if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { 2274fb59c426SYoshinobu Inoue tp->rcv_up = th->th_seq + th->th_urp; 2275df8bae1dSRodney W. Grimes so->so_oobmark = so->so_rcv.sb_cc + 2276df8bae1dSRodney W. Grimes (tp->rcv_up - tp->rcv_nxt) - 1; 2277927c5ceaSRobert Watson if (so->so_oobmark == 0) 2278c0b99ffaSRobert Watson so->so_rcv.sb_state |= SBS_RCVATMARK; 2279df8bae1dSRodney W. Grimes sohasoutofband(so); 2280df8bae1dSRodney W. Grimes tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 2281df8bae1dSRodney W. Grimes } 228218ad5842SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 2283df8bae1dSRodney W. Grimes /* 2284df8bae1dSRodney W. Grimes * Remove out of band data so doesn't get presented to user. 2285df8bae1dSRodney W. Grimes * This can happen independent of advancing the URG pointer, 2286df8bae1dSRodney W. Grimes * but if two URG's are pending at once, some out-of-band 2287df8bae1dSRodney W. Grimes * data may creep in... ick. 2288df8bae1dSRodney W. Grimes */ 228930613f56SJeffrey Hsu if (th->th_urp <= (u_long)tlen && 229030613f56SJeffrey Hsu !(so->so_options & SO_OOBINLINE)) { 229130613f56SJeffrey Hsu /* hdr drop is delayed */ 229230613f56SJeffrey Hsu tcp_pulloutofband(so, th, m, drop_hdrlen); 229330613f56SJeffrey Hsu } 2294c068736aSJeffrey Hsu } else { 2295df8bae1dSRodney W. Grimes /* 2296df8bae1dSRodney W. Grimes * If no out of band data is expected, 2297df8bae1dSRodney W. Grimes * pull receive urgent pointer along 2298df8bae1dSRodney W. Grimes * with the receive window. 2299df8bae1dSRodney W. Grimes */ 2300df8bae1dSRodney W. Grimes if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 2301df8bae1dSRodney W. Grimes tp->rcv_up = tp->rcv_nxt; 2302c068736aSJeffrey Hsu } 2303df8bae1dSRodney W. Grimes dodata: /* XXX */ 2304302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dodata: head not locked", __func__)); 23058501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 23067cfc6904SRobert Watson 2307df8bae1dSRodney W. Grimes /* 2308df8bae1dSRodney W. Grimes * Process the segment text, merging it into the TCP sequencing queue, 2309df8bae1dSRodney W. Grimes * and arranging for acknowledgment of receipt if necessary. 2310df8bae1dSRodney W. Grimes * This process logically involves adjusting tp->rcv_wnd as data 2311df8bae1dSRodney W. Grimes * is presented to the user (this happens in tcp_usrreq.c, 2312df8bae1dSRodney W. Grimes * case PRU_RCVD). If a FIN has already been received on this 2313df8bae1dSRodney W. Grimes * connection then we just ignore the text. 2314df8bae1dSRodney W. Grimes */ 2315fb59c426SYoshinobu Inoue if ((tlen || (thflags & TH_FIN)) && 2316df8bae1dSRodney W. Grimes TCPS_HAVERCVDFIN(tp->t_state) == 0) { 231769e03620SPaul Saab tcp_seq save_start = th->th_seq; 2318fb59c426SYoshinobu Inoue m_adj(m, drop_hdrlen); /* delayed header drop */ 2319e4b64281SJesper Skriver /* 2320c068736aSJeffrey Hsu * Insert segment which includes th into TCP reassembly queue 2321c068736aSJeffrey Hsu * with control block tp. Set thflags to whether reassembly now 2322c068736aSJeffrey Hsu * includes a segment with FIN. This handles the common case 2323c068736aSJeffrey Hsu * inline (segment is the next to be received on an established 2324c068736aSJeffrey Hsu * connection, and the queue is empty), avoiding linkage into 2325c068736aSJeffrey Hsu * and removal from the queue and repetition of various 2326c068736aSJeffrey Hsu * conversions. 2327c068736aSJeffrey Hsu * Set DELACK for segments received in order, but ack 2328c068736aSJeffrey Hsu * immediately when segments are out of order (so 2329c068736aSJeffrey Hsu * fast retransmit can work). 2330e4b64281SJesper Skriver */ 2331e4b64281SJesper Skriver if (th->th_seq == tp->rcv_nxt && 2332e4b64281SJesper Skriver LIST_EMPTY(&tp->t_segq) && 2333e4b64281SJesper Skriver TCPS_HAVEESTABLISHED(tp->t_state)) { 2334e4b64281SJesper Skriver if (DELAY_ACK(tp)) 23353bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2336e4b64281SJesper Skriver else 2337e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2338e4b64281SJesper Skriver tp->rcv_nxt += tlen; 2339e4b64281SJesper Skriver thflags = th->th_flags & TH_FIN; 2340603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvpack++; 2341603724d3SBjoern A. Zeeb V_tcpstat.tcps_rcvbyte += tlen; 2342e4b64281SJesper Skriver ND6_HINT(tp); 23431e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 2344c0b99ffaSRobert Watson if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 2345c1c36a2cSMike Silbersack m_freem(m); 2346c1c36a2cSMike Silbersack else 23471e4d7da7SRobert Watson sbappendstream_locked(&so->so_rcv, m); 2348e8949f74SAndre Oppermann /* NB: sorwakeup_locked() does an implicit unlock. */ 23491e4d7da7SRobert Watson sorwakeup_locked(so); 2350e4b64281SJesper Skriver } else { 2351e8949f74SAndre Oppermann /* 2352e8949f74SAndre Oppermann * XXX: Due to the header drop above "th" is 2353e8949f74SAndre Oppermann * theoretically invalid by now. Fortunately 2354e8949f74SAndre Oppermann * m_adj() doesn't actually frees any mbufs 2355e8949f74SAndre Oppermann * when trimming from the head. 2356e8949f74SAndre Oppermann */ 2357e4b64281SJesper Skriver thflags = tcp_reass(tp, th, &tlen, m); 2358e4b64281SJesper Skriver tp->t_flags |= TF_ACKNOW; 2359e4b64281SJesper Skriver } 23603529149eSAndre Oppermann if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT)) 2361f194524fSAndre Oppermann tcp_update_sack_list(tp, save_start, save_start + tlen); 2362302ce8d6SAndre Oppermann #if 0 2363df8bae1dSRodney W. Grimes /* 2364df8bae1dSRodney W. Grimes * Note the amount of data that peer has sent into 2365df8bae1dSRodney W. Grimes * our window, in order to estimate the sender's 2366df8bae1dSRodney W. Grimes * buffer size. 2367302ce8d6SAndre Oppermann * XXX: Unused. 2368df8bae1dSRodney W. Grimes */ 2369df8bae1dSRodney W. Grimes len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 2370302ce8d6SAndre Oppermann #endif 2371df8bae1dSRodney W. Grimes } else { 2372df8bae1dSRodney W. Grimes m_freem(m); 2373fb59c426SYoshinobu Inoue thflags &= ~TH_FIN; 2374df8bae1dSRodney W. Grimes } 2375df8bae1dSRodney W. Grimes 2376df8bae1dSRodney W. Grimes /* 2377df8bae1dSRodney W. Grimes * If FIN is received ACK the FIN and let the user know 2378df8bae1dSRodney W. Grimes * that the connection is closing. 2379df8bae1dSRodney W. Grimes */ 2380fb59c426SYoshinobu Inoue if (thflags & TH_FIN) { 2381df8bae1dSRodney W. Grimes if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 2382df8bae1dSRodney W. Grimes socantrcvmore(so); 2383a0292f23SGarrett Wollman /* 2384a0292f23SGarrett Wollman * If connection is half-synchronized 2385d3eede9dSAndras Olah * (ie NEEDSYN flag on) then delay ACK, 2386a0292f23SGarrett Wollman * so it may be piggybacked when SYN is sent. 2387a0292f23SGarrett Wollman * Otherwise, since we received a FIN then no 2388a0292f23SGarrett Wollman * more input can be expected, send ACK now. 2389a0292f23SGarrett Wollman */ 23903bfd6421SJonathan Lemon if (tp->t_flags & TF_NEEDSYN) 23913bfd6421SJonathan Lemon tp->t_flags |= TF_DELACK; 2392a0292f23SGarrett Wollman else 2393df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2394df8bae1dSRodney W. Grimes tp->rcv_nxt++; 2395df8bae1dSRodney W. Grimes } 2396df8bae1dSRodney W. Grimes switch (tp->t_state) { 2397df8bae1dSRodney W. Grimes 2398df8bae1dSRodney W. Grimes /* 2399df8bae1dSRodney W. Grimes * In SYN_RECEIVED and ESTABLISHED STATES 2400df8bae1dSRodney W. Grimes * enter the CLOSE_WAIT state. 2401df8bae1dSRodney W. Grimes */ 2402df8bae1dSRodney W. Grimes case TCPS_SYN_RECEIVED: 24039b8b58e0SJonathan Lemon tp->t_starttime = ticks; 24049b8b58e0SJonathan Lemon /* FALLTHROUGH */ 2405df8bae1dSRodney W. Grimes case TCPS_ESTABLISHED: 2406df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSE_WAIT; 2407df8bae1dSRodney W. Grimes break; 2408df8bae1dSRodney W. Grimes 2409df8bae1dSRodney W. Grimes /* 2410df8bae1dSRodney W. Grimes * If still in FIN_WAIT_1 STATE FIN has not been acked so 2411df8bae1dSRodney W. Grimes * enter the CLOSING state. 2412df8bae1dSRodney W. Grimes */ 2413df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_1: 2414df8bae1dSRodney W. Grimes tp->t_state = TCPS_CLOSING; 2415df8bae1dSRodney W. Grimes break; 2416df8bae1dSRodney W. Grimes 2417df8bae1dSRodney W. Grimes /* 2418df8bae1dSRodney W. Grimes * In FIN_WAIT_2 state enter the TIME_WAIT state, 2419df8bae1dSRodney W. Grimes * starting the time-wait timer, turning off the other 2420df8bae1dSRodney W. Grimes * standard timers. 2421df8bae1dSRodney W. Grimes */ 2422df8bae1dSRodney W. Grimes case TCPS_FIN_WAIT_2: 2423302ce8d6SAndre Oppermann KASSERT(headlocked == 1, ("%s: dodata: " 2424302ce8d6SAndre Oppermann "TCP_FIN_WAIT_2: head not locked", __func__)); 2425340c35deSJonathan Lemon tcp_twstart(tp); 2426603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2427679d9708SAndre Oppermann return; 2428df8bae1dSRodney W. Grimes } 2429df8bae1dSRodney W. Grimes } 2430603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2431e0bef1cbSRobert Watson headlocked = 0; 2432610ee2f9SDavid Greenman #ifdef TCPDEBUG 24334cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2434fb59c426SYoshinobu Inoue tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, 2435fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2436610ee2f9SDavid Greenman #endif 2437df8bae1dSRodney W. Grimes 2438df8bae1dSRodney W. Grimes /* 2439df8bae1dSRodney W. Grimes * Return any desired output. 2440df8bae1dSRodney W. Grimes */ 2441df8bae1dSRodney W. Grimes if (needoutput || (tp->t_flags & TF_ACKNOW)) 2442df8bae1dSRodney W. Grimes (void) tcp_output(tp); 24437792ea27SJeffrey Hsu 2444a14c749fSJonathan Lemon check_delack: 2445302ce8d6SAndre Oppermann KASSERT(headlocked == 0, ("%s: check_delack: head locked", 2446302ce8d6SAndre Oppermann __func__)); 2447603724d3SBjoern A. Zeeb INP_INFO_UNLOCK_ASSERT(&V_tcbinfo); 24488501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2449a14c749fSJonathan Lemon if (tp->t_flags & TF_DELACK) { 24503bfd6421SJonathan Lemon tp->t_flags &= ~TF_DELACK; 2451b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, tcp_delacktime); 24523bfd6421SJonathan Lemon } 24538501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2454679d9708SAndre Oppermann return; 2455df8bae1dSRodney W. Grimes 2456df8bae1dSRodney W. Grimes dropafterack: 2457302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropafterack: head not locked", __func__)); 2458df8bae1dSRodney W. Grimes /* 2459df8bae1dSRodney W. Grimes * Generate an ACK dropping incoming segment if it occupies 2460df8bae1dSRodney W. Grimes * sequence space, where the ACK reflects our state. 246180ab7c0eSGarrett Wollman * 246280ab7c0eSGarrett Wollman * We can now skip the test for the RST flag since all 246380ab7c0eSGarrett Wollman * paths to this code happen after packets containing 246480ab7c0eSGarrett Wollman * RST have been dropped. 246580ab7c0eSGarrett Wollman * 246680ab7c0eSGarrett Wollman * In the SYN-RECEIVED state, don't send an ACK unless the 246780ab7c0eSGarrett Wollman * segment we received passes the SYN-RECEIVED ACK test. 246880ab7c0eSGarrett Wollman * If it fails send a RST. This breaks the loop in the 246980ab7c0eSGarrett Wollman * "LAND" DoS attack, and also prevents an ACK storm 247080ab7c0eSGarrett Wollman * between two listening ports that have been sent forged 247180ab7c0eSGarrett Wollman * SYN segments, each with the source address of the other. 2472df8bae1dSRodney W. Grimes */ 2473fb59c426SYoshinobu Inoue if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && 2474fb59c426SYoshinobu Inoue (SEQ_GT(tp->snd_una, th->th_ack) || 2475a57815efSBosko Milekic SEQ_GT(th->th_ack, tp->snd_max)) ) { 2476a57815efSBosko Milekic rstreason = BANDLIM_RST_OPENPORT; 2477a57815efSBosko Milekic goto dropwithreset; 2478a57815efSBosko Milekic } 2479a0292f23SGarrett Wollman #ifdef TCPDEBUG 24804cc20ab1SSeigo Tanimura if (so->so_options & SO_DEBUG) 2481fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2482fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2483a0292f23SGarrett Wollman #endif 2484302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: headlocked should be 1", __func__)); 2485603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2486df8bae1dSRodney W. Grimes tp->t_flags |= TF_ACKNOW; 2487df8bae1dSRodney W. Grimes (void) tcp_output(tp); 24888501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2489d6915262SRobert Watson m_freem(m); 2490679d9708SAndre Oppermann return; 2491df8bae1dSRodney W. Grimes 2492df8bae1dSRodney W. Grimes dropwithreset: 2493302ce8d6SAndre Oppermann KASSERT(headlocked, ("%s: dropwithreset: head not locked", __func__)); 2494dd8ac7f9SRobert Watson INP_INFO_WUNLOCK(&V_tcbinfo); 2495a57815efSBosko Milekic 2496a0ca0871SRobert Watson if (tp != NULL) { 2497302ce8d6SAndre Oppermann tcp_dropwithreset(m, th, tp, tlen, rstreason); 24988501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2499dd8ac7f9SRobert Watson } else 2500a0ca0871SRobert Watson tcp_dropwithreset(m, th, NULL, tlen, rstreason); 2501679d9708SAndre Oppermann return; 2502df8bae1dSRodney W. Grimes 2503df8bae1dSRodney W. Grimes drop: 2504df8bae1dSRodney W. Grimes /* 2505df8bae1dSRodney W. Grimes * Drop space held by incoming segment and return. 2506df8bae1dSRodney W. Grimes */ 2507610ee2f9SDavid Greenman #ifdef TCPDEBUG 25081c53f806SRobert Watson if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 2509fb59c426SYoshinobu Inoue tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, 2510fb59c426SYoshinobu Inoue &tcp_savetcp, 0); 2511a0292f23SGarrett Wollman #endif 25121c53f806SRobert Watson if (tp != NULL) 25138501a69cSRobert Watson INP_WUNLOCK(tp->t_inpcb); 2514f76fcf6dSJeffrey Hsu if (headlocked) 2515603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_tcbinfo); 2516d6915262SRobert Watson m_freem(m); 2517302ce8d6SAndre Oppermann } 2518302ce8d6SAndre Oppermann 2519302ce8d6SAndre Oppermann /* 25200ca3f933SAndre Oppermann * Issue RST and make ACK acceptable to originator of segment. 25210ca3f933SAndre Oppermann * The mbuf must still include the original packet header. 25220ca3f933SAndre Oppermann * tp may be NULL. 2523302ce8d6SAndre Oppermann */ 2524302ce8d6SAndre Oppermann static void 2525302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp, 2526302ce8d6SAndre Oppermann int tlen, int rstreason) 2527302ce8d6SAndre Oppermann { 2528302ce8d6SAndre Oppermann struct ip *ip; 2529302ce8d6SAndre Oppermann #ifdef INET6 2530302ce8d6SAndre Oppermann struct ip6_hdr *ip6; 2531302ce8d6SAndre Oppermann #endif 25327a3244ccSRobert Watson 25337a3244ccSRobert Watson if (tp != NULL) { 25348501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 25357a3244ccSRobert Watson } 25367a3244ccSRobert Watson 25370ca3f933SAndre Oppermann /* Don't bother if destination was broadcast/multicast. */ 2538302ce8d6SAndre Oppermann if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) 2539302ce8d6SAndre Oppermann goto drop; 2540302ce8d6SAndre Oppermann #ifdef INET6 2541302ce8d6SAndre Oppermann if (mtod(m, struct ip *)->ip_v == 6) { 2542302ce8d6SAndre Oppermann ip6 = mtod(m, struct ip6_hdr *); 2543302ce8d6SAndre Oppermann if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || 2544302ce8d6SAndre Oppermann IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) 2545302ce8d6SAndre Oppermann goto drop; 2546302ce8d6SAndre Oppermann /* IPv6 anycast check is done at tcp6_input() */ 2547302ce8d6SAndre Oppermann } else 2548302ce8d6SAndre Oppermann #endif 2549302ce8d6SAndre Oppermann { 2550302ce8d6SAndre Oppermann ip = mtod(m, struct ip *); 2551302ce8d6SAndre Oppermann if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 2552302ce8d6SAndre Oppermann IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || 2553302ce8d6SAndre Oppermann ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || 2554302ce8d6SAndre Oppermann in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) 2555302ce8d6SAndre Oppermann goto drop; 2556302ce8d6SAndre Oppermann } 2557302ce8d6SAndre Oppermann 2558302ce8d6SAndre Oppermann /* Perform bandwidth limiting. */ 2559302ce8d6SAndre Oppermann if (badport_bandlim(rstreason) < 0) 2560302ce8d6SAndre Oppermann goto drop; 2561302ce8d6SAndre Oppermann 2562302ce8d6SAndre Oppermann /* tcp_respond consumes the mbuf chain. */ 2563302ce8d6SAndre Oppermann if (th->th_flags & TH_ACK) { 2564302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, 2565302ce8d6SAndre Oppermann th->th_ack, TH_RST); 2566302ce8d6SAndre Oppermann } else { 2567302ce8d6SAndre Oppermann if (th->th_flags & TH_SYN) 2568302ce8d6SAndre Oppermann tlen++; 2569302ce8d6SAndre Oppermann tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, 2570302ce8d6SAndre Oppermann (tcp_seq)0, TH_RST|TH_ACK); 2571302ce8d6SAndre Oppermann } 2572302ce8d6SAndre Oppermann return; 2573302ce8d6SAndre Oppermann drop: 2574302ce8d6SAndre Oppermann m_freem(m); 2575df8bae1dSRodney W. Grimes } 2576df8bae1dSRodney W. Grimes 2577be2ac88cSJonathan Lemon /* 2578be2ac88cSJonathan Lemon * Parse TCP options and place in tcpopt. 2579be2ac88cSJonathan Lemon */ 25800312fbe9SPoul-Henning Kamp static void 2581ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags) 2582df8bae1dSRodney W. Grimes { 25838b615593SMarko Zec INIT_VNET_INET(curvnet); 2584df8bae1dSRodney W. Grimes int opt, optlen; 2585df8bae1dSRodney W. Grimes 2586be2ac88cSJonathan Lemon to->to_flags = 0; 2587df8bae1dSRodney W. Grimes for (; cnt > 0; cnt -= optlen, cp += optlen) { 2588df8bae1dSRodney W. Grimes opt = cp[0]; 2589df8bae1dSRodney W. Grimes if (opt == TCPOPT_EOL) 2590df8bae1dSRodney W. Grimes break; 2591df8bae1dSRodney W. Grimes if (opt == TCPOPT_NOP) 2592df8bae1dSRodney W. Grimes optlen = 1; 2593df8bae1dSRodney W. Grimes else { 2594b474779fSJun-ichiro itojun Hagino if (cnt < 2) 2595b474779fSJun-ichiro itojun Hagino break; 2596df8bae1dSRodney W. Grimes optlen = cp[1]; 2597b474779fSJun-ichiro itojun Hagino if (optlen < 2 || optlen > cnt) 2598df8bae1dSRodney W. Grimes break; 2599df8bae1dSRodney W. Grimes } 2600df8bae1dSRodney W. Grimes switch (opt) { 2601df8bae1dSRodney W. Grimes case TCPOPT_MAXSEG: 2602df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_MAXSEG) 2603df8bae1dSRodney W. Grimes continue; 2604f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2605df8bae1dSRodney W. Grimes continue; 2606be2ac88cSJonathan Lemon to->to_flags |= TOF_MSS; 2607be2ac88cSJonathan Lemon bcopy((char *)cp + 2, 2608be2ac88cSJonathan Lemon (char *)&to->to_mss, sizeof(to->to_mss)); 2609fd8e4ebcSMike Barcroft to->to_mss = ntohs(to->to_mss); 2610df8bae1dSRodney W. Grimes break; 2611df8bae1dSRodney W. Grimes case TCPOPT_WINDOW: 2612df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_WINDOW) 2613df8bae1dSRodney W. Grimes continue; 2614f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2615df8bae1dSRodney W. Grimes continue; 2616be2ac88cSJonathan Lemon to->to_flags |= TOF_SCALE; 261702a1a643SAndre Oppermann to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT); 2618df8bae1dSRodney W. Grimes break; 2619df8bae1dSRodney W. Grimes case TCPOPT_TIMESTAMP: 2620df8bae1dSRodney W. Grimes if (optlen != TCPOLEN_TIMESTAMP) 2621df8bae1dSRodney W. Grimes continue; 2622be2ac88cSJonathan Lemon to->to_flags |= TOF_TS; 2623a0292f23SGarrett Wollman bcopy((char *)cp + 2, 2624a0292f23SGarrett Wollman (char *)&to->to_tsval, sizeof(to->to_tsval)); 2625fd8e4ebcSMike Barcroft to->to_tsval = ntohl(to->to_tsval); 2626a0292f23SGarrett Wollman bcopy((char *)cp + 6, 2627a0292f23SGarrett Wollman (char *)&to->to_tsecr, sizeof(to->to_tsecr)); 2628fd8e4ebcSMike Barcroft to->to_tsecr = ntohl(to->to_tsecr); 2629df8bae1dSRodney W. Grimes break; 26301cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 26311cfd4b53SBruce M Simpson /* 26321cfd4b53SBruce M Simpson * XXX In order to reply to a host which has set the 26331cfd4b53SBruce M Simpson * TCP_SIGNATURE option in its initial SYN, we have to 26341cfd4b53SBruce M Simpson * record the fact that the option was observed here 26351cfd4b53SBruce M Simpson * for the syncache code to perform the correct response. 26361cfd4b53SBruce M Simpson */ 26371cfd4b53SBruce M Simpson case TCPOPT_SIGNATURE: 26381cfd4b53SBruce M Simpson if (optlen != TCPOLEN_SIGNATURE) 26391cfd4b53SBruce M Simpson continue; 2640df47e437SAndre Oppermann to->to_flags |= TOF_SIGNATURE; 2641df47e437SAndre Oppermann to->to_signature = cp + 2; 26421cfd4b53SBruce M Simpson break; 2643265ed012SBruce M Simpson #endif 26446d90faf3SPaul Saab case TCPOPT_SACK_PERMITTED: 2645f72167f4SAndre Oppermann if (optlen != TCPOLEN_SACK_PERMITTED) 26466d90faf3SPaul Saab continue; 2647f72167f4SAndre Oppermann if (!(flags & TO_SYN)) 2648f72167f4SAndre Oppermann continue; 2649603724d3SBjoern A. Zeeb if (!V_tcp_do_sack) 2650f72167f4SAndre Oppermann continue; 2651fc30a251SAndre Oppermann to->to_flags |= TOF_SACKPERM; 26526d90faf3SPaul Saab break; 26536d90faf3SPaul Saab case TCPOPT_SACK: 26545a53ca16SPaul Saab if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) 26556d90faf3SPaul Saab continue; 2656b728e902SAndre Oppermann if (flags & TO_SYN) 2657b728e902SAndre Oppermann continue; 2658fc30a251SAndre Oppermann to->to_flags |= TOF_SACK; 26595a53ca16SPaul Saab to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; 26605a53ca16SPaul Saab to->to_sacks = cp + 2; 2661603724d3SBjoern A. Zeeb V_tcpstat.tcps_sack_rcv_blocks++; 26626d90faf3SPaul Saab break; 2663be2ac88cSJonathan Lemon default: 2664be2ac88cSJonathan Lemon continue; 2665df8bae1dSRodney W. Grimes } 2666df8bae1dSRodney W. Grimes } 2667df8bae1dSRodney W. Grimes } 2668df8bae1dSRodney W. Grimes 2669df8bae1dSRodney W. Grimes /* 2670df8bae1dSRodney W. Grimes * Pull out of band byte out of a segment so 2671df8bae1dSRodney W. Grimes * it doesn't appear in the user's data queue. 2672df8bae1dSRodney W. Grimes * It is still reflected in the segment length for 2673df8bae1dSRodney W. Grimes * sequencing purposes. 2674df8bae1dSRodney W. Grimes */ 26750312fbe9SPoul-Henning Kamp static void 2676ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, 2677ad3f9ab3SAndre Oppermann int off) 2678df8bae1dSRodney W. Grimes { 2679fb59c426SYoshinobu Inoue int cnt = off + th->th_urp - 1; 2680df8bae1dSRodney W. Grimes 2681df8bae1dSRodney W. Grimes while (cnt >= 0) { 2682df8bae1dSRodney W. Grimes if (m->m_len > cnt) { 2683df8bae1dSRodney W. Grimes char *cp = mtod(m, caddr_t) + cnt; 2684df8bae1dSRodney W. Grimes struct tcpcb *tp = sototcpcb(so); 2685df8bae1dSRodney W. Grimes 26868501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 26877a3244ccSRobert Watson 2688df8bae1dSRodney W. Grimes tp->t_iobc = *cp; 2689df8bae1dSRodney W. Grimes tp->t_oobflags |= TCPOOB_HAVEDATA; 2690df8bae1dSRodney W. Grimes bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 2691df8bae1dSRodney W. Grimes m->m_len--; 269269a34685SYoshinobu Inoue if (m->m_flags & M_PKTHDR) 269369a34685SYoshinobu Inoue m->m_pkthdr.len--; 2694df8bae1dSRodney W. Grimes return; 2695df8bae1dSRodney W. Grimes } 2696df8bae1dSRodney W. Grimes cnt -= m->m_len; 2697df8bae1dSRodney W. Grimes m = m->m_next; 26984dfdffe9SAndre Oppermann if (m == NULL) 2699df8bae1dSRodney W. Grimes break; 2700df8bae1dSRodney W. Grimes } 2701df8bae1dSRodney W. Grimes panic("tcp_pulloutofband"); 2702df8bae1dSRodney W. Grimes } 2703df8bae1dSRodney W. Grimes 2704df8bae1dSRodney W. Grimes /* 2705df8bae1dSRodney W. Grimes * Collect new round-trip time estimate 2706df8bae1dSRodney W. Grimes * and update averages and current timeout. 2707df8bae1dSRodney W. Grimes */ 27080312fbe9SPoul-Henning Kamp static void 2709ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt) 2710df8bae1dSRodney W. Grimes { 27118b615593SMarko Zec INIT_VNET_INET(tp->t_inpcb->inp_vnet); 2712ad3f9ab3SAndre Oppermann int delta; 2713233e8c18SGarrett Wollman 27148501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 27152be3bf22SRobert Watson 2716603724d3SBjoern A. Zeeb V_tcpstat.tcps_rttupdated++; 2717233e8c18SGarrett Wollman tp->t_rttupdated++; 2718233e8c18SGarrett Wollman if (tp->t_srtt != 0) { 2719233e8c18SGarrett Wollman /* 2720233e8c18SGarrett Wollman * srtt is stored as fixed point with 5 bits after the 2721233e8c18SGarrett Wollman * binary point (i.e., scaled by 8). The following magic 2722233e8c18SGarrett Wollman * is equivalent to the smoothing algorithm in rfc793 with 2723233e8c18SGarrett Wollman * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed 2724233e8c18SGarrett Wollman * point). Adjust rtt to origin 0. 2725233e8c18SGarrett Wollman */ 2726233e8c18SGarrett Wollman delta = ((rtt - 1) << TCP_DELTA_SHIFT) 2727233e8c18SGarrett Wollman - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); 2728233e8c18SGarrett Wollman 2729233e8c18SGarrett Wollman if ((tp->t_srtt += delta) <= 0) 2730233e8c18SGarrett Wollman tp->t_srtt = 1; 2731233e8c18SGarrett Wollman 2732233e8c18SGarrett Wollman /* 2733233e8c18SGarrett Wollman * We accumulate a smoothed rtt variance (actually, a 2734233e8c18SGarrett Wollman * smoothed mean difference), then set the retransmit 2735233e8c18SGarrett Wollman * timer to smoothed rtt + 4 times the smoothed variance. 2736233e8c18SGarrett Wollman * rttvar is stored as fixed point with 4 bits after the 2737233e8c18SGarrett Wollman * binary point (scaled by 16). The following is 2738233e8c18SGarrett Wollman * equivalent to rfc793 smoothing with an alpha of .75 2739233e8c18SGarrett Wollman * (rttvar = rttvar*3/4 + |delta| / 4). This replaces 2740233e8c18SGarrett Wollman * rfc793's wired-in beta. 2741233e8c18SGarrett Wollman */ 2742233e8c18SGarrett Wollman if (delta < 0) 2743233e8c18SGarrett Wollman delta = -delta; 2744233e8c18SGarrett Wollman delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); 2745233e8c18SGarrett Wollman if ((tp->t_rttvar += delta) <= 0) 2746233e8c18SGarrett Wollman tp->t_rttvar = 1; 27471fcc99b5SMatthew Dillon if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar) 27481fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2749233e8c18SGarrett Wollman } else { 2750233e8c18SGarrett Wollman /* 2751233e8c18SGarrett Wollman * No rtt measurement yet - use the unsmoothed rtt. 2752233e8c18SGarrett Wollman * Set the variance to half the rtt (so our first 2753233e8c18SGarrett Wollman * retransmit happens at 3*rtt). 2754233e8c18SGarrett Wollman */ 2755233e8c18SGarrett Wollman tp->t_srtt = rtt << TCP_RTT_SHIFT; 2756233e8c18SGarrett Wollman tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); 27571fcc99b5SMatthew Dillon tp->t_rttbest = tp->t_srtt + tp->t_rttvar; 2758233e8c18SGarrett Wollman } 27599b8b58e0SJonathan Lemon tp->t_rtttime = 0; 2760df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 2761df8bae1dSRodney W. Grimes 2762df8bae1dSRodney W. Grimes /* 2763df8bae1dSRodney W. Grimes * the retransmit should happen at rtt + 4 * rttvar. 2764df8bae1dSRodney W. Grimes * Because of the way we do the smoothing, srtt and rttvar 2765df8bae1dSRodney W. Grimes * will each average +1/2 tick of bias. When we compute 2766df8bae1dSRodney W. Grimes * the retransmit timer, we want 1/2 tick of rounding and 2767df8bae1dSRodney W. Grimes * 1 extra tick because of +-1/2 tick uncertainty in the 2768df8bae1dSRodney W. Grimes * firing of the timer. The bias will give us exactly the 2769df8bae1dSRodney W. Grimes * 1.5 tick we need. But, because the bias is 2770df8bae1dSRodney W. Grimes * statistical, we have to test that we don't drop below 2771df8bae1dSRodney W. Grimes * the minimum feasible timer (which is 2 ticks). 2772df8bae1dSRodney W. Grimes */ 2773233e8c18SGarrett Wollman TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), 27749e2874b0SGarrett Wollman max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); 2775df8bae1dSRodney W. Grimes 2776df8bae1dSRodney W. Grimes /* 2777df8bae1dSRodney W. Grimes * We received an ack for a packet that wasn't retransmitted; 2778df8bae1dSRodney W. Grimes * it is probably safe to discard any error indications we've 2779df8bae1dSRodney W. Grimes * received recently. This isn't quite right, but close enough 2780df8bae1dSRodney W. Grimes * for now (a route might have failed after we sent a segment, 2781df8bae1dSRodney W. Grimes * and the return path might not be symmetrical). 2782df8bae1dSRodney W. Grimes */ 2783df8bae1dSRodney W. Grimes tp->t_softerror = 0; 2784df8bae1dSRodney W. Grimes } 2785df8bae1dSRodney W. Grimes 2786df8bae1dSRodney W. Grimes /* 2787df8bae1dSRodney W. Grimes * Determine a reasonable value for maxseg size. 2788df8bae1dSRodney W. Grimes * If the route is known, check route for mtu. 2789df8bae1dSRodney W. Grimes * If none, use an mss that can be handled on the outgoing 2790df8bae1dSRodney W. Grimes * interface without forcing IP to fragment; if bigger than 2791df8bae1dSRodney W. Grimes * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES 2792df8bae1dSRodney W. Grimes * to utilize large mbufs. If no route is found, route has no mtu, 2793df8bae1dSRodney W. Grimes * or the destination isn't local, use a default, hopefully conservative 2794df8bae1dSRodney W. Grimes * size (usually 512 or the default IP max size, but no more than the mtu 2795df8bae1dSRodney W. Grimes * of the interface), as we can't discover anything about intervening 2796df8bae1dSRodney W. Grimes * gateways or networks. We also initialize the congestion/slow start 2797df8bae1dSRodney W. Grimes * window to be a single segment if the destination isn't local. 2798df8bae1dSRodney W. Grimes * While looking at the routing entry, we also initialize other path-dependent 2799df8bae1dSRodney W. Grimes * parameters from pre-set or cached values in the routing entry. 2800a0292f23SGarrett Wollman * 2801a0292f23SGarrett Wollman * Also take into account the space needed for options that we 2802a0292f23SGarrett Wollman * send regularly. Make maxseg shorter by that amount to assure 2803a0292f23SGarrett Wollman * that we can send maxseg amount of data even when the options 2804a0292f23SGarrett Wollman * are present. Store the upper limit of the length of options plus 2805a0292f23SGarrett Wollman * data in maxopd. 2806a0292f23SGarrett Wollman * 2807a0292f23SGarrett Wollman * In case of T/TCP, we call this routine during implicit connection 2808a0292f23SGarrett Wollman * setup as well (offer = -1), to initialize maxseg from the cached 2809a0292f23SGarrett Wollman * MSS of our peer. 281097d8d152SAndre Oppermann * 281197d8d152SAndre Oppermann * NOTE that this routine is only called when we process an incoming 281297d8d152SAndre Oppermann * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt(). 2813df8bae1dSRodney W. Grimes */ 2814a0292f23SGarrett Wollman void 28153cee92e0SBjoern A. Zeeb tcp_mss_update(struct tcpcb *tp, int offer, struct hc_metrics_lite *metricptr) 2816df8bae1dSRodney W. Grimes { 28178b615593SMarko Zec INIT_VNET_INET(tp->t_inpcb->inp_vnet); 28183cee92e0SBjoern A. Zeeb int mss; 281997d8d152SAndre Oppermann u_long maxmtu; 2820c068736aSJeffrey Hsu struct inpcb *inp = tp->t_inpcb; 282197d8d152SAndre Oppermann struct hc_metrics_lite metrics; 2822a0292f23SGarrett Wollman int origoffer = offer; 2823233dcce1SAndre Oppermann int mtuflags = 0; 2824fb59c426SYoshinobu Inoue #ifdef INET6 2825c068736aSJeffrey Hsu int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 2826c068736aSJeffrey Hsu size_t min_protoh = isipv6 ? 2827c068736aSJeffrey Hsu sizeof (struct ip6_hdr) + sizeof (struct tcphdr) : 2828c068736aSJeffrey Hsu sizeof (struct tcpiphdr); 2829c068736aSJeffrey Hsu #else 2830c068736aSJeffrey Hsu const size_t min_protoh = sizeof(struct tcpiphdr); 2831fb59c426SYoshinobu Inoue #endif 2832df8bae1dSRodney W. Grimes 28338501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 28347a3244ccSRobert Watson 2835e8949f74SAndre Oppermann /* Initialize. */ 283697d8d152SAndre Oppermann #ifdef INET6 283797d8d152SAndre Oppermann if (isipv6) { 2838233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags); 2839603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_v6mssdflt; 284097d8d152SAndre Oppermann } else 284197d8d152SAndre Oppermann #endif 284297d8d152SAndre Oppermann { 2843233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags); 2844603724d3SBjoern A. Zeeb tp->t_maxopd = tp->t_maxseg = V_tcp_mssdflt; 2845df8bae1dSRodney W. Grimes } 2846df8bae1dSRodney W. Grimes 284797d8d152SAndre Oppermann /* 2848e8949f74SAndre Oppermann * No route to sender, stay with default mss and return. 284997d8d152SAndre Oppermann */ 28506f01cac6SBjoern A. Zeeb if (maxmtu == 0) { 28516f01cac6SBjoern A. Zeeb /* 28526f01cac6SBjoern A. Zeeb * In case we return early we need to intialize metrics 28536f01cac6SBjoern A. Zeeb * to a defined state as tcp_hc_get() would do for us 28546f01cac6SBjoern A. Zeeb * if there was no cache hit. 28556f01cac6SBjoern A. Zeeb */ 28566f01cac6SBjoern A. Zeeb if (metricptr != NULL) 28576f01cac6SBjoern A. Zeeb bzero(metricptr, sizeof(struct hc_metrics_lite)); 285897d8d152SAndre Oppermann return; 28596f01cac6SBjoern A. Zeeb } 286097d8d152SAndre Oppermann 28613cee92e0SBjoern A. Zeeb /* Check the interface for TSO capabilities. */ 28623cee92e0SBjoern A. Zeeb if (mtuflags & CSUM_TSO) 28633cee92e0SBjoern A. Zeeb tp->t_flags |= TF_TSO; 28643cee92e0SBjoern A. Zeeb 2865e8949f74SAndre Oppermann /* What have we got? */ 286697d8d152SAndre Oppermann switch (offer) { 286797d8d152SAndre Oppermann case 0: 286897d8d152SAndre Oppermann /* 286997d8d152SAndre Oppermann * Offer == 0 means that there was no MSS on the SYN 2870c3b02504SBjoern A. Zeeb * segment, in this case we use tcp_mssdflt as 2871c3b02504SBjoern A. Zeeb * already assigned to t_maxopd above. 287297d8d152SAndre Oppermann */ 2873c3b02504SBjoern A. Zeeb offer = tp->t_maxopd; 287497d8d152SAndre Oppermann break; 287597d8d152SAndre Oppermann 287697d8d152SAndre Oppermann case -1: 2877a0292f23SGarrett Wollman /* 2878c94c54e4SAndre Oppermann * Offer == -1 means that we didn't receive SYN yet. 2879a0292f23SGarrett Wollman */ 288097d8d152SAndre Oppermann /* FALLTHROUGH */ 288197d8d152SAndre Oppermann 288297d8d152SAndre Oppermann default: 2883a0292f23SGarrett Wollman /* 288453369ac9SAndre Oppermann * Prevent DoS attack with too small MSS. Round up 288553369ac9SAndre Oppermann * to at least minmss. 288653369ac9SAndre Oppermann */ 2887603724d3SBjoern A. Zeeb offer = max(offer, V_tcp_minmss); 288897d8d152SAndre Oppermann } 2889a0292f23SGarrett Wollman 2890df8bae1dSRodney W. Grimes /* 2891e8949f74SAndre Oppermann * rmx information is now retrieved from tcp_hostcache. 2892df8bae1dSRodney W. Grimes */ 289397d8d152SAndre Oppermann tcp_hc_get(&inp->inp_inc, &metrics); 28943cee92e0SBjoern A. Zeeb if (metricptr != NULL) 28953cee92e0SBjoern A. Zeeb bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite)); 289697d8d152SAndre Oppermann 2897df8bae1dSRodney W. Grimes /* 2898e8949f74SAndre Oppermann * If there's a discovered mtu int tcp hostcache, use it 2899fb59c426SYoshinobu Inoue * else, use the link mtu. 2900df8bae1dSRodney W. Grimes */ 290197d8d152SAndre Oppermann if (metrics.rmx_mtu) 29022d166c02SAndre Oppermann mss = min(metrics.rmx_mtu, maxmtu) - min_protoh; 2903c068736aSJeffrey Hsu else { 290431b3783cSHajimu UMEMOTO #ifdef INET6 2905fb59c426SYoshinobu Inoue if (isipv6) { 290697d8d152SAndre Oppermann mss = maxmtu - min_protoh; 2907603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 290897d8d152SAndre Oppermann !in6_localaddr(&inp->in6p_faddr)) 2909603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_v6mssdflt); 2910b3399803SHajimu UMEMOTO } else 2911b3399803SHajimu UMEMOTO #endif 291297d8d152SAndre Oppermann { 291397d8d152SAndre Oppermann mss = maxmtu - min_protoh; 2914603724d3SBjoern A. Zeeb if (!V_path_mtu_discovery && 291597d8d152SAndre Oppermann !in_localaddr(inp->inp_faddr)) 2916603724d3SBjoern A. Zeeb mss = min(mss, V_tcp_mssdflt); 2917a0292f23SGarrett Wollman } 29183cee92e0SBjoern A. Zeeb /* 29193cee92e0SBjoern A. Zeeb * XXX - The above conditional (mss = maxmtu - min_protoh) 29203cee92e0SBjoern A. Zeeb * probably violates the TCP spec. 29213cee92e0SBjoern A. Zeeb * The problem is that, since we don't know the 29223cee92e0SBjoern A. Zeeb * other end's MSS, we are supposed to use a conservative 29233cee92e0SBjoern A. Zeeb * default. But, if we do that, then MTU discovery will 29243cee92e0SBjoern A. Zeeb * never actually take place, because the conservative 29253cee92e0SBjoern A. Zeeb * default is much less than the MTUs typically seen 29263cee92e0SBjoern A. Zeeb * on the Internet today. For the moment, we'll sweep 29273cee92e0SBjoern A. Zeeb * this under the carpet. 29283cee92e0SBjoern A. Zeeb * 29293cee92e0SBjoern A. Zeeb * The conservative default might not actually be a problem 29303cee92e0SBjoern A. Zeeb * if the only case this occurs is when sending an initial 29313cee92e0SBjoern A. Zeeb * SYN with options and data to a host we've never talked 29323cee92e0SBjoern A. Zeeb * to before. Then, they will reply with an MSS value which 29333cee92e0SBjoern A. Zeeb * will get recorded and the new parameters should get 29343cee92e0SBjoern A. Zeeb * recomputed. For Further Study. 29353cee92e0SBjoern A. Zeeb */ 293697d8d152SAndre Oppermann } 2937a0292f23SGarrett Wollman mss = min(mss, offer); 293897d8d152SAndre Oppermann 2939a0292f23SGarrett Wollman /* 29403cee92e0SBjoern A. Zeeb * Sanity check: make sure that maxopd will be large 29413cee92e0SBjoern A. Zeeb * enough to allow some data on segments even if the 29423cee92e0SBjoern A. Zeeb * all the option space is used (40bytes). Otherwise 29433cee92e0SBjoern A. Zeeb * funny things may happen in tcp_output. 29443cee92e0SBjoern A. Zeeb */ 29453cee92e0SBjoern A. Zeeb mss = max(mss, 64); 29463cee92e0SBjoern A. Zeeb 29473cee92e0SBjoern A. Zeeb /* 2948a0292f23SGarrett Wollman * maxopd stores the maximum length of data AND options 2949a0292f23SGarrett Wollman * in a segment; maxseg is the amount of data in a normal 2950a0292f23SGarrett Wollman * segment. We need to store this value (maxopd) apart 2951a0292f23SGarrett Wollman * from maxseg, because now every segment carries options 2952a0292f23SGarrett Wollman * and thus we normally have somewhat less data in segments. 2953a0292f23SGarrett Wollman */ 2954a0292f23SGarrett Wollman tp->t_maxopd = mss; 2955a0292f23SGarrett Wollman 2956a0292f23SGarrett Wollman /* 2957e8949f74SAndre Oppermann * origoffer==-1 indicates that no segments were received yet. 2958c94c54e4SAndre Oppermann * In this case we just guess. 2959a0292f23SGarrett Wollman */ 2960a0292f23SGarrett Wollman if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 2961a0292f23SGarrett Wollman (origoffer == -1 || 2962a0292f23SGarrett Wollman (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) 2963a0292f23SGarrett Wollman mss -= TCPOLEN_TSTAMP_APPA; 2964a0292f23SGarrett Wollman 2965df8bae1dSRodney W. Grimes #if (MCLBYTES & (MCLBYTES - 1)) == 0 2966df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2967df8bae1dSRodney W. Grimes mss &= ~(MCLBYTES-1); 2968df8bae1dSRodney W. Grimes #else 2969df8bae1dSRodney W. Grimes if (mss > MCLBYTES) 2970df8bae1dSRodney W. Grimes mss = mss / MCLBYTES * MCLBYTES; 2971df8bae1dSRodney W. Grimes #endif 297297d8d152SAndre Oppermann tp->t_maxseg = mss; 29733cee92e0SBjoern A. Zeeb } 29743cee92e0SBjoern A. Zeeb 29753cee92e0SBjoern A. Zeeb void 29763cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer) 29773cee92e0SBjoern A. Zeeb { 29783cee92e0SBjoern A. Zeeb int rtt, mss; 29793cee92e0SBjoern A. Zeeb u_long bufsize; 29803cee92e0SBjoern A. Zeeb struct inpcb *inp; 29813cee92e0SBjoern A. Zeeb struct socket *so; 29823cee92e0SBjoern A. Zeeb struct hc_metrics_lite metrics; 29833cee92e0SBjoern A. Zeeb #ifdef INET6 29843cee92e0SBjoern A. Zeeb int isipv6; 29853cee92e0SBjoern A. Zeeb #endif 29863cee92e0SBjoern A. Zeeb KASSERT(tp != NULL, ("%s: tp == NULL", __func__)); 29873cee92e0SBjoern A. Zeeb 29883cee92e0SBjoern A. Zeeb tcp_mss_update(tp, offer, &metrics); 29893cee92e0SBjoern A. Zeeb 29903cee92e0SBjoern A. Zeeb mss = tp->t_maxseg; 29913cee92e0SBjoern A. Zeeb inp = tp->t_inpcb; 29923cee92e0SBjoern A. Zeeb #ifdef INET6 29933cee92e0SBjoern A. Zeeb isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; 29943cee92e0SBjoern A. Zeeb #endif 299597d8d152SAndre Oppermann 2996df8bae1dSRodney W. Grimes /* 299797d8d152SAndre Oppermann * If there's a pipesize, change the socket buffer to that size, 299897d8d152SAndre Oppermann * don't change if sb_hiwat is different than default (then it 299997d8d152SAndre Oppermann * has been changed on purpose with setsockopt). 300097d8d152SAndre Oppermann * Make the socket buffers an integral number of mss units; 300197d8d152SAndre Oppermann * if the mss is larger than the socket buffer, decrease the mss. 3002df8bae1dSRodney W. Grimes */ 3003c3b02504SBjoern A. Zeeb so = inp->inp_socket; 30043f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_snd); 300597d8d152SAndre Oppermann if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe) 300697d8d152SAndre Oppermann bufsize = metrics.rmx_sendpipe; 300797d8d152SAndre Oppermann else 3008df8bae1dSRodney W. Grimes bufsize = so->so_snd.sb_hiwat; 3009df8bae1dSRodney W. Grimes if (bufsize < mss) 3010df8bae1dSRodney W. Grimes mss = bufsize; 3011df8bae1dSRodney W. Grimes else { 3012df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3013df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3014df8bae1dSRodney W. Grimes bufsize = sb_max; 301588c39af3SRuslan Ermilov if (bufsize > so->so_snd.sb_hiwat) 30163f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_snd, bufsize, so, NULL); 3017df8bae1dSRodney W. Grimes } 30183f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 3019df8bae1dSRodney W. Grimes tp->t_maxseg = mss; 3020df8bae1dSRodney W. Grimes 30213f11a2f3SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 302297d8d152SAndre Oppermann if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe) 302397d8d152SAndre Oppermann bufsize = metrics.rmx_recvpipe; 302497d8d152SAndre Oppermann else 3025df8bae1dSRodney W. Grimes bufsize = so->so_rcv.sb_hiwat; 3026df8bae1dSRodney W. Grimes if (bufsize > mss) { 3027df8bae1dSRodney W. Grimes bufsize = roundup(bufsize, mss); 3028df8bae1dSRodney W. Grimes if (bufsize > sb_max) 3029df8bae1dSRodney W. Grimes bufsize = sb_max; 303088c39af3SRuslan Ermilov if (bufsize > so->so_rcv.sb_hiwat) 30313f11a2f3SRobert Watson (void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL); 3032df8bae1dSRodney W. Grimes } 30333f11a2f3SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 3034a0292f23SGarrett Wollman /* 3035e8949f74SAndre Oppermann * While we're here, check the others too. 3036a0292f23SGarrett Wollman */ 303797d8d152SAndre Oppermann if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) { 303897d8d152SAndre Oppermann tp->t_srtt = rtt; 303997d8d152SAndre Oppermann tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE; 3040603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedrtt++; 304197d8d152SAndre Oppermann if (metrics.rmx_rttvar) { 304297d8d152SAndre Oppermann tp->t_rttvar = metrics.rmx_rttvar; 3043603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedrttvar++; 304497d8d152SAndre Oppermann } else { 304597d8d152SAndre Oppermann /* default variation is +- 1 rtt */ 304697d8d152SAndre Oppermann tp->t_rttvar = 304797d8d152SAndre Oppermann tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; 304897d8d152SAndre Oppermann } 304997d8d152SAndre Oppermann TCPT_RANGESET(tp->t_rxtcur, 305097d8d152SAndre Oppermann ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 305197d8d152SAndre Oppermann tp->t_rttmin, TCPTV_REXMTMAX); 305297d8d152SAndre Oppermann } 305397d8d152SAndre Oppermann if (metrics.rmx_ssthresh) { 3054df8bae1dSRodney W. Grimes /* 3055df8bae1dSRodney W. Grimes * There's some sort of gateway or interface 3056df8bae1dSRodney W. Grimes * buffer limit on the path. Use this to set 3057df8bae1dSRodney W. Grimes * the slow start threshhold, but set the 3058df8bae1dSRodney W. Grimes * threshold to no less than 2*mss. 3059df8bae1dSRodney W. Grimes */ 306097d8d152SAndre Oppermann tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh); 3061603724d3SBjoern A. Zeeb V_tcpstat.tcps_usedssthresh++; 3062df8bae1dSRodney W. Grimes } 306397d8d152SAndre Oppermann if (metrics.rmx_bandwidth) 306497d8d152SAndre Oppermann tp->snd_bandwidth = metrics.rmx_bandwidth; 306597d8d152SAndre Oppermann 306697d8d152SAndre Oppermann /* 306797d8d152SAndre Oppermann * Set the slow-start flight size depending on whether this 306897d8d152SAndre Oppermann * is a local network or not. 306997d8d152SAndre Oppermann * 307097d8d152SAndre Oppermann * Extend this so we cache the cwnd too and retrieve it here. 307197d8d152SAndre Oppermann * Make cwnd even bigger than RFC3390 suggests but only if we 307297d8d152SAndre Oppermann * have previous experience with the remote host. Be careful 307397d8d152SAndre Oppermann * not make cwnd bigger than remote receive window or our own 307497d8d152SAndre Oppermann * send socket buffer. Maybe put some additional upper bound 307597d8d152SAndre Oppermann * on the retrieved cwnd. Should do incremental updates to 307697d8d152SAndre Oppermann * hostcache when cwnd collapses so next connection doesn't 307797d8d152SAndre Oppermann * overloads the path again. 307897d8d152SAndre Oppermann * 307997d8d152SAndre Oppermann * RFC3390 says only do this if SYN or SYN/ACK didn't got lost. 308097d8d152SAndre Oppermann * We currently check only in syncache_socket for that. 308197d8d152SAndre Oppermann */ 308297d8d152SAndre Oppermann #define TCP_METRICS_CWND 308397d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND 308497d8d152SAndre Oppermann if (metrics.rmx_cwnd) 308597d8d152SAndre Oppermann tp->snd_cwnd = max(mss, 308697d8d152SAndre Oppermann min(metrics.rmx_cwnd / 2, 308797d8d152SAndre Oppermann min(tp->snd_wnd, so->so_snd.sb_hiwat))); 308897d8d152SAndre Oppermann else 308997d8d152SAndre Oppermann #endif 3090603724d3SBjoern A. Zeeb if (V_tcp_do_rfc3390) 309197d8d152SAndre Oppermann tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380)); 309297d8d152SAndre Oppermann #ifdef INET6 309397d8d152SAndre Oppermann else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) || 309497d8d152SAndre Oppermann (!isipv6 && in_localaddr(inp->inp_faddr))) 3095943ae302SAndre Oppermann #else 3096943ae302SAndre Oppermann else if (in_localaddr(inp->inp_faddr)) 309797d8d152SAndre Oppermann #endif 3098603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz_local; 309997d8d152SAndre Oppermann else 3100603724d3SBjoern A. Zeeb tp->snd_cwnd = mss * V_ss_fltsz; 3101a0292f23SGarrett Wollman } 3102a0292f23SGarrett Wollman 3103a0292f23SGarrett Wollman /* 3104a0292f23SGarrett Wollman * Determine the MSS option to send on an outgoing SYN. 3105a0292f23SGarrett Wollman */ 3106a0292f23SGarrett Wollman int 3107ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc) 3108a0292f23SGarrett Wollman { 31098b615593SMarko Zec INIT_VNET_INET(curvnet); 311097d8d152SAndre Oppermann int mss = 0; 311197d8d152SAndre Oppermann u_long maxmtu = 0; 311297d8d152SAndre Oppermann u_long thcmtu = 0; 311397d8d152SAndre Oppermann size_t min_protoh; 3114fb59c426SYoshinobu Inoue #ifdef INET6 311597d8d152SAndre Oppermann int isipv6 = inc->inc_isipv6 ? 1 : 0; 3116fb59c426SYoshinobu Inoue #endif 3117a0292f23SGarrett Wollman 311897d8d152SAndre Oppermann KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer")); 3119a0292f23SGarrett Wollman 312031b3783cSHajimu UMEMOTO #ifdef INET6 312197d8d152SAndre Oppermann if (isipv6) { 3122603724d3SBjoern A. Zeeb mss = V_tcp_v6mssdflt; 3123233dcce1SAndre Oppermann maxmtu = tcp_maxmtu6(inc, NULL); 312497d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 312597d8d152SAndre Oppermann min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 312697d8d152SAndre Oppermann } else 312731b3783cSHajimu UMEMOTO #endif 312897d8d152SAndre Oppermann { 3129603724d3SBjoern A. Zeeb mss = V_tcp_mssdflt; 3130233dcce1SAndre Oppermann maxmtu = tcp_maxmtu(inc, NULL); 313197d8d152SAndre Oppermann thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */ 313297d8d152SAndre Oppermann min_protoh = sizeof(struct tcpiphdr); 313397d8d152SAndre Oppermann } 313497d8d152SAndre Oppermann if (maxmtu && thcmtu) 313597d8d152SAndre Oppermann mss = min(maxmtu, thcmtu) - min_protoh; 313697d8d152SAndre Oppermann else if (maxmtu || thcmtu) 313797d8d152SAndre Oppermann mss = max(maxmtu, thcmtu) - min_protoh; 313897d8d152SAndre Oppermann 313997d8d152SAndre Oppermann return (mss); 3140df8bae1dSRodney W. Grimes } 314146f58482SJonathan Lemon 314246f58482SJonathan Lemon 314346f58482SJonathan Lemon /* 3144c068736aSJeffrey Hsu * On a partial ack arrives, force the retransmission of the 3145c068736aSJeffrey Hsu * next unacknowledged segment. Do not clear tp->t_dupacks. 3146c068736aSJeffrey Hsu * By setting snd_nxt to ti_ack, this forces retransmission timer to 3147c068736aSJeffrey Hsu * be started again. 314846f58482SJonathan Lemon */ 3149c068736aSJeffrey Hsu static void 3150ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) 315146f58482SJonathan Lemon { 315246f58482SJonathan Lemon tcp_seq onxt = tp->snd_nxt; 315346f58482SJonathan Lemon u_long ocwnd = tp->snd_cwnd; 315446f58482SJonathan Lemon 31558501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 31567a3244ccSRobert Watson 3157b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 315846f58482SJonathan Lemon tp->t_rtttime = 0; 315946f58482SJonathan Lemon tp->snd_nxt = th->th_ack; 31606b2a5f92SJayanth Vijayaraghavan /* 3161c068736aSJeffrey Hsu * Set snd_cwnd to one segment beyond acknowledged offset. 3162c068736aSJeffrey Hsu * (tp->snd_una has not yet been updated when this function is called.) 31636b2a5f92SJayanth Vijayaraghavan */ 31646b2a5f92SJayanth Vijayaraghavan tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una); 3165a84db8f4SMatthew Dillon tp->t_flags |= TF_ACKNOW; 31666b2a5f92SJayanth Vijayaraghavan (void) tcp_output(tp); 316746f58482SJonathan Lemon tp->snd_cwnd = ocwnd; 316846f58482SJonathan Lemon if (SEQ_GT(onxt, tp->snd_nxt)) 316946f58482SJonathan Lemon tp->snd_nxt = onxt; 317046f58482SJonathan Lemon /* 317146f58482SJonathan Lemon * Partial window deflation. Relies on fact that tp->snd_una 317246f58482SJonathan Lemon * not updated yet. 317346f58482SJonathan Lemon */ 3174d7587117SPaul Saab if (tp->snd_cwnd > th->th_ack - tp->snd_una) 3175d7587117SPaul Saab tp->snd_cwnd -= th->th_ack - tp->snd_una; 3176d7587117SPaul Saab else 3177d7587117SPaul Saab tp->snd_cwnd = 0; 3178d7587117SPaul Saab tp->snd_cwnd += tp->t_maxseg; 317946f58482SJonathan Lemon } 3180