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