2 * Copyright (c) 2000-2015 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>
83 #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */
85 #include <machine/endian.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
90 #include <net/ntstat.h>
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */
97 #include <netinet/in_var.h>
98 #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */
99 #include <netinet/in_pcb.h>
100 #include <netinet/ip_var.h>
101 #include <mach/sdt.h>
103 #include <netinet/ip6.h>
104 #include <netinet/icmp6.h>
105 #include <netinet6/nd6.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/in6_pcb.h>
109 #include <netinet/tcp.h>
110 #include <netinet/tcp_cache.h>
111 #include <netinet/tcp_fsm.h>
112 #include <netinet/tcp_seq.h>
113 #include <netinet/tcp_timer.h>
114 #include <netinet/tcp_var.h>
115 #include <netinet/tcp_cc.h>
116 #include <dev/random/randomdev.h>
117 #include <kern/zalloc.h>
119 #include <netinet6/tcp6_var.h>
121 #include <netinet/tcpip.h>
123 #include <netinet/tcp_debug.h>
124 u_char tcp_saveipgen
[40]; /* the size must be of max ip header, now IPv6 */
125 struct tcphdr tcp_savetcp
;
126 #endif /* TCPDEBUG */
129 #include <netinet6/ipsec.h>
131 #include <netinet6/ipsec6.h>
133 #include <netkey/key.h>
136 #if CONFIG_MACF_NET || CONFIG_MACF_SOCKET
137 #include <security/mac_framework.h>
138 #endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */
140 #include <sys/kdebug.h>
141 #include <netinet/lro_ext.h>
143 #include <netinet/mptcp_var.h>
144 #include <netinet/mptcp.h>
145 #include <netinet/mptcp_opt.h>
148 #include <corecrypto/ccaes.h>
150 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
151 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
152 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
153 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
155 #define TCP_RTT_HISTORY_EXPIRE_TIME (60 * TCP_RETRANSHZ)
156 #define TCP_RECV_THROTTLE_WIN (5 * TCP_RETRANSHZ)
160 struct tcpstat tcpstat
;
162 static int log_in_vain
= 0;
163 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
,
164 CTLFLAG_RW
| CTLFLAG_LOCKED
, &log_in_vain
, 0,
165 "Log all incoming TCP connections");
167 static int blackhole
= 0;
168 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
,
169 CTLFLAG_RW
| CTLFLAG_LOCKED
, &blackhole
, 0,
170 "Do not send RST when dropping refused connections");
172 int tcp_delack_enabled
= 3;
173 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
,
174 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_delack_enabled
, 0,
175 "Delay ACK to try and piggyback it onto a data packet");
177 int tcp_lq_overflow
= 1;
178 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_lq_overflow
,
179 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_lq_overflow
, 0,
180 "Listen Queue Overflow");
183 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recvbg
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
184 &tcp_recv_bg
, 0, "Receive background");
187 static int drop_synfin
= 1;
188 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, drop_synfin
,
189 CTLFLAG_RW
| CTLFLAG_LOCKED
, &drop_synfin
, 0,
190 "Drop TCP packets with SYN+FIN set");
193 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
194 "TCP Segment Reassembly Queue");
196 static int tcp_reass_overflows
= 0;
197 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
,
198 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_reass_overflows
, 0,
199 "Global number of TCP Segment Reassembly Queue Overflows");
202 __private_extern__
int slowlink_wsize
= 8192;
203 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowlink_wsize
,
204 CTLFLAG_RW
| CTLFLAG_LOCKED
,
205 &slowlink_wsize
, 0, "Maximum advertised window size for slowlink");
207 int maxseg_unacked
= 8;
208 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, maxseg_unacked
,
209 CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxseg_unacked
, 0,
210 "Maximum number of outstanding segments left unacked");
212 int tcp_do_rfc3465
= 1;
213 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
214 &tcp_do_rfc3465
, 0, "");
216 int tcp_do_rfc3465_lim2
= 1;
217 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465_lim2
,
218 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_do_rfc3465_lim2
, 0,
219 "Appropriate bytes counting w/ L=2*SMSS");
221 int rtt_samples_per_slot
= 20;
223 int tcp_allowed_iaj
= ALLOWED_IAJ
;
224 int tcp_acc_iaj_high_thresh
= ACC_IAJ_HIGH_THRESH
;
225 u_int32_t tcp_autorcvbuf_inc_shift
= 3;
226 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recv_allowed_iaj
,
227 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_allowed_iaj
, 0,
228 "Allowed inter-packet arrival jiter");
229 #if (DEVELOPMENT || DEBUG)
230 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_high_thresh
,
231 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_acc_iaj_high_thresh
, 0,
232 "Used in calculating maximum accumulated IAJ");
234 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufincshift
,
235 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_inc_shift
, 0,
236 "Shift for increment in receive socket buffer size");
237 #endif /* (DEVELOPMENT || DEBUG) */
239 u_int32_t tcp_do_autorcvbuf
= 1;
240 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, doautorcvbuf
,
241 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_do_autorcvbuf
, 0,
242 "Enable automatic socket buffer tuning");
244 u_int32_t tcp_autorcvbuf_max
= 512 * 1024;
245 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufmax
,
246 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_max
, 0,
247 "Maximum receive socket buffer size");
250 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
251 &sw_lro
, 0, "Used to coalesce TCP packets");
254 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lrodbg
,
255 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lrodebug
, 0,
256 "Used to debug SW LRO");
259 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro_startcnt
,
260 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lro_start
, 0,
261 "Segments for starting LRO computed as power of 2");
263 extern int tcp_do_autosendbuf
;
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");
286 #endif /* (DEVELOPMENT || DEBUG) */
288 #if CONFIG_IFEF_NOWINDOWSCALE
289 int tcp_obey_ifef_nowindowscale
= 0;
290 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, obey_ifef_nowindowscale
,
291 CTLFLAG_RW
| CTLFLAG_LOCKED
,
292 &tcp_obey_ifef_nowindowscale
, 0, "");
295 extern int tcp_TCPTV_MIN
;
296 extern int tcp_acc_iaj_high
;
297 extern int tcp_acc_iaj_react_limit
;
299 int tcprexmtthresh
= 3;
302 struct timeval tcp_uptime
; /* uptime when tcp_now was last updated */
303 lck_spin_t
*tcp_uptime_lock
; /* Used to sychronize updates to tcp_now */
305 struct inpcbhead tcb
;
306 #define tcb6 tcb /* for KAME src sync over BSD*'s */
307 struct inpcbinfo tcbinfo
;
309 static void tcp_dooptions(struct tcpcb
*, u_char
*, int, struct tcphdr
*,
311 static void tcp_finalize_options(struct tcpcb
*, struct tcpopt
*, unsigned int);
312 static void tcp_pulloutofband(struct socket
*,
313 struct tcphdr
*, struct mbuf
*, int);
314 static int tcp_reass(struct tcpcb
*, struct tcphdr
*, int *, struct mbuf
*,
316 static void tcp_xmit_timer(struct tcpcb
*, int, u_int32_t
, tcp_seq
);
317 static inline unsigned int tcp_maxmtu(struct rtentry
*);
318 static inline int tcp_stretch_ack_enable(struct tcpcb
*tp
);
319 static inline void tcp_adaptive_rwtimo_check(struct tcpcb
*, int);
322 static inline void update_iaj_state(struct tcpcb
*tp
, uint32_t tlen
,
324 void compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
);
325 static void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
);
326 #endif /* TRAFFIC_MGT */
329 static inline unsigned int tcp_maxmtu6(struct rtentry
*);
332 static void tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sb
,
333 struct tcpopt
*to
, u_int32_t tlen
);
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
);
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 int tcp_rcvsspktcnt
= TCP_RCV_SS_PKTCOUNT
;
381 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rcvsspktcnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
382 &tcp_rcvsspktcnt
, 0, "packets to be seen before receiver stretches acks");
384 #define DELAY_ACK(tp, th) \
385 (CC_ALGO(tp)->delay_ack != NULL && CC_ALGO(tp)->delay_ack(tp, th))
387 static int tcp_dropdropablreq(struct socket
*head
);
388 static void tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
);
389 static void update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
);
390 void tcp_set_background_cc(struct socket
*so
);
391 void tcp_set_foreground_cc(struct socket
*so
);
392 static void tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
);
393 static void tcp_bwmeas_check(struct tcpcb
*tp
);
397 reset_acc_iaj(struct tcpcb
*tp
)
405 update_iaj_state(struct tcpcb
*tp
, uint32_t size
, int rst_size
)
409 if (tp
->iaj_size
== 0 || size
>= tp
->iaj_size
) {
411 tp
->iaj_rcv_ts
= tcp_now
;
412 tp
->iaj_small_pkt
= 0;
416 /* For every 32 bit unsigned integer(v), this function will find the
417 * largest integer n such that (n*n <= v). This takes at most 16 iterations
418 * irrespective of the value of v and does not involve multiplications.
421 isqrt(unsigned int val
) {
422 unsigned int sqrt_cache
[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
423 unsigned int temp
, g
=0, b
=0x8000, bshft
=15;
425 for (g
= 0; g
<= 10; ++g
) {
426 if (sqrt_cache
[g
] > val
) {
429 } else if (sqrt_cache
[g
] == val
) {
435 temp
= (((g
<< 1) + b
) << (bshft
--));
441 } while ( b
> 0 && val
> 0);
447 * With LRO, roughly estimate the inter arrival time between
448 * each sub coalesced packet as an average. Count the delay
449 * cur_iaj to be the delay between the last packet received
450 * and the first packet of the LRO stream. Due to round off errors
451 * cur_iaj may be the same as lro_delay_factor. Averaging has
452 * round off errors too. lro_delay_factor may be close to 0
453 * in steady state leading to lower values fed to compute_iaj_meat.
456 compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
)
458 uint32_t cur_iaj
= tcp_now
- tp
->iaj_rcv_ts
;
459 uint32_t timediff
= 0;
461 if (cur_iaj
>= lro_delay_factor
) {
462 cur_iaj
= cur_iaj
- lro_delay_factor
;
465 compute_iaj_meat(tp
, cur_iaj
);
472 timediff
= lro_delay_factor
/nlropkts
;
476 compute_iaj_meat(tp
, timediff
);
482 void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
)
484 /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds,
485 * throttle the receive window to a minimum of MIN_IAJ_WIN packets
487 #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
488 #define IAJ_DIV_SHIFT 4
489 #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1))
491 uint32_t allowed_iaj
, acc_iaj
= 0;
496 cur_iaj_dev
= (cur_iaj
- tp
->avg_iaj
);
498 /* Allow a jitter of "allowed_iaj" milliseconds. Some connections
499 * may have a constant jitter more than that. We detect this by
500 * using standard deviation.
502 allowed_iaj
= tp
->avg_iaj
+ tp
->std_dev_iaj
;
503 if (allowed_iaj
< tcp_allowed_iaj
)
504 allowed_iaj
= tcp_allowed_iaj
;
506 /* Initially when the connection starts, the senders congestion
507 * window is small. During this period we avoid throttling a
508 * connection because we do not have a good starting point for
509 * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over
510 * the first few packets.
512 if (tp
->iaj_pktcnt
> IAJ_IGNORE_PKTCNT
) {
513 if ( cur_iaj
<= allowed_iaj
) {
514 if (tp
->acc_iaj
>= 2)
515 acc_iaj
= tp
->acc_iaj
- 2;
520 acc_iaj
= tp
->acc_iaj
+ (cur_iaj
- allowed_iaj
);
523 if (acc_iaj
> MAX_ACC_IAJ
)
524 acc_iaj
= MAX_ACC_IAJ
;
525 tp
->acc_iaj
= acc_iaj
;
528 /* Compute weighted average where the history has a weight of
529 * 15 out of 16 and the current value has a weight of 1 out of 16.
530 * This will make the short-term measurements have more weight.
532 * The addition of 8 will help to round-up the value
533 * instead of round-down
535 tp
->avg_iaj
= (((tp
->avg_iaj
<< IAJ_DIV_SHIFT
) - tp
->avg_iaj
)
536 + cur_iaj
+ IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
538 /* Compute Root-mean-square of deviation where mean is a weighted
539 * average as described above.
541 temp
= tp
->std_dev_iaj
* tp
->std_dev_iaj
;
542 mean
= (((temp
<< IAJ_DIV_SHIFT
) - temp
)
543 + (cur_iaj_dev
* cur_iaj_dev
)
544 + IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
546 tp
->std_dev_iaj
= isqrt(mean
);
548 DTRACE_TCP3(iaj
, struct tcpcb
*, tp
, uint32_t, cur_iaj
,
549 uint32_t, allowed_iaj
);
553 #endif /* TRAFFIC_MGT */
555 /* Check if enough amount of data has been acknowledged since
556 * bw measurement was started
559 tcp_bwmeas_check(struct tcpcb
*tp
)
561 int32_t bw_meas_bytes
;
562 uint32_t bw
, bytes
, elapsed_time
;
563 bw_meas_bytes
= tp
->snd_una
- tp
->t_bwmeas
->bw_start
;
564 if ((tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) != 0 &&
565 bw_meas_bytes
>= (int32_t)(tp
->t_bwmeas
->bw_size
)) {
566 bytes
= bw_meas_bytes
;
567 elapsed_time
= tcp_now
- tp
->t_bwmeas
->bw_ts
;
568 if (elapsed_time
> 0) {
569 bw
= bytes
/ elapsed_time
;
571 if (tp
->t_bwmeas
->bw_sndbw
> 0) {
572 tp
->t_bwmeas
->bw_sndbw
=
573 (((tp
->t_bwmeas
->bw_sndbw
<< 3) - tp
->t_bwmeas
->bw_sndbw
) + bw
) >> 3;
575 tp
->t_bwmeas
->bw_sndbw
= bw
;
579 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
584 tcp_reass(struct tcpcb
*tp
, struct tcphdr
*th
, int *tlenp
, struct mbuf
*m
,
588 struct tseg_qent
*p
= NULL
;
589 struct tseg_qent
*nq
;
590 struct tseg_qent
*te
= NULL
;
591 struct inpcb
*inp
= tp
->t_inpcb
;
592 struct socket
*so
= inp
->inp_socket
;
595 struct mbuf
*oodata
= NULL
;
598 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
599 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
600 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
601 boolean_t dsack_set
= FALSE
;
604 * Call with th==0 after become established to
605 * force pre-ESTABLISHED data up to user socket.
611 * If the reassembly queue already has entries or if we are going
612 * to add a new one, then the connection has reached a loss state.
613 * Reset the stretch-ack algorithm at this point.
615 tcp_reset_stretch_ack(tp
);
620 #endif /* TRAFFIC_MGT */
623 * Limit the number of segments in the reassembly queue to prevent
624 * holding on to too many segments (and thus running out of mbufs).
625 * Make sure to let the missing segment through which caused this
626 * queue. Always keep one global queue entry spare to be able to
627 * process the missing segment.
629 qlimit
= min(max(100, so
->so_rcv
.sb_hiwat
>> 10),
630 tcp_autorcvbuf_max
>> 10);
631 if (th
->th_seq
!= tp
->rcv_nxt
&&
632 (tp
->t_reassqlen
+ 1) >= qlimit
) {
633 tcp_reass_overflows
++;
634 tcpstat
.tcps_rcvmemdrop
++;
640 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
641 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
643 tcpstat
.tcps_rcvmemdrop
++;
650 * Find a segment which begins after this one does.
652 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
653 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
))
659 * If there is a preceding segment, it may provide some of
660 * our data already. If so, drop the data from the incoming
661 * segment. If it provides all of our data, drop us.
665 /* conversion to int (in i) handles seq wraparound */
666 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
668 if (TCP_DSACK_ENABLED(tp
) && i
> 1) {
670 * Note duplicate data sequnce numbers
671 * to report in DSACK option
673 tp
->t_dsack_lseq
= th
->th_seq
;
674 tp
->t_dsack_rseq
= th
->th_seq
+
678 * Report only the first part of partial/
679 * non-contiguous duplicate sequence space
684 tcpstat
.tcps_rcvduppack
++;
685 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
687 nstat_route_rx(inp
->inp_route
.ro_rt
,
689 NSTAT_RX_FLAG_DUPLICATE
);
690 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
692 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
694 tp
->t_stat
.rxduplicatebytes
+= *tlenp
;
697 zfree(tcp_reass_zone
, te
);
701 * Try to present any queued data
702 * at the left window edge to the user.
703 * This is needed after the 3-WHS
714 tcpstat
.tcps_rcvoopack
++;
715 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
717 nstat_route_rx(inp
->inp_route
.ro_rt
, 1, *tlenp
,
718 NSTAT_RX_FLAG_OUT_OF_ORDER
);
719 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
720 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, *tlenp
);
721 tp
->t_stat
.rxoutoforderbytes
+= *tlenp
;
725 * While we overlap succeeding segments trim them or,
726 * if they are completely covered, dequeue them.
729 int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
734 * Report only the first part of partial/non-contiguous
735 * duplicate segment in dsack option. The variable
736 * dsack_set will be true if a previous entry has some of
737 * the duplicate sequence space.
739 if (TCP_DSACK_ENABLED(tp
) && i
> 1 && !dsack_set
) {
740 if (tp
->t_dsack_lseq
== 0) {
741 tp
->t_dsack_lseq
= q
->tqe_th
->th_seq
;
743 tp
->t_dsack_lseq
+ min(i
, q
->tqe_len
);
746 * this segment overlaps data in multple
747 * entries in the reassembly queue, move
748 * the right sequence number further.
751 tp
->t_dsack_rseq
+ min(i
, q
->tqe_len
);
754 if (i
< q
->tqe_len
) {
755 q
->tqe_th
->th_seq
+= i
;
761 nq
= LIST_NEXT(q
, tqe_q
);
762 LIST_REMOVE(q
, tqe_q
);
764 zfree(tcp_reass_zone
, q
);
769 /* Insert the new segment queue entry into place. */
772 te
->tqe_len
= *tlenp
;
775 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
777 LIST_INSERT_AFTER(p
, te
, tqe_q
);
781 * New out-of-order data exists, and is pointed to by
782 * queue entry te. Set copy_oodata to 1 so out-of-order data
783 * can be copied off to sockbuf after in-order data
786 if (!(so
->so_state
& SS_CANTRCVMORE
))
791 * Present data to user, advancing rcv_nxt through
792 * completed sequence space.
794 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
796 q
= LIST_FIRST(&tp
->t_segq
);
797 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
) {
798 /* Stop using LRO once out of order packets arrive */
799 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
800 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
801 th
->th_dport
, th
->th_sport
);
802 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
806 * continue processing if out-of-order data
809 if (q
&& (so
->so_flags
& SOF_ENABLE_MSGS
))
810 goto msg_unordered_delivery
;
815 /* lost packet was recovered, so ooo data can be returned */
816 tcpstat
.tcps_recovered_pkts
++;
819 tp
->rcv_nxt
+= q
->tqe_len
;
820 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
821 nq
= LIST_NEXT(q
, tqe_q
);
822 LIST_REMOVE(q
, tqe_q
);
823 if (so
->so_state
& SS_CANTRCVMORE
) {
826 so_recv_data_stat(so
, q
->tqe_m
, 0); /* XXXX */
827 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
829 * Append the inorder data as a message to the
830 * receive socket buffer. Also check to see if
831 * the data we are about to deliver is the same
832 * data that we wanted to pass up to the user
833 * out of order. If so, reset copy_oodata --
834 * the received data filled a gap, and
840 if (sbappendstream_rcvdemux(so
, q
->tqe_m
,
841 q
->tqe_th
->th_seq
- (tp
->irs
+ 1), 0))
843 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
844 tcp_update_lro_seq(tp
->rcv_nxt
,
845 inp
->inp_laddr
, inp
->inp_faddr
,
846 th
->th_dport
, th
->th_sport
);
849 zfree(tcp_reass_zone
, q
);
852 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
855 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
857 KERNEL_DEBUG(DBG_LAYER_BEG
,
858 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
859 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
860 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
866 KERNEL_DEBUG(DBG_LAYER_BEG
,
867 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
868 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
869 (inp
->inp_faddr
.s_addr
& 0xffff)),
873 msg_unordered_delivery
:
874 /* Deliver out-of-order data as a message */
875 if (te
&& (so
->so_flags
& SOF_ENABLE_MSGS
) && copy_oodata
&& te
->tqe_len
) {
877 * make a copy of the mbuf to be delivered up to
878 * the user, and add it to the sockbuf
880 oodata
= m_copym(te
->tqe_m
, 0, M_COPYALL
, M_DONTWAIT
);
881 if (oodata
!= NULL
) {
882 if (sbappendmsgstream_rcv(&so
->so_rcv
, oodata
,
883 te
->tqe_th
->th_seq
- (tp
->irs
+ 1), 1)) {
885 tcpstat
.tcps_msg_unopkts
++;
887 tcpstat
.tcps_msg_unoappendfail
++;
893 sorwakeup(so
); /* done with socket lock held */
898 * Reduce congestion window -- used when ECN is seen or when a tail loss
899 * probe recovers the last packet.
902 tcp_reduce_congestion_window(
906 * If the current tcp cc module has
907 * defined a hook for tasks to run
908 * before entering FR, call it
910 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
911 CC_ALGO(tp
)->pre_fr(tp
);
912 ENTER_FASTRECOVERY(tp
);
913 if (tp
->t_flags
& TF_SENTFIN
)
914 tp
->snd_recover
= tp
->snd_max
- 1;
916 tp
->snd_recover
= tp
->snd_max
;
917 tp
->t_timer
[TCPT_REXMT
] = 0;
918 tp
->t_timer
[TCPT_PTO
] = 0;
920 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
921 tcp_cc_adjust_nonvalidated_cwnd(tp
);
923 tp
->snd_cwnd
= tp
->snd_ssthresh
+
924 tp
->t_maxseg
* tcprexmtthresh
;
929 * This function is called upon reception of data on a socket. It's purpose is
930 * to handle the adaptive keepalive timers that monitor whether the connection
931 * is making progress. First the adaptive read-timer, second the TFO probe-timer.
933 * The application wants to get an event if there is a stall during read.
934 * Set the initial keepalive timeout to be equal to twice RTO.
936 * If the outgoing interface is in marginal conditions, we need to
937 * enable read probes for that too.
940 tcp_adaptive_rwtimo_check(struct tcpcb
*tp
, int tlen
)
942 struct ifnet
*outifp
= tp
->t_inpcb
->inp_last_outifp
;
944 if ((tp
->t_adaptive_rtimo
> 0 ||
946 (outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
)))
948 tp
->t_state
== TCPS_ESTABLISHED
) {
949 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
950 (TCP_REXMTVAL(tp
) << 1));
951 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
952 tp
->t_rtimo_probes
= 0;
957 tcp_keepalive_reset(struct tcpcb
*tp
)
959 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
960 TCP_CONN_KEEPIDLE(tp
));
961 tp
->t_flagsext
&= ~(TF_DETECT_READSTALL
);
962 tp
->t_rtimo_probes
= 0;
966 * TCP input routine, follows pages 65-76 of the
967 * protocol specification dated September, 1981 very closely.
971 tcp6_input(struct mbuf
**mp
, int *offp
, int proto
)
973 #pragma unused(proto)
974 register struct mbuf
*m
= *mp
;
976 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
978 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
980 /* Expect 32-bit aligned data pointer on strict-align platforms */
981 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
984 * draft-itojun-ipv6-tcp-to-anycast
985 * better place to put this in?
987 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) == 0) {
988 if (ia6_flags
& IN6_IFF_ANYCAST
) {
991 ip6
= mtod(m
, struct ip6_hdr
*);
992 icmp6_error(m
, ICMP6_DST_UNREACH
,
993 ICMP6_DST_UNREACH_ADDR
,
994 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
996 IF_TCP_STATINC(ifp
, icmp6unreach
);
998 return (IPPROTO_DONE
);
1002 tcp_input(m
, *offp
);
1003 return (IPPROTO_DONE
);
1007 /* Depending on the usage of mbuf space in the system, this function
1008 * will return true or false. This is used to determine if a socket
1009 * buffer can take more memory from the system for auto-tuning or not.
1012 tcp_cansbgrow(struct sockbuf
*sb
)
1014 /* Calculate the host level space limit in terms of MSIZE buffers.
1015 * We can use a maximum of half of the available mbuf space for
1018 u_int32_t mblim
= ((nmbclusters
>> 1) << (MCLSHIFT
- MSIZESHIFT
));
1020 /* Calculate per sb limit in terms of bytes. We optimize this limit
1021 * for upto 16 socket buffers.
1024 u_int32_t sbspacelim
= ((nmbclusters
>> 4) << MCLSHIFT
);
1026 if ((total_sbmb_cnt
< mblim
) &&
1027 (sb
->sb_hiwat
< sbspacelim
)) {
1030 OSIncrementAtomic64(&sbmb_limreached
);
1036 tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1037 u_int32_t newsize
, u_int32_t idealsize
)
1040 /* newsize should not exceed max */
1041 newsize
= min(newsize
, tcp_autorcvbuf_max
);
1043 /* The receive window scale negotiated at the
1044 * beginning of the connection will also set a
1045 * limit on the socket buffer size
1047 newsize
= min(newsize
, TCP_MAXWIN
<< tp
->rcv_scale
);
1049 /* Set new socket buffer size */
1050 if (newsize
> sbrcv
->sb_hiwat
&&
1051 (sbreserve(sbrcv
, newsize
) == 1)) {
1052 sbrcv
->sb_idealsize
= min(max(sbrcv
->sb_idealsize
,
1053 (idealsize
!= 0) ? idealsize
: newsize
),
1054 tcp_autorcvbuf_max
);
1056 /* Again check the limit set by the advertised
1059 sbrcv
->sb_idealsize
= min(sbrcv
->sb_idealsize
,
1060 TCP_MAXWIN
<< tp
->rcv_scale
);
1065 * This function is used to grow a receive socket buffer. It
1066 * will take into account system-level memory usage and the
1067 * bandwidth available on the link to make a decision.
1070 tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1071 struct tcpopt
*to
, u_int32_t pktlen
)
1073 struct socket
*so
= sbrcv
->sb_so
;
1076 * Do not grow the receive socket buffer if
1077 * - auto resizing is disabled, globally or on this socket
1078 * - the high water mark already reached the maximum
1079 * - the stream is in background and receive side is being
1081 * - if there are segments in reassembly queue indicating loss,
1082 * do not need to increase recv window during recovery as more
1083 * data is not going to be sent. A duplicate ack sent during
1084 * recovery should not change the receive window
1086 if (tcp_do_autorcvbuf
== 0 ||
1087 (sbrcv
->sb_flags
& SB_AUTOSIZE
) == 0 ||
1088 tcp_cansbgrow(sbrcv
) == 0 ||
1089 sbrcv
->sb_hiwat
>= tcp_autorcvbuf_max
||
1090 (tp
->t_flagsext
& TF_RECV_THROTTLE
) ||
1091 (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) ||
1092 !LIST_EMPTY(&tp
->t_segq
)) {
1093 /* Can not resize the socket buffer, just return */
1097 if (TSTMP_GT(tcp_now
,
1098 tp
->rfbuf_ts
+ TCPTV_RCVBUFIDLE
)) {
1099 /* If there has been an idle period in the
1100 * connection, just restart the measurement
1105 if (!TSTMP_SUPPORTED(tp
)) {
1107 * Timestamp option is not supported on this connection.
1108 * If the connection reached a state to indicate that
1109 * the receive socket buffer needs to grow, increase
1110 * the high water mark.
1112 if (TSTMP_GEQ(tcp_now
,
1113 tp
->rfbuf_ts
+ TCPTV_RCVNOTS_QUANTUM
)) {
1114 if (tp
->rfbuf_cnt
>= TCP_RCVNOTS_BYTELEVEL
) {
1115 tcp_sbrcv_reserve(tp
, sbrcv
,
1116 tcp_autorcvbuf_max
, 0);
1120 tp
->rfbuf_cnt
+= pktlen
;
1123 } else if (to
->to_tsecr
!= 0) {
1125 * If the timestamp shows that one RTT has
1126 * completed, we can stop counting the
1127 * bytes. Here we consider increasing
1128 * the socket buffer if the bandwidth measured in
1129 * last rtt, is more than half of sb_hiwat, this will
1130 * help to scale the buffer according to the bandwidth
1133 if (TSTMP_GEQ(to
->to_tsecr
, tp
->rfbuf_ts
)) {
1134 if (tp
->rfbuf_cnt
> (sbrcv
->sb_hiwat
-
1135 (sbrcv
->sb_hiwat
>> 1))) {
1136 int32_t rcvbuf_inc
, min_incr
;
1138 * Increment the receive window by a
1139 * multiple of maximum sized segments.
1140 * This will prevent a connection from
1141 * sending smaller segments on wire if it
1142 * is limited by the receive window.
1144 * Set the ideal size based on current
1145 * bandwidth measurements. We set the
1146 * ideal size on receive socket buffer to
1147 * be twice the bandwidth delay product.
1149 rcvbuf_inc
= (tp
->rfbuf_cnt
<< 1)
1153 * Make the increment equal to 8 segments
1156 min_incr
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1157 if (rcvbuf_inc
< min_incr
)
1158 rcvbuf_inc
= min_incr
;
1161 (rcvbuf_inc
/ tp
->t_maxseg
) * tp
->t_maxseg
;
1162 tcp_sbrcv_reserve(tp
, sbrcv
,
1163 sbrcv
->sb_hiwat
+ rcvbuf_inc
,
1164 (tp
->rfbuf_cnt
* 2));
1168 tp
->rfbuf_cnt
+= pktlen
;
1173 /* Restart the measurement */
1179 /* This function will trim the excess space added to the socket buffer
1180 * to help a slow-reading app. The ideal-size of a socket buffer depends
1181 * on the link bandwidth or it is set by an application and we aim to
1185 tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sbrcv
) {
1186 if (tcp_do_autorcvbuf
== 1 && sbrcv
->sb_idealsize
> 0 &&
1187 sbrcv
->sb_hiwat
> sbrcv
->sb_idealsize
) {
1189 /* compute the difference between ideal and current sizes */
1190 u_int32_t diff
= sbrcv
->sb_hiwat
- sbrcv
->sb_idealsize
;
1192 /* Compute the maximum advertised window for
1195 u_int32_t advwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
1197 /* How much can we trim the receive socket buffer?
1198 * 1. it can not be trimmed beyond the max rcv win advertised
1199 * 2. if possible, leave 1/16 of bandwidth*delay to
1200 * avoid closing the win completely
1202 u_int32_t leave
= max(advwin
, (sbrcv
->sb_idealsize
>> 4));
1204 /* Sometimes leave can be zero, in that case leave at least
1205 * a few segments worth of space.
1208 leave
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1210 trim
= sbrcv
->sb_hiwat
- (sbrcv
->sb_cc
+ leave
);
1211 trim
= imin(trim
, (int32_t)diff
);
1214 sbreserve(sbrcv
, (sbrcv
->sb_hiwat
- trim
));
1218 /* We may need to trim the send socket buffer size for two reasons:
1219 * 1. if the rtt seen on the connection is climbing up, we do not
1220 * want to fill the buffers any more.
1221 * 2. if the congestion win on the socket backed off, there is no need
1222 * to hold more mbufs for that connection than what the cwnd will allow.
1225 tcp_sbsnd_trim(struct sockbuf
*sbsnd
) {
1226 if (tcp_do_autosendbuf
== 1 &&
1227 ((sbsnd
->sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) ==
1228 (SB_AUTOSIZE
| SB_TRIM
)) &&
1229 (sbsnd
->sb_idealsize
> 0) &&
1230 (sbsnd
->sb_hiwat
> sbsnd
->sb_idealsize
)) {
1232 if (sbsnd
->sb_cc
<= sbsnd
->sb_idealsize
) {
1233 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_idealsize
;
1235 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_cc
;
1237 sbreserve(sbsnd
, (sbsnd
->sb_hiwat
- trim
));
1239 if (sbsnd
->sb_hiwat
<= sbsnd
->sb_idealsize
)
1240 sbsnd
->sb_flags
&= ~(SB_TRIM
);
1244 * If timestamp option was not negotiated on this connection
1245 * and this connection is on the receiving side of a stream
1246 * then we can not measure the delay on the link accurately.
1247 * Instead of enabling automatic receive socket buffer
1248 * resizing, just give more space to the receive socket buffer.
1251 tcp_sbrcv_tstmp_check(struct tcpcb
*tp
) {
1252 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1253 u_int32_t newsize
= 2 * tcp_recvspace
;
1254 struct sockbuf
*sbrcv
= &so
->so_rcv
;
1256 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_RCVD_TSTMP
)) !=
1257 (TF_REQ_TSTMP
| TF_RCVD_TSTMP
) &&
1258 (sbrcv
->sb_flags
& SB_AUTOSIZE
) != 0) {
1259 tcp_sbrcv_reserve(tp
, sbrcv
, newsize
, 0);
1263 /* A receiver will evaluate the flow of packets on a connection
1264 * to see if it can reduce ack traffic. The receiver will start
1265 * stretching acks if all of the following conditions are met:
1266 * 1. tcp_delack_enabled is set to 3
1267 * 2. If the bytes received in the last 100ms is greater than a threshold
1268 * defined by maxseg_unacked
1269 * 3. If the connection has not been idle for tcp_maxrcvidle period.
1270 * 4. If the connection has seen enough packets to let the slow-start
1271 * finish after connection establishment or after some packet loss.
1273 * The receiver will stop stretching acks if there is congestion/reordering
1274 * as indicated by packets on reassembly queue or an ECN. If the delayed-ack
1275 * timer fires while stretching acks, it means that the packet flow has gone
1276 * below the threshold defined by maxseg_unacked and the receiver will stop
1277 * stretching acks. The receiver gets no indication when slow-start is completed
1278 * or when the connection reaches an idle state. That is why we use
1279 * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle
1283 tcp_stretch_ack_enable(struct tcpcb
*tp
)
1285 if (!(tp
->t_flagsext
& (TF_NOSTRETCHACK
|TF_DISABLE_STRETCHACK
)) &&
1286 tp
->rcv_by_unackwin
>= (maxseg_unacked
* tp
->t_maxseg
) &&
1287 TSTMP_GT(tp
->rcv_unackwin
+ tcp_maxrcvidle
, tcp_now
) &&
1288 (!(tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) ||
1289 (tp
->rcv_waitforss
>= tcp_rcvsspktcnt
))) {
1297 * Reset the state related to stretch-ack algorithm. This will make
1298 * the receiver generate an ack every other packet. The receiver
1299 * will start re-evaluating the rate at which packets come to decide
1300 * if it can benefit by lowering the ack traffic.
1303 tcp_reset_stretch_ack(struct tcpcb
*tp
)
1305 tp
->t_flags
&= ~(TF_STRETCHACK
);
1306 tp
->rcv_by_unackwin
= 0;
1307 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
1310 * When there is packet loss or packet re-ordering or CWR due to
1311 * ECN, the sender's congestion window is reduced. In these states,
1312 * generate an ack for every other packet for some time to allow
1313 * the sender's congestion window to grow.
1315 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
1316 tp
->rcv_waitforss
= 0;
1320 * The last packet was a retransmission, check if this ack
1321 * indicates that the retransmission was spurious.
1323 * If the connection supports timestamps, we could use it to
1324 * detect if the last retransmit was not needed. Otherwise,
1325 * we check if the ACK arrived within RTT/2 window, then it
1326 * was a mistake to do the retransmit in the first place.
1328 * This function will return 1 if it is a spurious retransmit,
1332 tcp_detect_bad_rexmt(struct tcpcb
*tp
, struct tcphdr
*th
,
1333 struct tcpopt
*to
, u_int32_t rxtime
)
1335 int32_t tdiff
, bad_rexmt_win
;
1336 bad_rexmt_win
= (tp
->t_srtt
>> (TCP_RTT_SHIFT
+ 1));
1338 /* If the ack has ECN CE bit, then cwnd has to be adjusted */
1339 if (TCP_ECN_ENABLED(tp
) && (th
->th_flags
& TH_ECE
))
1341 if (TSTMP_SUPPORTED(tp
)) {
1342 if (rxtime
> 0 && (to
->to_flags
& TOF_TS
)
1343 && to
->to_tsecr
!= 0
1344 && TSTMP_LT(to
->to_tsecr
, rxtime
))
1347 if ((tp
->t_rxtshift
== 1
1348 || (tp
->t_flagsext
& TF_SENT_TLPROBE
))
1350 tdiff
= (int32_t)(tcp_now
- rxtime
);
1351 if (tdiff
< bad_rexmt_win
)
1360 * Restore congestion window state if a spurious timeout
1364 tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
)
1366 if (TSTMP_SUPPORTED(tp
)) {
1367 u_int32_t fsize
, acked
;
1368 fsize
= tp
->snd_max
- th
->th_ack
;
1369 acked
= BYTES_ACKED(th
, tp
);
1372 * Implement bad retransmit recovery as
1373 * described in RFC 4015.
1375 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1377 /* Initialize cwnd to the initial window */
1378 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
1379 CC_ALGO(tp
)->cwnd_init(tp
);
1381 tp
->snd_cwnd
= fsize
+ min(acked
, tp
->snd_cwnd
);
1384 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1385 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1386 if (tp
->t_flags
& TF_WASFRECOVERY
)
1387 ENTER_FASTRECOVERY(tp
);
1389 /* Do not use the loss flight size in this case */
1390 tp
->t_lossflightsize
= 0;
1392 tp
->snd_cwnd
= max(tp
->snd_cwnd
, TCP_CC_CWND_INIT_BYTES
);
1393 tp
->snd_recover
= tp
->snd_recover_prev
;
1394 tp
->snd_nxt
= tp
->snd_max
;
1398 /* Fix send socket buffer to reflect the change in cwnd */
1399 tcp_bad_rexmt_fix_sndbuf(tp
);
1402 * This RTT might reflect the extra delay induced
1403 * by the network. Skip using this sample for RTO
1404 * calculation and mark the connection so we can
1405 * recompute RTT when the next eligible sample is
1408 tp
->t_flagsext
|= TF_RECOMPUTE_RTT
;
1409 tp
->t_badrexmt_time
= tcp_now
;
1414 * If the previous packet was sent in retransmission timer, and it was
1415 * not needed, then restore the congestion window to the state before that
1418 * If the last packet was sent in tail loss probe timeout, check if that
1419 * recovered the last packet. If so, that will indicate a real loss and
1420 * the congestion window needs to be lowered.
1423 tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
, struct tcpopt
*to
)
1425 if (tp
->t_rxtshift
> 0 &&
1426 tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_rxtstart
)) {
1427 ++tcpstat
.tcps_sndrexmitbad
;
1428 tcp_bad_rexmt_restore_state(tp
, th
);
1429 tcp_ccdbg_trace(tp
, th
, TCP_CC_BAD_REXMT_RECOVERY
);
1430 } else if ((tp
->t_flagsext
& TF_SENT_TLPROBE
)
1431 && tp
->t_tlphighrxt
> 0
1432 && SEQ_GEQ(th
->th_ack
, tp
->t_tlphighrxt
)
1433 && !tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_tlpstart
)) {
1435 * check DSACK information also to make sure that
1436 * the TLP was indeed needed
1438 if (tcp_rxtseg_dsack_for_tlp(tp
)) {
1440 * received a DSACK to indicate that TLP was
1443 tcp_rxtseg_clean(tp
);
1448 * The tail loss probe recovered the last packet and
1449 * we need to adjust the congestion window to take
1450 * this loss into account.
1452 ++tcpstat
.tcps_tlp_recoverlastpkt
;
1453 if (!IN_FASTRECOVERY(tp
)) {
1454 tcp_reduce_congestion_window(tp
);
1455 EXIT_FASTRECOVERY(tp
);
1457 tcp_ccdbg_trace(tp
, th
, TCP_CC_TLP_RECOVER_LASTPACKET
);
1458 } else if (tcp_rxtseg_detect_bad_rexmt(tp
, th
->th_ack
)) {
1460 * All of the retransmitted segments were duplicated, this
1461 * can be an indication of bad fast retransmit.
1463 tcpstat
.tcps_dsack_badrexmt
++;
1464 tcp_bad_rexmt_restore_state(tp
, th
);
1465 tcp_ccdbg_trace(tp
, th
, TCP_CC_DSACK_BAD_REXMT
);
1466 tcp_rxtseg_clean(tp
);
1469 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1470 tp
->t_tlphighrxt
= 0;
1474 * check if the latest ack was for a segment sent during PMTU
1475 * blackhole detection. If the timestamp on the ack is before
1476 * PMTU blackhole detection, then revert the size of the max
1477 * segment to previous size.
1479 if (tp
->t_rxtshift
> 0 && (tp
->t_flags
& TF_BLACKHOLE
) &&
1480 tp
->t_pmtud_start_ts
> 0 && TSTMP_SUPPORTED(tp
)) {
1481 if ((to
->to_flags
& TOF_TS
) && to
->to_tsecr
!= 0
1482 && TSTMP_LT(to
->to_tsecr
, tp
->t_pmtud_start_ts
)) {
1483 tcp_pmtud_revert_segment_size(tp
);
1486 if (tp
->t_pmtud_start_ts
> 0)
1487 tp
->t_pmtud_start_ts
= 0;
1491 * Check if early retransmit can be attempted according to RFC 5827.
1493 * If packet reordering is detected on a connection, fast recovery will
1494 * be delayed until it is clear that the packet was lost and not reordered.
1495 * But reordering detection is done only when SACK is enabled.
1497 * On connections that do not support SACK, there is a limit on the number
1498 * of early retransmits that can be done per minute. This limit is needed
1499 * to make sure that too many packets are not retransmitted when there is
1500 * packet reordering.
1503 tcp_early_rexmt_check (struct tcpcb
*tp
, struct tcphdr
*th
)
1505 u_int32_t obytes
, snd_off
;
1507 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1509 if (early_rexmt
&& (SACK_ENABLED(tp
) ||
1510 tp
->t_early_rexmt_count
< TCP_EARLY_REXMT_LIMIT
) &&
1511 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1512 (tp
->t_dupacks
== 1 ||
1513 (SACK_ENABLED(tp
) &&
1514 !TAILQ_EMPTY(&tp
->snd_holes
)))) {
1516 * If there are only a few outstanding
1517 * segments on the connection, we might need
1518 * to lower the retransmit threshold. This
1519 * will allow us to do Early Retransmit as
1520 * described in RFC 5827.
1522 if (SACK_ENABLED(tp
) &&
1523 !TAILQ_EMPTY(&tp
->snd_holes
)) {
1524 obytes
= (tp
->snd_max
- tp
->snd_fack
) +
1525 tp
->sackhint
.sack_bytes_rexmit
;
1527 obytes
= (tp
->snd_max
- tp
->snd_una
);
1531 * In order to lower retransmit threshold the
1532 * following two conditions must be met.
1533 * 1. the amount of outstanding data is less
1535 * 2. there is no unsent data ready for
1536 * transmission or the advertised window
1537 * will limit sending new segments.
1539 snd_off
= tp
->snd_max
- tp
->snd_una
;
1540 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
) - snd_off
;
1541 if (obytes
< (tp
->t_maxseg
<< 2) &&
1545 osegs
= obytes
/ tp
->t_maxseg
;
1546 if ((osegs
* tp
->t_maxseg
) < obytes
)
1550 * Since the connection might have already
1551 * received some dupacks, we add them to
1552 * to the outstanding segments count to get
1553 * the correct retransmit threshold.
1555 * By checking for early retransmit after
1556 * receiving some duplicate acks when SACK
1557 * is supported, the connection will
1558 * enter fast recovery even if multiple
1559 * segments are lost in the same window.
1561 osegs
+= tp
->t_dupacks
;
1564 ((osegs
- 1) > 1) ? (osegs
- 1) : 1;
1566 min(tp
->t_rexmtthresh
, tcprexmtthresh
);
1568 max(tp
->t_rexmtthresh
, tp
->t_dupacks
);
1570 if (tp
->t_early_rexmt_count
== 0)
1571 tp
->t_early_rexmt_win
= tcp_now
;
1573 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1574 tcpstat
.tcps_tlp_recovery
++;
1575 tcp_ccdbg_trace(tp
, th
,
1576 TCP_CC_TLP_RECOVERY
);
1578 tcpstat
.tcps_early_rexmt
++;
1579 tp
->t_early_rexmt_count
++;
1580 tcp_ccdbg_trace(tp
, th
,
1581 TCP_CC_EARLY_RETRANSMIT
);
1588 * If we ever sent a TLP probe, the acknowledgement will trigger
1589 * early retransmit because the value of snd_fack will be close
1590 * to snd_max. This will take care of adjustments to the
1591 * congestion window. So we can reset TF_SENT_PROBE flag.
1593 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1594 tp
->t_tlphighrxt
= 0;
1603 u_char out
[CCAES_BLOCK_SIZE
];
1606 if (!(to
->to_flags
& (TOF_TFO
| TOF_TFOREQ
)) ||
1607 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
))
1610 if ((to
->to_flags
& TOF_TFOREQ
)) {
1611 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1613 tp
->t_tfo_stats
|= TFO_S_COOKIEREQ_RECV
;
1614 tcpstat
.tcps_tfo_cookie_req_rcv
++;
1618 /* Ok, then it must be an offered cookie. We need to check that ... */
1619 tcp_tfo_gen_cookie(tp
->t_inpcb
, out
, sizeof(out
));
1621 len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1623 if (memcmp(out
, to
->to_tfo
, len
)) {
1624 /* Cookies are different! Let's return and offer a new cookie */
1625 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1627 tp
->t_tfo_stats
|= TFO_S_COOKIE_INVALID
;
1628 tcpstat
.tcps_tfo_cookie_invalid
++;
1632 if (OSIncrementAtomic(&tcp_tfo_halfcnt
) >= tcp_tfo_backlog
) {
1633 /* Need to decrement again as we just increased it... */
1634 OSDecrementAtomic(&tcp_tfo_halfcnt
);
1638 tp
->t_tfo_flags
|= TFO_F_COOKIE_VALID
;
1640 tp
->t_tfo_stats
|= TFO_S_SYNDATA_RCV
;
1641 tcpstat
.tcps_tfo_syn_data_rcv
++;
1647 tcp_tfo_synack(tp
, to
)
1651 if (to
->to_flags
& TOF_TFO
) {
1652 unsigned char len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1655 * If this happens, things have gone terribly wrong. len should
1656 * have been checked in tcp_dooptions.
1658 VERIFY(len
<= TFO_COOKIE_LEN_MAX
);
1662 tcp_cache_set_cookie(tp
, to
->to_tfo
, len
);
1663 tcp_heuristic_tfo_success(tp
);
1665 tp
->t_tfo_stats
|= TFO_S_COOKIE_RCV
;
1666 tcpstat
.tcps_tfo_cookie_rcv
++;
1669 * Thus, no cookie in the response, but we either asked for one
1670 * or sent SYN+DATA. Now, we need to check whether we had to
1671 * rexmit the SYN. If that's the case, it's better to start
1672 * backing of TFO-cookie requests.
1674 if (tp
->t_tfo_flags
& TFO_F_SYN_LOSS
)
1675 tcp_heuristic_inc_loss(tp
, 1, 0);
1677 tcp_heuristic_reset_loss(tp
, 1, 0);
1682 tcp_tfo_rcv_probe(struct tcpcb
*tp
, int tlen
)
1685 tp
->t_tfo_probe_state
= TFO_PROBE_PROBING
;
1688 * We send the probe out rather quickly (after one RTO). It does not
1689 * really hurt that much, it's only one additional segment on the wire.
1691 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, (TCP_REXMTVAL(tp
)));
1693 /* If SYN/ACK+data, don't probe. We got the data! */
1694 tcp_heuristic_tfo_rcv_good(tp
);
1699 tcp_tfo_rcv_data(struct tcpcb
*tp
)
1701 /* Transition from PROBING to NONE as data has been received */
1702 if (tp
->t_tfo_probe_state
>= TFO_PROBE_PROBING
) {
1703 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1705 /* Data has been received - we are good to go! */
1706 tcp_heuristic_tfo_rcv_good(tp
);
1711 tcp_tfo_rcv_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
1713 if (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
&&
1714 tp
->t_tfo_probes
> 0) {
1715 if (th
->th_seq
== tp
->rcv_nxt
) {
1716 /* No hole, so stop probing */
1717 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1718 } else if (SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1719 /* There is a hole! Wait a bit for data... */
1720 tp
->t_tfo_probe_state
= TFO_PROBE_WAIT_DATA
;
1721 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1732 register struct tcphdr
*th
;
1733 register struct ip
*ip
= NULL
;
1734 register struct inpcb
*inp
;
1735 u_char
*optp
= NULL
;
1739 register struct tcpcb
*tp
= 0;
1740 register int thflags
;
1741 struct socket
*so
= 0;
1742 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
1743 struct in_addr laddr
;
1745 struct in6_addr laddr6
;
1748 int iss
= 0, nosock
= 0;
1749 u_int32_t tiwin
, sack_bytes_acked
= 0;
1750 struct tcpopt to
; /* options in this segment */
1755 struct sockaddr_in
*next_hop
= NULL
;
1756 struct m_tag
*fwd_tag
;
1757 #endif /* IPFIREWALL */
1758 u_char ip_ecn
= IPTOS_ECN_NOTECT
;
1759 unsigned int ifscope
;
1760 uint8_t isconnected
, isdisconnected
;
1761 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1762 int pktf_sw_lro_pkt
= (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) ? 1 : 0;
1763 int nlropkts
= (pktf_sw_lro_pkt
== 1) ? m
->m_pkthdr
.lro_npkts
: 1;
1764 int turnoff_lro
= 0, win
;
1766 struct mptcb
*mp_tp
= NULL
;
1768 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
1769 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
1770 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
1771 boolean_t recvd_dsack
= FALSE
;
1772 struct tcp_respond_args tra
;
1774 #define TCP_INC_VAR(stat, npkts) do { \
1778 TCP_INC_VAR(tcpstat
.tcps_rcvtotal
, nlropkts
);
1780 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
1781 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1782 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1783 KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
1787 if (fwd_tag
!= NULL
) {
1788 struct ip_fwd_tag
*ipfwd_tag
=
1789 (struct ip_fwd_tag
*)(fwd_tag
+1);
1791 next_hop
= ipfwd_tag
->next_hop
;
1792 m_tag_delete(m
, fwd_tag
);
1794 #endif /* IPFIREWALL */
1797 struct ip6_hdr
*ip6
= NULL
;
1800 int rstreason
; /* For badport_bandlim accounting purposes */
1801 struct proc
*proc0
=current_proc();
1803 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
1806 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
1808 bzero((char *)&to
, sizeof(to
));
1813 * Expect 32-bit aligned data pointer on
1814 * strict-align platforms
1816 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1818 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
1819 ip6
= mtod(m
, struct ip6_hdr
*);
1820 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
1821 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
1823 if (tcp_input_checksum(AF_INET6
, m
, th
, off0
, tlen
))
1826 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1827 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
1828 th
->th_seq
, th
->th_ack
, th
->th_win
);
1830 * Be proactive about unspecified IPv6 address in source.
1831 * As we use all-zero to indicate unbounded/unconnected pcb,
1832 * unspecified IPv6 address can be used to confuse us.
1834 * Note that packets with unspecified IPv6 destination is
1835 * already dropped in ip6_input.
1837 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
1839 IF_TCP_STATINC(ifp
, unspecv6
);
1842 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1843 struct ip6_hdr
*, ip6
, struct tcpcb
*, NULL
,
1844 struct tcphdr
*, th
);
1846 ip_ecn
= (ntohl(ip6
->ip6_flow
) >> 20) & IPTOS_ECN_MASK
;
1851 * Get IP and TCP header together in first mbuf.
1852 * Note: IP leaves IP header in first mbuf.
1854 if (off0
> sizeof (struct ip
)) {
1855 ip_stripoptions(m
, (struct mbuf
*)0);
1856 off0
= sizeof(struct ip
);
1858 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
1859 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
1860 tcpstat
.tcps_rcvshort
++;
1865 /* Expect 32-bit aligned data pointer on strict-align platforms */
1866 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1868 ip
= mtod(m
, struct ip
*);
1869 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
1872 if (tcp_input_checksum(AF_INET
, m
, th
, off0
, tlen
))
1876 /* Re-initialization for later version check */
1877 ip
->ip_v
= IPVERSION
;
1879 ip_ecn
= (ip
->ip_tos
& IPTOS_ECN_MASK
);
1881 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1882 struct ip
*, ip
, struct tcpcb
*, NULL
, struct tcphdr
*, th
);
1884 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1885 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
1886 th
->th_seq
, th
->th_ack
, th
->th_win
);
1891 * Check that TCP offset makes sense,
1892 * pull out TCP options and adjust length. XXX
1894 off
= th
->th_off
<< 2;
1895 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
1896 tcpstat
.tcps_rcvbadoff
++;
1897 IF_TCP_STATINC(ifp
, badformat
);
1900 tlen
-= off
; /* tlen is used instead of ti->ti_len */
1901 if (off
> sizeof (struct tcphdr
)) {
1904 IP6_EXTHDR_CHECK(m
, off0
, off
, return);
1905 ip6
= mtod(m
, struct ip6_hdr
*);
1906 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
1910 if (m
->m_len
< sizeof(struct ip
) + off
) {
1911 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
1912 tcpstat
.tcps_rcvshort
++;
1915 ip
= mtod(m
, struct ip
*);
1916 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
1919 optlen
= off
- sizeof (struct tcphdr
);
1920 optp
= (u_char
*)(th
+ 1);
1922 * Do quick retrieval of timestamp options ("options
1923 * prediction?"). If timestamp is the only option and it's
1924 * formatted as recommended in RFC 1323 appendix A, we
1925 * quickly get the values now and not bother calling
1926 * tcp_dooptions(), etc.
1928 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
1929 (optlen
> TCPOLEN_TSTAMP_APPA
&&
1930 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
1931 *(u_int32_t
*)(void *)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
1932 (th
->th_flags
& TH_SYN
) == 0) {
1933 to
.to_flags
|= TOF_TS
;
1934 to
.to_tsval
= ntohl(*(u_int32_t
*)(void *)(optp
+ 4));
1935 to
.to_tsecr
= ntohl(*(u_int32_t
*)(void *)(optp
+ 8));
1936 optp
= NULL
; /* we've parsed the options */
1939 thflags
= th
->th_flags
;
1943 * If the drop_synfin option is enabled, drop all packets with
1944 * both the SYN and FIN bits set. This prevents e.g. nmap from
1945 * identifying the TCP/IP stack.
1947 * This is a violation of the TCP specification.
1949 if (drop_synfin
&& (thflags
& (TH_SYN
|TH_FIN
)) == (TH_SYN
|TH_FIN
)) {
1950 IF_TCP_STATINC(ifp
, synfin
);
1956 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
1957 * until after ip6_savecontrol() is called and before other functions
1958 * which don't want those proto headers.
1959 * Because ip6_savecontrol() is going to parse the mbuf to
1960 * search for data to be passed up to user-land, it wants mbuf
1961 * parameters to be unchanged.
1963 drop_hdrlen
= off0
+ off
;
1965 /* Since this is an entry point for input processing of tcp packets, we
1966 * can update the tcp clock here.
1968 calculate_tcp_clock();
1971 * Record the interface where this segment arrived on; this does not
1972 * affect normal data output (for non-detached TCP) as it provides a
1973 * hint about which route and interface to use for sending in the
1974 * absence of a PCB, when scoped routing (and thus source interface
1975 * selection) are enabled.
1977 if ((m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) || m
->m_pkthdr
.rcvif
== NULL
)
1978 ifscope
= IFSCOPE_NONE
;
1980 ifscope
= m
->m_pkthdr
.rcvif
->if_index
;
1983 * Convert TCP protocol specific fields to host format.
1986 #if BYTE_ORDER != BIG_ENDIAN
1994 * Locate pcb for segment.
1998 isconnected
= FALSE
;
1999 isdisconnected
= FALSE
;
2001 #if IPFIREWALL_FORWARD
2002 if (next_hop
!= NULL
2004 && isipv6
== 0 /* IPv6 support is not yet */
2008 * Diverted. Pretend to be the destination.
2009 * already got one like this?
2011 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2012 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
2015 * No, then it's new. Try find the ambushing socket
2017 if (!next_hop
->sin_port
) {
2018 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
2019 th
->th_sport
, next_hop
->sin_addr
,
2020 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2022 inp
= in_pcblookup_hash(&tcbinfo
,
2023 ip
->ip_src
, th
->th_sport
,
2025 ntohs(next_hop
->sin_port
), 1,
2030 #endif /* IPFIREWALL_FORWARD */
2034 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
2035 &ip6
->ip6_dst
, th
->th_dport
, 1,
2039 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2040 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2044 * Use the interface scope information from the PCB for outbound
2045 * segments. If the PCB isn't present and if scoped routing is
2046 * enabled, tcp_respond will use the scope of the interface where
2047 * the segment arrived on.
2049 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
))
2050 ifscope
= inp
->inp_boundifp
->if_index
;
2053 * If the state is CLOSED (i.e., TCB does not exist) then
2054 * all data in the incoming segment is discarded.
2055 * If the TCB exists but is in CLOSED state, it is embryonic,
2056 * but should either do a listen or a connect soon.
2061 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
2063 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
2068 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
2069 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
2073 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
2074 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
2076 switch (log_in_vain
) {
2078 if(thflags
& TH_SYN
)
2080 "Connection attempt to TCP %s:%d from %s:%d\n",
2081 dbuf
, ntohs(th
->th_dport
),
2083 ntohs(th
->th_sport
));
2087 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
2088 dbuf
, ntohs(th
->th_dport
), sbuf
,
2089 ntohs(th
->th_sport
), thflags
);
2093 if ((thflags
& TH_SYN
) && !(thflags
& TH_ACK
) &&
2094 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
2096 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
2097 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
2099 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
2102 log_in_vain_log((LOG_INFO
,
2103 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
2104 dbuf
, ntohs(th
->th_dport
),
2106 ntohs(th
->th_sport
)));
2113 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
2115 switch (blackhole
) {
2117 if (thflags
& TH_SYN
)
2126 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2127 IF_TCP_STATINC(ifp
, noconnnolist
);
2128 goto dropwithresetnosock
;
2130 so
= inp
->inp_socket
;
2132 /* This case shouldn't happen as the socket shouldn't be null
2133 * if inp_state isn't set to INPCB_STATE_DEAD
2134 * But just in case, we pretend we didn't find the socket if we hit this case
2135 * as this isn't cause for a panic (the socket might be leaked however)...
2139 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp
);
2145 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2146 tcp_unlock(so
, 1, (void *)2);
2147 inp
= NULL
; // pretend we didn't find it
2154 if (!necp_socket_is_allowed_to_send_recv_v6(inp
, th
->th_dport
,
2159 IF_TCP_STATINC(ifp
, badformatipsec
);
2165 if (!necp_socket_is_allowed_to_send_recv_v4(inp
, th
->th_dport
,
2170 IF_TCP_STATINC(ifp
, badformatipsec
);
2176 tp
= intotcpcb(inp
);
2178 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2179 IF_TCP_STATINC(ifp
, noconnlist
);
2182 if (tp
->t_state
== TCPS_CLOSED
)
2185 /* Unscale the window into a 32-bit value. */
2186 if ((thflags
& TH_SYN
) == 0)
2187 tiwin
= th
->th_win
<< tp
->snd_scale
;
2192 if (mac_inpcb_check_deliver(inp
, m
, AF_INET
, SOCK_STREAM
))
2196 /* Avoid processing packets while closing a listen socket */
2197 if (tp
->t_state
== TCPS_LISTEN
&&
2198 (so
->so_options
& SO_ACCEPTCONN
) == 0)
2201 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
2203 if (so
->so_options
& SO_DEBUG
) {
2204 ostate
= tp
->t_state
;
2207 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
2211 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
2215 if (so
->so_options
& SO_ACCEPTCONN
) {
2216 register struct tcpcb
*tp0
= tp
;
2219 struct sockaddr_storage from
;
2221 struct inpcb
*oinp
= sotoinpcb(so
);
2223 struct ifnet
*head_ifscope
;
2224 unsigned int head_nocell
, head_recvanyif
,
2225 head_noexpensive
, head_awdl_unrestricted
;
2227 /* Get listener's bound-to-interface, if any */
2228 head_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2229 inp
->inp_boundifp
: NULL
;
2230 /* Get listener's no-cellular information, if any */
2231 head_nocell
= INP_NO_CELLULAR(inp
);
2232 /* Get listener's recv-any-interface, if any */
2233 head_recvanyif
= (inp
->inp_flags
& INP_RECV_ANYIF
);
2234 /* Get listener's no-expensive information, if any */
2235 head_noexpensive
= INP_NO_EXPENSIVE(inp
);
2236 head_awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
2239 * If the state is LISTEN then ignore segment if it contains an RST.
2240 * If the segment contains an ACK then it is bad and send a RST.
2241 * If it does not contain a SYN then it is not interesting; drop it.
2242 * If it is from this socket, drop it, it must be forged.
2244 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
2245 IF_TCP_STATINC(ifp
, listbadsyn
);
2247 if (thflags
& TH_RST
) {
2250 if (thflags
& TH_ACK
) {
2252 tcpstat
.tcps_badsyn
++;
2253 rstreason
= BANDLIM_RST_OPENPORT
;
2257 /* We come here if there is no SYN set */
2258 tcpstat
.tcps_badsyn
++;
2261 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
2262 if (th
->th_dport
== th
->th_sport
) {
2265 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
2270 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
2274 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2275 * in_broadcast() should never return true on a received
2276 * packet with M_BCAST not set.
2278 * Packets with a multicast source address should also
2281 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
2285 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2286 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
2290 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2291 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2292 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2293 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
2299 * If deprecated address is forbidden,
2300 * we do not accept SYN to deprecated interface
2301 * address to prevent any new inbound connection from
2302 * getting established.
2303 * When we do not accept SYN, we send a TCP RST,
2304 * with deprecated source address (instead of dropping
2305 * it). We compromise it as it is much better for peer
2306 * to send a RST, and RST will be the final packet
2309 * If we do not forbid deprecated addresses, we accept
2310 * the SYN packet. RFC 4862 forbids dropping SYN in
2313 if (isipv6
&& !ip6_use_deprecated
) {
2316 if (ip6_getdstifaddr_info(m
, NULL
,
2318 if (ia6_flags
& IN6_IFF_DEPRECATED
) {
2320 rstreason
= BANDLIM_RST_OPENPORT
;
2321 IF_TCP_STATINC(ifp
, deprecate6
);
2330 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
2332 sin6
->sin6_len
= sizeof(*sin6
);
2333 sin6
->sin6_family
= AF_INET6
;
2334 sin6
->sin6_port
= th
->th_sport
;
2335 sin6
->sin6_flowinfo
= 0;
2336 sin6
->sin6_addr
= ip6
->ip6_src
;
2337 sin6
->sin6_scope_id
= 0;
2342 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
2344 sin
->sin_len
= sizeof(*sin
);
2345 sin
->sin_family
= AF_INET
;
2346 sin
->sin_port
= th
->th_sport
;
2347 sin
->sin_addr
= ip
->ip_src
;
2349 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2351 so2
= sonewconn(so
, 0, NULL
);
2354 tcpstat
.tcps_listendrop
++;
2355 if (tcp_dropdropablreq(so
)) {
2357 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2359 so2
= sonewconn(so
, 0, NULL
);
2365 /* Point "inp" and "tp" in tandem to new socket */
2366 inp
= (struct inpcb
*)so2
->so_pcb
;
2367 tp
= intotcpcb(inp
);
2370 tcp_unlock(so
, 0, 0); /* Unlock but keep a reference on listener for now */
2375 * Mark socket as temporary until we're
2376 * committed to keeping it. The code at
2377 * ``drop'' and ``dropwithreset'' check the
2378 * flag dropsocket to see if the temporary
2379 * socket created here should be discarded.
2380 * We mark the socket as discardable until
2381 * we're committed to it below in TCPS_LISTEN.
2382 * There are some error conditions in which we
2383 * have to drop the temporary socket.
2387 * Inherit INP_BOUND_IF from listener; testing if
2388 * head_ifscope is non-NULL is sufficient, since it
2389 * can only be set to a non-zero value earlier if
2390 * the listener has such a flag set.
2392 if (head_ifscope
!= NULL
) {
2393 inp
->inp_flags
|= INP_BOUND_IF
;
2394 inp
->inp_boundifp
= head_ifscope
;
2396 inp
->inp_flags
&= ~INP_BOUND_IF
;
2399 * Inherit restrictions from listener.
2402 inp_set_nocellular(inp
);
2403 if (head_noexpensive
)
2404 inp_set_noexpensive(inp
);
2405 if (head_awdl_unrestricted
)
2406 inp_set_awdl_unrestricted(inp
);
2408 * Inherit {IN,IN6}_RECV_ANYIF from listener.
2411 inp
->inp_flags
|= INP_RECV_ANYIF
;
2413 inp
->inp_flags
&= ~INP_RECV_ANYIF
;
2416 inp
->in6p_laddr
= ip6
->ip6_dst
;
2418 inp
->inp_vflag
&= ~INP_IPV6
;
2419 inp
->inp_vflag
|= INP_IPV4
;
2421 inp
->inp_laddr
= ip
->ip_dst
;
2425 inp
->inp_lport
= th
->th_dport
;
2426 if (in_pcbinshash(inp
, 0) != 0) {
2428 * Undo the assignments above if we failed to
2429 * put the PCB on the hash lists.
2433 inp
->in6p_laddr
= in6addr_any
;
2436 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
2438 tcp_lock(oso
, 0, 0); /* release ref on parent */
2439 tcp_unlock(oso
, 1, 0);
2445 * Inherit socket options from the listening
2447 * Note that in6p_inputopts are not (even
2448 * should not be) copied, since it stores
2449 * previously received options and is used to
2450 * detect if each new option is different than
2451 * the previous one and hence should be passed
2453 * If we copied in6p_inputopts, a user would
2454 * not be able to receive options just after
2455 * calling the accept system call.
2458 oinp
->inp_flags
& INP_CONTROLOPTS
;
2459 if (oinp
->in6p_outputopts
)
2460 inp
->in6p_outputopts
=
2461 ip6_copypktopts(oinp
->in6p_outputopts
,
2466 inp
->inp_options
= ip_srcroute();
2467 inp
->inp_ip_tos
= oinp
->inp_ip_tos
;
2469 tcp_lock(oso
, 0, 0);
2471 /* copy old policy into new socket's */
2472 if (sotoinpcb(oso
)->inp_sp
)
2475 /* Is it a security hole here to silently fail to copy the policy? */
2476 if (inp
->inp_sp
!= NULL
)
2477 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
2478 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
))
2479 printf("tcp_input: could not copy policy\n");
2482 /* inherit states from the listener */
2483 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2484 struct tcpcb
*, tp
, int32_t, TCPS_LISTEN
);
2485 tp
->t_state
= TCPS_LISTEN
;
2486 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
|TF_NODELAY
);
2487 tp
->t_flagsext
|= (tp0
->t_flagsext
& (TF_RXTFINDROP
|TF_NOTIMEWAIT
|TF_FASTOPEN
));
2488 tp
->t_keepinit
= tp0
->t_keepinit
;
2489 tp
->t_keepcnt
= tp0
->t_keepcnt
;
2490 tp
->t_keepintvl
= tp0
->t_keepintvl
;
2491 tp
->t_adaptive_wtimo
= tp0
->t_adaptive_wtimo
;
2492 tp
->t_adaptive_rtimo
= tp0
->t_adaptive_rtimo
;
2493 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
2494 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0)
2495 tp
->t_notsent_lowat
= tp0
->t_notsent_lowat
;
2497 /* now drop the reference on the listener */
2498 tcp_unlock(oso
, 1, 0);
2500 tcp_set_max_rwinscale(tp
, so
);
2502 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
2505 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2506 LCK_MTX_ASSERT_OWNED
);
2508 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
2510 * Evaluate the rate of arrival of packets to see if the
2511 * receiver can reduce the ack traffic. The algorithm to
2512 * stretch acks will be enabled if the connection meets
2513 * certain criteria defined in tcp_stretch_ack_enable function.
2515 if ((tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) != 0) {
2516 TCP_INC_VAR(tp
->rcv_waitforss
, nlropkts
);
2518 if (tcp_stretch_ack_enable(tp
)) {
2519 tp
->t_flags
|= TF_STRETCHACK
;
2520 tp
->t_flagsext
&= ~(TF_RCVUNACK_WAITSS
);
2521 tp
->rcv_waitforss
= 0;
2523 tp
->t_flags
&= ~(TF_STRETCHACK
);
2525 if (TSTMP_GT(tp
->rcv_unackwin
, tcp_now
)) {
2526 tp
->rcv_by_unackwin
+= (tlen
+ off
);
2528 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
2529 tp
->rcv_by_unackwin
= tlen
+ off
;
2534 * Keep track of how many bytes were received in the LRO packet
2536 if ((pktf_sw_lro_pkt
) && (nlropkts
> 2)) {
2537 tp
->t_lropktlen
+= tlen
;
2540 * Explicit Congestion Notification - Flag that we need to send ECT if
2541 * + The IP Congestion experienced flag was set.
2542 * + Socket is in established state
2543 * + We negotiated ECN in the TCP setup
2544 * + This isn't a pure ack (tlen > 0)
2545 * + The data is in the valid window
2547 * TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
2549 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2550 TCP_ECN_ENABLED(tp
) && tlen
> 0 &&
2551 SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2552 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
2553 tp
->t_ecn_recv_ce
++;
2554 tcpstat
.tcps_ecn_recv_ce
++;
2555 INP_INC_IFNET_STAT(inp
, ecn_recv_ce
);
2556 /* Mark this connection as it received CE from network */
2557 tp
->ecn_flags
|= TE_RECV_ECN_CE
;
2558 tp
->ecn_flags
|= TE_SENDECE
;
2562 * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
2563 * bother doing extensive checks for state and whatnot.
2565 if (thflags
& TH_CWR
) {
2566 tp
->ecn_flags
&= ~TE_SENDECE
;
2567 tp
->t_ecn_recv_cwr
++;
2571 * If we received an explicit notification of congestion in
2572 * ip tos ecn bits or by the CWR bit in TCP header flags, reset
2573 * the ack-strteching state. We need to handle ECN notification if
2574 * an ECN setup SYN was sent even once.
2576 if (tp
->t_state
== TCPS_ESTABLISHED
2577 && (tp
->ecn_flags
& TE_SETUPSENT
)
2578 && (ip_ecn
== IPTOS_ECN_CE
|| (thflags
& TH_CWR
))) {
2579 tcp_reset_stretch_ack(tp
);
2580 CLEAR_IAJ_STATE(tp
);
2583 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2584 !TCP_ECN_ENABLED(tp
) && !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2585 tcpstat
.tcps_ecn_fallback_ce
++;
2586 tcp_heuristic_ecn_aggressive(tp
);
2587 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2590 if (tp
->t_state
== TCPS_ESTABLISHED
&& TCP_ECN_ENABLED(tp
) &&
2591 ip_ecn
== IPTOS_ECN_CE
&& !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2592 if (inp
->inp_stat
->rxpackets
< ECN_MIN_CE_PROBES
) {
2593 tp
->t_ecn_recv_ce_pkt
++;
2594 } else if (tp
->t_ecn_recv_ce_pkt
> ECN_MAX_CE_RATIO
) {
2595 tcpstat
.tcps_ecn_fallback_ce
++;
2596 tcp_heuristic_ecn_aggressive(tp
);
2597 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2598 INP_INC_IFNET_STAT(inp
,ecn_fallback_ce
);
2600 /* We tracked the first ECN_MIN_CE_PROBES segments, we
2601 * now know that the path is good.
2603 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2608 * Try to determine if we are receiving a packet after a long time.
2609 * Use our own approximation of idletime to roughly measure remote
2610 * end's idle time. Since slowstart is used after an idle period
2611 * we want to avoid doing LRO if the remote end is not up to date
2612 * on initial window support and starts with 1 or 2 packets as its IW.
2614 if (sw_lro
&& (tp
->t_flagsext
& TF_LRO_OFFLOADED
) &&
2615 ((tcp_now
- tp
->t_rcvtime
) >= (TCP_IDLETIMEOUT(tp
)))) {
2619 /* Update rcvtime as a new segment was received on the connection */
2620 tp
->t_rcvtime
= tcp_now
;
2623 * Segment received on connection.
2624 * Reset idle time and keep-alive timer.
2626 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
2627 tcp_keepalive_reset(tp
);
2630 * Process options if not in LISTEN state,
2631 * else do it below (after getting remote address).
2633 if (tp
->t_state
!= TCPS_LISTEN
&& optp
) {
2634 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
2636 if (mptcp_input_preproc(tp
, m
, drop_hdrlen
) != 0) {
2637 tp
->t_flags
|= TF_ACKNOW
;
2638 (void) tcp_output(tp
);
2639 tcp_check_timer_state(tp
);
2640 tcp_unlock(so
, 1, 0);
2641 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
|
2642 DBG_FUNC_END
,0,0,0,0,0);
2647 if (tp
->t_state
== TCPS_SYN_SENT
&& (thflags
& TH_SYN
)) {
2648 if (!(thflags
& TH_ACK
) ||
2649 (SEQ_GT(th
->th_ack
, tp
->iss
) &&
2650 SEQ_LEQ(th
->th_ack
, tp
->snd_max
)))
2651 tcp_finalize_options(tp
, &to
, ifscope
);
2656 * Compute inter-packet arrival jitter. According to RFC 3550,
2657 * inter-packet arrival jitter is defined as the difference in
2658 * packet spacing at the receiver compared to the sender for a
2659 * pair of packets. When two packets of maximum segment size come
2660 * one after the other with consecutive sequence numbers, we
2661 * consider them as packets sent together at the sender and use
2662 * them as a pair to compute inter-packet arrival jitter. This
2663 * metric indicates the delay induced by the network components due
2664 * to queuing in edge/access routers.
2666 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2667 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
|TH_PUSH
)) == TH_ACK
&&
2668 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2669 ((to
.to_flags
& TOF_TS
) == 0 ||
2670 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2671 th
->th_seq
== tp
->rcv_nxt
&&
2672 LIST_EMPTY(&tp
->t_segq
)) {
2673 int seg_size
= tlen
;
2674 if (tp
->iaj_pktcnt
<= IAJ_IGNORE_PKTCNT
) {
2675 TCP_INC_VAR(tp
->iaj_pktcnt
, nlropkts
);
2678 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
2679 seg_size
= m
->m_pkthdr
.lro_pktlen
;
2681 if ( tp
->iaj_size
== 0 || seg_size
> tp
->iaj_size
||
2682 (seg_size
== tp
->iaj_size
&& tp
->iaj_rcv_ts
== 0)) {
2684 * State related to inter-arrival jitter is
2685 * uninitialized or we are trying to find a good
2686 * first packet to start computing the metric
2688 update_iaj_state(tp
, seg_size
, 0);
2690 if (seg_size
== tp
->iaj_size
) {
2692 * Compute inter-arrival jitter taking
2693 * this packet as the second packet
2695 if (pktf_sw_lro_pkt
)
2696 compute_iaj(tp
, nlropkts
,
2697 m
->m_pkthdr
.lro_elapsed
);
2699 compute_iaj(tp
, 1, 0);
2701 if (seg_size
< tp
->iaj_size
) {
2703 * There is a smaller packet in the stream.
2704 * Some times the maximum size supported
2705 * on a path can change if there is a new
2706 * link with smaller MTU. The receiver will
2707 * not know about this change. If there
2708 * are too many packets smaller than
2709 * iaj_size, we try to learn the iaj_size
2712 TCP_INC_VAR(tp
->iaj_small_pkt
, nlropkts
);
2713 if (tp
->iaj_small_pkt
> RESET_IAJ_SIZE_THRESH
) {
2714 update_iaj_state(tp
, seg_size
, 1);
2716 CLEAR_IAJ_STATE(tp
);
2719 update_iaj_state(tp
, seg_size
, 0);
2723 CLEAR_IAJ_STATE(tp
);
2725 #endif /* TRAFFIC_MGT */
2728 * Header prediction: check for the two common cases
2729 * of a uni-directional data xfer. If the packet has
2730 * no control flags, is in-sequence, the window didn't
2731 * change and we're not retransmitting, it's a
2732 * candidate. If the length is zero and the ack moved
2733 * forward, we're the sender side of the xfer. Just
2734 * free the data acked & wake any higher level process
2735 * that was blocked waiting for space. If the length
2736 * is non-zero and the ack didn't move, we're the
2737 * receiver side. If we're getting packets in-order
2738 * (the reassembly queue is empty), add the data to
2739 * the socket buffer and note that we need a delayed ack.
2740 * Make sure that the hidden state-flags are also off.
2741 * Since we check for TCPS_ESTABLISHED above, it can only
2744 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2745 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
|TH_CWR
)) == TH_ACK
&&
2746 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2747 ((to
.to_flags
& TOF_TS
) == 0 ||
2748 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2749 th
->th_seq
== tp
->rcv_nxt
&&
2750 tiwin
&& tiwin
== tp
->snd_wnd
&&
2751 tp
->snd_nxt
== tp
->snd_max
) {
2754 * If last ACK falls within this segment's sequence numbers,
2755 * record the timestamp.
2756 * NOTE that the test is modified according to the latest
2757 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2759 if ((to
.to_flags
& TOF_TS
) != 0 &&
2760 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
2761 tp
->ts_recent_age
= tcp_now
;
2762 tp
->ts_recent
= to
.to_tsval
;
2766 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
2767 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
2768 tp
->snd_cwnd
>= tp
->snd_ssthresh
&&
2769 (!IN_FASTRECOVERY(tp
) &&
2770 ((!(SACK_ENABLED(tp
)) &&
2771 tp
->t_dupacks
< tp
->t_rexmtthresh
) ||
2772 (SACK_ENABLED(tp
) && to
.to_nsacks
== 0 &&
2773 TAILQ_EMPTY(&tp
->snd_holes
))))) {
2775 * this is a pure ack for outstanding data.
2777 ++tcpstat
.tcps_predack
;
2779 tcp_bad_rexmt_check(tp
, th
, &to
),
2781 /* Recalculate the RTT */
2782 tcp_compute_rtt(tp
, &to
, th
);
2784 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
2785 acked
= BYTES_ACKED(th
, tp
);
2786 tcpstat
.tcps_rcvackpack
++;
2787 tcpstat
.tcps_rcvackbyte
+= acked
;
2790 * Handle an ack that is in sequence during
2791 * congestion avoidance phase. The
2792 * calculations in this function
2793 * assume that snd_una is not updated yet.
2795 if (CC_ALGO(tp
)->congestion_avd
!= NULL
)
2796 CC_ALGO(tp
)->congestion_avd(tp
, th
);
2797 tcp_ccdbg_trace(tp
, th
, TCP_CC_INSEQ_ACK_RCVD
);
2798 sbdrop(&so
->so_snd
, acked
);
2799 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2800 VERIFY(acked
<= so
->so_msg_state
->msg_serial_bytes
);
2801 so
->so_msg_state
->msg_serial_bytes
-= acked
;
2803 tcp_sbsnd_trim(&so
->so_snd
);
2805 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
2806 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
2807 tp
->snd_recover
= th
->th_ack
- 1;
2808 tp
->snd_una
= th
->th_ack
;
2811 * pull snd_wl2 up to prevent seq wrap relative
2814 tp
->snd_wl2
= th
->th_ack
;
2816 if (tp
->t_dupacks
> 0) {
2818 tp
->t_rexmtthresh
= tcprexmtthresh
;
2824 * If all outstanding data are acked, stop
2825 * retransmit timer, otherwise restart timer
2826 * using current (possibly backed-off) value.
2827 * If process is waiting for space,
2828 * wakeup/selwakeup/signal. If data
2829 * are ready to send, let tcp_output
2830 * decide between more output or persist.
2832 if (tp
->snd_una
== tp
->snd_max
) {
2833 tp
->t_timer
[TCPT_REXMT
] = 0;
2834 tp
->t_timer
[TCPT_PTO
] = 0;
2835 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
2836 tp
->t_timer
[TCPT_REXMT
] =
2837 OFFSET_FROM_START(tp
,
2840 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
2841 !TCP_DSACK_SEQ_IN_WINDOW(tp
,
2842 tp
->t_dsack_lastuna
, tp
->snd_una
))
2843 tcp_rxtseg_clean(tp
);
2845 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
2846 tp
->t_bwmeas
!= NULL
)
2847 tcp_bwmeas_check(tp
);
2848 sowwakeup(so
); /* has to be done with socket lock held */
2849 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
)) {
2850 (void) tcp_output(tp
);
2853 tcp_tfo_rcv_ack(tp
, th
);
2855 tcp_check_timer_state(tp
);
2856 tcp_unlock(so
, 1, 0);
2857 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2860 } else if (th
->th_ack
== tp
->snd_una
&&
2861 LIST_EMPTY(&tp
->t_segq
) &&
2862 tlen
<= tcp_sbspace(tp
)) {
2864 * this is a pure, in-sequence data packet
2865 * with nothing on the reassembly queue and
2866 * we have enough buffer space to take it.
2870 * If this is a connection in steady state, start
2871 * coalescing packets belonging to this flow.
2874 tcp_lro_remove_state(tp
->t_inpcb
->inp_laddr
,
2875 tp
->t_inpcb
->inp_faddr
,
2876 tp
->t_inpcb
->inp_lport
,
2877 tp
->t_inpcb
->inp_fport
);
2878 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
2879 tp
->t_idleat
= tp
->rcv_nxt
;
2880 } else if (sw_lro
&& !pktf_sw_lro_pkt
&& !isipv6
&&
2881 (so
->so_flags
& SOF_USELRO
) &&
2882 !IFNET_IS_CELLULAR(m
->m_pkthdr
.rcvif
) &&
2883 (m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) &&
2884 ((th
->th_seq
- tp
->irs
) >
2885 (tp
->t_maxseg
<< lro_start
)) &&
2886 ((tp
->t_idleat
== 0) || ((th
->th_seq
-
2887 tp
->t_idleat
) > (tp
->t_maxseg
<< lro_start
)))) {
2888 tp
->t_flagsext
|= TF_LRO_OFFLOADED
;
2889 tcp_start_coalescing(ip
, th
, tlen
);
2893 /* Clean receiver SACK report if present */
2894 if (SACK_ENABLED(tp
) && tp
->rcv_numsacks
)
2895 tcp_clean_sackreport(tp
);
2896 ++tcpstat
.tcps_preddat
;
2897 tp
->rcv_nxt
+= tlen
;
2899 * Pull snd_wl1 up to prevent seq wrap relative to
2902 tp
->snd_wl1
= th
->th_seq
;
2904 * Pull rcv_up up to prevent seq wrap relative to
2907 tp
->rcv_up
= tp
->rcv_nxt
;
2908 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
2909 tcpstat
.tcps_rcvbyte
+= tlen
;
2910 if (nstat_collect
) {
2911 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
2912 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2913 rxpackets
, m
->m_pkthdr
.lro_npkts
);
2915 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2918 INP_ADD_STAT(inp
, cell
, wifi
, wired
,rxbytes
,
2923 * Calculate the RTT on the receiver only if the
2924 * connection is in streaming mode and the last
2925 * packet was not an end-of-write
2927 if ((tp
->t_flags
& TF_STRETCHACK
) &&
2928 !(tp
->t_flagsext
& TF_STREAMEOW
))
2929 tcp_compute_rtt(tp
, &to
, th
);
2931 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
);
2934 * Add data to socket buffer.
2936 so_recv_data_stat(so
, m
, 0);
2937 m_adj(m
, drop_hdrlen
); /* delayed header drop */
2940 * If message delivery (SOF_ENABLE_MSGS) is enabled on
2941 * this socket, deliver the packet received as an
2942 * in-order message with sequence number attached to it.
2944 if (sbappendstream_rcvdemux(so
, m
,
2945 th
->th_seq
- (tp
->irs
+ 1), 0)) {
2950 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2951 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
2952 th
->th_seq
, th
->th_ack
, th
->th_win
);
2957 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2958 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
2959 th
->th_seq
, th
->th_ack
, th
->th_win
);
2961 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
2962 if (DELAY_ACK(tp
, th
)) {
2963 if ((tp
->t_flags
& TF_DELACK
) == 0) {
2964 tp
->t_flags
|= TF_DELACK
;
2965 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
2968 tp
->t_flags
|= TF_ACKNOW
;
2972 tcp_adaptive_rwtimo_check(tp
, tlen
);
2975 tcp_tfo_rcv_data(tp
);
2977 tcp_check_timer_state(tp
);
2978 tcp_unlock(so
, 1, 0);
2979 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2985 * Calculate amount of space in receive window,
2986 * and then do TCP input processing.
2987 * Receive window is amount of space in rcv queue,
2988 * but not less than advertised window.
2990 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2991 LCK_MTX_ASSERT_OWNED
);
2992 win
= tcp_sbspace(tp
);
2995 else { /* clip rcv window to 4K for modems */
2996 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
2997 win
= min(win
, slowlink_wsize
);
2999 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
3002 * Ensure that the subflow receive window isn't greater
3003 * than the connection level receive window.
3005 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
3006 (mp_tp
= tptomptp(tp
))) {
3008 if (tp
->rcv_wnd
> mp_tp
->mpt_rcvwnd
) {
3009 tp
->rcv_wnd
= mp_tp
->mpt_rcvwnd
;
3010 tcpstat
.tcps_mp_reducedwin
++;
3016 switch (tp
->t_state
) {
3019 * Initialize tp->rcv_nxt, and tp->irs, select an initial
3020 * tp->iss, and send a segment:
3021 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3022 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3023 * Fill in remote peer address fields if not previously specified.
3024 * Enter SYN_RECEIVED state, and process any other fields of this
3025 * segment in this state.
3028 register struct sockaddr_in
*sin
;
3030 register struct sockaddr_in6
*sin6
;
3033 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
3034 LCK_MTX_ASSERT_OWNED
);
3037 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
3038 M_SONAME
, M_NOWAIT
);
3041 bzero(sin6
, sizeof(*sin6
));
3042 sin6
->sin6_family
= AF_INET6
;
3043 sin6
->sin6_len
= sizeof(*sin6
);
3044 sin6
->sin6_addr
= ip6
->ip6_src
;
3045 sin6
->sin6_port
= th
->th_sport
;
3046 laddr6
= inp
->in6p_laddr
;
3047 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
3048 inp
->in6p_laddr
= ip6
->ip6_dst
;
3049 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
3051 inp
->in6p_laddr
= laddr6
;
3052 FREE(sin6
, M_SONAME
);
3055 FREE(sin6
, M_SONAME
);
3060 &((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
3061 LCK_MTX_ASSERT_OWNED
);
3062 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
3066 sin
->sin_family
= AF_INET
;
3067 sin
->sin_len
= sizeof(*sin
);
3068 sin
->sin_addr
= ip
->ip_src
;
3069 sin
->sin_port
= th
->th_sport
;
3070 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
3071 laddr
= inp
->inp_laddr
;
3072 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
3073 inp
->inp_laddr
= ip
->ip_dst
;
3074 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
,
3075 IFSCOPE_NONE
, NULL
)) {
3076 inp
->inp_laddr
= laddr
;
3077 FREE(sin
, M_SONAME
);
3080 FREE(sin
, M_SONAME
);
3083 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
3084 tcp_finalize_options(tp
, &to
, ifscope
);
3086 if (tfo_enabled(tp
) && tcp_tfo_syn(tp
, &to
))
3092 tp
->iss
= tcp_new_isn(tp
);
3094 tp
->irs
= th
->th_seq
;
3095 tcp_sendseqinit(tp
);
3097 tp
->snd_recover
= tp
->snd_una
;
3099 * Initialization of the tcpcb for transaction;
3100 * set SND.WND = SEG.WND,
3101 * initialize CCsend and CCrecv.
3103 tp
->snd_wnd
= tiwin
; /* initial send-window */
3104 tp
->t_flags
|= TF_ACKNOW
;
3105 tp
->t_unacksegs
= 0;
3106 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3107 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3108 tp
->t_state
= TCPS_SYN_RECEIVED
;
3109 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3110 TCP_CONN_KEEPINIT(tp
));
3111 dropsocket
= 0; /* committed to socket */
3113 if (inp
->inp_flowhash
== 0)
3114 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
3116 /* update flowinfo - RFC 6437 */
3117 if (inp
->inp_flow
== 0 &&
3118 inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
3119 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
3121 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
3125 /* reset the incomp processing flag */
3126 so
->so_flags
&= ~(SOF_INCOMP_INPROGRESS
);
3127 tcpstat
.tcps_accepts
++;
3128 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
| TH_CWR
)) {
3130 tp
->ecn_flags
|= (TE_SETUPRECEIVED
| TE_SENDIPECT
);
3133 #if CONFIG_IFEF_NOWINDOWSCALE
3134 if (tcp_obey_ifef_nowindowscale
&& m
->m_pkthdr
.rcvif
!= NULL
&&
3135 (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_NOWINDOWSCALE
)) {
3136 /* Window scaling is not enabled on this interface */
3137 tp
->t_flags
&= ~TF_REQ_SCALE
;
3144 * If the state is SYN_RECEIVED and the seg contains an ACK,
3145 * but not for our SYN/ACK, send a RST.
3147 case TCPS_SYN_RECEIVED
:
3148 if ((thflags
& TH_ACK
) &&
3149 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
3150 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3151 rstreason
= BANDLIM_RST_OPENPORT
;
3152 IF_TCP_STATINC(ifp
, ooopacket
);
3157 * In SYN_RECEIVED state, if we recv some SYNS with
3158 * window scale and others without, window scaling should
3159 * be disabled. Otherwise the window advertised will be
3160 * lower if we assume scaling and the other end does not.
3162 if ((thflags
& TH_SYN
) &&
3163 (tp
->irs
== th
->th_seq
) &&
3164 !(to
.to_flags
& TOF_SCALE
))
3165 tp
->t_flags
&= ~TF_RCVD_SCALE
;
3169 * If the state is SYN_SENT:
3170 * if seg contains an ACK, but not for our SYN, drop the input.
3171 * if seg contains a RST, then drop the connection.
3172 * if seg does not contain SYN, then drop it.
3173 * Otherwise this is an acceptable SYN segment
3174 * initialize tp->rcv_nxt and tp->irs
3175 * if seg contains ack then advance tp->snd_una
3176 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
3177 * arrange for segment to be acked (eventually)
3178 * continue processing rest of data/controls, beginning with URG
3181 if ((thflags
& TH_ACK
) &&
3182 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
3183 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3184 rstreason
= BANDLIM_UNLIMITED
;
3185 IF_TCP_STATINC(ifp
, ooopacket
);
3188 if (thflags
& TH_RST
) {
3189 if ((thflags
& TH_ACK
) != 0) {
3191 if ((so
->so_flags
& SOF_MPTCP_FASTJOIN
) &&
3192 SEQ_GT(th
->th_ack
, tp
->iss
+1)) {
3193 so
->so_flags
&= ~SOF_MPTCP_FASTJOIN
;
3194 /* ignore the RST and retransmit SYN */
3199 (SO_FILT_HINT_LOCKED
|
3200 SO_FILT_HINT_CONNRESET
));
3201 tp
= tcp_drop(tp
, ECONNREFUSED
);
3202 postevent(so
, 0, EV_RESET
);
3206 if ((thflags
& TH_SYN
) == 0)
3208 tp
->snd_wnd
= th
->th_win
; /* initial send window */
3210 tp
->irs
= th
->th_seq
;
3212 if (thflags
& TH_ACK
) {
3213 tcpstat
.tcps_connects
++;
3215 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
)) {
3216 /* ECN-setup SYN-ACK */
3217 tp
->ecn_flags
|= TE_SETUPRECEIVED
;
3218 if (TCP_ECN_ENABLED(tp
)) {
3219 tcp_heuristic_reset_loss(tp
, 0, 1);
3220 tcpstat
.tcps_ecn_client_success
++;
3223 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3224 tp
->t_rxtshift
== 0) {
3225 tcp_heuristic_reset_loss(tp
, 0, 1);
3226 tcpstat
.tcps_ecn_not_supported
++;
3228 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3230 tcp_heuristic_inc_loss(tp
, 0, 1);
3232 /* non-ECN-setup SYN-ACK */
3233 tp
->ecn_flags
&= ~TE_SENDIPECT
;
3236 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
3237 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
3238 mac_socketpeer_label_associate_mbuf(m
, so
);
3239 /* XXXMAC: SOCK_UNLOCK(so); */
3241 /* Do window scaling on this connection? */
3242 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3243 tp
->snd_scale
= tp
->requested_s_scale
;
3244 tp
->rcv_scale
= tp
->request_r_scale
;
3247 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
<< tp
->rcv_scale
);
3248 tp
->snd_una
++; /* SYN is acked */
3249 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
3250 tp
->snd_nxt
= tp
->snd_una
;
3253 * We have sent more in the SYN than what is being
3254 * acked. (e.g., TFO)
3255 * We should restart the sending from what the receiver
3256 * has acknowledged immediately.
3258 if (SEQ_GT(tp
->snd_nxt
, th
->th_ack
))
3259 tp
->snd_max
= tp
->snd_nxt
= th
->th_ack
;
3262 * If there's data, delay ACK; if there's also a FIN
3263 * ACKNOW will be turned on later.
3265 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3266 if (DELAY_ACK(tp
, th
) && tlen
!= 0 ) {
3267 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3268 tp
->t_flags
|= TF_DELACK
;
3269 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3273 tp
->t_flags
|= TF_ACKNOW
;
3276 * Received <SYN,ACK> in SYN_SENT[*] state.
3278 * SYN_SENT --> ESTABLISHED
3279 * SYN_SENT* --> FIN_WAIT_1
3281 tp
->t_starttime
= tcp_now
;
3282 tcp_sbrcv_tstmp_check(tp
);
3283 if (tp
->t_flags
& TF_NEEDFIN
) {
3284 DTRACE_TCP4(state__change
, void, NULL
,
3285 struct inpcb
*, inp
,
3286 struct tcpcb
*, tp
, int32_t,
3288 tp
->t_state
= TCPS_FIN_WAIT_1
;
3289 tp
->t_flags
&= ~TF_NEEDFIN
;
3292 DTRACE_TCP4(state__change
, void, NULL
,
3293 struct inpcb
*, inp
, struct tcpcb
*,
3294 tp
, int32_t, TCPS_ESTABLISHED
);
3295 tp
->t_state
= TCPS_ESTABLISHED
;
3296 tp
->t_timer
[TCPT_KEEP
] =
3297 OFFSET_FROM_START(tp
,
3298 TCP_CONN_KEEPIDLE(tp
));
3300 nstat_route_connect_success(
3301 tp
->t_inpcb
->inp_route
.ro_rt
);
3305 * Do not send the connect notification for additional
3306 * subflows until ACK for 3-way handshake arrives.
3308 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3309 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3310 isconnected
= FALSE
;
3311 /* Start data xmit if fastjoin */
3312 if (mptcp_fastjoin
&& (so
->so_flags
& SOF_MPTCP_FASTJOIN
)) {
3313 soevent(so
, (SO_FILT_HINT_LOCKED
|
3314 SO_FILT_HINT_MPFASTJ
));
3320 if (tp
->t_tfo_flags
& (TFO_F_COOKIE_REQ
| TFO_F_COOKIE_SENT
)) {
3321 tcp_tfo_synack(tp
, &to
);
3323 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3324 SEQ_LT(tp
->snd_una
, th
->th_ack
)) {
3325 tp
->t_tfo_stats
|= TFO_S_SYN_DATA_ACKED
;
3326 tcpstat
.tcps_tfo_syn_data_acked
++;
3328 if (so
->so_flags
& SOF_MP_SUBFLOW
)
3329 so
->so_flags1
|= SOF1_TFO_REWIND
;
3331 if (!(tp
->t_tfo_flags
& TFO_F_NO_RCVPROBING
))
3332 tcp_tfo_rcv_probe(tp
, tlen
);
3337 * Received initial SYN in SYN-SENT[*] state => simul-
3338 * taneous open. If segment contains CC option and there is
3339 * a cached CC, apply TAO test; if it succeeds, connection is
3340 * half-synchronized. Otherwise, do 3-way handshake:
3341 * SYN-SENT -> SYN-RECEIVED
3342 * SYN-SENT* -> SYN-RECEIVED*
3344 tp
->t_flags
|= TF_ACKNOW
;
3345 tp
->t_timer
[TCPT_REXMT
] = 0;
3346 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3347 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3348 tp
->t_state
= TCPS_SYN_RECEIVED
;
3351 * During simultaneous open, TFO should not be used.
3352 * So, we disable it here, to prevent that data gets
3353 * sent on the SYN/ACK.
3355 tcp_disable_tfo(tp
);
3360 * Advance th->th_seq to correspond to first data byte.
3361 * If data, trim to stay within window,
3362 * dropping FIN if necessary.
3365 if (tlen
> tp
->rcv_wnd
) {
3366 todrop
= tlen
- tp
->rcv_wnd
;
3370 tcpstat
.tcps_rcvpackafterwin
++;
3371 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3373 tp
->snd_wl1
= th
->th_seq
- 1;
3374 tp
->rcv_up
= th
->th_seq
;
3376 * Client side of transaction: already sent SYN and data.
3377 * If the remote host used T/TCP to validate the SYN,
3378 * our data will be ACK'd; if so, enter normal data segment
3379 * processing in the middle of step 5, ack processing.
3380 * Otherwise, goto step 6.
3382 if (thflags
& TH_ACK
)
3386 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
3387 * do normal processing.
3389 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
3393 case TCPS_TIME_WAIT
:
3394 break; /* continue normal processing */
3396 /* Received a SYN while connection is already established.
3397 * This is a "half open connection and other anomalies" described
3398 * in RFC793 page 34, send an ACK so the remote reset the connection
3399 * or recovers by adjusting its sequence numberering
3401 case TCPS_ESTABLISHED
:
3402 if (thflags
& TH_SYN
)
3408 * States other than LISTEN or SYN_SENT.
3409 * First check the RST flag and sequence number since reset segments
3410 * are exempt from the timestamp and connection count tests. This
3411 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
3412 * below which allowed reset segments in half the sequence space
3413 * to fall though and be processed (which gives forged reset
3414 * segments with a random sequence number a 50 percent chance of
3415 * killing a connection).
3416 * Then check timestamp, if present.
3417 * Then check the connection count, if present.
3418 * Then check that at least some bytes of segment are within
3419 * receive window. If segment begins before rcv_nxt,
3420 * drop leading data (and SYN); if nothing left, just ack.
3423 * If the RST bit is set, check the sequence number to see
3424 * if this is a valid reset segment.
3426 * In all states except SYN-SENT, all reset (RST) segments
3427 * are validated by checking their SEQ-fields. A reset is
3428 * valid if its sequence number is in the window.
3429 * Note: this does not take into account delayed ACKs, so
3430 * we should test against last_ack_sent instead of rcv_nxt.
3431 * The sequence number in the reset segment is normally an
3432 * echo of our outgoing acknowlegement numbers, but some hosts
3433 * send a reset with the sequence number at the rightmost edge
3434 * of our receive window, and we have to handle this case.
3435 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
3436 * that brute force RST attacks are possible. To combat this,
3437 * we use a much stricter check while in the ESTABLISHED state,
3438 * only accepting RSTs where the sequence number is equal to
3439 * last_ack_sent. In all other states (the states in which a
3440 * RST is more likely), the more permissive check is used.
3441 * If we have multiple segments in flight, the intial reset
3442 * segment sequence numbers will be to the left of last_ack_sent,
3443 * but they will eventually catch up.
3444 * In any case, it never made sense to trim reset segments to
3445 * fit the receive window since RFC 1122 says:
3446 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
3448 * A TCP SHOULD allow a received RST segment to include data.
3451 * It has been suggested that a RST segment could contain
3452 * ASCII text that encoded and explained the cause of the
3453 * RST. No standard has yet been established for such
3456 * If the reset segment passes the sequence number test examine
3458 * SYN_RECEIVED STATE:
3459 * If passive open, return to LISTEN state.
3460 * If active open, inform user that connection was refused.
3461 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
3462 * Inform user that connection was reset, and close tcb.
3463 * CLOSING, LAST_ACK STATES:
3466 * Drop the segment - see Stevens, vol. 2, p. 964 and
3469 * Radar 4803931: Allows for the case where we ACKed the FIN but
3470 * there is already a RST in flight from the peer.
3471 * In that case, accept the RST for non-established
3472 * state if it's one off from last_ack_sent.
3475 if (thflags
& TH_RST
) {
3476 if ((SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3477 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) ||
3478 (tp
->rcv_wnd
== 0 &&
3479 ((tp
->last_ack_sent
== th
->th_seq
) ||
3480 ((tp
->last_ack_sent
-1) == th
->th_seq
)))) {
3481 switch (tp
->t_state
) {
3483 case TCPS_SYN_RECEIVED
:
3484 IF_TCP_STATINC(ifp
, rstinsynrcv
);
3485 so
->so_error
= ECONNREFUSED
;
3488 case TCPS_ESTABLISHED
:
3489 if (tp
->last_ack_sent
!= th
->th_seq
) {
3490 tcpstat
.tcps_badrst
++;
3493 case TCPS_FIN_WAIT_1
:
3494 case TCPS_CLOSE_WAIT
:
3498 case TCPS_FIN_WAIT_2
:
3499 so
->so_error
= ECONNRESET
;
3501 postevent(so
, 0, EV_RESET
);
3503 (SO_FILT_HINT_LOCKED
|
3504 SO_FILT_HINT_CONNRESET
));
3506 tcpstat
.tcps_drops
++;
3515 case TCPS_TIME_WAIT
:
3523 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3524 * and it's less than ts_recent, drop it.
3526 if ((to
.to_flags
& TOF_TS
) != 0 && tp
->ts_recent
&&
3527 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
3529 /* Check to see if ts_recent is over 24 days old. */
3530 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
3532 * Invalidate ts_recent. If this segment updates
3533 * ts_recent, the age will be reset later and ts_recent
3534 * will get a valid value. If it does not, setting
3535 * ts_recent to zero will at least satisfy the
3536 * requirement that zero be placed in the timestamp
3537 * echo reply when ts_recent isn't valid. The
3538 * age isn't reset until we get a valid ts_recent
3539 * because we don't want out-of-order segments to be
3540 * dropped when ts_recent is old.
3544 tcpstat
.tcps_rcvduppack
++;
3545 tcpstat
.tcps_rcvdupbyte
+= tlen
;
3547 tcpstat
.tcps_pawsdrop
++;
3550 * PAWS-drop when ECN is being used? That indicates
3551 * that ECT-marked packets take a different path, with
3552 * different congestion-characteristics.
3554 * Only fallback when we did send less than 2GB as PAWS
3555 * really has no reason to kick in earlier.
3557 if (TCP_ECN_ENABLED(tp
) &&
3558 inp
->inp_stat
->rxbytes
< 2147483648) {
3559 INP_INC_IFNET_STAT(inp
, ecn_fallback_reorder
);
3560 tcpstat
.tcps_ecn_fallback_reorder
++;
3561 tcp_heuristic_ecn_aggressive(tp
);
3564 if (nstat_collect
) {
3565 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
,
3566 1, tlen
, NSTAT_RX_FLAG_DUPLICATE
);
3567 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3569 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3571 tp
->t_stat
.rxduplicatebytes
+= tlen
;
3580 * In the SYN-RECEIVED state, validate that the packet belongs to
3581 * this connection before trimming the data to fit the receive
3582 * window. Check the sequence number versus IRS since we know
3583 * the sequence numbers haven't wrapped. This is a partial fix
3584 * for the "LAND" DoS attack.
3586 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
3587 rstreason
= BANDLIM_RST_OPENPORT
;
3588 IF_TCP_STATINC(ifp
, dospacket
);
3592 todrop
= tp
->rcv_nxt
- th
->th_seq
;
3594 if (thflags
& TH_SYN
) {
3604 * Following if statement from Stevens, vol. 2, p. 960.
3607 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
3609 * Any valid FIN must be to the left of the window.
3610 * At this point the FIN must be a duplicate or out
3611 * of sequence; drop it.
3616 * Send an ACK to resynchronize and drop any data.
3617 * But keep on processing for RST or ACK.
3619 tp
->t_flags
|= TF_ACKNOW
;
3621 /* This could be a keepalive */
3622 soevent(so
, SO_FILT_HINT_LOCKED
|
3623 SO_FILT_HINT_KEEPALIVE
);
3626 tcpstat
.tcps_rcvduppack
++;
3627 tcpstat
.tcps_rcvdupbyte
+= todrop
;
3629 tcpstat
.tcps_rcvpartduppack
++;
3630 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
3633 if (TCP_DSACK_ENABLED(tp
) && todrop
> 1) {
3635 * Note the duplicate data sequence space so that
3636 * it can be reported in DSACK option.
3638 tp
->t_dsack_lseq
= th
->th_seq
;
3639 tp
->t_dsack_rseq
= th
->th_seq
+ todrop
;
3640 tp
->t_flags
|= TF_ACKNOW
;
3642 if (nstat_collect
) {
3643 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
, 1,
3644 todrop
, NSTAT_RX_FLAG_DUPLICATE
);
3645 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
3646 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, todrop
);
3647 tp
->t_stat
.rxduplicatebytes
+= todrop
;
3649 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
3650 th
->th_seq
+= todrop
;
3652 if (th
->th_urp
> todrop
)
3653 th
->th_urp
-= todrop
;
3661 * If new data are received on a connection after the user
3662 * processes are gone, then RST the other end.
3663 * Send also a RST when we received a data segment after we've
3664 * sent our FIN when the socket is defunct.
3665 * Note that an MPTCP subflow socket would have SS_NOFDREF set
3666 * by default so check to make sure that we test for SOF_MP_SUBFLOW
3667 * socket flag (which would be cleared when the socket is closed.)
3669 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && tlen
&&
3670 (((so
->so_state
& SS_NOFDREF
) &&
3671 tp
->t_state
> TCPS_CLOSE_WAIT
) ||
3672 ((so
->so_flags
& SOF_DEFUNCT
) &&
3673 tp
->t_state
> TCPS_FIN_WAIT_1
))) {
3675 tcpstat
.tcps_rcvafterclose
++;
3676 rstreason
= BANDLIM_UNLIMITED
;
3677 IF_TCP_STATINC(ifp
, cleanup
);
3682 * If segment ends after window, drop trailing data
3683 * (and PUSH and FIN); if nothing left, just ACK.
3685 todrop
= (th
->th_seq
+tlen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
3687 tcpstat
.tcps_rcvpackafterwin
++;
3688 if (todrop
>= tlen
) {
3689 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
3691 * If a new connection request is received
3692 * while in TIME_WAIT, drop the old connection
3693 * and start over if the sequence numbers
3694 * are above the previous ones.
3696 if (thflags
& TH_SYN
&&
3697 tp
->t_state
== TCPS_TIME_WAIT
&&
3698 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
3699 iss
= tcp_new_isn(tp
);
3701 tcp_unlock(so
, 1, 0);
3705 * If window is closed can only take segments at
3706 * window edge, and have to drop data and PUSH from
3707 * incoming segments. Continue processing, but
3708 * remember to ack. Otherwise, drop segment
3711 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
3712 tp
->t_flags
|= TF_ACKNOW
;
3713 tcpstat
.tcps_rcvwinprobe
++;
3717 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3720 thflags
&= ~(TH_PUSH
|TH_FIN
);
3724 * If last ACK falls within this segment's sequence numbers,
3725 * record its timestamp.
3727 * 1) That the test incorporates suggestions from the latest
3728 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
3729 * 2) That updating only on newer timestamps interferes with
3730 * our earlier PAWS tests, so this check should be solely
3731 * predicated on the sequence space of this segment.
3732 * 3) That we modify the segment boundary check to be
3733 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
3734 * instead of RFC1323's
3735 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
3736 * This modified check allows us to overcome RFC1323's
3737 * limitations as described in Stevens TCP/IP Illustrated
3738 * Vol. 2 p.869. In such cases, we can still calculate the
3739 * RTT correctly when RCV.NXT == Last.ACK.Sent.
3741 if ((to
.to_flags
& TOF_TS
) != 0 &&
3742 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3743 SEQ_LEQ(tp
->last_ack_sent
, th
->th_seq
+ tlen
+
3744 ((thflags
& (TH_SYN
|TH_FIN
)) != 0))) {
3745 tp
->ts_recent_age
= tcp_now
;
3746 tp
->ts_recent
= to
.to_tsval
;
3750 * If a SYN is in the window, then this is an
3751 * error and we send an RST and drop the connection.
3753 if (thflags
& TH_SYN
) {
3754 tp
= tcp_drop(tp
, ECONNRESET
);
3755 rstreason
= BANDLIM_UNLIMITED
;
3756 postevent(so
, 0, EV_RESET
);
3757 IF_TCP_STATINC(ifp
, synwindow
);
3762 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
3763 * flag is on (half-synchronized state), then queue data for
3764 * later processing; else drop segment and return.
3766 if ((thflags
& TH_ACK
) == 0) {
3767 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
3768 (tp
->t_flags
& TF_NEEDSYN
)) {
3769 if ((tfo_enabled(tp
))) {
3771 * So, we received a valid segment while in
3772 * SYN-RECEIVED (TF_NEEDSYN is actually never
3773 * set, so this is dead code).
3774 * As this cannot be an RST (see that if a bit
3775 * higher), and it does not have the ACK-flag
3776 * set, we want to retransmit the SYN/ACK.
3777 * Thus, we have to reset snd_nxt to snd_una to
3778 * trigger the going back to sending of the
3779 * SYN/ACK. This is more consistent with the
3780 * behavior of tcp_output(), which expects
3781 * to send the segment that is pointed to by
3784 tp
->snd_nxt
= tp
->snd_una
;
3787 * We need to make absolutely sure that we are
3788 * going to reply upon a duplicate SYN-segment.
3790 if (th
->th_flags
& TH_SYN
)
3795 } else if (tp
->t_flags
& TF_ACKNOW
)
3805 switch (tp
->t_state
) {
3808 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
3809 * ESTABLISHED state and continue processing.
3810 * The ACK was checked above.
3812 case TCPS_SYN_RECEIVED
:
3814 tcpstat
.tcps_connects
++;
3816 /* Do window scaling? */
3817 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3818 tp
->snd_scale
= tp
->requested_s_scale
;
3819 tp
->rcv_scale
= tp
->request_r_scale
;
3820 tp
->snd_wnd
= th
->th_win
<< tp
->snd_scale
;
3821 tiwin
= tp
->snd_wnd
;
3825 * SYN-RECEIVED -> ESTABLISHED
3826 * SYN-RECEIVED* -> FIN-WAIT-1
3828 tp
->t_starttime
= tcp_now
;
3829 tcp_sbrcv_tstmp_check(tp
);
3830 if (tp
->t_flags
& TF_NEEDFIN
) {
3831 DTRACE_TCP4(state__change
, void, NULL
,
3832 struct inpcb
*, inp
,
3833 struct tcpcb
*, tp
, int32_t, TCPS_FIN_WAIT_1
);
3834 tp
->t_state
= TCPS_FIN_WAIT_1
;
3835 tp
->t_flags
&= ~TF_NEEDFIN
;
3837 DTRACE_TCP4(state__change
, void, NULL
,
3838 struct inpcb
*, inp
,
3839 struct tcpcb
*, tp
, int32_t, TCPS_ESTABLISHED
);
3840 tp
->t_state
= TCPS_ESTABLISHED
;
3841 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3842 TCP_CONN_KEEPIDLE(tp
));
3844 nstat_route_connect_success(
3845 tp
->t_inpcb
->inp_route
.ro_rt
);
3848 * If segment contains data or ACK, will call tcp_reass()
3849 * later; if not, do so now to pass queued data to user.
3851 if (tlen
== 0 && (thflags
& TH_FIN
) == 0)
3852 (void) tcp_reass(tp
, (struct tcphdr
*)0, &tlen
,
3854 tp
->snd_wl1
= th
->th_seq
- 1;
3858 * Do not send the connect notification for additional subflows
3859 * until ACK for 3-way handshake arrives.
3861 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3862 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3863 isconnected
= FALSE
;
3867 if ((tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)) {
3868 /* Done this when receiving the SYN */
3869 isconnected
= FALSE
;
3871 OSDecrementAtomic(&tcp_tfo_halfcnt
);
3873 /* Panic if something has gone terribly wrong. */
3874 VERIFY(tcp_tfo_halfcnt
>= 0);
3876 tp
->t_tfo_flags
&= ~TFO_F_COOKIE_VALID
;
3880 * In case there is data in the send-queue (e.g., TFO is being
3881 * used, or connectx+data has been done), then if we would
3882 * "FALLTHROUGH", we would handle this ACK as if data has been
3883 * acknowledged. But, we have to prevent this. And this
3884 * can be prevented by increasing snd_una by 1, so that the
3885 * SYN is not considered as data (snd_una++ is actually also
3886 * done in SYN_SENT-state as part of the regular TCP stack).
3888 * In case there is data on this ack as well, the data will be
3889 * handled by the label "dodata" right after step6.
3891 if (so
->so_snd
.sb_cc
) {
3892 tp
->snd_una
++; /* SYN is acked */
3893 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
3894 tp
->snd_nxt
= tp
->snd_una
;
3897 * No duplicate-ACK handling is needed. So, we
3898 * directly advance to processing the ACK (aka,
3899 * updating the RTT estimation,...)
3901 * But, we first need to handle eventual SACKs,
3902 * because TFO will start sending data with the
3903 * SYN/ACK, so it might be that the client
3904 * includes a SACK with its ACK.
3906 if (SACK_ENABLED(tp
) &&
3907 (to
.to_nsacks
> 0 ||
3908 !TAILQ_EMPTY(&tp
->snd_holes
)))
3909 tcp_sack_doack(tp
, &to
, th
,
3918 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
3919 * ACKs. If the ack is in the range
3920 * tp->snd_una < th->th_ack <= tp->snd_max
3921 * then advance tp->snd_una to th->th_ack and drop
3922 * data from the retransmission queue. If this ACK reflects
3923 * more up to date window information we update our window information.
3925 case TCPS_ESTABLISHED
:
3926 case TCPS_FIN_WAIT_1
:
3927 case TCPS_FIN_WAIT_2
:
3928 case TCPS_CLOSE_WAIT
:
3931 case TCPS_TIME_WAIT
:
3932 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
3933 tcpstat
.tcps_rcvacktoomuch
++;
3936 if (SACK_ENABLED(tp
) && to
.to_nsacks
> 0) {
3937 recvd_dsack
= tcp_sack_process_dsack(tp
, &to
, th
);
3939 * If DSACK is received and this packet has no
3940 * other SACK information, it can be dropped.
3941 * We do not want to treat it as a duplicate ack.
3944 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) &&
3945 to
.to_nsacks
== 0) {
3946 tcp_bad_rexmt_check(tp
, th
, &to
);
3951 if (SACK_ENABLED(tp
) &&
3952 (to
.to_nsacks
> 0 || !TAILQ_EMPTY(&tp
->snd_holes
)))
3953 tcp_sack_doack(tp
, &to
, th
, &sack_bytes_acked
);
3956 if ((tp
->t_mpuna
) && (SEQ_GEQ(th
->th_ack
, tp
->t_mpuna
))) {
3957 if (tp
->t_mpflags
& TMPF_PREESTABLISHED
) {
3958 /* MP TCP establishment succeeded */
3960 if (tp
->t_mpflags
& TMPF_JOINED_FLOW
) {
3961 if (tp
->t_mpflags
& TMPF_SENT_JOIN
) {
3963 ~TMPF_PREESTABLISHED
;
3966 so
->so_flags
|= SOF_MPTCP_TRUE
;
3967 mptcplog((LOG_DEBUG
, "MPTCP "
3968 "Sockets: %s \n",__func__
),
3972 tp
->t_timer
[TCPT_JACK_RXMT
] = 0;
3973 tp
->t_mprxtshift
= 0;
3976 isconnected
= FALSE
;
3980 tp
->t_mpflags
&= ~TMPF_SENT_KEYS
;
3986 tcp_tfo_rcv_ack(tp
, th
);
3989 * If we have outstanding data (other than
3990 * a window probe), this is a completely
3991 * duplicate ack (ie, window info didn't
3992 * change) and the ack is the biggest we've seen.
3994 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
3995 if (tlen
== 0 && tiwin
== tp
->snd_wnd
) {
3997 * If both ends send FIN at the same time,
3998 * then the ack will be a duplicate ack
3999 * but we have to process the FIN. Check
4000 * for this condition and process the FIN
4001 * instead of the dupack
4003 if ((thflags
& TH_FIN
) &&
4004 (tp
->t_flags
& TF_SENTFIN
) &&
4005 !TCPS_HAVERCVDFIN(tp
->t_state
) &&
4006 (th
->th_ack
+ 1) == tp
->snd_max
)
4011 * MPTCP options that are ignored must
4012 * not be treated as duplicate ACKs.
4014 if (to
.to_flags
& TOF_MPTCP
) {
4018 if ((isconnected
) && (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
4019 mptcplog((LOG_DEBUG
, "MPTCP "
4020 "Sockets: bypass ack recovery\n"),
4022 MPTCP_LOGLVL_VERBOSE
);
4027 * If a duplicate acknowledgement was seen
4028 * after ECN, it indicates packet loss in
4029 * addition to ECN. Reset INRECOVERY flag
4030 * so that we can process partial acks
4033 if (tp
->ecn_flags
& TE_INRECOVERY
)
4034 tp
->ecn_flags
&= ~TE_INRECOVERY
;
4036 tcpstat
.tcps_rcvdupack
++;
4040 * Check if we need to reset the limit on
4043 if (tp
->t_early_rexmt_count
> 0 &&
4045 (tp
->t_early_rexmt_win
+
4046 TCP_EARLY_REXMT_WIN
)))
4047 tp
->t_early_rexmt_count
= 0;
4050 * Is early retransmit needed? We check for
4051 * this when the connection is waiting for
4052 * duplicate acks to enter fast recovery.
4054 if (!IN_FASTRECOVERY(tp
))
4055 tcp_early_rexmt_check(tp
, th
);
4058 * If we've seen exactly rexmt threshold
4059 * of duplicate acks, assume a packet
4060 * has been dropped and retransmit it.
4061 * Kludge snd_nxt & the congestion
4062 * window so we send only this one
4065 * We know we're losing at the current
4066 * window size so do congestion avoidance
4067 * (set ssthresh to half the current window
4068 * and pull our congestion window back to
4069 * the new ssthresh).
4071 * Dup acks mean that packets have left the
4072 * network (they're now cached at the receiver)
4073 * so bump cwnd by the amount in the receiver
4074 * to keep a constant cwnd packets in the
4077 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
4078 (th
->th_ack
!= tp
->snd_una
4079 && sack_bytes_acked
== 0)) {
4081 tp
->t_rexmtthresh
= tcprexmtthresh
;
4082 } else if (tp
->t_dupacks
> tp
->t_rexmtthresh
||
4083 IN_FASTRECOVERY(tp
)) {
4086 * If this connection was seeing packet
4087 * reordering, then recovery might be
4088 * delayed to disambiguate between
4089 * reordering and loss
4091 if (SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
) &&
4093 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) ==
4094 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
4096 * Since the SACK information is already
4097 * updated, this ACK will be dropped
4102 if (SACK_ENABLED(tp
)
4103 && IN_FASTRECOVERY(tp
)) {
4107 * Compute the amount of data in flight first.
4108 * We can inject new data into the pipe iff
4109 * we have less than 1/2 the original window's
4110 * worth of data in flight.
4112 awnd
= (tp
->snd_nxt
- tp
->snd_fack
) +
4113 tp
->sackhint
.sack_bytes_rexmit
;
4114 if (awnd
< tp
->snd_ssthresh
) {
4115 tp
->snd_cwnd
+= tp
->t_maxseg
;
4116 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
4117 tp
->snd_cwnd
= tp
->snd_ssthresh
;
4120 tp
->snd_cwnd
+= tp
->t_maxseg
;
4122 tcp_ccdbg_trace(tp
, th
, TCP_CC_IN_FASTRECOVERY
);
4124 (void) tcp_output(tp
);
4126 } else if (tp
->t_dupacks
== tp
->t_rexmtthresh
) {
4127 tcp_seq onxt
= tp
->snd_nxt
;
4130 * If we're doing sack, check to
4131 * see if we're already in sack
4132 * recovery. If we're not doing sack,
4133 * check to see if we're in newreno
4136 if (SACK_ENABLED(tp
)) {
4137 if (IN_FASTRECOVERY(tp
)) {
4140 } else if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
4144 if (SEQ_LEQ(th
->th_ack
,
4150 if (tp
->t_flags
& TF_SENTFIN
)
4151 tp
->snd_recover
= tp
->snd_max
- 1;
4153 tp
->snd_recover
= tp
->snd_max
;
4154 tp
->t_timer
[TCPT_PTO
] = 0;
4158 * If the connection has seen pkt
4159 * reordering, delay recovery until
4160 * it is clear that the packet
4163 if (SACK_ENABLED(tp
) &&
4165 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
4166 == TF_PKTS_REORDERED
&&
4167 !IN_FASTRECOVERY(tp
) &&
4168 tp
->t_reorderwin
> 0 &&
4169 (tp
->t_state
== TCPS_ESTABLISHED
||
4170 tp
->t_state
== TCPS_FIN_WAIT_1
)) {
4171 tp
->t_timer
[TCPT_DELAYFR
] =
4172 OFFSET_FROM_START(tp
,
4174 tp
->t_flagsext
|= TF_DELAY_RECOVERY
;
4175 tcpstat
.tcps_delay_recovery
++;
4176 tcp_ccdbg_trace(tp
, th
,
4177 TCP_CC_DELAY_FASTRECOVERY
);
4181 tcp_rexmt_save_state(tp
);
4183 * If the current tcp cc module has
4184 * defined a hook for tasks to run
4185 * before entering FR, call it
4187 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
4188 CC_ALGO(tp
)->pre_fr(tp
);
4189 ENTER_FASTRECOVERY(tp
);
4190 tp
->t_timer
[TCPT_REXMT
] = 0;
4191 if (TCP_ECN_ENABLED(tp
))
4192 tp
->ecn_flags
|= TE_SENDCWR
;
4194 if (SACK_ENABLED(tp
)) {
4195 tcpstat
.tcps_sack_recovery_episode
++;
4196 tp
->t_sack_recovery_episode
++;
4197 tp
->sack_newdata
= tp
->snd_nxt
;
4198 tp
->snd_cwnd
= tp
->t_maxseg
;
4200 ~TF_CWND_NONVALIDATED
;
4201 tcp_ccdbg_trace(tp
, th
,
4202 TCP_CC_ENTER_FASTRECOVERY
);
4203 (void) tcp_output(tp
);
4206 tp
->snd_nxt
= th
->th_ack
;
4207 tp
->snd_cwnd
= tp
->t_maxseg
;
4208 (void) tcp_output(tp
);
4209 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
4210 tcp_cc_adjust_nonvalidated_cwnd(tp
);
4212 tp
->snd_cwnd
= tp
->snd_ssthresh
+
4213 tp
->t_maxseg
* tp
->t_dupacks
;
4215 if (SEQ_GT(onxt
, tp
->snd_nxt
))
4217 tcp_ccdbg_trace(tp
, th
,
4218 TCP_CC_ENTER_FASTRECOVERY
);
4220 } else if (limited_txmt
&&
4221 ALLOW_LIMITED_TRANSMIT(tp
) &&
4222 (!(SACK_ENABLED(tp
)) || sack_bytes_acked
> 0) &&
4223 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) > 0) {
4224 u_int32_t incr
= (tp
->t_maxseg
* tp
->t_dupacks
);
4226 /* Use Limited Transmit algorithm on the first two
4227 * duplicate acks when there is new data to transmit
4229 tp
->snd_cwnd
+= incr
;
4230 tcpstat
.tcps_limited_txt
++;
4231 (void) tcp_output(tp
);
4233 tcp_ccdbg_trace(tp
, th
, TCP_CC_LIMITED_TRANSMIT
);
4235 /* Reset snd_cwnd back to normal */
4236 tp
->snd_cwnd
-= incr
;
4240 tp
->t_rexmtthresh
= tcprexmtthresh
;
4245 * If the congestion window was inflated to account
4246 * for the other side's cached packets, retract it.
4248 if (IN_FASTRECOVERY(tp
)) {
4249 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
4251 * If we received an ECE and entered
4252 * recovery, the subsequent ACKs should
4253 * not be treated as partial acks.
4255 if (tp
->ecn_flags
& TE_INRECOVERY
)
4258 if (SACK_ENABLED(tp
))
4259 tcp_sack_partialack(tp
, th
);
4261 tcp_newreno_partial_ack(tp
, th
);
4262 tcp_ccdbg_trace(tp
, th
, TCP_CC_PARTIAL_ACK
);
4264 EXIT_FASTRECOVERY(tp
);
4265 if (CC_ALGO(tp
)->post_fr
!= NULL
)
4266 CC_ALGO(tp
)->post_fr(tp
, th
);
4268 tcp_clear_pipeack_state(tp
);
4269 tcp_ccdbg_trace(tp
, th
,
4270 TCP_CC_EXIT_FASTRECOVERY
);
4272 } else if ((tp
->t_flagsext
&
4273 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
4274 == (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
4276 * If the ack acknowledges upto snd_recover or if
4277 * it acknowledges all the snd holes, exit
4278 * recovery and cancel the timer. Otherwise,
4279 * this is a partial ack. Wait for recovery timer
4280 * to enter recovery. The snd_holes have already
4283 if (SEQ_GEQ(th
->th_ack
, tp
->snd_recover
) ||
4284 TAILQ_EMPTY(&tp
->snd_holes
)) {
4285 tp
->t_timer
[TCPT_DELAYFR
] = 0;
4286 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
4287 EXIT_FASTRECOVERY(tp
);
4288 tcp_ccdbg_trace(tp
, th
,
4289 TCP_CC_EXIT_FASTRECOVERY
);
4293 * We were not in fast recovery. Reset the
4294 * duplicate ack counter.
4297 tp
->t_rexmtthresh
= tcprexmtthresh
;
4302 * If we reach this point, ACK is not a duplicate,
4303 * i.e., it ACKs something we sent.
4305 if (tp
->t_flags
& TF_NEEDSYN
) {
4307 * T/TCP: Connection was half-synchronized, and our
4308 * SYN has been ACK'd (so connection is now fully
4309 * synchronized). Go to non-starred state,
4310 * increment snd_una for ACK of SYN, and check if
4311 * we can do window scaling.
4313 tp
->t_flags
&= ~TF_NEEDSYN
;
4315 /* Do window scaling? */
4316 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
4317 tp
->snd_scale
= tp
->requested_s_scale
;
4318 tp
->rcv_scale
= tp
->request_r_scale
;
4323 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
4324 acked
= BYTES_ACKED(th
, tp
);
4325 tcpstat
.tcps_rcvackpack
++;
4326 tcpstat
.tcps_rcvackbyte
+= acked
;
4329 * If the last packet was a retransmit, make sure
4330 * it was not spurious.
4332 * This will also take care of congestion window
4333 * adjustment if a last packet was recovered due to a
4336 tcp_bad_rexmt_check(tp
, th
, &to
);
4338 /* Recalculate the RTT */
4339 tcp_compute_rtt(tp
, &to
, th
);
4342 * If all outstanding data is acked, stop retransmit
4343 * timer and remember to restart (more output or persist).
4344 * If there is more data to be acked, restart retransmit
4345 * timer, using current (possibly backed-off) value.
4347 if (th
->th_ack
== tp
->snd_max
) {
4348 tp
->t_timer
[TCPT_REXMT
] = 0;
4349 tp
->t_timer
[TCPT_PTO
] = 0;
4351 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
4352 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
4356 * If no data (only SYN) was ACK'd, skip rest of ACK
4363 * When outgoing data has been acked (except the SYN+data), we
4364 * mark this connection as "sending good" for TFO.
4366 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
4367 !(tp
->t_tfo_flags
& TFO_F_NO_SNDPROBING
) &&
4368 !(th
->th_flags
& TH_SYN
))
4369 tcp_heuristic_tfo_snd_good(tp
);
4372 * If TH_ECE is received, make sure that ECN is enabled
4373 * on that connection and we have sent ECT on data packets.
4375 if ((thflags
& TH_ECE
) != 0 && TCP_ECN_ENABLED(tp
) &&
4376 (tp
->ecn_flags
& TE_SENDIPECT
)) {
4378 * Reduce the congestion window if we haven't
4381 if (!IN_FASTRECOVERY(tp
)) {
4382 tcp_reduce_congestion_window(tp
);
4383 tp
->ecn_flags
|= (TE_INRECOVERY
|TE_SENDCWR
);
4385 * Also note that the connection received
4388 tp
->ecn_flags
|= TE_RECV_ECN_ECE
;
4389 INP_INC_IFNET_STAT(inp
, ecn_recv_ece
);
4390 tcpstat
.tcps_ecn_recv_ece
++;
4391 tcp_ccdbg_trace(tp
, th
, TCP_CC_ECN_RCVD
);
4396 * When new data is acked, open the congestion window.
4397 * The specifics of how this is achieved are up to the
4398 * congestion control algorithm in use for this connection.
4400 * The calculations in this function assume that snd_una is
4403 if (!IN_FASTRECOVERY(tp
)) {
4404 if (CC_ALGO(tp
)->ack_rcvd
!= NULL
)
4405 CC_ALGO(tp
)->ack_rcvd(tp
, th
);
4406 tcp_ccdbg_trace(tp
, th
, TCP_CC_ACK_RCVD
);
4408 if (acked
> so
->so_snd
.sb_cc
) {
4409 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
4410 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
4411 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
4412 so
->so_msg_state
->msg_serial_bytes
-=
4413 (int)so
->so_snd
.sb_cc
;
4417 sbdrop(&so
->so_snd
, acked
);
4418 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
4419 so
->so_msg_state
->msg_serial_bytes
-=
4422 tcp_sbsnd_trim(&so
->so_snd
);
4423 tp
->snd_wnd
-= acked
;
4426 /* detect una wraparound */
4427 if ( !IN_FASTRECOVERY(tp
) &&
4428 SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
4429 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
4430 tp
->snd_recover
= th
->th_ack
- 1;
4432 if (IN_FASTRECOVERY(tp
) &&
4433 SEQ_GEQ(th
->th_ack
, tp
->snd_recover
))
4434 EXIT_FASTRECOVERY(tp
);
4436 tp
->snd_una
= th
->th_ack
;
4437 if (SACK_ENABLED(tp
)) {
4438 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
))
4439 tp
->snd_recover
= tp
->snd_una
;
4441 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
4442 tp
->snd_nxt
= tp
->snd_una
;
4443 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
4444 !TCP_DSACK_SEQ_IN_WINDOW(tp
, tp
->t_dsack_lastuna
,
4446 tcp_rxtseg_clean(tp
);
4447 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
4448 tp
->t_bwmeas
!= NULL
)
4449 tcp_bwmeas_check(tp
);
4452 * sowwakeup must happen after snd_una, et al. are updated so that
4453 * the sequence numbers are in sync with so_snd
4457 switch (tp
->t_state
) {
4460 * In FIN_WAIT_1 STATE in addition to the processing
4461 * for the ESTABLISHED state if our FIN is now acknowledged
4462 * then enter FIN_WAIT_2.
4464 case TCPS_FIN_WAIT_1
:
4465 if (ourfinisacked
) {
4467 * If we can't receive any more
4468 * data, then closing user can proceed.
4469 * Starting the TCPT_2MSL timer is contrary to the
4470 * specification, but if we don't get a FIN
4471 * we'll hang forever.
4473 if (so
->so_state
& SS_CANTRCVMORE
) {
4474 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
4475 TCP_CONN_MAXIDLE(tp
));
4476 isconnected
= FALSE
;
4477 isdisconnected
= TRUE
;
4479 DTRACE_TCP4(state__change
, void, NULL
,
4480 struct inpcb
*, inp
,
4482 int32_t, TCPS_FIN_WAIT_2
);
4483 tp
->t_state
= TCPS_FIN_WAIT_2
;
4484 /* fall through and make sure we also recognize
4485 * data ACKed with the FIN
4488 tp
->t_flags
|= TF_ACKNOW
;
4492 * In CLOSING STATE in addition to the processing for
4493 * the ESTABLISHED state if the ACK acknowledges our FIN
4494 * then enter the TIME-WAIT state, otherwise ignore
4498 if (ourfinisacked
) {
4499 DTRACE_TCP4(state__change
, void, NULL
,
4500 struct inpcb
*, inp
,
4502 int32_t, TCPS_TIME_WAIT
);
4503 tp
->t_state
= TCPS_TIME_WAIT
;
4504 tcp_canceltimers(tp
);
4505 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
4506 tp
->t_flags
|= TF_CLOSING
;
4508 add_to_time_wait(tp
, 2 * tcp_msl
);
4510 isconnected
= FALSE
;
4511 isdisconnected
= TRUE
;
4513 tp
->t_flags
|= TF_ACKNOW
;
4517 * In LAST_ACK, we may still be waiting for data to drain
4518 * and/or to be acked, as well as for the ack of our FIN.
4519 * If our FIN is now acknowledged, delete the TCB,
4520 * enter the closed state and return.
4523 if (ourfinisacked
) {
4530 * In TIME_WAIT state the only thing that should arrive
4531 * is a retransmission of the remote FIN. Acknowledge
4532 * it and restart the finack timer.
4534 case TCPS_TIME_WAIT
:
4535 add_to_time_wait(tp
, 2 * tcp_msl
);
4540 * If there is a SACK option on the ACK and we
4541 * haven't seen any duplicate acks before, count
4542 * it as a duplicate ack even if the cumulative
4543 * ack is advanced. If the receiver delayed an
4544 * ack and detected loss afterwards, then the ack
4545 * will advance cumulative ack and will also have
4546 * a SACK option. So counting it as one duplicate
4549 if (sack_ackadv
== 1 &&
4550 tp
->t_state
== TCPS_ESTABLISHED
&&
4551 SACK_ENABLED(tp
) && sack_bytes_acked
> 0 &&
4552 to
.to_nsacks
> 0 && tp
->t_dupacks
== 0 &&
4553 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) && tlen
== 0 &&
4554 !(tp
->t_flagsext
& TF_PKTS_REORDERED
)) {
4555 tcpstat
.tcps_sack_ackadv
++;
4556 goto process_dupack
;
4562 * Update window information.
4563 * Don't look at window if no ACK: TAC's send garbage on first SYN.
4565 if ((thflags
& TH_ACK
) &&
4566 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
4567 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
4568 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
4569 /* keep track of pure window updates */
4571 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
4572 tcpstat
.tcps_rcvwinupd
++;
4573 tp
->snd_wnd
= tiwin
;
4574 tp
->snd_wl1
= th
->th_seq
;
4575 tp
->snd_wl2
= th
->th_ack
;
4576 if (tp
->snd_wnd
> tp
->max_sndwnd
)
4577 tp
->max_sndwnd
= tp
->snd_wnd
;
4582 * Process segments with URG.
4584 if ((thflags
& TH_URG
) && th
->th_urp
&&
4585 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4587 * This is a kludge, but if we receive and accept
4588 * random urgent pointers, we'll crash in
4589 * soreceive. It's hard to imagine someone
4590 * actually wanting to send this much urgent data.
4592 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
4593 th
->th_urp
= 0; /* XXX */
4594 thflags
&= ~TH_URG
; /* XXX */
4595 goto dodata
; /* XXX */
4598 * If this segment advances the known urgent pointer,
4599 * then mark the data stream. This should not happen
4600 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
4601 * a FIN has been received from the remote side.
4602 * In these states we ignore the URG.
4604 * According to RFC961 (Assigned Protocols),
4605 * the urgent pointer points to the last octet
4606 * of urgent data. We continue, however,
4607 * to consider it to indicate the first octet
4608 * of data past the urgent section as the original
4609 * spec states (in one of two places).
4611 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
4612 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
4613 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
4614 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
4615 if (so
->so_oobmark
== 0) {
4616 so
->so_state
|= SS_RCVATMARK
;
4617 postevent(so
, 0, EV_OOB
);
4620 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
4623 * Remove out of band data so doesn't get presented to user.
4624 * This can happen independent of advancing the URG pointer,
4625 * but if two URG's are pending at once, some out-of-band
4626 * data may creep in... ick.
4628 if (th
->th_urp
<= (u_int32_t
)tlen
4630 && (so
->so_options
& SO_OOBINLINE
) == 0
4633 tcp_pulloutofband(so
, th
, m
,
4634 drop_hdrlen
); /* hdr drop is delayed */
4637 * If no out of band data is expected,
4638 * pull receive urgent pointer along
4639 * with the receive window.
4641 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
4642 tp
->rcv_up
= tp
->rcv_nxt
;
4646 /* Set socket's connect or disconnect state correcly before doing data.
4647 * The following might unlock the socket if there is an upcall or a socket
4652 } else if (isdisconnected
) {
4653 soisdisconnected(so
);
4656 /* Let's check the state of pcb just to make sure that it did not get closed
4657 * when we unlocked above
4659 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
4660 /* Just drop the packet that we are processing and return */
4665 * Process the segment text, merging it into the TCP sequencing queue,
4666 * and arranging for acknowledgment of receipt if necessary.
4667 * This process logically involves adjusting tp->rcv_wnd as data
4668 * is presented to the user (this happens in tcp_usrreq.c,
4669 * case PRU_RCVD). If a FIN has already been received on this
4670 * connection then we just ignore the text.
4672 * If we are in SYN-received state and got a valid TFO cookie, we want
4673 * to process the data.
4675 if ((tlen
|| (thflags
& TH_FIN
)) &&
4676 TCPS_HAVERCVDFIN(tp
->t_state
) == 0 &&
4677 (TCPS_HAVEESTABLISHED(tp
->t_state
) ||
4678 (tp
->t_state
== TCPS_SYN_RECEIVED
&&
4679 (tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)))) {
4680 tcp_seq save_start
= th
->th_seq
;
4681 tcp_seq save_end
= th
->th_seq
+ tlen
;
4682 m_adj(m
, drop_hdrlen
); /* delayed header drop */
4684 * Insert segment which includes th into TCP reassembly queue
4685 * with control block tp. Set thflags to whether reassembly now
4686 * includes a segment with FIN. This handles the common case
4687 * inline (segment is the next to be received on an established
4688 * connection, and the queue is empty), avoiding linkage into
4689 * and removal from the queue and repetition of various
4691 * Set DELACK for segments received in order, but ack
4692 * immediately when segments are out of order (so
4693 * fast retransmit can work).
4695 if (th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
4696 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4698 * Calculate the RTT on the receiver only if the
4699 * connection is in streaming mode and the last
4700 * packet was not an end-of-write
4702 if ((tp
->t_flags
& TF_STRETCHACK
) &&
4703 !(tp
->t_flagsext
& TF_STREAMEOW
))
4704 tcp_compute_rtt(tp
, &to
, th
);
4706 if (DELAY_ACK(tp
, th
) &&
4707 ((tp
->t_flags
& TF_ACKNOW
) == 0) ) {
4708 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4709 tp
->t_flags
|= TF_DELACK
;
4710 tp
->t_timer
[TCPT_DELACK
] =
4711 OFFSET_FROM_START(tp
, tcp_delack
);
4715 tp
->t_flags
|= TF_ACKNOW
;
4717 tp
->rcv_nxt
+= tlen
;
4718 thflags
= th
->th_flags
& TH_FIN
;
4719 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
4720 tcpstat
.tcps_rcvbyte
+= tlen
;
4721 if (nstat_collect
) {
4722 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
4723 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4724 rxpackets
, m
->m_pkthdr
.lro_npkts
);
4726 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4729 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4732 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
);
4733 so_recv_data_stat(so
, m
, drop_hdrlen
);
4735 if (sbappendstream_rcvdemux(so
, m
,
4736 th
->th_seq
- (tp
->irs
+ 1), 0)) {
4740 thflags
= tcp_reass(tp
, th
, &tlen
, m
, ifp
);
4741 tp
->t_flags
|= TF_ACKNOW
;
4744 if (tlen
> 0 && SACK_ENABLED(tp
))
4745 tcp_update_sack_list(tp
, save_start
, save_end
);
4747 tcp_adaptive_rwtimo_check(tp
, tlen
);
4750 tcp_tfo_rcv_data(tp
);
4752 if (tp
->t_flags
& TF_DELACK
)
4756 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4757 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
4758 th
->th_seq
, th
->th_ack
, th
->th_win
);
4763 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4764 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
4765 th
->th_seq
, th
->th_ack
, th
->th_win
);
4775 * If FIN is received ACK the FIN and let the user know
4776 * that the connection is closing.
4778 if (thflags
& TH_FIN
) {
4779 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4781 postevent(so
, 0, EV_FIN
);
4783 * If connection is half-synchronized
4784 * (ie NEEDSYN flag on) then delay ACK,
4785 * so it may be piggybacked when SYN is sent.
4786 * Otherwise, since we received a FIN then no
4787 * more input can be expected, send ACK now.
4789 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4790 if (DELAY_ACK(tp
, th
) && (tp
->t_flags
& TF_NEEDSYN
)) {
4791 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4792 tp
->t_flags
|= TF_DELACK
;
4793 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
4796 tp
->t_flags
|= TF_ACKNOW
;
4800 switch (tp
->t_state
) {
4803 * In SYN_RECEIVED and ESTABLISHED STATES
4804 * enter the CLOSE_WAIT state.
4806 case TCPS_SYN_RECEIVED
:
4807 tp
->t_starttime
= tcp_now
;
4808 case TCPS_ESTABLISHED
:
4809 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
4810 struct tcpcb
*, tp
, int32_t, TCPS_CLOSE_WAIT
);
4811 tp
->t_state
= TCPS_CLOSE_WAIT
;
4815 * If still in FIN_WAIT_1 STATE FIN has not been acked so
4816 * enter the CLOSING state.
4818 case TCPS_FIN_WAIT_1
:
4819 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
4820 struct tcpcb
*, tp
, int32_t, TCPS_CLOSING
);
4821 tp
->t_state
= TCPS_CLOSING
;
4825 * In FIN_WAIT_2 state enter the TIME_WAIT state,
4826 * starting the time-wait timer, turning off the other
4829 case TCPS_FIN_WAIT_2
:
4830 DTRACE_TCP4(state__change
, void, NULL
,
4831 struct inpcb
*, inp
,
4833 int32_t, TCPS_TIME_WAIT
);
4834 tp
->t_state
= TCPS_TIME_WAIT
;
4835 tcp_canceltimers(tp
);
4836 tp
->t_flags
|= TF_ACKNOW
;
4837 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
4838 tp
->t_flags
|= TF_CLOSING
;
4840 add_to_time_wait(tp
, 2 * tcp_msl
);
4842 soisdisconnected(so
);
4846 * In TIME_WAIT state restart the 2 MSL time_wait timer.
4848 case TCPS_TIME_WAIT
:
4849 add_to_time_wait(tp
, 2 * tcp_msl
);
4854 if (so
->so_options
& SO_DEBUG
)
4855 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
4860 * Return any desired output.
4862 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
4863 (void) tcp_output(tp
);
4866 tcp_check_timer_state(tp
);
4869 tcp_unlock(so
, 1, 0);
4870 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4875 * Generate an ACK dropping incoming segment if it occupies
4876 * sequence space, where the ACK reflects our state.
4878 * We can now skip the test for the RST flag since all
4879 * paths to this code happen after packets containing
4880 * RST have been dropped.
4882 * In the SYN-RECEIVED state, don't send an ACK unless the
4883 * segment we received passes the SYN-RECEIVED ACK test.
4884 * If it fails send a RST. This breaks the loop in the
4885 * "LAND" DoS attack, and also prevents an ACK storm
4886 * between two listening ports that have been sent forged
4887 * SYN segments, each with the source address of the other.
4889 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
4890 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
4891 SEQ_GT(th
->th_ack
, tp
->snd_max
)) ) {
4892 rstreason
= BANDLIM_RST_OPENPORT
;
4893 IF_TCP_STATINC(ifp
, dospacket
);
4897 if (so
->so_options
& SO_DEBUG
)
4898 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
4902 tp
->t_flags
|= TF_ACKNOW
;
4903 (void) tcp_output(tp
);
4905 /* Don't need to check timer state as we should have done it during tcp_output */
4906 tcp_unlock(so
, 1, 0);
4907 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4909 dropwithresetnosock
:
4913 * Generate a RST, dropping incoming segment.
4914 * Make ACK acceptable to originator of segment.
4915 * Don't bother to respond if destination was broadcast/multicast.
4917 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
4921 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
4922 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
4926 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
4927 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
4928 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
4929 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
4931 /* IPv6 anycast check is done at tcp6_input() */
4934 * Perform bandwidth limiting.
4937 if (badport_bandlim(rstreason
) < 0)
4942 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
4943 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
4946 bzero(&tra
, sizeof(tra
));
4947 tra
.ifscope
= ifscope
;
4948 tra
.awdl_unrestricted
= 1;
4949 if (thflags
& TH_ACK
)
4950 /* mtod() below is safe as long as hdr dropping is delayed */
4951 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
4954 if (thflags
& TH_SYN
)
4956 /* mtod() below is safe as long as hdr dropping is delayed */
4957 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+tlen
,
4958 (tcp_seq
)0, TH_RST
|TH_ACK
, &tra
);
4960 /* destroy temporarily created socket */
4963 tcp_unlock(so
, 1, 0);
4964 } else if ((inp
!= NULL
) && (nosock
== 0)) {
4965 tcp_unlock(so
, 1, 0);
4967 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4973 * Drop space held by incoming segment and return.
4976 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
4977 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
4981 /* destroy temporarily created socket */
4984 tcp_unlock(so
, 1, 0);
4986 else if (nosock
== 0) {
4987 tcp_unlock(so
, 1, 0);
4989 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4994 * Parse TCP options and place in tcpopt.
4997 tcp_dooptions(struct tcpcb
*tp
, u_char
*cp
, int cnt
, struct tcphdr
*th
,
5003 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
5005 if (opt
== TCPOPT_EOL
)
5007 if (opt
== TCPOPT_NOP
)
5013 if (optlen
< 2 || optlen
> cnt
)
5022 if (optlen
!= TCPOLEN_MAXSEG
)
5024 if (!(th
->th_flags
& TH_SYN
))
5026 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
5029 to
->to_flags
|= TOF_MSS
;
5033 if (optlen
!= TCPOLEN_WINDOW
)
5035 if (!(th
->th_flags
& TH_SYN
))
5037 to
->to_flags
|= TOF_SCALE
;
5038 to
->to_requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
5041 case TCPOPT_TIMESTAMP
:
5042 if (optlen
!= TCPOLEN_TIMESTAMP
)
5044 to
->to_flags
|= TOF_TS
;
5045 bcopy((char *)cp
+ 2,
5046 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
5047 NTOHL(to
->to_tsval
);
5048 bcopy((char *)cp
+ 6,
5049 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
5050 NTOHL(to
->to_tsecr
);
5051 /* Re-enable sending Timestamps if we received them */
5052 if (!(tp
->t_flags
& TF_REQ_TSTMP
) &&
5053 tcp_do_rfc1323
== 1)
5054 tp
->t_flags
|= TF_REQ_TSTMP
;
5056 case TCPOPT_SACK_PERMITTED
:
5058 optlen
!= TCPOLEN_SACK_PERMITTED
)
5060 if (th
->th_flags
& TH_SYN
)
5061 to
->to_flags
|= TOF_SACK
;
5064 if (optlen
<= 2 || (optlen
- 2) % TCPOLEN_SACK
!= 0)
5066 to
->to_nsacks
= (optlen
- 2) / TCPOLEN_SACK
;
5067 to
->to_sacks
= cp
+ 2;
5068 tcpstat
.tcps_sack_rcv_blocks
++;
5071 case TCPOPT_FASTOPEN
:
5072 if (optlen
== TCPOLEN_FASTOPEN_REQ
) {
5073 if (tp
->t_state
!= TCPS_LISTEN
)
5076 to
->to_flags
|= TOF_TFOREQ
;
5078 if (optlen
< TCPOLEN_FASTOPEN_REQ
||
5079 (optlen
- TCPOLEN_FASTOPEN_REQ
) > TFO_COOKIE_LEN_MAX
||
5080 (optlen
- TCPOLEN_FASTOPEN_REQ
) < TFO_COOKIE_LEN_MIN
)
5082 if (tp
->t_state
!= TCPS_LISTEN
&&
5083 tp
->t_state
!= TCPS_SYN_SENT
)
5086 to
->to_flags
|= TOF_TFO
;
5087 to
->to_tfo
= cp
+ 1;
5092 case TCPOPT_MULTIPATH
:
5093 tcp_do_mptcp_options(tp
, cp
, th
, to
, optlen
);
5101 tcp_finalize_options(struct tcpcb
*tp
, struct tcpopt
*to
, unsigned int ifscope
)
5103 if (to
->to_flags
& TOF_TS
) {
5104 tp
->t_flags
|= TF_RCVD_TSTMP
;
5105 tp
->ts_recent
= to
->to_tsval
;
5106 tp
->ts_recent_age
= tcp_now
;
5109 if (to
->to_flags
& TOF_MSS
)
5110 tcp_mss(tp
, to
->to_mss
, ifscope
);
5111 if (SACK_ENABLED(tp
)) {
5112 if (!(to
->to_flags
& TOF_SACK
))
5113 tp
->t_flagsext
&= ~(TF_SACK_ENABLE
);
5115 tp
->t_flags
|= TF_SACK_PERMIT
;
5117 if (to
->to_flags
& TOF_SCALE
) {
5118 tp
->t_flags
|= TF_RCVD_SCALE
;
5119 tp
->requested_s_scale
= to
->to_requested_s_scale
;
5121 /* Re-enable window scaling, if the option is received */
5122 if (tp
->request_r_scale
> 0)
5123 tp
->t_flags
|= TF_REQ_SCALE
;
5128 * Pull out of band byte out of a segment so
5129 * it doesn't appear in the user's data queue.
5130 * It is still reflected in the segment length for
5131 * sequencing purposes.
5134 tcp_pulloutofband(so
, th
, m
, off
)
5137 register struct mbuf
*m
;
5138 int off
; /* delayed to be droped hdrlen */
5140 int cnt
= off
+ th
->th_urp
- 1;
5143 if (m
->m_len
> cnt
) {
5144 char *cp
= mtod(m
, caddr_t
) + cnt
;
5145 struct tcpcb
*tp
= sototcpcb(so
);
5148 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
5149 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
5151 if (m
->m_flags
& M_PKTHDR
)
5160 panic("tcp_pulloutofband");
5164 get_base_rtt(struct tcpcb
*tp
)
5166 uint32_t base_rtt
= 0, i
;
5167 struct rtentry
*rt
= tp
->t_inpcb
->inp_route
.ro_rt
;
5170 for (i
= 0; i
< NRTT_HIST
; ++i
) {
5171 if (rt
->rtt_hist
[i
] != 0 &&
5172 (base_rtt
== 0 || rt
->rtt_hist
[i
] < base_rtt
))
5173 base_rtt
= rt
->rtt_hist
[i
];
5180 /* Each value of RTT base represents the minimum RTT seen in a minute.
5181 * We keep upto N_RTT_BASE minutes worth of history.
5184 update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
)
5189 if ((rt
= tp
->t_inpcb
->inp_route
.ro_rt
) == NULL
)
5191 if (rt
->rtt_expire_ts
== 0) {
5193 /* check again to avoid any race */
5194 if (rt
->rtt_expire_ts
!= 0) {
5198 rt
->rtt_expire_ts
= tcp_now
;
5200 rt
->rtt_hist
[0] = rtt
;
5207 * If the recv side is being throttled, check if the
5208 * current RTT is closer to the base RTT seen in
5209 * first (recent) two slots. If so, unthrottle the stream.
5211 if ((tp
->t_flagsext
& TF_RECV_THROTTLE
) &&
5212 (int)(tcp_now
- tp
->t_recv_throttle_ts
) >= TCP_RECV_THROTTLE_WIN
) {
5213 base_rtt
= get_base_rtt(tp
);
5214 if (tp
->t_rttcur
<= (base_rtt
+ target_qdelay
)) {
5215 tp
->t_flagsext
&= ~TF_RECV_THROTTLE
;
5216 tp
->t_recv_throttle_ts
= 0;
5219 #endif /* TRAFFIC_MGT */
5220 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5221 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5223 /* check the condition again to avoid race */
5224 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5225 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5227 if (rt
->rtt_index
>= NRTT_HIST
)
5229 rt
->rtt_hist
[rt
->rtt_index
] = rtt
;
5230 rt
->rtt_expire_ts
= tcp_now
;
5232 rt
->rtt_hist
[rt
->rtt_index
] =
5233 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5237 rt
->rtt_hist
[rt
->rtt_index
] =
5238 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5243 * If we have a timestamp reply, update smoothed RTT. If no timestamp is
5244 * present but transmit timer is running and timed sequence number was
5245 * acked, update smoothed RTT.
5247 * If timestamps are supported, a receiver can update RTT even if
5248 * there is no outstanding data.
5250 * Some boxes send broken timestamp replies during the SYN+ACK phase,
5251 * ignore timestamps of 0or we could calculate a huge RTT and blow up
5252 * the retransmit timer.
5255 tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
, struct tcphdr
*th
)
5258 VERIFY(to
!= NULL
&& th
!= NULL
);
5259 if (tp
->t_rtttime
!= 0 && SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
5260 u_int32_t pipe_ack_val
;
5261 rtt
= tcp_now
- tp
->t_rtttime
;
5263 * Compute pipe ack -- the amount of data acknowledged
5266 if (SEQ_GT(th
->th_ack
, tp
->t_pipeack_lastuna
)) {
5267 pipe_ack_val
= th
->th_ack
- tp
->t_pipeack_lastuna
;
5268 /* Update the sample */
5269 tp
->t_pipeack_sample
[tp
->t_pipeack_ind
++] =
5271 tp
->t_pipeack_ind
%= TCP_PIPEACK_SAMPLE_COUNT
;
5273 /* Compute the max of the pipeack samples */
5274 pipe_ack_val
= tcp_get_max_pipeack(tp
);
5275 tp
->t_pipeack
= (pipe_ack_val
>
5276 TCP_CC_CWND_INIT_BYTES
) ?
5279 /* start another measurement */
5282 if (((to
->to_flags
& TOF_TS
) != 0) &&
5283 (to
->to_tsecr
!= 0) &&
5284 TSTMP_GEQ(tcp_now
, to
->to_tsecr
)) {
5285 tcp_xmit_timer(tp
, (tcp_now
- to
->to_tsecr
),
5286 to
->to_tsecr
, th
->th_ack
);
5287 } else if (rtt
> 0) {
5288 tcp_xmit_timer(tp
, rtt
, 0, th
->th_ack
);
5293 * Collect new round-trip time estimate
5294 * and update averages and current timeout.
5297 tcp_xmit_timer(register struct tcpcb
*tp
, int rtt
,
5298 u_int32_t tsecr
, tcp_seq th_ack
)
5302 if (tp
->t_flagsext
& TF_RECOMPUTE_RTT
) {
5303 if (SEQ_GT(th_ack
, tp
->snd_una
) &&
5304 SEQ_LEQ(th_ack
, tp
->snd_max
) &&
5306 TSTMP_GEQ(tsecr
, tp
->t_badrexmt_time
))) {
5308 * We received a new ACk after a
5309 * spurious timeout. Adapt retransmission
5310 * timer as described in rfc 4015.
5312 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
5313 tp
->t_badrexmt_time
= 0;
5314 tp
->t_srtt
= max(tp
->t_srtt_prev
, rtt
);
5315 tp
->t_srtt
= tp
->t_srtt
<< TCP_RTT_SHIFT
;
5316 tp
->t_rttvar
= max(tp
->t_rttvar_prev
, (rtt
>> 1));
5317 tp
->t_rttvar
= tp
->t_rttvar
<< TCP_RTTVAR_SHIFT
;
5319 if (tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
5320 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
5328 tcpstat
.tcps_rttupdated
++;
5333 update_base_rtt(tp
, rtt
);
5336 if (tp
->t_srtt
!= 0) {
5338 * srtt is stored as fixed point with 5 bits after the
5339 * binary point (i.e., scaled by 32). The following magic
5340 * is equivalent to the smoothing algorithm in rfc793 with
5341 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
5344 * Freebsd adjusts rtt to origin 0 by subtracting 1
5345 * from the provided rtt value. This was required because
5346 * of the way t_rtttime was initiailised to 1 before.
5347 * Since we changed t_rtttime to be based on
5348 * tcp_now, this extra adjustment is not needed.
5350 delta
= (rtt
<< TCP_DELTA_SHIFT
)
5351 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
5353 if ((tp
->t_srtt
+= delta
) <= 0)
5357 * We accumulate a smoothed rtt variance (actually, a
5358 * smoothed mean difference), then set the retransmit
5359 * timer to smoothed rtt + 4 times the smoothed variance.
5360 * rttvar is stored as fixed point with 4 bits after the
5361 * binary point (scaled by 16). The following is
5362 * equivalent to rfc793 smoothing with an alpha of .75
5363 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
5364 * rfc793's wired-in beta.
5368 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
5369 if ((tp
->t_rttvar
+= delta
) <= 0)
5371 if (tp
->t_rttbest
== 0 ||
5372 tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
5373 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
5376 * No rtt measurement yet - use the unsmoothed rtt.
5377 * Set the variance to half the rtt (so our first
5378 * retransmit happens at 3*rtt).
5380 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
5381 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
5385 nstat_route_rtt(tp
->t_inpcb
->inp_route
.ro_rt
, tp
->t_srtt
,
5391 * the retransmit should happen at rtt + 4 * rttvar.
5392 * Because of the way we do the smoothing, srtt and rttvar
5393 * will each average +1/2 tick of bias. When we compute
5394 * the retransmit timer, we want 1/2 tick of rounding and
5395 * 1 extra tick because of +-1/2 tick uncertainty in the
5396 * firing of the timer. The bias will give us exactly the
5397 * 1.5 tick we need. But, because the bias is
5398 * statistical, we have to test that we don't drop below
5399 * the minimum feasible timer (which is 2 ticks).
5401 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
5402 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
,
5403 TCP_ADD_REXMTSLOP(tp
));
5406 * We received an ack for a packet that wasn't retransmitted;
5407 * it is probably safe to discard any error indications we've
5408 * received recently. This isn't quite right, but close enough
5409 * for now (a route might have failed after we sent a segment,
5410 * and the return path might not be symmetrical).
5412 tp
->t_softerror
= 0;
5415 static inline unsigned int
5416 tcp_maxmtu(struct rtentry
*rt
)
5418 unsigned int maxmtu
;
5420 RT_LOCK_ASSERT_HELD(rt
);
5421 if (rt
->rt_rmx
.rmx_mtu
== 0)
5422 maxmtu
= rt
->rt_ifp
->if_mtu
;
5424 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, rt
->rt_ifp
->if_mtu
);
5430 static inline unsigned int
5431 tcp_maxmtu6(struct rtentry
*rt
)
5433 unsigned int maxmtu
;
5434 struct nd_ifinfo
*ndi
= NULL
;
5436 RT_LOCK_ASSERT_HELD(rt
);
5437 if ((ndi
= ND_IFINFO(rt
->rt_ifp
)) != NULL
&& !ndi
->initialized
)
5440 lck_mtx_lock(&ndi
->lock
);
5441 if (rt
->rt_rmx
.rmx_mtu
== 0)
5442 maxmtu
= IN6_LINKMTU(rt
->rt_ifp
);
5444 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, IN6_LINKMTU(rt
->rt_ifp
));
5446 lck_mtx_unlock(&ndi
->lock
);
5453 * Determine a reasonable value for maxseg size.
5454 * If the route is known, check route for mtu.
5455 * If none, use an mss that can be handled on the outgoing
5456 * interface without forcing IP to fragment; if bigger than
5457 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
5458 * to utilize large mbufs. If no route is found, route has no mtu,
5459 * or the destination isn't local, use a default, hopefully conservative
5460 * size (usually 512 or the default IP max size, but no more than the mtu
5461 * of the interface), as we can't discover anything about intervening
5462 * gateways or networks. We also initialize the congestion/slow start
5463 * window. While looking at the routing entry, we also initialize
5464 * other path-dependent parameters from pre-set or cached values
5465 * in the routing entry.
5467 * Also take into account the space needed for options that we
5468 * send regularly. Make maxseg shorter by that amount to assure
5469 * that we can send maxseg amount of data even when the options
5470 * are present. Store the upper limit of the length of options plus
5473 * NOTE that this routine is only called when we process an incoming
5474 * segment, for outgoing segments only tcp_mssopt is called.
5478 tcp_mss(tp
, offer
, input_ifscope
)
5481 unsigned int input_ifscope
;
5489 struct rmxp_tao
*taop
;
5490 int origoffer
= offer
;
5491 u_int32_t sb_max_corrected
;
5500 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
5501 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
5502 : sizeof (struct tcpiphdr
);
5504 #define min_protoh (sizeof (struct tcpiphdr))
5509 rt
= tcp_rtlookup6(inp
, input_ifscope
);
5514 rt
= tcp_rtlookup(inp
, input_ifscope
);
5516 isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
5519 tp
->t_maxopd
= tp
->t_maxseg
=
5521 isipv6
? tcp_v6mssdflt
:
5528 * Slower link window correction:
5529 * If a value is specificied for slowlink_wsize use it for
5530 * PPP links believed to be on a serial modem (speed <128Kbps).
5531 * Excludes 9600bps as it is the default value adversized
5532 * by pseudo-devices over ppp.
5534 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
5535 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
5536 tp
->t_flags
|= TF_SLOWLINK
;
5538 so
= inp
->inp_socket
;
5540 taop
= rmx_taop(rt
->rt_rmx
);
5542 * Offer == -1 means that we didn't receive SYN yet,
5543 * use cached value in that case;
5546 offer
= taop
->tao_mssopt
;
5548 * Offer == 0 means that there was no MSS on the SYN segment,
5549 * in this case we use tcp_mssdflt.
5554 isipv6
? tcp_v6mssdflt
:
5559 * Prevent DoS attack with too small MSS. Round up
5560 * to at least minmss.
5562 offer
= max(offer
, tcp_minmss
);
5564 * Sanity check: make sure that maxopd will be large
5565 * enough to allow some data on segments even is the
5566 * all the option space is used (40bytes). Otherwise
5567 * funny things may happen in tcp_output.
5569 offer
= max(offer
, 64);
5571 taop
->tao_mssopt
= offer
;
5574 * While we're here, check if there's an initial rtt
5575 * or rttvar. Convert from the route-table units
5576 * to scaled multiples of the slow timeout timer.
5578 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
) != 0) {
5579 tcp_getrt_rtt(tp
, rt
);
5581 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
5585 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5587 mss
= tcp_maxmtu(rt
);
5591 // At this point, the mss is just the MTU. Adjust if necessary.
5592 mss
= necp_socket_get_effective_mtu(inp
, mss
);
5597 if (rt
->rt_rmx
.rmx_mtu
== 0) {
5601 mss
= min(mss
, tcp_v6mssdflt
);
5605 mss
= min(mss
, tcp_mssdflt
);
5608 mss
= min(mss
, offer
);
5610 * maxopd stores the maximum length of data AND options
5611 * in a segment; maxseg is the amount of data in a normal
5612 * segment. We need to store this value (maxopd) apart
5613 * from maxseg, because now every segment carries options
5614 * and thus we normally have somewhat less data in segments.
5619 * origoffer==-1 indicates, that no segments were received yet.
5620 * In this case we just guess.
5622 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
5624 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
5625 mss
-= TCPOLEN_TSTAMP_APPA
;
5628 mss
-= mptcp_adj_mss(tp
, FALSE
);
5633 * Calculate corrected value for sb_max; ensure to upgrade the
5634 * numerator for large sb_max values else it will overflow.
5636 sb_max_corrected
= (sb_max
* (u_int64_t
)MCLBYTES
) / (MSIZE
+ MCLBYTES
);
5639 * If there's a pipesize (ie loopback), change the socket
5640 * buffer to that size only if it's bigger than the current
5641 * sockbuf size. Make the socket buffers an integral
5642 * number of mss units; if the mss is larger than
5643 * the socket buffer, decrease the mss.
5646 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
5647 if (bufsize
< so
->so_snd
.sb_hiwat
)
5649 bufsize
= so
->so_snd
.sb_hiwat
;
5653 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5654 if (bufsize
> sb_max_corrected
)
5655 bufsize
= sb_max_corrected
;
5656 (void)sbreserve(&so
->so_snd
, bufsize
);
5661 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
5662 if (bufsize
< so
->so_rcv
.sb_hiwat
)
5664 bufsize
= so
->so_rcv
.sb_hiwat
;
5665 if (bufsize
> mss
) {
5666 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5667 if (bufsize
> sb_max_corrected
)
5668 bufsize
= sb_max_corrected
;
5669 (void)sbreserve(&so
->so_rcv
, bufsize
);
5672 set_tcp_stream_priority(so
);
5674 if (rt
->rt_rmx
.rmx_ssthresh
) {
5676 * There's some sort of gateway or interface
5677 * buffer limit on the path. Use this to set
5678 * slow-start threshold, but set the threshold to
5679 * no less than 2*mss.
5681 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
5682 tcpstat
.tcps_usedssthresh
++;
5684 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
5688 * Set the slow-start flight size depending on whether this
5689 * is a local network or not.
5691 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
5692 CC_ALGO(tp
)->cwnd_init(tp
);
5694 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CWND_INIT
);
5696 /* Route locked during lookup above */
5701 * Determine the MSS option to send on an outgoing SYN.
5715 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
5716 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
5717 : sizeof (struct tcpiphdr
);
5719 #define min_protoh (sizeof (struct tcpiphdr))
5724 rt
= tcp_rtlookup6(tp
->t_inpcb
, IFSCOPE_NONE
);
5727 rt
= tcp_rtlookup(tp
->t_inpcb
, IFSCOPE_NONE
);
5731 isipv6
? tcp_v6mssdflt
:
5736 * Slower link window correction:
5737 * If a value is specificied for slowlink_wsize use it for PPP links
5738 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
5739 * it is the default value adversized by pseudo-devices over ppp.
5741 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
5742 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
5743 tp
->t_flags
|= TF_SLOWLINK
;
5747 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5749 mss
= tcp_maxmtu(rt
);
5751 /* Route locked during lookup above */
5755 // At this point, the mss is just the MTU. Adjust if necessary.
5756 mss
= necp_socket_get_effective_mtu(tp
->t_inpcb
, mss
);
5759 return (mss
- min_protoh
);
5763 * On a partial ack arrives, force the retransmission of the
5764 * next unacknowledged segment. Do not clear tp->t_dupacks.
5765 * By setting snd_nxt to th_ack, this forces retransmission timer to
5769 tcp_newreno_partial_ack(tp
, th
)
5773 tcp_seq onxt
= tp
->snd_nxt
;
5774 u_int32_t ocwnd
= tp
->snd_cwnd
;
5775 tp
->t_timer
[TCPT_REXMT
] = 0;
5776 tp
->t_timer
[TCPT_PTO
] = 0;
5778 tp
->snd_nxt
= th
->th_ack
;
5780 * Set snd_cwnd to one segment beyond acknowledged offset
5781 * (tp->snd_una has not yet been updated when this function
5784 tp
->snd_cwnd
= tp
->t_maxseg
+ BYTES_ACKED(th
, tp
);
5785 tp
->t_flags
|= TF_ACKNOW
;
5786 (void) tcp_output(tp
);
5787 tp
->snd_cwnd
= ocwnd
;
5788 if (SEQ_GT(onxt
, tp
->snd_nxt
))
5791 * Partial window deflation. Relies on fact that tp->snd_una
5794 if (tp
->snd_cwnd
> BYTES_ACKED(th
, tp
))
5795 tp
->snd_cwnd
-= BYTES_ACKED(th
, tp
);
5798 tp
->snd_cwnd
+= tp
->t_maxseg
;
5803 * Drop a random TCP connection that hasn't been serviced yet and
5804 * is eligible for discard. There is a one in qlen chance that
5805 * we will return a null, saying that there are no dropable
5806 * requests. In this case, the protocol specific code should drop
5807 * the new request. This insures fairness.
5809 * The listening TCP socket "head" must be locked
5812 tcp_dropdropablreq(struct socket
*head
)
5814 struct socket
*so
, *sonext
;
5815 unsigned int i
, j
, qlen
;
5816 static u_int32_t rnd
= 0;
5817 static u_int64_t old_runtime
;
5818 static unsigned int cur_cnt
, old_cnt
;
5820 struct inpcb
*inp
= NULL
;
5823 if ((head
->so_options
& SO_ACCEPTCONN
) == 0)
5826 if (TAILQ_EMPTY(&head
->so_incomp
))
5830 * Check if there is any socket in the incomp queue
5831 * that is closed because of a reset from the peer and is
5832 * waiting to be garbage collected. If so, pick that as
5835 TAILQ_FOREACH_SAFE(so
, &head
->so_incomp
, so_list
, sonext
) {
5836 inp
= sotoinpcb(so
);
5837 tp
= intotcpcb(inp
);
5838 if (tp
!= NULL
&& tp
->t_state
== TCPS_CLOSED
&&
5839 so
->so_head
!= NULL
&&
5840 (so
->so_state
& (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) ==
5841 (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) {
5843 * The listen socket is already locked but we
5844 * can lock this socket here without lock ordering
5845 * issues because it is in the incomp queue and
5846 * is not visible to others.
5848 if (lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
5857 so
= TAILQ_FIRST(&head
->so_incomp
);
5859 now_sec
= net_uptime();
5860 if ((i
= (now_sec
- old_runtime
)) != 0) {
5861 old_runtime
= now_sec
;
5862 old_cnt
= cur_cnt
/ i
;
5867 qlen
= head
->so_incqlen
;
5869 rnd
= RandomULong();
5871 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
5872 rnd
= (314159 * rnd
+ 66329) & 0xffff;
5873 j
= ((qlen
+ 1) * rnd
) >> 16;
5876 so
= TAILQ_NEXT(so
, so_list
);
5878 /* Find a connection that is not already closing (or being served) */
5880 inp
= (struct inpcb
*)so
->so_pcb
;
5882 sonext
= TAILQ_NEXT(so
, so_list
);
5884 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0)
5887 * Avoid the issue of a socket being accepted
5888 * by one input thread and being dropped by
5889 * another input thread. If we can't get a hold
5890 * on this mutex, then grab the next socket in
5893 if (lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
5895 if ((so
->so_usecount
== 2) &&
5896 (so
->so_state
& SS_INCOMP
) &&
5897 !(so
->so_flags
& SOF_INCOMP_INPROGRESS
)) {
5901 * don't use if being accepted or
5902 * used in any other way
5904 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
5905 tcp_unlock(so
, 1, 0);
5909 * do not try to lock the inp in
5910 * in_pcb_checkstate because the lock
5911 * is already held in some other thread.
5912 * Only drop the inp_wntcnt reference.
5914 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
5924 /* Makes sure socket is still in the right state to be discarded */
5926 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
5927 tcp_unlock(so
, 1, 0);
5932 if (so
->so_usecount
!= 2 || !(so
->so_state
& SS_INCOMP
)) {
5933 /* do not discard: that socket is being accepted */
5934 tcp_unlock(so
, 1, 0);
5938 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
5939 tcp_unlock(head
, 0, 0);
5941 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
5943 so
->so_flags
|= SOF_OVERFLOW
;
5947 if (inp
->inp_wantcnt
> 0 && inp
->inp_wantcnt
!= WNT_STOPUSING
) {
5949 * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE
5950 * doesn't require a lock, it could have happened while
5951 * we are holding the lock. This pcb will have to
5952 * be garbage collected later.
5953 * Release the reference held for so_incomp queue
5956 tcp_unlock(so
, 1, 0);
5959 * Unlock this socket and leave the reference on.
5960 * We need to acquire the pcbinfo lock in order to
5961 * fully dispose it off
5963 tcp_unlock(so
, 0, 0);
5965 lck_rw_lock_exclusive(tcbinfo
.ipi_lock
);
5968 /* Release the reference held for so_incomp queue */
5971 if (so
->so_usecount
!= 1 ||
5972 (inp
->inp_wantcnt
> 0 &&
5973 inp
->inp_wantcnt
!= WNT_STOPUSING
)) {
5975 * There is an extra wantcount or usecount
5976 * that must have been added when the socket
5977 * was unlocked. This socket will have to be
5978 * garbage collected later
5980 tcp_unlock(so
, 1, 0);
5983 /* Drop the reference held for this function */
5988 lck_rw_done(tcbinfo
.ipi_lock
);
5990 tcpstat
.tcps_drops
++;
5992 tcp_lock(head
, 0, 0);
5998 /* Set background congestion control on a socket */
6000 tcp_set_background_cc(struct socket
*so
)
6002 tcp_set_new_cc(so
, TCP_CC_ALGO_BACKGROUND_INDEX
);
6005 /* Set foreground congestion control on a socket */
6007 tcp_set_foreground_cc(struct socket
*so
)
6009 if (tcp_use_newreno
)
6010 tcp_set_new_cc(so
, TCP_CC_ALGO_NEWRENO_INDEX
);
6012 tcp_set_new_cc(so
, TCP_CC_ALGO_CUBIC_INDEX
);
6016 tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
)
6018 struct inpcb
*inp
= sotoinpcb(so
);
6019 struct tcpcb
*tp
= intotcpcb(inp
);
6020 u_char old_cc_index
= 0;
6021 if (tp
->tcp_cc_index
!= cc_index
) {
6023 old_cc_index
= tp
->tcp_cc_index
;
6025 if (CC_ALGO(tp
)->cleanup
!= NULL
)
6026 CC_ALGO(tp
)->cleanup(tp
);
6027 tp
->tcp_cc_index
= cc_index
;
6029 tcp_cc_allocate_state(tp
);
6031 if (CC_ALGO(tp
)->switch_to
!= NULL
)
6032 CC_ALGO(tp
)->switch_to(tp
, old_cc_index
);
6034 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CHANGE_ALGO
);
6039 tcp_set_recv_bg(struct socket
*so
)
6041 if (!IS_TCP_RECV_BG(so
))
6042 so
->so_traffic_mgt_flags
|= TRAFFIC_MGT_TCP_RECVBG
;
6044 /* Unset Large Receive Offload on background sockets */
6045 so_set_lro(so
, SO_TC_BK
);
6049 tcp_clear_recv_bg(struct socket
*so
)
6051 if (IS_TCP_RECV_BG(so
))
6052 so
->so_traffic_mgt_flags
&= ~(TRAFFIC_MGT_TCP_RECVBG
);
6055 * Set/unset use of Large Receive Offload depending on
6058 so_set_lro(so
, so
->so_traffic_class
);
6062 inp_fc_unthrottle_tcp(struct inpcb
*inp
)
6064 struct tcpcb
*tp
= inp
->inp_ppcb
;
6066 * Back off the slow-start threshold and enter
6067 * congestion avoidance phase
6069 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
6070 CC_ALGO(tp
)->pre_fr(tp
);
6072 tp
->snd_cwnd
= tp
->snd_ssthresh
;
6073 tp
->t_flagsext
&= ~TF_CWND_NONVALIDATED
;
6075 * Restart counting for ABC as we changed the
6076 * congestion window just now.
6078 tp
->t_bytes_acked
= 0;
6080 /* Reset retransmit shift as we know that the reason
6081 * for delay in sending a packet is due to flow
6082 * control on the outgoing interface. There is no need
6083 * to backoff retransmit timer.
6089 * Start the output stream again. Since we are
6090 * not retransmitting data, do not reset the
6091 * retransmit timer or rtt calculation.
6097 tcp_getstat SYSCTL_HANDLER_ARGS
6099 #pragma unused(oidp, arg1, arg2)
6103 proc_t caller
= PROC_NULL
;
6104 proc_t caller_parent
= PROC_NULL
;
6105 char command_name
[MAXCOMLEN
+ 1] = "";
6106 char parent_name
[MAXCOMLEN
+ 1] = "";
6108 if ((caller
= proc_self()) != PROC_NULL
) {
6109 /* get process name */
6110 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
6112 /* get parent process name if possible */
6113 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
6114 strlcpy(parent_name
, caller_parent
->p_comm
,
6115 sizeof(parent_name
));
6116 proc_rele(caller_parent
);
6119 if ((escape_str(command_name
, strlen(command_name
),
6120 sizeof(command_name
)) == 0) &&
6121 (escape_str(parent_name
, strlen(parent_name
),
6122 sizeof(parent_name
)) == 0)) {
6123 kern_asl_msg(LOG_DEBUG
, "messagetracer",
6125 "com.apple.message.domain",
6126 "com.apple.kernel.tcpstat", /* 1 */
6127 "com.apple.message.signature",
6129 "com.apple.message.signature2", command_name
, /* 3 */
6130 "com.apple.message.signature3", parent_name
, /* 4 */
6131 "com.apple.message.summarize", "YES", /* 5 */
6135 if (caller
!= PROC_NULL
)
6138 if (req
->oldptr
== 0) {
6139 req
->oldlen
= (size_t)sizeof(struct tcpstat
);
6142 error
= SYSCTL_OUT(req
, &tcpstat
, MIN(sizeof (tcpstat
), req
->oldlen
));
6149 * Checksum extended TCP header and data.
6152 tcp_input_checksum(int af
, struct mbuf
*m
, struct tcphdr
*th
, int off
, int tlen
)
6154 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
6158 struct ip
*ip
= mtod(m
, struct ip
*);
6159 struct ipovly
*ipov
= (struct ipovly
*)ip
;
6161 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
6164 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6165 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6166 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6167 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6168 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6170 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
6171 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
6174 * Perform 1's complement adjustment of octets
6175 * that got included/excluded in the hardware-
6176 * calculated checksum value. Ignore cases
6177 * where the value includes or excludes the IP
6178 * header span, as the sum for those octets
6179 * would already be 0xffff and thus no-op.
6181 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6182 start
!= 0 && (off
- start
) != off
) {
6183 #if BYTE_ORDER != BIG_ENDIAN
6189 /* callee folds in sum */
6190 sum
= m_adj_sum16(m
, start
, off
, sum
);
6191 #if BYTE_ORDER != BIG_ENDIAN
6199 /* callee folds in sum */
6200 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
6202 sum
+ htonl(tlen
+ IPPROTO_TCP
));
6204 th
->th_sum
^= 0xffff;
6210 bcopy(ipov
->ih_x1
, b
, sizeof (ipov
->ih_x1
));
6211 bzero(ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
6212 ip_sum
= ipov
->ih_len
;
6213 ipov
->ih_len
= (u_short
)tlen
;
6214 #if BYTE_ORDER != BIG_ENDIAN
6215 HTONS(ipov
->ih_len
);
6217 len
= sizeof (struct ip
) + tlen
;
6218 th
->th_sum
= in_cksum(m
, len
);
6219 bcopy(b
, ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
6220 ipov
->ih_len
= ip_sum
;
6222 tcp_in_cksum_stats(len
);
6228 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
6230 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
6233 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6234 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6235 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6236 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6237 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6239 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
6240 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
6243 * Perform 1's complement adjustment of octets
6244 * that got included/excluded in the hardware-
6245 * calculated checksum value.
6247 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6251 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
6252 s
= ip6
->ip6_src
.s6_addr16
[1];
6253 ip6
->ip6_src
.s6_addr16
[1] = 0 ;
6255 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
6256 d
= ip6
->ip6_dst
.s6_addr16
[1];
6257 ip6
->ip6_dst
.s6_addr16
[1] = 0;
6260 /* callee folds in sum */
6261 sum
= m_adj_sum16(m
, start
, off
, sum
);
6263 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
))
6264 ip6
->ip6_src
.s6_addr16
[1] = s
;
6265 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
))
6266 ip6
->ip6_dst
.s6_addr16
[1] = d
;
6269 th
->th_sum
= in6_pseudo(
6270 &ip6
->ip6_src
, &ip6
->ip6_dst
,
6271 sum
+ htonl(tlen
+ IPPROTO_TCP
));
6273 th
->th_sum
^= 0xffff;
6275 tcp_in6_cksum_stats(tlen
);
6276 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, off
, tlen
);
6286 if (th
->th_sum
!= 0) {
6287 tcpstat
.tcps_rcvbadsum
++;
6288 IF_TCP_STATINC(ifp
, badformat
);
6295 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_STATS
, stats
,
6296 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, tcp_getstat
,
6297 "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
6300 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
6302 #pragma unused(arg1, arg2)
6304 int error
, val
= tcprexmtthresh
;
6306 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
6307 if (error
|| !req
->newptr
)
6311 * Constrain the number of duplicate ACKs
6312 * to consider for TCP fast retransmit
6316 if (val
< 2 || val
> 3)
6319 tcprexmtthresh
= val
;
6324 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, rexmt_thresh
, CTLTYPE_INT
| CTLFLAG_RW
|
6325 CTLFLAG_LOCKED
, &tcprexmtthresh
, 0, &sysctl_rexmtthresh
, "I",
6326 "Duplicate ACK Threshold for Fast Retransmit");