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
, autorcvbufmax
,
240 CTLFLAG_RW
| CTLFLAG_LOCKED
, u_int32_t
, tcp_autorcvbuf_max
, 512 * 1024,
241 "Maximum receive socket buffer size");
247 #endif /* !CONFIG_EMBEDDED */
248 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
249 &sw_lro
, 0, "Used to coalesce TCP packets");
252 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lrodbg
,
253 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lrodebug
, 0,
254 "Used to debug SW LRO");
257 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro_startcnt
,
258 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lro_start
, 0,
259 "Segments for starting LRO computed as power of 2");
261 int limited_txmt
= 1;
264 int tcp_dsack_enable
= 1;
266 #if (DEVELOPMENT || DEBUG)
267 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, limited_transmit
,
268 CTLFLAG_RW
| CTLFLAG_LOCKED
, &limited_txmt
, 0,
269 "Enable limited transmit");
271 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, early_rexmt
,
272 CTLFLAG_RW
| CTLFLAG_LOCKED
, &early_rexmt
, 0,
273 "Enable Early Retransmit");
275 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_ackadv
,
276 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sack_ackadv
, 0,
277 "Use SACK with cumulative ack advancement as a dupack");
279 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, dsack_enable
,
280 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_dsack_enable
, 0,
281 "use DSACK TCP option to report duplicate segments");
283 #endif /* (DEVELOPMENT || DEBUG) */
284 int tcp_disable_access_to_stats
= 1;
285 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, disable_access_to_stats
,
286 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_disable_access_to_stats
, 0,
287 "Disable access to tcpstat");
289 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, challengeack_limit
,
290 CTLFLAG_RW
| CTLFLAG_LOCKED
, uint32_t, tcp_challengeack_limit
, 10,
291 "Maximum number of challenge ACKs per connection per second");
293 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, do_rfc5961
,
294 CTLFLAG_RW
| CTLFLAG_LOCKED
, static int, tcp_do_rfc5961
, 1,
295 "Enable/Disable full RFC 5961 compliance");
297 extern int tcp_acc_iaj_high
;
298 extern int tcp_acc_iaj_react_limit
;
300 int tcprexmtthresh
= 3;
303 struct timeval tcp_uptime
; /* uptime when tcp_now was last updated */
304 lck_spin_t
*tcp_uptime_lock
; /* Used to sychronize updates to tcp_now */
306 struct inpcbhead tcb
;
307 #define tcb6 tcb /* for KAME src sync over BSD*'s */
308 struct inpcbinfo tcbinfo
;
310 static void tcp_dooptions(struct tcpcb
*, u_char
*, int, struct tcphdr
*,
312 static void tcp_finalize_options(struct tcpcb
*, struct tcpopt
*, unsigned int);
313 static void tcp_pulloutofband(struct socket
*,
314 struct tcphdr
*, struct mbuf
*, int);
315 static void tcp_xmit_timer(struct tcpcb
*, int, u_int32_t
, tcp_seq
);
316 static inline unsigned int tcp_maxmtu(struct rtentry
*);
317 static inline int tcp_stretch_ack_enable(struct tcpcb
*tp
, int thflags
);
318 static inline void tcp_adaptive_rwtimo_check(struct tcpcb
*, int);
321 static inline void update_iaj_state(struct tcpcb
*tp
, uint32_t tlen
,
323 void compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
);
324 static void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
);
325 #endif /* TRAFFIC_MGT */
328 static inline unsigned int tcp_maxmtu6(struct rtentry
*);
331 unsigned int get_maxmtu(struct rtentry
*);
333 static void tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sb
,
334 struct tcpopt
*to
, u_int32_t tlen
, u_int32_t rcvbuf_max
);
335 void tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sb
);
336 static void tcp_sbsnd_trim(struct sockbuf
*sbsnd
);
337 static inline void tcp_sbrcv_tstmp_check(struct tcpcb
*tp
);
338 static inline void tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sb
,
339 u_int32_t newsize
, u_int32_t idealsize
, u_int32_t rcvbuf_max
);
340 static void tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
);
341 static void tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
,
343 static void tcp_early_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
);
344 static void tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
,
347 * Constants used for resizing receive socket buffer
348 * when timestamps are not supported
350 #define TCPTV_RCVNOTS_QUANTUM 100
351 #define TCP_RCVNOTS_BYTELEVEL 204800
354 * Constants used for limiting early retransmits
357 #define TCP_EARLY_REXMT_WIN (60 * TCP_RETRANSHZ) /* 60 seconds */
358 #define TCP_EARLY_REXMT_LIMIT 10
360 extern void ipfwsyslog( int level
, const char *format
, ...);
361 extern int fw_verbose
;
364 extern void ipfw_stealth_stats_incr_tcp(void);
366 #define log_in_vain_log( a ) { \
367 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
369 } else if ( (log_in_vain == 4 ) && (fw_verbose == 2)) { \
370 ipfw_stealth_stats_incr_tcp(); \
375 #define log_in_vain_log( a ) { log a; }
378 int tcp_rcvunackwin
= TCPTV_UNACKWIN
;
379 int tcp_maxrcvidle
= TCPTV_MAXRCVIDLE
;
380 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, rcvsspktcnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
381 int, tcp_rcvsspktcnt
, TCP_RCV_SS_PKTCOUNT
, "packets to be seen before receiver stretches acks");
383 #define DELAY_ACK(tp, th) \
384 (CC_ALGO(tp)->delay_ack != NULL && CC_ALGO(tp)->delay_ack(tp, th))
386 static int tcp_dropdropablreq(struct socket
*head
);
387 static void tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
);
388 static void update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
);
389 void tcp_set_background_cc(struct socket
*so
);
390 void tcp_set_foreground_cc(struct socket
*so
);
391 static void tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
);
392 static void tcp_bwmeas_check(struct tcpcb
*tp
);
396 reset_acc_iaj(struct tcpcb
*tp
)
403 update_iaj_state(struct tcpcb
*tp
, uint32_t size
, int rst_size
)
408 if (tp
->iaj_size
== 0 || size
>= tp
->iaj_size
) {
410 tp
->iaj_rcv_ts
= tcp_now
;
411 tp
->iaj_small_pkt
= 0;
415 /* For every 32 bit unsigned integer(v), this function will find the
416 * largest integer n such that (n*n <= v). This takes at most 16 iterations
417 * irrespective of the value of v and does not involve multiplications.
420 isqrt(unsigned int val
)
422 unsigned int sqrt_cache
[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
423 unsigned int temp
, g
= 0, b
= 0x8000, bshft
= 15;
425 for (g
= 0; g
<= 10; ++g
) {
426 if (sqrt_cache
[g
] > val
) {
429 } else if (sqrt_cache
[g
] == val
) {
435 temp
= (((g
<< 1) + b
) << (bshft
--));
441 } while (b
> 0 && val
> 0);
447 * With LRO, roughly estimate the inter arrival time between
448 * each sub coalesced packet as an average. Count the delay
449 * cur_iaj to be the delay between the last packet received
450 * and the first packet of the LRO stream. Due to round off errors
451 * cur_iaj may be the same as lro_delay_factor. Averaging has
452 * round off errors too. lro_delay_factor may be close to 0
453 * in steady state leading to lower values fed to compute_iaj_meat.
456 compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
)
458 uint32_t cur_iaj
= tcp_now
- tp
->iaj_rcv_ts
;
459 uint32_t timediff
= 0;
461 if (cur_iaj
>= lro_delay_factor
) {
462 cur_iaj
= cur_iaj
- lro_delay_factor
;
465 compute_iaj_meat(tp
, cur_iaj
);
473 timediff
= lro_delay_factor
/ nlropkts
;
475 while (nlropkts
> 0) {
476 compute_iaj_meat(tp
, timediff
);
483 compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
)
485 /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds,
486 * throttle the receive window to a minimum of MIN_IAJ_WIN packets
488 #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
489 #define IAJ_DIV_SHIFT 4
490 #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1))
492 uint32_t allowed_iaj
, acc_iaj
= 0;
497 cur_iaj_dev
= (cur_iaj
- tp
->avg_iaj
);
499 /* Allow a jitter of "allowed_iaj" milliseconds. Some connections
500 * may have a constant jitter more than that. We detect this by
501 * using standard deviation.
503 allowed_iaj
= tp
->avg_iaj
+ tp
->std_dev_iaj
;
504 if (allowed_iaj
< tcp_allowed_iaj
) {
505 allowed_iaj
= tcp_allowed_iaj
;
508 /* Initially when the connection starts, the senders congestion
509 * window is small. During this period we avoid throttling a
510 * connection because we do not have a good starting point for
511 * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over
512 * the first few packets.
514 if (tp
->iaj_pktcnt
> IAJ_IGNORE_PKTCNT
) {
515 if (cur_iaj
<= allowed_iaj
) {
516 if (tp
->acc_iaj
>= 2) {
517 acc_iaj
= tp
->acc_iaj
- 2;
522 acc_iaj
= tp
->acc_iaj
+ (cur_iaj
- allowed_iaj
);
525 if (acc_iaj
> MAX_ACC_IAJ
) {
526 acc_iaj
= MAX_ACC_IAJ
;
528 tp
->acc_iaj
= acc_iaj
;
531 /* Compute weighted average where the history has a weight of
532 * 15 out of 16 and the current value has a weight of 1 out of 16.
533 * This will make the short-term measurements have more weight.
535 * The addition of 8 will help to round-up the value
536 * instead of round-down
538 tp
->avg_iaj
= (((tp
->avg_iaj
<< IAJ_DIV_SHIFT
) - tp
->avg_iaj
)
539 + cur_iaj
+ IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
541 /* Compute Root-mean-square of deviation where mean is a weighted
542 * average as described above.
544 temp
= tp
->std_dev_iaj
* tp
->std_dev_iaj
;
545 mean
= (((temp
<< IAJ_DIV_SHIFT
) - temp
)
546 + (cur_iaj_dev
* cur_iaj_dev
)
547 + IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
549 tp
->std_dev_iaj
= isqrt(mean
);
551 DTRACE_TCP3(iaj
, struct tcpcb
*, tp
, uint32_t, cur_iaj
,
552 uint32_t, allowed_iaj
);
556 #endif /* TRAFFIC_MGT */
559 * Perform rate limit check per connection per second
560 * tp->t_challengeack_last is the last_time diff was greater than 1sec
561 * tp->t_challengeack_count is the number of ACKs sent (within 1sec)
562 * Return TRUE if we shouldn't send the ACK due to rate limitation
563 * Return FALSE if it is still ok to send challenge ACK
566 tcp_is_ack_ratelimited(struct tcpcb
*tp
)
568 boolean_t ret
= TRUE
;
569 uint32_t now
= tcp_now
;
572 diff
= timer_diff(now
, 0, tp
->t_challengeack_last
, 0);
573 /* If it is first time or diff > 1000ms,
574 * update the challengeack_last and reset the
575 * current count of ACKs
577 if (tp
->t_challengeack_last
== 0 || diff
>= 1000) {
578 tp
->t_challengeack_last
= now
;
579 tp
->t_challengeack_count
= 0;
581 } else if (tp
->t_challengeack_count
< tcp_challengeack_limit
) {
585 /* Careful about wrap-around */
586 if (ret
== FALSE
&& (tp
->t_challengeack_count
+ 1 > 0)) {
587 tp
->t_challengeack_count
++;
593 /* Check if enough amount of data has been acknowledged since
594 * bw measurement was started
597 tcp_bwmeas_check(struct tcpcb
*tp
)
599 int32_t bw_meas_bytes
;
600 uint32_t bw
, bytes
, elapsed_time
;
602 if (SEQ_LEQ(tp
->snd_una
, tp
->t_bwmeas
->bw_start
)) {
606 bw_meas_bytes
= tp
->snd_una
- tp
->t_bwmeas
->bw_start
;
607 if ((tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) &&
608 bw_meas_bytes
>= (int32_t)(tp
->t_bwmeas
->bw_size
)) {
609 bytes
= bw_meas_bytes
;
610 elapsed_time
= tcp_now
- tp
->t_bwmeas
->bw_ts
;
611 if (elapsed_time
> 0) {
612 bw
= bytes
/ elapsed_time
;
614 if (tp
->t_bwmeas
->bw_sndbw
> 0) {
615 tp
->t_bwmeas
->bw_sndbw
=
616 (((tp
->t_bwmeas
->bw_sndbw
<< 3)
617 - tp
->t_bwmeas
->bw_sndbw
)
620 tp
->t_bwmeas
->bw_sndbw
= bw
;
623 /* Store the maximum value */
624 if (tp
->t_bwmeas
->bw_sndbw_max
== 0) {
625 tp
->t_bwmeas
->bw_sndbw_max
=
626 tp
->t_bwmeas
->bw_sndbw
;
628 tp
->t_bwmeas
->bw_sndbw_max
=
629 max(tp
->t_bwmeas
->bw_sndbw
,
630 tp
->t_bwmeas
->bw_sndbw_max
);
634 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
639 tcp_reass(struct tcpcb
*tp
, struct tcphdr
*th
, int *tlenp
, struct mbuf
*m
,
640 struct ifnet
*ifp
, int *dowakeup
)
643 struct tseg_qent
*p
= NULL
;
644 struct tseg_qent
*nq
;
645 struct tseg_qent
*te
= NULL
;
646 struct inpcb
*inp
= tp
->t_inpcb
;
647 struct socket
*so
= inp
->inp_socket
;
649 struct mbuf
*oodata
= NULL
;
652 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
653 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
654 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
655 boolean_t dsack_set
= FALSE
;
658 * Call with th==0 after become established to
659 * force pre-ESTABLISHED data up to user socket.
666 * If the reassembly queue already has entries or if we are going
667 * to add a new one, then the connection has reached a loss state.
668 * Reset the stretch-ack algorithm at this point.
670 tcp_reset_stretch_ack(tp
);
673 if (tp
->acc_iaj
> 0) {
676 #endif /* TRAFFIC_MGT */
679 * Limit the number of segments in the reassembly queue to prevent
680 * holding on to too many segments (and thus running out of mbufs).
681 * Make sure to let the missing segment through which caused this
682 * queue. Always keep one global queue entry spare to be able to
683 * process the missing segment.
685 qlimit
= min(max(100, so
->so_rcv
.sb_hiwat
>> 10),
686 (TCP_AUTORCVBUF_MAX(ifp
) >> 10));
687 if (th
->th_seq
!= tp
->rcv_nxt
&&
688 (tp
->t_reassqlen
+ 1) >= qlimit
) {
689 tcp_reass_overflows
++;
690 tcpstat
.tcps_rcvmemdrop
++;
696 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
697 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
699 tcpstat
.tcps_rcvmemdrop
++;
706 * Find a segment which begins after this one does.
708 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
709 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
)) {
716 * If there is a preceding segment, it may provide some of
717 * our data already. If so, drop the data from the incoming
718 * segment. If it provides all of our data, drop us.
722 /* conversion to int (in i) handles seq wraparound */
723 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
725 if (TCP_DSACK_ENABLED(tp
) && i
> 1) {
727 * Note duplicate data sequnce numbers
728 * to report in DSACK option
730 tp
->t_dsack_lseq
= th
->th_seq
;
731 tp
->t_dsack_rseq
= th
->th_seq
+
735 * Report only the first part of partial/
736 * non-contiguous duplicate sequence space
741 tcpstat
.tcps_rcvduppack
++;
742 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
744 nstat_route_rx(inp
->inp_route
.ro_rt
,
746 NSTAT_RX_FLAG_DUPLICATE
);
747 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
749 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
751 tp
->t_stat
.rxduplicatebytes
+= *tlenp
;
752 inp_set_activity_bitmap(inp
);
755 zfree(tcp_reass_zone
, te
);
759 * Try to present any queued data
760 * at the left window edge to the user.
761 * This is needed after the 3-WHS
772 tcpstat
.tcps_rcvoopack
++;
773 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
775 nstat_route_rx(inp
->inp_route
.ro_rt
, 1, *tlenp
,
776 NSTAT_RX_FLAG_OUT_OF_ORDER
);
777 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
778 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, *tlenp
);
779 tp
->t_stat
.rxoutoforderbytes
+= *tlenp
;
780 inp_set_activity_bitmap(inp
);
784 * While we overlap succeeding segments trim them or,
785 * if they are completely covered, dequeue them.
788 int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
794 * Report only the first part of partial/non-contiguous
795 * duplicate segment in dsack option. The variable
796 * dsack_set will be true if a previous entry has some of
797 * the duplicate sequence space.
799 if (TCP_DSACK_ENABLED(tp
) && i
> 1 && !dsack_set
) {
800 if (tp
->t_dsack_lseq
== 0) {
801 tp
->t_dsack_lseq
= q
->tqe_th
->th_seq
;
803 tp
->t_dsack_lseq
+ min(i
, q
->tqe_len
);
806 * this segment overlaps data in multple
807 * entries in the reassembly queue, move
808 * the right sequence number further.
811 tp
->t_dsack_rseq
+ min(i
, q
->tqe_len
);
814 if (i
< q
->tqe_len
) {
815 q
->tqe_th
->th_seq
+= i
;
821 nq
= LIST_NEXT(q
, tqe_q
);
822 LIST_REMOVE(q
, tqe_q
);
824 zfree(tcp_reass_zone
, q
);
829 /* Insert the new segment queue entry into place. */
832 te
->tqe_len
= *tlenp
;
835 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
837 LIST_INSERT_AFTER(p
, te
, tqe_q
);
841 * New out-of-order data exists, and is pointed to by
842 * queue entry te. Set copy_oodata to 1 so out-of-order data
843 * can be copied off to sockbuf after in-order data
846 if (!(so
->so_state
& SS_CANTRCVMORE
)) {
852 * Present data to user, advancing rcv_nxt through
853 * completed sequence space.
855 if (!TCPS_HAVEESTABLISHED(tp
->t_state
)) {
858 q
= LIST_FIRST(&tp
->t_segq
);
859 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
) {
860 /* Stop using LRO once out of order packets arrive */
861 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
862 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
863 th
->th_dport
, th
->th_sport
);
864 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
868 * continue processing if out-of-order data
871 if (q
&& (so
->so_flags
& SOF_ENABLE_MSGS
)) {
872 goto msg_unordered_delivery
;
879 * If there is already another thread doing reassembly for this
880 * connection, it is better to let it finish the job --
883 if (tp
->t_flagsext
& TF_REASS_INPROG
) {
887 tp
->t_flagsext
|= TF_REASS_INPROG
;
888 /* lost packet was recovered, so ooo data can be returned */
889 tcpstat
.tcps_recovered_pkts
++;
892 tp
->rcv_nxt
+= q
->tqe_len
;
893 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
894 LIST_REMOVE(q
, tqe_q
);
895 if (so
->so_state
& SS_CANTRCVMORE
) {
899 * The mbuf may be freed after it has been added to the
900 * receive socket buffer so we reinitialize th to point
901 * to a safe copy of the TCP header
903 struct tcphdr saved_tcphdr
= {};
905 so_recv_data_stat(so
, q
->tqe_m
, 0); /* XXXX */
906 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
908 * Append the inorder data as a message to the
909 * receive socket buffer. Also check to see if
910 * the data we are about to deliver is the same
911 * data that we wanted to pass up to the user
912 * out of order. If so, reset copy_oodata --
913 * the received data filled a gap, and
920 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
921 if (sbappendstream_rcvdemux(so
, q
->tqe_m
,
922 q
->tqe_th
->th_seq
- (tp
->irs
+ 1), 0)) {
927 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
928 tcp_update_lro_seq(tp
->rcv_nxt
,
929 inp
->inp_laddr
, inp
->inp_faddr
,
930 th
->th_dport
, th
->th_sport
);
933 zfree(tcp_reass_zone
, q
);
935 q
= LIST_FIRST(&tp
->t_segq
);
936 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
937 tp
->t_flagsext
&= ~TF_REASS_INPROG
;
940 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
941 KERNEL_DEBUG(DBG_LAYER_BEG
,
942 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
943 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
944 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
949 KERNEL_DEBUG(DBG_LAYER_BEG
,
950 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
951 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
952 (inp
->inp_faddr
.s_addr
& 0xffff)),
956 msg_unordered_delivery
:
957 /* Deliver out-of-order data as a message */
958 if (te
&& (so
->so_flags
& SOF_ENABLE_MSGS
) && copy_oodata
&& te
->tqe_len
) {
960 * make a copy of the mbuf to be delivered up to
961 * the user, and add it to the sockbuf
963 oodata
= m_copym(te
->tqe_m
, 0, M_COPYALL
, M_DONTWAIT
);
964 if (oodata
!= NULL
) {
965 if (sbappendmsgstream_rcv(&so
->so_rcv
, oodata
,
966 te
->tqe_th
->th_seq
- (tp
->irs
+ 1), 1)) {
968 tcpstat
.tcps_msg_unopkts
++;
970 tcpstat
.tcps_msg_unoappendfail
++;
979 * Reduce congestion window -- used when ECN is seen or when a tail loss
980 * probe recovers the last packet.
983 tcp_reduce_congestion_window(
987 * If the current tcp cc module has
988 * defined a hook for tasks to run
989 * before entering FR, call it
991 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
992 CC_ALGO(tp
)->pre_fr(tp
);
994 ENTER_FASTRECOVERY(tp
);
995 if (tp
->t_flags
& TF_SENTFIN
) {
996 tp
->snd_recover
= tp
->snd_max
- 1;
998 tp
->snd_recover
= tp
->snd_max
;
1000 tp
->t_timer
[TCPT_REXMT
] = 0;
1001 tp
->t_timer
[TCPT_PTO
] = 0;
1003 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
1004 tcp_cc_adjust_nonvalidated_cwnd(tp
);
1006 tp
->snd_cwnd
= tp
->snd_ssthresh
+
1007 tp
->t_maxseg
* tcprexmtthresh
;
1012 * This function is called upon reception of data on a socket. It's purpose is
1013 * to handle the adaptive keepalive timers that monitor whether the connection
1014 * is making progress. First the adaptive read-timer, second the TFO probe-timer.
1016 * The application wants to get an event if there is a stall during read.
1017 * Set the initial keepalive timeout to be equal to twice RTO.
1019 * If the outgoing interface is in marginal conditions, we need to
1020 * enable read probes for that too.
1023 tcp_adaptive_rwtimo_check(struct tcpcb
*tp
, int tlen
)
1025 struct ifnet
*outifp
= tp
->t_inpcb
->inp_last_outifp
;
1027 if ((tp
->t_adaptive_rtimo
> 0 ||
1029 (outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
)))
1031 tp
->t_state
== TCPS_ESTABLISHED
) {
1032 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1033 (TCP_REXMTVAL(tp
) << 1));
1034 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
1035 tp
->t_rtimo_probes
= 0;
1040 tcp_keepalive_reset(struct tcpcb
*tp
)
1042 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1043 TCP_CONN_KEEPIDLE(tp
));
1044 tp
->t_flagsext
&= ~(TF_DETECT_READSTALL
);
1045 tp
->t_rtimo_probes
= 0;
1049 * TCP input routine, follows pages 65-76 of the
1050 * protocol specification dated September, 1981 very closely.
1054 tcp6_input(struct mbuf
**mp
, int *offp
, int proto
)
1056 #pragma unused(proto)
1057 struct mbuf
*m
= *mp
;
1059 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1061 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
1063 /* Expect 32-bit aligned data pointer on strict-align platforms */
1064 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1067 * draft-itojun-ipv6-tcp-to-anycast
1068 * better place to put this in?
1070 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) == 0) {
1071 if (ia6_flags
& IN6_IFF_ANYCAST
) {
1072 struct ip6_hdr
*ip6
;
1074 ip6
= mtod(m
, struct ip6_hdr
*);
1075 icmp6_error(m
, ICMP6_DST_UNREACH
,
1076 ICMP6_DST_UNREACH_ADDR
,
1077 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
1079 IF_TCP_STATINC(ifp
, icmp6unreach
);
1081 return IPPROTO_DONE
;
1085 tcp_input(m
, *offp
);
1086 return IPPROTO_DONE
;
1090 /* Depending on the usage of mbuf space in the system, this function
1091 * will return true or false. This is used to determine if a socket
1092 * buffer can take more memory from the system for auto-tuning or not.
1095 tcp_cansbgrow(struct sockbuf
*sb
)
1097 /* Calculate the host level space limit in terms of MSIZE buffers.
1098 * We can use a maximum of half of the available mbuf space for
1101 u_int32_t mblim
= ((nmbclusters
>> 1) << (MCLSHIFT
- MSIZESHIFT
));
1103 /* Calculate per sb limit in terms of bytes. We optimize this limit
1104 * for upto 16 socket buffers.
1107 u_int32_t sbspacelim
= ((nmbclusters
>> 4) << MCLSHIFT
);
1109 if ((total_sbmb_cnt
< mblim
) &&
1110 (sb
->sb_hiwat
< sbspacelim
)) {
1113 OSIncrementAtomic64(&sbmb_limreached
);
1119 tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1120 u_int32_t newsize
, u_int32_t idealsize
, u_int32_t rcvbuf_max
)
1122 /* newsize should not exceed max */
1123 newsize
= min(newsize
, rcvbuf_max
);
1125 /* The receive window scale negotiated at the
1126 * beginning of the connection will also set a
1127 * limit on the socket buffer size
1129 newsize
= min(newsize
, TCP_MAXWIN
<< tp
->rcv_scale
);
1131 /* Set new socket buffer size */
1132 if (newsize
> sbrcv
->sb_hiwat
&&
1133 (sbreserve(sbrcv
, newsize
) == 1)) {
1134 sbrcv
->sb_idealsize
= min(max(sbrcv
->sb_idealsize
,
1135 (idealsize
!= 0) ? idealsize
: newsize
), rcvbuf_max
);
1137 /* Again check the limit set by the advertised
1140 sbrcv
->sb_idealsize
= min(sbrcv
->sb_idealsize
,
1141 TCP_MAXWIN
<< tp
->rcv_scale
);
1146 * This function is used to grow a receive socket buffer. It
1147 * will take into account system-level memory usage and the
1148 * bandwidth available on the link to make a decision.
1151 tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1152 struct tcpopt
*to
, u_int32_t pktlen
, u_int32_t rcvbuf_max
)
1154 struct socket
*so
= sbrcv
->sb_so
;
1157 * Do not grow the receive socket buffer if
1158 * - auto resizing is disabled, globally or on this socket
1159 * - the high water mark already reached the maximum
1160 * - the stream is in background and receive side is being
1162 * - if there are segments in reassembly queue indicating loss,
1163 * do not need to increase recv window during recovery as more
1164 * data is not going to be sent. A duplicate ack sent during
1165 * recovery should not change the receive window
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 !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 tp
->rfbuf_cnt
+= pktlen
;
1219 int32_t rcvbuf_inc
, min_incr
;
1221 * Increment the receive window by a
1222 * multiple of maximum sized segments.
1223 * This will prevent a connection from
1224 * sending smaller segments on wire if it
1225 * is limited by the receive window.
1227 * Set the ideal size based on current
1228 * bandwidth measurements. We set the
1229 * ideal size on receive socket buffer to
1230 * be twice the bandwidth delay product.
1232 rcvbuf_inc
= (tp
->rfbuf_cnt
<< 1)
1236 * Make the increment equal to 8 segments
1239 min_incr
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1240 if (rcvbuf_inc
< min_incr
) {
1241 rcvbuf_inc
= min_incr
;
1245 (rcvbuf_inc
/ tp
->t_maxseg
) * tp
->t_maxseg
;
1246 tcp_sbrcv_reserve(tp
, sbrcv
,
1247 sbrcv
->sb_hiwat
+ rcvbuf_inc
,
1248 (tp
->rfbuf_cnt
<< 1), rcvbuf_max
);
1250 /* Measure instantaneous receive bandwidth */
1251 if (tp
->t_bwmeas
!= NULL
&& tp
->rfbuf_cnt
> 0 &&
1252 TSTMP_GT(tcp_now
, tp
->rfbuf_ts
)) {
1254 rcv_bw
= tp
->rfbuf_cnt
/
1255 (int)(tcp_now
- tp
->rfbuf_ts
);
1256 if (tp
->t_bwmeas
->bw_rcvbw_max
== 0) {
1257 tp
->t_bwmeas
->bw_rcvbw_max
= rcv_bw
;
1259 tp
->t_bwmeas
->bw_rcvbw_max
= max(
1260 tp
->t_bwmeas
->bw_rcvbw_max
, rcv_bw
);
1265 tp
->rfbuf_cnt
+= pktlen
;
1270 /* Restart the measurement */
1276 /* This function will trim the excess space added to the socket buffer
1277 * to help a slow-reading app. The ideal-size of a socket buffer depends
1278 * on the link bandwidth or it is set by an application and we aim to
1282 tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sbrcv
)
1284 if (tcp_do_autorcvbuf
== 1 && sbrcv
->sb_idealsize
> 0 &&
1285 sbrcv
->sb_hiwat
> sbrcv
->sb_idealsize
) {
1287 /* compute the difference between ideal and current sizes */
1288 u_int32_t diff
= sbrcv
->sb_hiwat
- sbrcv
->sb_idealsize
;
1290 /* Compute the maximum advertised window for
1293 u_int32_t advwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
1295 /* How much can we trim the receive socket buffer?
1296 * 1. it can not be trimmed beyond the max rcv win advertised
1297 * 2. if possible, leave 1/16 of bandwidth*delay to
1298 * avoid closing the win completely
1300 u_int32_t leave
= max(advwin
, (sbrcv
->sb_idealsize
>> 4));
1302 /* Sometimes leave can be zero, in that case leave at least
1303 * a few segments worth of space.
1306 leave
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1309 trim
= sbrcv
->sb_hiwat
- (sbrcv
->sb_cc
+ leave
);
1310 trim
= imin(trim
, (int32_t)diff
);
1313 sbreserve(sbrcv
, (sbrcv
->sb_hiwat
- trim
));
1318 /* We may need to trim the send socket buffer size for two reasons:
1319 * 1. if the rtt seen on the connection is climbing up, we do not
1320 * want to fill the buffers any more.
1321 * 2. if the congestion win on the socket backed off, there is no need
1322 * to hold more mbufs for that connection than what the cwnd will allow.
1325 tcp_sbsnd_trim(struct sockbuf
*sbsnd
)
1327 if (tcp_do_autosendbuf
== 1 &&
1328 ((sbsnd
->sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) ==
1329 (SB_AUTOSIZE
| SB_TRIM
)) &&
1330 (sbsnd
->sb_idealsize
> 0) &&
1331 (sbsnd
->sb_hiwat
> sbsnd
->sb_idealsize
)) {
1333 if (sbsnd
->sb_cc
<= sbsnd
->sb_idealsize
) {
1334 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_idealsize
;
1336 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_cc
;
1338 sbreserve(sbsnd
, (sbsnd
->sb_hiwat
- trim
));
1340 if (sbsnd
->sb_hiwat
<= sbsnd
->sb_idealsize
) {
1341 sbsnd
->sb_flags
&= ~(SB_TRIM
);
1346 * If timestamp option was not negotiated on this connection
1347 * and this connection is on the receiving side of a stream
1348 * then we can not measure the delay on the link accurately.
1349 * Instead of enabling automatic receive socket buffer
1350 * resizing, just give more space to the receive socket buffer.
1353 tcp_sbrcv_tstmp_check(struct tcpcb
*tp
)
1355 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1356 u_int32_t newsize
= 2 * tcp_recvspace
;
1357 struct sockbuf
*sbrcv
= &so
->so_rcv
;
1359 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_RCVD_TSTMP
)) !=
1360 (TF_REQ_TSTMP
| TF_RCVD_TSTMP
) &&
1361 (sbrcv
->sb_flags
& SB_AUTOSIZE
) != 0) {
1362 tcp_sbrcv_reserve(tp
, sbrcv
, newsize
, 0, newsize
);
1366 /* A receiver will evaluate the flow of packets on a connection
1367 * to see if it can reduce ack traffic. The receiver will start
1368 * stretching acks if all of the following conditions are met:
1369 * 1. tcp_delack_enabled is set to 3
1370 * 2. If the bytes received in the last 100ms is greater than a threshold
1371 * defined by maxseg_unacked
1372 * 3. If the connection has not been idle for tcp_maxrcvidle period.
1373 * 4. If the connection has seen enough packets to let the slow-start
1374 * finish after connection establishment or after some packet loss.
1376 * The receiver will stop stretching acks if there is congestion/reordering
1377 * as indicated by packets on reassembly queue or an ECN. If the delayed-ack
1378 * timer fires while stretching acks, it means that the packet flow has gone
1379 * below the threshold defined by maxseg_unacked and the receiver will stop
1380 * stretching acks. The receiver gets no indication when slow-start is completed
1381 * or when the connection reaches an idle state. That is why we use
1382 * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle
1386 tcp_stretch_ack_enable(struct tcpcb
*tp
, int thflags
)
1388 if (tp
->rcv_by_unackwin
>= (maxseg_unacked
* tp
->t_maxseg
) &&
1389 TSTMP_GEQ(tp
->rcv_unackwin
, tcp_now
)) {
1390 tp
->t_flags
|= TF_STREAMING_ON
;
1392 tp
->t_flags
&= ~TF_STREAMING_ON
;
1395 /* If there has been an idle time, reset streaming detection */
1396 if (TSTMP_GT(tcp_now
, tp
->rcv_unackwin
+ tcp_maxrcvidle
)) {
1397 tp
->t_flags
&= ~TF_STREAMING_ON
;
1401 * If there are flags other than TH_ACK set, reset streaming
1404 if (thflags
& ~TH_ACK
) {
1405 tp
->t_flags
&= ~TF_STREAMING_ON
;
1408 if (tp
->t_flagsext
& TF_DISABLE_STRETCHACK
) {
1409 if (tp
->rcv_nostrack_pkts
>= TCP_STRETCHACK_ENABLE_PKTCNT
) {
1410 tp
->t_flagsext
&= ~TF_DISABLE_STRETCHACK
;
1411 tp
->rcv_nostrack_pkts
= 0;
1412 tp
->rcv_nostrack_ts
= 0;
1414 tp
->rcv_nostrack_pkts
++;
1418 if (!(tp
->t_flagsext
& (TF_NOSTRETCHACK
| TF_DISABLE_STRETCHACK
)) &&
1419 (tp
->t_flags
& TF_STREAMING_ON
) &&
1420 (!(tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) ||
1421 (tp
->rcv_waitforss
>= tcp_rcvsspktcnt
))) {
1429 * Reset the state related to stretch-ack algorithm. This will make
1430 * the receiver generate an ack every other packet. The receiver
1431 * will start re-evaluating the rate at which packets come to decide
1432 * if it can benefit by lowering the ack traffic.
1435 tcp_reset_stretch_ack(struct tcpcb
*tp
)
1437 tp
->t_flags
&= ~(TF_STRETCHACK
| TF_STREAMING_ON
);
1438 tp
->rcv_by_unackwin
= 0;
1439 tp
->rcv_by_unackhalfwin
= 0;
1440 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
1443 * When there is packet loss or packet re-ordering or CWR due to
1444 * ECN, the sender's congestion window is reduced. In these states,
1445 * generate an ack for every other packet for some time to allow
1446 * the sender's congestion window to grow.
1448 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
1449 tp
->rcv_waitforss
= 0;
1453 * The last packet was a retransmission, check if this ack
1454 * indicates that the retransmission was spurious.
1456 * If the connection supports timestamps, we could use it to
1457 * detect if the last retransmit was not needed. Otherwise,
1458 * we check if the ACK arrived within RTT/2 window, then it
1459 * was a mistake to do the retransmit in the first place.
1461 * This function will return 1 if it is a spurious retransmit,
1465 tcp_detect_bad_rexmt(struct tcpcb
*tp
, struct tcphdr
*th
,
1466 struct tcpopt
*to
, u_int32_t rxtime
)
1468 int32_t tdiff
, bad_rexmt_win
;
1469 bad_rexmt_win
= (tp
->t_srtt
>> (TCP_RTT_SHIFT
+ 1));
1471 /* If the ack has ECN CE bit, then cwnd has to be adjusted */
1472 if (TCP_ECN_ENABLED(tp
) && (th
->th_flags
& TH_ECE
)) {
1475 if (TSTMP_SUPPORTED(tp
)) {
1476 if (rxtime
> 0 && (to
->to_flags
& TOF_TS
)
1477 && to
->to_tsecr
!= 0
1478 && TSTMP_LT(to
->to_tsecr
, rxtime
)) {
1482 if ((tp
->t_rxtshift
== 1
1483 || (tp
->t_flagsext
& TF_SENT_TLPROBE
))
1485 tdiff
= (int32_t)(tcp_now
- rxtime
);
1486 if (tdiff
< bad_rexmt_win
) {
1496 * Restore congestion window state if a spurious timeout
1500 tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
)
1502 if (TSTMP_SUPPORTED(tp
)) {
1503 u_int32_t fsize
, acked
;
1504 fsize
= tp
->snd_max
- th
->th_ack
;
1505 acked
= BYTES_ACKED(th
, tp
);
1508 * Implement bad retransmit recovery as
1509 * described in RFC 4015.
1511 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1513 /* Initialize cwnd to the initial window */
1514 if (CC_ALGO(tp
)->cwnd_init
!= NULL
) {
1515 CC_ALGO(tp
)->cwnd_init(tp
);
1518 tp
->snd_cwnd
= fsize
+ min(acked
, tp
->snd_cwnd
);
1520 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1521 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1522 if (tp
->t_flags
& TF_WASFRECOVERY
) {
1523 ENTER_FASTRECOVERY(tp
);
1526 /* Do not use the loss flight size in this case */
1527 tp
->t_lossflightsize
= 0;
1529 tp
->snd_cwnd
= max(tp
->snd_cwnd
, TCP_CC_CWND_INIT_BYTES
);
1530 tp
->snd_recover
= tp
->snd_recover_prev
;
1531 tp
->snd_nxt
= tp
->snd_max
;
1533 /* Fix send socket buffer to reflect the change in cwnd */
1534 tcp_bad_rexmt_fix_sndbuf(tp
);
1537 * This RTT might reflect the extra delay induced
1538 * by the network. Skip using this sample for RTO
1539 * calculation and mark the connection so we can
1540 * recompute RTT when the next eligible sample is
1543 tp
->t_flagsext
|= TF_RECOMPUTE_RTT
;
1544 tp
->t_badrexmt_time
= tcp_now
;
1549 * If the previous packet was sent in retransmission timer, and it was
1550 * not needed, then restore the congestion window to the state before that
1553 * If the last packet was sent in tail loss probe timeout, check if that
1554 * recovered the last packet. If so, that will indicate a real loss and
1555 * the congestion window needs to be lowered.
1558 tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
, struct tcpopt
*to
)
1560 if (tp
->t_rxtshift
> 0 &&
1561 tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_rxtstart
)) {
1562 ++tcpstat
.tcps_sndrexmitbad
;
1563 tcp_bad_rexmt_restore_state(tp
, th
);
1564 tcp_ccdbg_trace(tp
, th
, TCP_CC_BAD_REXMT_RECOVERY
);
1565 } else if ((tp
->t_flagsext
& TF_SENT_TLPROBE
)
1566 && tp
->t_tlphighrxt
> 0
1567 && SEQ_GEQ(th
->th_ack
, tp
->t_tlphighrxt
)
1568 && !tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_tlpstart
)) {
1570 * check DSACK information also to make sure that
1571 * the TLP was indeed needed
1573 if (tcp_rxtseg_dsack_for_tlp(tp
)) {
1575 * received a DSACK to indicate that TLP was
1578 tcp_rxtseg_clean(tp
);
1583 * The tail loss probe recovered the last packet and
1584 * we need to adjust the congestion window to take
1585 * this loss into account.
1587 ++tcpstat
.tcps_tlp_recoverlastpkt
;
1588 if (!IN_FASTRECOVERY(tp
)) {
1589 tcp_reduce_congestion_window(tp
);
1590 EXIT_FASTRECOVERY(tp
);
1592 tcp_ccdbg_trace(tp
, th
, TCP_CC_TLP_RECOVER_LASTPACKET
);
1593 } else if (tcp_rxtseg_detect_bad_rexmt(tp
, th
->th_ack
)) {
1595 * All of the retransmitted segments were duplicated, this
1596 * can be an indication of bad fast retransmit.
1598 tcpstat
.tcps_dsack_badrexmt
++;
1599 tcp_bad_rexmt_restore_state(tp
, th
);
1600 tcp_ccdbg_trace(tp
, th
, TCP_CC_DSACK_BAD_REXMT
);
1601 tcp_rxtseg_clean(tp
);
1604 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1605 tp
->t_tlphighrxt
= 0;
1609 * check if the latest ack was for a segment sent during PMTU
1610 * blackhole detection. If the timestamp on the ack is before
1611 * PMTU blackhole detection, then revert the size of the max
1612 * segment to previous size.
1614 if (tp
->t_rxtshift
> 0 && (tp
->t_flags
& TF_BLACKHOLE
) &&
1615 tp
->t_pmtud_start_ts
> 0 && TSTMP_SUPPORTED(tp
)) {
1616 if ((to
->to_flags
& TOF_TS
) && to
->to_tsecr
!= 0
1617 && TSTMP_LT(to
->to_tsecr
, tp
->t_pmtud_start_ts
)) {
1618 tcp_pmtud_revert_segment_size(tp
);
1621 if (tp
->t_pmtud_start_ts
> 0) {
1622 tp
->t_pmtud_start_ts
= 0;
1627 * Check if early retransmit can be attempted according to RFC 5827.
1629 * If packet reordering is detected on a connection, fast recovery will
1630 * be delayed until it is clear that the packet was lost and not reordered.
1631 * But reordering detection is done only when SACK is enabled.
1633 * On connections that do not support SACK, there is a limit on the number
1634 * of early retransmits that can be done per minute. This limit is needed
1635 * to make sure that too many packets are not retransmitted when there is
1636 * packet reordering.
1639 tcp_early_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
)
1641 u_int32_t obytes
, snd_off
;
1643 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1645 if (early_rexmt
&& (SACK_ENABLED(tp
) ||
1646 tp
->t_early_rexmt_count
< TCP_EARLY_REXMT_LIMIT
) &&
1647 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1648 (tp
->t_dupacks
== 1 ||
1649 (SACK_ENABLED(tp
) &&
1650 !TAILQ_EMPTY(&tp
->snd_holes
)))) {
1652 * If there are only a few outstanding
1653 * segments on the connection, we might need
1654 * to lower the retransmit threshold. This
1655 * will allow us to do Early Retransmit as
1656 * described in RFC 5827.
1658 if (SACK_ENABLED(tp
) &&
1659 !TAILQ_EMPTY(&tp
->snd_holes
)) {
1660 obytes
= (tp
->snd_max
- tp
->snd_fack
) +
1661 tp
->sackhint
.sack_bytes_rexmit
;
1663 obytes
= (tp
->snd_max
- tp
->snd_una
);
1667 * In order to lower retransmit threshold the
1668 * following two conditions must be met.
1669 * 1. the amount of outstanding data is less
1671 * 2. there is no unsent data ready for
1672 * transmission or the advertised window
1673 * will limit sending new segments.
1675 snd_off
= tp
->snd_max
- tp
->snd_una
;
1676 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
) - snd_off
;
1677 if (obytes
< (tp
->t_maxseg
<< 2) &&
1681 osegs
= obytes
/ tp
->t_maxseg
;
1682 if ((osegs
* tp
->t_maxseg
) < obytes
) {
1687 * Since the connection might have already
1688 * received some dupacks, we add them to
1689 * to the outstanding segments count to get
1690 * the correct retransmit threshold.
1692 * By checking for early retransmit after
1693 * receiving some duplicate acks when SACK
1694 * is supported, the connection will
1695 * enter fast recovery even if multiple
1696 * segments are lost in the same window.
1698 osegs
+= tp
->t_dupacks
;
1701 ((osegs
- 1) > 1) ? (osegs
- 1) : 1;
1703 min(tp
->t_rexmtthresh
, tcprexmtthresh
);
1705 max(tp
->t_rexmtthresh
, tp
->t_dupacks
);
1707 if (tp
->t_early_rexmt_count
== 0) {
1708 tp
->t_early_rexmt_win
= tcp_now
;
1711 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1712 tcpstat
.tcps_tlp_recovery
++;
1713 tcp_ccdbg_trace(tp
, th
,
1714 TCP_CC_TLP_RECOVERY
);
1716 tcpstat
.tcps_early_rexmt
++;
1717 tp
->t_early_rexmt_count
++;
1718 tcp_ccdbg_trace(tp
, th
,
1719 TCP_CC_EARLY_RETRANSMIT
);
1726 * If we ever sent a TLP probe, the acknowledgement will trigger
1727 * early retransmit because the value of snd_fack will be close
1728 * to snd_max. This will take care of adjustments to the
1729 * congestion window. So we can reset TF_SENT_PROBE flag.
1731 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1732 tp
->t_tlphighrxt
= 0;
1737 tcp_tfo_syn(struct tcpcb
*tp
, struct tcpopt
*to
)
1739 u_char out
[CCAES_BLOCK_SIZE
];
1742 if (!(to
->to_flags
& (TOF_TFO
| TOF_TFOREQ
)) ||
1743 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
1747 if ((to
->to_flags
& TOF_TFOREQ
)) {
1748 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1750 tp
->t_tfo_stats
|= TFO_S_COOKIEREQ_RECV
;
1751 tcpstat
.tcps_tfo_cookie_req_rcv
++;
1755 /* Ok, then it must be an offered cookie. We need to check that ... */
1756 tcp_tfo_gen_cookie(tp
->t_inpcb
, out
, sizeof(out
));
1758 len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1760 if (memcmp(out
, to
->to_tfo
, len
)) {
1761 /* Cookies are different! Let's return and offer a new cookie */
1762 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1764 tp
->t_tfo_stats
|= TFO_S_COOKIE_INVALID
;
1765 tcpstat
.tcps_tfo_cookie_invalid
++;
1769 if (OSIncrementAtomic(&tcp_tfo_halfcnt
) >= tcp_tfo_backlog
) {
1770 /* Need to decrement again as we just increased it... */
1771 OSDecrementAtomic(&tcp_tfo_halfcnt
);
1775 tp
->t_tfo_flags
|= TFO_F_COOKIE_VALID
;
1777 tp
->t_tfo_stats
|= TFO_S_SYNDATA_RCV
;
1778 tcpstat
.tcps_tfo_syn_data_rcv
++;
1784 tcp_tfo_synack(struct tcpcb
*tp
, struct tcpopt
*to
)
1786 if (to
->to_flags
& TOF_TFO
) {
1787 unsigned char len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1790 * If this happens, things have gone terribly wrong. len should
1791 * have been checked in tcp_dooptions.
1793 VERIFY(len
<= TFO_COOKIE_LEN_MAX
);
1797 tcp_cache_set_cookie(tp
, to
->to_tfo
, len
);
1798 tcp_heuristic_tfo_success(tp
);
1800 tp
->t_tfo_stats
|= TFO_S_COOKIE_RCV
;
1801 tcpstat
.tcps_tfo_cookie_rcv
++;
1802 if (tp
->t_tfo_flags
& TFO_F_COOKIE_SENT
) {
1803 tcpstat
.tcps_tfo_cookie_wrong
++;
1804 tp
->t_tfo_stats
|= TFO_S_COOKIE_WRONG
;
1808 * Thus, no cookie in the response, but we either asked for one
1809 * or sent SYN+DATA. Now, we need to check whether we had to
1810 * rexmit the SYN. If that's the case, it's better to start
1811 * backing of TFO-cookie requests.
1813 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
1814 tp
->t_tfo_flags
& TFO_F_SYN_LOSS
) {
1815 tp
->t_tfo_stats
|= TFO_S_SYN_LOSS
;
1816 tcpstat
.tcps_tfo_syn_loss
++;
1818 tcp_heuristic_tfo_loss(tp
);
1820 if (tp
->t_tfo_flags
& TFO_F_COOKIE_REQ
) {
1821 tp
->t_tfo_stats
|= TFO_S_NO_COOKIE_RCV
;
1822 tcpstat
.tcps_tfo_no_cookie_rcv
++;
1825 tcp_heuristic_tfo_success(tp
);
1831 tcp_tfo_rcv_probe(struct tcpcb
*tp
, int tlen
)
1837 tp
->t_tfo_probe_state
= TFO_PROBE_PROBING
;
1840 * We send the probe out rather quickly (after one RTO). It does not
1841 * really hurt that much, it's only one additional segment on the wire.
1843 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, (TCP_REXMTVAL(tp
)));
1847 tcp_tfo_rcv_data(struct tcpcb
*tp
)
1849 /* Transition from PROBING to NONE as data has been received */
1850 if (tp
->t_tfo_probe_state
>= TFO_PROBE_PROBING
) {
1851 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1856 tcp_tfo_rcv_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
1858 if (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
&&
1859 tp
->t_tfo_probes
> 0) {
1860 if (th
->th_seq
== tp
->rcv_nxt
) {
1861 /* No hole, so stop probing */
1862 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1863 } else if (SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1864 /* There is a hole! Wait a bit for data... */
1865 tp
->t_tfo_probe_state
= TFO_PROBE_WAIT_DATA
;
1866 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1873 * Update snd_wnd information.
1876 tcp_update_window(struct tcpcb
*tp
, int thflags
, struct tcphdr
* th
,
1877 u_int32_t tiwin
, int tlen
)
1879 /* Don't look at the window if there is no ACK flag */
1880 if ((thflags
& TH_ACK
) &&
1881 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
1882 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
1883 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
1884 /* keep track of pure window updates */
1886 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
) {
1887 tcpstat
.tcps_rcvwinupd
++;
1889 tp
->snd_wnd
= tiwin
;
1890 tp
->snd_wl1
= th
->th_seq
;
1891 tp
->snd_wl2
= th
->th_ack
;
1892 if (tp
->snd_wnd
> tp
->max_sndwnd
) {
1893 tp
->max_sndwnd
= tp
->snd_wnd
;
1896 if (tp
->t_inpcb
->inp_socket
->so_flags
& SOF_MP_SUBFLOW
) {
1897 mptcp_update_window_wakeup(tp
);
1905 tcp_handle_wakeup(struct socket
*so
, int read_wakeup
, int write_wakeup
)
1907 if (read_wakeup
!= 0) {
1910 if (write_wakeup
!= 0) {
1916 tcp_input(struct mbuf
*m
, int off0
)
1919 struct ip
*ip
= NULL
;
1921 u_char
*optp
= NULL
;
1925 struct tcpcb
*tp
= 0;
1927 struct socket
*so
= 0;
1928 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
1929 int read_wakeup
= 0;
1930 int write_wakeup
= 0;
1931 struct in_addr laddr
;
1933 struct in6_addr laddr6
;
1936 int iss
= 0, nosock
= 0;
1937 u_int32_t tiwin
, sack_bytes_acked
= 0;
1938 struct tcpopt to
; /* options in this segment */
1943 struct sockaddr_in
*next_hop
= NULL
;
1944 struct m_tag
*fwd_tag
;
1945 #endif /* IPFIREWALL */
1946 u_char ip_ecn
= IPTOS_ECN_NOTECT
;
1947 unsigned int ifscope
;
1948 uint8_t isconnected
, isdisconnected
;
1949 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1950 int pktf_sw_lro_pkt
= (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) ? 1 : 0;
1951 int nlropkts
= (pktf_sw_lro_pkt
== 1) ? m
->m_pkthdr
.lro_npkts
: 1;
1952 int turnoff_lro
= 0, win
;
1954 struct mptcb
*mp_tp
= NULL
;
1956 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
1957 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
1958 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
1959 boolean_t recvd_dsack
= FALSE
;
1960 struct tcp_respond_args tra
;
1962 boolean_t check_cfil
= cfil_filter_present();
1963 bool findpcb_iterated
= false;
1965 * The mbuf may be freed after it has been added to the receive socket
1966 * buffer or the reassembly queue, so we reinitialize th to point to a
1967 * safe copy of the TCP header
1969 struct tcphdr saved_tcphdr
= {};
1971 * Save copy of the IPv4/IPv6 header.
1972 * Note: use array of uint32_t to silence compiler warning when casting
1973 * to a struct ip6_hdr pointer.
1975 #define MAX_IPWORDS ((sizeof(struct ip) + MAX_IPOPTLEN) / sizeof(uint32_t))
1976 uint32_t saved_hdr
[MAX_IPWORDS
];
1978 #define TCP_INC_VAR(stat, npkts) do { \
1982 TCP_INC_VAR(tcpstat
.tcps_rcvtotal
, nlropkts
);
1984 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
1985 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1986 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1987 KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
1991 if (fwd_tag
!= NULL
) {
1992 struct ip_fwd_tag
*ipfwd_tag
=
1993 (struct ip_fwd_tag
*)(fwd_tag
+ 1);
1995 next_hop
= ipfwd_tag
->next_hop
;
1996 m_tag_delete(m
, fwd_tag
);
1998 #endif /* IPFIREWALL */
2001 struct ip6_hdr
*ip6
= NULL
;
2004 int rstreason
; /* For badport_bandlim accounting purposes */
2005 struct proc
*proc0
= current_proc();
2007 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
2010 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
2012 bzero((char *)&to
, sizeof(to
));
2017 * Expect 32-bit aligned data pointer on
2018 * strict-align platforms
2020 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
2022 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
2023 ip6
= mtod(m
, struct ip6_hdr
*);
2024 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
2025 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
2027 if (tcp_input_checksum(AF_INET6
, m
, th
, off0
, tlen
)) {
2028 TCP_LOG_DROP_PKT(ip6
, th
, ifp
, "IPv6 bad tcp checksum");
2032 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
2033 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
2034 th
->th_seq
, th
->th_ack
, th
->th_win
);
2036 * Be proactive about unspecified IPv6 address in source.
2037 * As we use all-zero to indicate unbounded/unconnected pcb,
2038 * unspecified IPv6 address can be used to confuse us.
2040 * Note that packets with unspecified IPv6 destination is
2041 * already dropped in ip6_input.
2043 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
2045 IF_TCP_STATINC(ifp
, unspecv6
);
2046 TCP_LOG_DROP_PKT(ip6
, th
, ifp
, "src IPv6 address unspecified");
2049 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
2050 struct ip6_hdr
*, ip6
, struct tcpcb
*, NULL
,
2051 struct tcphdr
*, th
);
2053 ip_ecn
= (ntohl(ip6
->ip6_flow
) >> 20) & IPTOS_ECN_MASK
;
2058 * Get IP and TCP header together in first mbuf.
2059 * Note: IP leaves IP header in first mbuf.
2061 if (off0
> sizeof(struct ip
)) {
2063 off0
= sizeof(struct ip
);
2065 if (m
->m_len
< sizeof(struct tcpiphdr
)) {
2066 if ((m
= m_pullup(m
, sizeof(struct tcpiphdr
))) == 0) {
2067 tcpstat
.tcps_rcvshort
++;
2072 /* Expect 32-bit aligned data pointer on strict-align platforms */
2073 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
2075 ip
= mtod(m
, struct ip
*);
2076 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
2079 if (tcp_input_checksum(AF_INET
, m
, th
, off0
, tlen
)) {
2080 TCP_LOG_DROP_PKT(ip
, th
, ifp
, "IPv4 bad tcp checksum");
2085 /* Re-initialization for later version check */
2086 ip
->ip_v
= IPVERSION
;
2088 ip_ecn
= (ip
->ip_tos
& IPTOS_ECN_MASK
);
2090 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
2091 struct ip
*, ip
, struct tcpcb
*, NULL
, struct tcphdr
*, th
);
2093 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
2094 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
2095 th
->th_seq
, th
->th_ack
, th
->th_win
);
2098 #define TCP_LOG_HDR (isipv6 ? (void *)ip6 : (void *)ip)
2101 * Check that TCP offset makes sense,
2102 * pull out TCP options and adjust length.
2104 off
= th
->th_off
<< 2;
2105 if (off
< sizeof(struct tcphdr
) || off
> tlen
) {
2106 tcpstat
.tcps_rcvbadoff
++;
2107 IF_TCP_STATINC(ifp
, badformat
);
2108 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "bad tcp offset");
2111 tlen
-= off
; /* tlen is used instead of ti->ti_len */
2112 if (off
> sizeof(struct tcphdr
)) {
2115 IP6_EXTHDR_CHECK(m
, off0
, off
, return );
2116 ip6
= mtod(m
, struct ip6_hdr
*);
2117 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
2121 if (m
->m_len
< sizeof(struct ip
) + off
) {
2122 if ((m
= m_pullup(m
, sizeof(struct ip
) + off
)) == 0) {
2123 tcpstat
.tcps_rcvshort
++;
2126 ip
= mtod(m
, struct ip
*);
2127 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
2130 optlen
= off
- sizeof(struct tcphdr
);
2131 optp
= (u_char
*)(th
+ 1);
2133 * Do quick retrieval of timestamp options ("options
2134 * prediction?"). If timestamp is the only option and it's
2135 * formatted as recommended in RFC 1323 appendix A, we
2136 * quickly get the values now and not bother calling
2137 * tcp_dooptions(), etc.
2139 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
2140 (optlen
> TCPOLEN_TSTAMP_APPA
&&
2141 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
2142 *(u_int32_t
*)(void *)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
2143 (th
->th_flags
& TH_SYN
) == 0) {
2144 to
.to_flags
|= TOF_TS
;
2145 to
.to_tsval
= ntohl(*(u_int32_t
*)(void *)(optp
+ 4));
2146 to
.to_tsecr
= ntohl(*(u_int32_t
*)(void *)(optp
+ 8));
2147 optp
= NULL
; /* we've parsed the options */
2150 thflags
= th
->th_flags
;
2154 * If the drop_synfin option is enabled, drop all packets with
2155 * both the SYN and FIN bits set. This prevents e.g. nmap from
2156 * identifying the TCP/IP stack.
2158 * This is a violation of the TCP specification.
2160 if (drop_synfin
&& (thflags
& (TH_SYN
| TH_FIN
)) == (TH_SYN
| TH_FIN
)) {
2161 IF_TCP_STATINC(ifp
, synfin
);
2162 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "drop SYN FIN");
2168 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
2169 * until after ip6_savecontrol() is called and before other functions
2170 * which don't want those proto headers.
2171 * Because ip6_savecontrol() is going to parse the mbuf to
2172 * search for data to be passed up to user-land, it wants mbuf
2173 * parameters to be unchanged.
2175 drop_hdrlen
= off0
+ off
;
2177 /* Since this is an entry point for input processing of tcp packets, we
2178 * can update the tcp clock here.
2180 calculate_tcp_clock();
2183 * Record the interface where this segment arrived on; this does not
2184 * affect normal data output (for non-detached TCP) as it provides a
2185 * hint about which route and interface to use for sending in the
2186 * absence of a PCB, when scoped routing (and thus source interface
2187 * selection) are enabled.
2189 if ((m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) || m
->m_pkthdr
.rcvif
== NULL
) {
2190 ifscope
= IFSCOPE_NONE
;
2192 ifscope
= m
->m_pkthdr
.rcvif
->if_index
;
2196 * Convert TCP protocol specific fields to host format.
2199 #if BYTE_ORDER != BIG_ENDIAN
2207 * Locate pcb for segment.
2211 isconnected
= FALSE
;
2212 isdisconnected
= FALSE
;
2214 #if IPFIREWALL_FORWARD
2215 if (next_hop
!= NULL
2217 && isipv6
== 0 /* IPv6 support is not yet */
2221 * Diverted. Pretend to be the destination.
2222 * already got one like this?
2224 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2225 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
2228 * No, then it's new. Try find the ambushing socket
2230 if (!next_hop
->sin_port
) {
2231 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
2232 th
->th_sport
, next_hop
->sin_addr
,
2233 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2235 inp
= in_pcblookup_hash(&tcbinfo
,
2236 ip
->ip_src
, th
->th_sport
,
2238 ntohs(next_hop
->sin_port
), 1,
2243 #endif /* IPFIREWALL_FORWARD */
2247 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
2248 &ip6
->ip6_dst
, th
->th_dport
, 1,
2252 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2253 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2257 * Use the interface scope information from the PCB for outbound
2258 * segments. If the PCB isn't present and if scoped routing is
2259 * enabled, tcp_respond will use the scope of the interface where
2260 * the segment arrived on.
2262 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
)) {
2263 ifscope
= inp
->inp_boundifp
->if_index
;
2267 * If the state is CLOSED (i.e., TCB does not exist) then
2268 * all data in the incoming segment is discarded.
2269 * If the TCB exists but is in CLOSED state, it is embryonic,
2270 * but should either do a listen or a connect soon.
2275 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
2277 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
2282 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
2283 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
2287 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
2288 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
2290 switch (log_in_vain
) {
2292 if (thflags
& TH_SYN
) {
2294 "Connection attempt to TCP %s:%d from %s:%d\n",
2295 dbuf
, ntohs(th
->th_dport
),
2297 ntohs(th
->th_sport
));
2302 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
2303 dbuf
, ntohs(th
->th_dport
), sbuf
,
2304 ntohs(th
->th_sport
), thflags
);
2308 if ((thflags
& TH_SYN
) && !(thflags
& TH_ACK
) &&
2309 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
2311 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
2312 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
2314 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
2317 log_in_vain_log((LOG_INFO
,
2318 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
2319 dbuf
, ntohs(th
->th_dport
),
2321 ntohs(th
->th_sport
)));
2329 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) {
2330 switch (blackhole
) {
2332 if (thflags
& TH_SYN
) {
2333 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "blackhole 1 syn for closed port");
2338 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "blackhole 2 closed port");
2341 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "blackhole closed port");
2346 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2347 IF_TCP_STATINC(ifp
, noconnnolist
);
2348 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "closed port");
2349 goto dropwithresetnosock
;
2351 so
= inp
->inp_socket
;
2353 /* This case shouldn't happen as the socket shouldn't be null
2354 * if inp_state isn't set to INPCB_STATE_DEAD
2355 * But just in case, we pretend we didn't find the socket if we hit this case
2356 * as this isn't cause for a panic (the socket might be leaked however)...
2360 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp
);
2362 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "inp_socket NULL");
2367 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2368 socket_unlock(so
, 1);
2369 inp
= NULL
; // pretend we didn't find it
2370 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "inp state WNT_STOPUSING");
2374 if (!isipv6
&& inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
2375 if (inp
->inp_faddr
.s_addr
!= ip
->ip_src
.s_addr
||
2376 inp
->inp_laddr
.s_addr
!= ip
->ip_dst
.s_addr
||
2377 inp
->inp_fport
!= th
->th_sport
||
2378 inp
->inp_lport
!= th
->th_dport
) {
2379 os_log_error(OS_LOG_DEFAULT
, "%s 5-tuple does not match: %u:%u %u:%u\n",
2381 ntohs(inp
->inp_fport
), ntohs(th
->th_sport
),
2382 ntohs(inp
->inp_lport
), ntohs(th
->th_dport
));
2383 if (findpcb_iterated
) {
2386 findpcb_iterated
= true;
2387 socket_unlock(so
, 1);
2391 } else if (isipv6
&& !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
2392 if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, &ip6
->ip6_src
) ||
2393 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &ip6
->ip6_dst
) ||
2394 inp
->inp_fport
!= th
->th_sport
||
2395 inp
->inp_lport
!= th
->th_dport
) {
2396 os_log_error(OS_LOG_DEFAULT
, "%s 5-tuple does not match: %u:%u %u:%u\n",
2398 ntohs(inp
->inp_fport
), ntohs(th
->th_sport
),
2399 ntohs(inp
->inp_lport
), ntohs(th
->th_dport
));
2400 if (findpcb_iterated
) {
2403 findpcb_iterated
= true;
2404 socket_unlock(so
, 1);
2410 tp
= intotcpcb(inp
);
2412 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2413 IF_TCP_STATINC(ifp
, noconnlist
);
2414 TCP_LOG_DROP_PKT(TCP_LOG_HDR
, th
, ifp
, "tp is NULL");
2418 TCP_LOG_TH_FLAGS(TCP_LOG_HDR
, th
, tp
, false, ifp
);
2420 if (tp
->t_state
== TCPS_CLOSED
) {
2421 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "tp state TCPS_CLOSED");
2426 if (so
->so_state
& SS_ISCONNECTED
) {
2427 // Connected TCP sockets have a fully-bound local and remote,
2428 // so the policy check doesn't need to override addresses
2429 if (!necp_socket_is_allowed_to_send_recv(inp
, ifp
, NULL
, NULL
, NULL
)) {
2430 TCP_LOG_DROP_NECP(TCP_LOG_HDR
, th
, intotcpcb(inp
), false);
2431 IF_TCP_STATINC(ifp
, badformat
);
2436 * If the proc_uuid_policy table has been updated since the last use
2437 * of the listening socket (i.e., the proc_uuid_policy_table_gencount
2438 * has been updated), the flags in the socket may be out of date.
2439 * If INP2_WANT_APP_POLICY is stale, inbound packets may
2440 * be dropped by NECP if the socket should now match a per-app
2442 * In order to avoid this refresh the proc_uuid_policy state to
2443 * potentially recalculate the socket's flags before checking
2446 (void) inp_update_policy(inp
);
2449 if (!necp_socket_is_allowed_to_send_recv_v6(inp
,
2450 th
->th_dport
, th
->th_sport
, &ip6
->ip6_dst
,
2451 &ip6
->ip6_src
, ifp
, NULL
, NULL
, NULL
)) {
2452 TCP_LOG_DROP_NECP(TCP_LOG_HDR
, th
, intotcpcb(inp
), false);
2453 IF_TCP_STATINC(ifp
, badformat
);
2459 if (!necp_socket_is_allowed_to_send_recv_v4(inp
,
2460 th
->th_dport
, th
->th_sport
, &ip
->ip_dst
, &ip
->ip_src
,
2461 ifp
, NULL
, NULL
, NULL
)) {
2462 TCP_LOG_DROP_NECP(TCP_LOG_HDR
, th
, intotcpcb(inp
), false);
2463 IF_TCP_STATINC(ifp
, badformat
);
2470 prev_t_state
= tp
->t_state
;
2472 /* If none of the FIN|SYN|RST|ACK flag is set, drop */
2473 if (tcp_do_rfc5961
&& (thflags
& TH_ACCEPT
) == 0) {
2474 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 TH_ACCEPT == 0");
2478 /* Unscale the window into a 32-bit value. */
2479 if ((thflags
& TH_SYN
) == 0) {
2480 tiwin
= th
->th_win
<< tp
->snd_scale
;
2487 if (mac_inpcb_check_deliver(inp
, m
, AF_INET
, SOCK_STREAM
)) {
2488 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "mac_inpcb_check_deliver failed");
2493 /* Avoid processing packets while closing a listen socket */
2494 if (tp
->t_state
== TCPS_LISTEN
&&
2495 (so
->so_options
& SO_ACCEPTCONN
) == 0) {
2496 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "closing a listening socket");
2500 if (so
->so_options
& (SO_DEBUG
| SO_ACCEPTCONN
)) {
2502 if (so
->so_options
& SO_DEBUG
) {
2503 ostate
= tp
->t_state
;
2506 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
2510 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
2514 if (so
->so_options
& SO_ACCEPTCONN
) {
2515 struct tcpcb
*tp0
= tp
;
2518 struct sockaddr_storage from
;
2519 struct sockaddr_storage to2
;
2521 struct inpcb
*oinp
= sotoinpcb(so
);
2523 struct ifnet
*head_ifscope
;
2524 unsigned int head_nocell
, head_recvanyif
,
2525 head_noexpensive
, head_awdl_unrestricted
,
2526 head_intcoproc_allowed
, head_external_port
,
2529 /* Get listener's bound-to-interface, if any */
2530 head_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2531 inp
->inp_boundifp
: NULL
;
2532 /* Get listener's no-cellular information, if any */
2533 head_nocell
= INP_NO_CELLULAR(inp
);
2534 /* Get listener's recv-any-interface, if any */
2535 head_recvanyif
= (inp
->inp_flags
& INP_RECV_ANYIF
);
2536 /* Get listener's no-expensive information, if any */
2537 head_noexpensive
= INP_NO_EXPENSIVE(inp
);
2538 head_noconstrained
= INP_NO_CONSTRAINED(inp
);
2539 head_awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
2540 head_intcoproc_allowed
= INP_INTCOPROC_ALLOWED(inp
);
2541 head_external_port
= (inp
->inp_flags2
& INP2_EXTERNAL_PORT
);
2544 * If the state is LISTEN then ignore segment if it contains an RST.
2545 * If the segment contains an ACK then it is bad and send a RST.
2546 * If it does not contain a SYN then it is not interesting; drop it.
2547 * If it is from this socket, drop it, it must be forged.
2549 if ((thflags
& (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
) {
2550 IF_TCP_STATINC(ifp
, listbadsyn
);
2552 if (thflags
& TH_RST
) {
2553 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN with RST");
2556 if (thflags
& TH_ACK
) {
2557 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN with ACK");
2559 tcpstat
.tcps_badsyn
++;
2560 rstreason
= BANDLIM_RST_OPENPORT
;
2564 /* We come here if there is no SYN set */
2565 tcpstat
.tcps_badsyn
++;
2566 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad SYN");
2569 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
, 0, 0, 0, 0, 0);
2570 if (th
->th_dport
== th
->th_sport
) {
2573 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
2575 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad tuple same port");
2580 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
) {
2581 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad tuple same IPv4 address");
2586 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2587 * in_broadcast() should never return true on a received
2588 * packet with M_BCAST not set.
2590 * Packets with a multicast source address should also
2593 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
2594 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "mbuf M_BCAST | M_MCAST");
2599 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2600 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
2601 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "IN6_IS_ADDR_MULTICAST");
2606 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2607 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2608 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2609 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
2610 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "multicast or broadcast address");
2617 * If deprecated address is forbidden,
2618 * we do not accept SYN to deprecated interface
2619 * address to prevent any new inbound connection from
2620 * getting established.
2621 * When we do not accept SYN, we send a TCP RST,
2622 * with deprecated source address (instead of dropping
2623 * it). We compromise it as it is much better for peer
2624 * to send a RST, and RST will be the final packet
2627 * If we do not forbid deprecated addresses, we accept
2628 * the SYN packet. RFC 4862 forbids dropping SYN in
2631 if (isipv6
&& !ip6_use_deprecated
) {
2634 if (ip6_getdstifaddr_info(m
, NULL
,
2636 if (ia6_flags
& IN6_IFF_DEPRECATED
) {
2638 rstreason
= BANDLIM_RST_OPENPORT
;
2639 IF_TCP_STATINC(ifp
, deprecate6
);
2640 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "deprecated IPv6 address");
2646 if (so
->so_filt
|| check_cfil
) {
2649 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
2651 sin6
->sin6_len
= sizeof(*sin6
);
2652 sin6
->sin6_family
= AF_INET6
;
2653 sin6
->sin6_port
= th
->th_sport
;
2654 sin6
->sin6_flowinfo
= 0;
2655 sin6
->sin6_addr
= ip6
->ip6_src
;
2656 sin6
->sin6_scope_id
= 0;
2658 sin6
= (struct sockaddr_in6
*)&to2
;
2660 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
2661 sin6
->sin6_family
= AF_INET6
;
2662 sin6
->sin6_port
= th
->th_dport
;
2663 sin6
->sin6_flowinfo
= 0;
2664 sin6
->sin6_addr
= ip6
->ip6_dst
;
2665 sin6
->sin6_scope_id
= 0;
2669 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
2671 sin
->sin_len
= sizeof(*sin
);
2672 sin
->sin_family
= AF_INET
;
2673 sin
->sin_port
= th
->th_sport
;
2674 sin
->sin_addr
= ip
->ip_src
;
2676 sin
= (struct sockaddr_in
*)&to2
;
2678 sin
->sin_len
= sizeof(struct sockaddr_in
);
2679 sin
->sin_family
= AF_INET
;
2680 sin
->sin_port
= th
->th_dport
;
2681 sin
->sin_addr
= ip
->ip_dst
;
2686 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2688 so2
= sonewconn(so
, 0, NULL
);
2691 tcpstat
.tcps_listendrop
++;
2692 if (tcp_dropdropablreq(so
)) {
2694 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2696 so2
= sonewconn(so
, 0, NULL
);
2700 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " listen drop");
2705 /* Point "inp" and "tp" in tandem to new socket */
2706 inp
= (struct inpcb
*)so2
->so_pcb
;
2707 tp
= intotcpcb(inp
);
2710 socket_unlock(so
, 0); /* Unlock but keep a reference on listener for now */
2715 * Mark socket as temporary until we're
2716 * committed to keeping it. The code at
2717 * ``drop'' and ``dropwithreset'' check the
2718 * flag dropsocket to see if the temporary
2719 * socket created here should be discarded.
2720 * We mark the socket as discardable until
2721 * we're committed to it below in TCPS_LISTEN.
2722 * There are some error conditions in which we
2723 * have to drop the temporary socket.
2727 * Inherit INP_BOUND_IF from listener; testing if
2728 * head_ifscope is non-NULL is sufficient, since it
2729 * can only be set to a non-zero value earlier if
2730 * the listener has such a flag set.
2732 if (head_ifscope
!= NULL
) {
2733 inp
->inp_flags
|= INP_BOUND_IF
;
2734 inp
->inp_boundifp
= head_ifscope
;
2736 inp
->inp_flags
&= ~INP_BOUND_IF
;
2739 * Inherit restrictions from listener.
2742 inp_set_nocellular(inp
);
2744 if (head_noexpensive
) {
2745 inp_set_noexpensive(inp
);
2747 if (head_noconstrained
) {
2748 inp_set_noconstrained(inp
);
2750 if (head_awdl_unrestricted
) {
2751 inp_set_awdl_unrestricted(inp
);
2753 if (head_intcoproc_allowed
) {
2754 inp_set_intcoproc_allowed(inp
);
2757 * Inherit {IN,IN6}_RECV_ANYIF from listener.
2759 if (head_recvanyif
) {
2760 inp
->inp_flags
|= INP_RECV_ANYIF
;
2762 inp
->inp_flags
&= ~INP_RECV_ANYIF
;
2765 if (head_external_port
) {
2766 inp
->inp_flags2
|= INP2_EXTERNAL_PORT
;
2770 inp
->in6p_laddr
= ip6
->ip6_dst
;
2772 inp
->inp_vflag
&= ~INP_IPV6
;
2773 inp
->inp_vflag
|= INP_IPV4
;
2775 inp
->inp_laddr
= ip
->ip_dst
;
2779 inp
->inp_lport
= th
->th_dport
;
2780 if (in_pcbinshash(inp
, 0) != 0) {
2782 * Undo the assignments above if we failed to
2783 * put the PCB on the hash lists.
2787 inp
->in6p_laddr
= in6addr_any
;
2790 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
2792 socket_lock(oso
, 0); /* release ref on parent */
2793 socket_unlock(oso
, 1);
2794 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " in_pcbinshash failed");
2797 socket_lock(oso
, 0);
2801 * Inherit socket options from the listening
2803 * Note that in6p_inputopts are not (even
2804 * should not be) copied, since it stores
2805 * previously received options and is used to
2806 * detect if each new option is different than
2807 * the previous one and hence should be passed
2809 * If we copied in6p_inputopts, a user would
2810 * not be able to receive options just after
2811 * calling the accept system call.
2814 oinp
->inp_flags
& INP_CONTROLOPTS
;
2815 if (oinp
->in6p_outputopts
) {
2816 inp
->in6p_outputopts
=
2817 ip6_copypktopts(oinp
->in6p_outputopts
,
2823 inp
->inp_options
= ip_srcroute();
2824 inp
->inp_ip_tos
= oinp
->inp_ip_tos
;
2827 /* copy old policy into new socket's */
2828 if (sotoinpcb(oso
)->inp_sp
) {
2830 /* Is it a security hole here to silently fail to copy the policy? */
2831 if (inp
->inp_sp
!= NULL
) {
2832 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
2834 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
)) {
2835 printf("tcp_input: could not copy policy\n");
2839 /* inherit states from the listener */
2840 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2841 struct tcpcb
*, tp
, int32_t, TCPS_LISTEN
);
2842 tp
->t_state
= TCPS_LISTEN
;
2843 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
| TF_NOOPT
| TF_NODELAY
);
2844 tp
->t_flagsext
|= (tp0
->t_flagsext
& (TF_RXTFINDROP
| TF_NOTIMEWAIT
| TF_FASTOPEN
));
2845 tp
->t_keepinit
= tp0
->t_keepinit
;
2846 tp
->t_keepcnt
= tp0
->t_keepcnt
;
2847 tp
->t_keepintvl
= tp0
->t_keepintvl
;
2848 tp
->t_adaptive_wtimo
= tp0
->t_adaptive_wtimo
;
2849 tp
->t_adaptive_rtimo
= tp0
->t_adaptive_rtimo
;
2850 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
2851 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0) {
2852 tp
->t_notsent_lowat
= tp0
->t_notsent_lowat
;
2854 tp
->t_inpcb
->inp_flags2
|=
2855 tp0
->t_inpcb
->inp_flags2
& INP2_KEEPALIVE_OFFLOAD
;
2857 /* now drop the reference on the listener */
2858 socket_unlock(oso
, 1);
2860 tcp_set_max_rwinscale(tp
, so
, ifp
);
2864 int error
= cfil_sock_attach(so2
, (struct sockaddr
*)&to2
, (struct sockaddr
*)&from
,
2865 CFS_CONNECTION_DIR_IN
);
2867 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " cfil_sock_attach failed");
2871 #endif /* CONTENT_FILTER */
2873 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
2876 socket_lock_assert_owned(so
);
2878 if (net_mpklog_enabled
&& (m
->m_pkthdr
.rcvif
->if_xflags
& IFXF_MPK_LOG
)) {
2879 MPKL_TCP_INPUT(tcp_mpkl_log_object
,
2880 ntohs(tp
->t_inpcb
->inp_lport
), ntohs(tp
->t_inpcb
->inp_fport
),
2881 th
->th_seq
, th
->th_ack
, tlen
, thflags
,
2882 so
->last_pid
, so
->so_log_seqn
++);
2885 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
2887 * Evaluate the rate of arrival of packets to see if the
2888 * receiver can reduce the ack traffic. The algorithm to
2889 * stretch acks will be enabled if the connection meets
2890 * certain criteria defined in tcp_stretch_ack_enable function.
2892 if ((tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) != 0) {
2893 TCP_INC_VAR(tp
->rcv_waitforss
, nlropkts
);
2895 if (tcp_stretch_ack_enable(tp
, thflags
)) {
2896 tp
->t_flags
|= TF_STRETCHACK
;
2897 tp
->t_flagsext
&= ~(TF_RCVUNACK_WAITSS
);
2898 tp
->rcv_waitforss
= 0;
2900 tp
->t_flags
&= ~(TF_STRETCHACK
);
2902 if (TSTMP_GT(tp
->rcv_unackwin
- (tcp_rcvunackwin
>> 1), tcp_now
)) {
2903 tp
->rcv_by_unackhalfwin
+= (tlen
+ off
);
2904 tp
->rcv_by_unackwin
+= (tlen
+ off
);
2906 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
2907 tp
->rcv_by_unackwin
= tp
->rcv_by_unackhalfwin
+ tlen
+ off
;
2908 tp
->rcv_by_unackhalfwin
= tlen
+ off
;
2913 * Keep track of how many bytes were received in the LRO packet
2915 if ((pktf_sw_lro_pkt
) && (nlropkts
> 2)) {
2916 tp
->t_lropktlen
+= tlen
;
2919 * Explicit Congestion Notification - Flag that we need to send ECT if
2920 * + The IP Congestion experienced flag was set.
2921 * + Socket is in established state
2922 * + We negotiated ECN in the TCP setup
2923 * + This isn't a pure ack (tlen > 0)
2924 * + The data is in the valid window
2926 * TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
2928 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2929 TCP_ECN_ENABLED(tp
) && tlen
> 0 &&
2930 SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2931 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
2932 tp
->t_ecn_recv_ce
++;
2933 tcpstat
.tcps_ecn_recv_ce
++;
2934 INP_INC_IFNET_STAT(inp
, ecn_recv_ce
);
2935 /* Mark this connection as it received CE from network */
2936 tp
->ecn_flags
|= TE_RECV_ECN_CE
;
2937 tp
->ecn_flags
|= TE_SENDECE
;
2941 * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
2942 * bother doing extensive checks for state and whatnot.
2944 if (thflags
& TH_CWR
) {
2945 tp
->ecn_flags
&= ~TE_SENDECE
;
2946 tp
->t_ecn_recv_cwr
++;
2950 * If we received an explicit notification of congestion in
2951 * ip tos ecn bits or by the CWR bit in TCP header flags, reset
2952 * the ack-strteching state. We need to handle ECN notification if
2953 * an ECN setup SYN was sent even once.
2955 if (tp
->t_state
== TCPS_ESTABLISHED
2956 && (tp
->ecn_flags
& TE_SETUPSENT
)
2957 && (ip_ecn
== IPTOS_ECN_CE
|| (thflags
& TH_CWR
))) {
2958 tcp_reset_stretch_ack(tp
);
2959 CLEAR_IAJ_STATE(tp
);
2962 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2963 !TCP_ECN_ENABLED(tp
) && !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2964 tcpstat
.tcps_ecn_fallback_ce
++;
2965 tcp_heuristic_ecn_aggressive(tp
);
2966 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2969 if (tp
->t_state
== TCPS_ESTABLISHED
&& TCP_ECN_ENABLED(tp
) &&
2970 ip_ecn
== IPTOS_ECN_CE
&& !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2971 if (inp
->inp_stat
->rxpackets
< ECN_MIN_CE_PROBES
) {
2972 tp
->t_ecn_recv_ce_pkt
++;
2973 } else if (tp
->t_ecn_recv_ce_pkt
> ECN_MAX_CE_RATIO
) {
2974 tcpstat
.tcps_ecn_fallback_ce
++;
2975 tcp_heuristic_ecn_aggressive(tp
);
2976 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2977 INP_INC_IFNET_STAT(inp
, ecn_fallback_ce
);
2979 /* We tracked the first ECN_MIN_CE_PROBES segments, we
2980 * now know that the path is good.
2982 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2987 * Try to determine if we are receiving a packet after a long time.
2988 * Use our own approximation of idletime to roughly measure remote
2989 * end's idle time. Since slowstart is used after an idle period
2990 * we want to avoid doing LRO if the remote end is not up to date
2991 * on initial window support and starts with 1 or 2 packets as its IW.
2993 if (sw_lro
&& (tp
->t_flagsext
& TF_LRO_OFFLOADED
) &&
2994 ((tcp_now
- tp
->t_rcvtime
) >= (TCP_IDLETIMEOUT(tp
)))) {
2998 /* Update rcvtime as a new segment was received on the connection */
2999 tp
->t_rcvtime
= tcp_now
;
3002 * Segment received on connection.
3003 * Reset idle time and keep-alive timer.
3005 if (TCPS_HAVEESTABLISHED(tp
->t_state
)) {
3006 tcp_keepalive_reset(tp
);
3009 mptcp_reset_keepalive(tp
);
3014 * Process options if not in LISTEN state,
3015 * else do it below (after getting remote address).
3017 if (tp
->t_state
!= TCPS_LISTEN
&& optp
) {
3018 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
3021 if (tp
->t_state
!= TCPS_LISTEN
&& (so
->so_flags
& SOF_MP_SUBFLOW
) &&
3022 mptcp_input_preproc(tp
, m
, th
, drop_hdrlen
) != 0) {
3023 tp
->t_flags
|= TF_ACKNOW
;
3024 (void) tcp_output(tp
);
3025 tcp_check_timer_state(tp
);
3026 socket_unlock(so
, 1);
3027 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
|
3028 DBG_FUNC_END
, 0, 0, 0, 0, 0);
3032 if (tp
->t_state
== TCPS_SYN_SENT
&& (thflags
& TH_SYN
)) {
3033 if (!(thflags
& TH_ACK
) ||
3034 (SEQ_GT(th
->th_ack
, tp
->iss
) &&
3035 SEQ_LEQ(th
->th_ack
, tp
->snd_max
))) {
3036 tcp_finalize_options(tp
, &to
, ifscope
);
3042 * Compute inter-packet arrival jitter. According to RFC 3550,
3043 * inter-packet arrival jitter is defined as the difference in
3044 * packet spacing at the receiver compared to the sender for a
3045 * pair of packets. When two packets of maximum segment size come
3046 * one after the other with consecutive sequence numbers, we
3047 * consider them as packets sent together at the sender and use
3048 * them as a pair to compute inter-packet arrival jitter. This
3049 * metric indicates the delay induced by the network components due
3050 * to queuing in edge/access routers.
3052 if (tp
->t_state
== TCPS_ESTABLISHED
&&
3053 (thflags
& (TH_SYN
| TH_FIN
| TH_RST
| TH_URG
| TH_ACK
| TH_ECE
| TH_PUSH
)) == TH_ACK
&&
3054 ((tp
->t_flags
& (TF_NEEDSYN
| TF_NEEDFIN
)) == 0) &&
3055 ((to
.to_flags
& TOF_TS
) == 0 ||
3056 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
3057 th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
3058 int seg_size
= tlen
;
3059 if (tp
->iaj_pktcnt
<= IAJ_IGNORE_PKTCNT
) {
3060 TCP_INC_VAR(tp
->iaj_pktcnt
, nlropkts
);
3063 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
3064 seg_size
= m
->m_pkthdr
.lro_pktlen
;
3066 if (tp
->iaj_size
== 0 || seg_size
> tp
->iaj_size
||
3067 (seg_size
== tp
->iaj_size
&& tp
->iaj_rcv_ts
== 0)) {
3069 * State related to inter-arrival jitter is
3070 * uninitialized or we are trying to find a good
3071 * first packet to start computing the metric
3073 update_iaj_state(tp
, seg_size
, 0);
3075 if (seg_size
== tp
->iaj_size
) {
3077 * Compute inter-arrival jitter taking
3078 * this packet as the second packet
3080 if (pktf_sw_lro_pkt
) {
3081 compute_iaj(tp
, nlropkts
,
3082 m
->m_pkthdr
.lro_elapsed
);
3084 compute_iaj(tp
, 1, 0);
3087 if (seg_size
< tp
->iaj_size
) {
3089 * There is a smaller packet in the stream.
3090 * Some times the maximum size supported
3091 * on a path can change if there is a new
3092 * link with smaller MTU. The receiver will
3093 * not know about this change. If there
3094 * are too many packets smaller than
3095 * iaj_size, we try to learn the iaj_size
3098 TCP_INC_VAR(tp
->iaj_small_pkt
, nlropkts
);
3099 if (tp
->iaj_small_pkt
> RESET_IAJ_SIZE_THRESH
) {
3100 update_iaj_state(tp
, seg_size
, 1);
3102 CLEAR_IAJ_STATE(tp
);
3105 update_iaj_state(tp
, seg_size
, 0);
3109 CLEAR_IAJ_STATE(tp
);
3111 #endif /* TRAFFIC_MGT */
3114 * Header prediction: check for the two common cases
3115 * of a uni-directional data xfer. If the packet has
3116 * no control flags, is in-sequence, the window didn't
3117 * change and we're not retransmitting, it's a
3118 * candidate. If the length is zero and the ack moved
3119 * forward, we're the sender side of the xfer. Just
3120 * free the data acked & wake any higher level process
3121 * that was blocked waiting for space. If the length
3122 * is non-zero and the ack didn't move, we're the
3123 * receiver side. If we're getting packets in-order
3124 * (the reassembly queue is empty), add the data to
3125 * the socket buffer and note that we need a delayed ack.
3126 * Make sure that the hidden state-flags are also off.
3127 * Since we check for TCPS_ESTABLISHED above, it can only
3130 if (tp
->t_state
== TCPS_ESTABLISHED
&&
3131 (thflags
& (TH_SYN
| TH_FIN
| TH_RST
| TH_URG
| TH_ACK
| TH_ECE
| TH_CWR
)) == TH_ACK
&&
3132 ((tp
->t_flags
& (TF_NEEDSYN
| TF_NEEDFIN
)) == 0) &&
3133 ((to
.to_flags
& TOF_TS
) == 0 ||
3134 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
3135 th
->th_seq
== tp
->rcv_nxt
&&
3136 tiwin
&& tiwin
== tp
->snd_wnd
&&
3137 tp
->snd_nxt
== tp
->snd_max
) {
3139 * If last ACK falls within this segment's sequence numbers,
3140 * record the timestamp.
3141 * NOTE that the test is modified according to the latest
3142 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
3144 if ((to
.to_flags
& TOF_TS
) != 0 &&
3145 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
3146 tp
->ts_recent_age
= tcp_now
;
3147 tp
->ts_recent
= to
.to_tsval
;
3151 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
3152 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
3153 tp
->snd_cwnd
>= tp
->snd_ssthresh
&&
3154 (!IN_FASTRECOVERY(tp
) &&
3155 ((!(SACK_ENABLED(tp
)) &&
3156 tp
->t_dupacks
< tp
->t_rexmtthresh
) ||
3157 (SACK_ENABLED(tp
) && to
.to_nsacks
== 0 &&
3158 TAILQ_EMPTY(&tp
->snd_holes
))))) {
3160 * this is a pure ack for outstanding data.
3162 ++tcpstat
.tcps_predack
;
3164 tcp_bad_rexmt_check(tp
, th
, &to
);
3166 /* Recalculate the RTT */
3167 tcp_compute_rtt(tp
, &to
, th
);
3169 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
3170 acked
= BYTES_ACKED(th
, tp
);
3171 tcpstat
.tcps_rcvackpack
++;
3172 tcpstat
.tcps_rcvackbyte
+= acked
;
3175 * Handle an ack that is in sequence during
3176 * congestion avoidance phase. The
3177 * calculations in this function
3178 * assume that snd_una is not updated yet.
3180 if (CC_ALGO(tp
)->congestion_avd
!= NULL
) {
3181 CC_ALGO(tp
)->congestion_avd(tp
, th
);
3183 tcp_ccdbg_trace(tp
, th
, TCP_CC_INSEQ_ACK_RCVD
);
3184 sbdrop(&so
->so_snd
, acked
);
3185 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
3186 VERIFY(acked
<= so
->so_msg_state
->msg_serial_bytes
);
3187 so
->so_msg_state
->msg_serial_bytes
-= acked
;
3189 tcp_sbsnd_trim(&so
->so_snd
);
3191 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
3192 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
)) {
3193 tp
->snd_recover
= th
->th_ack
- 1;
3195 tp
->snd_una
= th
->th_ack
;
3197 TCP_RESET_REXMT_STATE(tp
);
3200 * pull snd_wl2 up to prevent seq wrap relative
3203 tp
->snd_wl2
= th
->th_ack
;
3205 if (tp
->t_dupacks
> 0) {
3207 tp
->t_rexmtthresh
= tcprexmtthresh
;
3211 * If all outstanding data are acked, stop
3212 * retransmit timer, otherwise restart timer
3213 * using current (possibly backed-off) value.
3214 * If process is waiting for space,
3215 * wakeup/selwakeup/signal. If data
3216 * are ready to send, let tcp_output
3217 * decide between more output or persist.
3219 if (tp
->snd_una
== tp
->snd_max
) {
3220 tp
->t_timer
[TCPT_REXMT
] = 0;
3221 tp
->t_timer
[TCPT_PTO
] = 0;
3222 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
3223 tp
->t_timer
[TCPT_REXMT
] =
3224 OFFSET_FROM_START(tp
,
3227 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
3228 !TCP_DSACK_SEQ_IN_WINDOW(tp
,
3229 tp
->t_dsack_lastuna
, tp
->snd_una
)) {
3230 tcp_rxtseg_clean(tp
);
3233 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
3234 tp
->t_bwmeas
!= NULL
) {
3235 tcp_bwmeas_check(tp
);
3239 if (!SLIST_EMPTY(&tp
->t_notify_ack
)) {
3240 tcp_notify_acknowledgement(tp
, so
);
3243 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
)) {
3244 (void) tcp_output(tp
);
3247 tcp_tfo_rcv_ack(tp
, th
);
3251 tcp_check_timer_state(tp
);
3253 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
3255 socket_unlock(so
, 1);
3256 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
3259 } else if (th
->th_ack
== tp
->snd_una
&&
3260 LIST_EMPTY(&tp
->t_segq
) &&
3261 tlen
<= tcp_sbspace(tp
)) {
3263 * this is a pure, in-sequence data packet
3264 * with nothing on the reassembly queue and
3265 * we have enough buffer space to take it.
3269 * If this is a connection in steady state, start
3270 * coalescing packets belonging to this flow.
3273 tcp_lro_remove_state(tp
->t_inpcb
->inp_laddr
,
3274 tp
->t_inpcb
->inp_faddr
,
3275 tp
->t_inpcb
->inp_lport
,
3276 tp
->t_inpcb
->inp_fport
);
3277 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
3278 tp
->t_idleat
= tp
->rcv_nxt
;
3279 } else if (sw_lro
&& !pktf_sw_lro_pkt
&& !isipv6
&&
3280 (so
->so_flags
& SOF_USELRO
) &&
3281 !IFNET_IS_CELLULAR(m
->m_pkthdr
.rcvif
) &&
3282 (m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) &&
3283 ((th
->th_seq
- tp
->irs
) >
3284 (tp
->t_maxseg
<< lro_start
)) &&
3285 ((tp
->t_idleat
== 0) || ((th
->th_seq
-
3286 tp
->t_idleat
) > (tp
->t_maxseg
<< lro_start
)))) {
3287 tp
->t_flagsext
|= TF_LRO_OFFLOADED
;
3288 tcp_start_coalescing(ip
, th
, tlen
);
3292 /* Clean receiver SACK report if present */
3293 if (SACK_ENABLED(tp
) && tp
->rcv_numsacks
) {
3294 tcp_clean_sackreport(tp
);
3296 ++tcpstat
.tcps_preddat
;
3297 tp
->rcv_nxt
+= tlen
;
3299 * Pull snd_wl1 up to prevent seq wrap relative to
3302 tp
->snd_wl1
= th
->th_seq
;
3304 * Pull rcv_up up to prevent seq wrap relative to
3307 tp
->rcv_up
= tp
->rcv_nxt
;
3308 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
3309 tcpstat
.tcps_rcvbyte
+= tlen
;
3310 if (nstat_collect
) {
3311 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
3312 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3313 rxpackets
, m
->m_pkthdr
.lro_npkts
);
3315 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3318 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
,
3320 inp_set_activity_bitmap(inp
);
3324 * Calculate the RTT on the receiver only if the
3325 * connection is in streaming mode and the last
3326 * packet was not an end-of-write
3328 if (tp
->t_flags
& TF_STREAMING_ON
) {
3329 tcp_compute_rtt(tp
, &to
, th
);
3332 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
3333 TCP_AUTORCVBUF_MAX(ifp
));
3336 * Add data to socket buffer.
3338 so_recv_data_stat(so
, m
, 0);
3339 m_adj(m
, drop_hdrlen
); /* delayed header drop */
3342 * If message delivery (SOF_ENABLE_MSGS) is enabled on
3343 * this socket, deliver the packet received as an
3344 * in-order message with sequence number attached to it.
3347 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
3348 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
3350 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
3351 ip
= (struct ip
*)&saved_hdr
[0];
3353 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
3354 if (sbappendstream_rcvdemux(so
, m
,
3355 th
->th_seq
- (tp
->irs
+ 1), 0)) {
3356 mptcp_handle_input(so
);
3363 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3364 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
3365 th
->th_seq
, th
->th_ack
, th
->th_win
);
3369 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3370 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
3371 th
->th_seq
, th
->th_ack
, th
->th_win
);
3373 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3374 if (DELAY_ACK(tp
, th
)) {
3375 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3376 tp
->t_flags
|= TF_DELACK
;
3377 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3380 tp
->t_flags
|= TF_ACKNOW
;
3384 tcp_adaptive_rwtimo_check(tp
, tlen
);
3387 tcp_tfo_rcv_data(tp
);
3390 tcp_check_timer_state(tp
);
3392 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
3394 socket_unlock(so
, 1);
3395 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
3401 * Calculate amount of space in receive window,
3402 * and then do TCP input processing.
3403 * Receive window is amount of space in rcv queue,
3404 * but not less than advertised window.
3406 socket_lock_assert_owned(so
);
3407 win
= tcp_sbspace(tp
);
3410 } else { /* clip rcv window to 4K for modems */
3411 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0) {
3412 win
= min(win
, slowlink_wsize
);
3415 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
3418 * Ensure that the subflow receive window isn't greater
3419 * than the connection level receive window.
3421 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
3422 (mp_tp
= tptomptp(tp
))) {
3423 socket_lock_assert_owned(mptetoso(mp_tp
->mpt_mpte
));
3425 if (tp
->rcv_wnd
> (int)(mp_tp
->mpt_rcvadv
- (uint32_t)mp_tp
->mpt_rcvnxt
)) {
3426 tp
->rcv_wnd
= mp_tp
->mpt_rcvadv
- (uint32_t)mp_tp
->mpt_rcvnxt
;
3427 tcpstat
.tcps_mp_reducedwin
++;
3432 switch (tp
->t_state
) {
3434 * Initialize tp->rcv_nxt, and tp->irs, select an initial
3435 * tp->iss, and send a segment:
3436 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3437 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3438 * Fill in remote peer address fields if not previously specified.
3439 * Enter SYN_RECEIVED state, and process any other fields of this
3440 * segment in this state.
3443 struct sockaddr_in
*sin
;
3445 struct sockaddr_in6
*sin6
;
3448 socket_lock_assert_owned(so
);
3450 /* Clear the logging flags inherited from the listening socket */
3451 tp
->t_log_flags
= 0;
3452 tp
->t_flagsext
&= ~TF_LOGGED_CONN_SUMMARY
;
3456 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
3457 M_SONAME
, M_NOWAIT
);
3459 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "LISTEN malloc M_SONAME failed");
3462 bzero(sin6
, sizeof(*sin6
));
3463 sin6
->sin6_family
= AF_INET6
;
3464 sin6
->sin6_len
= sizeof(*sin6
);
3465 sin6
->sin6_addr
= ip6
->ip6_src
;
3466 sin6
->sin6_port
= th
->th_sport
;
3467 laddr6
= inp
->in6p_laddr
;
3468 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
3469 inp
->in6p_laddr
= ip6
->ip6_dst
;
3471 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
3473 inp
->in6p_laddr
= laddr6
;
3474 FREE(sin6
, M_SONAME
);
3475 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " LISTEN in6_pcbconnect failed");
3478 FREE(sin6
, M_SONAME
);
3482 socket_lock_assert_owned(so
);
3483 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
3486 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "LISTEN malloc M_SONAME failed");
3489 sin
->sin_family
= AF_INET
;
3490 sin
->sin_len
= sizeof(*sin
);
3491 sin
->sin_addr
= ip
->ip_src
;
3492 sin
->sin_port
= th
->th_sport
;
3493 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
3494 laddr
= inp
->inp_laddr
;
3495 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
3496 inp
->inp_laddr
= ip
->ip_dst
;
3498 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
,
3499 IFSCOPE_NONE
, NULL
)) {
3500 inp
->inp_laddr
= laddr
;
3501 FREE(sin
, M_SONAME
);
3502 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, " LISTEN in_pcbconnect failed");
3505 FREE(sin
, M_SONAME
);
3508 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
3509 tcp_finalize_options(tp
, &to
, ifscope
);
3511 if (tfo_enabled(tp
) && tcp_tfo_syn(tp
, &to
)) {
3518 tp
->iss
= tcp_new_isn(tp
);
3520 tp
->irs
= th
->th_seq
;
3521 tcp_sendseqinit(tp
);
3523 tp
->snd_recover
= tp
->snd_una
;
3525 * Initialization of the tcpcb for transaction;
3526 * set SND.WND = SEG.WND,
3527 * initialize CCsend and CCrecv.
3529 tp
->snd_wnd
= tiwin
; /* initial send-window */
3530 tp
->max_sndwnd
= tp
->snd_wnd
;
3531 tp
->t_flags
|= TF_ACKNOW
;
3532 tp
->t_unacksegs
= 0;
3533 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3534 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3535 tp
->t_state
= TCPS_SYN_RECEIVED
;
3536 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3537 TCP_CONN_KEEPINIT(tp
));
3538 tp
->t_connect_time
= tcp_now
;
3539 dropsocket
= 0; /* committed to socket */
3541 if (inp
->inp_flowhash
== 0) {
3542 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
3545 /* update flowinfo - RFC 6437 */
3546 if (inp
->inp_flow
== 0 &&
3547 inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
3548 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
3550 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
3554 /* reset the incomp processing flag */
3555 so
->so_flags
&= ~(SOF_INCOMP_INPROGRESS
);
3556 tcpstat
.tcps_accepts
++;
3557 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
| TH_CWR
)) {
3559 tp
->ecn_flags
|= (TE_SETUPRECEIVED
| TE_SENDIPECT
);
3563 * The address and connection state are finalized
3565 TCP_LOG_CONNECT(tp
, false, 0);
3571 * If the state is SYN_RECEIVED and the seg contains an ACK,
3572 * but not for our SYN/ACK, send a RST.
3574 case TCPS_SYN_RECEIVED
:
3575 if ((thflags
& TH_ACK
) &&
3576 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
3577 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3578 rstreason
= BANDLIM_RST_OPENPORT
;
3579 IF_TCP_STATINC(ifp
, ooopacket
);
3580 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_RECEIVED bad ACK");
3585 * In SYN_RECEIVED state, if we recv some SYNS with
3586 * window scale and others without, window scaling should
3587 * be disabled. Otherwise the window advertised will be
3588 * lower if we assume scaling and the other end does not.
3590 if ((thflags
& TH_SYN
) &&
3591 (tp
->irs
== th
->th_seq
) &&
3592 !(to
.to_flags
& TOF_SCALE
)) {
3593 tp
->t_flags
&= ~TF_RCVD_SCALE
;
3598 * If the state is SYN_SENT:
3599 * if seg contains an ACK, but not for our SYN, drop the input.
3600 * if seg contains a RST, then drop the connection.
3601 * if seg does not contain SYN, then drop it.
3602 * Otherwise this is an acceptable SYN segment
3603 * initialize tp->rcv_nxt and tp->irs
3604 * if seg contains ack then advance tp->snd_una
3605 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
3606 * arrange for segment to be acked (eventually)
3607 * continue processing rest of data/controls, beginning with URG
3610 if ((thflags
& TH_ACK
) &&
3611 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
3612 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3613 rstreason
= BANDLIM_UNLIMITED
;
3614 IF_TCP_STATINC(ifp
, ooopacket
);
3615 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_SENT bad ACK");
3618 if (thflags
& TH_RST
) {
3619 if ((thflags
& TH_ACK
) != 0) {
3620 if (tfo_enabled(tp
) &&
3621 !(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
)) {
3622 tcp_heuristic_tfo_rst(tp
);
3624 if ((tp
->ecn_flags
& (TE_SETUPSENT
| TE_RCVD_SYN_RST
)) == TE_SETUPSENT
) {
3626 * On local connections, send
3627 * non-ECN syn one time before
3628 * dropping the connection
3630 if (tp
->t_flags
& TF_LOCAL
) {
3631 tp
->ecn_flags
|= TE_RCVD_SYN_RST
;
3634 tcp_heuristic_ecn_synrst(tp
);
3638 (SO_FILT_HINT_LOCKED
|
3639 SO_FILT_HINT_CONNRESET
));
3640 tp
= tcp_drop(tp
, ECONNREFUSED
);
3641 postevent(so
, 0, EV_RESET
);
3643 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_SENT got RST");
3646 if ((thflags
& TH_SYN
) == 0) {
3647 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_SENT no SYN");
3650 tp
->snd_wnd
= th
->th_win
; /* initial send window */
3651 tp
->max_sndwnd
= tp
->snd_wnd
;
3653 tp
->irs
= th
->th_seq
;
3655 if (thflags
& TH_ACK
) {
3656 tcpstat
.tcps_connects
++;
3658 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
)) {
3659 /* ECN-setup SYN-ACK */
3660 tp
->ecn_flags
|= TE_SETUPRECEIVED
;
3661 if (TCP_ECN_ENABLED(tp
)) {
3662 tcp_heuristic_ecn_success(tp
);
3663 tcpstat
.tcps_ecn_client_success
++;
3666 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3667 tp
->t_rxtshift
== 0) {
3668 tcp_heuristic_ecn_success(tp
);
3669 tcpstat
.tcps_ecn_not_supported
++;
3671 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3672 tp
->t_rxtshift
> 0) {
3673 tcp_heuristic_ecn_loss(tp
);
3676 /* non-ECN-setup SYN-ACK */
3677 tp
->ecn_flags
&= ~TE_SENDIPECT
;
3680 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
3681 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
3682 mac_socketpeer_label_associate_mbuf(m
, so
);
3683 /* XXXMAC: SOCK_UNLOCK(so); */
3685 /* Do window scaling on this connection? */
3686 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3687 tp
->snd_scale
= tp
->requested_s_scale
;
3688 tp
->rcv_scale
= tp
->request_r_scale
;
3691 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
<< tp
->rcv_scale
);
3692 tp
->snd_una
++; /* SYN is acked */
3693 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
)) {
3694 tp
->snd_nxt
= tp
->snd_una
;
3698 * We have sent more in the SYN than what is being
3699 * acked. (e.g., TFO)
3700 * We should restart the sending from what the receiver
3701 * has acknowledged immediately.
3703 if (SEQ_GT(tp
->snd_nxt
, th
->th_ack
)) {
3705 * rdar://problem/33214601
3706 * There is a middlebox that acks all but one
3707 * byte and still drops the data.
3709 if (!(tp
->t_flagsext
& TF_FASTOPEN_FORCE_ENABLE
) &&
3710 (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3711 tp
->snd_max
== th
->th_ack
+ 1 &&
3712 tp
->snd_max
> tp
->snd_una
+ 1) {
3713 tcp_heuristic_tfo_middlebox(tp
);
3715 so
->so_error
= ENODATA
;
3717 (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_MP_SUB_ERROR
));
3719 tp
->t_tfo_stats
|= TFO_S_ONE_BYTE_PROXY
;
3722 tp
->snd_max
= tp
->snd_nxt
= th
->th_ack
;
3726 * If there's data, delay ACK; if there's also a FIN
3727 * ACKNOW will be turned on later.
3729 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3730 if (DELAY_ACK(tp
, th
) && tlen
!= 0) {
3731 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3732 tp
->t_flags
|= TF_DELACK
;
3733 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3736 tp
->t_flags
|= TF_ACKNOW
;
3739 * Received <SYN,ACK> in SYN_SENT[*] state.
3741 * SYN_SENT --> ESTABLISHED
3742 * SYN_SENT* --> FIN_WAIT_1
3744 tp
->t_starttime
= tcp_now
;
3745 tcp_sbrcv_tstmp_check(tp
);
3746 if (tp
->t_flags
& TF_NEEDFIN
) {
3747 DTRACE_TCP4(state__change
, void, NULL
,
3748 struct inpcb
*, inp
,
3749 struct tcpcb
*, tp
, int32_t,
3751 tp
->t_state
= TCPS_FIN_WAIT_1
;
3752 tp
->t_flags
&= ~TF_NEEDFIN
;
3755 TCP_LOG_CONNECTION_SUMMARY(tp
);
3757 DTRACE_TCP4(state__change
, void, NULL
,
3758 struct inpcb
*, inp
, struct tcpcb
*,
3759 tp
, int32_t, TCPS_ESTABLISHED
);
3760 tp
->t_state
= TCPS_ESTABLISHED
;
3761 tp
->t_timer
[TCPT_KEEP
] =
3762 OFFSET_FROM_START(tp
,
3763 TCP_CONN_KEEPIDLE(tp
));
3764 if (nstat_collect
) {
3765 nstat_route_connect_success(
3766 inp
->inp_route
.ro_rt
);
3769 * The SYN is acknowledged but una is not
3770 * updated yet. So pass the value of
3771 * ack to compute sndbytes correctly
3773 inp_count_sndbytes(inp
, th
->th_ack
);
3777 * Do not send the connect notification for additional
3778 * subflows until ACK for 3-way handshake arrives.
3780 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3781 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3782 isconnected
= FALSE
;
3787 if ((tp
->t_tfo_flags
& (TFO_F_COOKIE_REQ
| TFO_F_COOKIE_SENT
)) ||
3788 (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
)) {
3789 tcp_tfo_synack(tp
, &to
);
3791 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3792 SEQ_LT(tp
->snd_una
, th
->th_ack
)) {
3793 tp
->t_tfo_stats
|= TFO_S_SYN_DATA_ACKED
;
3794 tcpstat
.tcps_tfo_syn_data_acked
++;
3796 if (so
->so_flags
& SOF_MP_SUBFLOW
) {
3797 so
->so_flags1
|= SOF1_TFO_REWIND
;
3800 tcp_tfo_rcv_probe(tp
, tlen
);
3805 * Received initial SYN in SYN-SENT[*] state => simul-
3806 * taneous open. If segment contains CC option and there is
3807 * a cached CC, apply TAO test; if it succeeds, connection is
3808 * half-synchronized. Otherwise, do 3-way handshake:
3809 * SYN-SENT -> SYN-RECEIVED
3810 * SYN-SENT* -> SYN-RECEIVED*
3812 tp
->t_flags
|= TF_ACKNOW
;
3813 tp
->t_timer
[TCPT_REXMT
] = 0;
3814 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3815 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3816 tp
->t_state
= TCPS_SYN_RECEIVED
;
3819 * During simultaneous open, TFO should not be used.
3820 * So, we disable it here, to prevent that data gets
3821 * sent on the SYN/ACK.
3823 tcp_disable_tfo(tp
);
3828 * Advance th->th_seq to correspond to first data byte.
3829 * If data, trim to stay within window,
3830 * dropping FIN if necessary.
3833 if (tlen
> tp
->rcv_wnd
) {
3834 todrop
= tlen
- tp
->rcv_wnd
;
3838 tcpstat
.tcps_rcvpackafterwin
++;
3839 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3841 tp
->snd_wl1
= th
->th_seq
- 1;
3842 tp
->rcv_up
= th
->th_seq
;
3844 * Client side of transaction: already sent SYN and data.
3845 * If the remote host used T/TCP to validate the SYN,
3846 * our data will be ACK'd; if so, enter normal data segment
3847 * processing in the middle of step 5, ack processing.
3848 * Otherwise, goto step 6.
3850 if (thflags
& TH_ACK
) {
3855 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
3856 * do normal processing.
3858 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
3862 case TCPS_TIME_WAIT
:
3863 break; /* continue normal processing */
3865 /* Received a SYN while connection is already established.
3866 * This is a "half open connection and other anomalies" described
3867 * in RFC793 page 34, send an ACK so the remote reset the connection
3868 * or recovers by adjusting its sequence numbering. Sending an ACK is
3869 * in accordance with RFC 5961 Section 4.2
3871 case TCPS_ESTABLISHED
:
3872 if (thflags
& TH_SYN
) {
3873 /* Drop the packet silently if we have reached the limit */
3874 if (tcp_do_rfc5961
&& tcp_is_ack_ratelimited(tp
)) {
3875 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 rate limited");
3878 /* Send challenge ACK */
3879 tcpstat
.tcps_synchallenge
++;
3880 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 challenge ACK");
3888 * States other than LISTEN or SYN_SENT.
3889 * First check the RST flag and sequence number since reset segments
3890 * are exempt from the timestamp and connection count tests. This
3891 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
3892 * below which allowed reset segments in half the sequence space
3893 * to fall though and be processed (which gives forged reset
3894 * segments with a random sequence number a 50 percent chance of
3895 * killing a connection).
3896 * Then check timestamp, if present.
3897 * Then check the connection count, if present.
3898 * Then check that at least some bytes of segment are within
3899 * receive window. If segment begins before rcv_nxt,
3900 * drop leading data (and SYN); if nothing left, just ack.
3903 * If the RST bit is set, check the sequence number to see
3904 * if this is a valid reset segment.
3906 * In all states except SYN-SENT, all reset (RST) segments
3907 * are validated by checking their SEQ-fields. A reset is
3908 * valid if its sequence number is in the window.
3909 * Note: this does not take into account delayed ACKs, so
3910 * we should test against last_ack_sent instead of rcv_nxt.
3911 * The sequence number in the reset segment is normally an
3912 * echo of our outgoing acknowlegement numbers, but some hosts
3913 * send a reset with the sequence number at the rightmost edge
3914 * of our receive window, and we have to handle this case.
3915 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
3916 * that brute force RST attacks are possible. To combat this,
3917 * we use a much stricter check while in the ESTABLISHED state,
3918 * only accepting RSTs where the sequence number is equal to
3919 * last_ack_sent. In all other states (the states in which a
3920 * RST is more likely), the more permissive check is used.
3921 * RFC 5961 Section 3.2: if the RST bit is set, sequence # is
3922 * within the receive window and last_ack_sent == seq,
3923 * then reset the connection. Otherwise if the seq doesn't
3924 * match last_ack_sent, TCP must send challenge ACK. Perform
3925 * rate limitation when sending the challenge ACK.
3926 * If we have multiple segments in flight, the intial reset
3927 * segment sequence numbers will be to the left of last_ack_sent,
3928 * but they will eventually catch up.
3929 * In any case, it never made sense to trim reset segments to
3930 * fit the receive window since RFC 1122 says:
3931 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
3933 * A TCP SHOULD allow a received RST segment to include data.
3936 * It has been suggested that a RST segment could contain
3937 * ASCII text that encoded and explained the cause of the
3938 * RST. No standard has yet been established for such
3941 * If the reset segment passes the sequence number test examine
3943 * SYN_RECEIVED STATE:
3944 * If passive open, return to LISTEN state.
3945 * If active open, inform user that connection was refused.
3946 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
3947 * Inform user that connection was reset, and close tcb.
3948 * CLOSING, LAST_ACK STATES:
3951 * Drop the segment - see Stevens, vol. 2, p. 964 and
3954 * Radar 4803931: Allows for the case where we ACKed the FIN but
3955 * there is already a RST in flight from the peer.
3956 * In that case, accept the RST for non-established
3957 * state if it's one off from last_ack_sent.
3960 if (thflags
& TH_RST
) {
3961 if ((SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3962 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) ||
3963 (tp
->rcv_wnd
== 0 &&
3964 ((tp
->last_ack_sent
== th
->th_seq
) ||
3965 ((tp
->last_ack_sent
- 1) == th
->th_seq
)))) {
3966 if (tcp_do_rfc5961
== 0 || tp
->last_ack_sent
== th
->th_seq
) {
3967 switch (tp
->t_state
) {
3968 case TCPS_SYN_RECEIVED
:
3969 IF_TCP_STATINC(ifp
, rstinsynrcv
);
3970 so
->so_error
= ECONNREFUSED
;
3973 case TCPS_ESTABLISHED
:
3974 if (tcp_do_rfc5961
== 0 && tp
->last_ack_sent
!= th
->th_seq
) {
3975 tcpstat
.tcps_badrst
++;
3976 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 bad RST");
3979 if (TCP_ECN_ENABLED(tp
) &&
3980 tp
->snd_una
== tp
->iss
+ 1 &&
3981 SEQ_GT(tp
->snd_max
, tp
->snd_una
)) {
3983 * If the first data packet on an
3984 * ECN connection, receives a RST
3985 * increment the heuristic
3987 tcp_heuristic_ecn_droprst(tp
);
3989 case TCPS_FIN_WAIT_1
:
3990 case TCPS_CLOSE_WAIT
:
3994 case TCPS_FIN_WAIT_2
:
3995 so
->so_error
= ECONNRESET
;
3997 postevent(so
, 0, EV_RESET
);
3999 (SO_FILT_HINT_LOCKED
|
4000 SO_FILT_HINT_CONNRESET
));
4002 tcpstat
.tcps_drops
++;
4011 case TCPS_TIME_WAIT
:
4014 } else if (tcp_do_rfc5961
) {
4015 tcpstat
.tcps_badrst
++;
4016 /* Drop if we have reached the ACK limit */
4017 if (tcp_is_ack_ratelimited(tp
)) {
4018 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 rate limited");
4021 /* Send challenge ACK */
4022 tcpstat
.tcps_rstchallenge
++;
4023 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "ESTABLISHED rfc5961 challenge ACK");
4032 * RFC 1323 PAWS: If we have a timestamp reply on this segment
4033 * and it's less than ts_recent, drop it.
4035 if ((to
.to_flags
& TOF_TS
) != 0 && tp
->ts_recent
&&
4036 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
4037 /* Check to see if ts_recent is over 24 days old. */
4038 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
4040 * Invalidate ts_recent. If this segment updates
4041 * ts_recent, the age will be reset later and ts_recent
4042 * will get a valid value. If it does not, setting
4043 * ts_recent to zero will at least satisfy the
4044 * requirement that zero be placed in the timestamp
4045 * echo reply when ts_recent isn't valid. The
4046 * age isn't reset until we get a valid ts_recent
4047 * because we don't want out-of-order segments to be
4048 * dropped when ts_recent is old.
4052 tcpstat
.tcps_rcvduppack
++;
4053 tcpstat
.tcps_rcvdupbyte
+= tlen
;
4055 tcpstat
.tcps_pawsdrop
++;
4058 * PAWS-drop when ECN is being used? That indicates
4059 * that ECT-marked packets take a different path, with
4060 * different congestion-characteristics.
4062 * Only fallback when we did send less than 2GB as PAWS
4063 * really has no reason to kick in earlier.
4065 if (TCP_ECN_ENABLED(tp
) &&
4066 inp
->inp_stat
->rxbytes
< 2147483648) {
4067 INP_INC_IFNET_STAT(inp
, ecn_fallback_reorder
);
4068 tcpstat
.tcps_ecn_fallback_reorder
++;
4069 tcp_heuristic_ecn_aggressive(tp
);
4072 if (nstat_collect
) {
4073 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
,
4074 1, tlen
, NSTAT_RX_FLAG_DUPLICATE
);
4075 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4077 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4079 tp
->t_stat
.rxduplicatebytes
+= tlen
;
4080 inp_set_activity_bitmap(inp
);
4090 * In the SYN-RECEIVED state, validate that the packet belongs to
4091 * this connection before trimming the data to fit the receive
4092 * window. Check the sequence number versus IRS since we know
4093 * the sequence numbers haven't wrapped. This is a partial fix
4094 * for the "LAND" DoS attack.
4096 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
4097 rstreason
= BANDLIM_RST_OPENPORT
;
4098 IF_TCP_STATINC(ifp
, dospacket
);
4099 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SYN_RECEIVED bad SEQ");
4104 * Check if there is old data at the beginning of the window
4105 * i.e. the sequence number is before rcv_nxt
4107 todrop
= tp
->rcv_nxt
- th
->th_seq
;
4109 boolean_t is_syn_set
= FALSE
;
4111 if (thflags
& TH_SYN
) {
4115 if (th
->th_urp
> 1) {
4123 * Following if statement from Stevens, vol. 2, p. 960.
4124 * The amount of duplicate data is greater than or equal
4125 * to the size of the segment - entire segment is duplicate
4128 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
4130 * Any valid FIN must be to the left of the window.
4131 * At this point the FIN must be a duplicate or out
4132 * of sequence; drop it.
4137 * Send an ACK to resynchronize and drop any data.
4138 * But keep on processing for RST or ACK.
4140 * If the SYN bit was originally set, then only send
4141 * an ACK if we are not rate-limiting this connection.
4143 if (tcp_do_rfc5961
&& is_syn_set
) {
4144 if (!tcp_is_ack_ratelimited(tp
)) {
4145 tcpstat
.tcps_synchallenge
++;
4146 tp
->t_flags
|= TF_ACKNOW
;
4149 tp
->t_flags
|= TF_ACKNOW
;
4153 /* This could be a keepalive */
4154 soevent(so
, SO_FILT_HINT_LOCKED
|
4155 SO_FILT_HINT_KEEPALIVE
);
4158 tcpstat
.tcps_rcvduppack
++;
4159 tcpstat
.tcps_rcvdupbyte
+= todrop
;
4161 tcpstat
.tcps_rcvpartduppack
++;
4162 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
4165 if (TCP_DSACK_ENABLED(tp
) && todrop
> 1) {
4167 * Note the duplicate data sequence space so that
4168 * it can be reported in DSACK option.
4170 tp
->t_dsack_lseq
= th
->th_seq
;
4171 tp
->t_dsack_rseq
= th
->th_seq
+ todrop
;
4172 tp
->t_flags
|= TF_ACKNOW
;
4174 if (nstat_collect
) {
4175 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
, 1,
4176 todrop
, NSTAT_RX_FLAG_DUPLICATE
);
4177 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
4178 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, todrop
);
4179 tp
->t_stat
.rxduplicatebytes
+= todrop
;
4180 inp_set_activity_bitmap(inp
);
4182 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
4183 th
->th_seq
+= todrop
;
4185 if (th
->th_urp
> todrop
) {
4186 th
->th_urp
-= todrop
;
4194 * If new data are received on a connection after the user
4195 * processes are gone, then RST the other end.
4196 * Send also a RST when we received a data segment after we've
4197 * sent our FIN when the socket is defunct.
4198 * Note that an MPTCP subflow socket would have SS_NOFDREF set
4199 * by default. So, if it's an MPTCP-subflow we rather check the
4200 * MPTCP-level's socket state for SS_NOFDREF.
4203 boolean_t close_it
= FALSE
;
4205 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && (so
->so_state
& SS_NOFDREF
) &&
4206 tp
->t_state
> TCPS_CLOSE_WAIT
) {
4207 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SS_NOFDREF");
4211 if ((so
->so_flags
& SOF_MP_SUBFLOW
) && (mptetoso(tptomptp(tp
)->mpt_mpte
)->so_state
& SS_NOFDREF
) &&
4212 tp
->t_state
> TCPS_CLOSE_WAIT
) {
4213 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SOF_MP_SUBFLOW SS_NOFDREF");
4217 if ((so
->so_flags
& SOF_DEFUNCT
) && tp
->t_state
> TCPS_FIN_WAIT_1
) {
4218 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "SOF_DEFUNCT");
4224 tcpstat
.tcps_rcvafterclose
++;
4225 rstreason
= BANDLIM_UNLIMITED
;
4226 IF_TCP_STATINC(ifp
, cleanup
);
4232 * If segment ends after window, drop trailing data
4233 * (and PUSH and FIN); if nothing left, just ACK.
4235 todrop
= (th
->th_seq
+ tlen
) - (tp
->rcv_nxt
+ tp
->rcv_wnd
);
4237 tcpstat
.tcps_rcvpackafterwin
++;
4238 if (todrop
>= tlen
) {
4239 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
4241 * If a new connection request is received
4242 * while in TIME_WAIT, drop the old connection
4243 * and start over if the sequence numbers
4244 * are above the previous ones.
4246 if (thflags
& TH_SYN
&&
4247 tp
->t_state
== TCPS_TIME_WAIT
&&
4248 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
4249 iss
= tcp_new_isn(tp
);
4251 socket_unlock(so
, 1);
4255 * If window is closed can only take segments at
4256 * window edge, and have to drop data and PUSH from
4257 * incoming segments. Continue processing, but
4258 * remember to ack. Otherwise, drop segment
4261 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
4262 tp
->t_flags
|= TF_ACKNOW
;
4263 tcpstat
.tcps_rcvwinprobe
++;
4268 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
4272 thflags
&= ~(TH_PUSH
| TH_FIN
);
4276 * If last ACK falls within this segment's sequence numbers,
4277 * record its timestamp.
4279 * 1) That the test incorporates suggestions from the latest
4280 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
4281 * 2) That updating only on newer timestamps interferes with
4282 * our earlier PAWS tests, so this check should be solely
4283 * predicated on the sequence space of this segment.
4284 * 3) That we modify the segment boundary check to be
4285 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
4286 * instead of RFC1323's
4287 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
4288 * This modified check allows us to overcome RFC1323's
4289 * limitations as described in Stevens TCP/IP Illustrated
4290 * Vol. 2 p.869. In such cases, we can still calculate the
4291 * RTT correctly when RCV.NXT == Last.ACK.Sent.
4293 if ((to
.to_flags
& TOF_TS
) != 0 &&
4294 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
) &&
4295 SEQ_LEQ(tp
->last_ack_sent
, th
->th_seq
+ tlen
+
4296 ((thflags
& (TH_SYN
| TH_FIN
)) != 0))) {
4297 tp
->ts_recent_age
= tcp_now
;
4298 tp
->ts_recent
= to
.to_tsval
;
4302 * Stevens: If a SYN is in the window, then this is an
4303 * error and we send an RST and drop the connection.
4305 * RFC 5961 Section 4.2
4306 * Send challenge ACK for any SYN in synchronized state
4307 * Perform rate limitation in doing so.
4309 if (thflags
& TH_SYN
) {
4310 if (tcp_do_rfc5961
) {
4311 tcpstat
.tcps_badsyn
++;
4312 /* Drop if we have reached ACK limit */
4313 if (tcp_is_ack_ratelimited(tp
)) {
4314 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 bad SYN rate limited");
4317 /* Send challenge ACK */
4318 tcpstat
.tcps_synchallenge
++;
4319 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 bad SYN challenge ack");
4323 tp
= tcp_drop(tp
, ECONNRESET
);
4324 rstreason
= BANDLIM_UNLIMITED
;
4325 postevent(so
, 0, EV_RESET
);
4326 IF_TCP_STATINC(ifp
, synwindow
);
4327 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad SYN");
4333 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
4334 * flag is on (half-synchronized state), then queue data for
4335 * later processing; else drop segment and return.
4337 if ((thflags
& TH_ACK
) == 0) {
4338 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
4339 (tp
->t_flags
& TF_NEEDSYN
)) {
4340 if ((tfo_enabled(tp
))) {
4342 * So, we received a valid segment while in
4343 * SYN-RECEIVED (TF_NEEDSYN is actually never
4344 * set, so this is dead code).
4345 * As this cannot be an RST (see that if a bit
4346 * higher), and it does not have the ACK-flag
4347 * set, we want to retransmit the SYN/ACK.
4348 * Thus, we have to reset snd_nxt to snd_una to
4349 * trigger the going back to sending of the
4350 * SYN/ACK. This is more consistent with the
4351 * behavior of tcp_output(), which expects
4352 * to send the segment that is pointed to by
4355 tp
->snd_nxt
= tp
->snd_una
;
4358 * We need to make absolutely sure that we are
4359 * going to reply upon a duplicate SYN-segment.
4361 if (th
->th_flags
& TH_SYN
) {
4367 } else if (tp
->t_flags
& TF_ACKNOW
) {
4368 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad ACK");
4371 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "bad ACK");
4380 switch (tp
->t_state
) {
4382 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
4383 * ESTABLISHED state and continue processing.
4384 * The ACK was checked above.
4386 case TCPS_SYN_RECEIVED
:
4388 tcpstat
.tcps_connects
++;
4390 /* Do window scaling? */
4391 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
4392 tp
->snd_scale
= tp
->requested_s_scale
;
4393 tp
->rcv_scale
= tp
->request_r_scale
;
4394 tp
->snd_wnd
= th
->th_win
<< tp
->snd_scale
;
4395 tp
->max_sndwnd
= tp
->snd_wnd
;
4396 tiwin
= tp
->snd_wnd
;
4400 * SYN-RECEIVED -> ESTABLISHED
4401 * SYN-RECEIVED* -> FIN-WAIT-1
4403 tp
->t_starttime
= tcp_now
;
4404 tcp_sbrcv_tstmp_check(tp
);
4405 if (tp
->t_flags
& TF_NEEDFIN
) {
4406 DTRACE_TCP4(state__change
, void, NULL
,
4407 struct inpcb
*, inp
,
4408 struct tcpcb
*, tp
, int32_t, TCPS_FIN_WAIT_1
);
4409 tp
->t_state
= TCPS_FIN_WAIT_1
;
4410 tp
->t_flags
&= ~TF_NEEDFIN
;
4412 TCP_LOG_CONNECTION_SUMMARY(tp
);
4414 DTRACE_TCP4(state__change
, void, NULL
,
4415 struct inpcb
*, inp
,
4416 struct tcpcb
*, tp
, int32_t, TCPS_ESTABLISHED
);
4417 tp
->t_state
= TCPS_ESTABLISHED
;
4418 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
4419 TCP_CONN_KEEPIDLE(tp
));
4420 if (nstat_collect
) {
4421 nstat_route_connect_success(
4422 tp
->t_inpcb
->inp_route
.ro_rt
);
4425 * The SYN is acknowledged but una is not updated
4426 * yet. So pass the value of ack to compute
4427 * sndbytes correctly
4429 inp_count_sndbytes(inp
, th
->th_ack
);
4432 * If segment contains data or ACK, will call tcp_reass()
4433 * later; if not, do so now to pass queued data to user.
4435 if (tlen
== 0 && (thflags
& TH_FIN
) == 0) {
4437 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
4438 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
4440 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
4441 ip
= (struct ip
*)&saved_hdr
[0];
4443 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
4444 (void) tcp_reass(tp
, (struct tcphdr
*)0, &tlen
,
4445 NULL
, ifp
, &read_wakeup
);
4448 tp
->snd_wl1
= th
->th_seq
- 1;
4452 * Do not send the connect notification for additional subflows
4453 * until ACK for 3-way handshake arrives.
4455 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
4456 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
4457 isconnected
= FALSE
;
4461 if ((tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)) {
4462 /* Done this when receiving the SYN */
4463 isconnected
= FALSE
;
4465 OSDecrementAtomic(&tcp_tfo_halfcnt
);
4467 /* Panic if something has gone terribly wrong. */
4468 VERIFY(tcp_tfo_halfcnt
>= 0);
4470 tp
->t_tfo_flags
&= ~TFO_F_COOKIE_VALID
;
4474 * In case there is data in the send-queue (e.g., TFO is being
4475 * used, or connectx+data has been done), then if we would
4476 * "FALLTHROUGH", we would handle this ACK as if data has been
4477 * acknowledged. But, we have to prevent this. And this
4478 * can be prevented by increasing snd_una by 1, so that the
4479 * SYN is not considered as data (snd_una++ is actually also
4480 * done in SYN_SENT-state as part of the regular TCP stack).
4482 * In case there is data on this ack as well, the data will be
4483 * handled by the label "dodata" right after step6.
4485 if (so
->so_snd
.sb_cc
) {
4486 tp
->snd_una
++; /* SYN is acked */
4487 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
)) {
4488 tp
->snd_nxt
= tp
->snd_una
;
4492 * No duplicate-ACK handling is needed. So, we
4493 * directly advance to processing the ACK (aka,
4494 * updating the RTT estimation,...)
4496 * But, we first need to handle eventual SACKs,
4497 * because TFO will start sending data with the
4498 * SYN/ACK, so it might be that the client
4499 * includes a SACK with its ACK.
4501 if (SACK_ENABLED(tp
) &&
4502 (to
.to_nsacks
> 0 ||
4503 !TAILQ_EMPTY(&tp
->snd_holes
))) {
4504 tcp_sack_doack(tp
, &to
, th
,
4514 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4515 * ACKs. If the ack is in the range
4516 * tp->snd_una < th->th_ack <= tp->snd_max
4517 * then advance tp->snd_una to th->th_ack and drop
4518 * data from the retransmission queue. If this ACK reflects
4519 * more up to date window information we update our window information.
4521 case TCPS_ESTABLISHED
:
4522 case TCPS_FIN_WAIT_1
:
4523 case TCPS_FIN_WAIT_2
:
4524 case TCPS_CLOSE_WAIT
:
4527 case TCPS_TIME_WAIT
:
4528 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
4529 tcpstat
.tcps_rcvacktoomuch
++;
4530 if (tcp_do_rfc5961
&& tcp_is_ack_ratelimited(tp
)) {
4531 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 rcvacktoomuch");
4537 if (tcp_do_rfc5961
&& SEQ_LT(th
->th_ack
, tp
->snd_una
- tp
->max_sndwnd
)) {
4538 if (tcp_is_ack_ratelimited(tp
)) {
4539 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "rfc5961 bad ACK");
4545 if (SACK_ENABLED(tp
) && to
.to_nsacks
> 0) {
4546 recvd_dsack
= tcp_sack_process_dsack(tp
, &to
, th
);
4548 * If DSACK is received and this packet has no
4549 * other SACK information, it can be dropped.
4550 * We do not want to treat it as a duplicate ack.
4553 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) &&
4554 to
.to_nsacks
== 0) {
4555 tcp_bad_rexmt_check(tp
, th
, &to
);
4560 if (SACK_ENABLED(tp
) &&
4561 (to
.to_nsacks
> 0 || !TAILQ_EMPTY(&tp
->snd_holes
))) {
4562 tcp_sack_doack(tp
, &to
, th
, &sack_bytes_acked
);
4566 if (tp
->t_mpuna
&& SEQ_GEQ(th
->th_ack
, tp
->t_mpuna
)) {
4567 if (tp
->t_mpflags
& TMPF_PREESTABLISHED
) {
4568 /* MP TCP establishment succeeded */
4570 if (tp
->t_mpflags
& TMPF_JOINED_FLOW
) {
4571 if (tp
->t_mpflags
& TMPF_SENT_JOIN
) {
4573 ~TMPF_PREESTABLISHED
;
4577 tp
->t_timer
[TCPT_JACK_RXMT
] = 0;
4578 tp
->t_mprxtshift
= 0;
4581 isconnected
= FALSE
;
4590 tcp_tfo_rcv_ack(tp
, th
);
4593 * If we have outstanding data (other than
4594 * a window probe), this is a completely
4595 * duplicate ack and the ack is the biggest we've seen.
4597 * Need to accommodate a change in window on duplicate acks
4598 * to allow operating systems that update window during
4599 * recovery with SACK
4601 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
4602 if (tlen
== 0 && (tiwin
== tp
->snd_wnd
||
4603 (to
.to_nsacks
> 0 && sack_bytes_acked
> 0))) {
4605 * If both ends send FIN at the same time,
4606 * then the ack will be a duplicate ack
4607 * but we have to process the FIN. Check
4608 * for this condition and process the FIN
4609 * instead of the dupack
4611 if ((thflags
& TH_FIN
) &&
4612 !TCPS_HAVERCVDFIN(tp
->t_state
)) {
4618 * MPTCP options that are ignored must
4619 * not be treated as duplicate ACKs.
4621 if (to
.to_flags
& TOF_MPTCP
) {
4625 if ((isconnected
) && (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
4626 mptcplog((LOG_DEBUG
, "MPTCP "
4627 "Sockets: bypass ack recovery\n"),
4629 MPTCP_LOGLVL_VERBOSE
);
4634 * If a duplicate acknowledgement was seen
4635 * after ECN, it indicates packet loss in
4636 * addition to ECN. Reset INRECOVERY flag
4637 * so that we can process partial acks
4640 if (tp
->ecn_flags
& TE_INRECOVERY
) {
4641 tp
->ecn_flags
&= ~TE_INRECOVERY
;
4644 tcpstat
.tcps_rcvdupack
++;
4648 * Check if we need to reset the limit on
4651 if (tp
->t_early_rexmt_count
> 0 &&
4653 (tp
->t_early_rexmt_win
+
4654 TCP_EARLY_REXMT_WIN
))) {
4655 tp
->t_early_rexmt_count
= 0;
4659 * Is early retransmit needed? We check for
4660 * this when the connection is waiting for
4661 * duplicate acks to enter fast recovery.
4663 if (!IN_FASTRECOVERY(tp
)) {
4664 tcp_early_rexmt_check(tp
, th
);
4668 * If we've seen exactly rexmt threshold
4669 * of duplicate acks, assume a packet
4670 * has been dropped and retransmit it.
4671 * Kludge snd_nxt & the congestion
4672 * window so we send only this one
4675 * We know we're losing at the current
4676 * window size so do congestion avoidance
4677 * (set ssthresh to half the current window
4678 * and pull our congestion window back to
4679 * the new ssthresh).
4681 * Dup acks mean that packets have left the
4682 * network (they're now cached at the receiver)
4683 * so bump cwnd by the amount in the receiver
4684 * to keep a constant cwnd packets in the
4687 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
4688 (th
->th_ack
!= tp
->snd_una
4689 && sack_bytes_acked
== 0)) {
4691 tp
->t_rexmtthresh
= tcprexmtthresh
;
4692 } else if (tp
->t_dupacks
> tp
->t_rexmtthresh
||
4693 IN_FASTRECOVERY(tp
)) {
4695 * If this connection was seeing packet
4696 * reordering, then recovery might be
4697 * delayed to disambiguate between
4698 * reordering and loss
4700 if (SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
) &&
4702 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
)) ==
4703 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
)) {
4705 * Since the SACK information is already
4706 * updated, this ACK will be dropped
4711 if (SACK_ENABLED(tp
)
4712 && IN_FASTRECOVERY(tp
)) {
4716 * Compute the amount of data in flight first.
4717 * We can inject new data into the pipe iff
4718 * we have less than 1/2 the original window's
4719 * worth of data in flight.
4721 awnd
= (tp
->snd_nxt
- tp
->snd_fack
) +
4722 tp
->sackhint
.sack_bytes_rexmit
;
4723 if (awnd
< tp
->snd_ssthresh
) {
4724 tp
->snd_cwnd
+= tp
->t_maxseg
;
4725 if (tp
->snd_cwnd
> tp
->snd_ssthresh
) {
4726 tp
->snd_cwnd
= tp
->snd_ssthresh
;
4730 tp
->snd_cwnd
+= tp
->t_maxseg
;
4733 /* Process any window updates */
4734 if (tiwin
> tp
->snd_wnd
) {
4735 tcp_update_window(tp
, thflags
,
4738 tcp_ccdbg_trace(tp
, th
,
4739 TCP_CC_IN_FASTRECOVERY
);
4741 (void) tcp_output(tp
);
4744 } else if (tp
->t_dupacks
== tp
->t_rexmtthresh
) {
4745 tcp_seq onxt
= tp
->snd_nxt
;
4748 * If we're doing sack, check to
4749 * see if we're already in sack
4750 * recovery. If we're not doing sack,
4751 * check to see if we're in newreno
4754 if (SACK_ENABLED(tp
)) {
4755 if (IN_FASTRECOVERY(tp
)) {
4758 } else if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
4762 if (SEQ_LEQ(th
->th_ack
,
4768 if (tp
->t_flags
& TF_SENTFIN
) {
4769 tp
->snd_recover
= tp
->snd_max
- 1;
4771 tp
->snd_recover
= tp
->snd_max
;
4773 tp
->t_timer
[TCPT_PTO
] = 0;
4777 * If the connection has seen pkt
4778 * reordering, delay recovery until
4779 * it is clear that the packet
4782 if (SACK_ENABLED(tp
) &&
4784 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
))
4785 == TF_PKTS_REORDERED
&&
4786 !IN_FASTRECOVERY(tp
) &&
4787 tp
->t_reorderwin
> 0 &&
4788 (tp
->t_state
== TCPS_ESTABLISHED
||
4789 tp
->t_state
== TCPS_FIN_WAIT_1
)) {
4790 tp
->t_timer
[TCPT_DELAYFR
] =
4791 OFFSET_FROM_START(tp
,
4793 tp
->t_flagsext
|= TF_DELAY_RECOVERY
;
4794 tcpstat
.tcps_delay_recovery
++;
4795 tcp_ccdbg_trace(tp
, th
,
4796 TCP_CC_DELAY_FASTRECOVERY
);
4800 tcp_rexmt_save_state(tp
);
4802 * If the current tcp cc module has
4803 * defined a hook for tasks to run
4804 * before entering FR, call it
4806 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
4807 CC_ALGO(tp
)->pre_fr(tp
);
4809 ENTER_FASTRECOVERY(tp
);
4810 tp
->t_timer
[TCPT_REXMT
] = 0;
4811 if (TCP_ECN_ENABLED(tp
)) {
4812 tp
->ecn_flags
|= TE_SENDCWR
;
4815 if (SACK_ENABLED(tp
)) {
4816 tcpstat
.tcps_sack_recovery_episode
++;
4817 tp
->t_sack_recovery_episode
++;
4818 tp
->sack_newdata
= tp
->snd_nxt
;
4819 tp
->snd_cwnd
= tp
->t_maxseg
;
4821 ~TF_CWND_NONVALIDATED
;
4823 /* Process any window updates */
4824 if (tiwin
> tp
->snd_wnd
) {
4830 tcp_ccdbg_trace(tp
, th
,
4831 TCP_CC_ENTER_FASTRECOVERY
);
4832 (void) tcp_output(tp
);
4835 tp
->snd_nxt
= th
->th_ack
;
4836 tp
->snd_cwnd
= tp
->t_maxseg
;
4838 /* Process any window updates */
4839 if (tiwin
> tp
->snd_wnd
) {
4840 tcp_update_window(tp
,
4845 (void) tcp_output(tp
);
4846 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
4847 tcp_cc_adjust_nonvalidated_cwnd(tp
);
4849 tp
->snd_cwnd
= tp
->snd_ssthresh
+
4850 tp
->t_maxseg
* tp
->t_dupacks
;
4852 if (SEQ_GT(onxt
, tp
->snd_nxt
)) {
4856 tcp_ccdbg_trace(tp
, th
,
4857 TCP_CC_ENTER_FASTRECOVERY
);
4859 } else if (limited_txmt
&&
4860 ALLOW_LIMITED_TRANSMIT(tp
) &&
4861 (!(SACK_ENABLED(tp
)) || sack_bytes_acked
> 0) &&
4862 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) > 0) {
4863 u_int32_t incr
= (tp
->t_maxseg
* tp
->t_dupacks
);
4865 /* Use Limited Transmit algorithm on the first two
4866 * duplicate acks when there is new data to transmit
4868 tp
->snd_cwnd
+= incr
;
4869 tcpstat
.tcps_limited_txt
++;
4870 (void) tcp_output(tp
);
4872 tcp_ccdbg_trace(tp
, th
, TCP_CC_LIMITED_TRANSMIT
);
4874 /* Reset snd_cwnd back to normal */
4875 tp
->snd_cwnd
-= incr
;
4881 * If the congestion window was inflated to account
4882 * for the other side's cached packets, retract it.
4884 if (IN_FASTRECOVERY(tp
)) {
4885 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
4887 * If we received an ECE and entered
4888 * recovery, the subsequent ACKs should
4889 * not be treated as partial acks.
4891 if (tp
->ecn_flags
& TE_INRECOVERY
) {
4895 if (SACK_ENABLED(tp
)) {
4896 tcp_sack_partialack(tp
, th
);
4898 tcp_newreno_partial_ack(tp
, th
);
4900 tcp_ccdbg_trace(tp
, th
, TCP_CC_PARTIAL_ACK
);
4902 EXIT_FASTRECOVERY(tp
);
4903 if (CC_ALGO(tp
)->post_fr
!= NULL
) {
4904 CC_ALGO(tp
)->post_fr(tp
, th
);
4907 tcp_clear_pipeack_state(tp
);
4908 tcp_ccdbg_trace(tp
, th
,
4909 TCP_CC_EXIT_FASTRECOVERY
);
4911 } else if ((tp
->t_flagsext
&
4912 (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
))
4913 == (TF_PKTS_REORDERED
| TF_DELAY_RECOVERY
)) {
4915 * If the ack acknowledges upto snd_recover or if
4916 * it acknowledges all the snd holes, exit
4917 * recovery and cancel the timer. Otherwise,
4918 * this is a partial ack. Wait for recovery timer
4919 * to enter recovery. The snd_holes have already
4922 if (SEQ_GEQ(th
->th_ack
, tp
->snd_recover
) ||
4923 TAILQ_EMPTY(&tp
->snd_holes
)) {
4924 tp
->t_timer
[TCPT_DELAYFR
] = 0;
4925 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
4926 EXIT_FASTRECOVERY(tp
);
4927 tcp_ccdbg_trace(tp
, th
,
4928 TCP_CC_EXIT_FASTRECOVERY
);
4932 * We were not in fast recovery. Reset the
4933 * duplicate ack counter.
4936 tp
->t_rexmtthresh
= tcprexmtthresh
;
4941 * If we reach this point, ACK is not a duplicate,
4942 * i.e., it ACKs something we sent.
4944 if (tp
->t_flags
& TF_NEEDSYN
) {
4946 * T/TCP: Connection was half-synchronized, and our
4947 * SYN has been ACK'd (so connection is now fully
4948 * synchronized). Go to non-starred state,
4949 * increment snd_una for ACK of SYN, and check if
4950 * we can do window scaling.
4952 tp
->t_flags
&= ~TF_NEEDSYN
;
4954 /* Do window scaling? */
4955 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
4956 tp
->snd_scale
= tp
->requested_s_scale
;
4957 tp
->rcv_scale
= tp
->request_r_scale
;
4962 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
4963 acked
= BYTES_ACKED(th
, tp
);
4964 tcpstat
.tcps_rcvackpack
++;
4965 tcpstat
.tcps_rcvackbyte
+= acked
;
4968 * If the last packet was a retransmit, make sure
4969 * it was not spurious.
4971 * This will also take care of congestion window
4972 * adjustment if a last packet was recovered due to a
4975 tcp_bad_rexmt_check(tp
, th
, &to
);
4977 /* Recalculate the RTT */
4978 tcp_compute_rtt(tp
, &to
, th
);
4981 * If all outstanding data is acked, stop retransmit
4982 * timer and remember to restart (more output or persist).
4983 * If there is more data to be acked, restart retransmit
4984 * timer, using current (possibly backed-off) value.
4986 TCP_RESET_REXMT_STATE(tp
);
4987 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
4988 tp
->t_rttmin
, TCPTV_REXMTMAX
,
4989 TCP_ADD_REXMTSLOP(tp
));
4990 if (th
->th_ack
== tp
->snd_max
) {
4991 tp
->t_timer
[TCPT_REXMT
] = 0;
4992 tp
->t_timer
[TCPT_PTO
] = 0;
4994 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
4995 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
4999 if ((prev_t_state
== TCPS_SYN_SENT
||
5000 prev_t_state
== TCPS_SYN_RECEIVED
) &&
5001 tp
->t_state
== TCPS_ESTABLISHED
) {
5002 TCP_LOG_RTT_INFO(tp
);
5006 * If no data (only SYN) was ACK'd, skip rest of ACK
5014 * When outgoing data has been acked (except the SYN+data), we
5015 * mark this connection as "sending good" for TFO.
5017 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
5018 !(tp
->t_tfo_flags
& TFO_F_NO_SNDPROBING
) &&
5019 !(th
->th_flags
& TH_SYN
)) {
5020 tp
->t_tfo_flags
|= TFO_F_NO_SNDPROBING
;
5024 * If TH_ECE is received, make sure that ECN is enabled
5025 * on that connection and we have sent ECT on data packets.
5027 if ((thflags
& TH_ECE
) != 0 && TCP_ECN_ENABLED(tp
) &&
5028 (tp
->ecn_flags
& TE_SENDIPECT
)) {
5030 * Reduce the congestion window if we haven't
5033 if (!IN_FASTRECOVERY(tp
)) {
5034 tcp_reduce_congestion_window(tp
);
5035 tp
->ecn_flags
|= (TE_INRECOVERY
| TE_SENDCWR
);
5037 * Also note that the connection received
5040 tp
->ecn_flags
|= TE_RECV_ECN_ECE
;
5041 INP_INC_IFNET_STAT(inp
, ecn_recv_ece
);
5042 tcpstat
.tcps_ecn_recv_ece
++;
5043 tcp_ccdbg_trace(tp
, th
, TCP_CC_ECN_RCVD
);
5048 * When new data is acked, open the congestion window.
5049 * The specifics of how this is achieved are up to the
5050 * congestion control algorithm in use for this connection.
5052 * The calculations in this function assume that snd_una is
5055 if (!IN_FASTRECOVERY(tp
)) {
5056 if (CC_ALGO(tp
)->ack_rcvd
!= NULL
) {
5057 CC_ALGO(tp
)->ack_rcvd(tp
, th
);
5059 tcp_ccdbg_trace(tp
, th
, TCP_CC_ACK_RCVD
);
5061 if (acked
> so
->so_snd
.sb_cc
) {
5062 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
5063 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
5064 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
5065 so
->so_msg_state
->msg_serial_bytes
-=
5066 (int)so
->so_snd
.sb_cc
;
5070 sbdrop(&so
->so_snd
, acked
);
5071 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
5072 so
->so_msg_state
->msg_serial_bytes
-=
5075 tcp_sbsnd_trim(&so
->so_snd
);
5076 tp
->snd_wnd
-= acked
;
5079 /* detect una wraparound */
5080 if (!IN_FASTRECOVERY(tp
) &&
5081 SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
5082 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
)) {
5083 tp
->snd_recover
= th
->th_ack
- 1;
5086 if (IN_FASTRECOVERY(tp
) &&
5087 SEQ_GEQ(th
->th_ack
, tp
->snd_recover
)) {
5088 EXIT_FASTRECOVERY(tp
);
5091 tp
->snd_una
= th
->th_ack
;
5093 if (SACK_ENABLED(tp
)) {
5094 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
)) {
5095 tp
->snd_recover
= tp
->snd_una
;
5098 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
)) {
5099 tp
->snd_nxt
= tp
->snd_una
;
5101 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
5102 !TCP_DSACK_SEQ_IN_WINDOW(tp
, tp
->t_dsack_lastuna
,
5104 tcp_rxtseg_clean(tp
);
5106 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
5107 tp
->t_bwmeas
!= NULL
) {
5108 tcp_bwmeas_check(tp
);
5113 if (!SLIST_EMPTY(&tp
->t_notify_ack
)) {
5114 tcp_notify_acknowledgement(tp
, so
);
5117 switch (tp
->t_state
) {
5119 * In FIN_WAIT_1 STATE in addition to the processing
5120 * for the ESTABLISHED state if our FIN is now acknowledged
5121 * then enter FIN_WAIT_2.
5123 case TCPS_FIN_WAIT_1
:
5124 if (ourfinisacked
) {
5126 * If we can't receive any more
5127 * data, then closing user can proceed.
5128 * Starting the TCPT_2MSL timer is contrary to the
5129 * specification, but if we don't get a FIN
5130 * we'll hang forever.
5132 if (so
->so_state
& SS_CANTRCVMORE
) {
5133 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
5134 TCP_CONN_MAXIDLE(tp
));
5135 isconnected
= FALSE
;
5136 isdisconnected
= TRUE
;
5138 DTRACE_TCP4(state__change
, void, NULL
,
5139 struct inpcb
*, inp
,
5141 int32_t, TCPS_FIN_WAIT_2
);
5142 tp
->t_state
= TCPS_FIN_WAIT_2
;
5143 /* fall through and make sure we also recognize
5144 * data ACKed with the FIN
5150 * In CLOSING STATE in addition to the processing for
5151 * the ESTABLISHED state if the ACK acknowledges our FIN
5152 * then enter the TIME-WAIT state, otherwise ignore
5156 if (ourfinisacked
) {
5157 DTRACE_TCP4(state__change
, void, NULL
,
5158 struct inpcb
*, inp
,
5160 int32_t, TCPS_TIME_WAIT
);
5161 tp
->t_state
= TCPS_TIME_WAIT
;
5162 tcp_canceltimers(tp
);
5163 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
5164 tp
->t_flags
|= TF_CLOSING
;
5166 add_to_time_wait(tp
, 2 * tcp_msl
);
5168 isconnected
= FALSE
;
5169 isdisconnected
= TRUE
;
5174 * In LAST_ACK, we may still be waiting for data to drain
5175 * and/or to be acked, as well as for the ack of our FIN.
5176 * If our FIN is now acknowledged, delete the TCB,
5177 * enter the closed state and return.
5180 if (ourfinisacked
) {
5187 * In TIME_WAIT state the only thing that should arrive
5188 * is a retransmission of the remote FIN. Acknowledge
5189 * it and restart the finack timer.
5191 case TCPS_TIME_WAIT
:
5192 add_to_time_wait(tp
, 2 * tcp_msl
);
5197 * If there is a SACK option on the ACK and we
5198 * haven't seen any duplicate acks before, count
5199 * it as a duplicate ack even if the cumulative
5200 * ack is advanced. If the receiver delayed an
5201 * ack and detected loss afterwards, then the ack
5202 * will advance cumulative ack and will also have
5203 * a SACK option. So counting it as one duplicate
5206 if (sack_ackadv
== 1 &&
5207 tp
->t_state
== TCPS_ESTABLISHED
&&
5208 SACK_ENABLED(tp
) && sack_bytes_acked
> 0 &&
5209 to
.to_nsacks
> 0 && tp
->t_dupacks
== 0 &&
5210 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) && tlen
== 0 &&
5211 !(tp
->t_flagsext
& TF_PKTS_REORDERED
)) {
5212 tcpstat
.tcps_sack_ackadv
++;
5213 goto process_dupack
;
5219 * Update window information.
5221 if (tcp_update_window(tp
, thflags
, th
, tiwin
, tlen
)) {
5226 * Process segments with URG.
5228 if ((thflags
& TH_URG
) && th
->th_urp
&&
5229 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
5231 * This is a kludge, but if we receive and accept
5232 * random urgent pointers, we'll crash in
5233 * soreceive. It's hard to imagine someone
5234 * actually wanting to send this much urgent data.
5236 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
5237 th
->th_urp
= 0; /* XXX */
5238 thflags
&= ~TH_URG
; /* XXX */
5239 goto dodata
; /* XXX */
5242 * If this segment advances the known urgent pointer,
5243 * then mark the data stream. This should not happen
5244 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
5245 * a FIN has been received from the remote side.
5246 * In these states we ignore the URG.
5248 * According to RFC961 (Assigned Protocols),
5249 * the urgent pointer points to the last octet
5250 * of urgent data. We continue, however,
5251 * to consider it to indicate the first octet
5252 * of data past the urgent section as the original
5253 * spec states (in one of two places).
5255 if (SEQ_GT(th
->th_seq
+ th
->th_urp
, tp
->rcv_up
)) {
5256 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
5257 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
5258 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
5259 if (so
->so_oobmark
== 0) {
5260 so
->so_state
|= SS_RCVATMARK
;
5261 postevent(so
, 0, EV_OOB
);
5264 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
5267 * Remove out of band data so doesn't get presented to user.
5268 * This can happen independent of advancing the URG pointer,
5269 * but if two URG's are pending at once, some out-of-band
5270 * data may creep in... ick.
5272 if (th
->th_urp
<= (u_int32_t
)tlen
5274 && (so
->so_options
& SO_OOBINLINE
) == 0
5277 tcp_pulloutofband(so
, th
, m
,
5278 drop_hdrlen
); /* hdr drop is delayed */
5282 * If no out of band data is expected,
5283 * pull receive urgent pointer along
5284 * with the receive window.
5286 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
)) {
5287 tp
->rcv_up
= tp
->rcv_nxt
;
5292 /* Set socket's connect or disconnect state correcly before doing data.
5293 * The following might unlock the socket if there is an upcall or a socket
5298 } else if (isdisconnected
) {
5299 soisdisconnected(so
);
5302 /* Let's check the state of pcb just to make sure that it did not get closed
5303 * when we unlocked above
5305 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
5306 /* Just drop the packet that we are processing and return */
5307 TCP_LOG_DROP_PCB(TCP_LOG_HDR
, th
, tp
, false, "INPCB_STATE_DEAD");
5312 * Process the segment text, merging it into the TCP sequencing queue,
5313 * and arranging for acknowledgment of receipt if necessary.
5314 * This process logically involves adjusting tp->rcv_wnd as data
5315 * is presented to the user (this happens in tcp_usrreq.c,
5316 * case PRU_RCVD). If a FIN has already been received on this
5317 * connection then we just ignore the text.
5319 * If we are in SYN-received state and got a valid TFO cookie, we want
5320 * to process the data.
5322 if ((tlen
|| (thflags
& TH_FIN
)) &&
5323 TCPS_HAVERCVDFIN(tp
->t_state
) == 0 &&
5324 (TCPS_HAVEESTABLISHED(tp
->t_state
) ||
5325 (tp
->t_state
== TCPS_SYN_RECEIVED
&&
5326 (tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)))) {
5327 tcp_seq save_start
= th
->th_seq
;
5328 tcp_seq save_end
= th
->th_seq
+ tlen
;
5329 m_adj(m
, drop_hdrlen
); /* delayed header drop */
5331 * Insert segment which includes th into TCP reassembly queue
5332 * with control block tp. Set thflags to whether reassembly now
5333 * includes a segment with FIN. This handles the common case
5334 * inline (segment is the next to be received on an established
5335 * connection, and the queue is empty), avoiding linkage into
5336 * and removal from the queue and repetition of various
5338 * Set DELACK for segments received in order, but ack
5339 * immediately when segments are out of order (so
5340 * fast retransmit can work).
5342 if (th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
5343 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
5345 * Calculate the RTT on the receiver only if the
5346 * connection is in streaming mode and the last
5347 * packet was not an end-of-write
5349 if (tp
->t_flags
& TF_STREAMING_ON
) {
5350 tcp_compute_rtt(tp
, &to
, th
);
5353 if (DELAY_ACK(tp
, th
) &&
5354 ((tp
->t_flags
& TF_ACKNOW
) == 0)) {
5355 if ((tp
->t_flags
& TF_DELACK
) == 0) {
5356 tp
->t_flags
|= TF_DELACK
;
5357 tp
->t_timer
[TCPT_DELACK
] =
5358 OFFSET_FROM_START(tp
, tcp_delack
);
5361 tp
->t_flags
|= TF_ACKNOW
;
5363 tp
->rcv_nxt
+= tlen
;
5364 thflags
= th
->th_flags
& TH_FIN
;
5365 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
5366 tcpstat
.tcps_rcvbyte
+= tlen
;
5367 if (nstat_collect
) {
5368 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
5369 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
5370 rxpackets
, m
->m_pkthdr
.lro_npkts
);
5372 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
5375 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
5377 inp_set_activity_bitmap(inp
);
5379 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
5380 TCP_AUTORCVBUF_MAX(ifp
));
5381 so_recv_data_stat(so
, m
, drop_hdrlen
);
5384 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
5385 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
5387 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
5388 ip
= (struct ip
*)&saved_hdr
[0];
5390 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
5391 if (sbappendstream_rcvdemux(so
, m
,
5392 th
->th_seq
- (tp
->irs
+ 1), 0)) {
5398 memcpy(&saved_hdr
, ip6
, sizeof(struct ip6_hdr
));
5399 ip6
= (struct ip6_hdr
*)&saved_hdr
[0];
5401 memcpy(&saved_hdr
, ip
, ip
->ip_hl
<< 2);
5402 ip
= (struct ip
*)&saved_hdr
[0];
5404 memcpy(&saved_tcphdr
, th
, sizeof(struct tcphdr
));
5405 thflags
= tcp_reass(tp
, th
, &tlen
, m
, ifp
, &read_wakeup
);
5407 tp
->t_flags
|= TF_ACKNOW
;
5410 if ((tlen
> 0 || (th
->th_flags
& TH_FIN
)) && SACK_ENABLED(tp
)) {
5411 if (th
->th_flags
& TH_FIN
) {
5414 tcp_update_sack_list(tp
, save_start
, save_end
);
5417 tcp_adaptive_rwtimo_check(tp
, tlen
);
5420 tcp_tfo_rcv_data(tp
);
5423 if (tp
->t_flags
& TF_DELACK
) {
5426 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
5427 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
5428 th
->th_seq
, th
->th_ack
, th
->th_win
);
5432 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
5433 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
5434 th
->th_seq
, th
->th_ack
, th
->th_win
);
5438 if ((so
->so_flags
& SOF_MP_SUBFLOW
) && tlen
== 0 &&
5439 (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_DFIN
) &&
5440 (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP
)) {
5441 m_adj(m
, drop_hdrlen
); /* delayed header drop */
5442 mptcp_input(tptomptp(tp
)->mpt_mpte
, m
);
5443 tp
->t_flags
|= TF_ACKNOW
;
5451 * If FIN is received ACK the FIN and let the user know
5452 * that the connection is closing.
5454 if (thflags
& TH_FIN
) {
5455 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
5457 postevent(so
, 0, EV_FIN
);
5459 * If connection is half-synchronized
5460 * (ie NEEDSYN flag on) then delay ACK,
5461 * so it may be piggybacked when SYN is sent.
5462 * Otherwise, since we received a FIN then no
5463 * more input can be expected, send ACK now.
5465 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
5466 if (DELAY_ACK(tp
, th
) && (tp
->t_flags
& TF_NEEDSYN
)) {
5467 if ((tp
->t_flags
& TF_DELACK
) == 0) {
5468 tp
->t_flags
|= TF_DELACK
;
5469 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
5472 tp
->t_flags
|= TF_ACKNOW
;
5476 switch (tp
->t_state
) {
5478 * In SYN_RECEIVED and ESTABLISHED STATES
5479 * enter the CLOSE_WAIT state.
5481 case TCPS_SYN_RECEIVED
:
5482 tp
->t_starttime
= tcp_now
;
5483 case TCPS_ESTABLISHED
:
5484 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
5485 struct tcpcb
*, tp
, int32_t, TCPS_CLOSE_WAIT
);
5486 tp
->t_state
= TCPS_CLOSE_WAIT
;
5490 * If still in FIN_WAIT_1 STATE FIN has not been acked so
5491 * enter the CLOSING state.
5493 case TCPS_FIN_WAIT_1
:
5494 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
5495 struct tcpcb
*, tp
, int32_t, TCPS_CLOSING
);
5496 tp
->t_state
= TCPS_CLOSING
;
5500 * In FIN_WAIT_2 state enter the TIME_WAIT state,
5501 * starting the time-wait timer, turning off the other
5504 case TCPS_FIN_WAIT_2
:
5505 DTRACE_TCP4(state__change
, void, NULL
,
5506 struct inpcb
*, inp
,
5508 int32_t, TCPS_TIME_WAIT
);
5509 tp
->t_state
= TCPS_TIME_WAIT
;
5510 tcp_canceltimers(tp
);
5511 tp
->t_flags
|= TF_ACKNOW
;
5512 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
5513 tp
->t_flags
|= TF_CLOSING
;
5515 add_to_time_wait(tp
, 2 * tcp_msl
);
5517 soisdisconnected(so
);
5521 * In TIME_WAIT state restart the 2 MSL time_wait timer.
5523 case TCPS_TIME_WAIT
:
5524 add_to_time_wait(tp
, 2 * tcp_msl
);
5529 if (so
->so_options
& SO_DEBUG
) {
5530 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
5536 mptcp_handle_input(so
);
5540 * Return any desired output.
5542 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
5543 (void) tcp_output(tp
);
5546 tcp_check_timer_state(tp
);
5548 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5550 socket_unlock(so
, 1);
5551 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5556 * Generate an ACK dropping incoming segment if it occupies
5557 * sequence space, where the ACK reflects our state.
5559 * We can now skip the test for the RST flag since all
5560 * paths to this code happen after packets containing
5561 * RST have been dropped.
5563 * In the SYN-RECEIVED state, don't send an ACK unless the
5564 * segment we received passes the SYN-RECEIVED ACK test.
5565 * If it fails send a RST. This breaks the loop in the
5566 * "LAND" DoS attack, and also prevents an ACK storm
5567 * between two listening ports that have been sent forged
5568 * SYN segments, each with the source address of the other.
5570 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
5571 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
5572 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
5573 rstreason
= BANDLIM_RST_OPENPORT
;
5574 IF_TCP_STATINC(ifp
, dospacket
);
5578 if (so
->so_options
& SO_DEBUG
) {
5579 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5584 tp
->t_flags
|= TF_ACKNOW
;
5586 (void) tcp_output(tp
);
5588 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5590 /* Don't need to check timer state as we should have done it during tcp_output */
5591 socket_unlock(so
, 1);
5592 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5594 dropwithresetnosock
:
5598 * Generate a RST, dropping incoming segment.
5599 * Make ACK acceptable to originator of segment.
5600 * Don't bother to respond if destination was broadcast/multicast.
5602 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
| M_MCAST
)) {
5607 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
5608 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
5613 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
5614 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
5615 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
5616 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
5619 /* IPv6 anycast check is done at tcp6_input() */
5622 * Perform bandwidth limiting.
5625 if (badport_bandlim(rstreason
) < 0) {
5631 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
5632 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5636 bzero(&tra
, sizeof(tra
));
5637 tra
.ifscope
= ifscope
;
5638 tra
.awdl_unrestricted
= 1;
5639 tra
.intcoproc_allowed
= 1;
5640 if (thflags
& TH_ACK
) {
5641 /* mtod() below is safe as long as hdr dropping is delayed */
5642 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
5645 if (thflags
& TH_SYN
) {
5648 /* mtod() below is safe as long as hdr dropping is delayed */
5649 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+ tlen
,
5650 (tcp_seq
)0, TH_RST
| TH_ACK
, &tra
);
5652 /* destroy temporarily created socket */
5655 socket_unlock(so
, 1);
5656 } else if ((inp
!= NULL
) && (nosock
== 0)) {
5657 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5659 socket_unlock(so
, 1);
5661 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5667 * Drop space held by incoming segment and return.
5670 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
)) {
5671 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5676 /* destroy temporarily created socket */
5679 socket_unlock(so
, 1);
5680 } else if (nosock
== 0) {
5681 tcp_handle_wakeup(so
, read_wakeup
, write_wakeup
);
5683 socket_unlock(so
, 1);
5685 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
, 0, 0, 0, 0, 0);
5690 * Parse TCP options and place in tcpopt.
5693 tcp_dooptions(struct tcpcb
*tp
, u_char
*cp
, int cnt
, struct tcphdr
*th
,
5699 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
5701 if (opt
== TCPOPT_EOL
) {
5704 if (opt
== TCPOPT_NOP
) {
5711 if (optlen
< 2 || optlen
> cnt
) {
5720 if (optlen
!= TCPOLEN_MAXSEG
) {
5723 if (!(th
->th_flags
& TH_SYN
)) {
5726 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
5729 to
->to_flags
|= TOF_MSS
;
5733 if (optlen
!= TCPOLEN_WINDOW
) {
5736 if (!(th
->th_flags
& TH_SYN
)) {
5739 to
->to_flags
|= TOF_SCALE
;
5740 to
->to_requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
5743 case TCPOPT_TIMESTAMP
:
5744 if (optlen
!= TCPOLEN_TIMESTAMP
) {
5747 to
->to_flags
|= TOF_TS
;
5748 bcopy((char *)cp
+ 2,
5749 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
5750 NTOHL(to
->to_tsval
);
5751 bcopy((char *)cp
+ 6,
5752 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
5753 NTOHL(to
->to_tsecr
);
5754 /* Re-enable sending Timestamps if we received them */
5755 if (!(tp
->t_flags
& TF_REQ_TSTMP
) &&
5756 tcp_do_rfc1323
== 1) {
5757 tp
->t_flags
|= TF_REQ_TSTMP
;
5760 case TCPOPT_SACK_PERMITTED
:
5762 optlen
!= TCPOLEN_SACK_PERMITTED
) {
5765 if (th
->th_flags
& TH_SYN
) {
5766 to
->to_flags
|= TOF_SACK
;
5770 if (optlen
<= 2 || (optlen
- 2) % TCPOLEN_SACK
!= 0) {
5773 to
->to_nsacks
= (optlen
- 2) / TCPOLEN_SACK
;
5774 to
->to_sacks
= cp
+ 2;
5775 tcpstat
.tcps_sack_rcv_blocks
++;
5778 case TCPOPT_FASTOPEN
:
5779 if (optlen
== TCPOLEN_FASTOPEN_REQ
) {
5780 if (tp
->t_state
!= TCPS_LISTEN
) {
5784 to
->to_flags
|= TOF_TFOREQ
;
5786 if (optlen
< TCPOLEN_FASTOPEN_REQ
||
5787 (optlen
- TCPOLEN_FASTOPEN_REQ
) > TFO_COOKIE_LEN_MAX
||
5788 (optlen
- TCPOLEN_FASTOPEN_REQ
) < TFO_COOKIE_LEN_MIN
) {
5791 if (tp
->t_state
!= TCPS_LISTEN
&&
5792 tp
->t_state
!= TCPS_SYN_SENT
) {
5796 to
->to_flags
|= TOF_TFO
;
5797 to
->to_tfo
= cp
+ 1;
5802 case TCPOPT_MULTIPATH
:
5803 tcp_do_mptcp_options(tp
, cp
, th
, to
, optlen
);
5811 tcp_finalize_options(struct tcpcb
*tp
, struct tcpopt
*to
, unsigned int ifscope
)
5813 if (to
->to_flags
& TOF_TS
) {
5814 tp
->t_flags
|= TF_RCVD_TSTMP
;
5815 tp
->ts_recent
= to
->to_tsval
;
5816 tp
->ts_recent_age
= tcp_now
;
5818 if (to
->to_flags
& TOF_MSS
) {
5819 tcp_mss(tp
, to
->to_mss
, ifscope
);
5821 if (SACK_ENABLED(tp
)) {
5822 if (!(to
->to_flags
& TOF_SACK
)) {
5823 tp
->t_flagsext
&= ~(TF_SACK_ENABLE
);
5825 tp
->t_flags
|= TF_SACK_PERMIT
;
5828 if (to
->to_flags
& TOF_SCALE
) {
5829 tp
->t_flags
|= TF_RCVD_SCALE
;
5830 tp
->requested_s_scale
= to
->to_requested_s_scale
;
5832 /* Re-enable window scaling, if the option is received */
5833 if (tp
->request_r_scale
> 0) {
5834 tp
->t_flags
|= TF_REQ_SCALE
;
5840 * Pull out of band byte out of a segment so
5841 * it doesn't appear in the user's data queue.
5842 * It is still reflected in the segment length for
5843 * sequencing purposes.
5845 * @param off delayed to be droped hdrlen
5848 tcp_pulloutofband(struct socket
*so
, struct tcphdr
*th
, struct mbuf
*m
, int off
)
5850 int cnt
= off
+ th
->th_urp
- 1;
5853 if (m
->m_len
> cnt
) {
5854 char *cp
= mtod(m
, caddr_t
) + cnt
;
5855 struct tcpcb
*tp
= sototcpcb(so
);
5858 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
5859 bcopy(cp
+ 1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
5861 if (m
->m_flags
& M_PKTHDR
) {
5872 panic("tcp_pulloutofband");
5876 get_base_rtt(struct tcpcb
*tp
)
5878 struct rtentry
*rt
= tp
->t_inpcb
->inp_route
.ro_rt
;
5879 return (rt
== NULL
) ? 0 : rt
->rtt_min
;
5882 /* Each value of RTT base represents the minimum RTT seen in a minute.
5883 * We keep upto N_RTT_BASE minutes worth of history.
5886 update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
)
5888 u_int32_t base_rtt
, i
;
5891 if ((rt
= tp
->t_inpcb
->inp_route
.ro_rt
) == NULL
) {
5894 if (rt
->rtt_expire_ts
== 0) {
5896 if (rt
->rtt_expire_ts
!= 0) {
5900 rt
->rtt_expire_ts
= tcp_now
;
5902 rt
->rtt_hist
[0] = rtt
;
5910 * If the recv side is being throttled, check if the
5911 * current RTT is closer to the base RTT seen in
5912 * first (recent) two slots. If so, unthrottle the stream.
5914 if ((tp
->t_flagsext
& TF_RECV_THROTTLE
) &&
5915 (int)(tcp_now
- tp
->t_recv_throttle_ts
) >= TCP_RECV_THROTTLE_WIN
) {
5916 base_rtt
= rt
->rtt_min
;
5917 if (tp
->t_rttcur
<= (base_rtt
+ target_qdelay
)) {
5918 tp
->t_flagsext
&= ~TF_RECV_THROTTLE
;
5919 tp
->t_recv_throttle_ts
= 0;
5922 #endif /* TRAFFIC_MGT */
5923 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5924 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5926 /* check the condition again to avoid race */
5927 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5928 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5930 if (rt
->rtt_index
>= NRTT_HIST
) {
5933 rt
->rtt_hist
[rt
->rtt_index
] = rtt
;
5934 rt
->rtt_expire_ts
= tcp_now
;
5936 rt
->rtt_hist
[rt
->rtt_index
] =
5937 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5939 /* forget the old value and update minimum */
5941 for (i
= 0; i
< NRTT_HIST
; ++i
) {
5942 if (rt
->rtt_hist
[i
] != 0 &&
5943 (rt
->rtt_min
== 0 ||
5944 rt
->rtt_hist
[i
] < rt
->rtt_min
)) {
5945 rt
->rtt_min
= rt
->rtt_hist
[i
];
5950 rt
->rtt_hist
[rt
->rtt_index
] =
5951 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5952 if (rt
->rtt_min
== 0) {
5955 rt
->rtt_min
= min(rt
->rtt_min
, rtt
);
5961 * If we have a timestamp reply, update smoothed RTT. If no timestamp is
5962 * present but transmit timer is running and timed sequence number was
5963 * acked, update smoothed RTT.
5965 * If timestamps are supported, a receiver can update RTT even if
5966 * there is no outstanding data.
5968 * Some boxes send broken timestamp replies during the SYN+ACK phase,
5969 * ignore timestamps of 0or we could calculate a huge RTT and blow up
5970 * the retransmit timer.
5973 tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
, struct tcphdr
*th
)
5976 VERIFY(to
!= NULL
&& th
!= NULL
);
5977 if (tp
->t_rtttime
!= 0 && SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
5978 u_int32_t pipe_ack_val
;
5979 rtt
= tcp_now
- tp
->t_rtttime
;
5981 * Compute pipe ack -- the amount of data acknowledged
5984 if (SEQ_GT(th
->th_ack
, tp
->t_pipeack_lastuna
)) {
5985 pipe_ack_val
= th
->th_ack
- tp
->t_pipeack_lastuna
;
5986 /* Update the sample */
5987 tp
->t_pipeack_sample
[tp
->t_pipeack_ind
++] =
5989 tp
->t_pipeack_ind
%= TCP_PIPEACK_SAMPLE_COUNT
;
5991 /* Compute the max of the pipeack samples */
5992 pipe_ack_val
= tcp_get_max_pipeack(tp
);
5993 tp
->t_pipeack
= (pipe_ack_val
>
5994 TCP_CC_CWND_INIT_BYTES
) ?
5997 /* start another measurement */
6000 if (((to
->to_flags
& TOF_TS
) != 0) &&
6001 (to
->to_tsecr
!= 0) &&
6002 TSTMP_GEQ(tcp_now
, to
->to_tsecr
)) {
6003 tcp_xmit_timer(tp
, (tcp_now
- to
->to_tsecr
),
6004 to
->to_tsecr
, th
->th_ack
);
6005 } else if (rtt
> 0) {
6006 tcp_xmit_timer(tp
, rtt
, 0, th
->th_ack
);
6011 * Collect new round-trip time estimate and update averages and
6015 tcp_xmit_timer(struct tcpcb
*tp
, int rtt
,
6016 u_int32_t tsecr
, tcp_seq th_ack
)
6019 int old_srtt
= tp
->t_srtt
;
6020 int old_rttvar
= tp
->t_rttvar
;
6021 bool log_rtt
= false;
6024 * On AWDL interface, the initial RTT measurement on SYN
6025 * can be wrong due to peer caching. Avoid the first RTT
6026 * measurement as it might skew up the RTO.
6027 * <rdar://problem/28739046>
6029 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
&&
6030 (tp
->t_inpcb
->inp_last_outifp
->if_eflags
& IFEF_AWDL
) &&
6031 th_ack
== tp
->iss
+ 1) {
6035 if (tp
->t_flagsext
& TF_RECOMPUTE_RTT
) {
6036 if (SEQ_GT(th_ack
, tp
->snd_una
) &&
6037 SEQ_LEQ(th_ack
, tp
->snd_max
) &&
6039 TSTMP_GEQ(tsecr
, tp
->t_badrexmt_time
))) {
6041 * We received a new ACk after a
6042 * spurious timeout. Adapt retransmission
6043 * timer as described in rfc 4015.
6045 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
6046 tp
->t_badrexmt_time
= 0;
6047 tp
->t_srtt
= max(tp
->t_srtt_prev
, rtt
);
6048 tp
->t_srtt
= tp
->t_srtt
<< TCP_RTT_SHIFT
;
6049 tp
->t_rttvar
= max(tp
->t_rttvar_prev
, (rtt
>> 1));
6050 tp
->t_rttvar
= tp
->t_rttvar
<< TCP_RTTVAR_SHIFT
;
6052 if (tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
)) {
6053 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
6062 tcpstat
.tcps_rttupdated
++;
6067 update_base_rtt(tp
, rtt
);
6070 if (tp
->t_srtt
!= 0) {
6072 * srtt is stored as fixed point with 5 bits after the
6073 * binary point (i.e., scaled by 32). The following magic
6074 * is equivalent to the smoothing algorithm in rfc793 with
6075 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
6078 * Freebsd adjusts rtt to origin 0 by subtracting 1
6079 * from the provided rtt value. This was required because
6080 * of the way t_rtttime was initiailised to 1 before.
6081 * Since we changed t_rtttime to be based on
6082 * tcp_now, this extra adjustment is not needed.
6084 delta
= (rtt
<< TCP_DELTA_SHIFT
)
6085 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
6087 if ((tp
->t_srtt
+= delta
) <= 0) {
6092 * We accumulate a smoothed rtt variance (actually, a
6093 * smoothed mean difference), then set the retransmit
6094 * timer to smoothed rtt + 4 times the smoothed variance.
6095 * rttvar is stored as fixed point with 4 bits after the
6096 * binary point (scaled by 16). The following is
6097 * equivalent to rfc793 smoothing with an alpha of .75
6098 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
6099 * rfc793's wired-in beta.
6104 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
6105 if ((tp
->t_rttvar
+= delta
) <= 0) {
6108 if (tp
->t_rttbest
== 0 ||
6109 tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
)) {
6110 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
6114 * No rtt measurement yet - use the unsmoothed rtt.
6115 * Set the variance to half the rtt (so our first
6116 * retransmit happens at 3*rtt).
6118 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
6119 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
6123 nstat_route_rtt(tp
->t_inpcb
->inp_route
.ro_rt
, tp
->t_srtt
,
6127 * the retransmit should happen at rtt + 4 * rttvar.
6128 * Because of the way we do the smoothing, srtt and rttvar
6129 * will each average +1/2 tick of bias. When we compute
6130 * the retransmit timer, we want 1/2 tick of rounding and
6131 * 1 extra tick because of +-1/2 tick uncertainty in the
6132 * firing of the timer. The bias will give us exactly the
6133 * 1.5 tick we need. But, because the bias is
6134 * statistical, we have to test that we don't drop below
6135 * the minimum feasible timer (which is 2 ticks).
6137 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
6138 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
,
6139 TCP_ADD_REXMTSLOP(tp
));
6142 * We received an ack for a packet that wasn't retransmitted;
6143 * it is probably safe to discard any error indications we've
6144 * received recently. This isn't quite right, but close enough
6145 * for now (a route might have failed after we sent a segment,
6146 * and the return path might not be symmetrical).
6148 tp
->t_softerror
= 0;
6151 TCP_LOG_RTT_INFO(tp
);
6154 TCP_LOG_RTT_CHANGE(tp
, old_srtt
, old_rttvar
);
6157 static inline unsigned int
6158 tcp_maxmtu(struct rtentry
*rt
)
6160 unsigned int maxmtu
;
6161 int interface_mtu
= 0;
6163 RT_LOCK_ASSERT_HELD(rt
);
6164 interface_mtu
= rt
->rt_ifp
->if_mtu
;
6166 if (rt_key(rt
)->sa_family
== AF_INET
&&
6167 INTF_ADJUST_MTU_FOR_CLAT46(rt
->rt_ifp
)) {
6168 interface_mtu
= IN6_LINKMTU(rt
->rt_ifp
);
6169 /* Further adjust the size for CLAT46 expansion */
6170 interface_mtu
-= CLAT46_HDR_EXPANSION_OVERHD
;
6173 if (rt
->rt_rmx
.rmx_mtu
== 0) {
6174 maxmtu
= interface_mtu
;
6176 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, interface_mtu
);
6183 static inline unsigned int
6184 tcp_maxmtu6(struct rtentry
*rt
)
6186 unsigned int maxmtu
;
6187 struct nd_ifinfo
*ndi
= NULL
;
6189 RT_LOCK_ASSERT_HELD(rt
);
6190 if ((ndi
= ND_IFINFO(rt
->rt_ifp
)) != NULL
&& !ndi
->initialized
) {
6194 lck_mtx_lock(&ndi
->lock
);
6196 if (rt
->rt_rmx
.rmx_mtu
== 0) {
6197 maxmtu
= IN6_LINKMTU(rt
->rt_ifp
);
6199 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, IN6_LINKMTU(rt
->rt_ifp
));
6202 lck_mtx_unlock(&ndi
->lock
);
6210 get_maxmtu(struct rtentry
*rt
)
6212 unsigned int maxmtu
= 0;
6214 RT_LOCK_ASSERT_NOTHELD(rt
);
6218 if (rt_key(rt
)->sa_family
== AF_INET6
) {
6219 maxmtu
= tcp_maxmtu6(rt
);
6221 maxmtu
= tcp_maxmtu(rt
);
6230 * Determine a reasonable value for maxseg size.
6231 * If the route is known, check route for mtu.
6232 * If none, use an mss that can be handled on the outgoing
6233 * interface without forcing IP to fragment; if bigger than
6234 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
6235 * to utilize large mbufs. If no route is found, route has no mtu,
6236 * or the destination isn't local, use a default, hopefully conservative
6237 * size (usually 512 or the default IP max size, but no more than the mtu
6238 * of the interface), as we can't discover anything about intervening
6239 * gateways or networks. We also initialize the congestion/slow start
6240 * window. While looking at the routing entry, we also initialize
6241 * other path-dependent parameters from pre-set or cached values
6242 * in the routing entry.
6244 * Also take into account the space needed for options that we
6245 * send regularly. Make maxseg shorter by that amount to assure
6246 * that we can send maxseg amount of data even when the options
6247 * are present. Store the upper limit of the length of options plus
6250 * NOTE that this routine is only called when we process an incoming
6251 * segment, for outgoing segments only tcp_mssopt is called.
6255 tcp_mss(struct tcpcb
*tp
, int offer
, unsigned int input_ifscope
)
6263 struct rmxp_tao
*taop
;
6264 int origoffer
= offer
;
6265 u_int32_t sb_max_corrected
;
6274 so
= inp
->inp_socket
;
6276 * Nothing left to send after the socket is defunct or TCP is in the closed state
6278 if ((so
->so_state
& SS_DEFUNCT
) || tp
->t_state
== TCPS_CLOSED
) {
6283 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
6284 min_protoh
= isipv6
? sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
)
6285 : sizeof(struct tcpiphdr
);
6287 #define min_protoh (sizeof (struct tcpiphdr))
6292 rt
= tcp_rtlookup6(inp
, input_ifscope
);
6296 rt
= tcp_rtlookup(inp
, input_ifscope
);
6298 isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
6301 tp
->t_maxopd
= tp
->t_maxseg
=
6303 isipv6
? tcp_v6mssdflt
:
6310 * Slower link window correction:
6311 * If a value is specificied for slowlink_wsize use it for
6312 * PPP links believed to be on a serial modem (speed <128Kbps).
6313 * Excludes 9600bps as it is the default value adversized
6314 * by pseudo-devices over ppp.
6316 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
6317 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
6318 tp
->t_flags
|= TF_SLOWLINK
;
6321 taop
= rmx_taop(rt
->rt_rmx
);
6323 * Offer == -1 means that we didn't receive SYN yet,
6324 * use cached value in that case;
6327 offer
= taop
->tao_mssopt
;
6330 * Offer == 0 means that there was no MSS on the SYN segment,
6331 * in this case we use tcp_mssdflt.
6336 isipv6
? tcp_v6mssdflt
:
6341 * Prevent DoS attack with too small MSS. Round up
6342 * to at least minmss.
6344 offer
= max(offer
, tcp_minmss
);
6346 * Sanity check: make sure that maxopd will be large
6347 * enough to allow some data on segments even is the
6348 * all the option space is used (40bytes). Otherwise
6349 * funny things may happen in tcp_output.
6351 offer
= max(offer
, 64);
6353 taop
->tao_mssopt
= offer
;
6356 * While we're here, check if there's an initial rtt
6357 * or rttvar. Convert from the route-table units
6358 * to scaled multiples of the slow timeout timer.
6360 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
) != 0) {
6361 tcp_getrt_rtt(tp
, rt
);
6363 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
6367 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
6369 mss
= tcp_maxmtu(rt
);
6373 // At this point, the mss is just the MTU. Adjust if necessary.
6374 mss
= necp_socket_get_effective_mtu(inp
, mss
);
6379 if (rt
->rt_rmx
.rmx_mtu
== 0) {
6383 mss
= min(mss
, tcp_v6mssdflt
);
6388 mss
= min(mss
, tcp_mssdflt
);
6392 mss
= min(mss
, offer
);
6394 * maxopd stores the maximum length of data AND options
6395 * in a segment; maxseg is the amount of data in a normal
6396 * segment. We need to store this value (maxopd) apart
6397 * from maxseg, because now every segment carries options
6398 * and thus we normally have somewhat less data in segments.
6403 * origoffer==-1 indicates, that no segments were received yet.
6404 * In this case we just guess.
6406 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_NOOPT
)) == TF_REQ_TSTMP
&&
6408 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
)) {
6409 mss
-= TCPOLEN_TSTAMP_APPA
;
6413 mss
-= mptcp_adj_mss(tp
, FALSE
);
6418 * Calculate corrected value for sb_max; ensure to upgrade the
6419 * numerator for large sb_max values else it will overflow.
6421 sb_max_corrected
= (sb_max
* (u_int64_t
)MCLBYTES
) / (MSIZE
+ MCLBYTES
);
6424 * If there's a pipesize (ie loopback), change the socket
6425 * buffer to that size only if it's bigger than the current
6426 * sockbuf size. Make the socket buffers an integral
6427 * number of mss units; if the mss is larger than
6428 * the socket buffer, decrease the mss.
6431 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
6432 if (bufsize
< so
->so_snd
.sb_hiwat
)
6434 bufsize
= so
->so_snd
.sb_hiwat
;
6435 if (bufsize
< mss
) {
6438 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
6439 if (bufsize
> sb_max_corrected
) {
6440 bufsize
= sb_max_corrected
;
6442 (void)sbreserve(&so
->so_snd
, bufsize
);
6446 ASSERT(tp
->t_maxseg
);
6449 * Update MSS using recommendation from link status report. This is
6452 tcp_update_mss_locked(so
, ifp
);
6455 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
6456 if (bufsize
< so
->so_rcv
.sb_hiwat
)
6458 bufsize
= so
->so_rcv
.sb_hiwat
;
6459 if (bufsize
> mss
) {
6460 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
6461 if (bufsize
> sb_max_corrected
) {
6462 bufsize
= sb_max_corrected
;
6464 (void)sbreserve(&so
->so_rcv
, bufsize
);
6467 set_tcp_stream_priority(so
);
6469 if (rt
->rt_rmx
.rmx_ssthresh
) {
6471 * There's some sort of gateway or interface
6472 * buffer limit on the path. Use this to set
6473 * slow-start threshold, but set the threshold to
6474 * no less than 2*mss.
6476 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
6477 tcpstat
.tcps_usedssthresh
++;
6479 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
6483 * Set the slow-start flight size depending on whether this
6484 * is a local network or not.
6486 if (CC_ALGO(tp
)->cwnd_init
!= NULL
) {
6487 CC_ALGO(tp
)->cwnd_init(tp
);
6490 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CWND_INIT
);
6492 /* Route locked during lookup above */
6497 * Determine the MSS option to send on an outgoing SYN.
6500 tcp_mssopt(struct tcpcb
*tp
)
6510 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
6511 min_protoh
= isipv6
? sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
)
6512 : sizeof(struct tcpiphdr
);
6514 #define min_protoh (sizeof (struct tcpiphdr))
6519 rt
= tcp_rtlookup6(tp
->t_inpcb
, IFSCOPE_NONE
);
6522 rt
= tcp_rtlookup(tp
->t_inpcb
, IFSCOPE_NONE
);
6526 isipv6
? tcp_v6mssdflt
:
6531 * Slower link window correction:
6532 * If a value is specificied for slowlink_wsize use it for PPP links
6533 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
6534 * it is the default value adversized by pseudo-devices over ppp.
6536 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
6537 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
6538 tp
->t_flags
|= TF_SLOWLINK
;
6542 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
6544 mss
= tcp_maxmtu(rt
);
6546 /* Route locked during lookup above */
6550 // At this point, the mss is just the MTU. Adjust if necessary.
6551 mss
= necp_socket_get_effective_mtu(tp
->t_inpcb
, mss
);
6554 return mss
- min_protoh
;
6558 * On a partial ack arrives, force the retransmission of the
6559 * next unacknowledged segment. Do not clear tp->t_dupacks.
6560 * By setting snd_nxt to th_ack, this forces retransmission timer to
6564 tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
6566 tcp_seq onxt
= tp
->snd_nxt
;
6567 u_int32_t ocwnd
= tp
->snd_cwnd
;
6568 tp
->t_timer
[TCPT_REXMT
] = 0;
6569 tp
->t_timer
[TCPT_PTO
] = 0;
6571 tp
->snd_nxt
= th
->th_ack
;
6573 * Set snd_cwnd to one segment beyond acknowledged offset
6574 * (tp->snd_una has not yet been updated when this function
6577 tp
->snd_cwnd
= tp
->t_maxseg
+ BYTES_ACKED(th
, tp
);
6578 tp
->t_flags
|= TF_ACKNOW
;
6579 (void) tcp_output(tp
);
6580 tp
->snd_cwnd
= ocwnd
;
6581 if (SEQ_GT(onxt
, tp
->snd_nxt
)) {
6585 * Partial window deflation. Relies on fact that tp->snd_una
6588 if (tp
->snd_cwnd
> BYTES_ACKED(th
, tp
)) {
6589 tp
->snd_cwnd
-= BYTES_ACKED(th
, tp
);
6593 tp
->snd_cwnd
+= tp
->t_maxseg
;
6597 * Drop a random TCP connection that hasn't been serviced yet and
6598 * is eligible for discard. There is a one in qlen chance that
6599 * we will return a null, saying that there are no dropable
6600 * requests. In this case, the protocol specific code should drop
6601 * the new request. This insures fairness.
6603 * The listening TCP socket "head" must be locked
6606 tcp_dropdropablreq(struct socket
*head
)
6608 struct socket
*so
, *sonext
;
6609 unsigned int i
, j
, qlen
;
6610 static u_int32_t rnd
= 0;
6611 static u_int64_t old_runtime
;
6612 static unsigned int cur_cnt
, old_cnt
;
6614 struct inpcb
*inp
= NULL
;
6617 if ((head
->so_options
& SO_ACCEPTCONN
) == 0) {
6621 if (TAILQ_EMPTY(&head
->so_incomp
)) {
6625 so_acquire_accept_list(head
, NULL
);
6626 socket_unlock(head
, 0);
6629 * Check if there is any socket in the incomp queue
6630 * that is closed because of a reset from the peer and is
6631 * waiting to be garbage collected. If so, pick that as
6634 TAILQ_FOREACH_SAFE(so
, &head
->so_incomp
, so_list
, sonext
) {
6635 inp
= sotoinpcb(so
);
6636 tp
= intotcpcb(inp
);
6637 if (tp
!= NULL
&& tp
->t_state
== TCPS_CLOSED
&&
6638 so
->so_head
!= NULL
&&
6639 (so
->so_state
& (SS_INCOMP
| SS_CANTSENDMORE
| SS_CANTRCVMORE
)) ==
6640 (SS_INCOMP
| SS_CANTSENDMORE
| SS_CANTRCVMORE
)) {
6642 * The listen socket is already locked but we
6643 * can lock this socket here without lock ordering
6644 * issues because it is in the incomp queue and
6645 * is not visible to others.
6647 if (socket_try_lock(so
)) {
6656 so
= TAILQ_FIRST(&head
->so_incomp
);
6658 now_sec
= net_uptime();
6659 if ((i
= (now_sec
- old_runtime
)) != 0) {
6660 old_runtime
= now_sec
;
6661 old_cnt
= cur_cnt
/ i
;
6665 qlen
= head
->so_incqlen
;
6667 rnd
= RandomULong();
6670 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
6671 rnd
= (314159 * rnd
+ 66329) & 0xffff;
6672 j
= ((qlen
+ 1) * rnd
) >> 16;
6675 so
= TAILQ_NEXT(so
, so_list
);
6678 /* Find a connection that is not already closing (or being served) */
6680 inp
= (struct inpcb
*)so
->so_pcb
;
6682 sonext
= TAILQ_NEXT(so
, so_list
);
6684 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
6686 * Avoid the issue of a socket being accepted
6687 * by one input thread and being dropped by
6688 * another input thread. If we can't get a hold
6689 * on this mutex, then grab the next socket in
6692 if (socket_try_lock(so
)) {
6694 if ((so
->so_usecount
== 2) &&
6695 (so
->so_state
& SS_INCOMP
) &&
6696 !(so
->so_flags
& SOF_INCOMP_INPROGRESS
)) {
6700 * don't use if being accepted or
6701 * used in any other way
6703 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6704 socket_unlock(so
, 1);
6708 * do not try to lock the inp in
6709 * in_pcb_checkstate because the lock
6710 * is already held in some other thread.
6711 * Only drop the inp_wntcnt reference.
6713 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6719 socket_lock(head
, 0);
6720 so_release_accept_list(head
);
6724 /* Makes sure socket is still in the right state to be discarded */
6726 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
6727 socket_unlock(so
, 1);
6728 socket_lock(head
, 0);
6729 so_release_accept_list(head
);
6734 if (so
->so_usecount
!= 2 || !(so
->so_state
& SS_INCOMP
)) {
6735 /* do not discard: that socket is being accepted */
6736 socket_unlock(so
, 1);
6737 socket_lock(head
, 0);
6738 so_release_accept_list(head
);
6742 socket_lock(head
, 0);
6743 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
6746 so
->so_state
&= ~SS_INCOMP
;
6747 so
->so_flags
|= SOF_OVERFLOW
;
6749 so_release_accept_list(head
);
6750 socket_unlock(head
, 0);
6752 socket_lock_assert_owned(so
);
6756 if (inp
->inp_wantcnt
> 0 && inp
->inp_wantcnt
!= WNT_STOPUSING
) {
6758 * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE
6759 * doesn't require a lock, it could have happened while
6760 * we are holding the lock. This pcb will have to
6761 * be garbage collected later.
6762 * Release the reference held for so_incomp queue
6764 VERIFY(so
->so_usecount
> 0);
6766 socket_unlock(so
, 1);
6769 * Unlock this socket and leave the reference on.
6770 * We need to acquire the pcbinfo lock in order to
6771 * fully dispose it off
6773 socket_unlock(so
, 0);
6775 lck_rw_lock_exclusive(tcbinfo
.ipi_lock
);
6778 /* Release the reference held for so_incomp queue */
6779 VERIFY(so
->so_usecount
> 0);
6782 if (so
->so_usecount
!= 1 ||
6783 (inp
->inp_wantcnt
> 0 &&
6784 inp
->inp_wantcnt
!= WNT_STOPUSING
)) {
6786 * There is an extra wantcount or usecount
6787 * that must have been added when the socket
6788 * was unlocked. This socket will have to be
6789 * garbage collected later
6791 socket_unlock(so
, 1);
6793 /* Drop the reference held for this function */
6794 VERIFY(so
->so_usecount
> 0);
6799 lck_rw_done(tcbinfo
.ipi_lock
);
6801 tcpstat
.tcps_drops
++;
6803 socket_lock(head
, 0);
6807 /* Set background congestion control on a socket */
6809 tcp_set_background_cc(struct socket
*so
)
6811 tcp_set_new_cc(so
, TCP_CC_ALGO_BACKGROUND_INDEX
);
6814 /* Set foreground congestion control on a socket */
6816 tcp_set_foreground_cc(struct socket
*so
)
6818 if (tcp_use_newreno
) {
6819 tcp_set_new_cc(so
, TCP_CC_ALGO_NEWRENO_INDEX
);
6821 tcp_set_new_cc(so
, TCP_CC_ALGO_CUBIC_INDEX
);
6826 tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
)
6828 struct inpcb
*inp
= sotoinpcb(so
);
6829 struct tcpcb
*tp
= intotcpcb(inp
);
6830 u_char old_cc_index
= 0;
6831 if (tp
->tcp_cc_index
!= cc_index
) {
6832 old_cc_index
= tp
->tcp_cc_index
;
6834 if (CC_ALGO(tp
)->cleanup
!= NULL
) {
6835 CC_ALGO(tp
)->cleanup(tp
);
6837 tp
->tcp_cc_index
= cc_index
;
6839 tcp_cc_allocate_state(tp
);
6841 if (CC_ALGO(tp
)->switch_to
!= NULL
) {
6842 CC_ALGO(tp
)->switch_to(tp
, old_cc_index
);
6845 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CHANGE_ALGO
);
6850 tcp_set_recv_bg(struct socket
*so
)
6852 if (!IS_TCP_RECV_BG(so
)) {
6853 so
->so_flags1
|= SOF1_TRAFFIC_MGT_TCP_RECVBG
;
6856 /* Unset Large Receive Offload on background sockets */
6857 so_set_lro(so
, SO_TC_BK
);
6861 tcp_clear_recv_bg(struct socket
*so
)
6863 if (IS_TCP_RECV_BG(so
)) {
6864 so
->so_flags1
&= ~(SOF1_TRAFFIC_MGT_TCP_RECVBG
);
6868 * Set/unset use of Large Receive Offload depending on
6871 so_set_lro(so
, so
->so_traffic_class
);
6875 inp_fc_unthrottle_tcp(struct inpcb
*inp
)
6877 struct tcpcb
*tp
= inp
->inp_ppcb
;
6879 * Back off the slow-start threshold and enter
6880 * congestion avoidance phase
6882 if (CC_ALGO(tp
)->pre_fr
!= NULL
) {
6883 CC_ALGO(tp
)->pre_fr(tp
);
6886 tp
->snd_cwnd
= tp
->snd_ssthresh
;
6887 tp
->t_flagsext
&= ~TF_CWND_NONVALIDATED
;
6889 * Restart counting for ABC as we changed the
6890 * congestion window just now.
6892 tp
->t_bytes_acked
= 0;
6894 /* Reset retransmit shift as we know that the reason
6895 * for delay in sending a packet is due to flow
6896 * control on the outgoing interface. There is no need
6897 * to backoff retransmit timer.
6899 TCP_RESET_REXMT_STATE(tp
);
6902 * Start the output stream again. Since we are
6903 * not retransmitting data, do not reset the
6904 * retransmit timer or rtt calculation.
6910 tcp_getstat SYSCTL_HANDLER_ARGS
6912 #pragma unused(oidp, arg1, arg2)
6915 struct tcpstat
*stat
;
6917 #if !CONFIG_EMBEDDED
6918 struct tcpstat zero_stat
;
6920 if (tcp_disable_access_to_stats
&&
6921 !kauth_cred_issuser(kauth_cred_get())) {
6922 bzero(&zero_stat
, sizeof(zero_stat
));
6926 #endif /* !CONFIG_EMBEDDED */
6928 if (req
->oldptr
== 0) {
6929 req
->oldlen
= (size_t)sizeof(struct tcpstat
);
6932 error
= SYSCTL_OUT(req
, stat
, MIN(sizeof(tcpstat
), req
->oldlen
));
6938 * Checksum extended TCP header and data.
6941 tcp_input_checksum(int af
, struct mbuf
*m
, struct tcphdr
*th
, int off
, int tlen
)
6943 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
6947 struct ip
*ip
= mtod(m
, struct ip
*);
6948 struct ipovly
*ipov
= (struct ipovly
*)ip
;
6950 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
) {
6954 /* ip_stripoptions() must have been called before we get here */
6955 ASSERT((ip
->ip_hl
<< 2) == sizeof(*ip
));
6957 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6958 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6959 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6960 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6961 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6963 uint32_t sum
= m
->m_pkthdr
.csum_rx_val
;
6964 uint32_t start
= m
->m_pkthdr
.csum_rx_start
;
6965 int32_t trailer
= (m_pktlen(m
) - (off
+ tlen
));
6968 * Perform 1's complement adjustment of octets
6969 * that got included/excluded in the hardware-
6970 * calculated checksum value. Ignore cases
6971 * where the value already includes the entire
6972 * IP header span, as the sum for those octets
6973 * would already be 0 by the time we get here;
6974 * IP has already performed its header checksum
6975 * checks. If we do need to adjust, restore
6976 * the original fields in the IP header when
6977 * computing the adjustment value. Also take
6978 * care of any trailing bytes and subtract out
6979 * their partial sum.
6981 ASSERT(trailer
>= 0);
6982 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6983 ((start
!= 0 && start
!= off
) || trailer
)) {
6984 uint32_t swbytes
= (uint32_t)trailer
;
6987 ip
->ip_len
+= sizeof(*ip
);
6988 #if BYTE_ORDER != BIG_ENDIAN
6991 #endif /* BYTE_ORDER != BIG_ENDIAN */
6993 /* callee folds in sum */
6994 sum
= m_adj_sum16(m
, start
, off
,
6997 swbytes
+= (off
- start
);
6999 swbytes
+= (start
- off
);
7003 #if BYTE_ORDER != BIG_ENDIAN
7006 #endif /* BYTE_ORDER != BIG_ENDIAN */
7007 ip
->ip_len
-= sizeof(*ip
);
7011 tcp_in_cksum_stats(swbytes
);
7018 /* callee folds in sum */
7019 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
7021 sum
+ htonl(tlen
+ IPPROTO_TCP
));
7023 th
->th_sum
^= 0xffff;
7029 bcopy(ipov
->ih_x1
, b
, sizeof(ipov
->ih_x1
));
7030 bzero(ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
7031 ip_sum
= ipov
->ih_len
;
7032 ipov
->ih_len
= (u_short
)tlen
;
7033 #if BYTE_ORDER != BIG_ENDIAN
7034 HTONS(ipov
->ih_len
);
7036 len
= sizeof(struct ip
) + tlen
;
7037 th
->th_sum
= in_cksum(m
, len
);
7038 bcopy(b
, ipov
->ih_x1
, sizeof(ipov
->ih_x1
));
7039 ipov
->ih_len
= ip_sum
;
7041 tcp_in_cksum_stats(len
);
7047 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
7049 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
) {
7053 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
7054 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
7055 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
7056 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
7057 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
7059 uint32_t sum
= m
->m_pkthdr
.csum_rx_val
;
7060 uint32_t start
= m
->m_pkthdr
.csum_rx_start
;
7061 int32_t trailer
= (m_pktlen(m
) - (off
+ tlen
));
7064 * Perform 1's complement adjustment of octets
7065 * that got included/excluded in the hardware-
7066 * calculated checksum value. Also take care
7067 * of any trailing bytes and subtract out their
7070 ASSERT(trailer
>= 0);
7071 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
7072 (start
!= off
|| trailer
!= 0)) {
7073 uint16_t s
= 0, d
= 0;
7074 uint32_t swbytes
= (uint32_t)trailer
;
7076 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
7077 s
= ip6
->ip6_src
.s6_addr16
[1];
7078 ip6
->ip6_src
.s6_addr16
[1] = 0;
7080 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
7081 d
= ip6
->ip6_dst
.s6_addr16
[1];
7082 ip6
->ip6_dst
.s6_addr16
[1] = 0;
7085 /* callee folds in sum */
7086 sum
= m_adj_sum16(m
, start
, off
,
7089 swbytes
+= (off
- start
);
7091 swbytes
+= (start
- off
);
7094 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
7095 ip6
->ip6_src
.s6_addr16
[1] = s
;
7097 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
7098 ip6
->ip6_dst
.s6_addr16
[1] = d
;
7102 tcp_in6_cksum_stats(swbytes
);
7109 th
->th_sum
= in6_pseudo(
7110 &ip6
->ip6_src
, &ip6
->ip6_dst
,
7111 sum
+ htonl(tlen
+ IPPROTO_TCP
));
7113 th
->th_sum
^= 0xffff;
7115 tcp_in6_cksum_stats(tlen
);
7116 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, off
, tlen
);
7126 if (th
->th_sum
!= 0) {
7127 tcpstat
.tcps_rcvbadsum
++;
7128 IF_TCP_STATINC(ifp
, badformat
);
7136 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_STATS
, stats
,
7137 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, tcp_getstat
,
7138 "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
7141 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
7143 #pragma unused(arg1, arg2)
7145 int error
, val
= tcprexmtthresh
;
7147 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
7148 if (error
|| !req
->newptr
) {
7153 * Constrain the number of duplicate ACKs
7154 * to consider for TCP fast retransmit
7158 if (val
< 2 || val
> 3) {
7162 tcprexmtthresh
= val
;
7167 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, rexmt_thresh
, CTLTYPE_INT
| CTLFLAG_RW
|
7168 CTLFLAG_LOCKED
, &tcprexmtthresh
, 0, &sysctl_rexmtthresh
, "I",
7169 "Duplicate ACK Threshold for Fast Retransmit");