2 * Copyright (c) 2000-2017 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>
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
100 #include <netinet/in_var.h>
101 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
102 #include <netinet/in_pcb.h>
103 #include <netinet/ip_var.h>
104 #include <mach/sdt.h>
106 #include <netinet/ip6.h>
107 #include <netinet/icmp6.h>
108 #include <netinet6/nd6.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/in6_pcb.h>
112 #include <netinet/tcp.h>
113 #include <netinet/tcp_cache.h>
114 #include <netinet/tcp_fsm.h>
115 #include <netinet/tcp_seq.h>
116 #include <netinet/tcp_timer.h>
117 #include <netinet/tcp_var.h>
118 #include <netinet/tcp_cc.h>
119 #include <dev/random/randomdev.h>
120 #include <kern/zalloc.h>
122 #include <netinet6/tcp6_var.h>
124 #include <netinet/tcpip.h>
126 #include <netinet/tcp_debug.h>
127 u_char tcp_saveipgen
[40]; /* the size must be of max ip header, now IPv6 */
128 struct tcphdr tcp_savetcp
;
129 #endif /* TCPDEBUG */
132 #include <netinet6/ipsec.h>
134 #include <netinet6/ipsec6.h>
136 #include <netkey/key.h>
139 #if CONFIG_MACF_NET || CONFIG_MACF_SOCKET
140 #include <security/mac_framework.h>
141 #endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */
143 #include <sys/kdebug.h>
144 #include <netinet/lro_ext.h>
146 #include <netinet/mptcp_var.h>
147 #include <netinet/mptcp.h>
148 #include <netinet/mptcp_opt.h>
151 #include <corecrypto/ccaes.h>
153 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
154 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
155 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
156 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
158 #define TCP_RTT_HISTORY_EXPIRE_TIME (60 * TCP_RETRANSHZ)
159 #define TCP_RECV_THROTTLE_WIN (5 * TCP_RETRANSHZ)
160 #define TCP_STRETCHACK_ENABLE_PKTCNT 2000
162 struct tcpstat tcpstat
;
164 static int log_in_vain
= 0;
165 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
,
166 CTLFLAG_RW
| CTLFLAG_LOCKED
, &log_in_vain
, 0,
167 "Log all incoming TCP connections");
169 static int blackhole
= 0;
170 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
,
171 CTLFLAG_RW
| CTLFLAG_LOCKED
, &blackhole
, 0,
172 "Do not send RST when dropping refused connections");
174 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, delayed_ack
,
175 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_delack_enabled
, 3,
176 "Delay ACK to try and piggyback it onto a data packet");
178 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, tcp_lq_overflow
,
179 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_lq_overflow
, 1,
180 "Listen Queue Overflow");
182 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, recvbg
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
183 int, tcp_recv_bg
, 0, "Receive background");
186 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, drop_synfin
,
187 CTLFLAG_RW
| CTLFLAG_LOCKED
, static int, drop_synfin
, 1,
188 "Drop TCP packets with SYN+FIN set");
191 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
192 "TCP Segment Reassembly Queue");
194 static int tcp_reass_overflows
= 0;
195 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
,
196 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_reass_overflows
, 0,
197 "Global number of TCP Segment Reassembly Queue Overflows");
200 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, slowlink_wsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
201 __private_extern__
int, slowlink_wsize
, 8192,
202 "Maximum advertised window size for slowlink");
204 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, maxseg_unacked
,
205 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, maxseg_unacked
, 8,
206 "Maximum number of outstanding segments left unacked");
208 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, rfc3465
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
209 int, tcp_do_rfc3465
, 1, "");
211 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, rfc3465_lim2
,
212 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_do_rfc3465_lim2
, 1,
213 "Appropriate bytes counting w/ L=2*SMSS");
215 int rtt_samples_per_slot
= 20;
217 int tcp_acc_iaj_high_thresh
= ACC_IAJ_HIGH_THRESH
;
218 u_int32_t tcp_autorcvbuf_inc_shift
= 3;
219 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, recv_allowed_iaj
,
220 CTLFLAG_RW
| CTLFLAG_LOCKED
, int, tcp_allowed_iaj
, ALLOWED_IAJ
,
221 "Allowed inter-packet arrival jiter");
222 #if (DEVELOPMENT || DEBUG)
223 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_high_thresh
,
224 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_acc_iaj_high_thresh
, 0,
225 "Used in calculating maximum accumulated IAJ");
227 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufincshift
,
228 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_inc_shift
, 0,
229 "Shift for increment in receive socket buffer size");
230 #endif /* (DEVELOPMENT || DEBUG) */
232 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, doautorcvbuf
,
233 CTLFLAG_RW
| CTLFLAG_LOCKED
, u_int32_t
, tcp_do_autorcvbuf
, 1,
234 "Enable automatic socket buffer tuning");
236 SYSCTL_SKMEM_TCP_INT(OID_AUTO
, autorcvbufmax
,
237 CTLFLAG_RW
| CTLFLAG_LOCKED
, u_int32_t
, tcp_autorcvbuf_max
, 512 * 1024,
238 "Maximum receive socket buffer size");
240 u_int32_t tcp_autorcvbuf_max_ca
= 512 * 1024;
241 #if (DEBUG || DEVELOPMENT)
242 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufmaxca
,
243 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_max_ca
, 0,
244 "Maximum receive socket buffer size");
245 #endif /* (DEBUG || DEVELOPMENT) */
251 #endif /* !CONFIG_EMBEDDED */
252 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
253 &sw_lro
, 0, "Used to coalesce TCP packets");
256 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lrodbg
,
257 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lrodebug
, 0,
258 "Used to debug SW LRO");
261 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro_startcnt
,
262 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lro_start
, 0,
263 "Segments for starting LRO computed as power of 2");
265 int limited_txmt
= 1;
268 int tcp_dsack_enable
= 1;
270 #if (DEVELOPMENT || DEBUG)
271 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, limited_transmit
,
272 CTLFLAG_RW
| CTLFLAG_LOCKED
, &limited_txmt
, 0,
273 "Enable limited transmit");
275 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, early_rexmt
,
276 CTLFLAG_RW
| CTLFLAG_LOCKED
, &early_rexmt
, 0,
277 "Enable Early Retransmit");
279 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_ackadv
,
280 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sack_ackadv
, 0,
281 "Use SACK with cumulative ack advancement as a dupack");
283 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, dsack_enable
,
284 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_dsack_enable
, 0,
285 "use DSACK TCP option to report duplicate segments");
287 #endif /* (DEVELOPMENT || DEBUG) */
288 int tcp_disable_access_to_stats
= 1;
289 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, disable_access_to_stats
,
290 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_disable_access_to_stats
, 0,
291 "Disable access to tcpstat");
294 extern int tcp_TCPTV_MIN
;
295 extern int tcp_acc_iaj_high
;
296 extern int tcp_acc_iaj_react_limit
;
298 int tcprexmtthresh
= 3;
301 struct timeval tcp_uptime
; /* uptime when tcp_now was last updated */
302 lck_spin_t
*tcp_uptime_lock
; /* Used to sychronize updates to tcp_now */
304 struct inpcbhead tcb
;
305 #define tcb6 tcb /* for KAME src sync over BSD*'s */
306 struct inpcbinfo tcbinfo
;
308 static void tcp_dooptions(struct tcpcb
*, u_char
*, int, struct tcphdr
*,
310 static void tcp_finalize_options(struct tcpcb
*, struct tcpopt
*, unsigned int);
311 static void tcp_pulloutofband(struct socket
*,
312 struct tcphdr
*, struct mbuf
*, int);
313 static int tcp_reass(struct tcpcb
*, struct tcphdr
*, int *, struct mbuf
*,
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
)
407 if (tp
->iaj_size
== 0 || size
>= tp
->iaj_size
) {
409 tp
->iaj_rcv_ts
= tcp_now
;
410 tp
->iaj_small_pkt
= 0;
414 /* For every 32 bit unsigned integer(v), this function will find the
415 * largest integer n such that (n*n <= v). This takes at most 16 iterations
416 * irrespective of the value of v and does not involve multiplications.
419 isqrt(unsigned int val
)
421 unsigned int sqrt_cache
[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
422 unsigned int temp
, g
=0, b
=0x8000, bshft
=15;
424 for (g
= 0; g
<= 10; ++g
) {
425 if (sqrt_cache
[g
] > val
) {
428 } else if (sqrt_cache
[g
] == val
) {
434 temp
= (((g
<< 1) + b
) << (bshft
--));
440 } while ( b
> 0 && val
> 0);
446 * With LRO, roughly estimate the inter arrival time between
447 * each sub coalesced packet as an average. Count the delay
448 * cur_iaj to be the delay between the last packet received
449 * and the first packet of the LRO stream. Due to round off errors
450 * cur_iaj may be the same as lro_delay_factor. Averaging has
451 * round off errors too. lro_delay_factor may be close to 0
452 * in steady state leading to lower values fed to compute_iaj_meat.
455 compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
)
457 uint32_t cur_iaj
= tcp_now
- tp
->iaj_rcv_ts
;
458 uint32_t timediff
= 0;
460 if (cur_iaj
>= lro_delay_factor
) {
461 cur_iaj
= cur_iaj
- lro_delay_factor
;
464 compute_iaj_meat(tp
, cur_iaj
);
471 timediff
= lro_delay_factor
/nlropkts
;
475 compute_iaj_meat(tp
, timediff
);
481 void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
)
483 /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds,
484 * throttle the receive window to a minimum of MIN_IAJ_WIN packets
486 #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
487 #define IAJ_DIV_SHIFT 4
488 #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1))
490 uint32_t allowed_iaj
, acc_iaj
= 0;
495 cur_iaj_dev
= (cur_iaj
- tp
->avg_iaj
);
497 /* Allow a jitter of "allowed_iaj" milliseconds. Some connections
498 * may have a constant jitter more than that. We detect this by
499 * using standard deviation.
501 allowed_iaj
= tp
->avg_iaj
+ tp
->std_dev_iaj
;
502 if (allowed_iaj
< tcp_allowed_iaj
)
503 allowed_iaj
= tcp_allowed_iaj
;
505 /* Initially when the connection starts, the senders congestion
506 * window is small. During this period we avoid throttling a
507 * connection because we do not have a good starting point for
508 * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over
509 * the first few packets.
511 if (tp
->iaj_pktcnt
> IAJ_IGNORE_PKTCNT
) {
512 if ( cur_iaj
<= allowed_iaj
) {
513 if (tp
->acc_iaj
>= 2)
514 acc_iaj
= tp
->acc_iaj
- 2;
519 acc_iaj
= tp
->acc_iaj
+ (cur_iaj
- allowed_iaj
);
522 if (acc_iaj
> MAX_ACC_IAJ
)
523 acc_iaj
= MAX_ACC_IAJ
;
524 tp
->acc_iaj
= acc_iaj
;
527 /* Compute weighted average where the history has a weight of
528 * 15 out of 16 and the current value has a weight of 1 out of 16.
529 * This will make the short-term measurements have more weight.
531 * The addition of 8 will help to round-up the value
532 * instead of round-down
534 tp
->avg_iaj
= (((tp
->avg_iaj
<< IAJ_DIV_SHIFT
) - tp
->avg_iaj
)
535 + cur_iaj
+ IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
537 /* Compute Root-mean-square of deviation where mean is a weighted
538 * average as described above.
540 temp
= tp
->std_dev_iaj
* tp
->std_dev_iaj
;
541 mean
= (((temp
<< IAJ_DIV_SHIFT
) - temp
)
542 + (cur_iaj_dev
* cur_iaj_dev
)
543 + IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
545 tp
->std_dev_iaj
= isqrt(mean
);
547 DTRACE_TCP3(iaj
, struct tcpcb
*, tp
, uint32_t, cur_iaj
,
548 uint32_t, allowed_iaj
);
552 #endif /* TRAFFIC_MGT */
554 /* Check if enough amount of data has been acknowledged since
555 * bw measurement was started
558 tcp_bwmeas_check(struct tcpcb
*tp
)
560 int32_t bw_meas_bytes
;
561 uint32_t bw
, bytes
, elapsed_time
;
563 if (SEQ_LEQ(tp
->snd_una
, tp
->t_bwmeas
->bw_start
))
566 bw_meas_bytes
= tp
->snd_una
- tp
->t_bwmeas
->bw_start
;
567 if ((tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) &&
568 bw_meas_bytes
>= (int32_t)(tp
->t_bwmeas
->bw_size
)) {
569 bytes
= bw_meas_bytes
;
570 elapsed_time
= tcp_now
- tp
->t_bwmeas
->bw_ts
;
571 if (elapsed_time
> 0) {
572 bw
= bytes
/ elapsed_time
;
574 if (tp
->t_bwmeas
->bw_sndbw
> 0) {
575 tp
->t_bwmeas
->bw_sndbw
=
576 (((tp
->t_bwmeas
->bw_sndbw
<< 3)
577 - tp
->t_bwmeas
->bw_sndbw
)
580 tp
->t_bwmeas
->bw_sndbw
= bw
;
583 /* Store the maximum value */
584 if (tp
->t_bwmeas
->bw_sndbw_max
== 0) {
585 tp
->t_bwmeas
->bw_sndbw_max
=
586 tp
->t_bwmeas
->bw_sndbw
;
588 tp
->t_bwmeas
->bw_sndbw_max
=
589 max(tp
->t_bwmeas
->bw_sndbw
,
590 tp
->t_bwmeas
->bw_sndbw_max
);
594 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
599 tcp_reass(struct tcpcb
*tp
, struct tcphdr
*th
, int *tlenp
, struct mbuf
*m
,
603 struct tseg_qent
*p
= NULL
;
604 struct tseg_qent
*nq
;
605 struct tseg_qent
*te
= NULL
;
606 struct inpcb
*inp
= tp
->t_inpcb
;
607 struct socket
*so
= inp
->inp_socket
;
610 struct mbuf
*oodata
= NULL
;
613 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
614 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
615 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
616 boolean_t dsack_set
= FALSE
;
619 * Call with th==0 after become established to
620 * force pre-ESTABLISHED data up to user socket.
626 * If the reassembly queue already has entries or if we are going
627 * to add a new one, then the connection has reached a loss state.
628 * Reset the stretch-ack algorithm at this point.
630 tcp_reset_stretch_ack(tp
);
635 #endif /* TRAFFIC_MGT */
638 * Limit the number of segments in the reassembly queue to prevent
639 * holding on to too many segments (and thus running out of mbufs).
640 * Make sure to let the missing segment through which caused this
641 * queue. Always keep one global queue entry spare to be able to
642 * process the missing segment.
644 qlimit
= min(max(100, so
->so_rcv
.sb_hiwat
>> 10),
645 (TCP_AUTORCVBUF_MAX(ifp
) >> 10));
646 if (th
->th_seq
!= tp
->rcv_nxt
&&
647 (tp
->t_reassqlen
+ 1) >= qlimit
) {
648 tcp_reass_overflows
++;
649 tcpstat
.tcps_rcvmemdrop
++;
655 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
656 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
658 tcpstat
.tcps_rcvmemdrop
++;
665 * Find a segment which begins after this one does.
667 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
668 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
))
674 * If there is a preceding segment, it may provide some of
675 * our data already. If so, drop the data from the incoming
676 * segment. If it provides all of our data, drop us.
680 /* conversion to int (in i) handles seq wraparound */
681 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
683 if (TCP_DSACK_ENABLED(tp
) && i
> 1) {
685 * Note duplicate data sequnce numbers
686 * to report in DSACK option
688 tp
->t_dsack_lseq
= th
->th_seq
;
689 tp
->t_dsack_rseq
= th
->th_seq
+
693 * Report only the first part of partial/
694 * non-contiguous duplicate sequence space
699 tcpstat
.tcps_rcvduppack
++;
700 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
702 nstat_route_rx(inp
->inp_route
.ro_rt
,
704 NSTAT_RX_FLAG_DUPLICATE
);
705 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
707 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
709 tp
->t_stat
.rxduplicatebytes
+= *tlenp
;
710 inp_set_activity_bitmap(inp
);
713 zfree(tcp_reass_zone
, te
);
717 * Try to present any queued data
718 * at the left window edge to the user.
719 * This is needed after the 3-WHS
730 tcpstat
.tcps_rcvoopack
++;
731 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
733 nstat_route_rx(inp
->inp_route
.ro_rt
, 1, *tlenp
,
734 NSTAT_RX_FLAG_OUT_OF_ORDER
);
735 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
736 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, *tlenp
);
737 tp
->t_stat
.rxoutoforderbytes
+= *tlenp
;
738 inp_set_activity_bitmap(inp
);
742 * While we overlap succeeding segments trim them or,
743 * if they are completely covered, dequeue them.
746 int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
751 * Report only the first part of partial/non-contiguous
752 * duplicate segment in dsack option. The variable
753 * dsack_set will be true if a previous entry has some of
754 * the duplicate sequence space.
756 if (TCP_DSACK_ENABLED(tp
) && i
> 1 && !dsack_set
) {
757 if (tp
->t_dsack_lseq
== 0) {
758 tp
->t_dsack_lseq
= q
->tqe_th
->th_seq
;
760 tp
->t_dsack_lseq
+ min(i
, q
->tqe_len
);
763 * this segment overlaps data in multple
764 * entries in the reassembly queue, move
765 * the right sequence number further.
768 tp
->t_dsack_rseq
+ min(i
, q
->tqe_len
);
771 if (i
< q
->tqe_len
) {
772 q
->tqe_th
->th_seq
+= i
;
778 nq
= LIST_NEXT(q
, tqe_q
);
779 LIST_REMOVE(q
, tqe_q
);
781 zfree(tcp_reass_zone
, q
);
786 /* Insert the new segment queue entry into place. */
789 te
->tqe_len
= *tlenp
;
792 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
794 LIST_INSERT_AFTER(p
, te
, tqe_q
);
798 * New out-of-order data exists, and is pointed to by
799 * queue entry te. Set copy_oodata to 1 so out-of-order data
800 * can be copied off to sockbuf after in-order data
803 if (!(so
->so_state
& SS_CANTRCVMORE
))
808 * Present data to user, advancing rcv_nxt through
809 * completed sequence space.
811 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
813 q
= LIST_FIRST(&tp
->t_segq
);
814 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
) {
815 /* Stop using LRO once out of order packets arrive */
816 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
817 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
818 th
->th_dport
, th
->th_sport
);
819 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
823 * continue processing if out-of-order data
826 if (q
&& (so
->so_flags
& SOF_ENABLE_MSGS
))
827 goto msg_unordered_delivery
;
833 * If there is already another thread doing reassembly for this
834 * connection, it is better to let it finish the job --
837 if (tp
->t_flagsext
& TF_REASS_INPROG
)
840 tp
->t_flagsext
|= TF_REASS_INPROG
;
841 /* lost packet was recovered, so ooo data can be returned */
842 tcpstat
.tcps_recovered_pkts
++;
845 tp
->rcv_nxt
+= q
->tqe_len
;
846 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
847 LIST_REMOVE(q
, tqe_q
);
848 if (so
->so_state
& SS_CANTRCVMORE
) {
851 so_recv_data_stat(so
, q
->tqe_m
, 0); /* XXXX */
852 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
854 * Append the inorder data as a message to the
855 * receive socket buffer. Also check to see if
856 * the data we are about to deliver is the same
857 * data that we wanted to pass up to the user
858 * out of order. If so, reset copy_oodata --
859 * the received data filled a gap, and
865 if (sbappendstream_rcvdemux(so
, q
->tqe_m
,
866 q
->tqe_th
->th_seq
- (tp
->irs
+ 1), 0))
868 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
869 tcp_update_lro_seq(tp
->rcv_nxt
,
870 inp
->inp_laddr
, inp
->inp_faddr
,
871 th
->th_dport
, th
->th_sport
);
874 zfree(tcp_reass_zone
, q
);
876 q
= LIST_FIRST(&tp
->t_segq
);
877 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
878 tp
->t_flagsext
&= ~TF_REASS_INPROG
;
881 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
883 KERNEL_DEBUG(DBG_LAYER_BEG
,
884 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
885 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
886 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
892 KERNEL_DEBUG(DBG_LAYER_BEG
,
893 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
894 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
895 (inp
->inp_faddr
.s_addr
& 0xffff)),
899 msg_unordered_delivery
:
900 /* Deliver out-of-order data as a message */
901 if (te
&& (so
->so_flags
& SOF_ENABLE_MSGS
) && copy_oodata
&& te
->tqe_len
) {
903 * make a copy of the mbuf to be delivered up to
904 * the user, and add it to the sockbuf
906 oodata
= m_copym(te
->tqe_m
, 0, M_COPYALL
, M_DONTWAIT
);
907 if (oodata
!= NULL
) {
908 if (sbappendmsgstream_rcv(&so
->so_rcv
, oodata
,
909 te
->tqe_th
->th_seq
- (tp
->irs
+ 1), 1)) {
911 tcpstat
.tcps_msg_unopkts
++;
913 tcpstat
.tcps_msg_unoappendfail
++;
919 sorwakeup(so
); /* done with socket lock held */
924 * Reduce congestion window -- used when ECN is seen or when a tail loss
925 * probe recovers the last packet.
928 tcp_reduce_congestion_window(
932 * If the current tcp cc module has
933 * defined a hook for tasks to run
934 * before entering FR, call it
936 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
937 CC_ALGO(tp
)->pre_fr(tp
);
938 ENTER_FASTRECOVERY(tp
);
939 if (tp
->t_flags
& TF_SENTFIN
)
940 tp
->snd_recover
= tp
->snd_max
- 1;
942 tp
->snd_recover
= tp
->snd_max
;
943 tp
->t_timer
[TCPT_REXMT
] = 0;
944 tp
->t_timer
[TCPT_PTO
] = 0;
946 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
947 tcp_cc_adjust_nonvalidated_cwnd(tp
);
949 tp
->snd_cwnd
= tp
->snd_ssthresh
+
950 tp
->t_maxseg
* tcprexmtthresh
;
955 * This function is called upon reception of data on a socket. It's purpose is
956 * to handle the adaptive keepalive timers that monitor whether the connection
957 * is making progress. First the adaptive read-timer, second the TFO probe-timer.
959 * The application wants to get an event if there is a stall during read.
960 * Set the initial keepalive timeout to be equal to twice RTO.
962 * If the outgoing interface is in marginal conditions, we need to
963 * enable read probes for that too.
966 tcp_adaptive_rwtimo_check(struct tcpcb
*tp
, int tlen
)
968 struct ifnet
*outifp
= tp
->t_inpcb
->inp_last_outifp
;
970 if ((tp
->t_adaptive_rtimo
> 0 ||
972 (outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
)))
974 tp
->t_state
== TCPS_ESTABLISHED
) {
975 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
976 (TCP_REXMTVAL(tp
) << 1));
977 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
978 tp
->t_rtimo_probes
= 0;
983 tcp_keepalive_reset(struct tcpcb
*tp
)
985 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
986 TCP_CONN_KEEPIDLE(tp
));
987 tp
->t_flagsext
&= ~(TF_DETECT_READSTALL
);
988 tp
->t_rtimo_probes
= 0;
992 * TCP input routine, follows pages 65-76 of the
993 * protocol specification dated September, 1981 very closely.
997 tcp6_input(struct mbuf
**mp
, int *offp
, int proto
)
999 #pragma unused(proto)
1000 struct mbuf
*m
= *mp
;
1002 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1004 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
1006 /* Expect 32-bit aligned data pointer on strict-align platforms */
1007 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1010 * draft-itojun-ipv6-tcp-to-anycast
1011 * better place to put this in?
1013 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) == 0) {
1014 if (ia6_flags
& IN6_IFF_ANYCAST
) {
1015 struct ip6_hdr
*ip6
;
1017 ip6
= mtod(m
, struct ip6_hdr
*);
1018 icmp6_error(m
, ICMP6_DST_UNREACH
,
1019 ICMP6_DST_UNREACH_ADDR
,
1020 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
1022 IF_TCP_STATINC(ifp
, icmp6unreach
);
1024 return (IPPROTO_DONE
);
1028 tcp_input(m
, *offp
);
1029 return (IPPROTO_DONE
);
1033 /* Depending on the usage of mbuf space in the system, this function
1034 * will return true or false. This is used to determine if a socket
1035 * buffer can take more memory from the system for auto-tuning or not.
1038 tcp_cansbgrow(struct sockbuf
*sb
)
1040 /* Calculate the host level space limit in terms of MSIZE buffers.
1041 * We can use a maximum of half of the available mbuf space for
1044 u_int32_t mblim
= ((nmbclusters
>> 1) << (MCLSHIFT
- MSIZESHIFT
));
1046 /* Calculate per sb limit in terms of bytes. We optimize this limit
1047 * for upto 16 socket buffers.
1050 u_int32_t sbspacelim
= ((nmbclusters
>> 4) << MCLSHIFT
);
1052 if ((total_sbmb_cnt
< mblim
) &&
1053 (sb
->sb_hiwat
< sbspacelim
)) {
1056 OSIncrementAtomic64(&sbmb_limreached
);
1062 tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1063 u_int32_t newsize
, u_int32_t idealsize
, u_int32_t rcvbuf_max
)
1065 /* newsize should not exceed max */
1066 newsize
= min(newsize
, rcvbuf_max
);
1068 /* The receive window scale negotiated at the
1069 * beginning of the connection will also set a
1070 * limit on the socket buffer size
1072 newsize
= min(newsize
, TCP_MAXWIN
<< tp
->rcv_scale
);
1074 /* Set new socket buffer size */
1075 if (newsize
> sbrcv
->sb_hiwat
&&
1076 (sbreserve(sbrcv
, newsize
) == 1)) {
1077 sbrcv
->sb_idealsize
= min(max(sbrcv
->sb_idealsize
,
1078 (idealsize
!= 0) ? idealsize
: newsize
), rcvbuf_max
);
1080 /* Again check the limit set by the advertised
1083 sbrcv
->sb_idealsize
= min(sbrcv
->sb_idealsize
,
1084 TCP_MAXWIN
<< tp
->rcv_scale
);
1089 * This function is used to grow a receive socket buffer. It
1090 * will take into account system-level memory usage and the
1091 * bandwidth available on the link to make a decision.
1094 tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1095 struct tcpopt
*to
, u_int32_t pktlen
, u_int32_t rcvbuf_max
)
1097 struct socket
*so
= sbrcv
->sb_so
;
1100 * Do not grow the receive socket buffer if
1101 * - auto resizing is disabled, globally or on this socket
1102 * - the high water mark already reached the maximum
1103 * - the stream is in background and receive side is being
1105 * - if there are segments in reassembly queue indicating loss,
1106 * do not need to increase recv window during recovery as more
1107 * data is not going to be sent. A duplicate ack sent during
1108 * recovery should not change the receive window
1110 if (tcp_do_autorcvbuf
== 0 ||
1111 (sbrcv
->sb_flags
& SB_AUTOSIZE
) == 0 ||
1112 tcp_cansbgrow(sbrcv
) == 0 ||
1113 sbrcv
->sb_hiwat
>= rcvbuf_max
||
1114 (tp
->t_flagsext
& TF_RECV_THROTTLE
) ||
1115 (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) ||
1116 !LIST_EMPTY(&tp
->t_segq
)) {
1117 /* Can not resize the socket buffer, just return */
1121 if (TSTMP_GT(tcp_now
,
1122 tp
->rfbuf_ts
+ TCPTV_RCVBUFIDLE
)) {
1123 /* If there has been an idle period in the
1124 * connection, just restart the measurement
1129 if (!TSTMP_SUPPORTED(tp
)) {
1131 * Timestamp option is not supported on this connection.
1132 * If the connection reached a state to indicate that
1133 * the receive socket buffer needs to grow, increase
1134 * the high water mark.
1136 if (TSTMP_GEQ(tcp_now
,
1137 tp
->rfbuf_ts
+ TCPTV_RCVNOTS_QUANTUM
)) {
1138 if (tp
->rfbuf_cnt
>= TCP_RCVNOTS_BYTELEVEL
) {
1139 tcp_sbrcv_reserve(tp
, sbrcv
,
1140 tcp_autorcvbuf_max
, 0,
1141 tcp_autorcvbuf_max
);
1145 tp
->rfbuf_cnt
+= pktlen
;
1148 } else if (to
->to_tsecr
!= 0) {
1150 * If the timestamp shows that one RTT has
1151 * completed, we can stop counting the
1152 * bytes. Here we consider increasing
1153 * the socket buffer if the bandwidth measured in
1154 * last rtt, is more than half of sb_hiwat, this will
1155 * help to scale the buffer according to the bandwidth
1158 if (TSTMP_GEQ(to
->to_tsecr
, tp
->rfbuf_ts
)) {
1159 if (tp
->rfbuf_cnt
> (sbrcv
->sb_hiwat
-
1160 (sbrcv
->sb_hiwat
>> 1))) {
1161 int32_t rcvbuf_inc
, min_incr
;
1163 * Increment the receive window by a
1164 * multiple of maximum sized segments.
1165 * This will prevent a connection from
1166 * sending smaller segments on wire if it
1167 * is limited by the receive window.
1169 * Set the ideal size based on current
1170 * bandwidth measurements. We set the
1171 * ideal size on receive socket buffer to
1172 * be twice the bandwidth delay product.
1174 rcvbuf_inc
= (tp
->rfbuf_cnt
<< 1)
1178 * Make the increment equal to 8 segments
1181 min_incr
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1182 if (rcvbuf_inc
< min_incr
)
1183 rcvbuf_inc
= min_incr
;
1186 (rcvbuf_inc
/ tp
->t_maxseg
) * tp
->t_maxseg
;
1187 tcp_sbrcv_reserve(tp
, sbrcv
,
1188 sbrcv
->sb_hiwat
+ rcvbuf_inc
,
1189 (tp
->rfbuf_cnt
* 2), rcvbuf_max
);
1191 /* Measure instantaneous receive bandwidth */
1192 if (tp
->t_bwmeas
!= NULL
&& tp
->rfbuf_cnt
> 0 &&
1193 TSTMP_GT(tcp_now
, tp
->rfbuf_ts
)) {
1195 rcv_bw
= tp
->rfbuf_cnt
/
1196 (int)(tcp_now
- tp
->rfbuf_ts
);
1197 if (tp
->t_bwmeas
->bw_rcvbw_max
== 0) {
1198 tp
->t_bwmeas
->bw_rcvbw_max
= rcv_bw
;
1200 tp
->t_bwmeas
->bw_rcvbw_max
= max(
1201 tp
->t_bwmeas
->bw_rcvbw_max
, rcv_bw
);
1206 tp
->rfbuf_cnt
+= pktlen
;
1211 /* Restart the measurement */
1217 /* This function will trim the excess space added to the socket buffer
1218 * to help a slow-reading app. The ideal-size of a socket buffer depends
1219 * on the link bandwidth or it is set by an application and we aim to
1223 tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sbrcv
)
1225 if (tcp_do_autorcvbuf
== 1 && sbrcv
->sb_idealsize
> 0 &&
1226 sbrcv
->sb_hiwat
> sbrcv
->sb_idealsize
) {
1228 /* compute the difference between ideal and current sizes */
1229 u_int32_t diff
= sbrcv
->sb_hiwat
- sbrcv
->sb_idealsize
;
1231 /* Compute the maximum advertised window for
1234 u_int32_t advwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
1236 /* How much can we trim the receive socket buffer?
1237 * 1. it can not be trimmed beyond the max rcv win advertised
1238 * 2. if possible, leave 1/16 of bandwidth*delay to
1239 * avoid closing the win completely
1241 u_int32_t leave
= max(advwin
, (sbrcv
->sb_idealsize
>> 4));
1243 /* Sometimes leave can be zero, in that case leave at least
1244 * a few segments worth of space.
1247 leave
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1249 trim
= sbrcv
->sb_hiwat
- (sbrcv
->sb_cc
+ leave
);
1250 trim
= imin(trim
, (int32_t)diff
);
1253 sbreserve(sbrcv
, (sbrcv
->sb_hiwat
- trim
));
1257 /* We may need to trim the send socket buffer size for two reasons:
1258 * 1. if the rtt seen on the connection is climbing up, we do not
1259 * want to fill the buffers any more.
1260 * 2. if the congestion win on the socket backed off, there is no need
1261 * to hold more mbufs for that connection than what the cwnd will allow.
1264 tcp_sbsnd_trim(struct sockbuf
*sbsnd
)
1266 if (tcp_do_autosendbuf
== 1 &&
1267 ((sbsnd
->sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) ==
1268 (SB_AUTOSIZE
| SB_TRIM
)) &&
1269 (sbsnd
->sb_idealsize
> 0) &&
1270 (sbsnd
->sb_hiwat
> sbsnd
->sb_idealsize
)) {
1272 if (sbsnd
->sb_cc
<= sbsnd
->sb_idealsize
) {
1273 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_idealsize
;
1275 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_cc
;
1277 sbreserve(sbsnd
, (sbsnd
->sb_hiwat
- trim
));
1279 if (sbsnd
->sb_hiwat
<= sbsnd
->sb_idealsize
)
1280 sbsnd
->sb_flags
&= ~(SB_TRIM
);
1284 * If timestamp option was not negotiated on this connection
1285 * and this connection is on the receiving side of a stream
1286 * then we can not measure the delay on the link accurately.
1287 * Instead of enabling automatic receive socket buffer
1288 * resizing, just give more space to the receive socket buffer.
1291 tcp_sbrcv_tstmp_check(struct tcpcb
*tp
)
1293 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1294 u_int32_t newsize
= 2 * tcp_recvspace
;
1295 struct sockbuf
*sbrcv
= &so
->so_rcv
;
1297 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_RCVD_TSTMP
)) !=
1298 (TF_REQ_TSTMP
| TF_RCVD_TSTMP
) &&
1299 (sbrcv
->sb_flags
& SB_AUTOSIZE
) != 0) {
1300 tcp_sbrcv_reserve(tp
, sbrcv
, newsize
, 0, newsize
);
1304 /* A receiver will evaluate the flow of packets on a connection
1305 * to see if it can reduce ack traffic. The receiver will start
1306 * stretching acks if all of the following conditions are met:
1307 * 1. tcp_delack_enabled is set to 3
1308 * 2. If the bytes received in the last 100ms is greater than a threshold
1309 * defined by maxseg_unacked
1310 * 3. If the connection has not been idle for tcp_maxrcvidle period.
1311 * 4. If the connection has seen enough packets to let the slow-start
1312 * finish after connection establishment or after some packet loss.
1314 * The receiver will stop stretching acks if there is congestion/reordering
1315 * as indicated by packets on reassembly queue or an ECN. If the delayed-ack
1316 * timer fires while stretching acks, it means that the packet flow has gone
1317 * below the threshold defined by maxseg_unacked and the receiver will stop
1318 * stretching acks. The receiver gets no indication when slow-start is completed
1319 * or when the connection reaches an idle state. That is why we use
1320 * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle
1324 tcp_stretch_ack_enable(struct tcpcb
*tp
, int thflags
)
1326 if (tp
->rcv_by_unackwin
>= (maxseg_unacked
* tp
->t_maxseg
) &&
1327 TSTMP_GEQ(tp
->rcv_unackwin
, tcp_now
))
1328 tp
->t_flags
|= TF_STREAMING_ON
;
1330 tp
->t_flags
&= ~TF_STREAMING_ON
;
1332 /* If there has been an idle time, reset streaming detection */
1333 if (TSTMP_GT(tcp_now
, tp
->rcv_unackwin
+ tcp_maxrcvidle
))
1334 tp
->t_flags
&= ~TF_STREAMING_ON
;
1337 * If there are flags other than TH_ACK set, reset streaming
1340 if (thflags
& ~TH_ACK
)
1341 tp
->t_flags
&= ~TF_STREAMING_ON
;
1343 if (tp
->t_flagsext
& TF_DISABLE_STRETCHACK
) {
1344 if (tp
->rcv_nostrack_pkts
>= TCP_STRETCHACK_ENABLE_PKTCNT
) {
1345 tp
->t_flagsext
&= ~TF_DISABLE_STRETCHACK
;
1346 tp
->rcv_nostrack_pkts
= 0;
1347 tp
->rcv_nostrack_ts
= 0;
1349 tp
->rcv_nostrack_pkts
++;
1353 if (!(tp
->t_flagsext
& (TF_NOSTRETCHACK
|TF_DISABLE_STRETCHACK
)) &&
1354 (tp
->t_flags
& TF_STREAMING_ON
) &&
1355 (!(tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) ||
1356 (tp
->rcv_waitforss
>= tcp_rcvsspktcnt
))) {
1364 * Reset the state related to stretch-ack algorithm. This will make
1365 * the receiver generate an ack every other packet. The receiver
1366 * will start re-evaluating the rate at which packets come to decide
1367 * if it can benefit by lowering the ack traffic.
1370 tcp_reset_stretch_ack(struct tcpcb
*tp
)
1372 tp
->t_flags
&= ~(TF_STRETCHACK
|TF_STREAMING_ON
);
1373 tp
->rcv_by_unackwin
= 0;
1374 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
1377 * When there is packet loss or packet re-ordering or CWR due to
1378 * ECN, the sender's congestion window is reduced. In these states,
1379 * generate an ack for every other packet for some time to allow
1380 * the sender's congestion window to grow.
1382 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
1383 tp
->rcv_waitforss
= 0;
1387 * The last packet was a retransmission, check if this ack
1388 * indicates that the retransmission was spurious.
1390 * If the connection supports timestamps, we could use it to
1391 * detect if the last retransmit was not needed. Otherwise,
1392 * we check if the ACK arrived within RTT/2 window, then it
1393 * was a mistake to do the retransmit in the first place.
1395 * This function will return 1 if it is a spurious retransmit,
1399 tcp_detect_bad_rexmt(struct tcpcb
*tp
, struct tcphdr
*th
,
1400 struct tcpopt
*to
, u_int32_t rxtime
)
1402 int32_t tdiff
, bad_rexmt_win
;
1403 bad_rexmt_win
= (tp
->t_srtt
>> (TCP_RTT_SHIFT
+ 1));
1405 /* If the ack has ECN CE bit, then cwnd has to be adjusted */
1406 if (TCP_ECN_ENABLED(tp
) && (th
->th_flags
& TH_ECE
))
1408 if (TSTMP_SUPPORTED(tp
)) {
1409 if (rxtime
> 0 && (to
->to_flags
& TOF_TS
)
1410 && to
->to_tsecr
!= 0
1411 && TSTMP_LT(to
->to_tsecr
, rxtime
))
1414 if ((tp
->t_rxtshift
== 1
1415 || (tp
->t_flagsext
& TF_SENT_TLPROBE
))
1417 tdiff
= (int32_t)(tcp_now
- rxtime
);
1418 if (tdiff
< bad_rexmt_win
)
1427 * Restore congestion window state if a spurious timeout
1431 tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
)
1433 if (TSTMP_SUPPORTED(tp
)) {
1434 u_int32_t fsize
, acked
;
1435 fsize
= tp
->snd_max
- th
->th_ack
;
1436 acked
= BYTES_ACKED(th
, tp
);
1439 * Implement bad retransmit recovery as
1440 * described in RFC 4015.
1442 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1444 /* Initialize cwnd to the initial window */
1445 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
1446 CC_ALGO(tp
)->cwnd_init(tp
);
1448 tp
->snd_cwnd
= fsize
+ min(acked
, tp
->snd_cwnd
);
1451 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1452 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1453 if (tp
->t_flags
& TF_WASFRECOVERY
)
1454 ENTER_FASTRECOVERY(tp
);
1456 /* Do not use the loss flight size in this case */
1457 tp
->t_lossflightsize
= 0;
1459 tp
->snd_cwnd
= max(tp
->snd_cwnd
, TCP_CC_CWND_INIT_BYTES
);
1460 tp
->snd_recover
= tp
->snd_recover_prev
;
1461 tp
->snd_nxt
= tp
->snd_max
;
1463 /* Fix send socket buffer to reflect the change in cwnd */
1464 tcp_bad_rexmt_fix_sndbuf(tp
);
1467 * This RTT might reflect the extra delay induced
1468 * by the network. Skip using this sample for RTO
1469 * calculation and mark the connection so we can
1470 * recompute RTT when the next eligible sample is
1473 tp
->t_flagsext
|= TF_RECOMPUTE_RTT
;
1474 tp
->t_badrexmt_time
= tcp_now
;
1479 * If the previous packet was sent in retransmission timer, and it was
1480 * not needed, then restore the congestion window to the state before that
1483 * If the last packet was sent in tail loss probe timeout, check if that
1484 * recovered the last packet. If so, that will indicate a real loss and
1485 * the congestion window needs to be lowered.
1488 tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
, struct tcpopt
*to
)
1490 if (tp
->t_rxtshift
> 0 &&
1491 tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_rxtstart
)) {
1492 ++tcpstat
.tcps_sndrexmitbad
;
1493 tcp_bad_rexmt_restore_state(tp
, th
);
1494 tcp_ccdbg_trace(tp
, th
, TCP_CC_BAD_REXMT_RECOVERY
);
1495 } else if ((tp
->t_flagsext
& TF_SENT_TLPROBE
)
1496 && tp
->t_tlphighrxt
> 0
1497 && SEQ_GEQ(th
->th_ack
, tp
->t_tlphighrxt
)
1498 && !tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_tlpstart
)) {
1500 * check DSACK information also to make sure that
1501 * the TLP was indeed needed
1503 if (tcp_rxtseg_dsack_for_tlp(tp
)) {
1505 * received a DSACK to indicate that TLP was
1508 tcp_rxtseg_clean(tp
);
1513 * The tail loss probe recovered the last packet and
1514 * we need to adjust the congestion window to take
1515 * this loss into account.
1517 ++tcpstat
.tcps_tlp_recoverlastpkt
;
1518 if (!IN_FASTRECOVERY(tp
)) {
1519 tcp_reduce_congestion_window(tp
);
1520 EXIT_FASTRECOVERY(tp
);
1522 tcp_ccdbg_trace(tp
, th
, TCP_CC_TLP_RECOVER_LASTPACKET
);
1523 } else if (tcp_rxtseg_detect_bad_rexmt(tp
, th
->th_ack
)) {
1525 * All of the retransmitted segments were duplicated, this
1526 * can be an indication of bad fast retransmit.
1528 tcpstat
.tcps_dsack_badrexmt
++;
1529 tcp_bad_rexmt_restore_state(tp
, th
);
1530 tcp_ccdbg_trace(tp
, th
, TCP_CC_DSACK_BAD_REXMT
);
1531 tcp_rxtseg_clean(tp
);
1534 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1535 tp
->t_tlphighrxt
= 0;
1539 * check if the latest ack was for a segment sent during PMTU
1540 * blackhole detection. If the timestamp on the ack is before
1541 * PMTU blackhole detection, then revert the size of the max
1542 * segment to previous size.
1544 if (tp
->t_rxtshift
> 0 && (tp
->t_flags
& TF_BLACKHOLE
) &&
1545 tp
->t_pmtud_start_ts
> 0 && TSTMP_SUPPORTED(tp
)) {
1546 if ((to
->to_flags
& TOF_TS
) && to
->to_tsecr
!= 0
1547 && TSTMP_LT(to
->to_tsecr
, tp
->t_pmtud_start_ts
)) {
1548 tcp_pmtud_revert_segment_size(tp
);
1551 if (tp
->t_pmtud_start_ts
> 0)
1552 tp
->t_pmtud_start_ts
= 0;
1556 * Check if early retransmit can be attempted according to RFC 5827.
1558 * If packet reordering is detected on a connection, fast recovery will
1559 * be delayed until it is clear that the packet was lost and not reordered.
1560 * But reordering detection is done only when SACK is enabled.
1562 * On connections that do not support SACK, there is a limit on the number
1563 * of early retransmits that can be done per minute. This limit is needed
1564 * to make sure that too many packets are not retransmitted when there is
1565 * packet reordering.
1568 tcp_early_rexmt_check (struct tcpcb
*tp
, struct tcphdr
*th
)
1570 u_int32_t obytes
, snd_off
;
1572 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1574 if (early_rexmt
&& (SACK_ENABLED(tp
) ||
1575 tp
->t_early_rexmt_count
< TCP_EARLY_REXMT_LIMIT
) &&
1576 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1577 (tp
->t_dupacks
== 1 ||
1578 (SACK_ENABLED(tp
) &&
1579 !TAILQ_EMPTY(&tp
->snd_holes
)))) {
1581 * If there are only a few outstanding
1582 * segments on the connection, we might need
1583 * to lower the retransmit threshold. This
1584 * will allow us to do Early Retransmit as
1585 * described in RFC 5827.
1587 if (SACK_ENABLED(tp
) &&
1588 !TAILQ_EMPTY(&tp
->snd_holes
)) {
1589 obytes
= (tp
->snd_max
- tp
->snd_fack
) +
1590 tp
->sackhint
.sack_bytes_rexmit
;
1592 obytes
= (tp
->snd_max
- tp
->snd_una
);
1596 * In order to lower retransmit threshold the
1597 * following two conditions must be met.
1598 * 1. the amount of outstanding data is less
1600 * 2. there is no unsent data ready for
1601 * transmission or the advertised window
1602 * will limit sending new segments.
1604 snd_off
= tp
->snd_max
- tp
->snd_una
;
1605 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
) - snd_off
;
1606 if (obytes
< (tp
->t_maxseg
<< 2) &&
1610 osegs
= obytes
/ tp
->t_maxseg
;
1611 if ((osegs
* tp
->t_maxseg
) < obytes
)
1615 * Since the connection might have already
1616 * received some dupacks, we add them to
1617 * to the outstanding segments count to get
1618 * the correct retransmit threshold.
1620 * By checking for early retransmit after
1621 * receiving some duplicate acks when SACK
1622 * is supported, the connection will
1623 * enter fast recovery even if multiple
1624 * segments are lost in the same window.
1626 osegs
+= tp
->t_dupacks
;
1629 ((osegs
- 1) > 1) ? (osegs
- 1) : 1;
1631 min(tp
->t_rexmtthresh
, tcprexmtthresh
);
1633 max(tp
->t_rexmtthresh
, tp
->t_dupacks
);
1635 if (tp
->t_early_rexmt_count
== 0)
1636 tp
->t_early_rexmt_win
= tcp_now
;
1638 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1639 tcpstat
.tcps_tlp_recovery
++;
1640 tcp_ccdbg_trace(tp
, th
,
1641 TCP_CC_TLP_RECOVERY
);
1643 tcpstat
.tcps_early_rexmt
++;
1644 tp
->t_early_rexmt_count
++;
1645 tcp_ccdbg_trace(tp
, th
,
1646 TCP_CC_EARLY_RETRANSMIT
);
1653 * If we ever sent a TLP probe, the acknowledgement will trigger
1654 * early retransmit because the value of snd_fack will be close
1655 * to snd_max. This will take care of adjustments to the
1656 * congestion window. So we can reset TF_SENT_PROBE flag.
1658 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1659 tp
->t_tlphighrxt
= 0;
1664 tcp_tfo_syn(struct tcpcb
*tp
, struct tcpopt
*to
)
1666 u_char out
[CCAES_BLOCK_SIZE
];
1669 if (!(to
->to_flags
& (TOF_TFO
| TOF_TFOREQ
)) ||
1670 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
))
1673 if ((to
->to_flags
& TOF_TFOREQ
)) {
1674 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1676 tp
->t_tfo_stats
|= TFO_S_COOKIEREQ_RECV
;
1677 tcpstat
.tcps_tfo_cookie_req_rcv
++;
1681 /* Ok, then it must be an offered cookie. We need to check that ... */
1682 tcp_tfo_gen_cookie(tp
->t_inpcb
, out
, sizeof(out
));
1684 len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1686 if (memcmp(out
, to
->to_tfo
, len
)) {
1687 /* Cookies are different! Let's return and offer a new cookie */
1688 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1690 tp
->t_tfo_stats
|= TFO_S_COOKIE_INVALID
;
1691 tcpstat
.tcps_tfo_cookie_invalid
++;
1695 if (OSIncrementAtomic(&tcp_tfo_halfcnt
) >= tcp_tfo_backlog
) {
1696 /* Need to decrement again as we just increased it... */
1697 OSDecrementAtomic(&tcp_tfo_halfcnt
);
1701 tp
->t_tfo_flags
|= TFO_F_COOKIE_VALID
;
1703 tp
->t_tfo_stats
|= TFO_S_SYNDATA_RCV
;
1704 tcpstat
.tcps_tfo_syn_data_rcv
++;
1710 tcp_tfo_synack(struct tcpcb
*tp
, struct tcpopt
*to
)
1712 if (to
->to_flags
& TOF_TFO
) {
1713 unsigned char len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1716 * If this happens, things have gone terribly wrong. len should
1717 * have been checked in tcp_dooptions.
1719 VERIFY(len
<= TFO_COOKIE_LEN_MAX
);
1723 tcp_cache_set_cookie(tp
, to
->to_tfo
, len
);
1724 tcp_heuristic_tfo_success(tp
);
1726 tp
->t_tfo_stats
|= TFO_S_COOKIE_RCV
;
1727 tcpstat
.tcps_tfo_cookie_rcv
++;
1728 if (tp
->t_tfo_flags
& TFO_F_COOKIE_SENT
) {
1729 tcpstat
.tcps_tfo_cookie_wrong
++;
1730 tp
->t_tfo_stats
|= TFO_S_COOKIE_WRONG
;
1734 * Thus, no cookie in the response, but we either asked for one
1735 * or sent SYN+DATA. Now, we need to check whether we had to
1736 * rexmit the SYN. If that's the case, it's better to start
1737 * backing of TFO-cookie requests.
1739 if (tp
->t_tfo_flags
& TFO_F_SYN_LOSS
) {
1740 tp
->t_tfo_stats
|= TFO_S_SYN_LOSS
;
1741 tcpstat
.tcps_tfo_syn_loss
++;
1743 tcp_heuristic_tfo_loss(tp
);
1745 if (tp
->t_tfo_flags
& TFO_F_COOKIE_REQ
) {
1746 tp
->t_tfo_stats
|= TFO_S_NO_COOKIE_RCV
;
1747 tcpstat
.tcps_tfo_no_cookie_rcv
++;
1750 tcp_heuristic_tfo_success(tp
);
1756 tcp_tfo_rcv_probe(struct tcpcb
*tp
, int tlen
)
1761 tp
->t_tfo_probe_state
= TFO_PROBE_PROBING
;
1764 * We send the probe out rather quickly (after one RTO). It does not
1765 * really hurt that much, it's only one additional segment on the wire.
1767 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, (TCP_REXMTVAL(tp
)));
1771 tcp_tfo_rcv_data(struct tcpcb
*tp
)
1773 /* Transition from PROBING to NONE as data has been received */
1774 if (tp
->t_tfo_probe_state
>= TFO_PROBE_PROBING
)
1775 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1779 tcp_tfo_rcv_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
1781 if (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
&&
1782 tp
->t_tfo_probes
> 0) {
1783 if (th
->th_seq
== tp
->rcv_nxt
) {
1784 /* No hole, so stop probing */
1785 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1786 } else if (SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1787 /* There is a hole! Wait a bit for data... */
1788 tp
->t_tfo_probe_state
= TFO_PROBE_WAIT_DATA
;
1789 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1796 * Update snd_wnd information.
1799 tcp_update_window(struct tcpcb
*tp
, int thflags
, struct tcphdr
* th
,
1800 u_int32_t tiwin
, int tlen
)
1802 /* Don't look at the window if there is no ACK flag */
1803 if ((thflags
& TH_ACK
) &&
1804 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
1805 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
1806 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
1807 /* keep track of pure window updates */
1809 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
1810 tcpstat
.tcps_rcvwinupd
++;
1811 tp
->snd_wnd
= tiwin
;
1812 tp
->snd_wl1
= th
->th_seq
;
1813 tp
->snd_wl2
= th
->th_ack
;
1814 if (tp
->snd_wnd
> tp
->max_sndwnd
)
1815 tp
->max_sndwnd
= tp
->snd_wnd
;
1817 if (tp
->t_inpcb
->inp_socket
->so_flags
& SOF_MP_SUBFLOW
)
1818 mptcp_update_window_fallback(tp
);
1825 tcp_input(struct mbuf
*m
, int off0
)
1828 struct ip
*ip
= NULL
;
1830 u_char
*optp
= NULL
;
1834 struct tcpcb
*tp
= 0;
1836 struct socket
*so
= 0;
1837 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
1838 struct in_addr laddr
;
1840 struct in6_addr laddr6
;
1843 int iss
= 0, nosock
= 0;
1844 u_int32_t tiwin
, sack_bytes_acked
= 0;
1845 struct tcpopt to
; /* options in this segment */
1850 struct sockaddr_in
*next_hop
= NULL
;
1851 struct m_tag
*fwd_tag
;
1852 #endif /* IPFIREWALL */
1853 u_char ip_ecn
= IPTOS_ECN_NOTECT
;
1854 unsigned int ifscope
;
1855 uint8_t isconnected
, isdisconnected
;
1856 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1857 int pktf_sw_lro_pkt
= (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) ? 1 : 0;
1858 int nlropkts
= (pktf_sw_lro_pkt
== 1) ? m
->m_pkthdr
.lro_npkts
: 1;
1859 int turnoff_lro
= 0, win
;
1861 struct mptcb
*mp_tp
= NULL
;
1863 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
1864 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
1865 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
1866 boolean_t recvd_dsack
= FALSE
;
1867 struct tcp_respond_args tra
;
1869 #define TCP_INC_VAR(stat, npkts) do { \
1873 TCP_INC_VAR(tcpstat
.tcps_rcvtotal
, nlropkts
);
1875 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
1876 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1877 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1878 KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
1882 if (fwd_tag
!= NULL
) {
1883 struct ip_fwd_tag
*ipfwd_tag
=
1884 (struct ip_fwd_tag
*)(fwd_tag
+1);
1886 next_hop
= ipfwd_tag
->next_hop
;
1887 m_tag_delete(m
, fwd_tag
);
1889 #endif /* IPFIREWALL */
1892 struct ip6_hdr
*ip6
= NULL
;
1895 int rstreason
; /* For badport_bandlim accounting purposes */
1896 struct proc
*proc0
=current_proc();
1898 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
1901 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
1903 bzero((char *)&to
, sizeof(to
));
1908 * Expect 32-bit aligned data pointer on
1909 * strict-align platforms
1911 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1913 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
1914 ip6
= mtod(m
, struct ip6_hdr
*);
1915 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
1916 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
1918 if (tcp_input_checksum(AF_INET6
, m
, th
, off0
, tlen
))
1921 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1922 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
1923 th
->th_seq
, th
->th_ack
, th
->th_win
);
1925 * Be proactive about unspecified IPv6 address in source.
1926 * As we use all-zero to indicate unbounded/unconnected pcb,
1927 * unspecified IPv6 address can be used to confuse us.
1929 * Note that packets with unspecified IPv6 destination is
1930 * already dropped in ip6_input.
1932 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
1934 IF_TCP_STATINC(ifp
, unspecv6
);
1937 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1938 struct ip6_hdr
*, ip6
, struct tcpcb
*, NULL
,
1939 struct tcphdr
*, th
);
1941 ip_ecn
= (ntohl(ip6
->ip6_flow
) >> 20) & IPTOS_ECN_MASK
;
1946 * Get IP and TCP header together in first mbuf.
1947 * Note: IP leaves IP header in first mbuf.
1949 if (off0
> sizeof (struct ip
)) {
1951 off0
= sizeof(struct ip
);
1953 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
1954 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
1955 tcpstat
.tcps_rcvshort
++;
1960 /* Expect 32-bit aligned data pointer on strict-align platforms */
1961 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1963 ip
= mtod(m
, struct ip
*);
1964 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
1967 if (tcp_input_checksum(AF_INET
, m
, th
, off0
, tlen
))
1971 /* Re-initialization for later version check */
1972 ip
->ip_v
= IPVERSION
;
1974 ip_ecn
= (ip
->ip_tos
& IPTOS_ECN_MASK
);
1976 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1977 struct ip
*, ip
, struct tcpcb
*, NULL
, struct tcphdr
*, th
);
1979 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1980 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
1981 th
->th_seq
, th
->th_ack
, th
->th_win
);
1986 * Check that TCP offset makes sense,
1987 * pull out TCP options and adjust length.
1989 off
= th
->th_off
<< 2;
1990 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
1991 tcpstat
.tcps_rcvbadoff
++;
1992 IF_TCP_STATINC(ifp
, badformat
);
1995 tlen
-= off
; /* tlen is used instead of ti->ti_len */
1996 if (off
> sizeof (struct tcphdr
)) {
1999 IP6_EXTHDR_CHECK(m
, off0
, off
, return);
2000 ip6
= mtod(m
, struct ip6_hdr
*);
2001 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
2005 if (m
->m_len
< sizeof(struct ip
) + off
) {
2006 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
2007 tcpstat
.tcps_rcvshort
++;
2010 ip
= mtod(m
, struct ip
*);
2011 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
2014 optlen
= off
- sizeof (struct tcphdr
);
2015 optp
= (u_char
*)(th
+ 1);
2017 * Do quick retrieval of timestamp options ("options
2018 * prediction?"). If timestamp is the only option and it's
2019 * formatted as recommended in RFC 1323 appendix A, we
2020 * quickly get the values now and not bother calling
2021 * tcp_dooptions(), etc.
2023 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
2024 (optlen
> TCPOLEN_TSTAMP_APPA
&&
2025 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
2026 *(u_int32_t
*)(void *)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
2027 (th
->th_flags
& TH_SYN
) == 0) {
2028 to
.to_flags
|= TOF_TS
;
2029 to
.to_tsval
= ntohl(*(u_int32_t
*)(void *)(optp
+ 4));
2030 to
.to_tsecr
= ntohl(*(u_int32_t
*)(void *)(optp
+ 8));
2031 optp
= NULL
; /* we've parsed the options */
2034 thflags
= th
->th_flags
;
2038 * If the drop_synfin option is enabled, drop all packets with
2039 * both the SYN and FIN bits set. This prevents e.g. nmap from
2040 * identifying the TCP/IP stack.
2042 * This is a violation of the TCP specification.
2044 if (drop_synfin
&& (thflags
& (TH_SYN
|TH_FIN
)) == (TH_SYN
|TH_FIN
)) {
2045 IF_TCP_STATINC(ifp
, synfin
);
2051 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
2052 * until after ip6_savecontrol() is called and before other functions
2053 * which don't want those proto headers.
2054 * Because ip6_savecontrol() is going to parse the mbuf to
2055 * search for data to be passed up to user-land, it wants mbuf
2056 * parameters to be unchanged.
2058 drop_hdrlen
= off0
+ off
;
2060 /* Since this is an entry point for input processing of tcp packets, we
2061 * can update the tcp clock here.
2063 calculate_tcp_clock();
2066 * Record the interface where this segment arrived on; this does not
2067 * affect normal data output (for non-detached TCP) as it provides a
2068 * hint about which route and interface to use for sending in the
2069 * absence of a PCB, when scoped routing (and thus source interface
2070 * selection) are enabled.
2072 if ((m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) || m
->m_pkthdr
.rcvif
== NULL
)
2073 ifscope
= IFSCOPE_NONE
;
2075 ifscope
= m
->m_pkthdr
.rcvif
->if_index
;
2078 * Convert TCP protocol specific fields to host format.
2081 #if BYTE_ORDER != BIG_ENDIAN
2089 * Locate pcb for segment.
2093 isconnected
= FALSE
;
2094 isdisconnected
= FALSE
;
2096 #if IPFIREWALL_FORWARD
2097 if (next_hop
!= NULL
2099 && isipv6
== 0 /* IPv6 support is not yet */
2103 * Diverted. Pretend to be the destination.
2104 * already got one like this?
2106 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2107 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
2110 * No, then it's new. Try find the ambushing socket
2112 if (!next_hop
->sin_port
) {
2113 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
2114 th
->th_sport
, next_hop
->sin_addr
,
2115 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2117 inp
= in_pcblookup_hash(&tcbinfo
,
2118 ip
->ip_src
, th
->th_sport
,
2120 ntohs(next_hop
->sin_port
), 1,
2125 #endif /* IPFIREWALL_FORWARD */
2129 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
2130 &ip6
->ip6_dst
, th
->th_dport
, 1,
2134 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2135 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2139 * Use the interface scope information from the PCB for outbound
2140 * segments. If the PCB isn't present and if scoped routing is
2141 * enabled, tcp_respond will use the scope of the interface where
2142 * the segment arrived on.
2144 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
))
2145 ifscope
= inp
->inp_boundifp
->if_index
;
2148 * If the state is CLOSED (i.e., TCB does not exist) then
2149 * all data in the incoming segment is discarded.
2150 * If the TCB exists but is in CLOSED state, it is embryonic,
2151 * but should either do a listen or a connect soon.
2156 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
2158 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
2163 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
2164 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
2168 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
2169 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
2171 switch (log_in_vain
) {
2173 if(thflags
& TH_SYN
)
2175 "Connection attempt to TCP %s:%d from %s:%d\n",
2176 dbuf
, ntohs(th
->th_dport
),
2178 ntohs(th
->th_sport
));
2182 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
2183 dbuf
, ntohs(th
->th_dport
), sbuf
,
2184 ntohs(th
->th_sport
), thflags
);
2188 if ((thflags
& TH_SYN
) && !(thflags
& TH_ACK
) &&
2189 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
2191 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
2192 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
2194 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
2197 log_in_vain_log((LOG_INFO
,
2198 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
2199 dbuf
, ntohs(th
->th_dport
),
2201 ntohs(th
->th_sport
)));
2208 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
2210 switch (blackhole
) {
2212 if (thflags
& TH_SYN
)
2221 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2222 IF_TCP_STATINC(ifp
, noconnnolist
);
2223 goto dropwithresetnosock
;
2225 so
= inp
->inp_socket
;
2227 /* This case shouldn't happen as the socket shouldn't be null
2228 * if inp_state isn't set to INPCB_STATE_DEAD
2229 * But just in case, we pretend we didn't find the socket if we hit this case
2230 * as this isn't cause for a panic (the socket might be leaked however)...
2234 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp
);
2240 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2241 socket_unlock(so
, 1);
2242 inp
= NULL
; // pretend we didn't find it
2247 if (so
->so_state
& SS_ISCONNECTED
) {
2248 // Connected TCP sockets have a fully-bound local and remote,
2249 // so the policy check doesn't need to override addresses
2250 if (!necp_socket_is_allowed_to_send_recv(inp
, NULL
, NULL
)) {
2251 IF_TCP_STATINC(ifp
, badformat
);
2257 if (!necp_socket_is_allowed_to_send_recv_v6(inp
,
2258 th
->th_dport
, th
->th_sport
, &ip6
->ip6_dst
,
2259 &ip6
->ip6_src
, ifp
, NULL
, NULL
)) {
2260 IF_TCP_STATINC(ifp
, badformat
);
2266 if (!necp_socket_is_allowed_to_send_recv_v4(inp
,
2267 th
->th_dport
, th
->th_sport
, &ip
->ip_dst
, &ip
->ip_src
,
2269 IF_TCP_STATINC(ifp
, badformat
);
2276 tp
= intotcpcb(inp
);
2278 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2279 IF_TCP_STATINC(ifp
, noconnlist
);
2282 if (tp
->t_state
== TCPS_CLOSED
)
2285 /* Unscale the window into a 32-bit value. */
2286 if ((thflags
& TH_SYN
) == 0)
2287 tiwin
= th
->th_win
<< tp
->snd_scale
;
2292 if (mac_inpcb_check_deliver(inp
, m
, AF_INET
, SOCK_STREAM
))
2296 /* Avoid processing packets while closing a listen socket */
2297 if (tp
->t_state
== TCPS_LISTEN
&&
2298 (so
->so_options
& SO_ACCEPTCONN
) == 0)
2301 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
2303 if (so
->so_options
& SO_DEBUG
) {
2304 ostate
= tp
->t_state
;
2307 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
2311 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
2315 if (so
->so_options
& SO_ACCEPTCONN
) {
2316 struct tcpcb
*tp0
= tp
;
2319 struct sockaddr_storage from
;
2321 struct inpcb
*oinp
= sotoinpcb(so
);
2323 struct ifnet
*head_ifscope
;
2324 unsigned int head_nocell
, head_recvanyif
,
2325 head_noexpensive
, head_awdl_unrestricted
,
2326 head_intcoproc_allowed
;
2328 /* Get listener's bound-to-interface, if any */
2329 head_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2330 inp
->inp_boundifp
: NULL
;
2331 /* Get listener's no-cellular information, if any */
2332 head_nocell
= INP_NO_CELLULAR(inp
);
2333 /* Get listener's recv-any-interface, if any */
2334 head_recvanyif
= (inp
->inp_flags
& INP_RECV_ANYIF
);
2335 /* Get listener's no-expensive information, if any */
2336 head_noexpensive
= INP_NO_EXPENSIVE(inp
);
2337 head_awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
2338 head_intcoproc_allowed
= INP_INTCOPROC_ALLOWED(inp
);
2341 * If the state is LISTEN then ignore segment if it contains an RST.
2342 * If the segment contains an ACK then it is bad and send a RST.
2343 * If it does not contain a SYN then it is not interesting; drop it.
2344 * If it is from this socket, drop it, it must be forged.
2346 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
2347 IF_TCP_STATINC(ifp
, listbadsyn
);
2349 if (thflags
& TH_RST
) {
2352 if (thflags
& TH_ACK
) {
2354 tcpstat
.tcps_badsyn
++;
2355 rstreason
= BANDLIM_RST_OPENPORT
;
2359 /* We come here if there is no SYN set */
2360 tcpstat
.tcps_badsyn
++;
2363 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
2364 if (th
->th_dport
== th
->th_sport
) {
2367 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
2372 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
2376 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2377 * in_broadcast() should never return true on a received
2378 * packet with M_BCAST not set.
2380 * Packets with a multicast source address should also
2383 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
2387 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2388 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
2392 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2393 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2394 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2395 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
2401 * If deprecated address is forbidden,
2402 * we do not accept SYN to deprecated interface
2403 * address to prevent any new inbound connection from
2404 * getting established.
2405 * When we do not accept SYN, we send a TCP RST,
2406 * with deprecated source address (instead of dropping
2407 * it). We compromise it as it is much better for peer
2408 * to send a RST, and RST will be the final packet
2411 * If we do not forbid deprecated addresses, we accept
2412 * the SYN packet. RFC 4862 forbids dropping SYN in
2415 if (isipv6
&& !ip6_use_deprecated
) {
2418 if (ip6_getdstifaddr_info(m
, NULL
,
2420 if (ia6_flags
& IN6_IFF_DEPRECATED
) {
2422 rstreason
= BANDLIM_RST_OPENPORT
;
2423 IF_TCP_STATINC(ifp
, deprecate6
);
2432 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
2434 sin6
->sin6_len
= sizeof(*sin6
);
2435 sin6
->sin6_family
= AF_INET6
;
2436 sin6
->sin6_port
= th
->th_sport
;
2437 sin6
->sin6_flowinfo
= 0;
2438 sin6
->sin6_addr
= ip6
->ip6_src
;
2439 sin6
->sin6_scope_id
= 0;
2444 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
2446 sin
->sin_len
= sizeof(*sin
);
2447 sin
->sin_family
= AF_INET
;
2448 sin
->sin_port
= th
->th_sport
;
2449 sin
->sin_addr
= ip
->ip_src
;
2451 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2453 so2
= sonewconn(so
, 0, NULL
);
2456 tcpstat
.tcps_listendrop
++;
2457 if (tcp_dropdropablreq(so
)) {
2459 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2461 so2
= sonewconn(so
, 0, NULL
);
2467 /* Point "inp" and "tp" in tandem to new socket */
2468 inp
= (struct inpcb
*)so2
->so_pcb
;
2469 tp
= intotcpcb(inp
);
2472 socket_unlock(so
, 0); /* Unlock but keep a reference on listener for now */
2477 * Mark socket as temporary until we're
2478 * committed to keeping it. The code at
2479 * ``drop'' and ``dropwithreset'' check the
2480 * flag dropsocket to see if the temporary
2481 * socket created here should be discarded.
2482 * We mark the socket as discardable until
2483 * we're committed to it below in TCPS_LISTEN.
2484 * There are some error conditions in which we
2485 * have to drop the temporary socket.
2489 * Inherit INP_BOUND_IF from listener; testing if
2490 * head_ifscope is non-NULL is sufficient, since it
2491 * can only be set to a non-zero value earlier if
2492 * the listener has such a flag set.
2494 if (head_ifscope
!= NULL
) {
2495 inp
->inp_flags
|= INP_BOUND_IF
;
2496 inp
->inp_boundifp
= head_ifscope
;
2498 inp
->inp_flags
&= ~INP_BOUND_IF
;
2501 * Inherit restrictions from listener.
2504 inp_set_nocellular(inp
);
2505 if (head_noexpensive
)
2506 inp_set_noexpensive(inp
);
2507 if (head_awdl_unrestricted
)
2508 inp_set_awdl_unrestricted(inp
);
2509 if (head_intcoproc_allowed
)
2510 inp_set_intcoproc_allowed(inp
);
2512 * Inherit {IN,IN6}_RECV_ANYIF from listener.
2515 inp
->inp_flags
|= INP_RECV_ANYIF
;
2517 inp
->inp_flags
&= ~INP_RECV_ANYIF
;
2520 inp
->in6p_laddr
= ip6
->ip6_dst
;
2522 inp
->inp_vflag
&= ~INP_IPV6
;
2523 inp
->inp_vflag
|= INP_IPV4
;
2525 inp
->inp_laddr
= ip
->ip_dst
;
2529 inp
->inp_lport
= th
->th_dport
;
2530 if (in_pcbinshash(inp
, 0) != 0) {
2532 * Undo the assignments above if we failed to
2533 * put the PCB on the hash lists.
2537 inp
->in6p_laddr
= in6addr_any
;
2540 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
2542 socket_lock(oso
, 0); /* release ref on parent */
2543 socket_unlock(oso
, 1);
2549 * Inherit socket options from the listening
2551 * Note that in6p_inputopts are not (even
2552 * should not be) copied, since it stores
2553 * previously received options and is used to
2554 * detect if each new option is different than
2555 * the previous one and hence should be passed
2557 * If we copied in6p_inputopts, a user would
2558 * not be able to receive options just after
2559 * calling the accept system call.
2562 oinp
->inp_flags
& INP_CONTROLOPTS
;
2563 if (oinp
->in6p_outputopts
)
2564 inp
->in6p_outputopts
=
2565 ip6_copypktopts(oinp
->in6p_outputopts
,
2570 inp
->inp_options
= ip_srcroute();
2571 inp
->inp_ip_tos
= oinp
->inp_ip_tos
;
2573 socket_lock(oso
, 0);
2575 /* copy old policy into new socket's */
2576 if (sotoinpcb(oso
)->inp_sp
)
2579 /* Is it a security hole here to silently fail to copy the policy? */
2580 if (inp
->inp_sp
!= NULL
)
2581 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
2582 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
))
2583 printf("tcp_input: could not copy policy\n");
2586 /* inherit states from the listener */
2587 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2588 struct tcpcb
*, tp
, int32_t, TCPS_LISTEN
);
2589 tp
->t_state
= TCPS_LISTEN
;
2590 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
|TF_NODELAY
);
2591 tp
->t_flagsext
|= (tp0
->t_flagsext
& (TF_RXTFINDROP
|TF_NOTIMEWAIT
|TF_FASTOPEN
));
2592 tp
->t_keepinit
= tp0
->t_keepinit
;
2593 tp
->t_keepcnt
= tp0
->t_keepcnt
;
2594 tp
->t_keepintvl
= tp0
->t_keepintvl
;
2595 tp
->t_adaptive_wtimo
= tp0
->t_adaptive_wtimo
;
2596 tp
->t_adaptive_rtimo
= tp0
->t_adaptive_rtimo
;
2597 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
2598 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0)
2599 tp
->t_notsent_lowat
= tp0
->t_notsent_lowat
;
2600 tp
->t_inpcb
->inp_flags2
|=
2601 tp0
->t_inpcb
->inp_flags2
& INP2_KEEPALIVE_OFFLOAD
;
2603 /* now drop the reference on the listener */
2604 socket_unlock(oso
, 1);
2606 tcp_set_max_rwinscale(tp
, so
, TCP_AUTORCVBUF_MAX(ifp
));
2608 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
2611 socket_lock_assert_owned(so
);
2613 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
2615 * Evaluate the rate of arrival of packets to see if the
2616 * receiver can reduce the ack traffic. The algorithm to
2617 * stretch acks will be enabled if the connection meets
2618 * certain criteria defined in tcp_stretch_ack_enable function.
2620 if ((tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) != 0) {
2621 TCP_INC_VAR(tp
->rcv_waitforss
, nlropkts
);
2623 if (tcp_stretch_ack_enable(tp
, thflags
)) {
2624 tp
->t_flags
|= TF_STRETCHACK
;
2625 tp
->t_flagsext
&= ~(TF_RCVUNACK_WAITSS
);
2626 tp
->rcv_waitforss
= 0;
2628 tp
->t_flags
&= ~(TF_STRETCHACK
);
2630 if (TSTMP_GT(tp
->rcv_unackwin
, tcp_now
)) {
2631 tp
->rcv_by_unackwin
+= (tlen
+ off
);
2633 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
2634 tp
->rcv_by_unackwin
= tlen
+ off
;
2639 * Keep track of how many bytes were received in the LRO packet
2641 if ((pktf_sw_lro_pkt
) && (nlropkts
> 2)) {
2642 tp
->t_lropktlen
+= tlen
;
2645 * Explicit Congestion Notification - Flag that we need to send ECT if
2646 * + The IP Congestion experienced flag was set.
2647 * + Socket is in established state
2648 * + We negotiated ECN in the TCP setup
2649 * + This isn't a pure ack (tlen > 0)
2650 * + The data is in the valid window
2652 * TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
2654 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2655 TCP_ECN_ENABLED(tp
) && tlen
> 0 &&
2656 SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2657 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
2658 tp
->t_ecn_recv_ce
++;
2659 tcpstat
.tcps_ecn_recv_ce
++;
2660 INP_INC_IFNET_STAT(inp
, ecn_recv_ce
);
2661 /* Mark this connection as it received CE from network */
2662 tp
->ecn_flags
|= TE_RECV_ECN_CE
;
2663 tp
->ecn_flags
|= TE_SENDECE
;
2667 * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
2668 * bother doing extensive checks for state and whatnot.
2670 if (thflags
& TH_CWR
) {
2671 tp
->ecn_flags
&= ~TE_SENDECE
;
2672 tp
->t_ecn_recv_cwr
++;
2676 * If we received an explicit notification of congestion in
2677 * ip tos ecn bits or by the CWR bit in TCP header flags, reset
2678 * the ack-strteching state. We need to handle ECN notification if
2679 * an ECN setup SYN was sent even once.
2681 if (tp
->t_state
== TCPS_ESTABLISHED
2682 && (tp
->ecn_flags
& TE_SETUPSENT
)
2683 && (ip_ecn
== IPTOS_ECN_CE
|| (thflags
& TH_CWR
))) {
2684 tcp_reset_stretch_ack(tp
);
2685 CLEAR_IAJ_STATE(tp
);
2688 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2689 !TCP_ECN_ENABLED(tp
) && !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2690 tcpstat
.tcps_ecn_fallback_ce
++;
2691 tcp_heuristic_ecn_aggressive(tp
);
2692 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2695 if (tp
->t_state
== TCPS_ESTABLISHED
&& TCP_ECN_ENABLED(tp
) &&
2696 ip_ecn
== IPTOS_ECN_CE
&& !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2697 if (inp
->inp_stat
->rxpackets
< ECN_MIN_CE_PROBES
) {
2698 tp
->t_ecn_recv_ce_pkt
++;
2699 } else if (tp
->t_ecn_recv_ce_pkt
> ECN_MAX_CE_RATIO
) {
2700 tcpstat
.tcps_ecn_fallback_ce
++;
2701 tcp_heuristic_ecn_aggressive(tp
);
2702 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2703 INP_INC_IFNET_STAT(inp
,ecn_fallback_ce
);
2705 /* We tracked the first ECN_MIN_CE_PROBES segments, we
2706 * now know that the path is good.
2708 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2713 * Try to determine if we are receiving a packet after a long time.
2714 * Use our own approximation of idletime to roughly measure remote
2715 * end's idle time. Since slowstart is used after an idle period
2716 * we want to avoid doing LRO if the remote end is not up to date
2717 * on initial window support and starts with 1 or 2 packets as its IW.
2719 if (sw_lro
&& (tp
->t_flagsext
& TF_LRO_OFFLOADED
) &&
2720 ((tcp_now
- tp
->t_rcvtime
) >= (TCP_IDLETIMEOUT(tp
)))) {
2724 /* Update rcvtime as a new segment was received on the connection */
2725 tp
->t_rcvtime
= tcp_now
;
2728 * Segment received on connection.
2729 * Reset idle time and keep-alive timer.
2731 if (TCPS_HAVEESTABLISHED(tp
->t_state
)) {
2732 tcp_keepalive_reset(tp
);
2735 mptcp_reset_keepalive(tp
);
2739 * Process options if not in LISTEN state,
2740 * else do it below (after getting remote address).
2742 if (tp
->t_state
!= TCPS_LISTEN
&& optp
) {
2743 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
2746 if (tp
->t_state
!= TCPS_LISTEN
&& (so
->so_flags
& SOF_MP_SUBFLOW
) &&
2747 mptcp_input_preproc(tp
, m
, th
, drop_hdrlen
) != 0) {
2748 tp
->t_flags
|= TF_ACKNOW
;
2749 (void) tcp_output(tp
);
2750 tcp_check_timer_state(tp
);
2751 socket_unlock(so
, 1);
2752 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
|
2753 DBG_FUNC_END
,0,0,0,0,0);
2757 if (tp
->t_state
== TCPS_SYN_SENT
&& (thflags
& TH_SYN
)) {
2758 if (!(thflags
& TH_ACK
) ||
2759 (SEQ_GT(th
->th_ack
, tp
->iss
) &&
2760 SEQ_LEQ(th
->th_ack
, tp
->snd_max
)))
2761 tcp_finalize_options(tp
, &to
, ifscope
);
2766 * Compute inter-packet arrival jitter. According to RFC 3550,
2767 * inter-packet arrival jitter is defined as the difference in
2768 * packet spacing at the receiver compared to the sender for a
2769 * pair of packets. When two packets of maximum segment size come
2770 * one after the other with consecutive sequence numbers, we
2771 * consider them as packets sent together at the sender and use
2772 * them as a pair to compute inter-packet arrival jitter. This
2773 * metric indicates the delay induced by the network components due
2774 * to queuing in edge/access routers.
2776 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2777 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
|TH_PUSH
)) == TH_ACK
&&
2778 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2779 ((to
.to_flags
& TOF_TS
) == 0 ||
2780 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2781 th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
2782 int seg_size
= tlen
;
2783 if (tp
->iaj_pktcnt
<= IAJ_IGNORE_PKTCNT
) {
2784 TCP_INC_VAR(tp
->iaj_pktcnt
, nlropkts
);
2787 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
2788 seg_size
= m
->m_pkthdr
.lro_pktlen
;
2790 if ( tp
->iaj_size
== 0 || seg_size
> tp
->iaj_size
||
2791 (seg_size
== tp
->iaj_size
&& tp
->iaj_rcv_ts
== 0)) {
2793 * State related to inter-arrival jitter is
2794 * uninitialized or we are trying to find a good
2795 * first packet to start computing the metric
2797 update_iaj_state(tp
, seg_size
, 0);
2799 if (seg_size
== tp
->iaj_size
) {
2801 * Compute inter-arrival jitter taking
2802 * this packet as the second packet
2804 if (pktf_sw_lro_pkt
)
2805 compute_iaj(tp
, nlropkts
,
2806 m
->m_pkthdr
.lro_elapsed
);
2808 compute_iaj(tp
, 1, 0);
2810 if (seg_size
< tp
->iaj_size
) {
2812 * There is a smaller packet in the stream.
2813 * Some times the maximum size supported
2814 * on a path can change if there is a new
2815 * link with smaller MTU. The receiver will
2816 * not know about this change. If there
2817 * are too many packets smaller than
2818 * iaj_size, we try to learn the iaj_size
2821 TCP_INC_VAR(tp
->iaj_small_pkt
, nlropkts
);
2822 if (tp
->iaj_small_pkt
> RESET_IAJ_SIZE_THRESH
) {
2823 update_iaj_state(tp
, seg_size
, 1);
2825 CLEAR_IAJ_STATE(tp
);
2828 update_iaj_state(tp
, seg_size
, 0);
2832 CLEAR_IAJ_STATE(tp
);
2834 #endif /* TRAFFIC_MGT */
2837 * Header prediction: check for the two common cases
2838 * of a uni-directional data xfer. If the packet has
2839 * no control flags, is in-sequence, the window didn't
2840 * change and we're not retransmitting, it's a
2841 * candidate. If the length is zero and the ack moved
2842 * forward, we're the sender side of the xfer. Just
2843 * free the data acked & wake any higher level process
2844 * that was blocked waiting for space. If the length
2845 * is non-zero and the ack didn't move, we're the
2846 * receiver side. If we're getting packets in-order
2847 * (the reassembly queue is empty), add the data to
2848 * the socket buffer and note that we need a delayed ack.
2849 * Make sure that the hidden state-flags are also off.
2850 * Since we check for TCPS_ESTABLISHED above, it can only
2853 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2854 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
|TH_CWR
)) == TH_ACK
&&
2855 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2856 ((to
.to_flags
& TOF_TS
) == 0 ||
2857 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2858 th
->th_seq
== tp
->rcv_nxt
&&
2859 tiwin
&& tiwin
== tp
->snd_wnd
&&
2860 tp
->snd_nxt
== tp
->snd_max
) {
2863 * If last ACK falls within this segment's sequence numbers,
2864 * record the timestamp.
2865 * NOTE that the test is modified according to the latest
2866 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2868 if ((to
.to_flags
& TOF_TS
) != 0 &&
2869 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
2870 tp
->ts_recent_age
= tcp_now
;
2871 tp
->ts_recent
= to
.to_tsval
;
2875 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
2876 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
2877 tp
->snd_cwnd
>= tp
->snd_ssthresh
&&
2878 (!IN_FASTRECOVERY(tp
) &&
2879 ((!(SACK_ENABLED(tp
)) &&
2880 tp
->t_dupacks
< tp
->t_rexmtthresh
) ||
2881 (SACK_ENABLED(tp
) && to
.to_nsacks
== 0 &&
2882 TAILQ_EMPTY(&tp
->snd_holes
))))) {
2884 * this is a pure ack for outstanding data.
2886 ++tcpstat
.tcps_predack
;
2888 tcp_bad_rexmt_check(tp
, th
, &to
);
2890 /* Recalculate the RTT */
2891 tcp_compute_rtt(tp
, &to
, th
);
2893 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
2894 acked
= BYTES_ACKED(th
, tp
);
2895 tcpstat
.tcps_rcvackpack
++;
2896 tcpstat
.tcps_rcvackbyte
+= acked
;
2899 * Handle an ack that is in sequence during
2900 * congestion avoidance phase. The
2901 * calculations in this function
2902 * assume that snd_una is not updated yet.
2904 if (CC_ALGO(tp
)->congestion_avd
!= NULL
)
2905 CC_ALGO(tp
)->congestion_avd(tp
, th
);
2906 tcp_ccdbg_trace(tp
, th
, TCP_CC_INSEQ_ACK_RCVD
);
2907 sbdrop(&so
->so_snd
, acked
);
2908 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2909 VERIFY(acked
<= so
->so_msg_state
->msg_serial_bytes
);
2910 so
->so_msg_state
->msg_serial_bytes
-= acked
;
2912 tcp_sbsnd_trim(&so
->so_snd
);
2914 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
2915 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
2916 tp
->snd_recover
= th
->th_ack
- 1;
2917 tp
->snd_una
= th
->th_ack
;
2919 TCP_RESET_REXMT_STATE(tp
);
2922 * pull snd_wl2 up to prevent seq wrap relative
2925 tp
->snd_wl2
= th
->th_ack
;
2927 if (tp
->t_dupacks
> 0) {
2929 tp
->t_rexmtthresh
= tcprexmtthresh
;
2935 * If all outstanding data are acked, stop
2936 * retransmit timer, otherwise restart timer
2937 * using current (possibly backed-off) value.
2938 * If process is waiting for space,
2939 * wakeup/selwakeup/signal. If data
2940 * are ready to send, let tcp_output
2941 * decide between more output or persist.
2943 if (tp
->snd_una
== tp
->snd_max
) {
2944 tp
->t_timer
[TCPT_REXMT
] = 0;
2945 tp
->t_timer
[TCPT_PTO
] = 0;
2946 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
2947 tp
->t_timer
[TCPT_REXMT
] =
2948 OFFSET_FROM_START(tp
,
2951 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
2952 !TCP_DSACK_SEQ_IN_WINDOW(tp
,
2953 tp
->t_dsack_lastuna
, tp
->snd_una
))
2954 tcp_rxtseg_clean(tp
);
2956 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
2957 tp
->t_bwmeas
!= NULL
)
2958 tcp_bwmeas_check(tp
);
2960 sowwakeup(so
); /* has to be done with socket lock held */
2961 if (!SLIST_EMPTY(&tp
->t_notify_ack
))
2962 tcp_notify_acknowledgement(tp
, so
);
2964 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
)) {
2965 (void) tcp_output(tp
);
2968 tcp_tfo_rcv_ack(tp
, th
);
2970 tcp_check_timer_state(tp
);
2971 socket_unlock(so
, 1);
2972 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2975 } else if (th
->th_ack
== tp
->snd_una
&&
2976 LIST_EMPTY(&tp
->t_segq
) &&
2977 tlen
<= tcp_sbspace(tp
)) {
2979 * this is a pure, in-sequence data packet
2980 * with nothing on the reassembly queue and
2981 * we have enough buffer space to take it.
2985 * If this is a connection in steady state, start
2986 * coalescing packets belonging to this flow.
2989 tcp_lro_remove_state(tp
->t_inpcb
->inp_laddr
,
2990 tp
->t_inpcb
->inp_faddr
,
2991 tp
->t_inpcb
->inp_lport
,
2992 tp
->t_inpcb
->inp_fport
);
2993 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
2994 tp
->t_idleat
= tp
->rcv_nxt
;
2995 } else if (sw_lro
&& !pktf_sw_lro_pkt
&& !isipv6
&&
2996 (so
->so_flags
& SOF_USELRO
) &&
2997 !IFNET_IS_CELLULAR(m
->m_pkthdr
.rcvif
) &&
2998 (m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) &&
2999 ((th
->th_seq
- tp
->irs
) >
3000 (tp
->t_maxseg
<< lro_start
)) &&
3001 ((tp
->t_idleat
== 0) || ((th
->th_seq
-
3002 tp
->t_idleat
) > (tp
->t_maxseg
<< lro_start
)))) {
3003 tp
->t_flagsext
|= TF_LRO_OFFLOADED
;
3004 tcp_start_coalescing(ip
, th
, tlen
);
3008 /* Clean receiver SACK report if present */
3009 if (SACK_ENABLED(tp
) && tp
->rcv_numsacks
)
3010 tcp_clean_sackreport(tp
);
3011 ++tcpstat
.tcps_preddat
;
3012 tp
->rcv_nxt
+= tlen
;
3014 * Pull snd_wl1 up to prevent seq wrap relative to
3017 tp
->snd_wl1
= th
->th_seq
;
3019 * Pull rcv_up up to prevent seq wrap relative to
3022 tp
->rcv_up
= tp
->rcv_nxt
;
3023 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
3024 tcpstat
.tcps_rcvbyte
+= tlen
;
3025 if (nstat_collect
) {
3026 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
3027 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3028 rxpackets
, m
->m_pkthdr
.lro_npkts
);
3030 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3033 INP_ADD_STAT(inp
, cell
, wifi
, wired
,rxbytes
,
3035 inp_set_activity_bitmap(inp
);
3039 * Calculate the RTT on the receiver only if the
3040 * connection is in streaming mode and the last
3041 * packet was not an end-of-write
3043 if (tp
->t_flags
& TF_STREAMING_ON
)
3044 tcp_compute_rtt(tp
, &to
, th
);
3046 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
3047 TCP_AUTORCVBUF_MAX(ifp
));
3050 * Add data to socket buffer.
3052 so_recv_data_stat(so
, m
, 0);
3053 m_adj(m
, drop_hdrlen
); /* delayed header drop */
3056 * If message delivery (SOF_ENABLE_MSGS) is enabled on
3057 * this socket, deliver the packet received as an
3058 * in-order message with sequence number attached to it.
3060 if (sbappendstream_rcvdemux(so
, m
,
3061 th
->th_seq
- (tp
->irs
+ 1), 0)) {
3066 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3067 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
3068 th
->th_seq
, th
->th_ack
, th
->th_win
);
3073 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3074 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
3075 th
->th_seq
, th
->th_ack
, th
->th_win
);
3077 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3078 if (DELAY_ACK(tp
, th
)) {
3079 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3080 tp
->t_flags
|= TF_DELACK
;
3081 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3084 tp
->t_flags
|= TF_ACKNOW
;
3088 tcp_adaptive_rwtimo_check(tp
, tlen
);
3091 tcp_tfo_rcv_data(tp
);
3093 tcp_check_timer_state(tp
);
3094 socket_unlock(so
, 1);
3095 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
3101 * Calculate amount of space in receive window,
3102 * and then do TCP input processing.
3103 * Receive window is amount of space in rcv queue,
3104 * but not less than advertised window.
3106 socket_lock_assert_owned(so
);
3107 win
= tcp_sbspace(tp
);
3110 else { /* clip rcv window to 4K for modems */
3111 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
3112 win
= min(win
, slowlink_wsize
);
3114 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
3117 * Ensure that the subflow receive window isn't greater
3118 * than the connection level receive window.
3120 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
3121 (mp_tp
= tptomptp(tp
))) {
3122 mpte_lock_assert_held(mp_tp
->mpt_mpte
);
3123 if (tp
->rcv_wnd
> mp_tp
->mpt_rcvwnd
) {
3124 tp
->rcv_wnd
= imax(mp_tp
->mpt_rcvwnd
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
3125 tcpstat
.tcps_mp_reducedwin
++;
3130 switch (tp
->t_state
) {
3133 * Initialize tp->rcv_nxt, and tp->irs, select an initial
3134 * tp->iss, and send a segment:
3135 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3136 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3137 * Fill in remote peer address fields if not previously specified.
3138 * Enter SYN_RECEIVED state, and process any other fields of this
3139 * segment in this state.
3142 struct sockaddr_in
*sin
;
3144 struct sockaddr_in6
*sin6
;
3147 socket_lock_assert_owned(so
);
3150 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
3151 M_SONAME
, M_NOWAIT
);
3154 bzero(sin6
, sizeof(*sin6
));
3155 sin6
->sin6_family
= AF_INET6
;
3156 sin6
->sin6_len
= sizeof(*sin6
);
3157 sin6
->sin6_addr
= ip6
->ip6_src
;
3158 sin6
->sin6_port
= th
->th_sport
;
3159 laddr6
= inp
->in6p_laddr
;
3160 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
3161 inp
->in6p_laddr
= ip6
->ip6_dst
;
3162 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
3164 inp
->in6p_laddr
= laddr6
;
3165 FREE(sin6
, M_SONAME
);
3168 FREE(sin6
, M_SONAME
);
3172 socket_lock_assert_owned(so
);
3173 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
3177 sin
->sin_family
= AF_INET
;
3178 sin
->sin_len
= sizeof(*sin
);
3179 sin
->sin_addr
= ip
->ip_src
;
3180 sin
->sin_port
= th
->th_sport
;
3181 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
3182 laddr
= inp
->inp_laddr
;
3183 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
3184 inp
->inp_laddr
= ip
->ip_dst
;
3185 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
,
3186 IFSCOPE_NONE
, NULL
)) {
3187 inp
->inp_laddr
= laddr
;
3188 FREE(sin
, M_SONAME
);
3191 FREE(sin
, M_SONAME
);
3194 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
3195 tcp_finalize_options(tp
, &to
, ifscope
);
3197 if (tfo_enabled(tp
) && tcp_tfo_syn(tp
, &to
))
3203 tp
->iss
= tcp_new_isn(tp
);
3205 tp
->irs
= th
->th_seq
;
3206 tcp_sendseqinit(tp
);
3208 tp
->snd_recover
= tp
->snd_una
;
3210 * Initialization of the tcpcb for transaction;
3211 * set SND.WND = SEG.WND,
3212 * initialize CCsend and CCrecv.
3214 tp
->snd_wnd
= tiwin
; /* initial send-window */
3215 tp
->t_flags
|= TF_ACKNOW
;
3216 tp
->t_unacksegs
= 0;
3217 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3218 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3219 tp
->t_state
= TCPS_SYN_RECEIVED
;
3220 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3221 TCP_CONN_KEEPINIT(tp
));
3222 dropsocket
= 0; /* committed to socket */
3224 if (inp
->inp_flowhash
== 0)
3225 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
3227 /* update flowinfo - RFC 6437 */
3228 if (inp
->inp_flow
== 0 &&
3229 inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
3230 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
3232 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
3236 /* reset the incomp processing flag */
3237 so
->so_flags
&= ~(SOF_INCOMP_INPROGRESS
);
3238 tcpstat
.tcps_accepts
++;
3239 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
| TH_CWR
)) {
3241 tp
->ecn_flags
|= (TE_SETUPRECEIVED
| TE_SENDIPECT
);
3248 * If the state is SYN_RECEIVED and the seg contains an ACK,
3249 * but not for our SYN/ACK, send a RST.
3251 case TCPS_SYN_RECEIVED
:
3252 if ((thflags
& TH_ACK
) &&
3253 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
3254 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3255 rstreason
= BANDLIM_RST_OPENPORT
;
3256 IF_TCP_STATINC(ifp
, ooopacket
);
3261 * In SYN_RECEIVED state, if we recv some SYNS with
3262 * window scale and others without, window scaling should
3263 * be disabled. Otherwise the window advertised will be
3264 * lower if we assume scaling and the other end does not.
3266 if ((thflags
& TH_SYN
) &&
3267 (tp
->irs
== th
->th_seq
) &&
3268 !(to
.to_flags
& TOF_SCALE
))
3269 tp
->t_flags
&= ~TF_RCVD_SCALE
;
3273 * If the state is SYN_SENT:
3274 * if seg contains an ACK, but not for our SYN, drop the input.
3275 * if seg contains a RST, then drop the connection.
3276 * if seg does not contain SYN, then drop it.
3277 * Otherwise this is an acceptable SYN segment
3278 * initialize tp->rcv_nxt and tp->irs
3279 * if seg contains ack then advance tp->snd_una
3280 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
3281 * arrange for segment to be acked (eventually)
3282 * continue processing rest of data/controls, beginning with URG
3285 if ((thflags
& TH_ACK
) &&
3286 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
3287 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3288 rstreason
= BANDLIM_UNLIMITED
;
3289 IF_TCP_STATINC(ifp
, ooopacket
);
3292 if (thflags
& TH_RST
) {
3293 if ((thflags
& TH_ACK
) != 0) {
3294 if (tfo_enabled(tp
))
3295 tcp_heuristic_tfo_rst(tp
);
3296 if ((tp
->ecn_flags
& (TE_SETUPSENT
| TE_RCVD_SYN_RST
)) == TE_SETUPSENT
) {
3298 * On local connections, send
3299 * non-ECN syn one time before
3300 * dropping the connection
3302 if (tp
->t_flags
& TF_LOCAL
) {
3303 tp
->ecn_flags
|= TE_RCVD_SYN_RST
;
3306 tcp_heuristic_ecn_synrst(tp
);
3310 (SO_FILT_HINT_LOCKED
|
3311 SO_FILT_HINT_CONNRESET
));
3312 tp
= tcp_drop(tp
, ECONNREFUSED
);
3313 postevent(so
, 0, EV_RESET
);
3317 if ((thflags
& TH_SYN
) == 0)
3319 tp
->snd_wnd
= th
->th_win
; /* initial send window */
3321 tp
->irs
= th
->th_seq
;
3323 if (thflags
& TH_ACK
) {
3324 tcpstat
.tcps_connects
++;
3326 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
)) {
3327 /* ECN-setup SYN-ACK */
3328 tp
->ecn_flags
|= TE_SETUPRECEIVED
;
3329 if (TCP_ECN_ENABLED(tp
)) {
3330 tcp_heuristic_ecn_success(tp
);
3331 tcpstat
.tcps_ecn_client_success
++;
3334 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3335 tp
->t_rxtshift
== 0) {
3336 tcp_heuristic_ecn_success(tp
);
3337 tcpstat
.tcps_ecn_not_supported
++;
3339 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3341 tcp_heuristic_ecn_loss(tp
);
3343 /* non-ECN-setup SYN-ACK */
3344 tp
->ecn_flags
&= ~TE_SENDIPECT
;
3347 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
3348 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
3349 mac_socketpeer_label_associate_mbuf(m
, so
);
3350 /* XXXMAC: SOCK_UNLOCK(so); */
3352 /* Do window scaling on this connection? */
3353 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3354 tp
->snd_scale
= tp
->requested_s_scale
;
3355 tp
->rcv_scale
= tp
->request_r_scale
;
3358 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
<< tp
->rcv_scale
);
3359 tp
->snd_una
++; /* SYN is acked */
3360 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
3361 tp
->snd_nxt
= tp
->snd_una
;
3364 * We have sent more in the SYN than what is being
3365 * acked. (e.g., TFO)
3366 * We should restart the sending from what the receiver
3367 * has acknowledged immediately.
3369 if (SEQ_GT(tp
->snd_nxt
, th
->th_ack
)) {
3371 * rdar://problem/33214601
3372 * There is a middlebox that acks all but one
3373 * byte and still drops the data.
3375 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3376 tp
->snd_max
== th
->th_ack
+ 1 &&
3377 tp
->snd_max
> tp
->snd_una
+ 1) {
3378 tcp_heuristic_tfo_middlebox(tp
);
3380 so
->so_error
= ENODATA
;
3382 tp
->t_tfo_stats
|= TFO_S_ONE_BYTE_PROXY
;
3385 tp
->snd_max
= tp
->snd_nxt
= th
->th_ack
;
3389 * If there's data, delay ACK; if there's also a FIN
3390 * ACKNOW will be turned on later.
3392 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3393 if (DELAY_ACK(tp
, th
) && tlen
!= 0 ) {
3394 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3395 tp
->t_flags
|= TF_DELACK
;
3396 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3400 tp
->t_flags
|= TF_ACKNOW
;
3403 * Received <SYN,ACK> in SYN_SENT[*] state.
3405 * SYN_SENT --> ESTABLISHED
3406 * SYN_SENT* --> FIN_WAIT_1
3408 tp
->t_starttime
= tcp_now
;
3409 tcp_sbrcv_tstmp_check(tp
);
3410 if (tp
->t_flags
& TF_NEEDFIN
) {
3411 DTRACE_TCP4(state__change
, void, NULL
,
3412 struct inpcb
*, inp
,
3413 struct tcpcb
*, tp
, int32_t,
3415 tp
->t_state
= TCPS_FIN_WAIT_1
;
3416 tp
->t_flags
&= ~TF_NEEDFIN
;
3419 DTRACE_TCP4(state__change
, void, NULL
,
3420 struct inpcb
*, inp
, struct tcpcb
*,
3421 tp
, int32_t, TCPS_ESTABLISHED
);
3422 tp
->t_state
= TCPS_ESTABLISHED
;
3423 tp
->t_timer
[TCPT_KEEP
] =
3424 OFFSET_FROM_START(tp
,
3425 TCP_CONN_KEEPIDLE(tp
));
3427 nstat_route_connect_success(
3428 inp
->inp_route
.ro_rt
);
3430 * The SYN is acknowledged but una is not
3431 * updated yet. So pass the value of
3432 * ack to compute sndbytes correctly
3434 inp_count_sndbytes(inp
, th
->th_ack
);
3438 * Do not send the connect notification for additional
3439 * subflows until ACK for 3-way handshake arrives.
3441 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3442 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3443 isconnected
= FALSE
;
3448 if ((tp
->t_tfo_flags
& (TFO_F_COOKIE_REQ
| TFO_F_COOKIE_SENT
)) ||
3449 (tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
)) {
3450 tcp_tfo_synack(tp
, &to
);
3452 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3453 SEQ_LT(tp
->snd_una
, th
->th_ack
)) {
3454 tp
->t_tfo_stats
|= TFO_S_SYN_DATA_ACKED
;
3455 tcpstat
.tcps_tfo_syn_data_acked
++;
3457 if (so
->so_flags
& SOF_MP_SUBFLOW
)
3458 so
->so_flags1
|= SOF1_TFO_REWIND
;
3460 tcp_tfo_rcv_probe(tp
, tlen
);
3465 * Received initial SYN in SYN-SENT[*] state => simul-
3466 * taneous open. If segment contains CC option and there is
3467 * a cached CC, apply TAO test; if it succeeds, connection is
3468 * half-synchronized. Otherwise, do 3-way handshake:
3469 * SYN-SENT -> SYN-RECEIVED
3470 * SYN-SENT* -> SYN-RECEIVED*
3472 tp
->t_flags
|= TF_ACKNOW
;
3473 tp
->t_timer
[TCPT_REXMT
] = 0;
3474 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3475 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3476 tp
->t_state
= TCPS_SYN_RECEIVED
;
3479 * During simultaneous open, TFO should not be used.
3480 * So, we disable it here, to prevent that data gets
3481 * sent on the SYN/ACK.
3483 tcp_disable_tfo(tp
);
3488 * Advance th->th_seq to correspond to first data byte.
3489 * If data, trim to stay within window,
3490 * dropping FIN if necessary.
3493 if (tlen
> tp
->rcv_wnd
) {
3494 todrop
= tlen
- tp
->rcv_wnd
;
3498 tcpstat
.tcps_rcvpackafterwin
++;
3499 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3501 tp
->snd_wl1
= th
->th_seq
- 1;
3502 tp
->rcv_up
= th
->th_seq
;
3504 * Client side of transaction: already sent SYN and data.
3505 * If the remote host used T/TCP to validate the SYN,
3506 * our data will be ACK'd; if so, enter normal data segment
3507 * processing in the middle of step 5, ack processing.
3508 * Otherwise, goto step 6.
3510 if (thflags
& TH_ACK
)
3514 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
3515 * do normal processing.
3517 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
3521 case TCPS_TIME_WAIT
:
3522 break; /* continue normal processing */
3524 /* Received a SYN while connection is already established.
3525 * This is a "half open connection and other anomalies" described
3526 * in RFC793 page 34, send an ACK so the remote reset the connection
3527 * or recovers by adjusting its sequence numberering
3529 case TCPS_ESTABLISHED
:
3530 if (thflags
& TH_SYN
)
3536 * States other than LISTEN or SYN_SENT.
3537 * First check the RST flag and sequence number since reset segments
3538 * are exempt from the timestamp and connection count tests. This
3539 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
3540 * below which allowed reset segments in half the sequence space
3541 * to fall though and be processed (which gives forged reset
3542 * segments with a random sequence number a 50 percent chance of
3543 * killing a connection).
3544 * Then check timestamp, if present.
3545 * Then check the connection count, if present.
3546 * Then check that at least some bytes of segment are within
3547 * receive window. If segment begins before rcv_nxt,
3548 * drop leading data (and SYN); if nothing left, just ack.
3551 * If the RST bit is set, check the sequence number to see
3552 * if this is a valid reset segment.
3554 * In all states except SYN-SENT, all reset (RST) segments
3555 * are validated by checking their SEQ-fields. A reset is
3556 * valid if its sequence number is in the window.
3557 * Note: this does not take into account delayed ACKs, so
3558 * we should test against last_ack_sent instead of rcv_nxt.
3559 * The sequence number in the reset segment is normally an
3560 * echo of our outgoing acknowlegement numbers, but some hosts
3561 * send a reset with the sequence number at the rightmost edge
3562 * of our receive window, and we have to handle this case.
3563 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
3564 * that brute force RST attacks are possible. To combat this,
3565 * we use a much stricter check while in the ESTABLISHED state,
3566 * only accepting RSTs where the sequence number is equal to
3567 * last_ack_sent. In all other states (the states in which a
3568 * RST is more likely), the more permissive check is used.
3569 * If we have multiple segments in flight, the intial reset
3570 * segment sequence numbers will be to the left of last_ack_sent,
3571 * but they will eventually catch up.
3572 * In any case, it never made sense to trim reset segments to
3573 * fit the receive window since RFC 1122 says:
3574 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
3576 * A TCP SHOULD allow a received RST segment to include data.
3579 * It has been suggested that a RST segment could contain
3580 * ASCII text that encoded and explained the cause of the
3581 * RST. No standard has yet been established for such
3584 * If the reset segment passes the sequence number test examine
3586 * SYN_RECEIVED STATE:
3587 * If passive open, return to LISTEN state.
3588 * If active open, inform user that connection was refused.
3589 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
3590 * Inform user that connection was reset, and close tcb.
3591 * CLOSING, LAST_ACK STATES:
3594 * Drop the segment - see Stevens, vol. 2, p. 964 and
3597 * Radar 4803931: Allows for the case where we ACKed the FIN but
3598 * there is already a RST in flight from the peer.
3599 * In that case, accept the RST for non-established
3600 * state if it's one off from last_ack_sent.
3603 if (thflags
& TH_RST
) {
3604 if ((SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3605 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) ||
3606 (tp
->rcv_wnd
== 0 &&
3607 ((tp
->last_ack_sent
== th
->th_seq
) ||
3608 ((tp
->last_ack_sent
-1) == th
->th_seq
)))) {
3609 switch (tp
->t_state
) {
3611 case TCPS_SYN_RECEIVED
:
3612 IF_TCP_STATINC(ifp
, rstinsynrcv
);
3613 so
->so_error
= ECONNREFUSED
;
3616 case TCPS_ESTABLISHED
:
3617 if (tp
->last_ack_sent
!= th
->th_seq
) {
3618 tcpstat
.tcps_badrst
++;
3621 if (TCP_ECN_ENABLED(tp
) &&
3622 tp
->snd_una
== tp
->iss
+ 1 &&
3623 SEQ_GT(tp
->snd_max
, tp
->snd_una
)) {
3625 * If the first data packet on an
3626 * ECN connection, receives a RST
3627 * increment the heuristic
3629 tcp_heuristic_ecn_droprst(tp
);
3631 case TCPS_FIN_WAIT_1
:
3632 case TCPS_CLOSE_WAIT
:
3636 case TCPS_FIN_WAIT_2
:
3637 so
->so_error
= ECONNRESET
;
3639 postevent(so
, 0, EV_RESET
);
3641 (SO_FILT_HINT_LOCKED
|
3642 SO_FILT_HINT_CONNRESET
));
3644 tcpstat
.tcps_drops
++;
3653 case TCPS_TIME_WAIT
:
3661 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3662 * and it's less than ts_recent, drop it.
3664 if ((to
.to_flags
& TOF_TS
) != 0 && tp
->ts_recent
&&
3665 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
3667 /* Check to see if ts_recent is over 24 days old. */
3668 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
3670 * Invalidate ts_recent. If this segment updates
3671 * ts_recent, the age will be reset later and ts_recent
3672 * will get a valid value. If it does not, setting
3673 * ts_recent to zero will at least satisfy the
3674 * requirement that zero be placed in the timestamp
3675 * echo reply when ts_recent isn't valid. The
3676 * age isn't reset until we get a valid ts_recent
3677 * because we don't want out-of-order segments to be
3678 * dropped when ts_recent is old.
3682 tcpstat
.tcps_rcvduppack
++;
3683 tcpstat
.tcps_rcvdupbyte
+= tlen
;
3685 tcpstat
.tcps_pawsdrop
++;
3688 * PAWS-drop when ECN is being used? That indicates
3689 * that ECT-marked packets take a different path, with
3690 * different congestion-characteristics.
3692 * Only fallback when we did send less than 2GB as PAWS
3693 * really has no reason to kick in earlier.
3695 if (TCP_ECN_ENABLED(tp
) &&
3696 inp
->inp_stat
->rxbytes
< 2147483648) {
3697 INP_INC_IFNET_STAT(inp
, ecn_fallback_reorder
);
3698 tcpstat
.tcps_ecn_fallback_reorder
++;
3699 tcp_heuristic_ecn_aggressive(tp
);
3702 if (nstat_collect
) {
3703 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
,
3704 1, tlen
, NSTAT_RX_FLAG_DUPLICATE
);
3705 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3707 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3709 tp
->t_stat
.rxduplicatebytes
+= tlen
;
3710 inp_set_activity_bitmap(inp
);
3719 * In the SYN-RECEIVED state, validate that the packet belongs to
3720 * this connection before trimming the data to fit the receive
3721 * window. Check the sequence number versus IRS since we know
3722 * the sequence numbers haven't wrapped. This is a partial fix
3723 * for the "LAND" DoS attack.
3725 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
3726 rstreason
= BANDLIM_RST_OPENPORT
;
3727 IF_TCP_STATINC(ifp
, dospacket
);
3731 todrop
= tp
->rcv_nxt
- th
->th_seq
;
3733 if (thflags
& TH_SYN
) {
3743 * Following if statement from Stevens, vol. 2, p. 960.
3746 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
3748 * Any valid FIN must be to the left of the window.
3749 * At this point the FIN must be a duplicate or out
3750 * of sequence; drop it.
3755 * Send an ACK to resynchronize and drop any data.
3756 * But keep on processing for RST or ACK.
3758 tp
->t_flags
|= TF_ACKNOW
;
3760 /* This could be a keepalive */
3761 soevent(so
, SO_FILT_HINT_LOCKED
|
3762 SO_FILT_HINT_KEEPALIVE
);
3765 tcpstat
.tcps_rcvduppack
++;
3766 tcpstat
.tcps_rcvdupbyte
+= todrop
;
3768 tcpstat
.tcps_rcvpartduppack
++;
3769 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
3772 if (TCP_DSACK_ENABLED(tp
) && todrop
> 1) {
3774 * Note the duplicate data sequence space so that
3775 * it can be reported in DSACK option.
3777 tp
->t_dsack_lseq
= th
->th_seq
;
3778 tp
->t_dsack_rseq
= th
->th_seq
+ todrop
;
3779 tp
->t_flags
|= TF_ACKNOW
;
3781 if (nstat_collect
) {
3782 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
, 1,
3783 todrop
, NSTAT_RX_FLAG_DUPLICATE
);
3784 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
3785 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, todrop
);
3786 tp
->t_stat
.rxduplicatebytes
+= todrop
;
3787 inp_set_activity_bitmap(inp
);
3789 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
3790 th
->th_seq
+= todrop
;
3792 if (th
->th_urp
> todrop
)
3793 th
->th_urp
-= todrop
;
3801 * If new data are received on a connection after the user
3802 * processes are gone, then RST the other end.
3803 * Send also a RST when we received a data segment after we've
3804 * sent our FIN when the socket is defunct.
3805 * Note that an MPTCP subflow socket would have SS_NOFDREF set
3806 * by default. So, if it's an MPTCP-subflow we rather check the
3807 * MPTCP-level's socket state for SS_NOFDREF.
3810 boolean_t close_it
= FALSE
;
3812 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && (so
->so_state
& SS_NOFDREF
) &&
3813 tp
->t_state
> TCPS_CLOSE_WAIT
)
3816 if ((so
->so_flags
& SOF_MP_SUBFLOW
) && (mptetoso(tptomptp(tp
)->mpt_mpte
)->so_state
& SS_NOFDREF
) &&
3817 tp
->t_state
> TCPS_CLOSE_WAIT
)
3820 if ((so
->so_flags
& SOF_DEFUNCT
) && tp
->t_state
> TCPS_FIN_WAIT_1
)
3825 tcpstat
.tcps_rcvafterclose
++;
3826 rstreason
= BANDLIM_UNLIMITED
;
3827 IF_TCP_STATINC(ifp
, cleanup
);
3833 * If segment ends after window, drop trailing data
3834 * (and PUSH and FIN); if nothing left, just ACK.
3836 todrop
= (th
->th_seq
+tlen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
3838 tcpstat
.tcps_rcvpackafterwin
++;
3839 if (todrop
>= tlen
) {
3840 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
3842 * If a new connection request is received
3843 * while in TIME_WAIT, drop the old connection
3844 * and start over if the sequence numbers
3845 * are above the previous ones.
3847 if (thflags
& TH_SYN
&&
3848 tp
->t_state
== TCPS_TIME_WAIT
&&
3849 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
3850 iss
= tcp_new_isn(tp
);
3852 socket_unlock(so
, 1);
3856 * If window is closed can only take segments at
3857 * window edge, and have to drop data and PUSH from
3858 * incoming segments. Continue processing, but
3859 * remember to ack. Otherwise, drop segment
3862 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
3863 tp
->t_flags
|= TF_ACKNOW
;
3864 tcpstat
.tcps_rcvwinprobe
++;
3868 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3871 thflags
&= ~(TH_PUSH
|TH_FIN
);
3875 * If last ACK falls within this segment's sequence numbers,
3876 * record its timestamp.
3878 * 1) That the test incorporates suggestions from the latest
3879 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
3880 * 2) That updating only on newer timestamps interferes with
3881 * our earlier PAWS tests, so this check should be solely
3882 * predicated on the sequence space of this segment.
3883 * 3) That we modify the segment boundary check to be
3884 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
3885 * instead of RFC1323's
3886 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
3887 * This modified check allows us to overcome RFC1323's
3888 * limitations as described in Stevens TCP/IP Illustrated
3889 * Vol. 2 p.869. In such cases, we can still calculate the
3890 * RTT correctly when RCV.NXT == Last.ACK.Sent.
3892 if ((to
.to_flags
& TOF_TS
) != 0 &&
3893 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3894 SEQ_LEQ(tp
->last_ack_sent
, th
->th_seq
+ tlen
+
3895 ((thflags
& (TH_SYN
|TH_FIN
)) != 0))) {
3896 tp
->ts_recent_age
= tcp_now
;
3897 tp
->ts_recent
= to
.to_tsval
;
3901 * If a SYN is in the window, then this is an
3902 * error and we send an RST and drop the connection.
3904 if (thflags
& TH_SYN
) {
3905 tp
= tcp_drop(tp
, ECONNRESET
);
3906 rstreason
= BANDLIM_UNLIMITED
;
3907 postevent(so
, 0, EV_RESET
);
3908 IF_TCP_STATINC(ifp
, synwindow
);
3913 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
3914 * flag is on (half-synchronized state), then queue data for
3915 * later processing; else drop segment and return.
3917 if ((thflags
& TH_ACK
) == 0) {
3918 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
3919 (tp
->t_flags
& TF_NEEDSYN
)) {
3920 if ((tfo_enabled(tp
))) {
3922 * So, we received a valid segment while in
3923 * SYN-RECEIVED (TF_NEEDSYN is actually never
3924 * set, so this is dead code).
3925 * As this cannot be an RST (see that if a bit
3926 * higher), and it does not have the ACK-flag
3927 * set, we want to retransmit the SYN/ACK.
3928 * Thus, we have to reset snd_nxt to snd_una to
3929 * trigger the going back to sending of the
3930 * SYN/ACK. This is more consistent with the
3931 * behavior of tcp_output(), which expects
3932 * to send the segment that is pointed to by
3935 tp
->snd_nxt
= tp
->snd_una
;
3938 * We need to make absolutely sure that we are
3939 * going to reply upon a duplicate SYN-segment.
3941 if (th
->th_flags
& TH_SYN
)
3946 } else if (tp
->t_flags
& TF_ACKNOW
)
3956 switch (tp
->t_state
) {
3959 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
3960 * ESTABLISHED state and continue processing.
3961 * The ACK was checked above.
3963 case TCPS_SYN_RECEIVED
:
3965 tcpstat
.tcps_connects
++;
3967 /* Do window scaling? */
3968 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3969 tp
->snd_scale
= tp
->requested_s_scale
;
3970 tp
->rcv_scale
= tp
->request_r_scale
;
3971 tp
->snd_wnd
= th
->th_win
<< tp
->snd_scale
;
3972 tiwin
= tp
->snd_wnd
;
3976 * SYN-RECEIVED -> ESTABLISHED
3977 * SYN-RECEIVED* -> FIN-WAIT-1
3979 tp
->t_starttime
= tcp_now
;
3980 tcp_sbrcv_tstmp_check(tp
);
3981 if (tp
->t_flags
& TF_NEEDFIN
) {
3982 DTRACE_TCP4(state__change
, void, NULL
,
3983 struct inpcb
*, inp
,
3984 struct tcpcb
*, tp
, int32_t, TCPS_FIN_WAIT_1
);
3985 tp
->t_state
= TCPS_FIN_WAIT_1
;
3986 tp
->t_flags
&= ~TF_NEEDFIN
;
3988 DTRACE_TCP4(state__change
, void, NULL
,
3989 struct inpcb
*, inp
,
3990 struct tcpcb
*, tp
, int32_t, TCPS_ESTABLISHED
);
3991 tp
->t_state
= TCPS_ESTABLISHED
;
3992 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3993 TCP_CONN_KEEPIDLE(tp
));
3995 nstat_route_connect_success(
3996 tp
->t_inpcb
->inp_route
.ro_rt
);
3998 * The SYN is acknowledged but una is not updated
3999 * yet. So pass the value of ack to compute
4000 * sndbytes correctly
4002 inp_count_sndbytes(inp
, th
->th_ack
);
4005 * If segment contains data or ACK, will call tcp_reass()
4006 * later; if not, do so now to pass queued data to user.
4008 if (tlen
== 0 && (thflags
& TH_FIN
) == 0)
4009 (void) tcp_reass(tp
, (struct tcphdr
*)0, &tlen
,
4011 tp
->snd_wl1
= th
->th_seq
- 1;
4015 * Do not send the connect notification for additional subflows
4016 * until ACK for 3-way handshake arrives.
4018 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
4019 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
4020 isconnected
= FALSE
;
4024 if ((tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)) {
4025 /* Done this when receiving the SYN */
4026 isconnected
= FALSE
;
4028 OSDecrementAtomic(&tcp_tfo_halfcnt
);
4030 /* Panic if something has gone terribly wrong. */
4031 VERIFY(tcp_tfo_halfcnt
>= 0);
4033 tp
->t_tfo_flags
&= ~TFO_F_COOKIE_VALID
;
4037 * In case there is data in the send-queue (e.g., TFO is being
4038 * used, or connectx+data has been done), then if we would
4039 * "FALLTHROUGH", we would handle this ACK as if data has been
4040 * acknowledged. But, we have to prevent this. And this
4041 * can be prevented by increasing snd_una by 1, so that the
4042 * SYN is not considered as data (snd_una++ is actually also
4043 * done in SYN_SENT-state as part of the regular TCP stack).
4045 * In case there is data on this ack as well, the data will be
4046 * handled by the label "dodata" right after step6.
4048 if (so
->so_snd
.sb_cc
) {
4049 tp
->snd_una
++; /* SYN is acked */
4050 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
4051 tp
->snd_nxt
= tp
->snd_una
;
4054 * No duplicate-ACK handling is needed. So, we
4055 * directly advance to processing the ACK (aka,
4056 * updating the RTT estimation,...)
4058 * But, we first need to handle eventual SACKs,
4059 * because TFO will start sending data with the
4060 * SYN/ACK, so it might be that the client
4061 * includes a SACK with its ACK.
4063 if (SACK_ENABLED(tp
) &&
4064 (to
.to_nsacks
> 0 ||
4065 !TAILQ_EMPTY(&tp
->snd_holes
)))
4066 tcp_sack_doack(tp
, &to
, th
,
4075 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4076 * ACKs. If the ack is in the range
4077 * tp->snd_una < th->th_ack <= tp->snd_max
4078 * then advance tp->snd_una to th->th_ack and drop
4079 * data from the retransmission queue. If this ACK reflects
4080 * more up to date window information we update our window information.
4082 case TCPS_ESTABLISHED
:
4083 case TCPS_FIN_WAIT_1
:
4084 case TCPS_FIN_WAIT_2
:
4085 case TCPS_CLOSE_WAIT
:
4088 case TCPS_TIME_WAIT
:
4089 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
4090 tcpstat
.tcps_rcvacktoomuch
++;
4093 if (SACK_ENABLED(tp
) && to
.to_nsacks
> 0) {
4094 recvd_dsack
= tcp_sack_process_dsack(tp
, &to
, th
);
4096 * If DSACK is received and this packet has no
4097 * other SACK information, it can be dropped.
4098 * We do not want to treat it as a duplicate ack.
4101 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) &&
4102 to
.to_nsacks
== 0) {
4103 tcp_bad_rexmt_check(tp
, th
, &to
);
4108 if (SACK_ENABLED(tp
) &&
4109 (to
.to_nsacks
> 0 || !TAILQ_EMPTY(&tp
->snd_holes
)))
4110 tcp_sack_doack(tp
, &to
, th
, &sack_bytes_acked
);
4113 if (tp
->t_mpuna
&& SEQ_GEQ(th
->th_ack
, tp
->t_mpuna
)) {
4114 if (tp
->t_mpflags
& TMPF_PREESTABLISHED
) {
4115 /* MP TCP establishment succeeded */
4117 if (tp
->t_mpflags
& TMPF_JOINED_FLOW
) {
4118 if (tp
->t_mpflags
& TMPF_SENT_JOIN
) {
4120 ~TMPF_PREESTABLISHED
;
4123 so
->so_flags
|= SOF_MPTCP_TRUE
;
4124 mptcplog((LOG_DEBUG
, "MPTCP "
4125 "Sockets: %s \n",__func__
),
4129 tp
->t_timer
[TCPT_JACK_RXMT
] = 0;
4130 tp
->t_mprxtshift
= 0;
4133 isconnected
= FALSE
;
4142 tcp_tfo_rcv_ack(tp
, th
);
4145 * If we have outstanding data (other than
4146 * a window probe), this is a completely
4147 * duplicate ack and the ack is the biggest we've seen.
4149 * Need to accommodate a change in window on duplicate acks
4150 * to allow operating systems that update window during
4151 * recovery with SACK
4153 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
4154 if (tlen
== 0 && (tiwin
== tp
->snd_wnd
||
4155 (to
.to_nsacks
> 0 && sack_bytes_acked
> 0))) {
4157 * If both ends send FIN at the same time,
4158 * then the ack will be a duplicate ack
4159 * but we have to process the FIN. Check
4160 * for this condition and process the FIN
4161 * instead of the dupack
4163 if ((thflags
& TH_FIN
) &&
4164 !TCPS_HAVERCVDFIN(tp
->t_state
))
4169 * MPTCP options that are ignored must
4170 * not be treated as duplicate ACKs.
4172 if (to
.to_flags
& TOF_MPTCP
) {
4176 if ((isconnected
) && (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
4177 mptcplog((LOG_DEBUG
, "MPTCP "
4178 "Sockets: bypass ack recovery\n"),
4180 MPTCP_LOGLVL_VERBOSE
);
4185 * If a duplicate acknowledgement was seen
4186 * after ECN, it indicates packet loss in
4187 * addition to ECN. Reset INRECOVERY flag
4188 * so that we can process partial acks
4191 if (tp
->ecn_flags
& TE_INRECOVERY
)
4192 tp
->ecn_flags
&= ~TE_INRECOVERY
;
4194 tcpstat
.tcps_rcvdupack
++;
4198 * Check if we need to reset the limit on
4201 if (tp
->t_early_rexmt_count
> 0 &&
4203 (tp
->t_early_rexmt_win
+
4204 TCP_EARLY_REXMT_WIN
)))
4205 tp
->t_early_rexmt_count
= 0;
4208 * Is early retransmit needed? We check for
4209 * this when the connection is waiting for
4210 * duplicate acks to enter fast recovery.
4212 if (!IN_FASTRECOVERY(tp
))
4213 tcp_early_rexmt_check(tp
, th
);
4216 * If we've seen exactly rexmt threshold
4217 * of duplicate acks, assume a packet
4218 * has been dropped and retransmit it.
4219 * Kludge snd_nxt & the congestion
4220 * window so we send only this one
4223 * We know we're losing at the current
4224 * window size so do congestion avoidance
4225 * (set ssthresh to half the current window
4226 * and pull our congestion window back to
4227 * the new ssthresh).
4229 * Dup acks mean that packets have left the
4230 * network (they're now cached at the receiver)
4231 * so bump cwnd by the amount in the receiver
4232 * to keep a constant cwnd packets in the
4235 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
4236 (th
->th_ack
!= tp
->snd_una
4237 && sack_bytes_acked
== 0)) {
4239 tp
->t_rexmtthresh
= tcprexmtthresh
;
4240 } else if (tp
->t_dupacks
> tp
->t_rexmtthresh
||
4241 IN_FASTRECOVERY(tp
)) {
4244 * If this connection was seeing packet
4245 * reordering, then recovery might be
4246 * delayed to disambiguate between
4247 * reordering and loss
4249 if (SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
) &&
4251 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) ==
4252 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
4254 * Since the SACK information is already
4255 * updated, this ACK will be dropped
4260 if (SACK_ENABLED(tp
)
4261 && IN_FASTRECOVERY(tp
)) {
4265 * Compute the amount of data in flight first.
4266 * We can inject new data into the pipe iff
4267 * we have less than 1/2 the original window's
4268 * worth of data in flight.
4270 awnd
= (tp
->snd_nxt
- tp
->snd_fack
) +
4271 tp
->sackhint
.sack_bytes_rexmit
;
4272 if (awnd
< tp
->snd_ssthresh
) {
4273 tp
->snd_cwnd
+= tp
->t_maxseg
;
4274 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
4275 tp
->snd_cwnd
= tp
->snd_ssthresh
;
4278 tp
->snd_cwnd
+= tp
->t_maxseg
;
4281 /* Process any window updates */
4282 if (tiwin
> tp
->snd_wnd
)
4283 tcp_update_window(tp
, thflags
,
4285 tcp_ccdbg_trace(tp
, th
,
4286 TCP_CC_IN_FASTRECOVERY
);
4288 (void) tcp_output(tp
);
4291 } else if (tp
->t_dupacks
== tp
->t_rexmtthresh
) {
4292 tcp_seq onxt
= tp
->snd_nxt
;
4295 * If we're doing sack, check to
4296 * see if we're already in sack
4297 * recovery. If we're not doing sack,
4298 * check to see if we're in newreno
4301 if (SACK_ENABLED(tp
)) {
4302 if (IN_FASTRECOVERY(tp
)) {
4305 } else if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
4309 if (SEQ_LEQ(th
->th_ack
,
4315 if (tp
->t_flags
& TF_SENTFIN
)
4316 tp
->snd_recover
= tp
->snd_max
- 1;
4318 tp
->snd_recover
= tp
->snd_max
;
4319 tp
->t_timer
[TCPT_PTO
] = 0;
4323 * If the connection has seen pkt
4324 * reordering, delay recovery until
4325 * it is clear that the packet
4328 if (SACK_ENABLED(tp
) &&
4330 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
4331 == TF_PKTS_REORDERED
&&
4332 !IN_FASTRECOVERY(tp
) &&
4333 tp
->t_reorderwin
> 0 &&
4334 (tp
->t_state
== TCPS_ESTABLISHED
||
4335 tp
->t_state
== TCPS_FIN_WAIT_1
)) {
4336 tp
->t_timer
[TCPT_DELAYFR
] =
4337 OFFSET_FROM_START(tp
,
4339 tp
->t_flagsext
|= TF_DELAY_RECOVERY
;
4340 tcpstat
.tcps_delay_recovery
++;
4341 tcp_ccdbg_trace(tp
, th
,
4342 TCP_CC_DELAY_FASTRECOVERY
);
4346 tcp_rexmt_save_state(tp
);
4348 * If the current tcp cc module has
4349 * defined a hook for tasks to run
4350 * before entering FR, call it
4352 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
4353 CC_ALGO(tp
)->pre_fr(tp
);
4354 ENTER_FASTRECOVERY(tp
);
4355 tp
->t_timer
[TCPT_REXMT
] = 0;
4356 if (TCP_ECN_ENABLED(tp
))
4357 tp
->ecn_flags
|= TE_SENDCWR
;
4359 if (SACK_ENABLED(tp
)) {
4360 tcpstat
.tcps_sack_recovery_episode
++;
4361 tp
->t_sack_recovery_episode
++;
4362 tp
->sack_newdata
= tp
->snd_nxt
;
4363 tp
->snd_cwnd
= tp
->t_maxseg
;
4365 ~TF_CWND_NONVALIDATED
;
4367 /* Process any window updates */
4368 if (tiwin
> tp
->snd_wnd
)
4373 tcp_ccdbg_trace(tp
, th
,
4374 TCP_CC_ENTER_FASTRECOVERY
);
4375 (void) tcp_output(tp
);
4378 tp
->snd_nxt
= th
->th_ack
;
4379 tp
->snd_cwnd
= tp
->t_maxseg
;
4381 /* Process any window updates */
4382 if (tiwin
> tp
->snd_wnd
)
4383 tcp_update_window(tp
,
4387 (void) tcp_output(tp
);
4388 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
4389 tcp_cc_adjust_nonvalidated_cwnd(tp
);
4391 tp
->snd_cwnd
= tp
->snd_ssthresh
+
4392 tp
->t_maxseg
* tp
->t_dupacks
;
4394 if (SEQ_GT(onxt
, tp
->snd_nxt
))
4397 tcp_ccdbg_trace(tp
, th
,
4398 TCP_CC_ENTER_FASTRECOVERY
);
4400 } else if (limited_txmt
&&
4401 ALLOW_LIMITED_TRANSMIT(tp
) &&
4402 (!(SACK_ENABLED(tp
)) || sack_bytes_acked
> 0) &&
4403 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) > 0) {
4404 u_int32_t incr
= (tp
->t_maxseg
* tp
->t_dupacks
);
4406 /* Use Limited Transmit algorithm on the first two
4407 * duplicate acks when there is new data to transmit
4409 tp
->snd_cwnd
+= incr
;
4410 tcpstat
.tcps_limited_txt
++;
4411 (void) tcp_output(tp
);
4413 tcp_ccdbg_trace(tp
, th
, TCP_CC_LIMITED_TRANSMIT
);
4415 /* Reset snd_cwnd back to normal */
4416 tp
->snd_cwnd
-= incr
;
4422 * If the congestion window was inflated to account
4423 * for the other side's cached packets, retract it.
4425 if (IN_FASTRECOVERY(tp
)) {
4426 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
4428 * If we received an ECE and entered
4429 * recovery, the subsequent ACKs should
4430 * not be treated as partial acks.
4432 if (tp
->ecn_flags
& TE_INRECOVERY
)
4435 if (SACK_ENABLED(tp
))
4436 tcp_sack_partialack(tp
, th
);
4438 tcp_newreno_partial_ack(tp
, th
);
4439 tcp_ccdbg_trace(tp
, th
, TCP_CC_PARTIAL_ACK
);
4441 EXIT_FASTRECOVERY(tp
);
4442 if (CC_ALGO(tp
)->post_fr
!= NULL
)
4443 CC_ALGO(tp
)->post_fr(tp
, th
);
4445 tcp_clear_pipeack_state(tp
);
4446 tcp_ccdbg_trace(tp
, th
,
4447 TCP_CC_EXIT_FASTRECOVERY
);
4449 } else if ((tp
->t_flagsext
&
4450 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
4451 == (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
4453 * If the ack acknowledges upto snd_recover or if
4454 * it acknowledges all the snd holes, exit
4455 * recovery and cancel the timer. Otherwise,
4456 * this is a partial ack. Wait for recovery timer
4457 * to enter recovery. The snd_holes have already
4460 if (SEQ_GEQ(th
->th_ack
, tp
->snd_recover
) ||
4461 TAILQ_EMPTY(&tp
->snd_holes
)) {
4462 tp
->t_timer
[TCPT_DELAYFR
] = 0;
4463 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
4464 EXIT_FASTRECOVERY(tp
);
4465 tcp_ccdbg_trace(tp
, th
,
4466 TCP_CC_EXIT_FASTRECOVERY
);
4470 * We were not in fast recovery. Reset the
4471 * duplicate ack counter.
4474 tp
->t_rexmtthresh
= tcprexmtthresh
;
4479 * If we reach this point, ACK is not a duplicate,
4480 * i.e., it ACKs something we sent.
4482 if (tp
->t_flags
& TF_NEEDSYN
) {
4484 * T/TCP: Connection was half-synchronized, and our
4485 * SYN has been ACK'd (so connection is now fully
4486 * synchronized). Go to non-starred state,
4487 * increment snd_una for ACK of SYN, and check if
4488 * we can do window scaling.
4490 tp
->t_flags
&= ~TF_NEEDSYN
;
4492 /* Do window scaling? */
4493 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
4494 tp
->snd_scale
= tp
->requested_s_scale
;
4495 tp
->rcv_scale
= tp
->request_r_scale
;
4500 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
4501 acked
= BYTES_ACKED(th
, tp
);
4502 tcpstat
.tcps_rcvackpack
++;
4503 tcpstat
.tcps_rcvackbyte
+= acked
;
4506 * If the last packet was a retransmit, make sure
4507 * it was not spurious.
4509 * This will also take care of congestion window
4510 * adjustment if a last packet was recovered due to a
4513 tcp_bad_rexmt_check(tp
, th
, &to
);
4515 /* Recalculate the RTT */
4516 tcp_compute_rtt(tp
, &to
, th
);
4519 * If all outstanding data is acked, stop retransmit
4520 * timer and remember to restart (more output or persist).
4521 * If there is more data to be acked, restart retransmit
4522 * timer, using current (possibly backed-off) value.
4524 TCP_RESET_REXMT_STATE(tp
);
4525 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
4526 tp
->t_rttmin
, TCPTV_REXMTMAX
,
4527 TCP_ADD_REXMTSLOP(tp
));
4528 if (th
->th_ack
== tp
->snd_max
) {
4529 tp
->t_timer
[TCPT_REXMT
] = 0;
4530 tp
->t_timer
[TCPT_PTO
] = 0;
4532 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
4533 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
4537 * If no data (only SYN) was ACK'd, skip rest of ACK
4544 * When outgoing data has been acked (except the SYN+data), we
4545 * mark this connection as "sending good" for TFO.
4547 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
4548 !(tp
->t_tfo_flags
& TFO_F_NO_SNDPROBING
) &&
4549 !(th
->th_flags
& TH_SYN
))
4550 tp
->t_tfo_flags
|= TFO_F_NO_SNDPROBING
;
4553 * If TH_ECE is received, make sure that ECN is enabled
4554 * on that connection and we have sent ECT on data packets.
4556 if ((thflags
& TH_ECE
) != 0 && TCP_ECN_ENABLED(tp
) &&
4557 (tp
->ecn_flags
& TE_SENDIPECT
)) {
4559 * Reduce the congestion window if we haven't
4562 if (!IN_FASTRECOVERY(tp
)) {
4563 tcp_reduce_congestion_window(tp
);
4564 tp
->ecn_flags
|= (TE_INRECOVERY
|TE_SENDCWR
);
4566 * Also note that the connection received
4569 tp
->ecn_flags
|= TE_RECV_ECN_ECE
;
4570 INP_INC_IFNET_STAT(inp
, ecn_recv_ece
);
4571 tcpstat
.tcps_ecn_recv_ece
++;
4572 tcp_ccdbg_trace(tp
, th
, TCP_CC_ECN_RCVD
);
4577 * When new data is acked, open the congestion window.
4578 * The specifics of how this is achieved are up to the
4579 * congestion control algorithm in use for this connection.
4581 * The calculations in this function assume that snd_una is
4584 if (!IN_FASTRECOVERY(tp
)) {
4585 if (CC_ALGO(tp
)->ack_rcvd
!= NULL
)
4586 CC_ALGO(tp
)->ack_rcvd(tp
, th
);
4587 tcp_ccdbg_trace(tp
, th
, TCP_CC_ACK_RCVD
);
4589 if (acked
> so
->so_snd
.sb_cc
) {
4590 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
4591 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
4592 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
4593 so
->so_msg_state
->msg_serial_bytes
-=
4594 (int)so
->so_snd
.sb_cc
;
4598 sbdrop(&so
->so_snd
, acked
);
4599 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
4600 so
->so_msg_state
->msg_serial_bytes
-=
4603 tcp_sbsnd_trim(&so
->so_snd
);
4604 tp
->snd_wnd
-= acked
;
4607 /* detect una wraparound */
4608 if ( !IN_FASTRECOVERY(tp
) &&
4609 SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
4610 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
4611 tp
->snd_recover
= th
->th_ack
- 1;
4613 if (IN_FASTRECOVERY(tp
) &&
4614 SEQ_GEQ(th
->th_ack
, tp
->snd_recover
))
4615 EXIT_FASTRECOVERY(tp
);
4617 tp
->snd_una
= th
->th_ack
;
4619 if (SACK_ENABLED(tp
)) {
4620 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
))
4621 tp
->snd_recover
= tp
->snd_una
;
4623 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
4624 tp
->snd_nxt
= tp
->snd_una
;
4625 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
4626 !TCP_DSACK_SEQ_IN_WINDOW(tp
, tp
->t_dsack_lastuna
,
4628 tcp_rxtseg_clean(tp
);
4629 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
4630 tp
->t_bwmeas
!= NULL
)
4631 tcp_bwmeas_check(tp
);
4634 * sowwakeup must happen after snd_una, et al. are
4635 * updated so that the sequence numbers are in sync with
4640 if (!SLIST_EMPTY(&tp
->t_notify_ack
))
4641 tcp_notify_acknowledgement(tp
, so
);
4643 switch (tp
->t_state
) {
4646 * In FIN_WAIT_1 STATE in addition to the processing
4647 * for the ESTABLISHED state if our FIN is now acknowledged
4648 * then enter FIN_WAIT_2.
4650 case TCPS_FIN_WAIT_1
:
4651 if (ourfinisacked
) {
4653 * If we can't receive any more
4654 * data, then closing user can proceed.
4655 * Starting the TCPT_2MSL timer is contrary to the
4656 * specification, but if we don't get a FIN
4657 * we'll hang forever.
4659 if (so
->so_state
& SS_CANTRCVMORE
) {
4660 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
4661 TCP_CONN_MAXIDLE(tp
));
4662 isconnected
= FALSE
;
4663 isdisconnected
= TRUE
;
4665 DTRACE_TCP4(state__change
, void, NULL
,
4666 struct inpcb
*, inp
,
4668 int32_t, TCPS_FIN_WAIT_2
);
4669 tp
->t_state
= TCPS_FIN_WAIT_2
;
4670 /* fall through and make sure we also recognize
4671 * data ACKed with the FIN
4677 * In CLOSING STATE in addition to the processing for
4678 * the ESTABLISHED state if the ACK acknowledges our FIN
4679 * then enter the TIME-WAIT state, otherwise ignore
4683 if (ourfinisacked
) {
4684 DTRACE_TCP4(state__change
, void, NULL
,
4685 struct inpcb
*, inp
,
4687 int32_t, TCPS_TIME_WAIT
);
4688 tp
->t_state
= TCPS_TIME_WAIT
;
4689 tcp_canceltimers(tp
);
4690 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
4691 tp
->t_flags
|= TF_CLOSING
;
4693 add_to_time_wait(tp
, 2 * tcp_msl
);
4695 isconnected
= FALSE
;
4696 isdisconnected
= TRUE
;
4701 * In LAST_ACK, we may still be waiting for data to drain
4702 * and/or to be acked, as well as for the ack of our FIN.
4703 * If our FIN is now acknowledged, delete the TCB,
4704 * enter the closed state and return.
4707 if (ourfinisacked
) {
4714 * In TIME_WAIT state the only thing that should arrive
4715 * is a retransmission of the remote FIN. Acknowledge
4716 * it and restart the finack timer.
4718 case TCPS_TIME_WAIT
:
4719 add_to_time_wait(tp
, 2 * tcp_msl
);
4724 * If there is a SACK option on the ACK and we
4725 * haven't seen any duplicate acks before, count
4726 * it as a duplicate ack even if the cumulative
4727 * ack is advanced. If the receiver delayed an
4728 * ack and detected loss afterwards, then the ack
4729 * will advance cumulative ack and will also have
4730 * a SACK option. So counting it as one duplicate
4733 if (sack_ackadv
== 1 &&
4734 tp
->t_state
== TCPS_ESTABLISHED
&&
4735 SACK_ENABLED(tp
) && sack_bytes_acked
> 0 &&
4736 to
.to_nsacks
> 0 && tp
->t_dupacks
== 0 &&
4737 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) && tlen
== 0 &&
4738 !(tp
->t_flagsext
& TF_PKTS_REORDERED
)) {
4739 tcpstat
.tcps_sack_ackadv
++;
4740 goto process_dupack
;
4746 * Update window information.
4748 if (tcp_update_window(tp
, thflags
, th
, tiwin
, tlen
))
4752 * Process segments with URG.
4754 if ((thflags
& TH_URG
) && th
->th_urp
&&
4755 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4757 * This is a kludge, but if we receive and accept
4758 * random urgent pointers, we'll crash in
4759 * soreceive. It's hard to imagine someone
4760 * actually wanting to send this much urgent data.
4762 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
4763 th
->th_urp
= 0; /* XXX */
4764 thflags
&= ~TH_URG
; /* XXX */
4765 goto dodata
; /* XXX */
4768 * If this segment advances the known urgent pointer,
4769 * then mark the data stream. This should not happen
4770 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
4771 * a FIN has been received from the remote side.
4772 * In these states we ignore the URG.
4774 * According to RFC961 (Assigned Protocols),
4775 * the urgent pointer points to the last octet
4776 * of urgent data. We continue, however,
4777 * to consider it to indicate the first octet
4778 * of data past the urgent section as the original
4779 * spec states (in one of two places).
4781 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
4782 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
4783 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
4784 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
4785 if (so
->so_oobmark
== 0) {
4786 so
->so_state
|= SS_RCVATMARK
;
4787 postevent(so
, 0, EV_OOB
);
4790 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
4793 * Remove out of band data so doesn't get presented to user.
4794 * This can happen independent of advancing the URG pointer,
4795 * but if two URG's are pending at once, some out-of-band
4796 * data may creep in... ick.
4798 if (th
->th_urp
<= (u_int32_t
)tlen
4800 && (so
->so_options
& SO_OOBINLINE
) == 0
4803 tcp_pulloutofband(so
, th
, m
,
4804 drop_hdrlen
); /* hdr drop is delayed */
4807 * If no out of band data is expected,
4808 * pull receive urgent pointer along
4809 * with the receive window.
4811 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
4812 tp
->rcv_up
= tp
->rcv_nxt
;
4816 /* Set socket's connect or disconnect state correcly before doing data.
4817 * The following might unlock the socket if there is an upcall or a socket
4822 } else if (isdisconnected
) {
4823 soisdisconnected(so
);
4826 /* Let's check the state of pcb just to make sure that it did not get closed
4827 * when we unlocked above
4829 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
4830 /* Just drop the packet that we are processing and return */
4835 * Process the segment text, merging it into the TCP sequencing queue,
4836 * and arranging for acknowledgment of receipt if necessary.
4837 * This process logically involves adjusting tp->rcv_wnd as data
4838 * is presented to the user (this happens in tcp_usrreq.c,
4839 * case PRU_RCVD). If a FIN has already been received on this
4840 * connection then we just ignore the text.
4842 * If we are in SYN-received state and got a valid TFO cookie, we want
4843 * to process the data.
4845 if ((tlen
|| (thflags
& TH_FIN
)) &&
4846 TCPS_HAVERCVDFIN(tp
->t_state
) == 0 &&
4847 (TCPS_HAVEESTABLISHED(tp
->t_state
) ||
4848 (tp
->t_state
== TCPS_SYN_RECEIVED
&&
4849 (tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)))) {
4850 tcp_seq save_start
= th
->th_seq
;
4851 tcp_seq save_end
= th
->th_seq
+ tlen
;
4852 m_adj(m
, drop_hdrlen
); /* delayed header drop */
4854 * Insert segment which includes th into TCP reassembly queue
4855 * with control block tp. Set thflags to whether reassembly now
4856 * includes a segment with FIN. This handles the common case
4857 * inline (segment is the next to be received on an established
4858 * connection, and the queue is empty), avoiding linkage into
4859 * and removal from the queue and repetition of various
4861 * Set DELACK for segments received in order, but ack
4862 * immediately when segments are out of order (so
4863 * fast retransmit can work).
4865 if (th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
4866 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4868 * Calculate the RTT on the receiver only if the
4869 * connection is in streaming mode and the last
4870 * packet was not an end-of-write
4872 if (tp
->t_flags
& TF_STREAMING_ON
)
4873 tcp_compute_rtt(tp
, &to
, th
);
4875 if (DELAY_ACK(tp
, th
) &&
4876 ((tp
->t_flags
& TF_ACKNOW
) == 0) ) {
4877 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4878 tp
->t_flags
|= TF_DELACK
;
4879 tp
->t_timer
[TCPT_DELACK
] =
4880 OFFSET_FROM_START(tp
, tcp_delack
);
4884 tp
->t_flags
|= TF_ACKNOW
;
4886 tp
->rcv_nxt
+= tlen
;
4887 thflags
= th
->th_flags
& TH_FIN
;
4888 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
4889 tcpstat
.tcps_rcvbyte
+= tlen
;
4890 if (nstat_collect
) {
4891 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
4892 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4893 rxpackets
, m
->m_pkthdr
.lro_npkts
);
4895 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4898 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4900 inp_set_activity_bitmap(inp
);
4902 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
4903 TCP_AUTORCVBUF_MAX(ifp
));
4904 so_recv_data_stat(so
, m
, drop_hdrlen
);
4906 if (sbappendstream_rcvdemux(so
, m
,
4907 th
->th_seq
- (tp
->irs
+ 1), 0)) {
4911 thflags
= tcp_reass(tp
, th
, &tlen
, m
, ifp
);
4912 tp
->t_flags
|= TF_ACKNOW
;
4915 if ((tlen
> 0 || (th
->th_flags
& TH_FIN
)) && SACK_ENABLED(tp
)) {
4916 if (th
->th_flags
& TH_FIN
)
4918 tcp_update_sack_list(tp
, save_start
, save_end
);
4921 tcp_adaptive_rwtimo_check(tp
, tlen
);
4924 tcp_tfo_rcv_data(tp
);
4926 if (tp
->t_flags
& TF_DELACK
)
4930 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4931 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
4932 th
->th_seq
, th
->th_ack
, th
->th_win
);
4937 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4938 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
4939 th
->th_seq
, th
->th_ack
, th
->th_win
);
4944 if ((so
->so_flags
& SOF_MP_SUBFLOW
) && tlen
== 0 &&
4945 (m
->m_pkthdr
.pkt_flags
& PKTF_MPTCP_DFIN
)) {
4946 m_adj(m
, drop_hdrlen
); /* delayed header drop */
4947 mptcp_input(tptomptp(tp
)->mpt_mpte
, m
);
4948 tp
->t_flags
|= TF_ACKNOW
;
4956 * If FIN is received ACK the FIN and let the user know
4957 * that the connection is closing.
4959 if (thflags
& TH_FIN
) {
4960 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4962 postevent(so
, 0, EV_FIN
);
4964 * If connection is half-synchronized
4965 * (ie NEEDSYN flag on) then delay ACK,
4966 * so it may be piggybacked when SYN is sent.
4967 * Otherwise, since we received a FIN then no
4968 * more input can be expected, send ACK now.
4970 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4971 if (DELAY_ACK(tp
, th
) && (tp
->t_flags
& TF_NEEDSYN
)) {
4972 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4973 tp
->t_flags
|= TF_DELACK
;
4974 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
4977 tp
->t_flags
|= TF_ACKNOW
;
4981 switch (tp
->t_state
) {
4984 * In SYN_RECEIVED and ESTABLISHED STATES
4985 * enter the CLOSE_WAIT state.
4987 case TCPS_SYN_RECEIVED
:
4988 tp
->t_starttime
= tcp_now
;
4989 case TCPS_ESTABLISHED
:
4990 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
4991 struct tcpcb
*, tp
, int32_t, TCPS_CLOSE_WAIT
);
4992 tp
->t_state
= TCPS_CLOSE_WAIT
;
4996 * If still in FIN_WAIT_1 STATE FIN has not been acked so
4997 * enter the CLOSING state.
4999 case TCPS_FIN_WAIT_1
:
5000 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
5001 struct tcpcb
*, tp
, int32_t, TCPS_CLOSING
);
5002 tp
->t_state
= TCPS_CLOSING
;
5006 * In FIN_WAIT_2 state enter the TIME_WAIT state,
5007 * starting the time-wait timer, turning off the other
5010 case TCPS_FIN_WAIT_2
:
5011 DTRACE_TCP4(state__change
, void, NULL
,
5012 struct inpcb
*, inp
,
5014 int32_t, TCPS_TIME_WAIT
);
5015 tp
->t_state
= TCPS_TIME_WAIT
;
5016 tcp_canceltimers(tp
);
5017 tp
->t_flags
|= TF_ACKNOW
;
5018 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
5019 tp
->t_flags
|= TF_CLOSING
;
5021 add_to_time_wait(tp
, 2 * tcp_msl
);
5023 soisdisconnected(so
);
5027 * In TIME_WAIT state restart the 2 MSL time_wait timer.
5029 case TCPS_TIME_WAIT
:
5030 add_to_time_wait(tp
, 2 * tcp_msl
);
5035 if (so
->so_options
& SO_DEBUG
)
5036 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
5041 * Return any desired output.
5043 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
5044 (void) tcp_output(tp
);
5047 tcp_check_timer_state(tp
);
5050 socket_unlock(so
, 1);
5051 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
5056 * Generate an ACK dropping incoming segment if it occupies
5057 * sequence space, where the ACK reflects our state.
5059 * We can now skip the test for the RST flag since all
5060 * paths to this code happen after packets containing
5061 * RST have been dropped.
5063 * In the SYN-RECEIVED state, don't send an ACK unless the
5064 * segment we received passes the SYN-RECEIVED ACK test.
5065 * If it fails send a RST. This breaks the loop in the
5066 * "LAND" DoS attack, and also prevents an ACK storm
5067 * between two listening ports that have been sent forged
5068 * SYN segments, each with the source address of the other.
5070 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
5071 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
5072 SEQ_GT(th
->th_ack
, tp
->snd_max
)) ) {
5073 rstreason
= BANDLIM_RST_OPENPORT
;
5074 IF_TCP_STATINC(ifp
, dospacket
);
5078 if (so
->so_options
& SO_DEBUG
)
5079 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5083 tp
->t_flags
|= TF_ACKNOW
;
5084 (void) tcp_output(tp
);
5086 /* Don't need to check timer state as we should have done it during tcp_output */
5087 socket_unlock(so
, 1);
5088 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
5090 dropwithresetnosock
:
5094 * Generate a RST, dropping incoming segment.
5095 * Make ACK acceptable to originator of segment.
5096 * Don't bother to respond if destination was broadcast/multicast.
5098 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
5102 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
5103 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
5107 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
5108 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
5109 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
5110 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
5112 /* IPv6 anycast check is done at tcp6_input() */
5115 * Perform bandwidth limiting.
5118 if (badport_bandlim(rstreason
) < 0)
5123 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
5124 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5127 bzero(&tra
, sizeof(tra
));
5128 tra
.ifscope
= ifscope
;
5129 tra
.awdl_unrestricted
= 1;
5130 tra
.intcoproc_allowed
= 1;
5131 if (thflags
& TH_ACK
)
5132 /* mtod() below is safe as long as hdr dropping is delayed */
5133 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
5136 if (thflags
& TH_SYN
)
5138 /* mtod() below is safe as long as hdr dropping is delayed */
5139 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+tlen
,
5140 (tcp_seq
)0, TH_RST
|TH_ACK
, &tra
);
5142 /* destroy temporarily created socket */
5145 socket_unlock(so
, 1);
5146 } else if ((inp
!= NULL
) && (nosock
== 0)) {
5147 socket_unlock(so
, 1);
5149 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
5155 * Drop space held by incoming segment and return.
5158 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
5159 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5163 /* destroy temporarily created socket */
5166 socket_unlock(so
, 1);
5168 else if (nosock
== 0) {
5169 socket_unlock(so
, 1);
5171 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
5176 * Parse TCP options and place in tcpopt.
5179 tcp_dooptions(struct tcpcb
*tp
, u_char
*cp
, int cnt
, struct tcphdr
*th
,
5185 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
5187 if (opt
== TCPOPT_EOL
)
5189 if (opt
== TCPOPT_NOP
)
5195 if (optlen
< 2 || optlen
> cnt
)
5204 if (optlen
!= TCPOLEN_MAXSEG
)
5206 if (!(th
->th_flags
& TH_SYN
))
5208 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
5211 to
->to_flags
|= TOF_MSS
;
5215 if (optlen
!= TCPOLEN_WINDOW
)
5217 if (!(th
->th_flags
& TH_SYN
))
5219 to
->to_flags
|= TOF_SCALE
;
5220 to
->to_requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
5223 case TCPOPT_TIMESTAMP
:
5224 if (optlen
!= TCPOLEN_TIMESTAMP
)
5226 to
->to_flags
|= TOF_TS
;
5227 bcopy((char *)cp
+ 2,
5228 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
5229 NTOHL(to
->to_tsval
);
5230 bcopy((char *)cp
+ 6,
5231 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
5232 NTOHL(to
->to_tsecr
);
5233 /* Re-enable sending Timestamps if we received them */
5234 if (!(tp
->t_flags
& TF_REQ_TSTMP
) &&
5235 tcp_do_rfc1323
== 1)
5236 tp
->t_flags
|= TF_REQ_TSTMP
;
5238 case TCPOPT_SACK_PERMITTED
:
5240 optlen
!= TCPOLEN_SACK_PERMITTED
)
5242 if (th
->th_flags
& TH_SYN
)
5243 to
->to_flags
|= TOF_SACK
;
5246 if (optlen
<= 2 || (optlen
- 2) % TCPOLEN_SACK
!= 0)
5248 to
->to_nsacks
= (optlen
- 2) / TCPOLEN_SACK
;
5249 to
->to_sacks
= cp
+ 2;
5250 tcpstat
.tcps_sack_rcv_blocks
++;
5253 case TCPOPT_FASTOPEN
:
5254 if (optlen
== TCPOLEN_FASTOPEN_REQ
) {
5255 if (tp
->t_state
!= TCPS_LISTEN
)
5258 to
->to_flags
|= TOF_TFOREQ
;
5260 if (optlen
< TCPOLEN_FASTOPEN_REQ
||
5261 (optlen
- TCPOLEN_FASTOPEN_REQ
) > TFO_COOKIE_LEN_MAX
||
5262 (optlen
- TCPOLEN_FASTOPEN_REQ
) < TFO_COOKIE_LEN_MIN
)
5264 if (tp
->t_state
!= TCPS_LISTEN
&&
5265 tp
->t_state
!= TCPS_SYN_SENT
)
5268 to
->to_flags
|= TOF_TFO
;
5269 to
->to_tfo
= cp
+ 1;
5274 case TCPOPT_MULTIPATH
:
5275 tcp_do_mptcp_options(tp
, cp
, th
, to
, optlen
);
5283 tcp_finalize_options(struct tcpcb
*tp
, struct tcpopt
*to
, unsigned int ifscope
)
5285 if (to
->to_flags
& TOF_TS
) {
5286 tp
->t_flags
|= TF_RCVD_TSTMP
;
5287 tp
->ts_recent
= to
->to_tsval
;
5288 tp
->ts_recent_age
= tcp_now
;
5291 if (to
->to_flags
& TOF_MSS
)
5292 tcp_mss(tp
, to
->to_mss
, ifscope
);
5293 if (SACK_ENABLED(tp
)) {
5294 if (!(to
->to_flags
& TOF_SACK
))
5295 tp
->t_flagsext
&= ~(TF_SACK_ENABLE
);
5297 tp
->t_flags
|= TF_SACK_PERMIT
;
5299 if (to
->to_flags
& TOF_SCALE
) {
5300 tp
->t_flags
|= TF_RCVD_SCALE
;
5301 tp
->requested_s_scale
= to
->to_requested_s_scale
;
5303 /* Re-enable window scaling, if the option is received */
5304 if (tp
->request_r_scale
> 0)
5305 tp
->t_flags
|= TF_REQ_SCALE
;
5310 * Pull out of band byte out of a segment so
5311 * it doesn't appear in the user's data queue.
5312 * It is still reflected in the segment length for
5313 * sequencing purposes.
5315 * @param off delayed to be droped hdrlen
5318 tcp_pulloutofband(struct socket
*so
, struct tcphdr
*th
, struct mbuf
*m
, int off
)
5320 int cnt
= off
+ th
->th_urp
- 1;
5323 if (m
->m_len
> cnt
) {
5324 char *cp
= mtod(m
, caddr_t
) + cnt
;
5325 struct tcpcb
*tp
= sototcpcb(so
);
5328 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
5329 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
5331 if (m
->m_flags
& M_PKTHDR
)
5340 panic("tcp_pulloutofband");
5344 get_base_rtt(struct tcpcb
*tp
)
5346 struct rtentry
*rt
= tp
->t_inpcb
->inp_route
.ro_rt
;
5347 return ((rt
== NULL
) ? 0 : rt
->rtt_min
);
5350 /* Each value of RTT base represents the minimum RTT seen in a minute.
5351 * We keep upto N_RTT_BASE minutes worth of history.
5354 update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
)
5356 u_int32_t base_rtt
, i
;
5359 if ((rt
= tp
->t_inpcb
->inp_route
.ro_rt
) == NULL
)
5361 if (rt
->rtt_expire_ts
== 0) {
5363 if (rt
->rtt_expire_ts
!= 0) {
5367 rt
->rtt_expire_ts
= tcp_now
;
5369 rt
->rtt_hist
[0] = rtt
;
5377 * If the recv side is being throttled, check if the
5378 * current RTT is closer to the base RTT seen in
5379 * first (recent) two slots. If so, unthrottle the stream.
5381 if ((tp
->t_flagsext
& TF_RECV_THROTTLE
) &&
5382 (int)(tcp_now
- tp
->t_recv_throttle_ts
) >= TCP_RECV_THROTTLE_WIN
) {
5383 base_rtt
= rt
->rtt_min
;
5384 if (tp
->t_rttcur
<= (base_rtt
+ target_qdelay
)) {
5385 tp
->t_flagsext
&= ~TF_RECV_THROTTLE
;
5386 tp
->t_recv_throttle_ts
= 0;
5389 #endif /* TRAFFIC_MGT */
5390 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5391 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5393 /* check the condition again to avoid race */
5394 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5395 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5397 if (rt
->rtt_index
>= NRTT_HIST
)
5399 rt
->rtt_hist
[rt
->rtt_index
] = rtt
;
5400 rt
->rtt_expire_ts
= tcp_now
;
5402 rt
->rtt_hist
[rt
->rtt_index
] =
5403 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5405 /* forget the old value and update minimum */
5407 for (i
= 0; i
< NRTT_HIST
; ++i
) {
5408 if (rt
->rtt_hist
[i
] != 0 &&
5409 (rt
->rtt_min
== 0 ||
5410 rt
->rtt_hist
[i
] < rt
->rtt_min
))
5411 rt
->rtt_min
= rt
->rtt_hist
[i
];
5415 rt
->rtt_hist
[rt
->rtt_index
] =
5416 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5417 if (rt
->rtt_min
== 0)
5420 rt
->rtt_min
= min(rt
->rtt_min
, rtt
);
5425 * If we have a timestamp reply, update smoothed RTT. If no timestamp is
5426 * present but transmit timer is running and timed sequence number was
5427 * acked, update smoothed RTT.
5429 * If timestamps are supported, a receiver can update RTT even if
5430 * there is no outstanding data.
5432 * Some boxes send broken timestamp replies during the SYN+ACK phase,
5433 * ignore timestamps of 0or we could calculate a huge RTT and blow up
5434 * the retransmit timer.
5437 tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
, struct tcphdr
*th
)
5440 VERIFY(to
!= NULL
&& th
!= NULL
);
5441 if (tp
->t_rtttime
!= 0 && SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
5442 u_int32_t pipe_ack_val
;
5443 rtt
= tcp_now
- tp
->t_rtttime
;
5445 * Compute pipe ack -- the amount of data acknowledged
5448 if (SEQ_GT(th
->th_ack
, tp
->t_pipeack_lastuna
)) {
5449 pipe_ack_val
= th
->th_ack
- tp
->t_pipeack_lastuna
;
5450 /* Update the sample */
5451 tp
->t_pipeack_sample
[tp
->t_pipeack_ind
++] =
5453 tp
->t_pipeack_ind
%= TCP_PIPEACK_SAMPLE_COUNT
;
5455 /* Compute the max of the pipeack samples */
5456 pipe_ack_val
= tcp_get_max_pipeack(tp
);
5457 tp
->t_pipeack
= (pipe_ack_val
>
5458 TCP_CC_CWND_INIT_BYTES
) ?
5461 /* start another measurement */
5464 if (((to
->to_flags
& TOF_TS
) != 0) &&
5465 (to
->to_tsecr
!= 0) &&
5466 TSTMP_GEQ(tcp_now
, to
->to_tsecr
)) {
5467 tcp_xmit_timer(tp
, (tcp_now
- to
->to_tsecr
),
5468 to
->to_tsecr
, th
->th_ack
);
5469 } else if (rtt
> 0) {
5470 tcp_xmit_timer(tp
, rtt
, 0, th
->th_ack
);
5475 * Collect new round-trip time estimate and update averages and
5479 tcp_xmit_timer(struct tcpcb
*tp
, int rtt
,
5480 u_int32_t tsecr
, tcp_seq th_ack
)
5485 * On AWDL interface, the initial RTT measurement on SYN
5486 * can be wrong due to peer caching. Avoid the first RTT
5487 * measurement as it might skew up the RTO.
5488 * <rdar://problem/28739046>
5490 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
&&
5491 (tp
->t_inpcb
->inp_last_outifp
->if_eflags
& IFEF_AWDL
) &&
5492 th_ack
== tp
->iss
+ 1)
5495 if (tp
->t_flagsext
& TF_RECOMPUTE_RTT
) {
5496 if (SEQ_GT(th_ack
, tp
->snd_una
) &&
5497 SEQ_LEQ(th_ack
, tp
->snd_max
) &&
5499 TSTMP_GEQ(tsecr
, tp
->t_badrexmt_time
))) {
5501 * We received a new ACk after a
5502 * spurious timeout. Adapt retransmission
5503 * timer as described in rfc 4015.
5505 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
5506 tp
->t_badrexmt_time
= 0;
5507 tp
->t_srtt
= max(tp
->t_srtt_prev
, rtt
);
5508 tp
->t_srtt
= tp
->t_srtt
<< TCP_RTT_SHIFT
;
5509 tp
->t_rttvar
= max(tp
->t_rttvar_prev
, (rtt
>> 1));
5510 tp
->t_rttvar
= tp
->t_rttvar
<< TCP_RTTVAR_SHIFT
;
5512 if (tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
5513 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
5521 tcpstat
.tcps_rttupdated
++;
5526 update_base_rtt(tp
, rtt
);
5529 if (tp
->t_srtt
!= 0) {
5531 * srtt is stored as fixed point with 5 bits after the
5532 * binary point (i.e., scaled by 32). The following magic
5533 * is equivalent to the smoothing algorithm in rfc793 with
5534 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
5537 * Freebsd adjusts rtt to origin 0 by subtracting 1
5538 * from the provided rtt value. This was required because
5539 * of the way t_rtttime was initiailised to 1 before.
5540 * Since we changed t_rtttime to be based on
5541 * tcp_now, this extra adjustment is not needed.
5543 delta
= (rtt
<< TCP_DELTA_SHIFT
)
5544 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
5546 if ((tp
->t_srtt
+= delta
) <= 0)
5550 * We accumulate a smoothed rtt variance (actually, a
5551 * smoothed mean difference), then set the retransmit
5552 * timer to smoothed rtt + 4 times the smoothed variance.
5553 * rttvar is stored as fixed point with 4 bits after the
5554 * binary point (scaled by 16). The following is
5555 * equivalent to rfc793 smoothing with an alpha of .75
5556 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
5557 * rfc793's wired-in beta.
5561 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
5562 if ((tp
->t_rttvar
+= delta
) <= 0)
5564 if (tp
->t_rttbest
== 0 ||
5565 tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
5566 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
5569 * No rtt measurement yet - use the unsmoothed rtt.
5570 * Set the variance to half the rtt (so our first
5571 * retransmit happens at 3*rtt).
5573 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
5574 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
5578 nstat_route_rtt(tp
->t_inpcb
->inp_route
.ro_rt
, tp
->t_srtt
,
5582 * the retransmit should happen at rtt + 4 * rttvar.
5583 * Because of the way we do the smoothing, srtt and rttvar
5584 * will each average +1/2 tick of bias. When we compute
5585 * the retransmit timer, we want 1/2 tick of rounding and
5586 * 1 extra tick because of +-1/2 tick uncertainty in the
5587 * firing of the timer. The bias will give us exactly the
5588 * 1.5 tick we need. But, because the bias is
5589 * statistical, we have to test that we don't drop below
5590 * the minimum feasible timer (which is 2 ticks).
5592 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
5593 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
,
5594 TCP_ADD_REXMTSLOP(tp
));
5597 * We received an ack for a packet that wasn't retransmitted;
5598 * it is probably safe to discard any error indications we've
5599 * received recently. This isn't quite right, but close enough
5600 * for now (a route might have failed after we sent a segment,
5601 * and the return path might not be symmetrical).
5603 tp
->t_softerror
= 0;
5606 static inline unsigned int
5607 tcp_maxmtu(struct rtentry
*rt
)
5609 unsigned int maxmtu
;
5611 RT_LOCK_ASSERT_HELD(rt
);
5612 if (rt
->rt_rmx
.rmx_mtu
== 0)
5613 maxmtu
= rt
->rt_ifp
->if_mtu
;
5615 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, rt
->rt_ifp
->if_mtu
);
5621 static inline unsigned int
5622 tcp_maxmtu6(struct rtentry
*rt
)
5624 unsigned int maxmtu
;
5625 struct nd_ifinfo
*ndi
= NULL
;
5627 RT_LOCK_ASSERT_HELD(rt
);
5628 if ((ndi
= ND_IFINFO(rt
->rt_ifp
)) != NULL
&& !ndi
->initialized
)
5631 lck_mtx_lock(&ndi
->lock
);
5632 if (rt
->rt_rmx
.rmx_mtu
== 0)
5633 maxmtu
= IN6_LINKMTU(rt
->rt_ifp
);
5635 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, IN6_LINKMTU(rt
->rt_ifp
));
5637 lck_mtx_unlock(&ndi
->lock
);
5644 get_maxmtu(struct rtentry
*rt
)
5646 unsigned int maxmtu
= 0;
5648 RT_LOCK_ASSERT_NOTHELD(rt
);
5652 if (rt_key(rt
)->sa_family
== AF_INET6
) {
5653 maxmtu
= tcp_maxmtu6(rt
);
5655 maxmtu
= tcp_maxmtu(rt
);
5664 * Determine a reasonable value for maxseg size.
5665 * If the route is known, check route for mtu.
5666 * If none, use an mss that can be handled on the outgoing
5667 * interface without forcing IP to fragment; if bigger than
5668 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
5669 * to utilize large mbufs. If no route is found, route has no mtu,
5670 * or the destination isn't local, use a default, hopefully conservative
5671 * size (usually 512 or the default IP max size, but no more than the mtu
5672 * of the interface), as we can't discover anything about intervening
5673 * gateways or networks. We also initialize the congestion/slow start
5674 * window. While looking at the routing entry, we also initialize
5675 * other path-dependent parameters from pre-set or cached values
5676 * in the routing entry.
5678 * Also take into account the space needed for options that we
5679 * send regularly. Make maxseg shorter by that amount to assure
5680 * that we can send maxseg amount of data even when the options
5681 * are present. Store the upper limit of the length of options plus
5684 * NOTE that this routine is only called when we process an incoming
5685 * segment, for outgoing segments only tcp_mssopt is called.
5689 tcp_mss(struct tcpcb
*tp
, int offer
, unsigned int input_ifscope
)
5697 struct rmxp_tao
*taop
;
5698 int origoffer
= offer
;
5699 u_int32_t sb_max_corrected
;
5708 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
5709 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
5710 : sizeof (struct tcpiphdr
);
5712 #define min_protoh (sizeof (struct tcpiphdr))
5717 rt
= tcp_rtlookup6(inp
, input_ifscope
);
5722 rt
= tcp_rtlookup(inp
, input_ifscope
);
5724 isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
5727 tp
->t_maxopd
= tp
->t_maxseg
=
5729 isipv6
? tcp_v6mssdflt
:
5736 * Slower link window correction:
5737 * If a value is specificied for slowlink_wsize use it for
5738 * PPP links believed to be on a serial modem (speed <128Kbps).
5739 * Excludes 9600bps as it is the default value adversized
5740 * by pseudo-devices over ppp.
5742 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
5743 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
5744 tp
->t_flags
|= TF_SLOWLINK
;
5746 so
= inp
->inp_socket
;
5748 taop
= rmx_taop(rt
->rt_rmx
);
5750 * Offer == -1 means that we didn't receive SYN yet,
5751 * use cached value in that case;
5754 offer
= taop
->tao_mssopt
;
5756 * Offer == 0 means that there was no MSS on the SYN segment,
5757 * in this case we use tcp_mssdflt.
5762 isipv6
? tcp_v6mssdflt
:
5767 * Prevent DoS attack with too small MSS. Round up
5768 * to at least minmss.
5770 offer
= max(offer
, tcp_minmss
);
5772 * Sanity check: make sure that maxopd will be large
5773 * enough to allow some data on segments even is the
5774 * all the option space is used (40bytes). Otherwise
5775 * funny things may happen in tcp_output.
5777 offer
= max(offer
, 64);
5779 taop
->tao_mssopt
= offer
;
5782 * While we're here, check if there's an initial rtt
5783 * or rttvar. Convert from the route-table units
5784 * to scaled multiples of the slow timeout timer.
5786 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
) != 0) {
5787 tcp_getrt_rtt(tp
, rt
);
5789 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
5793 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5795 mss
= tcp_maxmtu(rt
);
5799 // At this point, the mss is just the MTU. Adjust if necessary.
5800 mss
= necp_socket_get_effective_mtu(inp
, mss
);
5805 if (rt
->rt_rmx
.rmx_mtu
== 0) {
5809 mss
= min(mss
, tcp_v6mssdflt
);
5813 mss
= min(mss
, tcp_mssdflt
);
5816 mss
= min(mss
, offer
);
5818 * maxopd stores the maximum length of data AND options
5819 * in a segment; maxseg is the amount of data in a normal
5820 * segment. We need to store this value (maxopd) apart
5821 * from maxseg, because now every segment carries options
5822 * and thus we normally have somewhat less data in segments.
5827 * origoffer==-1 indicates, that no segments were received yet.
5828 * In this case we just guess.
5830 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
5832 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
5833 mss
-= TCPOLEN_TSTAMP_APPA
;
5836 mss
-= mptcp_adj_mss(tp
, FALSE
);
5841 * Calculate corrected value for sb_max; ensure to upgrade the
5842 * numerator for large sb_max values else it will overflow.
5844 sb_max_corrected
= (sb_max
* (u_int64_t
)MCLBYTES
) / (MSIZE
+ MCLBYTES
);
5847 * If there's a pipesize (ie loopback), change the socket
5848 * buffer to that size only if it's bigger than the current
5849 * sockbuf size. Make the socket buffers an integral
5850 * number of mss units; if the mss is larger than
5851 * the socket buffer, decrease the mss.
5854 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
5855 if (bufsize
< so
->so_snd
.sb_hiwat
)
5857 bufsize
= so
->so_snd
.sb_hiwat
;
5861 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5862 if (bufsize
> sb_max_corrected
)
5863 bufsize
= sb_max_corrected
;
5864 (void)sbreserve(&so
->so_snd
, bufsize
);
5869 * Update MSS using recommendation from link status report. This is
5872 tcp_update_mss_locked(so
, ifp
);
5875 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
5876 if (bufsize
< so
->so_rcv
.sb_hiwat
)
5878 bufsize
= so
->so_rcv
.sb_hiwat
;
5879 if (bufsize
> mss
) {
5880 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5881 if (bufsize
> sb_max_corrected
)
5882 bufsize
= sb_max_corrected
;
5883 (void)sbreserve(&so
->so_rcv
, bufsize
);
5886 set_tcp_stream_priority(so
);
5888 if (rt
->rt_rmx
.rmx_ssthresh
) {
5890 * There's some sort of gateway or interface
5891 * buffer limit on the path. Use this to set
5892 * slow-start threshold, but set the threshold to
5893 * no less than 2*mss.
5895 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
5896 tcpstat
.tcps_usedssthresh
++;
5898 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
5902 * Set the slow-start flight size depending on whether this
5903 * is a local network or not.
5905 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
5906 CC_ALGO(tp
)->cwnd_init(tp
);
5908 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CWND_INIT
);
5910 /* Route locked during lookup above */
5915 * Determine the MSS option to send on an outgoing SYN.
5918 tcp_mssopt(struct tcpcb
*tp
)
5928 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
5929 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
5930 : sizeof (struct tcpiphdr
);
5932 #define min_protoh (sizeof (struct tcpiphdr))
5937 rt
= tcp_rtlookup6(tp
->t_inpcb
, IFSCOPE_NONE
);
5940 rt
= tcp_rtlookup(tp
->t_inpcb
, IFSCOPE_NONE
);
5944 isipv6
? tcp_v6mssdflt
:
5949 * Slower link window correction:
5950 * If a value is specificied for slowlink_wsize use it for PPP links
5951 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
5952 * it is the default value adversized by pseudo-devices over ppp.
5954 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
5955 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
5956 tp
->t_flags
|= TF_SLOWLINK
;
5960 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5962 mss
= tcp_maxmtu(rt
);
5964 /* Route locked during lookup above */
5968 // At this point, the mss is just the MTU. Adjust if necessary.
5969 mss
= necp_socket_get_effective_mtu(tp
->t_inpcb
, mss
);
5972 return (mss
- min_protoh
);
5976 * On a partial ack arrives, force the retransmission of the
5977 * next unacknowledged segment. Do not clear tp->t_dupacks.
5978 * By setting snd_nxt to th_ack, this forces retransmission timer to
5982 tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
5984 tcp_seq onxt
= tp
->snd_nxt
;
5985 u_int32_t ocwnd
= tp
->snd_cwnd
;
5986 tp
->t_timer
[TCPT_REXMT
] = 0;
5987 tp
->t_timer
[TCPT_PTO
] = 0;
5989 tp
->snd_nxt
= th
->th_ack
;
5991 * Set snd_cwnd to one segment beyond acknowledged offset
5992 * (tp->snd_una has not yet been updated when this function
5995 tp
->snd_cwnd
= tp
->t_maxseg
+ BYTES_ACKED(th
, tp
);
5996 tp
->t_flags
|= TF_ACKNOW
;
5997 (void) tcp_output(tp
);
5998 tp
->snd_cwnd
= ocwnd
;
5999 if (SEQ_GT(onxt
, tp
->snd_nxt
))
6002 * Partial window deflation. Relies on fact that tp->snd_una
6005 if (tp
->snd_cwnd
> BYTES_ACKED(th
, tp
))
6006 tp
->snd_cwnd
-= BYTES_ACKED(th
, tp
);
6009 tp
->snd_cwnd
+= tp
->t_maxseg
;
6013 * Drop a random TCP connection that hasn't been serviced yet and
6014 * is eligible for discard. There is a one in qlen chance that
6015 * we will return a null, saying that there are no dropable
6016 * requests. In this case, the protocol specific code should drop
6017 * the new request. This insures fairness.
6019 * The listening TCP socket "head" must be locked
6022 tcp_dropdropablreq(struct socket
*head
)
6024 struct socket
*so
, *sonext
;
6025 unsigned int i
, j
, qlen
;
6026 static u_int32_t rnd
= 0;
6027 static u_int64_t old_runtime
;
6028 static unsigned int cur_cnt
, old_cnt
;
6030 struct inpcb
*inp
= NULL
;
6033 if ((head
->so_options
& SO_ACCEPTCONN
) == 0)
6036 if (TAILQ_EMPTY(&head
->so_incomp
))
6039 so_acquire_accept_list(head
, NULL
);
6040 socket_unlock(head
, 0);
6043 * Check if there is any socket in the incomp queue
6044 * that is closed because of a reset from the peer and is
6045 * waiting to be garbage collected. If so, pick that as
6048 TAILQ_FOREACH_SAFE(so
, &head
->so_incomp
, so_list
, sonext
) {
6049 inp
= sotoinpcb(so
);
6050 tp
= intotcpcb(inp
);
6051 if (tp
!= NULL
&& tp
->t_state
== TCPS_CLOSED
&&
6052 so
->so_head
!= NULL
&&
6053 (so
->so_state
& (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) ==
6054 (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) {
6056 * The listen socket is already locked but we
6057 * can lock this socket here without lock ordering
6058 * issues because it is in the incomp queue and
6059 * is not visible to others.
6061 if (socket_try_lock(so
)) {
6070 so
= TAILQ_FIRST(&head
->so_incomp
);
6072 now_sec
= net_uptime();
6073 if ((i
= (now_sec
- old_runtime
)) != 0) {
6074 old_runtime
= now_sec
;
6075 old_cnt
= cur_cnt
/ i
;
6079 qlen
= head
->so_incqlen
;
6081 rnd
= RandomULong();
6083 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
6084 rnd
= (314159 * rnd
+ 66329) & 0xffff;
6085 j
= ((qlen
+ 1) * rnd
) >> 16;
6088 so
= TAILQ_NEXT(so
, so_list
);
6090 /* Find a connection that is not already closing (or being served) */
6092 inp
= (struct inpcb
*)so
->so_pcb
;
6094 sonext
= TAILQ_NEXT(so
, so_list
);
6096 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) != WNT_STOPUSING
) {
6098 * Avoid the issue of a socket being accepted
6099 * by one input thread and being dropped by
6100 * another input thread. If we can't get a hold
6101 * on this mutex, then grab the next socket in
6104 if (socket_try_lock(so
)) {
6106 if ((so
->so_usecount
== 2) &&
6107 (so
->so_state
& SS_INCOMP
) &&
6108 !(so
->so_flags
& SOF_INCOMP_INPROGRESS
)) {
6112 * don't use if being accepted or
6113 * used in any other way
6115 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6116 socket_unlock(so
, 1);
6120 * do not try to lock the inp in
6121 * in_pcb_checkstate because the lock
6122 * is already held in some other thread.
6123 * Only drop the inp_wntcnt reference.
6125 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6131 socket_lock(head
, 0);
6132 so_release_accept_list(head
);
6136 /* Makes sure socket is still in the right state to be discarded */
6138 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
6139 socket_unlock(so
, 1);
6140 socket_lock(head
, 0);
6141 so_release_accept_list(head
);
6146 if (so
->so_usecount
!= 2 || !(so
->so_state
& SS_INCOMP
)) {
6147 /* do not discard: that socket is being accepted */
6148 socket_unlock(so
, 1);
6149 socket_lock(head
, 0);
6150 so_release_accept_list(head
);
6154 socket_lock(head
, 0);
6155 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
6158 so
->so_state
&= ~SS_INCOMP
;
6159 so
->so_flags
|= SOF_OVERFLOW
;
6161 so_release_accept_list(head
);
6162 socket_unlock(head
, 0);
6164 socket_lock_assert_owned(so
);
6168 if (inp
->inp_wantcnt
> 0 && inp
->inp_wantcnt
!= WNT_STOPUSING
) {
6170 * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE
6171 * doesn't require a lock, it could have happened while
6172 * we are holding the lock. This pcb will have to
6173 * be garbage collected later.
6174 * Release the reference held for so_incomp queue
6176 VERIFY(so
->so_usecount
> 0);
6178 socket_unlock(so
, 1);
6181 * Unlock this socket and leave the reference on.
6182 * We need to acquire the pcbinfo lock in order to
6183 * fully dispose it off
6185 socket_unlock(so
, 0);
6187 lck_rw_lock_exclusive(tcbinfo
.ipi_lock
);
6190 /* Release the reference held for so_incomp queue */
6191 VERIFY(so
->so_usecount
> 0);
6194 if (so
->so_usecount
!= 1 ||
6195 (inp
->inp_wantcnt
> 0 &&
6196 inp
->inp_wantcnt
!= WNT_STOPUSING
)) {
6198 * There is an extra wantcount or usecount
6199 * that must have been added when the socket
6200 * was unlocked. This socket will have to be
6201 * garbage collected later
6203 socket_unlock(so
, 1);
6205 /* Drop the reference held for this function */
6206 VERIFY(so
->so_usecount
> 0);
6211 lck_rw_done(tcbinfo
.ipi_lock
);
6213 tcpstat
.tcps_drops
++;
6215 socket_lock(head
, 0);
6219 /* Set background congestion control on a socket */
6221 tcp_set_background_cc(struct socket
*so
)
6223 tcp_set_new_cc(so
, TCP_CC_ALGO_BACKGROUND_INDEX
);
6226 /* Set foreground congestion control on a socket */
6228 tcp_set_foreground_cc(struct socket
*so
)
6230 if (tcp_use_newreno
)
6231 tcp_set_new_cc(so
, TCP_CC_ALGO_NEWRENO_INDEX
);
6233 tcp_set_new_cc(so
, TCP_CC_ALGO_CUBIC_INDEX
);
6237 tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
)
6239 struct inpcb
*inp
= sotoinpcb(so
);
6240 struct tcpcb
*tp
= intotcpcb(inp
);
6241 u_char old_cc_index
= 0;
6242 if (tp
->tcp_cc_index
!= cc_index
) {
6244 old_cc_index
= tp
->tcp_cc_index
;
6246 if (CC_ALGO(tp
)->cleanup
!= NULL
)
6247 CC_ALGO(tp
)->cleanup(tp
);
6248 tp
->tcp_cc_index
= cc_index
;
6250 tcp_cc_allocate_state(tp
);
6252 if (CC_ALGO(tp
)->switch_to
!= NULL
)
6253 CC_ALGO(tp
)->switch_to(tp
, old_cc_index
);
6255 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CHANGE_ALGO
);
6260 tcp_set_recv_bg(struct socket
*so
)
6262 if (!IS_TCP_RECV_BG(so
))
6263 so
->so_flags1
|= SOF1_TRAFFIC_MGT_TCP_RECVBG
;
6265 /* Unset Large Receive Offload on background sockets */
6266 so_set_lro(so
, SO_TC_BK
);
6270 tcp_clear_recv_bg(struct socket
*so
)
6272 if (IS_TCP_RECV_BG(so
))
6273 so
->so_flags1
&= ~(SOF1_TRAFFIC_MGT_TCP_RECVBG
);
6276 * Set/unset use of Large Receive Offload depending on
6279 so_set_lro(so
, so
->so_traffic_class
);
6283 inp_fc_unthrottle_tcp(struct inpcb
*inp
)
6285 struct tcpcb
*tp
= inp
->inp_ppcb
;
6287 * Back off the slow-start threshold and enter
6288 * congestion avoidance phase
6290 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
6291 CC_ALGO(tp
)->pre_fr(tp
);
6293 tp
->snd_cwnd
= tp
->snd_ssthresh
;
6294 tp
->t_flagsext
&= ~TF_CWND_NONVALIDATED
;
6296 * Restart counting for ABC as we changed the
6297 * congestion window just now.
6299 tp
->t_bytes_acked
= 0;
6301 /* Reset retransmit shift as we know that the reason
6302 * for delay in sending a packet is due to flow
6303 * control on the outgoing interface. There is no need
6304 * to backoff retransmit timer.
6306 TCP_RESET_REXMT_STATE(tp
);
6309 * Start the output stream again. Since we are
6310 * not retransmitting data, do not reset the
6311 * retransmit timer or rtt calculation.
6317 tcp_getstat SYSCTL_HANDLER_ARGS
6319 #pragma unused(oidp, arg1, arg2)
6322 struct tcpstat
*stat
;
6324 #if !CONFIG_EMBEDDED
6325 proc_t caller
= PROC_NULL
;
6326 proc_t caller_parent
= PROC_NULL
;
6327 char command_name
[MAXCOMLEN
+ 1] = "";
6328 char parent_name
[MAXCOMLEN
+ 1] = "";
6329 struct tcpstat zero_stat
;
6330 if ((caller
= proc_self()) != PROC_NULL
) {
6331 /* get process name */
6332 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
6334 /* get parent process name if possible */
6335 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
6336 strlcpy(parent_name
, caller_parent
->p_comm
,
6337 sizeof(parent_name
));
6338 proc_rele(caller_parent
);
6341 if ((escape_str(command_name
, strlen(command_name
),
6342 sizeof(command_name
)) == 0) &&
6343 (escape_str(parent_name
, strlen(parent_name
),
6344 sizeof(parent_name
)) == 0)) {
6345 kern_asl_msg(LOG_DEBUG
, "messagetracer",
6347 "com.apple.message.domain",
6348 "com.apple.kernel.tcpstat", /* 1 */
6349 "com.apple.message.signature",
6351 "com.apple.message.signature2", command_name
, /* 3 */
6352 "com.apple.message.signature3", parent_name
, /* 4 */
6353 "com.apple.message.summarize", "YES", /* 5 */
6357 if (caller
!= PROC_NULL
)
6359 if (tcp_disable_access_to_stats
&&
6360 !kauth_cred_issuser(kauth_cred_get())) {
6361 bzero(&zero_stat
, sizeof(zero_stat
));
6365 #endif /* !CONFIG_EMBEDDED */
6367 if (req
->oldptr
== 0) {
6368 req
->oldlen
= (size_t)sizeof(struct tcpstat
);
6371 error
= SYSCTL_OUT(req
, stat
, MIN(sizeof (tcpstat
), req
->oldlen
));
6378 * Checksum extended TCP header and data.
6381 tcp_input_checksum(int af
, struct mbuf
*m
, struct tcphdr
*th
, int off
, int tlen
)
6383 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
6387 struct ip
*ip
= mtod(m
, struct ip
*);
6388 struct ipovly
*ipov
= (struct ipovly
*)ip
;
6390 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
6393 /* ip_stripoptions() must have been called before we get here */
6394 ASSERT((ip
->ip_hl
<< 2) == sizeof (*ip
));
6396 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6397 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6398 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6399 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6400 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6402 uint32_t sum
= m
->m_pkthdr
.csum_rx_val
;
6403 uint32_t start
= m
->m_pkthdr
.csum_rx_start
;
6404 int32_t trailer
= (m_pktlen(m
) - (off
+ tlen
));
6407 * Perform 1's complement adjustment of octets
6408 * that got included/excluded in the hardware-
6409 * calculated checksum value. Ignore cases
6410 * where the value already includes the entire
6411 * IP header span, as the sum for those octets
6412 * would already be 0 by the time we get here;
6413 * IP has already performed its header checksum
6414 * checks. If we do need to adjust, restore
6415 * the original fields in the IP header when
6416 * computing the adjustment value. Also take
6417 * care of any trailing bytes and subtract out
6418 * their partial sum.
6420 ASSERT(trailer
>= 0);
6421 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6422 ((start
!= 0 && start
!= off
) || trailer
)) {
6423 uint32_t swbytes
= (uint32_t)trailer
;
6426 ip
->ip_len
+= sizeof (*ip
);
6427 #if BYTE_ORDER != BIG_ENDIAN
6430 #endif /* BYTE_ORDER != BIG_ENDIAN */
6432 /* callee folds in sum */
6433 sum
= m_adj_sum16(m
, start
, off
,
6436 swbytes
+= (off
- start
);
6438 swbytes
+= (start
- off
);
6441 #if BYTE_ORDER != BIG_ENDIAN
6444 #endif /* BYTE_ORDER != BIG_ENDIAN */
6445 ip
->ip_len
-= sizeof (*ip
);
6449 tcp_in_cksum_stats(swbytes
);
6454 /* callee folds in sum */
6455 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
6457 sum
+ htonl(tlen
+ IPPROTO_TCP
));
6459 th
->th_sum
^= 0xffff;
6465 bcopy(ipov
->ih_x1
, b
, sizeof (ipov
->ih_x1
));
6466 bzero(ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
6467 ip_sum
= ipov
->ih_len
;
6468 ipov
->ih_len
= (u_short
)tlen
;
6469 #if BYTE_ORDER != BIG_ENDIAN
6470 HTONS(ipov
->ih_len
);
6472 len
= sizeof (struct ip
) + tlen
;
6473 th
->th_sum
= in_cksum(m
, len
);
6474 bcopy(b
, ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
6475 ipov
->ih_len
= ip_sum
;
6477 tcp_in_cksum_stats(len
);
6483 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
6485 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
6488 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6489 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6490 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6491 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6492 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6494 uint32_t sum
= m
->m_pkthdr
.csum_rx_val
;
6495 uint32_t start
= m
->m_pkthdr
.csum_rx_start
;
6496 int32_t trailer
= (m_pktlen(m
) - (off
+ tlen
));
6499 * Perform 1's complement adjustment of octets
6500 * that got included/excluded in the hardware-
6501 * calculated checksum value. Also take care
6502 * of any trailing bytes and subtract out their
6505 ASSERT(trailer
>= 0);
6506 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6507 (start
!= off
|| trailer
!= 0)) {
6508 uint16_t s
= 0, d
= 0;
6509 uint32_t swbytes
= (uint32_t)trailer
;
6511 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
6512 s
= ip6
->ip6_src
.s6_addr16
[1];
6513 ip6
->ip6_src
.s6_addr16
[1] = 0 ;
6515 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
6516 d
= ip6
->ip6_dst
.s6_addr16
[1];
6517 ip6
->ip6_dst
.s6_addr16
[1] = 0;
6520 /* callee folds in sum */
6521 sum
= m_adj_sum16(m
, start
, off
,
6524 swbytes
+= (off
- start
);
6526 swbytes
+= (start
- off
);
6528 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
))
6529 ip6
->ip6_src
.s6_addr16
[1] = s
;
6530 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
))
6531 ip6
->ip6_dst
.s6_addr16
[1] = d
;
6534 tcp_in6_cksum_stats(swbytes
);
6539 th
->th_sum
= in6_pseudo(
6540 &ip6
->ip6_src
, &ip6
->ip6_dst
,
6541 sum
+ htonl(tlen
+ IPPROTO_TCP
));
6543 th
->th_sum
^= 0xffff;
6545 tcp_in6_cksum_stats(tlen
);
6546 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, off
, tlen
);
6556 if (th
->th_sum
!= 0) {
6557 tcpstat
.tcps_rcvbadsum
++;
6558 IF_TCP_STATINC(ifp
, badformat
);
6565 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_STATS
, stats
,
6566 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, tcp_getstat
,
6567 "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
6570 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
6572 #pragma unused(arg1, arg2)
6574 int error
, val
= tcprexmtthresh
;
6576 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
6577 if (error
|| !req
->newptr
)
6581 * Constrain the number of duplicate ACKs
6582 * to consider for TCP fast retransmit
6586 if (val
< 2 || val
> 3)
6589 tcprexmtthresh
= val
;
6594 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, rexmt_thresh
, CTLTYPE_INT
| CTLFLAG_RW
|
6595 CTLFLAG_LOCKED
, &tcprexmtthresh
, 0, &sysctl_rexmtthresh
, "I",
6596 "Duplicate ACK Threshold for Fast Retransmit");