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