2 * Copyright (c) 2000-2016 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)
157 #define TCP_STRETCHACK_ENABLE_PKTCNT 2000
161 struct tcpstat tcpstat
;
163 static int log_in_vain
= 0;
164 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
,
165 CTLFLAG_RW
| CTLFLAG_LOCKED
, &log_in_vain
, 0,
166 "Log all incoming TCP connections");
168 static int blackhole
= 0;
169 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
,
170 CTLFLAG_RW
| CTLFLAG_LOCKED
, &blackhole
, 0,
171 "Do not send RST when dropping refused connections");
173 int tcp_delack_enabled
= 3;
174 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
,
175 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_delack_enabled
, 0,
176 "Delay ACK to try and piggyback it onto a data packet");
178 int tcp_lq_overflow
= 1;
179 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_lq_overflow
,
180 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_lq_overflow
, 0,
181 "Listen Queue Overflow");
184 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recvbg
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
185 &tcp_recv_bg
, 0, "Receive background");
188 static int drop_synfin
= 1;
189 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, drop_synfin
,
190 CTLFLAG_RW
| CTLFLAG_LOCKED
, &drop_synfin
, 0,
191 "Drop TCP packets with SYN+FIN set");
194 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
195 "TCP Segment Reassembly Queue");
197 static int tcp_reass_overflows
= 0;
198 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
,
199 CTLFLAG_RD
| CTLFLAG_LOCKED
, &tcp_reass_overflows
, 0,
200 "Global number of TCP Segment Reassembly Queue Overflows");
203 __private_extern__
int slowlink_wsize
= 8192;
204 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowlink_wsize
,
205 CTLFLAG_RW
| CTLFLAG_LOCKED
,
206 &slowlink_wsize
, 0, "Maximum advertised window size for slowlink");
208 int maxseg_unacked
= 8;
209 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, maxseg_unacked
,
210 CTLFLAG_RW
| CTLFLAG_LOCKED
, &maxseg_unacked
, 0,
211 "Maximum number of outstanding segments left unacked");
213 int tcp_do_rfc3465
= 1;
214 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
215 &tcp_do_rfc3465
, 0, "");
217 int tcp_do_rfc3465_lim2
= 1;
218 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465_lim2
,
219 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_do_rfc3465_lim2
, 0,
220 "Appropriate bytes counting w/ L=2*SMSS");
222 int rtt_samples_per_slot
= 20;
224 int tcp_allowed_iaj
= ALLOWED_IAJ
;
225 int tcp_acc_iaj_high_thresh
= ACC_IAJ_HIGH_THRESH
;
226 u_int32_t tcp_autorcvbuf_inc_shift
= 3;
227 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recv_allowed_iaj
,
228 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_allowed_iaj
, 0,
229 "Allowed inter-packet arrival jiter");
230 #if (DEVELOPMENT || DEBUG)
231 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_high_thresh
,
232 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_acc_iaj_high_thresh
, 0,
233 "Used in calculating maximum accumulated IAJ");
235 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufincshift
,
236 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_inc_shift
, 0,
237 "Shift for increment in receive socket buffer size");
238 #endif /* (DEVELOPMENT || DEBUG) */
240 u_int32_t tcp_do_autorcvbuf
= 1;
241 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, doautorcvbuf
,
242 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_do_autorcvbuf
, 0,
243 "Enable automatic socket buffer tuning");
245 u_int32_t tcp_autorcvbuf_max
= 512 * 1024;
246 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufmax
,
247 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_max
, 0,
248 "Maximum receive socket buffer size");
250 u_int32_t tcp_autorcvbuf_max_ca
= 512 * 1024;
251 #if (DEBUG || DEVELOPMENT)
252 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufmaxca
,
253 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_autorcvbuf_max_ca
, 0,
254 "Maximum receive socket buffer size");
255 #endif /* (DEBUG || DEVELOPMENT) */
258 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
259 &sw_lro
, 0, "Used to coalesce TCP packets");
262 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lrodbg
,
263 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lrodebug
, 0,
264 "Used to debug SW LRO");
267 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro_startcnt
,
268 CTLFLAG_RW
| CTLFLAG_LOCKED
, &lro_start
, 0,
269 "Segments for starting LRO computed as power of 2");
271 extern int tcp_do_autosendbuf
;
273 int limited_txmt
= 1;
276 int tcp_dsack_enable
= 1;
278 #if (DEVELOPMENT || DEBUG)
279 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, limited_transmit
,
280 CTLFLAG_RW
| CTLFLAG_LOCKED
, &limited_txmt
, 0,
281 "Enable limited transmit");
283 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, early_rexmt
,
284 CTLFLAG_RW
| CTLFLAG_LOCKED
, &early_rexmt
, 0,
285 "Enable Early Retransmit");
287 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_ackadv
,
288 CTLFLAG_RW
| CTLFLAG_LOCKED
, &sack_ackadv
, 0,
289 "Use SACK with cumulative ack advancement as a dupack");
291 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, dsack_enable
,
292 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_dsack_enable
, 0,
293 "use DSACK TCP option to report duplicate segments");
294 #endif /* (DEVELOPMENT || DEBUG) */
296 extern int tcp_TCPTV_MIN
;
297 extern int tcp_acc_iaj_high
;
298 extern int tcp_acc_iaj_react_limit
;
300 int tcprexmtthresh
= 3;
303 struct timeval tcp_uptime
; /* uptime when tcp_now was last updated */
304 lck_spin_t
*tcp_uptime_lock
; /* Used to sychronize updates to tcp_now */
306 struct inpcbhead tcb
;
307 #define tcb6 tcb /* for KAME src sync over BSD*'s */
308 struct inpcbinfo tcbinfo
;
310 static void tcp_dooptions(struct tcpcb
*, u_char
*, int, struct tcphdr
*,
312 static void tcp_finalize_options(struct tcpcb
*, struct tcpopt
*, unsigned int);
313 static void tcp_pulloutofband(struct socket
*,
314 struct tcphdr
*, struct mbuf
*, int);
315 static int tcp_reass(struct tcpcb
*, struct tcphdr
*, int *, struct mbuf
*,
317 static void tcp_xmit_timer(struct tcpcb
*, int, u_int32_t
, tcp_seq
);
318 static inline unsigned int tcp_maxmtu(struct rtentry
*);
319 static inline int tcp_stretch_ack_enable(struct tcpcb
*tp
, int thflags
);
320 static inline void tcp_adaptive_rwtimo_check(struct tcpcb
*, int);
323 static inline void update_iaj_state(struct tcpcb
*tp
, uint32_t tlen
,
325 void compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
);
326 static void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
);
327 #endif /* TRAFFIC_MGT */
330 static inline unsigned int tcp_maxmtu6(struct rtentry
*);
333 static void tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sb
,
334 struct tcpopt
*to
, u_int32_t tlen
, u_int32_t rcvbuf_max
);
335 void tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sb
);
336 static void tcp_sbsnd_trim(struct sockbuf
*sbsnd
);
337 static inline void tcp_sbrcv_tstmp_check(struct tcpcb
*tp
);
338 static inline void tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sb
,
339 u_int32_t newsize
, u_int32_t idealsize
, u_int32_t rcvbuf_max
);
340 static void tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
);
341 static void tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
,
343 static void tcp_early_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
);
344 static void tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
,
347 * Constants used for resizing receive socket buffer
348 * when timestamps are not supported
350 #define TCPTV_RCVNOTS_QUANTUM 100
351 #define TCP_RCVNOTS_BYTELEVEL 204800
354 * Constants used for limiting early retransmits
357 #define TCP_EARLY_REXMT_WIN (60 * TCP_RETRANSHZ) /* 60 seconds */
358 #define TCP_EARLY_REXMT_LIMIT 10
360 extern void ipfwsyslog( int level
, const char *format
,...);
361 extern int fw_verbose
;
364 extern void ipfw_stealth_stats_incr_tcp(void);
366 #define log_in_vain_log( a ) { \
367 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
369 } else if ( (log_in_vain == 4 ) && (fw_verbose == 2)) { \
370 ipfw_stealth_stats_incr_tcp(); \
375 #define log_in_vain_log( a ) { log a; }
378 int tcp_rcvunackwin
= TCPTV_UNACKWIN
;
379 int tcp_maxrcvidle
= TCPTV_MAXRCVIDLE
;
380 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
)
423 unsigned int sqrt_cache
[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
424 unsigned int temp
, g
=0, b
=0x8000, bshft
=15;
426 for (g
= 0; g
<= 10; ++g
) {
427 if (sqrt_cache
[g
] > val
) {
430 } else if (sqrt_cache
[g
] == val
) {
436 temp
= (((g
<< 1) + b
) << (bshft
--));
442 } while ( b
> 0 && val
> 0);
448 * With LRO, roughly estimate the inter arrival time between
449 * each sub coalesced packet as an average. Count the delay
450 * cur_iaj to be the delay between the last packet received
451 * and the first packet of the LRO stream. Due to round off errors
452 * cur_iaj may be the same as lro_delay_factor. Averaging has
453 * round off errors too. lro_delay_factor may be close to 0
454 * in steady state leading to lower values fed to compute_iaj_meat.
457 compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
)
459 uint32_t cur_iaj
= tcp_now
- tp
->iaj_rcv_ts
;
460 uint32_t timediff
= 0;
462 if (cur_iaj
>= lro_delay_factor
) {
463 cur_iaj
= cur_iaj
- lro_delay_factor
;
466 compute_iaj_meat(tp
, cur_iaj
);
473 timediff
= lro_delay_factor
/nlropkts
;
477 compute_iaj_meat(tp
, timediff
);
483 void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
)
485 /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds,
486 * throttle the receive window to a minimum of MIN_IAJ_WIN packets
488 #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
489 #define IAJ_DIV_SHIFT 4
490 #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1))
492 uint32_t allowed_iaj
, acc_iaj
= 0;
497 cur_iaj_dev
= (cur_iaj
- tp
->avg_iaj
);
499 /* Allow a jitter of "allowed_iaj" milliseconds. Some connections
500 * may have a constant jitter more than that. We detect this by
501 * using standard deviation.
503 allowed_iaj
= tp
->avg_iaj
+ tp
->std_dev_iaj
;
504 if (allowed_iaj
< tcp_allowed_iaj
)
505 allowed_iaj
= tcp_allowed_iaj
;
507 /* Initially when the connection starts, the senders congestion
508 * window is small. During this period we avoid throttling a
509 * connection because we do not have a good starting point for
510 * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over
511 * the first few packets.
513 if (tp
->iaj_pktcnt
> IAJ_IGNORE_PKTCNT
) {
514 if ( cur_iaj
<= allowed_iaj
) {
515 if (tp
->acc_iaj
>= 2)
516 acc_iaj
= tp
->acc_iaj
- 2;
521 acc_iaj
= tp
->acc_iaj
+ (cur_iaj
- allowed_iaj
);
524 if (acc_iaj
> MAX_ACC_IAJ
)
525 acc_iaj
= MAX_ACC_IAJ
;
526 tp
->acc_iaj
= acc_iaj
;
529 /* Compute weighted average where the history has a weight of
530 * 15 out of 16 and the current value has a weight of 1 out of 16.
531 * This will make the short-term measurements have more weight.
533 * The addition of 8 will help to round-up the value
534 * instead of round-down
536 tp
->avg_iaj
= (((tp
->avg_iaj
<< IAJ_DIV_SHIFT
) - tp
->avg_iaj
)
537 + cur_iaj
+ IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
539 /* Compute Root-mean-square of deviation where mean is a weighted
540 * average as described above.
542 temp
= tp
->std_dev_iaj
* tp
->std_dev_iaj
;
543 mean
= (((temp
<< IAJ_DIV_SHIFT
) - temp
)
544 + (cur_iaj_dev
* cur_iaj_dev
)
545 + IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
547 tp
->std_dev_iaj
= isqrt(mean
);
549 DTRACE_TCP3(iaj
, struct tcpcb
*, tp
, uint32_t, cur_iaj
,
550 uint32_t, allowed_iaj
);
554 #endif /* TRAFFIC_MGT */
556 /* Check if enough amount of data has been acknowledged since
557 * bw measurement was started
560 tcp_bwmeas_check(struct tcpcb
*tp
)
562 int32_t bw_meas_bytes
;
563 uint32_t bw
, bytes
, elapsed_time
;
564 bw_meas_bytes
= tp
->snd_una
- tp
->t_bwmeas
->bw_start
;
565 if ((tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) != 0 &&
566 bw_meas_bytes
>= (int32_t)(tp
->t_bwmeas
->bw_size
)) {
567 bytes
= bw_meas_bytes
;
568 elapsed_time
= tcp_now
- tp
->t_bwmeas
->bw_ts
;
569 if (elapsed_time
> 0) {
570 bw
= bytes
/ elapsed_time
;
572 if (tp
->t_bwmeas
->bw_sndbw
> 0) {
573 tp
->t_bwmeas
->bw_sndbw
=
574 (((tp
->t_bwmeas
->bw_sndbw
<< 3) - tp
->t_bwmeas
->bw_sndbw
) + bw
) >> 3;
576 tp
->t_bwmeas
->bw_sndbw
= bw
;
580 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
585 tcp_reass(struct tcpcb
*tp
, struct tcphdr
*th
, int *tlenp
, struct mbuf
*m
,
589 struct tseg_qent
*p
= NULL
;
590 struct tseg_qent
*nq
;
591 struct tseg_qent
*te
= NULL
;
592 struct inpcb
*inp
= tp
->t_inpcb
;
593 struct socket
*so
= inp
->inp_socket
;
596 struct mbuf
*oodata
= NULL
;
599 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
600 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
601 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
602 boolean_t dsack_set
= FALSE
;
605 * Call with th==0 after become established to
606 * force pre-ESTABLISHED data up to user socket.
612 * If the reassembly queue already has entries or if we are going
613 * to add a new one, then the connection has reached a loss state.
614 * Reset the stretch-ack algorithm at this point.
616 tcp_reset_stretch_ack(tp
);
621 #endif /* TRAFFIC_MGT */
624 * Limit the number of segments in the reassembly queue to prevent
625 * holding on to too many segments (and thus running out of mbufs).
626 * Make sure to let the missing segment through which caused this
627 * queue. Always keep one global queue entry spare to be able to
628 * process the missing segment.
630 qlimit
= min(max(100, so
->so_rcv
.sb_hiwat
>> 10),
631 (TCP_AUTORCVBUF_MAX(ifp
) >> 10));
632 if (th
->th_seq
!= tp
->rcv_nxt
&&
633 (tp
->t_reassqlen
+ 1) >= qlimit
) {
634 tcp_reass_overflows
++;
635 tcpstat
.tcps_rcvmemdrop
++;
641 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
642 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
644 tcpstat
.tcps_rcvmemdrop
++;
651 * Find a segment which begins after this one does.
653 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
654 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
))
660 * If there is a preceding segment, it may provide some of
661 * our data already. If so, drop the data from the incoming
662 * segment. If it provides all of our data, drop us.
666 /* conversion to int (in i) handles seq wraparound */
667 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
669 if (TCP_DSACK_ENABLED(tp
) && i
> 1) {
671 * Note duplicate data sequnce numbers
672 * to report in DSACK option
674 tp
->t_dsack_lseq
= th
->th_seq
;
675 tp
->t_dsack_rseq
= th
->th_seq
+
679 * Report only the first part of partial/
680 * non-contiguous duplicate sequence space
685 tcpstat
.tcps_rcvduppack
++;
686 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
688 nstat_route_rx(inp
->inp_route
.ro_rt
,
690 NSTAT_RX_FLAG_DUPLICATE
);
691 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
693 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
695 tp
->t_stat
.rxduplicatebytes
+= *tlenp
;
698 zfree(tcp_reass_zone
, te
);
702 * Try to present any queued data
703 * at the left window edge to the user.
704 * This is needed after the 3-WHS
715 tcpstat
.tcps_rcvoopack
++;
716 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
718 nstat_route_rx(inp
->inp_route
.ro_rt
, 1, *tlenp
,
719 NSTAT_RX_FLAG_OUT_OF_ORDER
);
720 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
721 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, *tlenp
);
722 tp
->t_stat
.rxoutoforderbytes
+= *tlenp
;
726 * While we overlap succeeding segments trim them or,
727 * if they are completely covered, dequeue them.
730 int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
735 * Report only the first part of partial/non-contiguous
736 * duplicate segment in dsack option. The variable
737 * dsack_set will be true if a previous entry has some of
738 * the duplicate sequence space.
740 if (TCP_DSACK_ENABLED(tp
) && i
> 1 && !dsack_set
) {
741 if (tp
->t_dsack_lseq
== 0) {
742 tp
->t_dsack_lseq
= q
->tqe_th
->th_seq
;
744 tp
->t_dsack_lseq
+ min(i
, q
->tqe_len
);
747 * this segment overlaps data in multple
748 * entries in the reassembly queue, move
749 * the right sequence number further.
752 tp
->t_dsack_rseq
+ min(i
, q
->tqe_len
);
755 if (i
< q
->tqe_len
) {
756 q
->tqe_th
->th_seq
+= i
;
762 nq
= LIST_NEXT(q
, tqe_q
);
763 LIST_REMOVE(q
, tqe_q
);
765 zfree(tcp_reass_zone
, q
);
770 /* Insert the new segment queue entry into place. */
773 te
->tqe_len
= *tlenp
;
776 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
778 LIST_INSERT_AFTER(p
, te
, tqe_q
);
782 * New out-of-order data exists, and is pointed to by
783 * queue entry te. Set copy_oodata to 1 so out-of-order data
784 * can be copied off to sockbuf after in-order data
787 if (!(so
->so_state
& SS_CANTRCVMORE
))
792 * Present data to user, advancing rcv_nxt through
793 * completed sequence space.
795 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
797 q
= LIST_FIRST(&tp
->t_segq
);
798 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
) {
799 /* Stop using LRO once out of order packets arrive */
800 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
801 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
802 th
->th_dport
, th
->th_sport
);
803 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
807 * continue processing if out-of-order data
810 if (q
&& (so
->so_flags
& SOF_ENABLE_MSGS
))
811 goto msg_unordered_delivery
;
817 * If there is already another thread doing reassembly for this
818 * connection, it is better to let it finish the job --
821 if (tp
->t_flagsext
& TF_REASS_INPROG
)
824 tp
->t_flagsext
|= TF_REASS_INPROG
;
825 /* lost packet was recovered, so ooo data can be returned */
826 tcpstat
.tcps_recovered_pkts
++;
829 tp
->rcv_nxt
+= q
->tqe_len
;
830 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
831 LIST_REMOVE(q
, tqe_q
);
832 if (so
->so_state
& SS_CANTRCVMORE
) {
835 so_recv_data_stat(so
, q
->tqe_m
, 0); /* XXXX */
836 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
838 * Append the inorder data as a message to the
839 * receive socket buffer. Also check to see if
840 * the data we are about to deliver is the same
841 * data that we wanted to pass up to the user
842 * out of order. If so, reset copy_oodata --
843 * the received data filled a gap, and
849 if (sbappendstream_rcvdemux(so
, q
->tqe_m
,
850 q
->tqe_th
->th_seq
- (tp
->irs
+ 1), 0))
852 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
853 tcp_update_lro_seq(tp
->rcv_nxt
,
854 inp
->inp_laddr
, inp
->inp_faddr
,
855 th
->th_dport
, th
->th_sport
);
858 zfree(tcp_reass_zone
, q
);
860 q
= LIST_FIRST(&tp
->t_segq
);
861 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
862 tp
->t_flagsext
&= ~TF_REASS_INPROG
;
865 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
867 KERNEL_DEBUG(DBG_LAYER_BEG
,
868 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
869 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
870 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
876 KERNEL_DEBUG(DBG_LAYER_BEG
,
877 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
878 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
879 (inp
->inp_faddr
.s_addr
& 0xffff)),
883 msg_unordered_delivery
:
884 /* Deliver out-of-order data as a message */
885 if (te
&& (so
->so_flags
& SOF_ENABLE_MSGS
) && copy_oodata
&& te
->tqe_len
) {
887 * make a copy of the mbuf to be delivered up to
888 * the user, and add it to the sockbuf
890 oodata
= m_copym(te
->tqe_m
, 0, M_COPYALL
, M_DONTWAIT
);
891 if (oodata
!= NULL
) {
892 if (sbappendmsgstream_rcv(&so
->so_rcv
, oodata
,
893 te
->tqe_th
->th_seq
- (tp
->irs
+ 1), 1)) {
895 tcpstat
.tcps_msg_unopkts
++;
897 tcpstat
.tcps_msg_unoappendfail
++;
903 sorwakeup(so
); /* done with socket lock held */
908 * Reduce congestion window -- used when ECN is seen or when a tail loss
909 * probe recovers the last packet.
912 tcp_reduce_congestion_window(
916 * If the current tcp cc module has
917 * defined a hook for tasks to run
918 * before entering FR, call it
920 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
921 CC_ALGO(tp
)->pre_fr(tp
);
922 ENTER_FASTRECOVERY(tp
);
923 if (tp
->t_flags
& TF_SENTFIN
)
924 tp
->snd_recover
= tp
->snd_max
- 1;
926 tp
->snd_recover
= tp
->snd_max
;
927 tp
->t_timer
[TCPT_REXMT
] = 0;
928 tp
->t_timer
[TCPT_PTO
] = 0;
930 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
931 tcp_cc_adjust_nonvalidated_cwnd(tp
);
933 tp
->snd_cwnd
= tp
->snd_ssthresh
+
934 tp
->t_maxseg
* tcprexmtthresh
;
939 * This function is called upon reception of data on a socket. It's purpose is
940 * to handle the adaptive keepalive timers that monitor whether the connection
941 * is making progress. First the adaptive read-timer, second the TFO probe-timer.
943 * The application wants to get an event if there is a stall during read.
944 * Set the initial keepalive timeout to be equal to twice RTO.
946 * If the outgoing interface is in marginal conditions, we need to
947 * enable read probes for that too.
950 tcp_adaptive_rwtimo_check(struct tcpcb
*tp
, int tlen
)
952 struct ifnet
*outifp
= tp
->t_inpcb
->inp_last_outifp
;
954 if ((tp
->t_adaptive_rtimo
> 0 ||
956 (outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
)))
958 tp
->t_state
== TCPS_ESTABLISHED
) {
959 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
960 (TCP_REXMTVAL(tp
) << 1));
961 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
962 tp
->t_rtimo_probes
= 0;
967 tcp_keepalive_reset(struct tcpcb
*tp
)
969 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
970 TCP_CONN_KEEPIDLE(tp
));
971 tp
->t_flagsext
&= ~(TF_DETECT_READSTALL
);
972 tp
->t_rtimo_probes
= 0;
976 * TCP input routine, follows pages 65-76 of the
977 * protocol specification dated September, 1981 very closely.
981 tcp6_input(struct mbuf
**mp
, int *offp
, int proto
)
983 #pragma unused(proto)
984 struct mbuf
*m
= *mp
;
986 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
988 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
990 /* Expect 32-bit aligned data pointer on strict-align platforms */
991 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
994 * draft-itojun-ipv6-tcp-to-anycast
995 * better place to put this in?
997 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) == 0) {
998 if (ia6_flags
& IN6_IFF_ANYCAST
) {
1001 ip6
= mtod(m
, struct ip6_hdr
*);
1002 icmp6_error(m
, ICMP6_DST_UNREACH
,
1003 ICMP6_DST_UNREACH_ADDR
,
1004 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
1006 IF_TCP_STATINC(ifp
, icmp6unreach
);
1008 return (IPPROTO_DONE
);
1012 tcp_input(m
, *offp
);
1013 return (IPPROTO_DONE
);
1017 /* Depending on the usage of mbuf space in the system, this function
1018 * will return true or false. This is used to determine if a socket
1019 * buffer can take more memory from the system for auto-tuning or not.
1022 tcp_cansbgrow(struct sockbuf
*sb
)
1024 /* Calculate the host level space limit in terms of MSIZE buffers.
1025 * We can use a maximum of half of the available mbuf space for
1028 u_int32_t mblim
= ((nmbclusters
>> 1) << (MCLSHIFT
- MSIZESHIFT
));
1030 /* Calculate per sb limit in terms of bytes. We optimize this limit
1031 * for upto 16 socket buffers.
1034 u_int32_t sbspacelim
= ((nmbclusters
>> 4) << MCLSHIFT
);
1036 if ((total_sbmb_cnt
< mblim
) &&
1037 (sb
->sb_hiwat
< sbspacelim
)) {
1040 OSIncrementAtomic64(&sbmb_limreached
);
1046 tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1047 u_int32_t newsize
, u_int32_t idealsize
, u_int32_t rcvbuf_max
)
1049 /* newsize should not exceed max */
1050 newsize
= min(newsize
, rcvbuf_max
);
1052 /* The receive window scale negotiated at the
1053 * beginning of the connection will also set a
1054 * limit on the socket buffer size
1056 newsize
= min(newsize
, TCP_MAXWIN
<< tp
->rcv_scale
);
1058 /* Set new socket buffer size */
1059 if (newsize
> sbrcv
->sb_hiwat
&&
1060 (sbreserve(sbrcv
, newsize
) == 1)) {
1061 sbrcv
->sb_idealsize
= min(max(sbrcv
->sb_idealsize
,
1062 (idealsize
!= 0) ? idealsize
: newsize
), rcvbuf_max
);
1064 /* Again check the limit set by the advertised
1067 sbrcv
->sb_idealsize
= min(sbrcv
->sb_idealsize
,
1068 TCP_MAXWIN
<< tp
->rcv_scale
);
1073 * This function is used to grow a receive socket buffer. It
1074 * will take into account system-level memory usage and the
1075 * bandwidth available on the link to make a decision.
1078 tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
1079 struct tcpopt
*to
, u_int32_t pktlen
, u_int32_t rcvbuf_max
)
1081 struct socket
*so
= sbrcv
->sb_so
;
1084 * Do not grow the receive socket buffer if
1085 * - auto resizing is disabled, globally or on this socket
1086 * - the high water mark already reached the maximum
1087 * - the stream is in background and receive side is being
1089 * - if there are segments in reassembly queue indicating loss,
1090 * do not need to increase recv window during recovery as more
1091 * data is not going to be sent. A duplicate ack sent during
1092 * recovery should not change the receive window
1094 if (tcp_do_autorcvbuf
== 0 ||
1095 (sbrcv
->sb_flags
& SB_AUTOSIZE
) == 0 ||
1096 tcp_cansbgrow(sbrcv
) == 0 ||
1097 sbrcv
->sb_hiwat
>= rcvbuf_max
||
1098 (tp
->t_flagsext
& TF_RECV_THROTTLE
) ||
1099 (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) ||
1100 !LIST_EMPTY(&tp
->t_segq
)) {
1101 /* Can not resize the socket buffer, just return */
1105 if (TSTMP_GT(tcp_now
,
1106 tp
->rfbuf_ts
+ TCPTV_RCVBUFIDLE
)) {
1107 /* If there has been an idle period in the
1108 * connection, just restart the measurement
1113 if (!TSTMP_SUPPORTED(tp
)) {
1115 * Timestamp option is not supported on this connection.
1116 * If the connection reached a state to indicate that
1117 * the receive socket buffer needs to grow, increase
1118 * the high water mark.
1120 if (TSTMP_GEQ(tcp_now
,
1121 tp
->rfbuf_ts
+ TCPTV_RCVNOTS_QUANTUM
)) {
1122 if (tp
->rfbuf_cnt
>= TCP_RCVNOTS_BYTELEVEL
) {
1123 tcp_sbrcv_reserve(tp
, sbrcv
,
1124 tcp_autorcvbuf_max
, 0,
1125 tcp_autorcvbuf_max
);
1129 tp
->rfbuf_cnt
+= pktlen
;
1132 } else if (to
->to_tsecr
!= 0) {
1134 * If the timestamp shows that one RTT has
1135 * completed, we can stop counting the
1136 * bytes. Here we consider increasing
1137 * the socket buffer if the bandwidth measured in
1138 * last rtt, is more than half of sb_hiwat, this will
1139 * help to scale the buffer according to the bandwidth
1142 if (TSTMP_GEQ(to
->to_tsecr
, tp
->rfbuf_ts
)) {
1143 if (tp
->rfbuf_cnt
> (sbrcv
->sb_hiwat
-
1144 (sbrcv
->sb_hiwat
>> 1))) {
1145 int32_t rcvbuf_inc
, min_incr
;
1147 * Increment the receive window by a
1148 * multiple of maximum sized segments.
1149 * This will prevent a connection from
1150 * sending smaller segments on wire if it
1151 * is limited by the receive window.
1153 * Set the ideal size based on current
1154 * bandwidth measurements. We set the
1155 * ideal size on receive socket buffer to
1156 * be twice the bandwidth delay product.
1158 rcvbuf_inc
= (tp
->rfbuf_cnt
<< 1)
1162 * Make the increment equal to 8 segments
1165 min_incr
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1166 if (rcvbuf_inc
< min_incr
)
1167 rcvbuf_inc
= min_incr
;
1170 (rcvbuf_inc
/ tp
->t_maxseg
) * tp
->t_maxseg
;
1171 tcp_sbrcv_reserve(tp
, sbrcv
,
1172 sbrcv
->sb_hiwat
+ rcvbuf_inc
,
1173 (tp
->rfbuf_cnt
* 2), rcvbuf_max
);
1177 tp
->rfbuf_cnt
+= pktlen
;
1182 /* Restart the measurement */
1188 /* This function will trim the excess space added to the socket buffer
1189 * to help a slow-reading app. The ideal-size of a socket buffer depends
1190 * on the link bandwidth or it is set by an application and we aim to
1194 tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sbrcv
)
1196 if (tcp_do_autorcvbuf
== 1 && sbrcv
->sb_idealsize
> 0 &&
1197 sbrcv
->sb_hiwat
> sbrcv
->sb_idealsize
) {
1199 /* compute the difference between ideal and current sizes */
1200 u_int32_t diff
= sbrcv
->sb_hiwat
- sbrcv
->sb_idealsize
;
1202 /* Compute the maximum advertised window for
1205 u_int32_t advwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
1207 /* How much can we trim the receive socket buffer?
1208 * 1. it can not be trimmed beyond the max rcv win advertised
1209 * 2. if possible, leave 1/16 of bandwidth*delay to
1210 * avoid closing the win completely
1212 u_int32_t leave
= max(advwin
, (sbrcv
->sb_idealsize
>> 4));
1214 /* Sometimes leave can be zero, in that case leave at least
1215 * a few segments worth of space.
1218 leave
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1220 trim
= sbrcv
->sb_hiwat
- (sbrcv
->sb_cc
+ leave
);
1221 trim
= imin(trim
, (int32_t)diff
);
1224 sbreserve(sbrcv
, (sbrcv
->sb_hiwat
- trim
));
1228 /* We may need to trim the send socket buffer size for two reasons:
1229 * 1. if the rtt seen on the connection is climbing up, we do not
1230 * want to fill the buffers any more.
1231 * 2. if the congestion win on the socket backed off, there is no need
1232 * to hold more mbufs for that connection than what the cwnd will allow.
1235 tcp_sbsnd_trim(struct sockbuf
*sbsnd
)
1237 if (tcp_do_autosendbuf
== 1 &&
1238 ((sbsnd
->sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) ==
1239 (SB_AUTOSIZE
| SB_TRIM
)) &&
1240 (sbsnd
->sb_idealsize
> 0) &&
1241 (sbsnd
->sb_hiwat
> sbsnd
->sb_idealsize
)) {
1243 if (sbsnd
->sb_cc
<= sbsnd
->sb_idealsize
) {
1244 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_idealsize
;
1246 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_cc
;
1248 sbreserve(sbsnd
, (sbsnd
->sb_hiwat
- trim
));
1250 if (sbsnd
->sb_hiwat
<= sbsnd
->sb_idealsize
)
1251 sbsnd
->sb_flags
&= ~(SB_TRIM
);
1255 * If timestamp option was not negotiated on this connection
1256 * and this connection is on the receiving side of a stream
1257 * then we can not measure the delay on the link accurately.
1258 * Instead of enabling automatic receive socket buffer
1259 * resizing, just give more space to the receive socket buffer.
1262 tcp_sbrcv_tstmp_check(struct tcpcb
*tp
)
1264 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1265 u_int32_t newsize
= 2 * tcp_recvspace
;
1266 struct sockbuf
*sbrcv
= &so
->so_rcv
;
1268 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_RCVD_TSTMP
)) !=
1269 (TF_REQ_TSTMP
| TF_RCVD_TSTMP
) &&
1270 (sbrcv
->sb_flags
& SB_AUTOSIZE
) != 0) {
1271 tcp_sbrcv_reserve(tp
, sbrcv
, newsize
, 0, newsize
);
1275 /* A receiver will evaluate the flow of packets on a connection
1276 * to see if it can reduce ack traffic. The receiver will start
1277 * stretching acks if all of the following conditions are met:
1278 * 1. tcp_delack_enabled is set to 3
1279 * 2. If the bytes received in the last 100ms is greater than a threshold
1280 * defined by maxseg_unacked
1281 * 3. If the connection has not been idle for tcp_maxrcvidle period.
1282 * 4. If the connection has seen enough packets to let the slow-start
1283 * finish after connection establishment or after some packet loss.
1285 * The receiver will stop stretching acks if there is congestion/reordering
1286 * as indicated by packets on reassembly queue or an ECN. If the delayed-ack
1287 * timer fires while stretching acks, it means that the packet flow has gone
1288 * below the threshold defined by maxseg_unacked and the receiver will stop
1289 * stretching acks. The receiver gets no indication when slow-start is completed
1290 * or when the connection reaches an idle state. That is why we use
1291 * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle
1295 tcp_stretch_ack_enable(struct tcpcb
*tp
, int thflags
)
1297 if (tp
->rcv_by_unackwin
>= (maxseg_unacked
* tp
->t_maxseg
) &&
1298 TSTMP_GEQ(tp
->rcv_unackwin
, tcp_now
))
1299 tp
->t_flags
|= TF_STREAMING_ON
;
1301 tp
->t_flags
&= ~TF_STREAMING_ON
;
1303 /* If there has been an idle time, reset streaming detection */
1304 if (TSTMP_GT(tcp_now
, tp
->rcv_unackwin
+ tcp_maxrcvidle
))
1305 tp
->t_flags
&= ~TF_STREAMING_ON
;
1308 * If there are flags other than TH_ACK set, reset streaming
1311 if (thflags
& ~TH_ACK
)
1312 tp
->t_flags
&= ~TF_STREAMING_ON
;
1314 if (tp
->t_flagsext
& TF_DISABLE_STRETCHACK
) {
1315 if (tp
->rcv_nostrack_pkts
>= TCP_STRETCHACK_ENABLE_PKTCNT
) {
1316 tp
->t_flagsext
&= ~TF_DISABLE_STRETCHACK
;
1317 tp
->rcv_nostrack_pkts
= 0;
1318 tp
->rcv_nostrack_ts
= 0;
1320 tp
->rcv_nostrack_pkts
++;
1324 if (!(tp
->t_flagsext
& (TF_NOSTRETCHACK
|TF_DISABLE_STRETCHACK
)) &&
1325 (tp
->t_flags
& TF_STREAMING_ON
) &&
1326 (!(tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) ||
1327 (tp
->rcv_waitforss
>= tcp_rcvsspktcnt
))) {
1335 * Reset the state related to stretch-ack algorithm. This will make
1336 * the receiver generate an ack every other packet. The receiver
1337 * will start re-evaluating the rate at which packets come to decide
1338 * if it can benefit by lowering the ack traffic.
1341 tcp_reset_stretch_ack(struct tcpcb
*tp
)
1343 tp
->t_flags
&= ~(TF_STRETCHACK
|TF_STREAMING_ON
);
1344 tp
->rcv_by_unackwin
= 0;
1345 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
1348 * When there is packet loss or packet re-ordering or CWR due to
1349 * ECN, the sender's congestion window is reduced. In these states,
1350 * generate an ack for every other packet for some time to allow
1351 * the sender's congestion window to grow.
1353 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
1354 tp
->rcv_waitforss
= 0;
1358 * The last packet was a retransmission, check if this ack
1359 * indicates that the retransmission was spurious.
1361 * If the connection supports timestamps, we could use it to
1362 * detect if the last retransmit was not needed. Otherwise,
1363 * we check if the ACK arrived within RTT/2 window, then it
1364 * was a mistake to do the retransmit in the first place.
1366 * This function will return 1 if it is a spurious retransmit,
1370 tcp_detect_bad_rexmt(struct tcpcb
*tp
, struct tcphdr
*th
,
1371 struct tcpopt
*to
, u_int32_t rxtime
)
1373 int32_t tdiff
, bad_rexmt_win
;
1374 bad_rexmt_win
= (tp
->t_srtt
>> (TCP_RTT_SHIFT
+ 1));
1376 /* If the ack has ECN CE bit, then cwnd has to be adjusted */
1377 if (TCP_ECN_ENABLED(tp
) && (th
->th_flags
& TH_ECE
))
1379 if (TSTMP_SUPPORTED(tp
)) {
1380 if (rxtime
> 0 && (to
->to_flags
& TOF_TS
)
1381 && to
->to_tsecr
!= 0
1382 && TSTMP_LT(to
->to_tsecr
, rxtime
))
1385 if ((tp
->t_rxtshift
== 1
1386 || (tp
->t_flagsext
& TF_SENT_TLPROBE
))
1388 tdiff
= (int32_t)(tcp_now
- rxtime
);
1389 if (tdiff
< bad_rexmt_win
)
1398 * Restore congestion window state if a spurious timeout
1402 tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
)
1404 if (TSTMP_SUPPORTED(tp
)) {
1405 u_int32_t fsize
, acked
;
1406 fsize
= tp
->snd_max
- th
->th_ack
;
1407 acked
= BYTES_ACKED(th
, tp
);
1410 * Implement bad retransmit recovery as
1411 * described in RFC 4015.
1413 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1415 /* Initialize cwnd to the initial window */
1416 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
1417 CC_ALGO(tp
)->cwnd_init(tp
);
1419 tp
->snd_cwnd
= fsize
+ min(acked
, tp
->snd_cwnd
);
1422 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1423 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1424 if (tp
->t_flags
& TF_WASFRECOVERY
)
1425 ENTER_FASTRECOVERY(tp
);
1427 /* Do not use the loss flight size in this case */
1428 tp
->t_lossflightsize
= 0;
1430 tp
->snd_cwnd
= max(tp
->snd_cwnd
, TCP_CC_CWND_INIT_BYTES
);
1431 tp
->snd_recover
= tp
->snd_recover_prev
;
1432 tp
->snd_nxt
= tp
->snd_max
;
1434 /* Fix send socket buffer to reflect the change in cwnd */
1435 tcp_bad_rexmt_fix_sndbuf(tp
);
1438 * This RTT might reflect the extra delay induced
1439 * by the network. Skip using this sample for RTO
1440 * calculation and mark the connection so we can
1441 * recompute RTT when the next eligible sample is
1444 tp
->t_flagsext
|= TF_RECOMPUTE_RTT
;
1445 tp
->t_badrexmt_time
= tcp_now
;
1450 * If the previous packet was sent in retransmission timer, and it was
1451 * not needed, then restore the congestion window to the state before that
1454 * If the last packet was sent in tail loss probe timeout, check if that
1455 * recovered the last packet. If so, that will indicate a real loss and
1456 * the congestion window needs to be lowered.
1459 tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
, struct tcpopt
*to
)
1461 if (tp
->t_rxtshift
> 0 &&
1462 tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_rxtstart
)) {
1463 ++tcpstat
.tcps_sndrexmitbad
;
1464 tcp_bad_rexmt_restore_state(tp
, th
);
1465 tcp_ccdbg_trace(tp
, th
, TCP_CC_BAD_REXMT_RECOVERY
);
1466 } else if ((tp
->t_flagsext
& TF_SENT_TLPROBE
)
1467 && tp
->t_tlphighrxt
> 0
1468 && SEQ_GEQ(th
->th_ack
, tp
->t_tlphighrxt
)
1469 && !tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_tlpstart
)) {
1471 * check DSACK information also to make sure that
1472 * the TLP was indeed needed
1474 if (tcp_rxtseg_dsack_for_tlp(tp
)) {
1476 * received a DSACK to indicate that TLP was
1479 tcp_rxtseg_clean(tp
);
1484 * The tail loss probe recovered the last packet and
1485 * we need to adjust the congestion window to take
1486 * this loss into account.
1488 ++tcpstat
.tcps_tlp_recoverlastpkt
;
1489 if (!IN_FASTRECOVERY(tp
)) {
1490 tcp_reduce_congestion_window(tp
);
1491 EXIT_FASTRECOVERY(tp
);
1493 tcp_ccdbg_trace(tp
, th
, TCP_CC_TLP_RECOVER_LASTPACKET
);
1494 } else if (tcp_rxtseg_detect_bad_rexmt(tp
, th
->th_ack
)) {
1496 * All of the retransmitted segments were duplicated, this
1497 * can be an indication of bad fast retransmit.
1499 tcpstat
.tcps_dsack_badrexmt
++;
1500 tcp_bad_rexmt_restore_state(tp
, th
);
1501 tcp_ccdbg_trace(tp
, th
, TCP_CC_DSACK_BAD_REXMT
);
1502 tcp_rxtseg_clean(tp
);
1505 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1506 tp
->t_tlphighrxt
= 0;
1510 * check if the latest ack was for a segment sent during PMTU
1511 * blackhole detection. If the timestamp on the ack is before
1512 * PMTU blackhole detection, then revert the size of the max
1513 * segment to previous size.
1515 if (tp
->t_rxtshift
> 0 && (tp
->t_flags
& TF_BLACKHOLE
) &&
1516 tp
->t_pmtud_start_ts
> 0 && TSTMP_SUPPORTED(tp
)) {
1517 if ((to
->to_flags
& TOF_TS
) && to
->to_tsecr
!= 0
1518 && TSTMP_LT(to
->to_tsecr
, tp
->t_pmtud_start_ts
)) {
1519 tcp_pmtud_revert_segment_size(tp
);
1522 if (tp
->t_pmtud_start_ts
> 0)
1523 tp
->t_pmtud_start_ts
= 0;
1527 * Check if early retransmit can be attempted according to RFC 5827.
1529 * If packet reordering is detected on a connection, fast recovery will
1530 * be delayed until it is clear that the packet was lost and not reordered.
1531 * But reordering detection is done only when SACK is enabled.
1533 * On connections that do not support SACK, there is a limit on the number
1534 * of early retransmits that can be done per minute. This limit is needed
1535 * to make sure that too many packets are not retransmitted when there is
1536 * packet reordering.
1539 tcp_early_rexmt_check (struct tcpcb
*tp
, struct tcphdr
*th
)
1541 u_int32_t obytes
, snd_off
;
1543 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1545 if (early_rexmt
&& (SACK_ENABLED(tp
) ||
1546 tp
->t_early_rexmt_count
< TCP_EARLY_REXMT_LIMIT
) &&
1547 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1548 (tp
->t_dupacks
== 1 ||
1549 (SACK_ENABLED(tp
) &&
1550 !TAILQ_EMPTY(&tp
->snd_holes
)))) {
1552 * If there are only a few outstanding
1553 * segments on the connection, we might need
1554 * to lower the retransmit threshold. This
1555 * will allow us to do Early Retransmit as
1556 * described in RFC 5827.
1558 if (SACK_ENABLED(tp
) &&
1559 !TAILQ_EMPTY(&tp
->snd_holes
)) {
1560 obytes
= (tp
->snd_max
- tp
->snd_fack
) +
1561 tp
->sackhint
.sack_bytes_rexmit
;
1563 obytes
= (tp
->snd_max
- tp
->snd_una
);
1567 * In order to lower retransmit threshold the
1568 * following two conditions must be met.
1569 * 1. the amount of outstanding data is less
1571 * 2. there is no unsent data ready for
1572 * transmission or the advertised window
1573 * will limit sending new segments.
1575 snd_off
= tp
->snd_max
- tp
->snd_una
;
1576 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
) - snd_off
;
1577 if (obytes
< (tp
->t_maxseg
<< 2) &&
1581 osegs
= obytes
/ tp
->t_maxseg
;
1582 if ((osegs
* tp
->t_maxseg
) < obytes
)
1586 * Since the connection might have already
1587 * received some dupacks, we add them to
1588 * to the outstanding segments count to get
1589 * the correct retransmit threshold.
1591 * By checking for early retransmit after
1592 * receiving some duplicate acks when SACK
1593 * is supported, the connection will
1594 * enter fast recovery even if multiple
1595 * segments are lost in the same window.
1597 osegs
+= tp
->t_dupacks
;
1600 ((osegs
- 1) > 1) ? (osegs
- 1) : 1;
1602 min(tp
->t_rexmtthresh
, tcprexmtthresh
);
1604 max(tp
->t_rexmtthresh
, tp
->t_dupacks
);
1606 if (tp
->t_early_rexmt_count
== 0)
1607 tp
->t_early_rexmt_win
= tcp_now
;
1609 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1610 tcpstat
.tcps_tlp_recovery
++;
1611 tcp_ccdbg_trace(tp
, th
,
1612 TCP_CC_TLP_RECOVERY
);
1614 tcpstat
.tcps_early_rexmt
++;
1615 tp
->t_early_rexmt_count
++;
1616 tcp_ccdbg_trace(tp
, th
,
1617 TCP_CC_EARLY_RETRANSMIT
);
1624 * If we ever sent a TLP probe, the acknowledgement will trigger
1625 * early retransmit because the value of snd_fack will be close
1626 * to snd_max. This will take care of adjustments to the
1627 * congestion window. So we can reset TF_SENT_PROBE flag.
1629 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1630 tp
->t_tlphighrxt
= 0;
1635 tcp_tfo_syn(struct tcpcb
*tp
, struct tcpopt
*to
)
1637 u_char out
[CCAES_BLOCK_SIZE
];
1640 if (!(to
->to_flags
& (TOF_TFO
| TOF_TFOREQ
)) ||
1641 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
))
1644 if ((to
->to_flags
& TOF_TFOREQ
)) {
1645 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1647 tp
->t_tfo_stats
|= TFO_S_COOKIEREQ_RECV
;
1648 tcpstat
.tcps_tfo_cookie_req_rcv
++;
1652 /* Ok, then it must be an offered cookie. We need to check that ... */
1653 tcp_tfo_gen_cookie(tp
->t_inpcb
, out
, sizeof(out
));
1655 len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1657 if (memcmp(out
, to
->to_tfo
, len
)) {
1658 /* Cookies are different! Let's return and offer a new cookie */
1659 tp
->t_tfo_flags
|= TFO_F_OFFER_COOKIE
;
1661 tp
->t_tfo_stats
|= TFO_S_COOKIE_INVALID
;
1662 tcpstat
.tcps_tfo_cookie_invalid
++;
1666 if (OSIncrementAtomic(&tcp_tfo_halfcnt
) >= tcp_tfo_backlog
) {
1667 /* Need to decrement again as we just increased it... */
1668 OSDecrementAtomic(&tcp_tfo_halfcnt
);
1672 tp
->t_tfo_flags
|= TFO_F_COOKIE_VALID
;
1674 tp
->t_tfo_stats
|= TFO_S_SYNDATA_RCV
;
1675 tcpstat
.tcps_tfo_syn_data_rcv
++;
1681 tcp_tfo_synack(struct tcpcb
*tp
, struct tcpopt
*to
)
1683 if (to
->to_flags
& TOF_TFO
) {
1684 unsigned char len
= *to
->to_tfo
- TCPOLEN_FASTOPEN_REQ
;
1687 * If this happens, things have gone terribly wrong. len should
1688 * have been checked in tcp_dooptions.
1690 VERIFY(len
<= TFO_COOKIE_LEN_MAX
);
1694 tcp_cache_set_cookie(tp
, to
->to_tfo
, len
);
1695 tcp_heuristic_tfo_success(tp
);
1697 tp
->t_tfo_stats
|= TFO_S_COOKIE_RCV
;
1698 tcpstat
.tcps_tfo_cookie_rcv
++;
1699 if (tp
->t_tfo_flags
& TFO_F_COOKIE_SENT
) {
1700 tcpstat
.tcps_tfo_cookie_wrong
++;
1701 tp
->t_tfo_stats
|= TFO_S_COOKIE_WRONG
;
1705 * Thus, no cookie in the response, but we either asked for one
1706 * or sent SYN+DATA. Now, we need to check whether we had to
1707 * rexmit the SYN. If that's the case, it's better to start
1708 * backing of TFO-cookie requests.
1710 if (tp
->t_tfo_flags
& TFO_F_SYN_LOSS
) {
1711 tp
->t_tfo_stats
|= TFO_S_SYN_LOSS
;
1712 tcpstat
.tcps_tfo_syn_loss
++;
1714 tcp_heuristic_tfo_loss(tp
);
1716 if (tp
->t_tfo_flags
& TFO_F_COOKIE_REQ
) {
1717 tp
->t_tfo_stats
|= TFO_S_NO_COOKIE_RCV
;
1718 tcpstat
.tcps_tfo_no_cookie_rcv
++;
1721 tcp_heuristic_tfo_success(tp
);
1727 tcp_tfo_rcv_probe(struct tcpcb
*tp
, int tlen
)
1730 tp
->t_tfo_probe_state
= TFO_PROBE_PROBING
;
1733 * We send the probe out rather quickly (after one RTO). It does not
1734 * really hurt that much, it's only one additional segment on the wire.
1736 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, (TCP_REXMTVAL(tp
)));
1738 /* If SYN/ACK+data, don't probe. We got the data! */
1739 tcp_heuristic_tfo_rcv_good(tp
);
1744 tcp_tfo_rcv_data(struct tcpcb
*tp
)
1746 /* Transition from PROBING to NONE as data has been received */
1747 if (tp
->t_tfo_probe_state
>= TFO_PROBE_PROBING
) {
1748 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1750 /* Data has been received - we are good to go! */
1751 tcp_heuristic_tfo_rcv_good(tp
);
1756 tcp_tfo_rcv_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
1758 if (tp
->t_tfo_probe_state
== TFO_PROBE_PROBING
&&
1759 tp
->t_tfo_probes
> 0) {
1760 if (th
->th_seq
== tp
->rcv_nxt
) {
1761 /* No hole, so stop probing */
1762 tp
->t_tfo_probe_state
= TFO_PROBE_NONE
;
1763 } else if (SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
1764 /* There is a hole! Wait a bit for data... */
1765 tp
->t_tfo_probe_state
= TFO_PROBE_WAIT_DATA
;
1766 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1773 * Update snd_wnd information.
1776 tcp_update_window(struct tcpcb
*tp
, int thflags
, struct tcphdr
* th
,
1777 u_int32_t tiwin
, int tlen
)
1779 /* Don't look at the window if there is no ACK flag */
1780 if ((thflags
& TH_ACK
) &&
1781 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
1782 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
1783 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
1784 /* keep track of pure window updates */
1786 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
1787 tcpstat
.tcps_rcvwinupd
++;
1788 tp
->snd_wnd
= tiwin
;
1789 tp
->snd_wl1
= th
->th_seq
;
1790 tp
->snd_wl2
= th
->th_ack
;
1791 if (tp
->snd_wnd
> tp
->max_sndwnd
)
1792 tp
->max_sndwnd
= tp
->snd_wnd
;
1799 tcp_input(struct mbuf
*m
, int off0
)
1802 struct ip
*ip
= NULL
;
1804 u_char
*optp
= NULL
;
1808 struct tcpcb
*tp
= 0;
1810 struct socket
*so
= 0;
1811 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
1812 struct in_addr laddr
;
1814 struct in6_addr laddr6
;
1817 int iss
= 0, nosock
= 0;
1818 u_int32_t tiwin
, sack_bytes_acked
= 0;
1819 struct tcpopt to
; /* options in this segment */
1824 struct sockaddr_in
*next_hop
= NULL
;
1825 struct m_tag
*fwd_tag
;
1826 #endif /* IPFIREWALL */
1827 u_char ip_ecn
= IPTOS_ECN_NOTECT
;
1828 unsigned int ifscope
;
1829 uint8_t isconnected
, isdisconnected
;
1830 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1831 int pktf_sw_lro_pkt
= (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) ? 1 : 0;
1832 int nlropkts
= (pktf_sw_lro_pkt
== 1) ? m
->m_pkthdr
.lro_npkts
: 1;
1833 int turnoff_lro
= 0, win
;
1835 struct mptcb
*mp_tp
= NULL
;
1837 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
1838 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
1839 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
1840 boolean_t recvd_dsack
= FALSE
;
1841 struct tcp_respond_args tra
;
1843 #define TCP_INC_VAR(stat, npkts) do { \
1847 TCP_INC_VAR(tcpstat
.tcps_rcvtotal
, nlropkts
);
1849 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
1850 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1851 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1852 KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
1856 if (fwd_tag
!= NULL
) {
1857 struct ip_fwd_tag
*ipfwd_tag
=
1858 (struct ip_fwd_tag
*)(fwd_tag
+1);
1860 next_hop
= ipfwd_tag
->next_hop
;
1861 m_tag_delete(m
, fwd_tag
);
1863 #endif /* IPFIREWALL */
1866 struct ip6_hdr
*ip6
= NULL
;
1869 int rstreason
; /* For badport_bandlim accounting purposes */
1870 struct proc
*proc0
=current_proc();
1872 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
1875 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
1877 bzero((char *)&to
, sizeof(to
));
1882 * Expect 32-bit aligned data pointer on
1883 * strict-align platforms
1885 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1887 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
1888 ip6
= mtod(m
, struct ip6_hdr
*);
1889 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
1890 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
1892 if (tcp_input_checksum(AF_INET6
, m
, th
, off0
, tlen
))
1895 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1896 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
1897 th
->th_seq
, th
->th_ack
, th
->th_win
);
1899 * Be proactive about unspecified IPv6 address in source.
1900 * As we use all-zero to indicate unbounded/unconnected pcb,
1901 * unspecified IPv6 address can be used to confuse us.
1903 * Note that packets with unspecified IPv6 destination is
1904 * already dropped in ip6_input.
1906 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
1908 IF_TCP_STATINC(ifp
, unspecv6
);
1911 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1912 struct ip6_hdr
*, ip6
, struct tcpcb
*, NULL
,
1913 struct tcphdr
*, th
);
1915 ip_ecn
= (ntohl(ip6
->ip6_flow
) >> 20) & IPTOS_ECN_MASK
;
1920 * Get IP and TCP header together in first mbuf.
1921 * Note: IP leaves IP header in first mbuf.
1923 if (off0
> sizeof (struct ip
)) {
1924 ip_stripoptions(m
, (struct mbuf
*)0);
1925 off0
= sizeof(struct ip
);
1927 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
1928 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
1929 tcpstat
.tcps_rcvshort
++;
1934 /* Expect 32-bit aligned data pointer on strict-align platforms */
1935 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1937 ip
= mtod(m
, struct ip
*);
1938 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
1941 if (tcp_input_checksum(AF_INET
, m
, th
, off0
, tlen
))
1945 /* Re-initialization for later version check */
1946 ip
->ip_v
= IPVERSION
;
1948 ip_ecn
= (ip
->ip_tos
& IPTOS_ECN_MASK
);
1950 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1951 struct ip
*, ip
, struct tcpcb
*, NULL
, struct tcphdr
*, th
);
1953 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1954 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
1955 th
->th_seq
, th
->th_ack
, th
->th_win
);
1960 * Check that TCP offset makes sense,
1961 * pull out TCP options and adjust length.
1963 off
= th
->th_off
<< 2;
1964 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
1965 tcpstat
.tcps_rcvbadoff
++;
1966 IF_TCP_STATINC(ifp
, badformat
);
1969 tlen
-= off
; /* tlen is used instead of ti->ti_len */
1970 if (off
> sizeof (struct tcphdr
)) {
1973 IP6_EXTHDR_CHECK(m
, off0
, off
, return);
1974 ip6
= mtod(m
, struct ip6_hdr
*);
1975 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
1979 if (m
->m_len
< sizeof(struct ip
) + off
) {
1980 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
1981 tcpstat
.tcps_rcvshort
++;
1984 ip
= mtod(m
, struct ip
*);
1985 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
1988 optlen
= off
- sizeof (struct tcphdr
);
1989 optp
= (u_char
*)(th
+ 1);
1991 * Do quick retrieval of timestamp options ("options
1992 * prediction?"). If timestamp is the only option and it's
1993 * formatted as recommended in RFC 1323 appendix A, we
1994 * quickly get the values now and not bother calling
1995 * tcp_dooptions(), etc.
1997 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
1998 (optlen
> TCPOLEN_TSTAMP_APPA
&&
1999 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
2000 *(u_int32_t
*)(void *)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
2001 (th
->th_flags
& TH_SYN
) == 0) {
2002 to
.to_flags
|= TOF_TS
;
2003 to
.to_tsval
= ntohl(*(u_int32_t
*)(void *)(optp
+ 4));
2004 to
.to_tsecr
= ntohl(*(u_int32_t
*)(void *)(optp
+ 8));
2005 optp
= NULL
; /* we've parsed the options */
2008 thflags
= th
->th_flags
;
2012 * If the drop_synfin option is enabled, drop all packets with
2013 * both the SYN and FIN bits set. This prevents e.g. nmap from
2014 * identifying the TCP/IP stack.
2016 * This is a violation of the TCP specification.
2018 if (drop_synfin
&& (thflags
& (TH_SYN
|TH_FIN
)) == (TH_SYN
|TH_FIN
)) {
2019 IF_TCP_STATINC(ifp
, synfin
);
2025 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
2026 * until after ip6_savecontrol() is called and before other functions
2027 * which don't want those proto headers.
2028 * Because ip6_savecontrol() is going to parse the mbuf to
2029 * search for data to be passed up to user-land, it wants mbuf
2030 * parameters to be unchanged.
2032 drop_hdrlen
= off0
+ off
;
2034 /* Since this is an entry point for input processing of tcp packets, we
2035 * can update the tcp clock here.
2037 calculate_tcp_clock();
2040 * Record the interface where this segment arrived on; this does not
2041 * affect normal data output (for non-detached TCP) as it provides a
2042 * hint about which route and interface to use for sending in the
2043 * absence of a PCB, when scoped routing (and thus source interface
2044 * selection) are enabled.
2046 if ((m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) || m
->m_pkthdr
.rcvif
== NULL
)
2047 ifscope
= IFSCOPE_NONE
;
2049 ifscope
= m
->m_pkthdr
.rcvif
->if_index
;
2052 * Convert TCP protocol specific fields to host format.
2055 #if BYTE_ORDER != BIG_ENDIAN
2063 * Locate pcb for segment.
2067 isconnected
= FALSE
;
2068 isdisconnected
= FALSE
;
2070 #if IPFIREWALL_FORWARD
2071 if (next_hop
!= NULL
2073 && isipv6
== 0 /* IPv6 support is not yet */
2077 * Diverted. Pretend to be the destination.
2078 * already got one like this?
2080 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2081 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
2084 * No, then it's new. Try find the ambushing socket
2086 if (!next_hop
->sin_port
) {
2087 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
2088 th
->th_sport
, next_hop
->sin_addr
,
2089 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2091 inp
= in_pcblookup_hash(&tcbinfo
,
2092 ip
->ip_src
, th
->th_sport
,
2094 ntohs(next_hop
->sin_port
), 1,
2099 #endif /* IPFIREWALL_FORWARD */
2103 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
2104 &ip6
->ip6_dst
, th
->th_dport
, 1,
2108 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
2109 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
2113 * Use the interface scope information from the PCB for outbound
2114 * segments. If the PCB isn't present and if scoped routing is
2115 * enabled, tcp_respond will use the scope of the interface where
2116 * the segment arrived on.
2118 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
))
2119 ifscope
= inp
->inp_boundifp
->if_index
;
2122 * If the state is CLOSED (i.e., TCB does not exist) then
2123 * all data in the incoming segment is discarded.
2124 * If the TCB exists but is in CLOSED state, it is embryonic,
2125 * but should either do a listen or a connect soon.
2130 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
2132 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
2137 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
2138 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
2142 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
2143 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
2145 switch (log_in_vain
) {
2147 if(thflags
& TH_SYN
)
2149 "Connection attempt to TCP %s:%d from %s:%d\n",
2150 dbuf
, ntohs(th
->th_dport
),
2152 ntohs(th
->th_sport
));
2156 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
2157 dbuf
, ntohs(th
->th_dport
), sbuf
,
2158 ntohs(th
->th_sport
), thflags
);
2162 if ((thflags
& TH_SYN
) && !(thflags
& TH_ACK
) &&
2163 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
2165 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
2166 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
2168 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
2171 log_in_vain_log((LOG_INFO
,
2172 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
2173 dbuf
, ntohs(th
->th_dport
),
2175 ntohs(th
->th_sport
)));
2182 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
2184 switch (blackhole
) {
2186 if (thflags
& TH_SYN
)
2195 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2196 IF_TCP_STATINC(ifp
, noconnnolist
);
2197 goto dropwithresetnosock
;
2199 so
= inp
->inp_socket
;
2201 /* This case shouldn't happen as the socket shouldn't be null
2202 * if inp_state isn't set to INPCB_STATE_DEAD
2203 * But just in case, we pretend we didn't find the socket if we hit this case
2204 * as this isn't cause for a panic (the socket might be leaked however)...
2208 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp
);
2214 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2215 tcp_unlock(so
, 1, (void *)2);
2216 inp
= NULL
; // pretend we didn't find it
2223 if (!necp_socket_is_allowed_to_send_recv_v6(inp
,
2224 th
->th_dport
, th
->th_sport
, &ip6
->ip6_dst
,
2225 &ip6
->ip6_src
, ifp
, NULL
, NULL
)) {
2226 IF_TCP_STATINC(ifp
, badformatipsec
);
2232 if (!necp_socket_is_allowed_to_send_recv_v4(inp
,
2233 th
->th_dport
, th
->th_sport
, &ip
->ip_dst
, &ip
->ip_src
,
2235 IF_TCP_STATINC(ifp
, badformatipsec
);
2241 tp
= intotcpcb(inp
);
2243 rstreason
= BANDLIM_RST_CLOSEDPORT
;
2244 IF_TCP_STATINC(ifp
, noconnlist
);
2247 if (tp
->t_state
== TCPS_CLOSED
)
2250 /* Unscale the window into a 32-bit value. */
2251 if ((thflags
& TH_SYN
) == 0)
2252 tiwin
= th
->th_win
<< tp
->snd_scale
;
2257 if (mac_inpcb_check_deliver(inp
, m
, AF_INET
, SOCK_STREAM
))
2261 /* Avoid processing packets while closing a listen socket */
2262 if (tp
->t_state
== TCPS_LISTEN
&&
2263 (so
->so_options
& SO_ACCEPTCONN
) == 0)
2266 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
2268 if (so
->so_options
& SO_DEBUG
) {
2269 ostate
= tp
->t_state
;
2272 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
2276 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
2280 if (so
->so_options
& SO_ACCEPTCONN
) {
2281 struct tcpcb
*tp0
= tp
;
2284 struct sockaddr_storage from
;
2286 struct inpcb
*oinp
= sotoinpcb(so
);
2288 struct ifnet
*head_ifscope
;
2289 unsigned int head_nocell
, head_recvanyif
,
2290 head_noexpensive
, head_awdl_unrestricted
,
2291 head_intcoproc_allowed
;
2293 /* Get listener's bound-to-interface, if any */
2294 head_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2295 inp
->inp_boundifp
: NULL
;
2296 /* Get listener's no-cellular information, if any */
2297 head_nocell
= INP_NO_CELLULAR(inp
);
2298 /* Get listener's recv-any-interface, if any */
2299 head_recvanyif
= (inp
->inp_flags
& INP_RECV_ANYIF
);
2300 /* Get listener's no-expensive information, if any */
2301 head_noexpensive
= INP_NO_EXPENSIVE(inp
);
2302 head_awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
2303 head_intcoproc_allowed
= INP_INTCOPROC_ALLOWED(inp
);
2306 * If the state is LISTEN then ignore segment if it contains an RST.
2307 * If the segment contains an ACK then it is bad and send a RST.
2308 * If it does not contain a SYN then it is not interesting; drop it.
2309 * If it is from this socket, drop it, it must be forged.
2311 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
2312 IF_TCP_STATINC(ifp
, listbadsyn
);
2314 if (thflags
& TH_RST
) {
2317 if (thflags
& TH_ACK
) {
2319 tcpstat
.tcps_badsyn
++;
2320 rstreason
= BANDLIM_RST_OPENPORT
;
2324 /* We come here if there is no SYN set */
2325 tcpstat
.tcps_badsyn
++;
2328 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
2329 if (th
->th_dport
== th
->th_sport
) {
2332 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
2337 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
2341 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2342 * in_broadcast() should never return true on a received
2343 * packet with M_BCAST not set.
2345 * Packets with a multicast source address should also
2348 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
2352 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2353 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
2357 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2358 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2359 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2360 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
2366 * If deprecated address is forbidden,
2367 * we do not accept SYN to deprecated interface
2368 * address to prevent any new inbound connection from
2369 * getting established.
2370 * When we do not accept SYN, we send a TCP RST,
2371 * with deprecated source address (instead of dropping
2372 * it). We compromise it as it is much better for peer
2373 * to send a RST, and RST will be the final packet
2376 * If we do not forbid deprecated addresses, we accept
2377 * the SYN packet. RFC 4862 forbids dropping SYN in
2380 if (isipv6
&& !ip6_use_deprecated
) {
2383 if (ip6_getdstifaddr_info(m
, NULL
,
2385 if (ia6_flags
& IN6_IFF_DEPRECATED
) {
2387 rstreason
= BANDLIM_RST_OPENPORT
;
2388 IF_TCP_STATINC(ifp
, deprecate6
);
2397 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
2399 sin6
->sin6_len
= sizeof(*sin6
);
2400 sin6
->sin6_family
= AF_INET6
;
2401 sin6
->sin6_port
= th
->th_sport
;
2402 sin6
->sin6_flowinfo
= 0;
2403 sin6
->sin6_addr
= ip6
->ip6_src
;
2404 sin6
->sin6_scope_id
= 0;
2409 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
2411 sin
->sin_len
= sizeof(*sin
);
2412 sin
->sin_family
= AF_INET
;
2413 sin
->sin_port
= th
->th_sport
;
2414 sin
->sin_addr
= ip
->ip_src
;
2416 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2418 so2
= sonewconn(so
, 0, NULL
);
2421 tcpstat
.tcps_listendrop
++;
2422 if (tcp_dropdropablreq(so
)) {
2424 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2426 so2
= sonewconn(so
, 0, NULL
);
2432 /* Point "inp" and "tp" in tandem to new socket */
2433 inp
= (struct inpcb
*)so2
->so_pcb
;
2434 tp
= intotcpcb(inp
);
2437 tcp_unlock(so
, 0, 0); /* Unlock but keep a reference on listener for now */
2442 * Mark socket as temporary until we're
2443 * committed to keeping it. The code at
2444 * ``drop'' and ``dropwithreset'' check the
2445 * flag dropsocket to see if the temporary
2446 * socket created here should be discarded.
2447 * We mark the socket as discardable until
2448 * we're committed to it below in TCPS_LISTEN.
2449 * There are some error conditions in which we
2450 * have to drop the temporary socket.
2454 * Inherit INP_BOUND_IF from listener; testing if
2455 * head_ifscope is non-NULL is sufficient, since it
2456 * can only be set to a non-zero value earlier if
2457 * the listener has such a flag set.
2459 if (head_ifscope
!= NULL
) {
2460 inp
->inp_flags
|= INP_BOUND_IF
;
2461 inp
->inp_boundifp
= head_ifscope
;
2463 inp
->inp_flags
&= ~INP_BOUND_IF
;
2466 * Inherit restrictions from listener.
2469 inp_set_nocellular(inp
);
2470 if (head_noexpensive
)
2471 inp_set_noexpensive(inp
);
2472 if (head_awdl_unrestricted
)
2473 inp_set_awdl_unrestricted(inp
);
2474 if (head_intcoproc_allowed
)
2475 inp_set_intcoproc_allowed(inp
);
2477 * Inherit {IN,IN6}_RECV_ANYIF from listener.
2480 inp
->inp_flags
|= INP_RECV_ANYIF
;
2482 inp
->inp_flags
&= ~INP_RECV_ANYIF
;
2485 inp
->in6p_laddr
= ip6
->ip6_dst
;
2487 inp
->inp_vflag
&= ~INP_IPV6
;
2488 inp
->inp_vflag
|= INP_IPV4
;
2490 inp
->inp_laddr
= ip
->ip_dst
;
2494 inp
->inp_lport
= th
->th_dport
;
2495 if (in_pcbinshash(inp
, 0) != 0) {
2497 * Undo the assignments above if we failed to
2498 * put the PCB on the hash lists.
2502 inp
->in6p_laddr
= in6addr_any
;
2505 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
2507 tcp_lock(oso
, 0, 0); /* release ref on parent */
2508 tcp_unlock(oso
, 1, 0);
2514 * Inherit socket options from the listening
2516 * Note that in6p_inputopts are not (even
2517 * should not be) copied, since it stores
2518 * previously received options and is used to
2519 * detect if each new option is different than
2520 * the previous one and hence should be passed
2522 * If we copied in6p_inputopts, a user would
2523 * not be able to receive options just after
2524 * calling the accept system call.
2527 oinp
->inp_flags
& INP_CONTROLOPTS
;
2528 if (oinp
->in6p_outputopts
)
2529 inp
->in6p_outputopts
=
2530 ip6_copypktopts(oinp
->in6p_outputopts
,
2535 inp
->inp_options
= ip_srcroute();
2536 inp
->inp_ip_tos
= oinp
->inp_ip_tos
;
2538 tcp_lock(oso
, 0, 0);
2540 /* copy old policy into new socket's */
2541 if (sotoinpcb(oso
)->inp_sp
)
2544 /* Is it a security hole here to silently fail to copy the policy? */
2545 if (inp
->inp_sp
!= NULL
)
2546 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
2547 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
))
2548 printf("tcp_input: could not copy policy\n");
2551 /* inherit states from the listener */
2552 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2553 struct tcpcb
*, tp
, int32_t, TCPS_LISTEN
);
2554 tp
->t_state
= TCPS_LISTEN
;
2555 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
|TF_NODELAY
);
2556 tp
->t_flagsext
|= (tp0
->t_flagsext
& (TF_RXTFINDROP
|TF_NOTIMEWAIT
|TF_FASTOPEN
));
2557 tp
->t_keepinit
= tp0
->t_keepinit
;
2558 tp
->t_keepcnt
= tp0
->t_keepcnt
;
2559 tp
->t_keepintvl
= tp0
->t_keepintvl
;
2560 tp
->t_adaptive_wtimo
= tp0
->t_adaptive_wtimo
;
2561 tp
->t_adaptive_rtimo
= tp0
->t_adaptive_rtimo
;
2562 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
2563 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0)
2564 tp
->t_notsent_lowat
= tp0
->t_notsent_lowat
;
2565 tp
->t_inpcb
->inp_flags2
|=
2566 tp0
->t_inpcb
->inp_flags2
& INP2_KEEPALIVE_OFFLOAD
;
2568 /* now drop the reference on the listener */
2569 tcp_unlock(oso
, 1, 0);
2571 tcp_set_max_rwinscale(tp
, so
, TCP_AUTORCVBUF_MAX(ifp
));
2573 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
2576 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2577 LCK_MTX_ASSERT_OWNED
);
2579 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
2581 * Evaluate the rate of arrival of packets to see if the
2582 * receiver can reduce the ack traffic. The algorithm to
2583 * stretch acks will be enabled if the connection meets
2584 * certain criteria defined in tcp_stretch_ack_enable function.
2586 if ((tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) != 0) {
2587 TCP_INC_VAR(tp
->rcv_waitforss
, nlropkts
);
2589 if (tcp_stretch_ack_enable(tp
, thflags
)) {
2590 tp
->t_flags
|= TF_STRETCHACK
;
2591 tp
->t_flagsext
&= ~(TF_RCVUNACK_WAITSS
);
2592 tp
->rcv_waitforss
= 0;
2594 tp
->t_flags
&= ~(TF_STRETCHACK
);
2596 if (TSTMP_GT(tp
->rcv_unackwin
, tcp_now
)) {
2597 tp
->rcv_by_unackwin
+= (tlen
+ off
);
2599 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
2600 tp
->rcv_by_unackwin
= tlen
+ off
;
2605 * Keep track of how many bytes were received in the LRO packet
2607 if ((pktf_sw_lro_pkt
) && (nlropkts
> 2)) {
2608 tp
->t_lropktlen
+= tlen
;
2611 * Explicit Congestion Notification - Flag that we need to send ECT if
2612 * + The IP Congestion experienced flag was set.
2613 * + Socket is in established state
2614 * + We negotiated ECN in the TCP setup
2615 * + This isn't a pure ack (tlen > 0)
2616 * + The data is in the valid window
2618 * TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
2620 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2621 TCP_ECN_ENABLED(tp
) && tlen
> 0 &&
2622 SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2623 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
2624 tp
->t_ecn_recv_ce
++;
2625 tcpstat
.tcps_ecn_recv_ce
++;
2626 INP_INC_IFNET_STAT(inp
, ecn_recv_ce
);
2627 /* Mark this connection as it received CE from network */
2628 tp
->ecn_flags
|= TE_RECV_ECN_CE
;
2629 tp
->ecn_flags
|= TE_SENDECE
;
2633 * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
2634 * bother doing extensive checks for state and whatnot.
2636 if (thflags
& TH_CWR
) {
2637 tp
->ecn_flags
&= ~TE_SENDECE
;
2638 tp
->t_ecn_recv_cwr
++;
2642 * If we received an explicit notification of congestion in
2643 * ip tos ecn bits or by the CWR bit in TCP header flags, reset
2644 * the ack-strteching state. We need to handle ECN notification if
2645 * an ECN setup SYN was sent even once.
2647 if (tp
->t_state
== TCPS_ESTABLISHED
2648 && (tp
->ecn_flags
& TE_SETUPSENT
)
2649 && (ip_ecn
== IPTOS_ECN_CE
|| (thflags
& TH_CWR
))) {
2650 tcp_reset_stretch_ack(tp
);
2651 CLEAR_IAJ_STATE(tp
);
2654 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2655 !TCP_ECN_ENABLED(tp
) && !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2656 tcpstat
.tcps_ecn_fallback_ce
++;
2657 tcp_heuristic_ecn_aggressive(tp
);
2658 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2661 if (tp
->t_state
== TCPS_ESTABLISHED
&& TCP_ECN_ENABLED(tp
) &&
2662 ip_ecn
== IPTOS_ECN_CE
&& !(tp
->ecn_flags
& TE_CEHEURI_SET
)) {
2663 if (inp
->inp_stat
->rxpackets
< ECN_MIN_CE_PROBES
) {
2664 tp
->t_ecn_recv_ce_pkt
++;
2665 } else if (tp
->t_ecn_recv_ce_pkt
> ECN_MAX_CE_RATIO
) {
2666 tcpstat
.tcps_ecn_fallback_ce
++;
2667 tcp_heuristic_ecn_aggressive(tp
);
2668 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2669 INP_INC_IFNET_STAT(inp
,ecn_fallback_ce
);
2671 /* We tracked the first ECN_MIN_CE_PROBES segments, we
2672 * now know that the path is good.
2674 tp
->ecn_flags
|= TE_CEHEURI_SET
;
2679 * Try to determine if we are receiving a packet after a long time.
2680 * Use our own approximation of idletime to roughly measure remote
2681 * end's idle time. Since slowstart is used after an idle period
2682 * we want to avoid doing LRO if the remote end is not up to date
2683 * on initial window support and starts with 1 or 2 packets as its IW.
2685 if (sw_lro
&& (tp
->t_flagsext
& TF_LRO_OFFLOADED
) &&
2686 ((tcp_now
- tp
->t_rcvtime
) >= (TCP_IDLETIMEOUT(tp
)))) {
2690 /* Update rcvtime as a new segment was received on the connection */
2691 tp
->t_rcvtime
= tcp_now
;
2694 * Segment received on connection.
2695 * Reset idle time and keep-alive timer.
2697 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
2698 tcp_keepalive_reset(tp
);
2701 * Process options if not in LISTEN state,
2702 * else do it below (after getting remote address).
2704 if (tp
->t_state
!= TCPS_LISTEN
&& optp
) {
2705 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
2707 if (mptcp_input_preproc(tp
, m
, drop_hdrlen
) != 0) {
2708 tp
->t_flags
|= TF_ACKNOW
;
2709 (void) tcp_output(tp
);
2710 tcp_check_timer_state(tp
);
2711 tcp_unlock(so
, 1, 0);
2712 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
|
2713 DBG_FUNC_END
,0,0,0,0,0);
2718 if (tp
->t_state
== TCPS_SYN_SENT
&& (thflags
& TH_SYN
)) {
2719 if (!(thflags
& TH_ACK
) ||
2720 (SEQ_GT(th
->th_ack
, tp
->iss
) &&
2721 SEQ_LEQ(th
->th_ack
, tp
->snd_max
)))
2722 tcp_finalize_options(tp
, &to
, ifscope
);
2727 * Compute inter-packet arrival jitter. According to RFC 3550,
2728 * inter-packet arrival jitter is defined as the difference in
2729 * packet spacing at the receiver compared to the sender for a
2730 * pair of packets. When two packets of maximum segment size come
2731 * one after the other with consecutive sequence numbers, we
2732 * consider them as packets sent together at the sender and use
2733 * them as a pair to compute inter-packet arrival jitter. This
2734 * metric indicates the delay induced by the network components due
2735 * to queuing in edge/access routers.
2737 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2738 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
|TH_PUSH
)) == TH_ACK
&&
2739 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2740 ((to
.to_flags
& TOF_TS
) == 0 ||
2741 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2742 th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
2743 int seg_size
= tlen
;
2744 if (tp
->iaj_pktcnt
<= IAJ_IGNORE_PKTCNT
) {
2745 TCP_INC_VAR(tp
->iaj_pktcnt
, nlropkts
);
2748 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
2749 seg_size
= m
->m_pkthdr
.lro_pktlen
;
2751 if ( tp
->iaj_size
== 0 || seg_size
> tp
->iaj_size
||
2752 (seg_size
== tp
->iaj_size
&& tp
->iaj_rcv_ts
== 0)) {
2754 * State related to inter-arrival jitter is
2755 * uninitialized or we are trying to find a good
2756 * first packet to start computing the metric
2758 update_iaj_state(tp
, seg_size
, 0);
2760 if (seg_size
== tp
->iaj_size
) {
2762 * Compute inter-arrival jitter taking
2763 * this packet as the second packet
2765 if (pktf_sw_lro_pkt
)
2766 compute_iaj(tp
, nlropkts
,
2767 m
->m_pkthdr
.lro_elapsed
);
2769 compute_iaj(tp
, 1, 0);
2771 if (seg_size
< tp
->iaj_size
) {
2773 * There is a smaller packet in the stream.
2774 * Some times the maximum size supported
2775 * on a path can change if there is a new
2776 * link with smaller MTU. The receiver will
2777 * not know about this change. If there
2778 * are too many packets smaller than
2779 * iaj_size, we try to learn the iaj_size
2782 TCP_INC_VAR(tp
->iaj_small_pkt
, nlropkts
);
2783 if (tp
->iaj_small_pkt
> RESET_IAJ_SIZE_THRESH
) {
2784 update_iaj_state(tp
, seg_size
, 1);
2786 CLEAR_IAJ_STATE(tp
);
2789 update_iaj_state(tp
, seg_size
, 0);
2793 CLEAR_IAJ_STATE(tp
);
2795 #endif /* TRAFFIC_MGT */
2798 * Header prediction: check for the two common cases
2799 * of a uni-directional data xfer. If the packet has
2800 * no control flags, is in-sequence, the window didn't
2801 * change and we're not retransmitting, it's a
2802 * candidate. If the length is zero and the ack moved
2803 * forward, we're the sender side of the xfer. Just
2804 * free the data acked & wake any higher level process
2805 * that was blocked waiting for space. If the length
2806 * is non-zero and the ack didn't move, we're the
2807 * receiver side. If we're getting packets in-order
2808 * (the reassembly queue is empty), add the data to
2809 * the socket buffer and note that we need a delayed ack.
2810 * Make sure that the hidden state-flags are also off.
2811 * Since we check for TCPS_ESTABLISHED above, it can only
2814 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2815 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
|TH_CWR
)) == TH_ACK
&&
2816 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2817 ((to
.to_flags
& TOF_TS
) == 0 ||
2818 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2819 th
->th_seq
== tp
->rcv_nxt
&&
2820 tiwin
&& tiwin
== tp
->snd_wnd
&&
2821 tp
->snd_nxt
== tp
->snd_max
) {
2824 * If last ACK falls within this segment's sequence numbers,
2825 * record the timestamp.
2826 * NOTE that the test is modified according to the latest
2827 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2829 if ((to
.to_flags
& TOF_TS
) != 0 &&
2830 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
2831 tp
->ts_recent_age
= tcp_now
;
2832 tp
->ts_recent
= to
.to_tsval
;
2836 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
2837 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
2838 tp
->snd_cwnd
>= tp
->snd_ssthresh
&&
2839 (!IN_FASTRECOVERY(tp
) &&
2840 ((!(SACK_ENABLED(tp
)) &&
2841 tp
->t_dupacks
< tp
->t_rexmtthresh
) ||
2842 (SACK_ENABLED(tp
) && to
.to_nsacks
== 0 &&
2843 TAILQ_EMPTY(&tp
->snd_holes
))))) {
2845 * this is a pure ack for outstanding data.
2847 ++tcpstat
.tcps_predack
;
2849 tcp_bad_rexmt_check(tp
, th
, &to
);
2851 /* Recalculate the RTT */
2852 tcp_compute_rtt(tp
, &to
, th
);
2854 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
2855 acked
= BYTES_ACKED(th
, tp
);
2856 tcpstat
.tcps_rcvackpack
++;
2857 tcpstat
.tcps_rcvackbyte
+= acked
;
2860 * Handle an ack that is in sequence during
2861 * congestion avoidance phase. The
2862 * calculations in this function
2863 * assume that snd_una is not updated yet.
2865 if (CC_ALGO(tp
)->congestion_avd
!= NULL
)
2866 CC_ALGO(tp
)->congestion_avd(tp
, th
);
2867 tcp_ccdbg_trace(tp
, th
, TCP_CC_INSEQ_ACK_RCVD
);
2868 sbdrop(&so
->so_snd
, acked
);
2869 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2870 VERIFY(acked
<= so
->so_msg_state
->msg_serial_bytes
);
2871 so
->so_msg_state
->msg_serial_bytes
-= acked
;
2873 tcp_sbsnd_trim(&so
->so_snd
);
2875 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
2876 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
2877 tp
->snd_recover
= th
->th_ack
- 1;
2878 tp
->snd_una
= th
->th_ack
;
2880 TCP_RESET_REXMT_STATE(tp
);
2883 * pull snd_wl2 up to prevent seq wrap relative
2886 tp
->snd_wl2
= th
->th_ack
;
2888 if (tp
->t_dupacks
> 0) {
2890 tp
->t_rexmtthresh
= tcprexmtthresh
;
2896 * If all outstanding data are acked, stop
2897 * retransmit timer, otherwise restart timer
2898 * using current (possibly backed-off) value.
2899 * If process is waiting for space,
2900 * wakeup/selwakeup/signal. If data
2901 * are ready to send, let tcp_output
2902 * decide between more output or persist.
2904 if (tp
->snd_una
== tp
->snd_max
) {
2905 tp
->t_timer
[TCPT_REXMT
] = 0;
2906 tp
->t_timer
[TCPT_PTO
] = 0;
2907 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
2908 tp
->t_timer
[TCPT_REXMT
] =
2909 OFFSET_FROM_START(tp
,
2912 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
2913 !TCP_DSACK_SEQ_IN_WINDOW(tp
,
2914 tp
->t_dsack_lastuna
, tp
->snd_una
))
2915 tcp_rxtseg_clean(tp
);
2917 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
2918 tp
->t_bwmeas
!= NULL
)
2919 tcp_bwmeas_check(tp
);
2921 sowwakeup(so
); /* has to be done with socket lock held */
2922 if (!SLIST_EMPTY(&tp
->t_notify_ack
))
2923 tcp_notify_acknowledgement(tp
, so
);
2925 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
)) {
2926 (void) tcp_output(tp
);
2929 tcp_tfo_rcv_ack(tp
, th
);
2931 tcp_check_timer_state(tp
);
2932 tcp_unlock(so
, 1, 0);
2933 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2936 } else if (th
->th_ack
== tp
->snd_una
&&
2937 LIST_EMPTY(&tp
->t_segq
) &&
2938 tlen
<= tcp_sbspace(tp
)) {
2940 * this is a pure, in-sequence data packet
2941 * with nothing on the reassembly queue and
2942 * we have enough buffer space to take it.
2946 * If this is a connection in steady state, start
2947 * coalescing packets belonging to this flow.
2950 tcp_lro_remove_state(tp
->t_inpcb
->inp_laddr
,
2951 tp
->t_inpcb
->inp_faddr
,
2952 tp
->t_inpcb
->inp_lport
,
2953 tp
->t_inpcb
->inp_fport
);
2954 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
2955 tp
->t_idleat
= tp
->rcv_nxt
;
2956 } else if (sw_lro
&& !pktf_sw_lro_pkt
&& !isipv6
&&
2957 (so
->so_flags
& SOF_USELRO
) &&
2958 !IFNET_IS_CELLULAR(m
->m_pkthdr
.rcvif
) &&
2959 (m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) &&
2960 ((th
->th_seq
- tp
->irs
) >
2961 (tp
->t_maxseg
<< lro_start
)) &&
2962 ((tp
->t_idleat
== 0) || ((th
->th_seq
-
2963 tp
->t_idleat
) > (tp
->t_maxseg
<< lro_start
)))) {
2964 tp
->t_flagsext
|= TF_LRO_OFFLOADED
;
2965 tcp_start_coalescing(ip
, th
, tlen
);
2969 /* Clean receiver SACK report if present */
2970 if (SACK_ENABLED(tp
) && tp
->rcv_numsacks
)
2971 tcp_clean_sackreport(tp
);
2972 ++tcpstat
.tcps_preddat
;
2973 tp
->rcv_nxt
+= tlen
;
2975 * Pull snd_wl1 up to prevent seq wrap relative to
2978 tp
->snd_wl1
= th
->th_seq
;
2980 * Pull rcv_up up to prevent seq wrap relative to
2983 tp
->rcv_up
= tp
->rcv_nxt
;
2984 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
2985 tcpstat
.tcps_rcvbyte
+= tlen
;
2986 if (nstat_collect
) {
2987 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
2988 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2989 rxpackets
, m
->m_pkthdr
.lro_npkts
);
2991 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2994 INP_ADD_STAT(inp
, cell
, wifi
, wired
,rxbytes
,
2999 * Calculate the RTT on the receiver only if the
3000 * connection is in streaming mode and the last
3001 * packet was not an end-of-write
3003 if (tp
->t_flags
& TF_STREAMING_ON
)
3004 tcp_compute_rtt(tp
, &to
, th
);
3006 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
3007 TCP_AUTORCVBUF_MAX(ifp
));
3010 * Add data to socket buffer.
3012 so_recv_data_stat(so
, m
, 0);
3013 m_adj(m
, drop_hdrlen
); /* delayed header drop */
3016 * If message delivery (SOF_ENABLE_MSGS) is enabled on
3017 * this socket, deliver the packet received as an
3018 * in-order message with sequence number attached to it.
3020 if (sbappendstream_rcvdemux(so
, m
,
3021 th
->th_seq
- (tp
->irs
+ 1), 0)) {
3026 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3027 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
3028 th
->th_seq
, th
->th_ack
, th
->th_win
);
3033 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
3034 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
3035 th
->th_seq
, th
->th_ack
, th
->th_win
);
3037 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3038 if (DELAY_ACK(tp
, th
)) {
3039 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3040 tp
->t_flags
|= TF_DELACK
;
3041 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3044 tp
->t_flags
|= TF_ACKNOW
;
3048 tcp_adaptive_rwtimo_check(tp
, tlen
);
3051 tcp_tfo_rcv_data(tp
);
3053 tcp_check_timer_state(tp
);
3054 tcp_unlock(so
, 1, 0);
3055 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
3061 * Calculate amount of space in receive window,
3062 * and then do TCP input processing.
3063 * Receive window is amount of space in rcv queue,
3064 * but not less than advertised window.
3066 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
3067 LCK_MTX_ASSERT_OWNED
);
3068 win
= tcp_sbspace(tp
);
3071 else { /* clip rcv window to 4K for modems */
3072 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
3073 win
= min(win
, slowlink_wsize
);
3075 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
3078 * Ensure that the subflow receive window isn't greater
3079 * than the connection level receive window.
3081 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
3082 (mp_tp
= tptomptp(tp
))) {
3084 if (tp
->rcv_wnd
> mp_tp
->mpt_rcvwnd
) {
3085 tp
->rcv_wnd
= imax(mp_tp
->mpt_rcvwnd
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
3086 tcpstat
.tcps_mp_reducedwin
++;
3092 switch (tp
->t_state
) {
3095 * Initialize tp->rcv_nxt, and tp->irs, select an initial
3096 * tp->iss, and send a segment:
3097 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
3098 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
3099 * Fill in remote peer address fields if not previously specified.
3100 * Enter SYN_RECEIVED state, and process any other fields of this
3101 * segment in this state.
3104 struct sockaddr_in
*sin
;
3106 struct sockaddr_in6
*sin6
;
3109 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
3110 LCK_MTX_ASSERT_OWNED
);
3113 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
3114 M_SONAME
, M_NOWAIT
);
3117 bzero(sin6
, sizeof(*sin6
));
3118 sin6
->sin6_family
= AF_INET6
;
3119 sin6
->sin6_len
= sizeof(*sin6
);
3120 sin6
->sin6_addr
= ip6
->ip6_src
;
3121 sin6
->sin6_port
= th
->th_sport
;
3122 laddr6
= inp
->in6p_laddr
;
3123 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
3124 inp
->in6p_laddr
= ip6
->ip6_dst
;
3125 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
3127 inp
->in6p_laddr
= laddr6
;
3128 FREE(sin6
, M_SONAME
);
3131 FREE(sin6
, M_SONAME
);
3136 &((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
3137 LCK_MTX_ASSERT_OWNED
);
3138 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
3142 sin
->sin_family
= AF_INET
;
3143 sin
->sin_len
= sizeof(*sin
);
3144 sin
->sin_addr
= ip
->ip_src
;
3145 sin
->sin_port
= th
->th_sport
;
3146 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
3147 laddr
= inp
->inp_laddr
;
3148 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
3149 inp
->inp_laddr
= ip
->ip_dst
;
3150 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
,
3151 IFSCOPE_NONE
, NULL
)) {
3152 inp
->inp_laddr
= laddr
;
3153 FREE(sin
, M_SONAME
);
3156 FREE(sin
, M_SONAME
);
3159 tcp_dooptions(tp
, optp
, optlen
, th
, &to
);
3160 tcp_finalize_options(tp
, &to
, ifscope
);
3162 if (tfo_enabled(tp
) && tcp_tfo_syn(tp
, &to
))
3168 tp
->iss
= tcp_new_isn(tp
);
3170 tp
->irs
= th
->th_seq
;
3171 tcp_sendseqinit(tp
);
3173 tp
->snd_recover
= tp
->snd_una
;
3175 * Initialization of the tcpcb for transaction;
3176 * set SND.WND = SEG.WND,
3177 * initialize CCsend and CCrecv.
3179 tp
->snd_wnd
= tiwin
; /* initial send-window */
3180 tp
->t_flags
|= TF_ACKNOW
;
3181 tp
->t_unacksegs
= 0;
3182 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3183 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3184 tp
->t_state
= TCPS_SYN_RECEIVED
;
3185 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3186 TCP_CONN_KEEPINIT(tp
));
3187 dropsocket
= 0; /* committed to socket */
3189 if (inp
->inp_flowhash
== 0)
3190 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
3192 /* update flowinfo - RFC 6437 */
3193 if (inp
->inp_flow
== 0 &&
3194 inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
3195 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
3197 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
3201 /* reset the incomp processing flag */
3202 so
->so_flags
&= ~(SOF_INCOMP_INPROGRESS
);
3203 tcpstat
.tcps_accepts
++;
3204 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
| TH_CWR
)) {
3206 tp
->ecn_flags
|= (TE_SETUPRECEIVED
| TE_SENDIPECT
);
3213 * If the state is SYN_RECEIVED and the seg contains an ACK,
3214 * but not for our SYN/ACK, send a RST.
3216 case TCPS_SYN_RECEIVED
:
3217 if ((thflags
& TH_ACK
) &&
3218 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
3219 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3220 rstreason
= BANDLIM_RST_OPENPORT
;
3221 IF_TCP_STATINC(ifp
, ooopacket
);
3226 * In SYN_RECEIVED state, if we recv some SYNS with
3227 * window scale and others without, window scaling should
3228 * be disabled. Otherwise the window advertised will be
3229 * lower if we assume scaling and the other end does not.
3231 if ((thflags
& TH_SYN
) &&
3232 (tp
->irs
== th
->th_seq
) &&
3233 !(to
.to_flags
& TOF_SCALE
))
3234 tp
->t_flags
&= ~TF_RCVD_SCALE
;
3238 * If the state is SYN_SENT:
3239 * if seg contains an ACK, but not for our SYN, drop the input.
3240 * if seg contains a RST, then drop the connection.
3241 * if seg does not contain SYN, then drop it.
3242 * Otherwise this is an acceptable SYN segment
3243 * initialize tp->rcv_nxt and tp->irs
3244 * if seg contains ack then advance tp->snd_una
3245 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
3246 * arrange for segment to be acked (eventually)
3247 * continue processing rest of data/controls, beginning with URG
3250 if ((thflags
& TH_ACK
) &&
3251 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
3252 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
3253 rstreason
= BANDLIM_UNLIMITED
;
3254 IF_TCP_STATINC(ifp
, ooopacket
);
3257 if (thflags
& TH_RST
) {
3258 if ((thflags
& TH_ACK
) != 0) {
3260 if ((so
->so_flags
& SOF_MPTCP_FASTJOIN
) &&
3261 SEQ_GT(th
->th_ack
, tp
->iss
+1)) {
3262 so
->so_flags
&= ~SOF_MPTCP_FASTJOIN
;
3263 /* ignore the RST and retransmit SYN */
3268 (SO_FILT_HINT_LOCKED
|
3269 SO_FILT_HINT_CONNRESET
));
3270 tp
= tcp_drop(tp
, ECONNREFUSED
);
3271 postevent(so
, 0, EV_RESET
);
3275 if ((thflags
& TH_SYN
) == 0)
3277 tp
->snd_wnd
= th
->th_win
; /* initial send window */
3279 tp
->irs
= th
->th_seq
;
3281 if (thflags
& TH_ACK
) {
3282 tcpstat
.tcps_connects
++;
3284 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
)) {
3285 /* ECN-setup SYN-ACK */
3286 tp
->ecn_flags
|= TE_SETUPRECEIVED
;
3287 if (TCP_ECN_ENABLED(tp
)) {
3288 tcp_heuristic_ecn_success(tp
);
3289 tcpstat
.tcps_ecn_client_success
++;
3292 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3293 tp
->t_rxtshift
== 0) {
3294 tcp_heuristic_ecn_success(tp
);
3295 tcpstat
.tcps_ecn_not_supported
++;
3297 if (tp
->ecn_flags
& TE_SETUPSENT
&&
3299 tcp_heuristic_ecn_loss(tp
);
3301 /* non-ECN-setup SYN-ACK */
3302 tp
->ecn_flags
&= ~TE_SENDIPECT
;
3305 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
3306 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
3307 mac_socketpeer_label_associate_mbuf(m
, so
);
3308 /* XXXMAC: SOCK_UNLOCK(so); */
3310 /* Do window scaling on this connection? */
3311 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3312 tp
->snd_scale
= tp
->requested_s_scale
;
3313 tp
->rcv_scale
= tp
->request_r_scale
;
3316 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
<< tp
->rcv_scale
);
3317 tp
->snd_una
++; /* SYN is acked */
3318 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
3319 tp
->snd_nxt
= tp
->snd_una
;
3322 * We have sent more in the SYN than what is being
3323 * acked. (e.g., TFO)
3324 * We should restart the sending from what the receiver
3325 * has acknowledged immediately.
3327 if (SEQ_GT(tp
->snd_nxt
, th
->th_ack
))
3328 tp
->snd_max
= tp
->snd_nxt
= th
->th_ack
;
3331 * If there's data, delay ACK; if there's also a FIN
3332 * ACKNOW will be turned on later.
3334 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
3335 if (DELAY_ACK(tp
, th
) && tlen
!= 0 ) {
3336 if ((tp
->t_flags
& TF_DELACK
) == 0) {
3337 tp
->t_flags
|= TF_DELACK
;
3338 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
3342 tp
->t_flags
|= TF_ACKNOW
;
3345 * Received <SYN,ACK> in SYN_SENT[*] state.
3347 * SYN_SENT --> ESTABLISHED
3348 * SYN_SENT* --> FIN_WAIT_1
3350 tp
->t_starttime
= tcp_now
;
3351 tcp_sbrcv_tstmp_check(tp
);
3352 if (tp
->t_flags
& TF_NEEDFIN
) {
3353 DTRACE_TCP4(state__change
, void, NULL
,
3354 struct inpcb
*, inp
,
3355 struct tcpcb
*, tp
, int32_t,
3357 tp
->t_state
= TCPS_FIN_WAIT_1
;
3358 tp
->t_flags
&= ~TF_NEEDFIN
;
3361 DTRACE_TCP4(state__change
, void, NULL
,
3362 struct inpcb
*, inp
, struct tcpcb
*,
3363 tp
, int32_t, TCPS_ESTABLISHED
);
3364 tp
->t_state
= TCPS_ESTABLISHED
;
3365 tp
->t_timer
[TCPT_KEEP
] =
3366 OFFSET_FROM_START(tp
,
3367 TCP_CONN_KEEPIDLE(tp
));
3369 nstat_route_connect_success(
3370 inp
->inp_route
.ro_rt
);
3372 * The SYN is acknowledged but una is not
3373 * updated yet. So pass the value of
3374 * ack to compute sndbytes correctly
3376 inp_count_sndbytes(inp
, th
->th_ack
);
3380 * Do not send the connect notification for additional
3381 * subflows until ACK for 3-way handshake arrives.
3383 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3384 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3385 isconnected
= FALSE
;
3386 /* Start data xmit if fastjoin */
3387 if (mptcp_fastjoin
&& (so
->so_flags
& SOF_MPTCP_FASTJOIN
)) {
3388 soevent(so
, (SO_FILT_HINT_LOCKED
|
3389 SO_FILT_HINT_MPFASTJ
));
3395 if (tp
->t_tfo_flags
& (TFO_F_COOKIE_REQ
| TFO_F_COOKIE_SENT
)) {
3396 tcp_tfo_synack(tp
, &to
);
3398 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
3399 SEQ_LT(tp
->snd_una
, th
->th_ack
)) {
3400 tp
->t_tfo_stats
|= TFO_S_SYN_DATA_ACKED
;
3401 tcpstat
.tcps_tfo_syn_data_acked
++;
3403 if (so
->so_flags
& SOF_MP_SUBFLOW
)
3404 so
->so_flags1
|= SOF1_TFO_REWIND
;
3406 if (!(tp
->t_tfo_flags
& TFO_F_NO_RCVPROBING
))
3407 tcp_tfo_rcv_probe(tp
, tlen
);
3412 * Received initial SYN in SYN-SENT[*] state => simul-
3413 * taneous open. If segment contains CC option and there is
3414 * a cached CC, apply TAO test; if it succeeds, connection is
3415 * half-synchronized. Otherwise, do 3-way handshake:
3416 * SYN-SENT -> SYN-RECEIVED
3417 * SYN-SENT* -> SYN-RECEIVED*
3419 tp
->t_flags
|= TF_ACKNOW
;
3420 tp
->t_timer
[TCPT_REXMT
] = 0;
3421 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3422 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3423 tp
->t_state
= TCPS_SYN_RECEIVED
;
3426 * During simultaneous open, TFO should not be used.
3427 * So, we disable it here, to prevent that data gets
3428 * sent on the SYN/ACK.
3430 tcp_disable_tfo(tp
);
3435 * Advance th->th_seq to correspond to first data byte.
3436 * If data, trim to stay within window,
3437 * dropping FIN if necessary.
3440 if (tlen
> tp
->rcv_wnd
) {
3441 todrop
= tlen
- tp
->rcv_wnd
;
3445 tcpstat
.tcps_rcvpackafterwin
++;
3446 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3448 tp
->snd_wl1
= th
->th_seq
- 1;
3449 tp
->rcv_up
= th
->th_seq
;
3451 * Client side of transaction: already sent SYN and data.
3452 * If the remote host used T/TCP to validate the SYN,
3453 * our data will be ACK'd; if so, enter normal data segment
3454 * processing in the middle of step 5, ack processing.
3455 * Otherwise, goto step 6.
3457 if (thflags
& TH_ACK
)
3461 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
3462 * do normal processing.
3464 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
3468 case TCPS_TIME_WAIT
:
3469 break; /* continue normal processing */
3471 /* Received a SYN while connection is already established.
3472 * This is a "half open connection and other anomalies" described
3473 * in RFC793 page 34, send an ACK so the remote reset the connection
3474 * or recovers by adjusting its sequence numberering
3476 case TCPS_ESTABLISHED
:
3477 if (thflags
& TH_SYN
)
3483 * States other than LISTEN or SYN_SENT.
3484 * First check the RST flag and sequence number since reset segments
3485 * are exempt from the timestamp and connection count tests. This
3486 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
3487 * below which allowed reset segments in half the sequence space
3488 * to fall though and be processed (which gives forged reset
3489 * segments with a random sequence number a 50 percent chance of
3490 * killing a connection).
3491 * Then check timestamp, if present.
3492 * Then check the connection count, if present.
3493 * Then check that at least some bytes of segment are within
3494 * receive window. If segment begins before rcv_nxt,
3495 * drop leading data (and SYN); if nothing left, just ack.
3498 * If the RST bit is set, check the sequence number to see
3499 * if this is a valid reset segment.
3501 * In all states except SYN-SENT, all reset (RST) segments
3502 * are validated by checking their SEQ-fields. A reset is
3503 * valid if its sequence number is in the window.
3504 * Note: this does not take into account delayed ACKs, so
3505 * we should test against last_ack_sent instead of rcv_nxt.
3506 * The sequence number in the reset segment is normally an
3507 * echo of our outgoing acknowlegement numbers, but some hosts
3508 * send a reset with the sequence number at the rightmost edge
3509 * of our receive window, and we have to handle this case.
3510 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
3511 * that brute force RST attacks are possible. To combat this,
3512 * we use a much stricter check while in the ESTABLISHED state,
3513 * only accepting RSTs where the sequence number is equal to
3514 * last_ack_sent. In all other states (the states in which a
3515 * RST is more likely), the more permissive check is used.
3516 * If we have multiple segments in flight, the intial reset
3517 * segment sequence numbers will be to the left of last_ack_sent,
3518 * but they will eventually catch up.
3519 * In any case, it never made sense to trim reset segments to
3520 * fit the receive window since RFC 1122 says:
3521 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
3523 * A TCP SHOULD allow a received RST segment to include data.
3526 * It has been suggested that a RST segment could contain
3527 * ASCII text that encoded and explained the cause of the
3528 * RST. No standard has yet been established for such
3531 * If the reset segment passes the sequence number test examine
3533 * SYN_RECEIVED STATE:
3534 * If passive open, return to LISTEN state.
3535 * If active open, inform user that connection was refused.
3536 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
3537 * Inform user that connection was reset, and close tcb.
3538 * CLOSING, LAST_ACK STATES:
3541 * Drop the segment - see Stevens, vol. 2, p. 964 and
3544 * Radar 4803931: Allows for the case where we ACKed the FIN but
3545 * there is already a RST in flight from the peer.
3546 * In that case, accept the RST for non-established
3547 * state if it's one off from last_ack_sent.
3550 if (thflags
& TH_RST
) {
3551 if ((SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3552 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) ||
3553 (tp
->rcv_wnd
== 0 &&
3554 ((tp
->last_ack_sent
== th
->th_seq
) ||
3555 ((tp
->last_ack_sent
-1) == th
->th_seq
)))) {
3556 switch (tp
->t_state
) {
3558 case TCPS_SYN_RECEIVED
:
3559 IF_TCP_STATINC(ifp
, rstinsynrcv
);
3560 so
->so_error
= ECONNREFUSED
;
3563 case TCPS_ESTABLISHED
:
3564 if (tp
->last_ack_sent
!= th
->th_seq
) {
3565 tcpstat
.tcps_badrst
++;
3568 if (TCP_ECN_ENABLED(tp
) &&
3569 tp
->snd_una
== tp
->iss
+ 1 &&
3570 SEQ_GT(tp
->snd_max
, tp
->snd_una
)) {
3572 * If the first data packet on an
3573 * ECN connection, receives a RST
3574 * increment the heuristic
3576 tcp_heuristic_ecn_droprst(tp
);
3578 case TCPS_FIN_WAIT_1
:
3579 case TCPS_CLOSE_WAIT
:
3583 case TCPS_FIN_WAIT_2
:
3584 so
->so_error
= ECONNRESET
;
3586 postevent(so
, 0, EV_RESET
);
3588 (SO_FILT_HINT_LOCKED
|
3589 SO_FILT_HINT_CONNRESET
));
3591 tcpstat
.tcps_drops
++;
3600 case TCPS_TIME_WAIT
:
3608 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3609 * and it's less than ts_recent, drop it.
3611 if ((to
.to_flags
& TOF_TS
) != 0 && tp
->ts_recent
&&
3612 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
3614 /* Check to see if ts_recent is over 24 days old. */
3615 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
3617 * Invalidate ts_recent. If this segment updates
3618 * ts_recent, the age will be reset later and ts_recent
3619 * will get a valid value. If it does not, setting
3620 * ts_recent to zero will at least satisfy the
3621 * requirement that zero be placed in the timestamp
3622 * echo reply when ts_recent isn't valid. The
3623 * age isn't reset until we get a valid ts_recent
3624 * because we don't want out-of-order segments to be
3625 * dropped when ts_recent is old.
3629 tcpstat
.tcps_rcvduppack
++;
3630 tcpstat
.tcps_rcvdupbyte
+= tlen
;
3632 tcpstat
.tcps_pawsdrop
++;
3635 * PAWS-drop when ECN is being used? That indicates
3636 * that ECT-marked packets take a different path, with
3637 * different congestion-characteristics.
3639 * Only fallback when we did send less than 2GB as PAWS
3640 * really has no reason to kick in earlier.
3642 if (TCP_ECN_ENABLED(tp
) &&
3643 inp
->inp_stat
->rxbytes
< 2147483648) {
3644 INP_INC_IFNET_STAT(inp
, ecn_fallback_reorder
);
3645 tcpstat
.tcps_ecn_fallback_reorder
++;
3646 tcp_heuristic_ecn_aggressive(tp
);
3649 if (nstat_collect
) {
3650 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
,
3651 1, tlen
, NSTAT_RX_FLAG_DUPLICATE
);
3652 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3654 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3656 tp
->t_stat
.rxduplicatebytes
+= tlen
;
3665 * In the SYN-RECEIVED state, validate that the packet belongs to
3666 * this connection before trimming the data to fit the receive
3667 * window. Check the sequence number versus IRS since we know
3668 * the sequence numbers haven't wrapped. This is a partial fix
3669 * for the "LAND" DoS attack.
3671 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
3672 rstreason
= BANDLIM_RST_OPENPORT
;
3673 IF_TCP_STATINC(ifp
, dospacket
);
3677 todrop
= tp
->rcv_nxt
- th
->th_seq
;
3679 if (thflags
& TH_SYN
) {
3689 * Following if statement from Stevens, vol. 2, p. 960.
3692 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
3694 * Any valid FIN must be to the left of the window.
3695 * At this point the FIN must be a duplicate or out
3696 * of sequence; drop it.
3701 * Send an ACK to resynchronize and drop any data.
3702 * But keep on processing for RST or ACK.
3704 tp
->t_flags
|= TF_ACKNOW
;
3706 /* This could be a keepalive */
3707 soevent(so
, SO_FILT_HINT_LOCKED
|
3708 SO_FILT_HINT_KEEPALIVE
);
3711 tcpstat
.tcps_rcvduppack
++;
3712 tcpstat
.tcps_rcvdupbyte
+= todrop
;
3714 tcpstat
.tcps_rcvpartduppack
++;
3715 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
3718 if (TCP_DSACK_ENABLED(tp
) && todrop
> 1) {
3720 * Note the duplicate data sequence space so that
3721 * it can be reported in DSACK option.
3723 tp
->t_dsack_lseq
= th
->th_seq
;
3724 tp
->t_dsack_rseq
= th
->th_seq
+ todrop
;
3725 tp
->t_flags
|= TF_ACKNOW
;
3727 if (nstat_collect
) {
3728 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
, 1,
3729 todrop
, NSTAT_RX_FLAG_DUPLICATE
);
3730 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
3731 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, todrop
);
3732 tp
->t_stat
.rxduplicatebytes
+= todrop
;
3734 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
3735 th
->th_seq
+= todrop
;
3737 if (th
->th_urp
> todrop
)
3738 th
->th_urp
-= todrop
;
3746 * If new data are received on a connection after the user
3747 * processes are gone, then RST the other end.
3748 * Send also a RST when we received a data segment after we've
3749 * sent our FIN when the socket is defunct.
3750 * Note that an MPTCP subflow socket would have SS_NOFDREF set
3751 * by default so check to make sure that we test for SOF_MP_SUBFLOW
3752 * socket flag (which would be cleared when the socket is closed.)
3754 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) && tlen
&&
3755 (((so
->so_state
& SS_NOFDREF
) &&
3756 tp
->t_state
> TCPS_CLOSE_WAIT
) ||
3757 ((so
->so_flags
& SOF_DEFUNCT
) &&
3758 tp
->t_state
> TCPS_FIN_WAIT_1
))) {
3760 tcpstat
.tcps_rcvafterclose
++;
3761 rstreason
= BANDLIM_UNLIMITED
;
3762 IF_TCP_STATINC(ifp
, cleanup
);
3767 * If segment ends after window, drop trailing data
3768 * (and PUSH and FIN); if nothing left, just ACK.
3770 todrop
= (th
->th_seq
+tlen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
3772 tcpstat
.tcps_rcvpackafterwin
++;
3773 if (todrop
>= tlen
) {
3774 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
3776 * If a new connection request is received
3777 * while in TIME_WAIT, drop the old connection
3778 * and start over if the sequence numbers
3779 * are above the previous ones.
3781 if (thflags
& TH_SYN
&&
3782 tp
->t_state
== TCPS_TIME_WAIT
&&
3783 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
3784 iss
= tcp_new_isn(tp
);
3786 tcp_unlock(so
, 1, 0);
3790 * If window is closed can only take segments at
3791 * window edge, and have to drop data and PUSH from
3792 * incoming segments. Continue processing, but
3793 * remember to ack. Otherwise, drop segment
3796 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
3797 tp
->t_flags
|= TF_ACKNOW
;
3798 tcpstat
.tcps_rcvwinprobe
++;
3802 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3805 thflags
&= ~(TH_PUSH
|TH_FIN
);
3809 * If last ACK falls within this segment's sequence numbers,
3810 * record its timestamp.
3812 * 1) That the test incorporates suggestions from the latest
3813 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
3814 * 2) That updating only on newer timestamps interferes with
3815 * our earlier PAWS tests, so this check should be solely
3816 * predicated on the sequence space of this segment.
3817 * 3) That we modify the segment boundary check to be
3818 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
3819 * instead of RFC1323's
3820 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
3821 * This modified check allows us to overcome RFC1323's
3822 * limitations as described in Stevens TCP/IP Illustrated
3823 * Vol. 2 p.869. In such cases, we can still calculate the
3824 * RTT correctly when RCV.NXT == Last.ACK.Sent.
3826 if ((to
.to_flags
& TOF_TS
) != 0 &&
3827 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3828 SEQ_LEQ(tp
->last_ack_sent
, th
->th_seq
+ tlen
+
3829 ((thflags
& (TH_SYN
|TH_FIN
)) != 0))) {
3830 tp
->ts_recent_age
= tcp_now
;
3831 tp
->ts_recent
= to
.to_tsval
;
3835 * If a SYN is in the window, then this is an
3836 * error and we send an RST and drop the connection.
3838 if (thflags
& TH_SYN
) {
3839 tp
= tcp_drop(tp
, ECONNRESET
);
3840 rstreason
= BANDLIM_UNLIMITED
;
3841 postevent(so
, 0, EV_RESET
);
3842 IF_TCP_STATINC(ifp
, synwindow
);
3847 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
3848 * flag is on (half-synchronized state), then queue data for
3849 * later processing; else drop segment and return.
3851 if ((thflags
& TH_ACK
) == 0) {
3852 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
3853 (tp
->t_flags
& TF_NEEDSYN
)) {
3854 if ((tfo_enabled(tp
))) {
3856 * So, we received a valid segment while in
3857 * SYN-RECEIVED (TF_NEEDSYN is actually never
3858 * set, so this is dead code).
3859 * As this cannot be an RST (see that if a bit
3860 * higher), and it does not have the ACK-flag
3861 * set, we want to retransmit the SYN/ACK.
3862 * Thus, we have to reset snd_nxt to snd_una to
3863 * trigger the going back to sending of the
3864 * SYN/ACK. This is more consistent with the
3865 * behavior of tcp_output(), which expects
3866 * to send the segment that is pointed to by
3869 tp
->snd_nxt
= tp
->snd_una
;
3872 * We need to make absolutely sure that we are
3873 * going to reply upon a duplicate SYN-segment.
3875 if (th
->th_flags
& TH_SYN
)
3880 } else if (tp
->t_flags
& TF_ACKNOW
)
3890 switch (tp
->t_state
) {
3893 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
3894 * ESTABLISHED state and continue processing.
3895 * The ACK was checked above.
3897 case TCPS_SYN_RECEIVED
:
3899 tcpstat
.tcps_connects
++;
3901 /* Do window scaling? */
3902 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
3903 tp
->snd_scale
= tp
->requested_s_scale
;
3904 tp
->rcv_scale
= tp
->request_r_scale
;
3905 tp
->snd_wnd
= th
->th_win
<< tp
->snd_scale
;
3906 tiwin
= tp
->snd_wnd
;
3910 * SYN-RECEIVED -> ESTABLISHED
3911 * SYN-RECEIVED* -> FIN-WAIT-1
3913 tp
->t_starttime
= tcp_now
;
3914 tcp_sbrcv_tstmp_check(tp
);
3915 if (tp
->t_flags
& TF_NEEDFIN
) {
3916 DTRACE_TCP4(state__change
, void, NULL
,
3917 struct inpcb
*, inp
,
3918 struct tcpcb
*, tp
, int32_t, TCPS_FIN_WAIT_1
);
3919 tp
->t_state
= TCPS_FIN_WAIT_1
;
3920 tp
->t_flags
&= ~TF_NEEDFIN
;
3922 DTRACE_TCP4(state__change
, void, NULL
,
3923 struct inpcb
*, inp
,
3924 struct tcpcb
*, tp
, int32_t, TCPS_ESTABLISHED
);
3925 tp
->t_state
= TCPS_ESTABLISHED
;
3926 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3927 TCP_CONN_KEEPIDLE(tp
));
3929 nstat_route_connect_success(
3930 tp
->t_inpcb
->inp_route
.ro_rt
);
3932 * The SYN is acknowledged but una is not updated
3933 * yet. So pass the value of ack to compute
3934 * sndbytes correctly
3936 inp_count_sndbytes(inp
, th
->th_ack
);
3939 * If segment contains data or ACK, will call tcp_reass()
3940 * later; if not, do so now to pass queued data to user.
3942 if (tlen
== 0 && (thflags
& TH_FIN
) == 0)
3943 (void) tcp_reass(tp
, (struct tcphdr
*)0, &tlen
,
3945 tp
->snd_wl1
= th
->th_seq
- 1;
3949 * Do not send the connect notification for additional subflows
3950 * until ACK for 3-way handshake arrives.
3952 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3953 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3954 isconnected
= FALSE
;
3958 if ((tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)) {
3959 /* Done this when receiving the SYN */
3960 isconnected
= FALSE
;
3962 OSDecrementAtomic(&tcp_tfo_halfcnt
);
3964 /* Panic if something has gone terribly wrong. */
3965 VERIFY(tcp_tfo_halfcnt
>= 0);
3967 tp
->t_tfo_flags
&= ~TFO_F_COOKIE_VALID
;
3971 * In case there is data in the send-queue (e.g., TFO is being
3972 * used, or connectx+data has been done), then if we would
3973 * "FALLTHROUGH", we would handle this ACK as if data has been
3974 * acknowledged. But, we have to prevent this. And this
3975 * can be prevented by increasing snd_una by 1, so that the
3976 * SYN is not considered as data (snd_una++ is actually also
3977 * done in SYN_SENT-state as part of the regular TCP stack).
3979 * In case there is data on this ack as well, the data will be
3980 * handled by the label "dodata" right after step6.
3982 if (so
->so_snd
.sb_cc
) {
3983 tp
->snd_una
++; /* SYN is acked */
3984 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
3985 tp
->snd_nxt
= tp
->snd_una
;
3988 * No duplicate-ACK handling is needed. So, we
3989 * directly advance to processing the ACK (aka,
3990 * updating the RTT estimation,...)
3992 * But, we first need to handle eventual SACKs,
3993 * because TFO will start sending data with the
3994 * SYN/ACK, so it might be that the client
3995 * includes a SACK with its ACK.
3997 if (SACK_ENABLED(tp
) &&
3998 (to
.to_nsacks
> 0 ||
3999 !TAILQ_EMPTY(&tp
->snd_holes
)))
4000 tcp_sack_doack(tp
, &to
, th
,
4009 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
4010 * ACKs. If the ack is in the range
4011 * tp->snd_una < th->th_ack <= tp->snd_max
4012 * then advance tp->snd_una to th->th_ack and drop
4013 * data from the retransmission queue. If this ACK reflects
4014 * more up to date window information we update our window information.
4016 case TCPS_ESTABLISHED
:
4017 case TCPS_FIN_WAIT_1
:
4018 case TCPS_FIN_WAIT_2
:
4019 case TCPS_CLOSE_WAIT
:
4022 case TCPS_TIME_WAIT
:
4023 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
4024 tcpstat
.tcps_rcvacktoomuch
++;
4027 if (SACK_ENABLED(tp
) && to
.to_nsacks
> 0) {
4028 recvd_dsack
= tcp_sack_process_dsack(tp
, &to
, th
);
4030 * If DSACK is received and this packet has no
4031 * other SACK information, it can be dropped.
4032 * We do not want to treat it as a duplicate ack.
4035 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) &&
4036 to
.to_nsacks
== 0) {
4037 tcp_bad_rexmt_check(tp
, th
, &to
);
4042 if (SACK_ENABLED(tp
) &&
4043 (to
.to_nsacks
> 0 || !TAILQ_EMPTY(&tp
->snd_holes
)))
4044 tcp_sack_doack(tp
, &to
, th
, &sack_bytes_acked
);
4047 if ((tp
->t_mpuna
) && (SEQ_GEQ(th
->th_ack
, tp
->t_mpuna
))) {
4048 if (tp
->t_mpflags
& TMPF_PREESTABLISHED
) {
4049 /* MP TCP establishment succeeded */
4051 if (tp
->t_mpflags
& TMPF_JOINED_FLOW
) {
4052 if (tp
->t_mpflags
& TMPF_SENT_JOIN
) {
4054 ~TMPF_PREESTABLISHED
;
4057 so
->so_flags
|= SOF_MPTCP_TRUE
;
4058 mptcplog((LOG_DEBUG
, "MPTCP "
4059 "Sockets: %s \n",__func__
),
4063 tp
->t_timer
[TCPT_JACK_RXMT
] = 0;
4064 tp
->t_mprxtshift
= 0;
4067 isconnected
= FALSE
;
4071 tp
->t_mpflags
&= ~TMPF_SENT_KEYS
;
4077 tcp_tfo_rcv_ack(tp
, th
);
4080 * If we have outstanding data (other than
4081 * a window probe), this is a completely
4082 * duplicate ack and the ack is the biggest we've seen.
4084 * Need to accommodate a change in window on duplicate acks
4085 * to allow operating systems that update window during
4086 * recovery with SACK
4088 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
4089 if (tlen
== 0 && (tiwin
== tp
->snd_wnd
||
4090 (to
.to_nsacks
> 0 && sack_bytes_acked
> 0))) {
4092 * If both ends send FIN at the same time,
4093 * then the ack will be a duplicate ack
4094 * but we have to process the FIN. Check
4095 * for this condition and process the FIN
4096 * instead of the dupack
4098 if ((thflags
& TH_FIN
) &&
4099 !TCPS_HAVERCVDFIN(tp
->t_state
))
4104 * MPTCP options that are ignored must
4105 * not be treated as duplicate ACKs.
4107 if (to
.to_flags
& TOF_MPTCP
) {
4111 if ((isconnected
) && (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
4112 mptcplog((LOG_DEBUG
, "MPTCP "
4113 "Sockets: bypass ack recovery\n"),
4115 MPTCP_LOGLVL_VERBOSE
);
4120 * If a duplicate acknowledgement was seen
4121 * after ECN, it indicates packet loss in
4122 * addition to ECN. Reset INRECOVERY flag
4123 * so that we can process partial acks
4126 if (tp
->ecn_flags
& TE_INRECOVERY
)
4127 tp
->ecn_flags
&= ~TE_INRECOVERY
;
4129 tcpstat
.tcps_rcvdupack
++;
4133 * Check if we need to reset the limit on
4136 if (tp
->t_early_rexmt_count
> 0 &&
4138 (tp
->t_early_rexmt_win
+
4139 TCP_EARLY_REXMT_WIN
)))
4140 tp
->t_early_rexmt_count
= 0;
4143 * Is early retransmit needed? We check for
4144 * this when the connection is waiting for
4145 * duplicate acks to enter fast recovery.
4147 if (!IN_FASTRECOVERY(tp
))
4148 tcp_early_rexmt_check(tp
, th
);
4151 * If we've seen exactly rexmt threshold
4152 * of duplicate acks, assume a packet
4153 * has been dropped and retransmit it.
4154 * Kludge snd_nxt & the congestion
4155 * window so we send only this one
4158 * We know we're losing at the current
4159 * window size so do congestion avoidance
4160 * (set ssthresh to half the current window
4161 * and pull our congestion window back to
4162 * the new ssthresh).
4164 * Dup acks mean that packets have left the
4165 * network (they're now cached at the receiver)
4166 * so bump cwnd by the amount in the receiver
4167 * to keep a constant cwnd packets in the
4170 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
4171 (th
->th_ack
!= tp
->snd_una
4172 && sack_bytes_acked
== 0)) {
4174 tp
->t_rexmtthresh
= tcprexmtthresh
;
4175 } else if (tp
->t_dupacks
> tp
->t_rexmtthresh
||
4176 IN_FASTRECOVERY(tp
)) {
4179 * If this connection was seeing packet
4180 * reordering, then recovery might be
4181 * delayed to disambiguate between
4182 * reordering and loss
4184 if (SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
) &&
4186 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) ==
4187 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
4189 * Since the SACK information is already
4190 * updated, this ACK will be dropped
4195 if (SACK_ENABLED(tp
)
4196 && IN_FASTRECOVERY(tp
)) {
4200 * Compute the amount of data in flight first.
4201 * We can inject new data into the pipe iff
4202 * we have less than 1/2 the original window's
4203 * worth of data in flight.
4205 awnd
= (tp
->snd_nxt
- tp
->snd_fack
) +
4206 tp
->sackhint
.sack_bytes_rexmit
;
4207 if (awnd
< tp
->snd_ssthresh
) {
4208 tp
->snd_cwnd
+= tp
->t_maxseg
;
4209 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
4210 tp
->snd_cwnd
= tp
->snd_ssthresh
;
4213 tp
->snd_cwnd
+= tp
->t_maxseg
;
4216 /* Process any window updates */
4217 if (tiwin
> tp
->snd_wnd
)
4218 tcp_update_window(tp
, thflags
,
4220 tcp_ccdbg_trace(tp
, th
,
4221 TCP_CC_IN_FASTRECOVERY
);
4223 (void) tcp_output(tp
);
4226 } else if (tp
->t_dupacks
== tp
->t_rexmtthresh
) {
4227 tcp_seq onxt
= tp
->snd_nxt
;
4230 * If we're doing sack, check to
4231 * see if we're already in sack
4232 * recovery. If we're not doing sack,
4233 * check to see if we're in newreno
4236 if (SACK_ENABLED(tp
)) {
4237 if (IN_FASTRECOVERY(tp
)) {
4240 } else if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
4244 if (SEQ_LEQ(th
->th_ack
,
4250 if (tp
->t_flags
& TF_SENTFIN
)
4251 tp
->snd_recover
= tp
->snd_max
- 1;
4253 tp
->snd_recover
= tp
->snd_max
;
4254 tp
->t_timer
[TCPT_PTO
] = 0;
4258 * If the connection has seen pkt
4259 * reordering, delay recovery until
4260 * it is clear that the packet
4263 if (SACK_ENABLED(tp
) &&
4265 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
4266 == TF_PKTS_REORDERED
&&
4267 !IN_FASTRECOVERY(tp
) &&
4268 tp
->t_reorderwin
> 0 &&
4269 (tp
->t_state
== TCPS_ESTABLISHED
||
4270 tp
->t_state
== TCPS_FIN_WAIT_1
)) {
4271 tp
->t_timer
[TCPT_DELAYFR
] =
4272 OFFSET_FROM_START(tp
,
4274 tp
->t_flagsext
|= TF_DELAY_RECOVERY
;
4275 tcpstat
.tcps_delay_recovery
++;
4276 tcp_ccdbg_trace(tp
, th
,
4277 TCP_CC_DELAY_FASTRECOVERY
);
4281 tcp_rexmt_save_state(tp
);
4283 * If the current tcp cc module has
4284 * defined a hook for tasks to run
4285 * before entering FR, call it
4287 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
4288 CC_ALGO(tp
)->pre_fr(tp
);
4289 ENTER_FASTRECOVERY(tp
);
4290 tp
->t_timer
[TCPT_REXMT
] = 0;
4291 if (TCP_ECN_ENABLED(tp
))
4292 tp
->ecn_flags
|= TE_SENDCWR
;
4294 if (SACK_ENABLED(tp
)) {
4295 tcpstat
.tcps_sack_recovery_episode
++;
4296 tp
->t_sack_recovery_episode
++;
4297 tp
->sack_newdata
= tp
->snd_nxt
;
4298 tp
->snd_cwnd
= tp
->t_maxseg
;
4300 ~TF_CWND_NONVALIDATED
;
4302 /* Process any window updates */
4303 if (tiwin
> tp
->snd_wnd
)
4308 tcp_ccdbg_trace(tp
, th
,
4309 TCP_CC_ENTER_FASTRECOVERY
);
4310 (void) tcp_output(tp
);
4313 tp
->snd_nxt
= th
->th_ack
;
4314 tp
->snd_cwnd
= tp
->t_maxseg
;
4316 /* Process any window updates */
4317 if (tiwin
> tp
->snd_wnd
)
4318 tcp_update_window(tp
,
4322 (void) tcp_output(tp
);
4323 if (tp
->t_flagsext
& TF_CWND_NONVALIDATED
) {
4324 tcp_cc_adjust_nonvalidated_cwnd(tp
);
4326 tp
->snd_cwnd
= tp
->snd_ssthresh
+
4327 tp
->t_maxseg
* tp
->t_dupacks
;
4329 if (SEQ_GT(onxt
, tp
->snd_nxt
))
4332 tcp_ccdbg_trace(tp
, th
,
4333 TCP_CC_ENTER_FASTRECOVERY
);
4335 } else if (limited_txmt
&&
4336 ALLOW_LIMITED_TRANSMIT(tp
) &&
4337 (!(SACK_ENABLED(tp
)) || sack_bytes_acked
> 0) &&
4338 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) > 0) {
4339 u_int32_t incr
= (tp
->t_maxseg
* tp
->t_dupacks
);
4341 /* Use Limited Transmit algorithm on the first two
4342 * duplicate acks when there is new data to transmit
4344 tp
->snd_cwnd
+= incr
;
4345 tcpstat
.tcps_limited_txt
++;
4346 (void) tcp_output(tp
);
4348 tcp_ccdbg_trace(tp
, th
, TCP_CC_LIMITED_TRANSMIT
);
4350 /* Reset snd_cwnd back to normal */
4351 tp
->snd_cwnd
-= incr
;
4357 * If the congestion window was inflated to account
4358 * for the other side's cached packets, retract it.
4360 if (IN_FASTRECOVERY(tp
)) {
4361 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
4363 * If we received an ECE and entered
4364 * recovery, the subsequent ACKs should
4365 * not be treated as partial acks.
4367 if (tp
->ecn_flags
& TE_INRECOVERY
)
4370 if (SACK_ENABLED(tp
))
4371 tcp_sack_partialack(tp
, th
);
4373 tcp_newreno_partial_ack(tp
, th
);
4374 tcp_ccdbg_trace(tp
, th
, TCP_CC_PARTIAL_ACK
);
4376 EXIT_FASTRECOVERY(tp
);
4377 if (CC_ALGO(tp
)->post_fr
!= NULL
)
4378 CC_ALGO(tp
)->post_fr(tp
, th
);
4380 tcp_clear_pipeack_state(tp
);
4381 tcp_ccdbg_trace(tp
, th
,
4382 TCP_CC_EXIT_FASTRECOVERY
);
4384 } else if ((tp
->t_flagsext
&
4385 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
4386 == (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
4388 * If the ack acknowledges upto snd_recover or if
4389 * it acknowledges all the snd holes, exit
4390 * recovery and cancel the timer. Otherwise,
4391 * this is a partial ack. Wait for recovery timer
4392 * to enter recovery. The snd_holes have already
4395 if (SEQ_GEQ(th
->th_ack
, tp
->snd_recover
) ||
4396 TAILQ_EMPTY(&tp
->snd_holes
)) {
4397 tp
->t_timer
[TCPT_DELAYFR
] = 0;
4398 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
4399 EXIT_FASTRECOVERY(tp
);
4400 tcp_ccdbg_trace(tp
, th
,
4401 TCP_CC_EXIT_FASTRECOVERY
);
4405 * We were not in fast recovery. Reset the
4406 * duplicate ack counter.
4409 tp
->t_rexmtthresh
= tcprexmtthresh
;
4414 * If we reach this point, ACK is not a duplicate,
4415 * i.e., it ACKs something we sent.
4417 if (tp
->t_flags
& TF_NEEDSYN
) {
4419 * T/TCP: Connection was half-synchronized, and our
4420 * SYN has been ACK'd (so connection is now fully
4421 * synchronized). Go to non-starred state,
4422 * increment snd_una for ACK of SYN, and check if
4423 * we can do window scaling.
4425 tp
->t_flags
&= ~TF_NEEDSYN
;
4427 /* Do window scaling? */
4428 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
4429 tp
->snd_scale
= tp
->requested_s_scale
;
4430 tp
->rcv_scale
= tp
->request_r_scale
;
4435 VERIFY(SEQ_GEQ(th
->th_ack
, tp
->snd_una
));
4436 acked
= BYTES_ACKED(th
, tp
);
4437 tcpstat
.tcps_rcvackpack
++;
4438 tcpstat
.tcps_rcvackbyte
+= acked
;
4441 * If the last packet was a retransmit, make sure
4442 * it was not spurious.
4444 * This will also take care of congestion window
4445 * adjustment if a last packet was recovered due to a
4448 tcp_bad_rexmt_check(tp
, th
, &to
);
4450 /* Recalculate the RTT */
4451 tcp_compute_rtt(tp
, &to
, th
);
4454 * If all outstanding data is acked, stop retransmit
4455 * timer and remember to restart (more output or persist).
4456 * If there is more data to be acked, restart retransmit
4457 * timer, using current (possibly backed-off) value.
4459 TCP_RESET_REXMT_STATE(tp
);
4460 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
4461 tp
->t_rttmin
, TCPTV_REXMTMAX
,
4462 TCP_ADD_REXMTSLOP(tp
));
4463 if (th
->th_ack
== tp
->snd_max
) {
4464 tp
->t_timer
[TCPT_REXMT
] = 0;
4465 tp
->t_timer
[TCPT_PTO
] = 0;
4467 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
4468 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
4472 * If no data (only SYN) was ACK'd, skip rest of ACK
4479 * When outgoing data has been acked (except the SYN+data), we
4480 * mark this connection as "sending good" for TFO.
4482 if ((tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
) &&
4483 !(tp
->t_tfo_flags
& TFO_F_NO_SNDPROBING
) &&
4484 !(th
->th_flags
& TH_SYN
))
4485 tcp_heuristic_tfo_snd_good(tp
);
4488 * If TH_ECE is received, make sure that ECN is enabled
4489 * on that connection and we have sent ECT on data packets.
4491 if ((thflags
& TH_ECE
) != 0 && TCP_ECN_ENABLED(tp
) &&
4492 (tp
->ecn_flags
& TE_SENDIPECT
)) {
4494 * Reduce the congestion window if we haven't
4497 if (!IN_FASTRECOVERY(tp
)) {
4498 tcp_reduce_congestion_window(tp
);
4499 tp
->ecn_flags
|= (TE_INRECOVERY
|TE_SENDCWR
);
4501 * Also note that the connection received
4504 tp
->ecn_flags
|= TE_RECV_ECN_ECE
;
4505 INP_INC_IFNET_STAT(inp
, ecn_recv_ece
);
4506 tcpstat
.tcps_ecn_recv_ece
++;
4507 tcp_ccdbg_trace(tp
, th
, TCP_CC_ECN_RCVD
);
4512 * When new data is acked, open the congestion window.
4513 * The specifics of how this is achieved are up to the
4514 * congestion control algorithm in use for this connection.
4516 * The calculations in this function assume that snd_una is
4519 if (!IN_FASTRECOVERY(tp
)) {
4520 if (CC_ALGO(tp
)->ack_rcvd
!= NULL
)
4521 CC_ALGO(tp
)->ack_rcvd(tp
, th
);
4522 tcp_ccdbg_trace(tp
, th
, TCP_CC_ACK_RCVD
);
4524 if (acked
> so
->so_snd
.sb_cc
) {
4525 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
4526 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
4527 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
4528 so
->so_msg_state
->msg_serial_bytes
-=
4529 (int)so
->so_snd
.sb_cc
;
4533 sbdrop(&so
->so_snd
, acked
);
4534 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
4535 so
->so_msg_state
->msg_serial_bytes
-=
4538 tcp_sbsnd_trim(&so
->so_snd
);
4539 tp
->snd_wnd
-= acked
;
4542 /* detect una wraparound */
4543 if ( !IN_FASTRECOVERY(tp
) &&
4544 SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
4545 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
4546 tp
->snd_recover
= th
->th_ack
- 1;
4548 if (IN_FASTRECOVERY(tp
) &&
4549 SEQ_GEQ(th
->th_ack
, tp
->snd_recover
))
4550 EXIT_FASTRECOVERY(tp
);
4552 tp
->snd_una
= th
->th_ack
;
4554 if (SACK_ENABLED(tp
)) {
4555 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
))
4556 tp
->snd_recover
= tp
->snd_una
;
4558 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
4559 tp
->snd_nxt
= tp
->snd_una
;
4560 if (!SLIST_EMPTY(&tp
->t_rxt_segments
) &&
4561 !TCP_DSACK_SEQ_IN_WINDOW(tp
, tp
->t_dsack_lastuna
,
4563 tcp_rxtseg_clean(tp
);
4564 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
4565 tp
->t_bwmeas
!= NULL
)
4566 tcp_bwmeas_check(tp
);
4569 * sowwakeup must happen after snd_una, et al. are
4570 * updated so that the sequence numbers are in sync with
4575 if (!SLIST_EMPTY(&tp
->t_notify_ack
))
4576 tcp_notify_acknowledgement(tp
, so
);
4578 switch (tp
->t_state
) {
4581 * In FIN_WAIT_1 STATE in addition to the processing
4582 * for the ESTABLISHED state if our FIN is now acknowledged
4583 * then enter FIN_WAIT_2.
4585 case TCPS_FIN_WAIT_1
:
4586 if (ourfinisacked
) {
4588 * If we can't receive any more
4589 * data, then closing user can proceed.
4590 * Starting the TCPT_2MSL timer is contrary to the
4591 * specification, but if we don't get a FIN
4592 * we'll hang forever.
4594 if (so
->so_state
& SS_CANTRCVMORE
) {
4595 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
4596 TCP_CONN_MAXIDLE(tp
));
4597 isconnected
= FALSE
;
4598 isdisconnected
= TRUE
;
4600 DTRACE_TCP4(state__change
, void, NULL
,
4601 struct inpcb
*, inp
,
4603 int32_t, TCPS_FIN_WAIT_2
);
4604 tp
->t_state
= TCPS_FIN_WAIT_2
;
4605 /* fall through and make sure we also recognize
4606 * data ACKed with the FIN
4612 * In CLOSING STATE in addition to the processing for
4613 * the ESTABLISHED state if the ACK acknowledges our FIN
4614 * then enter the TIME-WAIT state, otherwise ignore
4618 if (ourfinisacked
) {
4619 DTRACE_TCP4(state__change
, void, NULL
,
4620 struct inpcb
*, inp
,
4622 int32_t, TCPS_TIME_WAIT
);
4623 tp
->t_state
= TCPS_TIME_WAIT
;
4624 tcp_canceltimers(tp
);
4625 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
4626 tp
->t_flags
|= TF_CLOSING
;
4628 add_to_time_wait(tp
, 2 * tcp_msl
);
4630 isconnected
= FALSE
;
4631 isdisconnected
= TRUE
;
4636 * In LAST_ACK, we may still be waiting for data to drain
4637 * and/or to be acked, as well as for the ack of our FIN.
4638 * If our FIN is now acknowledged, delete the TCB,
4639 * enter the closed state and return.
4642 if (ourfinisacked
) {
4649 * In TIME_WAIT state the only thing that should arrive
4650 * is a retransmission of the remote FIN. Acknowledge
4651 * it and restart the finack timer.
4653 case TCPS_TIME_WAIT
:
4654 add_to_time_wait(tp
, 2 * tcp_msl
);
4659 * If there is a SACK option on the ACK and we
4660 * haven't seen any duplicate acks before, count
4661 * it as a duplicate ack even if the cumulative
4662 * ack is advanced. If the receiver delayed an
4663 * ack and detected loss afterwards, then the ack
4664 * will advance cumulative ack and will also have
4665 * a SACK option. So counting it as one duplicate
4668 if (sack_ackadv
== 1 &&
4669 tp
->t_state
== TCPS_ESTABLISHED
&&
4670 SACK_ENABLED(tp
) && sack_bytes_acked
> 0 &&
4671 to
.to_nsacks
> 0 && tp
->t_dupacks
== 0 &&
4672 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) && tlen
== 0 &&
4673 !(tp
->t_flagsext
& TF_PKTS_REORDERED
)) {
4674 tcpstat
.tcps_sack_ackadv
++;
4675 goto process_dupack
;
4681 * Update window information.
4683 if (tcp_update_window(tp
, thflags
, th
, tiwin
, tlen
))
4687 * Process segments with URG.
4689 if ((thflags
& TH_URG
) && th
->th_urp
&&
4690 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4692 * This is a kludge, but if we receive and accept
4693 * random urgent pointers, we'll crash in
4694 * soreceive. It's hard to imagine someone
4695 * actually wanting to send this much urgent data.
4697 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
4698 th
->th_urp
= 0; /* XXX */
4699 thflags
&= ~TH_URG
; /* XXX */
4700 goto dodata
; /* XXX */
4703 * If this segment advances the known urgent pointer,
4704 * then mark the data stream. This should not happen
4705 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
4706 * a FIN has been received from the remote side.
4707 * In these states we ignore the URG.
4709 * According to RFC961 (Assigned Protocols),
4710 * the urgent pointer points to the last octet
4711 * of urgent data. We continue, however,
4712 * to consider it to indicate the first octet
4713 * of data past the urgent section as the original
4714 * spec states (in one of two places).
4716 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
4717 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
4718 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
4719 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
4720 if (so
->so_oobmark
== 0) {
4721 so
->so_state
|= SS_RCVATMARK
;
4722 postevent(so
, 0, EV_OOB
);
4725 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
4728 * Remove out of band data so doesn't get presented to user.
4729 * This can happen independent of advancing the URG pointer,
4730 * but if two URG's are pending at once, some out-of-band
4731 * data may creep in... ick.
4733 if (th
->th_urp
<= (u_int32_t
)tlen
4735 && (so
->so_options
& SO_OOBINLINE
) == 0
4738 tcp_pulloutofband(so
, th
, m
,
4739 drop_hdrlen
); /* hdr drop is delayed */
4742 * If no out of band data is expected,
4743 * pull receive urgent pointer along
4744 * with the receive window.
4746 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
4747 tp
->rcv_up
= tp
->rcv_nxt
;
4751 /* Set socket's connect or disconnect state correcly before doing data.
4752 * The following might unlock the socket if there is an upcall or a socket
4757 } else if (isdisconnected
) {
4758 soisdisconnected(so
);
4761 /* Let's check the state of pcb just to make sure that it did not get closed
4762 * when we unlocked above
4764 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
4765 /* Just drop the packet that we are processing and return */
4770 * Process the segment text, merging it into the TCP sequencing queue,
4771 * and arranging for acknowledgment of receipt if necessary.
4772 * This process logically involves adjusting tp->rcv_wnd as data
4773 * is presented to the user (this happens in tcp_usrreq.c,
4774 * case PRU_RCVD). If a FIN has already been received on this
4775 * connection then we just ignore the text.
4777 * If we are in SYN-received state and got a valid TFO cookie, we want
4778 * to process the data.
4780 if ((tlen
|| (thflags
& TH_FIN
)) &&
4781 TCPS_HAVERCVDFIN(tp
->t_state
) == 0 &&
4782 (TCPS_HAVEESTABLISHED(tp
->t_state
) ||
4783 (tp
->t_state
== TCPS_SYN_RECEIVED
&&
4784 (tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)))) {
4785 tcp_seq save_start
= th
->th_seq
;
4786 tcp_seq save_end
= th
->th_seq
+ tlen
;
4787 m_adj(m
, drop_hdrlen
); /* delayed header drop */
4789 * Insert segment which includes th into TCP reassembly queue
4790 * with control block tp. Set thflags to whether reassembly now
4791 * includes a segment with FIN. This handles the common case
4792 * inline (segment is the next to be received on an established
4793 * connection, and the queue is empty), avoiding linkage into
4794 * and removal from the queue and repetition of various
4796 * Set DELACK for segments received in order, but ack
4797 * immediately when segments are out of order (so
4798 * fast retransmit can work).
4800 if (th
->th_seq
== tp
->rcv_nxt
&& LIST_EMPTY(&tp
->t_segq
)) {
4801 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4803 * Calculate the RTT on the receiver only if the
4804 * connection is in streaming mode and the last
4805 * packet was not an end-of-write
4807 if (tp
->t_flags
& TF_STREAMING_ON
)
4808 tcp_compute_rtt(tp
, &to
, th
);
4810 if (DELAY_ACK(tp
, th
) &&
4811 ((tp
->t_flags
& TF_ACKNOW
) == 0) ) {
4812 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4813 tp
->t_flags
|= TF_DELACK
;
4814 tp
->t_timer
[TCPT_DELACK
] =
4815 OFFSET_FROM_START(tp
, tcp_delack
);
4819 tp
->t_flags
|= TF_ACKNOW
;
4821 tp
->rcv_nxt
+= tlen
;
4822 thflags
= th
->th_flags
& TH_FIN
;
4823 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
4824 tcpstat
.tcps_rcvbyte
+= tlen
;
4825 if (nstat_collect
) {
4826 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
4827 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4828 rxpackets
, m
->m_pkthdr
.lro_npkts
);
4830 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4833 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4836 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
,
4837 TCP_AUTORCVBUF_MAX(ifp
));
4838 so_recv_data_stat(so
, m
, drop_hdrlen
);
4840 if (sbappendstream_rcvdemux(so
, m
,
4841 th
->th_seq
- (tp
->irs
+ 1), 0)) {
4845 thflags
= tcp_reass(tp
, th
, &tlen
, m
, ifp
);
4846 tp
->t_flags
|= TF_ACKNOW
;
4849 if ((tlen
> 0 || (th
->th_flags
& TH_FIN
)) && SACK_ENABLED(tp
)) {
4850 if (th
->th_flags
& TH_FIN
)
4852 tcp_update_sack_list(tp
, save_start
, save_end
);
4855 tcp_adaptive_rwtimo_check(tp
, tlen
);
4858 tcp_tfo_rcv_data(tp
);
4860 if (tp
->t_flags
& TF_DELACK
)
4864 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4865 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
4866 th
->th_seq
, th
->th_ack
, th
->th_win
);
4871 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4872 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
4873 th
->th_seq
, th
->th_ack
, th
->th_win
);
4883 * If FIN is received ACK the FIN and let the user know
4884 * that the connection is closing.
4886 if (thflags
& TH_FIN
) {
4887 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4889 postevent(so
, 0, EV_FIN
);
4891 * If connection is half-synchronized
4892 * (ie NEEDSYN flag on) then delay ACK,
4893 * so it may be piggybacked when SYN is sent.
4894 * Otherwise, since we received a FIN then no
4895 * more input can be expected, send ACK now.
4897 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4898 if (DELAY_ACK(tp
, th
) && (tp
->t_flags
& TF_NEEDSYN
)) {
4899 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4900 tp
->t_flags
|= TF_DELACK
;
4901 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
4904 tp
->t_flags
|= TF_ACKNOW
;
4908 switch (tp
->t_state
) {
4911 * In SYN_RECEIVED and ESTABLISHED STATES
4912 * enter the CLOSE_WAIT state.
4914 case TCPS_SYN_RECEIVED
:
4915 tp
->t_starttime
= tcp_now
;
4916 case TCPS_ESTABLISHED
:
4917 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
4918 struct tcpcb
*, tp
, int32_t, TCPS_CLOSE_WAIT
);
4919 tp
->t_state
= TCPS_CLOSE_WAIT
;
4923 * If still in FIN_WAIT_1 STATE FIN has not been acked so
4924 * enter the CLOSING state.
4926 case TCPS_FIN_WAIT_1
:
4927 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
4928 struct tcpcb
*, tp
, int32_t, TCPS_CLOSING
);
4929 tp
->t_state
= TCPS_CLOSING
;
4933 * In FIN_WAIT_2 state enter the TIME_WAIT state,
4934 * starting the time-wait timer, turning off the other
4937 case TCPS_FIN_WAIT_2
:
4938 DTRACE_TCP4(state__change
, void, NULL
,
4939 struct inpcb
*, inp
,
4941 int32_t, TCPS_TIME_WAIT
);
4942 tp
->t_state
= TCPS_TIME_WAIT
;
4943 tcp_canceltimers(tp
);
4944 tp
->t_flags
|= TF_ACKNOW
;
4945 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
4946 tp
->t_flags
|= TF_CLOSING
;
4948 add_to_time_wait(tp
, 2 * tcp_msl
);
4950 soisdisconnected(so
);
4954 * In TIME_WAIT state restart the 2 MSL time_wait timer.
4956 case TCPS_TIME_WAIT
:
4957 add_to_time_wait(tp
, 2 * tcp_msl
);
4962 if (so
->so_options
& SO_DEBUG
)
4963 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
4968 * Return any desired output.
4970 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
4971 (void) tcp_output(tp
);
4974 tcp_check_timer_state(tp
);
4977 tcp_unlock(so
, 1, 0);
4978 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4983 * Generate an ACK dropping incoming segment if it occupies
4984 * sequence space, where the ACK reflects our state.
4986 * We can now skip the test for the RST flag since all
4987 * paths to this code happen after packets containing
4988 * RST have been dropped.
4990 * In the SYN-RECEIVED state, don't send an ACK unless the
4991 * segment we received passes the SYN-RECEIVED ACK test.
4992 * If it fails send a RST. This breaks the loop in the
4993 * "LAND" DoS attack, and also prevents an ACK storm
4994 * between two listening ports that have been sent forged
4995 * SYN segments, each with the source address of the other.
4997 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
4998 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
4999 SEQ_GT(th
->th_ack
, tp
->snd_max
)) ) {
5000 rstreason
= BANDLIM_RST_OPENPORT
;
5001 IF_TCP_STATINC(ifp
, dospacket
);
5005 if (so
->so_options
& SO_DEBUG
)
5006 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5010 tp
->t_flags
|= TF_ACKNOW
;
5011 (void) tcp_output(tp
);
5013 /* Don't need to check timer state as we should have done it during tcp_output */
5014 tcp_unlock(so
, 1, 0);
5015 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
5017 dropwithresetnosock
:
5021 * Generate a RST, dropping incoming segment.
5022 * Make ACK acceptable to originator of segment.
5023 * Don't bother to respond if destination was broadcast/multicast.
5025 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
5029 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
5030 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
5034 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
5035 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
5036 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
5037 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
5039 /* IPv6 anycast check is done at tcp6_input() */
5042 * Perform bandwidth limiting.
5045 if (badport_bandlim(rstreason
) < 0)
5050 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
5051 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5054 bzero(&tra
, sizeof(tra
));
5055 tra
.ifscope
= ifscope
;
5056 tra
.awdl_unrestricted
= 1;
5057 tra
.intcoproc_allowed
= 1;
5058 if (thflags
& TH_ACK
)
5059 /* mtod() below is safe as long as hdr dropping is delayed */
5060 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
5063 if (thflags
& TH_SYN
)
5065 /* mtod() below is safe as long as hdr dropping is delayed */
5066 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+tlen
,
5067 (tcp_seq
)0, TH_RST
|TH_ACK
, &tra
);
5069 /* destroy temporarily created socket */
5072 tcp_unlock(so
, 1, 0);
5073 } else if ((inp
!= NULL
) && (nosock
== 0)) {
5074 tcp_unlock(so
, 1, 0);
5076 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
5082 * Drop space held by incoming segment and return.
5085 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
5086 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
5090 /* destroy temporarily created socket */
5093 tcp_unlock(so
, 1, 0);
5095 else if (nosock
== 0) {
5096 tcp_unlock(so
, 1, 0);
5098 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
5103 * Parse TCP options and place in tcpopt.
5106 tcp_dooptions(struct tcpcb
*tp
, u_char
*cp
, int cnt
, struct tcphdr
*th
,
5112 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
5114 if (opt
== TCPOPT_EOL
)
5116 if (opt
== TCPOPT_NOP
)
5122 if (optlen
< 2 || optlen
> cnt
)
5131 if (optlen
!= TCPOLEN_MAXSEG
)
5133 if (!(th
->th_flags
& TH_SYN
))
5135 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
5138 to
->to_flags
|= TOF_MSS
;
5142 if (optlen
!= TCPOLEN_WINDOW
)
5144 if (!(th
->th_flags
& TH_SYN
))
5146 to
->to_flags
|= TOF_SCALE
;
5147 to
->to_requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
5150 case TCPOPT_TIMESTAMP
:
5151 if (optlen
!= TCPOLEN_TIMESTAMP
)
5153 to
->to_flags
|= TOF_TS
;
5154 bcopy((char *)cp
+ 2,
5155 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
5156 NTOHL(to
->to_tsval
);
5157 bcopy((char *)cp
+ 6,
5158 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
5159 NTOHL(to
->to_tsecr
);
5160 /* Re-enable sending Timestamps if we received them */
5161 if (!(tp
->t_flags
& TF_REQ_TSTMP
) &&
5162 tcp_do_rfc1323
== 1)
5163 tp
->t_flags
|= TF_REQ_TSTMP
;
5165 case TCPOPT_SACK_PERMITTED
:
5167 optlen
!= TCPOLEN_SACK_PERMITTED
)
5169 if (th
->th_flags
& TH_SYN
)
5170 to
->to_flags
|= TOF_SACK
;
5173 if (optlen
<= 2 || (optlen
- 2) % TCPOLEN_SACK
!= 0)
5175 to
->to_nsacks
= (optlen
- 2) / TCPOLEN_SACK
;
5176 to
->to_sacks
= cp
+ 2;
5177 tcpstat
.tcps_sack_rcv_blocks
++;
5180 case TCPOPT_FASTOPEN
:
5181 if (optlen
== TCPOLEN_FASTOPEN_REQ
) {
5182 if (tp
->t_state
!= TCPS_LISTEN
)
5185 to
->to_flags
|= TOF_TFOREQ
;
5187 if (optlen
< TCPOLEN_FASTOPEN_REQ
||
5188 (optlen
- TCPOLEN_FASTOPEN_REQ
) > TFO_COOKIE_LEN_MAX
||
5189 (optlen
- TCPOLEN_FASTOPEN_REQ
) < TFO_COOKIE_LEN_MIN
)
5191 if (tp
->t_state
!= TCPS_LISTEN
&&
5192 tp
->t_state
!= TCPS_SYN_SENT
)
5195 to
->to_flags
|= TOF_TFO
;
5196 to
->to_tfo
= cp
+ 1;
5201 case TCPOPT_MULTIPATH
:
5202 tcp_do_mptcp_options(tp
, cp
, th
, to
, optlen
);
5210 tcp_finalize_options(struct tcpcb
*tp
, struct tcpopt
*to
, unsigned int ifscope
)
5212 if (to
->to_flags
& TOF_TS
) {
5213 tp
->t_flags
|= TF_RCVD_TSTMP
;
5214 tp
->ts_recent
= to
->to_tsval
;
5215 tp
->ts_recent_age
= tcp_now
;
5218 if (to
->to_flags
& TOF_MSS
)
5219 tcp_mss(tp
, to
->to_mss
, ifscope
);
5220 if (SACK_ENABLED(tp
)) {
5221 if (!(to
->to_flags
& TOF_SACK
))
5222 tp
->t_flagsext
&= ~(TF_SACK_ENABLE
);
5224 tp
->t_flags
|= TF_SACK_PERMIT
;
5226 if (to
->to_flags
& TOF_SCALE
) {
5227 tp
->t_flags
|= TF_RCVD_SCALE
;
5228 tp
->requested_s_scale
= to
->to_requested_s_scale
;
5230 /* Re-enable window scaling, if the option is received */
5231 if (tp
->request_r_scale
> 0)
5232 tp
->t_flags
|= TF_REQ_SCALE
;
5237 * Pull out of band byte out of a segment so
5238 * it doesn't appear in the user's data queue.
5239 * It is still reflected in the segment length for
5240 * sequencing purposes.
5242 * @param off delayed to be droped hdrlen
5245 tcp_pulloutofband(struct socket
*so
, struct tcphdr
*th
, struct mbuf
*m
, int off
)
5247 int cnt
= off
+ th
->th_urp
- 1;
5250 if (m
->m_len
> cnt
) {
5251 char *cp
= mtod(m
, caddr_t
) + cnt
;
5252 struct tcpcb
*tp
= sototcpcb(so
);
5255 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
5256 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
5258 if (m
->m_flags
& M_PKTHDR
)
5267 panic("tcp_pulloutofband");
5271 get_base_rtt(struct tcpcb
*tp
)
5273 struct rtentry
*rt
= tp
->t_inpcb
->inp_route
.ro_rt
;
5274 return ((rt
== NULL
) ? 0 : rt
->rtt_min
);
5277 /* Each value of RTT base represents the minimum RTT seen in a minute.
5278 * We keep upto N_RTT_BASE minutes worth of history.
5281 update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
)
5283 u_int32_t base_rtt
, i
;
5286 if ((rt
= tp
->t_inpcb
->inp_route
.ro_rt
) == NULL
)
5288 if (rt
->rtt_expire_ts
== 0) {
5290 if (rt
->rtt_expire_ts
!= 0) {
5294 rt
->rtt_expire_ts
= tcp_now
;
5296 rt
->rtt_hist
[0] = rtt
;
5304 * If the recv side is being throttled, check if the
5305 * current RTT is closer to the base RTT seen in
5306 * first (recent) two slots. If so, unthrottle the stream.
5308 if ((tp
->t_flagsext
& TF_RECV_THROTTLE
) &&
5309 (int)(tcp_now
- tp
->t_recv_throttle_ts
) >= TCP_RECV_THROTTLE_WIN
) {
5310 base_rtt
= rt
->rtt_min
;
5311 if (tp
->t_rttcur
<= (base_rtt
+ target_qdelay
)) {
5312 tp
->t_flagsext
&= ~TF_RECV_THROTTLE
;
5313 tp
->t_recv_throttle_ts
= 0;
5316 #endif /* TRAFFIC_MGT */
5317 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5318 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5320 /* check the condition again to avoid race */
5321 if ((int)(tcp_now
- rt
->rtt_expire_ts
) >=
5322 TCP_RTT_HISTORY_EXPIRE_TIME
) {
5324 if (rt
->rtt_index
>= NRTT_HIST
)
5326 rt
->rtt_hist
[rt
->rtt_index
] = rtt
;
5327 rt
->rtt_expire_ts
= tcp_now
;
5329 rt
->rtt_hist
[rt
->rtt_index
] =
5330 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5332 /* forget the old value and update minimum */
5334 for (i
= 0; i
< NRTT_HIST
; ++i
) {
5335 if (rt
->rtt_hist
[i
] != 0 &&
5336 (rt
->rtt_min
== 0 ||
5337 rt
->rtt_hist
[i
] < rt
->rtt_min
))
5338 rt
->rtt_min
= rt
->rtt_hist
[i
];
5342 rt
->rtt_hist
[rt
->rtt_index
] =
5343 min(rt
->rtt_hist
[rt
->rtt_index
], rtt
);
5344 if (rt
->rtt_min
== 0)
5347 rt
->rtt_min
= min(rt
->rtt_min
, rtt
);
5352 * If we have a timestamp reply, update smoothed RTT. If no timestamp is
5353 * present but transmit timer is running and timed sequence number was
5354 * acked, update smoothed RTT.
5356 * If timestamps are supported, a receiver can update RTT even if
5357 * there is no outstanding data.
5359 * Some boxes send broken timestamp replies during the SYN+ACK phase,
5360 * ignore timestamps of 0or we could calculate a huge RTT and blow up
5361 * the retransmit timer.
5364 tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
, struct tcphdr
*th
)
5367 VERIFY(to
!= NULL
&& th
!= NULL
);
5368 if (tp
->t_rtttime
!= 0 && SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
5369 u_int32_t pipe_ack_val
;
5370 rtt
= tcp_now
- tp
->t_rtttime
;
5372 * Compute pipe ack -- the amount of data acknowledged
5375 if (SEQ_GT(th
->th_ack
, tp
->t_pipeack_lastuna
)) {
5376 pipe_ack_val
= th
->th_ack
- tp
->t_pipeack_lastuna
;
5377 /* Update the sample */
5378 tp
->t_pipeack_sample
[tp
->t_pipeack_ind
++] =
5380 tp
->t_pipeack_ind
%= TCP_PIPEACK_SAMPLE_COUNT
;
5382 /* Compute the max of the pipeack samples */
5383 pipe_ack_val
= tcp_get_max_pipeack(tp
);
5384 tp
->t_pipeack
= (pipe_ack_val
>
5385 TCP_CC_CWND_INIT_BYTES
) ?
5388 /* start another measurement */
5391 if (((to
->to_flags
& TOF_TS
) != 0) &&
5392 (to
->to_tsecr
!= 0) &&
5393 TSTMP_GEQ(tcp_now
, to
->to_tsecr
)) {
5394 tcp_xmit_timer(tp
, (tcp_now
- to
->to_tsecr
),
5395 to
->to_tsecr
, th
->th_ack
);
5396 } else if (rtt
> 0) {
5397 tcp_xmit_timer(tp
, rtt
, 0, th
->th_ack
);
5402 * Collect new round-trip time estimate and update averages and
5406 tcp_xmit_timer(struct tcpcb
*tp
, int rtt
,
5407 u_int32_t tsecr
, tcp_seq th_ack
)
5412 * On AWDL interface, the initial RTT measurement on SYN
5413 * can be wrong due to peer caching. Avoid the first RTT
5414 * measurement as it might skew up the RTO.
5415 * <rdar://problem/28739046>
5417 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
&&
5418 (tp
->t_inpcb
->inp_last_outifp
->if_eflags
& IFEF_AWDL
) &&
5419 th_ack
== tp
->iss
+ 1)
5422 if (tp
->t_flagsext
& TF_RECOMPUTE_RTT
) {
5423 if (SEQ_GT(th_ack
, tp
->snd_una
) &&
5424 SEQ_LEQ(th_ack
, tp
->snd_max
) &&
5426 TSTMP_GEQ(tsecr
, tp
->t_badrexmt_time
))) {
5428 * We received a new ACk after a
5429 * spurious timeout. Adapt retransmission
5430 * timer as described in rfc 4015.
5432 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
5433 tp
->t_badrexmt_time
= 0;
5434 tp
->t_srtt
= max(tp
->t_srtt_prev
, rtt
);
5435 tp
->t_srtt
= tp
->t_srtt
<< TCP_RTT_SHIFT
;
5436 tp
->t_rttvar
= max(tp
->t_rttvar_prev
, (rtt
>> 1));
5437 tp
->t_rttvar
= tp
->t_rttvar
<< TCP_RTTVAR_SHIFT
;
5439 if (tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
5440 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
5448 tcpstat
.tcps_rttupdated
++;
5453 update_base_rtt(tp
, rtt
);
5456 if (tp
->t_srtt
!= 0) {
5458 * srtt is stored as fixed point with 5 bits after the
5459 * binary point (i.e., scaled by 32). The following magic
5460 * is equivalent to the smoothing algorithm in rfc793 with
5461 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
5464 * Freebsd adjusts rtt to origin 0 by subtracting 1
5465 * from the provided rtt value. This was required because
5466 * of the way t_rtttime was initiailised to 1 before.
5467 * Since we changed t_rtttime to be based on
5468 * tcp_now, this extra adjustment is not needed.
5470 delta
= (rtt
<< TCP_DELTA_SHIFT
)
5471 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
5473 if ((tp
->t_srtt
+= delta
) <= 0)
5477 * We accumulate a smoothed rtt variance (actually, a
5478 * smoothed mean difference), then set the retransmit
5479 * timer to smoothed rtt + 4 times the smoothed variance.
5480 * rttvar is stored as fixed point with 4 bits after the
5481 * binary point (scaled by 16). The following is
5482 * equivalent to rfc793 smoothing with an alpha of .75
5483 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
5484 * rfc793's wired-in beta.
5488 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
5489 if ((tp
->t_rttvar
+= delta
) <= 0)
5491 if (tp
->t_rttbest
== 0 ||
5492 tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
5493 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
5496 * No rtt measurement yet - use the unsmoothed rtt.
5497 * Set the variance to half the rtt (so our first
5498 * retransmit happens at 3*rtt).
5500 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
5501 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
5505 nstat_route_rtt(tp
->t_inpcb
->inp_route
.ro_rt
, tp
->t_srtt
,
5509 * the retransmit should happen at rtt + 4 * rttvar.
5510 * Because of the way we do the smoothing, srtt and rttvar
5511 * will each average +1/2 tick of bias. When we compute
5512 * the retransmit timer, we want 1/2 tick of rounding and
5513 * 1 extra tick because of +-1/2 tick uncertainty in the
5514 * firing of the timer. The bias will give us exactly the
5515 * 1.5 tick we need. But, because the bias is
5516 * statistical, we have to test that we don't drop below
5517 * the minimum feasible timer (which is 2 ticks).
5519 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
5520 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
,
5521 TCP_ADD_REXMTSLOP(tp
));
5524 * We received an ack for a packet that wasn't retransmitted;
5525 * it is probably safe to discard any error indications we've
5526 * received recently. This isn't quite right, but close enough
5527 * for now (a route might have failed after we sent a segment,
5528 * and the return path might not be symmetrical).
5530 tp
->t_softerror
= 0;
5533 static inline unsigned int
5534 tcp_maxmtu(struct rtentry
*rt
)
5536 unsigned int maxmtu
;
5538 RT_LOCK_ASSERT_HELD(rt
);
5539 if (rt
->rt_rmx
.rmx_mtu
== 0)
5540 maxmtu
= rt
->rt_ifp
->if_mtu
;
5542 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, rt
->rt_ifp
->if_mtu
);
5548 static inline unsigned int
5549 tcp_maxmtu6(struct rtentry
*rt
)
5551 unsigned int maxmtu
;
5552 struct nd_ifinfo
*ndi
= NULL
;
5554 RT_LOCK_ASSERT_HELD(rt
);
5555 if ((ndi
= ND_IFINFO(rt
->rt_ifp
)) != NULL
&& !ndi
->initialized
)
5558 lck_mtx_lock(&ndi
->lock
);
5559 if (rt
->rt_rmx
.rmx_mtu
== 0)
5560 maxmtu
= IN6_LINKMTU(rt
->rt_ifp
);
5562 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, IN6_LINKMTU(rt
->rt_ifp
));
5564 lck_mtx_unlock(&ndi
->lock
);
5571 * Determine a reasonable value for maxseg size.
5572 * If the route is known, check route for mtu.
5573 * If none, use an mss that can be handled on the outgoing
5574 * interface without forcing IP to fragment; if bigger than
5575 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
5576 * to utilize large mbufs. If no route is found, route has no mtu,
5577 * or the destination isn't local, use a default, hopefully conservative
5578 * size (usually 512 or the default IP max size, but no more than the mtu
5579 * of the interface), as we can't discover anything about intervening
5580 * gateways or networks. We also initialize the congestion/slow start
5581 * window. While looking at the routing entry, we also initialize
5582 * other path-dependent parameters from pre-set or cached values
5583 * in the routing entry.
5585 * Also take into account the space needed for options that we
5586 * send regularly. Make maxseg shorter by that amount to assure
5587 * that we can send maxseg amount of data even when the options
5588 * are present. Store the upper limit of the length of options plus
5591 * NOTE that this routine is only called when we process an incoming
5592 * segment, for outgoing segments only tcp_mssopt is called.
5596 tcp_mss(struct tcpcb
*tp
, int offer
, unsigned int input_ifscope
)
5604 struct rmxp_tao
*taop
;
5605 int origoffer
= offer
;
5606 u_int32_t sb_max_corrected
;
5615 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
5616 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
5617 : sizeof (struct tcpiphdr
);
5619 #define min_protoh (sizeof (struct tcpiphdr))
5624 rt
= tcp_rtlookup6(inp
, input_ifscope
);
5629 rt
= tcp_rtlookup(inp
, input_ifscope
);
5631 isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
5634 tp
->t_maxopd
= tp
->t_maxseg
=
5636 isipv6
? tcp_v6mssdflt
:
5643 * Slower link window correction:
5644 * If a value is specificied for slowlink_wsize use it for
5645 * PPP links believed to be on a serial modem (speed <128Kbps).
5646 * Excludes 9600bps as it is the default value adversized
5647 * by pseudo-devices over ppp.
5649 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
5650 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
5651 tp
->t_flags
|= TF_SLOWLINK
;
5653 so
= inp
->inp_socket
;
5655 taop
= rmx_taop(rt
->rt_rmx
);
5657 * Offer == -1 means that we didn't receive SYN yet,
5658 * use cached value in that case;
5661 offer
= taop
->tao_mssopt
;
5663 * Offer == 0 means that there was no MSS on the SYN segment,
5664 * in this case we use tcp_mssdflt.
5669 isipv6
? tcp_v6mssdflt
:
5674 * Prevent DoS attack with too small MSS. Round up
5675 * to at least minmss.
5677 offer
= max(offer
, tcp_minmss
);
5679 * Sanity check: make sure that maxopd will be large
5680 * enough to allow some data on segments even is the
5681 * all the option space is used (40bytes). Otherwise
5682 * funny things may happen in tcp_output.
5684 offer
= max(offer
, 64);
5686 taop
->tao_mssopt
= offer
;
5689 * While we're here, check if there's an initial rtt
5690 * or rttvar. Convert from the route-table units
5691 * to scaled multiples of the slow timeout timer.
5693 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
) != 0) {
5694 tcp_getrt_rtt(tp
, rt
);
5696 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
5700 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5702 mss
= tcp_maxmtu(rt
);
5706 // At this point, the mss is just the MTU. Adjust if necessary.
5707 mss
= necp_socket_get_effective_mtu(inp
, mss
);
5712 if (rt
->rt_rmx
.rmx_mtu
== 0) {
5716 mss
= min(mss
, tcp_v6mssdflt
);
5720 mss
= min(mss
, tcp_mssdflt
);
5723 mss
= min(mss
, offer
);
5725 * maxopd stores the maximum length of data AND options
5726 * in a segment; maxseg is the amount of data in a normal
5727 * segment. We need to store this value (maxopd) apart
5728 * from maxseg, because now every segment carries options
5729 * and thus we normally have somewhat less data in segments.
5734 * origoffer==-1 indicates, that no segments were received yet.
5735 * In this case we just guess.
5737 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
5739 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
5740 mss
-= TCPOLEN_TSTAMP_APPA
;
5743 mss
-= mptcp_adj_mss(tp
, FALSE
);
5748 * Calculate corrected value for sb_max; ensure to upgrade the
5749 * numerator for large sb_max values else it will overflow.
5751 sb_max_corrected
= (sb_max
* (u_int64_t
)MCLBYTES
) / (MSIZE
+ MCLBYTES
);
5754 * If there's a pipesize (ie loopback), change the socket
5755 * buffer to that size only if it's bigger than the current
5756 * sockbuf size. Make the socket buffers an integral
5757 * number of mss units; if the mss is larger than
5758 * the socket buffer, decrease the mss.
5761 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
5762 if (bufsize
< so
->so_snd
.sb_hiwat
)
5764 bufsize
= so
->so_snd
.sb_hiwat
;
5768 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5769 if (bufsize
> sb_max_corrected
)
5770 bufsize
= sb_max_corrected
;
5771 (void)sbreserve(&so
->so_snd
, bufsize
);
5776 * Update MSS using recommendation from link status report. This is
5779 tcp_update_mss_locked(so
, ifp
);
5782 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
5783 if (bufsize
< so
->so_rcv
.sb_hiwat
)
5785 bufsize
= so
->so_rcv
.sb_hiwat
;
5786 if (bufsize
> mss
) {
5787 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5788 if (bufsize
> sb_max_corrected
)
5789 bufsize
= sb_max_corrected
;
5790 (void)sbreserve(&so
->so_rcv
, bufsize
);
5793 set_tcp_stream_priority(so
);
5795 if (rt
->rt_rmx
.rmx_ssthresh
) {
5797 * There's some sort of gateway or interface
5798 * buffer limit on the path. Use this to set
5799 * slow-start threshold, but set the threshold to
5800 * no less than 2*mss.
5802 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
5803 tcpstat
.tcps_usedssthresh
++;
5805 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
5809 * Set the slow-start flight size depending on whether this
5810 * is a local network or not.
5812 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
5813 CC_ALGO(tp
)->cwnd_init(tp
);
5815 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CWND_INIT
);
5817 /* Route locked during lookup above */
5822 * Determine the MSS option to send on an outgoing SYN.
5825 tcp_mssopt(struct tcpcb
*tp
)
5835 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
5836 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
5837 : sizeof (struct tcpiphdr
);
5839 #define min_protoh (sizeof (struct tcpiphdr))
5844 rt
= tcp_rtlookup6(tp
->t_inpcb
, IFSCOPE_NONE
);
5847 rt
= tcp_rtlookup(tp
->t_inpcb
, IFSCOPE_NONE
);
5851 isipv6
? tcp_v6mssdflt
:
5856 * Slower link window correction:
5857 * If a value is specificied for slowlink_wsize use it for PPP links
5858 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
5859 * it is the default value adversized by pseudo-devices over ppp.
5861 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
5862 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
5863 tp
->t_flags
|= TF_SLOWLINK
;
5867 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5869 mss
= tcp_maxmtu(rt
);
5871 /* Route locked during lookup above */
5875 // At this point, the mss is just the MTU. Adjust if necessary.
5876 mss
= necp_socket_get_effective_mtu(tp
->t_inpcb
, mss
);
5879 return (mss
- min_protoh
);
5883 * On a partial ack arrives, force the retransmission of the
5884 * next unacknowledged segment. Do not clear tp->t_dupacks.
5885 * By setting snd_nxt to th_ack, this forces retransmission timer to
5889 tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
)
5891 tcp_seq onxt
= tp
->snd_nxt
;
5892 u_int32_t ocwnd
= tp
->snd_cwnd
;
5893 tp
->t_timer
[TCPT_REXMT
] = 0;
5894 tp
->t_timer
[TCPT_PTO
] = 0;
5896 tp
->snd_nxt
= th
->th_ack
;
5898 * Set snd_cwnd to one segment beyond acknowledged offset
5899 * (tp->snd_una has not yet been updated when this function
5902 tp
->snd_cwnd
= tp
->t_maxseg
+ BYTES_ACKED(th
, tp
);
5903 tp
->t_flags
|= TF_ACKNOW
;
5904 (void) tcp_output(tp
);
5905 tp
->snd_cwnd
= ocwnd
;
5906 if (SEQ_GT(onxt
, tp
->snd_nxt
))
5909 * Partial window deflation. Relies on fact that tp->snd_una
5912 if (tp
->snd_cwnd
> BYTES_ACKED(th
, tp
))
5913 tp
->snd_cwnd
-= BYTES_ACKED(th
, tp
);
5916 tp
->snd_cwnd
+= tp
->t_maxseg
;
5920 * Drop a random TCP connection that hasn't been serviced yet and
5921 * is eligible for discard. There is a one in qlen chance that
5922 * we will return a null, saying that there are no dropable
5923 * requests. In this case, the protocol specific code should drop
5924 * the new request. This insures fairness.
5926 * The listening TCP socket "head" must be locked
5929 tcp_dropdropablreq(struct socket
*head
)
5931 struct socket
*so
, *sonext
;
5932 unsigned int i
, j
, qlen
;
5933 static u_int32_t rnd
= 0;
5934 static u_int64_t old_runtime
;
5935 static unsigned int cur_cnt
, old_cnt
;
5937 struct inpcb
*inp
= NULL
;
5940 if ((head
->so_options
& SO_ACCEPTCONN
) == 0)
5943 if (TAILQ_EMPTY(&head
->so_incomp
))
5947 * Check if there is any socket in the incomp queue
5948 * that is closed because of a reset from the peer and is
5949 * waiting to be garbage collected. If so, pick that as
5952 TAILQ_FOREACH_SAFE(so
, &head
->so_incomp
, so_list
, sonext
) {
5953 inp
= sotoinpcb(so
);
5954 tp
= intotcpcb(inp
);
5955 if (tp
!= NULL
&& tp
->t_state
== TCPS_CLOSED
&&
5956 so
->so_head
!= NULL
&&
5957 (so
->so_state
& (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) ==
5958 (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) {
5960 * The listen socket is already locked but we
5961 * can lock this socket here without lock ordering
5962 * issues because it is in the incomp queue and
5963 * is not visible to others.
5965 if (lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
5974 so
= TAILQ_FIRST(&head
->so_incomp
);
5976 now_sec
= net_uptime();
5977 if ((i
= (now_sec
- old_runtime
)) != 0) {
5978 old_runtime
= now_sec
;
5979 old_cnt
= cur_cnt
/ i
;
5983 qlen
= head
->so_incqlen
;
5985 rnd
= RandomULong();
5987 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
5988 rnd
= (314159 * rnd
+ 66329) & 0xffff;
5989 j
= ((qlen
+ 1) * rnd
) >> 16;
5992 so
= TAILQ_NEXT(so
, so_list
);
5994 /* Find a connection that is not already closing (or being served) */
5996 inp
= (struct inpcb
*)so
->so_pcb
;
5998 sonext
= TAILQ_NEXT(so
, so_list
);
6000 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0)
6003 * Avoid the issue of a socket being accepted
6004 * by one input thread and being dropped by
6005 * another input thread. If we can't get a hold
6006 * on this mutex, then grab the next socket in
6009 if (lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
6011 if ((so
->so_usecount
== 2) &&
6012 (so
->so_state
& SS_INCOMP
) &&
6013 !(so
->so_flags
& SOF_INCOMP_INPROGRESS
)) {
6017 * don't use if being accepted or
6018 * used in any other way
6020 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6021 tcp_unlock(so
, 1, 0);
6025 * do not try to lock the inp in
6026 * in_pcb_checkstate because the lock
6027 * is already held in some other thread.
6028 * Only drop the inp_wntcnt reference.
6030 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
6039 /* Makes sure socket is still in the right state to be discarded */
6041 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
6042 tcp_unlock(so
, 1, 0);
6047 if (so
->so_usecount
!= 2 || !(so
->so_state
& SS_INCOMP
)) {
6048 /* do not discard: that socket is being accepted */
6049 tcp_unlock(so
, 1, 0);
6053 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
6056 so
->so_state
&= ~SS_INCOMP
;
6057 so
->so_flags
|= SOF_OVERFLOW
;
6059 tcp_unlock(head
, 0, 0);
6061 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
6065 if (inp
->inp_wantcnt
> 0 && inp
->inp_wantcnt
!= WNT_STOPUSING
) {
6067 * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE
6068 * doesn't require a lock, it could have happened while
6069 * we are holding the lock. This pcb will have to
6070 * be garbage collected later.
6071 * Release the reference held for so_incomp queue
6073 VERIFY(so
->so_usecount
> 0);
6075 tcp_unlock(so
, 1, 0);
6078 * Unlock this socket and leave the reference on.
6079 * We need to acquire the pcbinfo lock in order to
6080 * fully dispose it off
6082 tcp_unlock(so
, 0, 0);
6084 lck_rw_lock_exclusive(tcbinfo
.ipi_lock
);
6087 /* Release the reference held for so_incomp queue */
6088 VERIFY(so
->so_usecount
> 0);
6091 if (so
->so_usecount
!= 1 ||
6092 (inp
->inp_wantcnt
> 0 &&
6093 inp
->inp_wantcnt
!= WNT_STOPUSING
)) {
6095 * There is an extra wantcount or usecount
6096 * that must have been added when the socket
6097 * was unlocked. This socket will have to be
6098 * garbage collected later
6100 tcp_unlock(so
, 1, 0);
6102 /* Drop the reference held for this function */
6103 VERIFY(so
->so_usecount
> 0);
6108 lck_rw_done(tcbinfo
.ipi_lock
);
6110 tcpstat
.tcps_drops
++;
6112 tcp_lock(head
, 0, 0);
6116 /* Set background congestion control on a socket */
6118 tcp_set_background_cc(struct socket
*so
)
6120 tcp_set_new_cc(so
, TCP_CC_ALGO_BACKGROUND_INDEX
);
6123 /* Set foreground congestion control on a socket */
6125 tcp_set_foreground_cc(struct socket
*so
)
6127 if (tcp_use_newreno
)
6128 tcp_set_new_cc(so
, TCP_CC_ALGO_NEWRENO_INDEX
);
6130 tcp_set_new_cc(so
, TCP_CC_ALGO_CUBIC_INDEX
);
6134 tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
)
6136 struct inpcb
*inp
= sotoinpcb(so
);
6137 struct tcpcb
*tp
= intotcpcb(inp
);
6138 u_char old_cc_index
= 0;
6139 if (tp
->tcp_cc_index
!= cc_index
) {
6141 old_cc_index
= tp
->tcp_cc_index
;
6143 if (CC_ALGO(tp
)->cleanup
!= NULL
)
6144 CC_ALGO(tp
)->cleanup(tp
);
6145 tp
->tcp_cc_index
= cc_index
;
6147 tcp_cc_allocate_state(tp
);
6149 if (CC_ALGO(tp
)->switch_to
!= NULL
)
6150 CC_ALGO(tp
)->switch_to(tp
, old_cc_index
);
6152 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CHANGE_ALGO
);
6157 tcp_set_recv_bg(struct socket
*so
)
6159 if (!IS_TCP_RECV_BG(so
))
6160 so
->so_flags1
|= SOF1_TRAFFIC_MGT_TCP_RECVBG
;
6162 /* Unset Large Receive Offload on background sockets */
6163 so_set_lro(so
, SO_TC_BK
);
6167 tcp_clear_recv_bg(struct socket
*so
)
6169 if (IS_TCP_RECV_BG(so
))
6170 so
->so_flags1
&= ~(SOF1_TRAFFIC_MGT_TCP_RECVBG
);
6173 * Set/unset use of Large Receive Offload depending on
6176 so_set_lro(so
, so
->so_traffic_class
);
6180 inp_fc_unthrottle_tcp(struct inpcb
*inp
)
6182 struct tcpcb
*tp
= inp
->inp_ppcb
;
6184 * Back off the slow-start threshold and enter
6185 * congestion avoidance phase
6187 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
6188 CC_ALGO(tp
)->pre_fr(tp
);
6190 tp
->snd_cwnd
= tp
->snd_ssthresh
;
6191 tp
->t_flagsext
&= ~TF_CWND_NONVALIDATED
;
6193 * Restart counting for ABC as we changed the
6194 * congestion window just now.
6196 tp
->t_bytes_acked
= 0;
6198 /* Reset retransmit shift as we know that the reason
6199 * for delay in sending a packet is due to flow
6200 * control on the outgoing interface. There is no need
6201 * to backoff retransmit timer.
6203 TCP_RESET_REXMT_STATE(tp
);
6206 * Start the output stream again. Since we are
6207 * not retransmitting data, do not reset the
6208 * retransmit timer or rtt calculation.
6214 tcp_getstat SYSCTL_HANDLER_ARGS
6216 #pragma unused(oidp, arg1, arg2)
6220 proc_t caller
= PROC_NULL
;
6221 proc_t caller_parent
= PROC_NULL
;
6222 char command_name
[MAXCOMLEN
+ 1] = "";
6223 char parent_name
[MAXCOMLEN
+ 1] = "";
6225 if ((caller
= proc_self()) != PROC_NULL
) {
6226 /* get process name */
6227 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
6229 /* get parent process name if possible */
6230 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
6231 strlcpy(parent_name
, caller_parent
->p_comm
,
6232 sizeof(parent_name
));
6233 proc_rele(caller_parent
);
6236 if ((escape_str(command_name
, strlen(command_name
),
6237 sizeof(command_name
)) == 0) &&
6238 (escape_str(parent_name
, strlen(parent_name
),
6239 sizeof(parent_name
)) == 0)) {
6240 kern_asl_msg(LOG_DEBUG
, "messagetracer",
6242 "com.apple.message.domain",
6243 "com.apple.kernel.tcpstat", /* 1 */
6244 "com.apple.message.signature",
6246 "com.apple.message.signature2", command_name
, /* 3 */
6247 "com.apple.message.signature3", parent_name
, /* 4 */
6248 "com.apple.message.summarize", "YES", /* 5 */
6252 if (caller
!= PROC_NULL
)
6255 if (req
->oldptr
== 0) {
6256 req
->oldlen
= (size_t)sizeof(struct tcpstat
);
6259 error
= SYSCTL_OUT(req
, &tcpstat
, MIN(sizeof (tcpstat
), req
->oldlen
));
6266 * Checksum extended TCP header and data.
6269 tcp_input_checksum(int af
, struct mbuf
*m
, struct tcphdr
*th
, int off
, int tlen
)
6271 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
6275 struct ip
*ip
= mtod(m
, struct ip
*);
6276 struct ipovly
*ipov
= (struct ipovly
*)ip
;
6278 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
6281 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6282 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6283 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6284 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6285 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6287 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
6288 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
6291 * Perform 1's complement adjustment of octets
6292 * that got included/excluded in the hardware-
6293 * calculated checksum value. Ignore cases
6294 * where the value includes or excludes the IP
6295 * header span, as the sum for those octets
6296 * would already be 0xffff and thus no-op.
6298 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6299 start
!= 0 && (off
- start
) != off
) {
6300 #if BYTE_ORDER != BIG_ENDIAN
6306 /* callee folds in sum */
6307 sum
= m_adj_sum16(m
, start
, off
, sum
);
6308 #if BYTE_ORDER != BIG_ENDIAN
6316 /* callee folds in sum */
6317 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
6319 sum
+ htonl(tlen
+ IPPROTO_TCP
));
6321 th
->th_sum
^= 0xffff;
6327 bcopy(ipov
->ih_x1
, b
, sizeof (ipov
->ih_x1
));
6328 bzero(ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
6329 ip_sum
= ipov
->ih_len
;
6330 ipov
->ih_len
= (u_short
)tlen
;
6331 #if BYTE_ORDER != BIG_ENDIAN
6332 HTONS(ipov
->ih_len
);
6334 len
= sizeof (struct ip
) + tlen
;
6335 th
->th_sum
= in_cksum(m
, len
);
6336 bcopy(b
, ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
6337 ipov
->ih_len
= ip_sum
;
6339 tcp_in_cksum_stats(len
);
6345 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
6347 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
6350 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
6351 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
6352 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
6353 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
6354 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
6356 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
6357 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
6360 * Perform 1's complement adjustment of octets
6361 * that got included/excluded in the hardware-
6362 * calculated checksum value.
6364 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
6368 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
6369 s
= ip6
->ip6_src
.s6_addr16
[1];
6370 ip6
->ip6_src
.s6_addr16
[1] = 0 ;
6372 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
6373 d
= ip6
->ip6_dst
.s6_addr16
[1];
6374 ip6
->ip6_dst
.s6_addr16
[1] = 0;
6377 /* callee folds in sum */
6378 sum
= m_adj_sum16(m
, start
, off
, sum
);
6380 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
))
6381 ip6
->ip6_src
.s6_addr16
[1] = s
;
6382 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
))
6383 ip6
->ip6_dst
.s6_addr16
[1] = d
;
6386 th
->th_sum
= in6_pseudo(
6387 &ip6
->ip6_src
, &ip6
->ip6_dst
,
6388 sum
+ htonl(tlen
+ IPPROTO_TCP
));
6390 th
->th_sum
^= 0xffff;
6392 tcp_in6_cksum_stats(tlen
);
6393 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, off
, tlen
);
6403 if (th
->th_sum
!= 0) {
6404 tcpstat
.tcps_rcvbadsum
++;
6405 IF_TCP_STATINC(ifp
, badformat
);
6412 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_STATS
, stats
,
6413 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, tcp_getstat
,
6414 "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
6417 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
6419 #pragma unused(arg1, arg2)
6421 int error
, val
= tcprexmtthresh
;
6423 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
6424 if (error
|| !req
->newptr
)
6428 * Constrain the number of duplicate ACKs
6429 * to consider for TCP fast retransmit
6433 if (val
< 2 || val
> 3)
6436 tcprexmtthresh
= val
;
6441 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, rexmt_thresh
, CTLTYPE_INT
| CTLFLAG_RW
|
6442 CTLFLAG_LOCKED
, &tcprexmtthresh
, 0, &sysctl_rexmtthresh
, "I",
6443 "Duplicate ACK Threshold for Fast Retransmit");