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