]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
39037602 | 2 | * Copyright (c) 2000-2016 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
39037602 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
39037602 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
39037602 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
39037602 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 | |
9bccf70c | 61 | * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.107.2.16 2001/08/22 00:59:12 silby Exp $ |
1c79356b | 62 | */ |
2d21ac55 A |
63 | /* |
64 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
65 | * support for mandatory and extensible security protections. This notice | |
66 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
67 | * Version 2.0. | |
68 | */ | |
1c79356b A |
69 | |
70 | #include <sys/param.h> | |
71 | #include <sys/systm.h> | |
72 | #include <sys/kernel.h> | |
73 | #include <sys/sysctl.h> | |
74 | #include <sys/malloc.h> | |
75 | #include <sys/mbuf.h> | |
76 | #include <sys/proc.h> /* for proc0 declaration */ | |
77 | #include <sys/protosw.h> | |
78 | #include <sys/socket.h> | |
79 | #include <sys/socketvar.h> | |
80 | #include <sys/syslog.h> | |
316670eb | 81 | #include <sys/mcache.h> |
39236c6e | 82 | #include <sys/kasl.h> |
1c79356b A |
83 | #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */ |
84 | ||
b0d623f7 A |
85 | #include <machine/endian.h> |
86 | ||
1c79356b | 87 | #include <net/if.h> |
d12e1678 | 88 | #include <net/if_types.h> |
1c79356b | 89 | #include <net/route.h> |
6d2010ae | 90 | #include <net/ntstat.h> |
39236c6e | 91 | #include <net/dlil.h> |
1c79356b A |
92 | |
93 | #include <netinet/in.h> | |
94 | #include <netinet/in_systm.h> | |
95 | #include <netinet/ip.h> | |
39037602 | 96 | #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ |
1c79356b | 97 | #include <netinet/in_var.h> |
9bccf70c | 98 | #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ |
1c79356b | 99 | #include <netinet/in_pcb.h> |
9bccf70c | 100 | #include <netinet/ip_var.h> |
6d2010ae | 101 | #include <mach/sdt.h> |
1c79356b A |
102 | #if INET6 |
103 | #include <netinet/ip6.h> | |
104 | #include <netinet/icmp6.h> | |
105 | #include <netinet6/nd6.h> | |
106 | #include <netinet6/ip6_var.h> | |
107 | #include <netinet6/in6_pcb.h> | |
108 | #endif | |
109 | #include <netinet/tcp.h> | |
3e170ce0 | 110 | #include <netinet/tcp_cache.h> |
1c79356b A |
111 | #include <netinet/tcp_fsm.h> |
112 | #include <netinet/tcp_seq.h> | |
113 | #include <netinet/tcp_timer.h> | |
114 | #include <netinet/tcp_var.h> | |
6d2010ae | 115 | #include <netinet/tcp_cc.h> |
39236c6e | 116 | #include <dev/random/randomdev.h> |
6d2010ae | 117 | #include <kern/zalloc.h> |
9bccf70c A |
118 | #if INET6 |
119 | #include <netinet6/tcp6_var.h> | |
120 | #endif | |
1c79356b A |
121 | #include <netinet/tcpip.h> |
122 | #if TCPDEBUG | |
123 | #include <netinet/tcp_debug.h> | |
9bccf70c | 124 | u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */ |
1c79356b A |
125 | struct tcphdr tcp_savetcp; |
126 | #endif /* TCPDEBUG */ | |
127 | ||
128 | #if IPSEC | |
129 | #include <netinet6/ipsec.h> | |
9bccf70c A |
130 | #if INET6 |
131 | #include <netinet6/ipsec6.h> | |
132 | #endif | |
1c79356b A |
133 | #include <netkey/key.h> |
134 | #endif /*IPSEC*/ | |
135 | ||
2d21ac55 A |
136 | #if CONFIG_MACF_NET || CONFIG_MACF_SOCKET |
137 | #include <security/mac_framework.h> | |
138 | #endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */ | |
139 | ||
1c79356b | 140 | #include <sys/kdebug.h> |
316670eb | 141 | #include <netinet/lro_ext.h> |
39236c6e A |
142 | #if MPTCP |
143 | #include <netinet/mptcp_var.h> | |
144 | #include <netinet/mptcp.h> | |
39037602 | 145 | #include <netinet/mptcp_opt.h> |
39236c6e | 146 | #endif /* MPTCP */ |
1c79356b | 147 | |
3e170ce0 A |
148 | #include <corecrypto/ccaes.h> |
149 | ||
1c79356b A |
150 | #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0) |
151 | #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2) | |
152 | #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8)) | |
153 | #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8)) | |
154 | ||
ecc0ceb4 A |
155 | #define TCP_RTT_HISTORY_EXPIRE_TIME (60 * TCP_RETRANSHZ) |
156 | #define TCP_RECV_THROTTLE_WIN (5 * TCP_RETRANSHZ) | |
39037602 | 157 | #define TCP_STRETCHACK_ENABLE_PKTCNT 2000 |
ecc0ceb4 | 158 | |
1c79356b A |
159 | tcp_cc tcp_ccgen; |
160 | ||
161 | struct tcpstat tcpstat; | |
1c79356b | 162 | |
9bccf70c | 163 | static int log_in_vain = 0; |
3e170ce0 A |
164 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, |
165 | CTLFLAG_RW | CTLFLAG_LOCKED, &log_in_vain, 0, | |
166 | "Log all incoming TCP connections"); | |
9bccf70c A |
167 | |
168 | static int blackhole = 0; | |
3e170ce0 A |
169 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, |
170 | CTLFLAG_RW | CTLFLAG_LOCKED, &blackhole, 0, | |
171 | "Do not send RST when dropping refused connections"); | |
1c79356b | 172 | |
743b1565 | 173 | int tcp_delack_enabled = 3; |
3e170ce0 A |
174 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, |
175 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_delack_enabled, 0, | |
9bccf70c A |
176 | "Delay ACK to try and piggyback it onto a data packet"); |
177 | ||
178 | int tcp_lq_overflow = 1; | |
3e170ce0 A |
179 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_lq_overflow, |
180 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_lq_overflow, 0, | |
9bccf70c A |
181 | "Listen Queue Overflow"); |
182 | ||
6d2010ae A |
183 | int tcp_recv_bg = 0; |
184 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbg, CTLFLAG_RW | CTLFLAG_LOCKED, | |
3e170ce0 | 185 | &tcp_recv_bg, 0, "Receive background"); |
6d2010ae | 186 | |
9bccf70c | 187 | #if TCP_DROP_SYNFIN |
55e303ae | 188 | static int drop_synfin = 1; |
3e170ce0 A |
189 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, |
190 | CTLFLAG_RW | CTLFLAG_LOCKED, &drop_synfin, 0, | |
191 | "Drop TCP packets with SYN+FIN set"); | |
9bccf70c | 192 | #endif |
1c79356b | 193 | |
2d21ac55 | 194 | SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW|CTLFLAG_LOCKED, 0, |
e5568f75 A |
195 | "TCP Segment Reassembly Queue"); |
196 | ||
e5568f75 | 197 | static int tcp_reass_overflows = 0; |
3e170ce0 A |
198 | SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, |
199 | CTLFLAG_RD | CTLFLAG_LOCKED, &tcp_reass_overflows, 0, | |
e5568f75 A |
200 | "Global number of TCP Segment Reassembly Queue Overflows"); |
201 | ||
202 | ||
d12e1678 | 203 | __private_extern__ int slowlink_wsize = 8192; |
3e170ce0 A |
204 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowlink_wsize, |
205 | CTLFLAG_RW | CTLFLAG_LOCKED, | |
206 | &slowlink_wsize, 0, "Maximum advertised window size for slowlink"); | |
d12e1678 | 207 | |
6d2010ae | 208 | int maxseg_unacked = 8; |
3e170ce0 A |
209 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, maxseg_unacked, |
210 | CTLFLAG_RW | CTLFLAG_LOCKED, &maxseg_unacked, 0, | |
211 | "Maximum number of outstanding segments left unacked"); | |
2d21ac55 | 212 | |
3e170ce0 | 213 | int tcp_do_rfc3465 = 1; |
6d2010ae | 214 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_RW | CTLFLAG_LOCKED, |
3e170ce0 | 215 | &tcp_do_rfc3465, 0, ""); |
b0d623f7 | 216 | |
3e170ce0 A |
217 | int tcp_do_rfc3465_lim2 = 1; |
218 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465_lim2, | |
219 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_do_rfc3465_lim2, 0, | |
220 | "Appropriate bytes counting w/ L=2*SMSS"); | |
6d2010ae | 221 | |
3e170ce0 | 222 | int rtt_samples_per_slot = 20; |
6d2010ae | 223 | |
3e170ce0 A |
224 | int tcp_allowed_iaj = ALLOWED_IAJ; |
225 | int tcp_acc_iaj_high_thresh = ACC_IAJ_HIGH_THRESH; | |
226 | u_int32_t tcp_autorcvbuf_inc_shift = 3; | |
227 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, recv_allowed_iaj, | |
228 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_allowed_iaj, 0, | |
229 | "Allowed inter-packet arrival jiter"); | |
230 | #if (DEVELOPMENT || DEBUG) | |
231 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, acc_iaj_high_thresh, | |
232 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_acc_iaj_high_thresh, 0, | |
233 | "Used in calculating maximum accumulated IAJ"); | |
234 | ||
235 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, autorcvbufincshift, | |
236 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_autorcvbuf_inc_shift, 0, | |
237 | "Shift for increment in receive socket buffer size"); | |
238 | #endif /* (DEVELOPMENT || DEBUG) */ | |
6d2010ae | 239 | |
316670eb | 240 | u_int32_t tcp_do_autorcvbuf = 1; |
3e170ce0 A |
241 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, doautorcvbuf, |
242 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_do_autorcvbuf, 0, | |
243 | "Enable automatic socket buffer tuning"); | |
316670eb A |
244 | |
245 | u_int32_t tcp_autorcvbuf_max = 512 * 1024; | |
3e170ce0 A |
246 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, autorcvbufmax, |
247 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_autorcvbuf_max, 0, | |
248 | "Maximum receive socket buffer size"); | |
316670eb | 249 | |
39037602 A |
250 | u_int32_t tcp_autorcvbuf_max_ca = 512 * 1024; |
251 | #if (DEBUG || DEVELOPMENT) | |
252 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, autorcvbufmaxca, | |
253 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_autorcvbuf_max_ca, 0, | |
254 | "Maximum receive socket buffer size"); | |
255 | #endif /* (DEBUG || DEVELOPMENT) */ | |
256 | ||
39236c6e | 257 | int sw_lro = 0; |
316670eb A |
258 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, lro, CTLFLAG_RW | CTLFLAG_LOCKED, |
259 | &sw_lro, 0, "Used to coalesce TCP packets"); | |
260 | ||
261 | int lrodebug = 0; | |
3e170ce0 A |
262 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, lrodbg, |
263 | CTLFLAG_RW | CTLFLAG_LOCKED, &lrodebug, 0, | |
264 | "Used to debug SW LRO"); | |
316670eb | 265 | |
39236c6e | 266 | int lro_start = 4; |
3e170ce0 A |
267 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, lro_startcnt, |
268 | CTLFLAG_RW | CTLFLAG_LOCKED, &lro_start, 0, | |
269 | "Segments for starting LRO computed as power of 2"); | |
316670eb A |
270 | |
271 | extern int tcp_do_autosendbuf; | |
272 | ||
39236c6e | 273 | int limited_txmt = 1; |
39236c6e | 274 | int early_rexmt = 1; |
39236c6e | 275 | int sack_ackadv = 1; |
3e170ce0 A |
276 | int tcp_dsack_enable = 1; |
277 | ||
278 | #if (DEVELOPMENT || DEBUG) | |
279 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, limited_transmit, | |
280 | CTLFLAG_RW | CTLFLAG_LOCKED, &limited_txmt, 0, | |
281 | "Enable limited transmit"); | |
282 | ||
283 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, early_rexmt, | |
284 | CTLFLAG_RW | CTLFLAG_LOCKED, &early_rexmt, 0, | |
285 | "Enable Early Retransmit"); | |
286 | ||
287 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, sack_ackadv, | |
288 | CTLFLAG_RW | CTLFLAG_LOCKED, &sack_ackadv, 0, | |
289 | "Use SACK with cumulative ack advancement as a dupack"); | |
290 | ||
291 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, dsack_enable, | |
292 | CTLFLAG_RW | CTLFLAG_LOCKED, &tcp_dsack_enable, 0, | |
293 | "use DSACK TCP option to report duplicate segments"); | |
294 | #endif /* (DEVELOPMENT || DEBUG) */ | |
39236c6e | 295 | |
2d21ac55 | 296 | extern int tcp_TCPTV_MIN; |
6d2010ae A |
297 | extern int tcp_acc_iaj_high; |
298 | extern int tcp_acc_iaj_react_limit; | |
6d2010ae | 299 | |
39236c6e | 300 | int tcprexmtthresh = 3; |
d12e1678 | 301 | |
b0d623f7 | 302 | u_int32_t tcp_now; |
6d2010ae A |
303 | struct timeval tcp_uptime; /* uptime when tcp_now was last updated */ |
304 | lck_spin_t *tcp_uptime_lock; /* Used to sychronize updates to tcp_now */ | |
2d21ac55 | 305 | |
1c79356b A |
306 | struct inpcbhead tcb; |
307 | #define tcb6 tcb /* for KAME src sync over BSD*'s */ | |
308 | struct inpcbinfo tcbinfo; | |
309 | ||
6d2010ae | 310 | static void tcp_dooptions(struct tcpcb *, u_char *, int, struct tcphdr *, |
3e170ce0 A |
311 | struct tcpopt *); |
312 | static void tcp_finalize_options(struct tcpcb *, struct tcpopt *, unsigned int); | |
313 | static void tcp_pulloutofband(struct socket *, | |
314 | struct tcphdr *, struct mbuf *, int); | |
39236c6e A |
315 | static int tcp_reass(struct tcpcb *, struct tcphdr *, int *, struct mbuf *, |
316 | struct ifnet *); | |
3e170ce0 | 317 | static void tcp_xmit_timer(struct tcpcb *, int, u_int32_t, tcp_seq); |
2d21ac55 | 318 | static inline unsigned int tcp_maxmtu(struct rtentry *); |
39037602 | 319 | static inline int tcp_stretch_ack_enable(struct tcpcb *tp, int thflags); |
39236c6e | 320 | static inline void tcp_adaptive_rwtimo_check(struct tcpcb *, int); |
6d2010ae A |
321 | |
322 | #if TRAFFIC_MGT | |
fe8ab488 | 323 | static inline void update_iaj_state(struct tcpcb *tp, uint32_t tlen, |
3e170ce0 | 324 | int reset_size); |
39236c6e A |
325 | void compute_iaj(struct tcpcb *tp, int nlropkts, int lro_delay_factor); |
326 | static void compute_iaj_meat(struct tcpcb *tp, uint32_t cur_iaj); | |
6d2010ae A |
327 | #endif /* TRAFFIC_MGT */ |
328 | ||
2d21ac55 A |
329 | #if INET6 |
330 | static inline unsigned int tcp_maxmtu6(struct rtentry *); | |
331 | #endif | |
1c79356b | 332 | |
39037602 A |
333 | static void tcp_sbrcv_grow(struct tcpcb *tp, struct sockbuf *sb, |
334 | struct tcpopt *to, u_int32_t tlen, u_int32_t rcvbuf_max); | |
316670eb A |
335 | void tcp_sbrcv_trim(struct tcpcb *tp, struct sockbuf *sb); |
336 | static void tcp_sbsnd_trim(struct sockbuf *sbsnd); | |
337 | static inline void tcp_sbrcv_tstmp_check(struct tcpcb *tp); | |
338 | static inline void tcp_sbrcv_reserve(struct tcpcb *tp, struct sockbuf *sb, | |
39037602 | 339 | u_int32_t newsize, u_int32_t idealsize, u_int32_t rcvbuf_max); |
39236c6e | 340 | static void tcp_bad_rexmt_restore_state(struct tcpcb *tp, struct tcphdr *th); |
39037602 | 341 | static void tcp_compute_rtt(struct tcpcb *tp, struct tcpopt *to, |
3e170ce0 | 342 | struct tcphdr *th); |
fe8ab488 A |
343 | static void tcp_early_rexmt_check(struct tcpcb *tp, struct tcphdr *th); |
344 | static void tcp_bad_rexmt_check(struct tcpcb *tp, struct tcphdr *th, | |
3e170ce0 | 345 | struct tcpopt *to); |
39236c6e | 346 | /* |
39037602 A |
347 | * Constants used for resizing receive socket buffer |
348 | * when timestamps are not supported | |
39236c6e | 349 | */ |
316670eb A |
350 | #define TCPTV_RCVNOTS_QUANTUM 100 |
351 | #define TCP_RCVNOTS_BYTELEVEL 204800 | |
39236c6e | 352 | |
39037602 A |
353 | /* |
354 | * Constants used for limiting early retransmits | |
39236c6e A |
355 | * to 10 per minute. |
356 | */ | |
357 | #define TCP_EARLY_REXMT_WIN (60 * TCP_RETRANSHZ) /* 60 seconds */ | |
358 | #define TCP_EARLY_REXMT_LIMIT 10 | |
9bccf70c | 359 | |
3e170ce0 | 360 | extern void ipfwsyslog( int level, const char *format,...); |
91447636 A |
361 | extern int fw_verbose; |
362 | ||
2d21ac55 | 363 | #if IPFIREWALL |
39236c6e A |
364 | extern void ipfw_stealth_stats_incr_tcp(void); |
365 | ||
91447636 A |
366 | #define log_in_vain_log( a ) { \ |
367 | if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \ | |
368 | ipfwsyslog a ; \ | |
39236c6e A |
369 | } else if ( (log_in_vain == 4 ) && (fw_verbose == 2)) { \ |
370 | ipfw_stealth_stats_incr_tcp(); \ | |
91447636 A |
371 | } \ |
372 | else log a ; \ | |
373 | } | |
2d21ac55 A |
374 | #else |
375 | #define log_in_vain_log( a ) { log a; } | |
376 | #endif | |
377 | ||
6d2010ae A |
378 | int tcp_rcvunackwin = TCPTV_UNACKWIN; |
379 | int tcp_maxrcvidle = TCPTV_MAXRCVIDLE; | |
380 | int tcp_rcvsspktcnt = TCP_RCV_SS_PKTCOUNT; | |
316670eb A |
381 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, rcvsspktcnt, CTLFLAG_RW | CTLFLAG_LOCKED, |
382 | &tcp_rcvsspktcnt, 0, "packets to be seen before receiver stretches acks"); | |
91447636 | 383 | |
39236c6e A |
384 | #define DELAY_ACK(tp, th) \ |
385 | (CC_ALGO(tp)->delay_ack != NULL && CC_ALGO(tp)->delay_ack(tp, th)) | |
91447636 | 386 | |
2d21ac55 | 387 | static int tcp_dropdropablreq(struct socket *head); |
8ad349bb | 388 | static void tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th); |
6d2010ae | 389 | static void update_base_rtt(struct tcpcb *tp, uint32_t rtt); |
6d2010ae A |
390 | void tcp_set_background_cc(struct socket *so); |
391 | void tcp_set_foreground_cc(struct socket *so); | |
392 | static void tcp_set_new_cc(struct socket *so, uint16_t cc_index); | |
316670eb | 393 | static void tcp_bwmeas_check(struct tcpcb *tp); |
6d2010ae A |
394 | |
395 | #if TRAFFIC_MGT | |
396 | void | |
397 | reset_acc_iaj(struct tcpcb *tp) | |
398 | { | |
399 | tp->acc_iaj = 0; | |
400 | tp->iaj_rwintop = 0; | |
316670eb | 401 | CLEAR_IAJ_STATE(tp); |
6d2010ae A |
402 | } |
403 | ||
404 | static inline void | |
405 | update_iaj_state(struct tcpcb *tp, uint32_t size, int rst_size) | |
406 | { | |
407 | if (rst_size > 0) | |
408 | tp->iaj_size = 0; | |
409 | if (tp->iaj_size == 0 || size >= tp->iaj_size) { | |
410 | tp->iaj_size = size; | |
411 | tp->iaj_rcv_ts = tcp_now; | |
412 | tp->iaj_small_pkt = 0; | |
413 | } | |
414 | } | |
415 | ||
39037602 A |
416 | /* For every 32 bit unsigned integer(v), this function will find the |
417 | * largest integer n such that (n*n <= v). This takes at most 16 iterations | |
418 | * irrespective of the value of v and does not involve multiplications. | |
6d2010ae A |
419 | */ |
420 | static inline int | |
39037602 A |
421 | isqrt(unsigned int val) |
422 | { | |
6d2010ae A |
423 | unsigned int sqrt_cache[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}; |
424 | unsigned int temp, g=0, b=0x8000, bshft=15; | |
425 | if ( val <= 100) { | |
426 | for (g = 0; g <= 10; ++g) { | |
427 | if (sqrt_cache[g] > val) { | |
428 | g--; | |
429 | break; | |
430 | } else if (sqrt_cache[g] == val) { | |
431 | break; | |
432 | } | |
433 | } | |
434 | } else { | |
435 | do { | |
436 | temp = (((g << 1) + b) << (bshft--)); | |
437 | if (val >= temp) { | |
438 | g += b; | |
439 | val -= temp; | |
440 | } | |
441 | b >>= 1; | |
442 | } while ( b > 0 && val > 0); | |
443 | } | |
444 | return(g); | |
39037602 | 445 | } |
6d2010ae | 446 | |
39037602 | 447 | /* |
39236c6e A |
448 | * With LRO, roughly estimate the inter arrival time between |
449 | * each sub coalesced packet as an average. Count the delay | |
450 | * cur_iaj to be the delay between the last packet received | |
451 | * and the first packet of the LRO stream. Due to round off errors | |
39037602 | 452 | * cur_iaj may be the same as lro_delay_factor. Averaging has |
39236c6e A |
453 | * round off errors too. lro_delay_factor may be close to 0 |
454 | * in steady state leading to lower values fed to compute_iaj_meat. | |
455 | */ | |
6d2010ae | 456 | void |
39236c6e | 457 | compute_iaj(struct tcpcb *tp, int nlropkts, int lro_delay_factor) |
6d2010ae | 458 | { |
39236c6e A |
459 | uint32_t cur_iaj = tcp_now - tp->iaj_rcv_ts; |
460 | uint32_t timediff = 0; | |
461 | ||
462 | if (cur_iaj >= lro_delay_factor) { | |
463 | cur_iaj = cur_iaj - lro_delay_factor; | |
464 | } | |
465 | ||
466 | compute_iaj_meat(tp, cur_iaj); | |
39037602 | 467 | |
39236c6e A |
468 | if (nlropkts <= 1) |
469 | return; | |
470 | ||
471 | nlropkts--; | |
39037602 | 472 | |
39236c6e A |
473 | timediff = lro_delay_factor/nlropkts; |
474 | ||
39037602 | 475 | while (nlropkts > 0) |
39236c6e | 476 | { |
39037602 | 477 | compute_iaj_meat(tp, timediff); |
39236c6e A |
478 | nlropkts--; |
479 | } | |
480 | } | |
481 | ||
482 | static | |
483 | void compute_iaj_meat(struct tcpcb *tp, uint32_t cur_iaj) | |
484 | { | |
39037602 | 485 | /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds, |
39236c6e | 486 | * throttle the receive window to a minimum of MIN_IAJ_WIN packets |
6d2010ae A |
487 | */ |
488 | #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit) | |
39236c6e A |
489 | #define IAJ_DIV_SHIFT 4 |
490 | #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1)) | |
6d2010ae A |
491 | |
492 | uint32_t allowed_iaj, acc_iaj = 0; | |
6d2010ae A |
493 | |
494 | uint32_t mean, temp; | |
495 | int32_t cur_iaj_dev; | |
39236c6e | 496 | |
39037602 A |
497 | cur_iaj_dev = (cur_iaj - tp->avg_iaj); |
498 | ||
499 | /* Allow a jitter of "allowed_iaj" milliseconds. Some connections | |
500 | * may have a constant jitter more than that. We detect this by | |
39236c6e | 501 | * using standard deviation. |
6d2010ae A |
502 | */ |
503 | allowed_iaj = tp->avg_iaj + tp->std_dev_iaj; | |
504 | if (allowed_iaj < tcp_allowed_iaj) | |
505 | allowed_iaj = tcp_allowed_iaj; | |
506 | ||
39037602 A |
507 | /* Initially when the connection starts, the senders congestion |
508 | * window is small. During this period we avoid throttling a | |
509 | * connection because we do not have a good starting point for | |
510 | * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over | |
39236c6e | 511 | * the first few packets. |
6d2010ae A |
512 | */ |
513 | if (tp->iaj_pktcnt > IAJ_IGNORE_PKTCNT) { | |
514 | if ( cur_iaj <= allowed_iaj ) { | |
515 | if (tp->acc_iaj >= 2) | |
516 | acc_iaj = tp->acc_iaj - 2; | |
517 | else | |
518 | acc_iaj = 0; | |
39037602 | 519 | |
6d2010ae A |
520 | } else { |
521 | acc_iaj = tp->acc_iaj + (cur_iaj - allowed_iaj); | |
522 | } | |
523 | ||
524 | if (acc_iaj > MAX_ACC_IAJ) | |
525 | acc_iaj = MAX_ACC_IAJ; | |
526 | tp->acc_iaj = acc_iaj; | |
527 | } | |
528 | ||
529 | /* Compute weighted average where the history has a weight of | |
39037602 | 530 | * 15 out of 16 and the current value has a weight of 1 out of 16. |
6d2010ae | 531 | * This will make the short-term measurements have more weight. |
39236c6e | 532 | * |
39037602 | 533 | * The addition of 8 will help to round-up the value |
39236c6e | 534 | * instead of round-down |
6d2010ae | 535 | */ |
39037602 | 536 | tp->avg_iaj = (((tp->avg_iaj << IAJ_DIV_SHIFT) - tp->avg_iaj) |
39236c6e | 537 | + cur_iaj + IAJ_ROUNDUP_CONST) >> IAJ_DIV_SHIFT; |
6d2010ae A |
538 | |
539 | /* Compute Root-mean-square of deviation where mean is a weighted | |
39037602 | 540 | * average as described above. |
6d2010ae A |
541 | */ |
542 | temp = tp->std_dev_iaj * tp->std_dev_iaj; | |
39037602 A |
543 | mean = (((temp << IAJ_DIV_SHIFT) - temp) |
544 | + (cur_iaj_dev * cur_iaj_dev) | |
39236c6e | 545 | + IAJ_ROUNDUP_CONST) >> IAJ_DIV_SHIFT; |
39037602 | 546 | |
6d2010ae A |
547 | tp->std_dev_iaj = isqrt(mean); |
548 | ||
39037602 | 549 | DTRACE_TCP3(iaj, struct tcpcb *, tp, uint32_t, cur_iaj, |
39236c6e | 550 | uint32_t, allowed_iaj); |
6d2010ae A |
551 | |
552 | return; | |
553 | } | |
554 | #endif /* TRAFFIC_MGT */ | |
9bccf70c | 555 | |
39037602 | 556 | /* Check if enough amount of data has been acknowledged since |
316670eb A |
557 | * bw measurement was started |
558 | */ | |
559 | static void | |
560 | tcp_bwmeas_check(struct tcpcb *tp) | |
561 | { | |
562 | int32_t bw_meas_bytes; | |
563 | uint32_t bw, bytes, elapsed_time; | |
564 | bw_meas_bytes = tp->snd_una - tp->t_bwmeas->bw_start; | |
565 | if ((tp->t_flagsext & TF_BWMEAS_INPROGRESS) != 0 && | |
566 | bw_meas_bytes >= (int32_t)(tp->t_bwmeas->bw_size)) { | |
567 | bytes = bw_meas_bytes; | |
568 | elapsed_time = tcp_now - tp->t_bwmeas->bw_ts; | |
569 | if (elapsed_time > 0) { | |
570 | bw = bytes / elapsed_time; | |
571 | if ( bw > 0) { | |
572 | if (tp->t_bwmeas->bw_sndbw > 0) { | |
39037602 | 573 | tp->t_bwmeas->bw_sndbw = |
316670eb A |
574 | (((tp->t_bwmeas->bw_sndbw << 3) - tp->t_bwmeas->bw_sndbw) + bw) >> 3; |
575 | } else { | |
576 | tp->t_bwmeas->bw_sndbw = bw; | |
577 | } | |
578 | } | |
579 | } | |
580 | tp->t_flagsext &= ~(TF_BWMEAS_INPROGRESS); | |
581 | } | |
582 | } | |
583 | ||
9bccf70c | 584 | static int |
39236c6e A |
585 | tcp_reass(struct tcpcb *tp, struct tcphdr *th, int *tlenp, struct mbuf *m, |
586 | struct ifnet *ifp) | |
1c79356b | 587 | { |
9bccf70c A |
588 | struct tseg_qent *q; |
589 | struct tseg_qent *p = NULL; | |
590 | struct tseg_qent *nq; | |
8ad349bb | 591 | struct tseg_qent *te = NULL; |
39236c6e A |
592 | struct inpcb *inp = tp->t_inpcb; |
593 | struct socket *so = inp->inp_socket; | |
594 | int flags = 0; | |
91447636 | 595 | int dowakeup = 0; |
39236c6e A |
596 | struct mbuf *oodata = NULL; |
597 | int copy_oodata = 0; | |
fe8ab488 | 598 | u_int16_t qlimit; |
39236c6e A |
599 | boolean_t cell = IFNET_IS_CELLULAR(ifp); |
600 | boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
fe8ab488 | 601 | boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp)); |
3e170ce0 | 602 | boolean_t dsack_set = FALSE; |
1c79356b A |
603 | |
604 | /* | |
605 | * Call with th==0 after become established to | |
606 | * force pre-ESTABLISHED data up to user socket. | |
607 | */ | |
8ad349bb | 608 | if (th == NULL) |
1c79356b | 609 | goto present; |
39037602 | 610 | |
fe8ab488 | 611 | /* |
39037602 A |
612 | * If the reassembly queue already has entries or if we are going |
613 | * to add a new one, then the connection has reached a loss state. | |
6d2010ae A |
614 | * Reset the stretch-ack algorithm at this point. |
615 | */ | |
fe8ab488 | 616 | tcp_reset_stretch_ack(tp); |
6d2010ae A |
617 | |
618 | #if TRAFFIC_MGT | |
619 | if (tp->acc_iaj > 0) | |
620 | reset_acc_iaj(tp); | |
39037602 | 621 | #endif /* TRAFFIC_MGT */ |
1c79356b | 622 | |
e5568f75 A |
623 | /* |
624 | * Limit the number of segments in the reassembly queue to prevent | |
625 | * holding on to too many segments (and thus running out of mbufs). | |
626 | * Make sure to let the missing segment through which caused this | |
627 | * queue. Always keep one global queue entry spare to be able to | |
628 | * process the missing segment. | |
629 | */ | |
fe8ab488 | 630 | qlimit = min(max(100, so->so_rcv.sb_hiwat >> 10), |
39037602 | 631 | (TCP_AUTORCVBUF_MAX(ifp) >> 10)); |
e5568f75 | 632 | if (th->th_seq != tp->rcv_nxt && |
fe8ab488 | 633 | (tp->t_reassqlen + 1) >= qlimit) { |
e5568f75 A |
634 | tcp_reass_overflows++; |
635 | tcpstat.tcps_rcvmemdrop++; | |
636 | m_freem(m); | |
2d21ac55 | 637 | *tlenp = 0; |
e5568f75 A |
638 | return (0); |
639 | } | |
640 | ||
9bccf70c | 641 | /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */ |
fe8ab488 | 642 | te = (struct tseg_qent *) zalloc(tcp_reass_zone); |
9bccf70c | 643 | if (te == NULL) { |
1c79356b A |
644 | tcpstat.tcps_rcvmemdrop++; |
645 | m_freem(m); | |
646 | return (0); | |
647 | } | |
fe8ab488 | 648 | tp->t_reassqlen++; |
1c79356b A |
649 | |
650 | /* | |
651 | * Find a segment which begins after this one does. | |
652 | */ | |
9bccf70c A |
653 | LIST_FOREACH(q, &tp->t_segq, tqe_q) { |
654 | if (SEQ_GT(q->tqe_th->th_seq, th->th_seq)) | |
1c79356b | 655 | break; |
9bccf70c A |
656 | p = q; |
657 | } | |
1c79356b A |
658 | |
659 | /* | |
660 | * If there is a preceding segment, it may provide some of | |
661 | * our data already. If so, drop the data from the incoming | |
662 | * segment. If it provides all of our data, drop us. | |
663 | */ | |
664 | if (p != NULL) { | |
3e170ce0 | 665 | int i; |
1c79356b | 666 | /* conversion to int (in i) handles seq wraparound */ |
9bccf70c | 667 | i = p->tqe_th->th_seq + p->tqe_len - th->th_seq; |
1c79356b | 668 | if (i > 0) { |
3e170ce0 A |
669 | if (TCP_DSACK_ENABLED(tp) && i > 1) { |
670 | /* | |
671 | * Note duplicate data sequnce numbers | |
672 | * to report in DSACK option | |
673 | */ | |
674 | tp->t_dsack_lseq = th->th_seq; | |
675 | tp->t_dsack_rseq = th->th_seq + | |
676 | min(i, *tlenp); | |
677 | ||
678 | /* | |
679 | * Report only the first part of partial/ | |
680 | * non-contiguous duplicate sequence space | |
681 | */ | |
682 | dsack_set = TRUE; | |
683 | } | |
9bccf70c | 684 | if (i >= *tlenp) { |
1c79356b | 685 | tcpstat.tcps_rcvduppack++; |
9bccf70c | 686 | tcpstat.tcps_rcvdupbyte += *tlenp; |
6d2010ae | 687 | if (nstat_collect) { |
fe8ab488 A |
688 | nstat_route_rx(inp->inp_route.ro_rt, |
689 | 1, *tlenp, | |
690 | NSTAT_RX_FLAG_DUPLICATE); | |
691 | INP_ADD_STAT(inp, cell, wifi, wired, | |
692 | rxpackets, 1); | |
693 | INP_ADD_STAT(inp, cell, wifi, wired, | |
694 | rxbytes, *tlenp); | |
6d2010ae A |
695 | tp->t_stat.rxduplicatebytes += *tlenp; |
696 | } | |
1c79356b | 697 | m_freem(m); |
6d2010ae | 698 | zfree(tcp_reass_zone, te); |
39236c6e | 699 | te = NULL; |
fe8ab488 | 700 | tp->t_reassqlen--; |
1c79356b A |
701 | /* |
702 | * Try to present any queued data | |
703 | * at the left window edge to the user. | |
704 | * This is needed after the 3-WHS | |
705 | * completes. | |
706 | */ | |
39236c6e | 707 | goto present; |
1c79356b A |
708 | } |
709 | m_adj(m, i); | |
9bccf70c | 710 | *tlenp -= i; |
1c79356b A |
711 | th->th_seq += i; |
712 | } | |
713 | } | |
4bd07ac2 | 714 | tp->t_rcvoopack++; |
1c79356b | 715 | tcpstat.tcps_rcvoopack++; |
9bccf70c | 716 | tcpstat.tcps_rcvoobyte += *tlenp; |
6d2010ae | 717 | if (nstat_collect) { |
fe8ab488 A |
718 | nstat_route_rx(inp->inp_route.ro_rt, 1, *tlenp, |
719 | NSTAT_RX_FLAG_OUT_OF_ORDER); | |
720 | INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1); | |
721 | INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, *tlenp); | |
6d2010ae A |
722 | tp->t_stat.rxoutoforderbytes += *tlenp; |
723 | } | |
1c79356b A |
724 | |
725 | /* | |
726 | * While we overlap succeeding segments trim them or, | |
727 | * if they are completely covered, dequeue them. | |
728 | */ | |
729 | while (q) { | |
3e170ce0 | 730 | int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq; |
1c79356b A |
731 | if (i <= 0) |
732 | break; | |
3e170ce0 A |
733 | |
734 | /* | |
735 | * Report only the first part of partial/non-contiguous | |
736 | * duplicate segment in dsack option. The variable | |
737 | * dsack_set will be true if a previous entry has some of | |
738 | * the duplicate sequence space. | |
739 | */ | |
740 | if (TCP_DSACK_ENABLED(tp) && i > 1 && !dsack_set) { | |
741 | if (tp->t_dsack_lseq == 0) { | |
742 | tp->t_dsack_lseq = q->tqe_th->th_seq; | |
743 | tp->t_dsack_rseq = | |
744 | tp->t_dsack_lseq + min(i, q->tqe_len); | |
745 | } else { | |
746 | /* | |
747 | * this segment overlaps data in multple | |
748 | * entries in the reassembly queue, move | |
749 | * the right sequence number further. | |
750 | */ | |
751 | tp->t_dsack_rseq = | |
752 | tp->t_dsack_rseq + min(i, q->tqe_len); | |
753 | } | |
754 | } | |
9bccf70c A |
755 | if (i < q->tqe_len) { |
756 | q->tqe_th->th_seq += i; | |
757 | q->tqe_len -= i; | |
758 | m_adj(q->tqe_m, i); | |
1c79356b A |
759 | break; |
760 | } | |
9bccf70c A |
761 | |
762 | nq = LIST_NEXT(q, tqe_q); | |
763 | LIST_REMOVE(q, tqe_q); | |
764 | m_freem(q->tqe_m); | |
6d2010ae | 765 | zfree(tcp_reass_zone, q); |
fe8ab488 | 766 | tp->t_reassqlen--; |
1c79356b A |
767 | q = nq; |
768 | } | |
769 | ||
9bccf70c A |
770 | /* Insert the new segment queue entry into place. */ |
771 | te->tqe_m = m; | |
772 | te->tqe_th = th; | |
773 | te->tqe_len = *tlenp; | |
1c79356b | 774 | |
1c79356b | 775 | if (p == NULL) { |
9bccf70c | 776 | LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q); |
1c79356b | 777 | } else { |
9bccf70c | 778 | LIST_INSERT_AFTER(p, te, tqe_q); |
1c79356b A |
779 | } |
780 | ||
39037602 | 781 | /* |
39236c6e | 782 | * New out-of-order data exists, and is pointed to by |
39037602 A |
783 | * queue entry te. Set copy_oodata to 1 so out-of-order data |
784 | * can be copied off to sockbuf after in-order data | |
39236c6e A |
785 | * is copied off. |
786 | */ | |
787 | if (!(so->so_state & SS_CANTRCVMORE)) | |
788 | copy_oodata = 1; | |
789 | ||
1c79356b A |
790 | present: |
791 | /* | |
792 | * Present data to user, advancing rcv_nxt through | |
793 | * completed sequence space. | |
794 | */ | |
795 | if (!TCPS_HAVEESTABLISHED(tp->t_state)) | |
796 | return (0); | |
9bccf70c | 797 | q = LIST_FIRST(&tp->t_segq); |
316670eb A |
798 | if (!q || q->tqe_th->th_seq != tp->rcv_nxt) { |
799 | /* Stop using LRO once out of order packets arrive */ | |
800 | if (tp->t_flagsext & TF_LRO_OFFLOADED) { | |
39236c6e | 801 | tcp_lro_remove_state(inp->inp_laddr, inp->inp_faddr, |
316670eb | 802 | th->th_dport, th->th_sport); |
39037602 | 803 | tp->t_flagsext &= ~TF_LRO_OFFLOADED; |
316670eb | 804 | } |
39236c6e A |
805 | |
806 | /* | |
39037602 | 807 | * continue processing if out-of-order data |
39236c6e A |
808 | * can be delivered |
809 | */ | |
810 | if (q && (so->so_flags & SOF_ENABLE_MSGS)) | |
811 | goto msg_unordered_delivery; | |
812 | ||
1c79356b | 813 | return (0); |
fe8ab488 A |
814 | } |
815 | ||
39037602 A |
816 | /* |
817 | * If there is already another thread doing reassembly for this | |
818 | * connection, it is better to let it finish the job -- | |
819 | * (radar 16316196) | |
820 | */ | |
821 | if (tp->t_flagsext & TF_REASS_INPROG) | |
822 | return (0); | |
823 | ||
824 | tp->t_flagsext |= TF_REASS_INPROG; | |
fe8ab488 A |
825 | /* lost packet was recovered, so ooo data can be returned */ |
826 | tcpstat.tcps_recovered_pkts++; | |
827 | ||
1c79356b | 828 | do { |
9bccf70c A |
829 | tp->rcv_nxt += q->tqe_len; |
830 | flags = q->tqe_th->th_flags & TH_FIN; | |
9bccf70c | 831 | LIST_REMOVE(q, tqe_q); |
39236c6e | 832 | if (so->so_state & SS_CANTRCVMORE) { |
9bccf70c | 833 | m_freem(q->tqe_m); |
39236c6e | 834 | } else { |
6d2010ae | 835 | so_recv_data_stat(so, q->tqe_m, 0); /* XXXX */ |
39236c6e | 836 | if (so->so_flags & SOF_ENABLE_MSGS) { |
39037602 A |
837 | /* |
838 | * Append the inorder data as a message to the | |
839 | * receive socket buffer. Also check to see if | |
840 | * the data we are about to deliver is the same | |
841 | * data that we wanted to pass up to the user | |
842 | * out of order. If so, reset copy_oodata -- | |
39236c6e A |
843 | * the received data filled a gap, and |
844 | * is now in order! | |
845 | */ | |
846 | if (q == te) | |
847 | copy_oodata = 0; | |
848 | } | |
39037602 | 849 | if (sbappendstream_rcvdemux(so, q->tqe_m, |
39236c6e | 850 | q->tqe_th->th_seq - (tp->irs + 1), 0)) |
91447636 | 851 | dowakeup = 1; |
39037602 A |
852 | if (tp->t_flagsext & TF_LRO_OFFLOADED) { |
853 | tcp_update_lro_seq(tp->rcv_nxt, | |
39236c6e A |
854 | inp->inp_laddr, inp->inp_faddr, |
855 | th->th_dport, th->th_sport); | |
316670eb | 856 | } |
91447636 | 857 | } |
6d2010ae | 858 | zfree(tcp_reass_zone, q); |
fe8ab488 | 859 | tp->t_reassqlen--; |
39037602 | 860 | q = LIST_FIRST(&tp->t_segq); |
9bccf70c | 861 | } while (q && q->tqe_th->th_seq == tp->rcv_nxt); |
39037602 | 862 | tp->t_flagsext &= ~TF_REASS_INPROG; |
9bccf70c | 863 | |
1c79356b | 864 | #if INET6 |
39236c6e | 865 | if ((inp->inp_vflag & INP_IPV6) != 0) { |
39037602 | 866 | |
9bccf70c | 867 | KERNEL_DEBUG(DBG_LAYER_BEG, |
39236c6e A |
868 | ((inp->inp_fport << 16) | inp->inp_lport), |
869 | (((inp->in6p_laddr.s6_addr16[0] & 0xffff) << 16) | | |
870 | (inp->in6p_faddr.s6_addr16[0] & 0xffff)), | |
9bccf70c A |
871 | 0,0,0); |
872 | } | |
873 | else | |
1c79356b | 874 | #endif |
9bccf70c A |
875 | { |
876 | KERNEL_DEBUG(DBG_LAYER_BEG, | |
39236c6e A |
877 | ((inp->inp_fport << 16) | inp->inp_lport), |
878 | (((inp->inp_laddr.s_addr & 0xffff) << 16) | | |
879 | (inp->inp_faddr.s_addr & 0xffff)), | |
9bccf70c | 880 | 0,0,0); |
91447636 | 881 | } |
39236c6e A |
882 | |
883 | msg_unordered_delivery: | |
884 | /* Deliver out-of-order data as a message */ | |
885 | if (te && (so->so_flags & SOF_ENABLE_MSGS) && copy_oodata && te->tqe_len) { | |
39037602 A |
886 | /* |
887 | * make a copy of the mbuf to be delivered up to | |
39236c6e A |
888 | * the user, and add it to the sockbuf |
889 | */ | |
890 | oodata = m_copym(te->tqe_m, 0, M_COPYALL, M_DONTWAIT); | |
891 | if (oodata != NULL) { | |
39037602 | 892 | if (sbappendmsgstream_rcv(&so->so_rcv, oodata, |
39236c6e A |
893 | te->tqe_th->th_seq - (tp->irs + 1), 1)) { |
894 | dowakeup = 1; | |
895 | tcpstat.tcps_msg_unopkts++; | |
896 | } else { | |
897 | tcpstat.tcps_msg_unoappendfail++; | |
898 | } | |
899 | } | |
900 | } | |
901 | ||
91447636 A |
902 | if (dowakeup) |
903 | sorwakeup(so); /* done with socket lock held */ | |
1c79356b A |
904 | return (flags); |
905 | } | |
906 | ||
2d21ac55 | 907 | /* |
3e170ce0 A |
908 | * Reduce congestion window -- used when ECN is seen or when a tail loss |
909 | * probe recovers the last packet. | |
2d21ac55 A |
910 | */ |
911 | static void | |
912 | tcp_reduce_congestion_window( | |
316670eb | 913 | struct tcpcb *tp) |
2d21ac55 | 914 | { |
6d2010ae A |
915 | /* |
916 | * If the current tcp cc module has | |
917 | * defined a hook for tasks to run | |
918 | * before entering FR, call it | |
919 | */ | |
920 | if (CC_ALGO(tp)->pre_fr != NULL) | |
316670eb | 921 | CC_ALGO(tp)->pre_fr(tp); |
2d21ac55 | 922 | ENTER_FASTRECOVERY(tp); |
3e170ce0 A |
923 | if (tp->t_flags & TF_SENTFIN) |
924 | tp->snd_recover = tp->snd_max - 1; | |
925 | else | |
926 | tp->snd_recover = tp->snd_max; | |
2d21ac55 | 927 | tp->t_timer[TCPT_REXMT] = 0; |
fe8ab488 | 928 | tp->t_timer[TCPT_PTO] = 0; |
2d21ac55 | 929 | tp->t_rtttime = 0; |
3e170ce0 A |
930 | if (tp->t_flagsext & TF_CWND_NONVALIDATED) { |
931 | tcp_cc_adjust_nonvalidated_cwnd(tp); | |
932 | } else { | |
933 | tp->snd_cwnd = tp->snd_ssthresh + | |
934 | tp->t_maxseg * tcprexmtthresh; | |
935 | } | |
2d21ac55 A |
936 | } |
937 | ||
39236c6e | 938 | /* |
3e170ce0 A |
939 | * This function is called upon reception of data on a socket. It's purpose is |
940 | * to handle the adaptive keepalive timers that monitor whether the connection | |
941 | * is making progress. First the adaptive read-timer, second the TFO probe-timer. | |
942 | * | |
943 | * The application wants to get an event if there is a stall during read. | |
944 | * Set the initial keepalive timeout to be equal to twice RTO. | |
945 | * | |
946 | * If the outgoing interface is in marginal conditions, we need to | |
947 | * enable read probes for that too. | |
39236c6e A |
948 | */ |
949 | static inline void | |
3e170ce0 | 950 | tcp_adaptive_rwtimo_check(struct tcpcb *tp, int tlen) |
39236c6e | 951 | { |
3e170ce0 A |
952 | struct ifnet *outifp = tp->t_inpcb->inp_last_outifp; |
953 | ||
954 | if ((tp->t_adaptive_rtimo > 0 || | |
955 | (outifp != NULL && | |
956 | (outifp->if_eflags & IFEF_PROBE_CONNECTIVITY))) | |
957 | && tlen > 0 && | |
958 | tp->t_state == TCPS_ESTABLISHED) { | |
959 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, | |
39236c6e A |
960 | (TCP_REXMTVAL(tp) << 1)); |
961 | tp->t_flagsext |= TF_DETECT_READSTALL; | |
962 | tp->t_rtimo_probes = 0; | |
963 | } | |
964 | } | |
965 | ||
966 | inline void | |
967 | tcp_keepalive_reset(struct tcpcb *tp) | |
968 | { | |
39037602 | 969 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, |
39236c6e A |
970 | TCP_CONN_KEEPIDLE(tp)); |
971 | tp->t_flagsext &= ~(TF_DETECT_READSTALL); | |
972 | tp->t_rtimo_probes = 0; | |
973 | } | |
9bccf70c | 974 | |
1c79356b A |
975 | /* |
976 | * TCP input routine, follows pages 65-76 of the | |
977 | * protocol specification dated September, 1981 very closely. | |
978 | */ | |
979 | #if INET6 | |
980 | int | |
6d2010ae | 981 | tcp6_input(struct mbuf **mp, int *offp, int proto) |
1c79356b | 982 | { |
6d2010ae | 983 | #pragma unused(proto) |
39037602 | 984 | struct mbuf *m = *mp; |
39236c6e A |
985 | uint32_t ia6_flags; |
986 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
9bccf70c | 987 | |
91447636 | 988 | IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), return IPPROTO_DONE); |
9bccf70c | 989 | |
316670eb A |
990 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
991 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
992 | ||
9bccf70c A |
993 | /* |
994 | * draft-itojun-ipv6-tcp-to-anycast | |
995 | * better place to put this in? | |
996 | */ | |
39236c6e A |
997 | if (ip6_getdstifaddr_info(m, NULL, &ia6_flags) == 0) { |
998 | if (ia6_flags & IN6_IFF_ANYCAST) { | |
6d2010ae | 999 | struct ip6_hdr *ip6; |
9bccf70c | 1000 | |
6d2010ae A |
1001 | ip6 = mtod(m, struct ip6_hdr *); |
1002 | icmp6_error(m, ICMP6_DST_UNREACH, | |
1003 | ICMP6_DST_UNREACH_ADDR, | |
9bccf70c | 1004 | (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); |
39236c6e A |
1005 | |
1006 | IF_TCP_STATINC(ifp, icmp6unreach); | |
1007 | ||
6d2010ae A |
1008 | return (IPPROTO_DONE); |
1009 | } | |
9bccf70c A |
1010 | } |
1011 | ||
1012 | tcp_input(m, *offp); | |
6d2010ae | 1013 | return (IPPROTO_DONE); |
1c79356b A |
1014 | } |
1015 | #endif | |
1016 | ||
316670eb | 1017 | /* Depending on the usage of mbuf space in the system, this function |
39037602 | 1018 | * will return true or false. This is used to determine if a socket |
316670eb A |
1019 | * buffer can take more memory from the system for auto-tuning or not. |
1020 | */ | |
1021 | u_int8_t | |
1022 | tcp_cansbgrow(struct sockbuf *sb) | |
1023 | { | |
1024 | /* Calculate the host level space limit in terms of MSIZE buffers. | |
1025 | * We can use a maximum of half of the available mbuf space for | |
39037602 | 1026 | * socket buffers. |
316670eb A |
1027 | */ |
1028 | u_int32_t mblim = ((nmbclusters >> 1) << (MCLSHIFT - MSIZESHIFT)); | |
1029 | ||
1030 | /* Calculate per sb limit in terms of bytes. We optimize this limit | |
1031 | * for upto 16 socket buffers. | |
1032 | */ | |
1033 | ||
1034 | u_int32_t sbspacelim = ((nmbclusters >> 4) << MCLSHIFT); | |
1035 | ||
1036 | if ((total_sbmb_cnt < mblim) && | |
1037 | (sb->sb_hiwat < sbspacelim)) { | |
1038 | return(1); | |
fe8ab488 A |
1039 | } else { |
1040 | OSIncrementAtomic64(&sbmb_limreached); | |
316670eb A |
1041 | } |
1042 | return(0); | |
1043 | } | |
1044 | ||
39236c6e A |
1045 | static void |
1046 | tcp_sbrcv_reserve(struct tcpcb *tp, struct sockbuf *sbrcv, | |
39037602 | 1047 | u_int32_t newsize, u_int32_t idealsize, u_int32_t rcvbuf_max) |
39236c6e | 1048 | { |
316670eb | 1049 | /* newsize should not exceed max */ |
39037602 | 1050 | newsize = min(newsize, rcvbuf_max); |
316670eb | 1051 | |
39037602 A |
1052 | /* The receive window scale negotiated at the |
1053 | * beginning of the connection will also set a | |
316670eb A |
1054 | * limit on the socket buffer size |
1055 | */ | |
1056 | newsize = min(newsize, TCP_MAXWIN << tp->rcv_scale); | |
1057 | ||
1058 | /* Set new socket buffer size */ | |
1059 | if (newsize > sbrcv->sb_hiwat && | |
1060 | (sbreserve(sbrcv, newsize) == 1)) { | |
39037602 A |
1061 | sbrcv->sb_idealsize = min(max(sbrcv->sb_idealsize, |
1062 | (idealsize != 0) ? idealsize : newsize), rcvbuf_max); | |
316670eb | 1063 | |
39037602 A |
1064 | /* Again check the limit set by the advertised |
1065 | * window scale | |
316670eb | 1066 | */ |
39037602 | 1067 | sbrcv->sb_idealsize = min(sbrcv->sb_idealsize, |
316670eb A |
1068 | TCP_MAXWIN << tp->rcv_scale); |
1069 | } | |
1070 | } | |
1071 | ||
39037602 | 1072 | /* |
316670eb A |
1073 | * This function is used to grow a receive socket buffer. It |
1074 | * will take into account system-level memory usage and the | |
1075 | * bandwidth available on the link to make a decision. | |
1076 | */ | |
1077 | static void | |
39037602 A |
1078 | tcp_sbrcv_grow(struct tcpcb *tp, struct sockbuf *sbrcv, |
1079 | struct tcpopt *to, u_int32_t pktlen, u_int32_t rcvbuf_max) | |
3e170ce0 A |
1080 | { |
1081 | struct socket *so = sbrcv->sb_so; | |
39037602 | 1082 | |
39236c6e A |
1083 | /* |
1084 | * Do not grow the receive socket buffer if | |
1085 | * - auto resizing is disabled, globally or on this socket | |
fe8ab488 | 1086 | * - the high water mark already reached the maximum |
39037602 | 1087 | * - the stream is in background and receive side is being |
39236c6e A |
1088 | * throttled |
1089 | * - if there are segments in reassembly queue indicating loss, | |
39037602 | 1090 | * do not need to increase recv window during recovery as more |
fe8ab488 A |
1091 | * data is not going to be sent. A duplicate ack sent during |
1092 | * recovery should not change the receive window | |
39236c6e | 1093 | */ |
316670eb A |
1094 | if (tcp_do_autorcvbuf == 0 || |
1095 | (sbrcv->sb_flags & SB_AUTOSIZE) == 0 || | |
1096 | tcp_cansbgrow(sbrcv) == 0 || | |
39037602 | 1097 | sbrcv->sb_hiwat >= rcvbuf_max || |
39236c6e | 1098 | (tp->t_flagsext & TF_RECV_THROTTLE) || |
3e170ce0 | 1099 | (so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) || |
39236c6e | 1100 | !LIST_EMPTY(&tp->t_segq)) { |
316670eb A |
1101 | /* Can not resize the socket buffer, just return */ |
1102 | goto out; | |
1103 | } | |
1104 | ||
1105 | if (TSTMP_GT(tcp_now, | |
1106 | tp->rfbuf_ts + TCPTV_RCVBUFIDLE)) { | |
1107 | /* If there has been an idle period in the | |
1108 | * connection, just restart the measurement | |
1109 | */ | |
1110 | goto out; | |
1111 | } | |
1112 | ||
39236c6e | 1113 | if (!TSTMP_SUPPORTED(tp)) { |
316670eb A |
1114 | /* |
1115 | * Timestamp option is not supported on this connection. | |
1116 | * If the connection reached a state to indicate that | |
1117 | * the receive socket buffer needs to grow, increase | |
fe8ab488 A |
1118 | * the high water mark. |
1119 | */ | |
39037602 | 1120 | if (TSTMP_GEQ(tcp_now, |
316670eb A |
1121 | tp->rfbuf_ts + TCPTV_RCVNOTS_QUANTUM)) { |
1122 | if (tp->rfbuf_cnt >= TCP_RCVNOTS_BYTELEVEL) { | |
1123 | tcp_sbrcv_reserve(tp, sbrcv, | |
39037602 A |
1124 | tcp_autorcvbuf_max, 0, |
1125 | tcp_autorcvbuf_max); | |
316670eb A |
1126 | } |
1127 | goto out; | |
1128 | } else { | |
1129 | tp->rfbuf_cnt += pktlen; | |
1130 | return; | |
fe8ab488 | 1131 | } |
316670eb | 1132 | } else if (to->to_tsecr != 0) { |
fe8ab488 A |
1133 | /* |
1134 | * If the timestamp shows that one RTT has | |
316670eb A |
1135 | * completed, we can stop counting the |
1136 | * bytes. Here we consider increasing | |
fe8ab488 A |
1137 | * the socket buffer if the bandwidth measured in |
1138 | * last rtt, is more than half of sb_hiwat, this will | |
1139 | * help to scale the buffer according to the bandwidth | |
1140 | * on the link. | |
316670eb A |
1141 | */ |
1142 | if (TSTMP_GEQ(to->to_tsecr, tp->rfbuf_ts)) { | |
fe8ab488 A |
1143 | if (tp->rfbuf_cnt > (sbrcv->sb_hiwat - |
1144 | (sbrcv->sb_hiwat >> 1))) { | |
1145 | int32_t rcvbuf_inc, min_incr; | |
316670eb | 1146 | /* |
fe8ab488 A |
1147 | * Increment the receive window by a |
1148 | * multiple of maximum sized segments. | |
39037602 | 1149 | * This will prevent a connection from |
fe8ab488 A |
1150 | * sending smaller segments on wire if it |
1151 | * is limited by the receive window. | |
316670eb | 1152 | * |
fe8ab488 | 1153 | * Set the ideal size based on current |
39037602 | 1154 | * bandwidth measurements. We set the |
fe8ab488 A |
1155 | * ideal size on receive socket buffer to |
1156 | * be twice the bandwidth delay product. | |
1157 | */ | |
1158 | rcvbuf_inc = (tp->rfbuf_cnt << 1) | |
1159 | - sbrcv->sb_hiwat; | |
1160 | ||
1161 | /* | |
1162 | * Make the increment equal to 8 segments | |
1163 | * at least | |
316670eb | 1164 | */ |
fe8ab488 A |
1165 | min_incr = tp->t_maxseg << tcp_autorcvbuf_inc_shift; |
1166 | if (rcvbuf_inc < min_incr) | |
39037602 | 1167 | rcvbuf_inc = min_incr; |
fe8ab488 | 1168 | |
39037602 | 1169 | rcvbuf_inc = |
fe8ab488 | 1170 | (rcvbuf_inc / tp->t_maxseg) * tp->t_maxseg; |
316670eb | 1171 | tcp_sbrcv_reserve(tp, sbrcv, |
39037602 A |
1172 | sbrcv->sb_hiwat + rcvbuf_inc, |
1173 | (tp->rfbuf_cnt * 2), rcvbuf_max); | |
316670eb A |
1174 | } |
1175 | goto out; | |
1176 | } else { | |
1177 | tp->rfbuf_cnt += pktlen; | |
1178 | return; | |
1179 | } | |
1180 | } | |
1181 | out: | |
1182 | /* Restart the measurement */ | |
1183 | tp->rfbuf_ts = 0; | |
1184 | tp->rfbuf_cnt = 0; | |
1185 | return; | |
1186 | } | |
1187 | ||
1188 | /* This function will trim the excess space added to the socket buffer | |
1189 | * to help a slow-reading app. The ideal-size of a socket buffer depends | |
39037602 | 1190 | * on the link bandwidth or it is set by an application and we aim to |
316670eb A |
1191 | * reach that size. |
1192 | */ | |
1193 | void | |
39037602 A |
1194 | tcp_sbrcv_trim(struct tcpcb *tp, struct sockbuf *sbrcv) |
1195 | { | |
316670eb A |
1196 | if (tcp_do_autorcvbuf == 1 && sbrcv->sb_idealsize > 0 && |
1197 | sbrcv->sb_hiwat > sbrcv->sb_idealsize) { | |
1198 | int32_t trim; | |
1199 | /* compute the difference between ideal and current sizes */ | |
1200 | u_int32_t diff = sbrcv->sb_hiwat - sbrcv->sb_idealsize; | |
1201 | ||
1202 | /* Compute the maximum advertised window for | |
1203 | * this connection. | |
1204 | */ | |
1205 | u_int32_t advwin = tp->rcv_adv - tp->rcv_nxt; | |
39037602 | 1206 | |
316670eb A |
1207 | /* How much can we trim the receive socket buffer? |
1208 | * 1. it can not be trimmed beyond the max rcv win advertised | |
39037602 | 1209 | * 2. if possible, leave 1/16 of bandwidth*delay to |
316670eb A |
1210 | * avoid closing the win completely |
1211 | */ | |
1212 | u_int32_t leave = max(advwin, (sbrcv->sb_idealsize >> 4)); | |
1213 | ||
1214 | /* Sometimes leave can be zero, in that case leave at least | |
1215 | * a few segments worth of space. | |
1216 | */ | |
1217 | if (leave == 0) | |
1218 | leave = tp->t_maxseg << tcp_autorcvbuf_inc_shift; | |
39037602 | 1219 | |
316670eb A |
1220 | trim = sbrcv->sb_hiwat - (sbrcv->sb_cc + leave); |
1221 | trim = imin(trim, (int32_t)diff); | |
1222 | ||
1223 | if (trim > 0) | |
1224 | sbreserve(sbrcv, (sbrcv->sb_hiwat - trim)); | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | /* We may need to trim the send socket buffer size for two reasons: | |
1229 | * 1. if the rtt seen on the connection is climbing up, we do not | |
1230 | * want to fill the buffers any more. | |
1231 | * 2. if the congestion win on the socket backed off, there is no need | |
1232 | * to hold more mbufs for that connection than what the cwnd will allow. | |
1233 | */ | |
1234 | void | |
39037602 A |
1235 | tcp_sbsnd_trim(struct sockbuf *sbsnd) |
1236 | { | |
1237 | if (tcp_do_autosendbuf == 1 && | |
1238 | ((sbsnd->sb_flags & (SB_AUTOSIZE | SB_TRIM)) == | |
316670eb A |
1239 | (SB_AUTOSIZE | SB_TRIM)) && |
1240 | (sbsnd->sb_idealsize > 0) && | |
1241 | (sbsnd->sb_hiwat > sbsnd->sb_idealsize)) { | |
1242 | u_int32_t trim = 0; | |
1243 | if (sbsnd->sb_cc <= sbsnd->sb_idealsize) { | |
1244 | trim = sbsnd->sb_hiwat - sbsnd->sb_idealsize; | |
1245 | } else { | |
1246 | trim = sbsnd->sb_hiwat - sbsnd->sb_cc; | |
1247 | } | |
1248 | sbreserve(sbsnd, (sbsnd->sb_hiwat - trim)); | |
1249 | } | |
1250 | if (sbsnd->sb_hiwat <= sbsnd->sb_idealsize) | |
1251 | sbsnd->sb_flags &= ~(SB_TRIM); | |
1252 | } | |
1253 | ||
39037602 | 1254 | /* |
316670eb A |
1255 | * If timestamp option was not negotiated on this connection |
1256 | * and this connection is on the receiving side of a stream | |
1257 | * then we can not measure the delay on the link accurately. | |
1258 | * Instead of enabling automatic receive socket buffer | |
1259 | * resizing, just give more space to the receive socket buffer. | |
1260 | */ | |
39037602 A |
1261 | static inline void |
1262 | tcp_sbrcv_tstmp_check(struct tcpcb *tp) | |
1263 | { | |
316670eb A |
1264 | struct socket *so = tp->t_inpcb->inp_socket; |
1265 | u_int32_t newsize = 2 * tcp_recvspace; | |
1266 | struct sockbuf *sbrcv = &so->so_rcv; | |
1267 | ||
1268 | if ((tp->t_flags & (TF_REQ_TSTMP | TF_RCVD_TSTMP)) != | |
1269 | (TF_REQ_TSTMP | TF_RCVD_TSTMP) && | |
1270 | (sbrcv->sb_flags & SB_AUTOSIZE) != 0) { | |
39037602 | 1271 | tcp_sbrcv_reserve(tp, sbrcv, newsize, 0, newsize); |
316670eb A |
1272 | } |
1273 | } | |
1274 | ||
39037602 A |
1275 | /* A receiver will evaluate the flow of packets on a connection |
1276 | * to see if it can reduce ack traffic. The receiver will start | |
6d2010ae A |
1277 | * stretching acks if all of the following conditions are met: |
1278 | * 1. tcp_delack_enabled is set to 3 | |
1279 | * 2. If the bytes received in the last 100ms is greater than a threshold | |
1280 | * defined by maxseg_unacked | |
1281 | * 3. If the connection has not been idle for tcp_maxrcvidle period. | |
39037602 | 1282 | * 4. If the connection has seen enough packets to let the slow-start |
6d2010ae A |
1283 | * finish after connection establishment or after some packet loss. |
1284 | * | |
1285 | * The receiver will stop stretching acks if there is congestion/reordering | |
39037602 A |
1286 | * as indicated by packets on reassembly queue or an ECN. If the delayed-ack |
1287 | * timer fires while stretching acks, it means that the packet flow has gone | |
6d2010ae | 1288 | * below the threshold defined by maxseg_unacked and the receiver will stop |
39037602 A |
1289 | * stretching acks. The receiver gets no indication when slow-start is completed |
1290 | * or when the connection reaches an idle state. That is why we use | |
1291 | * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle | |
6d2010ae A |
1292 | * state. |
1293 | */ | |
39236c6e | 1294 | static inline int |
39037602 | 1295 | tcp_stretch_ack_enable(struct tcpcb *tp, int thflags) |
39236c6e | 1296 | { |
39037602 A |
1297 | if (tp->rcv_by_unackwin >= (maxseg_unacked * tp->t_maxseg) && |
1298 | TSTMP_GEQ(tp->rcv_unackwin, tcp_now)) | |
1299 | tp->t_flags |= TF_STREAMING_ON; | |
1300 | else | |
1301 | tp->t_flags &= ~TF_STREAMING_ON; | |
1302 | ||
1303 | /* If there has been an idle time, reset streaming detection */ | |
1304 | if (TSTMP_GT(tcp_now, tp->rcv_unackwin + tcp_maxrcvidle)) | |
1305 | tp->t_flags &= ~TF_STREAMING_ON; | |
1306 | ||
1307 | /* | |
1308 | * If there are flags other than TH_ACK set, reset streaming | |
1309 | * detection | |
1310 | */ | |
1311 | if (thflags & ~TH_ACK) | |
1312 | tp->t_flags &= ~TF_STREAMING_ON; | |
1313 | ||
1314 | if (tp->t_flagsext & TF_DISABLE_STRETCHACK) { | |
1315 | if (tp->rcv_nostrack_pkts >= TCP_STRETCHACK_ENABLE_PKTCNT) { | |
1316 | tp->t_flagsext &= ~TF_DISABLE_STRETCHACK; | |
1317 | tp->rcv_nostrack_pkts = 0; | |
1318 | tp->rcv_nostrack_ts = 0; | |
1319 | } else { | |
1320 | tp->rcv_nostrack_pkts++; | |
1321 | } | |
1322 | } | |
1323 | ||
fe8ab488 | 1324 | if (!(tp->t_flagsext & (TF_NOSTRETCHACK|TF_DISABLE_STRETCHACK)) && |
39037602 A |
1325 | (tp->t_flags & TF_STREAMING_ON) && |
1326 | (!(tp->t_flagsext & TF_RCVUNACK_WAITSS) || | |
1327 | (tp->rcv_waitforss >= tcp_rcvsspktcnt))) { | |
6d2010ae A |
1328 | return(1); |
1329 | } | |
39037602 | 1330 | |
6d2010ae A |
1331 | return(0); |
1332 | } | |
1333 | ||
fe8ab488 A |
1334 | /* |
1335 | * Reset the state related to stretch-ack algorithm. This will make | |
6d2010ae | 1336 | * the receiver generate an ack every other packet. The receiver |
39037602 | 1337 | * will start re-evaluating the rate at which packets come to decide |
6d2010ae A |
1338 | * if it can benefit by lowering the ack traffic. |
1339 | */ | |
1340 | void | |
1341 | tcp_reset_stretch_ack(struct tcpcb *tp) | |
1342 | { | |
39037602 | 1343 | tp->t_flags &= ~(TF_STRETCHACK|TF_STREAMING_ON); |
6d2010ae A |
1344 | tp->rcv_by_unackwin = 0; |
1345 | tp->rcv_unackwin = tcp_now + tcp_rcvunackwin; | |
fe8ab488 A |
1346 | |
1347 | /* | |
1348 | * When there is packet loss or packet re-ordering or CWR due to | |
1349 | * ECN, the sender's congestion window is reduced. In these states, | |
1350 | * generate an ack for every other packet for some time to allow | |
39037602 | 1351 | * the sender's congestion window to grow. |
fe8ab488 A |
1352 | */ |
1353 | tp->t_flagsext |= TF_RCVUNACK_WAITSS; | |
1354 | tp->rcv_waitforss = 0; | |
6d2010ae A |
1355 | } |
1356 | ||
39236c6e | 1357 | /* |
39037602 | 1358 | * The last packet was a retransmission, check if this ack |
39236c6e | 1359 | * indicates that the retransmission was spurious. |
39037602 | 1360 | * |
39236c6e A |
1361 | * If the connection supports timestamps, we could use it to |
1362 | * detect if the last retransmit was not needed. Otherwise, | |
39037602 | 1363 | * we check if the ACK arrived within RTT/2 window, then it |
39236c6e A |
1364 | * was a mistake to do the retransmit in the first place. |
1365 | * | |
39037602 A |
1366 | * This function will return 1 if it is a spurious retransmit, |
1367 | * 0 otherwise. | |
39236c6e | 1368 | */ |
fe8ab488 | 1369 | int |
39037602 | 1370 | tcp_detect_bad_rexmt(struct tcpcb *tp, struct tcphdr *th, |
fe8ab488 | 1371 | struct tcpopt *to, u_int32_t rxtime) |
39236c6e A |
1372 | { |
1373 | int32_t tdiff, bad_rexmt_win; | |
39236c6e A |
1374 | bad_rexmt_win = (tp->t_srtt >> (TCP_RTT_SHIFT + 1)); |
1375 | ||
fe8ab488 | 1376 | /* If the ack has ECN CE bit, then cwnd has to be adjusted */ |
3e170ce0 | 1377 | if (TCP_ECN_ENABLED(tp) && (th->th_flags & TH_ECE)) |
fe8ab488 A |
1378 | return (0); |
1379 | if (TSTMP_SUPPORTED(tp)) { | |
1380 | if (rxtime > 0 && (to->to_flags & TOF_TS) | |
39037602 | 1381 | && to->to_tsecr != 0 |
fe8ab488 A |
1382 | && TSTMP_LT(to->to_tsecr, rxtime)) |
1383 | return (1); | |
1384 | } else { | |
39037602 | 1385 | if ((tp->t_rxtshift == 1 |
fe8ab488 A |
1386 | || (tp->t_flagsext & TF_SENT_TLPROBE)) |
1387 | && rxtime > 0) { | |
1388 | tdiff = (int32_t)(tcp_now - rxtime); | |
1389 | if (tdiff < bad_rexmt_win) | |
1390 | return(1); | |
1391 | } | |
39236c6e A |
1392 | } |
1393 | return(0); | |
1394 | } | |
1395 | ||
1396 | ||
1397 | /* | |
1398 | * Restore congestion window state if a spurious timeout | |
1399 | * was detected. | |
1400 | */ | |
1401 | static void | |
1402 | tcp_bad_rexmt_restore_state(struct tcpcb *tp, struct tcphdr *th) | |
1403 | { | |
1404 | if (TSTMP_SUPPORTED(tp)) { | |
1405 | u_int32_t fsize, acked; | |
1406 | fsize = tp->snd_max - th->th_ack; | |
1407 | acked = BYTES_ACKED(th, tp); | |
1408 | ||
1409 | /* | |
1410 | * Implement bad retransmit recovery as | |
1411 | * described in RFC 4015. | |
1412 | */ | |
1413 | tp->snd_ssthresh = tp->snd_ssthresh_prev; | |
1414 | ||
1415 | /* Initialize cwnd to the initial window */ | |
1416 | if (CC_ALGO(tp)->cwnd_init != NULL) | |
1417 | CC_ALGO(tp)->cwnd_init(tp); | |
1418 | ||
1419 | tp->snd_cwnd = fsize + min(acked, tp->snd_cwnd); | |
39037602 | 1420 | |
39236c6e A |
1421 | } else { |
1422 | tp->snd_cwnd = tp->snd_cwnd_prev; | |
1423 | tp->snd_ssthresh = tp->snd_ssthresh_prev; | |
1424 | if (tp->t_flags & TF_WASFRECOVERY) | |
1425 | ENTER_FASTRECOVERY(tp); | |
3e170ce0 A |
1426 | |
1427 | /* Do not use the loss flight size in this case */ | |
1428 | tp->t_lossflightsize = 0; | |
39236c6e | 1429 | } |
fe8ab488 | 1430 | tp->snd_cwnd = max(tp->snd_cwnd, TCP_CC_CWND_INIT_BYTES); |
39236c6e A |
1431 | tp->snd_recover = tp->snd_recover_prev; |
1432 | tp->snd_nxt = tp->snd_max; | |
39236c6e A |
1433 | |
1434 | /* Fix send socket buffer to reflect the change in cwnd */ | |
1435 | tcp_bad_rexmt_fix_sndbuf(tp); | |
1436 | ||
1437 | /* | |
39037602 | 1438 | * This RTT might reflect the extra delay induced |
39236c6e A |
1439 | * by the network. Skip using this sample for RTO |
1440 | * calculation and mark the connection so we can | |
1441 | * recompute RTT when the next eligible sample is | |
1442 | * found. | |
1443 | */ | |
1444 | tp->t_flagsext |= TF_RECOMPUTE_RTT; | |
1445 | tp->t_badrexmt_time = tcp_now; | |
1446 | tp->t_rtttime = 0; | |
1447 | } | |
1448 | ||
fe8ab488 A |
1449 | /* |
1450 | * If the previous packet was sent in retransmission timer, and it was | |
1451 | * not needed, then restore the congestion window to the state before that | |
1452 | * transmission. | |
1453 | * | |
1454 | * If the last packet was sent in tail loss probe timeout, check if that | |
1455 | * recovered the last packet. If so, that will indicate a real loss and | |
1456 | * the congestion window needs to be lowered. | |
1457 | */ | |
1458 | static void | |
1459 | tcp_bad_rexmt_check(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to) | |
1460 | { | |
1461 | if (tp->t_rxtshift > 0 && | |
1462 | tcp_detect_bad_rexmt(tp, th, to, tp->t_rxtstart)) { | |
1463 | ++tcpstat.tcps_sndrexmitbad; | |
1464 | tcp_bad_rexmt_restore_state(tp, th); | |
1465 | tcp_ccdbg_trace(tp, th, TCP_CC_BAD_REXMT_RECOVERY); | |
1466 | } else if ((tp->t_flagsext & TF_SENT_TLPROBE) | |
1467 | && tp->t_tlphighrxt > 0 | |
1468 | && SEQ_GEQ(th->th_ack, tp->t_tlphighrxt) | |
1469 | && !tcp_detect_bad_rexmt(tp, th, to, tp->t_tlpstart)) { | |
3e170ce0 A |
1470 | /* |
1471 | * check DSACK information also to make sure that | |
1472 | * the TLP was indeed needed | |
1473 | */ | |
1474 | if (tcp_rxtseg_dsack_for_tlp(tp)) { | |
1475 | /* | |
1476 | * received a DSACK to indicate that TLP was | |
1477 | * not needed | |
1478 | */ | |
1479 | tcp_rxtseg_clean(tp); | |
1480 | goto out; | |
1481 | } | |
1482 | ||
fe8ab488 | 1483 | /* |
39037602 | 1484 | * The tail loss probe recovered the last packet and |
fe8ab488 A |
1485 | * we need to adjust the congestion window to take |
1486 | * this loss into account. | |
1487 | */ | |
1488 | ++tcpstat.tcps_tlp_recoverlastpkt; | |
1489 | if (!IN_FASTRECOVERY(tp)) { | |
1490 | tcp_reduce_congestion_window(tp); | |
1491 | EXIT_FASTRECOVERY(tp); | |
1492 | } | |
1493 | tcp_ccdbg_trace(tp, th, TCP_CC_TLP_RECOVER_LASTPACKET); | |
3e170ce0 A |
1494 | } else if (tcp_rxtseg_detect_bad_rexmt(tp, th->th_ack)) { |
1495 | /* | |
1496 | * All of the retransmitted segments were duplicated, this | |
1497 | * can be an indication of bad fast retransmit. | |
1498 | */ | |
1499 | tcpstat.tcps_dsack_badrexmt++; | |
1500 | tcp_bad_rexmt_restore_state(tp, th); | |
1501 | tcp_ccdbg_trace(tp, th, TCP_CC_DSACK_BAD_REXMT); | |
1502 | tcp_rxtseg_clean(tp); | |
fe8ab488 | 1503 | } |
3e170ce0 | 1504 | out: |
fe8ab488 A |
1505 | tp->t_flagsext &= ~(TF_SENT_TLPROBE); |
1506 | tp->t_tlphighrxt = 0; | |
1507 | tp->t_tlpstart = 0; | |
1508 | ||
1509 | /* | |
1510 | * check if the latest ack was for a segment sent during PMTU | |
1511 | * blackhole detection. If the timestamp on the ack is before | |
1512 | * PMTU blackhole detection, then revert the size of the max | |
1513 | * segment to previous size. | |
1514 | */ | |
1515 | if (tp->t_rxtshift > 0 && (tp->t_flags & TF_BLACKHOLE) && | |
1516 | tp->t_pmtud_start_ts > 0 && TSTMP_SUPPORTED(tp)) { | |
39037602 | 1517 | if ((to->to_flags & TOF_TS) && to->to_tsecr != 0 |
fe8ab488 A |
1518 | && TSTMP_LT(to->to_tsecr, tp->t_pmtud_start_ts)) { |
1519 | tcp_pmtud_revert_segment_size(tp); | |
1520 | } | |
1521 | } | |
1522 | if (tp->t_pmtud_start_ts > 0) | |
1523 | tp->t_pmtud_start_ts = 0; | |
1524 | } | |
1525 | ||
1526 | /* | |
1527 | * Check if early retransmit can be attempted according to RFC 5827. | |
1528 | * | |
1529 | * If packet reordering is detected on a connection, fast recovery will | |
1530 | * be delayed until it is clear that the packet was lost and not reordered. | |
1531 | * But reordering detection is done only when SACK is enabled. | |
1532 | * | |
1533 | * On connections that do not support SACK, there is a limit on the number | |
1534 | * of early retransmits that can be done per minute. This limit is needed | |
1535 | * to make sure that too many packets are not retransmitted when there is | |
1536 | * packet reordering. | |
1537 | */ | |
1538 | static void | |
1539 | tcp_early_rexmt_check (struct tcpcb *tp, struct tcphdr *th) | |
1540 | { | |
1541 | u_int32_t obytes, snd_off; | |
1542 | int32_t snd_len; | |
1543 | struct socket *so = tp->t_inpcb->inp_socket; | |
1544 | ||
1545 | if (early_rexmt && (SACK_ENABLED(tp) || | |
1546 | tp->t_early_rexmt_count < TCP_EARLY_REXMT_LIMIT) && | |
1547 | SEQ_GT(tp->snd_max, tp->snd_una) && | |
39037602 A |
1548 | (tp->t_dupacks == 1 || |
1549 | (SACK_ENABLED(tp) && | |
fe8ab488 A |
1550 | !TAILQ_EMPTY(&tp->snd_holes)))) { |
1551 | /* | |
39037602 | 1552 | * If there are only a few outstanding |
fe8ab488 A |
1553 | * segments on the connection, we might need |
1554 | * to lower the retransmit threshold. This | |
39037602 | 1555 | * will allow us to do Early Retransmit as |
fe8ab488 A |
1556 | * described in RFC 5827. |
1557 | */ | |
39037602 | 1558 | if (SACK_ENABLED(tp) && |
fe8ab488 A |
1559 | !TAILQ_EMPTY(&tp->snd_holes)) { |
1560 | obytes = (tp->snd_max - tp->snd_fack) + | |
1561 | tp->sackhint.sack_bytes_rexmit; | |
1562 | } else { | |
39037602 | 1563 | obytes = (tp->snd_max - tp->snd_una); |
fe8ab488 A |
1564 | } |
1565 | ||
1566 | /* | |
39037602 | 1567 | * In order to lower retransmit threshold the |
fe8ab488 | 1568 | * following two conditions must be met. |
39037602 | 1569 | * 1. the amount of outstanding data is less |
fe8ab488 | 1570 | * than 4*SMSS bytes |
39037602 A |
1571 | * 2. there is no unsent data ready for |
1572 | * transmission or the advertised window | |
fe8ab488 A |
1573 | * will limit sending new segments. |
1574 | */ | |
1575 | snd_off = tp->snd_max - tp->snd_una; | |
1576 | snd_len = min(so->so_snd.sb_cc, tp->snd_wnd) - snd_off; | |
39037602 | 1577 | if (obytes < (tp->t_maxseg << 2) && |
fe8ab488 A |
1578 | snd_len <= 0) { |
1579 | u_int32_t osegs; | |
1580 | ||
1581 | osegs = obytes / tp->t_maxseg; | |
1582 | if ((osegs * tp->t_maxseg) < obytes) | |
1583 | osegs++; | |
1584 | ||
39037602 A |
1585 | /* |
1586 | * Since the connection might have already | |
fe8ab488 A |
1587 | * received some dupacks, we add them to |
1588 | * to the outstanding segments count to get | |
1589 | * the correct retransmit threshold. | |
1590 | * | |
39037602 | 1591 | * By checking for early retransmit after |
fe8ab488 | 1592 | * receiving some duplicate acks when SACK |
39037602 A |
1593 | * is supported, the connection will |
1594 | * enter fast recovery even if multiple | |
fe8ab488 A |
1595 | * segments are lost in the same window. |
1596 | */ | |
1597 | osegs += tp->t_dupacks; | |
1598 | if (osegs < 4) { | |
39037602 | 1599 | tp->t_rexmtthresh = |
fe8ab488 A |
1600 | ((osegs - 1) > 1) ? (osegs - 1) : 1; |
1601 | tp->t_rexmtthresh = | |
1602 | min(tp->t_rexmtthresh, tcprexmtthresh); | |
1603 | tp->t_rexmtthresh = | |
1604 | max(tp->t_rexmtthresh, tp->t_dupacks); | |
1605 | ||
1606 | if (tp->t_early_rexmt_count == 0) | |
1607 | tp->t_early_rexmt_win = tcp_now; | |
1608 | ||
1609 | if (tp->t_flagsext & TF_SENT_TLPROBE) { | |
1610 | tcpstat.tcps_tlp_recovery++; | |
1611 | tcp_ccdbg_trace(tp, th, | |
1612 | TCP_CC_TLP_RECOVERY); | |
1613 | } else { | |
1614 | tcpstat.tcps_early_rexmt++; | |
1615 | tp->t_early_rexmt_count++; | |
1616 | tcp_ccdbg_trace(tp, th, | |
1617 | TCP_CC_EARLY_RETRANSMIT); | |
1618 | } | |
1619 | } | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | /* | |
1624 | * If we ever sent a TLP probe, the acknowledgement will trigger | |
1625 | * early retransmit because the value of snd_fack will be close | |
1626 | * to snd_max. This will take care of adjustments to the | |
1627 | * congestion window. So we can reset TF_SENT_PROBE flag. | |
1628 | */ | |
1629 | tp->t_flagsext &= ~(TF_SENT_TLPROBE); | |
1630 | tp->t_tlphighrxt = 0; | |
1631 | tp->t_tlpstart = 0; | |
1632 | } | |
1633 | ||
3e170ce0 | 1634 | static boolean_t |
39037602 | 1635 | tcp_tfo_syn(struct tcpcb *tp, struct tcpopt *to) |
3e170ce0 A |
1636 | { |
1637 | u_char out[CCAES_BLOCK_SIZE]; | |
1638 | unsigned char len; | |
1639 | ||
1640 | if (!(to->to_flags & (TOF_TFO | TOF_TFOREQ)) || | |
1641 | !(tcp_fastopen & TCP_FASTOPEN_SERVER)) | |
1642 | return (FALSE); | |
1643 | ||
1644 | if ((to->to_flags & TOF_TFOREQ)) { | |
1645 | tp->t_tfo_flags |= TFO_F_OFFER_COOKIE; | |
1646 | ||
1647 | tp->t_tfo_stats |= TFO_S_COOKIEREQ_RECV; | |
1648 | tcpstat.tcps_tfo_cookie_req_rcv++; | |
1649 | return (FALSE); | |
1650 | } | |
1651 | ||
1652 | /* Ok, then it must be an offered cookie. We need to check that ... */ | |
1653 | tcp_tfo_gen_cookie(tp->t_inpcb, out, sizeof(out)); | |
1654 | ||
1655 | len = *to->to_tfo - TCPOLEN_FASTOPEN_REQ; | |
1656 | to->to_tfo++; | |
1657 | if (memcmp(out, to->to_tfo, len)) { | |
1658 | /* Cookies are different! Let's return and offer a new cookie */ | |
1659 | tp->t_tfo_flags |= TFO_F_OFFER_COOKIE; | |
1660 | ||
1661 | tp->t_tfo_stats |= TFO_S_COOKIE_INVALID; | |
1662 | tcpstat.tcps_tfo_cookie_invalid++; | |
1663 | return (FALSE); | |
1664 | } | |
1665 | ||
1666 | if (OSIncrementAtomic(&tcp_tfo_halfcnt) >= tcp_tfo_backlog) { | |
1667 | /* Need to decrement again as we just increased it... */ | |
1668 | OSDecrementAtomic(&tcp_tfo_halfcnt); | |
1669 | return (FALSE); | |
1670 | } | |
1671 | ||
1672 | tp->t_tfo_flags |= TFO_F_COOKIE_VALID; | |
1673 | ||
1674 | tp->t_tfo_stats |= TFO_S_SYNDATA_RCV; | |
1675 | tcpstat.tcps_tfo_syn_data_rcv++; | |
1676 | ||
1677 | return (TRUE); | |
1678 | } | |
1679 | ||
1680 | static void | |
39037602 | 1681 | tcp_tfo_synack(struct tcpcb *tp, struct tcpopt *to) |
3e170ce0 A |
1682 | { |
1683 | if (to->to_flags & TOF_TFO) { | |
1684 | unsigned char len = *to->to_tfo - TCPOLEN_FASTOPEN_REQ; | |
1685 | ||
1686 | /* | |
1687 | * If this happens, things have gone terribly wrong. len should | |
4bd07ac2 | 1688 | * have been checked in tcp_dooptions. |
3e170ce0 A |
1689 | */ |
1690 | VERIFY(len <= TFO_COOKIE_LEN_MAX); | |
1691 | ||
1692 | to->to_tfo++; | |
1693 | ||
1694 | tcp_cache_set_cookie(tp, to->to_tfo, len); | |
1695 | tcp_heuristic_tfo_success(tp); | |
1696 | ||
1697 | tp->t_tfo_stats |= TFO_S_COOKIE_RCV; | |
1698 | tcpstat.tcps_tfo_cookie_rcv++; | |
39037602 A |
1699 | if (tp->t_tfo_flags & TFO_F_COOKIE_SENT) { |
1700 | tcpstat.tcps_tfo_cookie_wrong++; | |
1701 | tp->t_tfo_stats |= TFO_S_COOKIE_WRONG; | |
1702 | } | |
3e170ce0 A |
1703 | } else { |
1704 | /* | |
1705 | * Thus, no cookie in the response, but we either asked for one | |
1706 | * or sent SYN+DATA. Now, we need to check whether we had to | |
1707 | * rexmit the SYN. If that's the case, it's better to start | |
1708 | * backing of TFO-cookie requests. | |
1709 | */ | |
39037602 A |
1710 | if (tp->t_tfo_flags & TFO_F_SYN_LOSS) { |
1711 | tp->t_tfo_stats |= TFO_S_SYN_LOSS; | |
1712 | tcpstat.tcps_tfo_syn_loss++; | |
1713 | ||
1714 | tcp_heuristic_tfo_loss(tp); | |
1715 | } else { | |
1716 | if (tp->t_tfo_flags & TFO_F_COOKIE_REQ) { | |
1717 | tp->t_tfo_stats |= TFO_S_NO_COOKIE_RCV; | |
1718 | tcpstat.tcps_tfo_no_cookie_rcv++; | |
1719 | } | |
1720 | ||
1721 | tcp_heuristic_tfo_success(tp); | |
1722 | } | |
3e170ce0 A |
1723 | } |
1724 | } | |
1725 | ||
1726 | static void | |
1727 | tcp_tfo_rcv_probe(struct tcpcb *tp, int tlen) | |
1728 | { | |
1729 | if (tlen == 0) { | |
1730 | tp->t_tfo_probe_state = TFO_PROBE_PROBING; | |
1731 | ||
1732 | /* | |
1733 | * We send the probe out rather quickly (after one RTO). It does not | |
1734 | * really hurt that much, it's only one additional segment on the wire. | |
1735 | */ | |
1736 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, (TCP_REXMTVAL(tp))); | |
1737 | } else { | |
1738 | /* If SYN/ACK+data, don't probe. We got the data! */ | |
1739 | tcp_heuristic_tfo_rcv_good(tp); | |
1740 | } | |
1741 | } | |
1742 | ||
1743 | static void | |
1744 | tcp_tfo_rcv_data(struct tcpcb *tp) | |
1745 | { | |
1746 | /* Transition from PROBING to NONE as data has been received */ | |
1747 | if (tp->t_tfo_probe_state >= TFO_PROBE_PROBING) { | |
1748 | tp->t_tfo_probe_state = TFO_PROBE_NONE; | |
1749 | ||
1750 | /* Data has been received - we are good to go! */ | |
1751 | tcp_heuristic_tfo_rcv_good(tp); | |
1752 | } | |
1753 | } | |
1754 | ||
1755 | static void | |
1756 | tcp_tfo_rcv_ack(struct tcpcb *tp, struct tcphdr *th) | |
1757 | { | |
1758 | if (tp->t_tfo_probe_state == TFO_PROBE_PROBING && | |
1759 | tp->t_tfo_probes > 0) { | |
1760 | if (th->th_seq == tp->rcv_nxt) { | |
1761 | /* No hole, so stop probing */ | |
1762 | tp->t_tfo_probe_state = TFO_PROBE_NONE; | |
1763 | } else if (SEQ_GT(th->th_seq, tp->rcv_nxt)) { | |
1764 | /* There is a hole! Wait a bit for data... */ | |
1765 | tp->t_tfo_probe_state = TFO_PROBE_WAIT_DATA; | |
1766 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, | |
1767 | TCP_REXMTVAL(tp)); | |
1768 | } | |
1769 | } | |
1770 | } | |
1771 | ||
39037602 A |
1772 | /* |
1773 | * Update snd_wnd information. | |
1774 | */ | |
1775 | static inline bool | |
1776 | tcp_update_window(struct tcpcb *tp, int thflags, struct tcphdr * th, | |
1777 | u_int32_t tiwin, int tlen) | |
1778 | { | |
1779 | /* Don't look at the window if there is no ACK flag */ | |
1780 | if ((thflags & TH_ACK) && | |
1781 | (SEQ_LT(tp->snd_wl1, th->th_seq) || | |
1782 | (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || | |
1783 | (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { | |
1784 | /* keep track of pure window updates */ | |
1785 | if (tlen == 0 && | |
1786 | tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) | |
1787 | tcpstat.tcps_rcvwinupd++; | |
1788 | tp->snd_wnd = tiwin; | |
1789 | tp->snd_wl1 = th->th_seq; | |
1790 | tp->snd_wl2 = th->th_ack; | |
1791 | if (tp->snd_wnd > tp->max_sndwnd) | |
1792 | tp->max_sndwnd = tp->snd_wnd; | |
1793 | return (true); | |
1794 | } | |
1795 | return (false); | |
1796 | } | |
1797 | ||
1c79356b | 1798 | void |
39037602 | 1799 | tcp_input(struct mbuf *m, int off0) |
1c79356b | 1800 | { |
39037602 A |
1801 | struct tcphdr *th; |
1802 | struct ip *ip = NULL; | |
1803 | struct inpcb *inp; | |
1c79356b A |
1804 | u_char *optp = NULL; |
1805 | int optlen = 0; | |
39236c6e | 1806 | int tlen, off; |
9bccf70c | 1807 | int drop_hdrlen; |
39037602 A |
1808 | struct tcpcb *tp = 0; |
1809 | int thflags; | |
1c79356b A |
1810 | struct socket *so = 0; |
1811 | int todrop, acked, ourfinisacked, needoutput = 0; | |
1812 | struct in_addr laddr; | |
9bccf70c | 1813 | #if INET6 |
1c79356b A |
1814 | struct in6_addr laddr6; |
1815 | #endif | |
1816 | int dropsocket = 0; | |
39037602 | 1817 | int iss = 0, nosock = 0; |
39236c6e | 1818 | u_int32_t tiwin, sack_bytes_acked = 0; |
1c79356b | 1819 | struct tcpopt to; /* options in this segment */ |
1c79356b A |
1820 | #if TCPDEBUG |
1821 | short ostate = 0; | |
1822 | #endif | |
3e170ce0 A |
1823 | #if IPFIREWALL |
1824 | struct sockaddr_in *next_hop = NULL; | |
91447636 | 1825 | struct m_tag *fwd_tag; |
3e170ce0 | 1826 | #endif /* IPFIREWALL */ |
2d21ac55 | 1827 | u_char ip_ecn = IPTOS_ECN_NOTECT; |
fe8ab488 | 1828 | unsigned int ifscope; |
6d2010ae | 1829 | uint8_t isconnected, isdisconnected; |
39236c6e A |
1830 | struct ifnet *ifp = m->m_pkthdr.rcvif; |
1831 | int pktf_sw_lro_pkt = (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) ? 1 : 0; | |
1832 | int nlropkts = (pktf_sw_lro_pkt == 1) ? m->m_pkthdr.lro_npkts : 1; | |
1833 | int turnoff_lro = 0, win; | |
1834 | #if MPTCP | |
1835 | struct mptcb *mp_tp = NULL; | |
39236c6e A |
1836 | #endif /* MPTCP */ |
1837 | boolean_t cell = IFNET_IS_CELLULAR(ifp); | |
1838 | boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
fe8ab488 | 1839 | boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp)); |
3e170ce0 | 1840 | boolean_t recvd_dsack = FALSE; |
fe8ab488 | 1841 | struct tcp_respond_args tra; |
39236c6e | 1842 | |
316670eb | 1843 | #define TCP_INC_VAR(stat, npkts) do { \ |
39236c6e | 1844 | stat += npkts; \ |
316670eb | 1845 | } while (0) |
c910b4d9 | 1846 | |
316670eb | 1847 | TCP_INC_VAR(tcpstat.tcps_rcvtotal, nlropkts); |
3e170ce0 | 1848 | #if IPFIREWALL |
91447636 | 1849 | /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */ |
b0d623f7 A |
1850 | if (!SLIST_EMPTY(&m->m_pkthdr.tags)) { |
1851 | fwd_tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, | |
1852 | KERNEL_TAG_TYPE_IPFORWARD, NULL); | |
1853 | } else { | |
1854 | fwd_tag = NULL; | |
1855 | } | |
91447636 | 1856 | if (fwd_tag != NULL) { |
39037602 | 1857 | struct ip_fwd_tag *ipfwd_tag = |
39236c6e A |
1858 | (struct ip_fwd_tag *)(fwd_tag+1); |
1859 | ||
91447636 A |
1860 | next_hop = ipfwd_tag->next_hop; |
1861 | m_tag_delete(m, fwd_tag); | |
1862 | } | |
3e170ce0 | 1863 | #endif /* IPFIREWALL */ |
39236c6e | 1864 | |
1c79356b A |
1865 | #if INET6 |
1866 | struct ip6_hdr *ip6 = NULL; | |
9bccf70c | 1867 | int isipv6; |
1c79356b | 1868 | #endif /* INET6 */ |
9bccf70c | 1869 | int rstreason; /* For badport_bandlim accounting purposes */ |
1c79356b | 1870 | struct proc *proc0=current_proc(); |
39236c6e | 1871 | |
1c79356b A |
1872 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_START,0,0,0,0,0); |
1873 | ||
9bccf70c A |
1874 | #if INET6 |
1875 | isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; | |
1876 | #endif | |
1c79356b A |
1877 | bzero((char *)&to, sizeof(to)); |
1878 | ||
1c79356b A |
1879 | #if INET6 |
1880 | if (isipv6) { | |
39037602 A |
1881 | /* |
1882 | * Expect 32-bit aligned data pointer on | |
1883 | * strict-align platforms | |
39236c6e | 1884 | */ |
316670eb A |
1885 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); |
1886 | ||
9bccf70c | 1887 | /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */ |
1c79356b | 1888 | ip6 = mtod(m, struct ip6_hdr *); |
9bccf70c | 1889 | tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0; |
316670eb | 1890 | th = (struct tcphdr *)(void *)((caddr_t)ip6 + off0); |
9bccf70c | 1891 | |
39236c6e A |
1892 | if (tcp_input_checksum(AF_INET6, m, th, off0, tlen)) |
1893 | goto dropnosock; | |
6d2010ae | 1894 | |
9bccf70c A |
1895 | KERNEL_DEBUG(DBG_LAYER_BEG, ((th->th_dport << 16) | th->th_sport), |
1896 | (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])), | |
1897 | th->th_seq, th->th_ack, th->th_win); | |
1c79356b | 1898 | /* |
9bccf70c A |
1899 | * Be proactive about unspecified IPv6 address in source. |
1900 | * As we use all-zero to indicate unbounded/unconnected pcb, | |
1901 | * unspecified IPv6 address can be used to confuse us. | |
1902 | * | |
1903 | * Note that packets with unspecified IPv6 destination is | |
1904 | * already dropped in ip6_input. | |
1c79356b | 1905 | */ |
9bccf70c A |
1906 | if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) { |
1907 | /* XXX stat */ | |
39236c6e | 1908 | IF_TCP_STATINC(ifp, unspecv6); |
91447636 | 1909 | goto dropnosock; |
1c79356b | 1910 | } |
39236c6e | 1911 | DTRACE_TCP5(receive, struct mbuf *, m, struct inpcb *, NULL, |
39037602 | 1912 | struct ip6_hdr *, ip6, struct tcpcb *, NULL, |
6d2010ae | 1913 | struct tcphdr *, th); |
39236c6e A |
1914 | |
1915 | ip_ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; | |
9bccf70c A |
1916 | } else |
1917 | #endif /* INET6 */ | |
ac5ea4a9 | 1918 | { |
9bccf70c A |
1919 | /* |
1920 | * Get IP and TCP header together in first mbuf. | |
1921 | * Note: IP leaves IP header in first mbuf. | |
1922 | */ | |
1923 | if (off0 > sizeof (struct ip)) { | |
1924 | ip_stripoptions(m, (struct mbuf *)0); | |
1925 | off0 = sizeof(struct ip); | |
9bccf70c A |
1926 | } |
1927 | if (m->m_len < sizeof (struct tcpiphdr)) { | |
1928 | if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { | |
1929 | tcpstat.tcps_rcvshort++; | |
1930 | return; | |
0b4e3aa0 | 1931 | } |
9bccf70c | 1932 | } |
316670eb A |
1933 | |
1934 | /* Expect 32-bit aligned data pointer on strict-align platforms */ | |
1935 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
1936 | ||
9bccf70c | 1937 | ip = mtod(m, struct ip *); |
316670eb | 1938 | th = (struct tcphdr *)(void *)((caddr_t)ip + off0); |
9bccf70c | 1939 | tlen = ip->ip_len; |
0b4e3aa0 | 1940 | |
39236c6e | 1941 | if (tcp_input_checksum(AF_INET, m, th, off0, tlen)) |
91447636 | 1942 | goto dropnosock; |
39236c6e | 1943 | |
9bccf70c A |
1944 | #if INET6 |
1945 | /* Re-initialization for later version check */ | |
1946 | ip->ip_v = IPVERSION; | |
1947 | #endif | |
2d21ac55 | 1948 | ip_ecn = (ip->ip_tos & IPTOS_ECN_MASK); |
316670eb A |
1949 | |
1950 | DTRACE_TCP5(receive, struct mbuf *, m, struct inpcb *, NULL, | |
1951 | struct ip *, ip, struct tcpcb *, NULL, struct tcphdr *, th); | |
39236c6e | 1952 | |
316670eb A |
1953 | KERNEL_DEBUG(DBG_LAYER_BEG, ((th->th_dport << 16) | th->th_sport), |
1954 | (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)), | |
1955 | th->th_seq, th->th_ack, th->th_win); | |
1956 | ||
ac5ea4a9 | 1957 | } |
1c79356b A |
1958 | |
1959 | /* | |
1960 | * Check that TCP offset makes sense, | |
39037602 | 1961 | * pull out TCP options and adjust length. |
1c79356b | 1962 | */ |
9bccf70c A |
1963 | off = th->th_off << 2; |
1964 | if (off < sizeof (struct tcphdr) || off > tlen) { | |
1c79356b | 1965 | tcpstat.tcps_rcvbadoff++; |
39236c6e | 1966 | IF_TCP_STATINC(ifp, badformat); |
91447636 | 1967 | goto dropnosock; |
1c79356b | 1968 | } |
9bccf70c A |
1969 | tlen -= off; /* tlen is used instead of ti->ti_len */ |
1970 | if (off > sizeof (struct tcphdr)) { | |
1c79356b A |
1971 | #if INET6 |
1972 | if (isipv6) { | |
91447636 | 1973 | IP6_EXTHDR_CHECK(m, off0, off, return); |
1c79356b | 1974 | ip6 = mtod(m, struct ip6_hdr *); |
316670eb | 1975 | th = (struct tcphdr *)(void *)((caddr_t)ip6 + off0); |
1c79356b A |
1976 | } else |
1977 | #endif /* INET6 */ | |
2d21ac55 A |
1978 | { |
1979 | if (m->m_len < sizeof(struct ip) + off) { | |
1980 | if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { | |
1981 | tcpstat.tcps_rcvshort++; | |
1982 | return; | |
1983 | } | |
1984 | ip = mtod(m, struct ip *); | |
316670eb | 1985 | th = (struct tcphdr *)(void *)((caddr_t)ip + off0); |
1c79356b A |
1986 | } |
1987 | } | |
9bccf70c | 1988 | optlen = off - sizeof (struct tcphdr); |
1c79356b | 1989 | optp = (u_char *)(th + 1); |
39037602 | 1990 | /* |
1c79356b A |
1991 | * Do quick retrieval of timestamp options ("options |
1992 | * prediction?"). If timestamp is the only option and it's | |
1993 | * formatted as recommended in RFC 1323 appendix A, we | |
1994 | * quickly get the values now and not bother calling | |
1995 | * tcp_dooptions(), etc. | |
1996 | */ | |
1997 | if ((optlen == TCPOLEN_TSTAMP_APPA || | |
2d21ac55 | 1998 | (optlen > TCPOLEN_TSTAMP_APPA && |
1c79356b | 1999 | optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && |
316670eb | 2000 | *(u_int32_t *)(void *)optp == htonl(TCPOPT_TSTAMP_HDR) && |
2d21ac55 | 2001 | (th->th_flags & TH_SYN) == 0) { |
8ad349bb | 2002 | to.to_flags |= TOF_TS; |
316670eb A |
2003 | to.to_tsval = ntohl(*(u_int32_t *)(void *)(optp + 4)); |
2004 | to.to_tsecr = ntohl(*(u_int32_t *)(void *)(optp + 8)); | |
1c79356b A |
2005 | optp = NULL; /* we've parsed the options */ |
2006 | } | |
2007 | } | |
2008 | thflags = th->th_flags; | |
2009 | ||
9bccf70c A |
2010 | #if TCP_DROP_SYNFIN |
2011 | /* | |
2012 | * If the drop_synfin option is enabled, drop all packets with | |
2013 | * both the SYN and FIN bits set. This prevents e.g. nmap from | |
2014 | * identifying the TCP/IP stack. | |
2015 | * | |
8ad349bb | 2016 | * This is a violation of the TCP specification. |
9bccf70c | 2017 | */ |
316670eb | 2018 | if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN)) { |
39236c6e | 2019 | IF_TCP_STATINC(ifp, synfin); |
91447636 | 2020 | goto dropnosock; |
316670eb | 2021 | } |
b0d623f7 | 2022 | #endif |
1c79356b A |
2023 | |
2024 | /* | |
8ad349bb | 2025 | * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options, |
9bccf70c A |
2026 | * until after ip6_savecontrol() is called and before other functions |
2027 | * which don't want those proto headers. | |
2028 | * Because ip6_savecontrol() is going to parse the mbuf to | |
2029 | * search for data to be passed up to user-land, it wants mbuf | |
2030 | * parameters to be unchanged. | |
1c79356b | 2031 | */ |
9bccf70c | 2032 | drop_hdrlen = off0 + off; |
39037602 | 2033 | |
316670eb A |
2034 | /* Since this is an entry point for input processing of tcp packets, we |
2035 | * can update the tcp clock here. | |
2036 | */ | |
2037 | calculate_tcp_clock(); | |
2038 | ||
2039 | /* | |
39236c6e A |
2040 | * Record the interface where this segment arrived on; this does not |
2041 | * affect normal data output (for non-detached TCP) as it provides a | |
2042 | * hint about which route and interface to use for sending in the | |
2043 | * absence of a PCB, when scoped routing (and thus source interface | |
2044 | * selection) are enabled. | |
2045 | */ | |
2046 | if ((m->m_pkthdr.pkt_flags & PKTF_LOOP) || m->m_pkthdr.rcvif == NULL) | |
2047 | ifscope = IFSCOPE_NONE; | |
2048 | else | |
2049 | ifscope = m->m_pkthdr.rcvif->if_index; | |
316670eb A |
2050 | |
2051 | /* | |
2052 | * Convert TCP protocol specific fields to host format. | |
2053 | */ | |
2054 | ||
2055 | #if BYTE_ORDER != BIG_ENDIAN | |
2056 | NTOHL(th->th_seq); | |
2057 | NTOHL(th->th_ack); | |
2058 | NTOHS(th->th_win); | |
2059 | NTOHS(th->th_urp); | |
2060 | #endif | |
1c79356b A |
2061 | |
2062 | /* | |
2063 | * Locate pcb for segment. | |
2064 | */ | |
2065 | findpcb: | |
6d2010ae A |
2066 | |
2067 | isconnected = FALSE; | |
2068 | isdisconnected = FALSE; | |
2069 | ||
1c79356b | 2070 | #if IPFIREWALL_FORWARD |
91447636 | 2071 | if (next_hop != NULL |
1c79356b | 2072 | #if INET6 |
2d21ac55 | 2073 | && isipv6 == 0 /* IPv6 support is not yet */ |
1c79356b A |
2074 | #endif /* INET6 */ |
2075 | ) { | |
2076 | /* | |
2077 | * Diverted. Pretend to be the destination. | |
39037602 | 2078 | * already got one like this? |
1c79356b A |
2079 | */ |
2080 | inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, | |
2081 | ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif); | |
2082 | if (!inp) { | |
39037602 | 2083 | /* |
1c79356b A |
2084 | * No, then it's new. Try find the ambushing socket |
2085 | */ | |
91447636 | 2086 | if (!next_hop->sin_port) { |
1c79356b | 2087 | inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, |
91447636 | 2088 | th->th_sport, next_hop->sin_addr, |
1c79356b A |
2089 | th->th_dport, 1, m->m_pkthdr.rcvif); |
2090 | } else { | |
2091 | inp = in_pcblookup_hash(&tcbinfo, | |
2092 | ip->ip_src, th->th_sport, | |
91447636 A |
2093 | next_hop->sin_addr, |
2094 | ntohs(next_hop->sin_port), 1, | |
1c79356b A |
2095 | m->m_pkthdr.rcvif); |
2096 | } | |
2097 | } | |
1c79356b A |
2098 | } else |
2099 | #endif /* IPFIREWALL_FORWARD */ | |
9bccf70c | 2100 | { |
1c79356b A |
2101 | #if INET6 |
2102 | if (isipv6) | |
2103 | inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport, | |
2104 | &ip6->ip6_dst, th->th_dport, 1, | |
2105 | m->m_pkthdr.rcvif); | |
2106 | else | |
2107 | #endif /* INET6 */ | |
2108 | inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, | |
2109 | ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif); | |
9bccf70c | 2110 | } |
1c79356b | 2111 | |
c910b4d9 A |
2112 | /* |
2113 | * Use the interface scope information from the PCB for outbound | |
2114 | * segments. If the PCB isn't present and if scoped routing is | |
2115 | * enabled, tcp_respond will use the scope of the interface where | |
2116 | * the segment arrived on. | |
2117 | */ | |
2118 | if (inp != NULL && (inp->inp_flags & INP_BOUND_IF)) | |
316670eb | 2119 | ifscope = inp->inp_boundifp->if_index; |
1c79356b A |
2120 | |
2121 | /* | |
2122 | * If the state is CLOSED (i.e., TCB does not exist) then | |
2123 | * all data in the incoming segment is discarded. | |
2124 | * If the TCB exists but is in CLOSED state, it is embryonic, | |
2125 | * but should either do a listen or a connect soon. | |
2126 | */ | |
2127 | if (inp == NULL) { | |
9bccf70c | 2128 | if (log_in_vain) { |
1c79356b | 2129 | #if INET6 |
91447636 | 2130 | char dbuf[MAX_IPv6_STR_LEN], sbuf[MAX_IPv6_STR_LEN]; |
1c79356b | 2131 | #else /* INET6 */ |
91447636 | 2132 | char dbuf[MAX_IPv4_STR_LEN], sbuf[MAX_IPv4_STR_LEN]; |
1c79356b A |
2133 | #endif /* INET6 */ |
2134 | ||
2135 | #if INET6 | |
2136 | if (isipv6) { | |
91447636 A |
2137 | inet_ntop(AF_INET6, &ip6->ip6_dst, dbuf, sizeof(dbuf)); |
2138 | inet_ntop(AF_INET6, &ip6->ip6_src, sbuf, sizeof(sbuf)); | |
9bccf70c | 2139 | } else |
1c79356b | 2140 | #endif |
91447636 A |
2141 | { |
2142 | inet_ntop(AF_INET, &ip->ip_dst, dbuf, sizeof(dbuf)); | |
2143 | inet_ntop(AF_INET, &ip->ip_src, sbuf, sizeof(sbuf)); | |
2144 | } | |
9bccf70c A |
2145 | switch (log_in_vain) { |
2146 | case 1: | |
2147 | if(thflags & TH_SYN) | |
2148 | log(LOG_INFO, | |
91447636 A |
2149 | "Connection attempt to TCP %s:%d from %s:%d\n", |
2150 | dbuf, ntohs(th->th_dport), | |
2151 | sbuf, | |
2152 | ntohs(th->th_sport)); | |
9bccf70c A |
2153 | break; |
2154 | case 2: | |
2155 | log(LOG_INFO, | |
91447636 A |
2156 | "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n", |
2157 | dbuf, ntohs(th->th_dport), sbuf, | |
2158 | ntohs(th->th_sport), thflags); | |
2159 | break; | |
2160 | case 3: | |
39236c6e | 2161 | case 4: |
316670eb | 2162 | if ((thflags & TH_SYN) && !(thflags & TH_ACK) && |
91447636 A |
2163 | !(m->m_flags & (M_BCAST | M_MCAST)) && |
2164 | #if INET6 | |
2165 | ((isipv6 && !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) || | |
2166 | (!isipv6 && ip->ip_dst.s_addr != ip->ip_src.s_addr)) | |
2167 | #else | |
2168 | ip->ip_dst.s_addr != ip->ip_src.s_addr | |
2169 | #endif | |
2170 | ) | |
2171 | log_in_vain_log((LOG_INFO, | |
2172 | "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n", | |
2173 | dbuf, ntohs(th->th_dport), | |
2174 | sbuf, | |
2175 | ntohs(th->th_sport))); | |
9bccf70c A |
2176 | break; |
2177 | default: | |
2178 | break; | |
1c79356b | 2179 | } |
1c79356b | 2180 | } |
39037602 | 2181 | if (blackhole) { |
91447636 | 2182 | if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type != IFT_LOOP) |
39037602 | 2183 | |
91447636 A |
2184 | switch (blackhole) { |
2185 | case 1: | |
2186 | if (thflags & TH_SYN) | |
2187 | goto dropnosock; | |
2188 | break; | |
2189 | case 2: | |
2190 | goto dropnosock; | |
2191 | default: | |
2192 | goto dropnosock; | |
2193 | } | |
9bccf70c A |
2194 | } |
2195 | rstreason = BANDLIM_RST_CLOSEDPORT; | |
39236c6e | 2196 | IF_TCP_STATINC(ifp, noconnnolist); |
91447636 A |
2197 | goto dropwithresetnosock; |
2198 | } | |
2199 | so = inp->inp_socket; | |
2200 | if (so == NULL) { | |
b0d623f7 A |
2201 | /* This case shouldn't happen as the socket shouldn't be null |
2202 | * if inp_state isn't set to INPCB_STATE_DEAD | |
2203 | * But just in case, we pretend we didn't find the socket if we hit this case | |
2204 | * as this isn't cause for a panic (the socket might be leaked however)... | |
2205 | */ | |
2206 | inp = NULL; | |
91447636 | 2207 | #if TEMPDEBUG |
b0d623f7 | 2208 | printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp); |
91447636 A |
2209 | #endif |
2210 | goto dropnosock; | |
1c79356b | 2211 | } |
8ad349bb | 2212 | |
6d2010ae | 2213 | tcp_lock(so, 1, 0); |
91447636 | 2214 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { |
b0d623f7 | 2215 | tcp_unlock(so, 1, (void *)2); |
3e170ce0 | 2216 | inp = NULL; // pretend we didn't find it |
91447636 A |
2217 | goto dropnosock; |
2218 | } | |
2219 | ||
3e170ce0 A |
2220 | #if NECP |
2221 | #if INET6 | |
2222 | if (isipv6) { | |
39037602 A |
2223 | if (!necp_socket_is_allowed_to_send_recv_v6(inp, |
2224 | th->th_dport, th->th_sport, &ip6->ip6_dst, | |
2225 | &ip6->ip6_src, ifp, NULL, NULL)) { | |
3e170ce0 A |
2226 | IF_TCP_STATINC(ifp, badformatipsec); |
2227 | goto drop; | |
2228 | } | |
2229 | } else | |
2230 | #endif | |
2231 | { | |
39037602 A |
2232 | if (!necp_socket_is_allowed_to_send_recv_v4(inp, |
2233 | th->th_dport, th->th_sport, &ip->ip_dst, &ip->ip_src, | |
2234 | ifp, NULL, NULL)) { | |
3e170ce0 A |
2235 | IF_TCP_STATINC(ifp, badformatipsec); |
2236 | goto drop; | |
2237 | } | |
2238 | } | |
2239 | #endif /* NECP */ | |
2240 | ||
1c79356b | 2241 | tp = intotcpcb(inp); |
9bccf70c A |
2242 | if (tp == 0) { |
2243 | rstreason = BANDLIM_RST_CLOSEDPORT; | |
39236c6e | 2244 | IF_TCP_STATINC(ifp, noconnlist); |
1c79356b | 2245 | goto dropwithreset; |
9bccf70c | 2246 | } |
1c79356b A |
2247 | if (tp->t_state == TCPS_CLOSED) |
2248 | goto drop; | |
9bccf70c | 2249 | |
1c79356b A |
2250 | /* Unscale the window into a 32-bit value. */ |
2251 | if ((thflags & TH_SYN) == 0) | |
2252 | tiwin = th->th_win << tp->snd_scale; | |
2253 | else | |
2254 | tiwin = th->th_win; | |
2255 | ||
2d21ac55 A |
2256 | #if CONFIG_MACF_NET |
2257 | if (mac_inpcb_check_deliver(inp, m, AF_INET, SOCK_STREAM)) | |
2258 | goto drop; | |
2259 | #endif | |
2260 | ||
39236c6e | 2261 | /* Avoid processing packets while closing a listen socket */ |
39037602 A |
2262 | if (tp->t_state == TCPS_LISTEN && |
2263 | (so->so_options & SO_ACCEPTCONN) == 0) | |
b7266188 A |
2264 | goto drop; |
2265 | ||
1c79356b A |
2266 | if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { |
2267 | #if TCPDEBUG | |
2268 | if (so->so_options & SO_DEBUG) { | |
2269 | ostate = tp->t_state; | |
2270 | #if INET6 | |
2271 | if (isipv6) | |
9bccf70c A |
2272 | bcopy((char *)ip6, (char *)tcp_saveipgen, |
2273 | sizeof(*ip6)); | |
1c79356b | 2274 | else |
1c79356b | 2275 | #endif /* INET6 */ |
9bccf70c | 2276 | bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip)); |
1c79356b A |
2277 | tcp_savetcp = *th; |
2278 | } | |
2279 | #endif | |
2280 | if (so->so_options & SO_ACCEPTCONN) { | |
39037602 | 2281 | struct tcpcb *tp0 = tp; |
1c79356b | 2282 | struct socket *so2; |
1c79356b | 2283 | struct socket *oso; |
91447636 | 2284 | struct sockaddr_storage from; |
1c79356b A |
2285 | #if INET6 |
2286 | struct inpcb *oinp = sotoinpcb(so); | |
2287 | #endif /* INET6 */ | |
316670eb | 2288 | struct ifnet *head_ifscope; |
fe8ab488 | 2289 | unsigned int head_nocell, head_recvanyif, |
39037602 A |
2290 | head_noexpensive, head_awdl_unrestricted, |
2291 | head_intcoproc_allowed; | |
c910b4d9 A |
2292 | |
2293 | /* Get listener's bound-to-interface, if any */ | |
2294 | head_ifscope = (inp->inp_flags & INP_BOUND_IF) ? | |
316670eb | 2295 | inp->inp_boundifp : NULL; |
6d2010ae | 2296 | /* Get listener's no-cellular information, if any */ |
fe8ab488 | 2297 | head_nocell = INP_NO_CELLULAR(inp); |
316670eb A |
2298 | /* Get listener's recv-any-interface, if any */ |
2299 | head_recvanyif = (inp->inp_flags & INP_RECV_ANYIF); | |
fe8ab488 A |
2300 | /* Get listener's no-expensive information, if any */ |
2301 | head_noexpensive = INP_NO_EXPENSIVE(inp); | |
2302 | head_awdl_unrestricted = INP_AWDL_UNRESTRICTED(inp); | |
39037602 | 2303 | head_intcoproc_allowed = INP_INTCOPROC_ALLOWED(inp); |
1c79356b | 2304 | |
9bccf70c | 2305 | /* |
7e4a7d39 A |
2306 | * If the state is LISTEN then ignore segment if it contains an RST. |
2307 | * If the segment contains an ACK then it is bad and send a RST. | |
2308 | * If it does not contain a SYN then it is not interesting; drop it. | |
2309 | * If it is from this socket, drop it, it must be forged. | |
9bccf70c | 2310 | */ |
1c79356b | 2311 | if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { |
39236c6e A |
2312 | IF_TCP_STATINC(ifp, listbadsyn); |
2313 | ||
7e4a7d39 A |
2314 | if (thflags & TH_RST) { |
2315 | goto drop; | |
2316 | } | |
1c79356b | 2317 | if (thflags & TH_ACK) { |
7e4a7d39 | 2318 | tp = NULL; |
1c79356b | 2319 | tcpstat.tcps_badsyn++; |
9bccf70c | 2320 | rstreason = BANDLIM_RST_OPENPORT; |
1c79356b A |
2321 | goto dropwithreset; |
2322 | } | |
7e4a7d39 A |
2323 | |
2324 | /* We come here if there is no SYN set */ | |
2325 | tcpstat.tcps_badsyn++; | |
1c79356b A |
2326 | goto drop; |
2327 | } | |
1c79356b | 2328 | KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_START,0,0,0,0,0); |
39037602 | 2329 | if (th->th_dport == th->th_sport) { |
7e4a7d39 A |
2330 | #if INET6 |
2331 | if (isipv6) { | |
2332 | if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, | |
2333 | &ip6->ip6_src)) | |
2334 | goto drop; | |
2335 | } else | |
2336 | #endif /* INET6 */ | |
2337 | if (ip->ip_dst.s_addr == ip->ip_src.s_addr) | |
2338 | goto drop; | |
2339 | } | |
2340 | /* | |
2341 | * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN | |
2342 | * in_broadcast() should never return true on a received | |
2343 | * packet with M_BCAST not set. | |
2344 | * | |
2345 | * Packets with a multicast source address should also | |
2346 | * be discarded. | |
2347 | */ | |
2348 | if (m->m_flags & (M_BCAST|M_MCAST)) | |
2349 | goto drop; | |
2350 | #if INET6 | |
2351 | if (isipv6) { | |
2352 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || | |
2353 | IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) | |
2354 | goto drop; | |
2355 | } else | |
2356 | #endif | |
2357 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || | |
2358 | IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || | |
2359 | ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || | |
2360 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) | |
2361 | goto drop; | |
2362 | ||
1c79356b | 2363 | |
9bccf70c A |
2364 | #if INET6 |
2365 | /* | |
2366 | * If deprecated address is forbidden, | |
2367 | * we do not accept SYN to deprecated interface | |
2368 | * address to prevent any new inbound connection from | |
2369 | * getting established. | |
2370 | * When we do not accept SYN, we send a TCP RST, | |
2371 | * with deprecated source address (instead of dropping | |
2372 | * it). We compromise it as it is much better for peer | |
2373 | * to send a RST, and RST will be the final packet | |
2374 | * for the exchange. | |
2375 | * | |
2376 | * If we do not forbid deprecated addresses, we accept | |
39236c6e A |
2377 | * the SYN packet. RFC 4862 forbids dropping SYN in |
2378 | * this case. | |
9bccf70c A |
2379 | */ |
2380 | if (isipv6 && !ip6_use_deprecated) { | |
39236c6e A |
2381 | uint32_t ia6_flags; |
2382 | ||
2383 | if (ip6_getdstifaddr_info(m, NULL, | |
2384 | &ia6_flags) == 0) { | |
2385 | if (ia6_flags & IN6_IFF_DEPRECATED) { | |
6d2010ae A |
2386 | tp = NULL; |
2387 | rstreason = BANDLIM_RST_OPENPORT; | |
39236c6e | 2388 | IF_TCP_STATINC(ifp, deprecate6); |
6d2010ae A |
2389 | goto dropwithreset; |
2390 | } | |
9bccf70c A |
2391 | } |
2392 | } | |
2393 | #endif | |
91447636 | 2394 | if (so->so_filt) { |
2d21ac55 | 2395 | #if INET6 |
91447636 A |
2396 | if (isipv6) { |
2397 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)&from; | |
39037602 | 2398 | |
91447636 A |
2399 | sin6->sin6_len = sizeof(*sin6); |
2400 | sin6->sin6_family = AF_INET6; | |
2401 | sin6->sin6_port = th->th_sport; | |
2402 | sin6->sin6_flowinfo = 0; | |
2403 | sin6->sin6_addr = ip6->ip6_src; | |
2404 | sin6->sin6_scope_id = 0; | |
2d21ac55 A |
2405 | } |
2406 | else | |
2407 | #endif | |
2408 | { | |
91447636 | 2409 | struct sockaddr_in *sin = (struct sockaddr_in*)&from; |
39037602 | 2410 | |
91447636 A |
2411 | sin->sin_len = sizeof(*sin); |
2412 | sin->sin_family = AF_INET; | |
2413 | sin->sin_port = th->th_sport; | |
2414 | sin->sin_addr = ip->ip_src; | |
2415 | } | |
2416 | so2 = sonewconn(so, 0, (struct sockaddr*)&from); | |
2417 | } else { | |
2418 | so2 = sonewconn(so, 0, NULL); | |
2419 | } | |
1c79356b A |
2420 | if (so2 == 0) { |
2421 | tcpstat.tcps_listendrop++; | |
2d21ac55 | 2422 | if (tcp_dropdropablreq(so)) { |
91447636 A |
2423 | if (so->so_filt) |
2424 | so2 = sonewconn(so, 0, (struct sockaddr*)&from); | |
2425 | else | |
2426 | so2 = sonewconn(so, 0, NULL); | |
1c79356b | 2427 | } |
39037602 | 2428 | if (!so2) |
1c79356b A |
2429 | goto drop; |
2430 | } | |
b0d623f7 A |
2431 | |
2432 | /* Point "inp" and "tp" in tandem to new socket */ | |
2433 | inp = (struct inpcb *)so2->so_pcb; | |
2434 | tp = intotcpcb(inp); | |
91447636 | 2435 | |
1c79356b | 2436 | oso = so; |
91447636 A |
2437 | tcp_unlock(so, 0, 0); /* Unlock but keep a reference on listener for now */ |
2438 | ||
1c79356b | 2439 | so = so2; |
91447636 | 2440 | tcp_lock(so, 1, 0); |
1c79356b | 2441 | /* |
1c79356b A |
2442 | * Mark socket as temporary until we're |
2443 | * committed to keeping it. The code at | |
2444 | * ``drop'' and ``dropwithreset'' check the | |
2445 | * flag dropsocket to see if the temporary | |
2446 | * socket created here should be discarded. | |
2447 | * We mark the socket as discardable until | |
2448 | * we're committed to it below in TCPS_LISTEN. | |
7e4a7d39 A |
2449 | * There are some error conditions in which we |
2450 | * have to drop the temporary socket. | |
1c79356b A |
2451 | */ |
2452 | dropsocket++; | |
c910b4d9 A |
2453 | /* |
2454 | * Inherit INP_BOUND_IF from listener; testing if | |
316670eb | 2455 | * head_ifscope is non-NULL is sufficient, since it |
c910b4d9 A |
2456 | * can only be set to a non-zero value earlier if |
2457 | * the listener has such a flag set. | |
2458 | */ | |
316670eb | 2459 | if (head_ifscope != NULL) { |
c910b4d9 | 2460 | inp->inp_flags |= INP_BOUND_IF; |
316670eb A |
2461 | inp->inp_boundifp = head_ifscope; |
2462 | } else { | |
2463 | inp->inp_flags &= ~INP_BOUND_IF; | |
c910b4d9 | 2464 | } |
6d2010ae | 2465 | /* |
fe8ab488 | 2466 | * Inherit restrictions from listener. |
6d2010ae | 2467 | */ |
fe8ab488 A |
2468 | if (head_nocell) |
2469 | inp_set_nocellular(inp); | |
2470 | if (head_noexpensive) | |
2471 | inp_set_noexpensive(inp); | |
2472 | if (head_awdl_unrestricted) | |
2473 | inp_set_awdl_unrestricted(inp); | |
39037602 A |
2474 | if (head_intcoproc_allowed) |
2475 | inp_set_intcoproc_allowed(inp); | |
316670eb A |
2476 | /* |
2477 | * Inherit {IN,IN6}_RECV_ANYIF from listener. | |
2478 | */ | |
2479 | if (head_recvanyif) | |
2480 | inp->inp_flags |= INP_RECV_ANYIF; | |
2481 | else | |
2482 | inp->inp_flags &= ~INP_RECV_ANYIF; | |
1c79356b A |
2483 | #if INET6 |
2484 | if (isipv6) | |
2485 | inp->in6p_laddr = ip6->ip6_dst; | |
2486 | else { | |
9bccf70c A |
2487 | inp->inp_vflag &= ~INP_IPV6; |
2488 | inp->inp_vflag |= INP_IPV4; | |
1c79356b | 2489 | #endif /* INET6 */ |
7e4a7d39 | 2490 | inp->inp_laddr = ip->ip_dst; |
1c79356b A |
2491 | #if INET6 |
2492 | } | |
2493 | #endif /* INET6 */ | |
1c79356b | 2494 | inp->inp_lport = th->th_dport; |
91447636 | 2495 | if (in_pcbinshash(inp, 0) != 0) { |
1c79356b | 2496 | /* |
9bccf70c A |
2497 | * Undo the assignments above if we failed to |
2498 | * put the PCB on the hash lists. | |
1c79356b A |
2499 | */ |
2500 | #if INET6 | |
2501 | if (isipv6) | |
2502 | inp->in6p_laddr = in6addr_any; | |
2503 | else | |
2504 | #endif /* INET6 */ | |
316670eb | 2505 | inp->inp_laddr.s_addr = INADDR_ANY; |
1c79356b | 2506 | inp->inp_lport = 0; |
91447636 A |
2507 | tcp_lock(oso, 0, 0); /* release ref on parent */ |
2508 | tcp_unlock(oso, 1, 0); | |
1c79356b A |
2509 | goto drop; |
2510 | } | |
1c79356b A |
2511 | #if INET6 |
2512 | if (isipv6) { | |
9bccf70c A |
2513 | /* |
2514 | * Inherit socket options from the listening | |
2515 | * socket. | |
2516 | * Note that in6p_inputopts are not (even | |
2517 | * should not be) copied, since it stores | |
1c79356b | 2518 | * previously received options and is used to |
9bccf70c A |
2519 | * detect if each new option is different than |
2520 | * the previous one and hence should be passed | |
2521 | * to a user. | |
2522 | * If we copied in6p_inputopts, a user would | |
2523 | * not be able to receive options just after | |
2524 | * calling the accept system call. | |
2525 | */ | |
1c79356b A |
2526 | inp->inp_flags |= |
2527 | oinp->inp_flags & INP_CONTROLOPTS; | |
9bccf70c A |
2528 | if (oinp->in6p_outputopts) |
2529 | inp->in6p_outputopts = | |
2530 | ip6_copypktopts(oinp->in6p_outputopts, | |
2531 | M_NOWAIT); | |
1c79356b A |
2532 | } else |
2533 | #endif /* INET6 */ | |
3e170ce0 | 2534 | { |
316670eb | 2535 | inp->inp_options = ip_srcroute(); |
3e170ce0 A |
2536 | inp->inp_ip_tos = oinp->inp_ip_tos; |
2537 | } | |
91447636 | 2538 | tcp_lock(oso, 0, 0); |
1c79356b A |
2539 | #if IPSEC |
2540 | /* copy old policy into new socket's */ | |
9bccf70c A |
2541 | if (sotoinpcb(oso)->inp_sp) |
2542 | { | |
2543 | int error = 0; | |
2544 | /* Is it a security hole here to silently fail to copy the policy? */ | |
2545 | if (inp->inp_sp != NULL) | |
2546 | error = ipsec_init_policy(so, &inp->inp_sp); | |
2547 | if (error != 0 || ipsec_copy_policy(sotoinpcb(oso)->inp_sp, inp->inp_sp)) | |
2548 | printf("tcp_input: could not copy policy\n"); | |
2549 | } | |
1c79356b | 2550 | #endif |
b0d623f7 | 2551 | /* inherit states from the listener */ |
6d2010ae A |
2552 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
2553 | struct tcpcb *, tp, int32_t, TCPS_LISTEN); | |
1c79356b | 2554 | tp->t_state = TCPS_LISTEN; |
9bccf70c | 2555 | tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT|TF_NODELAY); |
3e170ce0 | 2556 | tp->t_flagsext |= (tp0->t_flagsext & (TF_RXTFINDROP|TF_NOTIMEWAIT|TF_FASTOPEN)); |
b0d623f7 | 2557 | tp->t_keepinit = tp0->t_keepinit; |
39236c6e A |
2558 | tp->t_keepcnt = tp0->t_keepcnt; |
2559 | tp->t_keepintvl = tp0->t_keepintvl; | |
2560 | tp->t_adaptive_wtimo = tp0->t_adaptive_wtimo; | |
2561 | tp->t_adaptive_rtimo = tp0->t_adaptive_rtimo; | |
91447636 | 2562 | tp->t_inpcb->inp_ip_ttl = tp0->t_inpcb->inp_ip_ttl; |
316670eb A |
2563 | if ((so->so_flags & SOF_NOTSENT_LOWAT) != 0) |
2564 | tp->t_notsent_lowat = tp0->t_notsent_lowat; | |
39037602 A |
2565 | tp->t_inpcb->inp_flags2 |= |
2566 | tp0->t_inpcb->inp_flags2 & INP2_KEEPALIVE_OFFLOAD; | |
b0d623f7 A |
2567 | |
2568 | /* now drop the reference on the listener */ | |
2569 | tcp_unlock(oso, 1, 0); | |
2570 | ||
39037602 | 2571 | tcp_set_max_rwinscale(tp, so, TCP_AUTORCVBUF_MAX(ifp)); |
1c79356b A |
2572 | |
2573 | KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_END,0,0,0,0,0); | |
2574 | } | |
2575 | } | |
fe8ab488 A |
2576 | lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx, |
2577 | LCK_MTX_ASSERT_OWNED); | |
6d2010ae | 2578 | |
743b1565 | 2579 | if (tp->t_state == TCPS_ESTABLISHED && tlen > 0) { |
39037602 A |
2580 | /* |
2581 | * Evaluate the rate of arrival of packets to see if the | |
2582 | * receiver can reduce the ack traffic. The algorithm to | |
2583 | * stretch acks will be enabled if the connection meets | |
6d2010ae A |
2584 | * certain criteria defined in tcp_stretch_ack_enable function. |
2585 | */ | |
2586 | if ((tp->t_flagsext & TF_RCVUNACK_WAITSS) != 0) { | |
316670eb | 2587 | TCP_INC_VAR(tp->rcv_waitforss, nlropkts); |
6d2010ae | 2588 | } |
39037602 | 2589 | if (tcp_stretch_ack_enable(tp, thflags)) { |
6d2010ae A |
2590 | tp->t_flags |= TF_STRETCHACK; |
2591 | tp->t_flagsext &= ~(TF_RCVUNACK_WAITSS); | |
2592 | tp->rcv_waitforss = 0; | |
2593 | } else { | |
2594 | tp->t_flags &= ~(TF_STRETCHACK); | |
2595 | } | |
2596 | if (TSTMP_GT(tp->rcv_unackwin, tcp_now)) { | |
2597 | tp->rcv_by_unackwin += (tlen + off); | |
2598 | } else { | |
2599 | tp->rcv_unackwin = tcp_now + tcp_rcvunackwin; | |
2600 | tp->rcv_by_unackwin = tlen + off; | |
2601 | } | |
91447636 | 2602 | } |
316670eb | 2603 | |
39037602 | 2604 | /* |
316670eb A |
2605 | * Keep track of how many bytes were received in the LRO packet |
2606 | */ | |
39236c6e | 2607 | if ((pktf_sw_lro_pkt) && (nlropkts > 2)) { |
316670eb A |
2608 | tp->t_lropktlen += tlen; |
2609 | } | |
2d21ac55 | 2610 | /* |
39236c6e A |
2611 | * Explicit Congestion Notification - Flag that we need to send ECT if |
2612 | * + The IP Congestion experienced flag was set. | |
2613 | * + Socket is in established state | |
2614 | * + We negotiated ECN in the TCP setup | |
2615 | * + This isn't a pure ack (tlen > 0) | |
2616 | * + The data is in the valid window | |
2617 | * | |
2618 | * TE_SENDECE will be cleared when we receive a packet with TH_CWR set. | |
2d21ac55 A |
2619 | */ |
2620 | if (ip_ecn == IPTOS_ECN_CE && tp->t_state == TCPS_ESTABLISHED && | |
3e170ce0 A |
2621 | TCP_ECN_ENABLED(tp) && tlen > 0 && |
2622 | SEQ_GEQ(th->th_seq, tp->last_ack_sent) && | |
2623 | SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) { | |
4bd07ac2 | 2624 | tp->t_ecn_recv_ce++; |
3e170ce0 | 2625 | tcpstat.tcps_ecn_recv_ce++; |
4bd07ac2 | 2626 | INP_INC_IFNET_STAT(inp, ecn_recv_ce); |
3e170ce0 A |
2627 | /* Mark this connection as it received CE from network */ |
2628 | tp->ecn_flags |= TE_RECV_ECN_CE; | |
2d21ac55 A |
2629 | tp->ecn_flags |= TE_SENDECE; |
2630 | } | |
4bd07ac2 | 2631 | |
2d21ac55 | 2632 | /* |
39236c6e A |
2633 | * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't |
2634 | * bother doing extensive checks for state and whatnot. | |
2d21ac55 | 2635 | */ |
3e170ce0 | 2636 | if (thflags & TH_CWR) { |
2d21ac55 | 2637 | tp->ecn_flags &= ~TE_SENDECE; |
4bd07ac2 | 2638 | tp->t_ecn_recv_cwr++; |
2d21ac55 | 2639 | } |
6d2010ae | 2640 | |
39037602 A |
2641 | /* |
2642 | * If we received an explicit notification of congestion in | |
6d2010ae | 2643 | * ip tos ecn bits or by the CWR bit in TCP header flags, reset |
fe8ab488 A |
2644 | * the ack-strteching state. We need to handle ECN notification if |
2645 | * an ECN setup SYN was sent even once. | |
6d2010ae | 2646 | */ |
fe8ab488 A |
2647 | if (tp->t_state == TCPS_ESTABLISHED |
2648 | && (tp->ecn_flags & TE_SETUPSENT) | |
3e170ce0 | 2649 | && (ip_ecn == IPTOS_ECN_CE || (thflags & TH_CWR))) { |
6d2010ae | 2650 | tcp_reset_stretch_ack(tp); |
3e170ce0 A |
2651 | CLEAR_IAJ_STATE(tp); |
2652 | } | |
316670eb | 2653 | |
4bd07ac2 A |
2654 | if (ip_ecn == IPTOS_ECN_CE && tp->t_state == TCPS_ESTABLISHED && |
2655 | !TCP_ECN_ENABLED(tp) && !(tp->ecn_flags & TE_CEHEURI_SET)) { | |
2656 | tcpstat.tcps_ecn_fallback_ce++; | |
2657 | tcp_heuristic_ecn_aggressive(tp); | |
2658 | tp->ecn_flags |= TE_CEHEURI_SET; | |
2659 | } | |
2660 | ||
2661 | if (tp->t_state == TCPS_ESTABLISHED && TCP_ECN_ENABLED(tp) && | |
2662 | ip_ecn == IPTOS_ECN_CE && !(tp->ecn_flags & TE_CEHEURI_SET)) { | |
2663 | if (inp->inp_stat->rxpackets < ECN_MIN_CE_PROBES) { | |
2664 | tp->t_ecn_recv_ce_pkt++; | |
2665 | } else if (tp->t_ecn_recv_ce_pkt > ECN_MAX_CE_RATIO) { | |
2666 | tcpstat.tcps_ecn_fallback_ce++; | |
2667 | tcp_heuristic_ecn_aggressive(tp); | |
2668 | tp->ecn_flags |= TE_CEHEURI_SET; | |
2669 | INP_INC_IFNET_STAT(inp,ecn_fallback_ce); | |
2670 | } else { | |
2671 | /* We tracked the first ECN_MIN_CE_PROBES segments, we | |
2672 | * now know that the path is good. | |
2673 | */ | |
2674 | tp->ecn_flags |= TE_CEHEURI_SET; | |
2675 | } | |
2676 | } | |
2677 | ||
39037602 | 2678 | /* |
316670eb | 2679 | * Try to determine if we are receiving a packet after a long time. |
39037602 | 2680 | * Use our own approximation of idletime to roughly measure remote |
316670eb A |
2681 | * end's idle time. Since slowstart is used after an idle period |
2682 | * we want to avoid doing LRO if the remote end is not up to date | |
2683 | * on initial window support and starts with 1 or 2 packets as its IW. | |
2684 | */ | |
2685 | if (sw_lro && (tp->t_flagsext & TF_LRO_OFFLOADED) && | |
2686 | ((tcp_now - tp->t_rcvtime) >= (TCP_IDLETIMEOUT(tp)))) { | |
2687 | turnoff_lro = 1; | |
2688 | } | |
2689 | ||
39236c6e A |
2690 | /* Update rcvtime as a new segment was received on the connection */ |
2691 | tp->t_rcvtime = tcp_now; | |
2692 | ||
1c79356b A |
2693 | /* |
2694 | * Segment received on connection. | |
2695 | * Reset idle time and keep-alive timer. | |
2696 | */ | |
1c79356b | 2697 | if (TCPS_HAVEESTABLISHED(tp->t_state)) |
39236c6e | 2698 | tcp_keepalive_reset(tp); |
1c79356b A |
2699 | |
2700 | /* | |
2701 | * Process options if not in LISTEN state, | |
2702 | * else do it below (after getting remote address). | |
2703 | */ | |
39236c6e | 2704 | if (tp->t_state != TCPS_LISTEN && optp) { |
3e170ce0 | 2705 | tcp_dooptions(tp, optp, optlen, th, &to); |
39236c6e | 2706 | #if MPTCP |
3e170ce0 A |
2707 | if (mptcp_input_preproc(tp, m, drop_hdrlen) != 0) { |
2708 | tp->t_flags |= TF_ACKNOW; | |
2709 | (void) tcp_output(tp); | |
39236c6e A |
2710 | tcp_check_timer_state(tp); |
2711 | tcp_unlock(so, 1, 0); | |
2712 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | | |
2713 | DBG_FUNC_END,0,0,0,0,0); | |
3e170ce0 | 2714 | return; |
8ad349bb | 2715 | } |
39236c6e A |
2716 | #endif /* MPTCP */ |
2717 | } | |
2718 | if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) { | |
3e170ce0 A |
2719 | if (!(thflags & TH_ACK) || |
2720 | (SEQ_GT(th->th_ack, tp->iss) && | |
2721 | SEQ_LEQ(th->th_ack, tp->snd_max))) | |
2722 | tcp_finalize_options(tp, &to, ifscope); | |
8ad349bb A |
2723 | } |
2724 | ||
6d2010ae | 2725 | #if TRAFFIC_MGT |
3e170ce0 A |
2726 | /* |
2727 | * Compute inter-packet arrival jitter. According to RFC 3550, | |
2728 | * inter-packet arrival jitter is defined as the difference in | |
2729 | * packet spacing at the receiver compared to the sender for a | |
2730 | * pair of packets. When two packets of maximum segment size come | |
2731 | * one after the other with consecutive sequence numbers, we | |
2732 | * consider them as packets sent together at the sender and use | |
2733 | * them as a pair to compute inter-packet arrival jitter. This | |
2734 | * metric indicates the delay induced by the network components due | |
6d2010ae A |
2735 | * to queuing in edge/access routers. |
2736 | */ | |
2737 | if (tp->t_state == TCPS_ESTABLISHED && | |
2738 | (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK|TH_ECE|TH_PUSH)) == TH_ACK && | |
2739 | ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && | |
2740 | ((to.to_flags & TOF_TS) == 0 || | |
2741 | TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && | |
39037602 | 2742 | th->th_seq == tp->rcv_nxt && LIST_EMPTY(&tp->t_segq)) { |
316670eb | 2743 | int seg_size = tlen; |
6d2010ae | 2744 | if (tp->iaj_pktcnt <= IAJ_IGNORE_PKTCNT) { |
316670eb | 2745 | TCP_INC_VAR(tp->iaj_pktcnt, nlropkts); |
6d2010ae A |
2746 | } |
2747 | ||
39236c6e | 2748 | if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) { |
316670eb A |
2749 | seg_size = m->m_pkthdr.lro_pktlen; |
2750 | } | |
2751 | if ( tp->iaj_size == 0 || seg_size > tp->iaj_size || | |
2752 | (seg_size == tp->iaj_size && tp->iaj_rcv_ts == 0)) { | |
3e170ce0 A |
2753 | /* |
2754 | * State related to inter-arrival jitter is | |
2755 | * uninitialized or we are trying to find a good | |
2756 | * first packet to start computing the metric | |
6d2010ae | 2757 | */ |
316670eb | 2758 | update_iaj_state(tp, seg_size, 0); |
6d2010ae | 2759 | } else { |
316670eb | 2760 | if (seg_size == tp->iaj_size) { |
3e170ce0 A |
2761 | /* |
2762 | * Compute inter-arrival jitter taking | |
2763 | * this packet as the second packet | |
6d2010ae | 2764 | */ |
39236c6e A |
2765 | if (pktf_sw_lro_pkt) |
2766 | compute_iaj(tp, nlropkts, | |
2767 | m->m_pkthdr.lro_elapsed); | |
2768 | else | |
2769 | compute_iaj(tp, 1, 0); | |
39037602 | 2770 | } |
316670eb | 2771 | if (seg_size < tp->iaj_size) { |
3e170ce0 A |
2772 | /* |
2773 | * There is a smaller packet in the stream. | |
2774 | * Some times the maximum size supported | |
2775 | * on a path can change if there is a new | |
2776 | * link with smaller MTU. The receiver will | |
2777 | * not know about this change. If there | |
2778 | * are too many packets smaller than | |
2779 | * iaj_size, we try to learn the iaj_size | |
2780 | * again. | |
6d2010ae | 2781 | */ |
39037602 | 2782 | TCP_INC_VAR(tp->iaj_small_pkt, nlropkts); |
6d2010ae | 2783 | if (tp->iaj_small_pkt > RESET_IAJ_SIZE_THRESH) { |
316670eb | 2784 | update_iaj_state(tp, seg_size, 1); |
6d2010ae | 2785 | } else { |
316670eb | 2786 | CLEAR_IAJ_STATE(tp); |
6d2010ae A |
2787 | } |
2788 | } else { | |
316670eb | 2789 | update_iaj_state(tp, seg_size, 0); |
6d2010ae A |
2790 | } |
2791 | } | |
2792 | } else { | |
316670eb | 2793 | CLEAR_IAJ_STATE(tp); |
6d2010ae A |
2794 | } |
2795 | #endif /* TRAFFIC_MGT */ | |
2796 | ||
1c79356b A |
2797 | /* |
2798 | * Header prediction: check for the two common cases | |
2799 | * of a uni-directional data xfer. If the packet has | |
2800 | * no control flags, is in-sequence, the window didn't | |
2801 | * change and we're not retransmitting, it's a | |
2802 | * candidate. If the length is zero and the ack moved | |
2803 | * forward, we're the sender side of the xfer. Just | |
2804 | * free the data acked & wake any higher level process | |
2805 | * that was blocked waiting for space. If the length | |
2806 | * is non-zero and the ack didn't move, we're the | |
2807 | * receiver side. If we're getting packets in-order | |
2808 | * (the reassembly queue is empty), add the data to | |
2809 | * the socket buffer and note that we need a delayed ack. | |
2810 | * Make sure that the hidden state-flags are also off. | |
2811 | * Since we check for TCPS_ESTABLISHED above, it can only | |
2812 | * be TH_NEEDSYN. | |
2813 | */ | |
2814 | if (tp->t_state == TCPS_ESTABLISHED && | |
4bd07ac2 | 2815 | (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK|TH_ECE|TH_CWR)) == TH_ACK && |
1c79356b | 2816 | ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && |
8ad349bb | 2817 | ((to.to_flags & TOF_TS) == 0 || |
1c79356b | 2818 | TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && |
1c79356b A |
2819 | th->th_seq == tp->rcv_nxt && |
2820 | tiwin && tiwin == tp->snd_wnd && | |
2821 | tp->snd_nxt == tp->snd_max) { | |
2822 | ||
2823 | /* | |
2824 | * If last ACK falls within this segment's sequence numbers, | |
2825 | * record the timestamp. | |
2826 | * NOTE that the test is modified according to the latest | |
2827 | * proposal of the tcplw@cray.com list (Braden 1993/04/26). | |
2828 | */ | |
8ad349bb | 2829 | if ((to.to_flags & TOF_TS) != 0 && |
1c79356b A |
2830 | SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { |
2831 | tp->ts_recent_age = tcp_now; | |
2832 | tp->ts_recent = to.to_tsval; | |
2833 | } | |
2834 | ||
9bccf70c | 2835 | if (tlen == 0) { |
1c79356b A |
2836 | if (SEQ_GT(th->th_ack, tp->snd_una) && |
2837 | SEQ_LEQ(th->th_ack, tp->snd_max) && | |
2d21ac55 | 2838 | tp->snd_cwnd >= tp->snd_ssthresh && |
39037602 A |
2839 | (!IN_FASTRECOVERY(tp) && |
2840 | ((!(SACK_ENABLED(tp)) && | |
fe8ab488 A |
2841 | tp->t_dupacks < tp->t_rexmtthresh) || |
2842 | (SACK_ENABLED(tp) && to.to_nsacks == 0 && | |
2843 | TAILQ_EMPTY(&tp->snd_holes))))) { | |
1c79356b A |
2844 | /* |
2845 | * this is a pure ack for outstanding data. | |
2846 | */ | |
2847 | ++tcpstat.tcps_predack; | |
39236c6e | 2848 | |
39037602 | 2849 | tcp_bad_rexmt_check(tp, th, &to); |
39236c6e A |
2850 | |
2851 | /* Recalculate the RTT */ | |
2852 | tcp_compute_rtt(tp, &to, th); | |
2853 | ||
3e170ce0 | 2854 | VERIFY(SEQ_GEQ(th->th_ack, tp->snd_una)); |
39236c6e | 2855 | acked = BYTES_ACKED(th, tp); |
1c79356b A |
2856 | tcpstat.tcps_rcvackpack++; |
2857 | tcpstat.tcps_rcvackbyte += acked; | |
39037602 | 2858 | |
3e170ce0 A |
2859 | /* |
2860 | * Handle an ack that is in sequence during | |
2861 | * congestion avoidance phase. The | |
2862 | * calculations in this function | |
39037602 | 2863 | * assume that snd_una is not updated yet. |
2d21ac55 | 2864 | */ |
fe8ab488 A |
2865 | if (CC_ALGO(tp)->congestion_avd != NULL) |
2866 | CC_ALGO(tp)->congestion_avd(tp, th); | |
2867 | tcp_ccdbg_trace(tp, th, TCP_CC_INSEQ_ACK_RCVD); | |
1c79356b | 2868 | sbdrop(&so->so_snd, acked); |
39236c6e A |
2869 | if (so->so_flags & SOF_ENABLE_MSGS) { |
2870 | VERIFY(acked <= so->so_msg_state->msg_serial_bytes); | |
2871 | so->so_msg_state->msg_serial_bytes -= acked; | |
2872 | } | |
316670eb A |
2873 | tcp_sbsnd_trim(&so->so_snd); |
2874 | ||
8ad349bb A |
2875 | if (SEQ_GT(tp->snd_una, tp->snd_recover) && |
2876 | SEQ_LEQ(th->th_ack, tp->snd_recover)) | |
2877 | tp->snd_recover = th->th_ack - 1; | |
1c79356b | 2878 | tp->snd_una = th->th_ack; |
316670eb | 2879 | |
39037602 A |
2880 | TCP_RESET_REXMT_STATE(tp); |
2881 | ||
8ad349bb A |
2882 | /* |
2883 | * pull snd_wl2 up to prevent seq wrap relative | |
2884 | * to th_ack. | |
2885 | */ | |
2886 | tp->snd_wl2 = th->th_ack; | |
39236c6e A |
2887 | |
2888 | if (tp->t_dupacks > 0) { | |
2889 | tp->t_dupacks = 0; | |
2890 | tp->t_rexmtthresh = tcprexmtthresh; | |
2891 | } | |
2892 | ||
1c79356b | 2893 | m_freem(m); |
1c79356b A |
2894 | |
2895 | /* | |
2896 | * If all outstanding data are acked, stop | |
2897 | * retransmit timer, otherwise restart timer | |
2898 | * using current (possibly backed-off) value. | |
2899 | * If process is waiting for space, | |
2900 | * wakeup/selwakeup/signal. If data | |
2901 | * are ready to send, let tcp_output | |
2902 | * decide between more output or persist. | |
2903 | */ | |
fe8ab488 | 2904 | if (tp->snd_una == tp->snd_max) { |
1c79356b | 2905 | tp->t_timer[TCPT_REXMT] = 0; |
fe8ab488 A |
2906 | tp->t_timer[TCPT_PTO] = 0; |
2907 | } else if (tp->t_timer[TCPT_PERSIST] == 0) { | |
2908 | tp->t_timer[TCPT_REXMT] = | |
2909 | OFFSET_FROM_START(tp, | |
2910 | tp->t_rxtcur); | |
2911 | } | |
3e170ce0 A |
2912 | if (!SLIST_EMPTY(&tp->t_rxt_segments) && |
2913 | !TCP_DSACK_SEQ_IN_WINDOW(tp, | |
2914 | tp->t_dsack_lastuna, tp->snd_una)) | |
2915 | tcp_rxtseg_clean(tp); | |
1c79356b | 2916 | |
316670eb A |
2917 | if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 && |
2918 | tp->t_bwmeas != NULL) | |
2919 | tcp_bwmeas_check(tp); | |
39037602 | 2920 | |
91447636 | 2921 | sowwakeup(so); /* has to be done with socket lock held */ |
39037602 A |
2922 | if (!SLIST_EMPTY(&tp->t_notify_ack)) |
2923 | tcp_notify_acknowledgement(tp, so); | |
2924 | ||
2d21ac55 | 2925 | if ((so->so_snd.sb_cc) || (tp->t_flags & TF_ACKNOW)) { |
1c79356b | 2926 | (void) tcp_output(tp); |
2d21ac55 | 2927 | } |
6d2010ae | 2928 | |
3e170ce0 A |
2929 | tcp_tfo_rcv_ack(tp, th); |
2930 | ||
6d2010ae | 2931 | tcp_check_timer_state(tp); |
91447636 | 2932 | tcp_unlock(so, 1, 0); |
1c79356b A |
2933 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); |
2934 | return; | |
2935 | } | |
2936 | } else if (th->th_ack == tp->snd_una && | |
9bccf70c | 2937 | LIST_EMPTY(&tp->t_segq) && |
2d21ac55 | 2938 | tlen <= tcp_sbspace(tp)) { |
1c79356b A |
2939 | /* |
2940 | * this is a pure, in-sequence data packet | |
2941 | * with nothing on the reassembly queue and | |
2942 | * we have enough buffer space to take it. | |
2943 | */ | |
316670eb A |
2944 | |
2945 | /* | |
2946 | * If this is a connection in steady state, start | |
2947 | * coalescing packets belonging to this flow. | |
2948 | */ | |
2949 | if (turnoff_lro) { | |
2950 | tcp_lro_remove_state(tp->t_inpcb->inp_laddr, | |
39236c6e | 2951 | tp->t_inpcb->inp_faddr, |
39037602 | 2952 | tp->t_inpcb->inp_lport, |
39236c6e | 2953 | tp->t_inpcb->inp_fport); |
316670eb A |
2954 | tp->t_flagsext &= ~TF_LRO_OFFLOADED; |
2955 | tp->t_idleat = tp->rcv_nxt; | |
39236c6e | 2956 | } else if (sw_lro && !pktf_sw_lro_pkt && !isipv6 && |
39037602 | 2957 | (so->so_flags & SOF_USELRO) && |
39236c6e | 2958 | !IFNET_IS_CELLULAR(m->m_pkthdr.rcvif) && |
316670eb | 2959 | (m->m_pkthdr.rcvif->if_type != IFT_LOOP) && |
39037602 | 2960 | ((th->th_seq - tp->irs) > |
39236c6e | 2961 | (tp->t_maxseg << lro_start)) && |
39037602 | 2962 | ((tp->t_idleat == 0) || ((th->th_seq - |
316670eb A |
2963 | tp->t_idleat) > (tp->t_maxseg << lro_start)))) { |
2964 | tp->t_flagsext |= TF_LRO_OFFLOADED; | |
2965 | tcp_start_coalescing(ip, th, tlen); | |
2966 | tp->t_idleat = 0; | |
2967 | } | |
2968 | ||
8ad349bb | 2969 | /* Clean receiver SACK report if present */ |
39236c6e | 2970 | if (SACK_ENABLED(tp) && tp->rcv_numsacks) |
8ad349bb | 2971 | tcp_clean_sackreport(tp); |
1c79356b | 2972 | ++tcpstat.tcps_preddat; |
9bccf70c | 2973 | tp->rcv_nxt += tlen; |
8ad349bb A |
2974 | /* |
2975 | * Pull snd_wl1 up to prevent seq wrap relative to | |
2976 | * th_seq. | |
2977 | */ | |
2978 | tp->snd_wl1 = th->th_seq; | |
2979 | /* | |
2980 | * Pull rcv_up up to prevent seq wrap relative to | |
2981 | * rcv_nxt. | |
2982 | */ | |
2983 | tp->rcv_up = tp->rcv_nxt; | |
316670eb | 2984 | TCP_INC_VAR(tcpstat.tcps_rcvpack, nlropkts); |
9bccf70c | 2985 | tcpstat.tcps_rcvbyte += tlen; |
6d2010ae | 2986 | if (nstat_collect) { |
39236c6e | 2987 | if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) { |
39037602 | 2988 | INP_ADD_STAT(inp, cell, wifi, wired, |
fe8ab488 | 2989 | rxpackets, m->m_pkthdr.lro_npkts); |
39236c6e | 2990 | } else { |
fe8ab488 A |
2991 | INP_ADD_STAT(inp, cell, wifi, wired, |
2992 | rxpackets, 1); | |
316670eb | 2993 | } |
fe8ab488 A |
2994 | INP_ADD_STAT(inp, cell, wifi, wired,rxbytes, |
2995 | tlen); | |
6d2010ae | 2996 | } |
39236c6e A |
2997 | |
2998 | /* | |
39037602 A |
2999 | * Calculate the RTT on the receiver only if the |
3000 | * connection is in streaming mode and the last | |
39236c6e A |
3001 | * packet was not an end-of-write |
3002 | */ | |
39037602 | 3003 | if (tp->t_flags & TF_STREAMING_ON) |
39236c6e | 3004 | tcp_compute_rtt(tp, &to, th); |
316670eb | 3005 | |
39037602 A |
3006 | tcp_sbrcv_grow(tp, &so->so_rcv, &to, tlen, |
3007 | TCP_AUTORCVBUF_MAX(ifp)); | |
3008 | ||
9bccf70c A |
3009 | /* |
3010 | * Add data to socket buffer. | |
3011 | */ | |
6d2010ae | 3012 | so_recv_data_stat(so, m, 0); |
9bccf70c | 3013 | m_adj(m, drop_hdrlen); /* delayed header drop */ |
39037602 | 3014 | |
39236c6e | 3015 | /* |
39037602 | 3016 | * If message delivery (SOF_ENABLE_MSGS) is enabled on |
39236c6e A |
3017 | * this socket, deliver the packet received as an |
3018 | * in-order message with sequence number attached to it. | |
3019 | */ | |
39037602 | 3020 | if (sbappendstream_rcvdemux(so, m, |
39236c6e | 3021 | th->th_seq - (tp->irs + 1), 0)) { |
91447636 | 3022 | sorwakeup(so); |
39037602 | 3023 | } |
9bccf70c A |
3024 | #if INET6 |
3025 | if (isipv6) { | |
3026 | KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport), | |
3027 | (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])), | |
39037602 | 3028 | th->th_seq, th->th_ack, th->th_win); |
9bccf70c A |
3029 | } |
3030 | else | |
39037602 | 3031 | #endif |
9bccf70c A |
3032 | { |
3033 | KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport), | |
3034 | (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)), | |
39037602 | 3035 | th->th_seq, th->th_ack, th->th_win); |
9bccf70c | 3036 | } |
316670eb | 3037 | TCP_INC_VAR(tp->t_unacksegs, nlropkts); |
6d2010ae A |
3038 | if (DELAY_ACK(tp, th)) { |
3039 | if ((tp->t_flags & TF_DELACK) == 0) { | |
3040 | tp->t_flags |= TF_DELACK; | |
3041 | tp->t_timer[TCPT_DELACK] = OFFSET_FROM_START(tp, tcp_delack); | |
3042 | } | |
1c79356b A |
3043 | } else { |
3044 | tp->t_flags |= TF_ACKNOW; | |
3045 | tcp_output(tp); | |
3046 | } | |
39236c6e A |
3047 | |
3048 | tcp_adaptive_rwtimo_check(tp, tlen); | |
3049 | ||
3e170ce0 A |
3050 | if (tlen > 0) |
3051 | tcp_tfo_rcv_data(tp); | |
3052 | ||
6d2010ae | 3053 | tcp_check_timer_state(tp); |
91447636 | 3054 | tcp_unlock(so, 1, 0); |
1c79356b A |
3055 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); |
3056 | return; | |
3057 | } | |
3058 | } | |
3059 | ||
3060 | /* | |
3061 | * Calculate amount of space in receive window, | |
3062 | * and then do TCP input processing. | |
3063 | * Receive window is amount of space in rcv queue, | |
3064 | * but not less than advertised window. | |
3065 | */ | |
fe8ab488 A |
3066 | lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx, |
3067 | LCK_MTX_ASSERT_OWNED); | |
2d21ac55 | 3068 | win = tcp_sbspace(tp); |
1c79356b A |
3069 | if (win < 0) |
3070 | win = 0; | |
d12e1678 A |
3071 | else { /* clip rcv window to 4K for modems */ |
3072 | if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) | |
3073 | win = min(win, slowlink_wsize); | |
3074 | } | |
1c79356b | 3075 | tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); |
39236c6e A |
3076 | #if MPTCP |
3077 | /* | |
3078 | * Ensure that the subflow receive window isn't greater | |
3079 | * than the connection level receive window. | |
3080 | */ | |
3081 | if ((tp->t_mpflags & TMPF_MPTCP_TRUE) && | |
3082 | (mp_tp = tptomptp(tp))) { | |
3083 | MPT_LOCK(mp_tp); | |
3084 | if (tp->rcv_wnd > mp_tp->mpt_rcvwnd) { | |
39037602 | 3085 | tp->rcv_wnd = imax(mp_tp->mpt_rcvwnd, (int)(tp->rcv_adv - tp->rcv_nxt)); |
39236c6e A |
3086 | tcpstat.tcps_mp_reducedwin++; |
3087 | } | |
3088 | MPT_UNLOCK(mp_tp); | |
1c79356b | 3089 | } |
39236c6e | 3090 | #endif /* MPTCP */ |
1c79356b A |
3091 | |
3092 | switch (tp->t_state) { | |
3093 | ||
3094 | /* | |
7e4a7d39 | 3095 | * Initialize tp->rcv_nxt, and tp->irs, select an initial |
1c79356b A |
3096 | * tp->iss, and send a segment: |
3097 | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> | |
3098 | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. | |
3099 | * Fill in remote peer address fields if not previously specified. | |
3100 | * Enter SYN_RECEIVED state, and process any other fields of this | |
3101 | * segment in this state. | |
3102 | */ | |
3103 | case TCPS_LISTEN: { | |
39037602 | 3104 | struct sockaddr_in *sin; |
9bccf70c | 3105 | #if INET6 |
39037602 | 3106 | struct sockaddr_in6 *sin6; |
1c79356b A |
3107 | #endif |
3108 | ||
fe8ab488 A |
3109 | lck_mtx_assert(&((struct inpcb *)so->so_pcb)->inpcb_mtx, |
3110 | LCK_MTX_ASSERT_OWNED); | |
9bccf70c A |
3111 | #if INET6 |
3112 | if (isipv6) { | |
1c79356b A |
3113 | MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, |
3114 | M_SONAME, M_NOWAIT); | |
3115 | if (sin6 == NULL) | |
3116 | goto drop; | |
3117 | bzero(sin6, sizeof(*sin6)); | |
3118 | sin6->sin6_family = AF_INET6; | |
3119 | sin6->sin6_len = sizeof(*sin6); | |
3120 | sin6->sin6_addr = ip6->ip6_src; | |
3121 | sin6->sin6_port = th->th_sport; | |
3122 | laddr6 = inp->in6p_laddr; | |
3123 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) | |
3124 | inp->in6p_laddr = ip6->ip6_dst; | |
3125 | if (in6_pcbconnect(inp, (struct sockaddr *)sin6, | |
9bccf70c | 3126 | proc0)) { |
1c79356b A |
3127 | inp->in6p_laddr = laddr6; |
3128 | FREE(sin6, M_SONAME); | |
3129 | goto drop; | |
3130 | } | |
3131 | FREE(sin6, M_SONAME); | |
9bccf70c | 3132 | } else |
1c79356b | 3133 | #endif |
9bccf70c | 3134 | { |
3e170ce0 A |
3135 | lck_mtx_assert( |
3136 | &((struct inpcb *)so->so_pcb)->inpcb_mtx, | |
3137 | LCK_MTX_ASSERT_OWNED); | |
1c79356b | 3138 | MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, |
9bccf70c | 3139 | M_NOWAIT); |
1c79356b A |
3140 | if (sin == NULL) |
3141 | goto drop; | |
3142 | sin->sin_family = AF_INET; | |
3143 | sin->sin_len = sizeof(*sin); | |
3144 | sin->sin_addr = ip->ip_src; | |
3145 | sin->sin_port = th->th_sport; | |
3146 | bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); | |
3147 | laddr = inp->inp_laddr; | |
3148 | if (inp->inp_laddr.s_addr == INADDR_ANY) | |
3149 | inp->inp_laddr = ip->ip_dst; | |
39236c6e A |
3150 | if (in_pcbconnect(inp, (struct sockaddr *)sin, proc0, |
3151 | IFSCOPE_NONE, NULL)) { | |
1c79356b A |
3152 | inp->inp_laddr = laddr; |
3153 | FREE(sin, M_SONAME); | |
3154 | goto drop; | |
3155 | } | |
3156 | FREE(sin, M_SONAME); | |
1c79356b | 3157 | } |
8ad349bb | 3158 | |
3e170ce0 A |
3159 | tcp_dooptions(tp, optp, optlen, th, &to); |
3160 | tcp_finalize_options(tp, &to, ifscope); | |
3161 | ||
3162 | if (tfo_enabled(tp) && tcp_tfo_syn(tp, &to)) | |
3163 | isconnected = TRUE; | |
8ad349bb | 3164 | |
1c79356b A |
3165 | if (iss) |
3166 | tp->iss = iss; | |
0b4e3aa0 | 3167 | else { |
9bccf70c A |
3168 | tp->iss = tcp_new_isn(tp); |
3169 | } | |
1c79356b A |
3170 | tp->irs = th->th_seq; |
3171 | tcp_sendseqinit(tp); | |
3172 | tcp_rcvseqinit(tp); | |
9bccf70c | 3173 | tp->snd_recover = tp->snd_una; |
1c79356b A |
3174 | /* |
3175 | * Initialization of the tcpcb for transaction; | |
3176 | * set SND.WND = SEG.WND, | |
3177 | * initialize CCsend and CCrecv. | |
3178 | */ | |
3179 | tp->snd_wnd = tiwin; /* initial send-window */ | |
1c79356b | 3180 | tp->t_flags |= TF_ACKNOW; |
2d21ac55 | 3181 | tp->t_unacksegs = 0; |
6d2010ae A |
3182 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
3183 | struct tcpcb *, tp, int32_t, TCPS_SYN_RECEIVED); | |
1c79356b | 3184 | tp->t_state = TCPS_SYN_RECEIVED; |
39037602 | 3185 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, |
39236c6e | 3186 | TCP_CONN_KEEPINIT(tp)); |
1c79356b | 3187 | dropsocket = 0; /* committed to socket */ |
6d2010ae | 3188 | |
316670eb A |
3189 | if (inp->inp_flowhash == 0) |
3190 | inp->inp_flowhash = inp_calc_flowhash(inp); | |
39236c6e A |
3191 | #if INET6 |
3192 | /* update flowinfo - RFC 6437 */ | |
3193 | if (inp->inp_flow == 0 && | |
3194 | inp->in6p_flags & IN6P_AUTOFLOWLABEL) { | |
3195 | inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; | |
3196 | inp->inp_flow |= | |
3197 | (htonl(inp->inp_flowhash) & IPV6_FLOWLABEL_MASK); | |
3198 | } | |
3199 | #endif /* INET6 */ | |
316670eb | 3200 | |
6d2010ae A |
3201 | /* reset the incomp processing flag */ |
3202 | so->so_flags &= ~(SOF_INCOMP_INPROGRESS); | |
1c79356b | 3203 | tcpstat.tcps_accepts++; |
2d21ac55 A |
3204 | if ((thflags & (TH_ECE | TH_CWR)) == (TH_ECE | TH_CWR)) { |
3205 | /* ECN-setup SYN */ | |
3206 | tp->ecn_flags |= (TE_SETUPRECEIVED | TE_SENDIPECT); | |
3207 | } | |
316670eb | 3208 | |
1c79356b A |
3209 | goto trimthenstep6; |
3210 | } | |
3211 | ||
3212 | /* | |
3e170ce0 A |
3213 | * If the state is SYN_RECEIVED and the seg contains an ACK, |
3214 | * but not for our SYN/ACK, send a RST. | |
1c79356b A |
3215 | */ |
3216 | case TCPS_SYN_RECEIVED: | |
3217 | if ((thflags & TH_ACK) && | |
3218 | (SEQ_LEQ(th->th_ack, tp->snd_una) || | |
9bccf70c A |
3219 | SEQ_GT(th->th_ack, tp->snd_max))) { |
3220 | rstreason = BANDLIM_RST_OPENPORT; | |
39236c6e | 3221 | IF_TCP_STATINC(ifp, ooopacket); |
1c79356b | 3222 | goto dropwithreset; |
9bccf70c | 3223 | } |
39236c6e A |
3224 | |
3225 | /* | |
3226 | * In SYN_RECEIVED state, if we recv some SYNS with | |
3227 | * window scale and others without, window scaling should | |
3228 | * be disabled. Otherwise the window advertised will be | |
3229 | * lower if we assume scaling and the other end does not. | |
3230 | */ | |
3231 | if ((thflags & TH_SYN) && | |
3e170ce0 | 3232 | (tp->irs == th->th_seq) && |
39236c6e A |
3233 | !(to.to_flags & TOF_SCALE)) |
3234 | tp->t_flags &= ~TF_RCVD_SCALE; | |
1c79356b A |
3235 | break; |
3236 | ||
3237 | /* | |
3238 | * If the state is SYN_SENT: | |
3239 | * if seg contains an ACK, but not for our SYN, drop the input. | |
3240 | * if seg contains a RST, then drop the connection. | |
3241 | * if seg does not contain SYN, then drop it. | |
3242 | * Otherwise this is an acceptable SYN segment | |
3243 | * initialize tp->rcv_nxt and tp->irs | |
3244 | * if seg contains ack then advance tp->snd_una | |
3245 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state | |
3246 | * arrange for segment to be acked (eventually) | |
3247 | * continue processing rest of data/controls, beginning with URG | |
3248 | */ | |
3249 | case TCPS_SYN_SENT: | |
1c79356b A |
3250 | if ((thflags & TH_ACK) && |
3251 | (SEQ_LEQ(th->th_ack, tp->iss) || | |
3252 | SEQ_GT(th->th_ack, tp->snd_max))) { | |
8ad349bb | 3253 | rstreason = BANDLIM_UNLIMITED; |
39236c6e | 3254 | IF_TCP_STATINC(ifp, ooopacket); |
8ad349bb | 3255 | goto dropwithreset; |
1c79356b A |
3256 | } |
3257 | if (thflags & TH_RST) { | |
2d21ac55 | 3258 | if ((thflags & TH_ACK) != 0) { |
fe8ab488 A |
3259 | #if MPTCP |
3260 | if ((so->so_flags & SOF_MPTCP_FASTJOIN) && | |
3261 | SEQ_GT(th->th_ack, tp->iss+1)) { | |
3262 | so->so_flags &= ~SOF_MPTCP_FASTJOIN; | |
3263 | /* ignore the RST and retransmit SYN */ | |
3264 | goto drop; | |
3265 | } | |
3266 | #endif /* MPTCP */ | |
39037602 | 3267 | soevent(so, |
316670eb A |
3268 | (SO_FILT_HINT_LOCKED | |
3269 | SO_FILT_HINT_CONNRESET)); | |
1c79356b A |
3270 | tp = tcp_drop(tp, ECONNREFUSED); |
3271 | postevent(so, 0, EV_RESET); | |
2d21ac55 | 3272 | } |
1c79356b A |
3273 | goto drop; |
3274 | } | |
3275 | if ((thflags & TH_SYN) == 0) | |
3276 | goto drop; | |
3277 | tp->snd_wnd = th->th_win; /* initial send window */ | |
1c79356b A |
3278 | |
3279 | tp->irs = th->th_seq; | |
3280 | tcp_rcvseqinit(tp); | |
3281 | if (thflags & TH_ACK) { | |
1c79356b | 3282 | tcpstat.tcps_connects++; |
39037602 | 3283 | |
2d21ac55 A |
3284 | if ((thflags & (TH_ECE | TH_CWR)) == (TH_ECE)) { |
3285 | /* ECN-setup SYN-ACK */ | |
3286 | tp->ecn_flags |= TE_SETUPRECEIVED; | |
4bd07ac2 | 3287 | if (TCP_ECN_ENABLED(tp)) { |
39037602 | 3288 | tcp_heuristic_ecn_success(tp); |
3e170ce0 | 3289 | tcpstat.tcps_ecn_client_success++; |
4bd07ac2 | 3290 | } |
3e170ce0 A |
3291 | } else { |
3292 | if (tp->ecn_flags & TE_SETUPSENT && | |
4bd07ac2 | 3293 | tp->t_rxtshift == 0) { |
39037602 | 3294 | tcp_heuristic_ecn_success(tp); |
3e170ce0 | 3295 | tcpstat.tcps_ecn_not_supported++; |
4bd07ac2 A |
3296 | } |
3297 | if (tp->ecn_flags & TE_SETUPSENT && | |
3298 | tp->t_rxtshift > 0) | |
39037602 | 3299 | tcp_heuristic_ecn_loss(tp); |
4bd07ac2 | 3300 | |
2d21ac55 A |
3301 | /* non-ECN-setup SYN-ACK */ |
3302 | tp->ecn_flags &= ~TE_SENDIPECT; | |
3303 | } | |
39037602 | 3304 | |
2d21ac55 A |
3305 | #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET |
3306 | /* XXXMAC: recursive lock: SOCK_LOCK(so); */ | |
3307 | mac_socketpeer_label_associate_mbuf(m, so); | |
3308 | /* XXXMAC: SOCK_UNLOCK(so); */ | |
3309 | #endif | |
1c79356b | 3310 | /* Do window scaling on this connection? */ |
3e170ce0 | 3311 | if (TCP_WINDOW_SCALE_ENABLED(tp)) { |
1c79356b A |
3312 | tp->snd_scale = tp->requested_s_scale; |
3313 | tp->rcv_scale = tp->request_r_scale; | |
3314 | } | |
3e170ce0 | 3315 | |
316670eb | 3316 | tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN << tp->rcv_scale); |
1c79356b | 3317 | tp->snd_una++; /* SYN is acked */ |
3e170ce0 A |
3318 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
3319 | tp->snd_nxt = tp->snd_una; | |
3320 | ||
3321 | /* | |
3322 | * We have sent more in the SYN than what is being | |
3323 | * acked. (e.g., TFO) | |
3324 | * We should restart the sending from what the receiver | |
3325 | * has acknowledged immediately. | |
3326 | */ | |
3327 | if (SEQ_GT(tp->snd_nxt, th->th_ack)) | |
490019cf | 3328 | tp->snd_max = tp->snd_nxt = th->th_ack; |
3e170ce0 | 3329 | |
1c79356b A |
3330 | /* |
3331 | * If there's data, delay ACK; if there's also a FIN | |
3332 | * ACKNOW will be turned on later. | |
3333 | */ | |
316670eb A |
3334 | TCP_INC_VAR(tp->t_unacksegs, nlropkts); |
3335 | if (DELAY_ACK(tp, th) && tlen != 0 ) { | |
6d2010ae A |
3336 | if ((tp->t_flags & TF_DELACK) == 0) { |
3337 | tp->t_flags |= TF_DELACK; | |
3338 | tp->t_timer[TCPT_DELACK] = OFFSET_FROM_START(tp, tcp_delack); | |
3339 | } | |
1c79356b | 3340 | } |
91447636 | 3341 | else { |
1c79356b | 3342 | tp->t_flags |= TF_ACKNOW; |
91447636 | 3343 | } |
1c79356b A |
3344 | /* |
3345 | * Received <SYN,ACK> in SYN_SENT[*] state. | |
3346 | * Transitions: | |
3347 | * SYN_SENT --> ESTABLISHED | |
3348 | * SYN_SENT* --> FIN_WAIT_1 | |
3349 | */ | |
6d2010ae | 3350 | tp->t_starttime = tcp_now; |
316670eb | 3351 | tcp_sbrcv_tstmp_check(tp); |
1c79356b | 3352 | if (tp->t_flags & TF_NEEDFIN) { |
3e170ce0 A |
3353 | DTRACE_TCP4(state__change, void, NULL, |
3354 | struct inpcb *, inp, | |
3355 | struct tcpcb *, tp, int32_t, | |
3356 | TCPS_FIN_WAIT_1); | |
1c79356b A |
3357 | tp->t_state = TCPS_FIN_WAIT_1; |
3358 | tp->t_flags &= ~TF_NEEDFIN; | |
3359 | thflags &= ~TH_SYN; | |
3360 | } else { | |
3e170ce0 A |
3361 | DTRACE_TCP4(state__change, void, NULL, |
3362 | struct inpcb *, inp, struct tcpcb *, | |
3363 | tp, int32_t, TCPS_ESTABLISHED); | |
1c79356b | 3364 | tp->t_state = TCPS_ESTABLISHED; |
3e170ce0 A |
3365 | tp->t_timer[TCPT_KEEP] = |
3366 | OFFSET_FROM_START(tp, | |
3367 | TCP_CONN_KEEPIDLE(tp)); | |
6d2010ae | 3368 | if (nstat_collect) |
3e170ce0 | 3369 | nstat_route_connect_success( |
39037602 A |
3370 | inp->inp_route.ro_rt); |
3371 | /* | |
3372 | * The SYN is acknowledged but una is not | |
3373 | * updated yet. So pass the value of | |
3374 | * ack to compute sndbytes correctly | |
3375 | */ | |
3376 | inp_count_sndbytes(inp, th->th_ack); | |
1c79356b | 3377 | } |
39236c6e A |
3378 | #if MPTCP |
3379 | /* | |
3380 | * Do not send the connect notification for additional | |
3381 | * subflows until ACK for 3-way handshake arrives. | |
3382 | */ | |
3383 | if ((!(tp->t_mpflags & TMPF_MPTCP_TRUE)) && | |
3384 | (tp->t_mpflags & TMPF_SENT_JOIN)) { | |
3385 | isconnected = FALSE; | |
fe8ab488 A |
3386 | /* Start data xmit if fastjoin */ |
3387 | if (mptcp_fastjoin && (so->so_flags & SOF_MPTCP_FASTJOIN)) { | |
39037602 | 3388 | soevent(so, (SO_FILT_HINT_LOCKED | |
fe8ab488 A |
3389 | SO_FILT_HINT_MPFASTJ)); |
3390 | } | |
39236c6e A |
3391 | } else |
3392 | #endif /* MPTCP */ | |
3393 | isconnected = TRUE; | |
3e170ce0 A |
3394 | |
3395 | if (tp->t_tfo_flags & (TFO_F_COOKIE_REQ | TFO_F_COOKIE_SENT)) { | |
3396 | tcp_tfo_synack(tp, &to); | |
3397 | ||
3398 | if ((tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) && | |
3399 | SEQ_LT(tp->snd_una, th->th_ack)) { | |
3400 | tp->t_tfo_stats |= TFO_S_SYN_DATA_ACKED; | |
3401 | tcpstat.tcps_tfo_syn_data_acked++; | |
490019cf A |
3402 | #if MPTCP |
3403 | if (so->so_flags & SOF_MP_SUBFLOW) | |
3404 | so->so_flags1 |= SOF1_TFO_REWIND; | |
3405 | #endif | |
3e170ce0 A |
3406 | if (!(tp->t_tfo_flags & TFO_F_NO_RCVPROBING)) |
3407 | tcp_tfo_rcv_probe(tp, tlen); | |
3408 | } | |
3409 | } | |
1c79356b | 3410 | } else { |
6d2010ae A |
3411 | /* |
3412 | * Received initial SYN in SYN-SENT[*] state => simul- | |
3413 | * taneous open. If segment contains CC option and there is | |
3414 | * a cached CC, apply TAO test; if it succeeds, connection is | |
3415 | * half-synchronized. Otherwise, do 3-way handshake: | |
3416 | * SYN-SENT -> SYN-RECEIVED | |
3417 | * SYN-SENT* -> SYN-RECEIVED* | |
3418 | */ | |
1c79356b A |
3419 | tp->t_flags |= TF_ACKNOW; |
3420 | tp->t_timer[TCPT_REXMT] = 0; | |
6d2010ae A |
3421 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
3422 | struct tcpcb *, tp, int32_t, TCPS_SYN_RECEIVED); | |
8ad349bb A |
3423 | tp->t_state = TCPS_SYN_RECEIVED; |
3424 | ||
3e170ce0 A |
3425 | /* |
3426 | * During simultaneous open, TFO should not be used. | |
3427 | * So, we disable it here, to prevent that data gets | |
3428 | * sent on the SYN/ACK. | |
3429 | */ | |
3430 | tcp_disable_tfo(tp); | |
1c79356b A |
3431 | } |
3432 | ||
3433 | trimthenstep6: | |
3434 | /* | |
3435 | * Advance th->th_seq to correspond to first data byte. | |
3436 | * If data, trim to stay within window, | |
3437 | * dropping FIN if necessary. | |
3438 | */ | |
3439 | th->th_seq++; | |
9bccf70c A |
3440 | if (tlen > tp->rcv_wnd) { |
3441 | todrop = tlen - tp->rcv_wnd; | |
1c79356b | 3442 | m_adj(m, -todrop); |
9bccf70c | 3443 | tlen = tp->rcv_wnd; |
1c79356b A |
3444 | thflags &= ~TH_FIN; |
3445 | tcpstat.tcps_rcvpackafterwin++; | |
3446 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
3447 | } | |
3448 | tp->snd_wl1 = th->th_seq - 1; | |
3449 | tp->rcv_up = th->th_seq; | |
3450 | /* | |
3451 | * Client side of transaction: already sent SYN and data. | |
3452 | * If the remote host used T/TCP to validate the SYN, | |
3453 | * our data will be ACK'd; if so, enter normal data segment | |
3454 | * processing in the middle of step 5, ack processing. | |
3455 | * Otherwise, goto step 6. | |
3456 | */ | |
3457 | if (thflags & TH_ACK) | |
3458 | goto process_ACK; | |
3459 | goto step6; | |
3460 | /* | |
3461 | * If the state is LAST_ACK or CLOSING or TIME_WAIT: | |
8ad349bb | 3462 | * do normal processing. |
1c79356b | 3463 | * |
8ad349bb | 3464 | * NB: Leftover from RFC1644 T/TCP. Cases to be reused later. |
1c79356b A |
3465 | */ |
3466 | case TCPS_LAST_ACK: | |
3467 | case TCPS_CLOSING: | |
3468 | case TCPS_TIME_WAIT: | |
1c79356b | 3469 | break; /* continue normal processing */ |
55e303ae A |
3470 | |
3471 | /* Received a SYN while connection is already established. | |
3472 | * This is a "half open connection and other anomalies" described | |
3473 | * in RFC793 page 34, send an ACK so the remote reset the connection | |
39037602 | 3474 | * or recovers by adjusting its sequence numberering |
55e303ae A |
3475 | */ |
3476 | case TCPS_ESTABLISHED: | |
3e170ce0 A |
3477 | if (thflags & TH_SYN) |
3478 | goto dropafterack; | |
55e303ae | 3479 | break; |
1c79356b A |
3480 | } |
3481 | ||
3482 | /* | |
3483 | * States other than LISTEN or SYN_SENT. | |
3484 | * First check the RST flag and sequence number since reset segments | |
3485 | * are exempt from the timestamp and connection count tests. This | |
3486 | * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix | |
3487 | * below which allowed reset segments in half the sequence space | |
3488 | * to fall though and be processed (which gives forged reset | |
3489 | * segments with a random sequence number a 50 percent chance of | |
3490 | * killing a connection). | |
3491 | * Then check timestamp, if present. | |
3492 | * Then check the connection count, if present. | |
3493 | * Then check that at least some bytes of segment are within | |
3494 | * receive window. If segment begins before rcv_nxt, | |
3495 | * drop leading data (and SYN); if nothing left, just ack. | |
3496 | * | |
3497 | * | |
3498 | * If the RST bit is set, check the sequence number to see | |
3499 | * if this is a valid reset segment. | |
3500 | * RFC 793 page 37: | |
3501 | * In all states except SYN-SENT, all reset (RST) segments | |
3502 | * are validated by checking their SEQ-fields. A reset is | |
3503 | * valid if its sequence number is in the window. | |
3504 | * Note: this does not take into account delayed ACKs, so | |
3505 | * we should test against last_ack_sent instead of rcv_nxt. | |
9bccf70c A |
3506 | * The sequence number in the reset segment is normally an |
3507 | * echo of our outgoing acknowlegement numbers, but some hosts | |
3508 | * send a reset with the sequence number at the rightmost edge | |
3509 | * of our receive window, and we have to handle this case. | |
8ad349bb A |
3510 | * Note 2: Paul Watson's paper "Slipping in the Window" has shown |
3511 | * that brute force RST attacks are possible. To combat this, | |
3512 | * we use a much stricter check while in the ESTABLISHED state, | |
3513 | * only accepting RSTs where the sequence number is equal to | |
3514 | * last_ack_sent. In all other states (the states in which a | |
3515 | * RST is more likely), the more permissive check is used. | |
1c79356b A |
3516 | * If we have multiple segments in flight, the intial reset |
3517 | * segment sequence numbers will be to the left of last_ack_sent, | |
3518 | * but they will eventually catch up. | |
3519 | * In any case, it never made sense to trim reset segments to | |
3520 | * fit the receive window since RFC 1122 says: | |
3521 | * 4.2.2.12 RST Segment: RFC-793 Section 3.4 | |
3522 | * | |
3523 | * A TCP SHOULD allow a received RST segment to include data. | |
3524 | * | |
3525 | * DISCUSSION | |
3526 | * It has been suggested that a RST segment could contain | |
3527 | * ASCII text that encoded and explained the cause of the | |
3528 | * RST. No standard has yet been established for such | |
3529 | * data. | |
3530 | * | |
3531 | * If the reset segment passes the sequence number test examine | |
3532 | * the state: | |
3533 | * SYN_RECEIVED STATE: | |
3534 | * If passive open, return to LISTEN state. | |
3535 | * If active open, inform user that connection was refused. | |
8ad349bb | 3536 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES: |
1c79356b | 3537 | * Inform user that connection was reset, and close tcb. |
9bccf70c | 3538 | * CLOSING, LAST_ACK STATES: |
1c79356b | 3539 | * Close the tcb. |
9bccf70c | 3540 | * TIME_WAIT STATE: |
1c79356b A |
3541 | * Drop the segment - see Stevens, vol. 2, p. 964 and |
3542 | * RFC 1337. | |
0c530ab8 | 3543 | * |
2d21ac55 A |
3544 | * Radar 4803931: Allows for the case where we ACKed the FIN but |
3545 | * there is already a RST in flight from the peer. | |
3546 | * In that case, accept the RST for non-established | |
3547 | * state if it's one off from last_ack_sent. | |
3548 | ||
1c79356b A |
3549 | */ |
3550 | if (thflags & TH_RST) { | |
8ad349bb A |
3551 | if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) && |
3552 | SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) || | |
39037602 A |
3553 | (tp->rcv_wnd == 0 && |
3554 | ((tp->last_ack_sent == th->th_seq) || | |
39236c6e | 3555 | ((tp->last_ack_sent -1) == th->th_seq)))) { |
1c79356b A |
3556 | switch (tp->t_state) { |
3557 | ||
3558 | case TCPS_SYN_RECEIVED: | |
39236c6e | 3559 | IF_TCP_STATINC(ifp, rstinsynrcv); |
1c79356b A |
3560 | so->so_error = ECONNREFUSED; |
3561 | goto close; | |
3562 | ||
3563 | case TCPS_ESTABLISHED: | |
8ad349bb | 3564 | if (tp->last_ack_sent != th->th_seq) { |
2d21ac55 | 3565 | tcpstat.tcps_badrst++; |
8ad349bb A |
3566 | goto drop; |
3567 | } | |
39037602 A |
3568 | if (TCP_ECN_ENABLED(tp) && |
3569 | tp->snd_una == tp->iss + 1 && | |
3570 | SEQ_GT(tp->snd_max, tp->snd_una)) { | |
3571 | /* | |
3572 | * If the first data packet on an | |
3573 | * ECN connection, receives a RST | |
3574 | * increment the heuristic | |
3575 | */ | |
3576 | tcp_heuristic_ecn_droprst(tp); | |
3577 | } | |
1c79356b A |
3578 | case TCPS_FIN_WAIT_1: |
3579 | case TCPS_CLOSE_WAIT: | |
1c79356b A |
3580 | /* |
3581 | Drop through ... | |
3582 | */ | |
3583 | case TCPS_FIN_WAIT_2: | |
3584 | so->so_error = ECONNRESET; | |
3585 | close: | |
3586 | postevent(so, 0, EV_RESET); | |
316670eb A |
3587 | soevent(so, |
3588 | (SO_FILT_HINT_LOCKED | | |
3589 | SO_FILT_HINT_CONNRESET)); | |
3590 | ||
1c79356b A |
3591 | tcpstat.tcps_drops++; |
3592 | tp = tcp_close(tp); | |
3593 | break; | |
3594 | ||
3595 | case TCPS_CLOSING: | |
3596 | case TCPS_LAST_ACK: | |
1c79356b A |
3597 | tp = tcp_close(tp); |
3598 | break; | |
3599 | ||
3600 | case TCPS_TIME_WAIT: | |
3601 | break; | |
3602 | } | |
3603 | } | |
3604 | goto drop; | |
3605 | } | |
3606 | ||
3607 | /* | |
3608 | * RFC 1323 PAWS: If we have a timestamp reply on this segment | |
3609 | * and it's less than ts_recent, drop it. | |
3610 | */ | |
8ad349bb | 3611 | if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent && |
1c79356b A |
3612 | TSTMP_LT(to.to_tsval, tp->ts_recent)) { |
3613 | ||
3614 | /* Check to see if ts_recent is over 24 days old. */ | |
3615 | if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { | |
3616 | /* | |
3617 | * Invalidate ts_recent. If this segment updates | |
3618 | * ts_recent, the age will be reset later and ts_recent | |
3619 | * will get a valid value. If it does not, setting | |
3620 | * ts_recent to zero will at least satisfy the | |
3621 | * requirement that zero be placed in the timestamp | |
3622 | * echo reply when ts_recent isn't valid. The | |
3623 | * age isn't reset until we get a valid ts_recent | |
3624 | * because we don't want out-of-order segments to be | |
3625 | * dropped when ts_recent is old. | |
3626 | */ | |
3627 | tp->ts_recent = 0; | |
3628 | } else { | |
3629 | tcpstat.tcps_rcvduppack++; | |
9bccf70c | 3630 | tcpstat.tcps_rcvdupbyte += tlen; |
4bd07ac2 | 3631 | tp->t_pawsdrop++; |
1c79356b | 3632 | tcpstat.tcps_pawsdrop++; |
4bd07ac2 A |
3633 | |
3634 | /* | |
3635 | * PAWS-drop when ECN is being used? That indicates | |
3636 | * that ECT-marked packets take a different path, with | |
3637 | * different congestion-characteristics. | |
3638 | * | |
3639 | * Only fallback when we did send less than 2GB as PAWS | |
3640 | * really has no reason to kick in earlier. | |
3641 | */ | |
3642 | if (TCP_ECN_ENABLED(tp) && | |
3643 | inp->inp_stat->rxbytes < 2147483648) { | |
3644 | INP_INC_IFNET_STAT(inp, ecn_fallback_reorder); | |
3645 | tcpstat.tcps_ecn_fallback_reorder++; | |
3646 | tcp_heuristic_ecn_aggressive(tp); | |
3647 | } | |
3648 | ||
6d2010ae | 3649 | if (nstat_collect) { |
39037602 | 3650 | nstat_route_rx(tp->t_inpcb->inp_route.ro_rt, |
39236c6e | 3651 | 1, tlen, NSTAT_RX_FLAG_DUPLICATE); |
fe8ab488 A |
3652 | INP_ADD_STAT(inp, cell, wifi, wired, |
3653 | rxpackets, 1); | |
3654 | INP_ADD_STAT(inp, cell, wifi, wired, | |
3655 | rxbytes, tlen); | |
6d2010ae A |
3656 | tp->t_stat.rxduplicatebytes += tlen; |
3657 | } | |
3e170ce0 | 3658 | if (tlen > 0) |
8ad349bb A |
3659 | goto dropafterack; |
3660 | goto drop; | |
1c79356b A |
3661 | } |
3662 | } | |
3663 | ||
1c79356b A |
3664 | /* |
3665 | * In the SYN-RECEIVED state, validate that the packet belongs to | |
3666 | * this connection before trimming the data to fit the receive | |
3667 | * window. Check the sequence number versus IRS since we know | |
3668 | * the sequence numbers haven't wrapped. This is a partial fix | |
3669 | * for the "LAND" DoS attack. | |
3670 | */ | |
9bccf70c A |
3671 | if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) { |
3672 | rstreason = BANDLIM_RST_OPENPORT; | |
39236c6e | 3673 | IF_TCP_STATINC(ifp, dospacket); |
1c79356b | 3674 | goto dropwithreset; |
9bccf70c | 3675 | } |
1c79356b A |
3676 | |
3677 | todrop = tp->rcv_nxt - th->th_seq; | |
3678 | if (todrop > 0) { | |
3679 | if (thflags & TH_SYN) { | |
3680 | thflags &= ~TH_SYN; | |
3681 | th->th_seq++; | |
3682 | if (th->th_urp > 1) | |
3683 | th->th_urp--; | |
3684 | else | |
3685 | thflags &= ~TH_URG; | |
3686 | todrop--; | |
3687 | } | |
3688 | /* | |
3689 | * Following if statement from Stevens, vol. 2, p. 960. | |
3690 | */ | |
9bccf70c A |
3691 | if (todrop > tlen |
3692 | || (todrop == tlen && (thflags & TH_FIN) == 0)) { | |
1c79356b A |
3693 | /* |
3694 | * Any valid FIN must be to the left of the window. | |
3695 | * At this point the FIN must be a duplicate or out | |
3696 | * of sequence; drop it. | |
3697 | */ | |
3698 | thflags &= ~TH_FIN; | |
3699 | ||
3700 | /* | |
3701 | * Send an ACK to resynchronize and drop any data. | |
3702 | * But keep on processing for RST or ACK. | |
3703 | */ | |
3704 | tp->t_flags |= TF_ACKNOW; | |
316670eb A |
3705 | if (todrop == 1) { |
3706 | /* This could be a keepalive */ | |
3707 | soevent(so, SO_FILT_HINT_LOCKED | | |
3708 | SO_FILT_HINT_KEEPALIVE); | |
3709 | } | |
9bccf70c | 3710 | todrop = tlen; |
1c79356b | 3711 | tcpstat.tcps_rcvduppack++; |
39037602 | 3712 | tcpstat.tcps_rcvdupbyte += todrop; |
1c79356b A |
3713 | } else { |
3714 | tcpstat.tcps_rcvpartduppack++; | |
3715 | tcpstat.tcps_rcvpartdupbyte += todrop; | |
3716 | } | |
3e170ce0 A |
3717 | |
3718 | if (TCP_DSACK_ENABLED(tp) && todrop > 1) { | |
3719 | /* | |
3720 | * Note the duplicate data sequence space so that | |
3721 | * it can be reported in DSACK option. | |
3722 | */ | |
3723 | tp->t_dsack_lseq = th->th_seq; | |
3724 | tp->t_dsack_rseq = th->th_seq + todrop; | |
3725 | tp->t_flags |= TF_ACKNOW; | |
3726 | } | |
6d2010ae | 3727 | if (nstat_collect) { |
39037602 | 3728 | nstat_route_rx(tp->t_inpcb->inp_route.ro_rt, 1, |
39236c6e | 3729 | todrop, NSTAT_RX_FLAG_DUPLICATE); |
fe8ab488 A |
3730 | INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1); |
3731 | INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, todrop); | |
6d2010ae A |
3732 | tp->t_stat.rxduplicatebytes += todrop; |
3733 | } | |
9bccf70c | 3734 | drop_hdrlen += todrop; /* drop from the top afterwards */ |
1c79356b | 3735 | th->th_seq += todrop; |
9bccf70c | 3736 | tlen -= todrop; |
1c79356b A |
3737 | if (th->th_urp > todrop) |
3738 | th->th_urp -= todrop; | |
3739 | else { | |
3740 | thflags &= ~TH_URG; | |
3741 | th->th_urp = 0; | |
3742 | } | |
3743 | } | |
3744 | ||
3745 | /* | |
3e170ce0 A |
3746 | * If new data are received on a connection after the user |
3747 | * processes are gone, then RST the other end. | |
3748 | * Send also a RST when we received a data segment after we've | |
3749 | * sent our FIN when the socket is defunct. | |
3750 | * Note that an MPTCP subflow socket would have SS_NOFDREF set | |
3751 | * by default so check to make sure that we test for SOF_MP_SUBFLOW | |
3752 | * socket flag (which would be cleared when the socket is closed.) | |
1c79356b | 3753 | */ |
3e170ce0 A |
3754 | if (!(so->so_flags & SOF_MP_SUBFLOW) && tlen && |
3755 | (((so->so_state & SS_NOFDREF) && | |
3756 | tp->t_state > TCPS_CLOSE_WAIT) || | |
3757 | ((so->so_flags & SOF_DEFUNCT) && | |
3758 | tp->t_state > TCPS_FIN_WAIT_1))) { | |
1c79356b A |
3759 | tp = tcp_close(tp); |
3760 | tcpstat.tcps_rcvafterclose++; | |
9bccf70c | 3761 | rstreason = BANDLIM_UNLIMITED; |
39236c6e | 3762 | IF_TCP_STATINC(ifp, cleanup); |
1c79356b A |
3763 | goto dropwithreset; |
3764 | } | |
3765 | ||
3766 | /* | |
3767 | * If segment ends after window, drop trailing data | |
3768 | * (and PUSH and FIN); if nothing left, just ACK. | |
3769 | */ | |
9bccf70c | 3770 | todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd); |
1c79356b A |
3771 | if (todrop > 0) { |
3772 | tcpstat.tcps_rcvpackafterwin++; | |
9bccf70c A |
3773 | if (todrop >= tlen) { |
3774 | tcpstat.tcps_rcvbyteafterwin += tlen; | |
1c79356b A |
3775 | /* |
3776 | * If a new connection request is received | |
3777 | * while in TIME_WAIT, drop the old connection | |
3778 | * and start over if the sequence numbers | |
3779 | * are above the previous ones. | |
3780 | */ | |
3781 | if (thflags & TH_SYN && | |
3782 | tp->t_state == TCPS_TIME_WAIT && | |
3783 | SEQ_GT(th->th_seq, tp->rcv_nxt)) { | |
9bccf70c | 3784 | iss = tcp_new_isn(tp); |
1c79356b | 3785 | tp = tcp_close(tp); |
91447636 | 3786 | tcp_unlock(so, 1, 0); |
1c79356b A |
3787 | goto findpcb; |
3788 | } | |
3789 | /* | |
3790 | * If window is closed can only take segments at | |
3791 | * window edge, and have to drop data and PUSH from | |
3792 | * incoming segments. Continue processing, but | |
3793 | * remember to ack. Otherwise, drop segment | |
3794 | * and ack. | |
3795 | */ | |
3796 | if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { | |
3797 | tp->t_flags |= TF_ACKNOW; | |
3798 | tcpstat.tcps_rcvwinprobe++; | |
3799 | } else | |
3800 | goto dropafterack; | |
3801 | } else | |
3802 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
3803 | m_adj(m, -todrop); | |
9bccf70c | 3804 | tlen -= todrop; |
1c79356b A |
3805 | thflags &= ~(TH_PUSH|TH_FIN); |
3806 | } | |
3807 | ||
3808 | /* | |
3809 | * If last ACK falls within this segment's sequence numbers, | |
3810 | * record its timestamp. | |
39037602 | 3811 | * NOTE: |
8ad349bb A |
3812 | * 1) That the test incorporates suggestions from the latest |
3813 | * proposal of the tcplw@cray.com list (Braden 1993/04/26). | |
3814 | * 2) That updating only on newer timestamps interferes with | |
3815 | * our earlier PAWS tests, so this check should be solely | |
3816 | * predicated on the sequence space of this segment. | |
39037602 A |
3817 | * 3) That we modify the segment boundary check to be |
3818 | * Last.ACK.Sent <= SEG.SEQ + SEG.Len | |
8ad349bb A |
3819 | * instead of RFC1323's |
3820 | * Last.ACK.Sent < SEG.SEQ + SEG.Len, | |
3821 | * This modified check allows us to overcome RFC1323's | |
3822 | * limitations as described in Stevens TCP/IP Illustrated | |
3823 | * Vol. 2 p.869. In such cases, we can still calculate the | |
3824 | * RTT correctly when RCV.NXT == Last.ACK.Sent. | |
1c79356b | 3825 | */ |
8ad349bb A |
3826 | if ((to.to_flags & TOF_TS) != 0 && |
3827 | SEQ_LEQ(th->th_seq, tp->last_ack_sent) && | |
3828 | SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen + | |
3829 | ((thflags & (TH_SYN|TH_FIN)) != 0))) { | |
1c79356b A |
3830 | tp->ts_recent_age = tcp_now; |
3831 | tp->ts_recent = to.to_tsval; | |
3832 | } | |
3833 | ||
3834 | /* | |
3835 | * If a SYN is in the window, then this is an | |
3836 | * error and we send an RST and drop the connection. | |
3837 | */ | |
3838 | if (thflags & TH_SYN) { | |
3839 | tp = tcp_drop(tp, ECONNRESET); | |
9bccf70c | 3840 | rstreason = BANDLIM_UNLIMITED; |
1c79356b | 3841 | postevent(so, 0, EV_RESET); |
39236c6e | 3842 | IF_TCP_STATINC(ifp, synwindow); |
1c79356b A |
3843 | goto dropwithreset; |
3844 | } | |
3845 | ||
3846 | /* | |
3847 | * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN | |
3848 | * flag is on (half-synchronized state), then queue data for | |
3849 | * later processing; else drop segment and return. | |
3850 | */ | |
3851 | if ((thflags & TH_ACK) == 0) { | |
3852 | if (tp->t_state == TCPS_SYN_RECEIVED || | |
3e170ce0 A |
3853 | (tp->t_flags & TF_NEEDSYN)) { |
3854 | if ((tfo_enabled(tp))) { | |
3855 | /* | |
3856 | * So, we received a valid segment while in | |
3857 | * SYN-RECEIVED (TF_NEEDSYN is actually never | |
3858 | * set, so this is dead code). | |
3859 | * As this cannot be an RST (see that if a bit | |
3860 | * higher), and it does not have the ACK-flag | |
3861 | * set, we want to retransmit the SYN/ACK. | |
3862 | * Thus, we have to reset snd_nxt to snd_una to | |
3863 | * trigger the going back to sending of the | |
3864 | * SYN/ACK. This is more consistent with the | |
3865 | * behavior of tcp_output(), which expects | |
3866 | * to send the segment that is pointed to by | |
3867 | * snd_nxt. | |
3868 | */ | |
3869 | tp->snd_nxt = tp->snd_una; | |
3870 | ||
3871 | /* | |
3872 | * We need to make absolutely sure that we are | |
3873 | * going to reply upon a duplicate SYN-segment. | |
3874 | */ | |
3875 | if (th->th_flags & TH_SYN) | |
3876 | needoutput = 1; | |
3877 | } | |
3878 | ||
1c79356b | 3879 | goto step6; |
3e170ce0 | 3880 | } else if (tp->t_flags & TF_ACKNOW) |
2d21ac55 | 3881 | goto dropafterack; |
1c79356b A |
3882 | else |
3883 | goto drop; | |
3884 | } | |
3885 | ||
3886 | /* | |
3887 | * Ack processing. | |
3888 | */ | |
39236c6e | 3889 | |
1c79356b A |
3890 | switch (tp->t_state) { |
3891 | ||
3892 | /* | |
3893 | * In SYN_RECEIVED state, the ack ACKs our SYN, so enter | |
3894 | * ESTABLISHED state and continue processing. | |
3895 | * The ACK was checked above. | |
3896 | */ | |
3897 | case TCPS_SYN_RECEIVED: | |
3898 | ||
3899 | tcpstat.tcps_connects++; | |
1c79356b A |
3900 | |
3901 | /* Do window scaling? */ | |
3e170ce0 | 3902 | if (TCP_WINDOW_SCALE_ENABLED(tp)) { |
1c79356b A |
3903 | tp->snd_scale = tp->requested_s_scale; |
3904 | tp->rcv_scale = tp->request_r_scale; | |
6d2010ae A |
3905 | tp->snd_wnd = th->th_win << tp->snd_scale; |
3906 | tiwin = tp->snd_wnd; | |
1c79356b | 3907 | } |
1c79356b A |
3908 | /* |
3909 | * Make transitions: | |
3910 | * SYN-RECEIVED -> ESTABLISHED | |
3911 | * SYN-RECEIVED* -> FIN-WAIT-1 | |
3912 | */ | |
6d2010ae | 3913 | tp->t_starttime = tcp_now; |
316670eb | 3914 | tcp_sbrcv_tstmp_check(tp); |
1c79356b | 3915 | if (tp->t_flags & TF_NEEDFIN) { |
3e170ce0 A |
3916 | DTRACE_TCP4(state__change, void, NULL, |
3917 | struct inpcb *, inp, | |
3918 | struct tcpcb *, tp, int32_t, TCPS_FIN_WAIT_1); | |
1c79356b A |
3919 | tp->t_state = TCPS_FIN_WAIT_1; |
3920 | tp->t_flags &= ~TF_NEEDFIN; | |
3921 | } else { | |
3e170ce0 A |
3922 | DTRACE_TCP4(state__change, void, NULL, |
3923 | struct inpcb *, inp, | |
3924 | struct tcpcb *, tp, int32_t, TCPS_ESTABLISHED); | |
1c79356b | 3925 | tp->t_state = TCPS_ESTABLISHED; |
39236c6e A |
3926 | tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, |
3927 | TCP_CONN_KEEPIDLE(tp)); | |
6d2010ae | 3928 | if (nstat_collect) |
3e170ce0 A |
3929 | nstat_route_connect_success( |
3930 | tp->t_inpcb->inp_route.ro_rt); | |
39037602 A |
3931 | /* |
3932 | * The SYN is acknowledged but una is not updated | |
3933 | * yet. So pass the value of ack to compute | |
3934 | * sndbytes correctly | |
3935 | */ | |
3936 | inp_count_sndbytes(inp, th->th_ack); | |
1c79356b A |
3937 | } |
3938 | /* | |
3939 | * If segment contains data or ACK, will call tcp_reass() | |
3940 | * later; if not, do so now to pass queued data to user. | |
3941 | */ | |
9bccf70c | 3942 | if (tlen == 0 && (thflags & TH_FIN) == 0) |
2d21ac55 | 3943 | (void) tcp_reass(tp, (struct tcphdr *)0, &tlen, |
39236c6e | 3944 | NULL, ifp); |
1c79356b | 3945 | tp->snd_wl1 = th->th_seq - 1; |
4a3eedf9 | 3946 | |
39236c6e A |
3947 | #if MPTCP |
3948 | /* | |
3949 | * Do not send the connect notification for additional subflows | |
3950 | * until ACK for 3-way handshake arrives. | |
3951 | */ | |
3952 | if ((!(tp->t_mpflags & TMPF_MPTCP_TRUE)) && | |
3953 | (tp->t_mpflags & TMPF_SENT_JOIN)) { | |
3954 | isconnected = FALSE; | |
3955 | } else | |
3956 | #endif /* MPTCP */ | |
3957 | isconnected = TRUE; | |
3e170ce0 A |
3958 | if ((tp->t_tfo_flags & TFO_F_COOKIE_VALID)) { |
3959 | /* Done this when receiving the SYN */ | |
3960 | isconnected = FALSE; | |
3961 | ||
3962 | OSDecrementAtomic(&tcp_tfo_halfcnt); | |
3963 | ||
3964 | /* Panic if something has gone terribly wrong. */ | |
3965 | VERIFY(tcp_tfo_halfcnt >= 0); | |
3966 | ||
3967 | tp->t_tfo_flags &= ~TFO_F_COOKIE_VALID; | |
3968 | } | |
3969 | ||
3970 | /* | |
3971 | * In case there is data in the send-queue (e.g., TFO is being | |
3972 | * used, or connectx+data has been done), then if we would | |
3973 | * "FALLTHROUGH", we would handle this ACK as if data has been | |
3974 | * acknowledged. But, we have to prevent this. And this | |
3975 | * can be prevented by increasing snd_una by 1, so that the | |
3976 | * SYN is not considered as data (snd_una++ is actually also | |
3977 | * done in SYN_SENT-state as part of the regular TCP stack). | |
3978 | * | |
3979 | * In case there is data on this ack as well, the data will be | |
3980 | * handled by the label "dodata" right after step6. | |
3981 | */ | |
3982 | if (so->so_snd.sb_cc) { | |
3983 | tp->snd_una++; /* SYN is acked */ | |
3984 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) | |
3985 | tp->snd_nxt = tp->snd_una; | |
3986 | ||
3987 | /* | |
3988 | * No duplicate-ACK handling is needed. So, we | |
3989 | * directly advance to processing the ACK (aka, | |
3990 | * updating the RTT estimation,...) | |
3991 | * | |
3992 | * But, we first need to handle eventual SACKs, | |
3993 | * because TFO will start sending data with the | |
3994 | * SYN/ACK, so it might be that the client | |
3995 | * includes a SACK with its ACK. | |
3996 | */ | |
3997 | if (SACK_ENABLED(tp) && | |
3998 | (to.to_nsacks > 0 || | |
3999 | !TAILQ_EMPTY(&tp->snd_holes))) | |
4000 | tcp_sack_doack(tp, &to, th, | |
4001 | &sack_bytes_acked); | |
4002 | ||
4003 | goto process_ACK; | |
4004 | } | |
4005 | ||
4006 | /* FALLTHROUGH */ | |
4a3eedf9 | 4007 | |
1c79356b A |
4008 | /* |
4009 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range | |
4010 | * ACKs. If the ack is in the range | |
4011 | * tp->snd_una < th->th_ack <= tp->snd_max | |
4012 | * then advance tp->snd_una to th->th_ack and drop | |
4013 | * data from the retransmission queue. If this ACK reflects | |
4014 | * more up to date window information we update our window information. | |
4015 | */ | |
4016 | case TCPS_ESTABLISHED: | |
4017 | case TCPS_FIN_WAIT_1: | |
4018 | case TCPS_FIN_WAIT_2: | |
4019 | case TCPS_CLOSE_WAIT: | |
4020 | case TCPS_CLOSING: | |
4021 | case TCPS_LAST_ACK: | |
4022 | case TCPS_TIME_WAIT: | |
8ad349bb A |
4023 | if (SEQ_GT(th->th_ack, tp->snd_max)) { |
4024 | tcpstat.tcps_rcvacktoomuch++; | |
4025 | goto dropafterack; | |
4026 | } | |
3e170ce0 A |
4027 | if (SACK_ENABLED(tp) && to.to_nsacks > 0) { |
4028 | recvd_dsack = tcp_sack_process_dsack(tp, &to, th); | |
4029 | /* | |
4030 | * If DSACK is received and this packet has no | |
4031 | * other SACK information, it can be dropped. | |
4032 | * We do not want to treat it as a duplicate ack. | |
4033 | */ | |
4034 | if (recvd_dsack && | |
4035 | SEQ_LEQ(th->th_ack, tp->snd_una) && | |
4036 | to.to_nsacks == 0) { | |
4037 | tcp_bad_rexmt_check(tp, th, &to); | |
4038 | goto drop; | |
4039 | } | |
4040 | } | |
4041 | ||
39236c6e | 4042 | if (SACK_ENABLED(tp) && |
8ad349bb | 4043 | (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes))) |
fe8ab488 A |
4044 | tcp_sack_doack(tp, &to, th, &sack_bytes_acked); |
4045 | ||
39236c6e A |
4046 | #if MPTCP |
4047 | if ((tp->t_mpuna) && (SEQ_GEQ(th->th_ack, tp->t_mpuna))) { | |
39236c6e A |
4048 | if (tp->t_mpflags & TMPF_PREESTABLISHED) { |
4049 | /* MP TCP establishment succeeded */ | |
4050 | tp->t_mpuna = 0; | |
4051 | if (tp->t_mpflags & TMPF_JOINED_FLOW) { | |
4052 | if (tp->t_mpflags & TMPF_SENT_JOIN) { | |
4053 | tp->t_mpflags &= | |
4054 | ~TMPF_PREESTABLISHED; | |
4055 | tp->t_mpflags |= | |
4056 | TMPF_MPTCP_TRUE; | |
4057 | so->so_flags |= SOF_MPTCP_TRUE; | |
3e170ce0 A |
4058 | mptcplog((LOG_DEBUG, "MPTCP " |
4059 | "Sockets: %s \n",__func__), | |
4060 | MPTCP_SOCKET_DBG, | |
4061 | MPTCP_LOGLVL_LOG); | |
4062 | ||
39236c6e A |
4063 | tp->t_timer[TCPT_JACK_RXMT] = 0; |
4064 | tp->t_mprxtshift = 0; | |
4065 | isconnected = TRUE; | |
4066 | } else { | |
4067 | isconnected = FALSE; | |
4068 | } | |
4069 | } else { | |
4070 | isconnected = TRUE; | |
4071 | tp->t_mpflags &= ~TMPF_SENT_KEYS; | |
39236c6e A |
4072 | } |
4073 | } | |
4074 | } | |
4075 | #endif /* MPTCP */ | |
3e170ce0 A |
4076 | |
4077 | tcp_tfo_rcv_ack(tp, th); | |
4078 | ||
39236c6e A |
4079 | /* |
4080 | * If we have outstanding data (other than | |
4081 | * a window probe), this is a completely | |
39037602 A |
4082 | * duplicate ack and the ack is the biggest we've seen. |
4083 | * | |
4084 | * Need to accommodate a change in window on duplicate acks | |
4085 | * to allow operating systems that update window during | |
4086 | * recovery with SACK | |
4087 | */ | |
1c79356b | 4088 | if (SEQ_LEQ(th->th_ack, tp->snd_una)) { |
39037602 A |
4089 | if (tlen == 0 && (tiwin == tp->snd_wnd || |
4090 | (to.to_nsacks > 0 && sack_bytes_acked > 0))) { | |
8a3053a0 A |
4091 | /* |
4092 | * If both ends send FIN at the same time, | |
4093 | * then the ack will be a duplicate ack | |
4094 | * but we have to process the FIN. Check | |
4095 | * for this condition and process the FIN | |
4096 | * instead of the dupack | |
39037602 | 4097 | */ |
8a3053a0 | 4098 | if ((thflags & TH_FIN) && |
39037602 | 4099 | !TCPS_HAVERCVDFIN(tp->t_state)) |
8a3053a0 | 4100 | break; |
39236c6e A |
4101 | process_dupack: |
4102 | #if MPTCP | |
4103 | /* | |
4104 | * MPTCP options that are ignored must | |
4105 | * not be treated as duplicate ACKs. | |
4106 | */ | |
4107 | if (to.to_flags & TOF_MPTCP) { | |
4108 | goto drop; | |
4109 | } | |
fe8ab488 A |
4110 | |
4111 | if ((isconnected) && (tp->t_mpflags & TMPF_JOINED_FLOW)) { | |
3e170ce0 A |
4112 | mptcplog((LOG_DEBUG, "MPTCP " |
4113 | "Sockets: bypass ack recovery\n"), | |
39037602 | 4114 | MPTCP_SOCKET_DBG, |
3e170ce0 | 4115 | MPTCP_LOGLVL_VERBOSE); |
fe8ab488 A |
4116 | break; |
4117 | } | |
39236c6e | 4118 | #endif /* MPTCP */ |
fe8ab488 A |
4119 | /* |
4120 | * If a duplicate acknowledgement was seen | |
4121 | * after ECN, it indicates packet loss in | |
4122 | * addition to ECN. Reset INRECOVERY flag | |
4123 | * so that we can process partial acks | |
4124 | * correctly | |
4125 | */ | |
4126 | if (tp->ecn_flags & TE_INRECOVERY) | |
4127 | tp->ecn_flags &= ~TE_INRECOVERY; | |
4128 | ||
1c79356b | 4129 | tcpstat.tcps_rcvdupack++; |
39236c6e | 4130 | ++tp->t_dupacks; |
fe8ab488 A |
4131 | |
4132 | /* | |
4133 | * Check if we need to reset the limit on | |
4134 | * early retransmit | |
39236c6e | 4135 | */ |
fe8ab488 A |
4136 | if (tp->t_early_rexmt_count > 0 && |
4137 | TSTMP_GEQ(tcp_now, | |
4138 | (tp->t_early_rexmt_win + | |
4139 | TCP_EARLY_REXMT_WIN))) | |
39236c6e A |
4140 | tp->t_early_rexmt_count = 0; |
4141 | ||
39037602 | 4142 | /* |
39236c6e A |
4143 | * Is early retransmit needed? We check for |
4144 | * this when the connection is waiting for | |
fe8ab488 | 4145 | * duplicate acks to enter fast recovery. |
39236c6e | 4146 | */ |
fe8ab488 A |
4147 | if (!IN_FASTRECOVERY(tp)) |
4148 | tcp_early_rexmt_check(tp, th); | |
39236c6e | 4149 | |
1c79356b | 4150 | /* |
39037602 | 4151 | * If we've seen exactly rexmt threshold |
39236c6e | 4152 | * of duplicate acks, assume a packet |
1c79356b A |
4153 | * has been dropped and retransmit it. |
4154 | * Kludge snd_nxt & the congestion | |
4155 | * window so we send only this one | |
4156 | * packet. | |
4157 | * | |
4158 | * We know we're losing at the current | |
4159 | * window size so do congestion avoidance | |
4160 | * (set ssthresh to half the current window | |
4161 | * and pull our congestion window back to | |
4162 | * the new ssthresh). | |
4163 | * | |
4164 | * Dup acks mean that packets have left the | |
4165 | * network (they're now cached at the receiver) | |
4166 | * so bump cwnd by the amount in the receiver | |
4167 | * to keep a constant cwnd packets in the | |
4168 | * network. | |
4169 | */ | |
4170 | if (tp->t_timer[TCPT_REXMT] == 0 || | |
fe8ab488 A |
4171 | (th->th_ack != tp->snd_una |
4172 | && sack_bytes_acked == 0)) { | |
1c79356b | 4173 | tp->t_dupacks = 0; |
39236c6e A |
4174 | tp->t_rexmtthresh = tcprexmtthresh; |
4175 | } else if (tp->t_dupacks > tp->t_rexmtthresh || | |
fe8ab488 A |
4176 | IN_FASTRECOVERY(tp)) { |
4177 | ||
4178 | /* | |
4179 | * If this connection was seeing packet | |
4180 | * reordering, then recovery might be | |
4181 | * delayed to disambiguate between | |
4182 | * reordering and loss | |
4183 | */ | |
4184 | if (SACK_ENABLED(tp) && !IN_FASTRECOVERY(tp) && | |
39037602 | 4185 | (tp->t_flagsext & |
fe8ab488 A |
4186 | (TF_PKTS_REORDERED|TF_DELAY_RECOVERY)) == |
4187 | (TF_PKTS_REORDERED|TF_DELAY_RECOVERY)) { | |
4188 | /* | |
4189 | * Since the SACK information is already | |
4190 | * updated, this ACK will be dropped | |
4191 | */ | |
4192 | break; | |
4193 | } | |
4194 | ||
39037602 | 4195 | if (SACK_ENABLED(tp) |
fe8ab488 | 4196 | && IN_FASTRECOVERY(tp)) { |
8ad349bb | 4197 | int awnd; |
39037602 | 4198 | |
8ad349bb A |
4199 | /* |
4200 | * Compute the amount of data in flight first. | |
39037602 A |
4201 | * We can inject new data into the pipe iff |
4202 | * we have less than 1/2 the original window's | |
8ad349bb | 4203 | * worth of data in flight. |
9bccf70c | 4204 | */ |
8ad349bb A |
4205 | awnd = (tp->snd_nxt - tp->snd_fack) + |
4206 | tp->sackhint.sack_bytes_rexmit; | |
4207 | if (awnd < tp->snd_ssthresh) { | |
4208 | tp->snd_cwnd += tp->t_maxseg; | |
4209 | if (tp->snd_cwnd > tp->snd_ssthresh) | |
4210 | tp->snd_cwnd = tp->snd_ssthresh; | |
4211 | } | |
39037602 | 4212 | } else { |
9bccf70c | 4213 | tp->snd_cwnd += tp->t_maxseg; |
39037602 | 4214 | } |
6d2010ae | 4215 | |
39037602 A |
4216 | /* Process any window updates */ |
4217 | if (tiwin > tp->snd_wnd) | |
4218 | tcp_update_window(tp, thflags, | |
4219 | th, tiwin, tlen); | |
4220 | tcp_ccdbg_trace(tp, th, | |
4221 | TCP_CC_IN_FASTRECOVERY); | |
6d2010ae | 4222 | |
8ad349bb | 4223 | (void) tcp_output(tp); |
39037602 | 4224 | |
8ad349bb | 4225 | goto drop; |
39236c6e | 4226 | } else if (tp->t_dupacks == tp->t_rexmtthresh) { |
8ad349bb | 4227 | tcp_seq onxt = tp->snd_nxt; |
8ad349bb A |
4228 | |
4229 | /* | |
4230 | * If we're doing sack, check to | |
4231 | * see if we're already in sack | |
4232 | * recovery. If we're not doing sack, | |
4233 | * check to see if we're in newreno | |
4234 | * recovery. | |
4235 | */ | |
39236c6e | 4236 | if (SACK_ENABLED(tp)) { |
8ad349bb A |
4237 | if (IN_FASTRECOVERY(tp)) { |
4238 | tp->t_dupacks = 0; | |
4239 | break; | |
fe8ab488 A |
4240 | } else if (tp->t_flagsext & TF_DELAY_RECOVERY) { |
4241 | break; | |
8ad349bb | 4242 | } |
6d2010ae | 4243 | } else { |
8ad349bb A |
4244 | if (SEQ_LEQ(th->th_ack, |
4245 | tp->snd_recover)) { | |
4246 | tp->t_dupacks = 0; | |
4247 | break; | |
4248 | } | |
9bccf70c | 4249 | } |
3e170ce0 A |
4250 | if (tp->t_flags & TF_SENTFIN) |
4251 | tp->snd_recover = tp->snd_max - 1; | |
4252 | else | |
4253 | tp->snd_recover = tp->snd_max; | |
fe8ab488 A |
4254 | tp->t_timer[TCPT_PTO] = 0; |
4255 | tp->t_rtttime = 0; | |
4256 | ||
4257 | /* | |
4258 | * If the connection has seen pkt | |
4259 | * reordering, delay recovery until | |
4260 | * it is clear that the packet | |
4261 | * was lost. | |
4262 | */ | |
4263 | if (SACK_ENABLED(tp) && | |
4264 | (tp->t_flagsext & | |
4265 | (TF_PKTS_REORDERED|TF_DELAY_RECOVERY)) | |
4266 | == TF_PKTS_REORDERED && | |
4267 | !IN_FASTRECOVERY(tp) && | |
4268 | tp->t_reorderwin > 0 && | |
3e170ce0 A |
4269 | (tp->t_state == TCPS_ESTABLISHED || |
4270 | tp->t_state == TCPS_FIN_WAIT_1)) { | |
fe8ab488 A |
4271 | tp->t_timer[TCPT_DELAYFR] = |
4272 | OFFSET_FROM_START(tp, | |
4273 | tp->t_reorderwin); | |
4274 | tp->t_flagsext |= TF_DELAY_RECOVERY; | |
4275 | tcpstat.tcps_delay_recovery++; | |
4276 | tcp_ccdbg_trace(tp, th, | |
4277 | TCP_CC_DELAY_FASTRECOVERY); | |
4278 | break; | |
4279 | } | |
4280 | ||
3e170ce0 | 4281 | tcp_rexmt_save_state(tp); |
6d2010ae | 4282 | /* |
39037602 | 4283 | * If the current tcp cc module has |
6d2010ae A |
4284 | * defined a hook for tasks to run |
4285 | * before entering FR, call it | |
4286 | */ | |
4287 | if (CC_ALGO(tp)->pre_fr != NULL) | |
316670eb | 4288 | CC_ALGO(tp)->pre_fr(tp); |
8ad349bb | 4289 | ENTER_FASTRECOVERY(tp); |
1c79356b | 4290 | tp->t_timer[TCPT_REXMT] = 0; |
3e170ce0 | 4291 | if (TCP_ECN_ENABLED(tp)) |
316670eb | 4292 | tp->ecn_flags |= TE_SENDCWR; |
fe8ab488 | 4293 | |
39236c6e | 4294 | if (SACK_ENABLED(tp)) { |
8ad349bb | 4295 | tcpstat.tcps_sack_recovery_episode++; |
4bd07ac2 | 4296 | tp->t_sack_recovery_episode++; |
8ad349bb A |
4297 | tp->sack_newdata = tp->snd_nxt; |
4298 | tp->snd_cwnd = tp->t_maxseg; | |
3e170ce0 A |
4299 | tp->t_flagsext &= |
4300 | ~TF_CWND_NONVALIDATED; | |
39037602 A |
4301 | |
4302 | /* Process any window updates */ | |
4303 | if (tiwin > tp->snd_wnd) | |
4304 | tcp_update_window( | |
4305 | tp, thflags, | |
4306 | th, tiwin, tlen); | |
4307 | ||
fe8ab488 A |
4308 | tcp_ccdbg_trace(tp, th, |
4309 | TCP_CC_ENTER_FASTRECOVERY); | |
8ad349bb A |
4310 | (void) tcp_output(tp); |
4311 | goto drop; | |
4312 | } | |
1c79356b A |
4313 | tp->snd_nxt = th->th_ack; |
4314 | tp->snd_cwnd = tp->t_maxseg; | |
39037602 A |
4315 | |
4316 | /* Process any window updates */ | |
4317 | if (tiwin > tp->snd_wnd) | |
4318 | tcp_update_window(tp, | |
4319 | thflags, | |
4320 | th, tiwin, tlen); | |
4321 | ||
1c79356b | 4322 | (void) tcp_output(tp); |
3e170ce0 A |
4323 | if (tp->t_flagsext & TF_CWND_NONVALIDATED) { |
4324 | tcp_cc_adjust_nonvalidated_cwnd(tp); | |
4325 | } else { | |
4326 | tp->snd_cwnd = tp->snd_ssthresh + | |
4327 | tp->t_maxseg * tp->t_dupacks; | |
4328 | } | |
1c79356b A |
4329 | if (SEQ_GT(onxt, tp->snd_nxt)) |
4330 | tp->snd_nxt = onxt; | |
39037602 | 4331 | |
fe8ab488 A |
4332 | tcp_ccdbg_trace(tp, th, |
4333 | TCP_CC_ENTER_FASTRECOVERY); | |
1c79356b | 4334 | goto drop; |
39037602 | 4335 | } else if (limited_txmt && |
39236c6e | 4336 | ALLOW_LIMITED_TRANSMIT(tp) && |
39037602 | 4337 | (!(SACK_ENABLED(tp)) || sack_bytes_acked > 0) && |
39236c6e A |
4338 | (so->so_snd.sb_cc - (tp->snd_max - tp->snd_una)) > 0) { |
4339 | u_int32_t incr = (tp->t_maxseg * tp->t_dupacks); | |
4340 | ||
4341 | /* Use Limited Transmit algorithm on the first two | |
4342 | * duplicate acks when there is new data to transmit | |
4343 | */ | |
4344 | tp->snd_cwnd += incr; | |
4345 | tcpstat.tcps_limited_txt++; | |
4346 | (void) tcp_output(tp); | |
4347 | ||
fe8ab488 | 4348 | tcp_ccdbg_trace(tp, th, TCP_CC_LIMITED_TRANSMIT); |
39037602 | 4349 | |
39236c6e A |
4350 | /* Reset snd_cwnd back to normal */ |
4351 | tp->snd_cwnd -= incr; | |
1c79356b | 4352 | } |
39236c6e | 4353 | } |
1c79356b A |
4354 | break; |
4355 | } | |
b0d623f7 A |
4356 | /* |
4357 | * If the congestion window was inflated to account | |
4358 | * for the other side's cached packets, retract it. | |
4359 | */ | |
6d2010ae A |
4360 | if (IN_FASTRECOVERY(tp)) { |
4361 | if (SEQ_LT(th->th_ack, tp->snd_recover)) { | |
fe8ab488 | 4362 | /* |
39037602 | 4363 | * If we received an ECE and entered |
fe8ab488 A |
4364 | * recovery, the subsequent ACKs should |
4365 | * not be treated as partial acks. | |
4366 | */ | |
4367 | if (tp->ecn_flags & TE_INRECOVERY) | |
4368 | goto process_ACK; | |
4369 | ||
39236c6e | 4370 | if (SACK_ENABLED(tp)) |
6d2010ae A |
4371 | tcp_sack_partialack(tp, th); |
4372 | else | |
39037602 | 4373 | tcp_newreno_partial_ack(tp, th); |
fe8ab488 | 4374 | tcp_ccdbg_trace(tp, th, TCP_CC_PARTIAL_ACK); |
6d2010ae A |
4375 | } else { |
4376 | EXIT_FASTRECOVERY(tp); | |
4377 | if (CC_ALGO(tp)->post_fr != NULL) | |
4378 | CC_ALGO(tp)->post_fr(tp, th); | |
3e170ce0 A |
4379 | tp->t_pipeack = 0; |
4380 | tcp_clear_pipeack_state(tp); | |
fe8ab488 A |
4381 | tcp_ccdbg_trace(tp, th, |
4382 | TCP_CC_EXIT_FASTRECOVERY); | |
4383 | } | |
39037602 | 4384 | } else if ((tp->t_flagsext & |
fe8ab488 A |
4385 | (TF_PKTS_REORDERED|TF_DELAY_RECOVERY)) |
4386 | == (TF_PKTS_REORDERED|TF_DELAY_RECOVERY)) { | |
4387 | /* | |
4388 | * If the ack acknowledges upto snd_recover or if | |
4389 | * it acknowledges all the snd holes, exit | |
4390 | * recovery and cancel the timer. Otherwise, | |
4391 | * this is a partial ack. Wait for recovery timer | |
4392 | * to enter recovery. The snd_holes have already | |
4393 | * been updated. | |
4394 | */ | |
4395 | if (SEQ_GEQ(th->th_ack, tp->snd_recover) || | |
4396 | TAILQ_EMPTY(&tp->snd_holes)) { | |
4397 | tp->t_timer[TCPT_DELAYFR] = 0; | |
4398 | tp->t_flagsext &= ~TF_DELAY_RECOVERY; | |
4399 | EXIT_FASTRECOVERY(tp); | |
4400 | tcp_ccdbg_trace(tp, th, | |
4401 | TCP_CC_EXIT_FASTRECOVERY); | |
6d2010ae A |
4402 | } |
4403 | } else { | |
593a1d5f | 4404 | /* |
fe8ab488 A |
4405 | * We were not in fast recovery. Reset the |
4406 | * duplicate ack counter. | |
593a1d5f A |
4407 | */ |
4408 | tp->t_dupacks = 0; | |
39236c6e | 4409 | tp->t_rexmtthresh = tcprexmtthresh; |
593a1d5f | 4410 | } |
593a1d5f A |
4411 | |
4412 | ||
1c79356b | 4413 | /* |
8ad349bb | 4414 | * If we reach this point, ACK is not a duplicate, |
1c79356b A |
4415 | * i.e., it ACKs something we sent. |
4416 | */ | |
4417 | if (tp->t_flags & TF_NEEDSYN) { | |
4418 | /* | |
4419 | * T/TCP: Connection was half-synchronized, and our | |
4420 | * SYN has been ACK'd (so connection is now fully | |
4421 | * synchronized). Go to non-starred state, | |
4422 | * increment snd_una for ACK of SYN, and check if | |
4423 | * we can do window scaling. | |
4424 | */ | |
4425 | tp->t_flags &= ~TF_NEEDSYN; | |
4426 | tp->snd_una++; | |
4427 | /* Do window scaling? */ | |
3e170ce0 | 4428 | if (TCP_WINDOW_SCALE_ENABLED(tp)) { |
1c79356b A |
4429 | tp->snd_scale = tp->requested_s_scale; |
4430 | tp->rcv_scale = tp->request_r_scale; | |
4431 | } | |
4432 | } | |
4433 | ||
4434 | process_ACK: | |
3e170ce0 | 4435 | VERIFY(SEQ_GEQ(th->th_ack, tp->snd_una)); |
39236c6e | 4436 | acked = BYTES_ACKED(th, tp); |
1c79356b A |
4437 | tcpstat.tcps_rcvackpack++; |
4438 | tcpstat.tcps_rcvackbyte += acked; | |
4439 | ||
9bccf70c | 4440 | /* |
39236c6e A |
4441 | * If the last packet was a retransmit, make sure |
4442 | * it was not spurious. | |
4443 | * | |
fe8ab488 A |
4444 | * This will also take care of congestion window |
4445 | * adjustment if a last packet was recovered due to a | |
4446 | * tail loss probe. | |
9bccf70c | 4447 | */ |
fe8ab488 | 4448 | tcp_bad_rexmt_check(tp, th, &to); |
9bccf70c | 4449 | |
39236c6e A |
4450 | /* Recalculate the RTT */ |
4451 | tcp_compute_rtt(tp, &to, th); | |
1c79356b A |
4452 | |
4453 | /* | |
4454 | * If all outstanding data is acked, stop retransmit | |
4455 | * timer and remember to restart (more output or persist). | |
4456 | * If there is more data to be acked, restart retransmit | |
4457 | * timer, using current (possibly backed-off) value. | |
4458 | */ | |
39037602 A |
4459 | TCP_RESET_REXMT_STATE(tp); |
4460 | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), | |
4461 | tp->t_rttmin, TCPTV_REXMTMAX, | |
4462 | TCP_ADD_REXMTSLOP(tp)); | |
1c79356b A |
4463 | if (th->th_ack == tp->snd_max) { |
4464 | tp->t_timer[TCPT_REXMT] = 0; | |
fe8ab488 | 4465 | tp->t_timer[TCPT_PTO] = 0; |
1c79356b A |
4466 | needoutput = 1; |
4467 | } else if (tp->t_timer[TCPT_PERSIST] == 0) | |
fe8ab488 A |
4468 | tp->t_timer[TCPT_REXMT] = OFFSET_FROM_START(tp, |
4469 | tp->t_rxtcur); | |
1c79356b A |
4470 | |
4471 | /* | |
fe8ab488 A |
4472 | * If no data (only SYN) was ACK'd, skip rest of ACK |
4473 | * processing. | |
1c79356b A |
4474 | */ |
4475 | if (acked == 0) | |
4476 | goto step6; | |
4477 | ||
3e170ce0 A |
4478 | /* |
4479 | * When outgoing data has been acked (except the SYN+data), we | |
4480 | * mark this connection as "sending good" for TFO. | |
4481 | */ | |
4482 | if ((tp->t_tfo_stats & TFO_S_SYN_DATA_SENT) && | |
4483 | !(tp->t_tfo_flags & TFO_F_NO_SNDPROBING) && | |
4484 | !(th->th_flags & TH_SYN)) | |
4485 | tcp_heuristic_tfo_snd_good(tp); | |
fe8ab488 | 4486 | |
3e170ce0 A |
4487 | /* |
4488 | * If TH_ECE is received, make sure that ECN is enabled | |
4489 | * on that connection and we have sent ECT on data packets. | |
4490 | */ | |
4491 | if ((thflags & TH_ECE) != 0 && TCP_ECN_ENABLED(tp) && | |
4492 | (tp->ecn_flags & TE_SENDIPECT)) { | |
2d21ac55 | 4493 | /* |
fe8ab488 A |
4494 | * Reduce the congestion window if we haven't |
4495 | * done so. | |
2d21ac55 | 4496 | */ |
fe8ab488 | 4497 | if (!IN_FASTRECOVERY(tp)) { |
316670eb | 4498 | tcp_reduce_congestion_window(tp); |
fe8ab488 | 4499 | tp->ecn_flags |= (TE_INRECOVERY|TE_SENDCWR); |
3e170ce0 A |
4500 | /* |
4501 | * Also note that the connection received | |
4502 | * ECE atleast once | |
4503 | */ | |
4504 | tp->ecn_flags |= TE_RECV_ECN_ECE; | |
4bd07ac2 | 4505 | INP_INC_IFNET_STAT(inp, ecn_recv_ece); |
3e170ce0 | 4506 | tcpstat.tcps_ecn_recv_ece++; |
fe8ab488 | 4507 | tcp_ccdbg_trace(tp, th, TCP_CC_ECN_RCVD); |
2d21ac55 | 4508 | } |
6d2010ae | 4509 | } |
b0d623f7 | 4510 | |
6d2010ae A |
4511 | /* |
4512 | * When new data is acked, open the congestion window. | |
4513 | * The specifics of how this is achieved are up to the | |
4514 | * congestion control algorithm in use for this connection. | |
4515 | * | |
4516 | * The calculations in this function assume that snd_una is | |
4517 | * not updated yet. | |
4518 | */ | |
4519 | if (!IN_FASTRECOVERY(tp)) { | |
4520 | if (CC_ALGO(tp)->ack_rcvd != NULL) | |
4521 | CC_ALGO(tp)->ack_rcvd(tp, th); | |
fe8ab488 | 4522 | tcp_ccdbg_trace(tp, th, TCP_CC_ACK_RCVD); |
1c79356b A |
4523 | } |
4524 | if (acked > so->so_snd.sb_cc) { | |
4525 | tp->snd_wnd -= so->so_snd.sb_cc; | |
4526 | sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); | |
39236c6e | 4527 | if (so->so_flags & SOF_ENABLE_MSGS) { |
39037602 | 4528 | so->so_msg_state->msg_serial_bytes -= |
39236c6e A |
4529 | (int)so->so_snd.sb_cc; |
4530 | } | |
1c79356b A |
4531 | ourfinisacked = 1; |
4532 | } else { | |
4533 | sbdrop(&so->so_snd, acked); | |
39236c6e | 4534 | if (so->so_flags & SOF_ENABLE_MSGS) { |
39037602 | 4535 | so->so_msg_state->msg_serial_bytes -= |
39236c6e A |
4536 | acked; |
4537 | } | |
316670eb | 4538 | tcp_sbsnd_trim(&so->so_snd); |
1c79356b A |
4539 | tp->snd_wnd -= acked; |
4540 | ourfinisacked = 0; | |
4541 | } | |
91447636 | 4542 | /* detect una wraparound */ |
6d2010ae | 4543 | if ( !IN_FASTRECOVERY(tp) && |
8ad349bb A |
4544 | SEQ_GT(tp->snd_una, tp->snd_recover) && |
4545 | SEQ_LEQ(th->th_ack, tp->snd_recover)) | |
4546 | tp->snd_recover = th->th_ack - 1; | |
6d2010ae A |
4547 | |
4548 | if (IN_FASTRECOVERY(tp) && | |
8ad349bb A |
4549 | SEQ_GEQ(th->th_ack, tp->snd_recover)) |
4550 | EXIT_FASTRECOVERY(tp); | |
6d2010ae | 4551 | |
1c79356b | 4552 | tp->snd_una = th->th_ack; |
39037602 | 4553 | |
39236c6e | 4554 | if (SACK_ENABLED(tp)) { |
8ad349bb A |
4555 | if (SEQ_GT(tp->snd_una, tp->snd_recover)) |
4556 | tp->snd_recover = tp->snd_una; | |
4557 | } | |
1c79356b A |
4558 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
4559 | tp->snd_nxt = tp->snd_una; | |
3e170ce0 A |
4560 | if (!SLIST_EMPTY(&tp->t_rxt_segments) && |
4561 | !TCP_DSACK_SEQ_IN_WINDOW(tp, tp->t_dsack_lastuna, | |
4562 | tp->snd_una)) | |
4563 | tcp_rxtseg_clean(tp); | |
316670eb A |
4564 | if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 && |
4565 | tp->t_bwmeas != NULL) | |
4566 | tcp_bwmeas_check(tp); | |
4567 | ||
4a3eedf9 | 4568 | /* |
39037602 A |
4569 | * sowwakeup must happen after snd_una, et al. are |
4570 | * updated so that the sequence numbers are in sync with | |
4571 | * so_snd | |
4a3eedf9 A |
4572 | */ |
4573 | sowwakeup(so); | |
1c79356b | 4574 | |
39037602 A |
4575 | if (!SLIST_EMPTY(&tp->t_notify_ack)) |
4576 | tcp_notify_acknowledgement(tp, so); | |
4577 | ||
1c79356b A |
4578 | switch (tp->t_state) { |
4579 | ||
4580 | /* | |
4581 | * In FIN_WAIT_1 STATE in addition to the processing | |
4582 | * for the ESTABLISHED state if our FIN is now acknowledged | |
4583 | * then enter FIN_WAIT_2. | |
4584 | */ | |
4585 | case TCPS_FIN_WAIT_1: | |
4586 | if (ourfinisacked) { | |
4587 | /* | |
4588 | * If we can't receive any more | |
4589 | * data, then closing user can proceed. | |
39236c6e | 4590 | * Starting the TCPT_2MSL timer is contrary to the |
1c79356b A |
4591 | * specification, but if we don't get a FIN |
4592 | * we'll hang forever. | |
4593 | */ | |
4594 | if (so->so_state & SS_CANTRCVMORE) { | |
39236c6e A |
4595 | tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp, |
4596 | TCP_CONN_MAXIDLE(tp)); | |
6d2010ae A |
4597 | isconnected = FALSE; |
4598 | isdisconnected = TRUE; | |
1c79356b | 4599 | } |
39037602 | 4600 | DTRACE_TCP4(state__change, void, NULL, |
39236c6e | 4601 | struct inpcb *, inp, |
39037602 | 4602 | struct tcpcb *, tp, |
39236c6e | 4603 | int32_t, TCPS_FIN_WAIT_2); |
1c79356b | 4604 | tp->t_state = TCPS_FIN_WAIT_2; |
39037602 A |
4605 | /* fall through and make sure we also recognize |
4606 | * data ACKed with the FIN | |
39236c6e | 4607 | */ |
1c79356b A |
4608 | } |
4609 | break; | |
4610 | ||
4611 | /* | |
4612 | * In CLOSING STATE in addition to the processing for | |
4613 | * the ESTABLISHED state if the ACK acknowledges our FIN | |
4614 | * then enter the TIME-WAIT state, otherwise ignore | |
4615 | * the segment. | |
4616 | */ | |
4617 | case TCPS_CLOSING: | |
4618 | if (ourfinisacked) { | |
39037602 | 4619 | DTRACE_TCP4(state__change, void, NULL, |
39236c6e | 4620 | struct inpcb *, inp, |
39037602 | 4621 | struct tcpcb *, tp, |
39236c6e | 4622 | int32_t, TCPS_TIME_WAIT); |
1c79356b A |
4623 | tp->t_state = TCPS_TIME_WAIT; |
4624 | tcp_canceltimers(tp); | |
fe8ab488 A |
4625 | if (tp->t_flagsext & TF_NOTIMEWAIT) { |
4626 | tp->t_flags |= TF_CLOSING; | |
4627 | } else { | |
4628 | add_to_time_wait(tp, 2 * tcp_msl); | |
4629 | } | |
6d2010ae A |
4630 | isconnected = FALSE; |
4631 | isdisconnected = TRUE; | |
1c79356b A |
4632 | } |
4633 | break; | |
4634 | ||
4635 | /* | |
4636 | * In LAST_ACK, we may still be waiting for data to drain | |
4637 | * and/or to be acked, as well as for the ack of our FIN. | |
4638 | * If our FIN is now acknowledged, delete the TCB, | |
4639 | * enter the closed state and return. | |
4640 | */ | |
4641 | case TCPS_LAST_ACK: | |
4642 | if (ourfinisacked) { | |
4643 | tp = tcp_close(tp); | |
4644 | goto drop; | |
4645 | } | |
4646 | break; | |
4647 | ||
4648 | /* | |
4649 | * In TIME_WAIT state the only thing that should arrive | |
4650 | * is a retransmission of the remote FIN. Acknowledge | |
4651 | * it and restart the finack timer. | |
4652 | */ | |
4653 | case TCPS_TIME_WAIT: | |
6d2010ae | 4654 | add_to_time_wait(tp, 2 * tcp_msl); |
1c79356b A |
4655 | goto dropafterack; |
4656 | } | |
39236c6e A |
4657 | |
4658 | /* | |
39037602 | 4659 | * If there is a SACK option on the ACK and we |
39236c6e A |
4660 | * haven't seen any duplicate acks before, count |
4661 | * it as a duplicate ack even if the cumulative | |
4662 | * ack is advanced. If the receiver delayed an | |
4663 | * ack and detected loss afterwards, then the ack | |
39037602 | 4664 | * will advance cumulative ack and will also have |
39236c6e A |
4665 | * a SACK option. So counting it as one duplicate |
4666 | * ack is ok. | |
39037602 | 4667 | */ |
39236c6e | 4668 | if (sack_ackadv == 1 && |
39037602 A |
4669 | tp->t_state == TCPS_ESTABLISHED && |
4670 | SACK_ENABLED(tp) && sack_bytes_acked > 0 && | |
fe8ab488 A |
4671 | to.to_nsacks > 0 && tp->t_dupacks == 0 && |
4672 | SEQ_LEQ(th->th_ack, tp->snd_una) && tlen == 0 && | |
4673 | !(tp->t_flagsext & TF_PKTS_REORDERED)) { | |
39236c6e A |
4674 | tcpstat.tcps_sack_ackadv++; |
4675 | goto process_dupack; | |
4676 | } | |
1c79356b A |
4677 | } |
4678 | ||
4679 | step6: | |
4680 | /* | |
4681 | * Update window information. | |
1c79356b | 4682 | */ |
39037602 | 4683 | if (tcp_update_window(tp, thflags, th, tiwin, tlen)) |
1c79356b | 4684 | needoutput = 1; |
1c79356b A |
4685 | |
4686 | /* | |
4687 | * Process segments with URG. | |
4688 | */ | |
4689 | if ((thflags & TH_URG) && th->th_urp && | |
4690 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
4691 | /* | |
4692 | * This is a kludge, but if we receive and accept | |
4693 | * random urgent pointers, we'll crash in | |
4694 | * soreceive. It's hard to imagine someone | |
4695 | * actually wanting to send this much urgent data. | |
4696 | */ | |
4697 | if (th->th_urp + so->so_rcv.sb_cc > sb_max) { | |
4698 | th->th_urp = 0; /* XXX */ | |
4699 | thflags &= ~TH_URG; /* XXX */ | |
4700 | goto dodata; /* XXX */ | |
4701 | } | |
4702 | /* | |
4703 | * If this segment advances the known urgent pointer, | |
4704 | * then mark the data stream. This should not happen | |
4705 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since | |
4706 | * a FIN has been received from the remote side. | |
4707 | * In these states we ignore the URG. | |
4708 | * | |
4709 | * According to RFC961 (Assigned Protocols), | |
4710 | * the urgent pointer points to the last octet | |
4711 | * of urgent data. We continue, however, | |
4712 | * to consider it to indicate the first octet | |
4713 | * of data past the urgent section as the original | |
4714 | * spec states (in one of two places). | |
4715 | */ | |
4716 | if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { | |
4717 | tp->rcv_up = th->th_seq + th->th_urp; | |
4718 | so->so_oobmark = so->so_rcv.sb_cc + | |
4719 | (tp->rcv_up - tp->rcv_nxt) - 1; | |
4720 | if (so->so_oobmark == 0) { | |
4721 | so->so_state |= SS_RCVATMARK; | |
4722 | postevent(so, 0, EV_OOB); | |
4723 | } | |
4724 | sohasoutofband(so); | |
4725 | tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); | |
4726 | } | |
4727 | /* | |
4728 | * Remove out of band data so doesn't get presented to user. | |
4729 | * This can happen independent of advancing the URG pointer, | |
4730 | * but if two URG's are pending at once, some out-of-band | |
4731 | * data may creep in... ick. | |
4732 | */ | |
b0d623f7 | 4733 | if (th->th_urp <= (u_int32_t)tlen |
1c79356b A |
4734 | #if SO_OOBINLINE |
4735 | && (so->so_options & SO_OOBINLINE) == 0 | |
4736 | #endif | |
4737 | ) | |
9bccf70c A |
4738 | tcp_pulloutofband(so, th, m, |
4739 | drop_hdrlen); /* hdr drop is delayed */ | |
6d2010ae | 4740 | } else { |
1c79356b A |
4741 | /* |
4742 | * If no out of band data is expected, | |
4743 | * pull receive urgent pointer along | |
4744 | * with the receive window. | |
4745 | */ | |
4746 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) | |
4747 | tp->rcv_up = tp->rcv_nxt; | |
6d2010ae A |
4748 | } |
4749 | dodata: | |
1c79356b | 4750 | |
6d2010ae A |
4751 | /* Set socket's connect or disconnect state correcly before doing data. |
4752 | * The following might unlock the socket if there is an upcall or a socket | |
4753 | * filter. | |
4754 | */ | |
4755 | if (isconnected) { | |
4756 | soisconnected(so); | |
4757 | } else if (isdisconnected) { | |
4758 | soisdisconnected(so); | |
4759 | } | |
4760 | ||
39037602 | 4761 | /* Let's check the state of pcb just to make sure that it did not get closed |
6d2010ae A |
4762 | * when we unlocked above |
4763 | */ | |
4764 | if (inp->inp_state == INPCB_STATE_DEAD) { | |
4765 | /* Just drop the packet that we are processing and return */ | |
4766 | goto drop; | |
4767 | } | |
39037602 | 4768 | |
1c79356b A |
4769 | /* |
4770 | * Process the segment text, merging it into the TCP sequencing queue, | |
4771 | * and arranging for acknowledgment of receipt if necessary. | |
4772 | * This process logically involves adjusting tp->rcv_wnd as data | |
4773 | * is presented to the user (this happens in tcp_usrreq.c, | |
4774 | * case PRU_RCVD). If a FIN has already been received on this | |
4775 | * connection then we just ignore the text. | |
3e170ce0 A |
4776 | * |
4777 | * If we are in SYN-received state and got a valid TFO cookie, we want | |
4778 | * to process the data. | |
1c79356b | 4779 | */ |
c910b4d9 | 4780 | if ((tlen || (thflags & TH_FIN)) && |
3e170ce0 A |
4781 | TCPS_HAVERCVDFIN(tp->t_state) == 0 && |
4782 | (TCPS_HAVEESTABLISHED(tp->t_state) || | |
4783 | (tp->t_state == TCPS_SYN_RECEIVED && | |
4784 | (tp->t_tfo_flags & TFO_F_COOKIE_VALID)))) { | |
8ad349bb A |
4785 | tcp_seq save_start = th->th_seq; |
4786 | tcp_seq save_end = th->th_seq + tlen; | |
9bccf70c A |
4787 | m_adj(m, drop_hdrlen); /* delayed header drop */ |
4788 | /* | |
8ad349bb A |
4789 | * Insert segment which includes th into TCP reassembly queue |
4790 | * with control block tp. Set thflags to whether reassembly now | |
4791 | * includes a segment with FIN. This handles the common case | |
4792 | * inline (segment is the next to be received on an established | |
4793 | * connection, and the queue is empty), avoiding linkage into | |
4794 | * and removal from the queue and repetition of various | |
4795 | * conversions. | |
4796 | * Set DELACK for segments received in order, but ack | |
4797 | * immediately when segments are out of order (so | |
4798 | * fast retransmit can work). | |
9bccf70c | 4799 | */ |
3e170ce0 | 4800 | if (th->th_seq == tp->rcv_nxt && LIST_EMPTY(&tp->t_segq)) { |
316670eb | 4801 | TCP_INC_VAR(tp->t_unacksegs, nlropkts); |
39236c6e | 4802 | /* |
39037602 A |
4803 | * Calculate the RTT on the receiver only if the |
4804 | * connection is in streaming mode and the last | |
39236c6e A |
4805 | * packet was not an end-of-write |
4806 | */ | |
39037602 | 4807 | if (tp->t_flags & TF_STREAMING_ON) |
39236c6e | 4808 | tcp_compute_rtt(tp, &to, th); |
39037602 A |
4809 | |
4810 | if (DELAY_ACK(tp, th) && | |
316670eb | 4811 | ((tp->t_flags & TF_ACKNOW) == 0) ) { |
6d2010ae A |
4812 | if ((tp->t_flags & TF_DELACK) == 0) { |
4813 | tp->t_flags |= TF_DELACK; | |
39037602 | 4814 | tp->t_timer[TCPT_DELACK] = |
39236c6e | 4815 | OFFSET_FROM_START(tp, tcp_delack); |
6d2010ae | 4816 | } |
39037602 | 4817 | } |
91447636 | 4818 | else { |
9bccf70c | 4819 | tp->t_flags |= TF_ACKNOW; |
91447636 | 4820 | } |
9bccf70c A |
4821 | tp->rcv_nxt += tlen; |
4822 | thflags = th->th_flags & TH_FIN; | |
316670eb | 4823 | TCP_INC_VAR(tcpstat.tcps_rcvpack, nlropkts); |
9bccf70c | 4824 | tcpstat.tcps_rcvbyte += tlen; |
6d2010ae | 4825 | if (nstat_collect) { |
39236c6e | 4826 | if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_PKT) { |
39037602 | 4827 | INP_ADD_STAT(inp, cell, wifi, wired, |
fe8ab488 | 4828 | rxpackets, m->m_pkthdr.lro_npkts); |
39236c6e | 4829 | } else { |
fe8ab488 A |
4830 | INP_ADD_STAT(inp, cell, wifi, wired, |
4831 | rxpackets, 1); | |
316670eb | 4832 | } |
fe8ab488 A |
4833 | INP_ADD_STAT(inp, cell, wifi, wired, |
4834 | rxbytes, tlen); | |
6d2010ae | 4835 | } |
39037602 A |
4836 | tcp_sbrcv_grow(tp, &so->so_rcv, &to, tlen, |
4837 | TCP_AUTORCVBUF_MAX(ifp)); | |
6d2010ae | 4838 | so_recv_data_stat(so, m, drop_hdrlen); |
39037602 | 4839 | |
39236c6e A |
4840 | if (sbappendstream_rcvdemux(so, m, |
4841 | th->th_seq - (tp->irs + 1), 0)) { | |
91447636 | 4842 | sorwakeup(so); |
39236c6e | 4843 | } |
9bccf70c | 4844 | } else { |
39236c6e | 4845 | thflags = tcp_reass(tp, th, &tlen, m, ifp); |
9bccf70c A |
4846 | tp->t_flags |= TF_ACKNOW; |
4847 | } | |
1c79356b | 4848 | |
39037602 A |
4849 | if ((tlen > 0 || (th->th_flags & TH_FIN)) && SACK_ENABLED(tp)) { |
4850 | if (th->th_flags & TH_FIN) | |
4851 | save_end++; | |
8ad349bb | 4852 | tcp_update_sack_list(tp, save_start, save_end); |
39037602 | 4853 | } |
8ad349bb | 4854 | |
39236c6e A |
4855 | tcp_adaptive_rwtimo_check(tp, tlen); |
4856 | ||
3e170ce0 A |
4857 | if (tlen > 0) |
4858 | tcp_tfo_rcv_data(tp); | |
4859 | ||
39037602 | 4860 | if (tp->t_flags & TF_DELACK) |
1c79356b | 4861 | { |
9bccf70c A |
4862 | #if INET6 |
4863 | if (isipv6) { | |
4864 | KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport), | |
4865 | (((ip6->ip6_src.s6_addr16[0]) << 16) | (ip6->ip6_dst.s6_addr16[0])), | |
39037602 | 4866 | th->th_seq, th->th_ack, th->th_win); |
9bccf70c A |
4867 | } |
4868 | else | |
4869 | #endif | |
4870 | { | |
4871 | KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport), | |
4872 | (((ip->ip_src.s_addr & 0xffff) << 16) | (ip->ip_dst.s_addr & 0xffff)), | |
39037602 | 4873 | th->th_seq, th->th_ack, th->th_win); |
9bccf70c | 4874 | } |
39037602 | 4875 | |
1c79356b | 4876 | } |
1c79356b A |
4877 | } else { |
4878 | m_freem(m); | |
4879 | thflags &= ~TH_FIN; | |
4880 | } | |
4881 | ||
4882 | /* | |
4883 | * If FIN is received ACK the FIN and let the user know | |
4884 | * that the connection is closing. | |
4885 | */ | |
4886 | if (thflags & TH_FIN) { | |
4887 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
4888 | socantrcvmore(so); | |
4889 | postevent(so, 0, EV_FIN); | |
4890 | /* | |
8ad349bb A |
4891 | * If connection is half-synchronized |
4892 | * (ie NEEDSYN flag on) then delay ACK, | |
4893 | * so it may be piggybacked when SYN is sent. | |
4894 | * Otherwise, since we received a FIN then no | |
4895 | * more input can be expected, send ACK now. | |
1c79356b | 4896 | */ |
316670eb | 4897 | TCP_INC_VAR(tp->t_unacksegs, nlropkts); |
6d2010ae A |
4898 | if (DELAY_ACK(tp, th) && (tp->t_flags & TF_NEEDSYN)) { |
4899 | if ((tp->t_flags & TF_DELACK) == 0) { | |
4900 | tp->t_flags |= TF_DELACK; | |
4901 | tp->t_timer[TCPT_DELACK] = OFFSET_FROM_START(tp, tcp_delack); | |
4902 | } | |
4bd07ac2 | 4903 | } else { |
1c79356b | 4904 | tp->t_flags |= TF_ACKNOW; |
91447636 | 4905 | } |
1c79356b A |
4906 | tp->rcv_nxt++; |
4907 | } | |
4908 | switch (tp->t_state) { | |
4909 | ||
4910 | /* | |
4911 | * In SYN_RECEIVED and ESTABLISHED STATES | |
4912 | * enter the CLOSE_WAIT state. | |
4913 | */ | |
4914 | case TCPS_SYN_RECEIVED: | |
6d2010ae | 4915 | tp->t_starttime = tcp_now; |
1c79356b | 4916 | case TCPS_ESTABLISHED: |
6d2010ae A |
4917 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
4918 | struct tcpcb *, tp, int32_t, TCPS_CLOSE_WAIT); | |
1c79356b A |
4919 | tp->t_state = TCPS_CLOSE_WAIT; |
4920 | break; | |
4921 | ||
4922 | /* | |
4923 | * If still in FIN_WAIT_1 STATE FIN has not been acked so | |
4924 | * enter the CLOSING state. | |
4925 | */ | |
4926 | case TCPS_FIN_WAIT_1: | |
6d2010ae A |
4927 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
4928 | struct tcpcb *, tp, int32_t, TCPS_CLOSING); | |
1c79356b A |
4929 | tp->t_state = TCPS_CLOSING; |
4930 | break; | |
4931 | ||
4932 | /* | |
4933 | * In FIN_WAIT_2 state enter the TIME_WAIT state, | |
4934 | * starting the time-wait timer, turning off the other | |
4935 | * standard timers. | |
4936 | */ | |
4937 | case TCPS_FIN_WAIT_2: | |
39037602 | 4938 | DTRACE_TCP4(state__change, void, NULL, |
39236c6e | 4939 | struct inpcb *, inp, |
39037602 | 4940 | struct tcpcb *, tp, |
39236c6e | 4941 | int32_t, TCPS_TIME_WAIT); |
1c79356b A |
4942 | tp->t_state = TCPS_TIME_WAIT; |
4943 | tcp_canceltimers(tp); | |
fe8ab488 A |
4944 | tp->t_flags |= TF_ACKNOW; |
4945 | if (tp->t_flagsext & TF_NOTIMEWAIT) { | |
4946 | tp->t_flags |= TF_CLOSING; | |
4947 | } else { | |
4948 | add_to_time_wait(tp, 2 * tcp_msl); | |
1c79356b | 4949 | } |
1c79356b A |
4950 | soisdisconnected(so); |
4951 | break; | |
4952 | ||
4953 | /* | |
4954 | * In TIME_WAIT state restart the 2 MSL time_wait timer. | |
4955 | */ | |
4956 | case TCPS_TIME_WAIT: | |
6d2010ae | 4957 | add_to_time_wait(tp, 2 * tcp_msl); |
1c79356b A |
4958 | break; |
4959 | } | |
4960 | } | |
4961 | #if TCPDEBUG | |
9bccf70c A |
4962 | if (so->so_options & SO_DEBUG) |
4963 | tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen, | |
1c79356b | 4964 | &tcp_savetcp, 0); |
1c79356b A |
4965 | #endif |
4966 | ||
4967 | /* | |
4968 | * Return any desired output. | |
4969 | */ | |
2d21ac55 | 4970 | if (needoutput || (tp->t_flags & TF_ACKNOW)) { |
1c79356b | 4971 | (void) tcp_output(tp); |
2d21ac55 | 4972 | } |
6d2010ae A |
4973 | |
4974 | tcp_check_timer_state(tp); | |
4975 | ||
39037602 | 4976 | |
91447636 | 4977 | tcp_unlock(so, 1, 0); |
1c79356b A |
4978 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); |
4979 | return; | |
4980 | ||
4981 | dropafterack: | |
4982 | /* | |
4983 | * Generate an ACK dropping incoming segment if it occupies | |
4984 | * sequence space, where the ACK reflects our state. | |
4985 | * | |
4986 | * We can now skip the test for the RST flag since all | |
4987 | * paths to this code happen after packets containing | |
4988 | * RST have been dropped. | |
4989 | * | |
4990 | * In the SYN-RECEIVED state, don't send an ACK unless the | |
4991 | * segment we received passes the SYN-RECEIVED ACK test. | |
4992 | * If it fails send a RST. This breaks the loop in the | |
4993 | * "LAND" DoS attack, and also prevents an ACK storm | |
4994 | * between two listening ports that have been sent forged | |
4995 | * SYN segments, each with the source address of the other. | |
4996 | */ | |
4997 | if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && | |
4998 | (SEQ_GT(tp->snd_una, th->th_ack) || | |
9bccf70c A |
4999 | SEQ_GT(th->th_ack, tp->snd_max)) ) { |
5000 | rstreason = BANDLIM_RST_OPENPORT; | |
39236c6e | 5001 | IF_TCP_STATINC(ifp, dospacket); |
1c79356b | 5002 | goto dropwithreset; |
9bccf70c | 5003 | } |
1c79356b | 5004 | #if TCPDEBUG |
9bccf70c A |
5005 | if (so->so_options & SO_DEBUG) |
5006 | tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, | |
1c79356b | 5007 | &tcp_savetcp, 0); |
1c79356b A |
5008 | #endif |
5009 | m_freem(m); | |
5010 | tp->t_flags |= TF_ACKNOW; | |
5011 | (void) tcp_output(tp); | |
6d2010ae A |
5012 | |
5013 | /* Don't need to check timer state as we should have done it during tcp_output */ | |
91447636 | 5014 | tcp_unlock(so, 1, 0); |
1c79356b A |
5015 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); |
5016 | return; | |
91447636 A |
5017 | dropwithresetnosock: |
5018 | nosock = 1; | |
1c79356b A |
5019 | dropwithreset: |
5020 | /* | |
5021 | * Generate a RST, dropping incoming segment. | |
5022 | * Make ACK acceptable to originator of segment. | |
5023 | * Don't bother to respond if destination was broadcast/multicast. | |
5024 | */ | |
5025 | if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) | |
5026 | goto drop; | |
5027 | #if INET6 | |
5028 | if (isipv6) { | |
9bccf70c A |
5029 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || |
5030 | IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) | |
5031 | goto drop; | |
1c79356b A |
5032 | } else |
5033 | #endif /* INET6 */ | |
14353aa8 A |
5034 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || |
5035 | IN_MULTICAST(ntohl(ip->ip_src.s_addr)) || | |
5036 | ip->ip_src.s_addr == htonl(INADDR_BROADCAST) || | |
5037 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) | |
1c79356b | 5038 | goto drop; |
9bccf70c A |
5039 | /* IPv6 anycast check is done at tcp6_input() */ |
5040 | ||
39037602 | 5041 | /* |
9bccf70c A |
5042 | * Perform bandwidth limiting. |
5043 | */ | |
5044 | #if ICMP_BANDLIM | |
5045 | if (badport_bandlim(rstreason) < 0) | |
5046 | goto drop; | |
5047 | #endif | |
5048 | ||
1c79356b | 5049 | #if TCPDEBUG |
9bccf70c A |
5050 | if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) |
5051 | tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, | |
1c79356b | 5052 | &tcp_savetcp, 0); |
1c79356b | 5053 | #endif |
fe8ab488 A |
5054 | bzero(&tra, sizeof(tra)); |
5055 | tra.ifscope = ifscope; | |
5056 | tra.awdl_unrestricted = 1; | |
39037602 | 5057 | tra.intcoproc_allowed = 1; |
1c79356b | 5058 | if (thflags & TH_ACK) |
9bccf70c A |
5059 | /* mtod() below is safe as long as hdr dropping is delayed */ |
5060 | tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack, | |
fe8ab488 | 5061 | TH_RST, &tra); |
1c79356b A |
5062 | else { |
5063 | if (thflags & TH_SYN) | |
9bccf70c A |
5064 | tlen++; |
5065 | /* mtod() below is safe as long as hdr dropping is delayed */ | |
5066 | tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen, | |
fe8ab488 | 5067 | (tcp_seq)0, TH_RST|TH_ACK, &tra); |
1c79356b A |
5068 | } |
5069 | /* destroy temporarily created socket */ | |
91447636 | 5070 | if (dropsocket) { |
39037602 | 5071 | (void) soabort(so); |
91447636 | 5072 | tcp_unlock(so, 1, 0); |
39236c6e | 5073 | } else if ((inp != NULL) && (nosock == 0)) { |
6d2010ae A |
5074 | tcp_unlock(so, 1, 0); |
5075 | } | |
1c79356b A |
5076 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); |
5077 | return; | |
91447636 A |
5078 | dropnosock: |
5079 | nosock = 1; | |
1c79356b A |
5080 | drop: |
5081 | /* | |
5082 | * Drop space held by incoming segment and return. | |
5083 | */ | |
5084 | #if TCPDEBUG | |
9bccf70c A |
5085 | if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) |
5086 | tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen, | |
1c79356b | 5087 | &tcp_savetcp, 0); |
1c79356b A |
5088 | #endif |
5089 | m_freem(m); | |
1c79356b | 5090 | /* destroy temporarily created socket */ |
91447636 | 5091 | if (dropsocket) { |
39037602 | 5092 | (void) soabort(so); |
91447636 A |
5093 | tcp_unlock(so, 1, 0); |
5094 | } | |
6d2010ae A |
5095 | else if (nosock == 0) { |
5096 | tcp_unlock(so, 1, 0); | |
5097 | } | |
1c79356b A |
5098 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); |
5099 | return; | |
5100 | } | |
5101 | ||
8ad349bb A |
5102 | /* |
5103 | * Parse TCP options and place in tcpopt. | |
5104 | */ | |
3e170ce0 A |
5105 | static void |
5106 | tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcphdr *th, | |
5107 | struct tcpopt *to) | |
1c79356b A |
5108 | { |
5109 | u_short mss = 0; | |
5110 | int opt, optlen; | |
5111 | ||
5112 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
5113 | opt = cp[0]; | |
5114 | if (opt == TCPOPT_EOL) | |
5115 | break; | |
5116 | if (opt == TCPOPT_NOP) | |
5117 | optlen = 1; | |
5118 | else { | |
9bccf70c A |
5119 | if (cnt < 2) |
5120 | break; | |
1c79356b | 5121 | optlen = cp[1]; |
9bccf70c | 5122 | if (optlen < 2 || optlen > cnt) |
1c79356b A |
5123 | break; |
5124 | } | |
5125 | switch (opt) { | |
5126 | ||
5127 | default: | |
5128 | continue; | |
5129 | ||
5130 | case TCPOPT_MAXSEG: | |
5131 | if (optlen != TCPOLEN_MAXSEG) | |
5132 | continue; | |
5133 | if (!(th->th_flags & TH_SYN)) | |
5134 | continue; | |
5135 | bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); | |
9bccf70c | 5136 | NTOHS(mss); |
3e170ce0 A |
5137 | to->to_mss = mss; |
5138 | to->to_flags |= TOF_MSS; | |
1c79356b A |
5139 | break; |
5140 | ||
5141 | case TCPOPT_WINDOW: | |
5142 | if (optlen != TCPOLEN_WINDOW) | |
5143 | continue; | |
5144 | if (!(th->th_flags & TH_SYN)) | |
5145 | continue; | |
39236c6e | 5146 | to->to_flags |= TOF_SCALE; |
3e170ce0 | 5147 | to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); |
1c79356b A |
5148 | break; |
5149 | ||
5150 | case TCPOPT_TIMESTAMP: | |
5151 | if (optlen != TCPOLEN_TIMESTAMP) | |
5152 | continue; | |
8ad349bb | 5153 | to->to_flags |= TOF_TS; |
1c79356b A |
5154 | bcopy((char *)cp + 2, |
5155 | (char *)&to->to_tsval, sizeof(to->to_tsval)); | |
5156 | NTOHL(to->to_tsval); | |
5157 | bcopy((char *)cp + 6, | |
5158 | (char *)&to->to_tsecr, sizeof(to->to_tsecr)); | |
5159 | NTOHL(to->to_tsecr); | |
3e170ce0 A |
5160 | /* Re-enable sending Timestamps if we received them */ |
5161 | if (!(tp->t_flags & TF_REQ_TSTMP) && | |
5162 | tcp_do_rfc1323 == 1) | |
5163 | tp->t_flags |= TF_REQ_TSTMP; | |
1c79356b | 5164 | break; |
8ad349bb A |
5165 | case TCPOPT_SACK_PERMITTED: |
5166 | if (!tcp_do_sack || | |
5167 | optlen != TCPOLEN_SACK_PERMITTED) | |
1c79356b | 5168 | continue; |
1c79356b | 5169 | if (th->th_flags & TH_SYN) |
8ad349bb | 5170 | to->to_flags |= TOF_SACK; |
1c79356b | 5171 | break; |
8ad349bb A |
5172 | case TCPOPT_SACK: |
5173 | if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0) | |
1c79356b | 5174 | continue; |
8ad349bb A |
5175 | to->to_nsacks = (optlen - 2) / TCPOLEN_SACK; |
5176 | to->to_sacks = cp + 2; | |
5177 | tcpstat.tcps_sack_rcv_blocks++; | |
5178 | ||
1c79356b | 5179 | break; |
3e170ce0 A |
5180 | case TCPOPT_FASTOPEN: |
5181 | if (optlen == TCPOLEN_FASTOPEN_REQ) { | |
5182 | if (tp->t_state != TCPS_LISTEN) | |
5183 | continue; | |
5184 | ||
5185 | to->to_flags |= TOF_TFOREQ; | |
5186 | } else { | |
5187 | if (optlen < TCPOLEN_FASTOPEN_REQ || | |
5188 | (optlen - TCPOLEN_FASTOPEN_REQ) > TFO_COOKIE_LEN_MAX || | |
5189 | (optlen - TCPOLEN_FASTOPEN_REQ) < TFO_COOKIE_LEN_MIN) | |
5190 | continue; | |
5191 | if (tp->t_state != TCPS_LISTEN && | |
5192 | tp->t_state != TCPS_SYN_SENT) | |
5193 | continue; | |
5194 | ||
5195 | to->to_flags |= TOF_TFO; | |
5196 | to->to_tfo = cp + 1; | |
5197 | } | |
5198 | ||
5199 | break; | |
39236c6e A |
5200 | #if MPTCP |
5201 | case TCPOPT_MULTIPATH: | |
5202 | tcp_do_mptcp_options(tp, cp, th, to, optlen); | |
5203 | break; | |
5204 | #endif /* MPTCP */ | |
1c79356b A |
5205 | } |
5206 | } | |
3e170ce0 A |
5207 | } |
5208 | ||
5209 | static void | |
5210 | tcp_finalize_options(struct tcpcb *tp, struct tcpopt *to, unsigned int ifscope) | |
5211 | { | |
5212 | if (to->to_flags & TOF_TS) { | |
5213 | tp->t_flags |= TF_RCVD_TSTMP; | |
5214 | tp->ts_recent = to->to_tsval; | |
5215 | tp->ts_recent_age = tcp_now; | |
5216 | ||
5217 | } | |
5218 | if (to->to_flags & TOF_MSS) | |
5219 | tcp_mss(tp, to->to_mss, ifscope); | |
5220 | if (SACK_ENABLED(tp)) { | |
5221 | if (!(to->to_flags & TOF_SACK)) | |
5222 | tp->t_flagsext &= ~(TF_SACK_ENABLE); | |
5223 | else | |
5224 | tp->t_flags |= TF_SACK_PERMIT; | |
5225 | } | |
5226 | if (to->to_flags & TOF_SCALE) { | |
5227 | tp->t_flags |= TF_RCVD_SCALE; | |
5228 | tp->requested_s_scale = to->to_requested_s_scale; | |
5229 | ||
5230 | /* Re-enable window scaling, if the option is received */ | |
5231 | if (tp->request_r_scale > 0) | |
5232 | tp->t_flags |= TF_REQ_SCALE; | |
5233 | } | |
1c79356b A |
5234 | } |
5235 | ||
5236 | /* | |
5237 | * Pull out of band byte out of a segment so | |
5238 | * it doesn't appear in the user's data queue. | |
5239 | * It is still reflected in the segment length for | |
5240 | * sequencing purposes. | |
39037602 A |
5241 | * |
5242 | * @param off delayed to be droped hdrlen | |
1c79356b A |
5243 | */ |
5244 | static void | |
39037602 | 5245 | tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m, int off) |
1c79356b | 5246 | { |
9bccf70c | 5247 | int cnt = off + th->th_urp - 1; |
1c79356b A |
5248 | |
5249 | while (cnt >= 0) { | |
5250 | if (m->m_len > cnt) { | |
5251 | char *cp = mtod(m, caddr_t) + cnt; | |
5252 | struct tcpcb *tp = sototcpcb(so); | |
5253 | ||
5254 | tp->t_iobc = *cp; | |
5255 | tp->t_oobflags |= TCPOOB_HAVEDATA; | |
5256 | bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); | |
5257 | m->m_len--; | |
9bccf70c A |
5258 | if (m->m_flags & M_PKTHDR) |
5259 | m->m_pkthdr.len--; | |
1c79356b A |
5260 | return; |
5261 | } | |
5262 | cnt -= m->m_len; | |
5263 | m = m->m_next; | |
5264 | if (m == 0) | |
5265 | break; | |
5266 | } | |
5267 | panic("tcp_pulloutofband"); | |
5268 | } | |
5269 | ||
6d2010ae | 5270 | uint32_t |
39037602 | 5271 | get_base_rtt(struct tcpcb *tp) |
6d2010ae | 5272 | { |
ecc0ceb4 | 5273 | struct rtentry *rt = tp->t_inpcb->inp_route.ro_rt; |
39037602 | 5274 | return ((rt == NULL) ? 0 : rt->rtt_min); |
6d2010ae A |
5275 | } |
5276 | ||
5277 | /* Each value of RTT base represents the minimum RTT seen in a minute. | |
5278 | * We keep upto N_RTT_BASE minutes worth of history. | |
5279 | */ | |
5280 | void | |
5281 | update_base_rtt(struct tcpcb *tp, uint32_t rtt) | |
5282 | { | |
39037602 | 5283 | u_int32_t base_rtt, i; |
ecc0ceb4 | 5284 | struct rtentry *rt; |
39236c6e | 5285 | |
ecc0ceb4 A |
5286 | if ((rt = tp->t_inpcb->inp_route.ro_rt) == NULL) |
5287 | return; | |
5288 | if (rt->rtt_expire_ts == 0) { | |
5289 | RT_LOCK_SPIN(rt); | |
ecc0ceb4 A |
5290 | if (rt->rtt_expire_ts != 0) { |
5291 | RT_UNLOCK(rt); | |
5292 | goto update; | |
5293 | } | |
5294 | rt->rtt_expire_ts = tcp_now; | |
5295 | rt->rtt_index = 0; | |
5296 | rt->rtt_hist[0] = rtt; | |
39037602 | 5297 | rt->rtt_min = rtt; |
ecc0ceb4 A |
5298 | RT_UNLOCK(rt); |
5299 | return; | |
5300 | } | |
5301 | update: | |
39236c6e | 5302 | #if TRAFFIC_MGT |
ecc0ceb4 A |
5303 | /* |
5304 | * If the recv side is being throttled, check if the | |
5305 | * current RTT is closer to the base RTT seen in | |
5306 | * first (recent) two slots. If so, unthrottle the stream. | |
5307 | */ | |
5308 | if ((tp->t_flagsext & TF_RECV_THROTTLE) && | |
5309 | (int)(tcp_now - tp->t_recv_throttle_ts) >= TCP_RECV_THROTTLE_WIN) { | |
39037602 | 5310 | base_rtt = rt->rtt_min; |
ecc0ceb4 A |
5311 | if (tp->t_rttcur <= (base_rtt + target_qdelay)) { |
5312 | tp->t_flagsext &= ~TF_RECV_THROTTLE; | |
5313 | tp->t_recv_throttle_ts = 0; | |
39236c6e | 5314 | } |
ecc0ceb4 | 5315 | } |
39236c6e | 5316 | #endif /* TRAFFIC_MGT */ |
ecc0ceb4 A |
5317 | if ((int)(tcp_now - rt->rtt_expire_ts) >= |
5318 | TCP_RTT_HISTORY_EXPIRE_TIME) { | |
5319 | RT_LOCK_SPIN(rt); | |
5320 | /* check the condition again to avoid race */ | |
5321 | if ((int)(tcp_now - rt->rtt_expire_ts) >= | |
5322 | TCP_RTT_HISTORY_EXPIRE_TIME) { | |
5323 | rt->rtt_index++; | |
5324 | if (rt->rtt_index >= NRTT_HIST) | |
5325 | rt->rtt_index = 0; | |
5326 | rt->rtt_hist[rt->rtt_index] = rtt; | |
5327 | rt->rtt_expire_ts = tcp_now; | |
5328 | } else { | |
5329 | rt->rtt_hist[rt->rtt_index] = | |
5330 | min(rt->rtt_hist[rt->rtt_index], rtt); | |
6d2010ae | 5331 | } |
39037602 A |
5332 | /* forget the old value and update minimum */ |
5333 | rt->rtt_min = 0; | |
5334 | for (i = 0; i < NRTT_HIST; ++i) { | |
5335 | if (rt->rtt_hist[i] != 0 && | |
5336 | (rt->rtt_min == 0 || | |
5337 | rt->rtt_hist[i] < rt->rtt_min)) | |
5338 | rt->rtt_min = rt->rtt_hist[i]; | |
5339 | } | |
ecc0ceb4 | 5340 | RT_UNLOCK(rt); |
6d2010ae | 5341 | } else { |
ecc0ceb4 A |
5342 | rt->rtt_hist[rt->rtt_index] = |
5343 | min(rt->rtt_hist[rt->rtt_index], rtt); | |
39037602 A |
5344 | if (rt->rtt_min == 0) |
5345 | rt->rtt_min = rtt; | |
5346 | else | |
5347 | rt->rtt_min = min(rt->rtt_min, rtt); | |
6d2010ae A |
5348 | } |
5349 | } | |
5350 | ||
39236c6e A |
5351 | /* |
5352 | * If we have a timestamp reply, update smoothed RTT. If no timestamp is | |
39037602 A |
5353 | * present but transmit timer is running and timed sequence number was |
5354 | * acked, update smoothed RTT. | |
39236c6e A |
5355 | * |
5356 | * If timestamps are supported, a receiver can update RTT even if | |
5357 | * there is no outstanding data. | |
5358 | * | |
5359 | * Some boxes send broken timestamp replies during the SYN+ACK phase, | |
39037602 | 5360 | * ignore timestamps of 0or we could calculate a huge RTT and blow up |
39236c6e A |
5361 | * the retransmit timer. |
5362 | */ | |
5363 | static void | |
5364 | tcp_compute_rtt(struct tcpcb *tp, struct tcpopt *to, struct tcphdr *th) | |
5365 | { | |
3e170ce0 | 5366 | int rtt = 0; |
39236c6e | 5367 | VERIFY(to != NULL && th != NULL); |
3e170ce0 A |
5368 | if (tp->t_rtttime != 0 && SEQ_GT(th->th_ack, tp->t_rtseq)) { |
5369 | u_int32_t pipe_ack_val; | |
5370 | rtt = tcp_now - tp->t_rtttime; | |
5371 | /* | |
5372 | * Compute pipe ack -- the amount of data acknowledged | |
5373 | * in the last RTT | |
5374 | */ | |
5375 | if (SEQ_GT(th->th_ack, tp->t_pipeack_lastuna)) { | |
5376 | pipe_ack_val = th->th_ack - tp->t_pipeack_lastuna; | |
5377 | /* Update the sample */ | |
5378 | tp->t_pipeack_sample[tp->t_pipeack_ind++] = | |
5379 | pipe_ack_val; | |
5380 | tp->t_pipeack_ind %= TCP_PIPEACK_SAMPLE_COUNT; | |
5381 | ||
5382 | /* Compute the max of the pipeack samples */ | |
5383 | pipe_ack_val = tcp_get_max_pipeack(tp); | |
5384 | tp->t_pipeack = (pipe_ack_val > | |
5385 | TCP_CC_CWND_INIT_BYTES) ? | |
5386 | pipe_ack_val : 0; | |
5387 | } | |
5388 | /* start another measurement */ | |
5389 | tp->t_rtttime = 0; | |
5390 | } | |
39037602 | 5391 | if (((to->to_flags & TOF_TS) != 0) && |
39236c6e A |
5392 | (to->to_tsecr != 0) && |
5393 | TSTMP_GEQ(tcp_now, to->to_tsecr)) { | |
3e170ce0 | 5394 | tcp_xmit_timer(tp, (tcp_now - to->to_tsecr), |
39236c6e | 5395 | to->to_tsecr, th->th_ack); |
3e170ce0 A |
5396 | } else if (rtt > 0) { |
5397 | tcp_xmit_timer(tp, rtt, 0, th->th_ack); | |
39236c6e A |
5398 | } |
5399 | } | |
5400 | ||
1c79356b | 5401 | /* |
d190cdc3 A |
5402 | * Collect new round-trip time estimate and update averages and |
5403 | * current timeout. | |
1c79356b A |
5404 | */ |
5405 | static void | |
39037602 | 5406 | tcp_xmit_timer(struct tcpcb *tp, int rtt, |
39236c6e | 5407 | u_int32_t tsecr, tcp_seq th_ack) |
1c79356b | 5408 | { |
39037602 | 5409 | int delta; |
1c79356b | 5410 | |
d190cdc3 A |
5411 | /* |
5412 | * On AWDL interface, the initial RTT measurement on SYN | |
5413 | * can be wrong due to peer caching. Avoid the first RTT | |
5414 | * measurement as it might skew up the RTO. | |
5415 | * <rdar://problem/28739046> | |
5416 | */ | |
5417 | if (tp->t_inpcb->inp_last_outifp != NULL && | |
5418 | (tp->t_inpcb->inp_last_outifp->if_eflags & IFEF_AWDL) && | |
5419 | th_ack == tp->iss + 1) | |
5420 | return; | |
5421 | ||
39236c6e A |
5422 | if (tp->t_flagsext & TF_RECOMPUTE_RTT) { |
5423 | if (SEQ_GT(th_ack, tp->snd_una) && | |
5424 | SEQ_LEQ(th_ack, tp->snd_max) && | |
5425 | (tsecr == 0 || | |
5426 | TSTMP_GEQ(tsecr, tp->t_badrexmt_time))) { | |
5427 | /* | |
5428 | * We received a new ACk after a | |
39037602 | 5429 | * spurious timeout. Adapt retransmission |
39236c6e A |
5430 | * timer as described in rfc 4015. |
5431 | */ | |
5432 | tp->t_flagsext &= ~(TF_RECOMPUTE_RTT); | |
5433 | tp->t_badrexmt_time = 0; | |
5434 | tp->t_srtt = max(tp->t_srtt_prev, rtt); | |
5435 | tp->t_srtt = tp->t_srtt << TCP_RTT_SHIFT; | |
5436 | tp->t_rttvar = max(tp->t_rttvar_prev, (rtt >> 1)); | |
5437 | tp->t_rttvar = tp->t_rttvar << TCP_RTTVAR_SHIFT; | |
5438 | ||
5439 | if (tp->t_rttbest > (tp->t_srtt + tp->t_rttvar)) | |
5440 | tp->t_rttbest = tp->t_srtt + tp->t_rttvar; | |
5441 | ||
5442 | goto compute_rto; | |
5443 | } else { | |
5444 | return; | |
5445 | } | |
5446 | } | |
5447 | ||
1c79356b A |
5448 | tcpstat.tcps_rttupdated++; |
5449 | tp->t_rttupdated++; | |
6d2010ae A |
5450 | |
5451 | if (rtt > 0) { | |
5452 | tp->t_rttcur = rtt; | |
5453 | update_base_rtt(tp, rtt); | |
5454 | } | |
5455 | ||
1c79356b A |
5456 | if (tp->t_srtt != 0) { |
5457 | /* | |
5458 | * srtt is stored as fixed point with 5 bits after the | |
6d2010ae | 5459 | * binary point (i.e., scaled by 32). The following magic |
1c79356b A |
5460 | * is equivalent to the smoothing algorithm in rfc793 with |
5461 | * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed | |
6d2010ae A |
5462 | * point). |
5463 | * | |
39037602 A |
5464 | * Freebsd adjusts rtt to origin 0 by subtracting 1 |
5465 | * from the provided rtt value. This was required because | |
5466 | * of the way t_rtttime was initiailised to 1 before. | |
39236c6e | 5467 | * Since we changed t_rtttime to be based on |
6d2010ae | 5468 | * tcp_now, this extra adjustment is not needed. |
1c79356b | 5469 | */ |
6d2010ae | 5470 | delta = (rtt << TCP_DELTA_SHIFT) |
1c79356b A |
5471 | - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); |
5472 | ||
5473 | if ((tp->t_srtt += delta) <= 0) | |
5474 | tp->t_srtt = 1; | |
5475 | ||
5476 | /* | |
5477 | * We accumulate a smoothed rtt variance (actually, a | |
5478 | * smoothed mean difference), then set the retransmit | |
5479 | * timer to smoothed rtt + 4 times the smoothed variance. | |
5480 | * rttvar is stored as fixed point with 4 bits after the | |
5481 | * binary point (scaled by 16). The following is | |
5482 | * equivalent to rfc793 smoothing with an alpha of .75 | |
5483 | * (rttvar = rttvar*3/4 + |delta| / 4). This replaces | |
5484 | * rfc793's wired-in beta. | |
5485 | */ | |
5486 | if (delta < 0) | |
5487 | delta = -delta; | |
5488 | delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); | |
5489 | if ((tp->t_rttvar += delta) <= 0) | |
5490 | tp->t_rttvar = 1; | |
39037602 | 5491 | if (tp->t_rttbest == 0 || |
316670eb A |
5492 | tp->t_rttbest > (tp->t_srtt + tp->t_rttvar)) |
5493 | tp->t_rttbest = tp->t_srtt + tp->t_rttvar; | |
1c79356b A |
5494 | } else { |
5495 | /* | |
5496 | * No rtt measurement yet - use the unsmoothed rtt. | |
5497 | * Set the variance to half the rtt (so our first | |
5498 | * retransmit happens at 3*rtt). | |
5499 | */ | |
5500 | tp->t_srtt = rtt << TCP_RTT_SHIFT; | |
5501 | tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); | |
5502 | } | |
39236c6e A |
5503 | |
5504 | compute_rto: | |
39037602 | 5505 | nstat_route_rtt(tp->t_inpcb->inp_route.ro_rt, tp->t_srtt, |
39236c6e | 5506 | tp->t_rttvar); |
1c79356b A |
5507 | |
5508 | /* | |
5509 | * the retransmit should happen at rtt + 4 * rttvar. | |
5510 | * Because of the way we do the smoothing, srtt and rttvar | |
5511 | * will each average +1/2 tick of bias. When we compute | |
5512 | * the retransmit timer, we want 1/2 tick of rounding and | |
5513 | * 1 extra tick because of +-1/2 tick uncertainty in the | |
5514 | * firing of the timer. The bias will give us exactly the | |
5515 | * 1.5 tick we need. But, because the bias is | |
5516 | * statistical, we have to test that we don't drop below | |
5517 | * the minimum feasible timer (which is 2 ticks). | |
5518 | */ | |
5519 | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), | |
39037602 | 5520 | max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX, |
6d2010ae | 5521 | TCP_ADD_REXMTSLOP(tp)); |
1c79356b A |
5522 | |
5523 | /* | |
5524 | * We received an ack for a packet that wasn't retransmitted; | |
5525 | * it is probably safe to discard any error indications we've | |
5526 | * received recently. This isn't quite right, but close enough | |
5527 | * for now (a route might have failed after we sent a segment, | |
5528 | * and the return path might not be symmetrical). | |
5529 | */ | |
5530 | tp->t_softerror = 0; | |
5531 | } | |
5532 | ||
2d21ac55 A |
5533 | static inline unsigned int |
5534 | tcp_maxmtu(struct rtentry *rt) | |
5535 | { | |
5536 | unsigned int maxmtu; | |
5537 | ||
b0d623f7 | 5538 | RT_LOCK_ASSERT_HELD(rt); |
2d21ac55 A |
5539 | if (rt->rt_rmx.rmx_mtu == 0) |
5540 | maxmtu = rt->rt_ifp->if_mtu; | |
5541 | else | |
5542 | maxmtu = MIN(rt->rt_rmx.rmx_mtu, rt->rt_ifp->if_mtu); | |
5543 | ||
5544 | return (maxmtu); | |
5545 | } | |
5546 | ||
5547 | #if INET6 | |
5548 | static inline unsigned int | |
5549 | tcp_maxmtu6(struct rtentry *rt) | |
5550 | { | |
5551 | unsigned int maxmtu; | |
3e170ce0 | 5552 | struct nd_ifinfo *ndi = NULL; |
2d21ac55 | 5553 | |
b0d623f7 | 5554 | RT_LOCK_ASSERT_HELD(rt); |
316670eb A |
5555 | if ((ndi = ND_IFINFO(rt->rt_ifp)) != NULL && !ndi->initialized) |
5556 | ndi = NULL; | |
5557 | if (ndi != NULL) | |
5558 | lck_mtx_lock(&ndi->lock); | |
2d21ac55 A |
5559 | if (rt->rt_rmx.rmx_mtu == 0) |
5560 | maxmtu = IN6_LINKMTU(rt->rt_ifp); | |
5561 | else | |
5562 | maxmtu = MIN(rt->rt_rmx.rmx_mtu, IN6_LINKMTU(rt->rt_ifp)); | |
316670eb A |
5563 | if (ndi != NULL) |
5564 | lck_mtx_unlock(&ndi->lock); | |
2d21ac55 A |
5565 | |
5566 | return (maxmtu); | |
5567 | } | |
5568 | #endif | |
5569 | ||
1c79356b A |
5570 | /* |
5571 | * Determine a reasonable value for maxseg size. | |
5572 | * If the route is known, check route for mtu. | |
5573 | * If none, use an mss that can be handled on the outgoing | |
5574 | * interface without forcing IP to fragment; if bigger than | |
5575 | * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES | |
5576 | * to utilize large mbufs. If no route is found, route has no mtu, | |
5577 | * or the destination isn't local, use a default, hopefully conservative | |
5578 | * size (usually 512 or the default IP max size, but no more than the mtu | |
5579 | * of the interface), as we can't discover anything about intervening | |
5580 | * gateways or networks. We also initialize the congestion/slow start | |
39037602 A |
5581 | * window. While looking at the routing entry, we also initialize |
5582 | * other path-dependent parameters from pre-set or cached values | |
fe8ab488 | 5583 | * in the routing entry. |
1c79356b A |
5584 | * |
5585 | * Also take into account the space needed for options that we | |
5586 | * send regularly. Make maxseg shorter by that amount to assure | |
5587 | * that we can send maxseg amount of data even when the options | |
5588 | * are present. Store the upper limit of the length of options plus | |
5589 | * data in maxopd. | |
5590 | * | |
5591 | * NOTE that this routine is only called when we process an incoming | |
5592 | * segment, for outgoing segments only tcp_mssopt is called. | |
5593 | * | |
1c79356b A |
5594 | */ |
5595 | void | |
39037602 | 5596 | tcp_mss(struct tcpcb *tp, int offer, unsigned int input_ifscope) |
1c79356b | 5597 | { |
ecc0ceb4 | 5598 | struct rtentry *rt; |
1c79356b | 5599 | struct ifnet *ifp; |
ecc0ceb4 | 5600 | int rtt, mss; |
b0d623f7 | 5601 | u_int32_t bufsize; |
1c79356b A |
5602 | struct inpcb *inp; |
5603 | struct socket *so; | |
5604 | struct rmxp_tao *taop; | |
5605 | int origoffer = offer; | |
b0d623f7 | 5606 | u_int32_t sb_max_corrected; |
2d21ac55 | 5607 | int isnetlocal = 0; |
1c79356b | 5608 | #if INET6 |
9bccf70c A |
5609 | int isipv6; |
5610 | int min_protoh; | |
5611 | #endif | |
1c79356b A |
5612 | |
5613 | inp = tp->t_inpcb; | |
9bccf70c A |
5614 | #if INET6 |
5615 | isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0; | |
5616 | min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) | |
5617 | : sizeof (struct tcpiphdr); | |
5618 | #else | |
5619 | #define min_protoh (sizeof (struct tcpiphdr)) | |
5620 | #endif | |
b0d623f7 | 5621 | |
1c79356b | 5622 | #if INET6 |
2d21ac55 | 5623 | if (isipv6) { |
6d2010ae | 5624 | rt = tcp_rtlookup6(inp, input_ifscope); |
2d21ac55 | 5625 | } |
1c79356b A |
5626 | else |
5627 | #endif /* INET6 */ | |
2d21ac55 | 5628 | { |
c910b4d9 | 5629 | rt = tcp_rtlookup(inp, input_ifscope); |
2d21ac55 | 5630 | } |
6d2010ae A |
5631 | isnetlocal = (tp->t_flags & TF_LOCAL); |
5632 | ||
1c79356b A |
5633 | if (rt == NULL) { |
5634 | tp->t_maxopd = tp->t_maxseg = | |
5635 | #if INET6 | |
5636 | isipv6 ? tcp_v6mssdflt : | |
5637 | #endif /* INET6 */ | |
5638 | tcp_mssdflt; | |
5639 | return; | |
5640 | } | |
5641 | ifp = rt->rt_ifp; | |
d12e1678 A |
5642 | /* |
5643 | * Slower link window correction: | |
fe8ab488 A |
5644 | * If a value is specificied for slowlink_wsize use it for |
5645 | * PPP links believed to be on a serial modem (speed <128Kbps). | |
5646 | * Excludes 9600bps as it is the default value adversized | |
5647 | * by pseudo-devices over ppp. | |
d12e1678 | 5648 | */ |
39037602 | 5649 | if (ifp->if_type == IFT_PPP && slowlink_wsize > 0 && |
d12e1678 A |
5650 | ifp->if_baudrate > 9600 && ifp->if_baudrate <= 128000) { |
5651 | tp->t_flags |= TF_SLOWLINK; | |
5652 | } | |
1c79356b A |
5653 | so = inp->inp_socket; |
5654 | ||
5655 | taop = rmx_taop(rt->rt_rmx); | |
5656 | /* | |
5657 | * Offer == -1 means that we didn't receive SYN yet, | |
5658 | * use cached value in that case; | |
5659 | */ | |
5660 | if (offer == -1) | |
5661 | offer = taop->tao_mssopt; | |
5662 | /* | |
5663 | * Offer == 0 means that there was no MSS on the SYN segment, | |
5664 | * in this case we use tcp_mssdflt. | |
5665 | */ | |
5666 | if (offer == 0) | |
5667 | offer = | |
5668 | #if INET6 | |
5669 | isipv6 ? tcp_v6mssdflt : | |
5670 | #endif /* INET6 */ | |
5671 | tcp_mssdflt; | |
e5568f75 A |
5672 | else { |
5673 | /* | |
5674 | * Prevent DoS attack with too small MSS. Round up | |
5675 | * to at least minmss. | |
5676 | */ | |
5677 | offer = max(offer, tcp_minmss); | |
1c79356b A |
5678 | /* |
5679 | * Sanity check: make sure that maxopd will be large | |
5680 | * enough to allow some data on segments even is the | |
5681 | * all the option space is used (40bytes). Otherwise | |
5682 | * funny things may happen in tcp_output. | |
5683 | */ | |
5684 | offer = max(offer, 64); | |
e5568f75 | 5685 | } |
1c79356b A |
5686 | taop->tao_mssopt = offer; |
5687 | ||
5688 | /* | |
5689 | * While we're here, check if there's an initial rtt | |
5690 | * or rttvar. Convert from the route-table units | |
5691 | * to scaled multiples of the slow timeout timer. | |
5692 | */ | |
316670eb A |
5693 | if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt) != 0) { |
5694 | tcp_getrt_rtt(tp, rt); | |
5695 | } else { | |
6d2010ae | 5696 | tp->t_rttmin = isnetlocal ? tcp_TCPTV_MIN : TCPTV_REXMTMIN; |
316670eb | 5697 | } |
2d21ac55 | 5698 | |
9bccf70c | 5699 | #if INET6 |
2d21ac55 A |
5700 | mss = (isipv6 ? tcp_maxmtu6(rt) : tcp_maxmtu(rt)); |
5701 | #else | |
5702 | mss = tcp_maxmtu(rt); | |
9bccf70c | 5703 | #endif |
3e170ce0 A |
5704 | |
5705 | #if NECP | |
5706 | // At this point, the mss is just the MTU. Adjust if necessary. | |
5707 | mss = necp_socket_get_effective_mtu(inp, mss); | |
5708 | #endif /* NECP */ | |
5709 | ||
2d21ac55 A |
5710 | mss -= min_protoh; |
5711 | ||
5712 | if (rt->rt_rmx.rmx_mtu == 0) { | |
1c79356b A |
5713 | #if INET6 |
5714 | if (isipv6) { | |
2d21ac55 | 5715 | if (!isnetlocal) |
1c79356b A |
5716 | mss = min(mss, tcp_v6mssdflt); |
5717 | } else | |
5718 | #endif /* INET6 */ | |
2d21ac55 | 5719 | if (!isnetlocal) |
1c79356b A |
5720 | mss = min(mss, tcp_mssdflt); |
5721 | } | |
2d21ac55 | 5722 | |
1c79356b A |
5723 | mss = min(mss, offer); |
5724 | /* | |
5725 | * maxopd stores the maximum length of data AND options | |
5726 | * in a segment; maxseg is the amount of data in a normal | |
5727 | * segment. We need to store this value (maxopd) apart | |
5728 | * from maxseg, because now every segment carries options | |
5729 | * and thus we normally have somewhat less data in segments. | |
5730 | */ | |
5731 | tp->t_maxopd = mss; | |
5732 | ||
5733 | /* | |
8ad349bb A |
5734 | * origoffer==-1 indicates, that no segments were received yet. |
5735 | * In this case we just guess. | |
1c79356b | 5736 | */ |
8ad349bb | 5737 | if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && |
1c79356b A |
5738 | (origoffer == -1 || |
5739 | (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) | |
5740 | mss -= TCPOLEN_TSTAMP_APPA; | |
1c79356b | 5741 | |
39236c6e A |
5742 | #if MPTCP |
5743 | mss -= mptcp_adj_mss(tp, FALSE); | |
5744 | #endif /* MPTCP */ | |
5745 | tp->t_maxseg = mss; | |
39037602 | 5746 | |
2d21ac55 A |
5747 | /* |
5748 | * Calculate corrected value for sb_max; ensure to upgrade the | |
5749 | * numerator for large sb_max values else it will overflow. | |
5750 | */ | |
5751 | sb_max_corrected = (sb_max * (u_int64_t)MCLBYTES) / (MSIZE + MCLBYTES); | |
5752 | ||
1c79356b | 5753 | /* |
55e303ae A |
5754 | * If there's a pipesize (ie loopback), change the socket |
5755 | * buffer to that size only if it's bigger than the current | |
5756 | * sockbuf size. Make the socket buffers an integral | |
1c79356b A |
5757 | * number of mss units; if the mss is larger than |
5758 | * the socket buffer, decrease the mss. | |
5759 | */ | |
5760 | #if RTV_SPIPE | |
55e303ae A |
5761 | bufsize = rt->rt_rmx.rmx_sendpipe; |
5762 | if (bufsize < so->so_snd.sb_hiwat) | |
1c79356b A |
5763 | #endif |
5764 | bufsize = so->so_snd.sb_hiwat; | |
5765 | if (bufsize < mss) | |
5766 | mss = bufsize; | |
5767 | else { | |
2d21ac55 A |
5768 | bufsize = (((bufsize + (u_int64_t)mss - 1) / (u_int64_t)mss) * (u_int64_t)mss); |
5769 | if (bufsize > sb_max_corrected) | |
5770 | bufsize = sb_max_corrected; | |
1c79356b A |
5771 | (void)sbreserve(&so->so_snd, bufsize); |
5772 | } | |
5773 | tp->t_maxseg = mss; | |
5774 | ||
39037602 A |
5775 | /* |
5776 | * Update MSS using recommendation from link status report. This is | |
5777 | * temporary | |
5778 | */ | |
5779 | tcp_update_mss_locked(so, ifp); | |
5780 | ||
1c79356b | 5781 | #if RTV_RPIPE |
55e303ae A |
5782 | bufsize = rt->rt_rmx.rmx_recvpipe; |
5783 | if (bufsize < so->so_rcv.sb_hiwat) | |
1c79356b A |
5784 | #endif |
5785 | bufsize = so->so_rcv.sb_hiwat; | |
5786 | if (bufsize > mss) { | |
2d21ac55 A |
5787 | bufsize = (((bufsize + (u_int64_t)mss - 1) / (u_int64_t)mss) * (u_int64_t)mss); |
5788 | if (bufsize > sb_max_corrected) | |
5789 | bufsize = sb_max_corrected; | |
1c79356b A |
5790 | (void)sbreserve(&so->so_rcv, bufsize); |
5791 | } | |
9bccf70c | 5792 | |
6d2010ae | 5793 | set_tcp_stream_priority(so); |
1c79356b A |
5794 | |
5795 | if (rt->rt_rmx.rmx_ssthresh) { | |
5796 | /* | |
5797 | * There's some sort of gateway or interface | |
5798 | * buffer limit on the path. Use this to set | |
fe8ab488 A |
5799 | * slow-start threshold, but set the threshold to |
5800 | * no less than 2*mss. | |
1c79356b A |
5801 | */ |
5802 | tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); | |
5803 | tcpstat.tcps_usedssthresh++; | |
b0d623f7 | 5804 | } else { |
cf7d32b8 | 5805 | tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; |
b0d623f7 | 5806 | } |
cf7d32b8 | 5807 | |
6d2010ae A |
5808 | /* |
5809 | * Set the slow-start flight size depending on whether this | |
5810 | * is a local network or not. | |
5811 | */ | |
5812 | if (CC_ALGO(tp)->cwnd_init != NULL) | |
5813 | CC_ALGO(tp)->cwnd_init(tp); | |
5814 | ||
fe8ab488 | 5815 | tcp_ccdbg_trace(tp, NULL, TCP_CC_CWND_INIT); |
6d2010ae | 5816 | |
b0d623f7 A |
5817 | /* Route locked during lookup above */ |
5818 | RT_UNLOCK(rt); | |
1c79356b A |
5819 | } |
5820 | ||
5821 | /* | |
5822 | * Determine the MSS option to send on an outgoing SYN. | |
5823 | */ | |
5824 | int | |
39037602 | 5825 | tcp_mssopt(struct tcpcb *tp) |
1c79356b A |
5826 | { |
5827 | struct rtentry *rt; | |
8ad349bb | 5828 | int mss; |
1c79356b | 5829 | #if INET6 |
9bccf70c A |
5830 | int isipv6; |
5831 | int min_protoh; | |
5832 | #endif | |
1c79356b | 5833 | |
9bccf70c A |
5834 | #if INET6 |
5835 | isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0; | |
5836 | min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr) | |
5837 | : sizeof (struct tcpiphdr); | |
5838 | #else | |
5839 | #define min_protoh (sizeof (struct tcpiphdr)) | |
5840 | #endif | |
b0d623f7 | 5841 | |
1c79356b A |
5842 | #if INET6 |
5843 | if (isipv6) | |
6d2010ae | 5844 | rt = tcp_rtlookup6(tp->t_inpcb, IFSCOPE_NONE); |
1c79356b A |
5845 | else |
5846 | #endif /* INET6 */ | |
c910b4d9 | 5847 | rt = tcp_rtlookup(tp->t_inpcb, IFSCOPE_NONE); |
2d21ac55 | 5848 | if (rt == NULL) { |
2d21ac55 | 5849 | return ( |
1c79356b A |
5850 | #if INET6 |
5851 | isipv6 ? tcp_v6mssdflt : | |
5852 | #endif /* INET6 */ | |
2d21ac55 A |
5853 | tcp_mssdflt); |
5854 | } | |
d12e1678 A |
5855 | /* |
5856 | * Slower link window correction: | |
5857 | * If a value is specificied for slowlink_wsize use it for PPP links | |
5858 | * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as | |
5859 | * it is the default value adversized by pseudo-devices over ppp. | |
5860 | */ | |
39037602 | 5861 | if (rt->rt_ifp->if_type == IFT_PPP && slowlink_wsize > 0 && |
d12e1678 A |
5862 | rt->rt_ifp->if_baudrate > 9600 && rt->rt_ifp->if_baudrate <= 128000) { |
5863 | tp->t_flags |= TF_SLOWLINK; | |
5864 | } | |
1c79356b | 5865 | |
8ad349bb | 5866 | #if INET6 |
2d21ac55 A |
5867 | mss = (isipv6 ? tcp_maxmtu6(rt) : tcp_maxmtu(rt)); |
5868 | #else | |
5869 | mss = tcp_maxmtu(rt); | |
8ad349bb | 5870 | #endif |
b0d623f7 A |
5871 | /* Route locked during lookup above */ |
5872 | RT_UNLOCK(rt); | |
3e170ce0 A |
5873 | |
5874 | #if NECP | |
5875 | // At this point, the mss is just the MTU. Adjust if necessary. | |
5876 | mss = necp_socket_get_effective_mtu(tp->t_inpcb, mss); | |
5877 | #endif /* NECP */ | |
5878 | ||
8ad349bb | 5879 | return (mss - min_protoh); |
9bccf70c A |
5880 | } |
5881 | ||
9bccf70c | 5882 | /* |
8ad349bb A |
5883 | * On a partial ack arrives, force the retransmission of the |
5884 | * next unacknowledged segment. Do not clear tp->t_dupacks. | |
6d2010ae | 5885 | * By setting snd_nxt to th_ack, this forces retransmission timer to |
8ad349bb | 5886 | * be started again. |
9bccf70c | 5887 | */ |
8ad349bb | 5888 | static void |
39037602 | 5889 | tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th) |
9bccf70c | 5890 | { |
9bccf70c | 5891 | tcp_seq onxt = tp->snd_nxt; |
b0d623f7 | 5892 | u_int32_t ocwnd = tp->snd_cwnd; |
9bccf70c | 5893 | tp->t_timer[TCPT_REXMT] = 0; |
fe8ab488 | 5894 | tp->t_timer[TCPT_PTO] = 0; |
9bccf70c A |
5895 | tp->t_rtttime = 0; |
5896 | tp->snd_nxt = th->th_ack; | |
5897 | /* | |
5898 | * Set snd_cwnd to one segment beyond acknowledged offset | |
39037602 | 5899 | * (tp->snd_una has not yet been updated when this function |
9bccf70c A |
5900 | * is called) |
5901 | */ | |
39236c6e | 5902 | tp->snd_cwnd = tp->t_maxseg + BYTES_ACKED(th, tp); |
91447636 | 5903 | tp->t_flags |= TF_ACKNOW; |
9bccf70c A |
5904 | (void) tcp_output(tp); |
5905 | tp->snd_cwnd = ocwnd; | |
5906 | if (SEQ_GT(onxt, tp->snd_nxt)) | |
5907 | tp->snd_nxt = onxt; | |
5908 | /* | |
5909 | * Partial window deflation. Relies on fact that tp->snd_una | |
5910 | * not updated yet. | |
5911 | */ | |
39236c6e A |
5912 | if (tp->snd_cwnd > BYTES_ACKED(th, tp)) |
5913 | tp->snd_cwnd -= BYTES_ACKED(th, tp); | |
2d21ac55 A |
5914 | else |
5915 | tp->snd_cwnd = 0; | |
5916 | tp->snd_cwnd += tp->t_maxseg; | |
1c79356b | 5917 | } |
91447636 A |
5918 | |
5919 | /* | |
5920 | * Drop a random TCP connection that hasn't been serviced yet and | |
5921 | * is eligible for discard. There is a one in qlen chance that | |
5922 | * we will return a null, saying that there are no dropable | |
5923 | * requests. In this case, the protocol specific code should drop | |
5924 | * the new request. This insures fairness. | |
5925 | * | |
5926 | * The listening TCP socket "head" must be locked | |
5927 | */ | |
5928 | static int | |
2d21ac55 | 5929 | tcp_dropdropablreq(struct socket *head) |
91447636 | 5930 | { |
2d21ac55 | 5931 | struct socket *so, *sonext; |
91447636 | 5932 | unsigned int i, j, qlen; |
39236c6e A |
5933 | static u_int32_t rnd = 0; |
5934 | static u_int64_t old_runtime; | |
91447636 | 5935 | static unsigned int cur_cnt, old_cnt; |
39236c6e | 5936 | u_int64_t now_sec; |
91447636 | 5937 | struct inpcb *inp = NULL; |
3a60a9f5 | 5938 | struct tcpcb *tp; |
2d21ac55 A |
5939 | |
5940 | if ((head->so_options & SO_ACCEPTCONN) == 0) | |
39236c6e A |
5941 | return (0); |
5942 | ||
5943 | if (TAILQ_EMPTY(&head->so_incomp)) | |
5944 | return (0); | |
5945 | ||
39037602 | 5946 | /* |
39236c6e A |
5947 | * Check if there is any socket in the incomp queue |
5948 | * that is closed because of a reset from the peer and is | |
5949 | * waiting to be garbage collected. If so, pick that as | |
5950 | * the victim | |
5951 | */ | |
5952 | TAILQ_FOREACH_SAFE(so, &head->so_incomp, so_list, sonext) { | |
5953 | inp = sotoinpcb(so); | |
5954 | tp = intotcpcb(inp); | |
5955 | if (tp != NULL && tp->t_state == TCPS_CLOSED && | |
5956 | so->so_head != NULL && | |
5957 | (so->so_state & (SS_INCOMP|SS_CANTSENDMORE|SS_CANTRCVMORE)) == | |
5958 | (SS_INCOMP|SS_CANTSENDMORE|SS_CANTRCVMORE)) { | |
39037602 A |
5959 | /* |
5960 | * The listen socket is already locked but we | |
39236c6e A |
5961 | * can lock this socket here without lock ordering |
5962 | * issues because it is in the incomp queue and | |
5963 | * is not visible to others. | |
5964 | */ | |
5965 | if (lck_mtx_try_lock(&inp->inpcb_mtx)) { | |
5966 | so->so_usecount++; | |
5967 | goto found_victim; | |
5968 | } else { | |
5969 | continue; | |
5970 | } | |
5971 | } | |
5972 | } | |
2d21ac55 A |
5973 | |
5974 | so = TAILQ_FIRST(&head->so_incomp); | |
2d21ac55 | 5975 | |
39236c6e A |
5976 | now_sec = net_uptime(); |
5977 | if ((i = (now_sec - old_runtime)) != 0) { | |
5978 | old_runtime = now_sec; | |
91447636 A |
5979 | old_cnt = cur_cnt / i; |
5980 | cur_cnt = 0; | |
5981 | } | |
39037602 | 5982 | |
91447636 | 5983 | qlen = head->so_incqlen; |
39236c6e A |
5984 | if (rnd == 0) |
5985 | rnd = RandomULong(); | |
5986 | ||
91447636 A |
5987 | if (++cur_cnt > qlen || old_cnt > qlen) { |
5988 | rnd = (314159 * rnd + 66329) & 0xffff; | |
5989 | j = ((qlen + 1) * rnd) >> 16; | |
5990 | ||
5991 | while (j-- && so) | |
5992 | so = TAILQ_NEXT(so, so_list); | |
5993 | } | |
2d21ac55 | 5994 | /* Find a connection that is not already closing (or being served) */ |
91447636 A |
5995 | while (so) { |
5996 | inp = (struct inpcb *)so->so_pcb; | |
39037602 | 5997 | |
2d21ac55 A |
5998 | sonext = TAILQ_NEXT(so, so_list); |
5999 | ||
39037602 | 6000 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) |
39236c6e | 6001 | != WNT_STOPUSING) { |
39037602 | 6002 | /* |
39236c6e | 6003 | * Avoid the issue of a socket being accepted |
39037602 A |
6004 | * by one input thread and being dropped by |
6005 | * another input thread. If we can't get a hold | |
6006 | * on this mutex, then grab the next socket in | |
39236c6e | 6007 | * line. |
2d21ac55 | 6008 | */ |
6d2010ae | 6009 | if (lck_mtx_try_lock(&inp->inpcb_mtx)) { |
2d21ac55 | 6010 | so->so_usecount++; |
39037602 | 6011 | if ((so->so_usecount == 2) && |
39236c6e A |
6012 | (so->so_state & SS_INCOMP) && |
6013 | !(so->so_flags & SOF_INCOMP_INPROGRESS)) { | |
2d21ac55 | 6014 | break; |
39236c6e | 6015 | } else { |
39037602 A |
6016 | /* |
6017 | * don't use if being accepted or | |
39236c6e A |
6018 | * used in any other way |
6019 | */ | |
2d21ac55 A |
6020 | in_pcb_checkstate(inp, WNT_RELEASE, 1); |
6021 | tcp_unlock(so, 1, 0); | |
6022 | } | |
39236c6e | 6023 | } else { |
39037602 A |
6024 | /* |
6025 | * do not try to lock the inp in | |
6026 | * in_pcb_checkstate because the lock | |
39236c6e | 6027 | * is already held in some other thread. |
b0d623f7 A |
6028 | * Only drop the inp_wntcnt reference. |
6029 | */ | |
6030 | in_pcb_checkstate(inp, WNT_RELEASE, 1); | |
6031 | } | |
2d21ac55 A |
6032 | } |
6033 | so = sonext; | |
91447636 | 6034 | } |
39236c6e A |
6035 | if (so == NULL) { |
6036 | return (0); | |
6037 | } | |
2d21ac55 | 6038 | |
2d21ac55 A |
6039 | /* Makes sure socket is still in the right state to be discarded */ |
6040 | ||
91447636 A |
6041 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { |
6042 | tcp_unlock(so, 1, 0); | |
39236c6e | 6043 | return (0); |
91447636 | 6044 | } |
2d21ac55 | 6045 | |
39236c6e | 6046 | found_victim: |
2d21ac55 | 6047 | if (so->so_usecount != 2 || !(so->so_state & SS_INCOMP)) { |
6d2010ae | 6048 | /* do not discard: that socket is being accepted */ |
2d21ac55 | 6049 | tcp_unlock(so, 1, 0); |
39236c6e | 6050 | return (0); |
2d21ac55 A |
6051 | } |
6052 | ||
6d2010ae | 6053 | TAILQ_REMOVE(&head->so_incomp, so, so_list); |
d190cdc3 A |
6054 | head->so_incqlen--; |
6055 | head->so_qlen--; | |
6056 | so->so_state &= ~SS_INCOMP; | |
6057 | so->so_flags |= SOF_OVERFLOW; | |
6058 | so->so_head = NULL; | |
6d2010ae | 6059 | tcp_unlock(head, 0, 0); |
91447636 | 6060 | |
6d2010ae | 6061 | lck_mtx_assert(&inp->inpcb_mtx, LCK_MTX_ASSERT_OWNED); |
3a60a9f5 | 6062 | tp = sototcpcb(so); |
6d2010ae A |
6063 | |
6064 | tcp_close(tp); | |
6d2010ae | 6065 | if (inp->inp_wantcnt > 0 && inp->inp_wantcnt != WNT_STOPUSING) { |
39037602 | 6066 | /* |
39236c6e | 6067 | * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE |
6d2010ae A |
6068 | * doesn't require a lock, it could have happened while |
6069 | * we are holding the lock. This pcb will have to | |
6070 | * be garbage collected later. | |
6071 | * Release the reference held for so_incomp queue | |
6072 | */ | |
d190cdc3 | 6073 | VERIFY(so->so_usecount > 0); |
6d2010ae | 6074 | so->so_usecount--; |
6d2010ae A |
6075 | tcp_unlock(so, 1, 0); |
6076 | } else { | |
39037602 A |
6077 | /* |
6078 | * Unlock this socket and leave the reference on. | |
6079 | * We need to acquire the pcbinfo lock in order to | |
6080 | * fully dispose it off | |
6d2010ae A |
6081 | */ |
6082 | tcp_unlock(so, 0, 0); | |
6083 | ||
39236c6e | 6084 | lck_rw_lock_exclusive(tcbinfo.ipi_lock); |
6d2010ae A |
6085 | |
6086 | tcp_lock(so, 0, 0); | |
6d2010ae | 6087 | /* Release the reference held for so_incomp queue */ |
d190cdc3 | 6088 | VERIFY(so->so_usecount > 0); |
6d2010ae A |
6089 | so->so_usecount--; |
6090 | ||
39037602 A |
6091 | if (so->so_usecount != 1 || |
6092 | (inp->inp_wantcnt > 0 && | |
39236c6e | 6093 | inp->inp_wantcnt != WNT_STOPUSING)) { |
39037602 A |
6094 | /* |
6095 | * There is an extra wantcount or usecount | |
6096 | * that must have been added when the socket | |
6097 | * was unlocked. This socket will have to be | |
39236c6e | 6098 | * garbage collected later |
6d2010ae A |
6099 | */ |
6100 | tcp_unlock(so, 1, 0); | |
6101 | } else { | |
6d2010ae | 6102 | /* Drop the reference held for this function */ |
d190cdc3 | 6103 | VERIFY(so->so_usecount > 0); |
6d2010ae A |
6104 | so->so_usecount--; |
6105 | ||
6106 | in_pcbdispose(inp); | |
6107 | } | |
39236c6e | 6108 | lck_rw_done(tcbinfo.ipi_lock); |
6d2010ae | 6109 | } |
3a60a9f5 | 6110 | tcpstat.tcps_drops++; |
6d2010ae | 6111 | |
3a60a9f5 | 6112 | tcp_lock(head, 0, 0); |
6d2010ae A |
6113 | return(1); |
6114 | } | |
6115 | ||
6116 | /* Set background congestion control on a socket */ | |
6117 | void | |
6118 | tcp_set_background_cc(struct socket *so) | |
6119 | { | |
6120 | tcp_set_new_cc(so, TCP_CC_ALGO_BACKGROUND_INDEX); | |
6121 | } | |
6122 | ||
6123 | /* Set foreground congestion control on a socket */ | |
6124 | void | |
6125 | tcp_set_foreground_cc(struct socket *so) | |
6126 | { | |
fe8ab488 A |
6127 | if (tcp_use_newreno) |
6128 | tcp_set_new_cc(so, TCP_CC_ALGO_NEWRENO_INDEX); | |
6129 | else | |
6130 | tcp_set_new_cc(so, TCP_CC_ALGO_CUBIC_INDEX); | |
6d2010ae A |
6131 | } |
6132 | ||
6133 | static void | |
6134 | tcp_set_new_cc(struct socket *so, uint16_t cc_index) | |
6135 | { | |
6136 | struct inpcb *inp = sotoinpcb(so); | |
6137 | struct tcpcb *tp = intotcpcb(inp); | |
39236c6e | 6138 | u_char old_cc_index = 0; |
6d2010ae A |
6139 | if (tp->tcp_cc_index != cc_index) { |
6140 | ||
6141 | old_cc_index = tp->tcp_cc_index; | |
6142 | ||
6143 | if (CC_ALGO(tp)->cleanup != NULL) | |
6144 | CC_ALGO(tp)->cleanup(tp); | |
6145 | tp->tcp_cc_index = cc_index; | |
6146 | ||
fe8ab488 A |
6147 | tcp_cc_allocate_state(tp); |
6148 | ||
6149 | if (CC_ALGO(tp)->switch_to != NULL) | |
6150 | CC_ALGO(tp)->switch_to(tp, old_cc_index); | |
6151 | ||
6152 | tcp_ccdbg_trace(tp, NULL, TCP_CC_CHANGE_ALGO); | |
6d2010ae | 6153 | } |
91447636 A |
6154 | } |
6155 | ||
316670eb A |
6156 | void |
6157 | tcp_set_recv_bg(struct socket *so) | |
6158 | { | |
6159 | if (!IS_TCP_RECV_BG(so)) | |
39037602 | 6160 | so->so_flags1 |= SOF1_TRAFFIC_MGT_TCP_RECVBG; |
39236c6e A |
6161 | |
6162 | /* Unset Large Receive Offload on background sockets */ | |
6163 | so_set_lro(so, SO_TC_BK); | |
316670eb A |
6164 | } |
6165 | ||
6166 | void | |
6167 | tcp_clear_recv_bg(struct socket *so) | |
6168 | { | |
6169 | if (IS_TCP_RECV_BG(so)) | |
39037602 | 6170 | so->so_flags1 &= ~(SOF1_TRAFFIC_MGT_TCP_RECVBG); |
39236c6e | 6171 | |
39037602 A |
6172 | /* |
6173 | * Set/unset use of Large Receive Offload depending on | |
39236c6e A |
6174 | * the traffic class |
6175 | */ | |
6176 | so_set_lro(so, so->so_traffic_class); | |
316670eb A |
6177 | } |
6178 | ||
6179 | void | |
6180 | inp_fc_unthrottle_tcp(struct inpcb *inp) | |
6181 | { | |
6182 | struct tcpcb *tp = inp->inp_ppcb; | |
6183 | /* | |
6184 | * Back off the slow-start threshold and enter | |
6185 | * congestion avoidance phase | |
6186 | */ | |
6187 | if (CC_ALGO(tp)->pre_fr != NULL) | |
6188 | CC_ALGO(tp)->pre_fr(tp); | |
6189 | ||
6190 | tp->snd_cwnd = tp->snd_ssthresh; | |
3e170ce0 | 6191 | tp->t_flagsext &= ~TF_CWND_NONVALIDATED; |
316670eb A |
6192 | /* |
6193 | * Restart counting for ABC as we changed the | |
6194 | * congestion window just now. | |
6195 | */ | |
6196 | tp->t_bytes_acked = 0; | |
6197 | ||
6198 | /* Reset retransmit shift as we know that the reason | |
39037602 | 6199 | * for delay in sending a packet is due to flow |
316670eb A |
6200 | * control on the outgoing interface. There is no need |
6201 | * to backoff retransmit timer. | |
6202 | */ | |
39037602 | 6203 | TCP_RESET_REXMT_STATE(tp); |
316670eb A |
6204 | |
6205 | /* | |
6206 | * Start the output stream again. Since we are | |
6207 | * not retransmitting data, do not reset the | |
6208 | * retransmit timer or rtt calculation. | |
6209 | */ | |
6210 | tcp_output(tp); | |
6211 | } | |
39037602 | 6212 | |
8ad349bb A |
6213 | static int |
6214 | tcp_getstat SYSCTL_HANDLER_ARGS | |
6215 | { | |
2d21ac55 | 6216 | #pragma unused(oidp, arg1, arg2) |
8ad349bb A |
6217 | |
6218 | int error; | |
6219 | ||
39236c6e A |
6220 | proc_t caller = PROC_NULL; |
6221 | proc_t caller_parent = PROC_NULL; | |
6222 | char command_name[MAXCOMLEN + 1] = ""; | |
6223 | char parent_name[MAXCOMLEN + 1] = ""; | |
6224 | ||
6225 | if ((caller = proc_self()) != PROC_NULL) { | |
6226 | /* get process name */ | |
6227 | strlcpy(command_name, caller->p_comm, sizeof(command_name)); | |
6228 | ||
6229 | /* get parent process name if possible */ | |
6230 | if ((caller_parent = proc_find(caller->p_ppid)) != PROC_NULL) { | |
6231 | strlcpy(parent_name, caller_parent->p_comm, | |
6232 | sizeof(parent_name)); | |
6233 | proc_rele(caller_parent); | |
6234 | } | |
6235 | ||
6236 | if ((escape_str(command_name, strlen(command_name), | |
6237 | sizeof(command_name)) == 0) && | |
6238 | (escape_str(parent_name, strlen(parent_name), | |
6239 | sizeof(parent_name)) == 0)) { | |
6240 | kern_asl_msg(LOG_DEBUG, "messagetracer", | |
6241 | 5, | |
6242 | "com.apple.message.domain", | |
6243 | "com.apple.kernel.tcpstat", /* 1 */ | |
6244 | "com.apple.message.signature", | |
6245 | "tcpstat", /* 2 */ | |
6246 | "com.apple.message.signature2", command_name, /* 3 */ | |
6247 | "com.apple.message.signature3", parent_name, /* 4 */ | |
6248 | "com.apple.message.summarize", "YES", /* 5 */ | |
6249 | NULL); | |
6250 | } | |
6251 | } | |
6252 | if (caller != PROC_NULL) | |
6253 | proc_rele(caller); | |
6254 | ||
8ad349bb A |
6255 | if (req->oldptr == 0) { |
6256 | req->oldlen= (size_t)sizeof(struct tcpstat); | |
6257 | } | |
6258 | ||
2d21ac55 | 6259 | error = SYSCTL_OUT(req, &tcpstat, MIN(sizeof (tcpstat), req->oldlen)); |
8ad349bb A |
6260 | |
6261 | return (error); | |
6262 | ||
6263 | } | |
6264 | ||
39236c6e A |
6265 | /* |
6266 | * Checksum extended TCP header and data. | |
6267 | */ | |
6268 | int | |
6269 | tcp_input_checksum(int af, struct mbuf *m, struct tcphdr *th, int off, int tlen) | |
6270 | { | |
6271 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
6272 | ||
6273 | switch (af) { | |
6274 | case AF_INET: { | |
6275 | struct ip *ip = mtod(m, struct ip *); | |
6276 | struct ipovly *ipov = (struct ipovly *)ip; | |
6277 | ||
6278 | if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_DID_CSUM) | |
6279 | return (0); | |
6280 | ||
6281 | if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) || | |
6282 | (m->m_pkthdr.pkt_flags & PKTF_LOOP)) && | |
6283 | (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) { | |
6284 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { | |
6285 | th->th_sum = m->m_pkthdr.csum_rx_val; | |
6286 | } else { | |
6287 | uint16_t sum = m->m_pkthdr.csum_rx_val; | |
6288 | uint16_t start = m->m_pkthdr.csum_rx_start; | |
6289 | ||
6290 | /* | |
6291 | * Perform 1's complement adjustment of octets | |
6292 | * that got included/excluded in the hardware- | |
6293 | * calculated checksum value. Ignore cases | |
6294 | * where the value includes or excludes the IP | |
6295 | * header span, as the sum for those octets | |
6296 | * would already be 0xffff and thus no-op. | |
6297 | */ | |
6298 | if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) && | |
6299 | start != 0 && (off - start) != off) { | |
6300 | #if BYTE_ORDER != BIG_ENDIAN | |
6301 | if (start < off) { | |
6302 | HTONS(ip->ip_len); | |
6303 | HTONS(ip->ip_off); | |
6304 | } | |
6305 | #endif | |
6306 | /* callee folds in sum */ | |
6307 | sum = m_adj_sum16(m, start, off, sum); | |
6308 | #if BYTE_ORDER != BIG_ENDIAN | |
6309 | if (start < off) { | |
6310 | NTOHS(ip->ip_off); | |
6311 | NTOHS(ip->ip_len); | |
6312 | } | |
6313 | #endif | |
6314 | } | |
6315 | ||
6316 | /* callee folds in sum */ | |
6317 | th->th_sum = in_pseudo(ip->ip_src.s_addr, | |
6318 | ip->ip_dst.s_addr, | |
6319 | sum + htonl(tlen + IPPROTO_TCP)); | |
6320 | } | |
6321 | th->th_sum ^= 0xffff; | |
6322 | } else { | |
6323 | uint16_t ip_sum; | |
6324 | int len; | |
6325 | char b[9]; | |
6326 | ||
6327 | bcopy(ipov->ih_x1, b, sizeof (ipov->ih_x1)); | |
6328 | bzero(ipov->ih_x1, sizeof (ipov->ih_x1)); | |
6329 | ip_sum = ipov->ih_len; | |
6330 | ipov->ih_len = (u_short)tlen; | |
6331 | #if BYTE_ORDER != BIG_ENDIAN | |
6332 | HTONS(ipov->ih_len); | |
6333 | #endif | |
6334 | len = sizeof (struct ip) + tlen; | |
6335 | th->th_sum = in_cksum(m, len); | |
6336 | bcopy(b, ipov->ih_x1, sizeof (ipov->ih_x1)); | |
6337 | ipov->ih_len = ip_sum; | |
6338 | ||
6339 | tcp_in_cksum_stats(len); | |
6340 | } | |
6341 | break; | |
6342 | } | |
6343 | #if INET6 | |
6344 | case AF_INET6: { | |
6345 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
6346 | ||
6347 | if (m->m_pkthdr.pkt_flags & PKTF_SW_LRO_DID_CSUM) | |
6348 | return (0); | |
6349 | ||
6350 | if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) || | |
6351 | (m->m_pkthdr.pkt_flags & PKTF_LOOP)) && | |
6352 | (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) { | |
6353 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { | |
6354 | th->th_sum = m->m_pkthdr.csum_rx_val; | |
6355 | } else { | |
6356 | uint16_t sum = m->m_pkthdr.csum_rx_val; | |
6357 | uint16_t start = m->m_pkthdr.csum_rx_start; | |
6358 | ||
6359 | /* | |
6360 | * Perform 1's complement adjustment of octets | |
6361 | * that got included/excluded in the hardware- | |
6362 | * calculated checksum value. | |
6363 | */ | |
6364 | if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) && | |
6365 | start != off) { | |
6366 | uint16_t s, d; | |
6367 | ||
6368 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { | |
6369 | s = ip6->ip6_src.s6_addr16[1]; | |
6370 | ip6->ip6_src.s6_addr16[1] = 0 ; | |
6371 | } | |
6372 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { | |
6373 | d = ip6->ip6_dst.s6_addr16[1]; | |
6374 | ip6->ip6_dst.s6_addr16[1] = 0; | |
6375 | } | |
6376 | ||
6377 | /* callee folds in sum */ | |
6378 | sum = m_adj_sum16(m, start, off, sum); | |
6379 | ||
6380 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) | |
6381 | ip6->ip6_src.s6_addr16[1] = s; | |
6382 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) | |
6383 | ip6->ip6_dst.s6_addr16[1] = d; | |
6384 | } | |
6385 | ||
6386 | th->th_sum = in6_pseudo( | |
6387 | &ip6->ip6_src, &ip6->ip6_dst, | |
6388 | sum + htonl(tlen + IPPROTO_TCP)); | |
6389 | } | |
6390 | th->th_sum ^= 0xffff; | |
6391 | } else { | |
6392 | tcp_in6_cksum_stats(tlen); | |
6393 | th->th_sum = in6_cksum(m, IPPROTO_TCP, off, tlen); | |
6394 | } | |
6395 | break; | |
6396 | } | |
6397 | #endif /* INET6 */ | |
6398 | default: | |
6399 | VERIFY(0); | |
6400 | /* NOTREACHED */ | |
6401 | } | |
6402 | ||
6403 | if (th->th_sum != 0) { | |
6404 | tcpstat.tcps_rcvbadsum++; | |
6405 | IF_TCP_STATINC(ifp, badformat); | |
6406 | return (-1); | |
6407 | } | |
6408 | ||
6409 | return (0); | |
6410 | } | |
6411 | ||
fe8ab488 | 6412 | SYSCTL_PROC(_net_inet_tcp, TCPCTL_STATS, stats, |
3e170ce0 A |
6413 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, tcp_getstat, |
6414 | "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)"); | |
8ad349bb | 6415 | |
2d21ac55 A |
6416 | static int |
6417 | sysctl_rexmtthresh SYSCTL_HANDLER_ARGS | |
6418 | { | |
6419 | #pragma unused(arg1, arg2) | |
6420 | ||
6421 | int error, val = tcprexmtthresh; | |
6422 | ||
6423 | error = sysctl_handle_int(oidp, &val, 0, req); | |
6424 | if (error || !req->newptr) | |
6425 | return (error); | |
6426 | ||
6427 | /* | |
6428 | * Constrain the number of duplicate ACKs | |
39037602 | 6429 | * to consider for TCP fast retransmit |
2d21ac55 A |
6430 | * to either 2 or 3 |
6431 | */ | |
6432 | ||
6433 | if (val < 2 || val > 3) | |
6434 | return (EINVAL); | |
6435 | ||
6436 | tcprexmtthresh = val; | |
6437 | ||
6438 | return (0); | |
6439 | } | |
91447636 | 6440 | |
3e170ce0 A |
6441 | SYSCTL_PROC(_net_inet_tcp, OID_AUTO, rexmt_thresh, CTLTYPE_INT | CTLFLAG_RW | |
6442 | CTLFLAG_LOCKED, &tcprexmtthresh, 0, &sysctl_rexmtthresh, "I", | |
6443 | "Duplicate ACK Threshold for Fast Retransmit"); |