]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2000-2020 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
39037602 | 5 | * |
2d21ac55 A |
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. | |
39037602 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
39037602 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
39037602 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
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. | |
47 | * | |
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 | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 | |
61 | */ | |
2d21ac55 A |
62 | /* |
63 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
64 | * support for mandatory and extensible security protections. This notice | |
65 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
66 | * Version 2.0. | |
67 | */ | |
1c79356b A |
68 | |
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/kernel.h> | |
72 | #include <sys/sysctl.h> | |
73 | #include <sys/malloc.h> | |
74 | #include <sys/mbuf.h> | |
75 | #include <sys/domain.h> | |
9bccf70c | 76 | #include <sys/proc.h> |
91447636 | 77 | #include <sys/kauth.h> |
1c79356b A |
78 | #include <sys/socket.h> |
79 | #include <sys/socketvar.h> | |
80 | #include <sys/protosw.h> | |
9bccf70c | 81 | #include <sys/random.h> |
1c79356b | 82 | #include <sys/syslog.h> |
6d2010ae | 83 | #include <sys/mcache.h> |
91447636 | 84 | #include <kern/locks.h> |
2d21ac55 | 85 | #include <kern/zalloc.h> |
1c79356b | 86 | |
39236c6e A |
87 | #include <dev/random/randomdev.h> |
88 | ||
1c79356b A |
89 | #include <net/route.h> |
90 | #include <net/if.h> | |
fe8ab488 | 91 | #include <net/content_filter.h> |
d9a64523 | 92 | #include <net/ntstat.h> |
cb323159 | 93 | #include <net/multi_layer_pkt_log.h> |
1c79356b | 94 | |
0a7de745 A |
95 | #define tcp_minmssoverload fring |
96 | #define _IP_VHL | |
1c79356b A |
97 | #include <netinet/in.h> |
98 | #include <netinet/in_systm.h> | |
99 | #include <netinet/ip.h> | |
b0d623f7 | 100 | #include <netinet/ip_icmp.h> |
9bccf70c | 101 | #include <netinet/ip6.h> |
5ba3f43e | 102 | #include <netinet/icmp6.h> |
1c79356b | 103 | #include <netinet/in_pcb.h> |
9bccf70c | 104 | #include <netinet6/in6_pcb.h> |
1c79356b A |
105 | #include <netinet/in_var.h> |
106 | #include <netinet/ip_var.h> | |
b0d623f7 | 107 | #include <netinet/icmp_var.h> |
1c79356b | 108 | #include <netinet6/ip6_var.h> |
5ba3f43e | 109 | #include <netinet/mptcp_var.h> |
1c79356b A |
110 | #include <netinet/tcp.h> |
111 | #include <netinet/tcp_fsm.h> | |
112 | #include <netinet/tcp_seq.h> | |
113 | #include <netinet/tcp_timer.h> | |
114 | #include <netinet/tcp_var.h> | |
6d2010ae | 115 | #include <netinet/tcp_cc.h> |
3e170ce0 | 116 | #include <netinet/tcp_cache.h> |
6d2010ae A |
117 | #include <kern/thread_call.h> |
118 | ||
9bccf70c | 119 | #include <netinet6/tcp6_var.h> |
1c79356b A |
120 | #include <netinet/tcpip.h> |
121 | #if TCPDEBUG | |
122 | #include <netinet/tcp_debug.h> | |
123 | #endif | |
cb323159 A |
124 | #include <netinet/tcp_log.h> |
125 | ||
1c79356b A |
126 | #include <netinet6/ip6protosw.h> |
127 | ||
128 | #if IPSEC | |
129 | #include <netinet6/ipsec.h> | |
9bccf70c | 130 | #include <netinet6/ipsec6.h> |
39037602 | 131 | #endif /* IPSEC */ |
1c79356b | 132 | |
fe8ab488 A |
133 | #if NECP |
134 | #include <net/necp.h> | |
135 | #endif /* NECP */ | |
136 | ||
b0d623f7 A |
137 | #undef tcp_minmssoverload |
138 | ||
3e170ce0 A |
139 | #include <corecrypto/ccaes.h> |
140 | #include <libkern/crypto/aes.h> | |
2d21ac55 | 141 | #include <libkern/crypto/md5.h> |
1c79356b | 142 | #include <sys/kdebug.h> |
6d2010ae | 143 | #include <mach/sdt.h> |
cb323159 A |
144 | #include <atm/atm_internal.h> |
145 | #include <pexpert/pexpert.h> | |
1c79356b | 146 | |
0a7de745 | 147 | #define DBG_FNC_TCP_CLOSE NETDBG_CODE(DBG_NETTCP, ((5 << 8) | 2)) |
1c79356b | 148 | |
5ba3f43e | 149 | static tcp_cc tcp_ccgen; |
1c79356b | 150 | |
39236c6e A |
151 | extern struct tcptimerlist tcp_timer_list; |
152 | extern struct tcptailq tcp_tw_tailq; | |
9bccf70c | 153 | |
5ba3f43e | 154 | SYSCTL_SKMEM_TCP_INT(TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 155 | int, tcp_mssdflt, TCP_MSS, "Default TCP Maximum Segment Size"); |
1c79356b | 156 | |
5ba3f43e | 157 | SYSCTL_SKMEM_TCP_INT(TCPCTL_V6MSSDFLT, v6mssdflt, |
0a7de745 A |
158 | CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_v6mssdflt, TCP6_MSS, |
159 | "Default TCP Maximum Segment Size for IPv6"); | |
1c79356b | 160 | |
39037602 | 161 | int tcp_sysctl_fastopenkey(struct sysctl_oid *, void *, int, |
3e170ce0 | 162 | struct sysctl_req *); |
39037602 | 163 | SYSCTL_PROC(_net_inet_tcp, OID_AUTO, fastopen_key, CTLTYPE_STRING | CTLFLAG_WR, |
0a7de745 | 164 | 0, 0, tcp_sysctl_fastopenkey, "S", "TCP Fastopen key"); |
3e170ce0 A |
165 | |
166 | /* Current count of half-open TFO connections */ | |
0a7de745 | 167 | int tcp_tfo_halfcnt = 0; |
3e170ce0 A |
168 | |
169 | /* Maximum of half-open TFO connection backlog */ | |
5ba3f43e | 170 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, fastopen_backlog, |
0a7de745 A |
171 | CTLFLAG_RW | CTLFLAG_LOCKED, int, tcp_tfo_backlog, 10, |
172 | "Backlog queue for half-open TFO connections"); | |
3e170ce0 | 173 | |
5ba3f43e | 174 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, fastopen, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 A |
175 | int, tcp_fastopen, TCP_FASTOPEN_CLIENT | TCP_FASTOPEN_SERVER, |
176 | "Enable TCP Fastopen (RFC 7413)"); | |
5ba3f43e A |
177 | |
178 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, now_init, CTLFLAG_RD | CTLFLAG_LOCKED, | |
0a7de745 | 179 | uint32_t, tcp_now_init, 0, "Initial tcp now value"); |
3e170ce0 | 180 | |
5ba3f43e | 181 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, microuptime_init, CTLFLAG_RD | CTLFLAG_LOCKED, |
0a7de745 | 182 | uint32_t, tcp_microuptime_init, 0, "Initial tcp uptime value in micro seconds"); |
3e170ce0 | 183 | |
e5568f75 A |
184 | /* |
185 | * Minimum MSS we accept and use. This prevents DoS attacks where | |
186 | * we are forced to a ridiculous low MSS like 20 and send hundreds | |
187 | * of packets instead of one. The effect scales with the available | |
188 | * bandwidth and quickly saturates the CPU and network interface | |
189 | * with packet generation and sending. Set to zero to disable MINMSS | |
190 | * checking. This setting prevents us from sending too small packets. | |
191 | */ | |
5ba3f43e | 192 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, minmss, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 193 | int, tcp_minmss, TCP_MINMSS, "Minmum TCP Maximum Segment Size"); |
1c79356b | 194 | |
39037602 | 195 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD | CTLFLAG_LOCKED, |
0a7de745 | 196 | &tcbinfo.ipi_count, 0, "Number of active PCBs"); |
1c79356b | 197 | |
39037602 | 198 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, tw_pcbcount, CTLFLAG_RD | CTLFLAG_LOCKED, |
0a7de745 | 199 | &tcbinfo.ipi_twcount, 0, "Number of pcbs in time-wait state"); |
39236c6e | 200 | |
5ba3f43e | 201 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, icmp_may_rst, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 A |
202 | static int, icmp_may_rst, 1, |
203 | "Certain ICMP unreachable messages may abort connections in SYN_SENT"); | |
1c79356b | 204 | |
0a7de745 A |
205 | static int tcp_strict_rfc1948 = 0; |
206 | static int tcp_isn_reseed_interval = 0; | |
3e170ce0 | 207 | #if (DEVELOPMENT || DEBUG) |
39037602 | 208 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, strict_rfc1948, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 209 | &tcp_strict_rfc1948, 0, "Determines if RFC1948 is followed exactly"); |
9bccf70c | 210 | |
3e170ce0 | 211 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, |
0a7de745 A |
212 | CTLFLAG_RW | CTLFLAG_LOCKED, |
213 | &tcp_isn_reseed_interval, 0, "Seconds between reseeding of ISN secret"); | |
3e170ce0 | 214 | #endif /* (DEVELOPMENT || DEBUG) */ |
2d21ac55 | 215 | |
5ba3f43e | 216 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, rtt_min, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 217 | int, tcp_TCPTV_MIN, 100, "min rtt value allowed"); |
9bccf70c | 218 | |
5ba3f43e | 219 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, rexmt_slop, CTLFLAG_RW, |
0a7de745 | 220 | int, tcp_rexmt_slop, TCPTV_REXMTSLOP, "Slop added to retransmit timeout"); |
6d2010ae | 221 | |
5ba3f43e | 222 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, randomize_ports, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 A |
223 | __private_extern__ int, tcp_use_randomport, 0, |
224 | "Randomize TCP port numbers"); | |
b0d623f7 | 225 | |
5ba3f43e | 226 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, win_scale_factor, CTLFLAG_RW | CTLFLAG_LOCKED, |
0a7de745 | 227 | __private_extern__ int, tcp_win_scale, 3, "Window scaling factor"); |
316670eb | 228 | |
cb323159 A |
229 | #if (DEVELOPMENT || DEBUG) |
230 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, init_rtt_from_cache, | |
231 | CTLFLAG_RW | CTLFLAG_LOCKED, static int, tcp_init_rtt_from_cache, 1, | |
232 | "Initalize RTT from route cache"); | |
233 | #else | |
234 | SYSCTL_SKMEM_TCP_INT(OID_AUTO, init_rtt_from_cache, | |
235 | CTLFLAG_RD | CTLFLAG_LOCKED, static int, tcp_init_rtt_from_cache, 1, | |
236 | "Initalize RTT from route cache"); | |
237 | #endif /* (DEVELOPMENT || DEBUG) */ | |
238 | ||
f427ee49 A |
239 | static int tso_debug = 0; |
240 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso_debug, CTLFLAG_RW | CTLFLAG_LOCKED, | |
241 | &tso_debug, 0, "TSO verbosity"); | |
242 | ||
0a7de745 | 243 | static void tcp_notify(struct inpcb *, int); |
6d2010ae | 244 | |
0a7de745 A |
245 | struct zone *sack_hole_zone; |
246 | struct zone *tcp_reass_zone; | |
247 | struct zone *tcp_bwmeas_zone; | |
248 | struct zone *tcp_rxt_seg_zone; | |
1c79356b | 249 | |
0a7de745 | 250 | extern int slowlink_wsize; /* window correction for slow links */ |
2d21ac55 A |
251 | extern int path_mtu_discovery; |
252 | ||
316670eb A |
253 | static void tcp_sbrcv_grow_rwin(struct tcpcb *tp, struct sockbuf *sb); |
254 | ||
0a7de745 A |
255 | #define TCP_BWMEAS_BURST_MINSIZE 6 |
256 | #define TCP_BWMEAS_BURST_MAXSIZE 25 | |
316670eb | 257 | |
1c79356b A |
258 | /* |
259 | * Target size of TCP PCB hash tables. Must be a power of two. | |
260 | * | |
261 | * Note that this can be overridden by the kernel environment | |
262 | * variable net.inet.tcp.tcbhashsize | |
263 | */ | |
264 | #ifndef TCBHASHSIZE | |
0a7de745 | 265 | #define TCBHASHSIZE CONFIG_TCBHASHSIZE |
1c79356b A |
266 | #endif |
267 | ||
0a7de745 | 268 | __private_extern__ int tcp_tcbhashsize = TCBHASHSIZE; |
6d2010ae | 269 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD | CTLFLAG_LOCKED, |
0a7de745 | 270 | &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable"); |
b0d623f7 | 271 | |
1c79356b A |
272 | /* |
273 | * This is the actual shape of what we allocate using the zone | |
274 | * allocator. Doing it this way allows us to protect both structures | |
275 | * using the same generation count, and also eliminates the overhead | |
276 | * of allocating tcpcbs separately. By hiding the structure here, | |
277 | * we avoid changing most of the rest of the code (although it needs | |
278 | * to be changed, eventually, for greater efficiency). | |
279 | */ | |
0a7de745 A |
280 | #define ALIGNMENT 32 |
281 | struct inp_tp { | |
282 | struct inpcb inp; | |
283 | struct tcpcb tcb __attribute__((aligned(ALIGNMENT))); | |
1c79356b A |
284 | }; |
285 | #undef ALIGNMENT | |
1c79356b | 286 | |
2d21ac55 A |
287 | int get_inpcb_str_size(void); |
288 | int get_tcp_str_size(void); | |
1c79356b | 289 | |
cb323159 A |
290 | os_log_t tcp_mpkl_log_object = NULL; |
291 | ||
b0d623f7 | 292 | static void tcpcb_to_otcpcb(struct tcpcb *, struct otcpcb *); |
2d21ac55 | 293 | |
39037602 A |
294 | static lck_attr_t *tcp_uptime_mtx_attr = NULL; |
295 | static lck_grp_t *tcp_uptime_mtx_grp = NULL; | |
296 | static lck_grp_attr_t *tcp_uptime_mtx_grp_attr = NULL; | |
316670eb | 297 | int tcp_notsent_lowat_check(struct socket *so); |
5ba3f43e A |
298 | static void tcp_flow_lim_stats(struct ifnet_stats_per_flow *ifs, |
299 | struct if_lim_perf_stat *stat); | |
300 | static void tcp_flow_ecn_perf_stats(struct ifnet_stats_per_flow *ifs, | |
301 | struct if_tcp_ecn_perf_stat *stat); | |
6d2010ae | 302 | |
3e170ce0 A |
303 | static aes_encrypt_ctx tfo_ctx; /* Crypto-context for TFO */ |
304 | ||
305 | void | |
306 | tcp_tfo_gen_cookie(struct inpcb *inp, u_char *out, size_t blk_size) | |
307 | { | |
308 | u_char in[CCAES_BLOCK_SIZE]; | |
3e170ce0 | 309 | int isipv6 = inp->inp_vflag & INP_IPV6; |
3e170ce0 A |
310 | |
311 | VERIFY(blk_size == CCAES_BLOCK_SIZE); | |
312 | ||
313 | bzero(&in[0], CCAES_BLOCK_SIZE); | |
314 | bzero(&out[0], CCAES_BLOCK_SIZE); | |
315 | ||
0a7de745 | 316 | if (isipv6) { |
3e170ce0 | 317 | memcpy(in, &inp->in6p_faddr, sizeof(struct in6_addr)); |
f427ee49 A |
318 | } else { |
319 | memcpy(in, &inp->inp_faddr, sizeof(struct in_addr)); | |
320 | } | |
3e170ce0 A |
321 | |
322 | aes_encrypt_cbc(in, NULL, 1, out, &tfo_ctx); | |
323 | } | |
324 | ||
325 | __private_extern__ int | |
326 | tcp_sysctl_fastopenkey(__unused struct sysctl_oid *oidp, __unused void *arg1, | |
327 | __unused int arg2, struct sysctl_req *req) | |
328 | { | |
329 | int error = 0; | |
39037602 A |
330 | /* |
331 | * TFO-key is expressed as a string in hex format | |
332 | * (+1 to account for \0 char) | |
333 | */ | |
3e170ce0 A |
334 | char keystring[TCP_FASTOPEN_KEYLEN * 2 + 1]; |
335 | u_int32_t key[TCP_FASTOPEN_KEYLEN / sizeof(u_int32_t)]; | |
336 | int i; | |
337 | ||
338 | /* -1, because newlen is len without the terminating \0 character */ | |
339 | if (req->newlen != (sizeof(keystring) - 1)) { | |
340 | error = EINVAL; | |
341 | goto exit; | |
342 | } | |
343 | ||
39037602 A |
344 | /* |
345 | * sysctl_io_string copies keystring into the oldptr of the sysctl_req. | |
3e170ce0 A |
346 | * Make sure everything is zero, to avoid putting garbage in there or |
347 | * leaking the stack. | |
348 | */ | |
349 | bzero(keystring, sizeof(keystring)); | |
350 | ||
351 | error = sysctl_io_string(req, keystring, sizeof(keystring), 0, NULL); | |
0a7de745 | 352 | if (error) { |
3e170ce0 | 353 | goto exit; |
0a7de745 | 354 | } |
3e170ce0 A |
355 | |
356 | for (i = 0; i < (TCP_FASTOPEN_KEYLEN / sizeof(u_int32_t)); i++) { | |
39037602 A |
357 | /* |
358 | * We jump over the keystring in 8-character (4 byte in hex) | |
359 | * steps | |
360 | */ | |
3e170ce0 A |
361 | if (sscanf(&keystring[i * 8], "%8x", &key[i]) != 1) { |
362 | error = EINVAL; | |
363 | goto exit; | |
364 | } | |
365 | } | |
366 | ||
367 | aes_encrypt_key128((u_char *)key, &tfo_ctx); | |
368 | ||
369 | exit: | |
0a7de745 | 370 | return error; |
3e170ce0 A |
371 | } |
372 | ||
39037602 A |
373 | int |
374 | get_inpcb_str_size(void) | |
1c79356b | 375 | { |
0a7de745 | 376 | return sizeof(struct inpcb); |
1c79356b A |
377 | } |
378 | ||
39037602 A |
379 | int |
380 | get_tcp_str_size(void) | |
1c79356b | 381 | { |
0a7de745 | 382 | return sizeof(struct tcpcb); |
1c79356b A |
383 | } |
384 | ||
fe8ab488 A |
385 | static int scale_to_powerof2(int size); |
386 | ||
6d2010ae | 387 | /* |
fe8ab488 | 388 | * This helper routine returns one of the following scaled value of size: |
39037602 | 389 | * 1. Rounded down power of two value of size if the size value passed as |
fe8ab488 A |
390 | * argument is not a power of two and the rounded up value overflows. |
391 | * OR | |
39037602 A |
392 | * 2. Rounded up power of two value of size if the size value passed as |
393 | * argument is not a power of two and the rounded up value does not overflow | |
fe8ab488 A |
394 | * OR |
395 | * 3. Same value as argument size if it is already a power of two. | |
39037602 A |
396 | */ |
397 | static int | |
0a7de745 A |
398 | scale_to_powerof2(int size) |
399 | { | |
fe8ab488 A |
400 | /* Handle special case of size = 0 */ |
401 | int ret = size ? size : 1; | |
402 | ||
403 | if (!powerof2(ret)) { | |
39037602 A |
404 | while (!powerof2(size)) { |
405 | /* | |
fe8ab488 A |
406 | * Clear out least significant |
407 | * set bit till size is left with | |
408 | * its highest set bit at which point | |
409 | * it is rounded down power of two. | |
39037602 | 410 | */ |
0a7de745 | 411 | size = size & (size - 1); |
fe8ab488 | 412 | } |
6d2010ae | 413 | |
fe8ab488 A |
414 | /* Check for overflow when rounding up */ |
415 | if (0 == (size << 1)) { | |
416 | ret = size; | |
417 | } else { | |
418 | ret = size << 1; | |
419 | } | |
420 | } | |
421 | ||
0a7de745 | 422 | return ret; |
6d2010ae | 423 | } |
1c79356b | 424 | |
3e170ce0 | 425 | static void |
39037602 | 426 | tcp_tfo_init(void) |
3e170ce0 A |
427 | { |
428 | u_char key[TCP_FASTOPEN_KEYLEN]; | |
429 | ||
5ba3f43e | 430 | read_frandom(key, sizeof(key)); |
3e170ce0 A |
431 | aes_encrypt_key128(key, &tfo_ctx); |
432 | } | |
433 | ||
1c79356b A |
434 | /* |
435 | * Tcp initialization | |
436 | */ | |
437 | void | |
39236c6e | 438 | tcp_init(struct protosw *pp, struct domain *dp) |
1c79356b | 439 | { |
39236c6e A |
440 | #pragma unused(dp) |
441 | static int tcp_initialized = 0; | |
39037602 | 442 | vm_size_t str_size; |
39236c6e | 443 | struct inpcbinfo *pcbinfo; |
cb323159 | 444 | uint32_t logging_config; |
39236c6e | 445 | |
0a7de745 | 446 | VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED); |
39236c6e | 447 | |
0a7de745 | 448 | if (tcp_initialized) { |
39236c6e | 449 | return; |
0a7de745 | 450 | } |
39236c6e A |
451 | tcp_initialized = 1; |
452 | ||
1c79356b | 453 | tcp_ccgen = 1; |
9bccf70c A |
454 | tcp_keepinit = TCPTV_KEEP_INIT; |
455 | tcp_keepidle = TCPTV_KEEP_IDLE; | |
456 | tcp_keepintvl = TCPTV_KEEPINTVL; | |
39236c6e | 457 | tcp_keepcnt = TCPTV_KEEPCNT; |
9bccf70c A |
458 | tcp_maxpersistidle = TCPTV_KEEP_IDLE; |
459 | tcp_msl = TCPTV_MSL; | |
d7e50217 | 460 | |
6d2010ae | 461 | microuptime(&tcp_uptime); |
5ba3f43e | 462 | read_frandom(&tcp_now, sizeof(tcp_now)); |
39037602 A |
463 | |
464 | /* Starts tcp internal clock at a random value */ | |
465 | tcp_now = tcp_now & 0x3fffffff; | |
9bccf70c | 466 | |
5ba3f43e A |
467 | /* expose initial uptime/now via systcl for utcp to keep time sync */ |
468 | tcp_now_init = tcp_now; | |
a39ff7e2 | 469 | tcp_microuptime_init = |
f427ee49 | 470 | (uint32_t)(tcp_uptime.tv_usec + (tcp_uptime.tv_sec * USEC_PER_SEC)); |
5ba3f43e A |
471 | SYSCTL_SKMEM_UPDATE_FIELD(tcp.microuptime_init, tcp_microuptime_init); |
472 | SYSCTL_SKMEM_UPDATE_FIELD(tcp.now_init, tcp_now_init); | |
473 | ||
3e170ce0 A |
474 | tcp_tfo_init(); |
475 | ||
1c79356b | 476 | LIST_INIT(&tcb); |
39236c6e A |
477 | tcbinfo.ipi_listhead = &tcb; |
478 | ||
91447636 | 479 | pcbinfo = &tcbinfo; |
39236c6e A |
480 | /* |
481 | * allocate lock group attribute and group for tcp pcb mutexes | |
482 | */ | |
483 | pcbinfo->ipi_lock_grp_attr = lck_grp_attr_alloc_init(); | |
39037602 A |
484 | pcbinfo->ipi_lock_grp = lck_grp_alloc_init("tcppcb", |
485 | pcbinfo->ipi_lock_grp_attr); | |
39236c6e A |
486 | |
487 | /* | |
488 | * allocate the lock attribute for tcp pcb mutexes | |
489 | */ | |
490 | pcbinfo->ipi_lock_attr = lck_attr_alloc_init(); | |
491 | ||
492 | if ((pcbinfo->ipi_lock = lck_rw_alloc_init(pcbinfo->ipi_lock_grp, | |
493 | pcbinfo->ipi_lock_attr)) == NULL) { | |
494 | panic("%s: unable to allocate PCB lock\n", __func__); | |
495 | /* NOTREACHED */ | |
496 | } | |
497 | ||
fe8ab488 A |
498 | if (tcp_tcbhashsize == 0) { |
499 | /* Set to default */ | |
500 | tcp_tcbhashsize = 512; | |
501 | } | |
502 | ||
b0d623f7 | 503 | if (!powerof2(tcp_tcbhashsize)) { |
fe8ab488 A |
504 | int old_hash_size = tcp_tcbhashsize; |
505 | tcp_tcbhashsize = scale_to_powerof2(tcp_tcbhashsize); | |
506 | /* Lower limit of 16 */ | |
507 | if (tcp_tcbhashsize < 16) { | |
508 | tcp_tcbhashsize = 16; | |
509 | } | |
510 | printf("WARNING: TCB hash size not a power of 2, " | |
0a7de745 A |
511 | "scaled from %d to %d.\n", |
512 | old_hash_size, | |
513 | tcp_tcbhashsize); | |
1c79356b | 514 | } |
fe8ab488 | 515 | |
39037602 A |
516 | tcbinfo.ipi_hashbase = hashinit(tcp_tcbhashsize, M_PCB, |
517 | &tcbinfo.ipi_hashmask); | |
39236c6e | 518 | tcbinfo.ipi_porthashbase = hashinit(tcp_tcbhashsize, M_PCB, |
0a7de745 | 519 | &tcbinfo.ipi_porthashmask); |
f427ee49 A |
520 | str_size = (vm_size_t)P2ROUNDUP(sizeof(struct inp_tp), sizeof(u_int64_t)); |
521 | tcbinfo.ipi_zone = zone_create("tcpcb", str_size, ZC_NONE); | |
6d2010ae | 522 | |
39236c6e | 523 | tcbinfo.ipi_gc = tcp_gc; |
3e170ce0 | 524 | tcbinfo.ipi_timer = tcp_itimer; |
39236c6e A |
525 | in_pcbinfo_attach(&tcbinfo); |
526 | ||
f427ee49 A |
527 | str_size = (vm_size_t)P2ROUNDUP(sizeof(struct sackhole), sizeof(u_int64_t)); |
528 | sack_hole_zone = zone_create("sack_hole zone", str_size, ZC_NONE); | |
e5568f75 | 529 | |
f427ee49 A |
530 | str_size = (vm_size_t)P2ROUNDUP(sizeof(struct tseg_qent), sizeof(u_int64_t)); |
531 | tcp_reass_zone = zone_create("tcp_reass_zone", str_size, ZC_NONE); | |
532 | ||
533 | str_size = (vm_size_t)P2ROUNDUP(sizeof(struct bwmeas), sizeof(u_int64_t)); | |
534 | tcp_bwmeas_zone = zone_create("tcp_bwmeas_zone", str_size, ZC_ZFREE_CLEARMEM); | |
316670eb | 535 | |
f427ee49 A |
536 | str_size = (vm_size_t)P2ROUNDUP(sizeof(struct tcp_ccstate), sizeof(u_int64_t)); |
537 | tcp_cc_zone = zone_create("tcp_cc_zone", str_size, ZC_NONE); | |
fe8ab488 | 538 | |
f427ee49 A |
539 | str_size = (vm_size_t)P2ROUNDUP(sizeof(struct tcp_rxt_seg), sizeof(u_int64_t)); |
540 | tcp_rxt_seg_zone = zone_create("tcp_rxt_seg_zone", str_size, ZC_NONE); | |
3e170ce0 | 541 | |
0a7de745 | 542 | #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) |
316670eb A |
543 | if (max_protohdr < TCP_MINPROTOHDR) { |
544 | _max_protohdr = TCP_MINPROTOHDR; | |
f427ee49 | 545 | _max_protohdr = (int)max_protohdr; /* round it up */ |
316670eb | 546 | } |
0a7de745 | 547 | if (max_linkhdr + max_protohdr > MCLBYTES) { |
1c79356b | 548 | panic("tcp_init"); |
0a7de745 | 549 | } |
9bccf70c | 550 | #undef TCP_MINPROTOHDR |
91447636 | 551 | |
39236c6e A |
552 | /* Initialize time wait and timer lists */ |
553 | TAILQ_INIT(&tcp_tw_tailq); | |
2d21ac55 | 554 | |
6d2010ae A |
555 | bzero(&tcp_timer_list, sizeof(tcp_timer_list)); |
556 | LIST_INIT(&tcp_timer_list.lhead); | |
557 | /* | |
39037602 A |
558 | * allocate lock group attribute, group and attribute for |
559 | * the tcp timer list | |
6d2010ae A |
560 | */ |
561 | tcp_timer_list.mtx_grp_attr = lck_grp_attr_alloc_init(); | |
39037602 A |
562 | tcp_timer_list.mtx_grp = lck_grp_alloc_init("tcptimerlist", |
563 | tcp_timer_list.mtx_grp_attr); | |
6d2010ae | 564 | tcp_timer_list.mtx_attr = lck_attr_alloc_init(); |
39037602 A |
565 | if ((tcp_timer_list.mtx = lck_mtx_alloc_init(tcp_timer_list.mtx_grp, |
566 | tcp_timer_list.mtx_attr)) == NULL) { | |
6d2010ae | 567 | panic("failed to allocate memory for tcp_timer_list.mtx\n"); |
0a7de745 A |
568 | } |
569 | ; | |
39037602 A |
570 | tcp_timer_list.call = thread_call_allocate(tcp_run_timerlist, NULL); |
571 | if (tcp_timer_list.call == NULL) { | |
6d2010ae A |
572 | panic("failed to allocate call entry 1 in tcp_init\n"); |
573 | } | |
574 | ||
575 | /* | |
39037602 A |
576 | * allocate lock group attribute, group and attribute for |
577 | * tcp_uptime_lock | |
6d2010ae A |
578 | */ |
579 | tcp_uptime_mtx_grp_attr = lck_grp_attr_alloc_init(); | |
39037602 A |
580 | tcp_uptime_mtx_grp = lck_grp_alloc_init("tcpuptime", |
581 | tcp_uptime_mtx_grp_attr); | |
6d2010ae | 582 | tcp_uptime_mtx_attr = lck_attr_alloc_init(); |
39037602 A |
583 | tcp_uptime_lock = lck_spin_alloc_init(tcp_uptime_mtx_grp, |
584 | tcp_uptime_mtx_attr); | |
6d2010ae | 585 | |
3e170ce0 A |
586 | /* Initialize TCP Cache */ |
587 | tcp_cache_init(); | |
588 | ||
cb323159 A |
589 | tcp_mpkl_log_object = MPKL_CREATE_LOGOBJECT("com.apple.xnu.tcp"); |
590 | if (tcp_mpkl_log_object == NULL) { | |
591 | panic("MPKL_CREATE_LOGOBJECT failed"); | |
592 | } | |
593 | ||
594 | logging_config = atm_get_diagnostic_config(); | |
595 | if (logging_config & 0x80000000) { | |
596 | tcp_log_privacy = 1; | |
597 | } | |
598 | ||
599 | PE_parse_boot_argn("tcp_log", &tcp_log_enable_flags, sizeof(tcp_log_enable_flags)); | |
600 | ||
fe8ab488 | 601 | /* |
f427ee49 | 602 | * If more than 4GB of actual memory is available, increase the |
fe8ab488 A |
603 | * maximum allowed receive and send socket buffer size. |
604 | */ | |
f427ee49 A |
605 | if (mem_actual >= (1ULL << (GBSHIFT + 2))) { |
606 | tcp_autorcvbuf_max = 4 * 1024 * 1024; | |
607 | tcp_autosndbuf_max = 4 * 1024 * 1024; | |
d9a64523 | 608 | |
5ba3f43e A |
609 | SYSCTL_SKMEM_UPDATE_FIELD(tcp.autorcvbufmax, tcp_autorcvbuf_max); |
610 | SYSCTL_SKMEM_UPDATE_FIELD(tcp.autosndbufmax, tcp_autosndbuf_max); | |
fe8ab488 | 611 | } |
9bccf70c A |
612 | } |
613 | ||
614 | /* | |
615 | * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb. | |
616 | * tcp_template used to store this data in mbufs, but we now recopy it out | |
617 | * of the tcpcb each time to conserve mbufs. | |
618 | */ | |
619 | void | |
39037602 | 620 | tcp_fillheaders(struct tcpcb *tp, void *ip_ptr, void *tcp_ptr) |
9bccf70c A |
621 | { |
622 | struct inpcb *inp = tp->t_inpcb; | |
623 | struct tcphdr *tcp_hdr = (struct tcphdr *)tcp_ptr; | |
624 | ||
9bccf70c A |
625 | if ((inp->inp_vflag & INP_IPV6) != 0) { |
626 | struct ip6_hdr *ip6; | |
627 | ||
628 | ip6 = (struct ip6_hdr *)ip_ptr; | |
629 | ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | | |
0a7de745 | 630 | (inp->inp_flow & IPV6_FLOWINFO_MASK); |
9bccf70c | 631 | ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | |
0a7de745 | 632 | (IPV6_VERSION & IPV6_VERSION_MASK); |
39037602 | 633 | ip6->ip6_plen = htons(sizeof(struct tcphdr)); |
9bccf70c | 634 | ip6->ip6_nxt = IPPROTO_TCP; |
39037602 | 635 | ip6->ip6_hlim = 0; |
9bccf70c A |
636 | ip6->ip6_src = inp->in6p_laddr; |
637 | ip6->ip6_dst = inp->in6p_faddr; | |
39236c6e | 638 | tcp_hdr->th_sum = in6_pseudo(&inp->in6p_laddr, &inp->in6p_faddr, |
0a7de745 | 639 | htonl(sizeof(struct tcphdr) + IPPROTO_TCP)); |
f427ee49 | 640 | } else { |
39037602 A |
641 | struct ip *ip = (struct ip *) ip_ptr; |
642 | ||
643 | ip->ip_vhl = IP_VHL_BORING; | |
644 | ip->ip_tos = 0; | |
645 | ip->ip_len = 0; | |
646 | ip->ip_id = 0; | |
647 | ip->ip_off = 0; | |
648 | ip->ip_ttl = 0; | |
649 | ip->ip_sum = 0; | |
650 | ip->ip_p = IPPROTO_TCP; | |
651 | ip->ip_src = inp->inp_laddr; | |
652 | ip->ip_dst = inp->inp_faddr; | |
653 | tcp_hdr->th_sum = | |
654 | in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, | |
655 | htons(sizeof(struct tcphdr) + IPPROTO_TCP)); | |
9bccf70c A |
656 | } |
657 | ||
658 | tcp_hdr->th_sport = inp->inp_lport; | |
659 | tcp_hdr->th_dport = inp->inp_fport; | |
660 | tcp_hdr->th_seq = 0; | |
661 | tcp_hdr->th_ack = 0; | |
662 | tcp_hdr->th_x2 = 0; | |
663 | tcp_hdr->th_off = 5; | |
664 | tcp_hdr->th_flags = 0; | |
665 | tcp_hdr->th_win = 0; | |
666 | tcp_hdr->th_urp = 0; | |
1c79356b A |
667 | } |
668 | ||
669 | /* | |
670 | * Create template to be used to send tcp packets on a connection. | |
9bccf70c A |
671 | * Allocates an mbuf and fills in a skeletal tcp/ip header. The only |
672 | * use for this function is in keepalives, which use tcp_respond. | |
1c79356b A |
673 | */ |
674 | struct tcptemp * | |
39037602 | 675 | tcp_maketemplate(struct tcpcb *tp) |
1c79356b | 676 | { |
9bccf70c A |
677 | struct mbuf *m; |
678 | struct tcptemp *n; | |
1c79356b | 679 | |
9bccf70c | 680 | m = m_get(M_DONTWAIT, MT_HEADER); |
0a7de745 | 681 | if (m == NULL) { |
f427ee49 | 682 | return NULL; |
0a7de745 | 683 | } |
9bccf70c A |
684 | m->m_len = sizeof(struct tcptemp); |
685 | n = mtod(m, struct tcptemp *); | |
0b4e3aa0 | 686 | |
9bccf70c | 687 | tcp_fillheaders(tp, (void *)&n->tt_ipgen, (void *)&n->tt_t); |
0a7de745 | 688 | return n; |
1c79356b A |
689 | } |
690 | ||
691 | /* | |
692 | * Send a single message to the TCP at address specified by | |
693 | * the given TCP/IP header. If m == 0, then we make a copy | |
694 | * of the tcpiphdr at ti and send directly to the addressed host. | |
695 | * This is used to force keep alive messages out using the TCP | |
9bccf70c A |
696 | * template for a connection. If flags are given then we send |
697 | * a message back to the TCP which originated the * segment ti, | |
698 | * and discard the mbuf containing it and any other attached mbufs. | |
1c79356b A |
699 | * |
700 | * In any case the ack and sequence number of the transmitted | |
701 | * segment are as specified by the parameters. | |
702 | * | |
703 | * NOTE: If m != NULL, then ti must point to *inside* the mbuf. | |
704 | */ | |
705 | void | |
fe8ab488 | 706 | tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m, |
f427ee49 | 707 | tcp_seq ack, tcp_seq seq, uint8_t flags, struct tcp_respond_args *tra) |
1c79356b | 708 | { |
f427ee49 | 709 | uint16_t tlen; |
1c79356b A |
710 | int win = 0; |
711 | struct route *ro = 0; | |
712 | struct route sro; | |
9bccf70c | 713 | struct ip *ip; |
1c79356b | 714 | struct tcphdr *nth; |
1c79356b A |
715 | struct route_in6 *ro6 = 0; |
716 | struct route_in6 sro6; | |
9bccf70c A |
717 | struct ip6_hdr *ip6; |
718 | int isipv6; | |
316670eb | 719 | struct ifnet *outif; |
39037602 | 720 | int sotc = SO_TC_UNSPEC; |
f427ee49 | 721 | bool check_qos_marking_again = FALSE; |
9bccf70c | 722 | |
9bccf70c A |
723 | isipv6 = IP_VHL_V(((struct ip *)ipgen)->ip_vhl) == 6; |
724 | ip6 = ipgen; | |
9bccf70c | 725 | ip = ipgen; |
1c79356b A |
726 | |
727 | if (tp) { | |
f427ee49 | 728 | check_qos_marking_again = tp->t_inpcb->inp_socket->so_flags1 & SOF1_QOSMARKING_POLICY_OVERRIDE ? FALSE : TRUE; |
9bccf70c | 729 | if (!(flags & TH_RST)) { |
2d21ac55 | 730 | win = tcp_sbspace(tp); |
0a7de745 | 731 | if (win > (int32_t)TCP_MAXWIN << tp->rcv_scale) { |
b0d623f7 | 732 | win = (int32_t)TCP_MAXWIN << tp->rcv_scale; |
0a7de745 | 733 | } |
9bccf70c | 734 | } |
0a7de745 | 735 | if (isipv6) { |
1c79356b | 736 | ro6 = &tp->t_inpcb->in6p_route; |
f427ee49 A |
737 | } else { |
738 | ro = &tp->t_inpcb->inp_route; | |
739 | } | |
1c79356b | 740 | } else { |
1c79356b A |
741 | if (isipv6) { |
742 | ro6 = &sro6; | |
39037602 | 743 | bzero(ro6, sizeof(*ro6)); |
f427ee49 | 744 | } else { |
9bccf70c | 745 | ro = &sro; |
39037602 | 746 | bzero(ro, sizeof(*ro)); |
1c79356b | 747 | } |
1c79356b A |
748 | } |
749 | if (m == 0) { | |
0a7de745 A |
750 | m = m_gethdr(M_DONTWAIT, MT_HEADER); /* MAC-OK */ |
751 | if (m == NULL) { | |
1c79356b | 752 | return; |
0a7de745 | 753 | } |
1c79356b | 754 | tlen = 0; |
1c79356b | 755 | m->m_data += max_linkhdr; |
1c79356b | 756 | if (isipv6) { |
39236c6e | 757 | VERIFY((MHLEN - max_linkhdr) >= |
0a7de745 | 758 | (sizeof(*ip6) + sizeof(*nth))); |
39037602 A |
759 | bcopy((caddr_t)ip6, mtod(m, caddr_t), |
760 | sizeof(struct ip6_hdr)); | |
9bccf70c | 761 | ip6 = mtod(m, struct ip6_hdr *); |
316670eb | 762 | nth = (struct tcphdr *)(void *)(ip6 + 1); |
f427ee49 | 763 | } else { |
39236c6e | 764 | VERIFY((MHLEN - max_linkhdr) >= |
0a7de745 | 765 | (sizeof(*ip) + sizeof(*nth))); |
9bccf70c A |
766 | bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); |
767 | ip = mtod(m, struct ip *); | |
316670eb | 768 | nth = (struct tcphdr *)(void *)(ip + 1); |
1c79356b | 769 | } |
1c79356b | 770 | bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr)); |
39236c6e | 771 | #if MPTCP |
0a7de745 | 772 | if ((tp) && (tp->t_mpflags & TMPF_RESET)) { |
39236c6e | 773 | flags = (TH_RST | TH_ACK); |
0a7de745 | 774 | } else |
39236c6e | 775 | #endif |
1c79356b A |
776 | flags = TH_ACK; |
777 | } else { | |
778 | m_freem(m->m_next); | |
779 | m->m_next = 0; | |
9bccf70c | 780 | m->m_data = (caddr_t)ipgen; |
1c79356b A |
781 | /* m_len is set later */ |
782 | tlen = 0; | |
0a7de745 | 783 | #define xchg(a, b, type) { type t; t = a; a = b; b = t; } |
1c79356b | 784 | if (isipv6) { |
316670eb A |
785 | /* Expect 32-bit aligned IP on strict-align platforms */ |
786 | IP6_HDR_STRICT_ALIGNMENT_CHECK(ip6); | |
9bccf70c | 787 | xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); |
316670eb | 788 | nth = (struct tcphdr *)(void *)(ip6 + 1); |
f427ee49 | 789 | } else { |
39037602 A |
790 | /* Expect 32-bit aligned IP on strict-align platforms */ |
791 | IP_HDR_STRICT_ALIGNMENT_CHECK(ip); | |
792 | xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long); | |
793 | nth = (struct tcphdr *)(void *)(ip + 1); | |
794 | } | |
9bccf70c A |
795 | if (th != nth) { |
796 | /* | |
797 | * this is usually a case when an extension header | |
798 | * exists between the IPv6 header and the | |
799 | * TCP header. | |
800 | */ | |
801 | nth->th_sport = th->th_sport; | |
802 | nth->th_dport = th->th_dport; | |
1c79356b | 803 | } |
1c79356b A |
804 | xchg(nth->th_dport, nth->th_sport, n_short); |
805 | #undef xchg | |
806 | } | |
9bccf70c | 807 | if (isipv6) { |
0a7de745 A |
808 | ip6->ip6_plen = htons((u_short)(sizeof(struct tcphdr) + |
809 | tlen)); | |
810 | tlen += sizeof(struct ip6_hdr) + sizeof(struct tcphdr); | |
f427ee49 | 811 | } else { |
0a7de745 | 812 | tlen += sizeof(struct tcpiphdr); |
39037602 | 813 | ip->ip_len = tlen; |
f427ee49 | 814 | ip->ip_ttl = (uint8_t)ip_defttl; |
39037602 | 815 | } |
9bccf70c A |
816 | m->m_len = tlen; |
817 | m->m_pkthdr.len = tlen; | |
91447636 | 818 | m->m_pkthdr.rcvif = 0; |
cb323159 A |
819 | if (tra->keep_alive) { |
820 | m->m_pkthdr.pkt_flags |= PKTF_KEEPALIVE; | |
821 | } | |
b0d623f7 | 822 | |
1c79356b A |
823 | nth->th_seq = htonl(seq); |
824 | nth->th_ack = htonl(ack); | |
825 | nth->th_x2 = 0; | |
0a7de745 | 826 | nth->th_off = sizeof(struct tcphdr) >> 2; |
1c79356b | 827 | nth->th_flags = flags; |
0a7de745 | 828 | if (tp) { |
1c79356b | 829 | nth->th_win = htons((u_short) (win >> tp->rcv_scale)); |
0a7de745 | 830 | } else { |
1c79356b | 831 | nth->th_win = htons((u_short)win); |
0a7de745 | 832 | } |
1c79356b | 833 | nth->th_urp = 0; |
1c79356b | 834 | if (isipv6) { |
9bccf70c | 835 | nth->th_sum = 0; |
39236c6e | 836 | nth->th_sum = in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst, |
0a7de745 | 837 | htonl((tlen - sizeof(struct ip6_hdr)) + IPPROTO_TCP)); |
6d2010ae A |
838 | m->m_pkthdr.csum_flags = CSUM_TCPIPV6; |
839 | m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); | |
1c79356b | 840 | ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL, |
39037602 | 841 | ro6 && ro6->ro_rt ? ro6->ro_rt->rt_ifp : NULL); |
f427ee49 | 842 | } else { |
9bccf70c | 843 | nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, |
0a7de745 | 844 | htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p))); |
9bccf70c A |
845 | m->m_pkthdr.csum_flags = CSUM_TCP; |
846 | m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); | |
1c79356b | 847 | } |
1c79356b | 848 | #if TCPDEBUG |
0a7de745 | 849 | if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) { |
9bccf70c | 850 | tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0); |
0a7de745 | 851 | } |
1c79356b | 852 | #endif |
fe8ab488 A |
853 | |
854 | #if NECP | |
f427ee49 | 855 | necp_mark_packet_from_socket(m, tp ? tp->t_inpcb : NULL, 0, 0, 0, 0); |
fe8ab488 | 856 | #endif /* NECP */ |
3e170ce0 | 857 | |
1c79356b | 858 | #if IPSEC |
fe8ab488 | 859 | if (tp != NULL && tp->t_inpcb->inp_sp != NULL && |
0a7de745 | 860 | ipsec_setsocket(m, tp ? tp->t_inpcb->inp_socket : NULL) != 0) { |
9bccf70c A |
861 | m_freem(m); |
862 | return; | |
863 | } | |
864 | #endif | |
6d2010ae | 865 | |
316670eb A |
866 | if (tp != NULL) { |
867 | u_int32_t svc_flags = 0; | |
868 | if (isipv6) { | |
869 | svc_flags |= PKT_SCF_IPV6; | |
870 | } | |
39037602 | 871 | sotc = tp->t_inpcb->inp_socket->so_traffic_class; |
f427ee49 A |
872 | if ((flags & TH_RST) == 0) { |
873 | set_packet_service_class(m, tp->t_inpcb->inp_socket, | |
874 | sotc, svc_flags); | |
875 | } else { | |
876 | m_set_service_class(m, MBUF_SC_BK_SYS); | |
877 | } | |
316670eb A |
878 | |
879 | /* Embed flowhash and flow control flags */ | |
39236c6e A |
880 | m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB; |
881 | m->m_pkthdr.pkt_flowid = tp->t_inpcb->inp_flowhash; | |
5ba3f43e | 882 | m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC | PKTF_FLOW_ADV); |
39236c6e | 883 | m->m_pkthdr.pkt_proto = IPPROTO_TCP; |
d9a64523 A |
884 | m->m_pkthdr.tx_tcp_pid = tp->t_inpcb->inp_socket->last_pid; |
885 | m->m_pkthdr.tx_tcp_e_pid = tp->t_inpcb->inp_socket->e_pid; | |
f427ee49 A |
886 | |
887 | if (flags & TH_RST) { | |
888 | m->m_pkthdr.comp_gencnt = tp->t_comp_gencnt; | |
889 | } | |
890 | } else { | |
891 | if (flags & TH_RST) { | |
892 | m->m_pkthdr.comp_gencnt = TCP_ACK_COMPRESSION_DUMMY; | |
893 | m_set_service_class(m, MBUF_SC_BK_SYS); | |
894 | } | |
316670eb | 895 | } |
6d2010ae | 896 | |
1c79356b | 897 | if (isipv6) { |
a39ff7e2 A |
898 | struct ip6_out_args ip6oa; |
899 | bzero(&ip6oa, sizeof(ip6oa)); | |
900 | ip6oa.ip6oa_boundif = tra->ifscope; | |
901 | ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR; | |
902 | ip6oa.ip6oa_sotc = SO_TC_UNSPEC; | |
903 | ip6oa.ip6oa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; | |
316670eb | 904 | |
0a7de745 | 905 | if (tra->ifscope != IFSCOPE_NONE) { |
316670eb | 906 | ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; |
0a7de745 A |
907 | } |
908 | if (tra->nocell) { | |
316670eb | 909 | ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR; |
0a7de745 A |
910 | } |
911 | if (tra->noexpensive) { | |
fe8ab488 | 912 | ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE; |
0a7de745 | 913 | } |
cb323159 A |
914 | if (tra->noconstrained) { |
915 | ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED; | |
916 | } | |
0a7de745 | 917 | if (tra->awdl_unrestricted) { |
fe8ab488 | 918 | ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED; |
0a7de745 A |
919 | } |
920 | if (tra->intcoproc_allowed) { | |
39037602 | 921 | ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED; |
0a7de745 | 922 | } |
39037602 A |
923 | ip6oa.ip6oa_sotc = sotc; |
924 | if (tp != NULL) { | |
0a7de745 | 925 | if ((tp->t_inpcb->inp_socket->so_flags1 & SOF1_QOSMARKING_ALLOWED)) { |
39037602 | 926 | ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED; |
0a7de745 | 927 | } |
f427ee49 A |
928 | ip6oa.qos_marking_gencount = tp->t_inpcb->inp_policyresult.results.qos_marking_gencount; |
929 | if (check_qos_marking_again) { | |
930 | ip6oa.ip6oa_flags |= IP6OAF_REDO_QOSMARKING_POLICY; | |
931 | } | |
39037602 A |
932 | ip6oa.ip6oa_netsvctype = tp->t_inpcb->inp_socket->so_netsvctype; |
933 | } | |
6d2010ae A |
934 | (void) ip6_output(m, NULL, ro6, IPV6_OUTARGS, NULL, |
935 | NULL, &ip6oa); | |
39236c6e | 936 | |
f427ee49 A |
937 | if (check_qos_marking_again) { |
938 | struct inpcb *inp = tp->t_inpcb; | |
939 | inp->inp_policyresult.results.qos_marking_gencount = ip6oa.qos_marking_gencount; | |
940 | if (ip6oa.ip6oa_flags & IP6OAF_QOSMARKING_ALLOWED) { | |
941 | inp->inp_socket->so_flags1 |= SOF1_QOSMARKING_ALLOWED; | |
942 | } else { | |
943 | inp->inp_socket->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED; | |
944 | } | |
945 | } | |
946 | ||
39236c6e A |
947 | if (tp != NULL && ro6 != NULL && ro6->ro_rt != NULL && |
948 | (outif = ro6->ro_rt->rt_ifp) != | |
5ba3f43e | 949 | tp->t_inpcb->in6p_last_outifp) { |
39236c6e | 950 | tp->t_inpcb->in6p_last_outifp = outif; |
5ba3f43e | 951 | } |
39236c6e | 952 | |
0a7de745 | 953 | if (ro6 == &sro6) { |
39236c6e | 954 | ROUTE_RELEASE(ro6); |
0a7de745 | 955 | } |
f427ee49 | 956 | } else { |
a39ff7e2 A |
957 | struct ip_out_args ipoa; |
958 | bzero(&ipoa, sizeof(ipoa)); | |
959 | ipoa.ipoa_boundif = tra->ifscope; | |
960 | ipoa.ipoa_flags = IPOAF_SELECT_SRCIF | IPOAF_BOUND_SRCADDR; | |
961 | ipoa.ipoa_sotc = SO_TC_UNSPEC; | |
962 | ipoa.ipoa_netsvctype = _NET_SERVICE_TYPE_UNSPEC; | |
316670eb | 963 | |
0a7de745 | 964 | if (tra->ifscope != IFSCOPE_NONE) { |
316670eb | 965 | ipoa.ipoa_flags |= IPOAF_BOUND_IF; |
0a7de745 A |
966 | } |
967 | if (tra->nocell) { | |
316670eb | 968 | ipoa.ipoa_flags |= IPOAF_NO_CELLULAR; |
0a7de745 A |
969 | } |
970 | if (tra->noexpensive) { | |
fe8ab488 | 971 | ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE; |
0a7de745 | 972 | } |
cb323159 A |
973 | if (tra->noconstrained) { |
974 | ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED; | |
975 | } | |
0a7de745 | 976 | if (tra->awdl_unrestricted) { |
fe8ab488 | 977 | ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED; |
0a7de745 | 978 | } |
39037602 A |
979 | ipoa.ipoa_sotc = sotc; |
980 | if (tp != NULL) { | |
0a7de745 | 981 | if ((tp->t_inpcb->inp_socket->so_flags1 & SOF1_QOSMARKING_ALLOWED)) { |
39037602 | 982 | ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED; |
0a7de745 | 983 | } |
f427ee49 A |
984 | if (!(tp->t_inpcb->inp_socket->so_flags1 & SOF1_QOSMARKING_POLICY_OVERRIDE)) { |
985 | ipoa.ipoa_flags |= IPOAF_REDO_QOSMARKING_POLICY; | |
986 | } | |
987 | ipoa.qos_marking_gencount = tp->t_inpcb->inp_policyresult.results.qos_marking_gencount; | |
39037602 A |
988 | ipoa.ipoa_netsvctype = tp->t_inpcb->inp_socket->so_netsvctype; |
989 | } | |
b0d623f7 A |
990 | if (ro != &sro) { |
991 | /* Copy the cached route and take an extra reference */ | |
992 | inp_route_copyout(tp->t_inpcb, &sro); | |
993 | } | |
994 | /* | |
995 | * For consistency, pass a local route copy. | |
996 | */ | |
997 | (void) ip_output(m, NULL, &sro, IP_OUTARGS, NULL, &ipoa); | |
c910b4d9 | 998 | |
f427ee49 A |
999 | if (check_qos_marking_again) { |
1000 | struct inpcb *inp = tp->t_inpcb; | |
1001 | inp->inp_policyresult.results.qos_marking_gencount = ipoa.qos_marking_gencount; | |
1002 | if (ipoa.ipoa_flags & IPOAF_QOSMARKING_ALLOWED) { | |
1003 | inp->inp_socket->so_flags1 |= SOF1_QOSMARKING_ALLOWED; | |
1004 | } else { | |
1005 | inp->inp_socket->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED; | |
1006 | } | |
1007 | } | |
39236c6e A |
1008 | if (tp != NULL && sro.ro_rt != NULL && |
1009 | (outif = sro.ro_rt->rt_ifp) != | |
5ba3f43e | 1010 | tp->t_inpcb->inp_last_outifp) { |
39236c6e | 1011 | tp->t_inpcb->inp_last_outifp = outif; |
5ba3f43e | 1012 | } |
b0d623f7 A |
1013 | if (ro != &sro) { |
1014 | /* Synchronize cached PCB route */ | |
1015 | inp_route_copyin(tp->t_inpcb, &sro); | |
39236c6e A |
1016 | } else { |
1017 | ROUTE_RELEASE(&sro); | |
9bccf70c | 1018 | } |
1c79356b | 1019 | } |
1c79356b A |
1020 | } |
1021 | ||
1022 | /* | |
1023 | * Create a new TCP control block, making an | |
1024 | * empty reassembly queue and hooking it to the argument | |
1025 | * protocol control block. The `inp' parameter must have | |
1026 | * come from the zone allocator set up in tcp_init(). | |
1027 | */ | |
1028 | struct tcpcb * | |
39037602 | 1029 | tcp_newtcpcb(struct inpcb *inp) |
1c79356b A |
1030 | { |
1031 | struct inp_tp *it; | |
39037602 A |
1032 | struct tcpcb *tp; |
1033 | struct socket *so = inp->inp_socket; | |
9bccf70c | 1034 | int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; |
c3c9b80d | 1035 | uint32_t random_32; |
1c79356b | 1036 | |
6d2010ae A |
1037 | calculate_tcp_clock(); |
1038 | ||
3e170ce0 | 1039 | if ((so->so_flags1 & SOF1_CACHED_IN_SOCK_LAYER) == 0) { |
0a7de745 A |
1040 | it = (struct inp_tp *)(void *)inp; |
1041 | tp = &it->tcb; | |
39236c6e | 1042 | } else { |
0a7de745 | 1043 | tp = (struct tcpcb *)(void *)inp->inp_saved_ppcb; |
39236c6e | 1044 | } |
39037602 | 1045 | |
1c79356b | 1046 | bzero((char *) tp, sizeof(struct tcpcb)); |
9bccf70c | 1047 | LIST_INIT(&tp->t_segq); |
f427ee49 | 1048 | tp->t_maxseg = tp->t_maxopd = isipv6 ? tcp_v6mssdflt : tcp_mssdflt; |
9bccf70c | 1049 | |
f427ee49 A |
1050 | tp->t_flags = (TF_REQ_SCALE | TF_REQ_TSTMP); |
1051 | tp->t_flagsext |= TF_SACK_ENABLE; | |
39236c6e | 1052 | |
8ad349bb | 1053 | TAILQ_INIT(&tp->snd_holes); |
3e170ce0 | 1054 | SLIST_INIT(&tp->t_rxt_segments); |
39037602 A |
1055 | SLIST_INIT(&tp->t_notify_ack); |
1056 | tp->t_inpcb = inp; | |
1c79356b A |
1057 | /* |
1058 | * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no | |
1059 | * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives | |
1060 | * reasonable initial retransmit time. | |
1061 | */ | |
1062 | tp->t_srtt = TCPTV_SRTTBASE; | |
39037602 A |
1063 | tp->t_rttvar = |
1064 | ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; | |
2d21ac55 | 1065 | tp->t_rttmin = tcp_TCPTV_MIN; |
1c79356b | 1066 | tp->t_rxtcur = TCPTV_RTOBASE; |
6d2010ae | 1067 | |
0a7de745 | 1068 | if (tcp_use_newreno) { |
fe8ab488 A |
1069 | /* use newreno by default */ |
1070 | tp->tcp_cc_index = TCP_CC_ALGO_NEWRENO_INDEX; | |
0a7de745 | 1071 | } else { |
fe8ab488 | 1072 | tp->tcp_cc_index = TCP_CC_ALGO_CUBIC_INDEX; |
0a7de745 | 1073 | } |
fe8ab488 A |
1074 | |
1075 | tcp_cc_allocate_state(tp); | |
1076 | ||
0a7de745 | 1077 | if (CC_ALGO(tp)->init != NULL) { |
6d2010ae | 1078 | CC_ALGO(tp)->init(tp); |
0a7de745 | 1079 | } |
6d2010ae | 1080 | |
f427ee49 | 1081 | tp->snd_cwnd = tcp_initial_cwnd(tp); |
1c79356b | 1082 | tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; |
cf7d32b8 | 1083 | tp->snd_ssthresh_prev = TCP_MAXWIN << TCP_MAX_WINSHIFT; |
6d2010ae | 1084 | tp->t_rcvtime = tcp_now; |
6d2010ae | 1085 | tp->tentry.timer_start = tcp_now; |
cb323159 | 1086 | tp->rcv_unackwin = tcp_now; |
6d2010ae A |
1087 | tp->t_persist_timeout = tcp_max_persist_timeout; |
1088 | tp->t_persist_stop = 0; | |
1089 | tp->t_flagsext |= TF_RCVUNACK_WAITSS; | |
f427ee49 A |
1090 | tp->t_rexmtthresh = (uint8_t)tcprexmtthresh; |
1091 | tp->rfbuf_ts = tcp_now; | |
1092 | tp->rfbuf_space = tcp_initial_cwnd(tp); | |
1093 | tp->t_forced_acks = TCP_FORCED_ACKS_COUNT; | |
39236c6e | 1094 | |
5ba3f43e A |
1095 | /* Enable bandwidth measurement on this connection */ |
1096 | tp->t_flagsext |= TF_MEASURESNDBW; | |
1097 | if (tp->t_bwmeas == NULL) { | |
1098 | tp->t_bwmeas = tcp_bwmeas_alloc(tp); | |
0a7de745 | 1099 | if (tp->t_bwmeas == NULL) { |
5ba3f43e | 1100 | tp->t_flagsext &= ~TF_MEASURESNDBW; |
0a7de745 | 1101 | } |
5ba3f43e A |
1102 | } |
1103 | ||
39236c6e A |
1104 | /* Clear time wait tailq entry */ |
1105 | tp->t_twentry.tqe_next = NULL; | |
1106 | tp->t_twentry.tqe_prev = NULL; | |
1107 | ||
c3c9b80d | 1108 | read_frandom(&random_32, sizeof(random_32)); |
f427ee49 | 1109 | if (__probable(tcp_do_ack_compression)) { |
c3c9b80d | 1110 | tp->t_comp_gencnt = random_32; |
f427ee49 A |
1111 | if (tp->t_comp_gencnt <= TCP_ACK_COMPRESSION_DUMMY) { |
1112 | tp->t_comp_gencnt = TCP_ACK_COMPRESSION_DUMMY + 1; | |
1113 | } | |
1114 | tp->t_comp_lastinc = tcp_now; | |
1115 | } | |
1116 | ||
c3c9b80d A |
1117 | if (__probable(tcp_randomize_timestamps)) { |
1118 | tp->t_ts_offset = random_32; | |
1119 | } | |
1120 | ||
8ad349bb | 1121 | /* |
1c79356b A |
1122 | * IPv4 TTL initialization is necessary for an IPv6 socket as well, |
1123 | * because the socket may be bound to an IPv6 wildcard address, | |
1124 | * which may match an IPv4-mapped IPv6 address. | |
1c79356b | 1125 | */ |
f427ee49 | 1126 | inp->inp_ip_ttl = (uint8_t)ip_defttl; |
1c79356b | 1127 | inp->inp_ppcb = (caddr_t)tp; |
0a7de745 | 1128 | return tp; /* XXX */ |
1c79356b A |
1129 | } |
1130 | ||
1131 | /* | |
1132 | * Drop a TCP connection, reporting | |
1133 | * the specified error. If connection is synchronized, | |
1134 | * then send a RST to peer. | |
1135 | */ | |
1136 | struct tcpcb * | |
39037602 | 1137 | tcp_drop(struct tcpcb *tp, int errno) |
1c79356b A |
1138 | { |
1139 | struct socket *so = tp->t_inpcb->inp_socket; | |
6d2010ae A |
1140 | #if CONFIG_DTRACE |
1141 | struct inpcb *inp = tp->t_inpcb; | |
316670eb | 1142 | #endif |
6d2010ae | 1143 | |
1c79356b | 1144 | if (TCPS_HAVERCVDSYN(tp->t_state)) { |
6d2010ae | 1145 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
0a7de745 | 1146 | struct tcpcb *, tp, int32_t, TCPS_CLOSED); |
1c79356b A |
1147 | tp->t_state = TCPS_CLOSED; |
1148 | (void) tcp_output(tp); | |
1149 | tcpstat.tcps_drops++; | |
0a7de745 | 1150 | } else { |
1c79356b | 1151 | tcpstat.tcps_conndrops++; |
0a7de745 A |
1152 | } |
1153 | if (errno == ETIMEDOUT && tp->t_softerror) { | |
1c79356b | 1154 | errno = tp->t_softerror; |
0a7de745 | 1155 | } |
f427ee49 | 1156 | so->so_error = (u_short)errno; |
cb323159 A |
1157 | |
1158 | TCP_LOG_CONNECTION_SUMMARY(tp); | |
1159 | ||
0a7de745 | 1160 | return tcp_close(tp); |
1c79356b A |
1161 | } |
1162 | ||
316670eb | 1163 | void |
39037602 | 1164 | tcp_getrt_rtt(struct tcpcb *tp, struct rtentry *rt) |
316670eb A |
1165 | { |
1166 | u_int32_t rtt = rt->rt_rmx.rmx_rtt; | |
1167 | int isnetlocal = (tp->t_flags & TF_LOCAL); | |
1168 | ||
cb323159 A |
1169 | TCP_LOG_RTM_RTT(tp, rt); |
1170 | ||
1171 | if (rtt != 0 && tcp_init_rtt_from_cache != 0) { | |
316670eb A |
1172 | /* |
1173 | * XXX the lock bit for RTT indicates that the value | |
1174 | * is also a minimum value; this is subject to time. | |
1175 | */ | |
0a7de745 | 1176 | if (rt->rt_rmx.rmx_locks & RTV_RTT) { |
316670eb | 1177 | tp->t_rttmin = rtt / (RTM_RTTUNIT / TCP_RETRANSHZ); |
0a7de745 | 1178 | } else { |
39037602 A |
1179 | tp->t_rttmin = isnetlocal ? tcp_TCPTV_MIN : |
1180 | TCPTV_REXMTMIN; | |
0a7de745 | 1181 | } |
cb323159 | 1182 | |
39037602 A |
1183 | tp->t_srtt = |
1184 | rtt / (RTM_RTTUNIT / (TCP_RETRANSHZ * TCP_RTT_SCALE)); | |
316670eb | 1185 | tcpstat.tcps_usedrtt++; |
cb323159 | 1186 | |
316670eb A |
1187 | if (rt->rt_rmx.rmx_rttvar) { |
1188 | tp->t_rttvar = rt->rt_rmx.rmx_rttvar / | |
39037602 | 1189 | (RTM_RTTUNIT / (TCP_RETRANSHZ * TCP_RTTVAR_SCALE)); |
316670eb A |
1190 | tcpstat.tcps_usedrttvar++; |
1191 | } else { | |
1192 | /* default variation is +- 1 rtt */ | |
1193 | tp->t_rttvar = | |
39037602 | 1194 | tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; |
316670eb | 1195 | } |
cb323159 A |
1196 | |
1197 | /* | |
1198 | * The RTO formula in the route metric case is based on: | |
1199 | * 4 * srtt + 8 * rttvar | |
1200 | * modulo the min, max and slop | |
1201 | */ | |
316670eb | 1202 | TCPT_RANGESET(tp->t_rxtcur, |
0a7de745 A |
1203 | ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, |
1204 | tp->t_rttmin, TCPTV_REXMTMAX, | |
1205 | TCP_ADD_REXMTSLOP(tp)); | |
316670eb | 1206 | } |
cb323159 A |
1207 | |
1208 | TCP_LOG_RTT_INFO(tp); | |
316670eb A |
1209 | } |
1210 | ||
4bd07ac2 | 1211 | static inline void |
5ba3f43e A |
1212 | tcp_create_ifnet_stats_per_flow(struct tcpcb *tp, |
1213 | struct ifnet_stats_per_flow *ifs) | |
1214 | { | |
1215 | struct inpcb *inp; | |
1216 | struct socket *so; | |
0a7de745 | 1217 | if (tp == NULL || ifs == NULL) { |
5ba3f43e | 1218 | return; |
0a7de745 | 1219 | } |
5ba3f43e A |
1220 | |
1221 | bzero(ifs, sizeof(*ifs)); | |
1222 | inp = tp->t_inpcb; | |
1223 | so = inp->inp_socket; | |
1224 | ||
1225 | ifs->ipv4 = (inp->inp_vflag & INP_IPV6) ? 0 : 1; | |
1226 | ifs->local = (tp->t_flags & TF_LOCAL) ? 1 : 0; | |
1227 | ifs->connreset = (so->so_error == ECONNRESET) ? 1 : 0; | |
1228 | ifs->conntimeout = (so->so_error == ETIMEDOUT) ? 1 : 0; | |
1229 | ifs->ecn_flags = tp->ecn_flags; | |
1230 | ifs->txretransmitbytes = tp->t_stat.txretransmitbytes; | |
1231 | ifs->rxoutoforderbytes = tp->t_stat.rxoutoforderbytes; | |
1232 | ifs->rxmitpkts = tp->t_stat.rxmitpkts; | |
1233 | ifs->rcvoopack = tp->t_rcvoopack; | |
1234 | ifs->pawsdrop = tp->t_pawsdrop; | |
1235 | ifs->sack_recovery_episodes = tp->t_sack_recovery_episode; | |
1236 | ifs->reordered_pkts = tp->t_reordered_pkts; | |
1237 | ifs->dsack_sent = tp->t_dsack_sent; | |
1238 | ifs->dsack_recvd = tp->t_dsack_recvd; | |
1239 | ifs->srtt = tp->t_srtt; | |
1240 | ifs->rttupdated = tp->t_rttupdated; | |
1241 | ifs->rttvar = tp->t_rttvar; | |
1242 | ifs->rttmin = get_base_rtt(tp); | |
1243 | if (tp->t_bwmeas != NULL && tp->t_bwmeas->bw_sndbw_max > 0) { | |
1244 | ifs->bw_sndbw_max = tp->t_bwmeas->bw_sndbw_max; | |
1245 | } else { | |
1246 | ifs->bw_sndbw_max = 0; | |
1247 | } | |
0a7de745 | 1248 | if (tp->t_bwmeas != NULL && tp->t_bwmeas->bw_rcvbw_max > 0) { |
5ba3f43e A |
1249 | ifs->bw_rcvbw_max = tp->t_bwmeas->bw_rcvbw_max; |
1250 | } else { | |
1251 | ifs->bw_rcvbw_max = 0; | |
1252 | } | |
1253 | ifs->bk_txpackets = so->so_tc_stats[MBUF_TC_BK].txpackets; | |
1254 | ifs->txpackets = inp->inp_stat->txpackets; | |
1255 | ifs->rxpackets = inp->inp_stat->rxpackets; | |
1256 | } | |
1257 | ||
1258 | static inline void | |
1259 | tcp_flow_ecn_perf_stats(struct ifnet_stats_per_flow *ifs, | |
4bd07ac2 A |
1260 | struct if_tcp_ecn_perf_stat *stat) |
1261 | { | |
1262 | u_int64_t curval, oldval; | |
5ba3f43e A |
1263 | stat->total_txpkts += ifs->txpackets; |
1264 | stat->total_rxpkts += ifs->rxpackets; | |
1265 | stat->total_rxmitpkts += ifs->rxmitpkts; | |
1266 | stat->total_oopkts += ifs->rcvoopack; | |
1267 | stat->total_reorderpkts += (ifs->reordered_pkts + | |
1268 | ifs->pawsdrop + ifs->dsack_sent + ifs->dsack_recvd); | |
4bd07ac2 A |
1269 | |
1270 | /* Average RTT */ | |
5ba3f43e A |
1271 | curval = ifs->srtt >> TCP_RTT_SHIFT; |
1272 | if (curval > 0 && ifs->rttupdated >= 16) { | |
4bd07ac2 A |
1273 | if (stat->rtt_avg == 0) { |
1274 | stat->rtt_avg = curval; | |
1275 | } else { | |
1276 | oldval = stat->rtt_avg; | |
5ba3f43e | 1277 | stat->rtt_avg = ((oldval << 4) - oldval + curval) >> 4; |
4bd07ac2 A |
1278 | } |
1279 | } | |
1280 | ||
1281 | /* RTT variance */ | |
5ba3f43e A |
1282 | curval = ifs->rttvar >> TCP_RTTVAR_SHIFT; |
1283 | if (curval > 0 && ifs->rttupdated >= 16) { | |
4bd07ac2 A |
1284 | if (stat->rtt_var == 0) { |
1285 | stat->rtt_var = curval; | |
1286 | } else { | |
1287 | oldval = stat->rtt_var; | |
1288 | stat->rtt_var = | |
1289 | ((oldval << 4) - oldval + curval) >> 4; | |
1290 | } | |
1291 | } | |
1292 | ||
5ba3f43e A |
1293 | /* SACK episodes */ |
1294 | stat->sack_episodes += ifs->sack_recovery_episodes; | |
0a7de745 | 1295 | if (ifs->connreset) { |
490019cf | 1296 | stat->rst_drop++; |
0a7de745 | 1297 | } |
4bd07ac2 A |
1298 | } |
1299 | ||
5ba3f43e A |
1300 | static inline void |
1301 | tcp_flow_lim_stats(struct ifnet_stats_per_flow *ifs, | |
1302 | struct if_lim_perf_stat *stat) | |
1303 | { | |
1304 | u_int64_t curval, oldval; | |
1305 | ||
1306 | stat->lim_total_txpkts += ifs->txpackets; | |
1307 | stat->lim_total_rxpkts += ifs->rxpackets; | |
1308 | stat->lim_total_retxpkts += ifs->rxmitpkts; | |
1309 | stat->lim_total_oopkts += ifs->rcvoopack; | |
1310 | ||
1311 | if (ifs->bw_sndbw_max > 0) { | |
1312 | /* convert from bytes per ms to bits per second */ | |
1313 | ifs->bw_sndbw_max *= 8000; | |
f427ee49 | 1314 | stat->lim_ul_max_bandwidth = MAX(stat->lim_ul_max_bandwidth, |
5ba3f43e A |
1315 | ifs->bw_sndbw_max); |
1316 | } | |
1317 | ||
1318 | if (ifs->bw_rcvbw_max > 0) { | |
1319 | /* convert from bytes per ms to bits per second */ | |
1320 | ifs->bw_rcvbw_max *= 8000; | |
f427ee49 | 1321 | stat->lim_dl_max_bandwidth = MAX(stat->lim_dl_max_bandwidth, |
5ba3f43e A |
1322 | ifs->bw_rcvbw_max); |
1323 | } | |
1324 | ||
1325 | /* Average RTT */ | |
1326 | curval = ifs->srtt >> TCP_RTT_SHIFT; | |
1327 | if (curval > 0 && ifs->rttupdated >= 16) { | |
1328 | if (stat->lim_rtt_average == 0) { | |
1329 | stat->lim_rtt_average = curval; | |
1330 | } else { | |
1331 | oldval = stat->lim_rtt_average; | |
1332 | stat->lim_rtt_average = | |
1333 | ((oldval << 4) - oldval + curval) >> 4; | |
1334 | } | |
1335 | } | |
1336 | ||
1337 | /* RTT variance */ | |
1338 | curval = ifs->rttvar >> TCP_RTTVAR_SHIFT; | |
1339 | if (curval > 0 && ifs->rttupdated >= 16) { | |
1340 | if (stat->lim_rtt_variance == 0) { | |
1341 | stat->lim_rtt_variance = curval; | |
1342 | } else { | |
1343 | oldval = stat->lim_rtt_variance; | |
1344 | stat->lim_rtt_variance = | |
1345 | ((oldval << 4) - oldval + curval) >> 4; | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | if (stat->lim_rtt_min == 0) { | |
1350 | stat->lim_rtt_min = ifs->rttmin; | |
1351 | } else { | |
f427ee49 | 1352 | stat->lim_rtt_min = MIN(stat->lim_rtt_min, ifs->rttmin); |
5ba3f43e A |
1353 | } |
1354 | ||
1355 | /* connection timeouts */ | |
1356 | stat->lim_conn_attempts++; | |
0a7de745 | 1357 | if (ifs->conntimeout) { |
5ba3f43e | 1358 | stat->lim_conn_timeouts++; |
0a7de745 | 1359 | } |
5ba3f43e A |
1360 | |
1361 | /* bytes sent using background delay-based algorithms */ | |
1362 | stat->lim_bk_txpkts += ifs->bk_txpackets; | |
5ba3f43e A |
1363 | } |
1364 | ||
1c79356b A |
1365 | /* |
1366 | * Close a TCP control block: | |
1367 | * discard all space held by the tcp | |
1368 | * discard internet protocol block | |
1369 | * wake up any sleepers | |
1370 | */ | |
1371 | struct tcpcb * | |
39037602 | 1372 | tcp_close(struct tcpcb *tp) |
1c79356b | 1373 | { |
1c79356b A |
1374 | struct inpcb *inp = tp->t_inpcb; |
1375 | struct socket *so = inp->inp_socket; | |
9bccf70c | 1376 | int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; |
39236c6e | 1377 | struct route *ro; |
b0d623f7 | 1378 | struct rtentry *rt; |
1c79356b | 1379 | int dosavessthresh; |
5ba3f43e | 1380 | struct ifnet_stats_per_flow ifs; |
1c79356b | 1381 | |
39236c6e | 1382 | /* tcp_close was called previously, bail */ |
0a7de745 A |
1383 | if (inp->inp_ppcb == NULL) { |
1384 | return NULL; | |
1385 | } | |
1c79356b | 1386 | |
f427ee49 A |
1387 | tcp_del_fsw_flow(tp); |
1388 | ||
6d2010ae | 1389 | tcp_canceltimers(tp); |
39037602 | 1390 | KERNEL_DEBUG(DBG_FNC_TCP_CLOSE | DBG_FUNC_START, tp, 0, 0, 0, 0); |
1c79356b | 1391 | |
2d21ac55 A |
1392 | /* |
1393 | * If another thread for this tcp is currently in ip (indicated by | |
1394 | * the TF_SENDINPROG flag), defer the cleanup until after it returns | |
1395 | * back to tcp. This is done to serialize the close until after all | |
1396 | * pending output is finished, in order to avoid having the PCB be | |
1397 | * detached and the cached route cleaned, only for ip to cache the | |
1398 | * route back into the PCB again. Note that we've cleared all the | |
1399 | * timers at this point. Set TF_CLOSING to indicate to tcp_output() | |
1400 | * that is should call us again once it returns from ip; at that | |
1401 | * point both flags should be cleared and we can proceed further | |
1402 | * with the cleanup. | |
1403 | */ | |
316670eb | 1404 | if ((tp->t_flags & TF_CLOSING) || |
0a7de745 | 1405 | inp->inp_sndinprog_cnt > 0) { |
2d21ac55 | 1406 | tp->t_flags |= TF_CLOSING; |
0a7de745 | 1407 | return NULL; |
2d21ac55 | 1408 | } |
1c79356b | 1409 | |
cb323159 A |
1410 | TCP_LOG_CONNECTION_SUMMARY(tp); |
1411 | ||
39236c6e | 1412 | DTRACE_TCP4(state__change, void, NULL, struct inpcb *, inp, |
0a7de745 | 1413 | struct tcpcb *, tp, int32_t, TCPS_CLOSED); |
39236c6e | 1414 | |
39236c6e | 1415 | ro = (isipv6 ? (struct route *)&inp->in6p_route : &inp->inp_route); |
39236c6e | 1416 | rt = ro->ro_rt; |
0a7de745 | 1417 | if (rt != NULL) { |
b0d623f7 | 1418 | RT_LOCK_SPIN(rt); |
0a7de745 | 1419 | } |
b0d623f7 | 1420 | |
1c79356b A |
1421 | /* |
1422 | * If we got enough samples through the srtt filter, | |
1423 | * save the rtt and rttvar in the routing entry. | |
1424 | * 'Enough' is arbitrarily defined as the 16 samples. | |
1425 | * 16 samples is enough for the srtt filter to converge | |
1426 | * to within 5% of the correct value; fewer samples and | |
1427 | * we could save a very bogus rtt. | |
1428 | * | |
1429 | * Don't update the default route's characteristics and don't | |
1430 | * update anything that the user "locked". | |
1431 | */ | |
1432 | if (tp->t_rttupdated >= 16) { | |
39037602 | 1433 | u_int32_t i = 0; |
cb323159 | 1434 | bool log_rtt = false; |
2d21ac55 | 1435 | |
1c79356b A |
1436 | if (isipv6) { |
1437 | struct sockaddr_in6 *sin6; | |
1438 | ||
0a7de745 | 1439 | if (rt == NULL) { |
1c79356b | 1440 | goto no_valid_rt; |
0a7de745 | 1441 | } |
316670eb | 1442 | sin6 = (struct sockaddr_in6 *)(void *)rt_key(rt); |
0a7de745 | 1443 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
1c79356b | 1444 | goto no_valid_rt; |
0a7de745 | 1445 | } |
f427ee49 | 1446 | } else if (ROUTE_UNUSABLE(ro) || |
39236c6e | 1447 | SIN(rt_key(rt))->sin_addr.s_addr == INADDR_ANY) { |
fe8ab488 A |
1448 | DTRACE_TCP4(state__change, void, NULL, |
1449 | struct inpcb *, inp, struct tcpcb *, tp, | |
1450 | int32_t, TCPS_CLOSED); | |
1451 | tp->t_state = TCPS_CLOSED; | |
1c79356b | 1452 | goto no_valid_rt; |
55e303ae | 1453 | } |
1c79356b | 1454 | |
b0d623f7 | 1455 | RT_LOCK_ASSERT_HELD(rt); |
1c79356b A |
1456 | if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { |
1457 | i = tp->t_srtt * | |
2d21ac55 | 1458 | (RTM_RTTUNIT / (TCP_RETRANSHZ * TCP_RTT_SCALE)); |
0a7de745 | 1459 | if (rt->rt_rmx.rmx_rtt && i) { |
1c79356b A |
1460 | /* |
1461 | * filter this update to half the old & half | |
1462 | * the new values, converting scale. | |
1463 | * See route.h and tcp_var.h for a | |
1464 | * description of the scaling constants. | |
1465 | */ | |
1466 | rt->rt_rmx.rmx_rtt = | |
1467 | (rt->rt_rmx.rmx_rtt + i) / 2; | |
0a7de745 | 1468 | } else { |
1c79356b | 1469 | rt->rt_rmx.rmx_rtt = i; |
0a7de745 | 1470 | } |
1c79356b | 1471 | tcpstat.tcps_cachedrtt++; |
cb323159 | 1472 | log_rtt = true; |
1c79356b A |
1473 | } |
1474 | if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { | |
1475 | i = tp->t_rttvar * | |
2d21ac55 | 1476 | (RTM_RTTUNIT / (TCP_RETRANSHZ * TCP_RTTVAR_SCALE)); |
0a7de745 | 1477 | if (rt->rt_rmx.rmx_rttvar && i) { |
1c79356b A |
1478 | rt->rt_rmx.rmx_rttvar = |
1479 | (rt->rt_rmx.rmx_rttvar + i) / 2; | |
0a7de745 | 1480 | } else { |
1c79356b | 1481 | rt->rt_rmx.rmx_rttvar = i; |
0a7de745 | 1482 | } |
1c79356b | 1483 | tcpstat.tcps_cachedrttvar++; |
cb323159 A |
1484 | log_rtt = true; |
1485 | } | |
1486 | if (log_rtt) { | |
1487 | TCP_LOG_RTM_RTT(tp, rt); | |
1488 | TCP_LOG_RTT_INFO(tp); | |
1c79356b A |
1489 | } |
1490 | /* | |
1491 | * The old comment here said: | |
1492 | * update the pipelimit (ssthresh) if it has been updated | |
1493 | * already or if a pipesize was specified & the threshhold | |
1494 | * got below half the pipesize. I.e., wait for bad news | |
1495 | * before we start updating, then update on both good | |
1496 | * and bad news. | |
1497 | * | |
1498 | * But we want to save the ssthresh even if no pipesize is | |
1499 | * specified explicitly in the route, because such | |
1500 | * connections still have an implicit pipesize specified | |
1501 | * by the global tcp_sendspace. In the absence of a reliable | |
1502 | * way to calculate the pipesize, it will have to do. | |
1503 | */ | |
1504 | i = tp->snd_ssthresh; | |
0a7de745 | 1505 | if (rt->rt_rmx.rmx_sendpipe != 0) { |
1c79356b | 1506 | dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2); |
0a7de745 | 1507 | } else { |
1c79356b | 1508 | dosavessthresh = (i < so->so_snd.sb_hiwat / 2); |
0a7de745 | 1509 | } |
1c79356b | 1510 | if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && |
39037602 A |
1511 | i != 0 && rt->rt_rmx.rmx_ssthresh != 0) || |
1512 | dosavessthresh) { | |
1c79356b A |
1513 | /* |
1514 | * convert the limit from user data bytes to | |
1515 | * packets then to packet data bytes. | |
1516 | */ | |
1517 | i = (i + tp->t_maxseg / 2) / tp->t_maxseg; | |
0a7de745 | 1518 | if (i < 2) { |
1c79356b | 1519 | i = 2; |
0a7de745 | 1520 | } |
b0d623f7 | 1521 | i *= (u_int32_t)(tp->t_maxseg + |
0a7de745 A |
1522 | isipv6 ? sizeof(struct ip6_hdr) + |
1523 | sizeof(struct tcphdr) : | |
0a7de745 A |
1524 | sizeof(struct tcpiphdr)); |
1525 | if (rt->rt_rmx.rmx_ssthresh) { | |
1c79356b A |
1526 | rt->rt_rmx.rmx_ssthresh = |
1527 | (rt->rt_rmx.rmx_ssthresh + i) / 2; | |
0a7de745 | 1528 | } else { |
1c79356b | 1529 | rt->rt_rmx.rmx_ssthresh = i; |
0a7de745 | 1530 | } |
1c79356b A |
1531 | tcpstat.tcps_cachedssthresh++; |
1532 | } | |
1533 | } | |
b0d623f7 A |
1534 | |
1535 | /* | |
1536 | * Mark route for deletion if no information is cached. | |
1537 | */ | |
f427ee49 | 1538 | if (rt != NULL && (so->so_flags & SOF_OVERFLOW)) { |
b0d623f7 A |
1539 | if (!(rt->rt_rmx.rmx_locks & RTV_RTT) && |
1540 | rt->rt_rmx.rmx_rtt == 0) { | |
1541 | rt->rt_flags |= RTF_DELCLONE; | |
9bccf70c A |
1542 | } |
1543 | } | |
2d21ac55 | 1544 | |
b0d623f7 | 1545 | no_valid_rt: |
0a7de745 | 1546 | if (rt != NULL) { |
b0d623f7 | 1547 | RT_UNLOCK(rt); |
0a7de745 | 1548 | } |
b0d623f7 A |
1549 | |
1550 | /* free the reassembly queue, if any */ | |
1c79356b | 1551 | (void) tcp_freeq(tp); |
4bd07ac2 | 1552 | |
5ba3f43e A |
1553 | /* performance stats per interface */ |
1554 | tcp_create_ifnet_stats_per_flow(tp, &ifs); | |
1555 | tcp_update_stats_per_flow(&ifs, inp->inp_last_outifp); | |
1c79356b | 1556 | |
8ad349bb | 1557 | tcp_free_sackholes(tp); |
39037602 A |
1558 | tcp_notify_ack_free(tp); |
1559 | ||
1560 | inp_decr_sndbytes_allunsent(so, tp->snd_una); | |
1561 | ||
316670eb A |
1562 | if (tp->t_bwmeas != NULL) { |
1563 | tcp_bwmeas_free(tp); | |
1564 | } | |
3e170ce0 | 1565 | tcp_rxtseg_clean(tp); |
2d21ac55 | 1566 | /* Free the packet list */ |
0a7de745 | 1567 | if (tp->t_pktlist_head != NULL) { |
2d21ac55 | 1568 | m_freem_list(tp->t_pktlist_head); |
0a7de745 | 1569 | } |
2d21ac55 A |
1570 | TCP_PKTLIST_CLEAR(tp); |
1571 | ||
0a7de745 A |
1572 | if (so->so_flags1 & SOF1_CACHED_IN_SOCK_LAYER) { |
1573 | inp->inp_saved_ppcb = (caddr_t) tp; | |
1574 | } | |
39236c6e | 1575 | |
fe8ab488 A |
1576 | tp->t_state = TCPS_CLOSED; |
1577 | ||
39037602 A |
1578 | /* |
1579 | * Issue a wakeup before detach so that we don't miss | |
6d2010ae A |
1580 | * a wakeup |
1581 | */ | |
1582 | sodisconnectwakeup(so); | |
1c79356b | 1583 | |
cb323159 A |
1584 | /* |
1585 | * Make sure to clear the TCP Keep Alive Offload as it is | |
1586 | * ref counted on the interface | |
1587 | */ | |
1588 | tcp_clear_keep_alive_offload(so); | |
fe8ab488 A |
1589 | |
1590 | /* | |
1591 | * If this is a socket that does not want to wakeup the device | |
1592 | * for it's traffic, the application might need to know that the | |
1593 | * socket is closed, send a notification. | |
1594 | */ | |
1595 | if ((so->so_options & SO_NOWAKEFROMSLEEP) && | |
1596 | inp->inp_state != INPCB_STATE_DEAD && | |
0a7de745 | 1597 | !(inp->inp_flags2 & INP2_TIMEWAIT)) { |
fe8ab488 | 1598 | socket_post_kev_msg_closed(so); |
0a7de745 | 1599 | } |
fe8ab488 A |
1600 | |
1601 | if (CC_ALGO(tp)->cleanup != NULL) { | |
1602 | CC_ALGO(tp)->cleanup(tp); | |
1603 | } | |
1604 | ||
1605 | if (tp->t_ccstate != NULL) { | |
1606 | zfree(tcp_cc_zone, tp->t_ccstate); | |
1607 | tp->t_ccstate = NULL; | |
1608 | } | |
1609 | tp->tcp_cc_index = TCP_CC_ALGO_NONE; | |
1610 | ||
3e170ce0 A |
1611 | /* Can happen if we close the socket before receiving the third ACK */ |
1612 | if ((tp->t_tfo_flags & TFO_F_COOKIE_VALID)) { | |
1613 | OSDecrementAtomic(&tcp_tfo_halfcnt); | |
1614 | ||
1615 | /* Panic if something has gone terribly wrong. */ | |
1616 | VERIFY(tcp_tfo_halfcnt >= 0); | |
1617 | ||
1618 | tp->t_tfo_flags &= ~TFO_F_COOKIE_VALID; | |
1619 | } | |
1620 | ||
0a7de745 | 1621 | if (SOCK_CHECK_DOM(so, PF_INET6)) { |
1c79356b | 1622 | in6_pcbdetach(inp); |
f427ee49 A |
1623 | } else { |
1624 | in_pcbdetach(inp); | |
1625 | } | |
6d2010ae | 1626 | |
39037602 A |
1627 | /* |
1628 | * Call soisdisconnected after detach because it might unlock the socket | |
1629 | */ | |
6d2010ae | 1630 | soisdisconnected(so); |
1c79356b | 1631 | tcpstat.tcps_closed++; |
fe8ab488 A |
1632 | KERNEL_DEBUG(DBG_FNC_TCP_CLOSE | DBG_FUNC_END, |
1633 | tcpstat.tcps_closed, 0, 0, 0, 0); | |
0a7de745 | 1634 | return NULL; |
1c79356b A |
1635 | } |
1636 | ||
1637 | int | |
39037602 | 1638 | tcp_freeq(struct tcpcb *tp) |
1c79356b | 1639 | { |
39037602 | 1640 | struct tseg_qent *q; |
1c79356b A |
1641 | int rv = 0; |
1642 | ||
39037602 | 1643 | while ((q = LIST_FIRST(&tp->t_segq)) != NULL) { |
9bccf70c A |
1644 | LIST_REMOVE(q, tqe_q); |
1645 | m_freem(q->tqe_m); | |
6d2010ae | 1646 | zfree(tcp_reass_zone, q); |
1c79356b A |
1647 | rv = 1; |
1648 | } | |
fe8ab488 | 1649 | tp->t_reassqlen = 0; |
0a7de745 | 1650 | return rv; |
1c79356b A |
1651 | } |
1652 | ||
3e170ce0 | 1653 | |
1c79356b | 1654 | void |
39037602 | 1655 | tcp_drain(void) |
1c79356b | 1656 | { |
3e170ce0 A |
1657 | struct inpcb *inp; |
1658 | struct tcpcb *tp; | |
2d21ac55 | 1659 | |
0a7de745 | 1660 | if (!lck_rw_try_lock_exclusive(tcbinfo.ipi_lock)) { |
3e170ce0 | 1661 | return; |
0a7de745 | 1662 | } |
3e170ce0 A |
1663 | |
1664 | LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) { | |
1665 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != | |
0a7de745 | 1666 | WNT_STOPUSING) { |
5ba3f43e | 1667 | socket_lock(inp->inp_socket, 1); |
3e170ce0 | 1668 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) |
0a7de745 | 1669 | == WNT_STOPUSING) { |
3e170ce0 | 1670 | /* lost a race, try the next one */ |
5ba3f43e | 1671 | socket_unlock(inp->inp_socket, 1); |
3e170ce0 | 1672 | continue; |
39037602 | 1673 | } |
3e170ce0 A |
1674 | tp = intotcpcb(inp); |
1675 | ||
3e170ce0 A |
1676 | so_drain_extended_bk_idle(inp->inp_socket); |
1677 | ||
5ba3f43e | 1678 | socket_unlock(inp->inp_socket, 1); |
3e170ce0 | 1679 | } |
9bccf70c | 1680 | } |
3e170ce0 | 1681 | lck_rw_done(tcbinfo.ipi_lock); |
1c79356b A |
1682 | } |
1683 | ||
1684 | /* | |
1685 | * Notify a tcp user of an asynchronous error; | |
1686 | * store error as soft error, but wake up user | |
1687 | * (for now, won't do anything until can select for soft error). | |
9bccf70c A |
1688 | * |
1689 | * Do not wake up user since there currently is no mechanism for | |
1690 | * reporting soft errors (yet - a kqueue filter may be added). | |
1c79356b A |
1691 | */ |
1692 | static void | |
39037602 | 1693 | tcp_notify(struct inpcb *inp, int error) |
1c79356b | 1694 | { |
55e303ae A |
1695 | struct tcpcb *tp; |
1696 | ||
0a7de745 | 1697 | if (inp == NULL || (inp->inp_state == INPCB_STATE_DEAD)) { |
55e303ae | 1698 | return; /* pcb is gone already */ |
0a7de745 | 1699 | } |
55e303ae | 1700 | tp = (struct tcpcb *)inp->inp_ppcb; |
1c79356b | 1701 | |
5ba3f43e | 1702 | VERIFY(tp != NULL); |
1c79356b A |
1703 | /* |
1704 | * Ignore some errors if we are hooked up. | |
1705 | * If connection hasn't completed, has retransmitted several times, | |
1706 | * and receives a second error, give up now. This is better | |
1707 | * than waiting a long time to establish a connection that | |
1708 | * can never complete. | |
1709 | */ | |
1710 | if (tp->t_state == TCPS_ESTABLISHED && | |
39037602 A |
1711 | (error == EHOSTUNREACH || error == ENETUNREACH || |
1712 | error == EHOSTDOWN)) { | |
5ba3f43e A |
1713 | if (inp->inp_route.ro_rt) { |
1714 | rtfree(inp->inp_route.ro_rt); | |
1715 | inp->inp_route.ro_rt = (struct rtentry *)NULL; | |
1716 | } | |
1c79356b | 1717 | } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && |
0a7de745 | 1718 | tp->t_softerror) { |
9bccf70c | 1719 | tcp_drop(tp, error); |
0a7de745 | 1720 | } else { |
1c79356b | 1721 | tp->t_softerror = error; |
0a7de745 | 1722 | } |
1c79356b A |
1723 | } |
1724 | ||
39037602 | 1725 | struct bwmeas * |
316670eb A |
1726 | tcp_bwmeas_alloc(struct tcpcb *tp) |
1727 | { | |
1728 | struct bwmeas *elm; | |
f427ee49 | 1729 | elm = zalloc_flags(tcp_bwmeas_zone, Z_ZERO | Z_WAITOK); |
316670eb | 1730 | elm->bw_minsizepkts = TCP_BWMEAS_BURST_MINSIZE; |
316670eb | 1731 | elm->bw_minsize = elm->bw_minsizepkts * tp->t_maxseg; |
0a7de745 | 1732 | return elm; |
316670eb A |
1733 | } |
1734 | ||
1735 | void | |
39037602 | 1736 | tcp_bwmeas_free(struct tcpcb *tp) |
316670eb A |
1737 | { |
1738 | zfree(tcp_bwmeas_zone, tp->t_bwmeas); | |
1739 | tp->t_bwmeas = NULL; | |
1740 | tp->t_flagsext &= ~(TF_MEASURESNDBW); | |
1741 | } | |
1742 | ||
39037602 A |
1743 | int |
1744 | get_tcp_inp_list(struct inpcb **inp_list, int n, inp_gen_t gencnt) | |
1745 | { | |
1746 | struct tcpcb *tp; | |
1747 | struct inpcb *inp; | |
1748 | int i = 0; | |
1749 | ||
1750 | LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) { | |
1751 | if (inp->inp_gencnt <= gencnt && | |
0a7de745 | 1752 | inp->inp_state != INPCB_STATE_DEAD) { |
39037602 | 1753 | inp_list[i++] = inp; |
0a7de745 A |
1754 | } |
1755 | if (i >= n) { | |
39037602 | 1756 | break; |
0a7de745 | 1757 | } |
39037602 A |
1758 | } |
1759 | ||
1760 | TAILQ_FOREACH(tp, &tcp_tw_tailq, t_twentry) { | |
1761 | inp = tp->t_inpcb; | |
1762 | if (inp->inp_gencnt <= gencnt && | |
0a7de745 | 1763 | inp->inp_state != INPCB_STATE_DEAD) { |
39037602 | 1764 | inp_list[i++] = inp; |
0a7de745 A |
1765 | } |
1766 | if (i >= n) { | |
39037602 | 1767 | break; |
0a7de745 | 1768 | } |
39037602 | 1769 | } |
0a7de745 | 1770 | return i; |
39037602 A |
1771 | } |
1772 | ||
b0d623f7 A |
1773 | /* |
1774 | * tcpcb_to_otcpcb copies specific bits of a tcpcb to a otcpcb format. | |
1775 | * The otcpcb data structure is passed to user space and must not change. | |
1776 | */ | |
1777 | static void | |
1778 | tcpcb_to_otcpcb(struct tcpcb *tp, struct otcpcb *otp) | |
1779 | { | |
fe8ab488 | 1780 | otp->t_segq = (uint32_t)VM_KERNEL_ADDRPERM(tp->t_segq.lh_first); |
b0d623f7 | 1781 | otp->t_dupacks = tp->t_dupacks; |
fe8ab488 A |
1782 | otp->t_timer[TCPT_REXMT_EXT] = tp->t_timer[TCPT_REXMT]; |
1783 | otp->t_timer[TCPT_PERSIST_EXT] = tp->t_timer[TCPT_PERSIST]; | |
1784 | otp->t_timer[TCPT_KEEP_EXT] = tp->t_timer[TCPT_KEEP]; | |
1785 | otp->t_timer[TCPT_2MSL_EXT] = tp->t_timer[TCPT_2MSL]; | |
39037602 A |
1786 | otp->t_inpcb = |
1787 | (_TCPCB_PTR(struct inpcb *))VM_KERNEL_ADDRPERM(tp->t_inpcb); | |
b0d623f7 A |
1788 | otp->t_state = tp->t_state; |
1789 | otp->t_flags = tp->t_flags; | |
fe8ab488 | 1790 | otp->t_force = (tp->t_flagsext & TF_FORCE) ? 1 : 0; |
b0d623f7 A |
1791 | otp->snd_una = tp->snd_una; |
1792 | otp->snd_max = tp->snd_max; | |
1793 | otp->snd_nxt = tp->snd_nxt; | |
1794 | otp->snd_up = tp->snd_up; | |
1795 | otp->snd_wl1 = tp->snd_wl1; | |
1796 | otp->snd_wl2 = tp->snd_wl2; | |
1797 | otp->iss = tp->iss; | |
1798 | otp->irs = tp->irs; | |
1799 | otp->rcv_nxt = tp->rcv_nxt; | |
1800 | otp->rcv_adv = tp->rcv_adv; | |
1801 | otp->rcv_wnd = tp->rcv_wnd; | |
1802 | otp->rcv_up = tp->rcv_up; | |
1803 | otp->snd_wnd = tp->snd_wnd; | |
1804 | otp->snd_cwnd = tp->snd_cwnd; | |
1805 | otp->snd_ssthresh = tp->snd_ssthresh; | |
1806 | otp->t_maxopd = tp->t_maxopd; | |
1807 | otp->t_rcvtime = tp->t_rcvtime; | |
1808 | otp->t_starttime = tp->t_starttime; | |
1809 | otp->t_rtttime = tp->t_rtttime; | |
1810 | otp->t_rtseq = tp->t_rtseq; | |
1811 | otp->t_rxtcur = tp->t_rxtcur; | |
1812 | otp->t_maxseg = tp->t_maxseg; | |
1813 | otp->t_srtt = tp->t_srtt; | |
1814 | otp->t_rttvar = tp->t_rttvar; | |
1815 | otp->t_rxtshift = tp->t_rxtshift; | |
1816 | otp->t_rttmin = tp->t_rttmin; | |
1817 | otp->t_rttupdated = tp->t_rttupdated; | |
1818 | otp->max_sndwnd = tp->max_sndwnd; | |
1819 | otp->t_softerror = tp->t_softerror; | |
1820 | otp->t_oobflags = tp->t_oobflags; | |
1821 | otp->t_iobc = tp->t_iobc; | |
1822 | otp->snd_scale = tp->snd_scale; | |
1823 | otp->rcv_scale = tp->rcv_scale; | |
1824 | otp->request_r_scale = tp->request_r_scale; | |
1825 | otp->requested_s_scale = tp->requested_s_scale; | |
1826 | otp->ts_recent = tp->ts_recent; | |
1827 | otp->ts_recent_age = tp->ts_recent_age; | |
1828 | otp->last_ack_sent = tp->last_ack_sent; | |
490019cf A |
1829 | otp->cc_send = 0; |
1830 | otp->cc_recv = 0; | |
b0d623f7 A |
1831 | otp->snd_recover = tp->snd_recover; |
1832 | otp->snd_cwnd_prev = tp->snd_cwnd_prev; | |
1833 | otp->snd_ssthresh_prev = tp->snd_ssthresh_prev; | |
39236c6e | 1834 | otp->t_badrxtwin = 0; |
b0d623f7 A |
1835 | } |
1836 | ||
1c79356b A |
1837 | static int |
1838 | tcp_pcblist SYSCTL_HANDLER_ARGS | |
1839 | { | |
2d21ac55 | 1840 | #pragma unused(oidp, arg1, arg2) |
39236c6e | 1841 | int error, i = 0, n; |
39037602 | 1842 | struct inpcb **inp_list; |
1c79356b A |
1843 | inp_gen_t gencnt; |
1844 | struct xinpgen xig; | |
1845 | ||
1846 | /* | |
1847 | * The process of preparing the TCB list is too time-consuming and | |
1848 | * resource-intensive to repeat twice on every request. | |
1849 | */ | |
39236c6e | 1850 | lck_rw_lock_shared(tcbinfo.ipi_lock); |
91447636 | 1851 | if (req->oldptr == USER_ADDR_NULL) { |
1c79356b | 1852 | n = tcbinfo.ipi_count; |
39037602 | 1853 | req->oldidx = 2 * (sizeof(xig)) |
0a7de745 | 1854 | + (n + n / 8) * sizeof(struct xtcpcb); |
39236c6e | 1855 | lck_rw_done(tcbinfo.ipi_lock); |
0a7de745 | 1856 | return 0; |
1c79356b A |
1857 | } |
1858 | ||
91447636 | 1859 | if (req->newptr != USER_ADDR_NULL) { |
39236c6e | 1860 | lck_rw_done(tcbinfo.ipi_lock); |
0a7de745 | 1861 | return EPERM; |
91447636 | 1862 | } |
1c79356b A |
1863 | |
1864 | /* | |
1865 | * OK, now we're committed to doing something. | |
1866 | */ | |
1c79356b A |
1867 | gencnt = tcbinfo.ipi_gencnt; |
1868 | n = tcbinfo.ipi_count; | |
1c79356b | 1869 | |
3a60a9f5 | 1870 | bzero(&xig, sizeof(xig)); |
39037602 | 1871 | xig.xig_len = sizeof(xig); |
1c79356b A |
1872 | xig.xig_count = n; |
1873 | xig.xig_gen = gencnt; | |
1874 | xig.xig_sogen = so_gencnt; | |
39037602 | 1875 | error = SYSCTL_OUT(req, &xig, sizeof(xig)); |
91447636 | 1876 | if (error) { |
39236c6e | 1877 | lck_rw_done(tcbinfo.ipi_lock); |
0a7de745 | 1878 | return error; |
91447636 | 1879 | } |
2d21ac55 A |
1880 | /* |
1881 | * We are done if there is no pcb | |
1882 | */ | |
1883 | if (n == 0) { | |
39236c6e | 1884 | lck_rw_done(tcbinfo.ipi_lock); |
0a7de745 | 1885 | return 0; |
91447636 | 1886 | } |
1c79356b | 1887 | |
0a7de745 | 1888 | inp_list = _MALLOC(n * sizeof(*inp_list), M_TEMP, M_WAITOK); |
91447636 | 1889 | if (inp_list == 0) { |
39236c6e | 1890 | lck_rw_done(tcbinfo.ipi_lock); |
0a7de745 | 1891 | return ENOMEM; |
2d21ac55 A |
1892 | } |
1893 | ||
39037602 | 1894 | n = get_tcp_inp_list(inp_list, n, gencnt); |
1c79356b A |
1895 | |
1896 | error = 0; | |
1897 | for (i = 0; i < n; i++) { | |
39037602 A |
1898 | struct xtcpcb xt; |
1899 | caddr_t inp_ppcb; | |
1900 | struct inpcb *inp; | |
1901 | ||
1c79356b | 1902 | inp = inp_list[i]; |
39037602 | 1903 | |
0a7de745 | 1904 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) { |
39037602 | 1905 | continue; |
0a7de745 | 1906 | } |
5ba3f43e | 1907 | socket_lock(inp->inp_socket, 1); |
39037602 | 1908 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { |
5ba3f43e | 1909 | socket_unlock(inp->inp_socket, 1); |
39037602 A |
1910 | continue; |
1911 | } | |
1912 | if (inp->inp_gencnt > gencnt) { | |
5ba3f43e | 1913 | socket_unlock(inp->inp_socket, 1); |
39037602 A |
1914 | continue; |
1915 | } | |
1916 | ||
1917 | bzero(&xt, sizeof(xt)); | |
1918 | xt.xt_len = sizeof(xt); | |
1919 | /* XXX should avoid extra copy */ | |
1920 | inpcb_to_compat(inp, &xt.xt_inp); | |
1921 | inp_ppcb = inp->inp_ppcb; | |
1922 | if (inp_ppcb != NULL) { | |
1923 | tcpcb_to_otcpcb((struct tcpcb *)(void *)inp_ppcb, | |
1924 | &xt.xt_tp); | |
1925 | } else { | |
1926 | bzero((char *) &xt.xt_tp, sizeof(xt.xt_tp)); | |
1c79356b | 1927 | } |
0a7de745 | 1928 | if (inp->inp_socket) { |
39037602 | 1929 | sotoxsocket(inp->inp_socket, &xt.xt_socket); |
0a7de745 | 1930 | } |
39037602 | 1931 | |
5ba3f43e | 1932 | socket_unlock(inp->inp_socket, 1); |
39037602 A |
1933 | |
1934 | error = SYSCTL_OUT(req, &xt, sizeof(xt)); | |
1c79356b A |
1935 | } |
1936 | if (!error) { | |
1937 | /* | |
1938 | * Give the user an updated idea of our state. | |
1939 | * If the generation differs from what we told | |
1940 | * her before, she knows that something happened | |
1941 | * while we were processing this request, and it | |
1942 | * might be necessary to retry. | |
1943 | */ | |
3a60a9f5 | 1944 | bzero(&xig, sizeof(xig)); |
39037602 | 1945 | xig.xig_len = sizeof(xig); |
1c79356b A |
1946 | xig.xig_gen = tcbinfo.ipi_gencnt; |
1947 | xig.xig_sogen = so_gencnt; | |
1948 | xig.xig_count = tcbinfo.ipi_count; | |
39037602 | 1949 | error = SYSCTL_OUT(req, &xig, sizeof(xig)); |
1c79356b A |
1950 | } |
1951 | FREE(inp_list, M_TEMP); | |
39236c6e | 1952 | lck_rw_done(tcbinfo.ipi_lock); |
0a7de745 | 1953 | return error; |
1c79356b A |
1954 | } |
1955 | ||
fe8ab488 | 1956 | SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, |
0a7de745 A |
1957 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, |
1958 | tcp_pcblist, "S,xtcpcb", "List of active TCP connections"); | |
1c79356b | 1959 | |
f427ee49 | 1960 | #if XNU_TARGET_OS_OSX |
b0d623f7 A |
1961 | |
1962 | static void | |
1963 | tcpcb_to_xtcpcb64(struct tcpcb *tp, struct xtcpcb64 *otp) | |
9bccf70c | 1964 | { |
39037602 A |
1965 | otp->t_segq = (uint32_t)VM_KERNEL_ADDRPERM(tp->t_segq.lh_first); |
1966 | otp->t_dupacks = tp->t_dupacks; | |
fe8ab488 A |
1967 | otp->t_timer[TCPT_REXMT_EXT] = tp->t_timer[TCPT_REXMT]; |
1968 | otp->t_timer[TCPT_PERSIST_EXT] = tp->t_timer[TCPT_PERSIST]; | |
1969 | otp->t_timer[TCPT_KEEP_EXT] = tp->t_timer[TCPT_KEEP]; | |
1970 | otp->t_timer[TCPT_2MSL_EXT] = tp->t_timer[TCPT_2MSL]; | |
39037602 A |
1971 | otp->t_state = tp->t_state; |
1972 | otp->t_flags = tp->t_flags; | |
1973 | otp->t_force = (tp->t_flagsext & TF_FORCE) ? 1 : 0; | |
1974 | otp->snd_una = tp->snd_una; | |
1975 | otp->snd_max = tp->snd_max; | |
1976 | otp->snd_nxt = tp->snd_nxt; | |
1977 | otp->snd_up = tp->snd_up; | |
1978 | otp->snd_wl1 = tp->snd_wl1; | |
1979 | otp->snd_wl2 = tp->snd_wl2; | |
1980 | otp->iss = tp->iss; | |
1981 | otp->irs = tp->irs; | |
1982 | otp->rcv_nxt = tp->rcv_nxt; | |
1983 | otp->rcv_adv = tp->rcv_adv; | |
1984 | otp->rcv_wnd = tp->rcv_wnd; | |
1985 | otp->rcv_up = tp->rcv_up; | |
1986 | otp->snd_wnd = tp->snd_wnd; | |
1987 | otp->snd_cwnd = tp->snd_cwnd; | |
1988 | otp->snd_ssthresh = tp->snd_ssthresh; | |
1989 | otp->t_maxopd = tp->t_maxopd; | |
1990 | otp->t_rcvtime = tp->t_rcvtime; | |
1991 | otp->t_starttime = tp->t_starttime; | |
1992 | otp->t_rtttime = tp->t_rtttime; | |
1993 | otp->t_rtseq = tp->t_rtseq; | |
1994 | otp->t_rxtcur = tp->t_rxtcur; | |
1995 | otp->t_maxseg = tp->t_maxseg; | |
1996 | otp->t_srtt = tp->t_srtt; | |
1997 | otp->t_rttvar = tp->t_rttvar; | |
1998 | otp->t_rxtshift = tp->t_rxtshift; | |
1999 | otp->t_rttmin = tp->t_rttmin; | |
2000 | otp->t_rttupdated = tp->t_rttupdated; | |
2001 | otp->max_sndwnd = tp->max_sndwnd; | |
2002 | otp->t_softerror = tp->t_softerror; | |
2003 | otp->t_oobflags = tp->t_oobflags; | |
2004 | otp->t_iobc = tp->t_iobc; | |
2005 | otp->snd_scale = tp->snd_scale; | |
2006 | otp->rcv_scale = tp->rcv_scale; | |
2007 | otp->request_r_scale = tp->request_r_scale; | |
2008 | otp->requested_s_scale = tp->requested_s_scale; | |
2009 | otp->ts_recent = tp->ts_recent; | |
2010 | otp->ts_recent_age = tp->ts_recent_age; | |
2011 | otp->last_ack_sent = tp->last_ack_sent; | |
2012 | otp->cc_send = 0; | |
2013 | otp->cc_recv = 0; | |
2014 | otp->snd_recover = tp->snd_recover; | |
2015 | otp->snd_cwnd_prev = tp->snd_cwnd_prev; | |
2016 | otp->snd_ssthresh_prev = tp->snd_ssthresh_prev; | |
2017 | otp->t_badrxtwin = 0; | |
9bccf70c A |
2018 | } |
2019 | ||
9bccf70c | 2020 | |
9bccf70c | 2021 | static int |
b0d623f7 | 2022 | tcp_pcblist64 SYSCTL_HANDLER_ARGS |
9bccf70c | 2023 | { |
b0d623f7 | 2024 | #pragma unused(oidp, arg1, arg2) |
39037602 A |
2025 | int error, i = 0, n; |
2026 | struct inpcb **inp_list; | |
2027 | inp_gen_t gencnt; | |
2028 | struct xinpgen xig; | |
b0d623f7 | 2029 | |
39037602 A |
2030 | /* |
2031 | * The process of preparing the TCB list is too time-consuming and | |
2032 | * resource-intensive to repeat twice on every request. | |
2033 | */ | |
2034 | lck_rw_lock_shared(tcbinfo.ipi_lock); | |
2035 | if (req->oldptr == USER_ADDR_NULL) { | |
2036 | n = tcbinfo.ipi_count; | |
2037 | req->oldidx = 2 * (sizeof(xig)) | |
0a7de745 | 2038 | + (n + n / 8) * sizeof(struct xtcpcb64); |
39037602 | 2039 | lck_rw_done(tcbinfo.ipi_lock); |
0a7de745 | 2040 | return 0; |
39037602 | 2041 | } |
b0d623f7 | 2042 | |
39037602 A |
2043 | if (req->newptr != USER_ADDR_NULL) { |
2044 | lck_rw_done(tcbinfo.ipi_lock); | |
0a7de745 | 2045 | return EPERM; |
39037602 A |
2046 | } |
2047 | ||
2048 | /* | |
2049 | * OK, now we're committed to doing something. | |
2050 | */ | |
2051 | gencnt = tcbinfo.ipi_gencnt; | |
2052 | n = tcbinfo.ipi_count; | |
2053 | ||
2054 | bzero(&xig, sizeof(xig)); | |
2055 | xig.xig_len = sizeof(xig); | |
2056 | xig.xig_count = n; | |
2057 | xig.xig_gen = gencnt; | |
2058 | xig.xig_sogen = so_gencnt; | |
2059 | error = SYSCTL_OUT(req, &xig, sizeof(xig)); | |
2060 | if (error) { | |
2061 | lck_rw_done(tcbinfo.ipi_lock); | |
0a7de745 | 2062 | return error; |
39037602 A |
2063 | } |
2064 | /* | |
2065 | * We are done if there is no pcb | |
2066 | */ | |
2067 | if (n == 0) { | |
2068 | lck_rw_done(tcbinfo.ipi_lock); | |
0a7de745 | 2069 | return 0; |
39037602 A |
2070 | } |
2071 | ||
0a7de745 | 2072 | inp_list = _MALLOC(n * sizeof(*inp_list), M_TEMP, M_WAITOK); |
39037602 A |
2073 | if (inp_list == 0) { |
2074 | lck_rw_done(tcbinfo.ipi_lock); | |
0a7de745 | 2075 | return ENOMEM; |
39037602 A |
2076 | } |
2077 | ||
2078 | n = get_tcp_inp_list(inp_list, n, gencnt); | |
2079 | ||
2080 | error = 0; | |
2081 | for (i = 0; i < n; i++) { | |
2082 | struct xtcpcb64 xt; | |
2083 | struct inpcb *inp; | |
5ba3f43e | 2084 | |
39037602 A |
2085 | inp = inp_list[i]; |
2086 | ||
0a7de745 | 2087 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) { |
39037602 | 2088 | continue; |
0a7de745 | 2089 | } |
5ba3f43e | 2090 | socket_lock(inp->inp_socket, 1); |
39037602 | 2091 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { |
5ba3f43e | 2092 | socket_unlock(inp->inp_socket, 1); |
39037602 A |
2093 | continue; |
2094 | } | |
2095 | if (inp->inp_gencnt > gencnt) { | |
5ba3f43e | 2096 | socket_unlock(inp->inp_socket, 1); |
39037602 A |
2097 | continue; |
2098 | } | |
2099 | ||
2100 | bzero(&xt, sizeof(xt)); | |
2101 | xt.xt_len = sizeof(xt); | |
2102 | inpcb_to_xinpcb64(inp, &xt.xt_inpcb); | |
2103 | xt.xt_inpcb.inp_ppcb = | |
2104 | (uint64_t)VM_KERNEL_ADDRPERM(inp->inp_ppcb); | |
0a7de745 | 2105 | if (inp->inp_ppcb != NULL) { |
39037602 A |
2106 | tcpcb_to_xtcpcb64((struct tcpcb *)inp->inp_ppcb, |
2107 | &xt); | |
0a7de745 A |
2108 | } |
2109 | if (inp->inp_socket) { | |
39037602 A |
2110 | sotoxsocket64(inp->inp_socket, |
2111 | &xt.xt_inpcb.xi_socket); | |
0a7de745 | 2112 | } |
39037602 | 2113 | |
5ba3f43e | 2114 | socket_unlock(inp->inp_socket, 1); |
39037602 A |
2115 | |
2116 | error = SYSCTL_OUT(req, &xt, sizeof(xt)); | |
2117 | } | |
2118 | if (!error) { | |
0a7de745 A |
2119 | /* |
2120 | * Give the user an updated idea of our state. | |
2121 | * If the generation differs from what we told | |
2122 | * her before, she knows that something happened | |
2123 | * while we were processing this request, and it | |
2124 | * might be necessary to retry. | |
2125 | */ | |
2126 | bzero(&xig, sizeof(xig)); | |
2127 | xig.xig_len = sizeof(xig); | |
2128 | xig.xig_gen = tcbinfo.ipi_gencnt; | |
2129 | xig.xig_sogen = so_gencnt; | |
2130 | xig.xig_count = tcbinfo.ipi_count; | |
2131 | error = SYSCTL_OUT(req, &xig, sizeof(xig)); | |
39037602 A |
2132 | } |
2133 | FREE(inp_list, M_TEMP); | |
2134 | lck_rw_done(tcbinfo.ipi_lock); | |
0a7de745 | 2135 | return error; |
b0d623f7 A |
2136 | } |
2137 | ||
fe8ab488 | 2138 | SYSCTL_PROC(_net_inet_tcp, OID_AUTO, pcblist64, |
0a7de745 A |
2139 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, |
2140 | tcp_pcblist64, "S,xtcpcb64", "List of active TCP connections"); | |
b0d623f7 | 2141 | |
f427ee49 | 2142 | #endif /* XNU_TARGET_OS_OSX */ |
9bccf70c | 2143 | |
6d2010ae A |
2144 | static int |
2145 | tcp_pcblist_n SYSCTL_HANDLER_ARGS | |
2146 | { | |
2147 | #pragma unused(oidp, arg1, arg2) | |
2148 | int error = 0; | |
39037602 | 2149 | |
6d2010ae | 2150 | error = get_pcblist_n(IPPROTO_TCP, req, &tcbinfo); |
39037602 | 2151 | |
0a7de745 | 2152 | return error; |
6d2010ae A |
2153 | } |
2154 | ||
2155 | ||
fe8ab488 | 2156 | SYSCTL_PROC(_net_inet_tcp, OID_AUTO, pcblist_n, |
0a7de745 A |
2157 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, |
2158 | tcp_pcblist_n, "S,xtcpcb_n", "List of active TCP connections"); | |
d9a64523 A |
2159 | |
2160 | static int | |
2161 | tcp_progress_indicators SYSCTL_HANDLER_ARGS | |
2162 | { | |
2163 | #pragma unused(oidp, arg1, arg2) | |
2164 | ||
0a7de745 | 2165 | return ntstat_tcp_progress_indicators(req); |
d9a64523 A |
2166 | } |
2167 | ||
2168 | SYSCTL_PROC(_net_inet_tcp, OID_AUTO, progress, | |
0a7de745 A |
2169 | CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY, 0, 0, |
2170 | tcp_progress_indicators, "S", "Various items that indicate the current state of progress on the link"); | |
6d2010ae A |
2171 | |
2172 | ||
316670eb | 2173 | __private_extern__ void |
fe8ab488 | 2174 | tcp_get_ports_used(uint32_t ifindex, int protocol, uint32_t flags, |
39236c6e | 2175 | bitstr_t *bitfield) |
316670eb | 2176 | { |
d9a64523 A |
2177 | inpcb_get_ports_used(ifindex, protocol, flags, bitfield, |
2178 | &tcbinfo); | |
2179 | } | |
316670eb A |
2180 | |
2181 | __private_extern__ uint32_t | |
2182 | tcp_count_opportunistic(unsigned int ifindex, u_int32_t flags) | |
2183 | { | |
0a7de745 | 2184 | return inpcb_count_opportunistic(ifindex, &tcbinfo, flags); |
316670eb A |
2185 | } |
2186 | ||
39236c6e A |
2187 | __private_extern__ uint32_t |
2188 | tcp_find_anypcb_byaddr(struct ifaddr *ifa) | |
2189 | { | |
0a7de745 | 2190 | return inpcb_find_anypcb_byaddr(ifa, &tcbinfo); |
39037602 A |
2191 | } |
2192 | ||
2193 | static void | |
2194 | tcp_handle_msgsize(struct ip *ip, struct inpcb *inp) | |
2195 | { | |
2196 | struct rtentry *rt = NULL; | |
2197 | u_short ifscope = IFSCOPE_NONE; | |
2198 | int mtu; | |
2199 | struct sockaddr_in icmpsrc = { | |
cb323159 A |
2200 | .sin_len = sizeof(struct sockaddr_in), |
2201 | .sin_family = AF_INET, .sin_port = 0, .sin_addr = { .s_addr = 0 }, | |
2202 | .sin_zero = { 0, 0, 0, 0, 0, 0, 0, 0 } | |
0a7de745 | 2203 | }; |
39037602 A |
2204 | struct icmp *icp = NULL; |
2205 | ||
2206 | icp = (struct icmp *)(void *) | |
2207 | ((caddr_t)ip - offsetof(struct icmp, icmp_ip)); | |
2208 | ||
2209 | icmpsrc.sin_addr = icp->icmp_ip.ip_dst; | |
2210 | ||
2211 | /* | |
2212 | * MTU discovery: | |
2213 | * If we got a needfrag and there is a host route to the | |
2214 | * original destination, and the MTU is not locked, then | |
2215 | * set the MTU in the route to the suggested new value | |
2216 | * (if given) and then notify as usual. The ULPs will | |
2217 | * notice that the MTU has changed and adapt accordingly. | |
2218 | * If no new MTU was suggested, then we guess a new one | |
2219 | * less than the current value. If the new MTU is | |
2220 | * unreasonably small (defined by sysctl tcp_minmss), then | |
2221 | * we reset the MTU to the interface value and enable the | |
2222 | * lock bit, indicating that we are no longer doing MTU | |
2223 | * discovery. | |
2224 | */ | |
0a7de745 | 2225 | if (ROUTE_UNUSABLE(&(inp->inp_route)) == false) { |
39037602 | 2226 | rt = inp->inp_route.ro_rt; |
0a7de745 | 2227 | } |
39037602 A |
2228 | |
2229 | /* | |
2230 | * icmp6_mtudisc_update scopes the routing lookup | |
2231 | * to the incoming interface (delivered from mbuf | |
2232 | * packet header. | |
2233 | * That is mostly ok but for asymmetric networks | |
2234 | * that may be an issue. | |
2235 | * Frag needed OR Packet too big really communicates | |
2236 | * MTU for the out data path. | |
2237 | * Take the interface scope from cached route or | |
2238 | * the last outgoing interface from inp | |
2239 | */ | |
0a7de745 | 2240 | if (rt != NULL) { |
39037602 A |
2241 | ifscope = (rt->rt_ifp != NULL) ? |
2242 | rt->rt_ifp->if_index : IFSCOPE_NONE; | |
0a7de745 | 2243 | } else { |
39037602 A |
2244 | ifscope = (inp->inp_last_outifp != NULL) ? |
2245 | inp->inp_last_outifp->if_index : IFSCOPE_NONE; | |
0a7de745 | 2246 | } |
39037602 A |
2247 | |
2248 | if ((rt == NULL) || | |
2249 | !(rt->rt_flags & RTF_HOST) || | |
2250 | (rt->rt_flags & (RTF_CLONING | RTF_PRCLONING))) { | |
2251 | rt = rtalloc1_scoped((struct sockaddr *)&icmpsrc, 0, | |
2252 | RTF_CLONING | RTF_PRCLONING, ifscope); | |
2253 | } else if (rt) { | |
2254 | RT_LOCK(rt); | |
2255 | rtref(rt); | |
2256 | RT_UNLOCK(rt); | |
2257 | } | |
2258 | ||
2259 | if (rt != NULL) { | |
2260 | RT_LOCK(rt); | |
2261 | if ((rt->rt_flags & RTF_HOST) && | |
2262 | !(rt->rt_rmx.rmx_locks & RTV_MTU)) { | |
2263 | mtu = ntohs(icp->icmp_nextmtu); | |
2264 | /* | |
2265 | * XXX Stock BSD has changed the following | |
2266 | * to compare with icp->icmp_ip.ip_len | |
2267 | * to converge faster when sent packet | |
2268 | * < route's MTU. We may want to adopt | |
2269 | * that change. | |
2270 | */ | |
0a7de745 | 2271 | if (mtu == 0) { |
39037602 A |
2272 | mtu = ip_next_mtu(rt->rt_rmx. |
2273 | rmx_mtu, 1); | |
0a7de745 | 2274 | } |
39037602 A |
2275 | #if DEBUG_MTUDISC |
2276 | printf("MTU for %s reduced to %d\n", | |
2277 | inet_ntop(AF_INET, | |
2278 | &icmpsrc.sin_addr, ipv4str, | |
0a7de745 | 2279 | sizeof(ipv4str)), mtu); |
39037602 A |
2280 | #endif |
2281 | if (mtu < max(296, (tcp_minmss + | |
0a7de745 | 2282 | sizeof(struct tcpiphdr)))) { |
39037602 A |
2283 | rt->rt_rmx.rmx_locks |= RTV_MTU; |
2284 | } else if (rt->rt_rmx.rmx_mtu > mtu) { | |
2285 | rt->rt_rmx.rmx_mtu = mtu; | |
2286 | } | |
2287 | } | |
2288 | RT_UNLOCK(rt); | |
2289 | rtfree(rt); | |
2290 | } | |
39236c6e A |
2291 | } |
2292 | ||
1c79356b | 2293 | void |
5ba3f43e | 2294 | tcp_ctlinput(int cmd, struct sockaddr *sa, void *vip, __unused struct ifnet *ifp) |
1c79356b | 2295 | { |
b0d623f7 | 2296 | tcp_seq icmp_tcp_seq; |
9bccf70c | 2297 | struct ip *ip = vip; |
9bccf70c A |
2298 | struct in_addr faddr; |
2299 | struct inpcb *inp; | |
2300 | struct tcpcb *tp; | |
5ba3f43e A |
2301 | struct tcphdr *th; |
2302 | struct icmp *icp; | |
316670eb | 2303 | void (*notify)(struct inpcb *, int) = tcp_notify; |
9bccf70c | 2304 | |
316670eb | 2305 | faddr = ((struct sockaddr_in *)(void *)sa)->sin_addr; |
0a7de745 | 2306 | if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) { |
9bccf70c | 2307 | return; |
0a7de745 | 2308 | } |
1c79356b | 2309 | |
0a7de745 | 2310 | if ((unsigned)cmd >= PRC_NCMDS) { |
3e170ce0 | 2311 | return; |
0a7de745 | 2312 | } |
3e170ce0 | 2313 | |
5ba3f43e | 2314 | /* Source quench is deprecated */ |
0a7de745 A |
2315 | if (cmd == PRC_QUENCH) { |
2316 | return; | |
2317 | } | |
5ba3f43e | 2318 | |
0a7de745 | 2319 | if (cmd == PRC_MSGSIZE) { |
b0d623f7 | 2320 | notify = tcp_mtudisc; |
0a7de745 A |
2321 | } else if (icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB || |
2322 | cmd == PRC_UNREACH_PORT || cmd == PRC_UNREACH_PROTOCOL || | |
2323 | cmd == PRC_TIMXCEED_INTRANS) && ip) { | |
9bccf70c | 2324 | notify = tcp_drop_syn_sent; |
0a7de745 | 2325 | } |
5ba3f43e A |
2326 | /* |
2327 | * Hostdead is ugly because it goes linearly through all PCBs. | |
2328 | * XXX: We never get this from ICMP, otherwise it makes an | |
2329 | * excellent DoS attack on machines with many connections. | |
2330 | */ | |
0a7de745 | 2331 | else if (cmd == PRC_HOSTDEAD) { |
5ba3f43e | 2332 | ip = NULL; |
0a7de745 | 2333 | } else if (inetctlerrmap[cmd] == 0 && !PRC_IS_REDIRECT(cmd)) { |
b0d623f7 | 2334 | return; |
0a7de745 | 2335 | } |
5ba3f43e A |
2336 | |
2337 | ||
2338 | if (ip == NULL) { | |
2339 | in_pcbnotifyall(&tcbinfo, faddr, inetctlerrmap[cmd], notify); | |
1c79356b | 2340 | return; |
5ba3f43e | 2341 | } |
316670eb | 2342 | |
5ba3f43e A |
2343 | icp = (struct icmp *)(void *) |
2344 | ((caddr_t)ip - offsetof(struct icmp, icmp_ip)); | |
2345 | th = (struct tcphdr *)(void *)((caddr_t)ip + (IP_VHL_HL(ip->ip_vhl) << 2)); | |
2346 | icmp_tcp_seq = ntohl(th->th_seq); | |
b0d623f7 | 2347 | |
5ba3f43e A |
2348 | inp = in_pcblookup_hash(&tcbinfo, faddr, th->th_dport, |
2349 | ip->ip_src, th->th_sport, 0, NULL); | |
2350 | ||
2351 | if (inp == NULL || | |
2352 | inp->inp_socket == NULL) { | |
2353 | return; | |
2354 | } | |
2355 | ||
2356 | socket_lock(inp->inp_socket, 1); | |
2357 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == | |
2358 | WNT_STOPUSING) { | |
2359 | socket_unlock(inp->inp_socket, 1); | |
2360 | return; | |
2361 | } | |
2362 | ||
2363 | if (PRC_IS_REDIRECT(cmd)) { | |
2364 | /* signal EHOSTDOWN, as it flushes the cached route */ | |
2365 | (*notify)(inp, EHOSTDOWN); | |
2366 | } else { | |
2367 | tp = intotcpcb(inp); | |
2368 | if (SEQ_GEQ(icmp_tcp_seq, tp->snd_una) && | |
2369 | SEQ_LT(icmp_tcp_seq, tp->snd_max)) { | |
0a7de745 | 2370 | if (cmd == PRC_MSGSIZE) { |
5ba3f43e | 2371 | tcp_handle_msgsize(ip, inp); |
0a7de745 | 2372 | } |
5ba3f43e A |
2373 | |
2374 | (*notify)(inp, inetctlerrmap[cmd]); | |
9bccf70c | 2375 | } |
5ba3f43e A |
2376 | } |
2377 | socket_unlock(inp->inp_socket, 1); | |
1c79356b A |
2378 | } |
2379 | ||
1c79356b | 2380 | void |
5ba3f43e | 2381 | tcp6_ctlinput(int cmd, struct sockaddr *sa, void *d, __unused struct ifnet *ifp) |
1c79356b | 2382 | { |
5ba3f43e A |
2383 | tcp_seq icmp_tcp_seq; |
2384 | struct in6_addr *dst; | |
91447636 | 2385 | void (*notify)(struct inpcb *, int) = tcp_notify; |
1c79356b A |
2386 | struct ip6_hdr *ip6; |
2387 | struct mbuf *m; | |
5ba3f43e A |
2388 | struct inpcb *inp; |
2389 | struct tcpcb *tp; | |
2390 | struct icmp6_hdr *icmp6; | |
9bccf70c A |
2391 | struct ip6ctlparam *ip6cp = NULL; |
2392 | const struct sockaddr_in6 *sa6_src = NULL; | |
5ba3f43e A |
2393 | unsigned int mtu; |
2394 | unsigned int off; | |
1c79356b | 2395 | |
0a7de745 A |
2396 | struct tcp_ports { |
2397 | uint16_t th_sport; | |
2398 | uint16_t th_dport; | |
2399 | } t_ports; | |
2400 | ||
1c79356b | 2401 | if (sa->sa_family != AF_INET6 || |
0a7de745 | 2402 | sa->sa_len != sizeof(struct sockaddr_in6)) { |
1c79356b | 2403 | return; |
0a7de745 | 2404 | } |
1c79356b | 2405 | |
5ba3f43e | 2406 | /* Source quench is deprecated */ |
0a7de745 | 2407 | if (cmd == PRC_QUENCH) { |
3e170ce0 | 2408 | return; |
0a7de745 | 2409 | } |
3e170ce0 | 2410 | |
0a7de745 | 2411 | if ((unsigned)cmd >= PRC_NCMDS) { |
b0d623f7 | 2412 | return; |
0a7de745 | 2413 | } |
1c79356b A |
2414 | |
2415 | /* if the parameter is from icmp6, decode it. */ | |
2416 | if (d != NULL) { | |
9bccf70c | 2417 | ip6cp = (struct ip6ctlparam *)d; |
5ba3f43e | 2418 | icmp6 = ip6cp->ip6c_icmp6; |
1c79356b A |
2419 | m = ip6cp->ip6c_m; |
2420 | ip6 = ip6cp->ip6c_ip6; | |
2421 | off = ip6cp->ip6c_off; | |
9bccf70c | 2422 | sa6_src = ip6cp->ip6c_src; |
5ba3f43e | 2423 | dst = ip6cp->ip6c_finaldst; |
1c79356b A |
2424 | } else { |
2425 | m = NULL; | |
2426 | ip6 = NULL; | |
0a7de745 | 2427 | off = 0; /* fool gcc */ |
9bccf70c | 2428 | sa6_src = &sa6_any; |
5ba3f43e | 2429 | dst = NULL; |
1c79356b A |
2430 | } |
2431 | ||
0a7de745 | 2432 | if (cmd == PRC_MSGSIZE) { |
5ba3f43e | 2433 | notify = tcp_mtudisc; |
0a7de745 | 2434 | } else if (icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB || |
5ba3f43e | 2435 | cmd == PRC_UNREACH_PORT || cmd == PRC_TIMXCEED_INTRANS) && |
0a7de745 | 2436 | ip6 != NULL) { |
5ba3f43e | 2437 | notify = tcp_drop_syn_sent; |
0a7de745 | 2438 | } |
5ba3f43e A |
2439 | /* |
2440 | * Hostdead is ugly because it goes linearly through all PCBs. | |
2441 | * XXX: We never get this from ICMP, otherwise it makes an | |
2442 | * excellent DoS attack on machines with many connections. | |
2443 | */ | |
0a7de745 | 2444 | else if (cmd == PRC_HOSTDEAD) { |
5ba3f43e | 2445 | ip6 = NULL; |
0a7de745 | 2446 | } else if (inet6ctlerrmap[cmd] == 0 && !PRC_IS_REDIRECT(cmd)) { |
5ba3f43e | 2447 | return; |
0a7de745 | 2448 | } |
5ba3f43e A |
2449 | |
2450 | ||
2451 | if (ip6 == NULL) { | |
2452 | in6_pcbnotify(&tcbinfo, sa, 0, (struct sockaddr *)(size_t)sa6_src, | |
2453 | 0, cmd, NULL, notify); | |
2454 | return; | |
2455 | } | |
2456 | ||
0a7de745 | 2457 | /* Check if we can safely get the ports from the tcp hdr */ |
5ba3f43e | 2458 | if (m == NULL || |
0a7de745 A |
2459 | (m->m_pkthdr.len < |
2460 | (int32_t) (off + sizeof(struct tcp_ports)))) { | |
5ba3f43e | 2461 | return; |
0a7de745 A |
2462 | } |
2463 | bzero(&t_ports, sizeof(struct tcp_ports)); | |
2464 | m_copydata(m, off, sizeof(struct tcp_ports), (caddr_t)&t_ports); | |
5ba3f43e | 2465 | |
0a7de745 A |
2466 | off += sizeof(struct tcp_ports); |
2467 | if (m->m_pkthdr.len < (int32_t) (off + sizeof(tcp_seq))) { | |
2468 | return; | |
2469 | } | |
2470 | m_copydata(m, off, sizeof(tcp_seq), (caddr_t)&icmp_tcp_seq); | |
2471 | icmp_tcp_seq = ntohl(icmp_tcp_seq); | |
5ba3f43e A |
2472 | |
2473 | if (cmd == PRC_MSGSIZE) { | |
2474 | mtu = ntohl(icmp6->icmp6_mtu); | |
1c79356b | 2475 | /* |
5ba3f43e A |
2476 | * If no alternative MTU was proposed, or the proposed |
2477 | * MTU was too small, set to the min. | |
1c79356b | 2478 | */ |
0a7de745 | 2479 | if (mtu < IPV6_MMTU) { |
5ba3f43e | 2480 | mtu = IPV6_MMTU - 8; |
0a7de745 | 2481 | } |
5ba3f43e | 2482 | } |
1c79356b | 2483 | |
0a7de745 A |
2484 | inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_dst, t_ports.th_dport, |
2485 | &ip6->ip6_src, t_ports.th_sport, 0, NULL); | |
1c79356b | 2486 | |
5ba3f43e A |
2487 | if (inp == NULL || |
2488 | inp->inp_socket == NULL) { | |
2489 | return; | |
2490 | } | |
1c79356b | 2491 | |
5ba3f43e A |
2492 | socket_lock(inp->inp_socket, 1); |
2493 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == | |
2494 | WNT_STOPUSING) { | |
2495 | socket_unlock(inp->inp_socket, 1); | |
2496 | return; | |
2497 | } | |
2498 | ||
2499 | if (PRC_IS_REDIRECT(cmd)) { | |
2500 | /* signal EHOSTDOWN, as it flushes the cached route */ | |
2501 | (*notify)(inp, EHOSTDOWN); | |
b0d623f7 | 2502 | } else { |
5ba3f43e A |
2503 | tp = intotcpcb(inp); |
2504 | if (SEQ_GEQ(icmp_tcp_seq, tp->snd_una) && | |
2505 | SEQ_LT(icmp_tcp_seq, tp->snd_max)) { | |
2506 | if (cmd == PRC_MSGSIZE) { | |
2507 | /* | |
2508 | * Only process the offered MTU if it | |
2509 | * is smaller than the current one. | |
2510 | */ | |
2511 | if (mtu < tp->t_maxseg + | |
0a7de745 | 2512 | (sizeof(struct tcphdr) + sizeof(struct ip6_hdr))) { |
5ba3f43e | 2513 | (*notify)(inp, inetctlerrmap[cmd]); |
0a7de745 A |
2514 | } |
2515 | } else { | |
5ba3f43e | 2516 | (*notify)(inp, inetctlerrmap[cmd]); |
0a7de745 | 2517 | } |
5ba3f43e | 2518 | } |
b0d623f7 | 2519 | } |
5ba3f43e | 2520 | socket_unlock(inp->inp_socket, 1); |
1c79356b | 2521 | } |
1c79356b | 2522 | |
0b4e3aa0 | 2523 | |
9bccf70c A |
2524 | /* |
2525 | * Following is where TCP initial sequence number generation occurs. | |
2526 | * | |
2527 | * There are two places where we must use initial sequence numbers: | |
2528 | * 1. In SYN-ACK packets. | |
2529 | * 2. In SYN packets. | |
2530 | * | |
39037602 | 2531 | * The ISNs in SYN-ACK packets have no monotonicity requirement, |
9bccf70c A |
2532 | * and should be as unpredictable as possible to avoid the possibility |
2533 | * of spoofing and/or connection hijacking. To satisfy this | |
2534 | * requirement, SYN-ACK ISNs are generated via the arc4random() | |
2535 | * function. If exact RFC 1948 compliance is requested via sysctl, | |
2536 | * these ISNs will be generated just like those in SYN packets. | |
2537 | * | |
2538 | * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling | |
2539 | * depends on this property. In addition, these ISNs should be | |
2540 | * unguessable so as to prevent connection hijacking. To satisfy | |
2541 | * the requirements of this situation, the algorithm outlined in | |
2542 | * RFC 1948 is used to generate sequence numbers. | |
2543 | * | |
2544 | * For more information on the theory of operation, please see | |
2545 | * RFC 1948. | |
2546 | * | |
2547 | * Implementation details: | |
2548 | * | |
2549 | * Time is based off the system timer, and is corrected so that it | |
2550 | * increases by one megabyte per second. This allows for proper | |
2551 | * recycling on high speed LANs while still leaving over an hour | |
2552 | * before rollover. | |
2553 | * | |
2554 | * Two sysctls control the generation of ISNs: | |
2555 | * | |
2556 | * net.inet.tcp.isn_reseed_interval controls the number of seconds | |
2557 | * between seeding of isn_secret. This is normally set to zero, | |
2558 | * as reseeding should not be necessary. | |
2559 | * | |
2560 | * net.inet.tcp.strict_rfc1948 controls whether RFC 1948 is followed | |
2561 | * strictly. When strict compliance is requested, reseeding is | |
2562 | * disabled and SYN-ACKs will be generated in the same manner as | |
2563 | * SYNs. Strict mode is disabled by default. | |
2564 | * | |
2565 | */ | |
0b4e3aa0 | 2566 | |
0a7de745 | 2567 | #define ISN_BYTES_PER_SECOND 1048576 |
0b4e3aa0 | 2568 | |
0b4e3aa0 | 2569 | tcp_seq |
39037602 | 2570 | tcp_new_isn(struct tcpcb *tp) |
0b4e3aa0 | 2571 | { |
9bccf70c A |
2572 | u_int32_t md5_buffer[4]; |
2573 | tcp_seq new_isn; | |
8ad349bb | 2574 | struct timeval timenow; |
2d21ac55 | 2575 | u_char isn_secret[32]; |
f427ee49 | 2576 | long isn_last_reseed = 0; |
2d21ac55 | 2577 | MD5_CTX isn_ctx; |
9bccf70c A |
2578 | |
2579 | /* Use arc4random for SYN-ACKs when not in exact RFC1948 mode. */ | |
39037602 A |
2580 | if (((tp->t_state == TCPS_LISTEN) || (tp->t_state == TCPS_TIME_WAIT)) && |
2581 | tcp_strict_rfc1948 == 0) | |
9bccf70c | 2582 | #ifdef __APPLE__ |
0a7de745 | 2583 | { return RandomULong(); } |
9bccf70c | 2584 | #else |
0a7de745 | 2585 | { return arc4random(); } |
9bccf70c | 2586 | #endif |
8ad349bb | 2587 | getmicrotime(&timenow); |
0b4e3aa0 | 2588 | |
9bccf70c A |
2589 | /* Seed if this is the first use, reseed if requested. */ |
2590 | if ((isn_last_reseed == 0) || | |
2591 | ((tcp_strict_rfc1948 == 0) && (tcp_isn_reseed_interval > 0) && | |
0a7de745 A |
2592 | (((u_int)isn_last_reseed + (u_int)tcp_isn_reseed_interval * hz) |
2593 | < (u_int)timenow.tv_sec))) { | |
9bccf70c | 2594 | #ifdef __APPLE__ |
5ba3f43e | 2595 | read_frandom(&isn_secret, sizeof(isn_secret)); |
9bccf70c A |
2596 | #else |
2597 | read_random_unlimited(&isn_secret, sizeof(isn_secret)); | |
2598 | #endif | |
8ad349bb | 2599 | isn_last_reseed = timenow.tv_sec; |
9bccf70c | 2600 | } |
39037602 | 2601 | |
9bccf70c A |
2602 | /* Compute the md5 hash and return the ISN. */ |
2603 | MD5Init(&isn_ctx); | |
39037602 A |
2604 | MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_fport, |
2605 | sizeof(u_short)); | |
2606 | MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_lport, | |
2607 | sizeof(u_short)); | |
9bccf70c A |
2608 | if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) { |
2609 | MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr, | |
39037602 | 2610 | sizeof(struct in6_addr)); |
9bccf70c | 2611 | MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr, |
39037602 | 2612 | sizeof(struct in6_addr)); |
f427ee49 | 2613 | } else { |
9bccf70c | 2614 | MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr, |
39037602 | 2615 | sizeof(struct in_addr)); |
9bccf70c | 2616 | MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr, |
39037602 | 2617 | sizeof(struct in_addr)); |
9bccf70c A |
2618 | } |
2619 | MD5Update(&isn_ctx, (u_char *) &isn_secret, sizeof(isn_secret)); | |
2620 | MD5Final((u_char *) &md5_buffer, &isn_ctx); | |
2621 | new_isn = (tcp_seq) md5_buffer[0]; | |
8ad349bb | 2622 | new_isn += timenow.tv_sec * (ISN_BYTES_PER_SECOND / hz); |
0a7de745 | 2623 | return new_isn; |
0b4e3aa0 A |
2624 | } |
2625 | ||
1c79356b | 2626 | |
9bccf70c A |
2627 | /* |
2628 | * When a specific ICMP unreachable message is received and the | |
2629 | * connection state is SYN-SENT, drop the connection. This behavior | |
2630 | * is controlled by the icmp_may_rst sysctl. | |
2631 | */ | |
2632 | void | |
39037602 | 2633 | tcp_drop_syn_sent(struct inpcb *inp, int errno) |
9bccf70c A |
2634 | { |
2635 | struct tcpcb *tp = intotcpcb(inp); | |
2636 | ||
0a7de745 | 2637 | if (tp && tp->t_state == TCPS_SYN_SENT) { |
9bccf70c | 2638 | tcp_drop(tp, errno); |
0a7de745 | 2639 | } |
9bccf70c A |
2640 | } |
2641 | ||
1c79356b A |
2642 | /* |
2643 | * When `need fragmentation' ICMP is received, update our idea of the MSS | |
2644 | * based on the new value in the route. Also nudge TCP to send something, | |
2645 | * since we know the packet we just sent was dropped. | |
2646 | * This duplicates some code in the tcp_mss() function in tcp_input.c. | |
2647 | */ | |
2648 | void | |
f427ee49 | 2649 | tcp_mtudisc(struct inpcb *inp, __unused int errno) |
1c79356b A |
2650 | { |
2651 | struct tcpcb *tp = intotcpcb(inp); | |
2652 | struct rtentry *rt; | |
1c79356b | 2653 | struct socket *so = inp->inp_socket; |
1c79356b | 2654 | int mss; |
3e170ce0 | 2655 | u_int32_t mtu; |
0a7de745 | 2656 | u_int32_t protoHdrOverhead = sizeof(struct tcpiphdr); |
9bccf70c | 2657 | int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; |
39037602 | 2658 | |
cb323159 A |
2659 | /* |
2660 | * Nothing left to send after the socket is defunct or TCP is in the closed state | |
2661 | */ | |
2662 | if ((so->so_state & SS_DEFUNCT) || (tp != NULL && tp->t_state == TCPS_CLOSED)) { | |
2663 | return; | |
2664 | } | |
2665 | ||
0a7de745 | 2666 | if (isipv6) { |
39037602 A |
2667 | protoHdrOverhead = sizeof(struct ip6_hdr) + |
2668 | sizeof(struct tcphdr); | |
0a7de745 | 2669 | } |
1c79356b | 2670 | |
cb323159 | 2671 | if (tp != NULL) { |
0a7de745 | 2672 | if (isipv6) { |
6d2010ae | 2673 | rt = tcp_rtlookup6(inp, IFSCOPE_NONE); |
f427ee49 A |
2674 | } else { |
2675 | rt = tcp_rtlookup(inp, IFSCOPE_NONE); | |
2676 | } | |
1c79356b A |
2677 | if (!rt || !rt->rt_rmx.rmx_mtu) { |
2678 | tp->t_maxopd = tp->t_maxseg = | |
0a7de745 | 2679 | isipv6 ? tcp_v6mssdflt : |
0a7de745 | 2680 | tcp_mssdflt; |
b0d623f7 A |
2681 | |
2682 | /* Route locked during lookup above */ | |
0a7de745 | 2683 | if (rt != NULL) { |
b0d623f7 | 2684 | RT_UNLOCK(rt); |
0a7de745 | 2685 | } |
1c79356b A |
2686 | return; |
2687 | } | |
3e170ce0 A |
2688 | mtu = rt->rt_rmx.rmx_mtu; |
2689 | ||
2690 | /* Route locked during lookup above */ | |
2691 | RT_UNLOCK(rt); | |
2692 | ||
2693 | #if NECP | |
2694 | // Adjust MTU if necessary. | |
2695 | mtu = necp_socket_get_effective_mtu(inp, mtu); | |
2696 | #endif /* NECP */ | |
39037602 | 2697 | mss = mtu - protoHdrOverhead; |
1c79356b | 2698 | |
f427ee49 A |
2699 | if (tp->t_maxopd) { |
2700 | mss = min(mss, tp->t_maxopd); | |
0a7de745 | 2701 | } |
1c79356b A |
2702 | /* |
2703 | * XXX - The above conditional probably violates the TCP | |
2704 | * spec. The problem is that, since we don't know the | |
2705 | * other end's MSS, we are supposed to use a conservative | |
2706 | * default. But, if we do that, then MTU discovery will | |
2707 | * never actually take place, because the conservative | |
2708 | * default is much less than the MTUs typically seen | |
2709 | * on the Internet today. For the moment, we'll sweep | |
2710 | * this under the carpet. | |
2711 | * | |
2712 | * The conservative default might not actually be a problem | |
2713 | * if the only case this occurs is when sending an initial | |
2714 | * SYN with options and data to a host we've never talked | |
2715 | * to before. Then, they will reply with an MSS value which | |
2716 | * will get recorded and the new parameters should get | |
2717 | * recomputed. For Further Study. | |
2718 | */ | |
0a7de745 | 2719 | if (tp->t_maxopd <= mss) { |
1c79356b | 2720 | return; |
0a7de745 | 2721 | } |
1c79356b A |
2722 | tp->t_maxopd = mss; |
2723 | ||
0a7de745 A |
2724 | if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP && |
2725 | (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) { | |
1c79356b | 2726 | mss -= TCPOLEN_TSTAMP_APPA; |
0a7de745 | 2727 | } |
55e303ae | 2728 | |
39236c6e A |
2729 | #if MPTCP |
2730 | mss -= mptcp_adj_mss(tp, TRUE); | |
2731 | #endif | |
0a7de745 | 2732 | if (so->so_snd.sb_hiwat < mss) { |
1c79356b | 2733 | mss = so->so_snd.sb_hiwat; |
0a7de745 | 2734 | } |
1c79356b A |
2735 | |
2736 | tp->t_maxseg = mss; | |
2737 | ||
a39ff7e2 A |
2738 | ASSERT(tp->t_maxseg); |
2739 | ||
6d2010ae | 2740 | /* |
39037602 A |
2741 | * Reset the slow-start flight size as it may depends on the |
2742 | * new MSS | |
6d2010ae | 2743 | */ |
0a7de745 | 2744 | if (CC_ALGO(tp)->cwnd_init != NULL) { |
6d2010ae | 2745 | CC_ALGO(tp)->cwnd_init(tp); |
0a7de745 | 2746 | } |
1c79356b | 2747 | tcpstat.tcps_mturesent++; |
9bccf70c | 2748 | tp->t_rtttime = 0; |
1c79356b A |
2749 | tp->snd_nxt = tp->snd_una; |
2750 | tcp_output(tp); | |
2751 | } | |
2752 | } | |
2753 | ||
2754 | /* | |
2755 | * Look-up the routing entry to the peer of this inpcb. If no route | |
b0d623f7 | 2756 | * is found and it cannot be allocated the return NULL. This routine |
1c79356b | 2757 | * is called by TCP routines that access the rmx structure and by tcp_mss |
b0d623f7 A |
2758 | * to get the interface MTU. If a route is found, this routine will |
2759 | * hold the rtentry lock; the caller is responsible for unlocking. | |
1c79356b A |
2760 | */ |
2761 | struct rtentry * | |
39037602 | 2762 | tcp_rtlookup(struct inpcb *inp, unsigned int input_ifscope) |
1c79356b A |
2763 | { |
2764 | struct route *ro; | |
2765 | struct rtentry *rt; | |
2d21ac55 | 2766 | struct tcpcb *tp; |
1c79356b | 2767 | |
5ba3f43e | 2768 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); |
2d21ac55 | 2769 | |
b0d623f7 | 2770 | ro = &inp->inp_route; |
0a7de745 | 2771 | if ((rt = ro->ro_rt) != NULL) { |
b0d623f7 | 2772 | RT_LOCK(rt); |
0a7de745 | 2773 | } |
2d21ac55 | 2774 | |
39236c6e A |
2775 | if (ROUTE_UNUSABLE(ro)) { |
2776 | if (rt != NULL) { | |
2777 | RT_UNLOCK(rt); | |
2778 | rt = NULL; | |
2779 | } | |
2780 | ROUTE_RELEASE(ro); | |
1c79356b A |
2781 | /* No route yet, so try to acquire one */ |
2782 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
c910b4d9 A |
2783 | unsigned int ifscope; |
2784 | ||
1c79356b | 2785 | ro->ro_dst.sa_family = AF_INET; |
9bccf70c | 2786 | ro->ro_dst.sa_len = sizeof(struct sockaddr_in); |
316670eb | 2787 | ((struct sockaddr_in *)(void *)&ro->ro_dst)->sin_addr = |
0a7de745 | 2788 | inp->inp_faddr; |
c910b4d9 A |
2789 | |
2790 | /* | |
2791 | * If the socket was bound to an interface, then | |
2792 | * the bound-to-interface takes precedence over | |
2793 | * the inbound interface passed in by the caller | |
2794 | * (if we get here as part of the output path then | |
2795 | * input_ifscope is IFSCOPE_NONE). | |
2796 | */ | |
2797 | ifscope = (inp->inp_flags & INP_BOUND_IF) ? | |
316670eb | 2798 | inp->inp_boundifp->if_index : input_ifscope; |
c910b4d9 | 2799 | |
6d2010ae | 2800 | rtalloc_scoped(ro, ifscope); |
0a7de745 | 2801 | if ((rt = ro->ro_rt) != NULL) { |
b0d623f7 | 2802 | RT_LOCK(rt); |
0a7de745 | 2803 | } |
1c79356b A |
2804 | } |
2805 | } | |
0a7de745 | 2806 | if (rt != NULL) { |
39236c6e | 2807 | RT_LOCK_ASSERT_HELD(rt); |
0a7de745 | 2808 | } |
2d21ac55 A |
2809 | |
2810 | /* | |
2811 | * Update MTU discovery determination. Don't do it if: | |
2812 | * 1) it is disabled via the sysctl | |
2813 | * 2) the route isn't up | |
2814 | * 3) the MTU is locked (if it is, then discovery has been | |
2815 | * disabled) | |
2816 | */ | |
2817 | ||
39037602 | 2818 | tp = intotcpcb(inp); |
2d21ac55 | 2819 | |
39037602 | 2820 | if (!path_mtu_discovery || ((rt != NULL) && |
0a7de745 | 2821 | (!(rt->rt_flags & RTF_UP) || (rt->rt_rmx.rmx_locks & RTV_MTU)))) { |
2d21ac55 | 2822 | tp->t_flags &= ~TF_PMTUD; |
0a7de745 | 2823 | } else { |
2d21ac55 | 2824 | tp->t_flags |= TF_PMTUD; |
0a7de745 | 2825 | } |
2d21ac55 | 2826 | |
b0d623f7 A |
2827 | if (rt != NULL && rt->rt_ifp != NULL) { |
2828 | somultipages(inp->inp_socket, | |
2829 | (rt->rt_ifp->if_hwassist & IFNET_MULTIPAGES)); | |
2830 | tcp_set_tso(tp, rt->rt_ifp); | |
fe8ab488 A |
2831 | soif2kcl(inp->inp_socket, |
2832 | (rt->rt_ifp->if_eflags & IFEF_2KCL)); | |
4bd07ac2 | 2833 | tcp_set_ecn(tp, rt->rt_ifp); |
5ba3f43e | 2834 | if (inp->inp_last_outifp == NULL) { |
39037602 | 2835 | inp->inp_last_outifp = rt->rt_ifp; |
5ba3f43e | 2836 | } |
b0d623f7 A |
2837 | } |
2838 | ||
39236c6e | 2839 | /* Note if the peer is local */ |
fe8ab488 | 2840 | if (rt != NULL && !(rt->rt_ifp->if_flags & IFF_POINTOPOINT) && |
0a7de745 A |
2841 | (rt->rt_gateway->sa_family == AF_LINK || |
2842 | rt->rt_ifp->if_flags & IFF_LOOPBACK || | |
2843 | in_localaddr(inp->inp_faddr))) { | |
39236c6e A |
2844 | tp->t_flags |= TF_LOCAL; |
2845 | } | |
39037602 | 2846 | |
b0d623f7 A |
2847 | /* |
2848 | * Caller needs to call RT_UNLOCK(rt). | |
2849 | */ | |
0a7de745 | 2850 | return rt; |
1c79356b A |
2851 | } |
2852 | ||
1c79356b | 2853 | struct rtentry * |
39037602 | 2854 | tcp_rtlookup6(struct inpcb *inp, unsigned int input_ifscope) |
1c79356b A |
2855 | { |
2856 | struct route_in6 *ro6; | |
2857 | struct rtentry *rt; | |
2d21ac55 A |
2858 | struct tcpcb *tp; |
2859 | ||
5ba3f43e | 2860 | LCK_MTX_ASSERT(rnh_lock, LCK_MTX_ASSERT_NOTOWNED); |
1c79356b A |
2861 | |
2862 | ro6 = &inp->in6p_route; | |
0a7de745 | 2863 | if ((rt = ro6->ro_rt) != NULL) { |
b0d623f7 | 2864 | RT_LOCK(rt); |
0a7de745 | 2865 | } |
b0d623f7 | 2866 | |
39236c6e A |
2867 | if (ROUTE_UNUSABLE(ro6)) { |
2868 | if (rt != NULL) { | |
2869 | RT_UNLOCK(rt); | |
2870 | rt = NULL; | |
2871 | } | |
2872 | ROUTE_RELEASE(ro6); | |
1c79356b A |
2873 | /* No route yet, so try to acquire one */ |
2874 | if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { | |
9bccf70c | 2875 | struct sockaddr_in6 *dst6; |
6d2010ae | 2876 | unsigned int ifscope; |
9bccf70c A |
2877 | |
2878 | dst6 = (struct sockaddr_in6 *)&ro6->ro_dst; | |
2879 | dst6->sin6_family = AF_INET6; | |
2880 | dst6->sin6_len = sizeof(*dst6); | |
2881 | dst6->sin6_addr = inp->in6p_faddr; | |
6d2010ae A |
2882 | |
2883 | /* | |
2884 | * If the socket was bound to an interface, then | |
2885 | * the bound-to-interface takes precedence over | |
2886 | * the inbound interface passed in by the caller | |
2887 | * (if we get here as part of the output path then | |
2888 | * input_ifscope is IFSCOPE_NONE). | |
2889 | */ | |
2890 | ifscope = (inp->inp_flags & INP_BOUND_IF) ? | |
316670eb | 2891 | inp->inp_boundifp->if_index : input_ifscope; |
6d2010ae | 2892 | |
6d2010ae | 2893 | rtalloc_scoped((struct route *)ro6, ifscope); |
0a7de745 | 2894 | if ((rt = ro6->ro_rt) != NULL) { |
b0d623f7 | 2895 | RT_LOCK(rt); |
0a7de745 | 2896 | } |
1c79356b A |
2897 | } |
2898 | } | |
0a7de745 | 2899 | if (rt != NULL) { |
39236c6e | 2900 | RT_LOCK_ASSERT_HELD(rt); |
0a7de745 | 2901 | } |
39236c6e | 2902 | |
2d21ac55 A |
2903 | /* |
2904 | * Update path MTU Discovery determination | |
2905 | * while looking up the route: | |
2906 | * 1) we have a valid route to the destination | |
2907 | * 2) the MTU is not locked (if it is, then discovery has been | |
2908 | * disabled) | |
2909 | */ | |
2910 | ||
2911 | ||
39037602 | 2912 | tp = intotcpcb(inp); |
2d21ac55 A |
2913 | |
2914 | /* | |
2915 | * Update MTU discovery determination. Don't do it if: | |
2916 | * 1) it is disabled via the sysctl | |
2917 | * 2) the route isn't up | |
2918 | * 3) the MTU is locked (if it is, then discovery has been | |
2919 | * disabled) | |
2920 | */ | |
2921 | ||
39037602 | 2922 | if (!path_mtu_discovery || ((rt != NULL) && |
0a7de745 | 2923 | (!(rt->rt_flags & RTF_UP) || (rt->rt_rmx.rmx_locks & RTV_MTU)))) { |
2d21ac55 | 2924 | tp->t_flags &= ~TF_PMTUD; |
0a7de745 | 2925 | } else { |
2d21ac55 | 2926 | tp->t_flags |= TF_PMTUD; |
0a7de745 | 2927 | } |
2d21ac55 | 2928 | |
b0d623f7 A |
2929 | if (rt != NULL && rt->rt_ifp != NULL) { |
2930 | somultipages(inp->inp_socket, | |
2931 | (rt->rt_ifp->if_hwassist & IFNET_MULTIPAGES)); | |
2932 | tcp_set_tso(tp, rt->rt_ifp); | |
fe8ab488 A |
2933 | soif2kcl(inp->inp_socket, |
2934 | (rt->rt_ifp->if_eflags & IFEF_2KCL)); | |
4bd07ac2 | 2935 | tcp_set_ecn(tp, rt->rt_ifp); |
5ba3f43e | 2936 | if (inp->inp_last_outifp == NULL) { |
39037602 | 2937 | inp->inp_last_outifp = rt->rt_ifp; |
5ba3f43e | 2938 | } |
b0d623f7 | 2939 | |
d9a64523 A |
2940 | /* Note if the peer is local */ |
2941 | if (!(rt->rt_ifp->if_flags & IFF_POINTOPOINT) && | |
0a7de745 A |
2942 | (IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr) || |
2943 | IN6_IS_ADDR_LINKLOCAL(&inp->in6p_faddr) || | |
2944 | rt->rt_gateway->sa_family == AF_LINK || | |
2945 | in6_localaddr(&inp->in6p_faddr))) { | |
d9a64523 A |
2946 | tp->t_flags |= TF_LOCAL; |
2947 | } | |
39236c6e A |
2948 | } |
2949 | ||
b0d623f7 A |
2950 | /* |
2951 | * Caller needs to call RT_UNLOCK(rt). | |
2952 | */ | |
0a7de745 | 2953 | return rt; |
1c79356b | 2954 | } |
1c79356b A |
2955 | |
2956 | #if IPSEC | |
2957 | /* compute ESP/AH header size for TCP, including outer IP header. */ | |
2958 | size_t | |
39037602 | 2959 | ipsec_hdrsiz_tcp(struct tcpcb *tp) |
1c79356b A |
2960 | { |
2961 | struct inpcb *inp; | |
2962 | struct mbuf *m; | |
2963 | size_t hdrsiz; | |
2964 | struct ip *ip; | |
1c79356b | 2965 | struct ip6_hdr *ip6 = NULL; |
1c79356b A |
2966 | struct tcphdr *th; |
2967 | ||
0a7de745 A |
2968 | if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL)) { |
2969 | return 0; | |
2970 | } | |
2971 | MGETHDR(m, M_DONTWAIT, MT_DATA); /* MAC-OK */ | |
2972 | if (!m) { | |
2973 | return 0; | |
2974 | } | |
9bccf70c | 2975 | |
9bccf70c | 2976 | if ((inp->inp_vflag & INP_IPV6) != 0) { |
1c79356b | 2977 | ip6 = mtod(m, struct ip6_hdr *); |
316670eb | 2978 | th = (struct tcphdr *)(void *)(ip6 + 1); |
9bccf70c | 2979 | m->m_pkthdr.len = m->m_len = |
0a7de745 | 2980 | sizeof(struct ip6_hdr) + sizeof(struct tcphdr); |
9bccf70c A |
2981 | tcp_fillheaders(tp, ip6, th); |
2982 | hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); | |
f427ee49 | 2983 | } else { |
39037602 A |
2984 | ip = mtod(m, struct ip *); |
2985 | th = (struct tcphdr *)(ip + 1); | |
2986 | m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr); | |
2987 | tcp_fillheaders(tp, ip, th); | |
2988 | hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); | |
2989 | } | |
1c79356b | 2990 | m_free(m); |
0a7de745 | 2991 | return hdrsiz; |
1c79356b | 2992 | } |
39037602 | 2993 | #endif /* IPSEC */ |
1c79356b | 2994 | |
91447636 | 2995 | int |
b0d623f7 | 2996 | tcp_lock(struct socket *so, int refcount, void *lr) |
91447636 | 2997 | { |
b0d623f7 A |
2998 | void *lr_saved; |
2999 | ||
0a7de745 | 3000 | if (lr == NULL) { |
b0d623f7 | 3001 | lr_saved = __builtin_return_address(0); |
0a7de745 | 3002 | } else { |
b0d623f7 | 3003 | lr_saved = lr; |
0a7de745 | 3004 | } |
91447636 | 3005 | |
5ba3f43e | 3006 | retry: |
b0d623f7 | 3007 | if (so->so_pcb != NULL) { |
5ba3f43e A |
3008 | if (so->so_flags & SOF_MP_SUBFLOW) { |
3009 | struct mptcb *mp_tp = tptomptp(sototcpcb(so)); | |
cb323159 | 3010 | struct socket *mp_so = mptetoso(mp_tp->mpt_mpte); |
5ba3f43e | 3011 | |
cb323159 | 3012 | socket_lock(mp_so, refcount); |
5ba3f43e A |
3013 | |
3014 | /* | |
3015 | * Check if we became non-MPTCP while waiting for the lock. | |
3016 | * If yes, we have to retry to grab the right lock. | |
3017 | */ | |
3018 | if (!(so->so_flags & SOF_MP_SUBFLOW)) { | |
cb323159 | 3019 | socket_unlock(mp_so, refcount); |
5ba3f43e A |
3020 | goto retry; |
3021 | } | |
3022 | } else { | |
3023 | lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx); | |
3024 | ||
3025 | if (so->so_flags & SOF_MP_SUBFLOW) { | |
3026 | /* | |
3027 | * While waiting for the lock, we might have | |
3028 | * become MPTCP-enabled (see mptcp_subflow_socreate). | |
3029 | */ | |
3030 | lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx); | |
3031 | goto retry; | |
3032 | } | |
3033 | } | |
0a7de745 | 3034 | } else { |
39037602 | 3035 | panic("tcp_lock: so=%p NO PCB! lr=%p lrh= %s\n", |
b0d623f7 A |
3036 | so, lr_saved, solockhistory_nr(so)); |
3037 | /* NOTREACHED */ | |
91447636 | 3038 | } |
91447636 | 3039 | |
b0d623f7 A |
3040 | if (so->so_usecount < 0) { |
3041 | panic("tcp_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n", | |
39037602 A |
3042 | so, so->so_pcb, lr_saved, so->so_usecount, |
3043 | solockhistory_nr(so)); | |
b0d623f7 A |
3044 | /* NOTREACHED */ |
3045 | } | |
0a7de745 | 3046 | if (refcount) { |
91447636 | 3047 | so->so_usecount++; |
0a7de745 | 3048 | } |
b0d623f7 | 3049 | so->lock_lr[so->next_lock_lr] = lr_saved; |
0a7de745 A |
3050 | so->next_lock_lr = (so->next_lock_lr + 1) % SO_LCKDBG_MAX; |
3051 | return 0; | |
91447636 A |
3052 | } |
3053 | ||
3054 | int | |
b0d623f7 | 3055 | tcp_unlock(struct socket *so, int refcount, void *lr) |
91447636 | 3056 | { |
b0d623f7 A |
3057 | void *lr_saved; |
3058 | ||
0a7de745 | 3059 | if (lr == NULL) { |
b0d623f7 | 3060 | lr_saved = __builtin_return_address(0); |
0a7de745 | 3061 | } else { |
b0d623f7 | 3062 | lr_saved = lr; |
0a7de745 | 3063 | } |
91447636 A |
3064 | |
3065 | #ifdef MORE_TCPLOCK_DEBUG | |
39236c6e A |
3066 | printf("tcp_unlock: so=0x%llx sopcb=0x%llx lock=0x%llx ref=%x " |
3067 | "lr=0x%llx\n", (uint64_t)VM_KERNEL_ADDRPERM(so), | |
3068 | (uint64_t)VM_KERNEL_ADDRPERM(so->so_pcb), | |
3069 | (uint64_t)VM_KERNEL_ADDRPERM(&(sotoinpcb(so)->inpcb_mtx)), | |
3070 | so->so_usecount, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved)); | |
91447636 | 3071 | #endif |
0a7de745 | 3072 | if (refcount) { |
91447636 | 3073 | so->so_usecount--; |
0a7de745 | 3074 | } |
91447636 | 3075 | |
b0d623f7 | 3076 | if (so->so_usecount < 0) { |
39037602 | 3077 | panic("tcp_unlock: so=%p usecount=%x lrh= %s\n", |
b0d623f7 A |
3078 | so, so->so_usecount, solockhistory_nr(so)); |
3079 | /* NOTREACHED */ | |
3080 | } | |
3081 | if (so->so_pcb == NULL) { | |
39037602 | 3082 | panic("tcp_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n", |
b0d623f7 A |
3083 | so, so->so_usecount, lr_saved, solockhistory_nr(so)); |
3084 | /* NOTREACHED */ | |
3085 | } else { | |
b0d623f7 | 3086 | so->unlock_lr[so->next_unlock_lr] = lr_saved; |
0a7de745 | 3087 | so->next_unlock_lr = (so->next_unlock_lr + 1) % SO_LCKDBG_MAX; |
5ba3f43e A |
3088 | |
3089 | if (so->so_flags & SOF_MP_SUBFLOW) { | |
3090 | struct mptcb *mp_tp = tptomptp(sototcpcb(so)); | |
cb323159 | 3091 | struct socket *mp_so = mptetoso(mp_tp->mpt_mpte); |
5ba3f43e | 3092 | |
cb323159 | 3093 | socket_lock_assert_owned(mp_so); |
5ba3f43e | 3094 | |
cb323159 | 3095 | socket_unlock(mp_so, refcount); |
5ba3f43e A |
3096 | } else { |
3097 | LCK_MTX_ASSERT(&((struct inpcb *)so->so_pcb)->inpcb_mtx, | |
3098 | LCK_MTX_ASSERT_OWNED); | |
3099 | lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx); | |
3100 | } | |
91447636 | 3101 | } |
0a7de745 | 3102 | return 0; |
91447636 A |
3103 | } |
3104 | ||
3105 | lck_mtx_t * | |
5ba3f43e | 3106 | tcp_getlock(struct socket *so, int flags) |
91447636 A |
3107 | { |
3108 | struct inpcb *inp = sotoinpcb(so); | |
3109 | ||
0a7de745 A |
3110 | if (so->so_pcb) { |
3111 | if (so->so_usecount < 0) { | |
39037602 A |
3112 | panic("tcp_getlock: so=%p usecount=%x lrh= %s\n", |
3113 | so, so->so_usecount, solockhistory_nr(so)); | |
0a7de745 | 3114 | } |
5ba3f43e A |
3115 | |
3116 | if (so->so_flags & SOF_MP_SUBFLOW) { | |
3117 | struct mptcb *mp_tp = tptomptp(sototcpcb(so)); | |
cb323159 | 3118 | struct socket *mp_so = mptetoso(mp_tp->mpt_mpte); |
5ba3f43e | 3119 | |
cb323159 | 3120 | return mp_so->so_proto->pr_getlock(mp_so, flags); |
5ba3f43e | 3121 | } else { |
0a7de745 | 3122 | return &inp->inpcb_mtx; |
5ba3f43e | 3123 | } |
39037602 A |
3124 | } else { |
3125 | panic("tcp_getlock: so=%p NULL so_pcb %s\n", | |
b0d623f7 | 3126 | so, solockhistory_nr(so)); |
0a7de745 | 3127 | return so->so_proto->pr_domain->dom_mtx; |
91447636 A |
3128 | } |
3129 | } | |
b0d623f7 | 3130 | |
39037602 | 3131 | /* |
3e170ce0 | 3132 | * Determine if we can grow the recieve socket buffer to avoid sending |
39037602 | 3133 | * a zero window update to the peer. We allow even socket buffers that |
316670eb A |
3134 | * have fixed size (set by the application) to grow if the resource |
3135 | * constraints are met. They will also be trimmed after the application | |
3136 | * reads data. | |
3137 | */ | |
3138 | static void | |
3e170ce0 A |
3139 | tcp_sbrcv_grow_rwin(struct tcpcb *tp, struct sockbuf *sb) |
3140 | { | |
fe8ab488 | 3141 | u_int32_t rcvbufinc = tp->t_maxseg << 4; |
39236c6e A |
3142 | u_int32_t rcvbuf = sb->sb_hiwat; |
3143 | struct socket *so = tp->t_inpcb->inp_socket; | |
3144 | ||
0a7de745 | 3145 | if (tcp_recv_bg == 1 || IS_TCP_RECV_BG(so)) { |
813fb2f6 | 3146 | return; |
0a7de745 | 3147 | } |
39236c6e | 3148 | |
316670eb | 3149 | if (tcp_do_autorcvbuf == 1 && |
0a7de745 A |
3150 | tcp_cansbgrow(sb) && |
3151 | (tp->t_flags & TF_SLOWLINK) == 0 && | |
3152 | (so->so_flags1 & SOF1_EXTEND_BK_IDLE_WANTED) == 0 && | |
3153 | (rcvbuf - sb->sb_cc) < rcvbufinc && | |
3154 | rcvbuf < tcp_autorcvbuf_max && | |
3155 | (sb->sb_idealsize > 0 && | |
3156 | sb->sb_hiwat <= (sb->sb_idealsize + rcvbufinc))) { | |
fe8ab488 A |
3157 | sbreserve(sb, |
3158 | min((sb->sb_hiwat + rcvbufinc), tcp_autorcvbuf_max)); | |
316670eb A |
3159 | } |
3160 | } | |
3161 | ||
b0d623f7 | 3162 | int32_t |
2d21ac55 A |
3163 | tcp_sbspace(struct tcpcb *tp) |
3164 | { | |
39037602 A |
3165 | struct socket *so = tp->t_inpcb->inp_socket; |
3166 | struct sockbuf *sb = &so->so_rcv; | |
d190cdc3 | 3167 | u_int32_t rcvbuf; |
316670eb | 3168 | int32_t space; |
fe8ab488 | 3169 | int32_t pending = 0; |
39236c6e | 3170 | |
cb323159 A |
3171 | if (so->so_flags & SOF_MP_SUBFLOW) { |
3172 | /* We still need to grow TCP's buffer to have a BDP-estimate */ | |
3173 | tcp_sbrcv_grow_rwin(tp, sb); | |
3174 | ||
3175 | return mptcp_sbspace(tptomptp(tp)); | |
3176 | } | |
3177 | ||
d190cdc3 A |
3178 | tcp_sbrcv_grow_rwin(tp, sb); |
3179 | ||
3180 | /* hiwat might have changed */ | |
3181 | rcvbuf = sb->sb_hiwat; | |
3182 | ||
39236c6e | 3183 | space = ((int32_t) imin((rcvbuf - sb->sb_cc), |
0a7de745 A |
3184 | (sb->sb_mbmax - sb->sb_mbcnt))); |
3185 | if (space < 0) { | |
b0d623f7 | 3186 | space = 0; |
0a7de745 | 3187 | } |
2d21ac55 | 3188 | |
fe8ab488 A |
3189 | #if CONTENT_FILTER |
3190 | /* Compensate for data being processed by content filters */ | |
3191 | pending = cfil_sock_data_space(sb); | |
3192 | #endif /* CONTENT_FILTER */ | |
0a7de745 | 3193 | if (pending > space) { |
fe8ab488 | 3194 | space = 0; |
0a7de745 | 3195 | } else { |
fe8ab488 | 3196 | space -= pending; |
0a7de745 | 3197 | } |
fe8ab488 | 3198 | |
39037602 A |
3199 | /* |
3200 | * Avoid increasing window size if the current window | |
2d21ac55 A |
3201 | * is already very low, we could be in "persist" mode and |
3202 | * we could break some apps (see rdar://5409343) | |
3203 | */ | |
3204 | ||
0a7de745 A |
3205 | if (space < tp->t_maxseg) { |
3206 | return space; | |
3207 | } | |
2d21ac55 | 3208 | |
39037602 | 3209 | /* Clip window size for slower link */ |
2d21ac55 | 3210 | |
0a7de745 A |
3211 | if (((tp->t_flags & TF_SLOWLINK) != 0) && slowlink_wsize > 0) { |
3212 | return imin(space, slowlink_wsize); | |
3213 | } | |
2d21ac55 | 3214 | |
0a7de745 | 3215 | return space; |
2d21ac55 | 3216 | } |
b0d623f7 | 3217 | /* |
fe8ab488 A |
3218 | * Checks TCP Segment Offloading capability for a given connection |
3219 | * and interface pair. | |
b0d623f7 A |
3220 | */ |
3221 | void | |
39236c6e | 3222 | tcp_set_tso(struct tcpcb *tp, struct ifnet *ifp) |
b0d623f7 | 3223 | { |
fe8ab488 A |
3224 | struct inpcb *inp; |
3225 | int isipv6; | |
f427ee49 A |
3226 | struct ifnet *tunnel_ifp = NULL; |
3227 | #define IFNET_TSO_MASK (IFNET_TSO_IPV6 | IFNET_TSO_IPV4) | |
3228 | ||
3229 | tp->t_flags &= ~TF_TSO; | |
3230 | ||
3231 | if (ifp == NULL) { | |
3232 | return; | |
3233 | } | |
3234 | ||
39236c6e A |
3235 | #if MPTCP |
3236 | /* | |
3237 | * We can't use TSO if this tcpcb belongs to an MPTCP session. | |
3238 | */ | |
3239 | if (tp->t_mpflags & TMPF_MPTCP_TRUE) { | |
39236c6e A |
3240 | return; |
3241 | } | |
3242 | #endif | |
fe8ab488 A |
3243 | inp = tp->t_inpcb; |
3244 | isipv6 = (inp->inp_vflag & INP_IPV6) != 0; | |
b0d623f7 | 3245 | |
f427ee49 A |
3246 | /* |
3247 | * We can't use TSO if the TSO capability of the tunnel interface does | |
3248 | * not match the capability of another interface known by TCP | |
3249 | */ | |
3250 | if (inp->inp_policyresult.results.result == NECP_KERNEL_POLICY_RESULT_IP_TUNNEL) { | |
3251 | u_int tunnel_if_index = inp->inp_policyresult.results.result_parameter.tunnel_interface_index; | |
3252 | ||
3253 | if (tunnel_if_index != 0) { | |
3254 | ifnet_head_lock_shared(); | |
3255 | tunnel_ifp = ifindex2ifnet[tunnel_if_index]; | |
3256 | ifnet_head_done(); | |
3257 | } | |
3258 | ||
3259 | if (tunnel_ifp == NULL) { | |
3260 | return; | |
3261 | } | |
3262 | ||
3263 | if ((ifp->if_hwassist & IFNET_TSO_MASK) != (tunnel_ifp->if_hwassist & IFNET_TSO_MASK)) { | |
3264 | if (tso_debug > 0) { | |
3265 | os_log(OS_LOG_DEFAULT, | |
3266 | "%s: %u > %u TSO 0 tunnel_ifp %s hwassist mismatch with ifp %s", | |
3267 | __func__, | |
3268 | ntohs(tp->t_inpcb->inp_lport), ntohs(tp->t_inpcb->inp_fport), | |
3269 | tunnel_ifp->if_xname, ifp->if_xname); | |
3270 | } | |
3271 | return; | |
3272 | } | |
3273 | if (inp->inp_last_outifp != NULL && | |
3274 | (inp->inp_last_outifp->if_hwassist & IFNET_TSO_MASK) != (tunnel_ifp->if_hwassist & IFNET_TSO_MASK)) { | |
3275 | if (tso_debug > 0) { | |
3276 | os_log(OS_LOG_DEFAULT, | |
3277 | "%s: %u > %u TSO 0 tunnel_ifp %s hwassist mismatch with inp_last_outifp %s", | |
3278 | __func__, | |
3279 | ntohs(tp->t_inpcb->inp_lport), ntohs(tp->t_inpcb->inp_fport), | |
3280 | tunnel_ifp->if_xname, inp->inp_last_outifp->if_xname); | |
3281 | } | |
3282 | return; | |
3283 | } | |
3284 | if ((inp->inp_flags & INP_BOUND_IF) && inp->inp_boundifp != NULL && | |
3285 | (inp->inp_boundifp->if_hwassist & IFNET_TSO_MASK) != (tunnel_ifp->if_hwassist & IFNET_TSO_MASK)) { | |
3286 | if (tso_debug > 0) { | |
3287 | os_log(OS_LOG_DEFAULT, | |
3288 | "%s: %u > %u TSO 0 tunnel_ifp %s hwassist mismatch with inp_boundifp %s", | |
3289 | __func__, | |
3290 | ntohs(tp->t_inpcb->inp_lport), ntohs(tp->t_inpcb->inp_fport), | |
3291 | tunnel_ifp->if_xname, inp->inp_boundifp->if_xname); | |
3292 | } | |
3293 | return; | |
3294 | } | |
3295 | } | |
3296 | ||
b0d623f7 | 3297 | if (isipv6) { |
f427ee49 | 3298 | if (ifp->if_hwassist & IFNET_TSO_IPV6) { |
b0d623f7 | 3299 | tp->t_flags |= TF_TSO; |
0a7de745 | 3300 | if (ifp->if_tso_v6_mtu != 0) { |
b0d623f7 | 3301 | tp->tso_max_segment_size = ifp->if_tso_v6_mtu; |
0a7de745 | 3302 | } else { |
b0d623f7 | 3303 | tp->tso_max_segment_size = TCP_MAXWIN; |
0a7de745 | 3304 | } |
0a7de745 | 3305 | } |
f427ee49 A |
3306 | } else { |
3307 | if (ifp->if_hwassist & IFNET_TSO_IPV4) { | |
b0d623f7 | 3308 | tp->t_flags |= TF_TSO; |
0a7de745 | 3309 | if (ifp->if_tso_v4_mtu != 0) { |
b0d623f7 | 3310 | tp->tso_max_segment_size = ifp->if_tso_v4_mtu; |
0a7de745 | 3311 | } else { |
b0d623f7 | 3312 | tp->tso_max_segment_size = TCP_MAXWIN; |
0a7de745 | 3313 | } |
f427ee49 A |
3314 | if (INTF_ADJUST_MTU_FOR_CLAT46(ifp)) { |
3315 | tp->tso_max_segment_size -= | |
3316 | CLAT46_HDR_EXPANSION_OVERHD; | |
3317 | } | |
0a7de745 | 3318 | } |
b0d623f7 | 3319 | } |
f427ee49 A |
3320 | |
3321 | if (tso_debug > 1) { | |
3322 | os_log(OS_LOG_DEFAULT, "%s: %u > %u TSO %d ifp %s", | |
3323 | __func__, | |
3324 | ntohs(tp->t_inpcb->inp_lport), | |
3325 | ntohs(tp->t_inpcb->inp_fport), | |
3326 | (tp->t_flags & TF_TSO) != 0, | |
3327 | ifp != NULL ? ifp->if_xname : "<NULL>"); | |
3328 | } | |
b0d623f7 | 3329 | } |
6d2010ae | 3330 | |
f427ee49 A |
3331 | #define TIMEVAL_TO_TCPHZ(_tv_) ((uint32_t)((_tv_).tv_sec * TCP_RETRANSHZ + \ |
3332 | (_tv_).tv_usec / TCP_RETRANSHZ_TO_USEC)) | |
6d2010ae | 3333 | |
39037602 A |
3334 | /* |
3335 | * Function to calculate the tcp clock. The tcp clock will get updated | |
6d2010ae | 3336 | * at the boundaries of the tcp layer. This is done at 3 places: |
39037602 | 3337 | * 1. Right before processing an input tcp packet |
6d2010ae A |
3338 | * 2. Whenever a connection wants to access the network using tcp_usrreqs |
3339 | * 3. When a tcp timer fires or before tcp slow timeout | |
3340 | * | |
3341 | */ | |
3342 | ||
3343 | void | |
39037602 | 3344 | calculate_tcp_clock(void) |
6d2010ae A |
3345 | { |
3346 | struct timeval tv = tcp_uptime; | |
cb323159 | 3347 | struct timeval interval = {.tv_sec = 0, .tv_usec = TCP_RETRANSHZ_TO_USEC}; |
6d2010ae A |
3348 | struct timeval now, hold_now; |
3349 | uint32_t incr = 0; | |
3350 | ||
6d2010ae | 3351 | microuptime(&now); |
39236c6e A |
3352 | |
3353 | /* | |
3354 | * Update coarse-grained networking timestamp (in sec.); the idea | |
3355 | * is to update the counter returnable via net_uptime() when | |
3356 | * we read time. | |
3357 | */ | |
5ba3f43e | 3358 | net_update_uptime_with_time(&now); |
39236c6e A |
3359 | |
3360 | timevaladd(&tv, &interval); | |
6d2010ae A |
3361 | if (timevalcmp(&now, &tv, >)) { |
3362 | /* time to update the clock */ | |
3363 | lck_spin_lock(tcp_uptime_lock); | |
3364 | if (timevalcmp(&tcp_uptime, &now, >=)) { | |
fe8ab488 | 3365 | /* clock got updated while waiting for the lock */ |
6d2010ae A |
3366 | lck_spin_unlock(tcp_uptime_lock); |
3367 | return; | |
fe8ab488 | 3368 | } |
6d2010ae A |
3369 | |
3370 | microuptime(&now); | |
3371 | hold_now = now; | |
3372 | tv = tcp_uptime; | |
3373 | timevalsub(&now, &tv); | |
3374 | ||
3375 | incr = TIMEVAL_TO_TCPHZ(now); | |
3376 | if (incr > 0) { | |
3377 | tcp_uptime = hold_now; | |
3378 | tcp_now += incr; | |
3379 | } | |
3380 | ||
39037602 A |
3381 | lck_spin_unlock(tcp_uptime_lock); |
3382 | } | |
6d2010ae A |
3383 | } |
3384 | ||
39037602 A |
3385 | /* |
3386 | * Compute receive window scaling that we are going to request | |
3387 | * for this connection based on sb_hiwat. Try to leave some | |
3388 | * room to potentially increase the window size upto a maximum | |
316670eb A |
3389 | * defined by the constant tcp_autorcvbuf_max. |
3390 | */ | |
3391 | void | |
f427ee49 | 3392 | tcp_set_max_rwinscale(struct tcpcb *tp, struct socket *so) |
39037602 | 3393 | { |
d9a64523 | 3394 | uint32_t maxsockbufsize; |
d9a64523 | 3395 | |
f427ee49 A |
3396 | tp->request_r_scale = MAX((uint8_t)tcp_win_scale, tp->request_r_scale); |
3397 | maxsockbufsize = ((so->so_rcv.sb_flags & SB_USRSIZE) != 0) ? | |
3398 | so->so_rcv.sb_hiwat : tcp_autorcvbuf_max; | |
316670eb | 3399 | |
d9a64523 | 3400 | /* |
f427ee49 A |
3401 | * Window scale should not exceed what is needed |
3402 | * to send the max receive window size; adding 1 to TCP_MAXWIN | |
3403 | * ensures that. | |
d9a64523 | 3404 | */ |
316670eb | 3405 | while (tp->request_r_scale < TCP_MAX_WINSHIFT && |
f427ee49 | 3406 | ((TCP_MAXWIN + 1) << tp->request_r_scale) < maxsockbufsize) { |
316670eb | 3407 | tp->request_r_scale++; |
0a7de745 | 3408 | } |
f427ee49 | 3409 | tp->request_r_scale = MIN(tp->request_r_scale, TCP_MAX_WINSHIFT); |
316670eb A |
3410 | } |
3411 | ||
3412 | int | |
d9a64523 A |
3413 | tcp_notsent_lowat_check(struct socket *so) |
3414 | { | |
316670eb A |
3415 | struct inpcb *inp = sotoinpcb(so); |
3416 | struct tcpcb *tp = NULL; | |
3417 | int notsent = 0; | |
d9a64523 | 3418 | |
316670eb A |
3419 | if (inp != NULL) { |
3420 | tp = intotcpcb(inp); | |
3421 | } | |
3422 | ||
d9a64523 | 3423 | if (tp == NULL) { |
0a7de745 | 3424 | return 0; |
d9a64523 A |
3425 | } |
3426 | ||
39037602 | 3427 | notsent = so->so_snd.sb_cc - |
0a7de745 | 3428 | (tp->snd_nxt - tp->snd_una); |
316670eb | 3429 | |
39037602 A |
3430 | /* |
3431 | * When we send a FIN or SYN, not_sent can be negative. | |
3432 | * In that case also we need to send a write event to the | |
316670eb A |
3433 | * process if it is waiting. In the FIN case, it will |
3434 | * get an error from send because cantsendmore will be set. | |
3435 | */ | |
3436 | if (notsent <= tp->t_notsent_lowat) { | |
0a7de745 | 3437 | return 1; |
316670eb A |
3438 | } |
3439 | ||
39037602 A |
3440 | /* |
3441 | * When Nagle's algorithm is not disabled, it is better | |
316670eb A |
3442 | * to wakeup the client until there is atleast one |
3443 | * maxseg of data to write. | |
3444 | */ | |
39037602 | 3445 | if ((tp->t_flags & TF_NODELAY) == 0 && |
0a7de745 A |
3446 | notsent > 0 && notsent < tp->t_maxseg) { |
3447 | return 1; | |
316670eb | 3448 | } |
0a7de745 | 3449 | return 0; |
316670eb A |
3450 | } |
3451 | ||
3e170ce0 | 3452 | void |
39037602 A |
3453 | tcp_rxtseg_insert(struct tcpcb *tp, tcp_seq start, tcp_seq end) |
3454 | { | |
3e170ce0 | 3455 | struct tcp_rxt_seg *rxseg = NULL, *prev = NULL, *next = NULL; |
f427ee49 | 3456 | uint16_t rxcount = 0; |
3e170ce0 | 3457 | |
0a7de745 | 3458 | if (SLIST_EMPTY(&tp->t_rxt_segments)) { |
3e170ce0 | 3459 | tp->t_dsack_lastuna = tp->snd_una; |
0a7de745 | 3460 | } |
3e170ce0 A |
3461 | /* |
3462 | * First check if there is a segment already existing for this | |
3463 | * sequence space. | |
3464 | */ | |
3465 | ||
3466 | SLIST_FOREACH(rxseg, &tp->t_rxt_segments, rx_link) { | |
0a7de745 | 3467 | if (SEQ_GT(rxseg->rx_start, start)) { |
3e170ce0 | 3468 | break; |
0a7de745 | 3469 | } |
3e170ce0 A |
3470 | prev = rxseg; |
3471 | } | |
3472 | next = rxseg; | |
3473 | ||
3474 | /* check if prev seg is for this sequence */ | |
3475 | if (prev != NULL && SEQ_LEQ(prev->rx_start, start) && | |
3476 | SEQ_GEQ(prev->rx_end, end)) { | |
3477 | prev->rx_count++; | |
3478 | return; | |
3479 | } | |
3480 | ||
3481 | /* | |
3482 | * There are a couple of possibilities at this point. | |
3483 | * 1. prev overlaps with the beginning of this sequence | |
3484 | * 2. next overlaps with the end of this sequence | |
3485 | * 3. there is no overlap. | |
3486 | */ | |
3487 | ||
3488 | if (prev != NULL && SEQ_GT(prev->rx_end, start)) { | |
3489 | if (prev->rx_start == start && SEQ_GT(end, prev->rx_end)) { | |
3490 | start = prev->rx_end + 1; | |
3491 | prev->rx_count++; | |
3492 | } else { | |
3493 | prev->rx_end = (start - 1); | |
3494 | rxcount = prev->rx_count; | |
3495 | } | |
3496 | } | |
3497 | ||
3498 | if (next != NULL && SEQ_LT(next->rx_start, end)) { | |
3499 | if (SEQ_LEQ(next->rx_end, end)) { | |
3500 | end = next->rx_start - 1; | |
3501 | next->rx_count++; | |
3502 | } else { | |
3503 | next->rx_start = end + 1; | |
3504 | rxcount = next->rx_count; | |
3505 | } | |
3506 | } | |
0a7de745 | 3507 | if (!SEQ_LT(start, end)) { |
3e170ce0 | 3508 | return; |
0a7de745 | 3509 | } |
3e170ce0 A |
3510 | |
3511 | rxseg = (struct tcp_rxt_seg *) zalloc(tcp_rxt_seg_zone); | |
3512 | if (rxseg == NULL) { | |
3513 | return; | |
3514 | } | |
3515 | bzero(rxseg, sizeof(*rxseg)); | |
3516 | rxseg->rx_start = start; | |
3517 | rxseg->rx_end = end; | |
3518 | rxseg->rx_count = rxcount + 1; | |
3519 | ||
3520 | if (prev != NULL) { | |
3521 | SLIST_INSERT_AFTER(prev, rxseg, rx_link); | |
3522 | } else { | |
3523 | SLIST_INSERT_HEAD(&tp->t_rxt_segments, rxseg, rx_link); | |
3524 | } | |
3e170ce0 A |
3525 | } |
3526 | ||
3527 | struct tcp_rxt_seg * | |
3528 | tcp_rxtseg_find(struct tcpcb *tp, tcp_seq start, tcp_seq end) | |
3529 | { | |
3530 | struct tcp_rxt_seg *rxseg; | |
0a7de745 A |
3531 | if (SLIST_EMPTY(&tp->t_rxt_segments)) { |
3532 | return NULL; | |
3533 | } | |
3e170ce0 A |
3534 | |
3535 | SLIST_FOREACH(rxseg, &tp->t_rxt_segments, rx_link) { | |
3536 | if (SEQ_LEQ(rxseg->rx_start, start) && | |
0a7de745 A |
3537 | SEQ_GEQ(rxseg->rx_end, end)) { |
3538 | return rxseg; | |
3539 | } | |
3540 | if (SEQ_GT(rxseg->rx_start, start)) { | |
3e170ce0 | 3541 | break; |
0a7de745 | 3542 | } |
3e170ce0 | 3543 | } |
0a7de745 | 3544 | return NULL; |
3e170ce0 A |
3545 | } |
3546 | ||
f427ee49 A |
3547 | void |
3548 | tcp_rxtseg_set_spurious(struct tcpcb *tp, tcp_seq start, tcp_seq end) | |
3549 | { | |
3550 | struct tcp_rxt_seg *rxseg; | |
3551 | if (SLIST_EMPTY(&tp->t_rxt_segments)) { | |
3552 | return; | |
3553 | } | |
3554 | ||
3555 | SLIST_FOREACH(rxseg, &tp->t_rxt_segments, rx_link) { | |
3556 | if (SEQ_GEQ(rxseg->rx_start, start) && | |
3557 | SEQ_LEQ(rxseg->rx_end, end)) { | |
3558 | /* | |
3559 | * If the segment was retransmitted only once, mark it as | |
3560 | * spurious. | |
3561 | */ | |
3562 | if (rxseg->rx_count == 1) { | |
3563 | rxseg->rx_flags |= TCP_RXT_SPURIOUS; | |
3564 | } | |
3565 | } | |
3566 | ||
3567 | if (SEQ_GEQ(rxseg->rx_start, end)) { | |
3568 | break; | |
3569 | } | |
3570 | } | |
3571 | return; | |
3572 | } | |
3573 | ||
3e170ce0 A |
3574 | void |
3575 | tcp_rxtseg_clean(struct tcpcb *tp) | |
3576 | { | |
3577 | struct tcp_rxt_seg *rxseg, *next; | |
3578 | ||
3579 | SLIST_FOREACH_SAFE(rxseg, &tp->t_rxt_segments, rx_link, next) { | |
3580 | SLIST_REMOVE(&tp->t_rxt_segments, rxseg, | |
3581 | tcp_rxt_seg, rx_link); | |
3582 | zfree(tcp_rxt_seg_zone, rxseg); | |
3583 | } | |
3584 | tp->t_dsack_lastuna = tp->snd_max; | |
3585 | } | |
3586 | ||
3587 | boolean_t | |
3588 | tcp_rxtseg_detect_bad_rexmt(struct tcpcb *tp, tcp_seq th_ack) | |
3589 | { | |
3590 | boolean_t bad_rexmt; | |
3591 | struct tcp_rxt_seg *rxseg; | |
3592 | ||
0a7de745 A |
3593 | if (SLIST_EMPTY(&tp->t_rxt_segments)) { |
3594 | return FALSE; | |
3595 | } | |
3e170ce0 A |
3596 | |
3597 | /* | |
3598 | * If all of the segments in this window are not cumulatively | |
3599 | * acknowledged, then there can still be undetected packet loss. | |
3600 | * Do not restore congestion window in that case. | |
3601 | */ | |
0a7de745 A |
3602 | if (SEQ_LT(th_ack, tp->snd_recover)) { |
3603 | return FALSE; | |
3604 | } | |
3e170ce0 A |
3605 | |
3606 | bad_rexmt = TRUE; | |
3607 | SLIST_FOREACH(rxseg, &tp->t_rxt_segments, rx_link) { | |
f427ee49 | 3608 | if (!(rxseg->rx_flags & TCP_RXT_SPURIOUS)) { |
3e170ce0 A |
3609 | bad_rexmt = FALSE; |
3610 | break; | |
3611 | } | |
3612 | } | |
0a7de745 | 3613 | return bad_rexmt; |
3e170ce0 A |
3614 | } |
3615 | ||
3616 | boolean_t | |
3617 | tcp_rxtseg_dsack_for_tlp(struct tcpcb *tp) | |
3618 | { | |
3619 | boolean_t dsack_for_tlp = FALSE; | |
3620 | struct tcp_rxt_seg *rxseg; | |
0a7de745 A |
3621 | if (SLIST_EMPTY(&tp->t_rxt_segments)) { |
3622 | return FALSE; | |
3623 | } | |
3e170ce0 A |
3624 | |
3625 | SLIST_FOREACH(rxseg, &tp->t_rxt_segments, rx_link) { | |
3626 | if (rxseg->rx_count == 1 && | |
39037602 | 3627 | SLIST_NEXT(rxseg, rx_link) == NULL && |
3e170ce0 A |
3628 | (rxseg->rx_flags & TCP_RXT_DSACK_FOR_TLP)) { |
3629 | dsack_for_tlp = TRUE; | |
3630 | break; | |
3631 | } | |
3632 | } | |
0a7de745 | 3633 | return dsack_for_tlp; |
3e170ce0 A |
3634 | } |
3635 | ||
3636 | u_int32_t | |
39037602 A |
3637 | tcp_rxtseg_total_size(struct tcpcb *tp) |
3638 | { | |
3e170ce0 A |
3639 | struct tcp_rxt_seg *rxseg; |
3640 | u_int32_t total_size = 0; | |
3641 | ||
3642 | SLIST_FOREACH(rxseg, &tp->t_rxt_segments, rx_link) { | |
3643 | total_size += (rxseg->rx_end - rxseg->rx_start) + 1; | |
3644 | } | |
0a7de745 | 3645 | return total_size; |
3e170ce0 A |
3646 | } |
3647 | ||
3648 | void | |
3649 | tcp_get_connectivity_status(struct tcpcb *tp, | |
0a7de745 | 3650 | struct tcp_conn_status *connstatus) |
3e170ce0 | 3651 | { |
0a7de745 | 3652 | if (tp == NULL || connstatus == NULL) { |
3e170ce0 | 3653 | return; |
0a7de745 | 3654 | } |
3e170ce0 A |
3655 | bzero(connstatus, sizeof(*connstatus)); |
3656 | if (tp->t_rxtshift >= TCP_CONNECTIVITY_PROBES_MAX) { | |
3657 | if (TCPS_HAVEESTABLISHED(tp->t_state)) { | |
3658 | connstatus->write_probe_failed = 1; | |
3659 | } else { | |
3660 | connstatus->conn_probe_failed = 1; | |
3661 | } | |
3662 | } | |
0a7de745 | 3663 | if (tp->t_rtimo_probes >= TCP_CONNECTIVITY_PROBES_MAX) { |
3e170ce0 | 3664 | connstatus->read_probe_failed = 1; |
0a7de745 | 3665 | } |
39037602 | 3666 | if (tp->t_inpcb != NULL && tp->t_inpcb->inp_last_outifp != NULL && |
0a7de745 | 3667 | (tp->t_inpcb->inp_last_outifp->if_eflags & IFEF_PROBE_CONNECTIVITY)) { |
3e170ce0 | 3668 | connstatus->probe_activated = 1; |
0a7de745 | 3669 | } |
3e170ce0 A |
3670 | } |
3671 | ||
3672 | boolean_t | |
3673 | tfo_enabled(const struct tcpcb *tp) | |
3674 | { | |
0a7de745 | 3675 | return (tp->t_flagsext & TF_FASTOPEN)? TRUE : FALSE; |
3e170ce0 A |
3676 | } |
3677 | ||
3678 | void | |
3679 | tcp_disable_tfo(struct tcpcb *tp) | |
3680 | { | |
3681 | tp->t_flagsext &= ~TF_FASTOPEN; | |
3682 | } | |
316670eb | 3683 | |
39037602 A |
3684 | static struct mbuf * |
3685 | tcp_make_keepalive_frame(struct tcpcb *tp, struct ifnet *ifp, | |
3686 | boolean_t is_probe) | |
3687 | { | |
3688 | struct inpcb *inp = tp->t_inpcb; | |
3689 | struct tcphdr *th; | |
3690 | u_int8_t *data; | |
3691 | int win = 0; | |
3692 | struct mbuf *m; | |
3693 | ||
3694 | /* | |
3695 | * The code assumes the IP + TCP headers fit in an mbuf packet header | |
3696 | */ | |
3697 | _CASSERT(sizeof(struct ip) + sizeof(struct tcphdr) <= _MHLEN); | |
3698 | _CASSERT(sizeof(struct ip6_hdr) + sizeof(struct tcphdr) <= _MHLEN); | |
3699 | ||
3700 | MGETHDR(m, M_WAIT, MT_HEADER); | |
3701 | if (m == NULL) { | |
0a7de745 | 3702 | return NULL; |
39037602 A |
3703 | } |
3704 | m->m_pkthdr.pkt_proto = IPPROTO_TCP; | |
3705 | ||
3706 | data = mbuf_datastart(m); | |
3707 | ||
3708 | if (inp->inp_vflag & INP_IPV4) { | |
3709 | bzero(data, sizeof(struct ip) + sizeof(struct tcphdr)); | |
3710 | th = (struct tcphdr *)(void *) (data + sizeof(struct ip)); | |
3711 | m->m_len = sizeof(struct ip) + sizeof(struct tcphdr); | |
3712 | m->m_pkthdr.len = m->m_len; | |
3713 | } else { | |
3714 | VERIFY(inp->inp_vflag & INP_IPV6); | |
3715 | ||
3716 | bzero(data, sizeof(struct ip6_hdr) | |
3717 | + sizeof(struct tcphdr)); | |
3718 | th = (struct tcphdr *)(void *)(data + sizeof(struct ip6_hdr)); | |
3719 | m->m_len = sizeof(struct ip6_hdr) + | |
3720 | sizeof(struct tcphdr); | |
3721 | m->m_pkthdr.len = m->m_len; | |
3722 | } | |
3723 | ||
3724 | tcp_fillheaders(tp, data, th); | |
3725 | ||
3726 | if (inp->inp_vflag & INP_IPV4) { | |
3727 | struct ip *ip; | |
3728 | ||
3729 | ip = (__typeof__(ip))(void *)data; | |
3730 | ||
5ba3f43e A |
3731 | ip->ip_id = rfc6864 ? 0 : ip_randomid(); |
3732 | ip->ip_off = htons(IP_DF); | |
39037602 A |
3733 | ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr)); |
3734 | ip->ip_ttl = inp->inp_ip_ttl; | |
813fb2f6 | 3735 | ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK); |
39037602 A |
3736 | ip->ip_sum = in_cksum_hdr(ip); |
3737 | } else { | |
3738 | struct ip6_hdr *ip6; | |
3739 | ||
3740 | ip6 = (__typeof__(ip6))(void *)data; | |
3741 | ||
3742 | ip6->ip6_plen = htons(sizeof(struct tcphdr)); | |
3743 | ip6->ip6_hlim = in6_selecthlim(inp, ifp); | |
813fb2f6 | 3744 | ip6->ip6_flow = ip6->ip6_flow & ~IPV6_FLOW_ECN_MASK; |
39037602 | 3745 | |
0a7de745 | 3746 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { |
39037602 | 3747 | ip6->ip6_src.s6_addr16[1] = 0; |
0a7de745 A |
3748 | } |
3749 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { | |
39037602 | 3750 | ip6->ip6_dst.s6_addr16[1] = 0; |
0a7de745 | 3751 | } |
39037602 A |
3752 | } |
3753 | th->th_flags = TH_ACK; | |
3754 | ||
3755 | win = tcp_sbspace(tp); | |
0a7de745 A |
3756 | if (win > ((int32_t)TCP_MAXWIN << tp->rcv_scale)) { |
3757 | win = (int32_t)TCP_MAXWIN << tp->rcv_scale; | |
3758 | } | |
39037602 A |
3759 | th->th_win = htons((u_short) (win >> tp->rcv_scale)); |
3760 | ||
3761 | if (is_probe) { | |
3762 | th->th_seq = htonl(tp->snd_una - 1); | |
3763 | } else { | |
3764 | th->th_seq = htonl(tp->snd_una); | |
3765 | } | |
3766 | th->th_ack = htonl(tp->rcv_nxt); | |
3767 | ||
3768 | /* Force recompute TCP checksum to be the final value */ | |
3769 | th->th_sum = 0; | |
3770 | if (inp->inp_vflag & INP_IPV4) { | |
3771 | th->th_sum = inet_cksum(m, IPPROTO_TCP, | |
3772 | sizeof(struct ip), sizeof(struct tcphdr)); | |
3773 | } else { | |
3774 | th->th_sum = inet6_cksum(m, IPPROTO_TCP, | |
3775 | sizeof(struct ip6_hdr), sizeof(struct tcphdr)); | |
3776 | } | |
3777 | ||
0a7de745 | 3778 | return m; |
39037602 A |
3779 | } |
3780 | ||
3781 | void | |
3782 | tcp_fill_keepalive_offload_frames(ifnet_t ifp, | |
3783 | struct ifnet_keepalive_offload_frame *frames_array, | |
3784 | u_int32_t frames_array_count, size_t frame_data_offset, | |
3785 | u_int32_t *used_frames_count) | |
3786 | { | |
3787 | struct inpcb *inp; | |
3788 | inp_gen_t gencnt; | |
3789 | u_int32_t frame_index = *used_frames_count; | |
3790 | ||
3791 | if (ifp == NULL || frames_array == NULL || | |
3792 | frames_array_count == 0 || | |
3793 | frame_index >= frames_array_count || | |
0a7de745 | 3794 | frame_data_offset >= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) { |
39037602 | 3795 | return; |
0a7de745 | 3796 | } |
39037602 A |
3797 | |
3798 | /* | |
3799 | * This function is called outside the regular TCP processing | |
3800 | * so we need to update the TCP clock. | |
3801 | */ | |
3802 | calculate_tcp_clock(); | |
3803 | ||
3804 | lck_rw_lock_shared(tcbinfo.ipi_lock); | |
3805 | gencnt = tcbinfo.ipi_gencnt; | |
3806 | LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) { | |
3807 | struct socket *so; | |
3808 | struct ifnet_keepalive_offload_frame *frame; | |
3809 | struct mbuf *m = NULL; | |
3810 | struct tcpcb *tp = intotcpcb(inp); | |
3811 | ||
0a7de745 | 3812 | if (frame_index >= frames_array_count) { |
39037602 | 3813 | break; |
0a7de745 | 3814 | } |
39037602 A |
3815 | |
3816 | if (inp->inp_gencnt > gencnt || | |
0a7de745 | 3817 | inp->inp_state == INPCB_STATE_DEAD) { |
39037602 | 3818 | continue; |
0a7de745 | 3819 | } |
39037602 A |
3820 | |
3821 | if ((so = inp->inp_socket) == NULL || | |
0a7de745 | 3822 | (so->so_state & SS_DEFUNCT)) { |
39037602 | 3823 | continue; |
0a7de745 | 3824 | } |
39037602 A |
3825 | /* |
3826 | * check for keepalive offload flag without socket | |
3827 | * lock to avoid a deadlock | |
3828 | */ | |
3829 | if (!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD)) { | |
3830 | continue; | |
3831 | } | |
3832 | ||
3833 | if (!(inp->inp_vflag & (INP_IPV4 | INP_IPV6))) { | |
3834 | continue; | |
3835 | } | |
3836 | if (inp->inp_ppcb == NULL || | |
0a7de745 | 3837 | in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) { |
39037602 | 3838 | continue; |
0a7de745 | 3839 | } |
5ba3f43e | 3840 | socket_lock(so, 1); |
39037602 A |
3841 | /* Release the want count */ |
3842 | if (inp->inp_ppcb == NULL || | |
3843 | (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING)) { | |
5ba3f43e | 3844 | socket_unlock(so, 1); |
39037602 A |
3845 | continue; |
3846 | } | |
3847 | if ((inp->inp_vflag & INP_IPV4) && | |
3848 | (inp->inp_laddr.s_addr == INADDR_ANY || | |
3849 | inp->inp_faddr.s_addr == INADDR_ANY)) { | |
5ba3f43e | 3850 | socket_unlock(so, 1); |
39037602 A |
3851 | continue; |
3852 | } | |
3853 | if ((inp->inp_vflag & INP_IPV6) && | |
3854 | (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) || | |
3855 | IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))) { | |
5ba3f43e | 3856 | socket_unlock(so, 1); |
39037602 A |
3857 | continue; |
3858 | } | |
3859 | if (inp->inp_lport == 0 || inp->inp_fport == 0) { | |
5ba3f43e | 3860 | socket_unlock(so, 1); |
39037602 A |
3861 | continue; |
3862 | } | |
3863 | if (inp->inp_last_outifp == NULL || | |
3864 | inp->inp_last_outifp->if_index != ifp->if_index) { | |
5ba3f43e | 3865 | socket_unlock(so, 1); |
39037602 A |
3866 | continue; |
3867 | } | |
3868 | if ((inp->inp_vflag & INP_IPV4) && frame_data_offset + | |
3869 | sizeof(struct ip) + sizeof(struct tcphdr) > | |
3870 | IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) { | |
5ba3f43e | 3871 | socket_unlock(so, 1); |
39037602 A |
3872 | continue; |
3873 | } else if (!(inp->inp_vflag & INP_IPV4) && frame_data_offset + | |
3874 | sizeof(struct ip6_hdr) + sizeof(struct tcphdr) > | |
3875 | IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) { | |
5ba3f43e | 3876 | socket_unlock(so, 1); |
39037602 A |
3877 | continue; |
3878 | } | |
3879 | /* | |
3880 | * There is no point in waking up the device for connections | |
3881 | * that are not established. Long lived connection are meant | |
3882 | * for processes that will sent and receive data | |
3883 | */ | |
3884 | if (tp->t_state != TCPS_ESTABLISHED) { | |
5ba3f43e | 3885 | socket_unlock(so, 1); |
39037602 A |
3886 | continue; |
3887 | } | |
3888 | /* | |
3889 | * This inp has all the information that is needed to | |
3890 | * generate an offload frame. | |
3891 | */ | |
3892 | frame = &frames_array[frame_index]; | |
3893 | frame->type = IFNET_KEEPALIVE_OFFLOAD_FRAME_TCP; | |
3894 | frame->ether_type = (inp->inp_vflag & INP_IPV4) ? | |
3895 | IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4 : | |
3896 | IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6; | |
f427ee49 A |
3897 | frame->interval = (uint16_t)(tp->t_keepidle > 0 ? tp->t_keepidle : |
3898 | tcp_keepidle); | |
3899 | frame->keep_cnt = (uint8_t)TCP_CONN_KEEPCNT(tp); | |
3900 | frame->keep_retry = (uint16_t)TCP_CONN_KEEPINTVL(tp); | |
cb323159 A |
3901 | if (so->so_options & SO_NOWAKEFROMSLEEP) { |
3902 | frame->flags |= | |
3903 | IFNET_KEEPALIVE_OFFLOAD_FLAG_NOWAKEFROMSLEEP; | |
3904 | } | |
39037602 A |
3905 | frame->local_port = ntohs(inp->inp_lport); |
3906 | frame->remote_port = ntohs(inp->inp_fport); | |
3907 | frame->local_seq = tp->snd_nxt; | |
3908 | frame->remote_seq = tp->rcv_nxt; | |
3909 | if (inp->inp_vflag & INP_IPV4) { | |
f427ee49 A |
3910 | ASSERT(frame_data_offset + sizeof(struct ip) + sizeof(struct tcphdr) <= UINT8_MAX); |
3911 | frame->length = (uint8_t)(frame_data_offset + | |
3912 | sizeof(struct ip) + sizeof(struct tcphdr)); | |
39037602 A |
3913 | frame->reply_length = frame->length; |
3914 | ||
3915 | frame->addr_length = sizeof(struct in_addr); | |
3916 | bcopy(&inp->inp_laddr, frame->local_addr, | |
3917 | sizeof(struct in_addr)); | |
3918 | bcopy(&inp->inp_faddr, frame->remote_addr, | |
3919 | sizeof(struct in_addr)); | |
3920 | } else { | |
3921 | struct in6_addr *ip6; | |
3922 | ||
f427ee49 A |
3923 | ASSERT(frame_data_offset + sizeof(struct ip6_hdr) + sizeof(struct tcphdr) <= UINT8_MAX); |
3924 | frame->length = (uint8_t)(frame_data_offset + | |
3925 | sizeof(struct ip6_hdr) + sizeof(struct tcphdr)); | |
39037602 A |
3926 | frame->reply_length = frame->length; |
3927 | ||
3928 | frame->addr_length = sizeof(struct in6_addr); | |
3929 | ip6 = (struct in6_addr *)(void *)frame->local_addr; | |
3930 | bcopy(&inp->in6p_laddr, ip6, sizeof(struct in6_addr)); | |
0a7de745 | 3931 | if (IN6_IS_SCOPE_EMBED(ip6)) { |
39037602 | 3932 | ip6->s6_addr16[1] = 0; |
0a7de745 | 3933 | } |
39037602 A |
3934 | |
3935 | ip6 = (struct in6_addr *)(void *)frame->remote_addr; | |
3936 | bcopy(&inp->in6p_faddr, ip6, sizeof(struct in6_addr)); | |
0a7de745 | 3937 | if (IN6_IS_SCOPE_EMBED(ip6)) { |
39037602 | 3938 | ip6->s6_addr16[1] = 0; |
0a7de745 | 3939 | } |
39037602 A |
3940 | } |
3941 | ||
3942 | /* | |
3943 | * First the probe | |
3944 | */ | |
3945 | m = tcp_make_keepalive_frame(tp, ifp, TRUE); | |
3946 | if (m == NULL) { | |
5ba3f43e | 3947 | socket_unlock(so, 1); |
39037602 A |
3948 | continue; |
3949 | } | |
3950 | bcopy(m->m_data, frame->data + frame_data_offset, | |
3951 | m->m_len); | |
3952 | m_freem(m); | |
3953 | ||
3954 | /* | |
3955 | * Now the response packet to incoming probes | |
3956 | */ | |
3957 | m = tcp_make_keepalive_frame(tp, ifp, FALSE); | |
3958 | if (m == NULL) { | |
5ba3f43e | 3959 | socket_unlock(so, 1); |
39037602 A |
3960 | continue; |
3961 | } | |
3962 | bcopy(m->m_data, frame->reply_data + frame_data_offset, | |
3963 | m->m_len); | |
3964 | m_freem(m); | |
3965 | ||
3966 | frame_index++; | |
5ba3f43e | 3967 | socket_unlock(so, 1); |
39037602 A |
3968 | } |
3969 | lck_rw_done(tcbinfo.ipi_lock); | |
3970 | *used_frames_count = frame_index; | |
3971 | } | |
3972 | ||
cb323159 A |
3973 | static bool |
3974 | inp_matches_kao_frame(ifnet_t ifp, struct ifnet_keepalive_offload_frame *frame, | |
3975 | struct inpcb *inp) | |
3976 | { | |
3977 | if (inp->inp_ppcb == NULL) { | |
3978 | return false; | |
3979 | } | |
3980 | /* Release the want count */ | |
3981 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
3982 | return false; | |
3983 | } | |
3984 | if (inp->inp_last_outifp == NULL || | |
3985 | inp->inp_last_outifp->if_index != ifp->if_index) { | |
3986 | return false; | |
3987 | } | |
3988 | if (frame->local_port != ntohs(inp->inp_lport) || | |
3989 | frame->remote_port != ntohs(inp->inp_fport)) { | |
3990 | return false; | |
3991 | } | |
3992 | if (inp->inp_vflag & INP_IPV4) { | |
3993 | if (memcmp(&inp->inp_laddr, frame->local_addr, | |
3994 | sizeof(struct in_addr)) != 0 || | |
3995 | memcmp(&inp->inp_faddr, frame->remote_addr, | |
3996 | sizeof(struct in_addr)) != 0) { | |
3997 | return false; | |
3998 | } | |
3999 | } else if (inp->inp_vflag & INP_IPV6) { | |
4000 | if (memcmp(&inp->inp_laddr, frame->local_addr, | |
4001 | sizeof(struct in6_addr)) != 0 || | |
4002 | memcmp(&inp->inp_faddr, frame->remote_addr, | |
4003 | sizeof(struct in6_addr)) != 0) { | |
4004 | return false; | |
4005 | } | |
4006 | } else { | |
4007 | return false; | |
4008 | } | |
4009 | return true; | |
4010 | } | |
4011 | ||
4012 | int | |
4013 | tcp_notify_kao_timeout(ifnet_t ifp, | |
4014 | struct ifnet_keepalive_offload_frame *frame) | |
4015 | { | |
4016 | struct inpcb *inp = NULL; | |
4017 | struct socket *so = NULL; | |
4018 | bool found = false; | |
4019 | ||
4020 | /* | |
4021 | * Unlock the list before posting event on the matching socket | |
4022 | */ | |
4023 | lck_rw_lock_shared(tcbinfo.ipi_lock); | |
4024 | ||
4025 | LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) { | |
4026 | if ((so = inp->inp_socket) == NULL || | |
4027 | (so->so_state & SS_DEFUNCT)) { | |
4028 | continue; | |
4029 | } | |
4030 | if (!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD)) { | |
4031 | continue; | |
4032 | } | |
4033 | if (!(inp->inp_vflag & (INP_IPV4 | INP_IPV6))) { | |
4034 | continue; | |
4035 | } | |
4036 | if (inp->inp_ppcb == NULL || | |
4037 | in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) { | |
4038 | continue; | |
4039 | } | |
4040 | socket_lock(so, 1); | |
4041 | if (inp_matches_kao_frame(ifp, frame, inp)) { | |
4042 | /* | |
4043 | * Keep the matching socket locked | |
4044 | */ | |
4045 | found = true; | |
4046 | break; | |
4047 | } | |
4048 | socket_unlock(so, 1); | |
4049 | } | |
4050 | lck_rw_done(tcbinfo.ipi_lock); | |
4051 | ||
4052 | if (found) { | |
4053 | ASSERT(inp != NULL); | |
4054 | ASSERT(so != NULL); | |
4055 | ASSERT(so == inp->inp_socket); | |
4056 | /* | |
4057 | * Drop the TCP connection like tcptimers() does | |
4058 | */ | |
4059 | struct tcpcb *tp = inp->inp_ppcb; | |
4060 | ||
4061 | tcpstat.tcps_keepdrops++; | |
cb323159 A |
4062 | soevent(so, |
4063 | (SO_FILT_HINT_LOCKED | SO_FILT_HINT_TIMEOUT)); | |
4064 | tp = tcp_drop(tp, ETIMEDOUT); | |
4065 | ||
4066 | tcpstat.tcps_ka_offload_drops++; | |
4067 | os_log_info(OS_LOG_DEFAULT, "%s: dropped lport %u fport %u\n", | |
4068 | __func__, frame->local_port, frame->remote_port); | |
4069 | ||
4070 | socket_unlock(so, 1); | |
4071 | } | |
4072 | ||
4073 | return 0; | |
4074 | } | |
4075 | ||
39037602 A |
4076 | errno_t |
4077 | tcp_notify_ack_id_valid(struct tcpcb *tp, struct socket *so, | |
4078 | u_int32_t notify_id) | |
4079 | { | |
4080 | struct tcp_notify_ack_marker *elm; | |
4081 | ||
0a7de745 A |
4082 | if (so->so_snd.sb_cc == 0) { |
4083 | return ENOBUFS; | |
4084 | } | |
39037602 A |
4085 | |
4086 | SLIST_FOREACH(elm, &tp->t_notify_ack, notify_next) { | |
4087 | /* Duplicate id is not allowed */ | |
0a7de745 A |
4088 | if (elm->notify_id == notify_id) { |
4089 | return EINVAL; | |
4090 | } | |
39037602 | 4091 | /* Duplicate position is not allowed */ |
0a7de745 A |
4092 | if (elm->notify_snd_una == tp->snd_una + so->so_snd.sb_cc) { |
4093 | return EINVAL; | |
4094 | } | |
39037602 | 4095 | } |
0a7de745 | 4096 | return 0; |
39037602 A |
4097 | } |
4098 | ||
4099 | errno_t | |
4100 | tcp_add_notify_ack_marker(struct tcpcb *tp, u_int32_t notify_id) | |
4101 | { | |
4102 | struct tcp_notify_ack_marker *nm, *elm = NULL; | |
4103 | struct socket *so = tp->t_inpcb->inp_socket; | |
4104 | ||
0a7de745 | 4105 | MALLOC(nm, struct tcp_notify_ack_marker *, sizeof(*nm), |
39037602 | 4106 | M_TEMP, M_WAIT | M_ZERO); |
0a7de745 A |
4107 | if (nm == NULL) { |
4108 | return ENOMEM; | |
4109 | } | |
39037602 A |
4110 | nm->notify_id = notify_id; |
4111 | nm->notify_snd_una = tp->snd_una + so->so_snd.sb_cc; | |
4112 | ||
4113 | SLIST_FOREACH(elm, &tp->t_notify_ack, notify_next) { | |
0a7de745 | 4114 | if (SEQ_GT(nm->notify_snd_una, elm->notify_snd_una)) { |
39037602 | 4115 | break; |
0a7de745 | 4116 | } |
39037602 A |
4117 | } |
4118 | ||
4119 | if (elm == NULL) { | |
4120 | VERIFY(SLIST_EMPTY(&tp->t_notify_ack)); | |
4121 | SLIST_INSERT_HEAD(&tp->t_notify_ack, nm, notify_next); | |
4122 | } else { | |
4123 | SLIST_INSERT_AFTER(elm, nm, notify_next); | |
4124 | } | |
4125 | tp->t_notify_ack_count++; | |
0a7de745 | 4126 | return 0; |
39037602 A |
4127 | } |
4128 | ||
4129 | void | |
4130 | tcp_notify_ack_free(struct tcpcb *tp) | |
4131 | { | |
4132 | struct tcp_notify_ack_marker *elm, *next; | |
0a7de745 | 4133 | if (SLIST_EMPTY(&tp->t_notify_ack)) { |
39037602 | 4134 | return; |
0a7de745 | 4135 | } |
39037602 A |
4136 | |
4137 | SLIST_FOREACH_SAFE(elm, &tp->t_notify_ack, notify_next, next) { | |
4138 | SLIST_REMOVE(&tp->t_notify_ack, elm, tcp_notify_ack_marker, | |
4139 | notify_next); | |
4140 | FREE(elm, M_TEMP); | |
4141 | } | |
4142 | SLIST_INIT(&tp->t_notify_ack); | |
4143 | tp->t_notify_ack_count = 0; | |
4144 | } | |
4145 | ||
4146 | inline void | |
4147 | tcp_notify_acknowledgement(struct tcpcb *tp, struct socket *so) | |
4148 | { | |
4149 | struct tcp_notify_ack_marker *elm; | |
4150 | ||
4151 | elm = SLIST_FIRST(&tp->t_notify_ack); | |
4152 | if (SEQ_GEQ(tp->snd_una, elm->notify_snd_una)) { | |
4153 | soevent(so, SO_FILT_HINT_LOCKED | SO_FILT_HINT_NOTIFY_ACK); | |
4154 | } | |
4155 | } | |
4156 | ||
4157 | void | |
4158 | tcp_get_notify_ack_count(struct tcpcb *tp, | |
4159 | struct tcp_notify_ack_complete *retid) | |
4160 | { | |
4161 | struct tcp_notify_ack_marker *elm; | |
f427ee49 | 4162 | uint32_t complete = 0; |
39037602 A |
4163 | |
4164 | SLIST_FOREACH(elm, &tp->t_notify_ack, notify_next) { | |
0a7de745 | 4165 | if (SEQ_GEQ(tp->snd_una, elm->notify_snd_una)) { |
f427ee49 | 4166 | ASSERT(complete < UINT32_MAX); |
39037602 | 4167 | complete++; |
0a7de745 | 4168 | } else { |
39037602 | 4169 | break; |
0a7de745 | 4170 | } |
39037602 A |
4171 | } |
4172 | retid->notify_pending = tp->t_notify_ack_count - complete; | |
4173 | retid->notify_complete_count = min(TCP_MAX_NOTIFY_ACK, complete); | |
4174 | } | |
4175 | ||
4176 | void | |
4177 | tcp_get_notify_ack_ids(struct tcpcb *tp, | |
4178 | struct tcp_notify_ack_complete *retid) | |
4179 | { | |
4180 | size_t i = 0; | |
4181 | struct tcp_notify_ack_marker *elm, *next; | |
4182 | ||
4183 | SLIST_FOREACH_SAFE(elm, &tp->t_notify_ack, notify_next, next) { | |
0a7de745 | 4184 | if (i >= retid->notify_complete_count) { |
39037602 | 4185 | break; |
0a7de745 | 4186 | } |
39037602 A |
4187 | if (SEQ_GEQ(tp->snd_una, elm->notify_snd_una)) { |
4188 | retid->notify_complete_id[i++] = elm->notify_id; | |
4189 | SLIST_REMOVE(&tp->t_notify_ack, elm, | |
4190 | tcp_notify_ack_marker, notify_next); | |
4191 | FREE(elm, M_TEMP); | |
4192 | tp->t_notify_ack_count--; | |
4193 | } else { | |
4194 | break; | |
4195 | } | |
4196 | } | |
4197 | } | |
4198 | ||
4199 | bool | |
4200 | tcp_notify_ack_active(struct socket *so) | |
4201 | { | |
4202 | if ((SOCK_DOM(so) == PF_INET || SOCK_DOM(so) == PF_INET6) && | |
4203 | SOCK_TYPE(so) == SOCK_STREAM) { | |
4204 | struct tcpcb *tp = intotcpcb(sotoinpcb(so)); | |
4205 | ||
4206 | if (!SLIST_EMPTY(&tp->t_notify_ack)) { | |
4207 | struct tcp_notify_ack_marker *elm; | |
4208 | elm = SLIST_FIRST(&tp->t_notify_ack); | |
0a7de745 A |
4209 | if (SEQ_GEQ(tp->snd_una, elm->notify_snd_una)) { |
4210 | return true; | |
4211 | } | |
39037602 A |
4212 | } |
4213 | } | |
0a7de745 | 4214 | return false; |
39037602 A |
4215 | } |
4216 | ||
4217 | inline int32_t | |
4218 | inp_get_sndbytes_allunsent(struct socket *so, u_int32_t th_ack) | |
4219 | { | |
4220 | struct inpcb *inp = sotoinpcb(so); | |
4221 | struct tcpcb *tp = intotcpcb(inp); | |
4222 | ||
4223 | if ((so->so_snd.sb_flags & SB_SNDBYTE_CNT) && | |
4224 | so->so_snd.sb_cc > 0) { | |
4225 | int32_t unsent, sent; | |
4226 | sent = tp->snd_max - th_ack; | |
0a7de745 | 4227 | if (tp->t_flags & TF_SENTFIN) { |
39037602 | 4228 | sent--; |
0a7de745 | 4229 | } |
39037602 | 4230 | unsent = so->so_snd.sb_cc - sent; |
0a7de745 | 4231 | return unsent; |
39037602 | 4232 | } |
0a7de745 | 4233 | return 0; |
39037602 | 4234 | } |
5ba3f43e A |
4235 | |
4236 | #define IFP_PER_FLOW_STAT(_ipv4_, _stat_) { \ | |
4237 | if (_ipv4_) { \ | |
0a7de745 | 4238 | ifp->if_ipv4_stat->_stat_++; \ |
5ba3f43e | 4239 | } else { \ |
0a7de745 | 4240 | ifp->if_ipv6_stat->_stat_++; \ |
5ba3f43e A |
4241 | } \ |
4242 | } | |
4243 | ||
4244 | #define FLOW_ECN_ENABLED(_flags_) \ | |
4245 | ((_flags_ & (TE_ECN_ON)) == (TE_ECN_ON)) | |
4246 | ||
0a7de745 A |
4247 | void |
4248 | tcp_update_stats_per_flow(struct ifnet_stats_per_flow *ifs, | |
5ba3f43e A |
4249 | struct ifnet *ifp) |
4250 | { | |
0a7de745 | 4251 | if (ifp == NULL || !IF_FULLY_ATTACHED(ifp)) { |
5ba3f43e | 4252 | return; |
0a7de745 | 4253 | } |
5ba3f43e A |
4254 | |
4255 | ifnet_lock_shared(ifp); | |
4256 | if (ifs->ecn_flags & TE_SETUPSENT) { | |
4257 | if (ifs->ecn_flags & TE_CLIENT_SETUP) { | |
4258 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_client_setup); | |
4259 | if (FLOW_ECN_ENABLED(ifs->ecn_flags)) { | |
4260 | IFP_PER_FLOW_STAT(ifs->ipv4, | |
4261 | ecn_client_success); | |
4262 | } else if (ifs->ecn_flags & TE_LOST_SYN) { | |
4263 | IFP_PER_FLOW_STAT(ifs->ipv4, | |
4264 | ecn_syn_lost); | |
4265 | } else { | |
4266 | IFP_PER_FLOW_STAT(ifs->ipv4, | |
4267 | ecn_peer_nosupport); | |
4268 | } | |
4269 | } else { | |
4270 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_server_setup); | |
4271 | if (FLOW_ECN_ENABLED(ifs->ecn_flags)) { | |
4272 | IFP_PER_FLOW_STAT(ifs->ipv4, | |
4273 | ecn_server_success); | |
4274 | } else if (ifs->ecn_flags & TE_LOST_SYN) { | |
4275 | IFP_PER_FLOW_STAT(ifs->ipv4, | |
4276 | ecn_synack_lost); | |
4277 | } else { | |
4278 | IFP_PER_FLOW_STAT(ifs->ipv4, | |
4279 | ecn_peer_nosupport); | |
4280 | } | |
4281 | } | |
4282 | } else { | |
4283 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_off_conn); | |
4284 | } | |
4285 | if (FLOW_ECN_ENABLED(ifs->ecn_flags)) { | |
4286 | if (ifs->ecn_flags & TE_RECV_ECN_CE) { | |
4287 | tcpstat.tcps_ecn_conn_recv_ce++; | |
4288 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_conn_recv_ce); | |
4289 | } | |
4290 | if (ifs->ecn_flags & TE_RECV_ECN_ECE) { | |
4291 | tcpstat.tcps_ecn_conn_recv_ece++; | |
4292 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_conn_recv_ece); | |
4293 | } | |
4294 | if (ifs->ecn_flags & (TE_RECV_ECN_CE | TE_RECV_ECN_ECE)) { | |
4295 | if (ifs->txretransmitbytes > 0 || | |
4296 | ifs->rxoutoforderbytes > 0) { | |
4297 | tcpstat.tcps_ecn_conn_pl_ce++; | |
4298 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_conn_plce); | |
4299 | } else { | |
4300 | tcpstat.tcps_ecn_conn_nopl_ce++; | |
4301 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_conn_noplce); | |
4302 | } | |
4303 | } else { | |
4304 | if (ifs->txretransmitbytes > 0 || | |
4305 | ifs->rxoutoforderbytes > 0) { | |
4306 | tcpstat.tcps_ecn_conn_plnoce++; | |
4307 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_conn_plnoce); | |
4308 | } | |
4309 | } | |
4310 | } | |
4311 | ||
4312 | /* Other stats are interesting for non-local connections only */ | |
4313 | if (ifs->local) { | |
4314 | ifnet_lock_done(ifp); | |
4315 | return; | |
4316 | } | |
4317 | ||
4318 | if (ifs->ipv4) { | |
4319 | ifp->if_ipv4_stat->timestamp = net_uptime(); | |
4320 | if (FLOW_ECN_ENABLED(ifs->ecn_flags)) { | |
4321 | tcp_flow_ecn_perf_stats(ifs, &ifp->if_ipv4_stat->ecn_on); | |
4322 | } else { | |
4323 | tcp_flow_ecn_perf_stats(ifs, &ifp->if_ipv4_stat->ecn_off); | |
4324 | } | |
4325 | } else { | |
4326 | ifp->if_ipv6_stat->timestamp = net_uptime(); | |
4327 | if (FLOW_ECN_ENABLED(ifs->ecn_flags)) { | |
4328 | tcp_flow_ecn_perf_stats(ifs, &ifp->if_ipv6_stat->ecn_on); | |
4329 | } else { | |
4330 | tcp_flow_ecn_perf_stats(ifs, &ifp->if_ipv6_stat->ecn_off); | |
4331 | } | |
4332 | } | |
4333 | ||
4334 | if (ifs->rxmit_drop) { | |
4335 | if (FLOW_ECN_ENABLED(ifs->ecn_flags)) { | |
4336 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_on.rxmit_drop); | |
4337 | } else { | |
4338 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_off.rxmit_drop); | |
4339 | } | |
4340 | } | |
0a7de745 | 4341 | if (ifs->ecn_fallback_synloss) { |
5ba3f43e | 4342 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_fallback_synloss); |
0a7de745 A |
4343 | } |
4344 | if (ifs->ecn_fallback_droprst) { | |
5ba3f43e | 4345 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_fallback_droprst); |
0a7de745 A |
4346 | } |
4347 | if (ifs->ecn_fallback_droprxmt) { | |
5ba3f43e | 4348 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_fallback_droprxmt); |
0a7de745 A |
4349 | } |
4350 | if (ifs->ecn_fallback_ce) { | |
5ba3f43e | 4351 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_fallback_ce); |
0a7de745 A |
4352 | } |
4353 | if (ifs->ecn_fallback_reorder) { | |
5ba3f43e | 4354 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_fallback_reorder); |
0a7de745 A |
4355 | } |
4356 | if (ifs->ecn_recv_ce > 0) { | |
5ba3f43e | 4357 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_recv_ce); |
0a7de745 A |
4358 | } |
4359 | if (ifs->ecn_recv_ece > 0) { | |
5ba3f43e | 4360 | IFP_PER_FLOW_STAT(ifs->ipv4, ecn_recv_ece); |
0a7de745 | 4361 | } |
5ba3f43e A |
4362 | |
4363 | tcp_flow_lim_stats(ifs, &ifp->if_lim_stat); | |
4364 | ifnet_lock_done(ifp); | |
4365 | } | |
f427ee49 | 4366 |