2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
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 $
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,
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>
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>
85 #include <sys/kauth.h>
86 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
88 #include <machine/endian.h>
91 #include <net/if_types.h>
92 #include <net/route.h>
93 #include <net/ntstat.h>
94 #include <net/content_filter.h>
96 #include <net/multi_layer_pkt_log.h>
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>
108 #include <netinet/ip6.h>
109 #include <netinet/icmp6.h>
110 #include <netinet6/nd6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/in6_pcb.h>
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_cache.h>
116 #include <netinet/tcp_fsm.h>
117 #include <netinet/tcp_seq.h>
118 #include <netinet/tcp_timer.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet/tcp_cc.h>
121 #include <dev/random/randomdev.h>
122 #include <kern/zalloc.h>
124 #include <netinet6/tcp6_var.h>
126 #include <netinet/tcpip.h>
128 #include <netinet/tcp_debug.h>
129 u_char tcp_saveipgen
[40]; /* the size must be of max ip header, now IPv6 */
130 struct tcphdr tcp_savetcp
;
131 #endif /* TCPDEBUG */
132 #include <netinet/tcp_log.h>
135 #include <netinet6/ipsec.h>
137 #include <netinet6/ipsec6.h>
139 #include <netkey/key.h>
142 #if CONFIG_MACF_NET || CONFIG_MACF_SOCKET
143 #include <security/mac_framework.h>
144 #endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */
146 #include <sys/kdebug.h>
147 #include <netinet/lro_ext.h>
149 #include <netinet/mptcp_var.h>
150 #include <netinet/mptcp.h>
151 #include <netinet/mptcp_opt.h>
154 #include <corecrypto/ccaes.h>
156 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
157 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
158 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
159 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
161 #define TCP_RTT_HISTORY_EXPIRE_TIME (60 * TCP_RETRANSHZ)
162 #define TCP_RECV_THROTTLE_WIN (5 * TCP_RETRANSHZ)
163 #define TCP_STRETCHACK_ENABLE_PKTCNT 2000
165 struct tcpstat tcpstat
;
167 static int log_in_vain
= 0;
168 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
,
169 CTLFLAG_RW
| CTLFLAG_LOCKED
, &log_in_vain
, 0,
170 "Log all incoming TCP connections");
172 static int blackhole
= 0;
173 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
,
174 CTLFLAG_RW
| CTLFLAG_LOCKED
, &blackhole
, 0,
175 "Do not send RST when dropping refused connections");
177 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, delayed_ack
,
178 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_delack_enabled
, 3,
179 "Delay ACK to try and piggyback it onto a data packet");
181 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, tcp_lq_overflow
,
182 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_lq_overflow
, 1,
183 "Listen Queue Overflow");
185 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, recvbg
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
186 int, tcp_recv_bg
, 0, "Receive background");
189 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, drop_synfin
,
190 CTLFLAG_RW
| CTLFLAG_LOCKED
, static int, drop_synfin
, 1,
191 "Drop TCP packets with SYN+FIN set");
194 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0,
195 "TCP Segment Reassembly Queue");
197 static int tcp_reass_overflows
= 0;
198 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
,
199 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_reass_overflows
, 0,
200 "Global number of TCP Segment Reassembly Queue Overflows");
203 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, slowlink_wsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
204 __private_extern__
int, slowlink_wsize
, 8192,
205 "Maximum advertised window size for slowlink");
207 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, maxseg_unacked
,
208 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, maxseg_unacked
, 8,
209 "Maximum number of outstanding segments left unacked");
211 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, rfc3465
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
212 int, tcp_do_rfc3465
, 1, "");
214 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, rfc3465_lim2
,
215 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_do_rfc3465_lim2
, 1,
216 "Appropriate bytes counting w/ L=2*SMSS");
218 int rtt_samples_per_slot
= 20;
220 int tcp_acc_iaj_high_thresh
= ACC_IAJ_HIGH_THRESH
;
221 u_int32_t tcp_autorcvbuf_inc_shift
= 3;
222 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, recv_allowed_iaj
,
223 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_allowed_iaj
, ALLOWED_IAJ
,
224 "Allowed inter-packet arrival jiter");
225 #if (DEVELOPMENT || DEBUG)
226 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_high_thresh
,
227 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_acc_iaj_high_thresh
, 0,
228 "Used in calculating maximum accumulated IAJ");
230 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufincshift
,
231 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_inc_shift
, 0,
232 "Shift for increment in receive socket buffer size");
233 #endif /* (DEVELOPMENT || DEBUG) */
235 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, doautorcvbuf
,
236 CTLFLAG_RW
| CTLFLAG_LOCKED
, u_int32_t
, tcp_do_autorcvbuf
, 1,
237 "Enable automatic socket buffer tuning");
239 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, autotunereorder
,
240 CTLFLAG_RW
| CTLFLAG_LOCKED
, u_int32_t
, tcp_autotune_reorder
, 1,
241 "Enable automatic socket buffer tuning even when reordering is present");
243 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, autorcvbufmax
,
244 CTLFLAG_RW
| CTLFLAG_LOCKED
, u_int32_t
, tcp_autorcvbuf_max
, 512 * 1024,
245 "Maximum receive socket buffer size");
251 #endif /* !CONFIG_EMBEDDED */
252 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
253 &sw_lro
, 0, "Used to coalesce TCP packets");
256 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lrodbg
,
257 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lrodebug
, 0,
258 "Used to debug SW LRO");
261 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro_startcnt
,
262 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lro_start
, 0,
263 "Segments for starting LRO computed as power of 2");
265 int limited_txmt
= 1;
268 int tcp_dsack_enable
= 1;
270 #if (DEVELOPMENT || DEBUG)
271 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, limited_transmit
,
272 CTLFLAG_RW
| CTLFLAG_LOCKED
, &limited_txmt
, 0,
273 "Enable limited transmit");
275 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, early_rexmt
,
276 CTLFLAG_RW
| CTLFLAG_LOCKED
, &early_rexmt
, 0,
277 "Enable Early Retransmit");
279 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_ackadv
,
280 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sack_ackadv
, 0,
281 "Use SACK with cumulative ack advancement as a dupack");
283 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, dsack_enable
,
284 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_dsack_enable
, 0,
285 "use DSACK TCP option to report duplicate segments");
287 #endif /* (DEVELOPMENT || DEBUG) */
288 int tcp_disable_access_to_stats
= 1;
289 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, disable_access_to_stats
,
290 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_disable_access_to_stats
, 0,
291 "Disable access to tcpstat");
293 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, challengeack_limit
,
294 CTLFLAG_RW
| CTLFLAG_LOCKED
, uint32_t, tcp_challengeack_limit
, 10,
295 "Maximum number of challenge ACKs per connection per second");
297 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, do_rfc5961
,
298 CTLFLAG_RW
| CTLFLAG_LOCKED
, static int, tcp_do_rfc5961
, 1,
299 "Enable/Disable full RFC 5961 compliance");
301 extern int tcp_acc_iaj_high
;
302 extern int tcp_acc_iaj_react_limit
;
304 int tcprexmtthresh
= 3;
307 struct timeval tcp_uptime
; /* uptime when tcp_now was last updated */
308 lck_spin_t
*tcp_uptime_lock
; /* Used to sychronize updates to tcp_now */
310 struct inpcbhead tcb
;
311 #define tcb6 tcb /* for KAME src sync over BSD*'s */
312 struct inpcbinfo tcbinfo
;
314 static void tcp_dooptions(struct tcpcb
*, u_char
*, int, struct tcphdr
*,
316 static void tcp_finalize_options(struct tcpcb
*, struct tcpopt
*, unsigned int);
317 static void tcp_pulloutofband(struct socket
*,
318 struct tcphdr
*, struct mbuf
*, int);
319 static void tcp_xmit_timer(struct tcpcb
*, int, u_int32_t
, tcp_seq
);
320 static inline unsigned int tcp_maxmtu(struct rtentry
*);
321 static inline int tcp_stretch_ack_enable(struct tcpcb
*tp
, int thflags
);
322 static inline void tcp_adaptive_rwtimo_check(struct tcpcb
*, int);
325 static inline void update_iaj_state(struct tcpcb
*tp
, uint32_t tlen
,
327 void compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
);
328 static void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
);
329 #endif /* TRAFFIC_MGT */
332 static inline unsigned int tcp_maxmtu6(struct rtentry
*);
335 unsigned int get_maxmtu(struct rtentry
*);
337 static void tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sb
,
338 struct tcpopt
*to
, u_int32_t tlen
, u_int32_t rcvbuf_max
);
339 void tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sb
);
340 static void tcp_sbsnd_trim(struct sockbuf
*sbsnd
);
341 static inline void tcp_sbrcv_tstmp_check(struct tcpcb
*tp
);
342 static inline void tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sb
,
343 u_int32_t newsize
, u_int32_t idealsize
, u_int32_t rcvbuf_max
);
344 static void tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
);
345 static void tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
,
347 static void tcp_early_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
);
348 static void tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
,
351 * Constants used for resizing receive socket buffer
352 * when timestamps are not supported
354 #define TCPTV_RCVNOTS_QUANTUM 100
355 #define TCP_RCVNOTS_BYTELEVEL 204800
358 * Constants used for limiting early retransmits
361 #define TCP_EARLY_REXMT_WIN (60 * TCP_RETRANSHZ) /* 60 seconds */
362 #define TCP_EARLY_REXMT_LIMIT 10
364 extern void ipfwsyslog( int level
, const char *format
, ...);
365 extern int fw_verbose
;
368 extern void ipfw_stealth_stats_incr_tcp(void);
370 #define log_in_vain_log( a ) { \
371 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
373 } else if ( (log_in_vain == 4 ) && (fw_verbose == 2)) { \
374 ipfw_stealth_stats_incr_tcp(); \
379 #define log_in_vain_log( a ) { log a; }
382 int tcp_rcvunackwin
= TCPTV_UNACKWIN
;
383 int tcp_maxrcvidle
= TCPTV_MAXRCVIDLE
;
384 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, rcvsspktcnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
385 int, tcp_rcvsspktcnt
, TCP_RCV_SS_PKTCOUNT
, "packets to be seen before receiver stretches acks");
387 #define DELAY_ACK(tp, th) \
388 (CC_ALGO(tp)->delay_ack != NULL && CC_ALGO(tp)->delay_ack(tp, th))
390 static int tcp_dropdropablreq(struct socket
*head
);
391 static void tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
);
392 static void update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
);
393 void tcp_set_background_cc(struct socket
*so
);
394 void tcp_set_foreground_cc(struct socket
*so
);
395 static void tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
);
396 static void tcp_bwmeas_check(struct tcpcb
*tp
);
400 reset_acc_iaj(struct tcpcb
*tp
)
407 update_iaj_state(struct tcpcb
*tp
, uint32_t size
, int rst_size
)
412 if (tp
->iaj_size
== 0 || size
>= tp
->iaj_size
) {
414 tp
->iaj_rcv_ts
= tcp_now
;
415 tp
->iaj_small_pkt
= 0;
419 /* For every 32 bit unsigned integer(v), this function will find the
420 * largest integer n such that (n*n <= v). This takes at most 16 iterations
421 * irrespective of the value of v and does not involve multiplications.
424 isqrt(unsigned int val
)
426 unsigned int sqrt_cache
[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
427 unsigned int temp
, g
= 0, b
= 0x8000, bshft
= 15;
429 for (g
= 0; g
<= 10; ++g
) {
430 if (sqrt_cache
[g
] > val
) {
433 } else if (sqrt_cache
[g
] == val
) {
439 temp
= (((g
<< 1) + b
) << (bshft
--));
445 } while (b
> 0 && val
> 0);
451 * With LRO, roughly estimate the inter arrival time between
452 * each sub coalesced packet as an average. Count the delay
453 * cur_iaj to be the delay between the last packet received
454 * and the first packet of the LRO stream. Due to round off errors
455 * cur_iaj may be the same as lro_delay_factor. Averaging has
456 * round off errors too. lro_delay_factor may be close to 0
457 * in steady state leading to lower values fed to compute_iaj_meat.
460 compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
)
462 uint32_t cur_iaj
= tcp_now
- tp
->iaj_rcv_ts
;
463 uint32_t timediff
= 0;
465 if (cur_iaj
>= lro_delay_factor
) {
466 cur_iaj
= cur_iaj
- lro_delay_factor
;
469 compute_iaj_meat(tp
, cur_iaj
);
477 timediff
= lro_delay_factor
/ nlropkts
;
479 while (nlropkts
> 0) {
480 compute_iaj_meat(tp
, timediff
);
487 compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
)
489 /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds,
490 * throttle the receive window to a minimum of MIN_IAJ_WIN packets
492 #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
493 #define IAJ_DIV_SHIFT 4
494 #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1))
496 uint32_t allowed_iaj
, acc_iaj
= 0;
501 cur_iaj_dev
= (cur_iaj
- tp
->avg_iaj
);
503 /* Allow a jitter of "allowed_iaj" milliseconds. Some connections
504 * may have a constant jitter more than that. We detect this by
505 * using standard deviation.
507 allowed_iaj
= tp
->avg_iaj
+ tp
->std_dev_iaj
;
508 if (allowed_iaj
< tcp_allowed_iaj
) {
509 allowed_iaj
= tcp_allowed_iaj
;
512 /* Initially when the connection starts, the senders congestion
513 * window is small. During this period we avoid throttling a
514 * connection because we do not have a good starting point for
515 * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over
516 * the first few packets.
518 if (tp
->iaj_pktcnt
> IAJ_IGNORE_PKTCNT
) {
519 if (cur_iaj
<= allowed_iaj
) {
520 if (tp
->acc_iaj
>= 2) {
521 acc_iaj
= tp
->acc_iaj
- 2;
526 acc_iaj
= tp
->acc_iaj
+ (cur_iaj
- allowed_iaj
);
529 if (acc_iaj
> MAX_ACC_IAJ
) {
530 acc_iaj
= MAX_ACC_IAJ
;
532 tp
->acc_iaj
= acc_iaj
;
535 /* Compute weighted average where the history has a weight of
536 * 15 out of 16 and the current value has a weight of 1 out of 16.
537 * This will make the short-term measurements have more weight.
539 * The addition of 8 will help to round-up the value
540 * instead of round-down
542 tp
->avg_iaj
= (((tp
->avg_iaj
<< IAJ_DIV_SHIFT
) - tp
->avg_iaj
)
543 + cur_iaj
+ IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
545 /* Compute Root-mean-square of deviation where mean is a weighted
546 * average as described above.
548 temp
= tp
->std_dev_iaj
* tp
->std_dev_iaj
;
549 mean
= (((temp
<< IAJ_DIV_SHIFT
) - temp
)
550 + (cur_iaj_dev
* cur_iaj_dev
)
551 + IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
553 tp
->std_dev_iaj
= isqrt(mean
);
555 DTRACE_TCP3(iaj
, struct tcpcb
*, tp
, uint32_t, cur_iaj
,
556 uint32_t, allowed_iaj
);
560 #endif /* TRAFFIC_MGT */
563 * Perform rate limit check per connection per second
564 * tp->t_challengeack_last is the last_time diff was greater than 1sec
565 * tp->t_challengeack_count is the number of ACKs sent (within 1sec)
566 * Return TRUE if we shouldn't send the ACK due to rate limitation
567 * Return FALSE if it is still ok to send challenge ACK
570 tcp_is_ack_ratelimited(struct tcpcb
*tp
)
572 boolean_t ret
= TRUE
;
573 uint32_t now
= tcp_now
;
576 diff
= timer_diff(now
, 0, tp
->t_challengeack_last
, 0);
577 /* If it is first time or diff > 1000ms,
578 * update the challengeack_last and reset the
579 * current count of ACKs
581 if (tp
->t_challengeack_last
== 0 || diff
>= 1000) {
582 tp
->t_challengeack_last
= now
;
583 tp
->t_challengeack_count
= 0;
585 } else if (tp
->t_challengeack_count
< tcp_challengeack_limit
) {
589 /* Careful about wrap-around */
590 if (ret
== FALSE
&& (tp
->t_challengeack_count
+ 1 > 0)) {
591 tp
->t_challengeack_count
++;
597 /* Check if enough amount of data has been acknowledged since
598 * bw measurement was started
601 tcp_bwmeas_check(struct tcpcb
*tp
)
603 int32_t bw_meas_bytes
;
604 uint32_t bw
, bytes
, elapsed_time
;
606 if (SEQ_LEQ(tp
->snd_una
, tp
->t_bwmeas
->bw_start
)) {
610 bw_meas_bytes
= tp
->snd_una
- tp
->t_bwmeas
->bw_start
;
611 if ((tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) &&
612 bw_meas_bytes
>= (int32_t)(tp
->t_bwmeas
->bw_size
)) {
613 bytes
= bw_meas_bytes
;
614 elapsed_time
= tcp_now
- tp
->t_bwmeas
->bw_ts
;
615 if (elapsed_time
> 0) {
616 bw
= bytes
/ elapsed_time
;
618 if (tp
->t_bwmeas
->bw_sndbw
> 0) {
619 tp
->t_bwmeas
->bw_sndbw
=
620 (((tp
->t_bwmeas
->bw_sndbw
<< 3)
621 - tp
->t_bwmeas
->bw_sndbw
)
624 tp
->t_bwmeas
->bw_sndbw
= bw
;
627 /* Store the maximum value */
628 if (tp
->t_bwmeas
->bw_sndbw_max
== 0) {
629 tp
->t_bwmeas
->bw_sndbw_max
=
630 tp
->t_bwmeas
->bw_sndbw
;
632 tp
->t_bwmeas
->bw_sndbw_max
=
633 max(tp
->t_bwmeas
->bw_sndbw
,
634 tp
->t_bwmeas
->bw_sndbw_max
);
638 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
643 tcp_reass(struct tcpcb
*tp
, struct tcphdr
*th
, int *tlenp
, struct mbuf
*m
,
644 struct ifnet
*ifp
, int *dowakeup
)
647 struct tseg_qent
*p
= NULL
;
648 struct tseg_qent
*nq
;
649 struct tseg_qent
*te
= NULL
;
650 struct inpcb
*inp
= tp
->t_inpcb
;
651 struct socket
*so
= inp
->inp_socket
;
653 struct mbuf
*oodata
= NULL
;
656 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
657 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
658 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
659 boolean_t dsack_set
= FALSE
;
662 * Call with th==0 after become established to
663 * force pre-ESTABLISHED data up to user socket.
670 * If the reassembly queue already has entries or if we are going
671 * to add a new one, then the connection has reached a loss state.
672 * Reset the stretch-ack algorithm at this point.
674 tcp_reset_stretch_ack(tp
);
677 if (tp
->acc_iaj
> 0) {
680 #endif /* TRAFFIC_MGT */
683 * Limit the number of segments in the reassembly queue to prevent
684 * holding on to too many segments (and thus running out of mbufs).
685 * Make sure to let the missing segment through which caused this
686 * queue. Always keep one global queue entry spare to be able to
687 * process the missing segment.
689 qlimit
= min(max(100, so
->so_rcv
.sb_hiwat
>> 10),
690 (TCP_AUTORCVBUF_MAX(ifp
) >> 10));
691 if (th
->th_seq
!= tp
->rcv_nxt
&&
692 (tp
->t_reassqlen
+ 1) >= qlimit
) {
693 tcp_reass_overflows
++;
694 tcpstat
.tcps_rcvmemdrop
++;
700 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
701 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
703 tcpstat
.tcps_rcvmemdrop
++;
710 * Find a segment which begins after this one does.
712 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
713 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
)) {
720 * If there is a preceding segment, it may provide some of
721 * our data already. If so, drop the data from the incoming
722 * segment. If it provides all of our data, drop us.
726 /* conversion to int (in i) handles seq wraparound */
727 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
729 if (TCP_DSACK_ENABLED(tp
) && i
> 1) {
731 * Note duplicate data sequnce numbers
732 * to report in DSACK option
734 tp
->t_dsack_lseq
= th
->th_seq
;
735 tp
->t_dsack_rseq
= th
->th_seq
+
739 * Report only the first part of partial/
740 * non-contiguous duplicate sequence space
745 tcpstat
.tcps_rcvduppack
++;
746 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
748 nstat_route_rx(inp
->inp_route
.ro_rt
,
750 NSTAT_RX_FLAG_DUPLICATE
);
751 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
753 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
755 tp
->t_stat
.rxduplicatebytes
+= *tlenp
;
756 inp_set_activity_bitmap(inp
);
759 zfree(tcp_reass_zone
, te
);
763 * Try to present any queued data
764 * at the left window edge to the user.
765 * This is needed after the 3-WHS
776 tcpstat
.tcps_rcvoopack
++;
777 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
779 nstat_route_rx(inp
->inp_route
.ro_rt
, 1, *tlenp
,
780 NSTAT_RX_FLAG_OUT_OF_ORDER
);
781 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
782 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, *tlenp
);
783 tp
->t_stat
.rxoutoforderbytes
+= *tlenp
;
784 inp_set_activity_bitmap(inp
);
788 * While we overlap succeeding segments trim them or,
789 * if they are completely covered, dequeue them.
792 int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
798 * Report only the first part of partial/non-contiguous
799 * duplicate segment in dsack option. The variable
800 * dsack_set will be true if a previous entry has some of
801 * the duplicate sequence space.
803 if (TCP_DSACK_ENABLED(tp
) && i
> 1 && !dsack_set
) {
804 if (tp
->t_dsack_lseq
== 0) {
805 tp
->t_dsack_lseq
= q
->tqe_th
->th_seq
;
807 tp
->t_dsack_lseq
+ min(i
, q
->tqe_len
);
810 * this segment overlaps data in multple
811 * entries in the reassembly queue, move
812 * the right sequence number further.
815 tp
->t_dsack_rseq
+ min(i
, q
->tqe_len
);
818 if (i
< q
->tqe_len
) {
819 q
->tqe_th
->th_seq
+= i
;
825 nq
= LIST_NEXT(q
, tqe_q
);
826 LIST_REMOVE(q
, tqe_q
);
828 zfree(tcp_reass_zone
, q
);
833 /* Insert the new segment queue entry into place. */
836 te
->tqe_len
= *tlenp
;
839 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
841 LIST_INSERT_AFTER(p
, te
, tqe_q
);
845 * New out-of-order data exists, and is pointed to by
846 * queue entry te. Set copy_oodata to 1 so out-of-order data
847 * can be copied off to sockbuf after in-order data
850 if (!(so
->so_state
& SS_CANTRCVMORE
)) {
856 * Present data to user, advancing rcv_nxt through
857 * completed sequence space.
859 if (!TCPS_HAVEESTABLISHED(tp
->t_state
)) {
862 q
= LIST_FIRST(&tp
->t_segq
);
863 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
) {
864 /* Stop using LRO once out of order packets arrive */
865 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
866 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
867 th
->th_dport
, th
->th_sport
);
868 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
872 * continue processing if out-of-order data
875 if (q
&& (so
->so_flags
& SOF_ENABLE_MSGS
)) {
876 goto msg_unordered_delivery
;
883 * If there is already another thread doing reassembly for this
884 * connection, it is better to let it finish the job --
887 if (tp
->t_flagsext
& TF_REASS_INPROG
) {
891 tp
->t_flagsext
|= TF_REASS_INPROG
;
892 /* lost packet was recovered, so ooo data can be returned */
893 tcpstat
.tcps_recovered_pkts
++;
896 tp
->rcv_nxt
+= q
->tqe_len
;
897 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
898 LIST_REMOVE(q
, tqe_q
);
899 if (so
->so_state
& SS_CANTRCVMORE
) {
903 * The mbuf may be freed after it has been added to the
904 * receive socket buffer so we reinitialize th to point
905 * to a safe copy of the TCP header
907 struct tcphdr saved_tcphdr
= {};
909 so_recv_data_stat(so
, q
->tqe_m
, 0); /* XXXX */
910 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
912 * Append the inorder data as a message to the
913 * receive socket buffer. Also check to see if
914 * the data we are about to deliver is the same
915 * data that we wanted to pass up to the user
916 * out of order. If so, reset copy_oodata --
917 * the received data filled a gap, and
924 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
925 if (sbappendstream_rcvdemux(so
, q
->tqe_m
,
926 q
->tqe_th
->th_seq
- (tp
->irs
+ 1), 0)) {
931 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
932 tcp_update_lro_seq(tp
->rcv_nxt
,
933 inp
->inp_laddr
, inp
->inp_faddr
,
934 th
->th_dport
, th
->th_sport
);
937 zfree(tcp_reass_zone
, q
);
939 q
= LIST_FIRST(&tp
->t_segq
);
940 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
941 tp
->t_flagsext
&= ~TF_REASS_INPROG
;
944 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
945 KERNEL_DEBUG(DBG_LAYER_BEG
,
946 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
947 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
948 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
953 KERNEL_DEBUG(DBG_LAYER_BEG
,
954 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
955 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
956 (inp
->inp_faddr
.s_addr
& 0xffff)),
960 msg_unordered_delivery
:
961 /* Deliver out-of-order data as a message */
962 if (te
&& (so
->so_flags
& SOF_ENABLE_MSGS
) && copy_oodata
&& te
->tqe_len
) {
964 * make a copy of the mbuf to be delivered up to
965 * the user, and add it to the sockbuf
967 oodata
= m_copym(te
->tqe_m
, 0, M_COPYALL
, M_DONTWAIT
);
968 if (oodata
!= NULL
) {
969 if (sbappendmsgstream_rcv(&so
->so_rcv
, oodata
,
970 te
->tqe_th
->th_seq
- (tp
->irs
+ 1), 1)) {
972 tcpstat
.tcps_msg_unopkts
++;
974 tcpstat
.tcps_msg_unoappendfail
++;
983 * Reduce congestion window -- used when ECN is seen or when a tail loss
984 * probe recovers the last packet.
987 tcp_reduce_congestion_window(
991 * If the current tcp cc module has
992 * defined a hook for tasks to run
993 * before entering FR, call it
995 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
996 CC_ALGO(tp
)->pre_fr(tp
);
998 ENTER_FASTRECOVERY(tp
);
999 if (tp
->t_flags
& TF_SENTFIN
) {
1000 tp
->snd_recover
= tp
->snd_max
- 1;
1002 tp
->snd_recover
= tp
->snd_max
;
1004 tp
->t_timer
[TCPT_REXMT
] = 0;
1005 tp
->t_timer
[TCPT_PTO
] = 0;
1007 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
1008 tcp_cc_adjust_nonvalidated_cwnd(tp
);
1010 tp
->snd_cwnd
= tp
->snd_ssthresh
+
1011 tp
->t_maxseg
* tcprexmtthresh
;
1016 * This function is called upon reception of data on a socket. It's purpose is
1017 * to handle the adaptive keepalive timers that monitor whether the connection
1018 * is making progress. First the adaptive read-timer, second the TFO probe-timer.
1020 * The application wants to get an event if there is a stall during read.
1021 * Set the initial keepalive timeout to be equal to twice RTO.
1023 * If the outgoing interface is in marginal conditions, we need to
1024 * enable read probes for that too.
1027 tcp_adaptive_rwtimo_check(struct tcpcb
*tp
, int tlen
)
1029 struct ifnet
*outifp
= tp
->t_inpcb
->inp_last_outifp
;
1031 if ((tp
->t_adaptive_rtimo
> 0 ||
1033 (outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
)))
1035 tp
->t_state
== TCPS_ESTABLISHED
) {
1036 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1037 (TCP_REXMTVAL(tp
) << 1));
1038 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
1039 tp
->t_rtimo_probes
= 0;
1044 tcp_keepalive_reset(struct tcpcb
*tp
)
1046 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1047 TCP_CONN_KEEPIDLE(tp
));
1048 tp
->t_flagsext
&= ~(TF_DETECT_READSTALL
);
1049 tp
->t_rtimo_probes
= 0;
1053 * TCP input routine, follows pages 65-76 of the
1054 * protocol specification dated September, 1981 very closely.
1058 tcp6_input(struct mbuf
**mp
, int *offp
, int proto
)
1060 #pragma unused(proto)
1061 struct mbuf
*m
= *mp
;
1063 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1065 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
1067 /* Expect 32-bit aligned data pointer on strict-align platforms */
1068 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1071 * draft-itojun-ipv6-tcp-to-anycast
1072 * better place to put this in?
1074 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) == 0) {
1075 if (ia6_flags
& IN6_IFF_ANYCAST
) {
1076 struct ip6_hdr
*ip6
;
1078 ip6
= mtod(m
, struct ip6_hdr
*);
1079 icmp6_error(m
, ICMP6_DST_UNREACH
,
1080 ICMP6_DST_UNREACH_ADDR
,
1081 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
1083 IF_TCP_STATINC(ifp
, icmp6unreach
);
1085 return IPPROTO_DONE
;
1089 tcp_input(m
, *offp
);
1090 return IPPROTO_DONE
;
1094 /* Depending on the usage of mbuf space in the system, this function
1095 * will return true or false. This is used to determine if a socket
1096 * buffer can take more memory from the system for auto-tuning or not.
1099 tcp_cansbgrow(struct sockbuf
*sb
)
1101 /* Calculate the host level space limit in terms of MSIZE buffers.
1102 * We can use a maximum of half of the available mbuf space for
1105 u_int32_t mblim
= ((nmbclusters
>> 1) << (MCLSHIFT
- MSIZESHIFT
));
1107 /* Calculate per sb limit in terms of bytes. We optimize this limit
1108 * for upto 16 socket buffers.
1111 u_int32_t sbspacelim
= ((nmbclusters
>> 4) << MCLSHIFT
);
1113 if ((total_sbmb_cnt
< mblim
) &&
1114 (sb
->sb_hiwat
< sbspacelim
)) {
1117 OSIncrementAtomic64(&sbmb_limreached
);
1123 tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1124 u_int32_t newsize
, u_int32_t idealsize
, u_int32_t rcvbuf_max
)
1126 /* newsize should not exceed max */
1127 newsize
= min(newsize
, rcvbuf_max
);
1129 /* The receive window scale negotiated at the
1130 * beginning of the connection will also set a
1131 * limit on the socket buffer size
1133 newsize
= min(newsize
, TCP_MAXWIN
<< tp
->rcv_scale
);
1135 /* Set new socket buffer size */
1136 if (newsize
> sbrcv
->sb_hiwat
&&
1137 (sbreserve(sbrcv
, newsize
) == 1)) {
1138 sbrcv
->sb_idealsize
= min(max(sbrcv
->sb_idealsize
,
1139 (idealsize
!= 0) ? idealsize
: newsize
), rcvbuf_max
);
1141 /* Again check the limit set by the advertised
1144 sbrcv
->sb_idealsize
= min(sbrcv
->sb_idealsize
,
1145 TCP_MAXWIN
<< tp
->rcv_scale
);
1150 * This function is used to grow a receive socket buffer. It
1151 * will take into account system-level memory usage and the
1152 * bandwidth available on the link to make a decision.
1155 tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1156 struct tcpopt
*to
, u_int32_t pktlen
, u_int32_t rcvbuf_max
)
1158 struct socket
*so
= sbrcv
->sb_so
;
1161 * Do not grow the receive socket buffer if
1162 * - auto resizing is disabled, globally or on this socket
1163 * - the high water mark already reached the maximum
1164 * - the stream is in background and receive side is being
1167 if (tcp_do_autorcvbuf
== 0 ||
1168 (sbrcv
->sb_flags
& SB_AUTOSIZE
) == 0 ||
1169 tcp_cansbgrow(sbrcv
) == 0 ||
1170 sbrcv
->sb_hiwat
>= rcvbuf_max
||
1171 (tp
->t_flagsext
& TF_RECV_THROTTLE
) ||
1172 (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) ||
1173 (!tcp_autotune_reorder
&& !LIST_EMPTY(&tp
->t_segq
))) {
1174 /* Can not resize the socket buffer, just return */
1178 if (TSTMP_GT(tcp_now
,
1179 tp
->rfbuf_ts
+ TCPTV_RCVBUFIDLE
)) {
1180 /* If there has been an idle period in the
1181 * connection, just restart the measurement
1186 if (!TSTMP_SUPPORTED(tp
)) {
1188 * Timestamp option is not supported on this connection.
1189 * If the connection reached a state to indicate that
1190 * the receive socket buffer needs to grow, increase
1191 * the high water mark.
1193 if (TSTMP_GEQ(tcp_now
,
1194 tp
->rfbuf_ts
+ TCPTV_RCVNOTS_QUANTUM
)) {
1195 if (tp
->rfbuf_cnt
+ pktlen
>= TCP_RCVNOTS_BYTELEVEL
) {
1196 tcp_sbrcv_reserve(tp
, sbrcv
,
1197 tcp_autorcvbuf_max
, 0,
1198 tcp_autorcvbuf_max
);
1202 tp
->rfbuf_cnt
+= pktlen
;
1205 } else if (to
->to_tsecr
!= 0) {
1207 * If the timestamp shows that one RTT has
1208 * completed, we can stop counting the
1209 * bytes. Here we consider increasing
1210 * the socket buffer if the bandwidth measured in
1211 * last rtt, is more than half of sb_hiwat, this will
1212 * help to scale the buffer according to the bandwidth
1215 if (TSTMP_GEQ(to
->to_tsecr
, tp
->rfbuf_ts
)) {
1216 if (tp
->rfbuf_cnt
+ pktlen
> (sbrcv
->sb_hiwat
-
1217 (sbrcv
->sb_hiwat
>> 1))) {
1218 int32_t rcvbuf_inc
, min_incr
;
1220 tp
->rfbuf_cnt
+= pktlen
;
1222 * Increment the receive window by a
1223 * multiple of maximum sized segments.
1224 * This will prevent a connection from
1225 * sending smaller segments on wire if it
1226 * is limited by the receive window.
1228 * Set the ideal size based on current
1229 * bandwidth measurements. We set the
1230 * ideal size on receive socket buffer to
1231 * be twice the bandwidth delay product.
1233 rcvbuf_inc
= (tp
->rfbuf_cnt
<< 1)
1237 * Make the increment equal to 8 segments
1240 min_incr
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1241 if (rcvbuf_inc
< min_incr
) {
1242 rcvbuf_inc
= min_incr
;
1246 (rcvbuf_inc
/ tp
->t_maxseg
) * tp
->t_maxseg
;
1247 tcp_sbrcv_reserve(tp
, sbrcv
,
1248 sbrcv
->sb_hiwat
+ rcvbuf_inc
,
1249 (tp
->rfbuf_cnt
<< 1), rcvbuf_max
);
1251 /* Measure instantaneous receive bandwidth */
1252 if (tp
->t_bwmeas
!= NULL
&& tp
->rfbuf_cnt
> 0 &&
1253 TSTMP_GT(tcp_now
, tp
->rfbuf_ts
)) {
1255 rcv_bw
= tp
->rfbuf_cnt
/
1256 (int)(tcp_now
- tp
->rfbuf_ts
);
1257 if (tp
->t_bwmeas
->bw_rcvbw_max
== 0) {
1258 tp
->t_bwmeas
->bw_rcvbw_max
= rcv_bw
;
1260 tp
->t_bwmeas
->bw_rcvbw_max
= max(
1261 tp
->t_bwmeas
->bw_rcvbw_max
, rcv_bw
);
1266 tp
->rfbuf_cnt
+= pktlen
;
1271 /* Restart the measurement */
1277 /* This function will trim the excess space added to the socket buffer
1278 * to help a slow-reading app. The ideal-size of a socket buffer depends
1279 * on the link bandwidth or it is set by an application and we aim to
1283 tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sbrcv
)
1285 if (tcp_do_autorcvbuf
== 1 && sbrcv
->sb_idealsize
> 0 &&
1286 sbrcv
->sb_hiwat
> sbrcv
->sb_idealsize
) {
1288 /* compute the difference between ideal and current sizes */
1289 u_int32_t diff
= sbrcv
->sb_hiwat
- sbrcv
->sb_idealsize
;
1291 /* Compute the maximum advertised window for
1294 u_int32_t advwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
1296 /* How much can we trim the receive socket buffer?
1297 * 1. it can not be trimmed beyond the max rcv win advertised
1298 * 2. if possible, leave 1/16 of bandwidth*delay to
1299 * avoid closing the win completely
1301 u_int32_t leave
= max(advwin
, (sbrcv
->sb_idealsize
>> 4));
1303 /* Sometimes leave can be zero, in that case leave at least
1304 * a few segments worth of space.
1307 leave
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1310 trim
= sbrcv
->sb_hiwat
- (sbrcv
->sb_cc
+ leave
);
1311 trim
= imin(trim
, (int32_t)diff
);
1314 sbreserve(sbrcv
, (sbrcv
->sb_hiwat
- trim
));
1319 /* We may need to trim the send socket buffer size for two reasons:
1320 * 1. if the rtt seen on the connection is climbing up, we do not
1321 * want to fill the buffers any more.
1322 * 2. if the congestion win on the socket backed off, there is no need
1323 * to hold more mbufs for that connection than what the cwnd will allow.
1326 tcp_sbsnd_trim(struct sockbuf
*sbsnd
)
1328 if (tcp_do_autosendbuf
== 1 &&
1329 ((sbsnd
->sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) ==
1330 (SB_AUTOSIZE
| SB_TRIM
)) &&
1331 (sbsnd
->sb_idealsize
> 0) &&
1332 (sbsnd
->sb_hiwat
> sbsnd
->sb_idealsize
)) {
1334 if (sbsnd
->sb_cc
<= sbsnd
->sb_idealsize
) {
1335 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_idealsize
;
1337 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_cc
;
1339 sbreserve(sbsnd
, (sbsnd
->sb_hiwat
- trim
));
1341 if (sbsnd
->sb_hiwat
<= sbsnd
->sb_idealsize
) {
1342 sbsnd
->sb_flags
&= ~(SB_TRIM
);
1347 * If timestamp option was not negotiated on this connection
1348 * and this connection is on the receiving side of a stream
1349 * then we can not measure the delay on the link accurately.
1350 * Instead of enabling automatic receive socket buffer
1351 * resizing, just give more space to the receive socket buffer.
1354 tcp_sbrcv_tstmp_check(struct tcpcb
*tp
)
1356 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1357 u_int32_t newsize
= 2 * tcp_recvspace
;
1358 struct sockbuf
*sbrcv
= &so
->so_rcv
;
1360 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_RCVD_TSTMP
)) !=
1361 (TF_REQ_TSTMP
| TF_RCVD_TSTMP
) &&
1362 (sbrcv
->sb_flags
& SB_AUTOSIZE
) != 0) {
1363 tcp_sbrcv_reserve(tp
, sbrcv
, newsize
, 0, newsize
);
1367 /* A receiver will evaluate the flow of packets on a connection
1368 * to see if it can reduce ack traffic. The receiver will start
1369 * stretching acks if all of the following conditions are met:
1370 * 1. tcp_delack_enabled is set to 3
1371 * 2. If the bytes received in the last 100ms is greater than a threshold
1372 * defined by maxseg_unacked
1373 * 3. If the connection has not been idle for tcp_maxrcvidle period.
1374 * 4. If the connection has seen enough packets to let the slow-start
1375 * finish after connection establishment or after some packet loss.
1377 * The receiver will stop stretching acks if there is congestion/reordering
1378 * as indicated by packets on reassembly queue or an ECN. If the delayed-ack
1379 * timer fires while stretching acks, it means that the packet flow has gone
1380 * below the threshold defined by maxseg_unacked and the receiver will stop
1381 * stretching acks. The receiver gets no indication when slow-start is completed
1382 * or when the connection reaches an idle state. That is why we use
1383 * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle
1387 tcp_stretch_ack_enable(struct tcpcb
*tp
, int thflags
)
1389 if (tp
->rcv_by_unackwin
>= (maxseg_unacked
* tp
->t_maxseg
) &&
1390 TSTMP_GEQ(tp
->rcv_unackwin
, tcp_now
)) {
1391 tp
->t_flags
|= TF_STREAMING_ON
;
1393 tp
->t_flags
&= ~TF_STREAMING_ON
;
1396 /* If there has been an idle time, reset streaming detection */
1397 if (TSTMP_GT(tcp_now
, tp
->rcv_unackwin
+ tcp_maxrcvidle
)) {
1398 tp
->t_flags
&= ~TF_STREAMING_ON
;
1402 * If there are flags other than TH_ACK set, reset streaming
1405 if (thflags
& ~TH_ACK
) {
1406 tp
->t_flags
&= ~TF_STREAMING_ON
;
1409 if (tp
->t_flagsext
& TF_DISABLE_STRETCHACK
) {
1410 if (tp
->rcv_nostrack_pkts
>= TCP_STRETCHACK_ENABLE_PKTCNT
) {
1411 tp
->t_flagsext
&= ~TF_DISABLE_STRETCHACK
;
1412 tp
->rcv_nostrack_pkts
= 0;
1413 tp
->rcv_nostrack_ts
= 0;
1415 tp
->rcv_nostrack_pkts
++;
1419 if (!(tp
->t_flagsext
& (TF_NOSTRETCHACK
| TF_DISABLE_STRETCHACK
)) &&
1420 (tp
->t_flags
& TF_STREAMING_ON
) &&
1421 (!(tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) ||
1422 (tp
->rcv_waitforss
>= tcp_rcvsspktcnt
))) {
1430 * Reset the state related to stretch-ack algorithm. This will make
1431 * the receiver generate an ack every other packet. The receiver
1432 * will start re-evaluating the rate at which packets come to decide
1433 * if it can benefit by lowering the ack traffic.
1436 tcp_reset_stretch_ack(struct tcpcb
*tp
)
1438 tp
->t_flags
&= ~(TF_STRETCHACK
| TF_STREAMING_ON
);
1439 tp
->rcv_by_unackwin
= 0;
1440 tp
->rcv_by_unackhalfwin
= 0;
1441 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
1444 * When there is packet loss or packet re-ordering or CWR due to
1445 * ECN, the sender's congestion window is reduced. In these states,
1446 * generate an ack for every other packet for some time to allow
1447 * the sender's congestion window to grow.
1449 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
1450 tp
->rcv_waitforss
= 0;
1454 * The last packet was a retransmission, check if this ack
1455 * indicates that the retransmission was spurious.
1457 * If the connection supports timestamps, we could use it to
1458 * detect if the last retransmit was not needed. Otherwise,
1459 * we check if the ACK arrived within RTT/2 window, then it
1460 * was a mistake to do the retransmit in the first place.
1462 * This function will return 1 if it is a spurious retransmit,
1466 tcp_detect_bad_rexmt(struct tcpcb
*tp
, struct tcphdr
*th
,
1467 struct tcpopt
*to
, u_int32_t rxtime
)
1469 int32_t tdiff
, bad_rexmt_win
;
1470 bad_rexmt_win
= (tp
->t_srtt
>> (TCP_RTT_SHIFT
+ 1));
1472 /* If the ack has ECN CE bit, then cwnd has to be adjusted */
1473 if (TCP_ECN_ENABLED(tp
) && (th
->th_flags
& TH_ECE
)) {
1476 if (TSTMP_SUPPORTED(tp
)) {
1477 if (rxtime
> 0 && (to
->to_flags
& TOF_TS
)
1478 && to
->to_tsecr
!= 0
1479 && TSTMP_LT(to
->to_tsecr
, rxtime
)) {
1483 if ((tp
->t_rxtshift
== 1
1484 || (tp
->t_flagsext
& TF_SENT_TLPROBE
))
1486 tdiff
= (int32_t)(tcp_now
- rxtime
);
1487 if (tdiff
< bad_rexmt_win
) {
1497 * Restore congestion window state if a spurious timeout
1501 tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
)
1503 if (TSTMP_SUPPORTED(tp
)) {
1504 u_int32_t fsize
, acked
;
1505 fsize
= tp
->snd_max
- th
->th_ack
;
1506 acked
= BYTES_ACKED(th
, tp
);
1509 * Implement bad retransmit recovery as
1510 * described in RFC 4015.
1512 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1514 /* Initialize cwnd to the initial window */
1515 if (CC_ALGO(tp
)->cwnd_init
!= NULL
) {
1516 CC_ALGO(tp
)->cwnd_init(tp
);
1519 tp
->snd_cwnd
= fsize
+ min(acked
, tp
->snd_cwnd
);
1521 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1522 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1523 if (tp
->t_flags
& TF_WASFRECOVERY
) {
1524 ENTER_FASTRECOVERY(tp
);
1527 /* Do not use the loss flight size in this case */
1528 tp
->t_lossflightsize
= 0;
1530 tp
->snd_cwnd
= max(tp
->snd_cwnd
, TCP_CC_CWND_INIT_BYTES
);
1531 tp
->snd_recover
= tp
->snd_recover_prev
;
1532 tp
->snd_nxt
= tp
->snd_max
;
1534 /* Fix send socket buffer to reflect the change in cwnd */
1535 tcp_bad_rexmt_fix_sndbuf(tp
);
1538 * This RTT might reflect the extra delay induced
1539 * by the network. Skip using this sample for RTO
1540 * calculation and mark the connection so we can
1541 * recompute RTT when the next eligible sample is
1544 tp
->t_flagsext
|= TF_RECOMPUTE_RTT
;
1545 tp
->t_badrexmt_time
= tcp_now
;
1550 * If the previous packet was sent in retransmission timer, and it was
1551 * not needed, then restore the congestion window to the state before that
1554 * If the last packet was sent in tail loss probe timeout, check if that
1555 * recovered the last packet. If so, that will indicate a real loss and
1556 * the congestion window needs to be lowered.
1559 tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
, struct tcpopt
*to
)
1561 if (tp
->t_rxtshift
> 0 &&
1562 tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_rxtstart
)) {
1563 ++tcpstat
.tcps_sndrexmitbad
;
1564 tcp_bad_rexmt_restore_state(tp
, th
);
1565 tcp_ccdbg_trace(tp
, th
, TCP_CC_BAD_REXMT_RECOVERY
);
1566 } else if ((tp
->t_flagsext
& TF_SENT_TLPROBE
)
1567 && tp
->t_tlphighrxt
> 0
1568 && SEQ_GEQ(th
->th_ack
, tp
->t_tlphighrxt
)
1569 && !tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_tlpstart
)) {
1571 * check DSACK information also to make sure that
1572 * the TLP was indeed needed
1574 if (tcp_rxtseg_dsack_for_tlp(tp
)) {
1576 * received a DSACK to indicate that TLP was
1579 tcp_rxtseg_clean(tp
);
1584 * The tail loss probe recovered the last packet and
1585 * we need to adjust the congestion window to take
1586 * this loss into account.
1588 ++tcpstat
.tcps_tlp_recoverlastpkt
;
1589 if (!IN_FASTRECOVERY(tp
)) {
1590 tcp_reduce_congestion_window(tp
);
1591 EXIT_FASTRECOVERY(tp
);
1593 tcp_ccdbg_trace(tp
, th
, TCP_CC_TLP_RECOVER_LASTPACKET
);
1594 } else if (tcp_rxtseg_detect_bad_rexmt(tp
, th
->th_ack
)) {
1596 * All of the retransmitted segments were duplicated, this
1597 * can be an indication of bad fast retransmit.
1599 tcpstat
.tcps_dsack_badrexmt
++;
1600 tcp_bad_rexmt_restore_state(tp
, th
);
1601 tcp_ccdbg_trace(tp
, th
, TCP_CC_DSACK_BAD_REXMT
);
1602 tcp_rxtseg_clean(tp
);
1605 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1606 tp
->t_tlphighrxt
= 0;
1610 * check if the latest ack was for a segment sent during PMTU
1611 * blackhole detection. If the timestamp on the ack is before
1612 * PMTU blackhole detection, then revert the size of the max
1613 * segment to previous size.
1615 if (tp
->t_rxtshift
> 0 && (tp
->t_flags
& TF_BLACKHOLE
) &&
1616 tp
->t_pmtud_start_ts
> 0 && TSTMP_SUPPORTED(tp
)) {
1617 if ((to
->to_flags
& TOF_TS
) && to
->to_tsecr
!= 0
1618 && TSTMP_LT(to
->to_tsecr
, tp
->t_pmtud_start_ts
)) {
1619 tcp_pmtud_revert_segment_size(tp
);
1622 if (tp
->t_pmtud_start_ts
> 0) {
1623 tp
->t_pmtud_start_ts
= 0;
1628 * Check if early retransmit can be attempted according to RFC 5827.
1630 * If packet reordering is detected on a connection, fast recovery will
1631 * be delayed until it is clear that the packet was lost and not reordered.
1632 * But reordering detection is done only when SACK is enabled.
1634 * On connections that do not support SACK, there is a limit on the number
1635 * of early retransmits that can be done per minute. This limit is needed
1636 * to make sure that too many packets are not retransmitted when there is
1637 * packet reordering.
1640 tcp_early_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
)
1642 u_int32_t obytes
, snd_off
;
1644 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1646 if (early_rexmt
&& (SACK_ENABLED(tp
) ||
1647 tp
->t_early_rexmt_count
< TCP_EARLY_REXMT_LIMIT
) &&
1648 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1649 (tp
->t_dupacks
== 1 ||
1650 (SACK_ENABLED(tp
) &&
1651 !TAILQ_EMPTY(&tp
->snd_holes
)))) {
1653 * If there are only a few outstanding
1654 * segments on the connection, we might need
1655 * to lower the retransmit threshold. This
1656 * will allow us to do Early Retransmit as
1657 * described in RFC 5827.
1659 if (SACK_ENABLED(tp
) &&
1660 !TAILQ_EMPTY(&tp
->snd_holes
)) {
1661 obytes
= (tp
->snd_max
- tp
->snd_fack
) +
1662 tp
->sackhint
.sack_bytes_rexmit
;
1664 obytes
= (tp
->snd_max
- tp
->snd_una
);
1668 * In order to lower retransmit threshold the
1669 * following two conditions must be met.
1670 * 1. the amount of outstanding data is less
1672 * 2. there is no unsent data ready for
1673 * transmission or the advertised window
1674 * will limit sending new segments.
1676 snd_off
= tp
->snd_max
- tp
->snd_una
;
1677 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
) - snd_off
;
1678 if (obytes
< (tp
->t_maxseg
<< 2) &&
1682 osegs
= obytes
/ tp
->t_maxseg
;
1683 if ((osegs
* tp
->t_maxseg
) < obytes
) {
1688 * Since the connection might have already
1689 * received some dupacks, we add them to
1690 * to the outstanding segments count to get
1691 * the correct retransmit threshold.
1693 * By checking for early retransmit after
1694 * receiving some duplicate acks when SACK
1695 * is supported, the connection will
1696 * enter fast recovery even if multiple
1697 * segments are lost in the same window.
1699 osegs
+= tp
->t_dupacks
;
1702 ((osegs
- 1) > 1) ? (osegs
- 1) : 1;
1704 min(tp
->t_rexmtthresh
, tcprexmtthresh
);
1706 max(tp
->t_rexmtthresh
, tp
->t_dupacks
);
1708 if (tp
->t_early_rexmt_count
== 0) {
1709 tp
->t_early_rexmt_win
= tcp_now
;
1712 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1713 tcpstat
.tcps_tlp_recovery
++;
1714 tcp_ccdbg_trace(tp
, th
,
1715 TCP_CC_TLP_RECOVERY
);
1717 tcpstat
.tcps_early_rexmt
++;
1718 tp
->t_early_rexmt_count
++;
1719 tcp_ccdbg_trace(tp
, th
,
1720 TCP_CC_EARLY_RETRANSMIT
);
1727 * If we ever sent a TLP probe, the acknowledgement will trigger
1728 * early retransmit because the value of snd_fack will be close
1729 * to snd_max. This will take care of adjustments to the
1730 * congestion window. So we can reset TF_SENT_PROBE flag.
1732 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1733 tp
->t_tlphighrxt
= 0;
1738 tcp_tfo_syn(struct tcpcb
*tp
, struct tcpopt
*to
)
1740 u_char out
[CCAES_BLOCK_SIZE
];
1743 if (!(to
->to_flags
& (TOF_TFO
| TOF_TFOREQ
)) ||
1744 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
1748 if ((to
->to_flags
& TOF_TFOREQ
)) {
1749 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1751 tp
->t_tfo_stats
|= TFO_S_COOKIEREQ_RECV
;
1752 tcpstat
.tcps_tfo_cookie_req_rcv
++;
1756 /* Ok, then it must be an offered cookie. We need to check that ... */
1757 tcp_tfo_gen_cookie(tp
->t_inpcb
, out
, sizeof(out
));
1759 len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1761 if (memcmp(out
, to
->to_tfo
, len
)) {
1762 /* Cookies are different! Let's return and offer a new cookie */
1763 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1765 tp
->t_tfo_stats
|= TFO_S_COOKIE_INVALID
;
1766 tcpstat
.tcps_tfo_cookie_invalid
++;
1770 if (OSIncrementAtomic(&tcp_tfo_halfcnt
) >= tcp_tfo_backlog
) {
1771 /* Need to decrement again as we just increased it... */
1772 OSDecrementAtomic(&tcp_tfo_halfcnt
);
1776 tp
->t_tfo_flags
|= TFO_F_COOKIE_VALID
;
1778 tp
->t_tfo_stats
|= TFO_S_SYNDATA_RCV
;
1779 tcpstat
.tcps_tfo_syn_data_rcv
++;
1785 tcp_tfo_synack(struct tcpcb
*tp
, struct tcpopt
*to
)
1787 if (to
->to_flags
& TOF_TFO
) {
1788 unsigned char len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1791 * If this happens, things have gone terribly wrong. len should
1792 * have been checked in tcp_dooptions.
1794 VERIFY(len
<= TFO_COOKIE_LEN_MAX
);
1798 tcp_cache_set_cookie(tp
, to
->to_tfo
, len
);
1799 tcp_heuristic_tfo_success(tp
);
1801 tp
->t_tfo_stats
|= TFO_S_COOKIE_RCV
;
1802 tcpstat
.tcps_tfo_cookie_rcv
++;
1803 if (tp
->t_tfo_flags
& TFO_F_COOKIE_SENT
) {
1804 tcpstat
.tcps_tfo_cookie_wrong
++;
1805 tp
->t_tfo_stats
|= TFO_S_COOKIE_WRONG
;
1809 * Thus, no cookie in the response, but we either asked for one
1810 * or sent SYN+DATA. Now, we need to check whether we had to
1811 * rexmit the SYN. If that's the case, it's better to start
1812 * backing of TFO-cookie requests.
1814 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
1815 tp
->t_tfo_flags
& TFO_F_SYN_LOSS
) {
1816 tp
->t_tfo_stats
|= TFO_S_SYN_LOSS
;
1817 tcpstat
.tcps_tfo_syn_loss
++;
1819 tcp_heuristic_tfo_loss(tp
);
1821 if (tp
->t_tfo_flags
& TFO_F_COOKIE_REQ
) {
1822 tp
->t_tfo_stats
|= TFO_S_NO_COOKIE_RCV
;
1823 tcpstat
.tcps_tfo_no_cookie_rcv
++;
1826 tcp_heuristic_tfo_success(tp
);
1832 tcp_tfo_rcv_probe(struct tcpcb
*tp
, int tlen
)
1838 tp
->t_tfo_probe_state
= TFO_PROBE_PROBING
;
1841 * We send the probe out rather quickly (after one RTO). It does not
1842 * really hurt that much, it's only one additional segment on the wire.
1844 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, (TCP_REXMTVAL(tp
)));
1848 tcp_tfo_rcv_data(struct tcpcb
*tp
)
1850 /* Transition from PROBING to NONE as data has been received */
1851 if (tp
->t_tfo_probe_state
>= TFO_PROBE_PROBING
) {
1852 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1857 tcp_tfo_rcv_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
1859 if (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
&&
1860 tp
->t_tfo_probes
> 0) {
1861 if (th
->th_seq
== tp
->rcv_nxt
) {
1862 /* No hole, so stop probing */
1863 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1864 } else if (SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1865 /* There is a hole! Wait a bit for data... */
1866 tp
->t_tfo_probe_state
= TFO_PROBE_WAIT_DATA
;
1867 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1874 * Update snd_wnd information.
1877 tcp_update_window(struct tcpcb
*tp
, int thflags
, struct tcphdr
* th
,
1878 u_int32_t tiwin
, int tlen
)
1880 /* Don't look at the window if there is no ACK flag */
1881 if ((thflags
& TH_ACK
) &&
1882 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
1883 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
1884 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
1885 /* keep track of pure window updates */
1887 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
) {
1888 tcpstat
.tcps_rcvwinupd
++;
1890 tp
->snd_wnd
= tiwin
;
1891 tp
->snd_wl1
= th
->th_seq
;
1892 tp
->snd_wl2
= th
->th_ack
;
1893 if (tp
->snd_wnd
> tp
->max_sndwnd
) {
1894 tp
->max_sndwnd
= tp
->snd_wnd
;
1897 if (tp
->t_inpcb
->inp_socket
->so_flags
& SOF_MP_SUBFLOW
) {
1898 mptcp_update_window_wakeup(tp
);
1906 tcp_handle_wakeup(struct socket
*so
, int read_wakeup
, int write_wakeup
)
1908 if (read_wakeup
!= 0) {
1911 if (write_wakeup
!= 0) {
1917 tcp_input(struct mbuf
*m
, int off0
)
1920 struct ip
*ip
= NULL
;
1922 u_char
*optp
= NULL
;
1926 struct tcpcb
*tp
= 0;
1928 struct socket
*so
= 0;
1929 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
1930 int read_wakeup
= 0;
1931 int write_wakeup
= 0;
1932 struct in_addr laddr
;
1934 struct in6_addr laddr6
;
1937 int iss
= 0, nosock
= 0;
1938 u_int32_t tiwin
, sack_bytes_acked
= 0;
1939 struct tcpopt to
; /* options in this segment */
1944 struct sockaddr_in
*next_hop
= NULL
;
1945 struct m_tag
*fwd_tag
;
1946 #endif /* IPFIREWALL */
1947 u_char ip_ecn
= IPTOS_ECN_NOTECT
;
1948 unsigned int ifscope
;
1949 uint8_t isconnected
, isdisconnected
;
1950 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1951 int pktf_sw_lro_pkt
= (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) ? 1 : 0;
1952 int nlropkts
= (pktf_sw_lro_pkt
== 1) ? m
->m_pkthdr
.lro_npkts
: 1;
1953 int turnoff_lro
= 0, win
;
1955 struct mptcb
*mp_tp
= NULL
;
1957 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
1958 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
1959 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
1960 boolean_t recvd_dsack
= FALSE
;
1961 struct tcp_respond_args tra
;
1963 boolean_t check_cfil
= cfil_filter_present();
1964 bool findpcb_iterated
= false;
1966 * The mbuf may be freed after it has been added to the receive socket
1967 * buffer or the reassembly queue, so we reinitialize th to point to a
1968 * safe copy of the TCP header
1970 struct tcphdr saved_tcphdr
= {};
1972 * Save copy of the IPv4/IPv6 header.
1973 * Note: use array of uint32_t to silence compiler warning when casting
1974 * to a struct ip6_hdr pointer.
1976 #define MAX_IPWORDS ((sizeof(struct ip) + MAX_IPOPTLEN) / sizeof(uint32_t))
1977 uint32_t saved_hdr
[MAX_IPWORDS
];
1979 #define TCP_INC_VAR(stat, npkts) do { \
1983 TCP_INC_VAR(tcpstat
.tcps_rcvtotal
, nlropkts
);
1985 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
1986 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1987 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1988 KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
1992 if (fwd_tag
!= NULL
) {
1993 struct ip_fwd_tag
*ipfwd_tag
=
1994 (struct ip_fwd_tag
*)(fwd_tag
+ 1);
1996 next_hop
= ipfwd_tag
->next_hop
;
1997 m_tag_delete(m
, fwd_tag
);
1999 #endif /* IPFIREWALL */
2002 struct ip6_hdr
*ip6
= NULL
;
2005 int rstreason
; /* For badport_bandlim accounting purposes */
2006 struct proc
*proc0
= current_proc();
2008 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
2011 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
2013 bzero((char *)&to
, sizeof(to
));
2018 * Expect 32-bit aligned data pointer on
2019 * strict-align platforms
2021 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
2023 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
2024 ip6
= mtod(m
, struct ip6_hdr
*);
2025 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
2026 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
2028 if (tcp_input_checksum(AF_INET6
, m
, th
, off0
, tlen
)) {
2029 TCP_LOG_DROP_PKT(ip6
, th
, ifp
, "IPv6 bad tcp checksum");
2033 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
2034 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
2035 th
->th_seq
, th
->th_ack
, th
->th_win
);
2037 * Be proactive about unspecified IPv6 address in source.
2038 * As we use all-zero to indicate unbounded/unconnected pcb,
2039 * unspecified IPv6 address can be used to confuse us.
2041 * Note that packets with unspecified IPv6 destination is
2042 * already dropped in ip6_input.
2044 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
2046 IF_TCP_STATINC(ifp
, unspecv6
);
2047 TCP_LOG_DROP_PKT(ip6
, th
, ifp
, "src IPv6 address unspecified");
2050 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
2051 struct ip6_hdr
*, ip6
, struct tcpcb
*, NULL
,
2052 struct tcphdr
*, th
);
2054 ip_ecn
= (ntohl(ip6
->ip6_flow
) >> 20) & IPTOS_ECN_MASK
;
2059 * Get IP and TCP header together in first mbuf.
2060 * Note: IP leaves IP header in first mbuf.
2062 if (off0
> sizeof(struct ip
)) {
2064 off0
= sizeof(struct ip
);
2066 if (m
->m_len
< sizeof(struct tcpiphdr
)) {
2067 if ((m
= m_pullup(m
, sizeof(struct tcpiphdr
))) == 0) {
2068 tcpstat
.tcps_rcvshort
++;
2073 /* Expect 32-bit aligned data pointer on strict-align platforms */
2074 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
2076 ip
= mtod(m
, struct ip
*);
2077 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
2080 if (tcp_input_checksum(AF_INET
, m
, th
, off0
, tlen
)) {
2081 TCP_LOG_DROP_PKT(ip
, th
, ifp
, "IPv4 bad tcp checksum");
2086 /* Re-initialization for later version check */
2087 ip
->ip_v
= IPVERSION
;
2089 ip_ecn
= (ip
->ip_tos
& IPTOS_ECN_MASK
);
2091 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
2092 struct ip
*, ip
, struct tcpcb
*, NULL
, struct tcphdr
*, th
);
2094 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
2095 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
2096 th
->th_seq
, th
->th_ack
, th
->th_win
);
2099 #define TCP_LOG_HDR (isipv6 ? (void *)ip6 : (void *)ip)
2102 * Check that TCP offset makes sense,
2103 * pull out TCP options and adjust length.
2105 off
= th
->th_off
<< 2;
2106 if (off
< sizeof(struct tcphdr
) || off
> tlen
) {
2107 tcpstat
.tcps_rcvbadoff
++;
2108 IF_TCP_STATINC(ifp
, badformat
);
2109 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "bad tcp offset");
2112 tlen
-= off
; /* tlen is used instead of ti->ti_len */
2113 if (off
> sizeof(struct tcphdr
)) {
2116 IP6_EXTHDR_CHECK(m
, off0
, off
, return );
2117 ip6
= mtod(m
, struct ip6_hdr
*);
2118 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
2122 if (m
->m_len
< sizeof(struct ip
) + off
) {
2123 if ((m
= m_pullup(m
, sizeof(struct ip
) + off
)) == 0) {
2124 tcpstat
.tcps_rcvshort
++;
2127 ip
= mtod(m
, struct ip
*);
2128 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
2131 optlen
= off
- sizeof(struct tcphdr
);
2132 optp
= (u_char
*)(th
+ 1);
2134 * Do quick retrieval of timestamp options ("options
2135 * prediction?"). If timestamp is the only option and it's
2136 * formatted as recommended in RFC 1323 appendix A, we
2137 * quickly get the values now and not bother calling
2138 * tcp_dooptions(), etc.
2140 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
2141 (optlen
> TCPOLEN_TSTAMP_APPA
&&
2142 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
2143 *(u_int32_t
*)(void *)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
2144 (th
->th_flags
& TH_SYN
) == 0) {
2145 to
.to_flags
|= TOF_TS
;
2146 to
.to_tsval
= ntohl(*(u_int32_t
*)(void *)(optp
+ 4));
2147 to
.to_tsecr
= ntohl(*(u_int32_t
*)(void *)(optp
+ 8));
2148 optp
= NULL
; /* we've parsed the options */
2151 thflags
= th
->th_flags
;
2155 * If the drop_synfin option is enabled, drop all packets with
2156 * both the SYN and FIN bits set. This prevents e.g. nmap from
2157 * identifying the TCP/IP stack.
2159 * This is a violation of the TCP specification.
2161 if (drop_synfin
&& (thflags
& (TH_SYN
| TH_FIN
)) == (TH_SYN
| TH_FIN
)) {
2162 IF_TCP_STATINC(ifp
, synfin
);
2163 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "drop SYN FIN");
2169 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
2170 * until after ip6_savecontrol() is called and before other functions
2171 * which don't want those proto headers.
2172 * Because ip6_savecontrol() is going to parse the mbuf to
2173 * search for data to be passed up to user-land, it wants mbuf
2174 * parameters to be unchanged.
2176 drop_hdrlen
= off0
+ off
;
2178 /* Since this is an entry point for input processing of tcp packets, we
2179 * can update the tcp clock here.
2181 calculate_tcp_clock();
2184 * Record the interface where this segment arrived on; this does not
2185 * affect normal data output (for non-detached TCP) as it provides a
2186 * hint about which route and interface to use for sending in the
2187 * absence of a PCB, when scoped routing (and thus source interface
2188 * selection) are enabled.
2190 if ((m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) || m
->m_pkthdr
.rcvif
== NULL
) {
2191 ifscope
= IFSCOPE_NONE
;
2193 ifscope
= m
->m_pkthdr
.rcvif
->if_index
;
2197 * Convert TCP protocol specific fields to host format.
2200 #if BYTE_ORDER != BIG_ENDIAN
2208 * Locate pcb for segment.
2212 isconnected
= FALSE
;
2213 isdisconnected
= FALSE
;
2215 #if IPFIREWALL_FORWARD
2216 if (next_hop
!= NULL
2218 && isipv6
== 0 /* IPv6 support is not yet */
2222 * Diverted. Pretend to be the destination.
2223 * already got one like this?
2225 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2226 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
2229 * No, then it's new. Try find the ambushing socket
2231 if (!next_hop
->sin_port
) {
2232 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
2233 th
->th_sport
, next_hop
->sin_addr
,
2234 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2236 inp
= in_pcblookup_hash(&tcbinfo
,
2237 ip
->ip_src
, th
->th_sport
,
2239 ntohs(next_hop
->sin_port
), 1,
2244 #endif /* IPFIREWALL_FORWARD */
2248 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
2249 &ip6
->ip6_dst
, th
->th_dport
, 1,
2253 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2254 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2258 * Use the interface scope information from the PCB for outbound
2259 * segments. If the PCB isn't present and if scoped routing is
2260 * enabled, tcp_respond will use the scope of the interface where
2261 * the segment arrived on.
2263 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
)) {
2264 ifscope
= inp
->inp_boundifp
->if_index
;
2268 * If the state is CLOSED (i.e., TCB does not exist) then
2269 * all data in the incoming segment is discarded.
2270 * If the TCB exists but is in CLOSED state, it is embryonic,
2271 * but should either do a listen or a connect soon.
2276 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
2278 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
2283 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
2284 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
2288 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
2289 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
2291 switch (log_in_vain
) {
2293 if (thflags
& TH_SYN
) {
2295 "Connection attempt to TCP %s:%d from %s:%d\n",
2296 dbuf
, ntohs(th
->th_dport
),
2298 ntohs(th
->th_sport
));
2303 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
2304 dbuf
, ntohs(th
->th_dport
), sbuf
,
2305 ntohs(th
->th_sport
), thflags
);
2309 if ((thflags
& TH_SYN
) && !(thflags
& TH_ACK
) &&
2310 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
2312 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
2313 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
2315 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
2318 log_in_vain_log((LOG_INFO
,
2319 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
2320 dbuf
, ntohs(th
->th_dport
),
2322 ntohs(th
->th_sport
)));
2330 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) {
2331 switch (blackhole
) {
2333 if (thflags
& TH_SYN
) {
2334 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "blackhole 1 syn for closed port");
2339 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "blackhole 2 closed port");
2342 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "blackhole closed port");
2347 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2348 IF_TCP_STATINC(ifp
, noconnnolist
);
2349 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "closed port");
2350 goto dropwithresetnosock
;
2352 so
= inp
->inp_socket
;
2354 /* This case shouldn't happen as the socket shouldn't be null
2355 * if inp_state isn't set to INPCB_STATE_DEAD
2356 * But just in case, we pretend we didn't find the socket if we hit this case
2357 * as this isn't cause for a panic (the socket might be leaked however)...
2361 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp
);
2363 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "inp_socket NULL");
2368 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2369 socket_unlock(so
, 1);
2370 inp
= NULL
; // pretend we didn't find it
2371 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "inp state WNT_STOPUSING");
2375 if (!isipv6
&& inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
2376 if (inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
||
2377 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
||
2378 inp
->inp_fport
!= th
->th_sport
||
2379 inp
->inp_lport
!= th
->th_dport
) {
2380 os_log_error(OS_LOG_DEFAULT
, "%s 5-tuple does not match: %u:%u %u:%u\n",
2382 ntohs(inp
->inp_fport
), ntohs(th
->th_sport
),
2383 ntohs(inp
->inp_lport
), ntohs(th
->th_dport
));
2384 if (findpcb_iterated
) {
2387 findpcb_iterated
= true;
2388 socket_unlock(so
, 1);
2392 } else if (isipv6
&& !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
2393 if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, &ip6
->ip6_src
) ||
2394 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &ip6
->ip6_dst
) ||
2395 inp
->inp_fport
!= th
->th_sport
||
2396 inp
->inp_lport
!= th
->th_dport
) {
2397 os_log_error(OS_LOG_DEFAULT
, "%s 5-tuple does not match: %u:%u %u:%u\n",
2399 ntohs(inp
->inp_fport
), ntohs(th
->th_sport
),
2400 ntohs(inp
->inp_lport
), ntohs(th
->th_dport
));
2401 if (findpcb_iterated
) {
2404 findpcb_iterated
= true;
2405 socket_unlock(so
, 1);
2411 tp
= intotcpcb(inp
);
2413 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2414 IF_TCP_STATINC(ifp
, noconnlist
);
2415 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "tp is NULL");
2419 TCP_LOG_TH_FLAGS(TCP_LOG_HDR
, th
, tp
, false, ifp
);
2421 if (tp
->t_state
== TCPS_CLOSED
) {
2422 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "tp state TCPS_CLOSED");
2427 if (so
->so_state
& SS_ISCONNECTED
) {
2428 // Connected TCP sockets have a fully-bound local and remote,
2429 // so the policy check doesn't need to override addresses
2430 if (!necp_socket_is_allowed_to_send_recv(inp
, ifp
, NULL
, NULL
, NULL
)) {
2431 TCP_LOG_DROP_NECP(TCP_LOG_HDR
, th
, intotcpcb(inp
), false);
2432 IF_TCP_STATINC(ifp
, badformat
);
2437 * If the proc_uuid_policy table has been updated since the last use
2438 * of the listening socket (i.e., the proc_uuid_policy_table_gencount
2439 * has been updated), the flags in the socket may be out of date.
2440 * If INP2_WANT_APP_POLICY is stale, inbound packets may
2441 * be dropped by NECP if the socket should now match a per-app
2443 * In order to avoid this refresh the proc_uuid_policy state to
2444 * potentially recalculate the socket's flags before checking
2447 (void) inp_update_policy(inp
);
2450 if (!necp_socket_is_allowed_to_send_recv_v6(inp
,
2451 th
->th_dport
, th
->th_sport
, &ip6
->ip6_dst
,
2452 &ip6
->ip6_src
, ifp
, NULL
, NULL
, NULL
)) {
2453 TCP_LOG_DROP_NECP(TCP_LOG_HDR
, th
, intotcpcb(inp
), false);
2454 IF_TCP_STATINC(ifp
, badformat
);
2460 if (!necp_socket_is_allowed_to_send_recv_v4(inp
,
2461 th
->th_dport
, th
->th_sport
, &ip
->ip_dst
, &ip
->ip_src
,
2462 ifp
, NULL
, NULL
, NULL
)) {
2463 TCP_LOG_DROP_NECP(TCP_LOG_HDR
, th
, intotcpcb(inp
), false);
2464 IF_TCP_STATINC(ifp
, badformat
);
2471 prev_t_state
= tp
->t_state
;
2473 /* If none of the FIN|SYN|RST|ACK flag is set, drop */
2474 if (tcp_do_rfc5961
&& (thflags
& TH_ACCEPT
) == 0) {
2475 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 TH_ACCEPT == 0");
2479 /* Unscale the window into a 32-bit value. */
2480 if ((thflags
& TH_SYN
) == 0) {
2481 tiwin
= th
->th_win
<< tp
->snd_scale
;
2488 if (mac_inpcb_check_deliver(inp
, m
, AF_INET
, SOCK_STREAM
)) {
2489 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "mac_inpcb_check_deliver failed");
2494 /* Avoid processing packets while closing a listen socket */
2495 if (tp
->t_state
== TCPS_LISTEN
&&
2496 (so
->so_options
& SO_ACCEPTCONN
) == 0) {
2497 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "closing a listening socket");
2501 if (so
->so_options
& (SO_DEBUG
| SO_ACCEPTCONN
)) {
2503 if (so
->so_options
& SO_DEBUG
) {
2504 ostate
= tp
->t_state
;
2507 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
2511 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
2515 if (so
->so_options
& SO_ACCEPTCONN
) {
2516 struct tcpcb
*tp0
= tp
;
2519 struct sockaddr_storage from
;
2520 struct sockaddr_storage to2
;
2522 struct inpcb
*oinp
= sotoinpcb(so
);
2524 struct ifnet
*head_ifscope
;
2525 unsigned int head_nocell
, head_recvanyif
,
2526 head_noexpensive
, head_awdl_unrestricted
,
2527 head_intcoproc_allowed
, head_external_port
,
2530 /* Get listener's bound-to-interface, if any */
2531 head_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2532 inp
->inp_boundifp
: NULL
;
2533 /* Get listener's no-cellular information, if any */
2534 head_nocell
= INP_NO_CELLULAR(inp
);
2535 /* Get listener's recv-any-interface, if any */
2536 head_recvanyif
= (inp
->inp_flags
& INP_RECV_ANYIF
);
2537 /* Get listener's no-expensive information, if any */
2538 head_noexpensive
= INP_NO_EXPENSIVE(inp
);
2539 head_noconstrained
= INP_NO_CONSTRAINED(inp
);
2540 head_awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
2541 head_intcoproc_allowed
= INP_INTCOPROC_ALLOWED(inp
);
2542 head_external_port
= (inp
->inp_flags2
& INP2_EXTERNAL_PORT
);
2545 * If the state is LISTEN then ignore segment if it contains an RST.
2546 * If the segment contains an ACK then it is bad and send a RST.
2547 * If it does not contain a SYN then it is not interesting; drop it.
2548 * If it is from this socket, drop it, it must be forged.
2550 if ((thflags
& (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
) {
2551 IF_TCP_STATINC(ifp
, listbadsyn
);
2553 if (thflags
& TH_RST
) {
2554 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN with RST");
2557 if (thflags
& TH_ACK
) {
2558 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN with ACK");
2560 tcpstat
.tcps_badsyn
++;
2561 rstreason
= BANDLIM_RST_OPENPORT
;
2565 /* We come here if there is no SYN set */
2566 tcpstat
.tcps_badsyn
++;
2567 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad SYN");
2570 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
2571 if (th
->th_dport
== th
->th_sport
) {
2574 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
2576 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad tuple same port");
2581 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
) {
2582 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad tuple same IPv4 address");
2587 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2588 * in_broadcast() should never return true on a received
2589 * packet with M_BCAST not set.
2591 * Packets with a multicast source address should also
2594 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
2595 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "mbuf M_BCAST | M_MCAST");
2600 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2601 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
2602 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "IN6_IS_ADDR_MULTICAST");
2607 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2608 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2609 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2610 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
2611 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "multicast or broadcast address");
2618 * If deprecated address is forbidden,
2619 * we do not accept SYN to deprecated interface
2620 * address to prevent any new inbound connection from
2621 * getting established.
2622 * When we do not accept SYN, we send a TCP RST,
2623 * with deprecated source address (instead of dropping
2624 * it). We compromise it as it is much better for peer
2625 * to send a RST, and RST will be the final packet
2628 * If we do not forbid deprecated addresses, we accept
2629 * the SYN packet. RFC 4862 forbids dropping SYN in
2632 if (isipv6
&& !ip6_use_deprecated
) {
2635 if (ip6_getdstifaddr_info(m
, NULL
,
2637 if (ia6_flags
& IN6_IFF_DEPRECATED
) {
2639 rstreason
= BANDLIM_RST_OPENPORT
;
2640 IF_TCP_STATINC(ifp
, deprecate6
);
2641 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "deprecated IPv6 address");
2647 if (so
->so_filt
|| check_cfil
) {
2650 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
2652 sin6
->sin6_len
= sizeof(*sin6
);
2653 sin6
->sin6_family
= AF_INET6
;
2654 sin6
->sin6_port
= th
->th_sport
;
2655 sin6
->sin6_flowinfo
= 0;
2656 sin6
->sin6_addr
= ip6
->ip6_src
;
2657 sin6
->sin6_scope_id
= 0;
2659 sin6
= (struct sockaddr_in6
*)&to2
;
2661 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2662 sin6
->sin6_family
= AF_INET6
;
2663 sin6
->sin6_port
= th
->th_dport
;
2664 sin6
->sin6_flowinfo
= 0;
2665 sin6
->sin6_addr
= ip6
->ip6_dst
;
2666 sin6
->sin6_scope_id
= 0;
2670 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
2672 sin
->sin_len
= sizeof(*sin
);
2673 sin
->sin_family
= AF_INET
;
2674 sin
->sin_port
= th
->th_sport
;
2675 sin
->sin_addr
= ip
->ip_src
;
2677 sin
= (struct sockaddr_in
*)&to2
;
2679 sin
->sin_len
= sizeof(struct sockaddr_in
);
2680 sin
->sin_family
= AF_INET
;
2681 sin
->sin_port
= th
->th_dport
;
2682 sin
->sin_addr
= ip
->ip_dst
;
2687 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2689 so2
= sonewconn(so
, 0, NULL
);
2692 tcpstat
.tcps_listendrop
++;
2693 if (tcp_dropdropablreq(so
)) {
2695 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2697 so2
= sonewconn(so
, 0, NULL
);
2701 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " listen drop");
2706 /* Point "inp" and "tp" in tandem to new socket */
2707 inp
= (struct inpcb
*)so2
->so_pcb
;
2708 tp
= intotcpcb(inp
);
2711 socket_unlock(so
, 0); /* Unlock but keep a reference on listener for now */
2716 * Mark socket as temporary until we're
2717 * committed to keeping it. The code at
2718 * ``drop'' and ``dropwithreset'' check the
2719 * flag dropsocket to see if the temporary
2720 * socket created here should be discarded.
2721 * We mark the socket as discardable until
2722 * we're committed to it below in TCPS_LISTEN.
2723 * There are some error conditions in which we
2724 * have to drop the temporary socket.
2728 * Inherit INP_BOUND_IF from listener; testing if
2729 * head_ifscope is non-NULL is sufficient, since it
2730 * can only be set to a non-zero value earlier if
2731 * the listener has such a flag set.
2733 if (head_ifscope
!= NULL
) {
2734 inp
->inp_flags
|= INP_BOUND_IF
;
2735 inp
->inp_boundifp
= head_ifscope
;
2737 inp
->inp_flags
&= ~INP_BOUND_IF
;
2740 * Inherit restrictions from listener.
2743 inp_set_nocellular(inp
);
2745 if (head_noexpensive
) {
2746 inp_set_noexpensive(inp
);
2748 if (head_noconstrained
) {
2749 inp_set_noconstrained(inp
);
2751 if (head_awdl_unrestricted
) {
2752 inp_set_awdl_unrestricted(inp
);
2754 if (head_intcoproc_allowed
) {
2755 inp_set_intcoproc_allowed(inp
);
2758 * Inherit {IN,IN6}_RECV_ANYIF from listener.
2760 if (head_recvanyif
) {
2761 inp
->inp_flags
|= INP_RECV_ANYIF
;
2763 inp
->inp_flags
&= ~INP_RECV_ANYIF
;
2766 if (head_external_port
) {
2767 inp
->inp_flags2
|= INP2_EXTERNAL_PORT
;
2771 inp
->in6p_laddr
= ip6
->ip6_dst
;
2773 inp
->inp_vflag
&= ~INP_IPV6
;
2774 inp
->inp_vflag
|= INP_IPV4
;
2776 inp
->inp_laddr
= ip
->ip_dst
;
2780 inp
->inp_lport
= th
->th_dport
;
2781 if (in_pcbinshash(inp
, 0) != 0) {
2783 * Undo the assignments above if we failed to
2784 * put the PCB on the hash lists.
2788 inp
->in6p_laddr
= in6addr_any
;
2791 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
2793 socket_lock(oso
, 0); /* release ref on parent */
2794 socket_unlock(oso
, 1);
2795 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " in_pcbinshash failed");
2798 socket_lock(oso
, 0);
2802 * Inherit socket options from the listening
2804 * Note that in6p_inputopts are not (even
2805 * should not be) copied, since it stores
2806 * previously received options and is used to
2807 * detect if each new option is different than
2808 * the previous one and hence should be passed
2810 * If we copied in6p_inputopts, a user would
2811 * not be able to receive options just after
2812 * calling the accept system call.
2815 oinp
->inp_flags
& INP_CONTROLOPTS
;
2816 if (oinp
->in6p_outputopts
) {
2817 inp
->in6p_outputopts
=
2818 ip6_copypktopts(oinp
->in6p_outputopts
,
2824 inp
->inp_options
= ip_srcroute();
2825 inp
->inp_ip_tos
= oinp
->inp_ip_tos
;
2828 /* copy old policy into new socket's */
2829 if (sotoinpcb(oso
)->inp_sp
) {
2831 /* Is it a security hole here to silently fail to copy the policy? */
2832 if (inp
->inp_sp
!= NULL
) {
2833 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
2835 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
)) {
2836 printf("tcp_input: could not copy policy\n");
2840 /* inherit states from the listener */
2841 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2842 struct tcpcb
*, tp
, int32_t, TCPS_LISTEN
);
2843 tp
->t_state
= TCPS_LISTEN
;
2844 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
| TF_NOOPT
| TF_NODELAY
);
2845 tp
->t_flagsext
|= (tp0
->t_flagsext
& (TF_RXTFINDROP
| TF_NOTIMEWAIT
| TF_FASTOPEN
));
2846 tp
->t_keepinit
= tp0
->t_keepinit
;
2847 tp
->t_keepcnt
= tp0
->t_keepcnt
;
2848 tp
->t_keepintvl
= tp0
->t_keepintvl
;
2849 tp
->t_adaptive_wtimo
= tp0
->t_adaptive_wtimo
;
2850 tp
->t_adaptive_rtimo
= tp0
->t_adaptive_rtimo
;
2851 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
2852 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0) {
2853 tp
->t_notsent_lowat
= tp0
->t_notsent_lowat
;
2855 tp
->t_inpcb
->inp_flags2
|=
2856 tp0
->t_inpcb
->inp_flags2
& INP2_KEEPALIVE_OFFLOAD
;
2858 /* now drop the reference on the listener */
2859 socket_unlock(oso
, 1);
2861 tcp_set_max_rwinscale(tp
, so
, ifp
);
2865 int error
= cfil_sock_attach(so2
, (struct sockaddr
*)&to2
, (struct sockaddr
*)&from
,
2866 CFS_CONNECTION_DIR_IN
);
2868 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " cfil_sock_attach failed");
2872 #endif /* CONTENT_FILTER */
2874 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
2877 socket_lock_assert_owned(so
);
2879 if (net_mpklog_enabled
&& (m
->m_pkthdr
.rcvif
->if_xflags
& IFXF_MPK_LOG
)) {
2880 MPKL_TCP_INPUT(tcp_mpkl_log_object
,
2881 ntohs(tp
->t_inpcb
->inp_lport
), ntohs(tp
->t_inpcb
->inp_fport
),
2882 th
->th_seq
, th
->th_ack
, tlen
, thflags
,
2883 so
->last_pid
, so
->so_log_seqn
++);
2886 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
2888 * Evaluate the rate of arrival of packets to see if the
2889 * receiver can reduce the ack traffic. The algorithm to
2890 * stretch acks will be enabled if the connection meets
2891 * certain criteria defined in tcp_stretch_ack_enable function.
2893 if ((tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) != 0) {
2894 TCP_INC_VAR(tp
->rcv_waitforss
, nlropkts
);
2896 if (tcp_stretch_ack_enable(tp
, thflags
)) {
2897 tp
->t_flags
|= TF_STRETCHACK
;
2898 tp
->t_flagsext
&= ~(TF_RCVUNACK_WAITSS
);
2899 tp
->rcv_waitforss
= 0;
2901 tp
->t_flags
&= ~(TF_STRETCHACK
);
2903 if (TSTMP_GT(tp
->rcv_unackwin
- (tcp_rcvunackwin
>> 1), tcp_now
)) {
2904 tp
->rcv_by_unackhalfwin
+= (tlen
+ off
);
2905 tp
->rcv_by_unackwin
+= (tlen
+ off
);
2907 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
2908 tp
->rcv_by_unackwin
= tp
->rcv_by_unackhalfwin
+ tlen
+ off
;
2909 tp
->rcv_by_unackhalfwin
= tlen
+ off
;
2914 * Keep track of how many bytes were received in the LRO packet
2916 if ((pktf_sw_lro_pkt
) && (nlropkts
> 2)) {
2917 tp
->t_lropktlen
+= tlen
;
2920 * Explicit Congestion Notification - Flag that we need to send ECT if
2921 * + The IP Congestion experienced flag was set.
2922 * + Socket is in established state
2923 * + We negotiated ECN in the TCP setup
2924 * + This isn't a pure ack (tlen > 0)
2925 * + The data is in the valid window
2927 * TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
2929 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2930 TCP_ECN_ENABLED(tp
) && tlen
> 0 &&
2931 SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2932 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
2933 tp
->t_ecn_recv_ce
++;
2934 tcpstat
.tcps_ecn_recv_ce
++;
2935 INP_INC_IFNET_STAT(inp
, ecn_recv_ce
);
2936 /* Mark this connection as it received CE from network */
2937 tp
->ecn_flags
|= TE_RECV_ECN_CE
;
2938 tp
->ecn_flags
|= TE_SENDECE
;
2942 * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
2943 * bother doing extensive checks for state and whatnot.
2945 if (thflags
& TH_CWR
) {
2946 tp
->ecn_flags
&= ~TE_SENDECE
;
2947 tp
->t_ecn_recv_cwr
++;
2951 * If we received an explicit notification of congestion in
2952 * ip tos ecn bits or by the CWR bit in TCP header flags, reset
2953 * the ack-strteching state. We need to handle ECN notification if
2954 * an ECN setup SYN was sent even once.
2956 if (tp
->t_state
== TCPS_ESTABLISHED
2957 && (tp
->ecn_flags
& TE_SETUPSENT
)
2958 && (ip_ecn
== IPTOS_ECN_CE
|| (thflags
& TH_CWR
))) {
2959 tcp_reset_stretch_ack(tp
);
2960 CLEAR_IAJ_STATE(tp
);
2963 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2964 !TCP_ECN_ENABLED(tp
) && !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2965 tcpstat
.tcps_ecn_fallback_ce
++;
2966 tcp_heuristic_ecn_aggressive(tp
);
2967 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2970 if (tp
->t_state
== TCPS_ESTABLISHED
&& TCP_ECN_ENABLED(tp
) &&
2971 ip_ecn
== IPTOS_ECN_CE
&& !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2972 if (inp
->inp_stat
->rxpackets
< ECN_MIN_CE_PROBES
) {
2973 tp
->t_ecn_recv_ce_pkt
++;
2974 } else if (tp
->t_ecn_recv_ce_pkt
> ECN_MAX_CE_RATIO
) {
2975 tcpstat
.tcps_ecn_fallback_ce
++;
2976 tcp_heuristic_ecn_aggressive(tp
);
2977 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2978 INP_INC_IFNET_STAT(inp
, ecn_fallback_ce
);
2980 /* We tracked the first ECN_MIN_CE_PROBES segments, we
2981 * now know that the path is good.
2983 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2988 * Try to determine if we are receiving a packet after a long time.
2989 * Use our own approximation of idletime to roughly measure remote
2990 * end's idle time. Since slowstart is used after an idle period
2991 * we want to avoid doing LRO if the remote end is not up to date
2992 * on initial window support and starts with 1 or 2 packets as its IW.
2994 if (sw_lro
&& (tp
->t_flagsext
& TF_LRO_OFFLOADED
) &&
2995 ((tcp_now
- tp
->t_rcvtime
) >= (TCP_IDLETIMEOUT(tp
)))) {
2999 /* Update rcvtime as a new segment was received on the connection */
3000 tp
->t_rcvtime
= tcp_now
;
3003 * Segment received on connection.
3004 * Reset idle time and keep-alive timer.
3006 if (TCPS_HAVEESTABLISHED(tp
->t_state
)) {
3007 tcp_keepalive_reset(tp
);
3010 mptcp_reset_keepalive(tp
);
3015 * Process options if not in LISTEN state,
3016 * else do it below (after getting remote address).
3018 if (tp
->t_state
!= TCPS_LISTEN
&& optp
) {
3019 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
3022 if (tp
->t_state
!= TCPS_LISTEN
&& (so
->so_flags
& SOF_MP_SUBFLOW
) &&
3023 mptcp_input_preproc(tp
, m
, th
, drop_hdrlen
) != 0) {
3024 tp
->t_flags
|= TF_ACKNOW
;
3025 (void) tcp_output(tp
);
3026 tcp_check_timer_state(tp
);
3027 socket_unlock(so
, 1);
3028 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
|
3029 DBG_FUNC_END
, 0, 0, 0, 0, 0);
3033 if (tp
->t_state
== TCPS_SYN_SENT
&& (thflags
& TH_SYN
)) {
3034 if (!(thflags
& TH_ACK
) ||
3035 (SEQ_GT(th
->th_ack
, tp
->iss
) &&
3036 SEQ_LEQ(th
->th_ack
, tp
->snd_max
))) {
3037 tcp_finalize_options(tp
, &to
, ifscope
);
3043 * Compute inter-packet arrival jitter. According to RFC 3550,
3044 * inter-packet arrival jitter is defined as the difference in
3045 * packet spacing at the receiver compared to the sender for a
3046 * pair of packets. When two packets of maximum segment size come
3047 * one after the other with consecutive sequence numbers, we
3048 * consider them as packets sent together at the sender and use
3049 * them as a pair to compute inter-packet arrival jitter. This
3050 * metric indicates the delay induced by the network components due
3051 * to queuing in edge/access routers.
3053 if (tp
->t_state
== TCPS_ESTABLISHED
&&
3054 (thflags
& (TH_SYN
| TH_FIN
| TH_RST
| TH_URG
| TH_ACK
| TH_ECE
| TH_PUSH
)) == TH_ACK
&&
3055 ((tp
->t_flags
& (TF_NEEDSYN
| TF_NEEDFIN
)) == 0) &&
3056 ((to
.to_flags
& TOF_TS
) == 0 ||
3057 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
3058 th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
3059 int seg_size
= tlen
;
3060 if (tp
->iaj_pktcnt
<= IAJ_IGNORE_PKTCNT
) {
3061 TCP_INC_VAR(tp
->iaj_pktcnt
, nlropkts
);
3064 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
3065 seg_size
= m
->m_pkthdr
.lro_pktlen
;
3067 if (tp
->iaj_size
== 0 || seg_size
> tp
->iaj_size
||
3068 (seg_size
== tp
->iaj_size
&& tp
->iaj_rcv_ts
== 0)) {
3070 * State related to inter-arrival jitter is
3071 * uninitialized or we are trying to find a good
3072 * first packet to start computing the metric
3074 update_iaj_state(tp
, seg_size
, 0);
3076 if (seg_size
== tp
->iaj_size
) {
3078 * Compute inter-arrival jitter taking
3079 * this packet as the second packet
3081 if (pktf_sw_lro_pkt
) {
3082 compute_iaj(tp
, nlropkts
,
3083 m
->m_pkthdr
.lro_elapsed
);
3085 compute_iaj(tp
, 1, 0);
3088 if (seg_size
< tp
->iaj_size
) {
3090 * There is a smaller packet in the stream.
3091 * Some times the maximum size supported
3092 * on a path can change if there is a new
3093 * link with smaller MTU. The receiver will
3094 * not know about this change. If there
3095 * are too many packets smaller than
3096 * iaj_size, we try to learn the iaj_size
3099 TCP_INC_VAR(tp
->iaj_small_pkt
, nlropkts
);
3100 if (tp
->iaj_small_pkt
> RESET_IAJ_SIZE_THRESH
) {
3101 update_iaj_state(tp
, seg_size
, 1);
3103 CLEAR_IAJ_STATE(tp
);
3106 update_iaj_state(tp
, seg_size
, 0);
3110 CLEAR_IAJ_STATE(tp
);
3112 #endif /* TRAFFIC_MGT */
3115 * Header prediction: check for the two common cases
3116 * of a uni-directional data xfer. If the packet has
3117 * no control flags, is in-sequence, the window didn't
3118 * change and we're not retransmitting, it's a
3119 * candidate. If the length is zero and the ack moved
3120 * forward, we're the sender side of the xfer. Just
3121 * free the data acked & wake any higher level process
3122 * that was blocked waiting for space. If the length
3123 * is non-zero and the ack didn't move, we're the
3124 * receiver side. If we're getting packets in-order
3125 * (the reassembly queue is empty), add the data to
3126 * the socket buffer and note that we need a delayed ack.
3127 * Make sure that the hidden state-flags are also off.
3128 * Since we check for TCPS_ESTABLISHED above, it can only
3131 if (tp
->t_state
== TCPS_ESTABLISHED
&&
3132 (thflags
& (TH_SYN
| TH_FIN
| TH_RST
| TH_URG
| TH_ACK
| TH_ECE
| TH_CWR
)) == TH_ACK
&&
3133 ((tp
->t_flags
& (TF_NEEDSYN
| TF_NEEDFIN
)) == 0) &&
3134 ((to
.to_flags
& TOF_TS
) == 0 ||
3135 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
3136 th
->th_seq
== tp
->rcv_nxt
&&
3137 tiwin
&& tiwin
== tp
->snd_wnd
&&
3138 tp
->snd_nxt
== tp
->snd_max
) {
3140 * If last ACK falls within this segment's sequence numbers,
3141 * record the timestamp.
3142 * NOTE that the test is modified according to the latest
3143 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
3145 if ((to
.to_flags
& TOF_TS
) != 0 &&
3146 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
3147 tp
->ts_recent_age
= tcp_now
;
3148 tp
->ts_recent
= to
.to_tsval
;
3152 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
3153 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
3154 tp
->snd_cwnd
>= tp
->snd_ssthresh
&&
3155 (!IN_FASTRECOVERY(tp
) &&
3156 ((!(SACK_ENABLED(tp
)) &&
3157 tp
->t_dupacks
< tp
->t_rexmtthresh
) ||
3158 (SACK_ENABLED(tp
) && to
.to_nsacks
== 0 &&
3159 TAILQ_EMPTY(&tp
->snd_holes
))))) {
3161 * this is a pure ack for outstanding data.
3163 ++tcpstat
.tcps_predack
;
3165 tcp_bad_rexmt_check(tp
, th
, &to
);
3167 /* Recalculate the RTT */
3168 tcp_compute_rtt(tp
, &to
, th
);
3170 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
3171 acked
= BYTES_ACKED(th
, tp
);
3172 tcpstat
.tcps_rcvackpack
++;
3173 tcpstat
.tcps_rcvackbyte
+= acked
;
3176 * Handle an ack that is in sequence during
3177 * congestion avoidance phase. The
3178 * calculations in this function
3179 * assume that snd_una is not updated yet.
3181 if (CC_ALGO(tp
)->congestion_avd
!= NULL
) {
3182 CC_ALGO(tp
)->congestion_avd(tp
, th
);
3184 tcp_ccdbg_trace(tp
, th
, TCP_CC_INSEQ_ACK_RCVD
);
3185 sbdrop(&so
->so_snd
, acked
);
3186 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
3187 VERIFY(acked
<= so
->so_msg_state
->msg_serial_bytes
);
3188 so
->so_msg_state
->msg_serial_bytes
-= acked
;
3190 tcp_sbsnd_trim(&so
->so_snd
);
3192 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
3193 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
)) {
3194 tp
->snd_recover
= th
->th_ack
- 1;
3196 tp
->snd_una
= th
->th_ack
;
3198 TCP_RESET_REXMT_STATE(tp
);
3201 * pull snd_wl2 up to prevent seq wrap relative
3204 tp
->snd_wl2
= th
->th_ack
;
3206 if (tp
->t_dupacks
> 0) {
3208 tp
->t_rexmtthresh
= tcprexmtthresh
;
3212 * If all outstanding data are acked, stop
3213 * retransmit timer, otherwise restart timer
3214 * using current (possibly backed-off) value.
3215 * If process is waiting for space,
3216 * wakeup/selwakeup/signal. If data
3217 * are ready to send, let tcp_output
3218 * decide between more output or persist.
3220 if (tp
->snd_una
== tp
->snd_max
) {
3221 tp
->t_timer
[TCPT_REXMT
] = 0;
3222 tp
->t_timer
[TCPT_PTO
] = 0;
3223 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
3224 tp
->t_timer
[TCPT_REXMT
] =
3225 OFFSET_FROM_START(tp
,
3228 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
3229 !TCP_DSACK_SEQ_IN_WINDOW(tp
,
3230 tp
->t_dsack_lastuna
, tp
->snd_una
)) {
3231 tcp_rxtseg_clean(tp
);
3234 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
3235 tp
->t_bwmeas
!= NULL
) {
3236 tcp_bwmeas_check(tp
);
3240 if (!SLIST_EMPTY(&tp
->t_notify_ack
)) {
3241 tcp_notify_acknowledgement(tp
, so
);
3244 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
)) {
3245 (void) tcp_output(tp
);
3248 tcp_tfo_rcv_ack(tp
, th
);
3252 tcp_check_timer_state(tp
);
3254 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
3256 socket_unlock(so
, 1);
3257 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
3260 } else if (th
->th_ack
== tp
->snd_una
&&
3261 LIST_EMPTY(&tp
->t_segq
) &&
3262 tlen
<= tcp_sbspace(tp
)) {
3264 * this is a pure, in-sequence data packet
3265 * with nothing on the reassembly queue and
3266 * we have enough buffer space to take it.
3270 * If this is a connection in steady state, start
3271 * coalescing packets belonging to this flow.
3274 tcp_lro_remove_state(tp
->t_inpcb
->inp_laddr
,
3275 tp
->t_inpcb
->inp_faddr
,
3276 tp
->t_inpcb
->inp_lport
,
3277 tp
->t_inpcb
->inp_fport
);
3278 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
3279 tp
->t_idleat
= tp
->rcv_nxt
;
3280 } else if (sw_lro
&& !pktf_sw_lro_pkt
&& !isipv6
&&
3281 (so
->so_flags
& SOF_USELRO
) &&
3282 !IFNET_IS_CELLULAR(m
->m_pkthdr
.rcvif
) &&
3283 (m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) &&
3284 ((th
->th_seq
- tp
->irs
) >
3285 (tp
->t_maxseg
<< lro_start
)) &&
3286 ((tp
->t_idleat
== 0) || ((th
->th_seq
-
3287 tp
->t_idleat
) > (tp
->t_maxseg
<< lro_start
)))) {
3288 tp
->t_flagsext
|= TF_LRO_OFFLOADED
;
3289 tcp_start_coalescing(ip
, th
, tlen
);
3293 /* Clean receiver SACK report if present */
3294 if (SACK_ENABLED(tp
) && tp
->rcv_numsacks
) {
3295 tcp_clean_sackreport(tp
);
3297 ++tcpstat
.tcps_preddat
;
3298 tp
->rcv_nxt
+= tlen
;
3300 * Pull snd_wl1 up to prevent seq wrap relative to
3303 tp
->snd_wl1
= th
->th_seq
;
3305 * Pull rcv_up up to prevent seq wrap relative to
3308 tp
->rcv_up
= tp
->rcv_nxt
;
3309 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
3310 tcpstat
.tcps_rcvbyte
+= tlen
;
3311 if (nstat_collect
) {
3312 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
3313 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3314 rxpackets
, m
->m_pkthdr
.lro_npkts
);
3316 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3319 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
,
3321 inp_set_activity_bitmap(inp
);
3325 * Calculate the RTT on the receiver only if the
3326 * connection is in streaming mode and the last
3327 * packet was not an end-of-write
3329 if (tp
->t_flags
& TF_STREAMING_ON
) {
3330 tcp_compute_rtt(tp
, &to
, th
);
3333 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
3334 TCP_AUTORCVBUF_MAX(ifp
));
3337 * Add data to socket buffer.
3339 so_recv_data_stat(so
, m
, 0);
3340 m_adj(m
, drop_hdrlen
); /* delayed header drop */
3343 * If message delivery (SOF_ENABLE_MSGS) is enabled on
3344 * this socket, deliver the packet received as an
3345 * in-order message with sequence number attached to it.
3348 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
3349 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
3351 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
3352 ip
= (struct ip
*)&saved_hdr
[0];
3354 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
3355 if (sbappendstream_rcvdemux(so
, m
,
3356 th
->th_seq
- (tp
->irs
+ 1), 0)) {
3357 mptcp_handle_input(so
);
3364 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3365 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
3366 th
->th_seq
, th
->th_ack
, th
->th_win
);
3370 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3371 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
3372 th
->th_seq
, th
->th_ack
, th
->th_win
);
3374 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3375 if (DELAY_ACK(tp
, th
)) {
3376 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3377 tp
->t_flags
|= TF_DELACK
;
3378 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3381 tp
->t_flags
|= TF_ACKNOW
;
3385 tcp_adaptive_rwtimo_check(tp
, tlen
);
3388 tcp_tfo_rcv_data(tp
);
3391 tcp_check_timer_state(tp
);
3393 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
3395 socket_unlock(so
, 1);
3396 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
3402 * Calculate amount of space in receive window,
3403 * and then do TCP input processing.
3404 * Receive window is amount of space in rcv queue,
3405 * but not less than advertised window.
3407 socket_lock_assert_owned(so
);
3408 win
= tcp_sbspace(tp
);
3411 } else { /* clip rcv window to 4K for modems */
3412 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
3413 win
= min(win
, slowlink_wsize
);
3416 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
3419 * Ensure that the subflow receive window isn't greater
3420 * than the connection level receive window.
3422 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
3423 (mp_tp
= tptomptp(tp
))) {
3424 socket_lock_assert_owned(mptetoso(mp_tp
->mpt_mpte
));
3426 if (tp
->rcv_wnd
> (int)(mp_tp
->mpt_rcvadv
- (uint32_t)mp_tp
->mpt_rcvnxt
)) {
3427 tp
->rcv_wnd
= mp_tp
->mpt_rcvadv
- (uint32_t)mp_tp
->mpt_rcvnxt
;
3428 tcpstat
.tcps_mp_reducedwin
++;
3433 switch (tp
->t_state
) {
3435 * Initialize tp->rcv_nxt, and tp->irs, select an initial
3436 * tp->iss, and send a segment:
3437 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3438 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3439 * Fill in remote peer address fields if not previously specified.
3440 * Enter SYN_RECEIVED state, and process any other fields of this
3441 * segment in this state.
3444 struct sockaddr_in
*sin
;
3446 struct sockaddr_in6
*sin6
;
3449 socket_lock_assert_owned(so
);
3451 /* Clear the logging flags inherited from the listening socket */
3452 tp
->t_log_flags
= 0;
3453 tp
->t_flagsext
&= ~TF_LOGGED_CONN_SUMMARY
;
3457 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
3458 M_SONAME
, M_NOWAIT
);
3460 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "LISTEN malloc M_SONAME failed");
3463 bzero(sin6
, sizeof(*sin6
));
3464 sin6
->sin6_family
= AF_INET6
;
3465 sin6
->sin6_len
= sizeof(*sin6
);
3466 sin6
->sin6_addr
= ip6
->ip6_src
;
3467 sin6
->sin6_port
= th
->th_sport
;
3468 laddr6
= inp
->in6p_laddr
;
3469 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
3470 inp
->in6p_laddr
= ip6
->ip6_dst
;
3472 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
3474 inp
->in6p_laddr
= laddr6
;
3475 FREE(sin6
, M_SONAME
);
3476 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " LISTEN in6_pcbconnect failed");
3479 FREE(sin6
, M_SONAME
);
3483 socket_lock_assert_owned(so
);
3484 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
3487 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "LISTEN malloc M_SONAME failed");
3490 sin
->sin_family
= AF_INET
;
3491 sin
->sin_len
= sizeof(*sin
);
3492 sin
->sin_addr
= ip
->ip_src
;
3493 sin
->sin_port
= th
->th_sport
;
3494 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
3495 laddr
= inp
->inp_laddr
;
3496 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
3497 inp
->inp_laddr
= ip
->ip_dst
;
3499 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
,
3500 IFSCOPE_NONE
, NULL
)) {
3501 inp
->inp_laddr
= laddr
;
3502 FREE(sin
, M_SONAME
);
3503 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " LISTEN in_pcbconnect failed");
3506 FREE(sin
, M_SONAME
);
3509 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
3510 tcp_finalize_options(tp
, &to
, ifscope
);
3512 if (tfo_enabled(tp
) && tcp_tfo_syn(tp
, &to
)) {
3519 tp
->iss
= tcp_new_isn(tp
);
3521 tp
->irs
= th
->th_seq
;
3522 tcp_sendseqinit(tp
);
3524 tp
->snd_recover
= tp
->snd_una
;
3526 * Initialization of the tcpcb for transaction;
3527 * set SND.WND = SEG.WND,
3528 * initialize CCsend and CCrecv.
3530 tp
->snd_wnd
= tiwin
; /* initial send-window */
3531 tp
->max_sndwnd
= tp
->snd_wnd
;
3532 tp
->t_flags
|= TF_ACKNOW
;
3533 tp
->t_unacksegs
= 0;
3534 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3535 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3536 tp
->t_state
= TCPS_SYN_RECEIVED
;
3537 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3538 TCP_CONN_KEEPINIT(tp
));
3539 tp
->t_connect_time
= tcp_now
;
3540 dropsocket
= 0; /* committed to socket */
3542 if (inp
->inp_flowhash
== 0) {
3543 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
3546 /* update flowinfo - RFC 6437 */
3547 if (inp
->inp_flow
== 0 &&
3548 inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
3549 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
3551 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
3555 /* reset the incomp processing flag */
3556 so
->so_flags
&= ~(SOF_INCOMP_INPROGRESS
);
3557 tcpstat
.tcps_accepts
++;
3558 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
| TH_CWR
)) {
3560 tp
->ecn_flags
|= (TE_SETUPRECEIVED
| TE_SENDIPECT
);
3564 * The address and connection state are finalized
3566 TCP_LOG_CONNECT(tp
, false, 0);
3572 * If the state is SYN_RECEIVED and the seg contains an ACK,
3573 * but not for our SYN/ACK, send a RST.
3575 case TCPS_SYN_RECEIVED
:
3576 if ((thflags
& TH_ACK
) &&
3577 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
3578 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3579 rstreason
= BANDLIM_RST_OPENPORT
;
3580 IF_TCP_STATINC(ifp
, ooopacket
);
3581 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_RECEIVED bad ACK");
3586 * In SYN_RECEIVED state, if we recv some SYNS with
3587 * window scale and others without, window scaling should
3588 * be disabled. Otherwise the window advertised will be
3589 * lower if we assume scaling and the other end does not.
3591 if ((thflags
& TH_SYN
) &&
3592 (tp
->irs
== th
->th_seq
) &&
3593 !(to
.to_flags
& TOF_SCALE
)) {
3594 tp
->t_flags
&= ~TF_RCVD_SCALE
;
3599 * If the state is SYN_SENT:
3600 * if seg contains an ACK, but not for our SYN, drop the input.
3601 * if seg contains a RST, then drop the connection.
3602 * if seg does not contain SYN, then drop it.
3603 * Otherwise this is an acceptable SYN segment
3604 * initialize tp->rcv_nxt and tp->irs
3605 * if seg contains ack then advance tp->snd_una
3606 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
3607 * arrange for segment to be acked (eventually)
3608 * continue processing rest of data/controls, beginning with URG
3611 if ((thflags
& TH_ACK
) &&
3612 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
3613 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3614 rstreason
= BANDLIM_UNLIMITED
;
3615 IF_TCP_STATINC(ifp
, ooopacket
);
3616 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_SENT bad ACK");
3619 if (thflags
& TH_RST
) {
3620 if ((thflags
& TH_ACK
) != 0) {
3621 if (tfo_enabled(tp
) &&
3622 !(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
)) {
3623 tcp_heuristic_tfo_rst(tp
);
3625 if ((tp
->ecn_flags
& (TE_SETUPSENT
| TE_RCVD_SYN_RST
)) == TE_SETUPSENT
) {
3627 * On local connections, send
3628 * non-ECN syn one time before
3629 * dropping the connection
3631 if (tp
->t_flags
& TF_LOCAL
) {
3632 tp
->ecn_flags
|= TE_RCVD_SYN_RST
;
3635 tcp_heuristic_ecn_synrst(tp
);
3639 (SO_FILT_HINT_LOCKED
|
3640 SO_FILT_HINT_CONNRESET
));
3641 tp
= tcp_drop(tp
, ECONNREFUSED
);
3642 postevent(so
, 0, EV_RESET
);
3644 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_SENT got RST");
3647 if ((thflags
& TH_SYN
) == 0) {
3648 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_SENT no SYN");
3651 tp
->snd_wnd
= th
->th_win
; /* initial send window */
3652 tp
->max_sndwnd
= tp
->snd_wnd
;
3654 tp
->irs
= th
->th_seq
;
3656 if (thflags
& TH_ACK
) {
3657 tcpstat
.tcps_connects
++;
3659 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
)) {
3660 /* ECN-setup SYN-ACK */
3661 tp
->ecn_flags
|= TE_SETUPRECEIVED
;
3662 if (TCP_ECN_ENABLED(tp
)) {
3663 tcp_heuristic_ecn_success(tp
);
3664 tcpstat
.tcps_ecn_client_success
++;
3667 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3668 tp
->t_rxtshift
== 0) {
3669 tcp_heuristic_ecn_success(tp
);
3670 tcpstat
.tcps_ecn_not_supported
++;
3672 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3673 tp
->t_rxtshift
> 0) {
3674 tcp_heuristic_ecn_loss(tp
);
3677 /* non-ECN-setup SYN-ACK */
3678 tp
->ecn_flags
&= ~TE_SENDIPECT
;
3681 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
3682 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
3683 mac_socketpeer_label_associate_mbuf(m
, so
);
3684 /* XXXMAC: SOCK_UNLOCK(so); */
3686 /* Do window scaling on this connection? */
3687 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3688 tp
->snd_scale
= tp
->requested_s_scale
;
3689 tp
->rcv_scale
= tp
->request_r_scale
;
3692 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
<< tp
->rcv_scale
);
3693 tp
->snd_una
++; /* SYN is acked */
3694 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
)) {
3695 tp
->snd_nxt
= tp
->snd_una
;
3699 * We have sent more in the SYN than what is being
3700 * acked. (e.g., TFO)
3701 * We should restart the sending from what the receiver
3702 * has acknowledged immediately.
3704 if (SEQ_GT(tp
->snd_nxt
, th
->th_ack
)) {
3706 * rdar://problem/33214601
3707 * There is a middlebox that acks all but one
3708 * byte and still drops the data.
3710 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
3711 (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3712 tp
->snd_max
== th
->th_ack
+ 1 &&
3713 tp
->snd_max
> tp
->snd_una
+ 1) {
3714 tcp_heuristic_tfo_middlebox(tp
);
3716 so
->so_error
= ENODATA
;
3718 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MP_SUB_ERROR
));
3720 tp
->t_tfo_stats
|= TFO_S_ONE_BYTE_PROXY
;
3723 tp
->snd_max
= tp
->snd_nxt
= th
->th_ack
;
3727 * If there's data, delay ACK; if there's also a FIN
3728 * ACKNOW will be turned on later.
3730 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3731 if (DELAY_ACK(tp
, th
) && tlen
!= 0) {
3732 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3733 tp
->t_flags
|= TF_DELACK
;
3734 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3737 tp
->t_flags
|= TF_ACKNOW
;
3740 * Received <SYN,ACK> in SYN_SENT[*] state.
3742 * SYN_SENT --> ESTABLISHED
3743 * SYN_SENT* --> FIN_WAIT_1
3745 tp
->t_starttime
= tcp_now
;
3746 tcp_sbrcv_tstmp_check(tp
);
3747 if (tp
->t_flags
& TF_NEEDFIN
) {
3748 DTRACE_TCP4(state__change
, void, NULL
,
3749 struct inpcb
*, inp
,
3750 struct tcpcb
*, tp
, int32_t,
3752 tp
->t_state
= TCPS_FIN_WAIT_1
;
3753 tp
->t_flags
&= ~TF_NEEDFIN
;
3756 TCP_LOG_CONNECTION_SUMMARY(tp
);
3758 DTRACE_TCP4(state__change
, void, NULL
,
3759 struct inpcb
*, inp
, struct tcpcb
*,
3760 tp
, int32_t, TCPS_ESTABLISHED
);
3761 tp
->t_state
= TCPS_ESTABLISHED
;
3762 tp
->t_timer
[TCPT_KEEP
] =
3763 OFFSET_FROM_START(tp
,
3764 TCP_CONN_KEEPIDLE(tp
));
3765 if (nstat_collect
) {
3766 nstat_route_connect_success(
3767 inp
->inp_route
.ro_rt
);
3770 * The SYN is acknowledged but una is not
3771 * updated yet. So pass the value of
3772 * ack to compute sndbytes correctly
3774 inp_count_sndbytes(inp
, th
->th_ack
);
3778 * Do not send the connect notification for additional
3779 * subflows until ACK for 3-way handshake arrives.
3781 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3782 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3783 isconnected
= FALSE
;
3788 if ((tp
->t_tfo_flags
& (TFO_F_COOKIE_REQ
| TFO_F_COOKIE_SENT
)) ||
3789 (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
)) {
3790 tcp_tfo_synack(tp
, &to
);
3792 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3793 SEQ_LT(tp
->snd_una
, th
->th_ack
)) {
3794 tp
->t_tfo_stats
|= TFO_S_SYN_DATA_ACKED
;
3795 tcpstat
.tcps_tfo_syn_data_acked
++;
3797 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
3798 so
->so_flags1
|= SOF1_TFO_REWIND
;
3801 tcp_tfo_rcv_probe(tp
, tlen
);
3806 * Received initial SYN in SYN-SENT[*] state => simul-
3807 * taneous open. If segment contains CC option and there is
3808 * a cached CC, apply TAO test; if it succeeds, connection is
3809 * half-synchronized. Otherwise, do 3-way handshake:
3810 * SYN-SENT -> SYN-RECEIVED
3811 * SYN-SENT* -> SYN-RECEIVED*
3813 tp
->t_flags
|= TF_ACKNOW
;
3814 tp
->t_timer
[TCPT_REXMT
] = 0;
3815 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3816 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3817 tp
->t_state
= TCPS_SYN_RECEIVED
;
3820 * During simultaneous open, TFO should not be used.
3821 * So, we disable it here, to prevent that data gets
3822 * sent on the SYN/ACK.
3824 tcp_disable_tfo(tp
);
3829 * Advance th->th_seq to correspond to first data byte.
3830 * If data, trim to stay within window,
3831 * dropping FIN if necessary.
3834 if (tlen
> tp
->rcv_wnd
) {
3835 todrop
= tlen
- tp
->rcv_wnd
;
3839 tcpstat
.tcps_rcvpackafterwin
++;
3840 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3842 tp
->snd_wl1
= th
->th_seq
- 1;
3843 tp
->rcv_up
= th
->th_seq
;
3845 * Client side of transaction: already sent SYN and data.
3846 * If the remote host used T/TCP to validate the SYN,
3847 * our data will be ACK'd; if so, enter normal data segment
3848 * processing in the middle of step 5, ack processing.
3849 * Otherwise, goto step 6.
3851 if (thflags
& TH_ACK
) {
3856 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
3857 * do normal processing.
3859 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
3863 case TCPS_TIME_WAIT
:
3864 break; /* continue normal processing */
3866 /* Received a SYN while connection is already established.
3867 * This is a "half open connection and other anomalies" described
3868 * in RFC793 page 34, send an ACK so the remote reset the connection
3869 * or recovers by adjusting its sequence numbering. Sending an ACK is
3870 * in accordance with RFC 5961 Section 4.2
3872 case TCPS_ESTABLISHED
:
3873 if (thflags
& TH_SYN
) {
3874 /* Drop the packet silently if we have reached the limit */
3875 if (tcp_do_rfc5961
&& tcp_is_ack_ratelimited(tp
)) {
3876 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 rate limited");
3879 /* Send challenge ACK */
3880 tcpstat
.tcps_synchallenge
++;
3881 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 challenge ACK");
3889 * States other than LISTEN or SYN_SENT.
3890 * First check the RST flag and sequence number since reset segments
3891 * are exempt from the timestamp and connection count tests. This
3892 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
3893 * below which allowed reset segments in half the sequence space
3894 * to fall though and be processed (which gives forged reset
3895 * segments with a random sequence number a 50 percent chance of
3896 * killing a connection).
3897 * Then check timestamp, if present.
3898 * Then check the connection count, if present.
3899 * Then check that at least some bytes of segment are within
3900 * receive window. If segment begins before rcv_nxt,
3901 * drop leading data (and SYN); if nothing left, just ack.
3904 * If the RST bit is set, check the sequence number to see
3905 * if this is a valid reset segment.
3907 * In all states except SYN-SENT, all reset (RST) segments
3908 * are validated by checking their SEQ-fields. A reset is
3909 * valid if its sequence number is in the window.
3910 * Note: this does not take into account delayed ACKs, so
3911 * we should test against last_ack_sent instead of rcv_nxt.
3912 * The sequence number in the reset segment is normally an
3913 * echo of our outgoing acknowlegement numbers, but some hosts
3914 * send a reset with the sequence number at the rightmost edge
3915 * of our receive window, and we have to handle this case.
3916 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
3917 * that brute force RST attacks are possible. To combat this,
3918 * we use a much stricter check while in the ESTABLISHED state,
3919 * only accepting RSTs where the sequence number is equal to
3920 * last_ack_sent. In all other states (the states in which a
3921 * RST is more likely), the more permissive check is used.
3922 * RFC 5961 Section 3.2: if the RST bit is set, sequence # is
3923 * within the receive window and last_ack_sent == seq,
3924 * then reset the connection. Otherwise if the seq doesn't
3925 * match last_ack_sent, TCP must send challenge ACK. Perform
3926 * rate limitation when sending the challenge ACK.
3927 * If we have multiple segments in flight, the intial reset
3928 * segment sequence numbers will be to the left of last_ack_sent,
3929 * but they will eventually catch up.
3930 * In any case, it never made sense to trim reset segments to
3931 * fit the receive window since RFC 1122 says:
3932 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
3934 * A TCP SHOULD allow a received RST segment to include data.
3937 * It has been suggested that a RST segment could contain
3938 * ASCII text that encoded and explained the cause of the
3939 * RST. No standard has yet been established for such
3942 * If the reset segment passes the sequence number test examine
3944 * SYN_RECEIVED STATE:
3945 * If passive open, return to LISTEN state.
3946 * If active open, inform user that connection was refused.
3947 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
3948 * Inform user that connection was reset, and close tcb.
3949 * CLOSING, LAST_ACK STATES:
3952 * Drop the segment - see Stevens, vol. 2, p. 964 and
3955 * Radar 4803931: Allows for the case where we ACKed the FIN but
3956 * there is already a RST in flight from the peer.
3957 * In that case, accept the RST for non-established
3958 * state if it's one off from last_ack_sent.
3961 if (thflags
& TH_RST
) {
3962 if ((SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3963 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) ||
3964 (tp
->rcv_wnd
== 0 &&
3965 ((tp
->last_ack_sent
== th
->th_seq
) ||
3966 ((tp
->last_ack_sent
- 1) == th
->th_seq
)))) {
3967 if (tcp_do_rfc5961
== 0 || tp
->last_ack_sent
== th
->th_seq
) {
3968 switch (tp
->t_state
) {
3969 case TCPS_SYN_RECEIVED
:
3970 IF_TCP_STATINC(ifp
, rstinsynrcv
);
3971 so
->so_error
= ECONNREFUSED
;
3974 case TCPS_ESTABLISHED
:
3975 if (tcp_do_rfc5961
== 0 && tp
->last_ack_sent
!= th
->th_seq
) {
3976 tcpstat
.tcps_badrst
++;
3977 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 bad RST");
3980 if (TCP_ECN_ENABLED(tp
) &&
3981 tp
->snd_una
== tp
->iss
+ 1 &&
3982 SEQ_GT(tp
->snd_max
, tp
->snd_una
)) {
3984 * If the first data packet on an
3985 * ECN connection, receives a RST
3986 * increment the heuristic
3988 tcp_heuristic_ecn_droprst(tp
);
3990 case TCPS_FIN_WAIT_1
:
3991 case TCPS_CLOSE_WAIT
:
3995 case TCPS_FIN_WAIT_2
:
3996 so
->so_error
= ECONNRESET
;
3998 postevent(so
, 0, EV_RESET
);
4000 (SO_FILT_HINT_LOCKED
|
4001 SO_FILT_HINT_CONNRESET
));
4003 tcpstat
.tcps_drops
++;
4012 case TCPS_TIME_WAIT
:
4015 } else if (tcp_do_rfc5961
) {
4016 tcpstat
.tcps_badrst
++;
4017 /* Drop if we have reached the ACK limit */
4018 if (tcp_is_ack_ratelimited(tp
)) {
4019 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 rate limited");
4022 /* Send challenge ACK */
4023 tcpstat
.tcps_rstchallenge
++;
4024 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 challenge ACK");
4033 * RFC 1323 PAWS: If we have a timestamp reply on this segment
4034 * and it's less than ts_recent, drop it.
4036 if ((to
.to_flags
& TOF_TS
) != 0 && tp
->ts_recent
&&
4037 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
4038 /* Check to see if ts_recent is over 24 days old. */
4039 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
4041 * Invalidate ts_recent. If this segment updates
4042 * ts_recent, the age will be reset later and ts_recent
4043 * will get a valid value. If it does not, setting
4044 * ts_recent to zero will at least satisfy the
4045 * requirement that zero be placed in the timestamp
4046 * echo reply when ts_recent isn't valid. The
4047 * age isn't reset until we get a valid ts_recent
4048 * because we don't want out-of-order segments to be
4049 * dropped when ts_recent is old.
4053 tcpstat
.tcps_rcvduppack
++;
4054 tcpstat
.tcps_rcvdupbyte
+= tlen
;
4056 tcpstat
.tcps_pawsdrop
++;
4059 * PAWS-drop when ECN is being used? That indicates
4060 * that ECT-marked packets take a different path, with
4061 * different congestion-characteristics.
4063 * Only fallback when we did send less than 2GB as PAWS
4064 * really has no reason to kick in earlier.
4066 if (TCP_ECN_ENABLED(tp
) &&
4067 inp
->inp_stat
->rxbytes
< 2147483648) {
4068 INP_INC_IFNET_STAT(inp
, ecn_fallback_reorder
);
4069 tcpstat
.tcps_ecn_fallback_reorder
++;
4070 tcp_heuristic_ecn_aggressive(tp
);
4073 if (nstat_collect
) {
4074 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
,
4075 1, tlen
, NSTAT_RX_FLAG_DUPLICATE
);
4076 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4078 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4080 tp
->t_stat
.rxduplicatebytes
+= tlen
;
4081 inp_set_activity_bitmap(inp
);
4091 * In the SYN-RECEIVED state, validate that the packet belongs to
4092 * this connection before trimming the data to fit the receive
4093 * window. Check the sequence number versus IRS since we know
4094 * the sequence numbers haven't wrapped. This is a partial fix
4095 * for the "LAND" DoS attack.
4097 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
4098 rstreason
= BANDLIM_RST_OPENPORT
;
4099 IF_TCP_STATINC(ifp
, dospacket
);
4100 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_RECEIVED bad SEQ");
4105 * Check if there is old data at the beginning of the window
4106 * i.e. the sequence number is before rcv_nxt
4108 todrop
= tp
->rcv_nxt
- th
->th_seq
;
4110 boolean_t is_syn_set
= FALSE
;
4112 if (thflags
& TH_SYN
) {
4116 if (th
->th_urp
> 1) {
4124 * Following if statement from Stevens, vol. 2, p. 960.
4125 * The amount of duplicate data is greater than or equal
4126 * to the size of the segment - entire segment is duplicate
4129 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
4131 * Any valid FIN must be to the left of the window.
4132 * At this point the FIN must be a duplicate or out
4133 * of sequence; drop it.
4138 * Send an ACK to resynchronize and drop any data.
4139 * But keep on processing for RST or ACK.
4141 * If the SYN bit was originally set, then only send
4142 * an ACK if we are not rate-limiting this connection.
4144 if (tcp_do_rfc5961
&& is_syn_set
) {
4145 if (!tcp_is_ack_ratelimited(tp
)) {
4146 tcpstat
.tcps_synchallenge
++;
4147 tp
->t_flags
|= TF_ACKNOW
;
4150 tp
->t_flags
|= TF_ACKNOW
;
4154 /* This could be a keepalive */
4155 soevent(so
, SO_FILT_HINT_LOCKED
|
4156 SO_FILT_HINT_KEEPALIVE
);
4159 tcpstat
.tcps_rcvduppack
++;
4160 tcpstat
.tcps_rcvdupbyte
+= todrop
;
4162 tcpstat
.tcps_rcvpartduppack
++;
4163 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
4166 if (TCP_DSACK_ENABLED(tp
) && todrop
> 1) {
4168 * Note the duplicate data sequence space so that
4169 * it can be reported in DSACK option.
4171 tp
->t_dsack_lseq
= th
->th_seq
;
4172 tp
->t_dsack_rseq
= th
->th_seq
+ todrop
;
4173 tp
->t_flags
|= TF_ACKNOW
;
4175 if (nstat_collect
) {
4176 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
, 1,
4177 todrop
, NSTAT_RX_FLAG_DUPLICATE
);
4178 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
4179 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, todrop
);
4180 tp
->t_stat
.rxduplicatebytes
+= todrop
;
4181 inp_set_activity_bitmap(inp
);
4183 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
4184 th
->th_seq
+= todrop
;
4186 if (th
->th_urp
> todrop
) {
4187 th
->th_urp
-= todrop
;
4195 * If new data are received on a connection after the user
4196 * processes are gone, then RST the other end.
4197 * Send also a RST when we received a data segment after we've
4198 * sent our FIN when the socket is defunct.
4199 * Note that an MPTCP subflow socket would have SS_NOFDREF set
4200 * by default. So, if it's an MPTCP-subflow we rather check the
4201 * MPTCP-level's socket state for SS_NOFDREF.
4204 boolean_t close_it
= FALSE
;
4206 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && (so
->so_state
& SS_NOFDREF
) &&
4207 tp
->t_state
> TCPS_CLOSE_WAIT
) {
4208 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SS_NOFDREF");
4212 if ((so
->so_flags
& SOF_MP_SUBFLOW
) && (mptetoso(tptomptp(tp
)->mpt_mpte
)->so_state
& SS_NOFDREF
) &&
4213 tp
->t_state
> TCPS_CLOSE_WAIT
) {
4214 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SOF_MP_SUBFLOW SS_NOFDREF");
4218 if ((so
->so_flags
& SOF_DEFUNCT
) && tp
->t_state
> TCPS_FIN_WAIT_1
) {
4219 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SOF_DEFUNCT");
4225 tcpstat
.tcps_rcvafterclose
++;
4226 rstreason
= BANDLIM_UNLIMITED
;
4227 IF_TCP_STATINC(ifp
, cleanup
);
4233 * If segment ends after window, drop trailing data
4234 * (and PUSH and FIN); if nothing left, just ACK.
4236 todrop
= (th
->th_seq
+ tlen
) - (tp
->rcv_nxt
+ tp
->rcv_wnd
);
4238 tcpstat
.tcps_rcvpackafterwin
++;
4239 if (todrop
>= tlen
) {
4240 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
4242 * If a new connection request is received
4243 * while in TIME_WAIT, drop the old connection
4244 * and start over if the sequence numbers
4245 * are above the previous ones.
4247 if (thflags
& TH_SYN
&&
4248 tp
->t_state
== TCPS_TIME_WAIT
&&
4249 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
4250 iss
= tcp_new_isn(tp
);
4252 socket_unlock(so
, 1);
4256 * If window is closed can only take segments at
4257 * window edge, and have to drop data and PUSH from
4258 * incoming segments. Continue processing, but
4259 * remember to ack. Otherwise, drop segment
4262 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
4263 tp
->t_flags
|= TF_ACKNOW
;
4264 tcpstat
.tcps_rcvwinprobe
++;
4269 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
4273 thflags
&= ~(TH_PUSH
| TH_FIN
);
4277 * If last ACK falls within this segment's sequence numbers,
4278 * record its timestamp.
4280 * 1) That the test incorporates suggestions from the latest
4281 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
4282 * 2) That updating only on newer timestamps interferes with
4283 * our earlier PAWS tests, so this check should be solely
4284 * predicated on the sequence space of this segment.
4285 * 3) That we modify the segment boundary check to be
4286 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
4287 * instead of RFC1323's
4288 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
4289 * This modified check allows us to overcome RFC1323's
4290 * limitations as described in Stevens TCP/IP Illustrated
4291 * Vol. 2 p.869. In such cases, we can still calculate the
4292 * RTT correctly when RCV.NXT == Last.ACK.Sent.
4294 if ((to
.to_flags
& TOF_TS
) != 0 &&
4295 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
) &&
4296 SEQ_LEQ(tp
->last_ack_sent
, th
->th_seq
+ tlen
+
4297 ((thflags
& (TH_SYN
| TH_FIN
)) != 0))) {
4298 tp
->ts_recent_age
= tcp_now
;
4299 tp
->ts_recent
= to
.to_tsval
;
4303 * Stevens: If a SYN is in the window, then this is an
4304 * error and we send an RST and drop the connection.
4306 * RFC 5961 Section 4.2
4307 * Send challenge ACK for any SYN in synchronized state
4308 * Perform rate limitation in doing so.
4310 if (thflags
& TH_SYN
) {
4311 if (tcp_do_rfc5961
) {
4312 tcpstat
.tcps_badsyn
++;
4313 /* Drop if we have reached ACK limit */
4314 if (tcp_is_ack_ratelimited(tp
)) {
4315 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 bad SYN rate limited");
4318 /* Send challenge ACK */
4319 tcpstat
.tcps_synchallenge
++;
4320 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 bad SYN challenge ack");
4324 tp
= tcp_drop(tp
, ECONNRESET
);
4325 rstreason
= BANDLIM_UNLIMITED
;
4326 postevent(so
, 0, EV_RESET
);
4327 IF_TCP_STATINC(ifp
, synwindow
);
4328 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad SYN");
4334 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
4335 * flag is on (half-synchronized state), then queue data for
4336 * later processing; else drop segment and return.
4338 if ((thflags
& TH_ACK
) == 0) {
4339 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
4340 (tp
->t_flags
& TF_NEEDSYN
)) {
4341 if ((tfo_enabled(tp
))) {
4343 * So, we received a valid segment while in
4344 * SYN-RECEIVED (TF_NEEDSYN is actually never
4345 * set, so this is dead code).
4346 * As this cannot be an RST (see that if a bit
4347 * higher), and it does not have the ACK-flag
4348 * set, we want to retransmit the SYN/ACK.
4349 * Thus, we have to reset snd_nxt to snd_una to
4350 * trigger the going back to sending of the
4351 * SYN/ACK. This is more consistent with the
4352 * behavior of tcp_output(), which expects
4353 * to send the segment that is pointed to by
4356 tp
->snd_nxt
= tp
->snd_una
;
4359 * We need to make absolutely sure that we are
4360 * going to reply upon a duplicate SYN-segment.
4362 if (th
->th_flags
& TH_SYN
) {
4368 } else if (tp
->t_flags
& TF_ACKNOW
) {
4369 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad ACK");
4372 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad ACK");
4381 switch (tp
->t_state
) {
4383 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
4384 * ESTABLISHED state and continue processing.
4385 * The ACK was checked above.
4387 case TCPS_SYN_RECEIVED
:
4389 tcpstat
.tcps_connects
++;
4391 /* Do window scaling? */
4392 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
4393 tp
->snd_scale
= tp
->requested_s_scale
;
4394 tp
->rcv_scale
= tp
->request_r_scale
;
4395 tp
->snd_wnd
= th
->th_win
<< tp
->snd_scale
;
4396 tp
->max_sndwnd
= tp
->snd_wnd
;
4397 tiwin
= tp
->snd_wnd
;
4401 * SYN-RECEIVED -> ESTABLISHED
4402 * SYN-RECEIVED* -> FIN-WAIT-1
4404 tp
->t_starttime
= tcp_now
;
4405 tcp_sbrcv_tstmp_check(tp
);
4406 if (tp
->t_flags
& TF_NEEDFIN
) {
4407 DTRACE_TCP4(state__change
, void, NULL
,
4408 struct inpcb
*, inp
,
4409 struct tcpcb
*, tp
, int32_t, TCPS_FIN_WAIT_1
);
4410 tp
->t_state
= TCPS_FIN_WAIT_1
;
4411 tp
->t_flags
&= ~TF_NEEDFIN
;
4413 TCP_LOG_CONNECTION_SUMMARY(tp
);
4415 DTRACE_TCP4(state__change
, void, NULL
,
4416 struct inpcb
*, inp
,
4417 struct tcpcb
*, tp
, int32_t, TCPS_ESTABLISHED
);
4418 tp
->t_state
= TCPS_ESTABLISHED
;
4419 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
4420 TCP_CONN_KEEPIDLE(tp
));
4421 if (nstat_collect
) {
4422 nstat_route_connect_success(
4423 tp
->t_inpcb
->inp_route
.ro_rt
);
4426 * The SYN is acknowledged but una is not updated
4427 * yet. So pass the value of ack to compute
4428 * sndbytes correctly
4430 inp_count_sndbytes(inp
, th
->th_ack
);
4433 * If segment contains data or ACK, will call tcp_reass()
4434 * later; if not, do so now to pass queued data to user.
4436 if (tlen
== 0 && (thflags
& TH_FIN
) == 0) {
4438 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
4439 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
4441 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
4442 ip
= (struct ip
*)&saved_hdr
[0];
4444 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
4445 (void) tcp_reass(tp
, (struct tcphdr
*)0, &tlen
,
4446 NULL
, ifp
, &read_wakeup
);
4449 tp
->snd_wl1
= th
->th_seq
- 1;
4453 * Do not send the connect notification for additional subflows
4454 * until ACK for 3-way handshake arrives.
4456 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
4457 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
4458 isconnected
= FALSE
;
4462 if ((tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)) {
4463 /* Done this when receiving the SYN */
4464 isconnected
= FALSE
;
4466 OSDecrementAtomic(&tcp_tfo_halfcnt
);
4468 /* Panic if something has gone terribly wrong. */
4469 VERIFY(tcp_tfo_halfcnt
>= 0);
4471 tp
->t_tfo_flags
&= ~TFO_F_COOKIE_VALID
;
4475 * In case there is data in the send-queue (e.g., TFO is being
4476 * used, or connectx+data has been done), then if we would
4477 * "FALLTHROUGH", we would handle this ACK as if data has been
4478 * acknowledged. But, we have to prevent this. And this
4479 * can be prevented by increasing snd_una by 1, so that the
4480 * SYN is not considered as data (snd_una++ is actually also
4481 * done in SYN_SENT-state as part of the regular TCP stack).
4483 * In case there is data on this ack as well, the data will be
4484 * handled by the label "dodata" right after step6.
4486 if (so
->so_snd
.sb_cc
) {
4487 tp
->snd_una
++; /* SYN is acked */
4488 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
)) {
4489 tp
->snd_nxt
= tp
->snd_una
;
4493 * No duplicate-ACK handling is needed. So, we
4494 * directly advance to processing the ACK (aka,
4495 * updating the RTT estimation,...)
4497 * But, we first need to handle eventual SACKs,
4498 * because TFO will start sending data with the
4499 * SYN/ACK, so it might be that the client
4500 * includes a SACK with its ACK.
4502 if (SACK_ENABLED(tp
) &&
4503 (to
.to_nsacks
> 0 ||
4504 !TAILQ_EMPTY(&tp
->snd_holes
))) {
4505 tcp_sack_doack(tp
, &to
, th
,
4515 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4516 * ACKs. If the ack is in the range
4517 * tp->snd_una < th->th_ack <= tp->snd_max
4518 * then advance tp->snd_una to th->th_ack and drop
4519 * data from the retransmission queue. If this ACK reflects
4520 * more up to date window information we update our window information.
4522 case TCPS_ESTABLISHED
:
4523 case TCPS_FIN_WAIT_1
:
4524 case TCPS_FIN_WAIT_2
:
4525 case TCPS_CLOSE_WAIT
:
4528 case TCPS_TIME_WAIT
:
4529 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
4530 tcpstat
.tcps_rcvacktoomuch
++;
4531 if (tcp_do_rfc5961
&& tcp_is_ack_ratelimited(tp
)) {
4532 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 rcvacktoomuch");
4538 if (tcp_do_rfc5961
&& SEQ_LT(th
->th_ack
, tp
->snd_una
- tp
->max_sndwnd
)) {
4539 if (tcp_is_ack_ratelimited(tp
)) {
4540 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 bad ACK");
4546 if (SACK_ENABLED(tp
) && to
.to_nsacks
> 0) {
4547 recvd_dsack
= tcp_sack_process_dsack(tp
, &to
, th
);
4549 * If DSACK is received and this packet has no
4550 * other SACK information, it can be dropped.
4551 * We do not want to treat it as a duplicate ack.
4554 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) &&
4555 to
.to_nsacks
== 0) {
4556 tcp_bad_rexmt_check(tp
, th
, &to
);
4561 if (SACK_ENABLED(tp
) &&
4562 (to
.to_nsacks
> 0 || !TAILQ_EMPTY(&tp
->snd_holes
))) {
4563 tcp_sack_doack(tp
, &to
, th
, &sack_bytes_acked
);
4567 if (tp
->t_mpuna
&& SEQ_GEQ(th
->th_ack
, tp
->t_mpuna
)) {
4568 if (tp
->t_mpflags
& TMPF_PREESTABLISHED
) {
4569 /* MP TCP establishment succeeded */
4571 if (tp
->t_mpflags
& TMPF_JOINED_FLOW
) {
4572 if (tp
->t_mpflags
& TMPF_SENT_JOIN
) {
4574 ~TMPF_PREESTABLISHED
;
4578 tp
->t_timer
[TCPT_JACK_RXMT
] = 0;
4579 tp
->t_mprxtshift
= 0;
4582 isconnected
= FALSE
;
4591 tcp_tfo_rcv_ack(tp
, th
);
4594 * If we have outstanding data (other than
4595 * a window probe), this is a completely
4596 * duplicate ack and the ack is the biggest we've seen.
4598 * Need to accommodate a change in window on duplicate acks
4599 * to allow operating systems that update window during
4600 * recovery with SACK
4602 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
4603 if (tlen
== 0 && (tiwin
== tp
->snd_wnd
||
4604 (to
.to_nsacks
> 0 && sack_bytes_acked
> 0))) {
4606 * If both ends send FIN at the same time,
4607 * then the ack will be a duplicate ack
4608 * but we have to process the FIN. Check
4609 * for this condition and process the FIN
4610 * instead of the dupack
4612 if ((thflags
& TH_FIN
) &&
4613 !TCPS_HAVERCVDFIN(tp
->t_state
)) {
4619 * MPTCP options that are ignored must
4620 * not be treated as duplicate ACKs.
4622 if (to
.to_flags
& TOF_MPTCP
) {
4626 if ((isconnected
) && (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
4627 mptcplog((LOG_DEBUG
, "MPTCP "
4628 "Sockets: bypass ack recovery\n"),
4630 MPTCP_LOGLVL_VERBOSE
);
4635 * If a duplicate acknowledgement was seen
4636 * after ECN, it indicates packet loss in
4637 * addition to ECN. Reset INRECOVERY flag
4638 * so that we can process partial acks
4641 if (tp
->ecn_flags
& TE_INRECOVERY
) {
4642 tp
->ecn_flags
&= ~TE_INRECOVERY
;
4645 tcpstat
.tcps_rcvdupack
++;
4649 * Check if we need to reset the limit on
4652 if (tp
->t_early_rexmt_count
> 0 &&
4654 (tp
->t_early_rexmt_win
+
4655 TCP_EARLY_REXMT_WIN
))) {
4656 tp
->t_early_rexmt_count
= 0;
4660 * Is early retransmit needed? We check for
4661 * this when the connection is waiting for
4662 * duplicate acks to enter fast recovery.
4664 if (!IN_FASTRECOVERY(tp
)) {
4665 tcp_early_rexmt_check(tp
, th
);
4669 * If we've seen exactly rexmt threshold
4670 * of duplicate acks, assume a packet
4671 * has been dropped and retransmit it.
4672 * Kludge snd_nxt & the congestion
4673 * window so we send only this one
4676 * We know we're losing at the current
4677 * window size so do congestion avoidance
4678 * (set ssthresh to half the current window
4679 * and pull our congestion window back to
4680 * the new ssthresh).
4682 * Dup acks mean that packets have left the
4683 * network (they're now cached at the receiver)
4684 * so bump cwnd by the amount in the receiver
4685 * to keep a constant cwnd packets in the
4688 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
4689 (th
->th_ack
!= tp
->snd_una
4690 && sack_bytes_acked
== 0)) {
4692 tp
->t_rexmtthresh
= tcprexmtthresh
;
4693 } else if (tp
->t_dupacks
> tp
->t_rexmtthresh
||
4694 IN_FASTRECOVERY(tp
)) {
4696 * If this connection was seeing packet
4697 * reordering, then recovery might be
4698 * delayed to disambiguate between
4699 * reordering and loss
4701 if (SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
) &&
4703 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
)) ==
4704 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
)) {
4706 * Since the SACK information is already
4707 * updated, this ACK will be dropped
4712 if (SACK_ENABLED(tp
)
4713 && IN_FASTRECOVERY(tp
)) {
4717 * Compute the amount of data in flight first.
4718 * We can inject new data into the pipe iff
4719 * we have less than 1/2 the original window's
4720 * worth of data in flight.
4722 awnd
= (tp
->snd_nxt
- tp
->snd_fack
) +
4723 tp
->sackhint
.sack_bytes_rexmit
;
4724 if (awnd
< tp
->snd_ssthresh
) {
4725 tp
->snd_cwnd
+= tp
->t_maxseg
;
4726 if (tp
->snd_cwnd
> tp
->snd_ssthresh
) {
4727 tp
->snd_cwnd
= tp
->snd_ssthresh
;
4731 tp
->snd_cwnd
+= tp
->t_maxseg
;
4734 /* Process any window updates */
4735 if (tiwin
> tp
->snd_wnd
) {
4736 tcp_update_window(tp
, thflags
,
4739 tcp_ccdbg_trace(tp
, th
,
4740 TCP_CC_IN_FASTRECOVERY
);
4742 (void) tcp_output(tp
);
4745 } else if (tp
->t_dupacks
== tp
->t_rexmtthresh
) {
4746 tcp_seq onxt
= tp
->snd_nxt
;
4749 * If we're doing sack, check to
4750 * see if we're already in sack
4751 * recovery. If we're not doing sack,
4752 * check to see if we're in newreno
4755 if (SACK_ENABLED(tp
)) {
4756 if (IN_FASTRECOVERY(tp
)) {
4759 } else if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
4763 if (SEQ_LEQ(th
->th_ack
,
4769 if (tp
->t_flags
& TF_SENTFIN
) {
4770 tp
->snd_recover
= tp
->snd_max
- 1;
4772 tp
->snd_recover
= tp
->snd_max
;
4774 tp
->t_timer
[TCPT_PTO
] = 0;
4778 * If the connection has seen pkt
4779 * reordering, delay recovery until
4780 * it is clear that the packet
4783 if (SACK_ENABLED(tp
) &&
4785 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
))
4786 == TF_PKTS_REORDERED
&&
4787 !IN_FASTRECOVERY(tp
) &&
4788 tp
->t_reorderwin
> 0 &&
4789 (tp
->t_state
== TCPS_ESTABLISHED
||
4790 tp
->t_state
== TCPS_FIN_WAIT_1
)) {
4791 tp
->t_timer
[TCPT_DELAYFR
] =
4792 OFFSET_FROM_START(tp
,
4794 tp
->t_flagsext
|= TF_DELAY_RECOVERY
;
4795 tcpstat
.tcps_delay_recovery
++;
4796 tcp_ccdbg_trace(tp
, th
,
4797 TCP_CC_DELAY_FASTRECOVERY
);
4801 tcp_rexmt_save_state(tp
);
4803 * If the current tcp cc module has
4804 * defined a hook for tasks to run
4805 * before entering FR, call it
4807 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
4808 CC_ALGO(tp
)->pre_fr(tp
);
4810 ENTER_FASTRECOVERY(tp
);
4811 tp
->t_timer
[TCPT_REXMT
] = 0;
4812 if (TCP_ECN_ENABLED(tp
)) {
4813 tp
->ecn_flags
|= TE_SENDCWR
;
4816 if (SACK_ENABLED(tp
)) {
4817 tcpstat
.tcps_sack_recovery_episode
++;
4818 tp
->t_sack_recovery_episode
++;
4819 tp
->sack_newdata
= tp
->snd_nxt
;
4820 tp
->snd_cwnd
= tp
->t_maxseg
;
4822 ~TF_CWND_NONVALIDATED
;
4824 /* Process any window updates */
4825 if (tiwin
> tp
->snd_wnd
) {
4831 tcp_ccdbg_trace(tp
, th
,
4832 TCP_CC_ENTER_FASTRECOVERY
);
4833 (void) tcp_output(tp
);
4836 tp
->snd_nxt
= th
->th_ack
;
4837 tp
->snd_cwnd
= tp
->t_maxseg
;
4839 /* Process any window updates */
4840 if (tiwin
> tp
->snd_wnd
) {
4841 tcp_update_window(tp
,
4846 (void) tcp_output(tp
);
4847 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
4848 tcp_cc_adjust_nonvalidated_cwnd(tp
);
4850 tp
->snd_cwnd
= tp
->snd_ssthresh
+
4851 tp
->t_maxseg
* tp
->t_dupacks
;
4853 if (SEQ_GT(onxt
, tp
->snd_nxt
)) {
4857 tcp_ccdbg_trace(tp
, th
,
4858 TCP_CC_ENTER_FASTRECOVERY
);
4860 } else if (limited_txmt
&&
4861 ALLOW_LIMITED_TRANSMIT(tp
) &&
4862 (!(SACK_ENABLED(tp
)) || sack_bytes_acked
> 0) &&
4863 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) > 0) {
4864 u_int32_t incr
= (tp
->t_maxseg
* tp
->t_dupacks
);
4866 /* Use Limited Transmit algorithm on the first two
4867 * duplicate acks when there is new data to transmit
4869 tp
->snd_cwnd
+= incr
;
4870 tcpstat
.tcps_limited_txt
++;
4871 (void) tcp_output(tp
);
4873 tcp_ccdbg_trace(tp
, th
, TCP_CC_LIMITED_TRANSMIT
);
4875 /* Reset snd_cwnd back to normal */
4876 tp
->snd_cwnd
-= incr
;
4882 * If the congestion window was inflated to account
4883 * for the other side's cached packets, retract it.
4885 if (IN_FASTRECOVERY(tp
)) {
4886 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
4888 * If we received an ECE and entered
4889 * recovery, the subsequent ACKs should
4890 * not be treated as partial acks.
4892 if (tp
->ecn_flags
& TE_INRECOVERY
) {
4896 if (SACK_ENABLED(tp
)) {
4897 tcp_sack_partialack(tp
, th
);
4899 tcp_newreno_partial_ack(tp
, th
);
4901 tcp_ccdbg_trace(tp
, th
, TCP_CC_PARTIAL_ACK
);
4903 EXIT_FASTRECOVERY(tp
);
4904 if (CC_ALGO(tp
)->post_fr
!= NULL
) {
4905 CC_ALGO(tp
)->post_fr(tp
, th
);
4908 tcp_clear_pipeack_state(tp
);
4909 tcp_ccdbg_trace(tp
, th
,
4910 TCP_CC_EXIT_FASTRECOVERY
);
4912 } else if ((tp
->t_flagsext
&
4913 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
))
4914 == (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
)) {
4916 * If the ack acknowledges upto snd_recover or if
4917 * it acknowledges all the snd holes, exit
4918 * recovery and cancel the timer. Otherwise,
4919 * this is a partial ack. Wait for recovery timer
4920 * to enter recovery. The snd_holes have already
4923 if (SEQ_GEQ(th
->th_ack
, tp
->snd_recover
) ||
4924 TAILQ_EMPTY(&tp
->snd_holes
)) {
4925 tp
->t_timer
[TCPT_DELAYFR
] = 0;
4926 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
4927 EXIT_FASTRECOVERY(tp
);
4928 tcp_ccdbg_trace(tp
, th
,
4929 TCP_CC_EXIT_FASTRECOVERY
);
4933 * We were not in fast recovery. Reset the
4934 * duplicate ack counter.
4937 tp
->t_rexmtthresh
= tcprexmtthresh
;
4942 * If we reach this point, ACK is not a duplicate,
4943 * i.e., it ACKs something we sent.
4945 if (tp
->t_flags
& TF_NEEDSYN
) {
4947 * T/TCP: Connection was half-synchronized, and our
4948 * SYN has been ACK'd (so connection is now fully
4949 * synchronized). Go to non-starred state,
4950 * increment snd_una for ACK of SYN, and check if
4951 * we can do window scaling.
4953 tp
->t_flags
&= ~TF_NEEDSYN
;
4955 /* Do window scaling? */
4956 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
4957 tp
->snd_scale
= tp
->requested_s_scale
;
4958 tp
->rcv_scale
= tp
->request_r_scale
;
4963 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
4964 acked
= BYTES_ACKED(th
, tp
);
4965 tcpstat
.tcps_rcvackpack
++;
4966 tcpstat
.tcps_rcvackbyte
+= acked
;
4969 * If the last packet was a retransmit, make sure
4970 * it was not spurious.
4972 * This will also take care of congestion window
4973 * adjustment if a last packet was recovered due to a
4976 tcp_bad_rexmt_check(tp
, th
, &to
);
4978 /* Recalculate the RTT */
4979 tcp_compute_rtt(tp
, &to
, th
);
4982 * If all outstanding data is acked, stop retransmit
4983 * timer and remember to restart (more output or persist).
4984 * If there is more data to be acked, restart retransmit
4985 * timer, using current (possibly backed-off) value.
4987 TCP_RESET_REXMT_STATE(tp
);
4988 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
4989 tp
->t_rttmin
, TCPTV_REXMTMAX
,
4990 TCP_ADD_REXMTSLOP(tp
));
4991 if (th
->th_ack
== tp
->snd_max
) {
4992 tp
->t_timer
[TCPT_REXMT
] = 0;
4993 tp
->t_timer
[TCPT_PTO
] = 0;
4995 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
4996 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
5000 if ((prev_t_state
== TCPS_SYN_SENT
||
5001 prev_t_state
== TCPS_SYN_RECEIVED
) &&
5002 tp
->t_state
== TCPS_ESTABLISHED
) {
5003 TCP_LOG_RTT_INFO(tp
);
5007 * If no data (only SYN) was ACK'd, skip rest of ACK
5015 * When outgoing data has been acked (except the SYN+data), we
5016 * mark this connection as "sending good" for TFO.
5018 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
5019 !(tp
->t_tfo_flags
& TFO_F_NO_SNDPROBING
) &&
5020 !(th
->th_flags
& TH_SYN
)) {
5021 tp
->t_tfo_flags
|= TFO_F_NO_SNDPROBING
;
5025 * If TH_ECE is received, make sure that ECN is enabled
5026 * on that connection and we have sent ECT on data packets.
5028 if ((thflags
& TH_ECE
) != 0 && TCP_ECN_ENABLED(tp
) &&
5029 (tp
->ecn_flags
& TE_SENDIPECT
)) {
5031 * Reduce the congestion window if we haven't
5034 if (!IN_FASTRECOVERY(tp
)) {
5035 tcp_reduce_congestion_window(tp
);
5036 tp
->ecn_flags
|= (TE_INRECOVERY
| TE_SENDCWR
);
5038 * Also note that the connection received
5041 tp
->ecn_flags
|= TE_RECV_ECN_ECE
;
5042 INP_INC_IFNET_STAT(inp
, ecn_recv_ece
);
5043 tcpstat
.tcps_ecn_recv_ece
++;
5044 tcp_ccdbg_trace(tp
, th
, TCP_CC_ECN_RCVD
);
5049 * When new data is acked, open the congestion window.
5050 * The specifics of how this is achieved are up to the
5051 * congestion control algorithm in use for this connection.
5053 * The calculations in this function assume that snd_una is
5056 if (!IN_FASTRECOVERY(tp
)) {
5057 if (CC_ALGO(tp
)->ack_rcvd
!= NULL
) {
5058 CC_ALGO(tp
)->ack_rcvd(tp
, th
);
5060 tcp_ccdbg_trace(tp
, th
, TCP_CC_ACK_RCVD
);
5062 if (acked
> so
->so_snd
.sb_cc
) {
5063 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
5064 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
5065 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
5066 so
->so_msg_state
->msg_serial_bytes
-=
5067 (int)so
->so_snd
.sb_cc
;
5071 sbdrop(&so
->so_snd
, acked
);
5072 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
5073 so
->so_msg_state
->msg_serial_bytes
-=
5076 tcp_sbsnd_trim(&so
->so_snd
);
5077 tp
->snd_wnd
-= acked
;
5080 /* detect una wraparound */
5081 if (!IN_FASTRECOVERY(tp
) &&
5082 SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
5083 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
)) {
5084 tp
->snd_recover
= th
->th_ack
- 1;
5087 if (IN_FASTRECOVERY(tp
) &&
5088 SEQ_GEQ(th
->th_ack
, tp
->snd_recover
)) {
5089 EXIT_FASTRECOVERY(tp
);
5092 tp
->snd_una
= th
->th_ack
;
5094 if (SACK_ENABLED(tp
)) {
5095 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
)) {
5096 tp
->snd_recover
= tp
->snd_una
;
5099 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
)) {
5100 tp
->snd_nxt
= tp
->snd_una
;
5102 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
5103 !TCP_DSACK_SEQ_IN_WINDOW(tp
, tp
->t_dsack_lastuna
,
5105 tcp_rxtseg_clean(tp
);
5107 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
5108 tp
->t_bwmeas
!= NULL
) {
5109 tcp_bwmeas_check(tp
);
5114 if (!SLIST_EMPTY(&tp
->t_notify_ack
)) {
5115 tcp_notify_acknowledgement(tp
, so
);
5118 switch (tp
->t_state
) {
5120 * In FIN_WAIT_1 STATE in addition to the processing
5121 * for the ESTABLISHED state if our FIN is now acknowledged
5122 * then enter FIN_WAIT_2.
5124 case TCPS_FIN_WAIT_1
:
5125 if (ourfinisacked
) {
5127 * If we can't receive any more
5128 * data, then closing user can proceed.
5129 * Starting the TCPT_2MSL timer is contrary to the
5130 * specification, but if we don't get a FIN
5131 * we'll hang forever.
5133 if (so
->so_state
& SS_CANTRCVMORE
) {
5134 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
5135 TCP_CONN_MAXIDLE(tp
));
5136 isconnected
= FALSE
;
5137 isdisconnected
= TRUE
;
5139 DTRACE_TCP4(state__change
, void, NULL
,
5140 struct inpcb
*, inp
,
5142 int32_t, TCPS_FIN_WAIT_2
);
5143 tp
->t_state
= TCPS_FIN_WAIT_2
;
5144 /* fall through and make sure we also recognize
5145 * data ACKed with the FIN
5151 * In CLOSING STATE in addition to the processing for
5152 * the ESTABLISHED state if the ACK acknowledges our FIN
5153 * then enter the TIME-WAIT state, otherwise ignore
5157 if (ourfinisacked
) {
5158 DTRACE_TCP4(state__change
, void, NULL
,
5159 struct inpcb
*, inp
,
5161 int32_t, TCPS_TIME_WAIT
);
5162 tp
->t_state
= TCPS_TIME_WAIT
;
5163 tcp_canceltimers(tp
);
5164 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
5165 tp
->t_flags
|= TF_CLOSING
;
5167 add_to_time_wait(tp
, 2 * tcp_msl
);
5169 isconnected
= FALSE
;
5170 isdisconnected
= TRUE
;
5175 * In LAST_ACK, we may still be waiting for data to drain
5176 * and/or to be acked, as well as for the ack of our FIN.
5177 * If our FIN is now acknowledged, delete the TCB,
5178 * enter the closed state and return.
5181 if (ourfinisacked
) {
5188 * In TIME_WAIT state the only thing that should arrive
5189 * is a retransmission of the remote FIN. Acknowledge
5190 * it and restart the finack timer.
5192 case TCPS_TIME_WAIT
:
5193 add_to_time_wait(tp
, 2 * tcp_msl
);
5198 * If there is a SACK option on the ACK and we
5199 * haven't seen any duplicate acks before, count
5200 * it as a duplicate ack even if the cumulative
5201 * ack is advanced. If the receiver delayed an
5202 * ack and detected loss afterwards, then the ack
5203 * will advance cumulative ack and will also have
5204 * a SACK option. So counting it as one duplicate
5207 if (sack_ackadv
== 1 &&
5208 tp
->t_state
== TCPS_ESTABLISHED
&&
5209 SACK_ENABLED(tp
) && sack_bytes_acked
> 0 &&
5210 to
.to_nsacks
> 0 && tp
->t_dupacks
== 0 &&
5211 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) && tlen
== 0 &&
5212 !(tp
->t_flagsext
& TF_PKTS_REORDERED
)) {
5213 tcpstat
.tcps_sack_ackadv
++;
5214 goto process_dupack
;
5220 * Update window information.
5222 if (tcp_update_window(tp
, thflags
, th
, tiwin
, tlen
)) {
5227 * Process segments with URG.
5229 if ((thflags
& TH_URG
) && th
->th_urp
&&
5230 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
5232 * This is a kludge, but if we receive and accept
5233 * random urgent pointers, we'll crash in
5234 * soreceive. It's hard to imagine someone
5235 * actually wanting to send this much urgent data.
5237 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
5238 th
->th_urp
= 0; /* XXX */
5239 thflags
&= ~TH_URG
; /* XXX */
5240 goto dodata
; /* XXX */
5243 * If this segment advances the known urgent pointer,
5244 * then mark the data stream. This should not happen
5245 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5246 * a FIN has been received from the remote side.
5247 * In these states we ignore the URG.
5249 * According to RFC961 (Assigned Protocols),
5250 * the urgent pointer points to the last octet
5251 * of urgent data. We continue, however,
5252 * to consider it to indicate the first octet
5253 * of data past the urgent section as the original
5254 * spec states (in one of two places).
5256 if (SEQ_GT(th
->th_seq
+ th
->th_urp
, tp
->rcv_up
)) {
5257 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
5258 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
5259 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
5260 if (so
->so_oobmark
== 0) {
5261 so
->so_state
|= SS_RCVATMARK
;
5262 postevent(so
, 0, EV_OOB
);
5265 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
5268 * Remove out of band data so doesn't get presented to user.
5269 * This can happen independent of advancing the URG pointer,
5270 * but if two URG's are pending at once, some out-of-band
5271 * data may creep in... ick.
5273 if (th
->th_urp
<= (u_int32_t
)tlen
5275 && (so
->so_options
& SO_OOBINLINE
) == 0
5278 tcp_pulloutofband(so
, th
, m
,
5279 drop_hdrlen
); /* hdr drop is delayed */
5283 * If no out of band data is expected,
5284 * pull receive urgent pointer along
5285 * with the receive window.
5287 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
)) {
5288 tp
->rcv_up
= tp
->rcv_nxt
;
5293 /* Set socket's connect or disconnect state correcly before doing data.
5294 * The following might unlock the socket if there is an upcall or a socket
5299 } else if (isdisconnected
) {
5300 soisdisconnected(so
);
5303 /* Let's check the state of pcb just to make sure that it did not get closed
5304 * when we unlocked above
5306 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
5307 /* Just drop the packet that we are processing and return */
5308 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "INPCB_STATE_DEAD");
5313 * Process the segment text, merging it into the TCP sequencing queue,
5314 * and arranging for acknowledgment of receipt if necessary.
5315 * This process logically involves adjusting tp->rcv_wnd as data
5316 * is presented to the user (this happens in tcp_usrreq.c,
5317 * case PRU_RCVD). If a FIN has already been received on this
5318 * connection then we just ignore the text.
5320 * If we are in SYN-received state and got a valid TFO cookie, we want
5321 * to process the data.
5323 if ((tlen
|| (thflags
& TH_FIN
)) &&
5324 TCPS_HAVERCVDFIN(tp
->t_state
) == 0 &&
5325 (TCPS_HAVEESTABLISHED(tp
->t_state
) ||
5326 (tp
->t_state
== TCPS_SYN_RECEIVED
&&
5327 (tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)))) {
5328 tcp_seq save_start
= th
->th_seq
;
5329 tcp_seq save_end
= th
->th_seq
+ tlen
;
5330 m_adj(m
, drop_hdrlen
); /* delayed header drop */
5332 * Insert segment which includes th into TCP reassembly queue
5333 * with control block tp. Set thflags to whether reassembly now
5334 * includes a segment with FIN. This handles the common case
5335 * inline (segment is the next to be received on an established
5336 * connection, and the queue is empty), avoiding linkage into
5337 * and removal from the queue and repetition of various
5339 * Set DELACK for segments received in order, but ack
5340 * immediately when segments are out of order (so
5341 * fast retransmit can work).
5343 if (th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
5344 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
5346 * Calculate the RTT on the receiver only if the
5347 * connection is in streaming mode and the last
5348 * packet was not an end-of-write
5350 if (tp
->t_flags
& TF_STREAMING_ON
) {
5351 tcp_compute_rtt(tp
, &to
, th
);
5354 if (DELAY_ACK(tp
, th
) &&
5355 ((tp
->t_flags
& TF_ACKNOW
) == 0)) {
5356 if ((tp
->t_flags
& TF_DELACK
) == 0) {
5357 tp
->t_flags
|= TF_DELACK
;
5358 tp
->t_timer
[TCPT_DELACK
] =
5359 OFFSET_FROM_START(tp
, tcp_delack
);
5362 tp
->t_flags
|= TF_ACKNOW
;
5364 tp
->rcv_nxt
+= tlen
;
5365 thflags
= th
->th_flags
& TH_FIN
;
5366 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
5367 tcpstat
.tcps_rcvbyte
+= tlen
;
5368 if (nstat_collect
) {
5369 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
5370 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
5371 rxpackets
, m
->m_pkthdr
.lro_npkts
);
5373 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
5376 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
5378 inp_set_activity_bitmap(inp
);
5380 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
5381 TCP_AUTORCVBUF_MAX(ifp
));
5382 so_recv_data_stat(so
, m
, drop_hdrlen
);
5385 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
5386 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
5388 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
5389 ip
= (struct ip
*)&saved_hdr
[0];
5391 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
5392 if (sbappendstream_rcvdemux(so
, m
,
5393 th
->th_seq
- (tp
->irs
+ 1), 0)) {
5399 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
5400 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
5402 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
5403 ip
= (struct ip
*)&saved_hdr
[0];
5406 if (tcp_autotune_reorder
) {
5407 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
, TCP_AUTORCVBUF_MAX(ifp
));
5410 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
5411 thflags
= tcp_reass(tp
, th
, &tlen
, m
, ifp
, &read_wakeup
);
5413 tp
->t_flags
|= TF_ACKNOW
;
5416 if ((tlen
> 0 || (th
->th_flags
& TH_FIN
)) && SACK_ENABLED(tp
)) {
5417 if (th
->th_flags
& TH_FIN
) {
5420 tcp_update_sack_list(tp
, save_start
, save_end
);
5423 tcp_adaptive_rwtimo_check(tp
, tlen
);
5426 tcp_tfo_rcv_data(tp
);
5429 if (tp
->t_flags
& TF_DELACK
) {
5432 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
5433 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
5434 th
->th_seq
, th
->th_ack
, th
->th_win
);
5438 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
5439 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
5440 th
->th_seq
, th
->th_ack
, th
->th_win
);
5444 if ((so
->so_flags
& SOF_MP_SUBFLOW
) && tlen
== 0 &&
5445 (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_DFIN
) &&
5446 (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
)) {
5447 m_adj(m
, drop_hdrlen
); /* delayed header drop */
5448 mptcp_input(tptomptp(tp
)->mpt_mpte
, m
);
5449 tp
->t_flags
|= TF_ACKNOW
;
5457 * If FIN is received ACK the FIN and let the user know
5458 * that the connection is closing.
5460 if (thflags
& TH_FIN
) {
5461 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
5463 postevent(so
, 0, EV_FIN
);
5465 * If connection is half-synchronized
5466 * (ie NEEDSYN flag on) then delay ACK,
5467 * so it may be piggybacked when SYN is sent.
5468 * Otherwise, since we received a FIN then no
5469 * more input can be expected, send ACK now.
5471 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
5472 if (DELAY_ACK(tp
, th
) && (tp
->t_flags
& TF_NEEDSYN
)) {
5473 if ((tp
->t_flags
& TF_DELACK
) == 0) {
5474 tp
->t_flags
|= TF_DELACK
;
5475 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
5478 tp
->t_flags
|= TF_ACKNOW
;
5482 switch (tp
->t_state
) {
5484 * In SYN_RECEIVED and ESTABLISHED STATES
5485 * enter the CLOSE_WAIT state.
5487 case TCPS_SYN_RECEIVED
:
5488 tp
->t_starttime
= tcp_now
;
5489 case TCPS_ESTABLISHED
:
5490 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
5491 struct tcpcb
*, tp
, int32_t, TCPS_CLOSE_WAIT
);
5492 tp
->t_state
= TCPS_CLOSE_WAIT
;
5496 * If still in FIN_WAIT_1 STATE FIN has not been acked so
5497 * enter the CLOSING state.
5499 case TCPS_FIN_WAIT_1
:
5500 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
5501 struct tcpcb
*, tp
, int32_t, TCPS_CLOSING
);
5502 tp
->t_state
= TCPS_CLOSING
;
5506 * In FIN_WAIT_2 state enter the TIME_WAIT state,
5507 * starting the time-wait timer, turning off the other
5510 case TCPS_FIN_WAIT_2
:
5511 DTRACE_TCP4(state__change
, void, NULL
,
5512 struct inpcb
*, inp
,
5514 int32_t, TCPS_TIME_WAIT
);
5515 tp
->t_state
= TCPS_TIME_WAIT
;
5516 tcp_canceltimers(tp
);
5517 tp
->t_flags
|= TF_ACKNOW
;
5518 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
5519 tp
->t_flags
|= TF_CLOSING
;
5521 add_to_time_wait(tp
, 2 * tcp_msl
);
5523 soisdisconnected(so
);
5527 * In TIME_WAIT state restart the 2 MSL time_wait timer.
5529 case TCPS_TIME_WAIT
:
5530 add_to_time_wait(tp
, 2 * tcp_msl
);
5535 if (so
->so_options
& SO_DEBUG
) {
5536 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
5542 mptcp_handle_input(so
);
5546 * Return any desired output.
5548 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
5549 (void) tcp_output(tp
);
5552 tcp_check_timer_state(tp
);
5554 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5556 socket_unlock(so
, 1);
5557 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5562 * Generate an ACK dropping incoming segment if it occupies
5563 * sequence space, where the ACK reflects our state.
5565 * We can now skip the test for the RST flag since all
5566 * paths to this code happen after packets containing
5567 * RST have been dropped.
5569 * In the SYN-RECEIVED state, don't send an ACK unless the
5570 * segment we received passes the SYN-RECEIVED ACK test.
5571 * If it fails send a RST. This breaks the loop in the
5572 * "LAND" DoS attack, and also prevents an ACK storm
5573 * between two listening ports that have been sent forged
5574 * SYN segments, each with the source address of the other.
5576 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
5577 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
5578 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
5579 rstreason
= BANDLIM_RST_OPENPORT
;
5580 IF_TCP_STATINC(ifp
, dospacket
);
5584 if (so
->so_options
& SO_DEBUG
) {
5585 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5590 tp
->t_flags
|= TF_ACKNOW
;
5592 (void) tcp_output(tp
);
5594 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5596 /* Don't need to check timer state as we should have done it during tcp_output */
5597 socket_unlock(so
, 1);
5598 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5600 dropwithresetnosock
:
5604 * Generate a RST, dropping incoming segment.
5605 * Make ACK acceptable to originator of segment.
5606 * Don't bother to respond if destination was broadcast/multicast.
5608 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
| M_MCAST
)) {
5613 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
5614 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
5619 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
5620 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
5621 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
5622 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
5625 /* IPv6 anycast check is done at tcp6_input() */
5628 * Perform bandwidth limiting.
5631 if (badport_bandlim(rstreason
) < 0) {
5637 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
5638 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5642 bzero(&tra
, sizeof(tra
));
5643 tra
.ifscope
= ifscope
;
5644 tra
.awdl_unrestricted
= 1;
5645 tra
.intcoproc_allowed
= 1;
5646 if (thflags
& TH_ACK
) {
5647 /* mtod() below is safe as long as hdr dropping is delayed */
5648 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
5651 if (thflags
& TH_SYN
) {
5654 /* mtod() below is safe as long as hdr dropping is delayed */
5655 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+ tlen
,
5656 (tcp_seq
)0, TH_RST
| TH_ACK
, &tra
);
5658 /* destroy temporarily created socket */
5661 socket_unlock(so
, 1);
5662 } else if ((inp
!= NULL
) && (nosock
== 0)) {
5663 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5665 socket_unlock(so
, 1);
5667 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5673 * Drop space held by incoming segment and return.
5676 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
5677 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5682 /* destroy temporarily created socket */
5685 socket_unlock(so
, 1);
5686 } else if (nosock
== 0) {
5687 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5689 socket_unlock(so
, 1);
5691 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5696 * Parse TCP options and place in tcpopt.
5699 tcp_dooptions(struct tcpcb
*tp
, u_char
*cp
, int cnt
, struct tcphdr
*th
,
5705 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
5707 if (opt
== TCPOPT_EOL
) {
5710 if (opt
== TCPOPT_NOP
) {
5717 if (optlen
< 2 || optlen
> cnt
) {
5726 if (optlen
!= TCPOLEN_MAXSEG
) {
5729 if (!(th
->th_flags
& TH_SYN
)) {
5732 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
5735 to
->to_flags
|= TOF_MSS
;
5739 if (optlen
!= TCPOLEN_WINDOW
) {
5742 if (!(th
->th_flags
& TH_SYN
)) {
5745 to
->to_flags
|= TOF_SCALE
;
5746 to
->to_requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
5749 case TCPOPT_TIMESTAMP
:
5750 if (optlen
!= TCPOLEN_TIMESTAMP
) {
5753 to
->to_flags
|= TOF_TS
;
5754 bcopy((char *)cp
+ 2,
5755 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
5756 NTOHL(to
->to_tsval
);
5757 bcopy((char *)cp
+ 6,
5758 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
5759 NTOHL(to
->to_tsecr
);
5760 /* Re-enable sending Timestamps if we received them */
5761 if (!(tp
->t_flags
& TF_REQ_TSTMP
) &&
5762 tcp_do_rfc1323
== 1) {
5763 tp
->t_flags
|= TF_REQ_TSTMP
;
5766 case TCPOPT_SACK_PERMITTED
:
5768 optlen
!= TCPOLEN_SACK_PERMITTED
) {
5771 if (th
->th_flags
& TH_SYN
) {
5772 to
->to_flags
|= TOF_SACK
;
5776 if (optlen
<= 2 || (optlen
- 2) % TCPOLEN_SACK
!= 0) {
5779 to
->to_nsacks
= (optlen
- 2) / TCPOLEN_SACK
;
5780 to
->to_sacks
= cp
+ 2;
5781 tcpstat
.tcps_sack_rcv_blocks
++;
5784 case TCPOPT_FASTOPEN
:
5785 if (optlen
== TCPOLEN_FASTOPEN_REQ
) {
5786 if (tp
->t_state
!= TCPS_LISTEN
) {
5790 to
->to_flags
|= TOF_TFOREQ
;
5792 if (optlen
< TCPOLEN_FASTOPEN_REQ
||
5793 (optlen
- TCPOLEN_FASTOPEN_REQ
) > TFO_COOKIE_LEN_MAX
||
5794 (optlen
- TCPOLEN_FASTOPEN_REQ
) < TFO_COOKIE_LEN_MIN
) {
5797 if (tp
->t_state
!= TCPS_LISTEN
&&
5798 tp
->t_state
!= TCPS_SYN_SENT
) {
5802 to
->to_flags
|= TOF_TFO
;
5803 to
->to_tfo
= cp
+ 1;
5808 case TCPOPT_MULTIPATH
:
5809 tcp_do_mptcp_options(tp
, cp
, th
, to
, optlen
);
5817 tcp_finalize_options(struct tcpcb
*tp
, struct tcpopt
*to
, unsigned int ifscope
)
5819 if (to
->to_flags
& TOF_TS
) {
5820 tp
->t_flags
|= TF_RCVD_TSTMP
;
5821 tp
->ts_recent
= to
->to_tsval
;
5822 tp
->ts_recent_age
= tcp_now
;
5824 if (to
->to_flags
& TOF_MSS
) {
5825 tcp_mss(tp
, to
->to_mss
, ifscope
);
5827 if (SACK_ENABLED(tp
)) {
5828 if (!(to
->to_flags
& TOF_SACK
)) {
5829 tp
->t_flagsext
&= ~(TF_SACK_ENABLE
);
5831 tp
->t_flags
|= TF_SACK_PERMIT
;
5834 if (to
->to_flags
& TOF_SCALE
) {
5835 tp
->t_flags
|= TF_RCVD_SCALE
;
5836 tp
->requested_s_scale
= to
->to_requested_s_scale
;
5838 /* Re-enable window scaling, if the option is received */
5839 if (tp
->request_r_scale
> 0) {
5840 tp
->t_flags
|= TF_REQ_SCALE
;
5846 * Pull out of band byte out of a segment so
5847 * it doesn't appear in the user's data queue.
5848 * It is still reflected in the segment length for
5849 * sequencing purposes.
5851 * @param off delayed to be droped hdrlen
5854 tcp_pulloutofband(struct socket
*so
, struct tcphdr
*th
, struct mbuf
*m
, int off
)
5856 int cnt
= off
+ th
->th_urp
- 1;
5859 if (m
->m_len
> cnt
) {
5860 char *cp
= mtod(m
, caddr_t
) + cnt
;
5861 struct tcpcb
*tp
= sototcpcb(so
);
5864 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
5865 bcopy(cp
+ 1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
5867 if (m
->m_flags
& M_PKTHDR
) {
5878 panic("tcp_pulloutofband");
5882 get_base_rtt(struct tcpcb
*tp
)
5884 struct rtentry
*rt
= tp
->t_inpcb
->inp_route
.ro_rt
;
5885 return (rt
== NULL
) ? 0 : rt
->rtt_min
;
5888 /* Each value of RTT base represents the minimum RTT seen in a minute.
5889 * We keep upto N_RTT_BASE minutes worth of history.
5892 update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
)
5894 u_int32_t base_rtt
, i
;
5897 if ((rt
= tp
->t_inpcb
->inp_route
.ro_rt
) == NULL
) {
5900 if (rt
->rtt_expire_ts
== 0) {
5902 if (rt
->rtt_expire_ts
!= 0) {
5906 rt
->rtt_expire_ts
= tcp_now
;
5908 rt
->rtt_hist
[0] = rtt
;
5916 * If the recv side is being throttled, check if the
5917 * current RTT is closer to the base RTT seen in
5918 * first (recent) two slots. If so, unthrottle the stream.
5920 if ((tp
->t_flagsext
& TF_RECV_THROTTLE
) &&
5921 (int)(tcp_now
- tp
->t_recv_throttle_ts
) >= TCP_RECV_THROTTLE_WIN
) {
5922 base_rtt
= rt
->rtt_min
;
5923 if (tp
->t_rttcur
<= (base_rtt
+ target_qdelay
)) {
5924 tp
->t_flagsext
&= ~TF_RECV_THROTTLE
;
5925 tp
->t_recv_throttle_ts
= 0;
5928 #endif /* TRAFFIC_MGT */
5929 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5930 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5932 /* check the condition again to avoid race */
5933 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5934 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5936 if (rt
->rtt_index
>= NRTT_HIST
) {
5939 rt
->rtt_hist
[rt
->rtt_index
] = rtt
;
5940 rt
->rtt_expire_ts
= tcp_now
;
5942 rt
->rtt_hist
[rt
->rtt_index
] =
5943 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5945 /* forget the old value and update minimum */
5947 for (i
= 0; i
< NRTT_HIST
; ++i
) {
5948 if (rt
->rtt_hist
[i
] != 0 &&
5949 (rt
->rtt_min
== 0 ||
5950 rt
->rtt_hist
[i
] < rt
->rtt_min
)) {
5951 rt
->rtt_min
= rt
->rtt_hist
[i
];
5956 rt
->rtt_hist
[rt
->rtt_index
] =
5957 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5958 if (rt
->rtt_min
== 0) {
5961 rt
->rtt_min
= min(rt
->rtt_min
, rtt
);
5967 * If we have a timestamp reply, update smoothed RTT. If no timestamp is
5968 * present but transmit timer is running and timed sequence number was
5969 * acked, update smoothed RTT.
5971 * If timestamps are supported, a receiver can update RTT even if
5972 * there is no outstanding data.
5974 * Some boxes send broken timestamp replies during the SYN+ACK phase,
5975 * ignore timestamps of 0or we could calculate a huge RTT and blow up
5976 * the retransmit timer.
5979 tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
, struct tcphdr
*th
)
5982 VERIFY(to
!= NULL
&& th
!= NULL
);
5983 if (tp
->t_rtttime
!= 0 && SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
5984 u_int32_t pipe_ack_val
;
5985 rtt
= tcp_now
- tp
->t_rtttime
;
5987 * Compute pipe ack -- the amount of data acknowledged
5990 if (SEQ_GT(th
->th_ack
, tp
->t_pipeack_lastuna
)) {
5991 pipe_ack_val
= th
->th_ack
- tp
->t_pipeack_lastuna
;
5992 /* Update the sample */
5993 tp
->t_pipeack_sample
[tp
->t_pipeack_ind
++] =
5995 tp
->t_pipeack_ind
%= TCP_PIPEACK_SAMPLE_COUNT
;
5997 /* Compute the max of the pipeack samples */
5998 pipe_ack_val
= tcp_get_max_pipeack(tp
);
5999 tp
->t_pipeack
= (pipe_ack_val
>
6000 TCP_CC_CWND_INIT_BYTES
) ?
6003 /* start another measurement */
6006 if (((to
->to_flags
& TOF_TS
) != 0) &&
6007 (to
->to_tsecr
!= 0) &&
6008 TSTMP_GEQ(tcp_now
, to
->to_tsecr
)) {
6009 tcp_xmit_timer(tp
, (tcp_now
- to
->to_tsecr
),
6010 to
->to_tsecr
, th
->th_ack
);
6011 } else if (rtt
> 0) {
6012 tcp_xmit_timer(tp
, rtt
, 0, th
->th_ack
);
6017 * Collect new round-trip time estimate and update averages and
6021 tcp_xmit_timer(struct tcpcb
*tp
, int rtt
,
6022 u_int32_t tsecr
, tcp_seq th_ack
)
6025 int old_srtt
= tp
->t_srtt
;
6026 int old_rttvar
= tp
->t_rttvar
;
6027 bool log_rtt
= false;
6030 * On AWDL interface, the initial RTT measurement on SYN
6031 * can be wrong due to peer caching. Avoid the first RTT
6032 * measurement as it might skew up the RTO.
6033 * <rdar://problem/28739046>
6035 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
&&
6036 (tp
->t_inpcb
->inp_last_outifp
->if_eflags
& IFEF_AWDL
) &&
6037 th_ack
== tp
->iss
+ 1) {
6041 if (tp
->t_flagsext
& TF_RECOMPUTE_RTT
) {
6042 if (SEQ_GT(th_ack
, tp
->snd_una
) &&
6043 SEQ_LEQ(th_ack
, tp
->snd_max
) &&
6045 TSTMP_GEQ(tsecr
, tp
->t_badrexmt_time
))) {
6047 * We received a new ACk after a
6048 * spurious timeout. Adapt retransmission
6049 * timer as described in rfc 4015.
6051 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
6052 tp
->t_badrexmt_time
= 0;
6053 tp
->t_srtt
= max(tp
->t_srtt_prev
, rtt
);
6054 tp
->t_srtt
= tp
->t_srtt
<< TCP_RTT_SHIFT
;
6055 tp
->t_rttvar
= max(tp
->t_rttvar_prev
, (rtt
>> 1));
6056 tp
->t_rttvar
= tp
->t_rttvar
<< TCP_RTTVAR_SHIFT
;
6058 if (tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
)) {
6059 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
6068 tcpstat
.tcps_rttupdated
++;
6073 update_base_rtt(tp
, rtt
);
6076 if (tp
->t_srtt
!= 0) {
6078 * srtt is stored as fixed point with 5 bits after the
6079 * binary point (i.e., scaled by 32). The following magic
6080 * is equivalent to the smoothing algorithm in rfc793 with
6081 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
6084 * Freebsd adjusts rtt to origin 0 by subtracting 1
6085 * from the provided rtt value. This was required because
6086 * of the way t_rtttime was initiailised to 1 before.
6087 * Since we changed t_rtttime to be based on
6088 * tcp_now, this extra adjustment is not needed.
6090 delta
= (rtt
<< TCP_DELTA_SHIFT
)
6091 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
6093 if ((tp
->t_srtt
+= delta
) <= 0) {
6098 * We accumulate a smoothed rtt variance (actually, a
6099 * smoothed mean difference), then set the retransmit
6100 * timer to smoothed rtt + 4 times the smoothed variance.
6101 * rttvar is stored as fixed point with 4 bits after the
6102 * binary point (scaled by 16). The following is
6103 * equivalent to rfc793 smoothing with an alpha of .75
6104 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
6105 * rfc793's wired-in beta.
6110 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
6111 if ((tp
->t_rttvar
+= delta
) <= 0) {
6114 if (tp
->t_rttbest
== 0 ||
6115 tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
)) {
6116 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
6120 * No rtt measurement yet - use the unsmoothed rtt.
6121 * Set the variance to half the rtt (so our first
6122 * retransmit happens at 3*rtt).
6124 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
6125 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
6129 nstat_route_rtt(tp
->t_inpcb
->inp_route
.ro_rt
, tp
->t_srtt
,
6133 * the retransmit should happen at rtt + 4 * rttvar.
6134 * Because of the way we do the smoothing, srtt and rttvar
6135 * will each average +1/2 tick of bias. When we compute
6136 * the retransmit timer, we want 1/2 tick of rounding and
6137 * 1 extra tick because of +-1/2 tick uncertainty in the
6138 * firing of the timer. The bias will give us exactly the
6139 * 1.5 tick we need. But, because the bias is
6140 * statistical, we have to test that we don't drop below
6141 * the minimum feasible timer (which is 2 ticks).
6143 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
6144 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
,
6145 TCP_ADD_REXMTSLOP(tp
));
6148 * We received an ack for a packet that wasn't retransmitted;
6149 * it is probably safe to discard any error indications we've
6150 * received recently. This isn't quite right, but close enough
6151 * for now (a route might have failed after we sent a segment,
6152 * and the return path might not be symmetrical).
6154 tp
->t_softerror
= 0;
6157 TCP_LOG_RTT_INFO(tp
);
6160 TCP_LOG_RTT_CHANGE(tp
, old_srtt
, old_rttvar
);
6163 static inline unsigned int
6164 tcp_maxmtu(struct rtentry
*rt
)
6166 unsigned int maxmtu
;
6167 int interface_mtu
= 0;
6169 RT_LOCK_ASSERT_HELD(rt
);
6170 interface_mtu
= rt
->rt_ifp
->if_mtu
;
6172 if (rt_key(rt
)->sa_family
== AF_INET
&&
6173 INTF_ADJUST_MTU_FOR_CLAT46(rt
->rt_ifp
)) {
6174 interface_mtu
= IN6_LINKMTU(rt
->rt_ifp
);
6175 /* Further adjust the size for CLAT46 expansion */
6176 interface_mtu
-= CLAT46_HDR_EXPANSION_OVERHD
;
6179 if (rt
->rt_rmx
.rmx_mtu
== 0) {
6180 maxmtu
= interface_mtu
;
6182 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, interface_mtu
);
6189 static inline unsigned int
6190 tcp_maxmtu6(struct rtentry
*rt
)
6192 unsigned int maxmtu
;
6193 struct nd_ifinfo
*ndi
= NULL
;
6195 RT_LOCK_ASSERT_HELD(rt
);
6196 if ((ndi
= ND_IFINFO(rt
->rt_ifp
)) != NULL
&& !ndi
->initialized
) {
6200 lck_mtx_lock(&ndi
->lock
);
6202 if (rt
->rt_rmx
.rmx_mtu
== 0) {
6203 maxmtu
= IN6_LINKMTU(rt
->rt_ifp
);
6205 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, IN6_LINKMTU(rt
->rt_ifp
));
6208 lck_mtx_unlock(&ndi
->lock
);
6216 get_maxmtu(struct rtentry
*rt
)
6218 unsigned int maxmtu
= 0;
6220 RT_LOCK_ASSERT_NOTHELD(rt
);
6224 if (rt_key(rt
)->sa_family
== AF_INET6
) {
6225 maxmtu
= tcp_maxmtu6(rt
);
6227 maxmtu
= tcp_maxmtu(rt
);
6236 * Determine a reasonable value for maxseg size.
6237 * If the route is known, check route for mtu.
6238 * If none, use an mss that can be handled on the outgoing
6239 * interface without forcing IP to fragment; if bigger than
6240 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
6241 * to utilize large mbufs. If no route is found, route has no mtu,
6242 * or the destination isn't local, use a default, hopefully conservative
6243 * size (usually 512 or the default IP max size, but no more than the mtu
6244 * of the interface), as we can't discover anything about intervening
6245 * gateways or networks. We also initialize the congestion/slow start
6246 * window. While looking at the routing entry, we also initialize
6247 * other path-dependent parameters from pre-set or cached values
6248 * in the routing entry.
6250 * Also take into account the space needed for options that we
6251 * send regularly. Make maxseg shorter by that amount to assure
6252 * that we can send maxseg amount of data even when the options
6253 * are present. Store the upper limit of the length of options plus
6256 * NOTE that this routine is only called when we process an incoming
6257 * segment, for outgoing segments only tcp_mssopt is called.
6261 tcp_mss(struct tcpcb
*tp
, int offer
, unsigned int input_ifscope
)
6269 struct rmxp_tao
*taop
;
6270 int origoffer
= offer
;
6271 u_int32_t sb_max_corrected
;
6280 so
= inp
->inp_socket
;
6282 * Nothing left to send after the socket is defunct or TCP is in the closed state
6284 if ((so
->so_state
& SS_DEFUNCT
) || tp
->t_state
== TCPS_CLOSED
) {
6289 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
6290 min_protoh
= isipv6
? sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
)
6291 : sizeof(struct tcpiphdr
);
6293 #define min_protoh (sizeof (struct tcpiphdr))
6298 rt
= tcp_rtlookup6(inp
, input_ifscope
);
6302 rt
= tcp_rtlookup(inp
, input_ifscope
);
6304 isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
6307 tp
->t_maxopd
= tp
->t_maxseg
=
6309 isipv6
? tcp_v6mssdflt
:
6316 * Slower link window correction:
6317 * If a value is specificied for slowlink_wsize use it for
6318 * PPP links believed to be on a serial modem (speed <128Kbps).
6319 * Excludes 9600bps as it is the default value adversized
6320 * by pseudo-devices over ppp.
6322 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
6323 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
6324 tp
->t_flags
|= TF_SLOWLINK
;
6327 taop
= rmx_taop(rt
->rt_rmx
);
6329 * Offer == -1 means that we didn't receive SYN yet,
6330 * use cached value in that case;
6333 offer
= taop
->tao_mssopt
;
6336 * Offer == 0 means that there was no MSS on the SYN segment,
6337 * in this case we use tcp_mssdflt.
6342 isipv6
? tcp_v6mssdflt
:
6347 * Prevent DoS attack with too small MSS. Round up
6348 * to at least minmss.
6350 offer
= max(offer
, tcp_minmss
);
6352 * Sanity check: make sure that maxopd will be large
6353 * enough to allow some data on segments even is the
6354 * all the option space is used (40bytes). Otherwise
6355 * funny things may happen in tcp_output.
6357 offer
= max(offer
, 64);
6359 taop
->tao_mssopt
= offer
;
6362 * While we're here, check if there's an initial rtt
6363 * or rttvar. Convert from the route-table units
6364 * to scaled multiples of the slow timeout timer.
6366 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
) != 0) {
6367 tcp_getrt_rtt(tp
, rt
);
6369 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
6373 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
6375 mss
= tcp_maxmtu(rt
);
6379 // At this point, the mss is just the MTU. Adjust if necessary.
6380 mss
= necp_socket_get_effective_mtu(inp
, mss
);
6385 if (rt
->rt_rmx
.rmx_mtu
== 0) {
6389 mss
= min(mss
, tcp_v6mssdflt
);
6394 mss
= min(mss
, tcp_mssdflt
);
6398 mss
= min(mss
, offer
);
6400 * maxopd stores the maximum length of data AND options
6401 * in a segment; maxseg is the amount of data in a normal
6402 * segment. We need to store this value (maxopd) apart
6403 * from maxseg, because now every segment carries options
6404 * and thus we normally have somewhat less data in segments.
6409 * origoffer==-1 indicates, that no segments were received yet.
6410 * In this case we just guess.
6412 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_NOOPT
)) == TF_REQ_TSTMP
&&
6414 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
)) {
6415 mss
-= TCPOLEN_TSTAMP_APPA
;
6419 mss
-= mptcp_adj_mss(tp
, FALSE
);
6424 * Calculate corrected value for sb_max; ensure to upgrade the
6425 * numerator for large sb_max values else it will overflow.
6427 sb_max_corrected
= (sb_max
* (u_int64_t
)MCLBYTES
) / (MSIZE
+ MCLBYTES
);
6430 * If there's a pipesize (ie loopback), change the socket
6431 * buffer to that size only if it's bigger than the current
6432 * sockbuf size. Make the socket buffers an integral
6433 * number of mss units; if the mss is larger than
6434 * the socket buffer, decrease the mss.
6437 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
6438 if (bufsize
< so
->so_snd
.sb_hiwat
)
6440 bufsize
= so
->so_snd
.sb_hiwat
;
6441 if (bufsize
< mss
) {
6444 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
6445 if (bufsize
> sb_max_corrected
) {
6446 bufsize
= sb_max_corrected
;
6448 (void)sbreserve(&so
->so_snd
, bufsize
);
6452 ASSERT(tp
->t_maxseg
);
6455 * Update MSS using recommendation from link status report. This is
6458 tcp_update_mss_locked(so
, ifp
);
6461 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
6462 if (bufsize
< so
->so_rcv
.sb_hiwat
)
6464 bufsize
= so
->so_rcv
.sb_hiwat
;
6465 if (bufsize
> mss
) {
6466 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
6467 if (bufsize
> sb_max_corrected
) {
6468 bufsize
= sb_max_corrected
;
6470 (void)sbreserve(&so
->so_rcv
, bufsize
);
6473 set_tcp_stream_priority(so
);
6475 if (rt
->rt_rmx
.rmx_ssthresh
) {
6477 * There's some sort of gateway or interface
6478 * buffer limit on the path. Use this to set
6479 * slow-start threshold, but set the threshold to
6480 * no less than 2*mss.
6482 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
6483 tcpstat
.tcps_usedssthresh
++;
6485 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
6489 * Set the slow-start flight size depending on whether this
6490 * is a local network or not.
6492 if (CC_ALGO(tp
)->cwnd_init
!= NULL
) {
6493 CC_ALGO(tp
)->cwnd_init(tp
);
6496 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CWND_INIT
);
6498 /* Route locked during lookup above */
6503 * Determine the MSS option to send on an outgoing SYN.
6506 tcp_mssopt(struct tcpcb
*tp
)
6516 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
6517 min_protoh
= isipv6
? sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
)
6518 : sizeof(struct tcpiphdr
);
6520 #define min_protoh (sizeof (struct tcpiphdr))
6525 rt
= tcp_rtlookup6(tp
->t_inpcb
, IFSCOPE_NONE
);
6528 rt
= tcp_rtlookup(tp
->t_inpcb
, IFSCOPE_NONE
);
6532 isipv6
? tcp_v6mssdflt
:
6537 * Slower link window correction:
6538 * If a value is specificied for slowlink_wsize use it for PPP links
6539 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
6540 * it is the default value adversized by pseudo-devices over ppp.
6542 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
6543 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
6544 tp
->t_flags
|= TF_SLOWLINK
;
6548 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
6550 mss
= tcp_maxmtu(rt
);
6552 /* Route locked during lookup above */
6556 // At this point, the mss is just the MTU. Adjust if necessary.
6557 mss
= necp_socket_get_effective_mtu(tp
->t_inpcb
, mss
);
6560 return mss
- min_protoh
;
6564 * On a partial ack arrives, force the retransmission of the
6565 * next unacknowledged segment. Do not clear tp->t_dupacks.
6566 * By setting snd_nxt to th_ack, this forces retransmission timer to
6570 tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
6572 tcp_seq onxt
= tp
->snd_nxt
;
6573 u_int32_t ocwnd
= tp
->snd_cwnd
;
6574 tp
->t_timer
[TCPT_REXMT
] = 0;
6575 tp
->t_timer
[TCPT_PTO
] = 0;
6577 tp
->snd_nxt
= th
->th_ack
;
6579 * Set snd_cwnd to one segment beyond acknowledged offset
6580 * (tp->snd_una has not yet been updated when this function
6583 tp
->snd_cwnd
= tp
->t_maxseg
+ BYTES_ACKED(th
, tp
);
6584 tp
->t_flags
|= TF_ACKNOW
;
6585 (void) tcp_output(tp
);
6586 tp
->snd_cwnd
= ocwnd
;
6587 if (SEQ_GT(onxt
, tp
->snd_nxt
)) {
6591 * Partial window deflation. Relies on fact that tp->snd_una
6594 if (tp
->snd_cwnd
> BYTES_ACKED(th
, tp
)) {
6595 tp
->snd_cwnd
-= BYTES_ACKED(th
, tp
);
6599 tp
->snd_cwnd
+= tp
->t_maxseg
;
6603 * Drop a random TCP connection that hasn't been serviced yet and
6604 * is eligible for discard. There is a one in qlen chance that
6605 * we will return a null, saying that there are no dropable
6606 * requests. In this case, the protocol specific code should drop
6607 * the new request. This insures fairness.
6609 * The listening TCP socket "head" must be locked
6612 tcp_dropdropablreq(struct socket
*head
)
6614 struct socket
*so
, *sonext
;
6615 unsigned int i
, j
, qlen
;
6616 static u_int32_t rnd
= 0;
6617 static u_int64_t old_runtime
;
6618 static unsigned int cur_cnt
, old_cnt
;
6620 struct inpcb
*inp
= NULL
;
6623 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
6627 if (TAILQ_EMPTY(&head
->so_incomp
)) {
6631 so_acquire_accept_list(head
, NULL
);
6632 socket_unlock(head
, 0);
6635 * Check if there is any socket in the incomp queue
6636 * that is closed because of a reset from the peer and is
6637 * waiting to be garbage collected. If so, pick that as
6640 TAILQ_FOREACH_SAFE(so
, &head
->so_incomp
, so_list
, sonext
) {
6641 inp
= sotoinpcb(so
);
6642 tp
= intotcpcb(inp
);
6643 if (tp
!= NULL
&& tp
->t_state
== TCPS_CLOSED
&&
6644 so
->so_head
!= NULL
&&
6645 (so
->so_state
& (SS_INCOMP
| SS_CANTSENDMORE
| SS_CANTRCVMORE
)) ==
6646 (SS_INCOMP
| SS_CANTSENDMORE
| SS_CANTRCVMORE
)) {
6648 * The listen socket is already locked but we
6649 * can lock this socket here without lock ordering
6650 * issues because it is in the incomp queue and
6651 * is not visible to others.
6653 if (socket_try_lock(so
)) {
6662 so
= TAILQ_FIRST(&head
->so_incomp
);
6664 now_sec
= net_uptime();
6665 if ((i
= (now_sec
- old_runtime
)) != 0) {
6666 old_runtime
= now_sec
;
6667 old_cnt
= cur_cnt
/ i
;
6671 qlen
= head
->so_incqlen
;
6673 rnd
= RandomULong();
6676 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
6677 rnd
= (314159 * rnd
+ 66329) & 0xffff;
6678 j
= ((qlen
+ 1) * rnd
) >> 16;
6681 so
= TAILQ_NEXT(so
, so_list
);
6684 /* Find a connection that is not already closing (or being served) */
6686 inp
= (struct inpcb
*)so
->so_pcb
;
6688 sonext
= TAILQ_NEXT(so
, so_list
);
6690 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
6692 * Avoid the issue of a socket being accepted
6693 * by one input thread and being dropped by
6694 * another input thread. If we can't get a hold
6695 * on this mutex, then grab the next socket in
6698 if (socket_try_lock(so
)) {
6700 if ((so
->so_usecount
== 2) &&
6701 (so
->so_state
& SS_INCOMP
) &&
6702 !(so
->so_flags
& SOF_INCOMP_INPROGRESS
)) {
6706 * don't use if being accepted or
6707 * used in any other way
6709 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6710 socket_unlock(so
, 1);
6714 * do not try to lock the inp in
6715 * in_pcb_checkstate because the lock
6716 * is already held in some other thread.
6717 * Only drop the inp_wntcnt reference.
6719 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6725 socket_lock(head
, 0);
6726 so_release_accept_list(head
);
6730 /* Makes sure socket is still in the right state to be discarded */
6732 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
6733 socket_unlock(so
, 1);
6734 socket_lock(head
, 0);
6735 so_release_accept_list(head
);
6740 if (so
->so_usecount
!= 2 || !(so
->so_state
& SS_INCOMP
)) {
6741 /* do not discard: that socket is being accepted */
6742 socket_unlock(so
, 1);
6743 socket_lock(head
, 0);
6744 so_release_accept_list(head
);
6748 socket_lock(head
, 0);
6749 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
6752 so
->so_state
&= ~SS_INCOMP
;
6753 so
->so_flags
|= SOF_OVERFLOW
;
6755 so_release_accept_list(head
);
6756 socket_unlock(head
, 0);
6758 socket_lock_assert_owned(so
);
6762 if (inp
->inp_wantcnt
> 0 && inp
->inp_wantcnt
!= WNT_STOPUSING
) {
6764 * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE
6765 * doesn't require a lock, it could have happened while
6766 * we are holding the lock. This pcb will have to
6767 * be garbage collected later.
6768 * Release the reference held for so_incomp queue
6770 VERIFY(so
->so_usecount
> 0);
6772 socket_unlock(so
, 1);
6775 * Unlock this socket and leave the reference on.
6776 * We need to acquire the pcbinfo lock in order to
6777 * fully dispose it off
6779 socket_unlock(so
, 0);
6781 lck_rw_lock_exclusive(tcbinfo
.ipi_lock
);
6784 /* Release the reference held for so_incomp queue */
6785 VERIFY(so
->so_usecount
> 0);
6788 if (so
->so_usecount
!= 1 ||
6789 (inp
->inp_wantcnt
> 0 &&
6790 inp
->inp_wantcnt
!= WNT_STOPUSING
)) {
6792 * There is an extra wantcount or usecount
6793 * that must have been added when the socket
6794 * was unlocked. This socket will have to be
6795 * garbage collected later
6797 socket_unlock(so
, 1);
6799 /* Drop the reference held for this function */
6800 VERIFY(so
->so_usecount
> 0);
6805 lck_rw_done(tcbinfo
.ipi_lock
);
6807 tcpstat
.tcps_drops
++;
6809 socket_lock(head
, 0);
6813 /* Set background congestion control on a socket */
6815 tcp_set_background_cc(struct socket
*so
)
6817 tcp_set_new_cc(so
, TCP_CC_ALGO_BACKGROUND_INDEX
);
6820 /* Set foreground congestion control on a socket */
6822 tcp_set_foreground_cc(struct socket
*so
)
6824 if (tcp_use_newreno
) {
6825 tcp_set_new_cc(so
, TCP_CC_ALGO_NEWRENO_INDEX
);
6827 tcp_set_new_cc(so
, TCP_CC_ALGO_CUBIC_INDEX
);
6832 tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
)
6834 struct inpcb
*inp
= sotoinpcb(so
);
6835 struct tcpcb
*tp
= intotcpcb(inp
);
6836 u_char old_cc_index
= 0;
6837 if (tp
->tcp_cc_index
!= cc_index
) {
6838 old_cc_index
= tp
->tcp_cc_index
;
6840 if (CC_ALGO(tp
)->cleanup
!= NULL
) {
6841 CC_ALGO(tp
)->cleanup(tp
);
6843 tp
->tcp_cc_index
= cc_index
;
6845 tcp_cc_allocate_state(tp
);
6847 if (CC_ALGO(tp
)->switch_to
!= NULL
) {
6848 CC_ALGO(tp
)->switch_to(tp
, old_cc_index
);
6851 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CHANGE_ALGO
);
6856 tcp_set_recv_bg(struct socket
*so
)
6858 if (!IS_TCP_RECV_BG(so
)) {
6859 so
->so_flags1
|= SOF1_TRAFFIC_MGT_TCP_RECVBG
;
6862 /* Unset Large Receive Offload on background sockets */
6863 so_set_lro(so
, SO_TC_BK
);
6867 tcp_clear_recv_bg(struct socket
*so
)
6869 if (IS_TCP_RECV_BG(so
)) {
6870 so
->so_flags1
&= ~(SOF1_TRAFFIC_MGT_TCP_RECVBG
);
6874 * Set/unset use of Large Receive Offload depending on
6877 so_set_lro(so
, so
->so_traffic_class
);
6881 inp_fc_unthrottle_tcp(struct inpcb
*inp
)
6883 struct tcpcb
*tp
= inp
->inp_ppcb
;
6885 * Back off the slow-start threshold and enter
6886 * congestion avoidance phase
6888 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
6889 CC_ALGO(tp
)->pre_fr(tp
);
6892 tp
->snd_cwnd
= tp
->snd_ssthresh
;
6893 tp
->t_flagsext
&= ~TF_CWND_NONVALIDATED
;
6895 * Restart counting for ABC as we changed the
6896 * congestion window just now.
6898 tp
->t_bytes_acked
= 0;
6900 /* Reset retransmit shift as we know that the reason
6901 * for delay in sending a packet is due to flow
6902 * control on the outgoing interface. There is no need
6903 * to backoff retransmit timer.
6905 TCP_RESET_REXMT_STATE(tp
);
6908 * Start the output stream again. Since we are
6909 * not retransmitting data, do not reset the
6910 * retransmit timer or rtt calculation.
6916 tcp_getstat SYSCTL_HANDLER_ARGS
6918 #pragma unused(oidp, arg1, arg2)
6921 struct tcpstat
*stat
;
6923 #if !CONFIG_EMBEDDED
6924 struct tcpstat zero_stat
;
6926 if (tcp_disable_access_to_stats
&&
6927 !kauth_cred_issuser(kauth_cred_get())) {
6928 bzero(&zero_stat
, sizeof(zero_stat
));
6932 #endif /* !CONFIG_EMBEDDED */
6934 if (req
->oldptr
== 0) {
6935 req
->oldlen
= (size_t)sizeof(struct tcpstat
);
6938 error
= SYSCTL_OUT(req
, stat
, MIN(sizeof(tcpstat
), req
->oldlen
));
6944 * Checksum extended TCP header and data.
6947 tcp_input_checksum(int af
, struct mbuf
*m
, struct tcphdr
*th
, int off
, int tlen
)
6949 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
6953 struct ip
*ip
= mtod(m
, struct ip
*);
6954 struct ipovly
*ipov
= (struct ipovly
*)ip
;
6956 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
) {
6960 /* ip_stripoptions() must have been called before we get here */
6961 ASSERT((ip
->ip_hl
<< 2) == sizeof(*ip
));
6963 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6964 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6965 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6966 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6967 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6969 uint32_t sum
= m
->m_pkthdr
.csum_rx_val
;
6970 uint32_t start
= m
->m_pkthdr
.csum_rx_start
;
6971 int32_t trailer
= (m_pktlen(m
) - (off
+ tlen
));
6974 * Perform 1's complement adjustment of octets
6975 * that got included/excluded in the hardware-
6976 * calculated checksum value. Ignore cases
6977 * where the value already includes the entire
6978 * IP header span, as the sum for those octets
6979 * would already be 0 by the time we get here;
6980 * IP has already performed its header checksum
6981 * checks. If we do need to adjust, restore
6982 * the original fields in the IP header when
6983 * computing the adjustment value. Also take
6984 * care of any trailing bytes and subtract out
6985 * their partial sum.
6987 ASSERT(trailer
>= 0);
6988 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6989 ((start
!= 0 && start
!= off
) || trailer
)) {
6990 uint32_t swbytes
= (uint32_t)trailer
;
6993 ip
->ip_len
+= sizeof(*ip
);
6994 #if BYTE_ORDER != BIG_ENDIAN
6997 #endif /* BYTE_ORDER != BIG_ENDIAN */
6999 /* callee folds in sum */
7000 sum
= m_adj_sum16(m
, start
, off
,
7003 swbytes
+= (off
- start
);
7005 swbytes
+= (start
- off
);
7009 #if BYTE_ORDER != BIG_ENDIAN
7012 #endif /* BYTE_ORDER != BIG_ENDIAN */
7013 ip
->ip_len
-= sizeof(*ip
);
7017 tcp_in_cksum_stats(swbytes
);
7024 /* callee folds in sum */
7025 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
7027 sum
+ htonl(tlen
+ IPPROTO_TCP
));
7029 th
->th_sum
^= 0xffff;
7035 bcopy(ipov
->ih_x1
, b
, sizeof(ipov
->ih_x1
));
7036 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
7037 ip_sum
= ipov
->ih_len
;
7038 ipov
->ih_len
= (u_short
)tlen
;
7039 #if BYTE_ORDER != BIG_ENDIAN
7040 HTONS(ipov
->ih_len
);
7042 len
= sizeof(struct ip
) + tlen
;
7043 th
->th_sum
= in_cksum(m
, len
);
7044 bcopy(b
, ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
7045 ipov
->ih_len
= ip_sum
;
7047 tcp_in_cksum_stats(len
);
7053 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
7055 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
) {
7059 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
7060 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
7061 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
7062 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
7063 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
7065 uint32_t sum
= m
->m_pkthdr
.csum_rx_val
;
7066 uint32_t start
= m
->m_pkthdr
.csum_rx_start
;
7067 int32_t trailer
= (m_pktlen(m
) - (off
+ tlen
));
7070 * Perform 1's complement adjustment of octets
7071 * that got included/excluded in the hardware-
7072 * calculated checksum value. Also take care
7073 * of any trailing bytes and subtract out their
7076 ASSERT(trailer
>= 0);
7077 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
7078 (start
!= off
|| trailer
!= 0)) {
7079 uint16_t s
= 0, d
= 0;
7080 uint32_t swbytes
= (uint32_t)trailer
;
7082 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
7083 s
= ip6
->ip6_src
.s6_addr16
[1];
7084 ip6
->ip6_src
.s6_addr16
[1] = 0;
7086 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
7087 d
= ip6
->ip6_dst
.s6_addr16
[1];
7088 ip6
->ip6_dst
.s6_addr16
[1] = 0;
7091 /* callee folds in sum */
7092 sum
= m_adj_sum16(m
, start
, off
,
7095 swbytes
+= (off
- start
);
7097 swbytes
+= (start
- off
);
7100 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
7101 ip6
->ip6_src
.s6_addr16
[1] = s
;
7103 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
7104 ip6
->ip6_dst
.s6_addr16
[1] = d
;
7108 tcp_in6_cksum_stats(swbytes
);
7115 th
->th_sum
= in6_pseudo(
7116 &ip6
->ip6_src
, &ip6
->ip6_dst
,
7117 sum
+ htonl(tlen
+ IPPROTO_TCP
));
7119 th
->th_sum
^= 0xffff;
7121 tcp_in6_cksum_stats(tlen
);
7122 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, off
, tlen
);
7132 if (th
->th_sum
!= 0) {
7133 tcpstat
.tcps_rcvbadsum
++;
7134 IF_TCP_STATINC(ifp
, badformat
);
7142 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_STATS
, stats
,
7143 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, tcp_getstat
,
7144 "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
7147 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
7149 #pragma unused(arg1, arg2)
7151 int error
, val
= tcprexmtthresh
;
7153 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
7154 if (error
|| !req
->newptr
) {
7159 * Constrain the number of duplicate ACKs
7160 * to consider for TCP fast retransmit
7164 if (val
< 2 || val
> 3) {
7168 tcprexmtthresh
= val
;
7173 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, rexmt_thresh
, CTLTYPE_INT
| CTLFLAG_RW
|
7174 CTLFLAG_LOCKED
, &tcprexmtthresh
, 0, &sysctl_rexmtthresh
, "I",
7175 "Duplicate ACK Threshold for Fast Retransmit");