2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 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_subr.c 8.2 (Berkeley) 5/24/95
61 * $FreeBSD: src/sys/netinet/tcp_subr.c,v 1.73.2.22 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/callout.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
75 #include <sys/malloc.h>
77 #include <sys/domain.h>
79 #include <sys/kauth.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/protosw.h>
83 #include <sys/random.h>
84 #include <sys/syslog.h>
85 #include <sys/mcache.h>
86 #include <kern/locks.h>
87 #include <kern/zalloc.h>
89 #include <dev/random/randomdev.h>
91 #include <net/route.h>
93 #include <net/content_filter.h>
95 #define tcp_minmssoverload fring
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/ip_icmp.h>
102 #include <netinet/ip6.h>
104 #include <netinet/in_pcb.h>
106 #include <netinet6/in6_pcb.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/icmp_var.h>
112 #include <netinet6/ip6_var.h>
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_fsm.h>
116 #include <netinet/tcp_seq.h>
117 #include <netinet/tcp_timer.h>
118 #include <netinet/tcp_var.h>
119 #include <netinet/tcp_cc.h>
120 #include <netinet/tcp_cache.h>
121 #include <kern/thread_call.h>
124 #include <netinet6/tcp6_var.h>
126 #include <netinet/tcpip.h>
128 #include <netinet/tcp_debug.h>
130 #include <netinet6/ip6protosw.h>
133 #include <netinet6/ipsec.h>
135 #include <netinet6/ipsec6.h>
140 #include <net/necp.h>
143 #undef tcp_minmssoverload
146 #include <security/mac_framework.h>
149 #include <corecrypto/ccaes.h>
150 #include <libkern/crypto/aes.h>
151 #include <libkern/crypto/md5.h>
152 #include <sys/kdebug.h>
153 #include <mach/sdt.h>
155 #include <netinet/lro_ext.h>
157 #define DBG_FNC_TCP_CLOSE NETDBG_CODE(DBG_NETTCP, ((5 << 8) | 2))
159 extern int tcp_lq_overflow
;
161 extern struct tcptimerlist tcp_timer_list
;
162 extern struct tcptailq tcp_tw_tailq
;
164 int tcp_mssdflt
= TCP_MSS
;
165 SYSCTL_INT(_net_inet_tcp
, TCPCTL_MSSDFLT
, mssdflt
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
166 &tcp_mssdflt
, 0, "Default TCP Maximum Segment Size");
169 int tcp_v6mssdflt
= TCP6_MSS
;
170 SYSCTL_INT(_net_inet_tcp
, TCPCTL_V6MSSDFLT
, v6mssdflt
,
171 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_v6mssdflt
, 0,
172 "Default TCP Maximum Segment Size for IPv6");
175 extern int tcp_do_autorcvbuf
;
177 int tcp_sysctl_fastopenkey(struct sysctl_oid
*, void *, int ,
178 struct sysctl_req
*);
179 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, fastopen_key
,
180 CTLTYPE_STRING
| CTLFLAG_WR
,
181 0 , 0, tcp_sysctl_fastopenkey
, "S", "TCP Fastopen key");
183 /* Current count of half-open TFO connections */
184 int tcp_tfo_halfcnt
= 0;
186 /* Maximum of half-open TFO connection backlog */
187 int tcp_tfo_backlog
= 10;
188 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, fastopen_backlog
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
189 &tcp_tfo_backlog
, 0, "Backlog queue for half-open TFO connections");
191 int tcp_fastopen
= TCP_FASTOPEN_CLIENT
| TCP_FASTOPEN_SERVER
;
192 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, fastopen
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
193 &tcp_fastopen
, 0, "Enable TCP Fastopen (RFC 7413)");
195 int tcp_tfo_fallback_min
= 10;
196 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, fastopen_fallback_min
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
197 &tcp_tfo_fallback_min
, 0, "Mininum number of trials without TFO when in fallback mode");
200 * Minimum MSS we accept and use. This prevents DoS attacks where
201 * we are forced to a ridiculous low MSS like 20 and send hundreds
202 * of packets instead of one. The effect scales with the available
203 * bandwidth and quickly saturates the CPU and network interface
204 * with packet generation and sending. Set to zero to disable MINMSS
205 * checking. This setting prevents us from sending too small packets.
207 int tcp_minmss
= TCP_MINMSS
;
208 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, minmss
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
209 &tcp_minmss
, 0, "Minmum TCP Maximum Segment Size");
210 int tcp_do_rfc1323
= 1;
211 #if (DEVELOPMENT || DEBUG)
212 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1323
, rfc1323
,
213 CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_do_rfc1323
, 0,
214 "Enable rfc1323 (high performance TCP) extensions");
215 #endif /* (DEVELOPMENT || DEBUG) */
218 static int tcp_do_rfc1644
= 0;
219 SYSCTL_INT(_net_inet_tcp
, TCPCTL_DO_RFC1644
, rfc1644
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
220 &tcp_do_rfc1644
, 0, "Enable rfc1644 (TTCP) extensions");
222 static int do_tcpdrain
= 0;
223 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, do_tcpdrain
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &do_tcpdrain
, 0,
224 "Enable tcp_drain routine for extra help when low on mbufs");
226 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
227 &tcbinfo
.ipi_count
, 0, "Number of active PCBs");
229 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tw_pcbcount
,
230 CTLFLAG_RD
| CTLFLAG_LOCKED
,
231 &tcbinfo
.ipi_twcount
, 0, "Number of pcbs in time-wait state");
233 static int icmp_may_rst
= 1;
234 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, icmp_may_rst
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &icmp_may_rst
, 0,
235 "Certain ICMP unreachable messages may abort connections in SYN_SENT");
237 static int tcp_strict_rfc1948
= 0;
238 static int tcp_isn_reseed_interval
= 0;
239 #if (DEVELOPMENT || DEBUG)
240 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, strict_rfc1948
,
241 CTLFLAG_RW
| CTLFLAG_LOCKED
,
242 &tcp_strict_rfc1948
, 0, "Determines if RFC1948 is followed exactly");
244 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, isn_reseed_interval
,
245 CTLFLAG_RW
| CTLFLAG_LOCKED
,
246 &tcp_isn_reseed_interval
, 0, "Seconds between reseeding of ISN secret");
247 #endif /* (DEVELOPMENT || DEBUG) */
249 int tcp_TCPTV_MIN
= 100; /* 100ms minimum RTT */
250 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rtt_min
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
251 &tcp_TCPTV_MIN
, 0, "min rtt value allowed");
253 int tcp_rexmt_slop
= TCPTV_REXMTSLOP
;
254 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, rexmt_slop
, CTLFLAG_RW
,
255 &tcp_rexmt_slop
, 0, "Slop added to retransmit timeout");
257 __private_extern__
int tcp_use_randomport
= 0;
258 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, randomize_ports
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
259 &tcp_use_randomport
, 0, "Randomize TCP port numbers");
261 __private_extern__
int tcp_win_scale
= 3;
262 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, win_scale_factor
,
263 CTLFLAG_RW
| CTLFLAG_LOCKED
,
264 &tcp_win_scale
, 0, "Window scaling factor");
266 static void tcp_cleartaocache(void);
267 static void tcp_notify(struct inpcb
*, int);
269 struct zone
*sack_hole_zone
;
270 struct zone
*tcp_reass_zone
;
271 struct zone
*tcp_bwmeas_zone
;
272 struct zone
*tcp_rxt_seg_zone
;
274 extern int slowlink_wsize
; /* window correction for slow links */
275 extern int path_mtu_discovery
;
277 extern u_int32_t tcp_autorcvbuf_max
;
278 extern u_int32_t tcp_autorcvbuf_inc_shift
;
279 static void tcp_sbrcv_grow_rwin(struct tcpcb
*tp
, struct sockbuf
*sb
);
281 #define TCP_BWMEAS_BURST_MINSIZE 6
282 #define TCP_BWMEAS_BURST_MAXSIZE 25
284 static uint32_t bwmeas_elm_size
;
287 * Target size of TCP PCB hash tables. Must be a power of two.
289 * Note that this can be overridden by the kernel environment
290 * variable net.inet.tcp.tcbhashsize
293 #define TCBHASHSIZE CONFIG_TCBHASHSIZE
296 __private_extern__
int tcp_tcbhashsize
= TCBHASHSIZE
;
297 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, tcbhashsize
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
298 &tcp_tcbhashsize
, 0, "Size of TCP control-block hashtable");
301 * This is the actual shape of what we allocate using the zone
302 * allocator. Doing it this way allows us to protect both structures
303 * using the same generation count, and also eliminates the overhead
304 * of allocating tcpcbs separately. By hiding the structure here,
305 * we avoid changing most of the rest of the code (although it needs
306 * to be changed, eventually, for greater efficiency).
311 struct tcpcb tcb
__attribute__((aligned(ALIGNMENT
)));
315 int get_inpcb_str_size(void);
316 int get_tcp_str_size(void);
318 static void tcpcb_to_otcpcb(struct tcpcb
*, struct otcpcb
*);
320 static lck_attr_t
*tcp_uptime_mtx_attr
= NULL
; /* mutex attributes */
321 static lck_grp_t
*tcp_uptime_mtx_grp
= NULL
; /* mutex group definition */
322 static lck_grp_attr_t
*tcp_uptime_mtx_grp_attr
= NULL
; /* mutex group attributes */
323 int tcp_notsent_lowat_check(struct socket
*so
);
325 static aes_encrypt_ctx tfo_ctx
; /* Crypto-context for TFO */
328 tcp_tfo_gen_cookie(struct inpcb
*inp
, u_char
*out
, size_t blk_size
)
330 u_char in
[CCAES_BLOCK_SIZE
];
332 int isipv6
= inp
->inp_vflag
& INP_IPV6
;
335 VERIFY(blk_size
== CCAES_BLOCK_SIZE
);
337 bzero(&in
[0], CCAES_BLOCK_SIZE
);
338 bzero(&out
[0], CCAES_BLOCK_SIZE
);
342 memcpy(in
, &inp
->in6p_faddr
, sizeof(struct in6_addr
));
345 memcpy(in
, &inp
->inp_faddr
, sizeof(struct in_addr
));
347 aes_encrypt_cbc(in
, NULL
, 1, out
, &tfo_ctx
);
350 __private_extern__
int
351 tcp_sysctl_fastopenkey(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
352 __unused
int arg2
, struct sysctl_req
*req
)
355 /* TFO-key is expressed as a string in hex format (+1 to account for \0 char) */
356 char keystring
[TCP_FASTOPEN_KEYLEN
* 2 + 1];
357 u_int32_t key
[TCP_FASTOPEN_KEYLEN
/ sizeof(u_int32_t
)];
360 /* -1, because newlen is len without the terminating \0 character */
361 if (req
->newlen
!= (sizeof(keystring
) - 1)) {
366 /* sysctl_io_string copies keystring into the oldptr of the sysctl_req.
367 * Make sure everything is zero, to avoid putting garbage in there or
370 bzero(keystring
, sizeof(keystring
));
372 error
= sysctl_io_string(req
, keystring
, sizeof(keystring
), 0, NULL
);
376 for (i
= 0; i
< (TCP_FASTOPEN_KEYLEN
/ sizeof(u_int32_t
)); i
++) {
377 /* We jump over the keystring in 8-character (4 byte in hex) steps */
378 if (sscanf(&keystring
[i
* 8], "%8x", &key
[i
]) != 1) {
384 aes_encrypt_key128((u_char
*)key
, &tfo_ctx
);
390 int get_inpcb_str_size(void)
392 return sizeof(struct inpcb
);
395 int get_tcp_str_size(void)
397 return sizeof(struct tcpcb
);
400 int tcp_freeq(struct tcpcb
*tp
);
402 static int scale_to_powerof2(int size
);
405 * This helper routine returns one of the following scaled value of size:
406 * 1. Rounded down power of two value of size if the size value passed as
407 * argument is not a power of two and the rounded up value overflows.
409 * 2. Rounded up power of two value of size if the size value passed as
410 * argument is not a power of two and the rounded up value does not overflow
412 * 3. Same value as argument size if it is already a power of two.
414 static int scale_to_powerof2(int size
) {
415 /* Handle special case of size = 0 */
416 int ret
= size
? size
: 1;
418 if (!powerof2(ret
)) {
419 while(!powerof2(size
)) {
421 * Clear out least significant
422 * set bit till size is left with
423 * its highest set bit at which point
424 * it is rounded down power of two.
426 size
= size
& (size
-1);
429 /* Check for overflow when rounding up */
430 if (0 == (size
<< 1)) {
443 u_char key
[TCP_FASTOPEN_KEYLEN
];
445 read_random(key
, sizeof(key
));
446 aes_encrypt_key128(key
, &tfo_ctx
);
453 tcp_init(struct protosw
*pp
, struct domain
*dp
)
456 static int tcp_initialized
= 0;
458 struct inpcbinfo
*pcbinfo
;
460 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
469 tcp_keepinit
= TCPTV_KEEP_INIT
;
470 tcp_keepidle
= TCPTV_KEEP_IDLE
;
471 tcp_keepintvl
= TCPTV_KEEPINTVL
;
472 tcp_keepcnt
= TCPTV_KEEPCNT
;
473 tcp_maxpersistidle
= TCPTV_KEEP_IDLE
;
476 microuptime(&tcp_uptime
);
477 read_random(&tcp_now
, sizeof(tcp_now
));
478 tcp_now
= tcp_now
& 0x3fffffff; /* Starts tcp internal clock at a random value */
483 tcbinfo
.ipi_listhead
= &tcb
;
487 * allocate lock group attribute and group for tcp pcb mutexes
489 pcbinfo
->ipi_lock_grp_attr
= lck_grp_attr_alloc_init();
490 pcbinfo
->ipi_lock_grp
= lck_grp_alloc_init("tcppcb", pcbinfo
->ipi_lock_grp_attr
);
493 * allocate the lock attribute for tcp pcb mutexes
495 pcbinfo
->ipi_lock_attr
= lck_attr_alloc_init();
497 if ((pcbinfo
->ipi_lock
= lck_rw_alloc_init(pcbinfo
->ipi_lock_grp
,
498 pcbinfo
->ipi_lock_attr
)) == NULL
) {
499 panic("%s: unable to allocate PCB lock\n", __func__
);
503 if (tcp_tcbhashsize
== 0) {
505 tcp_tcbhashsize
= 512;
508 if (!powerof2(tcp_tcbhashsize
)) {
509 int old_hash_size
= tcp_tcbhashsize
;
510 tcp_tcbhashsize
= scale_to_powerof2(tcp_tcbhashsize
);
511 /* Lower limit of 16 */
512 if (tcp_tcbhashsize
< 16) {
513 tcp_tcbhashsize
= 16;
515 printf("WARNING: TCB hash size not a power of 2, "
516 "scaled from %d to %d.\n",
521 tcbinfo
.ipi_hashbase
= hashinit(tcp_tcbhashsize
, M_PCB
, &tcbinfo
.ipi_hashmask
);
522 tcbinfo
.ipi_porthashbase
= hashinit(tcp_tcbhashsize
, M_PCB
,
523 &tcbinfo
.ipi_porthashmask
);
524 str_size
= P2ROUNDUP(sizeof(struct inp_tp
), sizeof(u_int64_t
));
525 tcbinfo
.ipi_zone
= zinit(str_size
, 120000*str_size
, 8192, "tcpcb");
526 zone_change(tcbinfo
.ipi_zone
, Z_CALLERACCT
, FALSE
);
527 zone_change(tcbinfo
.ipi_zone
, Z_EXPAND
, TRUE
);
529 tcbinfo
.ipi_gc
= tcp_gc
;
530 tcbinfo
.ipi_timer
= tcp_itimer
;
531 in_pcbinfo_attach(&tcbinfo
);
533 str_size
= P2ROUNDUP(sizeof(struct sackhole
), sizeof(u_int64_t
));
534 sack_hole_zone
= zinit(str_size
, 120000*str_size
, 8192, "sack_hole zone");
535 zone_change(sack_hole_zone
, Z_CALLERACCT
, FALSE
);
536 zone_change(sack_hole_zone
, Z_EXPAND
, TRUE
);
538 str_size
= P2ROUNDUP(sizeof(struct tseg_qent
), sizeof(u_int64_t
));
539 tcp_reass_zone
= zinit(str_size
, (nmbclusters
>> 4) * str_size
,
540 0, "tcp_reass_zone");
541 if (tcp_reass_zone
== NULL
) {
542 panic("%s: failed allocating tcp_reass_zone", __func__
);
545 zone_change(tcp_reass_zone
, Z_CALLERACCT
, FALSE
);
546 zone_change(tcp_reass_zone
, Z_EXPAND
, TRUE
);
548 bwmeas_elm_size
= P2ROUNDUP(sizeof(struct bwmeas
), sizeof(u_int64_t
));
549 tcp_bwmeas_zone
= zinit(bwmeas_elm_size
, (100 * bwmeas_elm_size
), 0, "tcp_bwmeas_zone");
550 if (tcp_bwmeas_zone
== NULL
) {
551 panic("%s: failed allocating tcp_bwmeas_zone", __func__
);
554 zone_change(tcp_bwmeas_zone
, Z_CALLERACCT
, FALSE
);
555 zone_change(tcp_bwmeas_zone
, Z_EXPAND
, TRUE
);
557 str_size
= P2ROUNDUP(sizeof(struct tcp_ccstate
), sizeof(u_int64_t
));
558 tcp_cc_zone
= zinit(str_size
, 20000 * str_size
, 0, "tcp_cc_zone");
559 zone_change(tcp_cc_zone
, Z_CALLERACCT
, FALSE
);
560 zone_change(tcp_cc_zone
, Z_EXPAND
, TRUE
);
562 str_size
= P2ROUNDUP(sizeof(struct tcp_rxt_seg
), sizeof(u_int64_t
));
563 tcp_rxt_seg_zone
= zinit(str_size
, 10000 * str_size
, 0,
565 zone_change(tcp_rxt_seg_zone
, Z_CALLERACCT
, FALSE
);
566 zone_change(tcp_rxt_seg_zone
, Z_EXPAND
, TRUE
);
569 #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
571 #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
573 if (max_protohdr
< TCP_MINPROTOHDR
) {
574 _max_protohdr
= TCP_MINPROTOHDR
;
575 _max_protohdr
= max_protohdr
; /* round it up */
577 if (max_linkhdr
+ max_protohdr
> MCLBYTES
)
579 #undef TCP_MINPROTOHDR
581 /* Initialize time wait and timer lists */
582 TAILQ_INIT(&tcp_tw_tailq
);
584 bzero(&tcp_timer_list
, sizeof(tcp_timer_list
));
585 LIST_INIT(&tcp_timer_list
.lhead
);
587 * allocate lock group attribute, group and attribute for the tcp timer list
589 tcp_timer_list
.mtx_grp_attr
= lck_grp_attr_alloc_init();
590 tcp_timer_list
.mtx_grp
= lck_grp_alloc_init("tcptimerlist", tcp_timer_list
.mtx_grp_attr
);
591 tcp_timer_list
.mtx_attr
= lck_attr_alloc_init();
592 if ((tcp_timer_list
.mtx
= lck_mtx_alloc_init(tcp_timer_list
.mtx_grp
, tcp_timer_list
.mtx_attr
)) == NULL
) {
593 panic("failed to allocate memory for tcp_timer_list.mtx\n");
595 if ((tcp_timer_list
.call
= thread_call_allocate(tcp_run_timerlist
, NULL
)) == NULL
) {
596 panic("failed to allocate call entry 1 in tcp_init\n");
600 * allocate lock group attribute, group and attribute for tcp_uptime_lock
602 tcp_uptime_mtx_grp_attr
= lck_grp_attr_alloc_init();
603 tcp_uptime_mtx_grp
= lck_grp_alloc_init("tcpuptime", tcp_uptime_mtx_grp_attr
);
604 tcp_uptime_mtx_attr
= lck_attr_alloc_init();
605 tcp_uptime_lock
= lck_spin_alloc_init(tcp_uptime_mtx_grp
, tcp_uptime_mtx_attr
);
607 /* Initialize TCP LRO data structures */
610 /* Initialize TCP Cache */
614 * If more than 60 MB of mbuf pool is available, increase the
615 * maximum allowed receive and send socket buffer size.
617 if (nmbclusters
> 30720) {
618 tcp_autorcvbuf_max
= 1024 * 1024;
619 tcp_autosndbuf_max
= 1024 * 1024;
624 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
625 * tcp_template used to store this data in mbufs, but we now recopy it out
626 * of the tcpcb each time to conserve mbufs.
629 tcp_fillheaders(tp
, ip_ptr
, tcp_ptr
)
634 struct inpcb
*inp
= tp
->t_inpcb
;
635 struct tcphdr
*tcp_hdr
= (struct tcphdr
*)tcp_ptr
;
638 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
641 ip6
= (struct ip6_hdr
*)ip_ptr
;
642 ip6
->ip6_flow
= (ip6
->ip6_flow
& ~IPV6_FLOWINFO_MASK
) |
643 (inp
->inp_flow
& IPV6_FLOWINFO_MASK
);
644 ip6
->ip6_vfc
= (ip6
->ip6_vfc
& ~IPV6_VERSION_MASK
) |
645 (IPV6_VERSION
& IPV6_VERSION_MASK
);
646 ip6
->ip6_nxt
= IPPROTO_TCP
;
647 ip6
->ip6_plen
= sizeof(struct tcphdr
);
648 ip6
->ip6_src
= inp
->in6p_laddr
;
649 ip6
->ip6_dst
= inp
->in6p_faddr
;
650 tcp_hdr
->th_sum
= in6_pseudo(&inp
->in6p_laddr
, &inp
->in6p_faddr
,
651 htonl(sizeof (struct tcphdr
) + IPPROTO_TCP
));
655 struct ip
*ip
= (struct ip
*) ip_ptr
;
657 ip
->ip_vhl
= IP_VHL_BORING
;
664 ip
->ip_p
= IPPROTO_TCP
;
665 ip
->ip_src
= inp
->inp_laddr
;
666 ip
->ip_dst
= inp
->inp_faddr
;
667 tcp_hdr
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
668 htons(sizeof(struct tcphdr
) + IPPROTO_TCP
));
671 tcp_hdr
->th_sport
= inp
->inp_lport
;
672 tcp_hdr
->th_dport
= inp
->inp_fport
;
677 tcp_hdr
->th_flags
= 0;
683 * Create template to be used to send tcp packets on a connection.
684 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only
685 * use for this function is in keepalives, which use tcp_respond.
694 m
= m_get(M_DONTWAIT
, MT_HEADER
);
697 m
->m_len
= sizeof(struct tcptemp
);
698 n
= mtod(m
, struct tcptemp
*);
700 tcp_fillheaders(tp
, (void *)&n
->tt_ipgen
, (void *)&n
->tt_t
);
705 * Send a single message to the TCP at address specified by
706 * the given TCP/IP header. If m == 0, then we make a copy
707 * of the tcpiphdr at ti and send directly to the addressed host.
708 * This is used to force keep alive messages out using the TCP
709 * template for a connection. If flags are given then we send
710 * a message back to the TCP which originated the * segment ti,
711 * and discard the mbuf containing it and any other attached mbufs.
713 * In any case the ack and sequence number of the transmitted
714 * segment are as specified by the parameters.
716 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
719 tcp_respond(struct tcpcb
*tp
, void *ipgen
, struct tcphdr
*th
, struct mbuf
*m
,
720 tcp_seq ack
, tcp_seq seq
, int flags
, struct tcp_respond_args
*tra
)
724 struct route
*ro
= 0;
729 struct route_in6
*ro6
= 0;
730 struct route_in6 sro6
;
737 isipv6
= IP_VHL_V(((struct ip
*)ipgen
)->ip_vhl
) == 6;
743 if (!(flags
& TH_RST
)) {
744 win
= tcp_sbspace(tp
);
745 if (win
> (int32_t)TCP_MAXWIN
<< tp
->rcv_scale
)
746 win
= (int32_t)TCP_MAXWIN
<< tp
->rcv_scale
;
750 ro6
= &tp
->t_inpcb
->in6p_route
;
753 ro
= &tp
->t_inpcb
->inp_route
;
758 bzero(ro6
, sizeof *ro6
);
763 bzero(ro
, sizeof *ro
);
767 m
= m_gethdr(M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
771 m
->m_data
+= max_linkhdr
;
774 VERIFY((MHLEN
- max_linkhdr
) >=
775 (sizeof (*ip6
) + sizeof (*nth
)));
776 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
),
777 sizeof(struct ip6_hdr
));
778 ip6
= mtod(m
, struct ip6_hdr
*);
779 nth
= (struct tcphdr
*)(void *)(ip6
+ 1);
783 VERIFY((MHLEN
- max_linkhdr
) >=
784 (sizeof (*ip
) + sizeof (*nth
)));
785 bcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
786 ip
= mtod(m
, struct ip
*);
787 nth
= (struct tcphdr
*)(void *)(ip
+ 1);
789 bcopy((caddr_t
)th
, (caddr_t
)nth
, sizeof(struct tcphdr
));
791 if ((tp
) && (tp
->t_mpflags
& TMPF_RESET
))
792 flags
= (TH_RST
| TH_ACK
);
799 m
->m_data
= (caddr_t
)ipgen
;
800 /* m_len is set later */
802 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
805 /* Expect 32-bit aligned IP on strict-align platforms */
806 IP6_HDR_STRICT_ALIGNMENT_CHECK(ip6
);
807 xchg(ip6
->ip6_dst
, ip6
->ip6_src
, struct in6_addr
);
808 nth
= (struct tcphdr
*)(void *)(ip6
+ 1);
812 /* Expect 32-bit aligned IP on strict-align platforms */
813 IP_HDR_STRICT_ALIGNMENT_CHECK(ip
);
814 xchg(ip
->ip_dst
.s_addr
, ip
->ip_src
.s_addr
, n_long
);
815 nth
= (struct tcphdr
*)(void *)(ip
+ 1);
819 * this is usually a case when an extension header
820 * exists between the IPv6 header and the
823 nth
->th_sport
= th
->th_sport
;
824 nth
->th_dport
= th
->th_dport
;
826 xchg(nth
->th_dport
, nth
->th_sport
, n_short
);
831 ip6
->ip6_plen
= htons((u_short
)(sizeof (struct tcphdr
) +
833 tlen
+= sizeof (struct ip6_hdr
) + sizeof (struct tcphdr
);
837 tlen
+= sizeof (struct tcpiphdr
);
839 ip
->ip_ttl
= ip_defttl
;
842 m
->m_pkthdr
.len
= tlen
;
843 m
->m_pkthdr
.rcvif
= 0;
845 if (tp
!= NULL
&& tp
->t_inpcb
!= NULL
) {
847 * Packet is associated with a socket, so allow the
848 * label of the response to reflect the socket label.
850 mac_mbuf_label_associate_inpcb(tp
->t_inpcb
, m
);
853 * Packet is not associated with a socket, so possibly
854 * update the label in place.
856 mac_netinet_tcp_reply(m
);
860 nth
->th_seq
= htonl(seq
);
861 nth
->th_ack
= htonl(ack
);
863 nth
->th_off
= sizeof (struct tcphdr
) >> 2;
864 nth
->th_flags
= flags
;
866 nth
->th_win
= htons((u_short
) (win
>> tp
->rcv_scale
));
868 nth
->th_win
= htons((u_short
)win
);
873 nth
->th_sum
= in6_pseudo(&ip6
->ip6_src
, &ip6
->ip6_dst
,
874 htonl((tlen
- sizeof (struct ip6_hdr
)) + IPPROTO_TCP
));
875 m
->m_pkthdr
.csum_flags
= CSUM_TCPIPV6
;
876 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
877 ip6
->ip6_hlim
= in6_selecthlim(tp
? tp
->t_inpcb
: NULL
,
884 nth
->th_sum
= in_pseudo(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
,
885 htons((u_short
)(tlen
- sizeof(struct ip
) + ip
->ip_p
)));
886 m
->m_pkthdr
.csum_flags
= CSUM_TCP
;
887 m
->m_pkthdr
.csum_data
= offsetof(struct tcphdr
, th_sum
);
890 if (tp
== NULL
|| (tp
->t_inpcb
->inp_socket
->so_options
& SO_DEBUG
))
891 tcp_trace(TA_OUTPUT
, 0, tp
, mtod(m
, void *), th
, 0);
895 necp_mark_packet_from_socket(m
, tp
? tp
->t_inpcb
: NULL
, 0, 0);
899 if (tp
!= NULL
&& tp
->t_inpcb
->inp_sp
!= NULL
&&
900 ipsec_setsocket(m
, tp
? tp
->t_inpcb
->inp_socket
: NULL
) != 0) {
907 u_int32_t svc_flags
= 0;
909 svc_flags
|= PKT_SCF_IPV6
;
911 set_packet_service_class(m
, tp
->t_inpcb
->inp_socket
,
912 MBUF_SC_UNSPEC
, svc_flags
);
914 /* Embed flowhash and flow control flags */
915 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
916 m
->m_pkthdr
.pkt_flowid
= tp
->t_inpcb
->inp_flowhash
;
917 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
;
919 /* Disable flow advisory when using MPTCP. */
920 if (!(tp
->t_mpflags
& TMPF_MPTCP_TRUE
))
922 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ADV
;
923 m
->m_pkthdr
.pkt_proto
= IPPROTO_TCP
;
928 struct ip6_out_args ip6oa
= { tra
->ifscope
, { 0 },
929 IP6OAF_SELECT_SRCIF
| IP6OAF_BOUND_SRCADDR
, 0 };
931 if (tra
->ifscope
!= IFSCOPE_NONE
)
932 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
934 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
935 if (tra
->noexpensive
)
936 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
937 if (tra
->awdl_unrestricted
)
938 ip6oa
.ip6oa_flags
|= IP6OAF_AWDL_UNRESTRICTED
;
940 (void) ip6_output(m
, NULL
, ro6
, IPV6_OUTARGS
, NULL
,
943 if (tp
!= NULL
&& ro6
!= NULL
&& ro6
->ro_rt
!= NULL
&&
944 (outif
= ro6
->ro_rt
->rt_ifp
) !=
945 tp
->t_inpcb
->in6p_last_outifp
)
946 tp
->t_inpcb
->in6p_last_outifp
= outif
;
953 struct ip_out_args ipoa
= { tra
->ifscope
, { 0 },
954 IPOAF_SELECT_SRCIF
| IPOAF_BOUND_SRCADDR
, 0 };
956 if (tra
->ifscope
!= IFSCOPE_NONE
)
957 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
959 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
960 if (tra
->noexpensive
)
961 ipoa
.ipoa_flags
|= IPOAF_NO_EXPENSIVE
;
962 if (tra
->awdl_unrestricted
)
963 ipoa
.ipoa_flags
|= IPOAF_AWDL_UNRESTRICTED
;
966 /* Copy the cached route and take an extra reference */
967 inp_route_copyout(tp
->t_inpcb
, &sro
);
970 * For consistency, pass a local route copy.
972 (void) ip_output(m
, NULL
, &sro
, IP_OUTARGS
, NULL
, &ipoa
);
974 if (tp
!= NULL
&& sro
.ro_rt
!= NULL
&&
975 (outif
= sro
.ro_rt
->rt_ifp
) !=
976 tp
->t_inpcb
->inp_last_outifp
)
977 tp
->t_inpcb
->inp_last_outifp
= outif
;
980 /* Synchronize cached PCB route */
981 inp_route_copyin(tp
->t_inpcb
, &sro
);
989 * Create a new TCP control block, making an
990 * empty reassembly queue and hooking it to the argument
991 * protocol control block. The `inp' parameter must have
992 * come from the zone allocator set up in tcp_init().
999 register struct tcpcb
*tp
;
1000 register struct socket
*so
= inp
->inp_socket
;
1002 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
1005 calculate_tcp_clock();
1007 if ((so
->so_flags1
& SOF1_CACHED_IN_SOCK_LAYER
) == 0) {
1008 it
= (struct inp_tp
*)(void *)inp
;
1011 tp
= (struct tcpcb
*)(void *)inp
->inp_saved_ppcb
;
1014 bzero((char *) tp
, sizeof(struct tcpcb
));
1015 LIST_INIT(&tp
->t_segq
);
1016 tp
->t_maxseg
= tp
->t_maxopd
=
1018 isipv6
? tcp_v6mssdflt
:
1023 tp
->t_flags
= (TF_REQ_SCALE
|TF_REQ_TSTMP
);
1025 tp
->t_flagsext
|= TF_SACK_ENABLE
;
1027 TAILQ_INIT(&tp
->snd_holes
);
1028 SLIST_INIT(&tp
->t_rxt_segments
);
1029 tp
->t_inpcb
= inp
; /* XXX */
1031 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
1032 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives
1033 * reasonable initial retransmit time.
1035 tp
->t_srtt
= TCPTV_SRTTBASE
;
1036 tp
->t_rttvar
= ((TCPTV_RTOBASE
- TCPTV_SRTTBASE
) << TCP_RTTVAR_SHIFT
) / 4;
1037 tp
->t_rttmin
= tcp_TCPTV_MIN
;
1038 tp
->t_rxtcur
= TCPTV_RTOBASE
;
1040 if (tcp_use_newreno
)
1041 /* use newreno by default */
1042 tp
->tcp_cc_index
= TCP_CC_ALGO_NEWRENO_INDEX
;
1044 tp
->tcp_cc_index
= TCP_CC_ALGO_CUBIC_INDEX
;
1046 tcp_cc_allocate_state(tp
);
1048 if (CC_ALGO(tp
)->init
!= NULL
)
1049 CC_ALGO(tp
)->init(tp
);
1051 tp
->snd_cwnd
= TCP_CC_CWND_INIT_BYTES
;
1052 tp
->snd_ssthresh
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
1053 tp
->snd_ssthresh_prev
= TCP_MAXWIN
<< TCP_MAX_WINSHIFT
;
1054 tp
->t_rcvtime
= tcp_now
;
1055 tp
->tentry
.timer_start
= tcp_now
;
1056 tp
->t_persist_timeout
= tcp_max_persist_timeout
;
1057 tp
->t_persist_stop
= 0;
1058 tp
->t_flagsext
|= TF_RCVUNACK_WAITSS
;
1059 tp
->t_rexmtthresh
= tcprexmtthresh
;
1061 /* Clear time wait tailq entry */
1062 tp
->t_twentry
.tqe_next
= NULL
;
1063 tp
->t_twentry
.tqe_prev
= NULL
;
1066 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1067 * because the socket may be bound to an IPv6 wildcard address,
1068 * which may match an IPv4-mapped IPv6 address.
1070 inp
->inp_ip_ttl
= ip_defttl
;
1071 inp
->inp_ppcb
= (caddr_t
)tp
;
1072 return (tp
); /* XXX */
1076 * Drop a TCP connection, reporting
1077 * the specified error. If connection is synchronized,
1078 * then send a RST to peer.
1082 register struct tcpcb
*tp
;
1085 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1087 struct inpcb
*inp
= tp
->t_inpcb
;
1090 if (TCPS_HAVERCVDSYN(tp
->t_state
)) {
1091 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
1092 struct tcpcb
*, tp
, int32_t, TCPS_CLOSED
);
1093 tp
->t_state
= TCPS_CLOSED
;
1094 (void) tcp_output(tp
);
1095 tcpstat
.tcps_drops
++;
1097 tcpstat
.tcps_conndrops
++;
1098 if (errno
== ETIMEDOUT
&& tp
->t_softerror
)
1099 errno
= tp
->t_softerror
;
1100 so
->so_error
= errno
;
1101 return (tcp_close(tp
));
1105 tcp_getrt_rtt(struct tcpcb
*tp
, struct rtentry
*rt
)
1107 u_int32_t rtt
= rt
->rt_rmx
.rmx_rtt
;
1108 int isnetlocal
= (tp
->t_flags
& TF_LOCAL
);
1112 * XXX the lock bit for RTT indicates that the value
1113 * is also a minimum value; this is subject to time.
1115 if (rt
->rt_rmx
.rmx_locks
& RTV_RTT
)
1116 tp
->t_rttmin
= rtt
/ (RTM_RTTUNIT
/ TCP_RETRANSHZ
);
1118 tp
->t_rttmin
= isnetlocal
? tcp_TCPTV_MIN
: TCPTV_REXMTMIN
;
1119 tp
->t_srtt
= rtt
/ (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTT_SCALE
));
1120 tcpstat
.tcps_usedrtt
++;
1121 if (rt
->rt_rmx
.rmx_rttvar
) {
1122 tp
->t_rttvar
= rt
->rt_rmx
.rmx_rttvar
/
1123 (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTTVAR_SCALE
));
1124 tcpstat
.tcps_usedrttvar
++;
1126 /* default variation is +- 1 rtt */
1128 tp
->t_srtt
* TCP_RTTVAR_SCALE
/ TCP_RTT_SCALE
;
1130 TCPT_RANGESET(tp
->t_rxtcur
,
1131 ((tp
->t_srtt
>> 2) + tp
->t_rttvar
) >> 1,
1132 tp
->t_rttmin
, TCPTV_REXMTMAX
,
1133 TCP_ADD_REXMTSLOP(tp
));
1138 tcp_update_ecn_perf_stats(struct tcpcb
*tp
,
1139 struct if_tcp_ecn_perf_stat
*stat
)
1141 u_int64_t curval
, oldval
;
1142 struct inpcb
*inp
= tp
->t_inpcb
;
1145 curval
= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1146 if (curval
> 0 && tp
->t_rttupdated
>= 16) {
1147 if (stat
->rtt_avg
== 0) {
1148 stat
->rtt_avg
= curval
;
1150 oldval
= stat
->rtt_avg
;
1152 ((oldval
<< 4) - oldval
+ curval
) >> 4;
1157 curval
= tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
;
1158 if (curval
> 0 && tp
->t_rttupdated
>= 16) {
1159 if (stat
->rtt_var
== 0) {
1160 stat
->rtt_var
= curval
;
1162 oldval
= stat
->rtt_var
;
1164 ((oldval
<< 4) - oldval
+ curval
) >> 4;
1168 /* Percentage of Out-of-order packets, shift by 10 for precision */
1169 curval
= (tp
->t_rcvoopack
<< 10);
1170 if (inp
->inp_stat
!= NULL
&& inp
->inp_stat
->rxpackets
> 0 &&
1172 /* Compute percentage */
1173 curval
= (curval
* 100)/inp
->inp_stat
->rxpackets
;
1174 if (stat
->oo_percent
== 0) {
1175 stat
->oo_percent
= curval
;
1177 oldval
= stat
->oo_percent
;
1179 ((oldval
<< 4) - oldval
+ curval
) >> 4;
1183 /* Total number of SACK recovery episodes */
1184 stat
->sack_episodes
+= tp
->t_sack_recovery_episode
;
1186 /* Percentage of reordered packets, shift by 10 for precision */
1187 curval
= tp
->t_reordered_pkts
+ tp
->t_pawsdrop
+ tp
->t_dsack_sent
+
1189 curval
= curval
<< 10;
1190 if (inp
->inp_stat
!= NULL
&& (inp
->inp_stat
->rxpackets
> 0 ||
1191 inp
->inp_stat
->txpackets
> 0) && curval
> 0) {
1192 /* Compute percentage */
1193 curval
= (curval
* 100) /
1194 (inp
->inp_stat
->rxpackets
+ inp
->inp_stat
->txpackets
);
1195 if (stat
->reorder_percent
== 0) {
1196 stat
->reorder_percent
= curval
;
1198 oldval
= stat
->reorder_percent
;
1199 stat
->reorder_percent
=
1200 ((oldval
<< 4) - oldval
+ curval
) >> 4;
1204 /* Percentage of retransmit bytes, shift by 10 for precision */
1205 curval
= tp
->t_stat
.txretransmitbytes
<< 10;
1206 if (inp
->inp_stat
!= NULL
&& inp
->inp_stat
->txbytes
> 0
1208 curval
= (curval
* 100) / inp
->inp_stat
->txbytes
;
1209 if (stat
->rxmit_percent
== 0) {
1210 stat
->rxmit_percent
= curval
;
1212 oldval
= stat
->rxmit_percent
;
1213 stat
->rxmit_percent
=
1214 ((oldval
<< 4) - oldval
+ curval
) >> 4;
1221 * Close a TCP control block:
1222 * discard all space held by the tcp
1223 * discard internet protocol block
1224 * wake up any sleepers
1228 register struct tcpcb
*tp
;
1230 struct inpcb
*inp
= tp
->t_inpcb
;
1231 struct socket
*so
= inp
->inp_socket
;
1233 int isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
1239 /* tcp_close was called previously, bail */
1240 if (inp
->inp_ppcb
== NULL
)
1243 tcp_canceltimers(tp
);
1244 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_START
, tp
,0,0,0,0);
1247 * If another thread for this tcp is currently in ip (indicated by
1248 * the TF_SENDINPROG flag), defer the cleanup until after it returns
1249 * back to tcp. This is done to serialize the close until after all
1250 * pending output is finished, in order to avoid having the PCB be
1251 * detached and the cached route cleaned, only for ip to cache the
1252 * route back into the PCB again. Note that we've cleared all the
1253 * timers at this point. Set TF_CLOSING to indicate to tcp_output()
1254 * that is should call us again once it returns from ip; at that
1255 * point both flags should be cleared and we can proceed further
1258 if ((tp
->t_flags
& TF_CLOSING
) ||
1259 inp
->inp_sndinprog_cnt
> 0) {
1260 tp
->t_flags
|= TF_CLOSING
;
1264 DTRACE_TCP4(state__change
, void, NULL
, struct inpcb
*, inp
,
1265 struct tcpcb
*, tp
, int32_t, TCPS_CLOSED
);
1268 ro
= (isipv6
? (struct route
*)&inp
->in6p_route
: &inp
->inp_route
);
1270 ro
= &inp
->inp_route
;
1277 * If we got enough samples through the srtt filter,
1278 * save the rtt and rttvar in the routing entry.
1279 * 'Enough' is arbitrarily defined as the 16 samples.
1280 * 16 samples is enough for the srtt filter to converge
1281 * to within 5% of the correct value; fewer samples and
1282 * we could save a very bogus rtt.
1284 * Don't update the default route's characteristics and don't
1285 * update anything that the user "locked".
1287 if (tp
->t_rttupdated
>= 16) {
1288 register u_int32_t i
= 0;
1292 struct sockaddr_in6
*sin6
;
1296 sin6
= (struct sockaddr_in6
*)(void *)rt_key(rt
);
1297 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
1302 if (ROUTE_UNUSABLE(ro
) ||
1303 SIN(rt_key(rt
))->sin_addr
.s_addr
== INADDR_ANY
) {
1304 DTRACE_TCP4(state__change
, void, NULL
,
1305 struct inpcb
*, inp
, struct tcpcb
*, tp
,
1306 int32_t, TCPS_CLOSED
);
1307 tp
->t_state
= TCPS_CLOSED
;
1311 RT_LOCK_ASSERT_HELD(rt
);
1312 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTT
) == 0) {
1314 (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTT_SCALE
));
1315 if (rt
->rt_rmx
.rmx_rtt
&& i
)
1317 * filter this update to half the old & half
1318 * the new values, converting scale.
1319 * See route.h and tcp_var.h for a
1320 * description of the scaling constants.
1322 rt
->rt_rmx
.rmx_rtt
=
1323 (rt
->rt_rmx
.rmx_rtt
+ i
) / 2;
1325 rt
->rt_rmx
.rmx_rtt
= i
;
1326 tcpstat
.tcps_cachedrtt
++;
1328 if ((rt
->rt_rmx
.rmx_locks
& RTV_RTTVAR
) == 0) {
1330 (RTM_RTTUNIT
/ (TCP_RETRANSHZ
* TCP_RTTVAR_SCALE
));
1331 if (rt
->rt_rmx
.rmx_rttvar
&& i
)
1332 rt
->rt_rmx
.rmx_rttvar
=
1333 (rt
->rt_rmx
.rmx_rttvar
+ i
) / 2;
1335 rt
->rt_rmx
.rmx_rttvar
= i
;
1336 tcpstat
.tcps_cachedrttvar
++;
1339 * The old comment here said:
1340 * update the pipelimit (ssthresh) if it has been updated
1341 * already or if a pipesize was specified & the threshhold
1342 * got below half the pipesize. I.e., wait for bad news
1343 * before we start updating, then update on both good
1346 * But we want to save the ssthresh even if no pipesize is
1347 * specified explicitly in the route, because such
1348 * connections still have an implicit pipesize specified
1349 * by the global tcp_sendspace. In the absence of a reliable
1350 * way to calculate the pipesize, it will have to do.
1352 i
= tp
->snd_ssthresh
;
1353 if (rt
->rt_rmx
.rmx_sendpipe
!= 0)
1354 dosavessthresh
= (i
< rt
->rt_rmx
.rmx_sendpipe
/ 2);
1356 dosavessthresh
= (i
< so
->so_snd
.sb_hiwat
/ 2);
1357 if (((rt
->rt_rmx
.rmx_locks
& RTV_SSTHRESH
) == 0 &&
1358 i
!= 0 && rt
->rt_rmx
.rmx_ssthresh
!= 0)
1359 || dosavessthresh
) {
1361 * convert the limit from user data bytes to
1362 * packets then to packet data bytes.
1364 i
= (i
+ tp
->t_maxseg
/ 2) / tp
->t_maxseg
;
1367 i
*= (u_int32_t
)(tp
->t_maxseg
+
1369 (isipv6
? sizeof (struct ip6_hdr
) +
1370 sizeof (struct tcphdr
) :
1372 sizeof (struct tcpiphdr
)
1377 if (rt
->rt_rmx
.rmx_ssthresh
)
1378 rt
->rt_rmx
.rmx_ssthresh
=
1379 (rt
->rt_rmx
.rmx_ssthresh
+ i
) / 2;
1381 rt
->rt_rmx
.rmx_ssthresh
= i
;
1382 tcpstat
.tcps_cachedssthresh
++;
1387 * Mark route for deletion if no information is cached.
1389 if (rt
!= NULL
&& (so
->so_flags
& SOF_OVERFLOW
) && tcp_lq_overflow
) {
1390 if (!(rt
->rt_rmx
.rmx_locks
& RTV_RTT
) &&
1391 rt
->rt_rmx
.rmx_rtt
== 0) {
1392 rt
->rt_flags
|= RTF_DELCLONE
;
1400 /* free the reassembly queue, if any */
1401 (void) tcp_freeq(tp
);
1403 /* Collect ECN related statistics */
1404 if (tp
->ecn_flags
& TE_SETUPSENT
) {
1405 if (tp
->ecn_flags
& TE_CLIENT_SETUP
) {
1406 INP_INC_IFNET_STAT(inp
, ecn_client_setup
);
1407 if (TCP_ECN_ENABLED(tp
)) {
1408 INP_INC_IFNET_STAT(inp
,
1409 ecn_client_success
);
1410 } else if (tp
->ecn_flags
& TE_LOST_SYN
) {
1411 INP_INC_IFNET_STAT(inp
, ecn_syn_lost
);
1413 INP_INC_IFNET_STAT(inp
,
1414 ecn_peer_nosupport
);
1417 INP_INC_IFNET_STAT(inp
, ecn_server_setup
);
1418 if (TCP_ECN_ENABLED(tp
)) {
1419 INP_INC_IFNET_STAT(inp
,
1420 ecn_server_success
);
1421 } else if (tp
->ecn_flags
& TE_LOST_SYNACK
) {
1422 INP_INC_IFNET_STAT(inp
,
1425 INP_INC_IFNET_STAT(inp
,
1426 ecn_peer_nosupport
);
1430 if (TCP_ECN_ENABLED(tp
)) {
1431 if (tp
->ecn_flags
& TE_RECV_ECN_CE
) {
1432 tcpstat
.tcps_ecn_conn_recv_ce
++;
1433 INP_INC_IFNET_STAT(inp
, ecn_conn_recv_ce
);
1435 if (tp
->ecn_flags
& TE_RECV_ECN_ECE
) {
1436 tcpstat
.tcps_ecn_conn_recv_ece
++;
1437 INP_INC_IFNET_STAT(inp
, ecn_conn_recv_ece
);
1439 if (tp
->ecn_flags
& (TE_RECV_ECN_CE
| TE_RECV_ECN_ECE
)) {
1440 if (tp
->t_stat
.txretransmitbytes
> 0 ||
1441 tp
->t_stat
.rxoutoforderbytes
> 0) {
1442 tcpstat
.tcps_ecn_conn_pl_ce
++;
1443 INP_INC_IFNET_STAT(inp
, ecn_conn_plce
);
1445 tcpstat
.tcps_ecn_conn_nopl_ce
++;
1446 INP_INC_IFNET_STAT(inp
, ecn_conn_noplce
);
1449 if (tp
->t_stat
.txretransmitbytes
> 0 ||
1450 tp
->t_stat
.rxoutoforderbytes
> 0) {
1451 tcpstat
.tcps_ecn_conn_plnoce
++;
1452 INP_INC_IFNET_STAT(inp
, ecn_conn_plnoce
);
1458 /* Aggregate performance stats */
1459 if (inp
->inp_last_outifp
!= NULL
) {
1460 struct ifnet
*ifp
= inp
->inp_last_outifp
;
1461 ifnet_lock_shared(ifp
);
1462 if ((ifp
->if_refflags
& (IFRF_ATTACHED
| IFRF_DETACHING
)) ==
1464 if (inp
->inp_vflag
& INP_IPV6
) {
1465 if (TCP_ECN_ENABLED(tp
)) {
1466 ifp
->if_ipv6_stat
->timestamp
1468 tcp_update_ecn_perf_stats(tp
,
1469 &ifp
->if_ipv6_stat
->ecn_on
);
1471 ifp
->if_ipv6_stat
->timestamp
1473 tcp_update_ecn_perf_stats(tp
,
1474 &ifp
->if_ipv6_stat
->ecn_off
);
1477 if (TCP_ECN_ENABLED(tp
)) {
1478 ifp
->if_ipv4_stat
->timestamp
1480 tcp_update_ecn_perf_stats(tp
,
1481 &ifp
->if_ipv4_stat
->ecn_on
);
1483 ifp
->if_ipv4_stat
->timestamp
1485 tcp_update_ecn_perf_stats(tp
,
1486 &ifp
->if_ipv4_stat
->ecn_off
);
1490 ifnet_lock_done(ifp
);
1493 tcp_free_sackholes(tp
);
1494 if (tp
->t_bwmeas
!= NULL
) {
1495 tcp_bwmeas_free(tp
);
1497 tcp_rxtseg_clean(tp
);
1498 /* Free the packet list */
1499 if (tp
->t_pktlist_head
!= NULL
)
1500 m_freem_list(tp
->t_pktlist_head
);
1501 TCP_PKTLIST_CLEAR(tp
);
1504 /* Clear MPTCP state */
1505 if ((so
->so_flags
& SOF_MPTCP_TRUE
) ||
1506 (so
->so_flags
& SOF_MP_SUBFLOW
)) {
1507 soevent(so
, (SO_FILT_HINT_LOCKED
| SO_FILT_HINT_DELETEOK
));
1513 if (so
->so_flags1
& SOF1_CACHED_IN_SOCK_LAYER
)
1514 inp
->inp_saved_ppcb
= (caddr_t
) tp
;
1516 tp
->t_state
= TCPS_CLOSED
;
1518 /* Issue a wakeup before detach so that we don't miss
1521 sodisconnectwakeup(so
);
1524 * Clean up any LRO state
1526 if (tp
->t_flagsext
& TF_LRO_OFFLOADED
) {
1527 tcp_lro_remove_state(inp
->inp_laddr
, inp
->inp_faddr
,
1528 inp
->inp_lport
, inp
->inp_fport
);
1529 tp
->t_flagsext
&= ~TF_LRO_OFFLOADED
;
1533 * If this is a socket that does not want to wakeup the device
1534 * for it's traffic, the application might need to know that the
1535 * socket is closed, send a notification.
1537 if ((so
->so_options
& SO_NOWAKEFROMSLEEP
) &&
1538 inp
->inp_state
!= INPCB_STATE_DEAD
&&
1539 !(inp
->inp_flags2
& INP2_TIMEWAIT
))
1540 socket_post_kev_msg_closed(so
);
1542 if (CC_ALGO(tp
)->cleanup
!= NULL
) {
1543 CC_ALGO(tp
)->cleanup(tp
);
1546 if (tp
->t_ccstate
!= NULL
) {
1547 zfree(tcp_cc_zone
, tp
->t_ccstate
);
1548 tp
->t_ccstate
= NULL
;
1550 tp
->tcp_cc_index
= TCP_CC_ALGO_NONE
;
1552 /* Can happen if we close the socket before receiving the third ACK */
1553 if ((tp
->t_tfo_flags
& TFO_F_COOKIE_VALID
)) {
1554 OSDecrementAtomic(&tcp_tfo_halfcnt
);
1556 /* Panic if something has gone terribly wrong. */
1557 VERIFY(tcp_tfo_halfcnt
>= 0);
1559 tp
->t_tfo_flags
&= ~TFO_F_COOKIE_VALID
;
1563 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1569 /* Call soisdisconnected after detach because it might unlock the socket */
1570 soisdisconnected(so
);
1571 tcpstat
.tcps_closed
++;
1572 KERNEL_DEBUG(DBG_FNC_TCP_CLOSE
| DBG_FUNC_END
,
1573 tcpstat
.tcps_closed
, 0, 0, 0, 0);
1582 register struct tseg_qent
*q
;
1585 while((q
= LIST_FIRST(&tp
->t_segq
)) != NULL
) {
1586 LIST_REMOVE(q
, tqe_q
);
1588 zfree(tcp_reass_zone
, q
);
1591 tp
->t_reassqlen
= 0;
1597 * Walk the tcpbs, if existing, and flush the reassembly queue,
1598 * if there is one when do_tcpdrain is enabled
1599 * Also defunct the extended background idle socket
1600 * Do it next time if the pcbinfo lock is in use
1608 if (!lck_rw_try_lock_exclusive(tcbinfo
.ipi_lock
))
1611 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
) {
1612 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) !=
1614 tcp_lock(inp
->inp_socket
, 1, 0);
1615 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1)
1617 /* lost a race, try the next one */
1618 tcp_unlock(inp
->inp_socket
, 1, 0);
1621 tp
= intotcpcb(inp
);
1626 so_drain_extended_bk_idle(inp
->inp_socket
);
1628 tcp_unlock(inp
->inp_socket
, 1, 0);
1631 lck_rw_done(tcbinfo
.ipi_lock
);
1636 * Notify a tcp user of an asynchronous error;
1637 * store error as soft error, but wake up user
1638 * (for now, won't do anything until can select for soft error).
1640 * Do not wake up user since there currently is no mechanism for
1641 * reporting soft errors (yet - a kqueue filter may be added).
1644 tcp_notify(inp
, error
)
1650 if (inp
== NULL
|| (inp
->inp_state
== INPCB_STATE_DEAD
))
1651 return; /* pcb is gone already */
1653 tp
= (struct tcpcb
*)inp
->inp_ppcb
;
1656 * Ignore some errors if we are hooked up.
1657 * If connection hasn't completed, has retransmitted several times,
1658 * and receives a second error, give up now. This is better
1659 * than waiting a long time to establish a connection that
1660 * can never complete.
1662 if (tp
->t_state
== TCPS_ESTABLISHED
&&
1663 (error
== EHOSTUNREACH
|| error
== ENETUNREACH
||
1664 error
== EHOSTDOWN
)) {
1666 } else if (tp
->t_state
< TCPS_ESTABLISHED
&& tp
->t_rxtshift
> 3 &&
1668 tcp_drop(tp
, error
);
1670 tp
->t_softerror
= error
;
1672 wakeup((caddr_t
) &so
->so_timeo
);
1679 tcp_bwmeas_alloc(struct tcpcb
*tp
)
1682 elm
= zalloc(tcp_bwmeas_zone
);
1686 bzero(elm
, bwmeas_elm_size
);
1687 elm
->bw_minsizepkts
= TCP_BWMEAS_BURST_MINSIZE
;
1688 elm
->bw_maxsizepkts
= TCP_BWMEAS_BURST_MAXSIZE
;
1689 elm
->bw_minsize
= elm
->bw_minsizepkts
* tp
->t_maxseg
;
1690 elm
->bw_maxsize
= elm
->bw_maxsizepkts
* tp
->t_maxseg
;
1695 tcp_bwmeas_free(struct tcpcb
* tp
)
1697 zfree(tcp_bwmeas_zone
, tp
->t_bwmeas
);
1698 tp
->t_bwmeas
= NULL
;
1699 tp
->t_flagsext
&= ~(TF_MEASURESNDBW
);
1703 * tcpcb_to_otcpcb copies specific bits of a tcpcb to a otcpcb format.
1704 * The otcpcb data structure is passed to user space and must not change.
1707 tcpcb_to_otcpcb(struct tcpcb
*tp
, struct otcpcb
*otp
)
1709 otp
->t_segq
= (uint32_t)VM_KERNEL_ADDRPERM(tp
->t_segq
.lh_first
);
1710 otp
->t_dupacks
= tp
->t_dupacks
;
1711 otp
->t_timer
[TCPT_REXMT_EXT
] = tp
->t_timer
[TCPT_REXMT
];
1712 otp
->t_timer
[TCPT_PERSIST_EXT
] = tp
->t_timer
[TCPT_PERSIST
];
1713 otp
->t_timer
[TCPT_KEEP_EXT
] = tp
->t_timer
[TCPT_KEEP
];
1714 otp
->t_timer
[TCPT_2MSL_EXT
] = tp
->t_timer
[TCPT_2MSL
];
1715 otp
->t_inpcb
= (_TCPCB_PTR(struct inpcb
*))VM_KERNEL_ADDRPERM(tp
->t_inpcb
);
1716 otp
->t_state
= tp
->t_state
;
1717 otp
->t_flags
= tp
->t_flags
;
1718 otp
->t_force
= (tp
->t_flagsext
& TF_FORCE
) ? 1 : 0;
1719 otp
->snd_una
= tp
->snd_una
;
1720 otp
->snd_max
= tp
->snd_max
;
1721 otp
->snd_nxt
= tp
->snd_nxt
;
1722 otp
->snd_up
= tp
->snd_up
;
1723 otp
->snd_wl1
= tp
->snd_wl1
;
1724 otp
->snd_wl2
= tp
->snd_wl2
;
1727 otp
->rcv_nxt
= tp
->rcv_nxt
;
1728 otp
->rcv_adv
= tp
->rcv_adv
;
1729 otp
->rcv_wnd
= tp
->rcv_wnd
;
1730 otp
->rcv_up
= tp
->rcv_up
;
1731 otp
->snd_wnd
= tp
->snd_wnd
;
1732 otp
->snd_cwnd
= tp
->snd_cwnd
;
1733 otp
->snd_ssthresh
= tp
->snd_ssthresh
;
1734 otp
->t_maxopd
= tp
->t_maxopd
;
1735 otp
->t_rcvtime
= tp
->t_rcvtime
;
1736 otp
->t_starttime
= tp
->t_starttime
;
1737 otp
->t_rtttime
= tp
->t_rtttime
;
1738 otp
->t_rtseq
= tp
->t_rtseq
;
1739 otp
->t_rxtcur
= tp
->t_rxtcur
;
1740 otp
->t_maxseg
= tp
->t_maxseg
;
1741 otp
->t_srtt
= tp
->t_srtt
;
1742 otp
->t_rttvar
= tp
->t_rttvar
;
1743 otp
->t_rxtshift
= tp
->t_rxtshift
;
1744 otp
->t_rttmin
= tp
->t_rttmin
;
1745 otp
->t_rttupdated
= tp
->t_rttupdated
;
1746 otp
->max_sndwnd
= tp
->max_sndwnd
;
1747 otp
->t_softerror
= tp
->t_softerror
;
1748 otp
->t_oobflags
= tp
->t_oobflags
;
1749 otp
->t_iobc
= tp
->t_iobc
;
1750 otp
->snd_scale
= tp
->snd_scale
;
1751 otp
->rcv_scale
= tp
->rcv_scale
;
1752 otp
->request_r_scale
= tp
->request_r_scale
;
1753 otp
->requested_s_scale
= tp
->requested_s_scale
;
1754 otp
->ts_recent
= tp
->ts_recent
;
1755 otp
->ts_recent_age
= tp
->ts_recent_age
;
1756 otp
->last_ack_sent
= tp
->last_ack_sent
;
1757 otp
->cc_send
= tp
->cc_send
;
1758 otp
->cc_recv
= tp
->cc_recv
;
1759 otp
->snd_recover
= tp
->snd_recover
;
1760 otp
->snd_cwnd_prev
= tp
->snd_cwnd_prev
;
1761 otp
->snd_ssthresh_prev
= tp
->snd_ssthresh_prev
;
1762 otp
->t_badrxtwin
= 0;
1766 tcp_pcblist SYSCTL_HANDLER_ARGS
1768 #pragma unused(oidp, arg1, arg2)
1769 int error
, i
= 0, n
;
1770 struct inpcb
*inp
, **inp_list
;
1776 * The process of preparing the TCB list is too time-consuming and
1777 * resource-intensive to repeat twice on every request.
1779 lck_rw_lock_shared(tcbinfo
.ipi_lock
);
1780 if (req
->oldptr
== USER_ADDR_NULL
) {
1781 n
= tcbinfo
.ipi_count
;
1782 req
->oldidx
= 2 * (sizeof xig
)
1783 + (n
+ n
/8) * sizeof(struct xtcpcb
);
1784 lck_rw_done(tcbinfo
.ipi_lock
);
1788 if (req
->newptr
!= USER_ADDR_NULL
) {
1789 lck_rw_done(tcbinfo
.ipi_lock
);
1794 * OK, now we're committed to doing something.
1796 gencnt
= tcbinfo
.ipi_gencnt
;
1797 n
= tcbinfo
.ipi_count
;
1799 bzero(&xig
, sizeof(xig
));
1800 xig
.xig_len
= sizeof xig
;
1802 xig
.xig_gen
= gencnt
;
1803 xig
.xig_sogen
= so_gencnt
;
1804 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1806 lck_rw_done(tcbinfo
.ipi_lock
);
1810 * We are done if there is no pcb
1813 lck_rw_done(tcbinfo
.ipi_lock
);
1817 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1818 if (inp_list
== 0) {
1819 lck_rw_done(tcbinfo
.ipi_lock
);
1823 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
) {
1824 if (inp
->inp_gencnt
<= gencnt
&&
1825 inp
->inp_state
!= INPCB_STATE_DEAD
)
1826 inp_list
[i
++] = inp
;
1830 TAILQ_FOREACH(tp
, &tcp_tw_tailq
, t_twentry
) {
1832 if (inp
->inp_gencnt
<= gencnt
&&
1833 inp
->inp_state
!= INPCB_STATE_DEAD
)
1834 inp_list
[i
++] = inp
;
1841 for (i
= 0; i
< n
; i
++) {
1843 if (inp
->inp_gencnt
<= gencnt
&&
1844 inp
->inp_state
!= INPCB_STATE_DEAD
) {
1848 bzero(&xt
, sizeof(xt
));
1849 xt
.xt_len
= sizeof xt
;
1850 /* XXX should avoid extra copy */
1851 inpcb_to_compat(inp
, &xt
.xt_inp
);
1852 inp_ppcb
= inp
->inp_ppcb
;
1853 if (inp_ppcb
!= NULL
) {
1855 (struct tcpcb
*)(void *)inp_ppcb
,
1858 bzero((char *) &xt
.xt_tp
, sizeof xt
.xt_tp
);
1860 if (inp
->inp_socket
)
1861 sotoxsocket(inp
->inp_socket
, &xt
.xt_socket
);
1862 error
= SYSCTL_OUT(req
, &xt
, sizeof xt
);
1867 * Give the user an updated idea of our state.
1868 * If the generation differs from what we told
1869 * her before, she knows that something happened
1870 * while we were processing this request, and it
1871 * might be necessary to retry.
1873 bzero(&xig
, sizeof(xig
));
1874 xig
.xig_len
= sizeof xig
;
1875 xig
.xig_gen
= tcbinfo
.ipi_gencnt
;
1876 xig
.xig_sogen
= so_gencnt
;
1877 xig
.xig_count
= tcbinfo
.ipi_count
;
1878 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1880 FREE(inp_list
, M_TEMP
);
1881 lck_rw_done(tcbinfo
.ipi_lock
);
1885 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_PCBLIST
, pcblist
,
1886 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1887 tcp_pcblist
, "S,xtcpcb", "List of active TCP connections");
1891 tcpcb_to_xtcpcb64(struct tcpcb
*tp
, struct xtcpcb64
*otp
)
1893 otp
->t_segq
= (uint32_t)VM_KERNEL_ADDRPERM(tp
->t_segq
.lh_first
);
1894 otp
->t_dupacks
= tp
->t_dupacks
;
1895 otp
->t_timer
[TCPT_REXMT_EXT
] = tp
->t_timer
[TCPT_REXMT
];
1896 otp
->t_timer
[TCPT_PERSIST_EXT
] = tp
->t_timer
[TCPT_PERSIST
];
1897 otp
->t_timer
[TCPT_KEEP_EXT
] = tp
->t_timer
[TCPT_KEEP
];
1898 otp
->t_timer
[TCPT_2MSL_EXT
] = tp
->t_timer
[TCPT_2MSL
];
1899 otp
->t_state
= tp
->t_state
;
1900 otp
->t_flags
= tp
->t_flags
;
1901 otp
->t_force
= (tp
->t_flagsext
& TF_FORCE
) ? 1 : 0;
1902 otp
->snd_una
= tp
->snd_una
;
1903 otp
->snd_max
= tp
->snd_max
;
1904 otp
->snd_nxt
= tp
->snd_nxt
;
1905 otp
->snd_up
= tp
->snd_up
;
1906 otp
->snd_wl1
= tp
->snd_wl1
;
1907 otp
->snd_wl2
= tp
->snd_wl2
;
1910 otp
->rcv_nxt
= tp
->rcv_nxt
;
1911 otp
->rcv_adv
= tp
->rcv_adv
;
1912 otp
->rcv_wnd
= tp
->rcv_wnd
;
1913 otp
->rcv_up
= tp
->rcv_up
;
1914 otp
->snd_wnd
= tp
->snd_wnd
;
1915 otp
->snd_cwnd
= tp
->snd_cwnd
;
1916 otp
->snd_ssthresh
= tp
->snd_ssthresh
;
1917 otp
->t_maxopd
= tp
->t_maxopd
;
1918 otp
->t_rcvtime
= tp
->t_rcvtime
;
1919 otp
->t_starttime
= tp
->t_starttime
;
1920 otp
->t_rtttime
= tp
->t_rtttime
;
1921 otp
->t_rtseq
= tp
->t_rtseq
;
1922 otp
->t_rxtcur
= tp
->t_rxtcur
;
1923 otp
->t_maxseg
= tp
->t_maxseg
;
1924 otp
->t_srtt
= tp
->t_srtt
;
1925 otp
->t_rttvar
= tp
->t_rttvar
;
1926 otp
->t_rxtshift
= tp
->t_rxtshift
;
1927 otp
->t_rttmin
= tp
->t_rttmin
;
1928 otp
->t_rttupdated
= tp
->t_rttupdated
;
1929 otp
->max_sndwnd
= tp
->max_sndwnd
;
1930 otp
->t_softerror
= tp
->t_softerror
;
1931 otp
->t_oobflags
= tp
->t_oobflags
;
1932 otp
->t_iobc
= tp
->t_iobc
;
1933 otp
->snd_scale
= tp
->snd_scale
;
1934 otp
->rcv_scale
= tp
->rcv_scale
;
1935 otp
->request_r_scale
= tp
->request_r_scale
;
1936 otp
->requested_s_scale
= tp
->requested_s_scale
;
1937 otp
->ts_recent
= tp
->ts_recent
;
1938 otp
->ts_recent_age
= tp
->ts_recent_age
;
1939 otp
->last_ack_sent
= tp
->last_ack_sent
;
1940 otp
->cc_send
= tp
->cc_send
;
1941 otp
->cc_recv
= tp
->cc_recv
;
1942 otp
->snd_recover
= tp
->snd_recover
;
1943 otp
->snd_cwnd_prev
= tp
->snd_cwnd_prev
;
1944 otp
->snd_ssthresh_prev
= tp
->snd_ssthresh_prev
;
1945 otp
->t_badrxtwin
= 0;
1950 tcp_pcblist64 SYSCTL_HANDLER_ARGS
1952 #pragma unused(oidp, arg1, arg2)
1953 int error
, i
= 0, n
;
1954 struct inpcb
*inp
, **inp_list
;
1960 * The process of preparing the TCB list is too time-consuming and
1961 * resource-intensive to repeat twice on every request.
1963 lck_rw_lock_shared(tcbinfo
.ipi_lock
);
1964 if (req
->oldptr
== USER_ADDR_NULL
) {
1965 n
= tcbinfo
.ipi_count
;
1966 req
->oldidx
= 2 * (sizeof xig
)
1967 + (n
+ n
/8) * sizeof(struct xtcpcb64
);
1968 lck_rw_done(tcbinfo
.ipi_lock
);
1972 if (req
->newptr
!= USER_ADDR_NULL
) {
1973 lck_rw_done(tcbinfo
.ipi_lock
);
1978 * OK, now we're committed to doing something.
1980 gencnt
= tcbinfo
.ipi_gencnt
;
1981 n
= tcbinfo
.ipi_count
;
1983 bzero(&xig
, sizeof(xig
));
1984 xig
.xig_len
= sizeof xig
;
1986 xig
.xig_gen
= gencnt
;
1987 xig
.xig_sogen
= so_gencnt
;
1988 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1990 lck_rw_done(tcbinfo
.ipi_lock
);
1994 * We are done if there is no pcb
1997 lck_rw_done(tcbinfo
.ipi_lock
);
2001 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
2002 if (inp_list
== 0) {
2003 lck_rw_done(tcbinfo
.ipi_lock
);
2007 LIST_FOREACH(inp
, tcbinfo
.ipi_listhead
, inp_list
) {
2008 if (inp
->inp_gencnt
<= gencnt
&&
2009 inp
->inp_state
!= INPCB_STATE_DEAD
)
2010 inp_list
[i
++] = inp
;
2014 TAILQ_FOREACH(tp
, &tcp_tw_tailq
, t_twentry
) {
2016 if (inp
->inp_gencnt
<= gencnt
&&
2017 inp
->inp_state
!= INPCB_STATE_DEAD
)
2018 inp_list
[i
++] = inp
;
2025 for (i
= 0; i
< n
; i
++) {
2027 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
2030 bzero(&xt
, sizeof(xt
));
2031 xt
.xt_len
= sizeof xt
;
2032 inpcb_to_xinpcb64(inp
, &xt
.xt_inpcb
);
2033 xt
.xt_inpcb
.inp_ppcb
= (uint64_t)VM_KERNEL_ADDRPERM(inp
->inp_ppcb
);
2034 if (inp
->inp_ppcb
!= NULL
)
2035 tcpcb_to_xtcpcb64((struct tcpcb
*)inp
->inp_ppcb
, &xt
);
2036 if (inp
->inp_socket
)
2037 sotoxsocket64(inp
->inp_socket
, &xt
.xt_inpcb
.xi_socket
);
2038 error
= SYSCTL_OUT(req
, &xt
, sizeof xt
);
2043 * Give the user an updated idea of our state.
2044 * If the generation differs from what we told
2045 * her before, she knows that something happened
2046 * while we were processing this request, and it
2047 * might be necessary to retry.
2049 bzero(&xig
, sizeof(xig
));
2050 xig
.xig_len
= sizeof xig
;
2051 xig
.xig_gen
= tcbinfo
.ipi_gencnt
;
2052 xig
.xig_sogen
= so_gencnt
;
2053 xig
.xig_count
= tcbinfo
.ipi_count
;
2054 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
2056 FREE(inp_list
, M_TEMP
);
2057 lck_rw_done(tcbinfo
.ipi_lock
);
2061 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, pcblist64
,
2062 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
2063 tcp_pcblist64
, "S,xtcpcb64", "List of active TCP connections");
2067 tcp_pcblist_n SYSCTL_HANDLER_ARGS
2069 #pragma unused(oidp, arg1, arg2)
2072 error
= get_pcblist_n(IPPROTO_TCP
, req
, &tcbinfo
);
2078 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, pcblist_n
,
2079 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
2080 tcp_pcblist_n
, "S,xtcpcb_n", "List of active TCP connections");
2083 __private_extern__
void
2084 tcp_get_ports_used(uint32_t ifindex
, int protocol
, uint32_t flags
,
2087 inpcb_get_ports_used(ifindex
, protocol
, flags
,
2088 bitfield
, &tcbinfo
);
2091 __private_extern__
uint32_t
2092 tcp_count_opportunistic(unsigned int ifindex
, u_int32_t flags
)
2094 return inpcb_count_opportunistic(ifindex
, &tcbinfo
, flags
);
2097 __private_extern__
uint32_t
2098 tcp_find_anypcb_byaddr(struct ifaddr
*ifa
)
2100 return inpcb_find_anypcb_byaddr(ifa
, &tcbinfo
);
2104 tcp_ctlinput(cmd
, sa
, vip
)
2106 struct sockaddr
*sa
;
2109 tcp_seq icmp_tcp_seq
;
2110 struct ip
*ip
= vip
;
2111 struct in_addr faddr
;
2115 void (*notify
)(struct inpcb
*, int) = tcp_notify
;
2117 faddr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
2118 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
2121 if ((unsigned)cmd
>= PRC_NCMDS
)
2124 if (cmd
== PRC_MSGSIZE
)
2125 notify
= tcp_mtudisc
;
2126 else if (icmp_may_rst
&& (cmd
== PRC_UNREACH_ADMIN_PROHIB
||
2127 cmd
== PRC_UNREACH_PORT
) && ip
)
2128 notify
= tcp_drop_syn_sent
;
2129 else if (PRC_IS_REDIRECT(cmd
)) {
2131 notify
= in_rtchange
;
2132 } else if (cmd
== PRC_HOSTDEAD
)
2134 /* Source quench is deprecated */
2135 else if (cmd
== PRC_QUENCH
)
2137 else if (inetctlerrmap
[cmd
] == 0)
2143 icp
= (struct icmp
*)(void *)
2144 ((caddr_t
)ip
- offsetof(struct icmp
, icmp_ip
));
2145 bcopy(((caddr_t
)ip
+ (IP_VHL_HL(ip
->ip_vhl
) << 2)),
2147 inp
= in_pcblookup_hash(&tcbinfo
, faddr
, th
.th_dport
,
2148 ip
->ip_src
, th
.th_sport
, 0, NULL
);
2149 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
2150 tcp_lock(inp
->inp_socket
, 1, 0);
2151 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
2152 tcp_unlock(inp
->inp_socket
, 1, 0);
2155 icmp_tcp_seq
= htonl(th
.th_seq
);
2156 tp
= intotcpcb(inp
);
2157 if (SEQ_GEQ(icmp_tcp_seq
, tp
->snd_una
) &&
2158 SEQ_LT(icmp_tcp_seq
, tp
->snd_max
)) {
2159 if (cmd
== PRC_MSGSIZE
) {
2163 * If we got a needfrag and there is a host route to the
2164 * original destination, and the MTU is not locked, then
2165 * set the MTU in the route to the suggested new value
2166 * (if given) and then notify as usual. The ULPs will
2167 * notice that the MTU has changed and adapt accordingly.
2168 * If no new MTU was suggested, then we guess a new one
2169 * less than the current value. If the new MTU is
2170 * unreasonably small (defined by sysctl tcp_minmss), then
2171 * we reset the MTU to the interface value and enable the
2172 * lock bit, indicating that we are no longer doing MTU
2177 struct sockaddr_in icmpsrc
= { sizeof (struct sockaddr_in
), AF_INET
,
2178 0 , { 0 }, { 0,0,0,0,0,0,0,0 } };
2179 icmpsrc
.sin_addr
= icp
->icmp_ip
.ip_dst
;
2181 rt
= rtalloc1((struct sockaddr
*)&icmpsrc
, 0,
2182 RTF_CLONING
| RTF_PRCLONING
);
2185 if ((rt
->rt_flags
& RTF_HOST
) &&
2186 !(rt
->rt_rmx
.rmx_locks
& RTV_MTU
)) {
2187 mtu
= ntohs(icp
->icmp_nextmtu
);
2189 mtu
= ip_next_mtu(rt
->rt_rmx
.
2192 printf("MTU for %s reduced to %d\n",
2194 &icmpsrc
.sin_addr
, ipv4str
,
2195 sizeof (ipv4str
)), mtu
);
2197 if (mtu
< max(296, (tcp_minmss
+
2198 sizeof (struct tcpiphdr
)))) {
2199 /* rt->rt_rmx.rmx_mtu =
2200 rt->rt_ifp->if_mtu; */
2201 rt
->rt_rmx
.rmx_locks
|= RTV_MTU
;
2202 } else if (rt
->rt_rmx
.rmx_mtu
> mtu
) {
2203 rt
->rt_rmx
.rmx_mtu
= mtu
;
2211 (*notify
)(inp
, inetctlerrmap
[cmd
]);
2213 tcp_unlock(inp
->inp_socket
, 1, 0);
2216 in_pcbnotifyall(&tcbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
2221 tcp6_ctlinput(cmd
, sa
, d
)
2223 struct sockaddr
*sa
;
2227 void (*notify
)(struct inpcb
*, int) = tcp_notify
;
2228 struct ip6_hdr
*ip6
;
2230 struct ip6ctlparam
*ip6cp
= NULL
;
2231 const struct sockaddr_in6
*sa6_src
= NULL
;
2233 struct tcp_portonly
{
2238 if (sa
->sa_family
!= AF_INET6
||
2239 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
2242 if ((unsigned)cmd
>= PRC_NCMDS
)
2245 if (cmd
== PRC_MSGSIZE
)
2246 notify
= tcp_mtudisc
;
2247 else if (!PRC_IS_REDIRECT(cmd
) && (inet6ctlerrmap
[cmd
] == 0))
2249 /* Source quench is deprecated */
2250 else if (cmd
== PRC_QUENCH
)
2253 /* if the parameter is from icmp6, decode it. */
2255 ip6cp
= (struct ip6ctlparam
*)d
;
2257 ip6
= ip6cp
->ip6c_ip6
;
2258 off
= ip6cp
->ip6c_off
;
2259 sa6_src
= ip6cp
->ip6c_src
;
2263 off
= 0; /* fool gcc */
2269 * XXX: We assume that when IPV6 is non NULL,
2270 * M and OFF are valid.
2273 /* check if we can safely examine src and dst ports */
2274 if (m
->m_pkthdr
.len
< off
+ sizeof(*thp
))
2277 bzero(&th
, sizeof(th
));
2278 m_copydata(m
, off
, sizeof(*thp
), (caddr_t
)&th
);
2280 in6_pcbnotify(&tcbinfo
, sa
, th
.th_dport
,
2281 (struct sockaddr
*)ip6cp
->ip6c_src
,
2282 th
.th_sport
, cmd
, NULL
, notify
);
2284 in6_pcbnotify(&tcbinfo
, sa
, 0,
2285 (struct sockaddr
*)(size_t)sa6_src
, 0, cmd
, NULL
, notify
);
2292 * Following is where TCP initial sequence number generation occurs.
2294 * There are two places where we must use initial sequence numbers:
2295 * 1. In SYN-ACK packets.
2296 * 2. In SYN packets.
2298 * The ISNs in SYN-ACK packets have no monotonicity requirement,
2299 * and should be as unpredictable as possible to avoid the possibility
2300 * of spoofing and/or connection hijacking. To satisfy this
2301 * requirement, SYN-ACK ISNs are generated via the arc4random()
2302 * function. If exact RFC 1948 compliance is requested via sysctl,
2303 * these ISNs will be generated just like those in SYN packets.
2305 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
2306 * depends on this property. In addition, these ISNs should be
2307 * unguessable so as to prevent connection hijacking. To satisfy
2308 * the requirements of this situation, the algorithm outlined in
2309 * RFC 1948 is used to generate sequence numbers.
2311 * For more information on the theory of operation, please see
2314 * Implementation details:
2316 * Time is based off the system timer, and is corrected so that it
2317 * increases by one megabyte per second. This allows for proper
2318 * recycling on high speed LANs while still leaving over an hour
2321 * Two sysctls control the generation of ISNs:
2323 * net.inet.tcp.isn_reseed_interval controls the number of seconds
2324 * between seeding of isn_secret. This is normally set to zero,
2325 * as reseeding should not be necessary.
2327 * net.inet.tcp.strict_rfc1948 controls whether RFC 1948 is followed
2328 * strictly. When strict compliance is requested, reseeding is
2329 * disabled and SYN-ACKs will be generated in the same manner as
2330 * SYNs. Strict mode is disabled by default.
2334 #define ISN_BYTES_PER_SECOND 1048576
2340 u_int32_t md5_buffer
[4];
2342 struct timeval timenow
;
2343 u_char isn_secret
[32];
2344 int isn_last_reseed
= 0;
2347 /* Use arc4random for SYN-ACKs when not in exact RFC1948 mode. */
2348 if (((tp
->t_state
== TCPS_LISTEN
) || (tp
->t_state
== TCPS_TIME_WAIT
))
2349 && tcp_strict_rfc1948
== 0)
2351 return RandomULong();
2353 return arc4random();
2355 getmicrotime(&timenow
);
2357 /* Seed if this is the first use, reseed if requested. */
2358 if ((isn_last_reseed
== 0) ||
2359 ((tcp_strict_rfc1948
== 0) && (tcp_isn_reseed_interval
> 0) &&
2360 (((u_int
)isn_last_reseed
+ (u_int
)tcp_isn_reseed_interval
*hz
)
2361 < (u_int
)timenow
.tv_sec
))) {
2363 read_random(&isn_secret
, sizeof(isn_secret
));
2365 read_random_unlimited(&isn_secret
, sizeof(isn_secret
));
2367 isn_last_reseed
= timenow
.tv_sec
;
2370 /* Compute the md5 hash and return the ISN. */
2372 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_fport
, sizeof(u_short
));
2373 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_lport
, sizeof(u_short
));
2375 if ((tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0) {
2376 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_faddr
,
2377 sizeof(struct in6_addr
));
2378 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->in6p_laddr
,
2379 sizeof(struct in6_addr
));
2383 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_faddr
,
2384 sizeof(struct in_addr
));
2385 MD5Update(&isn_ctx
, (u_char
*) &tp
->t_inpcb
->inp_laddr
,
2386 sizeof(struct in_addr
));
2388 MD5Update(&isn_ctx
, (u_char
*) &isn_secret
, sizeof(isn_secret
));
2389 MD5Final((u_char
*) &md5_buffer
, &isn_ctx
);
2390 new_isn
= (tcp_seq
) md5_buffer
[0];
2391 new_isn
+= timenow
.tv_sec
* (ISN_BYTES_PER_SECOND
/ hz
);
2397 * When a specific ICMP unreachable message is received and the
2398 * connection state is SYN-SENT, drop the connection. This behavior
2399 * is controlled by the icmp_may_rst sysctl.
2402 tcp_drop_syn_sent(inp
, errno
)
2406 struct tcpcb
*tp
= intotcpcb(inp
);
2408 if (tp
&& tp
->t_state
== TCPS_SYN_SENT
)
2409 tcp_drop(tp
, errno
);
2413 * When `need fragmentation' ICMP is received, update our idea of the MSS
2414 * based on the new value in the route. Also nudge TCP to send something,
2415 * since we know the packet we just sent was dropped.
2416 * This duplicates some code in the tcp_mss() function in tcp_input.c.
2424 struct tcpcb
*tp
= intotcpcb(inp
);
2426 struct rmxp_tao
*taop
;
2427 struct socket
*so
= inp
->inp_socket
;
2432 int isipv6
= (tp
->t_inpcb
->inp_vflag
& INP_IPV6
) != 0;
2438 rt
= tcp_rtlookup6(inp
, IFSCOPE_NONE
);
2441 rt
= tcp_rtlookup(inp
, IFSCOPE_NONE
);
2442 if (!rt
|| !rt
->rt_rmx
.rmx_mtu
) {
2443 tp
->t_maxopd
= tp
->t_maxseg
=
2445 isipv6
? tcp_v6mssdflt
:
2449 /* Route locked during lookup above */
2454 taop
= rmx_taop(rt
->rt_rmx
);
2455 offered
= taop
->tao_mssopt
;
2456 mtu
= rt
->rt_rmx
.rmx_mtu
;
2458 /* Route locked during lookup above */
2462 // Adjust MTU if necessary.
2463 mtu
= necp_socket_get_effective_mtu(inp
, mtu
);
2469 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
) :
2471 sizeof(struct tcpiphdr
)
2478 mss
= min(mss
, offered
);
2480 * XXX - The above conditional probably violates the TCP
2481 * spec. The problem is that, since we don't know the
2482 * other end's MSS, we are supposed to use a conservative
2483 * default. But, if we do that, then MTU discovery will
2484 * never actually take place, because the conservative
2485 * default is much less than the MTUs typically seen
2486 * on the Internet today. For the moment, we'll sweep
2487 * this under the carpet.
2489 * The conservative default might not actually be a problem
2490 * if the only case this occurs is when sending an initial
2491 * SYN with options and data to a host we've never talked
2492 * to before. Then, they will reply with an MSS value which
2493 * will get recorded and the new parameters should get
2494 * recomputed. For Further Study.
2496 if (tp
->t_maxopd
<= mss
)
2500 if ((tp
->t_flags
& (TF_REQ_TSTMP
|TF_NOOPT
)) == TF_REQ_TSTMP
&&
2501 (tp
->t_flags
& TF_RCVD_TSTMP
) == TF_RCVD_TSTMP
)
2502 mss
-= TCPOLEN_TSTAMP_APPA
;
2505 mss
-= mptcp_adj_mss(tp
, TRUE
);
2507 if (so
->so_snd
.sb_hiwat
< mss
)
2508 mss
= so
->so_snd
.sb_hiwat
;
2513 * Reset the slow-start flight size as it may depends on the new MSS
2515 if (CC_ALGO(tp
)->cwnd_init
!= NULL
)
2516 CC_ALGO(tp
)->cwnd_init(tp
);
2517 tcpstat
.tcps_mturesent
++;
2519 tp
->snd_nxt
= tp
->snd_una
;
2525 * Look-up the routing entry to the peer of this inpcb. If no route
2526 * is found and it cannot be allocated the return NULL. This routine
2527 * is called by TCP routines that access the rmx structure and by tcp_mss
2528 * to get the interface MTU. If a route is found, this routine will
2529 * hold the rtentry lock; the caller is responsible for unlocking.
2532 tcp_rtlookup(inp
, input_ifscope
)
2534 unsigned int input_ifscope
;
2540 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2542 ro
= &inp
->inp_route
;
2543 if ((rt
= ro
->ro_rt
) != NULL
)
2546 if (ROUTE_UNUSABLE(ro
)) {
2552 /* No route yet, so try to acquire one */
2553 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
2554 unsigned int ifscope
;
2556 ro
->ro_dst
.sa_family
= AF_INET
;
2557 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
2558 ((struct sockaddr_in
*)(void *)&ro
->ro_dst
)->sin_addr
=
2562 * If the socket was bound to an interface, then
2563 * the bound-to-interface takes precedence over
2564 * the inbound interface passed in by the caller
2565 * (if we get here as part of the output path then
2566 * input_ifscope is IFSCOPE_NONE).
2568 ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2569 inp
->inp_boundifp
->if_index
: input_ifscope
;
2571 rtalloc_scoped(ro
, ifscope
);
2572 if ((rt
= ro
->ro_rt
) != NULL
)
2577 RT_LOCK_ASSERT_HELD(rt
);
2580 * Update MTU discovery determination. Don't do it if:
2581 * 1) it is disabled via the sysctl
2582 * 2) the route isn't up
2583 * 3) the MTU is locked (if it is, then discovery has been
2587 tp
= intotcpcb(inp
);
2589 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
2590 (!(rt
->rt_flags
& RTF_UP
) || (rt
->rt_rmx
.rmx_locks
& RTV_MTU
))))
2591 tp
->t_flags
&= ~TF_PMTUD
;
2593 tp
->t_flags
|= TF_PMTUD
;
2595 #if CONFIG_IFEF_NOWINDOWSCALE
2596 if (tcp_obey_ifef_nowindowscale
&&
2597 tp
->t_state
== TCPS_SYN_SENT
&& rt
!= NULL
&& rt
->rt_ifp
!= NULL
&&
2598 (rt
->rt_ifp
->if_eflags
& IFEF_NOWINDOWSCALE
)) {
2599 /* Window scaling is enabled on this interface */
2600 tp
->t_flags
&= ~TF_REQ_SCALE
;
2604 if (rt
!= NULL
&& rt
->rt_ifp
!= NULL
) {
2605 somultipages(inp
->inp_socket
,
2606 (rt
->rt_ifp
->if_hwassist
& IFNET_MULTIPAGES
));
2607 tcp_set_tso(tp
, rt
->rt_ifp
);
2608 soif2kcl(inp
->inp_socket
,
2609 (rt
->rt_ifp
->if_eflags
& IFEF_2KCL
));
2610 tcp_set_ecn(tp
, rt
->rt_ifp
);
2613 /* Note if the peer is local */
2614 if (rt
!= NULL
&& !(rt
->rt_ifp
->if_flags
& IFF_POINTOPOINT
) &&
2615 (rt
->rt_gateway
->sa_family
== AF_LINK
||
2616 rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
||
2617 in_localaddr(inp
->inp_faddr
))) {
2618 tp
->t_flags
|= TF_LOCAL
;
2622 * Caller needs to call RT_UNLOCK(rt).
2629 tcp_rtlookup6(inp
, input_ifscope
)
2631 unsigned int input_ifscope
;
2633 struct route_in6
*ro6
;
2637 lck_mtx_assert(rnh_lock
, LCK_MTX_ASSERT_NOTOWNED
);
2639 ro6
= &inp
->in6p_route
;
2640 if ((rt
= ro6
->ro_rt
) != NULL
)
2643 if (ROUTE_UNUSABLE(ro6
)) {
2649 /* No route yet, so try to acquire one */
2650 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
)) {
2651 struct sockaddr_in6
*dst6
;
2652 unsigned int ifscope
;
2654 dst6
= (struct sockaddr_in6
*)&ro6
->ro_dst
;
2655 dst6
->sin6_family
= AF_INET6
;
2656 dst6
->sin6_len
= sizeof(*dst6
);
2657 dst6
->sin6_addr
= inp
->in6p_faddr
;
2660 * If the socket was bound to an interface, then
2661 * the bound-to-interface takes precedence over
2662 * the inbound interface passed in by the caller
2663 * (if we get here as part of the output path then
2664 * input_ifscope is IFSCOPE_NONE).
2666 ifscope
= (inp
->inp_flags
& INP_BOUND_IF
) ?
2667 inp
->inp_boundifp
->if_index
: input_ifscope
;
2669 rtalloc_scoped((struct route
*)ro6
, ifscope
);
2670 if ((rt
= ro6
->ro_rt
) != NULL
)
2675 RT_LOCK_ASSERT_HELD(rt
);
2678 * Update path MTU Discovery determination
2679 * while looking up the route:
2680 * 1) we have a valid route to the destination
2681 * 2) the MTU is not locked (if it is, then discovery has been
2686 tp
= intotcpcb(inp
);
2689 * Update MTU discovery determination. Don't do it if:
2690 * 1) it is disabled via the sysctl
2691 * 2) the route isn't up
2692 * 3) the MTU is locked (if it is, then discovery has been
2696 if (!path_mtu_discovery
|| ((rt
!= NULL
) &&
2697 (!(rt
->rt_flags
& RTF_UP
) || (rt
->rt_rmx
.rmx_locks
& RTV_MTU
))))
2698 tp
->t_flags
&= ~TF_PMTUD
;
2700 tp
->t_flags
|= TF_PMTUD
;
2702 #if CONFIG_IFEF_NOWINDOWSCALE
2703 if (tcp_obey_ifef_nowindowscale
&&
2704 tp
->t_state
== TCPS_SYN_SENT
&& rt
!= NULL
&& rt
->rt_ifp
!= NULL
&&
2705 (rt
->rt_ifp
->if_eflags
& IFEF_NOWINDOWSCALE
)) {
2706 /* Window scaling is not enabled on this interface */
2707 tp
->t_flags
&= ~TF_REQ_SCALE
;
2711 if (rt
!= NULL
&& rt
->rt_ifp
!= NULL
) {
2712 somultipages(inp
->inp_socket
,
2713 (rt
->rt_ifp
->if_hwassist
& IFNET_MULTIPAGES
));
2714 tcp_set_tso(tp
, rt
->rt_ifp
);
2715 soif2kcl(inp
->inp_socket
,
2716 (rt
->rt_ifp
->if_eflags
& IFEF_2KCL
));
2717 tcp_set_ecn(tp
, rt
->rt_ifp
);
2720 /* Note if the peer is local */
2721 if (rt
!= NULL
&& !(rt
->rt_ifp
->if_flags
& IFF_POINTOPOINT
) &&
2722 (IN6_IS_ADDR_LOOPBACK(&inp
->in6p_faddr
) ||
2723 IN6_IS_ADDR_LINKLOCAL(&inp
->in6p_faddr
) ||
2724 rt
->rt_gateway
->sa_family
== AF_LINK
||
2725 in6_localaddr(&inp
->in6p_faddr
))) {
2726 tp
->t_flags
|= TF_LOCAL
;
2730 * Caller needs to call RT_UNLOCK(rt).
2737 /* compute ESP/AH header size for TCP, including outer IP header. */
2739 ipsec_hdrsiz_tcp(tp
)
2747 struct ip6_hdr
*ip6
= NULL
;
2751 if ((tp
== NULL
) || ((inp
= tp
->t_inpcb
) == NULL
))
2753 MGETHDR(m
, M_DONTWAIT
, MT_DATA
); /* MAC-OK */
2758 if ((inp
->inp_vflag
& INP_IPV6
) != 0) {
2759 ip6
= mtod(m
, struct ip6_hdr
*);
2760 th
= (struct tcphdr
*)(void *)(ip6
+ 1);
2761 m
->m_pkthdr
.len
= m
->m_len
=
2762 sizeof(struct ip6_hdr
) + sizeof(struct tcphdr
);
2763 tcp_fillheaders(tp
, ip6
, th
);
2764 hdrsiz
= ipsec6_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
2768 ip
= mtod(m
, struct ip
*);
2769 th
= (struct tcphdr
*)(ip
+ 1);
2770 m
->m_pkthdr
.len
= m
->m_len
= sizeof(struct tcpiphdr
);
2771 tcp_fillheaders(tp
, ip
, th
);
2772 hdrsiz
= ipsec4_hdrsiz(m
, IPSEC_DIR_OUTBOUND
, inp
);
2780 * Return a pointer to the cached information about the remote host.
2781 * The cached information is stored in the protocol specific part of
2782 * the route metrics.
2785 tcp_gettaocache(inp
)
2789 struct rmxp_tao
*taop
;
2792 if ((inp
->inp_vflag
& INP_IPV6
) != 0)
2793 rt
= tcp_rtlookup6(inp
, IFSCOPE_NONE
);
2796 rt
= tcp_rtlookup(inp
, IFSCOPE_NONE
);
2798 /* Make sure this is a host route and is up. */
2800 (rt
->rt_flags
& (RTF_UP
|RTF_HOST
)) != (RTF_UP
|RTF_HOST
)) {
2801 /* Route locked during lookup above */
2807 taop
= rmx_taop(rt
->rt_rmx
);
2808 /* Route locked during lookup above */
2814 * Clear all the TAO cache entries, called from tcp_init.
2817 * This routine is just an empty one, because we assume that the routing
2818 * routing tables are initialized at the same time when TCP, so there is
2819 * nothing in the cache left over.
2827 tcp_lock(struct socket
*so
, int refcount
, void *lr
)
2832 lr_saved
= __builtin_return_address(0);
2836 if (so
->so_pcb
!= NULL
) {
2837 lck_mtx_lock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
2839 panic("tcp_lock: so=%p NO PCB! lr=%p lrh= %s\n",
2840 so
, lr_saved
, solockhistory_nr(so
));
2844 if (so
->so_usecount
< 0) {
2845 panic("tcp_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n",
2846 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
, solockhistory_nr(so
));
2851 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
2852 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
2857 tcp_unlock(struct socket
*so
, int refcount
, void *lr
)
2862 lr_saved
= __builtin_return_address(0);
2866 #ifdef MORE_TCPLOCK_DEBUG
2867 printf("tcp_unlock: so=0x%llx sopcb=0x%llx lock=0x%llx ref=%x "
2868 "lr=0x%llx\n", (uint64_t)VM_KERNEL_ADDRPERM(so
),
2869 (uint64_t)VM_KERNEL_ADDRPERM(so
->so_pcb
),
2870 (uint64_t)VM_KERNEL_ADDRPERM(&(sotoinpcb(so
)->inpcb_mtx
)),
2871 so
->so_usecount
, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved
));
2876 if (so
->so_usecount
< 0) {
2877 panic("tcp_unlock: so=%p usecount=%x lrh= %s\n",
2878 so
, so
->so_usecount
, solockhistory_nr(so
));
2881 if (so
->so_pcb
== NULL
) {
2882 panic("tcp_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n",
2883 so
, so
->so_usecount
, lr_saved
, solockhistory_nr(so
));
2886 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2887 LCK_MTX_ASSERT_OWNED
);
2888 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2889 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2890 lck_mtx_unlock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
2898 __unused
int locktype
)
2900 struct inpcb
*inp
= sotoinpcb(so
);
2903 if (so
->so_usecount
< 0)
2904 panic("tcp_getlock: so=%p usecount=%x lrh= %s\n",
2905 so
, so
->so_usecount
, solockhistory_nr(so
));
2906 return(&inp
->inpcb_mtx
);
2909 panic("tcp_getlock: so=%p NULL so_pcb %s\n",
2910 so
, solockhistory_nr(so
));
2911 return (so
->so_proto
->pr_domain
->dom_mtx
);
2916 * Determine if we can grow the recieve socket buffer to avoid sending
2917 * a zero window update to the peer. We allow even socket buffers that
2918 * have fixed size (set by the application) to grow if the resource
2919 * constraints are met. They will also be trimmed after the application
2923 tcp_sbrcv_grow_rwin(struct tcpcb
*tp
, struct sockbuf
*sb
)
2925 u_int32_t rcvbufinc
= tp
->t_maxseg
<< 4;
2926 u_int32_t rcvbuf
= sb
->sb_hiwat
;
2927 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2930 * If message delivery is enabled, do not count
2931 * unordered bytes in receive buffer towards hiwat
2933 if (so
->so_flags
& SOF_ENABLE_MSGS
)
2934 rcvbuf
= rcvbuf
- so
->so_msg_state
->msg_uno_bytes
;
2936 if (tcp_do_autorcvbuf
== 1 &&
2937 tcp_cansbgrow(sb
) &&
2938 (tp
->t_flags
& TF_SLOWLINK
) == 0 &&
2939 (so
->so_flags1
& SOF1_EXTEND_BK_IDLE_WANTED
) == 0 &&
2940 (rcvbuf
- sb
->sb_cc
) < rcvbufinc
&&
2941 rcvbuf
< tcp_autorcvbuf_max
&&
2942 (sb
->sb_idealsize
> 0 &&
2943 sb
->sb_hiwat
<= (sb
->sb_idealsize
+ rcvbufinc
))) {
2945 min((sb
->sb_hiwat
+ rcvbufinc
), tcp_autorcvbuf_max
));
2950 tcp_sbspace(struct tcpcb
*tp
)
2952 struct sockbuf
*sb
= &tp
->t_inpcb
->inp_socket
->so_rcv
;
2953 u_int32_t rcvbuf
= sb
->sb_hiwat
;
2955 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2956 int32_t pending
= 0;
2959 * If message delivery is enabled, do not count
2960 * unordered bytes in receive buffer towards hiwat mark.
2961 * This value is used to return correct rwnd that does
2962 * not reflect the extra unordered bytes added to the
2963 * receive socket buffer.
2965 if (so
->so_flags
& SOF_ENABLE_MSGS
)
2966 rcvbuf
= rcvbuf
- so
->so_msg_state
->msg_uno_bytes
;
2968 tcp_sbrcv_grow_rwin(tp
, sb
);
2970 space
= ((int32_t) imin((rcvbuf
- sb
->sb_cc
),
2971 (sb
->sb_mbmax
- sb
->sb_mbcnt
)));
2976 /* Compensate for data being processed by content filters */
2977 pending
= cfil_sock_data_space(sb
);
2978 #endif /* CONTENT_FILTER */
2979 if (pending
> space
)
2984 /* Avoid increasing window size if the current window
2985 * is already very low, we could be in "persist" mode and
2986 * we could break some apps (see rdar://5409343)
2989 if (space
< tp
->t_maxseg
)
2992 /* Clip window size for slower link */
2994 if (((tp
->t_flags
& TF_SLOWLINK
) != 0) && slowlink_wsize
> 0 )
2995 return imin(space
, slowlink_wsize
);
3000 * Checks TCP Segment Offloading capability for a given connection
3001 * and interface pair.
3004 tcp_set_tso(struct tcpcb
*tp
, struct ifnet
*ifp
)
3012 * We can't use TSO if this tcpcb belongs to an MPTCP session.
3014 if (tp
->t_mpflags
& TMPF_MPTCP_TRUE
) {
3015 tp
->t_flags
&= ~TF_TSO
;
3021 isipv6
= (inp
->inp_vflag
& INP_IPV6
) != 0;
3024 if (ifp
&& (ifp
->if_hwassist
& IFNET_TSO_IPV6
)) {
3025 tp
->t_flags
|= TF_TSO
;
3026 if (ifp
->if_tso_v6_mtu
!= 0)
3027 tp
->tso_max_segment_size
= ifp
->if_tso_v6_mtu
;
3029 tp
->tso_max_segment_size
= TCP_MAXWIN
;
3031 tp
->t_flags
&= ~TF_TSO
;
3037 if (ifp
&& (ifp
->if_hwassist
& IFNET_TSO_IPV4
)) {
3038 tp
->t_flags
|= TF_TSO
;
3039 if (ifp
->if_tso_v4_mtu
!= 0)
3040 tp
->tso_max_segment_size
= ifp
->if_tso_v4_mtu
;
3042 tp
->tso_max_segment_size
= TCP_MAXWIN
;
3044 tp
->t_flags
&= ~TF_TSO
;
3048 #define TIMEVAL_TO_TCPHZ(_tv_) ((_tv_).tv_sec * TCP_RETRANSHZ + (_tv_).tv_usec / TCP_RETRANSHZ_TO_USEC)
3050 /* Function to calculate the tcp clock. The tcp clock will get updated
3051 * at the boundaries of the tcp layer. This is done at 3 places:
3052 * 1. Right before processing an input tcp packet
3053 * 2. Whenever a connection wants to access the network using tcp_usrreqs
3054 * 3. When a tcp timer fires or before tcp slow timeout
3059 calculate_tcp_clock()
3061 struct timeval tv
= tcp_uptime
;
3062 struct timeval interval
= {0, TCP_RETRANSHZ_TO_USEC
};
3063 struct timeval now
, hold_now
;
3069 * Update coarse-grained networking timestamp (in sec.); the idea
3070 * is to update the counter returnable via net_uptime() when
3073 net_update_uptime_secs(now
.tv_sec
);
3075 timevaladd(&tv
, &interval
);
3076 if (timevalcmp(&now
, &tv
, >)) {
3077 /* time to update the clock */
3078 lck_spin_lock(tcp_uptime_lock
);
3079 if (timevalcmp(&tcp_uptime
, &now
, >=)) {
3080 /* clock got updated while waiting for the lock */
3081 lck_spin_unlock(tcp_uptime_lock
);
3088 timevalsub(&now
, &tv
);
3090 incr
= TIMEVAL_TO_TCPHZ(now
);
3092 tcp_uptime
= hold_now
;
3096 lck_spin_unlock(tcp_uptime_lock
);
3101 /* Compute receive window scaling that we are going to request
3102 * for this connection based on sb_hiwat. Try to leave some
3103 * room to potentially increase the window size upto a maximum
3104 * defined by the constant tcp_autorcvbuf_max.
3107 tcp_set_max_rwinscale(struct tcpcb
*tp
, struct socket
*so
) {
3108 u_int32_t maxsockbufsize
;
3109 if (!tcp_do_rfc1323
) {
3110 tp
->request_r_scale
= 0;
3114 tp
->request_r_scale
= max(tcp_win_scale
, tp
->request_r_scale
);
3115 maxsockbufsize
= ((so
->so_rcv
.sb_flags
& SB_USRSIZE
) != 0) ?
3116 so
->so_rcv
.sb_hiwat
: tcp_autorcvbuf_max
;
3118 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
3119 (TCP_MAXWIN
<< tp
->request_r_scale
) < maxsockbufsize
)
3120 tp
->request_r_scale
++;
3121 tp
->request_r_scale
= min(tp
->request_r_scale
, TCP_MAX_WINSHIFT
);
3126 tcp_notsent_lowat_check(struct socket
*so
) {
3127 struct inpcb
*inp
= sotoinpcb(so
);
3128 struct tcpcb
*tp
= NULL
;
3131 tp
= intotcpcb(inp
);
3134 notsent
= so
->so_snd
.sb_cc
-
3135 (tp
->snd_nxt
- tp
->snd_una
);
3137 /* When we send a FIN or SYN, not_sent can be negative.
3138 * In that case also we need to send a write event to the
3139 * process if it is waiting. In the FIN case, it will
3140 * get an error from send because cantsendmore will be set.
3142 if (notsent
<= tp
->t_notsent_lowat
) {
3146 /* When Nagle's algorithm is not disabled, it is better
3147 * to wakeup the client until there is atleast one
3148 * maxseg of data to write.
3150 if ((tp
->t_flags
& TF_NODELAY
) == 0 &&
3151 notsent
> 0 && notsent
< tp
->t_maxseg
) {
3158 tcp_rxtseg_insert(struct tcpcb
*tp
, tcp_seq start
, tcp_seq end
) {
3159 struct tcp_rxt_seg
*rxseg
= NULL
, *prev
= NULL
, *next
= NULL
;
3160 u_int32_t rxcount
= 0;
3162 if (SLIST_EMPTY(&tp
->t_rxt_segments
))
3163 tp
->t_dsack_lastuna
= tp
->snd_una
;
3165 * First check if there is a segment already existing for this
3169 SLIST_FOREACH(rxseg
, &tp
->t_rxt_segments
, rx_link
) {
3170 if (SEQ_GT(rxseg
->rx_start
, start
))
3176 /* check if prev seg is for this sequence */
3177 if (prev
!= NULL
&& SEQ_LEQ(prev
->rx_start
, start
) &&
3178 SEQ_GEQ(prev
->rx_end
, end
)) {
3184 * There are a couple of possibilities at this point.
3185 * 1. prev overlaps with the beginning of this sequence
3186 * 2. next overlaps with the end of this sequence
3187 * 3. there is no overlap.
3190 if (prev
!= NULL
&& SEQ_GT(prev
->rx_end
, start
)) {
3191 if (prev
->rx_start
== start
&& SEQ_GT(end
, prev
->rx_end
)) {
3192 start
= prev
->rx_end
+ 1;
3195 prev
->rx_end
= (start
- 1);
3196 rxcount
= prev
->rx_count
;
3200 if (next
!= NULL
&& SEQ_LT(next
->rx_start
, end
)) {
3201 if (SEQ_LEQ(next
->rx_end
, end
)) {
3202 end
= next
->rx_start
- 1;
3205 next
->rx_start
= end
+ 1;
3206 rxcount
= next
->rx_count
;
3209 if (!SEQ_LT(start
, end
))
3212 rxseg
= (struct tcp_rxt_seg
*) zalloc(tcp_rxt_seg_zone
);
3213 if (rxseg
== NULL
) {
3216 bzero(rxseg
, sizeof(*rxseg
));
3217 rxseg
->rx_start
= start
;
3218 rxseg
->rx_end
= end
;
3219 rxseg
->rx_count
= rxcount
+ 1;
3222 SLIST_INSERT_AFTER(prev
, rxseg
, rx_link
);
3224 SLIST_INSERT_HEAD(&tp
->t_rxt_segments
, rxseg
, rx_link
);
3229 struct tcp_rxt_seg
*
3230 tcp_rxtseg_find(struct tcpcb
*tp
, tcp_seq start
, tcp_seq end
)
3232 struct tcp_rxt_seg
*rxseg
;
3233 if (SLIST_EMPTY(&tp
->t_rxt_segments
))
3236 SLIST_FOREACH(rxseg
, &tp
->t_rxt_segments
, rx_link
) {
3237 if (SEQ_LEQ(rxseg
->rx_start
, start
) &&
3238 SEQ_GEQ(rxseg
->rx_end
, end
))
3240 if (SEQ_GT(rxseg
->rx_start
, start
))
3247 tcp_rxtseg_clean(struct tcpcb
*tp
)
3249 struct tcp_rxt_seg
*rxseg
, *next
;
3251 SLIST_FOREACH_SAFE(rxseg
, &tp
->t_rxt_segments
, rx_link
, next
) {
3252 SLIST_REMOVE(&tp
->t_rxt_segments
, rxseg
,
3253 tcp_rxt_seg
, rx_link
);
3254 zfree(tcp_rxt_seg_zone
, rxseg
);
3256 tp
->t_dsack_lastuna
= tp
->snd_max
;
3260 tcp_rxtseg_detect_bad_rexmt(struct tcpcb
*tp
, tcp_seq th_ack
)
3262 boolean_t bad_rexmt
;
3263 struct tcp_rxt_seg
*rxseg
;
3265 if (SLIST_EMPTY(&tp
->t_rxt_segments
))
3269 * If all of the segments in this window are not cumulatively
3270 * acknowledged, then there can still be undetected packet loss.
3271 * Do not restore congestion window in that case.
3273 if (SEQ_LT(th_ack
, tp
->snd_recover
))
3277 SLIST_FOREACH(rxseg
, &tp
->t_rxt_segments
, rx_link
) {
3278 if (rxseg
->rx_count
> 1 ||
3279 !(rxseg
->rx_flags
& TCP_RXT_SPURIOUS
)) {
3288 tcp_rxtseg_dsack_for_tlp(struct tcpcb
*tp
)
3290 boolean_t dsack_for_tlp
= FALSE
;
3291 struct tcp_rxt_seg
*rxseg
;
3292 if (SLIST_EMPTY(&tp
->t_rxt_segments
))
3295 SLIST_FOREACH(rxseg
, &tp
->t_rxt_segments
, rx_link
) {
3296 if (rxseg
->rx_count
== 1 &&
3297 SLIST_NEXT(rxseg
,rx_link
) == NULL
&&
3298 (rxseg
->rx_flags
& TCP_RXT_DSACK_FOR_TLP
)) {
3299 dsack_for_tlp
= TRUE
;
3303 return (dsack_for_tlp
);
3307 tcp_rxtseg_total_size(struct tcpcb
*tp
) {
3308 struct tcp_rxt_seg
*rxseg
;
3309 u_int32_t total_size
= 0;
3311 SLIST_FOREACH(rxseg
, &tp
->t_rxt_segments
, rx_link
) {
3312 total_size
+= (rxseg
->rx_end
- rxseg
->rx_start
) + 1;
3314 return (total_size
);
3318 tcp_get_connectivity_status(struct tcpcb
*tp
,
3319 struct tcp_conn_status
*connstatus
)
3321 if (tp
== NULL
|| connstatus
== NULL
)
3323 bzero(connstatus
, sizeof(*connstatus
));
3324 if (tp
->t_rxtshift
>= TCP_CONNECTIVITY_PROBES_MAX
) {
3325 if (TCPS_HAVEESTABLISHED(tp
->t_state
)) {
3326 connstatus
->write_probe_failed
= 1;
3328 connstatus
->conn_probe_failed
= 1;
3331 if (tp
->t_rtimo_probes
>= TCP_CONNECTIVITY_PROBES_MAX
)
3332 connstatus
->read_probe_failed
= 1;
3333 if (tp
->t_inpcb
!= NULL
&& tp
->t_inpcb
->inp_last_outifp
!= NULL
3334 && (tp
->t_inpcb
->inp_last_outifp
->if_eflags
& IFEF_PROBE_CONNECTIVITY
))
3335 connstatus
->probe_activated
= 1;
3340 tfo_enabled(const struct tcpcb
*tp
)
3342 return !!(tp
->t_flagsext
& TF_FASTOPEN
);
3346 tcp_disable_tfo(struct tcpcb
*tp
)
3348 tp
->t_flagsext
&= ~TF_FASTOPEN
;