2 * Copyright (c) 2000-2014 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_fsm.h>
111 #include <netinet/tcp_seq.h>
112 #include <netinet/tcp_timer.h>
113 #include <netinet/tcp_var.h>
114 #include <netinet/tcp_cc.h>
115 #include <dev/random/randomdev.h>
116 #include <kern/zalloc.h>
118 #include <netinet6/tcp6_var.h>
120 #include <netinet/tcpip.h>
122 #include <netinet/tcp_debug.h>
123 u_char tcp_saveipgen
[40]; /* the size must be of max ip header, now IPv6 */
124 struct tcphdr tcp_savetcp
;
125 #endif /* TCPDEBUG */
128 #include <netinet6/ipsec.h>
130 #include <netinet6/ipsec6.h>
132 #include <netkey/key.h>
135 #if CONFIG_MACF_NET || CONFIG_MACF_SOCKET
136 #include <security/mac_framework.h>
137 #endif /* CONFIG_MACF_NET || CONFIG_MACF_SOCKET */
139 #include <sys/kdebug.h>
140 #include <netinet/lro_ext.h>
142 #include <netinet/mptcp_var.h>
143 #include <netinet/mptcp.h>
144 #include <netinet/mptcp_opt.h>
147 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0)
148 #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2)
149 #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8))
150 #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8))
154 struct tcpstat tcpstat
;
156 static int log_in_vain
= 0;
157 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
158 &log_in_vain
, 0, "Log all incoming TCP connections");
160 static int blackhole
= 0;
161 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, blackhole
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
162 &blackhole
, 0, "Do not send RST when dropping refused connections");
164 int tcp_delack_enabled
= 3;
165 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, delayed_ack
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
166 &tcp_delack_enabled
, 0,
167 "Delay ACK to try and piggyback it onto a data packet");
169 int tcp_lq_overflow
= 1;
170 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcp_lq_overflow
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
172 "Listen Queue Overflow");
175 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recvbg
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
177 "Receive background");
180 static int drop_synfin
= 1;
181 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, drop_synfin
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
182 &drop_synfin
, 0, "Drop TCP packets with SYN+FIN set");
185 SYSCTL_NODE(_net_inet_tcp
, OID_AUTO
, reass
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0,
186 "TCP Segment Reassembly Queue");
188 static int tcp_reass_overflows
= 0;
189 SYSCTL_INT(_net_inet_tcp_reass
, OID_AUTO
, overflows
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
190 &tcp_reass_overflows
, 0,
191 "Global number of TCP Segment Reassembly Queue Overflows");
194 __private_extern__
int slowlink_wsize
= 8192;
195 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, slowlink_wsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
196 &slowlink_wsize
, 0, "Maximum advertised window size for slowlink");
198 int maxseg_unacked
= 8;
199 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, maxseg_unacked
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
200 &maxseg_unacked
, 0, "Maximum number of outstanding segments left unacked");
202 int tcp_do_rfc3465
= 1;
203 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
204 &tcp_do_rfc3465
, 0, "");
206 int tcp_do_rfc3465_lim2
= 1;
207 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rfc3465_lim2
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
208 &tcp_do_rfc3465_lim2
, 0, "Appropriate bytes counting w/ L=2*SMSS");
210 int rtt_samples_per_slot
= 20;
211 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rtt_samples_per_slot
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
212 &rtt_samples_per_slot
, 0, "Number of RTT samples stored for rtt history");
214 int tcp_allowed_iaj
= ALLOWED_IAJ
;
215 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, recv_allowed_iaj
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
216 &tcp_allowed_iaj
, 0, "Allowed inter-packet arrival jiter");
218 int tcp_acc_iaj_high_thresh
= ACC_IAJ_HIGH_THRESH
;
219 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, acc_iaj_high_thresh
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
220 &tcp_acc_iaj_high_thresh
, 0, "Used in calculating maximum accumulated IAJ");
222 u_int32_t tcp_do_autorcvbuf
= 1;
223 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, doautorcvbuf
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
224 &tcp_do_autorcvbuf
, 0, "Enable automatic socket buffer tuning");
226 u_int32_t tcp_autorcvbuf_inc_shift
= 3;
227 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufincshift
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
228 &tcp_autorcvbuf_inc_shift
, 0, "Shift for increment in receive socket buffer size");
230 u_int32_t tcp_autorcvbuf_max
= 512 * 1024;
231 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, autorcvbufmax
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
232 &tcp_autorcvbuf_max
, 0, "Maximum receive socket buffer size");
235 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
236 &sw_lro
, 0, "Used to coalesce TCP packets");
239 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lrodbg
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
240 &lrodebug
, 0, "Used to debug SW LRO");
243 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, lro_startcnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
244 &lro_start
, 0, "Segments for starting LRO computed as power of 2");
246 extern int tcp_do_autosendbuf
;
248 int limited_txmt
= 1;
249 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, limited_transmit
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
250 &limited_txmt
, 0, "Enable limited transmit");
253 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, early_rexmt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
254 &early_rexmt
, 0, "Enable Early Retransmit");
257 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sack_ackadv
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
258 &sack_ackadv
, 0, "Use SACK with cumulative ack advancement as a dupack");
260 #if CONFIG_IFEF_NOWINDOWSCALE
261 int tcp_obey_ifef_nowindowscale
= 0;
262 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, obey_ifef_nowindowscale
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
263 &tcp_obey_ifef_nowindowscale
, 0, "");
266 extern int tcp_TCPTV_MIN
;
267 extern int tcp_acc_iaj_high
;
268 extern int tcp_acc_iaj_react_limit
;
269 extern struct zone
*tcp_reass_zone
;
271 int tcprexmtthresh
= 3;
274 struct timeval tcp_uptime
; /* uptime when tcp_now was last updated */
275 lck_spin_t
*tcp_uptime_lock
; /* Used to sychronize updates to tcp_now */
277 struct inpcbhead tcb
;
278 #define tcb6 tcb /* for KAME src sync over BSD*'s */
279 struct inpcbinfo tcbinfo
;
281 static void tcp_dooptions(struct tcpcb
*, u_char
*, int, struct tcphdr
*,
282 struct tcpopt
*, unsigned int);
283 static void tcp_pulloutofband(struct socket
*,
284 struct tcphdr
*, struct mbuf
*, int);
285 static int tcp_reass(struct tcpcb
*, struct tcphdr
*, int *, struct mbuf
*,
287 static void tcp_xmit_timer(struct tcpcb
*, int, u_int32_t
, tcp_seq
);
288 static inline unsigned int tcp_maxmtu(struct rtentry
*);
289 static inline int tcp_stretch_ack_enable(struct tcpcb
*tp
);
290 static inline void tcp_adaptive_rwtimo_check(struct tcpcb
*, int);
293 static inline void update_iaj_state(struct tcpcb
*tp
, uint32_t tlen
,
295 void compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
);
296 static void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
);
297 #endif /* TRAFFIC_MGT */
300 static inline unsigned int tcp_maxmtu6(struct rtentry
*);
303 static void tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sb
,
304 struct tcpopt
*to
, u_int32_t tlen
);
306 void tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sb
);
307 static void tcp_sbsnd_trim(struct sockbuf
*sbsnd
);
308 static inline void tcp_sbrcv_tstmp_check(struct tcpcb
*tp
);
309 static inline void tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sb
,
310 u_int32_t newsize
, u_int32_t idealsize
);
311 static void tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
);
312 static void tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
,
314 static void tcp_early_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
);
315 static void tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
,
318 * Constants used for resizing receive socket buffer
319 * when timestamps are not supported
321 #define TCPTV_RCVNOTS_QUANTUM 100
322 #define TCP_RCVNOTS_BYTELEVEL 204800
325 * Constants used for limiting early retransmits
328 #define TCP_EARLY_REXMT_WIN (60 * TCP_RETRANSHZ) /* 60 seconds */
329 #define TCP_EARLY_REXMT_LIMIT 10
331 extern void ipfwsyslog( int level
, const char *format
,...);
332 extern int fw_verbose
;
335 extern void ipfw_stealth_stats_incr_tcp(void);
337 #define log_in_vain_log( a ) { \
338 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
340 } else if ( (log_in_vain == 4 ) && (fw_verbose == 2)) { \
341 ipfw_stealth_stats_incr_tcp(); \
346 #define log_in_vain_log( a ) { log a; }
349 int tcp_rcvunackwin
= TCPTV_UNACKWIN
;
350 int tcp_maxrcvidle
= TCPTV_MAXRCVIDLE
;
351 int tcp_rcvsspktcnt
= TCP_RCV_SS_PKTCOUNT
;
352 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rcvsspktcnt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
353 &tcp_rcvsspktcnt
, 0, "packets to be seen before receiver stretches acks");
355 #define DELAY_ACK(tp, th) \
356 (CC_ALGO(tp)->delay_ack != NULL && CC_ALGO(tp)->delay_ack(tp, th))
358 static int tcp_dropdropablreq(struct socket
*head
);
359 static void tcp_newreno_partial_ack(struct tcpcb
*tp
, struct tcphdr
*th
);
361 static void update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
);
362 void tcp_set_background_cc(struct socket
*so
);
363 void tcp_set_foreground_cc(struct socket
*so
);
364 static void tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
);
365 static void tcp_bwmeas_check(struct tcpcb
*tp
);
369 reset_acc_iaj(struct tcpcb
*tp
)
377 update_iaj_state(struct tcpcb
*tp
, uint32_t size
, int rst_size
)
381 if (tp
->iaj_size
== 0 || size
>= tp
->iaj_size
) {
383 tp
->iaj_rcv_ts
= tcp_now
;
384 tp
->iaj_small_pkt
= 0;
388 /* For every 32 bit unsigned integer(v), this function will find the
389 * largest integer n such that (n*n <= v). This takes at most 16 iterations
390 * irrespective of the value of v and does not involve multiplications.
393 isqrt(unsigned int val
) {
394 unsigned int sqrt_cache
[11] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
395 unsigned int temp
, g
=0, b
=0x8000, bshft
=15;
397 for (g
= 0; g
<= 10; ++g
) {
398 if (sqrt_cache
[g
] > val
) {
401 } else if (sqrt_cache
[g
] == val
) {
407 temp
= (((g
<< 1) + b
) << (bshft
--));
413 } while ( b
> 0 && val
> 0);
419 * With LRO, roughly estimate the inter arrival time between
420 * each sub coalesced packet as an average. Count the delay
421 * cur_iaj to be the delay between the last packet received
422 * and the first packet of the LRO stream. Due to round off errors
423 * cur_iaj may be the same as lro_delay_factor. Averaging has
424 * round off errors too. lro_delay_factor may be close to 0
425 * in steady state leading to lower values fed to compute_iaj_meat.
428 compute_iaj(struct tcpcb
*tp
, int nlropkts
, int lro_delay_factor
)
430 uint32_t cur_iaj
= tcp_now
- tp
->iaj_rcv_ts
;
431 uint32_t timediff
= 0;
433 if (cur_iaj
>= lro_delay_factor
) {
434 cur_iaj
= cur_iaj
- lro_delay_factor
;
437 compute_iaj_meat(tp
, cur_iaj
);
444 timediff
= lro_delay_factor
/nlropkts
;
448 compute_iaj_meat(tp
, timediff
);
454 void compute_iaj_meat(struct tcpcb
*tp
, uint32_t cur_iaj
)
456 /* When accumulated IAJ reaches MAX_ACC_IAJ in milliseconds,
457 * throttle the receive window to a minimum of MIN_IAJ_WIN packets
459 #define MAX_ACC_IAJ (tcp_acc_iaj_high_thresh + tcp_acc_iaj_react_limit)
460 #define IAJ_DIV_SHIFT 4
461 #define IAJ_ROUNDUP_CONST (1 << (IAJ_DIV_SHIFT - 1))
463 uint32_t allowed_iaj
, acc_iaj
= 0;
468 cur_iaj_dev
= (cur_iaj
- tp
->avg_iaj
);
470 /* Allow a jitter of "allowed_iaj" milliseconds. Some connections
471 * may have a constant jitter more than that. We detect this by
472 * using standard deviation.
474 allowed_iaj
= tp
->avg_iaj
+ tp
->std_dev_iaj
;
475 if (allowed_iaj
< tcp_allowed_iaj
)
476 allowed_iaj
= tcp_allowed_iaj
;
478 /* Initially when the connection starts, the senders congestion
479 * window is small. During this period we avoid throttling a
480 * connection because we do not have a good starting point for
481 * allowed_iaj. IAJ_IGNORE_PKTCNT is used to quietly gloss over
482 * the first few packets.
484 if (tp
->iaj_pktcnt
> IAJ_IGNORE_PKTCNT
) {
485 if ( cur_iaj
<= allowed_iaj
) {
486 if (tp
->acc_iaj
>= 2)
487 acc_iaj
= tp
->acc_iaj
- 2;
492 acc_iaj
= tp
->acc_iaj
+ (cur_iaj
- allowed_iaj
);
495 if (acc_iaj
> MAX_ACC_IAJ
)
496 acc_iaj
= MAX_ACC_IAJ
;
497 tp
->acc_iaj
= acc_iaj
;
500 /* Compute weighted average where the history has a weight of
501 * 15 out of 16 and the current value has a weight of 1 out of 16.
502 * This will make the short-term measurements have more weight.
504 * The addition of 8 will help to round-up the value
505 * instead of round-down
507 tp
->avg_iaj
= (((tp
->avg_iaj
<< IAJ_DIV_SHIFT
) - tp
->avg_iaj
)
508 + cur_iaj
+ IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
510 /* Compute Root-mean-square of deviation where mean is a weighted
511 * average as described above.
513 temp
= tp
->std_dev_iaj
* tp
->std_dev_iaj
;
514 mean
= (((temp
<< IAJ_DIV_SHIFT
) - temp
)
515 + (cur_iaj_dev
* cur_iaj_dev
)
516 + IAJ_ROUNDUP_CONST
) >> IAJ_DIV_SHIFT
;
518 tp
->std_dev_iaj
= isqrt(mean
);
520 DTRACE_TCP3(iaj
, struct tcpcb
*, tp
, uint32_t, cur_iaj
,
521 uint32_t, allowed_iaj
);
525 #endif /* TRAFFIC_MGT */
527 /* Check if enough amount of data has been acknowledged since
528 * bw measurement was started
531 tcp_bwmeas_check(struct tcpcb
*tp
)
533 int32_t bw_meas_bytes
;
534 uint32_t bw
, bytes
, elapsed_time
;
535 bw_meas_bytes
= tp
->snd_una
- tp
->t_bwmeas
->bw_start
;
536 if ((tp
->t_flagsext
& TF_BWMEAS_INPROGRESS
) != 0 &&
537 bw_meas_bytes
>= (int32_t)(tp
->t_bwmeas
->bw_size
)) {
538 bytes
= bw_meas_bytes
;
539 elapsed_time
= tcp_now
- tp
->t_bwmeas
->bw_ts
;
540 if (elapsed_time
> 0) {
541 bw
= bytes
/ elapsed_time
;
543 if (tp
->t_bwmeas
->bw_sndbw
> 0) {
544 tp
->t_bwmeas
->bw_sndbw
=
545 (((tp
->t_bwmeas
->bw_sndbw
<< 3) - tp
->t_bwmeas
->bw_sndbw
) + bw
) >> 3;
547 tp
->t_bwmeas
->bw_sndbw
= bw
;
551 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
556 tcp_reass(struct tcpcb
*tp
, struct tcphdr
*th
, int *tlenp
, struct mbuf
*m
,
560 struct tseg_qent
*p
= NULL
;
561 struct tseg_qent
*nq
;
562 struct tseg_qent
*te
= NULL
;
563 struct inpcb
*inp
= tp
->t_inpcb
;
564 struct socket
*so
= inp
->inp_socket
;
567 struct mbuf
*oodata
= NULL
;
570 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
571 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
572 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
575 * Call with th==0 after become established to
576 * force pre-ESTABLISHED data up to user socket.
582 * If the reassembly queue already has entries or if we are going
583 * to add a new one, then the connection has reached a loss state.
584 * Reset the stretch-ack algorithm at this point.
586 tcp_reset_stretch_ack(tp
);
591 #endif /* TRAFFIC_MGT */
594 * Limit the number of segments in the reassembly queue to prevent
595 * holding on to too many segments (and thus running out of mbufs).
596 * Make sure to let the missing segment through which caused this
597 * queue. Always keep one global queue entry spare to be able to
598 * process the missing segment.
600 qlimit
= min(max(100, so
->so_rcv
.sb_hiwat
>> 10),
601 tcp_autorcvbuf_max
>> 10);
602 if (th
->th_seq
!= tp
->rcv_nxt
&&
603 (tp
->t_reassqlen
+ 1) >= qlimit
) {
604 tcp_reass_overflows
++;
605 tcpstat
.tcps_rcvmemdrop
++;
611 /* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
612 te
= (struct tseg_qent
*) zalloc(tcp_reass_zone
);
614 tcpstat
.tcps_rcvmemdrop
++;
621 * Find a segment which begins after this one does.
623 LIST_FOREACH(q
, &tp
->t_segq
, tqe_q
) {
624 if (SEQ_GT(q
->tqe_th
->th_seq
, th
->th_seq
))
630 * If there is a preceding segment, it may provide some of
631 * our data already. If so, drop the data from the incoming
632 * segment. If it provides all of our data, drop us.
636 /* conversion to int (in i) handles seq wraparound */
637 i
= p
->tqe_th
->th_seq
+ p
->tqe_len
- th
->th_seq
;
640 tcpstat
.tcps_rcvduppack
++;
641 tcpstat
.tcps_rcvdupbyte
+= *tlenp
;
643 nstat_route_rx(inp
->inp_route
.ro_rt
,
645 NSTAT_RX_FLAG_DUPLICATE
);
646 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
648 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
650 tp
->t_stat
.rxduplicatebytes
+= *tlenp
;
653 zfree(tcp_reass_zone
, te
);
657 * Try to present any queued data
658 * at the left window edge to the user.
659 * This is needed after the 3-WHS
669 tcpstat
.tcps_rcvoopack
++;
670 tcpstat
.tcps_rcvoobyte
+= *tlenp
;
672 nstat_route_rx(inp
->inp_route
.ro_rt
, 1, *tlenp
,
673 NSTAT_RX_FLAG_OUT_OF_ORDER
);
674 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
675 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, *tlenp
);
676 tp
->t_stat
.rxoutoforderbytes
+= *tlenp
;
680 * While we overlap succeeding segments trim them or,
681 * if they are completely covered, dequeue them.
684 register int i
= (th
->th_seq
+ *tlenp
) - q
->tqe_th
->th_seq
;
687 if (i
< q
->tqe_len
) {
688 q
->tqe_th
->th_seq
+= i
;
694 nq
= LIST_NEXT(q
, tqe_q
);
695 LIST_REMOVE(q
, tqe_q
);
697 zfree(tcp_reass_zone
, q
);
702 /* Insert the new segment queue entry into place. */
705 te
->tqe_len
= *tlenp
;
708 LIST_INSERT_HEAD(&tp
->t_segq
, te
, tqe_q
);
710 LIST_INSERT_AFTER(p
, te
, tqe_q
);
714 * New out-of-order data exists, and is pointed to by
715 * queue entry te. Set copy_oodata to 1 so out-of-order data
716 * can be copied off to sockbuf after in-order data
719 if (!(so
->so_state
& SS_CANTRCVMORE
))
724 * Present data to user, advancing rcv_nxt through
725 * completed sequence space.
727 if (!TCPS_HAVEESTABLISHED(tp
->t_state
))
729 q
= LIST_FIRST(&tp
->t_segq
);
730 if (!q
|| q
->tqe_th
->th_seq
!= tp
->rcv_nxt
) {
731 /* Stop using LRO once out of order packets arrive */
732 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
733 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
734 th
->th_dport
, th
->th_sport
);
735 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
739 * continue processing if out-of-order data
742 if (q
&& (so
->so_flags
& SOF_ENABLE_MSGS
))
743 goto msg_unordered_delivery
;
748 /* lost packet was recovered, so ooo data can be returned */
749 tcpstat
.tcps_recovered_pkts
++;
752 tp
->rcv_nxt
+= q
->tqe_len
;
753 flags
= q
->tqe_th
->th_flags
& TH_FIN
;
754 nq
= LIST_NEXT(q
, tqe_q
);
755 LIST_REMOVE(q
, tqe_q
);
756 if (so
->so_state
& SS_CANTRCVMORE
) {
759 so_recv_data_stat(so
, q
->tqe_m
, 0); /* XXXX */
760 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
762 * Append the inorder data as a message to the
763 * receive socket buffer. Also check to see if
764 * the data we are about to deliver is the same
765 * data that we wanted to pass up to the user
766 * out of order. If so, reset copy_oodata --
767 * the received data filled a gap, and
773 if (sbappendstream_rcvdemux(so
, q
->tqe_m
,
774 q
->tqe_th
->th_seq
- (tp
->irs
+ 1), 0))
776 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
777 tcp_update_lro_seq(tp
->rcv_nxt
,
778 inp
->inp_laddr
, inp
->inp_faddr
,
779 th
->th_dport
, th
->th_sport
);
782 zfree(tcp_reass_zone
, q
);
785 } while (q
&& q
->tqe_th
->th_seq
== tp
->rcv_nxt
);
788 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
790 KERNEL_DEBUG(DBG_LAYER_BEG
,
791 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
792 (((inp
->in6p_laddr
.s6_addr16
[0] & 0xffff) << 16) |
793 (inp
->in6p_faddr
.s6_addr16
[0] & 0xffff)),
799 KERNEL_DEBUG(DBG_LAYER_BEG
,
800 ((inp
->inp_fport
<< 16) | inp
->inp_lport
),
801 (((inp
->inp_laddr
.s_addr
& 0xffff) << 16) |
802 (inp
->inp_faddr
.s_addr
& 0xffff)),
806 msg_unordered_delivery
:
807 /* Deliver out-of-order data as a message */
808 if (te
&& (so
->so_flags
& SOF_ENABLE_MSGS
) && copy_oodata
&& te
->tqe_len
) {
810 * make a copy of the mbuf to be delivered up to
811 * the user, and add it to the sockbuf
813 oodata
= m_copym(te
->tqe_m
, 0, M_COPYALL
, M_DONTWAIT
);
814 if (oodata
!= NULL
) {
815 if (sbappendmsgstream_rcv(&so
->so_rcv
, oodata
,
816 te
->tqe_th
->th_seq
- (tp
->irs
+ 1), 1)) {
818 tcpstat
.tcps_msg_unopkts
++;
820 tcpstat
.tcps_msg_unoappendfail
++;
826 sorwakeup(so
); /* done with socket lock held */
831 * Reduce congestion window.
834 tcp_reduce_congestion_window(
838 * If the current tcp cc module has
839 * defined a hook for tasks to run
840 * before entering FR, call it
842 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
843 CC_ALGO(tp
)->pre_fr(tp
);
844 ENTER_FASTRECOVERY(tp
);
845 tp
->snd_recover
= tp
->snd_max
;
846 tp
->t_timer
[TCPT_REXMT
] = 0;
847 tp
->t_timer
[TCPT_PTO
] = 0;
849 tp
->snd_cwnd
= tp
->snd_ssthresh
+
850 tp
->t_maxseg
* tcprexmtthresh
;
854 * The application wants to get an event if there
855 * is a stall during read. Set the initial keepalive
856 * timeout to be equal to twice RTO.
859 tcp_adaptive_rwtimo_check(struct tcpcb
*tp
, int tlen
)
861 if (tp
->t_adaptive_rtimo
> 0 && tlen
> 0 &&
862 tp
->t_state
== TCPS_ESTABLISHED
) {
863 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
864 (TCP_REXMTVAL(tp
) << 1));
865 tp
->t_flagsext
|= TF_DETECT_READSTALL
;
866 tp
->t_rtimo_probes
= 0;
871 tcp_keepalive_reset(struct tcpcb
*tp
)
873 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
874 TCP_CONN_KEEPIDLE(tp
));
875 tp
->t_flagsext
&= ~(TF_DETECT_READSTALL
);
876 tp
->t_rtimo_probes
= 0;
880 * TCP input routine, follows pages 65-76 of the
881 * protocol specification dated September, 1981 very closely.
885 tcp6_input(struct mbuf
**mp
, int *offp
, int proto
)
887 #pragma unused(proto)
888 register struct mbuf
*m
= *mp
;
890 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
892 IP6_EXTHDR_CHECK(m
, *offp
, sizeof(struct tcphdr
), return IPPROTO_DONE
);
894 /* Expect 32-bit aligned data pointer on strict-align platforms */
895 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
898 * draft-itojun-ipv6-tcp-to-anycast
899 * better place to put this in?
901 if (ip6_getdstifaddr_info(m
, NULL
, &ia6_flags
) == 0) {
902 if (ia6_flags
& IN6_IFF_ANYCAST
) {
905 ip6
= mtod(m
, struct ip6_hdr
*);
906 icmp6_error(m
, ICMP6_DST_UNREACH
,
907 ICMP6_DST_UNREACH_ADDR
,
908 (caddr_t
)&ip6
->ip6_dst
- (caddr_t
)ip6
);
910 IF_TCP_STATINC(ifp
, icmp6unreach
);
912 return (IPPROTO_DONE
);
917 return (IPPROTO_DONE
);
921 /* Depending on the usage of mbuf space in the system, this function
922 * will return true or false. This is used to determine if a socket
923 * buffer can take more memory from the system for auto-tuning or not.
926 tcp_cansbgrow(struct sockbuf
*sb
)
928 /* Calculate the host level space limit in terms of MSIZE buffers.
929 * We can use a maximum of half of the available mbuf space for
932 u_int32_t mblim
= ((nmbclusters
>> 1) << (MCLSHIFT
- MSIZESHIFT
));
934 /* Calculate per sb limit in terms of bytes. We optimize this limit
935 * for upto 16 socket buffers.
938 u_int32_t sbspacelim
= ((nmbclusters
>> 4) << MCLSHIFT
);
940 if ((total_sbmb_cnt
< mblim
) &&
941 (sb
->sb_hiwat
< sbspacelim
)) {
944 OSIncrementAtomic64(&sbmb_limreached
);
950 tcp_sbrcv_reserve(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
951 u_int32_t newsize
, u_int32_t idealsize
)
954 /* newsize should not exceed max */
955 newsize
= min(newsize
, tcp_autorcvbuf_max
);
957 /* The receive window scale negotiated at the
958 * beginning of the connection will also set a
959 * limit on the socket buffer size
961 newsize
= min(newsize
, TCP_MAXWIN
<< tp
->rcv_scale
);
963 /* Set new socket buffer size */
964 if (newsize
> sbrcv
->sb_hiwat
&&
965 (sbreserve(sbrcv
, newsize
) == 1)) {
966 sbrcv
->sb_idealsize
= min(max(sbrcv
->sb_idealsize
,
967 (idealsize
!= 0) ? idealsize
: newsize
),
970 /* Again check the limit set by the advertised
973 sbrcv
->sb_idealsize
= min(sbrcv
->sb_idealsize
,
974 TCP_MAXWIN
<< tp
->rcv_scale
);
979 * This function is used to grow a receive socket buffer. It
980 * will take into account system-level memory usage and the
981 * bandwidth available on the link to make a decision.
984 tcp_sbrcv_grow(struct tcpcb
*tp
, struct sockbuf
*sbrcv
,
985 struct tcpopt
*to
, u_int32_t pktlen
) {
988 * Do not grow the receive socket buffer if
989 * - auto resizing is disabled, globally or on this socket
990 * - the high water mark already reached the maximum
991 * - the stream is in background and receive side is being
993 * - if there are segments in reassembly queue indicating loss,
994 * do not need to increase recv window during recovery as more
995 * data is not going to be sent. A duplicate ack sent during
996 * recovery should not change the receive window
998 if (tcp_do_autorcvbuf
== 0 ||
999 (sbrcv
->sb_flags
& SB_AUTOSIZE
) == 0 ||
1000 tcp_cansbgrow(sbrcv
) == 0 ||
1001 sbrcv
->sb_hiwat
>= tcp_autorcvbuf_max
||
1002 (tp
->t_flagsext
& TF_RECV_THROTTLE
) ||
1003 !LIST_EMPTY(&tp
->t_segq
)) {
1004 /* Can not resize the socket buffer, just return */
1008 if (TSTMP_GT(tcp_now
,
1009 tp
->rfbuf_ts
+ TCPTV_RCVBUFIDLE
)) {
1010 /* If there has been an idle period in the
1011 * connection, just restart the measurement
1016 if (!TSTMP_SUPPORTED(tp
)) {
1018 * Timestamp option is not supported on this connection.
1019 * If the connection reached a state to indicate that
1020 * the receive socket buffer needs to grow, increase
1021 * the high water mark.
1023 if (TSTMP_GEQ(tcp_now
,
1024 tp
->rfbuf_ts
+ TCPTV_RCVNOTS_QUANTUM
)) {
1025 if (tp
->rfbuf_cnt
>= TCP_RCVNOTS_BYTELEVEL
) {
1026 tcp_sbrcv_reserve(tp
, sbrcv
,
1027 tcp_autorcvbuf_max
, 0);
1031 tp
->rfbuf_cnt
+= pktlen
;
1034 } else if (to
->to_tsecr
!= 0) {
1036 * If the timestamp shows that one RTT has
1037 * completed, we can stop counting the
1038 * bytes. Here we consider increasing
1039 * the socket buffer if the bandwidth measured in
1040 * last rtt, is more than half of sb_hiwat, this will
1041 * help to scale the buffer according to the bandwidth
1044 if (TSTMP_GEQ(to
->to_tsecr
, tp
->rfbuf_ts
)) {
1045 if (tp
->rfbuf_cnt
> (sbrcv
->sb_hiwat
-
1046 (sbrcv
->sb_hiwat
>> 1))) {
1047 int32_t rcvbuf_inc
, min_incr
;
1049 * Increment the receive window by a
1050 * multiple of maximum sized segments.
1051 * This will prevent a connection from
1052 * sending smaller segments on wire if it
1053 * is limited by the receive window.
1055 * Set the ideal size based on current
1056 * bandwidth measurements. We set the
1057 * ideal size on receive socket buffer to
1058 * be twice the bandwidth delay product.
1060 rcvbuf_inc
= (tp
->rfbuf_cnt
<< 1)
1064 * Make the increment equal to 8 segments
1067 min_incr
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1068 if (rcvbuf_inc
< min_incr
)
1069 rcvbuf_inc
= min_incr
;
1072 (rcvbuf_inc
/ tp
->t_maxseg
) * tp
->t_maxseg
;
1073 tcp_sbrcv_reserve(tp
, sbrcv
,
1074 sbrcv
->sb_hiwat
+ rcvbuf_inc
,
1075 (tp
->rfbuf_cnt
* 2));
1079 tp
->rfbuf_cnt
+= pktlen
;
1084 /* Restart the measurement */
1090 /* This function will trim the excess space added to the socket buffer
1091 * to help a slow-reading app. The ideal-size of a socket buffer depends
1092 * on the link bandwidth or it is set by an application and we aim to
1096 tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sbrcv
) {
1097 if (tcp_do_autorcvbuf
== 1 && sbrcv
->sb_idealsize
> 0 &&
1098 sbrcv
->sb_hiwat
> sbrcv
->sb_idealsize
) {
1100 /* compute the difference between ideal and current sizes */
1101 u_int32_t diff
= sbrcv
->sb_hiwat
- sbrcv
->sb_idealsize
;
1103 /* Compute the maximum advertised window for
1106 u_int32_t advwin
= tp
->rcv_adv
- tp
->rcv_nxt
;
1108 /* How much can we trim the receive socket buffer?
1109 * 1. it can not be trimmed beyond the max rcv win advertised
1110 * 2. if possible, leave 1/16 of bandwidth*delay to
1111 * avoid closing the win completely
1113 u_int32_t leave
= max(advwin
, (sbrcv
->sb_idealsize
>> 4));
1115 /* Sometimes leave can be zero, in that case leave at least
1116 * a few segments worth of space.
1119 leave
= tp
->t_maxseg
<< tcp_autorcvbuf_inc_shift
;
1121 trim
= sbrcv
->sb_hiwat
- (sbrcv
->sb_cc
+ leave
);
1122 trim
= imin(trim
, (int32_t)diff
);
1125 sbreserve(sbrcv
, (sbrcv
->sb_hiwat
- trim
));
1129 /* We may need to trim the send socket buffer size for two reasons:
1130 * 1. if the rtt seen on the connection is climbing up, we do not
1131 * want to fill the buffers any more.
1132 * 2. if the congestion win on the socket backed off, there is no need
1133 * to hold more mbufs for that connection than what the cwnd will allow.
1136 tcp_sbsnd_trim(struct sockbuf
*sbsnd
) {
1137 if (tcp_do_autosendbuf
== 1 &&
1138 ((sbsnd
->sb_flags
& (SB_AUTOSIZE
| SB_TRIM
)) ==
1139 (SB_AUTOSIZE
| SB_TRIM
)) &&
1140 (sbsnd
->sb_idealsize
> 0) &&
1141 (sbsnd
->sb_hiwat
> sbsnd
->sb_idealsize
)) {
1143 if (sbsnd
->sb_cc
<= sbsnd
->sb_idealsize
) {
1144 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_idealsize
;
1146 trim
= sbsnd
->sb_hiwat
- sbsnd
->sb_cc
;
1148 sbreserve(sbsnd
, (sbsnd
->sb_hiwat
- trim
));
1150 if (sbsnd
->sb_hiwat
<= sbsnd
->sb_idealsize
)
1151 sbsnd
->sb_flags
&= ~(SB_TRIM
);
1155 * If timestamp option was not negotiated on this connection
1156 * and this connection is on the receiving side of a stream
1157 * then we can not measure the delay on the link accurately.
1158 * Instead of enabling automatic receive socket buffer
1159 * resizing, just give more space to the receive socket buffer.
1162 tcp_sbrcv_tstmp_check(struct tcpcb
*tp
) {
1163 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1164 u_int32_t newsize
= 2 * tcp_recvspace
;
1165 struct sockbuf
*sbrcv
= &so
->so_rcv
;
1167 if ((tp
->t_flags
& (TF_REQ_TSTMP
| TF_RCVD_TSTMP
)) !=
1168 (TF_REQ_TSTMP
| TF_RCVD_TSTMP
) &&
1169 (sbrcv
->sb_flags
& SB_AUTOSIZE
) != 0) {
1170 tcp_sbrcv_reserve(tp
, sbrcv
, newsize
, 0);
1174 /* A receiver will evaluate the flow of packets on a connection
1175 * to see if it can reduce ack traffic. The receiver will start
1176 * stretching acks if all of the following conditions are met:
1177 * 1. tcp_delack_enabled is set to 3
1178 * 2. If the bytes received in the last 100ms is greater than a threshold
1179 * defined by maxseg_unacked
1180 * 3. If the connection has not been idle for tcp_maxrcvidle period.
1181 * 4. If the connection has seen enough packets to let the slow-start
1182 * finish after connection establishment or after some packet loss.
1184 * The receiver will stop stretching acks if there is congestion/reordering
1185 * as indicated by packets on reassembly queue or an ECN. If the delayed-ack
1186 * timer fires while stretching acks, it means that the packet flow has gone
1187 * below the threshold defined by maxseg_unacked and the receiver will stop
1188 * stretching acks. The receiver gets no indication when slow-start is completed
1189 * or when the connection reaches an idle state. That is why we use
1190 * tcp_rcvsspktcnt to cover slow-start and tcp_maxrcvidle to identify idle
1194 tcp_stretch_ack_enable(struct tcpcb
*tp
)
1196 if (!(tp
->t_flagsext
& (TF_NOSTRETCHACK
|TF_DISABLE_STRETCHACK
)) &&
1197 tp
->rcv_by_unackwin
>= (maxseg_unacked
* tp
->t_maxseg
) &&
1198 TSTMP_GT(tp
->rcv_unackwin
+ tcp_maxrcvidle
, tcp_now
) &&
1199 (!(tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) ||
1200 (tp
->rcv_waitforss
>= tcp_rcvsspktcnt
))) {
1208 * Reset the state related to stretch-ack algorithm. This will make
1209 * the receiver generate an ack every other packet. The receiver
1210 * will start re-evaluating the rate at which packets come to decide
1211 * if it can benefit by lowering the ack traffic.
1214 tcp_reset_stretch_ack(struct tcpcb
*tp
)
1216 tp
->t_flags
&= ~(TF_STRETCHACK
);
1217 tp
->rcv_by_unackwin
= 0;
1218 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
1221 * When there is packet loss or packet re-ordering or CWR due to
1222 * ECN, the sender's congestion window is reduced. In these states,
1223 * generate an ack for every other packet for some time to allow
1224 * the sender's congestion window to grow.
1226 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
1227 tp
->rcv_waitforss
= 0;
1231 * The last packet was a retransmission, check if this ack
1232 * indicates that the retransmission was spurious.
1234 * If the connection supports timestamps, we could use it to
1235 * detect if the last retransmit was not needed. Otherwise,
1236 * we check if the ACK arrived within RTT/2 window, then it
1237 * was a mistake to do the retransmit in the first place.
1239 * This function will return 1 if it is a spurious retransmit,
1243 tcp_detect_bad_rexmt(struct tcpcb
*tp
, struct tcphdr
*th
,
1244 struct tcpopt
*to
, u_int32_t rxtime
)
1246 int32_t tdiff
, bad_rexmt_win
;
1247 bad_rexmt_win
= (tp
->t_srtt
>> (TCP_RTT_SHIFT
+ 1));
1249 /* If the ack has ECN CE bit, then cwnd has to be adjusted */
1250 if ((tp
->ecn_flags
& (TE_ECN_ON
)) == (TE_ECN_ON
)
1251 && (th
->th_flags
& TH_ECE
))
1253 if (TSTMP_SUPPORTED(tp
)) {
1254 if (rxtime
> 0 && (to
->to_flags
& TOF_TS
)
1255 && to
->to_tsecr
!= 0
1256 && TSTMP_LT(to
->to_tsecr
, rxtime
))
1259 if ((tp
->t_rxtshift
== 1
1260 || (tp
->t_flagsext
& TF_SENT_TLPROBE
))
1262 tdiff
= (int32_t)(tcp_now
- rxtime
);
1263 if (tdiff
< bad_rexmt_win
)
1272 * Restore congestion window state if a spurious timeout
1276 tcp_bad_rexmt_restore_state(struct tcpcb
*tp
, struct tcphdr
*th
)
1278 if (TSTMP_SUPPORTED(tp
)) {
1279 u_int32_t fsize
, acked
;
1280 fsize
= tp
->snd_max
- th
->th_ack
;
1281 acked
= BYTES_ACKED(th
, tp
);
1284 * Implement bad retransmit recovery as
1285 * described in RFC 4015.
1287 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1289 /* Initialize cwnd to the initial window */
1290 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
1291 CC_ALGO(tp
)->cwnd_init(tp
);
1293 tp
->snd_cwnd
= fsize
+ min(acked
, tp
->snd_cwnd
);
1296 tp
->snd_cwnd
= tp
->snd_cwnd_prev
;
1297 tp
->snd_ssthresh
= tp
->snd_ssthresh_prev
;
1298 if (tp
->t_flags
& TF_WASFRECOVERY
)
1299 ENTER_FASTRECOVERY(tp
);
1301 tp
->snd_cwnd
= max(tp
->snd_cwnd
, TCP_CC_CWND_INIT_BYTES
);
1302 tp
->snd_recover
= tp
->snd_recover_prev
;
1303 tp
->snd_nxt
= tp
->snd_max
;
1307 /* Fix send socket buffer to reflect the change in cwnd */
1308 tcp_bad_rexmt_fix_sndbuf(tp
);
1311 * This RTT might reflect the extra delay induced
1312 * by the network. Skip using this sample for RTO
1313 * calculation and mark the connection so we can
1314 * recompute RTT when the next eligible sample is
1317 tp
->t_flagsext
|= TF_RECOMPUTE_RTT
;
1318 tp
->t_badrexmt_time
= tcp_now
;
1323 * If the previous packet was sent in retransmission timer, and it was
1324 * not needed, then restore the congestion window to the state before that
1327 * If the last packet was sent in tail loss probe timeout, check if that
1328 * recovered the last packet. If so, that will indicate a real loss and
1329 * the congestion window needs to be lowered.
1332 tcp_bad_rexmt_check(struct tcpcb
*tp
, struct tcphdr
*th
, struct tcpopt
*to
)
1334 if (tp
->t_rxtshift
> 0 &&
1335 tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_rxtstart
)) {
1336 ++tcpstat
.tcps_sndrexmitbad
;
1337 tcp_bad_rexmt_restore_state(tp
, th
);
1338 tcp_ccdbg_trace(tp
, th
, TCP_CC_BAD_REXMT_RECOVERY
);
1339 } else if ((tp
->t_flagsext
& TF_SENT_TLPROBE
)
1340 && tp
->t_tlphighrxt
> 0
1341 && SEQ_GEQ(th
->th_ack
, tp
->t_tlphighrxt
)
1342 && !tcp_detect_bad_rexmt(tp
, th
, to
, tp
->t_tlpstart
)) {
1344 * The tail loss probe recovered the last packet and
1345 * we need to adjust the congestion window to take
1346 * this loss into account.
1348 ++tcpstat
.tcps_tlp_recoverlastpkt
;
1349 if (!IN_FASTRECOVERY(tp
)) {
1350 tcp_reduce_congestion_window(tp
);
1351 EXIT_FASTRECOVERY(tp
);
1353 tcp_ccdbg_trace(tp
, th
, TCP_CC_TLP_RECOVER_LASTPACKET
);
1356 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1357 tp
->t_tlphighrxt
= 0;
1361 * check if the latest ack was for a segment sent during PMTU
1362 * blackhole detection. If the timestamp on the ack is before
1363 * PMTU blackhole detection, then revert the size of the max
1364 * segment to previous size.
1366 if (tp
->t_rxtshift
> 0 && (tp
->t_flags
& TF_BLACKHOLE
) &&
1367 tp
->t_pmtud_start_ts
> 0 && TSTMP_SUPPORTED(tp
)) {
1368 if ((to
->to_flags
& TOF_TS
) && to
->to_tsecr
!= 0
1369 && TSTMP_LT(to
->to_tsecr
, tp
->t_pmtud_start_ts
)) {
1370 tcp_pmtud_revert_segment_size(tp
);
1373 if (tp
->t_pmtud_start_ts
> 0)
1374 tp
->t_pmtud_start_ts
= 0;
1378 * Check if early retransmit can be attempted according to RFC 5827.
1380 * If packet reordering is detected on a connection, fast recovery will
1381 * be delayed until it is clear that the packet was lost and not reordered.
1382 * But reordering detection is done only when SACK is enabled.
1384 * On connections that do not support SACK, there is a limit on the number
1385 * of early retransmits that can be done per minute. This limit is needed
1386 * to make sure that too many packets are not retransmitted when there is
1387 * packet reordering.
1390 tcp_early_rexmt_check (struct tcpcb
*tp
, struct tcphdr
*th
)
1392 u_int32_t obytes
, snd_off
;
1394 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1396 if (early_rexmt
&& (SACK_ENABLED(tp
) ||
1397 tp
->t_early_rexmt_count
< TCP_EARLY_REXMT_LIMIT
) &&
1398 SEQ_GT(tp
->snd_max
, tp
->snd_una
) &&
1399 (tp
->t_dupacks
== 1 ||
1400 (SACK_ENABLED(tp
) &&
1401 !TAILQ_EMPTY(&tp
->snd_holes
)))) {
1403 * If there are only a few outstanding
1404 * segments on the connection, we might need
1405 * to lower the retransmit threshold. This
1406 * will allow us to do Early Retransmit as
1407 * described in RFC 5827.
1409 if (SACK_ENABLED(tp
) &&
1410 !TAILQ_EMPTY(&tp
->snd_holes
)) {
1411 obytes
= (tp
->snd_max
- tp
->snd_fack
) +
1412 tp
->sackhint
.sack_bytes_rexmit
;
1414 obytes
= (tp
->snd_max
- tp
->snd_una
);
1418 * In order to lower retransmit threshold the
1419 * following two conditions must be met.
1420 * 1. the amount of outstanding data is less
1422 * 2. there is no unsent data ready for
1423 * transmission or the advertised window
1424 * will limit sending new segments.
1426 snd_off
= tp
->snd_max
- tp
->snd_una
;
1427 snd_len
= min(so
->so_snd
.sb_cc
, tp
->snd_wnd
) - snd_off
;
1428 if (obytes
< (tp
->t_maxseg
<< 2) &&
1432 osegs
= obytes
/ tp
->t_maxseg
;
1433 if ((osegs
* tp
->t_maxseg
) < obytes
)
1437 * Since the connection might have already
1438 * received some dupacks, we add them to
1439 * to the outstanding segments count to get
1440 * the correct retransmit threshold.
1442 * By checking for early retransmit after
1443 * receiving some duplicate acks when SACK
1444 * is supported, the connection will
1445 * enter fast recovery even if multiple
1446 * segments are lost in the same window.
1448 osegs
+= tp
->t_dupacks
;
1451 ((osegs
- 1) > 1) ? (osegs
- 1) : 1;
1453 min(tp
->t_rexmtthresh
, tcprexmtthresh
);
1455 max(tp
->t_rexmtthresh
, tp
->t_dupacks
);
1457 if (tp
->t_early_rexmt_count
== 0)
1458 tp
->t_early_rexmt_win
= tcp_now
;
1460 if (tp
->t_flagsext
& TF_SENT_TLPROBE
) {
1461 tcpstat
.tcps_tlp_recovery
++;
1462 tcp_ccdbg_trace(tp
, th
,
1463 TCP_CC_TLP_RECOVERY
);
1465 tcpstat
.tcps_early_rexmt
++;
1466 tp
->t_early_rexmt_count
++;
1467 tcp_ccdbg_trace(tp
, th
,
1468 TCP_CC_EARLY_RETRANSMIT
);
1475 * If we ever sent a TLP probe, the acknowledgement will trigger
1476 * early retransmit because the value of snd_fack will be close
1477 * to snd_max. This will take care of adjustments to the
1478 * congestion window. So we can reset TF_SENT_PROBE flag.
1480 tp
->t_flagsext
&= ~(TF_SENT_TLPROBE
);
1481 tp
->t_tlphighrxt
= 0;
1490 register struct tcphdr
*th
;
1491 register struct ip
*ip
= NULL
;
1492 register struct inpcb
*inp
;
1493 u_char
*optp
= NULL
;
1497 register struct tcpcb
*tp
= 0;
1498 register int thflags
;
1499 struct socket
*so
= 0;
1500 int todrop
, acked
, ourfinisacked
, needoutput
= 0;
1501 struct in_addr laddr
;
1503 struct in6_addr laddr6
;
1506 int iss
= 0, nosock
= 0;
1507 u_int32_t tiwin
, sack_bytes_acked
= 0;
1508 struct tcpopt to
; /* options in this segment */
1509 struct sockaddr_in
*next_hop
= NULL
;
1513 struct m_tag
*fwd_tag
;
1514 u_char ip_ecn
= IPTOS_ECN_NOTECT
;
1515 unsigned int ifscope
;
1516 uint8_t isconnected
, isdisconnected
;
1517 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1518 int pktf_sw_lro_pkt
= (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) ? 1 : 0;
1519 int nlropkts
= (pktf_sw_lro_pkt
== 1) ? m
->m_pkthdr
.lro_npkts
: 1;
1520 int turnoff_lro
= 0, win
;
1522 struct mptcb
*mp_tp
= NULL
;
1523 uint16_t mptcp_csum
= 0;
1525 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
1526 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
1527 boolean_t wired
= (!wifi
&& IFNET_IS_WIRED(ifp
));
1528 struct tcp_respond_args tra
;
1530 #define TCP_INC_VAR(stat, npkts) do { \
1534 TCP_INC_VAR(tcpstat
.tcps_rcvtotal
, nlropkts
);
1536 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
1537 if (!SLIST_EMPTY(&m
->m_pkthdr
.tags
)) {
1538 fwd_tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
,
1539 KERNEL_TAG_TYPE_IPFORWARD
, NULL
);
1543 if (fwd_tag
!= NULL
) {
1544 struct ip_fwd_tag
*ipfwd_tag
=
1545 (struct ip_fwd_tag
*)(fwd_tag
+1);
1547 next_hop
= ipfwd_tag
->next_hop
;
1548 m_tag_delete(m
, fwd_tag
);
1552 struct ip6_hdr
*ip6
= NULL
;
1555 int rstreason
; /* For badport_bandlim accounting purposes */
1556 struct proc
*proc0
=current_proc();
1558 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_START
,0,0,0,0,0);
1561 isipv6
= (mtod(m
, struct ip
*)->ip_v
== 6) ? 1 : 0;
1563 bzero((char *)&to
, sizeof(to
));
1568 * Expect 32-bit aligned data pointer on
1569 * strict-align platforms
1571 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1573 /* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
1574 ip6
= mtod(m
, struct ip6_hdr
*);
1575 tlen
= sizeof(*ip6
) + ntohs(ip6
->ip6_plen
) - off0
;
1576 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
1578 if (tcp_input_checksum(AF_INET6
, m
, th
, off0
, tlen
))
1581 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1582 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
1583 th
->th_seq
, th
->th_ack
, th
->th_win
);
1585 * Be proactive about unspecified IPv6 address in source.
1586 * As we use all-zero to indicate unbounded/unconnected pcb,
1587 * unspecified IPv6 address can be used to confuse us.
1589 * Note that packets with unspecified IPv6 destination is
1590 * already dropped in ip6_input.
1592 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
)) {
1594 IF_TCP_STATINC(ifp
, unspecv6
);
1597 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1598 struct ip6_hdr
*, ip6
, struct tcpcb
*, NULL
,
1599 struct tcphdr
*, th
);
1601 ip_ecn
= (ntohl(ip6
->ip6_flow
) >> 20) & IPTOS_ECN_MASK
;
1606 * Get IP and TCP header together in first mbuf.
1607 * Note: IP leaves IP header in first mbuf.
1609 if (off0
> sizeof (struct ip
)) {
1610 ip_stripoptions(m
, (struct mbuf
*)0);
1611 off0
= sizeof(struct ip
);
1613 if (m
->m_len
< sizeof (struct tcpiphdr
)) {
1614 if ((m
= m_pullup(m
, sizeof (struct tcpiphdr
))) == 0) {
1615 tcpstat
.tcps_rcvshort
++;
1620 /* Expect 32-bit aligned data pointer on strict-align platforms */
1621 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
1623 ip
= mtod(m
, struct ip
*);
1624 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
1627 if (tcp_input_checksum(AF_INET
, m
, th
, off0
, tlen
))
1631 /* Re-initialization for later version check */
1632 ip
->ip_v
= IPVERSION
;
1634 ip_ecn
= (ip
->ip_tos
& IPTOS_ECN_MASK
);
1636 DTRACE_TCP5(receive
, struct mbuf
*, m
, struct inpcb
*, NULL
,
1637 struct ip
*, ip
, struct tcpcb
*, NULL
, struct tcphdr
*, th
);
1639 KERNEL_DEBUG(DBG_LAYER_BEG
, ((th
->th_dport
<< 16) | th
->th_sport
),
1640 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
1641 th
->th_seq
, th
->th_ack
, th
->th_win
);
1646 * Check that TCP offset makes sense,
1647 * pull out TCP options and adjust length. XXX
1649 off
= th
->th_off
<< 2;
1650 if (off
< sizeof (struct tcphdr
) || off
> tlen
) {
1651 tcpstat
.tcps_rcvbadoff
++;
1652 IF_TCP_STATINC(ifp
, badformat
);
1655 tlen
-= off
; /* tlen is used instead of ti->ti_len */
1656 if (off
> sizeof (struct tcphdr
)) {
1659 IP6_EXTHDR_CHECK(m
, off0
, off
, return);
1660 ip6
= mtod(m
, struct ip6_hdr
*);
1661 th
= (struct tcphdr
*)(void *)((caddr_t
)ip6
+ off0
);
1665 if (m
->m_len
< sizeof(struct ip
) + off
) {
1666 if ((m
= m_pullup(m
, sizeof (struct ip
) + off
)) == 0) {
1667 tcpstat
.tcps_rcvshort
++;
1670 ip
= mtod(m
, struct ip
*);
1671 th
= (struct tcphdr
*)(void *)((caddr_t
)ip
+ off0
);
1674 optlen
= off
- sizeof (struct tcphdr
);
1675 optp
= (u_char
*)(th
+ 1);
1677 * Do quick retrieval of timestamp options ("options
1678 * prediction?"). If timestamp is the only option and it's
1679 * formatted as recommended in RFC 1323 appendix A, we
1680 * quickly get the values now and not bother calling
1681 * tcp_dooptions(), etc.
1683 if ((optlen
== TCPOLEN_TSTAMP_APPA
||
1684 (optlen
> TCPOLEN_TSTAMP_APPA
&&
1685 optp
[TCPOLEN_TSTAMP_APPA
] == TCPOPT_EOL
)) &&
1686 *(u_int32_t
*)(void *)optp
== htonl(TCPOPT_TSTAMP_HDR
) &&
1687 (th
->th_flags
& TH_SYN
) == 0) {
1688 to
.to_flags
|= TOF_TS
;
1689 to
.to_tsval
= ntohl(*(u_int32_t
*)(void *)(optp
+ 4));
1690 to
.to_tsecr
= ntohl(*(u_int32_t
*)(void *)(optp
+ 8));
1691 optp
= NULL
; /* we've parsed the options */
1694 thflags
= th
->th_flags
;
1698 * If the drop_synfin option is enabled, drop all packets with
1699 * both the SYN and FIN bits set. This prevents e.g. nmap from
1700 * identifying the TCP/IP stack.
1702 * This is a violation of the TCP specification.
1704 if (drop_synfin
&& (thflags
& (TH_SYN
|TH_FIN
)) == (TH_SYN
|TH_FIN
)) {
1705 IF_TCP_STATINC(ifp
, synfin
);
1711 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
1712 * until after ip6_savecontrol() is called and before other functions
1713 * which don't want those proto headers.
1714 * Because ip6_savecontrol() is going to parse the mbuf to
1715 * search for data to be passed up to user-land, it wants mbuf
1716 * parameters to be unchanged.
1718 drop_hdrlen
= off0
+ off
;
1720 /* Since this is an entry point for input processing of tcp packets, we
1721 * can update the tcp clock here.
1723 calculate_tcp_clock();
1726 * Record the interface where this segment arrived on; this does not
1727 * affect normal data output (for non-detached TCP) as it provides a
1728 * hint about which route and interface to use for sending in the
1729 * absence of a PCB, when scoped routing (and thus source interface
1730 * selection) are enabled.
1732 if ((m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
) || m
->m_pkthdr
.rcvif
== NULL
)
1733 ifscope
= IFSCOPE_NONE
;
1735 ifscope
= m
->m_pkthdr
.rcvif
->if_index
;
1738 * Convert TCP protocol specific fields to host format.
1741 #if BYTE_ORDER != BIG_ENDIAN
1749 * Locate pcb for segment.
1753 isconnected
= FALSE
;
1754 isdisconnected
= FALSE
;
1756 #if IPFIREWALL_FORWARD
1757 if (next_hop
!= NULL
1759 && isipv6
== 0 /* IPv6 support is not yet */
1763 * Diverted. Pretend to be the destination.
1764 * already got one like this?
1766 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
1767 ip
->ip_dst
, th
->th_dport
, 0, m
->m_pkthdr
.rcvif
);
1770 * No, then it's new. Try find the ambushing socket
1772 if (!next_hop
->sin_port
) {
1773 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
,
1774 th
->th_sport
, next_hop
->sin_addr
,
1775 th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
1777 inp
= in_pcblookup_hash(&tcbinfo
,
1778 ip
->ip_src
, th
->th_sport
,
1780 ntohs(next_hop
->sin_port
), 1,
1785 #endif /* IPFIREWALL_FORWARD */
1789 inp
= in6_pcblookup_hash(&tcbinfo
, &ip6
->ip6_src
, th
->th_sport
,
1790 &ip6
->ip6_dst
, th
->th_dport
, 1,
1794 inp
= in_pcblookup_hash(&tcbinfo
, ip
->ip_src
, th
->th_sport
,
1795 ip
->ip_dst
, th
->th_dport
, 1, m
->m_pkthdr
.rcvif
);
1799 * Use the interface scope information from the PCB for outbound
1800 * segments. If the PCB isn't present and if scoped routing is
1801 * enabled, tcp_respond will use the scope of the interface where
1802 * the segment arrived on.
1804 if (inp
!= NULL
&& (inp
->inp_flags
& INP_BOUND_IF
))
1805 ifscope
= inp
->inp_boundifp
->if_index
;
1807 if (inp
!= NULL
&& (
1809 isipv6
? !necp_socket_is_allowed_to_send_recv_v6(inp
,
1810 th
->th_dport
, th
->th_sport
, &ip6
->ip6_dst
,
1811 &ip6
->ip6_src
, ifp
, NULL
) :
1813 !necp_socket_is_allowed_to_send_recv_v4(inp
, th
->th_dport
,
1814 th
->th_sport
, &ip
->ip_dst
, &ip
->ip_src
,
1816 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 0)
1818 inp
= NULL
; /* pretend we didn't find it */
1820 IF_TCP_STATINC(ifp
, badformatipsec
);
1826 * If the state is CLOSED (i.e., TCB does not exist) then
1827 * all data in the incoming segment is discarded.
1828 * If the TCB exists but is in CLOSED state, it is embryonic,
1829 * but should either do a listen or a connect soon.
1834 char dbuf
[MAX_IPv6_STR_LEN
], sbuf
[MAX_IPv6_STR_LEN
];
1836 char dbuf
[MAX_IPv4_STR_LEN
], sbuf
[MAX_IPv4_STR_LEN
];
1841 inet_ntop(AF_INET6
, &ip6
->ip6_dst
, dbuf
, sizeof(dbuf
));
1842 inet_ntop(AF_INET6
, &ip6
->ip6_src
, sbuf
, sizeof(sbuf
));
1846 inet_ntop(AF_INET
, &ip
->ip_dst
, dbuf
, sizeof(dbuf
));
1847 inet_ntop(AF_INET
, &ip
->ip_src
, sbuf
, sizeof(sbuf
));
1849 switch (log_in_vain
) {
1851 if(thflags
& TH_SYN
)
1853 "Connection attempt to TCP %s:%d from %s:%d\n",
1854 dbuf
, ntohs(th
->th_dport
),
1856 ntohs(th
->th_sport
));
1860 "Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
1861 dbuf
, ntohs(th
->th_dport
), sbuf
,
1862 ntohs(th
->th_sport
), thflags
);
1866 if ((thflags
& TH_SYN
) && !(thflags
& TH_ACK
) &&
1867 !(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
1869 ((isipv6
&& !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
)) ||
1870 (!isipv6
&& ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
))
1872 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
1875 log_in_vain_log((LOG_INFO
,
1876 "Stealth Mode connection attempt to TCP %s:%d from %s:%d\n",
1877 dbuf
, ntohs(th
->th_dport
),
1879 ntohs(th
->th_sport
)));
1886 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
1888 switch (blackhole
) {
1890 if (thflags
& TH_SYN
)
1899 rstreason
= BANDLIM_RST_CLOSEDPORT
;
1900 IF_TCP_STATINC(ifp
, noconnnolist
);
1901 goto dropwithresetnosock
;
1903 so
= inp
->inp_socket
;
1905 /* This case shouldn't happen as the socket shouldn't be null
1906 * if inp_state isn't set to INPCB_STATE_DEAD
1907 * But just in case, we pretend we didn't find the socket if we hit this case
1908 * as this isn't cause for a panic (the socket might be leaked however)...
1912 printf("tcp_input: no more socket for inp=%x. This shouldn't happen\n", inp
);
1918 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1919 tcp_unlock(so
, 1, (void *)2);
1920 inp
= NULL
; // pretend we didn't find it
1924 tp
= intotcpcb(inp
);
1926 rstreason
= BANDLIM_RST_CLOSEDPORT
;
1927 IF_TCP_STATINC(ifp
, noconnlist
);
1930 if (tp
->t_state
== TCPS_CLOSED
)
1933 /* Unscale the window into a 32-bit value. */
1934 if ((thflags
& TH_SYN
) == 0)
1935 tiwin
= th
->th_win
<< tp
->snd_scale
;
1940 if (mac_inpcb_check_deliver(inp
, m
, AF_INET
, SOCK_STREAM
))
1944 /* Avoid processing packets while closing a listen socket */
1945 if (tp
->t_state
== TCPS_LISTEN
&&
1946 (so
->so_options
& SO_ACCEPTCONN
) == 0)
1949 if (so
->so_options
& (SO_DEBUG
|SO_ACCEPTCONN
)) {
1951 if (so
->so_options
& SO_DEBUG
) {
1952 ostate
= tp
->t_state
;
1955 bcopy((char *)ip6
, (char *)tcp_saveipgen
,
1959 bcopy((char *)ip
, (char *)tcp_saveipgen
, sizeof(*ip
));
1963 if (so
->so_options
& SO_ACCEPTCONN
) {
1964 register struct tcpcb
*tp0
= tp
;
1967 struct sockaddr_storage from
;
1969 struct inpcb
*oinp
= sotoinpcb(so
);
1971 struct ifnet
*head_ifscope
;
1972 unsigned int head_nocell
, head_recvanyif
,
1973 head_noexpensive
, head_awdl_unrestricted
;
1975 /* Get listener's bound-to-interface, if any */
1976 head_ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
1977 inp
->inp_boundifp
: NULL
;
1978 /* Get listener's no-cellular information, if any */
1979 head_nocell
= INP_NO_CELLULAR(inp
);
1980 /* Get listener's recv-any-interface, if any */
1981 head_recvanyif
= (inp
->inp_flags
& INP_RECV_ANYIF
);
1982 /* Get listener's no-expensive information, if any */
1983 head_noexpensive
= INP_NO_EXPENSIVE(inp
);
1984 head_awdl_unrestricted
= INP_AWDL_UNRESTRICTED(inp
);
1987 * If the state is LISTEN then ignore segment if it contains an RST.
1988 * If the segment contains an ACK then it is bad and send a RST.
1989 * If it does not contain a SYN then it is not interesting; drop it.
1990 * If it is from this socket, drop it, it must be forged.
1992 if ((thflags
& (TH_RST
|TH_ACK
|TH_SYN
)) != TH_SYN
) {
1993 IF_TCP_STATINC(ifp
, listbadsyn
);
1995 if (thflags
& TH_RST
) {
1998 if (thflags
& TH_ACK
) {
2000 tcpstat
.tcps_badsyn
++;
2001 rstreason
= BANDLIM_RST_OPENPORT
;
2005 /* We come here if there is no SYN set */
2006 tcpstat
.tcps_badsyn
++;
2009 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_START
,0,0,0,0,0);
2010 if (th
->th_dport
== th
->th_sport
) {
2013 if (IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
,
2018 if (ip
->ip_dst
.s_addr
== ip
->ip_src
.s_addr
)
2022 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
2023 * in_broadcast() should never return true on a received
2024 * packet with M_BCAST not set.
2026 * Packets with a multicast source address should also
2029 if (m
->m_flags
& (M_BCAST
|M_MCAST
))
2033 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
2034 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
2038 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
2039 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
2040 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
2041 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
2047 * If deprecated address is forbidden,
2048 * we do not accept SYN to deprecated interface
2049 * address to prevent any new inbound connection from
2050 * getting established.
2051 * When we do not accept SYN, we send a TCP RST,
2052 * with deprecated source address (instead of dropping
2053 * it). We compromise it as it is much better for peer
2054 * to send a RST, and RST will be the final packet
2057 * If we do not forbid deprecated addresses, we accept
2058 * the SYN packet. RFC 4862 forbids dropping SYN in
2061 if (isipv6
&& !ip6_use_deprecated
) {
2064 if (ip6_getdstifaddr_info(m
, NULL
,
2066 if (ia6_flags
& IN6_IFF_DEPRECATED
) {
2068 rstreason
= BANDLIM_RST_OPENPORT
;
2069 IF_TCP_STATINC(ifp
, deprecate6
);
2078 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)&from
;
2080 sin6
->sin6_len
= sizeof(*sin6
);
2081 sin6
->sin6_family
= AF_INET6
;
2082 sin6
->sin6_port
= th
->th_sport
;
2083 sin6
->sin6_flowinfo
= 0;
2084 sin6
->sin6_addr
= ip6
->ip6_src
;
2085 sin6
->sin6_scope_id
= 0;
2090 struct sockaddr_in
*sin
= (struct sockaddr_in
*)&from
;
2092 sin
->sin_len
= sizeof(*sin
);
2093 sin
->sin_family
= AF_INET
;
2094 sin
->sin_port
= th
->th_sport
;
2095 sin
->sin_addr
= ip
->ip_src
;
2097 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2099 so2
= sonewconn(so
, 0, NULL
);
2102 tcpstat
.tcps_listendrop
++;
2103 if (tcp_dropdropablreq(so
)) {
2105 so2
= sonewconn(so
, 0, (struct sockaddr
*)&from
);
2107 so2
= sonewconn(so
, 0, NULL
);
2113 /* Point "inp" and "tp" in tandem to new socket */
2114 inp
= (struct inpcb
*)so2
->so_pcb
;
2115 tp
= intotcpcb(inp
);
2118 tcp_unlock(so
, 0, 0); /* Unlock but keep a reference on listener for now */
2123 * Mark socket as temporary until we're
2124 * committed to keeping it. The code at
2125 * ``drop'' and ``dropwithreset'' check the
2126 * flag dropsocket to see if the temporary
2127 * socket created here should be discarded.
2128 * We mark the socket as discardable until
2129 * we're committed to it below in TCPS_LISTEN.
2130 * There are some error conditions in which we
2131 * have to drop the temporary socket.
2135 * Inherit INP_BOUND_IF from listener; testing if
2136 * head_ifscope is non-NULL is sufficient, since it
2137 * can only be set to a non-zero value earlier if
2138 * the listener has such a flag set.
2140 if (head_ifscope
!= NULL
) {
2141 inp
->inp_flags
|= INP_BOUND_IF
;
2142 inp
->inp_boundifp
= head_ifscope
;
2144 inp
->inp_flags
&= ~INP_BOUND_IF
;
2147 * Inherit restrictions from listener.
2150 inp_set_nocellular(inp
);
2151 if (head_noexpensive
)
2152 inp_set_noexpensive(inp
);
2153 if (head_awdl_unrestricted
)
2154 inp_set_awdl_unrestricted(inp
);
2156 * Inherit {IN,IN6}_RECV_ANYIF from listener.
2159 inp
->inp_flags
|= INP_RECV_ANYIF
;
2161 inp
->inp_flags
&= ~INP_RECV_ANYIF
;
2164 inp
->in6p_laddr
= ip6
->ip6_dst
;
2166 inp
->inp_vflag
&= ~INP_IPV6
;
2167 inp
->inp_vflag
|= INP_IPV4
;
2169 inp
->inp_laddr
= ip
->ip_dst
;
2173 inp
->inp_lport
= th
->th_dport
;
2174 if (in_pcbinshash(inp
, 0) != 0) {
2176 * Undo the assignments above if we failed to
2177 * put the PCB on the hash lists.
2181 inp
->in6p_laddr
= in6addr_any
;
2184 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
2186 tcp_lock(oso
, 0, 0); /* release ref on parent */
2187 tcp_unlock(oso
, 1, 0);
2193 * Inherit socket options from the listening
2195 * Note that in6p_inputopts are not (even
2196 * should not be) copied, since it stores
2197 * previously received options and is used to
2198 * detect if each new option is different than
2199 * the previous one and hence should be passed
2201 * If we copied in6p_inputopts, a user would
2202 * not be able to receive options just after
2203 * calling the accept system call.
2206 oinp
->inp_flags
& INP_CONTROLOPTS
;
2207 if (oinp
->in6p_outputopts
)
2208 inp
->in6p_outputopts
=
2209 ip6_copypktopts(oinp
->in6p_outputopts
,
2213 inp
->inp_options
= ip_srcroute();
2214 tcp_lock(oso
, 0, 0);
2216 /* copy old policy into new socket's */
2217 if (sotoinpcb(oso
)->inp_sp
)
2220 /* Is it a security hole here to silently fail to copy the policy? */
2221 if (inp
->inp_sp
!= NULL
)
2222 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
2223 if (error
!= 0 || ipsec_copy_policy(sotoinpcb(oso
)->inp_sp
, inp
->inp_sp
))
2224 printf("tcp_input: could not copy policy\n");
2227 /* inherit states from the listener */
2228 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2229 struct tcpcb
*, tp
, int32_t, TCPS_LISTEN
);
2230 tp
->t_state
= TCPS_LISTEN
;
2231 tp
->t_flags
|= tp0
->t_flags
& (TF_NOPUSH
|TF_NOOPT
|TF_NODELAY
);
2232 tp
->t_flagsext
|= (tp0
->t_flagsext
& (TF_RXTFINDROP
|TF_NOTIMEWAIT
));
2233 tp
->t_keepinit
= tp0
->t_keepinit
;
2234 tp
->t_keepcnt
= tp0
->t_keepcnt
;
2235 tp
->t_keepintvl
= tp0
->t_keepintvl
;
2236 tp
->t_adaptive_wtimo
= tp0
->t_adaptive_wtimo
;
2237 tp
->t_adaptive_rtimo
= tp0
->t_adaptive_rtimo
;
2238 tp
->t_inpcb
->inp_ip_ttl
= tp0
->t_inpcb
->inp_ip_ttl
;
2239 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0)
2240 tp
->t_notsent_lowat
= tp0
->t_notsent_lowat
;
2242 /* now drop the reference on the listener */
2243 tcp_unlock(oso
, 1, 0);
2245 tcp_set_max_rwinscale(tp
, so
);
2247 KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN
| DBG_FUNC_END
,0,0,0,0,0);
2250 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2251 LCK_MTX_ASSERT_OWNED
);
2253 if (tp
->t_state
== TCPS_ESTABLISHED
&& tlen
> 0) {
2255 * Evaluate the rate of arrival of packets to see if the
2256 * receiver can reduce the ack traffic. The algorithm to
2257 * stretch acks will be enabled if the connection meets
2258 * certain criteria defined in tcp_stretch_ack_enable function.
2260 if ((tp
->t_flagsext
& TF_RCVUNACK_WAITSS
) != 0) {
2261 TCP_INC_VAR(tp
->rcv_waitforss
, nlropkts
);
2263 if (tcp_stretch_ack_enable(tp
)) {
2264 tp
->t_flags
|= TF_STRETCHACK
;
2265 tp
->t_flagsext
&= ~(TF_RCVUNACK_WAITSS
);
2266 tp
->rcv_waitforss
= 0;
2268 tp
->t_flags
&= ~(TF_STRETCHACK
);
2270 if (TSTMP_GT(tp
->rcv_unackwin
, tcp_now
)) {
2271 tp
->rcv_by_unackwin
+= (tlen
+ off
);
2273 tp
->rcv_unackwin
= tcp_now
+ tcp_rcvunackwin
;
2274 tp
->rcv_by_unackwin
= tlen
+ off
;
2279 * Keep track of how many bytes were received in the LRO packet
2281 if ((pktf_sw_lro_pkt
) && (nlropkts
> 2)) {
2282 tp
->t_lropktlen
+= tlen
;
2285 * Explicit Congestion Notification - Flag that we need to send ECT if
2286 * + The IP Congestion experienced flag was set.
2287 * + Socket is in established state
2288 * + We negotiated ECN in the TCP setup
2289 * + This isn't a pure ack (tlen > 0)
2290 * + The data is in the valid window
2292 * TE_SENDECE will be cleared when we receive a packet with TH_CWR set.
2294 if (ip_ecn
== IPTOS_ECN_CE
&& tp
->t_state
== TCPS_ESTABLISHED
&&
2295 ((tp
->ecn_flags
& (TE_ECN_ON
)) == (TE_ECN_ON
)) && tlen
> 0 &&
2296 SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
2297 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) {
2298 tp
->ecn_flags
|= TE_SENDECE
;
2302 * Clear TE_SENDECE if TH_CWR is set. This is harmless, so we don't
2303 * bother doing extensive checks for state and whatnot.
2305 if ((thflags
& TH_CWR
) == TH_CWR
) {
2306 tp
->ecn_flags
&= ~TE_SENDECE
;
2310 * If we received an explicit notification of congestion in
2311 * ip tos ecn bits or by the CWR bit in TCP header flags, reset
2312 * the ack-strteching state. We need to handle ECN notification if
2313 * an ECN setup SYN was sent even once.
2315 if (tp
->t_state
== TCPS_ESTABLISHED
2316 && (tp
->ecn_flags
& TE_SETUPSENT
)
2317 && (ip_ecn
== IPTOS_ECN_CE
|| (thflags
& TH_CWR
)))
2318 tcp_reset_stretch_ack(tp
);
2321 * Try to determine if we are receiving a packet after a long time.
2322 * Use our own approximation of idletime to roughly measure remote
2323 * end's idle time. Since slowstart is used after an idle period
2324 * we want to avoid doing LRO if the remote end is not up to date
2325 * on initial window support and starts with 1 or 2 packets as its IW.
2327 if (sw_lro
&& (tp
->t_flagsext
& TF_LRO_OFFLOADED
) &&
2328 ((tcp_now
- tp
->t_rcvtime
) >= (TCP_IDLETIMEOUT(tp
)))) {
2332 /* Update rcvtime as a new segment was received on the connection */
2333 tp
->t_rcvtime
= tcp_now
;
2336 * Segment received on connection.
2337 * Reset idle time and keep-alive timer.
2339 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
2340 tcp_keepalive_reset(tp
);
2343 * Process options if not in LISTEN state,
2344 * else do it below (after getting remote address).
2346 if (tp
->t_state
!= TCPS_LISTEN
&& optp
) {
2347 tcp_dooptions(tp
, optp
, optlen
, th
, &to
, ifscope
);
2349 mptcp_csum
= mptcp_input_csum(tp
, m
, drop_hdrlen
);
2351 tp
->t_mpflags
|= TMPF_SND_MPFAIL
;
2352 tp
->t_mpflags
&= ~TMPF_EMBED_DSN
;
2353 mptcp_notify_mpfail(so
);
2355 tcpstat
.tcps_mp_badcsum
++;
2356 tcp_check_timer_state(tp
);
2357 tcp_unlock(so
, 1, 0);
2358 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
|
2359 DBG_FUNC_END
,0,0,0,0,0);
2362 mptcp_insert_rmap(tp
, m
);
2365 if (tp
->t_state
== TCPS_SYN_SENT
&& (thflags
& TH_SYN
)) {
2366 if (to
.to_flags
& TOF_TS
) {
2367 tp
->t_flags
|= TF_RCVD_TSTMP
;
2368 tp
->ts_recent
= to
.to_tsval
;
2369 tp
->ts_recent_age
= tcp_now
;
2371 if (to
.to_flags
& TOF_MSS
)
2372 tcp_mss(tp
, to
.to_mss
, ifscope
);
2373 if (SACK_ENABLED(tp
)) {
2374 if (!(to
.to_flags
& TOF_SACK
))
2375 tp
->t_flagsext
&= ~(TF_SACK_ENABLE
);
2377 tp
->t_flags
|= TF_SACK_PERMIT
;
2382 /* Compute inter-packet arrival jitter. According to RFC 3550, inter-packet
2383 * arrival jitter is defined as the difference in packet spacing at the
2384 * receiver compared to the sender for a pair of packets. When two packets
2385 * of maximum segment size come one after the other with consecutive
2386 * sequence numbers, we consider them as packets sent together at the
2387 * sender and use them as a pair to compute inter-packet arrival jitter.
2388 * This metric indicates the delay induced by the network components due
2389 * to queuing in edge/access routers.
2391 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2392 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
|TH_PUSH
)) == TH_ACK
&&
2393 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2394 ((to
.to_flags
& TOF_TS
) == 0 ||
2395 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2396 th
->th_seq
== tp
->rcv_nxt
&&
2397 LIST_EMPTY(&tp
->t_segq
)) {
2398 int seg_size
= tlen
;
2399 if (tp
->iaj_pktcnt
<= IAJ_IGNORE_PKTCNT
) {
2400 TCP_INC_VAR(tp
->iaj_pktcnt
, nlropkts
);
2403 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
2404 seg_size
= m
->m_pkthdr
.lro_pktlen
;
2406 if ( tp
->iaj_size
== 0 || seg_size
> tp
->iaj_size
||
2407 (seg_size
== tp
->iaj_size
&& tp
->iaj_rcv_ts
== 0)) {
2408 /* State related to inter-arrival jitter is uninitialized
2409 * or we are trying to find a good first packet to start
2410 * computing the metric
2412 update_iaj_state(tp
, seg_size
, 0);
2414 if (seg_size
== tp
->iaj_size
) {
2415 /* Compute inter-arrival jitter taking this packet
2416 * as the second packet
2418 if (pktf_sw_lro_pkt
)
2419 compute_iaj(tp
, nlropkts
,
2420 m
->m_pkthdr
.lro_elapsed
);
2422 compute_iaj(tp
, 1, 0);
2424 if (seg_size
< tp
->iaj_size
) {
2425 /* There is a smaller packet in the stream.
2426 * Some times the maximum size supported on a path can
2427 * change if there is a new link with smaller MTU.
2428 * The receiver will not know about this change.
2429 * If there are too many packets smaller than iaj_size,
2430 * we try to learn the iaj_size again.
2432 TCP_INC_VAR(tp
->iaj_small_pkt
, nlropkts
);
2433 if (tp
->iaj_small_pkt
> RESET_IAJ_SIZE_THRESH
) {
2434 update_iaj_state(tp
, seg_size
, 1);
2436 CLEAR_IAJ_STATE(tp
);
2439 update_iaj_state(tp
, seg_size
, 0);
2443 CLEAR_IAJ_STATE(tp
);
2445 #endif /* TRAFFIC_MGT */
2448 * Header prediction: check for the two common cases
2449 * of a uni-directional data xfer. If the packet has
2450 * no control flags, is in-sequence, the window didn't
2451 * change and we're not retransmitting, it's a
2452 * candidate. If the length is zero and the ack moved
2453 * forward, we're the sender side of the xfer. Just
2454 * free the data acked & wake any higher level process
2455 * that was blocked waiting for space. If the length
2456 * is non-zero and the ack didn't move, we're the
2457 * receiver side. If we're getting packets in-order
2458 * (the reassembly queue is empty), add the data to
2459 * the socket buffer and note that we need a delayed ack.
2460 * Make sure that the hidden state-flags are also off.
2461 * Since we check for TCPS_ESTABLISHED above, it can only
2464 if (tp
->t_state
== TCPS_ESTABLISHED
&&
2465 (thflags
& (TH_SYN
|TH_FIN
|TH_RST
|TH_URG
|TH_ACK
|TH_ECE
)) == TH_ACK
&&
2466 ((tp
->t_flags
& (TF_NEEDSYN
|TF_NEEDFIN
)) == 0) &&
2467 ((to
.to_flags
& TOF_TS
) == 0 ||
2468 TSTMP_GEQ(to
.to_tsval
, tp
->ts_recent
)) &&
2469 th
->th_seq
== tp
->rcv_nxt
&&
2470 tiwin
&& tiwin
== tp
->snd_wnd
&&
2471 tp
->snd_nxt
== tp
->snd_max
) {
2474 * If last ACK falls within this segment's sequence numbers,
2475 * record the timestamp.
2476 * NOTE that the test is modified according to the latest
2477 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
2479 if ((to
.to_flags
& TOF_TS
) != 0 &&
2480 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
)) {
2481 tp
->ts_recent_age
= tcp_now
;
2482 tp
->ts_recent
= to
.to_tsval
;
2485 /* Force acknowledgment if we received a FIN */
2487 if (thflags
& TH_FIN
)
2488 tp
->t_flags
|= TF_ACKNOW
;
2491 if (SEQ_GT(th
->th_ack
, tp
->snd_una
) &&
2492 SEQ_LEQ(th
->th_ack
, tp
->snd_max
) &&
2493 tp
->snd_cwnd
>= tp
->snd_ssthresh
&&
2494 (!IN_FASTRECOVERY(tp
) &&
2495 ((!(SACK_ENABLED(tp
)) &&
2496 tp
->t_dupacks
< tp
->t_rexmtthresh
) ||
2497 (SACK_ENABLED(tp
) && to
.to_nsacks
== 0 &&
2498 TAILQ_EMPTY(&tp
->snd_holes
))))) {
2500 * this is a pure ack for outstanding data.
2502 ++tcpstat
.tcps_predack
;
2504 tcp_bad_rexmt_check(tp
, th
, &to
),
2506 /* Recalculate the RTT */
2507 tcp_compute_rtt(tp
, &to
, th
);
2509 acked
= BYTES_ACKED(th
, tp
);
2510 tcpstat
.tcps_rcvackpack
++;
2511 tcpstat
.tcps_rcvackbyte
+= acked
;
2513 /* Handle an ack that is in sequence during congestion
2514 * avoidance phase. The calculations in this function
2515 * assume that snd_una is not updated yet.
2517 if (CC_ALGO(tp
)->congestion_avd
!= NULL
)
2518 CC_ALGO(tp
)->congestion_avd(tp
, th
);
2519 tcp_ccdbg_trace(tp
, th
, TCP_CC_INSEQ_ACK_RCVD
);
2520 sbdrop(&so
->so_snd
, acked
);
2521 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2522 VERIFY(acked
<= so
->so_msg_state
->msg_serial_bytes
);
2523 so
->so_msg_state
->msg_serial_bytes
-= acked
;
2525 tcp_sbsnd_trim(&so
->so_snd
);
2527 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
2528 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
2529 tp
->snd_recover
= th
->th_ack
- 1;
2530 tp
->snd_una
= th
->th_ack
;
2533 * pull snd_wl2 up to prevent seq wrap relative
2536 tp
->snd_wl2
= th
->th_ack
;
2538 if (tp
->t_dupacks
> 0) {
2540 tp
->t_rexmtthresh
= tcprexmtthresh
;
2546 * If all outstanding data are acked, stop
2547 * retransmit timer, otherwise restart timer
2548 * using current (possibly backed-off) value.
2549 * If process is waiting for space,
2550 * wakeup/selwakeup/signal. If data
2551 * are ready to send, let tcp_output
2552 * decide between more output or persist.
2554 if (tp
->snd_una
== tp
->snd_max
) {
2555 tp
->t_timer
[TCPT_REXMT
] = 0;
2556 tp
->t_timer
[TCPT_PTO
] = 0;
2557 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0) {
2558 tp
->t_timer
[TCPT_REXMT
] =
2559 OFFSET_FROM_START(tp
,
2563 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
2564 tp
->t_bwmeas
!= NULL
)
2565 tcp_bwmeas_check(tp
);
2566 sowwakeup(so
); /* has to be done with socket lock held */
2567 if ((so
->so_snd
.sb_cc
) || (tp
->t_flags
& TF_ACKNOW
)) {
2568 (void) tcp_output(tp
);
2571 tcp_check_timer_state(tp
);
2572 tcp_unlock(so
, 1, 0);
2573 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2576 } else if (th
->th_ack
== tp
->snd_una
&&
2577 LIST_EMPTY(&tp
->t_segq
) &&
2578 tlen
<= tcp_sbspace(tp
)) {
2580 * this is a pure, in-sequence data packet
2581 * with nothing on the reassembly queue and
2582 * we have enough buffer space to take it.
2586 * If this is a connection in steady state, start
2587 * coalescing packets belonging to this flow.
2590 tcp_lro_remove_state(tp
->t_inpcb
->inp_laddr
,
2591 tp
->t_inpcb
->inp_faddr
,
2592 tp
->t_inpcb
->inp_lport
,
2593 tp
->t_inpcb
->inp_fport
);
2594 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
2595 tp
->t_idleat
= tp
->rcv_nxt
;
2596 } else if (sw_lro
&& !pktf_sw_lro_pkt
&& !isipv6
&&
2597 (so
->so_flags
& SOF_USELRO
) &&
2598 !IFNET_IS_CELLULAR(m
->m_pkthdr
.rcvif
) &&
2599 (m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
) &&
2600 ((th
->th_seq
- tp
->irs
) >
2601 (tp
->t_maxseg
<< lro_start
)) &&
2602 ((tp
->t_idleat
== 0) || ((th
->th_seq
-
2603 tp
->t_idleat
) > (tp
->t_maxseg
<< lro_start
)))) {
2604 tp
->t_flagsext
|= TF_LRO_OFFLOADED
;
2605 tcp_start_coalescing(ip
, th
, tlen
);
2609 /* Clean receiver SACK report if present */
2610 if (SACK_ENABLED(tp
) && tp
->rcv_numsacks
)
2611 tcp_clean_sackreport(tp
);
2612 ++tcpstat
.tcps_preddat
;
2613 tp
->rcv_nxt
+= tlen
;
2615 * Pull snd_wl1 up to prevent seq wrap relative to
2618 tp
->snd_wl1
= th
->th_seq
;
2620 * Pull rcv_up up to prevent seq wrap relative to
2623 tp
->rcv_up
= tp
->rcv_nxt
;
2624 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
2625 tcpstat
.tcps_rcvbyte
+= tlen
;
2626 if (nstat_collect
) {
2627 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
2628 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2629 rxpackets
, m
->m_pkthdr
.lro_npkts
);
2631 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
2634 INP_ADD_STAT(inp
, cell
, wifi
, wired
,rxbytes
,
2639 * Calculate the RTT on the receiver only if the
2640 * connection is in streaming mode and the last
2641 * packet was not an end-of-write
2643 if ((tp
->t_flags
& TF_STRETCHACK
) &&
2644 !(tp
->t_flagsext
& TF_STREAMEOW
))
2645 tcp_compute_rtt(tp
, &to
, th
);
2647 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
);
2650 * Add data to socket buffer.
2652 so_recv_data_stat(so
, m
, 0);
2653 m_adj(m
, drop_hdrlen
); /* delayed header drop */
2656 * If message delivery (SOF_ENABLE_MSGS) is enabled on
2657 * this socket, deliver the packet received as an
2658 * in-order message with sequence number attached to it.
2660 if (sbappendstream_rcvdemux(so
, m
,
2661 th
->th_seq
- (tp
->irs
+ 1), 0)) {
2666 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2667 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
2668 th
->th_seq
, th
->th_ack
, th
->th_win
);
2673 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
2674 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
2675 th
->th_seq
, th
->th_ack
, th
->th_win
);
2677 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
2678 if (DELAY_ACK(tp
, th
)) {
2679 if ((tp
->t_flags
& TF_DELACK
) == 0) {
2680 tp
->t_flags
|= TF_DELACK
;
2681 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
2684 tp
->t_flags
|= TF_ACKNOW
;
2688 tcp_adaptive_rwtimo_check(tp
, tlen
);
2690 tcp_check_timer_state(tp
);
2691 tcp_unlock(so
, 1, 0);
2692 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
2698 * Calculate amount of space in receive window,
2699 * and then do TCP input processing.
2700 * Receive window is amount of space in rcv queue,
2701 * but not less than advertised window.
2703 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2704 LCK_MTX_ASSERT_OWNED
);
2705 win
= tcp_sbspace(tp
);
2708 else { /* clip rcv window to 4K for modems */
2709 if (tp
->t_flags
& TF_SLOWLINK
&& slowlink_wsize
> 0)
2710 win
= min(win
, slowlink_wsize
);
2712 tp
->rcv_wnd
= imax(win
, (int)(tp
->rcv_adv
- tp
->rcv_nxt
));
2715 * Ensure that the subflow receive window isn't greater
2716 * than the connection level receive window.
2718 if ((tp
->t_mpflags
& TMPF_MPTCP_TRUE
) &&
2719 (mp_tp
= tptomptp(tp
))) {
2721 if (tp
->rcv_wnd
> mp_tp
->mpt_rcvwnd
) {
2722 tp
->rcv_wnd
= mp_tp
->mpt_rcvwnd
;
2723 tcpstat
.tcps_mp_reducedwin
++;
2729 switch (tp
->t_state
) {
2732 * Initialize tp->rcv_nxt, and tp->irs, select an initial
2733 * tp->iss, and send a segment:
2734 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
2735 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
2736 * Fill in remote peer address fields if not previously specified.
2737 * Enter SYN_RECEIVED state, and process any other fields of this
2738 * segment in this state.
2741 register struct sockaddr_in
*sin
;
2743 register struct sockaddr_in6
*sin6
;
2746 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2747 LCK_MTX_ASSERT_OWNED
);
2750 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
,
2751 M_SONAME
, M_NOWAIT
);
2754 bzero(sin6
, sizeof(*sin6
));
2755 sin6
->sin6_family
= AF_INET6
;
2756 sin6
->sin6_len
= sizeof(*sin6
);
2757 sin6
->sin6_addr
= ip6
->ip6_src
;
2758 sin6
->sin6_port
= th
->th_sport
;
2759 laddr6
= inp
->in6p_laddr
;
2760 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
2761 inp
->in6p_laddr
= ip6
->ip6_dst
;
2762 if (in6_pcbconnect(inp
, (struct sockaddr
*)sin6
,
2764 inp
->in6p_laddr
= laddr6
;
2765 FREE(sin6
, M_SONAME
);
2768 FREE(sin6
, M_SONAME
);
2772 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
2773 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
2777 sin
->sin_family
= AF_INET
;
2778 sin
->sin_len
= sizeof(*sin
);
2779 sin
->sin_addr
= ip
->ip_src
;
2780 sin
->sin_port
= th
->th_sport
;
2781 bzero((caddr_t
)sin
->sin_zero
, sizeof(sin
->sin_zero
));
2782 laddr
= inp
->inp_laddr
;
2783 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
2784 inp
->inp_laddr
= ip
->ip_dst
;
2785 if (in_pcbconnect(inp
, (struct sockaddr
*)sin
, proc0
,
2786 IFSCOPE_NONE
, NULL
)) {
2787 inp
->inp_laddr
= laddr
;
2788 FREE(sin
, M_SONAME
);
2791 FREE(sin
, M_SONAME
);
2794 tcp_dooptions(tp
, optp
, optlen
, th
, &to
, ifscope
);
2796 if (SACK_ENABLED(tp
)) {
2797 if (!(to
.to_flags
& TOF_SACK
))
2798 tp
->t_flagsext
&= ~(TF_SACK_ENABLE
);
2800 tp
->t_flags
|= TF_SACK_PERMIT
;
2806 tp
->iss
= tcp_new_isn(tp
);
2808 tp
->irs
= th
->th_seq
;
2809 tcp_sendseqinit(tp
);
2811 tp
->snd_recover
= tp
->snd_una
;
2813 * Initialization of the tcpcb for transaction;
2814 * set SND.WND = SEG.WND,
2815 * initialize CCsend and CCrecv.
2817 tp
->snd_wnd
= tiwin
; /* initial send-window */
2818 tp
->t_flags
|= TF_ACKNOW
;
2819 tp
->t_unacksegs
= 0;
2820 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2821 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
2822 tp
->t_state
= TCPS_SYN_RECEIVED
;
2823 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2824 TCP_CONN_KEEPINIT(tp
));
2825 dropsocket
= 0; /* committed to socket */
2827 if (inp
->inp_flowhash
== 0)
2828 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
2830 /* update flowinfo - RFC 6437 */
2831 if (inp
->inp_flow
== 0 &&
2832 inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
2833 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
2835 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
2839 /* reset the incomp processing flag */
2840 so
->so_flags
&= ~(SOF_INCOMP_INPROGRESS
);
2841 tcpstat
.tcps_accepts
++;
2842 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
| TH_CWR
)) {
2844 tp
->ecn_flags
|= (TE_SETUPRECEIVED
| TE_SENDIPECT
);
2847 #if CONFIG_IFEF_NOWINDOWSCALE
2848 if (tcp_obey_ifef_nowindowscale
&& m
->m_pkthdr
.rcvif
!= NULL
&&
2849 (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_NOWINDOWSCALE
)) {
2850 /* Window scaling is not enabled on this interface */
2851 tp
->t_flags
&= ~TF_REQ_SCALE
;
2858 * If the state is SYN_RECEIVED:
2859 * if seg contains an ACK, but not for our SYN/ACK, send a RST.
2861 case TCPS_SYN_RECEIVED
:
2862 if ((thflags
& TH_ACK
) &&
2863 (SEQ_LEQ(th
->th_ack
, tp
->snd_una
) ||
2864 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
2865 rstreason
= BANDLIM_RST_OPENPORT
;
2866 IF_TCP_STATINC(ifp
, ooopacket
);
2871 * In SYN_RECEIVED state, if we recv some SYNS with
2872 * window scale and others without, window scaling should
2873 * be disabled. Otherwise the window advertised will be
2874 * lower if we assume scaling and the other end does not.
2876 if ((thflags
& TH_SYN
) &&
2877 !(to
.to_flags
& TOF_SCALE
))
2878 tp
->t_flags
&= ~TF_RCVD_SCALE
;
2882 * If the state is SYN_SENT:
2883 * if seg contains an ACK, but not for our SYN, drop the input.
2884 * if seg contains a RST, then drop the connection.
2885 * if seg does not contain SYN, then drop it.
2886 * Otherwise this is an acceptable SYN segment
2887 * initialize tp->rcv_nxt and tp->irs
2888 * if seg contains ack then advance tp->snd_una
2889 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2890 * arrange for segment to be acked (eventually)
2891 * continue processing rest of data/controls, beginning with URG
2894 if ((thflags
& TH_ACK
) &&
2895 (SEQ_LEQ(th
->th_ack
, tp
->iss
) ||
2896 SEQ_GT(th
->th_ack
, tp
->snd_max
))) {
2897 rstreason
= BANDLIM_UNLIMITED
;
2898 IF_TCP_STATINC(ifp
, ooopacket
);
2901 if (thflags
& TH_RST
) {
2902 if ((thflags
& TH_ACK
) != 0) {
2904 if ((so
->so_flags
& SOF_MPTCP_FASTJOIN
) &&
2905 SEQ_GT(th
->th_ack
, tp
->iss
+1)) {
2906 so
->so_flags
&= ~SOF_MPTCP_FASTJOIN
;
2907 /* ignore the RST and retransmit SYN */
2912 (SO_FILT_HINT_LOCKED
|
2913 SO_FILT_HINT_CONNRESET
));
2914 tp
= tcp_drop(tp
, ECONNREFUSED
);
2915 postevent(so
, 0, EV_RESET
);
2919 if ((thflags
& TH_SYN
) == 0)
2921 tp
->snd_wnd
= th
->th_win
; /* initial send window */
2923 tp
->irs
= th
->th_seq
;
2925 if (thflags
& TH_ACK
) {
2926 tcpstat
.tcps_connects
++;
2928 if ((thflags
& (TH_ECE
| TH_CWR
)) == (TH_ECE
)) {
2929 /* ECN-setup SYN-ACK */
2930 tp
->ecn_flags
|= TE_SETUPRECEIVED
;
2931 tcpstat
.tcps_ecn_setup
++;
2934 /* non-ECN-setup SYN-ACK */
2935 tp
->ecn_flags
&= ~TE_SENDIPECT
;
2938 #if CONFIG_MACF_NET && CONFIG_MACF_SOCKET
2939 /* XXXMAC: recursive lock: SOCK_LOCK(so); */
2940 mac_socketpeer_label_associate_mbuf(m
, so
);
2941 /* XXXMAC: SOCK_UNLOCK(so); */
2943 /* Do window scaling on this connection? */
2944 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
2945 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
2946 tp
->snd_scale
= tp
->requested_s_scale
;
2947 tp
->rcv_scale
= tp
->request_r_scale
;
2949 tp
->rcv_adv
+= min(tp
->rcv_wnd
, TCP_MAXWIN
<< tp
->rcv_scale
);
2950 tp
->snd_una
++; /* SYN is acked */
2952 * If there's data, delay ACK; if there's also a FIN
2953 * ACKNOW will be turned on later.
2955 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
2956 if (DELAY_ACK(tp
, th
) && tlen
!= 0 ) {
2957 if ((tp
->t_flags
& TF_DELACK
) == 0) {
2958 tp
->t_flags
|= TF_DELACK
;
2959 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
2963 tp
->t_flags
|= TF_ACKNOW
;
2966 * Received <SYN,ACK> in SYN_SENT[*] state.
2968 * SYN_SENT --> ESTABLISHED
2969 * SYN_SENT* --> FIN_WAIT_1
2971 tp
->t_starttime
= tcp_now
;
2972 tcp_sbrcv_tstmp_check(tp
);
2973 if (tp
->t_flags
& TF_NEEDFIN
) {
2974 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2975 struct tcpcb
*, tp
, int32_t, TCPS_FIN_WAIT_1
);
2976 tp
->t_state
= TCPS_FIN_WAIT_1
;
2977 tp
->t_flags
&= ~TF_NEEDFIN
;
2980 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
2981 struct tcpcb
*, tp
, int32_t, TCPS_ESTABLISHED
);
2982 tp
->t_state
= TCPS_ESTABLISHED
;
2983 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2984 TCP_CONN_KEEPIDLE(tp
));
2986 nstat_route_connect_success(tp
->t_inpcb
->inp_route
.ro_rt
);
2990 * Do not send the connect notification for additional
2991 * subflows until ACK for 3-way handshake arrives.
2993 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
2994 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
2995 isconnected
= FALSE
;
2996 /* Start data xmit if fastjoin */
2997 if (mptcp_fastjoin
&& (so
->so_flags
& SOF_MPTCP_FASTJOIN
)) {
2998 soevent(so
, (SO_FILT_HINT_LOCKED
|
2999 SO_FILT_HINT_MPFASTJ
));
3006 * Received initial SYN in SYN-SENT[*] state => simul-
3007 * taneous open. If segment contains CC option and there is
3008 * a cached CC, apply TAO test; if it succeeds, connection is
3009 * half-synchronized. Otherwise, do 3-way handshake:
3010 * SYN-SENT -> SYN-RECEIVED
3011 * SYN-SENT* -> SYN-RECEIVED*
3013 tp
->t_flags
|= TF_ACKNOW
;
3014 tp
->t_timer
[TCPT_REXMT
] = 0;
3015 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3016 struct tcpcb
*, tp
, int32_t, TCPS_SYN_RECEIVED
);
3017 tp
->t_state
= TCPS_SYN_RECEIVED
;
3023 * Advance th->th_seq to correspond to first data byte.
3024 * If data, trim to stay within window,
3025 * dropping FIN if necessary.
3028 if (tlen
> tp
->rcv_wnd
) {
3029 todrop
= tlen
- tp
->rcv_wnd
;
3033 tcpstat
.tcps_rcvpackafterwin
++;
3034 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3036 tp
->snd_wl1
= th
->th_seq
- 1;
3037 tp
->rcv_up
= th
->th_seq
;
3039 * Client side of transaction: already sent SYN and data.
3040 * If the remote host used T/TCP to validate the SYN,
3041 * our data will be ACK'd; if so, enter normal data segment
3042 * processing in the middle of step 5, ack processing.
3043 * Otherwise, goto step 6.
3045 if (thflags
& TH_ACK
)
3049 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
3050 * do normal processing.
3052 * NB: Leftover from RFC1644 T/TCP. Cases to be reused later.
3056 case TCPS_TIME_WAIT
:
3057 break; /* continue normal processing */
3059 /* Received a SYN while connection is already established.
3060 * This is a "half open connection and other anomalies" described
3061 * in RFC793 page 34, send an ACK so the remote reset the connection
3062 * or recovers by adjusting its sequence numberering
3064 case TCPS_ESTABLISHED
:
3065 if (thflags
& TH_SYN
)
3071 * States other than LISTEN or SYN_SENT.
3072 * First check the RST flag and sequence number since reset segments
3073 * are exempt from the timestamp and connection count tests. This
3074 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
3075 * below which allowed reset segments in half the sequence space
3076 * to fall though and be processed (which gives forged reset
3077 * segments with a random sequence number a 50 percent chance of
3078 * killing a connection).
3079 * Then check timestamp, if present.
3080 * Then check the connection count, if present.
3081 * Then check that at least some bytes of segment are within
3082 * receive window. If segment begins before rcv_nxt,
3083 * drop leading data (and SYN); if nothing left, just ack.
3086 * If the RST bit is set, check the sequence number to see
3087 * if this is a valid reset segment.
3089 * In all states except SYN-SENT, all reset (RST) segments
3090 * are validated by checking their SEQ-fields. A reset is
3091 * valid if its sequence number is in the window.
3092 * Note: this does not take into account delayed ACKs, so
3093 * we should test against last_ack_sent instead of rcv_nxt.
3094 * The sequence number in the reset segment is normally an
3095 * echo of our outgoing acknowlegement numbers, but some hosts
3096 * send a reset with the sequence number at the rightmost edge
3097 * of our receive window, and we have to handle this case.
3098 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
3099 * that brute force RST attacks are possible. To combat this,
3100 * we use a much stricter check while in the ESTABLISHED state,
3101 * only accepting RSTs where the sequence number is equal to
3102 * last_ack_sent. In all other states (the states in which a
3103 * RST is more likely), the more permissive check is used.
3104 * If we have multiple segments in flight, the intial reset
3105 * segment sequence numbers will be to the left of last_ack_sent,
3106 * but they will eventually catch up.
3107 * In any case, it never made sense to trim reset segments to
3108 * fit the receive window since RFC 1122 says:
3109 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
3111 * A TCP SHOULD allow a received RST segment to include data.
3114 * It has been suggested that a RST segment could contain
3115 * ASCII text that encoded and explained the cause of the
3116 * RST. No standard has yet been established for such
3119 * If the reset segment passes the sequence number test examine
3121 * SYN_RECEIVED STATE:
3122 * If passive open, return to LISTEN state.
3123 * If active open, inform user that connection was refused.
3124 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
3125 * Inform user that connection was reset, and close tcb.
3126 * CLOSING, LAST_ACK STATES:
3129 * Drop the segment - see Stevens, vol. 2, p. 964 and
3132 * Radar 4803931: Allows for the case where we ACKed the FIN but
3133 * there is already a RST in flight from the peer.
3134 * In that case, accept the RST for non-established
3135 * state if it's one off from last_ack_sent.
3138 if (thflags
& TH_RST
) {
3139 if ((SEQ_GEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3140 SEQ_LT(th
->th_seq
, tp
->last_ack_sent
+ tp
->rcv_wnd
)) ||
3141 (tp
->rcv_wnd
== 0 &&
3142 ((tp
->last_ack_sent
== th
->th_seq
) ||
3143 ((tp
->last_ack_sent
-1) == th
->th_seq
)))) {
3144 switch (tp
->t_state
) {
3146 case TCPS_SYN_RECEIVED
:
3147 IF_TCP_STATINC(ifp
, rstinsynrcv
);
3148 so
->so_error
= ECONNREFUSED
;
3151 case TCPS_ESTABLISHED
:
3152 if (tp
->last_ack_sent
!= th
->th_seq
) {
3153 tcpstat
.tcps_badrst
++;
3156 case TCPS_FIN_WAIT_1
:
3157 case TCPS_CLOSE_WAIT
:
3161 case TCPS_FIN_WAIT_2
:
3162 so
->so_error
= ECONNRESET
;
3164 postevent(so
, 0, EV_RESET
);
3166 (SO_FILT_HINT_LOCKED
|
3167 SO_FILT_HINT_CONNRESET
));
3169 tcpstat
.tcps_drops
++;
3178 case TCPS_TIME_WAIT
:
3186 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3187 * and it's less than ts_recent, drop it.
3189 if ((to
.to_flags
& TOF_TS
) != 0 && tp
->ts_recent
&&
3190 TSTMP_LT(to
.to_tsval
, tp
->ts_recent
)) {
3192 /* Check to see if ts_recent is over 24 days old. */
3193 if ((int)(tcp_now
- tp
->ts_recent_age
) > TCP_PAWS_IDLE
) {
3195 * Invalidate ts_recent. If this segment updates
3196 * ts_recent, the age will be reset later and ts_recent
3197 * will get a valid value. If it does not, setting
3198 * ts_recent to zero will at least satisfy the
3199 * requirement that zero be placed in the timestamp
3200 * echo reply when ts_recent isn't valid. The
3201 * age isn't reset until we get a valid ts_recent
3202 * because we don't want out-of-order segments to be
3203 * dropped when ts_recent is old.
3207 tcpstat
.tcps_rcvduppack
++;
3208 tcpstat
.tcps_rcvdupbyte
+= tlen
;
3209 tcpstat
.tcps_pawsdrop
++;
3210 if (nstat_collect
) {
3211 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
,
3212 1, tlen
, NSTAT_RX_FLAG_DUPLICATE
);
3213 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3215 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
3217 tp
->t_stat
.rxduplicatebytes
+= tlen
;
3226 * In the SYN-RECEIVED state, validate that the packet belongs to
3227 * this connection before trimming the data to fit the receive
3228 * window. Check the sequence number versus IRS since we know
3229 * the sequence numbers haven't wrapped. This is a partial fix
3230 * for the "LAND" DoS attack.
3232 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& SEQ_LT(th
->th_seq
, tp
->irs
)) {
3233 rstreason
= BANDLIM_RST_OPENPORT
;
3234 IF_TCP_STATINC(ifp
, dospacket
);
3238 todrop
= tp
->rcv_nxt
- th
->th_seq
;
3240 if (thflags
& TH_SYN
) {
3250 * Following if statement from Stevens, vol. 2, p. 960.
3253 || (todrop
== tlen
&& (thflags
& TH_FIN
) == 0)) {
3255 * Any valid FIN must be to the left of the window.
3256 * At this point the FIN must be a duplicate or out
3257 * of sequence; drop it.
3262 * Send an ACK to resynchronize and drop any data.
3263 * But keep on processing for RST or ACK.
3265 tp
->t_flags
|= TF_ACKNOW
;
3267 /* This could be a keepalive */
3268 soevent(so
, SO_FILT_HINT_LOCKED
|
3269 SO_FILT_HINT_KEEPALIVE
);
3272 tcpstat
.tcps_rcvduppack
++;
3273 tcpstat
.tcps_rcvdupbyte
+= todrop
;
3275 tcpstat
.tcps_rcvpartduppack
++;
3276 tcpstat
.tcps_rcvpartdupbyte
+= todrop
;
3278 if (nstat_collect
) {
3279 nstat_route_rx(tp
->t_inpcb
->inp_route
.ro_rt
, 1,
3280 todrop
, NSTAT_RX_FLAG_DUPLICATE
);
3281 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxpackets
, 1);
3282 INP_ADD_STAT(inp
, cell
, wifi
, wired
, rxbytes
, todrop
);
3283 tp
->t_stat
.rxduplicatebytes
+= todrop
;
3285 drop_hdrlen
+= todrop
; /* drop from the top afterwards */
3286 th
->th_seq
+= todrop
;
3288 if (th
->th_urp
> todrop
)
3289 th
->th_urp
-= todrop
;
3297 * If new data are received on a connection after the user processes
3298 * are gone, then RST the other end. Note that an MPTCP subflow socket
3299 * would have SS_NOFDREF set by default, so check to make sure that
3300 * we test for SOF_MP_SUBFLOW socket flag (which would be cleared when
3301 * the socket is closed.)
3303 if (!(so
->so_flags
& SOF_MP_SUBFLOW
) &&
3304 (so
->so_state
& SS_NOFDREF
) &&
3305 tp
->t_state
> TCPS_CLOSE_WAIT
&& tlen
) {
3307 tcpstat
.tcps_rcvafterclose
++;
3308 rstreason
= BANDLIM_UNLIMITED
;
3309 IF_TCP_STATINC(ifp
, cleanup
);
3314 * If segment ends after window, drop trailing data
3315 * (and PUSH and FIN); if nothing left, just ACK.
3317 todrop
= (th
->th_seq
+tlen
) - (tp
->rcv_nxt
+tp
->rcv_wnd
);
3319 tcpstat
.tcps_rcvpackafterwin
++;
3320 if (todrop
>= tlen
) {
3321 tcpstat
.tcps_rcvbyteafterwin
+= tlen
;
3323 * If a new connection request is received
3324 * while in TIME_WAIT, drop the old connection
3325 * and start over if the sequence numbers
3326 * are above the previous ones.
3328 if (thflags
& TH_SYN
&&
3329 tp
->t_state
== TCPS_TIME_WAIT
&&
3330 SEQ_GT(th
->th_seq
, tp
->rcv_nxt
)) {
3331 iss
= tcp_new_isn(tp
);
3333 tcp_unlock(so
, 1, 0);
3337 * If window is closed can only take segments at
3338 * window edge, and have to drop data and PUSH from
3339 * incoming segments. Continue processing, but
3340 * remember to ack. Otherwise, drop segment
3343 if (tp
->rcv_wnd
== 0 && th
->th_seq
== tp
->rcv_nxt
) {
3344 tp
->t_flags
|= TF_ACKNOW
;
3345 tcpstat
.tcps_rcvwinprobe
++;
3349 tcpstat
.tcps_rcvbyteafterwin
+= todrop
;
3352 thflags
&= ~(TH_PUSH
|TH_FIN
);
3356 * If last ACK falls within this segment's sequence numbers,
3357 * record its timestamp.
3359 * 1) That the test incorporates suggestions from the latest
3360 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
3361 * 2) That updating only on newer timestamps interferes with
3362 * our earlier PAWS tests, so this check should be solely
3363 * predicated on the sequence space of this segment.
3364 * 3) That we modify the segment boundary check to be
3365 * Last.ACK.Sent <= SEG.SEQ + SEG.Len
3366 * instead of RFC1323's
3367 * Last.ACK.Sent < SEG.SEQ + SEG.Len,
3368 * This modified check allows us to overcome RFC1323's
3369 * limitations as described in Stevens TCP/IP Illustrated
3370 * Vol. 2 p.869. In such cases, we can still calculate the
3371 * RTT correctly when RCV.NXT == Last.ACK.Sent.
3373 if ((to
.to_flags
& TOF_TS
) != 0 &&
3374 SEQ_LEQ(th
->th_seq
, tp
->last_ack_sent
) &&
3375 SEQ_LEQ(tp
->last_ack_sent
, th
->th_seq
+ tlen
+
3376 ((thflags
& (TH_SYN
|TH_FIN
)) != 0))) {
3377 tp
->ts_recent_age
= tcp_now
;
3378 tp
->ts_recent
= to
.to_tsval
;
3382 * If a SYN is in the window, then this is an
3383 * error and we send an RST and drop the connection.
3385 if (thflags
& TH_SYN
) {
3386 tp
= tcp_drop(tp
, ECONNRESET
);
3387 rstreason
= BANDLIM_UNLIMITED
;
3388 postevent(so
, 0, EV_RESET
);
3389 IF_TCP_STATINC(ifp
, synwindow
);
3394 * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
3395 * flag is on (half-synchronized state), then queue data for
3396 * later processing; else drop segment and return.
3398 if ((thflags
& TH_ACK
) == 0) {
3399 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
3400 (tp
->t_flags
& TF_NEEDSYN
))
3402 else if (tp
->t_flags
& TF_ACKNOW
)
3412 switch (tp
->t_state
) {
3415 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
3416 * ESTABLISHED state and continue processing.
3417 * The ACK was checked above.
3419 case TCPS_SYN_RECEIVED
:
3421 tcpstat
.tcps_connects
++;
3423 /* Do window scaling? */
3424 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
3425 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
3426 tp
->snd_scale
= tp
->requested_s_scale
;
3427 tp
->rcv_scale
= tp
->request_r_scale
;
3428 tp
->snd_wnd
= th
->th_win
<< tp
->snd_scale
;
3429 tiwin
= tp
->snd_wnd
;
3433 * SYN-RECEIVED -> ESTABLISHED
3434 * SYN-RECEIVED* -> FIN-WAIT-1
3436 tp
->t_starttime
= tcp_now
;
3437 tcp_sbrcv_tstmp_check(tp
);
3438 if (tp
->t_flags
& TF_NEEDFIN
) {
3439 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3440 struct tcpcb
*, tp
, int32_t, TCPS_FIN_WAIT_1
);
3441 tp
->t_state
= TCPS_FIN_WAIT_1
;
3442 tp
->t_flags
&= ~TF_NEEDFIN
;
3444 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
3445 struct tcpcb
*, tp
, int32_t, TCPS_ESTABLISHED
);
3446 tp
->t_state
= TCPS_ESTABLISHED
;
3447 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
3448 TCP_CONN_KEEPIDLE(tp
));
3450 nstat_route_connect_success(tp
->t_inpcb
->inp_route
.ro_rt
);
3453 * If segment contains data or ACK, will call tcp_reass()
3454 * later; if not, do so now to pass queued data to user.
3456 if (tlen
== 0 && (thflags
& TH_FIN
) == 0)
3457 (void) tcp_reass(tp
, (struct tcphdr
*)0, &tlen
,
3459 tp
->snd_wl1
= th
->th_seq
- 1;
3464 * Do not send the connect notification for additional subflows
3465 * until ACK for 3-way handshake arrives.
3467 if ((!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
)) &&
3468 (tp
->t_mpflags
& TMPF_SENT_JOIN
)) {
3469 isconnected
= FALSE
;
3475 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
3476 * ACKs. If the ack is in the range
3477 * tp->snd_una < th->th_ack <= tp->snd_max
3478 * then advance tp->snd_una to th->th_ack and drop
3479 * data from the retransmission queue. If this ACK reflects
3480 * more up to date window information we update our window information.
3482 case TCPS_ESTABLISHED
:
3483 case TCPS_FIN_WAIT_1
:
3484 case TCPS_FIN_WAIT_2
:
3485 case TCPS_CLOSE_WAIT
:
3488 case TCPS_TIME_WAIT
:
3489 if (SEQ_GT(th
->th_ack
, tp
->snd_max
)) {
3490 tcpstat
.tcps_rcvacktoomuch
++;
3493 if (SACK_ENABLED(tp
) &&
3494 (to
.to_nsacks
> 0 || !TAILQ_EMPTY(&tp
->snd_holes
)))
3495 tcp_sack_doack(tp
, &to
, th
, &sack_bytes_acked
);
3498 if ((tp
->t_mpuna
) && (SEQ_GEQ(th
->th_ack
, tp
->t_mpuna
))) {
3499 if (tp
->t_mpflags
& TMPF_PREESTABLISHED
) {
3500 /* MP TCP establishment succeeded */
3502 if (tp
->t_mpflags
& TMPF_JOINED_FLOW
) {
3503 if (tp
->t_mpflags
& TMPF_SENT_JOIN
) {
3505 ~TMPF_PREESTABLISHED
;
3508 so
->so_flags
|= SOF_MPTCP_TRUE
;
3509 if (mptcp_dbg
>= MP_ERR_DEBUG
)
3510 printf("MPTCP SUCCESS"
3512 tp
->t_timer
[TCPT_JACK_RXMT
] = 0;
3513 tp
->t_mprxtshift
= 0;
3516 isconnected
= FALSE
;
3520 tp
->t_mpflags
&= ~TMPF_SENT_KEYS
;
3526 * If we have outstanding data (other than
3527 * a window probe), this is a completely
3528 * duplicate ack (ie, window info didn't
3529 * change) and the ack is the biggest we've seen.
3531 if (SEQ_LEQ(th
->th_ack
, tp
->snd_una
)) {
3532 if (tlen
== 0 && tiwin
== tp
->snd_wnd
) {
3534 * If both ends send FIN at the same time,
3535 * then the ack will be a duplicate ack
3536 * but we have to process the FIN. Check
3537 * for this condition and process the FIN
3538 * instead of the dupack
3540 if ((thflags
& TH_FIN
) &&
3541 (tp
->t_flags
& TF_SENTFIN
) &&
3542 !TCPS_HAVERCVDFIN(tp
->t_state
) &&
3543 (th
->th_ack
+ 1) == tp
->snd_max
) {
3549 * MPTCP options that are ignored must
3550 * not be treated as duplicate ACKs.
3552 if (to
.to_flags
& TOF_MPTCP
) {
3556 if ((isconnected
) && (tp
->t_mpflags
& TMPF_JOINED_FLOW
)) {
3557 if (mptcp_dbg
>= MP_ERR_DEBUG
)
3558 printf("%s: bypass ack recovery\n",__func__
);
3563 * If a duplicate acknowledgement was seen
3564 * after ECN, it indicates packet loss in
3565 * addition to ECN. Reset INRECOVERY flag
3566 * so that we can process partial acks
3569 if (tp
->ecn_flags
& TE_INRECOVERY
)
3570 tp
->ecn_flags
&= ~TE_INRECOVERY
;
3572 tcpstat
.tcps_rcvdupack
++;
3576 * Check if we need to reset the limit on
3579 if (tp
->t_early_rexmt_count
> 0 &&
3581 (tp
->t_early_rexmt_win
+
3582 TCP_EARLY_REXMT_WIN
)))
3583 tp
->t_early_rexmt_count
= 0;
3586 * Is early retransmit needed? We check for
3587 * this when the connection is waiting for
3588 * duplicate acks to enter fast recovery.
3590 if (!IN_FASTRECOVERY(tp
))
3591 tcp_early_rexmt_check(tp
, th
);
3594 * If we've seen exactly rexmt threshold
3595 * of duplicate acks, assume a packet
3596 * has been dropped and retransmit it.
3597 * Kludge snd_nxt & the congestion
3598 * window so we send only this one
3601 * We know we're losing at the current
3602 * window size so do congestion avoidance
3603 * (set ssthresh to half the current window
3604 * and pull our congestion window back to
3605 * the new ssthresh).
3607 * Dup acks mean that packets have left the
3608 * network (they're now cached at the receiver)
3609 * so bump cwnd by the amount in the receiver
3610 * to keep a constant cwnd packets in the
3613 if (tp
->t_timer
[TCPT_REXMT
] == 0 ||
3614 (th
->th_ack
!= tp
->snd_una
3615 && sack_bytes_acked
== 0)) {
3617 tp
->t_rexmtthresh
= tcprexmtthresh
;
3618 } else if (tp
->t_dupacks
> tp
->t_rexmtthresh
||
3619 IN_FASTRECOVERY(tp
)) {
3622 * If this connection was seeing packet
3623 * reordering, then recovery might be
3624 * delayed to disambiguate between
3625 * reordering and loss
3627 if (SACK_ENABLED(tp
) && !IN_FASTRECOVERY(tp
) &&
3629 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) ==
3630 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
3632 * Since the SACK information is already
3633 * updated, this ACK will be dropped
3638 if (SACK_ENABLED(tp
)
3639 && IN_FASTRECOVERY(tp
)) {
3643 * Compute the amount of data in flight first.
3644 * We can inject new data into the pipe iff
3645 * we have less than 1/2 the original window's
3646 * worth of data in flight.
3648 awnd
= (tp
->snd_nxt
- tp
->snd_fack
) +
3649 tp
->sackhint
.sack_bytes_rexmit
;
3650 if (awnd
< tp
->snd_ssthresh
) {
3651 tp
->snd_cwnd
+= tp
->t_maxseg
;
3652 if (tp
->snd_cwnd
> tp
->snd_ssthresh
)
3653 tp
->snd_cwnd
= tp
->snd_ssthresh
;
3656 tp
->snd_cwnd
+= tp
->t_maxseg
;
3658 tcp_ccdbg_trace(tp
, th
, TCP_CC_IN_FASTRECOVERY
);
3660 (void) tcp_output(tp
);
3662 } else if (tp
->t_dupacks
== tp
->t_rexmtthresh
) {
3663 tcp_seq onxt
= tp
->snd_nxt
;
3666 * If we're doing sack, check to
3667 * see if we're already in sack
3668 * recovery. If we're not doing sack,
3669 * check to see if we're in newreno
3672 if (SACK_ENABLED(tp
)) {
3673 if (IN_FASTRECOVERY(tp
)) {
3676 } else if (tp
->t_flagsext
& TF_DELAY_RECOVERY
) {
3680 if (SEQ_LEQ(th
->th_ack
,
3687 tp
->snd_recover
= tp
->snd_max
;
3688 tp
->t_timer
[TCPT_PTO
] = 0;
3692 * If the connection has seen pkt
3693 * reordering, delay recovery until
3694 * it is clear that the packet
3697 if (SACK_ENABLED(tp
) &&
3699 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
3700 == TF_PKTS_REORDERED
&&
3701 !IN_FASTRECOVERY(tp
) &&
3702 tp
->t_reorderwin
> 0 &&
3703 tp
->t_state
== TCPS_ESTABLISHED
) {
3704 tp
->t_timer
[TCPT_DELAYFR
] =
3705 OFFSET_FROM_START(tp
,
3707 tp
->t_flagsext
|= TF_DELAY_RECOVERY
;
3708 tcpstat
.tcps_delay_recovery
++;
3709 tcp_ccdbg_trace(tp
, th
,
3710 TCP_CC_DELAY_FASTRECOVERY
);
3715 * If the current tcp cc module has
3716 * defined a hook for tasks to run
3717 * before entering FR, call it
3719 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
3720 CC_ALGO(tp
)->pre_fr(tp
);
3721 ENTER_FASTRECOVERY(tp
);
3722 tp
->t_timer
[TCPT_REXMT
] = 0;
3723 if ((tp
->ecn_flags
& TE_ECN_ON
)
3725 tp
->ecn_flags
|= TE_SENDCWR
;
3727 if (SACK_ENABLED(tp
)) {
3728 tcpstat
.tcps_sack_recovery_episode
++;
3729 tp
->sack_newdata
= tp
->snd_nxt
;
3730 tp
->snd_cwnd
= tp
->t_maxseg
;
3733 * Enable probe timeout to detect
3734 * a tail loss in the recovery
3737 tp
->t_timer
[TCPT_PTO
] =
3738 OFFSET_FROM_START(tp
,
3739 max(10, (tp
->t_srtt
>> TCP_RTT_SHIFT
)));
3741 tcp_ccdbg_trace(tp
, th
,
3742 TCP_CC_ENTER_FASTRECOVERY
);
3744 (void) tcp_output(tp
);
3747 tp
->snd_nxt
= th
->th_ack
;
3748 tp
->snd_cwnd
= tp
->t_maxseg
;
3749 (void) tcp_output(tp
);
3750 tp
->snd_cwnd
= tp
->snd_ssthresh
+
3751 tp
->t_maxseg
* tp
->t_dupacks
;
3752 if (SEQ_GT(onxt
, tp
->snd_nxt
))
3754 tcp_ccdbg_trace(tp
, th
,
3755 TCP_CC_ENTER_FASTRECOVERY
);
3757 } else if (limited_txmt
&&
3758 ALLOW_LIMITED_TRANSMIT(tp
) &&
3759 (!(SACK_ENABLED(tp
)) || sack_bytes_acked
> 0) &&
3760 (so
->so_snd
.sb_cc
- (tp
->snd_max
- tp
->snd_una
)) > 0) {
3761 u_int32_t incr
= (tp
->t_maxseg
* tp
->t_dupacks
);
3763 /* Use Limited Transmit algorithm on the first two
3764 * duplicate acks when there is new data to transmit
3766 tp
->snd_cwnd
+= incr
;
3767 tcpstat
.tcps_limited_txt
++;
3768 (void) tcp_output(tp
);
3770 tcp_ccdbg_trace(tp
, th
, TCP_CC_LIMITED_TRANSMIT
);
3772 /* Reset snd_cwnd back to normal */
3773 tp
->snd_cwnd
-= incr
;
3777 tp
->t_rexmtthresh
= tcprexmtthresh
;
3782 * If the congestion window was inflated to account
3783 * for the other side's cached packets, retract it.
3785 if (IN_FASTRECOVERY(tp
)) {
3786 if (SEQ_LT(th
->th_ack
, tp
->snd_recover
)) {
3788 * If we received an ECE and entered
3789 * recovery, the subsequent ACKs should
3790 * not be treated as partial acks.
3792 if (tp
->ecn_flags
& TE_INRECOVERY
)
3795 if (SACK_ENABLED(tp
))
3796 tcp_sack_partialack(tp
, th
);
3798 tcp_newreno_partial_ack(tp
, th
);
3799 tcp_ccdbg_trace(tp
, th
, TCP_CC_PARTIAL_ACK
);
3801 EXIT_FASTRECOVERY(tp
);
3802 if (CC_ALGO(tp
)->post_fr
!= NULL
)
3803 CC_ALGO(tp
)->post_fr(tp
, th
);
3805 tcp_ccdbg_trace(tp
, th
,
3806 TCP_CC_EXIT_FASTRECOVERY
);
3808 } else if ((tp
->t_flagsext
&
3809 (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
))
3810 == (TF_PKTS_REORDERED
|TF_DELAY_RECOVERY
)) {
3812 * If the ack acknowledges upto snd_recover or if
3813 * it acknowledges all the snd holes, exit
3814 * recovery and cancel the timer. Otherwise,
3815 * this is a partial ack. Wait for recovery timer
3816 * to enter recovery. The snd_holes have already
3819 if (SEQ_GEQ(th
->th_ack
, tp
->snd_recover
) ||
3820 TAILQ_EMPTY(&tp
->snd_holes
)) {
3821 tp
->t_timer
[TCPT_DELAYFR
] = 0;
3822 tp
->t_flagsext
&= ~TF_DELAY_RECOVERY
;
3823 EXIT_FASTRECOVERY(tp
);
3824 tcp_ccdbg_trace(tp
, th
,
3825 TCP_CC_EXIT_FASTRECOVERY
);
3829 * We were not in fast recovery. Reset the
3830 * duplicate ack counter.
3833 tp
->t_rexmtthresh
= tcprexmtthresh
;
3838 * If we reach this point, ACK is not a duplicate,
3839 * i.e., it ACKs something we sent.
3841 if (tp
->t_flags
& TF_NEEDSYN
) {
3843 * T/TCP: Connection was half-synchronized, and our
3844 * SYN has been ACK'd (so connection is now fully
3845 * synchronized). Go to non-starred state,
3846 * increment snd_una for ACK of SYN, and check if
3847 * we can do window scaling.
3849 tp
->t_flags
&= ~TF_NEEDSYN
;
3851 /* Do window scaling? */
3852 if ((tp
->t_flags
& (TF_RCVD_SCALE
|TF_REQ_SCALE
)) ==
3853 (TF_RCVD_SCALE
|TF_REQ_SCALE
)) {
3854 tp
->snd_scale
= tp
->requested_s_scale
;
3855 tp
->rcv_scale
= tp
->request_r_scale
;
3860 acked
= BYTES_ACKED(th
, tp
);
3861 tcpstat
.tcps_rcvackpack
++;
3862 tcpstat
.tcps_rcvackbyte
+= acked
;
3865 * If the last packet was a retransmit, make sure
3866 * it was not spurious.
3868 * This will also take care of congestion window
3869 * adjustment if a last packet was recovered due to a
3872 tcp_bad_rexmt_check(tp
, th
, &to
);
3874 /* Recalculate the RTT */
3875 tcp_compute_rtt(tp
, &to
, th
);
3878 * If all outstanding data is acked, stop retransmit
3879 * timer and remember to restart (more output or persist).
3880 * If there is more data to be acked, restart retransmit
3881 * timer, using current (possibly backed-off) value.
3883 if (th
->th_ack
== tp
->snd_max
) {
3884 tp
->t_timer
[TCPT_REXMT
] = 0;
3885 tp
->t_timer
[TCPT_PTO
] = 0;
3887 } else if (tp
->t_timer
[TCPT_PERSIST
] == 0)
3888 tp
->t_timer
[TCPT_REXMT
] = OFFSET_FROM_START(tp
,
3892 * If no data (only SYN) was ACK'd, skip rest of ACK
3899 if ((thflags
& TH_ECE
) != 0 &&
3900 ((tp
->ecn_flags
& TE_ECN_ON
) == TE_ECN_ON
)) {
3902 * Reduce the congestion window if we haven't
3905 if (!IN_FASTRECOVERY(tp
)) {
3906 tcp_reduce_congestion_window(tp
);
3907 tp
->ecn_flags
|= (TE_INRECOVERY
|TE_SENDCWR
);
3908 tcp_ccdbg_trace(tp
, th
, TCP_CC_ECN_RCVD
);
3913 * When new data is acked, open the congestion window.
3914 * The specifics of how this is achieved are up to the
3915 * congestion control algorithm in use for this connection.
3917 * The calculations in this function assume that snd_una is
3920 if (!IN_FASTRECOVERY(tp
)) {
3921 if (CC_ALGO(tp
)->ack_rcvd
!= NULL
)
3922 CC_ALGO(tp
)->ack_rcvd(tp
, th
);
3923 tcp_ccdbg_trace(tp
, th
, TCP_CC_ACK_RCVD
);
3925 if (acked
> so
->so_snd
.sb_cc
) {
3926 tp
->snd_wnd
-= so
->so_snd
.sb_cc
;
3927 sbdrop(&so
->so_snd
, (int)so
->so_snd
.sb_cc
);
3928 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
3929 so
->so_msg_state
->msg_serial_bytes
-=
3930 (int)so
->so_snd
.sb_cc
;
3934 sbdrop(&so
->so_snd
, acked
);
3935 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
3936 so
->so_msg_state
->msg_serial_bytes
-=
3939 tcp_sbsnd_trim(&so
->so_snd
);
3940 tp
->snd_wnd
-= acked
;
3943 /* detect una wraparound */
3944 if ( !IN_FASTRECOVERY(tp
) &&
3945 SEQ_GT(tp
->snd_una
, tp
->snd_recover
) &&
3946 SEQ_LEQ(th
->th_ack
, tp
->snd_recover
))
3947 tp
->snd_recover
= th
->th_ack
- 1;
3949 if (IN_FASTRECOVERY(tp
) &&
3950 SEQ_GEQ(th
->th_ack
, tp
->snd_recover
))
3951 EXIT_FASTRECOVERY(tp
);
3953 tp
->snd_una
= th
->th_ack
;
3954 if (SACK_ENABLED(tp
)) {
3955 if (SEQ_GT(tp
->snd_una
, tp
->snd_recover
))
3956 tp
->snd_recover
= tp
->snd_una
;
3958 if (SEQ_LT(tp
->snd_nxt
, tp
->snd_una
))
3959 tp
->snd_nxt
= tp
->snd_una
;
3960 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
3961 tp
->t_bwmeas
!= NULL
)
3962 tcp_bwmeas_check(tp
);
3965 * sowwakeup must happen after snd_una, et al. are updated so that
3966 * the sequence numbers are in sync with so_snd
3970 switch (tp
->t_state
) {
3973 * In FIN_WAIT_1 STATE in addition to the processing
3974 * for the ESTABLISHED state if our FIN is now acknowledged
3975 * then enter FIN_WAIT_2.
3977 case TCPS_FIN_WAIT_1
:
3978 if (ourfinisacked
) {
3980 * If we can't receive any more
3981 * data, then closing user can proceed.
3982 * Starting the TCPT_2MSL timer is contrary to the
3983 * specification, but if we don't get a FIN
3984 * we'll hang forever.
3986 if (so
->so_state
& SS_CANTRCVMORE
) {
3987 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
3988 TCP_CONN_MAXIDLE(tp
));
3989 isconnected
= FALSE
;
3990 isdisconnected
= TRUE
;
3992 DTRACE_TCP4(state__change
, void, NULL
,
3993 struct inpcb
*, inp
,
3995 int32_t, TCPS_FIN_WAIT_2
);
3996 tp
->t_state
= TCPS_FIN_WAIT_2
;
3997 /* fall through and make sure we also recognize
3998 * data ACKed with the FIN
4001 tp
->t_flags
|= TF_ACKNOW
;
4005 * In CLOSING STATE in addition to the processing for
4006 * the ESTABLISHED state if the ACK acknowledges our FIN
4007 * then enter the TIME-WAIT state, otherwise ignore
4011 if (ourfinisacked
) {
4012 DTRACE_TCP4(state__change
, void, NULL
,
4013 struct inpcb
*, inp
,
4015 int32_t, TCPS_TIME_WAIT
);
4016 tp
->t_state
= TCPS_TIME_WAIT
;
4017 tcp_canceltimers(tp
);
4018 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
4019 tp
->t_flags
|= TF_CLOSING
;
4021 add_to_time_wait(tp
, 2 * tcp_msl
);
4023 isconnected
= FALSE
;
4024 isdisconnected
= TRUE
;
4026 tp
->t_flags
|= TF_ACKNOW
;
4030 * In LAST_ACK, we may still be waiting for data to drain
4031 * and/or to be acked, as well as for the ack of our FIN.
4032 * If our FIN is now acknowledged, delete the TCB,
4033 * enter the closed state and return.
4036 if (ourfinisacked
) {
4043 * In TIME_WAIT state the only thing that should arrive
4044 * is a retransmission of the remote FIN. Acknowledge
4045 * it and restart the finack timer.
4047 case TCPS_TIME_WAIT
:
4048 add_to_time_wait(tp
, 2 * tcp_msl
);
4053 * If there is a SACK option on the ACK and we
4054 * haven't seen any duplicate acks before, count
4055 * it as a duplicate ack even if the cumulative
4056 * ack is advanced. If the receiver delayed an
4057 * ack and detected loss afterwards, then the ack
4058 * will advance cumulative ack and will also have
4059 * a SACK option. So counting it as one duplicate
4062 if (sack_ackadv
== 1 &&
4063 tp
->t_state
== TCPS_ESTABLISHED
&&
4064 SACK_ENABLED(tp
) && sack_bytes_acked
> 0 &&
4065 to
.to_nsacks
> 0 && tp
->t_dupacks
== 0 &&
4066 SEQ_LEQ(th
->th_ack
, tp
->snd_una
) && tlen
== 0 &&
4067 !(tp
->t_flagsext
& TF_PKTS_REORDERED
)) {
4068 tcpstat
.tcps_sack_ackadv
++;
4069 goto process_dupack
;
4075 * Update window information.
4076 * Don't look at window if no ACK: TAC's send garbage on first SYN.
4078 if ((thflags
& TH_ACK
) &&
4079 (SEQ_LT(tp
->snd_wl1
, th
->th_seq
) ||
4080 (tp
->snd_wl1
== th
->th_seq
&& (SEQ_LT(tp
->snd_wl2
, th
->th_ack
) ||
4081 (tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
))))) {
4082 /* keep track of pure window updates */
4084 tp
->snd_wl2
== th
->th_ack
&& tiwin
> tp
->snd_wnd
)
4085 tcpstat
.tcps_rcvwinupd
++;
4086 tp
->snd_wnd
= tiwin
;
4087 tp
->snd_wl1
= th
->th_seq
;
4088 tp
->snd_wl2
= th
->th_ack
;
4089 if (tp
->snd_wnd
> tp
->max_sndwnd
)
4090 tp
->max_sndwnd
= tp
->snd_wnd
;
4095 * Process segments with URG.
4097 if ((thflags
& TH_URG
) && th
->th_urp
&&
4098 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4100 * This is a kludge, but if we receive and accept
4101 * random urgent pointers, we'll crash in
4102 * soreceive. It's hard to imagine someone
4103 * actually wanting to send this much urgent data.
4105 if (th
->th_urp
+ so
->so_rcv
.sb_cc
> sb_max
) {
4106 th
->th_urp
= 0; /* XXX */
4107 thflags
&= ~TH_URG
; /* XXX */
4108 goto dodata
; /* XXX */
4111 * If this segment advances the known urgent pointer,
4112 * then mark the data stream. This should not happen
4113 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
4114 * a FIN has been received from the remote side.
4115 * In these states we ignore the URG.
4117 * According to RFC961 (Assigned Protocols),
4118 * the urgent pointer points to the last octet
4119 * of urgent data. We continue, however,
4120 * to consider it to indicate the first octet
4121 * of data past the urgent section as the original
4122 * spec states (in one of two places).
4124 if (SEQ_GT(th
->th_seq
+th
->th_urp
, tp
->rcv_up
)) {
4125 tp
->rcv_up
= th
->th_seq
+ th
->th_urp
;
4126 so
->so_oobmark
= so
->so_rcv
.sb_cc
+
4127 (tp
->rcv_up
- tp
->rcv_nxt
) - 1;
4128 if (so
->so_oobmark
== 0) {
4129 so
->so_state
|= SS_RCVATMARK
;
4130 postevent(so
, 0, EV_OOB
);
4133 tp
->t_oobflags
&= ~(TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
4136 * Remove out of band data so doesn't get presented to user.
4137 * This can happen independent of advancing the URG pointer,
4138 * but if two URG's are pending at once, some out-of-band
4139 * data may creep in... ick.
4141 if (th
->th_urp
<= (u_int32_t
)tlen
4143 && (so
->so_options
& SO_OOBINLINE
) == 0
4146 tcp_pulloutofband(so
, th
, m
,
4147 drop_hdrlen
); /* hdr drop is delayed */
4150 * If no out of band data is expected,
4151 * pull receive urgent pointer along
4152 * with the receive window.
4154 if (SEQ_GT(tp
->rcv_nxt
, tp
->rcv_up
))
4155 tp
->rcv_up
= tp
->rcv_nxt
;
4159 /* Set socket's connect or disconnect state correcly before doing data.
4160 * The following might unlock the socket if there is an upcall or a socket
4165 } else if (isdisconnected
) {
4166 soisdisconnected(so
);
4169 /* Let's check the state of pcb just to make sure that it did not get closed
4170 * when we unlocked above
4172 if (inp
->inp_state
== INPCB_STATE_DEAD
) {
4173 /* Just drop the packet that we are processing and return */
4178 * Process the segment text, merging it into the TCP sequencing queue,
4179 * and arranging for acknowledgment of receipt if necessary.
4180 * This process logically involves adjusting tp->rcv_wnd as data
4181 * is presented to the user (this happens in tcp_usrreq.c,
4182 * case PRU_RCVD). If a FIN has already been received on this
4183 * connection then we just ignore the text.
4185 if ((tlen
|| (thflags
& TH_FIN
)) &&
4186 TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4187 tcp_seq save_start
= th
->th_seq
;
4188 tcp_seq save_end
= th
->th_seq
+ tlen
;
4189 m_adj(m
, drop_hdrlen
); /* delayed header drop */
4191 * Insert segment which includes th into TCP reassembly queue
4192 * with control block tp. Set thflags to whether reassembly now
4193 * includes a segment with FIN. This handles the common case
4194 * inline (segment is the next to be received on an established
4195 * connection, and the queue is empty), avoiding linkage into
4196 * and removal from the queue and repetition of various
4198 * Set DELACK for segments received in order, but ack
4199 * immediately when segments are out of order (so
4200 * fast retransmit can work).
4202 if (th
->th_seq
== tp
->rcv_nxt
&&
4203 LIST_EMPTY(&tp
->t_segq
) &&
4204 TCPS_HAVEESTABLISHED(tp
->t_state
)) {
4205 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4207 * Calculate the RTT on the receiver only if the
4208 * connection is in streaming mode and the last
4209 * packet was not an end-of-write
4211 if ((tp
->t_flags
& TF_STRETCHACK
) &&
4212 !(tp
->t_flagsext
& TF_STREAMEOW
))
4213 tcp_compute_rtt(tp
, &to
, th
);
4215 if (DELAY_ACK(tp
, th
) &&
4216 ((tp
->t_flags
& TF_ACKNOW
) == 0) ) {
4217 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4218 tp
->t_flags
|= TF_DELACK
;
4219 tp
->t_timer
[TCPT_DELACK
] =
4220 OFFSET_FROM_START(tp
, tcp_delack
);
4224 tp
->t_flags
|= TF_ACKNOW
;
4226 tp
->rcv_nxt
+= tlen
;
4227 thflags
= th
->th_flags
& TH_FIN
;
4228 TCP_INC_VAR(tcpstat
.tcps_rcvpack
, nlropkts
);
4229 tcpstat
.tcps_rcvbyte
+= tlen
;
4230 if (nstat_collect
) {
4231 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_PKT
) {
4232 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4233 rxpackets
, m
->m_pkthdr
.lro_npkts
);
4235 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4238 INP_ADD_STAT(inp
, cell
, wifi
, wired
,
4241 tcp_sbrcv_grow(tp
, &so
->so_rcv
, &to
, tlen
);
4242 so_recv_data_stat(so
, m
, drop_hdrlen
);
4244 if (sbappendstream_rcvdemux(so
, m
,
4245 th
->th_seq
- (tp
->irs
+ 1), 0)) {
4249 thflags
= tcp_reass(tp
, th
, &tlen
, m
, ifp
);
4250 tp
->t_flags
|= TF_ACKNOW
;
4253 if (tlen
> 0 && SACK_ENABLED(tp
))
4254 tcp_update_sack_list(tp
, save_start
, save_end
);
4256 tcp_adaptive_rwtimo_check(tp
, tlen
);
4258 if (tp
->t_flags
& TF_DELACK
)
4262 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4263 (((ip6
->ip6_src
.s6_addr16
[0]) << 16) | (ip6
->ip6_dst
.s6_addr16
[0])),
4264 th
->th_seq
, th
->th_ack
, th
->th_win
);
4269 KERNEL_DEBUG(DBG_LAYER_END
, ((th
->th_dport
<< 16) | th
->th_sport
),
4270 (((ip
->ip_src
.s_addr
& 0xffff) << 16) | (ip
->ip_dst
.s_addr
& 0xffff)),
4271 th
->th_seq
, th
->th_ack
, th
->th_win
);
4281 * If FIN is received ACK the FIN and let the user know
4282 * that the connection is closing.
4284 if (thflags
& TH_FIN
) {
4285 if (TCPS_HAVERCVDFIN(tp
->t_state
) == 0) {
4287 postevent(so
, 0, EV_FIN
);
4289 * If connection is half-synchronized
4290 * (ie NEEDSYN flag on) then delay ACK,
4291 * so it may be piggybacked when SYN is sent.
4292 * Otherwise, since we received a FIN then no
4293 * more input can be expected, send ACK now.
4295 TCP_INC_VAR(tp
->t_unacksegs
, nlropkts
);
4296 if (DELAY_ACK(tp
, th
) && (tp
->t_flags
& TF_NEEDSYN
)) {
4297 if ((tp
->t_flags
& TF_DELACK
) == 0) {
4298 tp
->t_flags
|= TF_DELACK
;
4299 tp
->t_timer
[TCPT_DELACK
] = OFFSET_FROM_START(tp
, tcp_delack
);
4303 tp
->t_flags
|= TF_ACKNOW
;
4307 switch (tp
->t_state
) {
4310 * In SYN_RECEIVED and ESTABLISHED STATES
4311 * enter the CLOSE_WAIT state.
4313 case TCPS_SYN_RECEIVED
:
4314 tp
->t_starttime
= tcp_now
;
4315 case TCPS_ESTABLISHED
:
4316 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
4317 struct tcpcb
*, tp
, int32_t, TCPS_CLOSE_WAIT
);
4318 tp
->t_state
= TCPS_CLOSE_WAIT
;
4322 * If still in FIN_WAIT_1 STATE FIN has not been acked so
4323 * enter the CLOSING state.
4325 case TCPS_FIN_WAIT_1
:
4326 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
4327 struct tcpcb
*, tp
, int32_t, TCPS_CLOSING
);
4328 tp
->t_state
= TCPS_CLOSING
;
4332 * In FIN_WAIT_2 state enter the TIME_WAIT state,
4333 * starting the time-wait timer, turning off the other
4336 case TCPS_FIN_WAIT_2
:
4337 DTRACE_TCP4(state__change
, void, NULL
,
4338 struct inpcb
*, inp
,
4340 int32_t, TCPS_TIME_WAIT
);
4341 tp
->t_state
= TCPS_TIME_WAIT
;
4342 tcp_canceltimers(tp
);
4343 tp
->t_flags
|= TF_ACKNOW
;
4344 if (tp
->t_flagsext
& TF_NOTIMEWAIT
) {
4345 tp
->t_flags
|= TF_CLOSING
;
4347 add_to_time_wait(tp
, 2 * tcp_msl
);
4349 soisdisconnected(so
);
4353 * In TIME_WAIT state restart the 2 MSL time_wait timer.
4355 case TCPS_TIME_WAIT
:
4356 add_to_time_wait(tp
, 2 * tcp_msl
);
4361 if (so
->so_options
& SO_DEBUG
)
4362 tcp_trace(TA_INPUT
, ostate
, tp
, (void *)tcp_saveipgen
,
4367 * Return any desired output.
4369 if (needoutput
|| (tp
->t_flags
& TF_ACKNOW
)) {
4370 (void) tcp_output(tp
);
4373 tcp_check_timer_state(tp
);
4376 tcp_unlock(so
, 1, 0);
4377 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4382 * Generate an ACK dropping incoming segment if it occupies
4383 * sequence space, where the ACK reflects our state.
4385 * We can now skip the test for the RST flag since all
4386 * paths to this code happen after packets containing
4387 * RST have been dropped.
4389 * In the SYN-RECEIVED state, don't send an ACK unless the
4390 * segment we received passes the SYN-RECEIVED ACK test.
4391 * If it fails send a RST. This breaks the loop in the
4392 * "LAND" DoS attack, and also prevents an ACK storm
4393 * between two listening ports that have been sent forged
4394 * SYN segments, each with the source address of the other.
4396 if (tp
->t_state
== TCPS_SYN_RECEIVED
&& (thflags
& TH_ACK
) &&
4397 (SEQ_GT(tp
->snd_una
, th
->th_ack
) ||
4398 SEQ_GT(th
->th_ack
, tp
->snd_max
)) ) {
4399 rstreason
= BANDLIM_RST_OPENPORT
;
4400 IF_TCP_STATINC(ifp
, dospacket
);
4404 if (so
->so_options
& SO_DEBUG
)
4405 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
4409 tp
->t_flags
|= TF_ACKNOW
;
4410 (void) tcp_output(tp
);
4412 /* Don't need to check timer state as we should have done it during tcp_output */
4413 tcp_unlock(so
, 1, 0);
4414 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4416 dropwithresetnosock
:
4420 * Generate a RST, dropping incoming segment.
4421 * Make ACK acceptable to originator of segment.
4422 * Don't bother to respond if destination was broadcast/multicast.
4424 if ((thflags
& TH_RST
) || m
->m_flags
& (M_BCAST
|M_MCAST
))
4428 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) ||
4429 IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
))
4433 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
4434 IN_MULTICAST(ntohl(ip
->ip_src
.s_addr
)) ||
4435 ip
->ip_src
.s_addr
== htonl(INADDR_BROADCAST
) ||
4436 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
))
4438 /* IPv6 anycast check is done at tcp6_input() */
4441 * Perform bandwidth limiting.
4444 if (badport_bandlim(rstreason
) < 0)
4449 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
4450 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
4453 bzero(&tra
, sizeof(tra
));
4454 tra
.ifscope
= ifscope
;
4455 tra
.awdl_unrestricted
= 1;
4456 if (thflags
& TH_ACK
)
4457 /* mtod() below is safe as long as hdr dropping is delayed */
4458 tcp_respond(tp
, mtod(m
, void *), th
, m
, (tcp_seq
)0, th
->th_ack
,
4461 if (thflags
& TH_SYN
)
4463 /* mtod() below is safe as long as hdr dropping is delayed */
4464 tcp_respond(tp
, mtod(m
, void *), th
, m
, th
->th_seq
+tlen
,
4465 (tcp_seq
)0, TH_RST
|TH_ACK
, &tra
);
4467 /* destroy temporarily created socket */
4470 tcp_unlock(so
, 1, 0);
4471 } else if ((inp
!= NULL
) && (nosock
== 0)) {
4472 tcp_unlock(so
, 1, 0);
4474 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4480 * Drop space held by incoming segment and return.
4483 if (tp
== 0 || (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
4484 tcp_trace(TA_DROP
, ostate
, tp
, (void *)tcp_saveipgen
,
4488 /* destroy temporarily created socket */
4491 tcp_unlock(so
, 1, 0);
4493 else if (nosock
== 0) {
4494 tcp_unlock(so
, 1, 0);
4496 KERNEL_DEBUG(DBG_FNC_TCP_INPUT
| DBG_FUNC_END
,0,0,0,0,0);
4501 tcp_dooptions(tp
, cp
, cnt
, th
, to
, input_ifscope
)
4503 * Parse TCP options and place in tcpopt.
4510 unsigned int input_ifscope
;
4515 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
4517 if (opt
== TCPOPT_EOL
)
4519 if (opt
== TCPOPT_NOP
)
4525 if (optlen
< 2 || optlen
> cnt
)
4534 if (optlen
!= TCPOLEN_MAXSEG
)
4536 if (!(th
->th_flags
& TH_SYN
))
4538 bcopy((char *) cp
+ 2, (char *) &mss
, sizeof(mss
));
4543 if (optlen
!= TCPOLEN_WINDOW
)
4545 if (!(th
->th_flags
& TH_SYN
))
4547 to
->to_flags
|= TOF_SCALE
;
4548 tp
->t_flags
|= TF_RCVD_SCALE
;
4549 tp
->requested_s_scale
= min(cp
[2], TCP_MAX_WINSHIFT
);
4552 case TCPOPT_TIMESTAMP
:
4553 if (optlen
!= TCPOLEN_TIMESTAMP
)
4555 to
->to_flags
|= TOF_TS
;
4556 bcopy((char *)cp
+ 2,
4557 (char *)&to
->to_tsval
, sizeof(to
->to_tsval
));
4558 NTOHL(to
->to_tsval
);
4559 bcopy((char *)cp
+ 6,
4560 (char *)&to
->to_tsecr
, sizeof(to
->to_tsecr
));
4561 NTOHL(to
->to_tsecr
);
4563 * A timestamp received in a SYN makes
4564 * it ok to send timestamp requests and replies.
4566 if (th
->th_flags
& TH_SYN
) {
4567 tp
->t_flags
|= TF_RCVD_TSTMP
;
4568 tp
->ts_recent
= to
->to_tsval
;
4569 tp
->ts_recent_age
= tcp_now
;
4572 case TCPOPT_SACK_PERMITTED
:
4574 optlen
!= TCPOLEN_SACK_PERMITTED
)
4576 if (th
->th_flags
& TH_SYN
)
4577 to
->to_flags
|= TOF_SACK
;
4580 if (optlen
<= 2 || (optlen
- 2) % TCPOLEN_SACK
!= 0)
4582 to
->to_nsacks
= (optlen
- 2) / TCPOLEN_SACK
;
4583 to
->to_sacks
= cp
+ 2;
4584 tcpstat
.tcps_sack_rcv_blocks
++;
4589 case TCPOPT_MULTIPATH
:
4590 tcp_do_mptcp_options(tp
, cp
, th
, to
, optlen
);
4595 if (th
->th_flags
& TH_SYN
)
4596 tcp_mss(tp
, mss
, input_ifscope
); /* sets t_maxseg */
4600 * Pull out of band byte out of a segment so
4601 * it doesn't appear in the user's data queue.
4602 * It is still reflected in the segment length for
4603 * sequencing purposes.
4606 tcp_pulloutofband(so
, th
, m
, off
)
4609 register struct mbuf
*m
;
4610 int off
; /* delayed to be droped hdrlen */
4612 int cnt
= off
+ th
->th_urp
- 1;
4615 if (m
->m_len
> cnt
) {
4616 char *cp
= mtod(m
, caddr_t
) + cnt
;
4617 struct tcpcb
*tp
= sototcpcb(so
);
4620 tp
->t_oobflags
|= TCPOOB_HAVEDATA
;
4621 bcopy(cp
+1, cp
, (unsigned)(m
->m_len
- cnt
- 1));
4623 if (m
->m_flags
& M_PKTHDR
)
4632 panic("tcp_pulloutofband");
4636 get_base_rtt(struct tcpcb
*tp
)
4638 uint32_t base_rtt
= 0, i
;
4639 for (i
= 0; i
< N_RTT_BASE
; ++i
) {
4640 if (tp
->rtt_hist
[i
] != 0 &&
4641 (base_rtt
== 0 || tp
->rtt_hist
[i
] < base_rtt
))
4642 base_rtt
= tp
->rtt_hist
[i
];
4647 /* Each value of RTT base represents the minimum RTT seen in a minute.
4648 * We keep upto N_RTT_BASE minutes worth of history.
4651 update_base_rtt(struct tcpcb
*tp
, uint32_t rtt
)
4656 if (++tp
->rtt_count
>= rtt_samples_per_slot
) {
4659 * If the recv side is being throttled, check if the
4660 * current RTT is closer to the base RTT seen in
4661 * first (recent) two slots. If so, unthrottle the stream.
4663 if (tp
->t_flagsext
& TF_RECV_THROTTLE
) {
4664 base_rtt
= min(tp
->rtt_hist
[0], tp
->rtt_hist
[1]);
4665 qdelay
= tp
->t_rttcur
- base_rtt
;
4666 if (qdelay
< target_qdelay
)
4667 tp
->t_flagsext
&= ~(TF_RECV_THROTTLE
);
4669 #endif /* TRAFFIC_MGT */
4671 for (i
= (N_RTT_BASE
-1); i
> 0; --i
) {
4672 tp
->rtt_hist
[i
] = tp
->rtt_hist
[i
-1];
4674 tp
->rtt_hist
[0] = rtt
;
4677 tp
->rtt_hist
[0] = min(tp
->rtt_hist
[0], rtt
);
4682 * If we have a timestamp reply, update smoothed RTT. If no timestamp is
4683 * present but transmit timer is running and timed sequence number was
4684 * acked, update smoothed RTT.
4686 * If timestamps are supported, a receiver can update RTT even if
4687 * there is no outstanding data.
4689 * Some boxes send broken timestamp replies during the SYN+ACK phase,
4690 * ignore timestamps of 0or we could calculate a huge RTT and blow up
4691 * the retransmit timer.
4694 tcp_compute_rtt(struct tcpcb
*tp
, struct tcpopt
*to
, struct tcphdr
*th
)
4696 VERIFY(to
!= NULL
&& th
!= NULL
);
4697 if (((to
->to_flags
& TOF_TS
) != 0) &&
4698 (to
->to_tsecr
!= 0) &&
4699 TSTMP_GEQ(tcp_now
, to
->to_tsecr
)) {
4700 tcp_xmit_timer(tp
, tcp_now
- to
->to_tsecr
,
4701 to
->to_tsecr
, th
->th_ack
);
4702 } else if (tp
->t_rtttime
!= 0 && SEQ_GT(th
->th_ack
, tp
->t_rtseq
)) {
4703 tcp_xmit_timer(tp
, tcp_now
- tp
->t_rtttime
, 0,
4709 * Collect new round-trip time estimate
4710 * and update averages and current timeout.
4713 tcp_xmit_timer(register struct tcpcb
*tp
, int rtt
,
4714 u_int32_t tsecr
, tcp_seq th_ack
)
4718 if (tp
->t_flagsext
& TF_RECOMPUTE_RTT
) {
4719 if (SEQ_GT(th_ack
, tp
->snd_una
) &&
4720 SEQ_LEQ(th_ack
, tp
->snd_max
) &&
4722 TSTMP_GEQ(tsecr
, tp
->t_badrexmt_time
))) {
4724 * We received a new ACk after a
4725 * spurious timeout. Adapt retransmission
4726 * timer as described in rfc 4015.
4728 tp
->t_flagsext
&= ~(TF_RECOMPUTE_RTT
);
4729 tp
->t_badrexmt_time
= 0;
4730 tp
->t_srtt
= max(tp
->t_srtt_prev
, rtt
);
4731 tp
->t_srtt
= tp
->t_srtt
<< TCP_RTT_SHIFT
;
4732 tp
->t_rttvar
= max(tp
->t_rttvar_prev
, (rtt
>> 1));
4733 tp
->t_rttvar
= tp
->t_rttvar
<< TCP_RTTVAR_SHIFT
;
4735 if (tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
4736 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
4744 tcpstat
.tcps_rttupdated
++;
4749 update_base_rtt(tp
, rtt
);
4752 if (tp
->t_srtt
!= 0) {
4754 * srtt is stored as fixed point with 5 bits after the
4755 * binary point (i.e., scaled by 32). The following magic
4756 * is equivalent to the smoothing algorithm in rfc793 with
4757 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
4760 * Freebsd adjusts rtt to origin 0 by subtracting 1
4761 * from the provided rtt value. This was required because
4762 * of the way t_rtttime was initiailised to 1 before.
4763 * Since we changed t_rtttime to be based on
4764 * tcp_now, this extra adjustment is not needed.
4766 delta
= (rtt
<< TCP_DELTA_SHIFT
)
4767 - (tp
->t_srtt
>> (TCP_RTT_SHIFT
- TCP_DELTA_SHIFT
));
4769 if ((tp
->t_srtt
+= delta
) <= 0)
4773 * We accumulate a smoothed rtt variance (actually, a
4774 * smoothed mean difference), then set the retransmit
4775 * timer to smoothed rtt + 4 times the smoothed variance.
4776 * rttvar is stored as fixed point with 4 bits after the
4777 * binary point (scaled by 16). The following is
4778 * equivalent to rfc793 smoothing with an alpha of .75
4779 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
4780 * rfc793's wired-in beta.
4784 delta
-= tp
->t_rttvar
>> (TCP_RTTVAR_SHIFT
- TCP_DELTA_SHIFT
);
4785 if ((tp
->t_rttvar
+= delta
) <= 0)
4787 if (tp
->t_rttbest
== 0 ||
4788 tp
->t_rttbest
> (tp
->t_srtt
+ tp
->t_rttvar
))
4789 tp
->t_rttbest
= tp
->t_srtt
+ tp
->t_rttvar
;
4792 * No rtt measurement yet - use the unsmoothed rtt.
4793 * Set the variance to half the rtt (so our first
4794 * retransmit happens at 3*rtt).
4796 tp
->t_srtt
= rtt
<< TCP_RTT_SHIFT
;
4797 tp
->t_rttvar
= rtt
<< (TCP_RTTVAR_SHIFT
- 1);
4801 nstat_route_rtt(tp
->t_inpcb
->inp_route
.ro_rt
, tp
->t_srtt
,
4808 * the retransmit should happen at rtt + 4 * rttvar.
4809 * Because of the way we do the smoothing, srtt and rttvar
4810 * will each average +1/2 tick of bias. When we compute
4811 * the retransmit timer, we want 1/2 tick of rounding and
4812 * 1 extra tick because of +-1/2 tick uncertainty in the
4813 * firing of the timer. The bias will give us exactly the
4814 * 1.5 tick we need. But, because the bias is
4815 * statistical, we have to test that we don't drop below
4816 * the minimum feasible timer (which is 2 ticks).
4818 TCPT_RANGESET(tp
->t_rxtcur
, TCP_REXMTVAL(tp
),
4819 max(tp
->t_rttmin
, rtt
+ 2), TCPTV_REXMTMAX
,
4820 TCP_ADD_REXMTSLOP(tp
));
4823 * We received an ack for a packet that wasn't retransmitted;
4824 * it is probably safe to discard any error indications we've
4825 * received recently. This isn't quite right, but close enough
4826 * for now (a route might have failed after we sent a segment,
4827 * and the return path might not be symmetrical).
4829 tp
->t_softerror
= 0;
4832 static inline unsigned int
4833 tcp_maxmtu(struct rtentry
*rt
)
4835 unsigned int maxmtu
;
4837 RT_LOCK_ASSERT_HELD(rt
);
4838 if (rt
->rt_rmx
.rmx_mtu
== 0)
4839 maxmtu
= rt
->rt_ifp
->if_mtu
;
4841 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, rt
->rt_ifp
->if_mtu
);
4847 static inline unsigned int
4848 tcp_maxmtu6(struct rtentry
*rt
)
4850 unsigned int maxmtu
;
4851 struct nd_ifinfo
*ndi
;
4853 RT_LOCK_ASSERT_HELD(rt
);
4854 lck_rw_lock_shared(nd_if_rwlock
);
4855 if ((ndi
= ND_IFINFO(rt
->rt_ifp
)) != NULL
&& !ndi
->initialized
)
4858 lck_mtx_lock(&ndi
->lock
);
4859 if (rt
->rt_rmx
.rmx_mtu
== 0)
4860 maxmtu
= IN6_LINKMTU(rt
->rt_ifp
);
4862 maxmtu
= MIN(rt
->rt_rmx
.rmx_mtu
, IN6_LINKMTU(rt
->rt_ifp
));
4864 lck_mtx_unlock(&ndi
->lock
);
4865 lck_rw_done(nd_if_rwlock
);
4872 * Determine a reasonable value for maxseg size.
4873 * If the route is known, check route for mtu.
4874 * If none, use an mss that can be handled on the outgoing
4875 * interface without forcing IP to fragment; if bigger than
4876 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
4877 * to utilize large mbufs. If no route is found, route has no mtu,
4878 * or the destination isn't local, use a default, hopefully conservative
4879 * size (usually 512 or the default IP max size, but no more than the mtu
4880 * of the interface), as we can't discover anything about intervening
4881 * gateways or networks. We also initialize the congestion/slow start
4882 * window. While looking at the routing entry, we also initialize
4883 * other path-dependent parameters from pre-set or cached values
4884 * in the routing entry.
4886 * Also take into account the space needed for options that we
4887 * send regularly. Make maxseg shorter by that amount to assure
4888 * that we can send maxseg amount of data even when the options
4889 * are present. Store the upper limit of the length of options plus
4892 * NOTE that this routine is only called when we process an incoming
4893 * segment, for outgoing segments only tcp_mssopt is called.
4897 tcp_mss(tp
, offer
, input_ifscope
)
4900 unsigned int input_ifscope
;
4902 register struct rtentry
*rt
;
4904 register int rtt
, mss
;
4908 struct rmxp_tao
*taop
;
4909 int origoffer
= offer
;
4910 u_int32_t sb_max_corrected
;
4919 isipv6
= ((inp
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
4920 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
4921 : sizeof (struct tcpiphdr
);
4923 #define min_protoh (sizeof (struct tcpiphdr))
4928 rt
= tcp_rtlookup6(inp
, input_ifscope
);
4933 rt
= tcp_rtlookup(inp
, input_ifscope
);
4935 isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
4938 tp
->t_maxopd
= tp
->t_maxseg
=
4940 isipv6
? tcp_v6mssdflt
:
4947 * Slower link window correction:
4948 * If a value is specificied for slowlink_wsize use it for
4949 * PPP links believed to be on a serial modem (speed <128Kbps).
4950 * Excludes 9600bps as it is the default value adversized
4951 * by pseudo-devices over ppp.
4953 if (ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
4954 ifp
->if_baudrate
> 9600 && ifp
->if_baudrate
<= 128000) {
4955 tp
->t_flags
|= TF_SLOWLINK
;
4957 so
= inp
->inp_socket
;
4959 taop
= rmx_taop(rt
->rt_rmx
);
4961 * Offer == -1 means that we didn't receive SYN yet,
4962 * use cached value in that case;
4965 offer
= taop
->tao_mssopt
;
4967 * Offer == 0 means that there was no MSS on the SYN segment,
4968 * in this case we use tcp_mssdflt.
4973 isipv6
? tcp_v6mssdflt
:
4978 * Prevent DoS attack with too small MSS. Round up
4979 * to at least minmss.
4981 offer
= max(offer
, tcp_minmss
);
4983 * Sanity check: make sure that maxopd will be large
4984 * enough to allow some data on segments even is the
4985 * all the option space is used (40bytes). Otherwise
4986 * funny things may happen in tcp_output.
4988 offer
= max(offer
, 64);
4990 taop
->tao_mssopt
= offer
;
4993 * While we're here, check if there's an initial rtt
4994 * or rttvar. Convert from the route-table units
4995 * to scaled multiples of the slow timeout timer.
4997 if (tp
->t_srtt
== 0 && (rtt
= rt
->rt_rmx
.rmx_rtt
) != 0) {
4998 tcp_getrt_rtt(tp
, rt
);
5000 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
5004 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5006 mss
= tcp_maxmtu(rt
);
5010 if (rt
->rt_rmx
.rmx_mtu
== 0) {
5014 mss
= min(mss
, tcp_v6mssdflt
);
5018 mss
= min(mss
, tcp_mssdflt
);
5021 mss
= min(mss
, offer
);
5023 * maxopd stores the maximum length of data AND options
5024 * in a segment; maxseg is the amount of data in a normal
5025 * segment. We need to store this value (maxopd) apart
5026 * from maxseg, because now every segment carries options
5027 * and thus we normally have somewhat less data in segments.
5032 * origoffer==-1 indicates, that no segments were received yet.
5033 * In this case we just guess.
5035 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
5037 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
))
5038 mss
-= TCPOLEN_TSTAMP_APPA
;
5041 mss
-= mptcp_adj_mss(tp
, FALSE
);
5046 * Calculate corrected value for sb_max; ensure to upgrade the
5047 * numerator for large sb_max values else it will overflow.
5049 sb_max_corrected
= (sb_max
* (u_int64_t
)MCLBYTES
) / (MSIZE
+ MCLBYTES
);
5052 * If there's a pipesize (ie loopback), change the socket
5053 * buffer to that size only if it's bigger than the current
5054 * sockbuf size. Make the socket buffers an integral
5055 * number of mss units; if the mss is larger than
5056 * the socket buffer, decrease the mss.
5059 bufsize
= rt
->rt_rmx
.rmx_sendpipe
;
5060 if (bufsize
< so
->so_snd
.sb_hiwat
)
5062 bufsize
= so
->so_snd
.sb_hiwat
;
5066 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5067 if (bufsize
> sb_max_corrected
)
5068 bufsize
= sb_max_corrected
;
5069 (void)sbreserve(&so
->so_snd
, bufsize
);
5074 bufsize
= rt
->rt_rmx
.rmx_recvpipe
;
5075 if (bufsize
< so
->so_rcv
.sb_hiwat
)
5077 bufsize
= so
->so_rcv
.sb_hiwat
;
5078 if (bufsize
> mss
) {
5079 bufsize
= (((bufsize
+ (u_int64_t
)mss
- 1) / (u_int64_t
)mss
) * (u_int64_t
)mss
);
5080 if (bufsize
> sb_max_corrected
)
5081 bufsize
= sb_max_corrected
;
5082 (void)sbreserve(&so
->so_rcv
, bufsize
);
5085 set_tcp_stream_priority(so
);
5087 if (rt
->rt_rmx
.rmx_ssthresh
) {
5089 * There's some sort of gateway or interface
5090 * buffer limit on the path. Use this to set
5091 * slow-start threshold, but set the threshold to
5092 * no less than 2*mss.
5094 tp
->snd_ssthresh
= max(2 * mss
, rt
->rt_rmx
.rmx_ssthresh
);
5095 tcpstat
.tcps_usedssthresh
++;
5097 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
5101 * Set the slow-start flight size depending on whether this
5102 * is a local network or not.
5104 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
5105 CC_ALGO(tp
)->cwnd_init(tp
);
5107 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CWND_INIT
);
5109 /* Route locked during lookup above */
5114 * Determine the MSS option to send on an outgoing SYN.
5128 isipv6
= ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) ? 1 : 0;
5129 min_protoh
= isipv6
? sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
)
5130 : sizeof (struct tcpiphdr
);
5132 #define min_protoh (sizeof (struct tcpiphdr))
5137 rt
= tcp_rtlookup6(tp
->t_inpcb
, IFSCOPE_NONE
);
5140 rt
= tcp_rtlookup(tp
->t_inpcb
, IFSCOPE_NONE
);
5144 isipv6
? tcp_v6mssdflt
:
5149 * Slower link window correction:
5150 * If a value is specificied for slowlink_wsize use it for PPP links
5151 * believed to be on a serial modem (speed <128Kbps). Excludes 9600bps as
5152 * it is the default value adversized by pseudo-devices over ppp.
5154 if (rt
->rt_ifp
->if_type
== IFT_PPP
&& slowlink_wsize
> 0 &&
5155 rt
->rt_ifp
->if_baudrate
> 9600 && rt
->rt_ifp
->if_baudrate
<= 128000) {
5156 tp
->t_flags
|= TF_SLOWLINK
;
5160 mss
= (isipv6
? tcp_maxmtu6(rt
) : tcp_maxmtu(rt
));
5162 mss
= tcp_maxmtu(rt
);
5164 /* Route locked during lookup above */
5166 return (mss
- min_protoh
);
5170 * On a partial ack arrives, force the retransmission of the
5171 * next unacknowledged segment. Do not clear tp->t_dupacks.
5172 * By setting snd_nxt to th_ack, this forces retransmission timer to
5176 tcp_newreno_partial_ack(tp
, th
)
5180 tcp_seq onxt
= tp
->snd_nxt
;
5181 u_int32_t ocwnd
= tp
->snd_cwnd
;
5182 tp
->t_timer
[TCPT_REXMT
] = 0;
5183 tp
->t_timer
[TCPT_PTO
] = 0;
5185 tp
->snd_nxt
= th
->th_ack
;
5187 * Set snd_cwnd to one segment beyond acknowledged offset
5188 * (tp->snd_una has not yet been updated when this function
5191 tp
->snd_cwnd
= tp
->t_maxseg
+ BYTES_ACKED(th
, tp
);
5192 tp
->t_flags
|= TF_ACKNOW
;
5193 (void) tcp_output(tp
);
5194 tp
->snd_cwnd
= ocwnd
;
5195 if (SEQ_GT(onxt
, tp
->snd_nxt
))
5198 * Partial window deflation. Relies on fact that tp->snd_una
5201 if (tp
->snd_cwnd
> BYTES_ACKED(th
, tp
))
5202 tp
->snd_cwnd
-= BYTES_ACKED(th
, tp
);
5205 tp
->snd_cwnd
+= tp
->t_maxseg
;
5210 * Drop a random TCP connection that hasn't been serviced yet and
5211 * is eligible for discard. There is a one in qlen chance that
5212 * we will return a null, saying that there are no dropable
5213 * requests. In this case, the protocol specific code should drop
5214 * the new request. This insures fairness.
5216 * The listening TCP socket "head" must be locked
5219 tcp_dropdropablreq(struct socket
*head
)
5221 struct socket
*so
, *sonext
;
5222 unsigned int i
, j
, qlen
;
5223 static u_int32_t rnd
= 0;
5224 static u_int64_t old_runtime
;
5225 static unsigned int cur_cnt
, old_cnt
;
5227 struct inpcb
*inp
= NULL
;
5230 if ((head
->so_options
& SO_ACCEPTCONN
) == 0)
5233 if (TAILQ_EMPTY(&head
->so_incomp
))
5237 * Check if there is any socket in the incomp queue
5238 * that is closed because of a reset from the peer and is
5239 * waiting to be garbage collected. If so, pick that as
5242 TAILQ_FOREACH_SAFE(so
, &head
->so_incomp
, so_list
, sonext
) {
5243 inp
= sotoinpcb(so
);
5244 tp
= intotcpcb(inp
);
5245 if (tp
!= NULL
&& tp
->t_state
== TCPS_CLOSED
&&
5246 so
->so_head
!= NULL
&&
5247 (so
->so_state
& (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) ==
5248 (SS_INCOMP
|SS_CANTSENDMORE
|SS_CANTRCVMORE
)) {
5250 * The listen socket is already locked but we
5251 * can lock this socket here without lock ordering
5252 * issues because it is in the incomp queue and
5253 * is not visible to others.
5255 if (lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
5264 so
= TAILQ_FIRST(&head
->so_incomp
);
5266 now_sec
= net_uptime();
5267 if ((i
= (now_sec
- old_runtime
)) != 0) {
5268 old_runtime
= now_sec
;
5269 old_cnt
= cur_cnt
/ i
;
5274 qlen
= head
->so_incqlen
;
5276 rnd
= RandomULong();
5278 if (++cur_cnt
> qlen
|| old_cnt
> qlen
) {
5279 rnd
= (314159 * rnd
+ 66329) & 0xffff;
5280 j
= ((qlen
+ 1) * rnd
) >> 16;
5283 so
= TAILQ_NEXT(so
, so_list
);
5285 /* Find a connection that is not already closing (or being served) */
5287 inp
= (struct inpcb
*)so
->so_pcb
;
5289 sonext
= TAILQ_NEXT(so
, so_list
);
5291 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0)
5294 * Avoid the issue of a socket being accepted
5295 * by one input thread and being dropped by
5296 * another input thread. If we can't get a hold
5297 * on this mutex, then grab the next socket in
5300 if (lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
5302 if ((so
->so_usecount
== 2) &&
5303 (so
->so_state
& SS_INCOMP
) &&
5304 !(so
->so_flags
& SOF_INCOMP_INPROGRESS
)) {
5308 * don't use if being accepted or
5309 * used in any other way
5311 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
5312 tcp_unlock(so
, 1, 0);
5316 * do not try to lock the inp in
5317 * in_pcb_checkstate because the lock
5318 * is already held in some other thread.
5319 * Only drop the inp_wntcnt reference.
5321 in_pcb_checkstate(inp
, WNT_RELEASE
, 1);
5331 /* Makes sure socket is still in the right state to be discarded */
5333 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
5334 tcp_unlock(so
, 1, 0);
5339 if (so
->so_usecount
!= 2 || !(so
->so_state
& SS_INCOMP
)) {
5340 /* do not discard: that socket is being accepted */
5341 tcp_unlock(so
, 1, 0);
5345 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
5346 tcp_unlock(head
, 0, 0);
5348 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
5350 so
->so_flags
|= SOF_OVERFLOW
;
5354 if (inp
->inp_wantcnt
> 0 && inp
->inp_wantcnt
!= WNT_STOPUSING
) {
5356 * Some one has a wantcnt on this pcb. Since WNT_ACQUIRE
5357 * doesn't require a lock, it could have happened while
5358 * we are holding the lock. This pcb will have to
5359 * be garbage collected later.
5360 * Release the reference held for so_incomp queue
5363 tcp_unlock(so
, 1, 0);
5366 * Unlock this socket and leave the reference on.
5367 * We need to acquire the pcbinfo lock in order to
5368 * fully dispose it off
5370 tcp_unlock(so
, 0, 0);
5372 lck_rw_lock_exclusive(tcbinfo
.ipi_lock
);
5375 /* Release the reference held for so_incomp queue */
5378 if (so
->so_usecount
!= 1 ||
5379 (inp
->inp_wantcnt
> 0 &&
5380 inp
->inp_wantcnt
!= WNT_STOPUSING
)) {
5382 * There is an extra wantcount or usecount
5383 * that must have been added when the socket
5384 * was unlocked. This socket will have to be
5385 * garbage collected later
5387 tcp_unlock(so
, 1, 0);
5390 /* Drop the reference held for this function */
5395 lck_rw_done(tcbinfo
.ipi_lock
);
5397 tcpstat
.tcps_drops
++;
5399 tcp_lock(head
, 0, 0);
5405 /* Set background congestion control on a socket */
5407 tcp_set_background_cc(struct socket
*so
)
5409 tcp_set_new_cc(so
, TCP_CC_ALGO_BACKGROUND_INDEX
);
5412 /* Set foreground congestion control on a socket */
5414 tcp_set_foreground_cc(struct socket
*so
)
5416 if (tcp_use_newreno
)
5417 tcp_set_new_cc(so
, TCP_CC_ALGO_NEWRENO_INDEX
);
5419 tcp_set_new_cc(so
, TCP_CC_ALGO_CUBIC_INDEX
);
5423 tcp_set_new_cc(struct socket
*so
, uint16_t cc_index
)
5425 struct inpcb
*inp
= sotoinpcb(so
);
5426 struct tcpcb
*tp
= intotcpcb(inp
);
5427 u_char old_cc_index
= 0;
5428 if (tp
->tcp_cc_index
!= cc_index
) {
5430 old_cc_index
= tp
->tcp_cc_index
;
5432 if (CC_ALGO(tp
)->cleanup
!= NULL
)
5433 CC_ALGO(tp
)->cleanup(tp
);
5434 tp
->tcp_cc_index
= cc_index
;
5436 tcp_cc_allocate_state(tp
);
5438 if (CC_ALGO(tp
)->switch_to
!= NULL
)
5439 CC_ALGO(tp
)->switch_to(tp
, old_cc_index
);
5441 tcp_ccdbg_trace(tp
, NULL
, TCP_CC_CHANGE_ALGO
);
5446 tcp_set_recv_bg(struct socket
*so
)
5448 if (!IS_TCP_RECV_BG(so
))
5449 so
->so_traffic_mgt_flags
|= TRAFFIC_MGT_TCP_RECVBG
;
5451 /* Unset Large Receive Offload on background sockets */
5452 so_set_lro(so
, SO_TC_BK
);
5456 tcp_clear_recv_bg(struct socket
*so
)
5458 if (IS_TCP_RECV_BG(so
))
5459 so
->so_traffic_mgt_flags
&= ~(TRAFFIC_MGT_TCP_RECVBG
);
5462 * Set/unset use of Large Receive Offload depending on
5465 so_set_lro(so
, so
->so_traffic_class
);
5469 inp_fc_unthrottle_tcp(struct inpcb
*inp
)
5471 struct tcpcb
*tp
= inp
->inp_ppcb
;
5473 * Back off the slow-start threshold and enter
5474 * congestion avoidance phase
5476 if (CC_ALGO(tp
)->pre_fr
!= NULL
)
5477 CC_ALGO(tp
)->pre_fr(tp
);
5479 tp
->snd_cwnd
= tp
->snd_ssthresh
;
5482 * Restart counting for ABC as we changed the
5483 * congestion window just now.
5485 tp
->t_bytes_acked
= 0;
5487 /* Reset retransmit shift as we know that the reason
5488 * for delay in sending a packet is due to flow
5489 * control on the outgoing interface. There is no need
5490 * to backoff retransmit timer.
5495 * Start the output stream again. Since we are
5496 * not retransmitting data, do not reset the
5497 * retransmit timer or rtt calculation.
5503 tcp_getstat SYSCTL_HANDLER_ARGS
5505 #pragma unused(oidp, arg1, arg2)
5509 proc_t caller
= PROC_NULL
;
5510 proc_t caller_parent
= PROC_NULL
;
5511 char command_name
[MAXCOMLEN
+ 1] = "";
5512 char parent_name
[MAXCOMLEN
+ 1] = "";
5514 if ((caller
= proc_self()) != PROC_NULL
) {
5515 /* get process name */
5516 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
5518 /* get parent process name if possible */
5519 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
5520 strlcpy(parent_name
, caller_parent
->p_comm
,
5521 sizeof(parent_name
));
5522 proc_rele(caller_parent
);
5525 if ((escape_str(command_name
, strlen(command_name
),
5526 sizeof(command_name
)) == 0) &&
5527 (escape_str(parent_name
, strlen(parent_name
),
5528 sizeof(parent_name
)) == 0)) {
5529 kern_asl_msg(LOG_DEBUG
, "messagetracer",
5531 "com.apple.message.domain",
5532 "com.apple.kernel.tcpstat", /* 1 */
5533 "com.apple.message.signature",
5535 "com.apple.message.signature2", command_name
, /* 3 */
5536 "com.apple.message.signature3", parent_name
, /* 4 */
5537 "com.apple.message.summarize", "YES", /* 5 */
5541 if (caller
!= PROC_NULL
)
5544 if (req
->oldptr
== 0) {
5545 req
->oldlen
= (size_t)sizeof(struct tcpstat
);
5548 error
= SYSCTL_OUT(req
, &tcpstat
, MIN(sizeof (tcpstat
), req
->oldlen
));
5555 * Checksum extended TCP header and data.
5558 tcp_input_checksum(int af
, struct mbuf
*m
, struct tcphdr
*th
, int off
, int tlen
)
5560 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
5564 struct ip
*ip
= mtod(m
, struct ip
*);
5565 struct ipovly
*ipov
= (struct ipovly
*)ip
;
5567 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
5570 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
5571 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
5572 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
5573 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
5574 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
5576 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
5577 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
5580 * Perform 1's complement adjustment of octets
5581 * that got included/excluded in the hardware-
5582 * calculated checksum value. Ignore cases
5583 * where the value includes or excludes the IP
5584 * header span, as the sum for those octets
5585 * would already be 0xffff and thus no-op.
5587 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
5588 start
!= 0 && (off
- start
) != off
) {
5589 #if BYTE_ORDER != BIG_ENDIAN
5595 /* callee folds in sum */
5596 sum
= m_adj_sum16(m
, start
, off
, sum
);
5597 #if BYTE_ORDER != BIG_ENDIAN
5605 /* callee folds in sum */
5606 th
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
,
5608 sum
+ htonl(tlen
+ IPPROTO_TCP
));
5610 th
->th_sum
^= 0xffff;
5616 bcopy(ipov
->ih_x1
, b
, sizeof (ipov
->ih_x1
));
5617 bzero(ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
5618 ip_sum
= ipov
->ih_len
;
5619 ipov
->ih_len
= (u_short
)tlen
;
5620 #if BYTE_ORDER != BIG_ENDIAN
5621 HTONS(ipov
->ih_len
);
5623 len
= sizeof (struct ip
) + tlen
;
5624 th
->th_sum
= in_cksum(m
, len
);
5625 bcopy(b
, ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
5626 ipov
->ih_len
= ip_sum
;
5628 tcp_in_cksum_stats(len
);
5634 struct ip6_hdr
*ip6
= mtod(m
, struct ip6_hdr
*);
5636 if (m
->m_pkthdr
.pkt_flags
& PKTF_SW_LRO_DID_CSUM
)
5639 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
5640 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
5641 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
5642 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
5643 th
->th_sum
= m
->m_pkthdr
.csum_rx_val
;
5645 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
5646 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
5649 * Perform 1's complement adjustment of octets
5650 * that got included/excluded in the hardware-
5651 * calculated checksum value.
5653 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
5657 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
)) {
5658 s
= ip6
->ip6_src
.s6_addr16
[1];
5659 ip6
->ip6_src
.s6_addr16
[1] = 0 ;
5661 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
)) {
5662 d
= ip6
->ip6_dst
.s6_addr16
[1];
5663 ip6
->ip6_dst
.s6_addr16
[1] = 0;
5666 /* callee folds in sum */
5667 sum
= m_adj_sum16(m
, start
, off
, sum
);
5669 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_src
))
5670 ip6
->ip6_src
.s6_addr16
[1] = s
;
5671 if (IN6_IS_SCOPE_EMBED(&ip6
->ip6_dst
))
5672 ip6
->ip6_dst
.s6_addr16
[1] = d
;
5675 th
->th_sum
= in6_pseudo(
5676 &ip6
->ip6_src
, &ip6
->ip6_dst
,
5677 sum
+ htonl(tlen
+ IPPROTO_TCP
));
5679 th
->th_sum
^= 0xffff;
5681 tcp_in6_cksum_stats(tlen
);
5682 th
->th_sum
= in6_cksum(m
, IPPROTO_TCP
, off
, tlen
);
5692 if (th
->th_sum
!= 0) {
5693 tcpstat
.tcps_rcvbadsum
++;
5694 IF_TCP_STATINC(ifp
, badformat
);
5701 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_STATS
, stats
,
5702 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
5703 tcp_getstat
, "S,tcpstat", "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
5706 sysctl_rexmtthresh SYSCTL_HANDLER_ARGS
5708 #pragma unused(arg1, arg2)
5710 int error
, val
= tcprexmtthresh
;
5712 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
5713 if (error
|| !req
->newptr
)
5717 * Constrain the number of duplicate ACKs
5718 * to consider for TCP fast retransmit
5722 if (val
< 2 || val
> 3)
5725 tcprexmtthresh
= val
;
5730 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, rexmt_thresh
, CTLTYPE_INT
|CTLFLAG_RW
| CTLFLAG_LOCKED
,
5731 &tcprexmtthresh
, 0, &sysctl_rexmtthresh
, "I", "Duplicate ACK Threshold for Fast Retransmit");