1/*
2 * Copyright (c) 2000-2023 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1993
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 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
61 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $
62 */
63
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/sysctl.h>
69#include <sys/mbuf.h>
70#include <sys/domain.h>
71#include <sys/priv.h>
72#include <sys/socket.h>
73#include <sys/socketvar.h>
74#include <sys/protosw.h>
75#include <sys/syslog.h>
76
77#include <net/if.h>
78#include <net/route.h>
79#include <net/ntstat.h>
80#include <net/content_filter.h>
81#include <net/multi_layer_pkt_log.h>
82
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip6.h>
86#include <netinet/in_pcb.h>
87#include <netinet6/in6_pcb.h>
88#include <netinet/in_var.h>
89#include <netinet/ip_var.h>
90#include <netinet6/ip6_var.h>
91#include <netinet/tcp.h>
92#include <netinet/tcp_fsm.h>
93#include <netinet/tcp_seq.h>
94#include <netinet/tcp_timer.h>
95#include <netinet/tcp_var.h>
96#include <netinet/tcpip.h>
97#include <netinet/tcp_cc.h>
98#include <netinet/tcp_log.h>
99#include <mach/sdt.h>
100#if TCPDEBUG
101#include <netinet/tcp_debug.h>
102#endif
103#if MPTCP
104#include <netinet/mptcp_var.h>
105#endif /* MPTCP */
106
107#if IPSEC
108#include <netinet6/ipsec.h>
109#endif /*IPSEC*/
110
111#if FLOW_DIVERT
112#include <netinet/flow_divert.h>
113#endif /* FLOW_DIVERT */
114
115#if SKYWALK
116#include <libkern/sysctl.h>
117#include <skywalk/os_stats_private.h>
118#endif /* SKYWALK */
119
120#include <net/sockaddr_utils.h>
121
122extern char *proc_name_address(void *p);
123
124errno_t tcp_fill_info_for_info_tuple(struct info_tuple *, struct tcp_info *);
125
126static int tcp_sysctl_info(struct sysctl_oid *, void *, int, struct sysctl_req *);
127static void tcp_connection_fill_info(struct tcpcb *tp,
128 struct tcp_connection_info *tci);
129static int tcp_get_mpkl_send_info(struct mbuf *, struct so_mpkl_send_info *);
130
131/*
132 * TCP protocol interface to socket abstraction.
133 */
134static int tcp_attach(struct socket *, struct proc *);
135static int tcp_connect(struct tcpcb *, struct sockaddr *, struct proc *);
136static int tcp6_connect(struct tcpcb *, struct sockaddr *, struct proc *);
137static int tcp6_usr_connect(struct socket *, struct sockaddr *,
138 struct proc *);
139static struct tcpcb *tcp_disconnect(struct tcpcb *);
140static struct tcpcb *tcp_usrclosed(struct tcpcb *);
141extern void tcp_sbrcv_trim(struct tcpcb *tp, struct sockbuf *sb);
142
143#if TCPDEBUG
144#define TCPDEBUG0 int ostate = 0
145#define TCPDEBUG1() ostate = tp ? tp->t_state : 0
146#define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
147 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
148#else
149#define TCPDEBUG0
150#define TCPDEBUG1()
151#define TCPDEBUG2(req)
152#endif
153
154SYSCTL_PROC(_net_inet_tcp, OID_AUTO, info,
155 CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_ANYBODY | CTLFLAG_KERN,
156 0, 0, tcp_sysctl_info, "S", "TCP info per tuple");
157
158int faster_mcopy = 1;
159SYSCTL_INT(_net_inet_tcp, OID_AUTO, faster_mcopy,
160 CTLFLAG_RW | CTLFLAG_LOCKED, &faster_mcopy, 1,
161 "Speed up m_copym");
162
163/*
164 * TCP attaches to socket via pru_attach(), reserving space,
165 * and an internet control block.
166 *
167 * Returns: 0 Success
168 * EISCONN
169 * tcp_attach:ENOBUFS
170 * tcp_attach:ENOMEM
171 * tcp_attach:??? [IPSEC specific]
172 */
173static int
174tcp_usr_attach(struct socket *so, __unused int proto, struct proc *p)
175{
176 int error;
177 struct inpcb *inp = sotoinpcb(so);
178 struct tcpcb *tp = 0;
179 TCPDEBUG0;
180
181 TCPDEBUG1();
182 if (inp) {
183 error = EISCONN;
184 goto out;
185 }
186
187 error = tcp_attach(so, p);
188 if (error) {
189 goto out;
190 }
191
192 if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
193 so->so_linger = (short)(TCP_LINGERTIME * hz);
194 }
195 if (faster_mcopy) {
196 so->so_snd.sb_flags |= SB_SENDHEAD;
197 so->so_snd.sb_sendhead = NULL;
198 so->so_snd.sb_sendoff = 0;
199 }
200
201 tp = sototcpcb(so);
202out:
203 TCPDEBUG2(PRU_ATTACH);
204 return error;
205}
206
207/*
208 * pru_detach() detaches the TCP protocol from the socket.
209 * If the protocol state is non-embryonic, then can't
210 * do this directly: have to initiate a pru_disconnect(),
211 * which may finish later; embryonic TCB's can just
212 * be discarded here.
213 */
214static int
215tcp_usr_detach(struct socket *so)
216{
217 int error = 0;
218 struct inpcb *inp = sotoinpcb(so);
219 struct tcpcb *tp;
220 TCPDEBUG0;
221
222 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) {
223 return EINVAL; /* XXX */
224 }
225 socket_lock_assert_owned(so);
226 tp = intotcpcb(inp);
227 /* In case we got disconnected from the peer */
228 if (tp == NULL) {
229 goto out;
230 }
231 TCPDEBUG1();
232
233 calculate_tcp_clock();
234
235 tp = tcp_disconnect(tp);
236out:
237 TCPDEBUG2(PRU_DETACH);
238 return error;
239}
240
241#if NECP
242#define COMMON_START_ALLOW_FLOW_DIVERT(allow) TCPDEBUG0; \
243do { \
244 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
245 return (EINVAL); \
246 if (!(allow) && necp_socket_should_use_flow_divert(inp)) \
247 return (EPROTOTYPE); \
248 tp = intotcpcb(inp); \
249 TCPDEBUG1(); \
250 calculate_tcp_clock(); \
251} while (0)
252#else /* NECP */
253#define COMMON_START_ALLOW_FLOW_DIVERT(allow) TCPDEBUG0; \
254do { \
255 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
256 return (EINVAL); \
257 tp = intotcpcb(inp); \
258 TCPDEBUG1(); \
259 calculate_tcp_clock(); \
260} while (0)
261#endif /* !NECP */
262
263#define COMMON_START() COMMON_START_ALLOW_FLOW_DIVERT(false)
264#define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
265
266
267/*
268 * Give the socket an address.
269 *
270 * Returns: 0 Success
271 * EINVAL Invalid argument [COMMON_START]
272 * EAFNOSUPPORT Address family not supported
273 * in_pcbbind:EADDRNOTAVAIL Address not available.
274 * in_pcbbind:EINVAL Invalid argument
275 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
276 * in_pcbbind:EACCES Permission denied
277 * in_pcbbind:EADDRINUSE Address in use
278 * in_pcbbind:EAGAIN Resource unavailable, try again
279 * in_pcbbind:EPERM Operation not permitted
280 */
281static int
282tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
283{
284 int error = 0;
285 struct inpcb *inp = sotoinpcb(so);
286 struct tcpcb *tp;
287 struct sockaddr_in *sinp;
288
289 COMMON_START_ALLOW_FLOW_DIVERT(true);
290
291 if (nam->sa_family != 0 && nam->sa_family != AF_INET) {
292 error = EAFNOSUPPORT;
293 goto out;
294 }
295 /*
296 * Must check for multicast and broadcast addresses and disallow binding
297 * to them.
298 */
299 sinp = SIN(nam);
300 if (sinp->sin_family == AF_INET &&
301 (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)) ||
302 sinp->sin_addr.s_addr == INADDR_BROADCAST)) {
303 error = EAFNOSUPPORT;
304 goto out;
305 }
306
307 error = in_pcbbind(inp, nam, p);
308 if (error) {
309 goto out;
310 }
311
312#if NECP
313 /* Update NECP client with bind result if not in middle of connect */
314 if ((inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS) &&
315 !uuid_is_null(uu: inp->necp_client_uuid)) {
316 socket_unlock(so, refcount: 0);
317 necp_client_assign_from_socket(pid: so->last_pid, client_id: inp->necp_client_uuid, inp);
318 socket_lock(so, refcount: 0);
319 }
320#endif /* NECP */
321
322 COMMON_END(PRU_BIND);
323}
324
325static int
326tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
327{
328 int error = 0;
329 struct inpcb *inp = sotoinpcb(so);
330 const uint8_t old_flags = inp->inp_vflag;
331 struct tcpcb *tp;
332 struct sockaddr_in6 *sin6p;
333
334 COMMON_START_ALLOW_FLOW_DIVERT(true);
335
336 if (nam->sa_family != 0 && nam->sa_family != AF_INET6) {
337 error = EAFNOSUPPORT;
338 goto out;
339 }
340 /*
341 * Must check for multicast and broadcast addresses and disallow binding
342 * to them.
343 */
344 sin6p = SIN6(nam);
345 if (sin6p->sin6_family == AF_INET6 &&
346 (IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr) ||
347 ((IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr) ||
348 IN6_IS_ADDR_V4COMPAT(&sin6p->sin6_addr)) &&
349 (IN_MULTICAST(ntohl(sin6p->sin6_addr.s6_addr32[3])) ||
350 sin6p->sin6_addr.s6_addr32[3] == INADDR_BROADCAST)))) {
351 error = EAFNOSUPPORT;
352 goto out;
353 }
354
355 /*
356 * Another thread won the binding race so do not change inp_vflag
357 */
358 if (inp->inp_flags2 & INP2_BIND_IN_PROGRESS) {
359 error = EINVAL;
360 goto out;
361 }
362
363 inp->inp_vflag &= ~INP_IPV4;
364 inp->inp_vflag |= INP_IPV6;
365 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
366 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr)) {
367 inp->inp_vflag |= INP_IPV4;
368 } else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
369 struct sockaddr_in sin;
370
371 in6_sin6_2_sin(sin: &sin, sin6: sin6p);
372 inp->inp_vflag |= INP_IPV4;
373 inp->inp_vflag &= ~INP_IPV6;
374
375 error = in_pcbbind(inp, SA(&sin), p);
376 if (error != 0) {
377 inp->inp_vflag = old_flags;
378 route_clear(&inp->inp_route);
379 }
380 goto out;
381 }
382 }
383 error = in6_pcbbind(inp, nam, p);
384 if (error) {
385 inp->inp_vflag = old_flags;
386 route_clear(&inp->inp_route);
387 goto out;
388 }
389
390 COMMON_END(PRU_BIND);
391}
392
393/*
394 * Prepare to accept connections.
395 *
396 * Returns: 0 Success
397 * EINVAL [COMMON_START]
398 * in_pcbbind:EADDRNOTAVAIL Address not available.
399 * in_pcbbind:EINVAL Invalid argument
400 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
401 * in_pcbbind:EACCES Permission denied
402 * in_pcbbind:EADDRINUSE Address in use
403 * in_pcbbind:EAGAIN Resource unavailable, try again
404 * in_pcbbind:EPERM Operation not permitted
405 */
406static int
407tcp_usr_listen(struct socket *so, struct proc *p)
408{
409 int error = 0;
410 struct inpcb *inp = sotoinpcb(so);
411 struct tcpcb *tp;
412
413 COMMON_START_ALLOW_FLOW_DIVERT(true);
414 if (inp->inp_lport == 0) {
415 error = in_pcbbind(inp, NULL, p);
416 }
417 if (error == 0) {
418 TCP_LOG_STATE(tp, TCPS_LISTEN);
419 tp->t_state = TCPS_LISTEN;
420 if (nstat_collect) {
421 nstat_pcb_event(inp, event: NSTAT_EVENT_SRC_FLOW_STATE_LISTEN);
422 }
423 }
424 TCP_LOG_LISTEN(tp, error);
425 COMMON_END(PRU_LISTEN);
426}
427
428static int
429tcp6_usr_listen(struct socket *so, struct proc *p)
430{
431 int error = 0;
432 struct inpcb *inp = sotoinpcb(so);
433 struct tcpcb *tp;
434
435 COMMON_START_ALLOW_FLOW_DIVERT(true);
436 if (inp->inp_lport == 0) {
437 inp->inp_vflag &= ~INP_IPV4;
438 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
439 inp->inp_vflag |= INP_IPV4;
440 }
441 error = in6_pcbbind(inp, NULL, p);
442 }
443 if (error == 0) {
444 TCP_LOG_STATE(tp, TCPS_LISTEN);
445 tp->t_state = TCPS_LISTEN;
446 if (nstat_collect) {
447 nstat_pcb_event(inp, event: NSTAT_EVENT_SRC_FLOW_STATE_LISTEN);
448 }
449 }
450 TCP_LOG_LISTEN(tp, error);
451 COMMON_END(PRU_LISTEN);
452}
453
454static int
455tcp_connect_complete(struct socket *so)
456{
457 struct tcpcb *tp = sototcpcb(so);
458 struct inpcb *inp = sotoinpcb(so);
459 int error = 0;
460
461 /* TFO delays the tcp_output until later, when the app calls write() */
462 if (so->so_flags1 & SOF1_PRECONNECT_DATA) {
463 if (!necp_socket_is_allowed_to_send_recv(sotoinpcb(so), NULL, pf_tag: 0, NULL, NULL, NULL, NULL)) {
464 TCP_LOG_DROP_NECP(NULL, NULL, tp, true);
465 return EHOSTUNREACH;
466 }
467
468 /* Initialize enough state so that we can actually send data */
469 tcp_mss(tp, -1, IFSCOPE_NONE);
470 tp->snd_wnd = tp->t_maxseg;
471 tp->max_sndwnd = tp->snd_wnd;
472 } else {
473 tp->t_flagsext |= TF_USR_OUTPUT;
474 error = tcp_output(tp);
475 tp->t_flagsext &= ~TF_USR_OUTPUT;
476 }
477
478#if NECP
479 /* Update NECP client with connected five-tuple */
480 if (error == 0 && !uuid_is_null(uu: inp->necp_client_uuid)) {
481 socket_unlock(so, refcount: 0);
482 necp_client_assign_from_socket(pid: so->last_pid, client_id: inp->necp_client_uuid, inp);
483 socket_lock(so, refcount: 0);
484 }
485#endif /* NECP */
486
487 return error;
488}
489
490__attribute__((noinline))
491static void
492tcp_log_address_error(int error, struct sockaddr *nam, struct proc *p)
493{
494 char buffer[MAX_IPv6_STR_LEN];
495
496 if (nam->sa_family == AF_INET6) {
497 struct sockaddr_in6 *sin6p = SIN6(nam);
498
499 inet_ntop(AF_INET6, &sin6p->sin6_addr, buffer, sizeof(buffer));
500 } else {
501 struct sockaddr_in *sinp = SIN(nam);
502
503 inet_ntop(AF_INET, &sinp->sin_addr, buffer, sizeof(buffer));
504 }
505 if (p == NULL) {
506 p = current_proc();
507 }
508 os_log(OS_LOG_DEFAULT, "connect address error %d for %s process %s:%u",
509 error, buffer, proc_name_address(p), proc_pid(p));
510}
511
512/*
513 * Note that connecting to the all-zeros address is OK and is treated as the
514 * loopback address
515 */
516static int
517tcp_usr_connect_common(struct socket *so, struct tcpcb *tp, struct sockaddr *nam,
518 struct proc *p, bool isipv6, bool need_connect_complete)
519{
520 int error = 0;
521 struct inpcb *inp = sotoinpcb(so);
522
523 if (isipv6 == 0) {
524 struct sockaddr_in *sinp;
525
526 if (nam->sa_family != 0 && nam->sa_family != AF_INET) {
527 error = EAFNOSUPPORT;
528 goto out;
529 }
530 /*
531 * Disallow connecting to multicast and broadcast addresses.
532 */
533 sinp = SIN(nam);
534 if (sinp->sin_family == AF_INET &&
535 (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)) ||
536 sinp->sin_addr.s_addr == INADDR_BROADCAST)) {
537 error = EAFNOSUPPORT;
538 goto out;
539 }
540
541 if ((error = tcp_connect(tp, nam, p)) != 0) {
542 goto out;
543 }
544 } else {
545 struct sockaddr_in6 *sin6p;
546
547 if (nam->sa_family != 0 && nam->sa_family != AF_INET6) {
548 error = EAFNOSUPPORT;
549 goto out;
550 }
551
552 /*
553 * Disallow connecting to multicast and broadcast addresses.
554 */
555 sin6p = SIN6(nam);
556 if (sin6p->sin6_family == AF_INET6 &&
557 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
558 error = EAFNOSUPPORT;
559 goto out;
560 }
561
562 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
563 struct sockaddr_in sin;
564
565 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
566 error = EINVAL;
567 goto out;
568 }
569
570 in6_sin6_2_sin(sin: &sin, sin6: sin6p);
571 /*
572 * Disallow connecting to multicast and broadcast addresses.
573 */
574 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr)) ||
575 sin.sin_addr.s_addr == INADDR_BROADCAST) {
576 error = EAFNOSUPPORT;
577 goto out;
578 }
579 inp->inp_vflag |= INP_IPV4;
580 inp->inp_vflag &= ~INP_IPV6;
581 if ((error = tcp_connect(tp, SA(&sin), p)) != 0) {
582 goto out;
583 }
584
585 goto out;
586 } else if (IN6_IS_ADDR_V4COMPAT(&sin6p->sin6_addr)) {
587 /*
588 * Disallow connecting to multicast and broadcast addresses.
589 */
590 if (IN_MULTICAST(ntohl(sin6p->sin6_addr.s6_addr32[3])) ||
591 sin6p->sin6_addr.s6_addr32[3] == INADDR_BROADCAST) {
592 error = EAFNOSUPPORT;
593 goto out;
594 }
595 }
596
597 inp->inp_vflag &= ~INP_IPV4;
598 inp->inp_vflag |= INP_IPV6;
599 if ((error = tcp6_connect(tp, nam, p)) != 0) {
600 goto out;
601 }
602 }
603out:
604 if (need_connect_complete && error == 0) {
605 error = tcp_connect_complete(so);
606 }
607 TCP_LOG_CONNECT(tp, true, error);
608 if (error == EAFNOSUPPORT) {
609 tcp_log_address_error(error, nam, p);
610 }
611 return error;
612}
613
614/*
615 * Initiate connection to peer.
616 * Create a template for use in transmissions on this connection.
617 * Enter SYN_SENT state, and mark socket as connecting.
618 * Start keep-alive timer, and seed output sequence space.
619 * Send initial segment on connection.
620 */
621static int
622tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
623{
624 int error = 0;
625 struct inpcb *inp = sotoinpcb(so);
626 struct tcpcb *tp;
627
628 TCPDEBUG0;
629 if (inp == NULL) {
630 return EINVAL;
631 } else if (inp->inp_state == INPCB_STATE_DEAD) {
632 if (so->so_error) {
633 error = so->so_error;
634 so->so_error = 0;
635 return error;
636 } else {
637 return EINVAL;
638 }
639 }
640#if NECP
641#if CONTENT_FILTER
642 error = cfil_sock_attach(so, NULL, remote: nam, CFS_CONNECTION_DIR_OUT);
643 if (error != 0) {
644 return error;
645 }
646#endif /* CONTENT_FILTER */
647#if FLOW_DIVERT
648 if (necp_socket_should_use_flow_divert(inp)) {
649 error = flow_divert_pcb_init(so);
650 if (error == 0) {
651 error = flow_divert_connect_out(so, to: nam, p);
652 }
653 return error;
654 } else {
655 so->so_flags1 |= SOF1_FLOW_DIVERT_SKIP;
656 }
657#endif /* FLOW_DIVERT */
658#endif /* NECP */
659 tp = intotcpcb(inp);
660 TCPDEBUG1();
661
662 calculate_tcp_clock();
663
664 error = tcp_usr_connect_common(so, tp, nam, p, false, true);
665 if (error != 0) {
666 goto out;
667 }
668
669 COMMON_END(PRU_CONNECT);
670}
671
672static int
673tcp_usr_connectx_common(struct socket *so, int af,
674 struct sockaddr *src, struct sockaddr *dst,
675 struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid,
676 uint32_t flags, void *arg, uint32_t arglen, struct uio *auio,
677 user_ssize_t *bytes_written)
678{
679#pragma unused(aid, flags, arg, arglen)
680 struct inpcb *inp = sotoinpcb(so);
681 int error = 0;
682 user_ssize_t datalen = 0;
683
684 if (inp == NULL) {
685 return EINVAL;
686 }
687
688 VERIFY(dst != NULL);
689
690 ASSERT(!(inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS));
691 inp->inp_flags2 |= INP2_CONNECT_IN_PROGRESS;
692
693#if NECP
694 inp_update_necp_policy(inp, src, dst, ifscope);
695#endif /* NECP */
696
697 if ((so->so_flags1 & SOF1_DATA_IDEMPOTENT) &&
698 (tcp_fastopen & TCP_FASTOPEN_CLIENT)) {
699 sototcpcb(so)->t_flagsext |= TF_FASTOPEN;
700 }
701
702 /* bind socket to the specified interface, if requested */
703 if (ifscope != IFSCOPE_NONE &&
704 (error = inp_bindif(inp, ifscope, NULL)) != 0) {
705 goto done;
706 }
707
708 /* if source address and/or port is specified, bind to it */
709 if (src != NULL) {
710 error = sobindlock(so, nam: src, dolock: 0); /* already locked */
711 if (error != 0) {
712 goto done;
713 }
714 }
715
716 switch (af) {
717 case AF_INET:
718 error = tcp_usr_connect(so, nam: dst, p);
719 break;
720 case AF_INET6:
721 error = tcp6_usr_connect(so, dst, p);
722 break;
723 default:
724 VERIFY(0);
725 /* NOTREACHED */
726 }
727
728 if (error != 0) {
729 goto done;
730 }
731
732 /* if there is data, copy it */
733 if (auio != NULL) {
734 socket_unlock(so, refcount: 0);
735
736 VERIFY(bytes_written != NULL);
737
738 datalen = uio_resid(a_uio: auio);
739 error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL,
740 (uio_t)auio, NULL, NULL, 0);
741 socket_lock(so, refcount: 0);
742
743 if (error == 0 || error == EWOULDBLOCK) {
744 *bytes_written = datalen - uio_resid(a_uio: auio);
745 }
746
747 /*
748 * sosend returns EWOULDBLOCK if it's a non-blocking
749 * socket or a timeout occured (this allows to return
750 * the amount of queued data through sendit()).
751 *
752 * However, connectx() returns EINPROGRESS in case of a
753 * blocking socket. So we change the return value here.
754 */
755 if (error == EWOULDBLOCK) {
756 error = EINPROGRESS;
757 }
758 }
759
760 if (error == 0 && pcid != NULL) {
761 *pcid = 1; /* there is only one connection in regular TCP */
762 }
763done:
764 if (error && error != EINPROGRESS) {
765 so->so_flags1 &= ~SOF1_PRECONNECT_DATA;
766 }
767
768 inp->inp_flags2 &= ~INP2_CONNECT_IN_PROGRESS;
769 return error;
770}
771
772static int
773tcp_usr_connectx(struct socket *so, struct sockaddr *src,
774 struct sockaddr *dst, struct proc *p, uint32_t ifscope,
775 sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
776 uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
777{
778 return tcp_usr_connectx_common(so, AF_INET, src, dst, p, ifscope, aid,
779 pcid, flags, arg, arglen, auio: uio, bytes_written);
780}
781
782static int
783tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
784{
785 int error = 0;
786 struct inpcb *inp = sotoinpcb(so);
787 struct tcpcb *tp;
788
789 TCPDEBUG0;
790 if (inp == NULL) {
791 return EINVAL;
792 } else if (inp->inp_state == INPCB_STATE_DEAD) {
793 if (so->so_error) {
794 error = so->so_error;
795 so->so_error = 0;
796 return error;
797 } else {
798 return EINVAL;
799 }
800 }
801#if NECP
802#if CONTENT_FILTER
803 error = cfil_sock_attach(so, NULL, remote: nam, CFS_CONNECTION_DIR_OUT);
804 if (error != 0) {
805 return error;
806 }
807#endif /* CONTENT_FILTER */
808#if FLOW_DIVERT
809 if (necp_socket_should_use_flow_divert(inp)) {
810 error = flow_divert_pcb_init(so);
811 if (error == 0) {
812 error = flow_divert_connect_out(so, to: nam, p);
813 }
814 return error;
815 } else {
816 so->so_flags1 |= SOF1_FLOW_DIVERT_SKIP;
817 }
818#endif /* FLOW_DIVERT */
819#endif /* NECP */
820
821 tp = intotcpcb(inp);
822 TCPDEBUG1();
823
824 calculate_tcp_clock();
825
826 error = tcp_usr_connect_common(so, tp, nam, p, true, true);
827 if (error != 0) {
828 route_clear(&inp->inp_route);
829 goto out;
830 }
831
832 COMMON_END(PRU_CONNECT);
833}
834
835static int
836tcp6_usr_connectx(struct socket *so, struct sockaddr*src,
837 struct sockaddr *dst, struct proc *p, uint32_t ifscope,
838 sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg,
839 uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written)
840{
841 return tcp_usr_connectx_common(so, AF_INET6, src, dst, p, ifscope, aid,
842 pcid, flags, arg, arglen, auio: uio, bytes_written);
843}
844
845/*
846 * Initiate disconnect from peer.
847 * If connection never passed embryonic stage, just drop;
848 * else if don't need to let data drain, then can just drop anyways,
849 * else have to begin TCP shutdown process: mark socket disconnecting,
850 * drain unread data, state switch to reflect user close, and
851 * send segment (e.g. FIN) to peer. Socket will be really disconnected
852 * when peer sends FIN and acks ours.
853 *
854 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
855 */
856static int
857tcp_usr_disconnect(struct socket *so)
858{
859 int error = 0;
860 struct inpcb *inp = sotoinpcb(so);
861 struct tcpcb *tp;
862
863 socket_lock_assert_owned(so);
864 COMMON_START();
865 /* In case we got disconnected from the peer */
866 if (tp == NULL) {
867 goto out;
868 }
869 tp = tcp_disconnect(tp);
870 COMMON_END(PRU_DISCONNECT);
871}
872
873/*
874 * User-protocol pru_disconnectx callback.
875 */
876static int
877tcp_usr_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid)
878{
879#pragma unused(cid)
880 if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) {
881 return EINVAL;
882 }
883
884 return tcp_usr_disconnect(so);
885}
886
887/*
888 * Accept a connection. Essentially all the work is
889 * done at higher levels; just return the address
890 * of the peer, storing through addr.
891 */
892static int
893tcp_usr_accept(struct socket *so, struct sockaddr **nam)
894{
895 int error = 0;
896 struct inpcb *inp = sotoinpcb(so);
897 struct tcpcb *tp = NULL;
898 TCPDEBUG0;
899
900 in_getpeeraddr(so, nam);
901
902 if (so->so_state & SS_ISDISCONNECTED) {
903 error = ECONNABORTED;
904 goto out;
905 }
906 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
907 return EINVAL;
908 }
909#if NECP
910 else if (necp_socket_should_use_flow_divert(inp)) {
911 return EPROTOTYPE;
912 }
913
914#endif /* NECP */
915
916 tp = intotcpcb(inp);
917 TCPDEBUG1();
918
919 TCP_LOG_ACCEPT(tp, 0);
920
921 calculate_tcp_clock();
922
923 COMMON_END(PRU_ACCEPT);
924}
925
926static int
927tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
928{
929 int error = 0;
930 struct inpcb *inp = sotoinpcb(so);
931 struct tcpcb *tp = NULL;
932 TCPDEBUG0;
933
934 if (so->so_state & SS_ISDISCONNECTED) {
935 error = ECONNABORTED;
936 goto out;
937 }
938 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
939 return EINVAL;
940 }
941#if NECP
942 else if (necp_socket_should_use_flow_divert(inp)) {
943 return EPROTOTYPE;
944 }
945
946#endif /* NECP */
947
948 tp = intotcpcb(inp);
949 TCPDEBUG1();
950
951 TCP_LOG_ACCEPT(tp, 0);
952
953 calculate_tcp_clock();
954
955 in6_mapped_peeraddr(so, nam);
956 COMMON_END(PRU_ACCEPT);
957}
958
959/*
960 * Mark the connection as being incapable of further output.
961 *
962 * Returns: 0 Success
963 * EINVAL [COMMON_START]
964 * tcp_output:EADDRNOTAVAIL
965 * tcp_output:ENOBUFS
966 * tcp_output:EMSGSIZE
967 * tcp_output:EHOSTUNREACH
968 * tcp_output:ENETUNREACH
969 * tcp_output:ENETDOWN
970 * tcp_output:ENOMEM
971 * tcp_output:EACCES
972 * tcp_output:EMSGSIZE
973 * tcp_output:ENOBUFS
974 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
975 */
976static int
977tcp_usr_shutdown(struct socket *so)
978{
979 int error = 0;
980 struct inpcb *inp = sotoinpcb(so);
981 struct tcpcb *tp;
982
983 TCPDEBUG0;
984 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
985 return EINVAL;
986 }
987
988 socantsendmore(so);
989
990 /*
991 * In case we got disconnected from the peer, or if this is
992 * a socket that is to be flow-diverted (but not yet).
993 */
994 tp = intotcpcb(inp);
995 TCPDEBUG1();
996
997 if (tp == NULL
998#if NECP
999 || (necp_socket_should_use_flow_divert(inp))
1000#endif /* NECP */
1001 ) {
1002 if (tp != NULL) {
1003 error = EPROTOTYPE;
1004 }
1005 goto out;
1006 }
1007
1008 calculate_tcp_clock();
1009
1010 tp = tcp_usrclosed(tp);
1011#if MPTCP
1012 /* A reset has been sent but socket exists, do not send FIN */
1013 if ((so->so_flags & SOF_MP_SUBFLOW) &&
1014 (tp) && (tp->t_mpflags & TMPF_RESET)) {
1015 goto out;
1016 }
1017#endif
1018#if CONTENT_FILTER
1019 /* Don't send a FIN yet */
1020 if (tp && !(so->so_state & SS_ISDISCONNECTED) &&
1021 cfil_sock_data_pending(sb: &so->so_snd)) {
1022 goto out;
1023 }
1024#endif /* CONTENT_FILTER */
1025 if (tp) {
1026 error = tcp_output(tp);
1027 }
1028 COMMON_END(PRU_SHUTDOWN);
1029}
1030
1031/*
1032 * After a receive, possibly send window update to peer.
1033 */
1034static int
1035tcp_usr_rcvd(struct socket *so, int flags)
1036{
1037 int error = 0;
1038 struct inpcb *inp = sotoinpcb(so);
1039 struct tcpcb *tp;
1040
1041 COMMON_START();
1042 /* In case we got disconnected from the peer */
1043 if (tp == NULL) {
1044 goto out;
1045 }
1046 tcp_sbrcv_trim(tp, sb: &so->so_rcv);
1047
1048 if ((flags & MSG_WAITALL) && SEQ_LT(tp->last_ack_sent, tp->rcv_nxt)) {
1049 tp->t_flags |= TF_ACKNOW;
1050 }
1051
1052 /*
1053 * This tcp_output is solely there to trigger window-updates.
1054 * However, we really do not want these window-updates while we
1055 * are still in SYN_SENT or SYN_RECEIVED.
1056 */
1057 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
1058 tcp_output(tp);
1059 }
1060
1061#if CONTENT_FILTER
1062 cfil_sock_buf_update(sb: &so->so_rcv);
1063#endif /* CONTENT_FILTER */
1064
1065 COMMON_END(PRU_RCVD);
1066}
1067
1068__attribute__((noinline))
1069static int
1070tcp_send_implied_connect(struct socket *so, struct tcpcb *tp, struct sockaddr *nam,
1071 struct proc *p, bool isipv6)
1072{
1073 int error = 0;
1074
1075 error = tcp_usr_connect_common(so, tp, nam, p, isipv6, false);
1076 if (error != 0) {
1077 goto out;
1078 }
1079 /*
1080 * initialize window to default value, and
1081 * initialize maxseg/maxopd using peer's cached
1082 * MSS.
1083 */
1084 tp->snd_wnd = TTCP_CLIENT_SND_WND;
1085 tp->max_sndwnd = tp->snd_wnd;
1086 tcp_mss(tp, -1, IFSCOPE_NONE);
1087out:
1088 return error;
1089}
1090
1091__attribute__((noinline))
1092static void
1093mpkl_tcp_send(struct socket *so, struct tcpcb *tp, uint32_t mpkl_seq, uint32_t mpkl_len,
1094 struct so_mpkl_send_info *mpkl_send_info)
1095{
1096 struct inpcb *inp = tp->t_inpcb;
1097
1098 if (inp == NULL) {
1099 return;
1100 }
1101
1102 if ((inp->inp_last_outifp != NULL &&
1103 (inp->inp_last_outifp->if_xflags & IFXF_MPK_LOG)) ||
1104 (inp->inp_boundifp != NULL &&
1105 (inp->inp_boundifp->if_xflags & IFXF_MPK_LOG))) {
1106 MPKL_TCP_SEND(tcp_mpkl_log_object,
1107 mpkl_send_info->mpkl_proto,
1108 mpkl_send_info->mpkl_uuid,
1109 ntohs(inp->inp_lport),
1110 ntohs(inp->inp_fport),
1111 mpkl_seq,
1112 mpkl_len,
1113 so->last_pid,
1114 so->so_log_seqn++);
1115 }
1116}
1117
1118/*
1119 * Do a send by putting data in output queue and updating urgent
1120 * marker if URG set. Possibly send more data. Unlike the other
1121 * pru_*() routines, the mbuf chains are our responsibility. We
1122 * must either enqueue them or free them. The other pru_* routines
1123 * generally are caller-frees.
1124 *
1125 * Returns: 0 Success
1126 * ECONNRESET
1127 * EINVAL
1128 * ENOBUFS
1129 * tcp_connect:EADDRINUSE Address in use
1130 * tcp_connect:EADDRNOTAVAIL Address not available.
1131 * tcp_connect:EINVAL Invalid argument
1132 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
1133 * tcp_connect:EACCES Permission denied
1134 * tcp_connect:EAGAIN Resource unavailable, try again
1135 * tcp_connect:EPERM Operation not permitted
1136 * tcp_output:EADDRNOTAVAIL
1137 * tcp_output:ENOBUFS
1138 * tcp_output:EMSGSIZE
1139 * tcp_output:EHOSTUNREACH
1140 * tcp_output:ENETUNREACH
1141 * tcp_output:ENETDOWN
1142 * tcp_output:ENOMEM
1143 * tcp_output:EACCES
1144 * tcp_output:EMSGSIZE
1145 * tcp_output:ENOBUFS
1146 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
1147 * tcp6_connect:??? [IPV6 only]
1148 */
1149static int
1150tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
1151 struct sockaddr *nam, struct mbuf *control, struct proc *p)
1152{
1153 int error = 0;
1154 struct inpcb *inp = sotoinpcb(so);
1155 struct tcpcb *tp;
1156 uint32_t mpkl_len = 0; /* length of mbuf chain */
1157 uint32_t mpkl_seq = 0; /* sequence number where new data is added */
1158 struct so_mpkl_send_info mpkl_send_info = {};
1159 bool isipv6;
1160
1161 TCPDEBUG0;
1162
1163 bool cant_connect = (inp->inp_flowhash == 0) && (nam == NULL);
1164
1165 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD || cant_connect
1166#if NECP
1167 || (necp_socket_should_use_flow_divert(inp))
1168#endif /* NECP */
1169 ) {
1170 /*
1171 * OOPS! we lost a race, the TCP session got reset after
1172 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
1173 * network interrupt in the non-splnet() section of sosend().
1174 */
1175 if (m != NULL) {
1176 m_freem(m);
1177 }
1178 if (control != NULL) {
1179 m_freem(control);
1180 control = NULL;
1181 }
1182
1183 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
1184 error = ECONNRESET; /* XXX EPIPE? */
1185 } else if (cant_connect) {
1186 error = EAFNOSUPPORT;
1187 } else {
1188 error = EPROTOTYPE;
1189 }
1190 tp = NULL;
1191 TCPDEBUG1();
1192 goto out;
1193 }
1194 isipv6 = nam && nam->sa_family == AF_INET6 ? true : false;
1195 tp = intotcpcb(inp);
1196 TCPDEBUG1();
1197
1198 calculate_tcp_clock();
1199
1200 if (net_mpklog_enabled) {
1201 mpkl_seq = tp->snd_una + so->so_snd.sb_cc;
1202 if (m) {
1203 mpkl_len = m_length(m);
1204 }
1205 if (so->so_flags1 & SOF1_MPKL_SEND_INFO) {
1206 uuid_copy(dst: mpkl_send_info.mpkl_uuid, src: so->so_mpkl_send_uuid);
1207 mpkl_send_info.mpkl_proto = so->so_mpkl_send_proto;
1208 }
1209 }
1210
1211 if (control != NULL) {
1212 if (control->m_len > 0 && net_mpklog_enabled) {
1213 error = tcp_get_mpkl_send_info(control, &mpkl_send_info);
1214 /*
1215 * Intepretation of the returned code:
1216 * 0: client wants us to use value passed in SCM_MPKL_SEND_INFO
1217 * 1: SCM_MPKL_SEND_INFO was not present
1218 * other: failure
1219 */
1220 if (error != 0 && error != ENOMSG) {
1221 m_freem(control);
1222 if (m != NULL) {
1223 m_freem(m);
1224 }
1225 control = NULL;
1226 m = NULL;
1227 goto out;
1228 }
1229 }
1230 /*
1231 * Silently drop unsupported ancillary data messages
1232 */
1233 m_freem(control);
1234 control = NULL;
1235 }
1236
1237 /* MPTCP sublow socket buffers must not be compressed */
1238 VERIFY(!(so->so_flags & SOF_MP_SUBFLOW) ||
1239 (so->so_snd.sb_flags & SB_NOCOMPRESS));
1240
1241 if (!(flags & PRUS_OOB) || (so->so_flags1 & SOF1_PRECONNECT_DATA)) {
1242 sbappendstream(sb: &so->so_snd, m);
1243
1244 if (nam && tp->t_state < TCPS_SYN_SENT) {
1245 /*
1246 * Do implied connect if not yet connected,
1247 */
1248 error = tcp_send_implied_connect(so, tp, nam, p, isipv6);
1249 if (error != 0) {
1250 goto out;
1251 }
1252 /* The sequence number of the data is past the SYN */
1253 mpkl_seq = tp->iss + 1;
1254 }
1255
1256 if (flags & PRUS_EOF) {
1257 /*
1258 * Close the send side of the connection after
1259 * the data is sent.
1260 */
1261 socantsendmore(so);
1262 tp = tcp_usrclosed(tp);
1263 }
1264 if (tp != NULL) {
1265 if (flags & PRUS_MORETOCOME) {
1266 tp->t_flags |= TF_MORETOCOME;
1267 }
1268 tp->t_flagsext |= TF_USR_OUTPUT;
1269 error = tcp_output(tp);
1270 tp->t_flagsext &= ~TF_USR_OUTPUT;
1271 if (flags & PRUS_MORETOCOME) {
1272 tp->t_flags &= ~TF_MORETOCOME;
1273 }
1274 }
1275 } else {
1276 if (sbspace(sb: &so->so_snd) == 0) {
1277 /* if no space is left in sockbuf,
1278 * do not try to squeeze in OOB traffic */
1279 m_freem(m);
1280 error = ENOBUFS;
1281 goto out;
1282 }
1283 /*
1284 * According to RFC961 (Assigned Protocols),
1285 * the urgent pointer points to the last octet
1286 * of urgent data. We continue, however,
1287 * to consider it to indicate the first octet
1288 * of data past the urgent section.
1289 * Otherwise, snd_up should be one lower.
1290 */
1291 sbappendstream(sb: &so->so_snd, m);
1292 if (nam && tp->t_state < TCPS_SYN_SENT) {
1293 /*
1294 * Do implied connect if not yet connected,
1295 * initialize window to default value, and
1296 * initialize maxseg/maxopd using peer's cached
1297 * MSS.
1298 */
1299 error = tcp_send_implied_connect(so, tp, nam, p, isipv6);
1300 if (error != 0) {
1301 goto out;
1302 }
1303 }
1304 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
1305 tp->t_flagsext |= TF_FORCE;
1306 tp->t_flagsext |= TF_USR_OUTPUT;
1307 error = tcp_output(tp);
1308 tp->t_flagsext &= ~TF_USR_OUTPUT;
1309 tp->t_flagsext &= ~TF_FORCE;
1310 }
1311
1312 if (net_mpklog_enabled) {
1313 mpkl_tcp_send(so, tp, mpkl_seq, mpkl_len, mpkl_send_info: &mpkl_send_info);
1314 }
1315
1316 /*
1317 * We wait for the socket to successfully connect before returning.
1318 * This allows us to signal a timeout to the application.
1319 */
1320 if (so->so_state & SS_ISCONNECTING) {
1321 if (so->so_state & SS_NBIO) {
1322 error = EWOULDBLOCK;
1323 } else {
1324 error = sbwait(sb: &so->so_snd);
1325 }
1326 }
1327
1328 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
1329 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1330}
1331
1332/*
1333 * Abort the TCP.
1334 */
1335static int
1336tcp_usr_abort(struct socket *so)
1337{
1338 int error = 0;
1339 struct inpcb *inp = sotoinpcb(so);
1340 struct tcpcb *tp;
1341
1342 COMMON_START();
1343 /* In case we got disconnected from the peer */
1344 if (tp == NULL) {
1345 goto out;
1346 }
1347 tp = tcp_drop(tp, ECONNABORTED);
1348 VERIFY(so->so_usecount > 0);
1349 so->so_usecount--;
1350 COMMON_END(PRU_ABORT);
1351}
1352
1353/*
1354 * Receive out-of-band data.
1355 *
1356 * Returns: 0 Success
1357 * EINVAL [COMMON_START]
1358 * EINVAL
1359 * EWOULDBLOCK
1360 */
1361static int
1362tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1363{
1364 int error = 0;
1365 struct inpcb *inp = sotoinpcb(so);
1366 struct tcpcb *tp;
1367
1368 COMMON_START();
1369 if ((so->so_oobmark == 0 &&
1370 (so->so_state & SS_RCVATMARK) == 0) ||
1371 so->so_options & SO_OOBINLINE ||
1372 tp->t_oobflags & TCPOOB_HADDATA) {
1373 error = EINVAL;
1374 goto out;
1375 }
1376 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1377 error = EWOULDBLOCK;
1378 goto out;
1379 }
1380 m->m_len = 1;
1381 *mtod(m, caddr_t) = tp->t_iobc;
1382 so->so_state &= ~SS_RCVATMARK;
1383 if ((flags & MSG_PEEK) == 0) {
1384 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1385 }
1386 COMMON_END(PRU_RCVOOB);
1387}
1388
1389static int
1390tcp_usr_preconnect(struct socket *so)
1391{
1392 struct inpcb *inp = sotoinpcb(so);
1393 int error = 0;
1394
1395#if NECP
1396 if (necp_socket_should_use_flow_divert(inp)) {
1397 /* May happen, if in tcp_usr_connect we did not had a chance
1398 * to set the usrreqs (due to some error). So, let's get out
1399 * of here.
1400 */
1401 goto out;
1402 }
1403#endif /* NECP */
1404
1405 error = tcp_output(sototcpcb(so));
1406
1407 soclearfastopen(so);
1408
1409 COMMON_END(PRU_PRECONNECT);
1410}
1411
1412/* xxx - should be const */
1413struct pr_usrreqs tcp_usrreqs = {
1414 .pru_abort = tcp_usr_abort,
1415 .pru_accept = tcp_usr_accept,
1416 .pru_attach = tcp_usr_attach,
1417 .pru_bind = tcp_usr_bind,
1418 .pru_connect = tcp_usr_connect,
1419 .pru_connectx = tcp_usr_connectx,
1420 .pru_control = in_control,
1421 .pru_detach = tcp_usr_detach,
1422 .pru_disconnect = tcp_usr_disconnect,
1423 .pru_disconnectx = tcp_usr_disconnectx,
1424 .pru_listen = tcp_usr_listen,
1425 .pru_peeraddr = in_getpeeraddr,
1426 .pru_rcvd = tcp_usr_rcvd,
1427 .pru_rcvoob = tcp_usr_rcvoob,
1428 .pru_send = tcp_usr_send,
1429 .pru_shutdown = tcp_usr_shutdown,
1430 .pru_sockaddr = in_getsockaddr,
1431 .pru_sosend = sosend,
1432 .pru_soreceive = soreceive,
1433 .pru_preconnect = tcp_usr_preconnect,
1434};
1435
1436struct pr_usrreqs tcp6_usrreqs = {
1437 .pru_abort = tcp_usr_abort,
1438 .pru_accept = tcp6_usr_accept,
1439 .pru_attach = tcp_usr_attach,
1440 .pru_bind = tcp6_usr_bind,
1441 .pru_connect = tcp6_usr_connect,
1442 .pru_connectx = tcp6_usr_connectx,
1443 .pru_control = in6_control,
1444 .pru_detach = tcp_usr_detach,
1445 .pru_disconnect = tcp_usr_disconnect,
1446 .pru_disconnectx = tcp_usr_disconnectx,
1447 .pru_listen = tcp6_usr_listen,
1448 .pru_peeraddr = in6_mapped_peeraddr,
1449 .pru_rcvd = tcp_usr_rcvd,
1450 .pru_rcvoob = tcp_usr_rcvoob,
1451 .pru_send = tcp_usr_send,
1452 .pru_shutdown = tcp_usr_shutdown,
1453 .pru_sockaddr = in6_mapped_sockaddr,
1454 .pru_sosend = sosend,
1455 .pru_soreceive = soreceive,
1456 .pru_preconnect = tcp_usr_preconnect,
1457};
1458
1459/*
1460 * Common subroutine to open a TCP connection to remote host specified
1461 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1462 * port number if needed. Call in_pcbladdr to do the routing and to choose
1463 * a local host address (interface). If there is an existing incarnation
1464 * of the same connection in TIME-WAIT state and if the remote host was
1465 * sending CC options and if the connection duration was < MSL, then
1466 * truncate the previous TIME-WAIT state and proceed.
1467 * Initialize connection parameters and enter SYN-SENT state.
1468 *
1469 * Returns: 0 Success
1470 * EADDRINUSE
1471 * EINVAL
1472 * in_pcbbind:EADDRNOTAVAIL Address not available.
1473 * in_pcbbind:EINVAL Invalid argument
1474 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1475 * in_pcbbind:EACCES Permission denied
1476 * in_pcbbind:EADDRINUSE Address in use
1477 * in_pcbbind:EAGAIN Resource unavailable, try again
1478 * in_pcbbind:EPERM Operation not permitted
1479 * in_pcbladdr:EINVAL Invalid argument
1480 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1481 * in_pcbladdr:EADDRNOTAVAIL Address not available
1482 */
1483static int
1484tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p)
1485{
1486 struct inpcb *inp = tp->t_inpcb, *oinp;
1487 struct socket *so = inp->inp_socket;
1488 struct tcpcb *otp;
1489 struct sockaddr_in *sin = SIN(nam);
1490 struct in_addr laddr;
1491 int error = 0;
1492 struct ifnet *outif = NULL;
1493
1494 if (inp->inp_lport == 0) {
1495 error = in_pcbbind(inp, NULL, p);
1496 if (error) {
1497 goto done;
1498 }
1499 }
1500
1501 /*
1502 * Cannot simply call in_pcbconnect, because there might be an
1503 * earlier incarnation of this same connection still in
1504 * TIME_WAIT state, creating an ADDRINUSE error.
1505 */
1506 error = in_pcbladdr(inp, nam, &laddr, IFSCOPE_NONE, &outif, 0);
1507 if (error) {
1508 goto done;
1509 }
1510
1511 socket_unlock(so: inp->inp_socket, refcount: 0);
1512 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
1513 sin->sin_addr, sin->sin_port,
1514 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr : laddr,
1515 inp->inp_lport, 0, NULL);
1516
1517 socket_lock(so: inp->inp_socket, refcount: 0);
1518 if (oinp) {
1519 if (oinp != inp) { /* 4143933: avoid deadlock if inp == oinp */
1520 socket_lock(so: oinp->inp_socket, refcount: 1);
1521 }
1522 if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) {
1523 if (oinp != inp) {
1524 socket_unlock(so: oinp->inp_socket, refcount: 1);
1525 }
1526 goto skip_oinp;
1527 }
1528
1529 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1530 otp->t_state == TCPS_TIME_WAIT &&
1531 ((int)(tcp_now - otp->t_starttime)) < tcp_msl &&
1532 (otp->t_flags & TF_RCVD_CC)) {
1533 otp = tcp_close(otp);
1534 } else {
1535 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1536 (uint64_t)VM_KERNEL_ADDRPERM(inp));
1537 if (oinp != inp) {
1538 socket_unlock(so: oinp->inp_socket, refcount: 1);
1539 }
1540 error = EADDRINUSE;
1541 goto done;
1542 }
1543 if (oinp != inp) {
1544 socket_unlock(so: oinp->inp_socket, refcount: 1);
1545 }
1546 }
1547skip_oinp:
1548 if ((inp->inp_laddr.s_addr == INADDR_ANY ? laddr.s_addr :
1549 inp->inp_laddr.s_addr) == sin->sin_addr.s_addr &&
1550 inp->inp_lport == sin->sin_port) {
1551 error = EINVAL;
1552 goto done;
1553 }
1554#if SKYWALK
1555 if (!NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1556 error = netns_reserve_in(token: &inp->inp_netns_token,
1557 addr: inp->inp_laddr.s_addr != INADDR_ANY ?
1558 inp->inp_laddr : laddr,
1559 IPPROTO_TCP, port: inp->inp_lport, NETNS_BSD, NULL);
1560 if (error) {
1561 goto done;
1562 }
1563 }
1564#endif /* SKYWALK */
1565 if (!lck_rw_try_lock_exclusive(lck: &inp->inp_pcbinfo->ipi_lock)) {
1566 /*lock inversion issue, mostly with udp multicast packets */
1567 socket_unlock(so: inp->inp_socket, refcount: 0);
1568 lck_rw_lock_exclusive(lck: &inp->inp_pcbinfo->ipi_lock);
1569 socket_lock(so: inp->inp_socket, refcount: 0);
1570 }
1571 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1572 inp->inp_laddr = laddr;
1573 /* no reference needed */
1574 inp->inp_last_outifp = outif;
1575#if SKYWALK
1576 if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1577 netns_set_ifnet(token: &inp->inp_netns_token, ifp: inp->inp_last_outifp);
1578 }
1579#endif /* SKYWALK */
1580
1581 inp->inp_flags |= INP_INADDR_ANY;
1582 }
1583 inp->inp_faddr = sin->sin_addr;
1584 inp->inp_fport = sin->sin_port;
1585 in_pcbrehash(inp);
1586 lck_rw_done(lck: &inp->inp_pcbinfo->ipi_lock);
1587
1588 if (inp->inp_flowhash == 0) {
1589 inp_calc_flowhash(inp);
1590 ASSERT(inp->inp_flowhash != 0);
1591 }
1592
1593 tcp_set_max_rwinscale(tp, so);
1594
1595 soisconnecting(so);
1596 tcpstat.tcps_connattempt++;
1597 TCP_LOG_STATE(tp, TCPS_SYN_SENT);
1598 tp->t_state = TCPS_SYN_SENT;
1599 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, TCP_CONN_KEEPINIT(tp));
1600 tp->iss = tcp_new_isn(tp);
1601 tcp_sendseqinit(tp);
1602 tp->t_connect_time = tcp_now;
1603 if (nstat_collect) {
1604 nstat_pcb_event(inp, event: NSTAT_EVENT_SRC_FLOW_STATE_OUTBOUND);
1605 nstat_route_connect_attempt(rte: inp->inp_route.ro_rt);
1606 }
1607
1608 tcp_add_fsw_flow(tp, outif);
1609
1610done:
1611 if (outif != NULL) {
1612 ifnet_release(interface: outif);
1613 }
1614
1615 return error;
1616}
1617
1618static int
1619tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p)
1620{
1621 struct inpcb *inp = tp->t_inpcb, *oinp;
1622 struct socket *so = inp->inp_socket;
1623 struct tcpcb *otp;
1624 struct sockaddr_in6 *sin6 = SIN6(nam);
1625 struct in6_addr addr6;
1626 int error = 0;
1627 struct ifnet *outif = NULL;
1628
1629 if (inp->inp_lport == 0) {
1630 error = in6_pcbbind(inp, NULL, p);
1631 if (error) {
1632 goto done;
1633 }
1634 }
1635
1636 /*
1637 * Cannot simply call in_pcbconnect, because there might be an
1638 * earlier incarnation of this same connection still in
1639 * TIME_WAIT state, creating an ADDRINUSE error.
1640 *
1641 * in6_pcbladdr() might return an ifp with its reference held
1642 * even in the error case, so make sure that it's released
1643 * whenever it's non-NULL.
1644 */
1645 error = in6_pcbladdr(inp, nam, &addr6, &outif);
1646 if (error) {
1647 goto done;
1648 }
1649 socket_unlock(so: inp->inp_socket, refcount: 0);
1650
1651 uint32_t lifscope = IFSCOPE_NONE;
1652 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1653 lifscope = inp->inp_lifscope;
1654 } else if (sin6->sin6_scope_id != IFSCOPE_NONE) {
1655 lifscope = sin6->sin6_scope_id;
1656 } else if (outif != NULL) {
1657 lifscope = outif->if_index;
1658 }
1659 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1660 &sin6->sin6_addr, sin6->sin6_port, sin6->sin6_scope_id,
1661 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1662 ? &addr6
1663 : &inp->in6p_laddr,
1664 inp->inp_lport, lifscope, 0, NULL);
1665 socket_lock(so: inp->inp_socket, refcount: 0);
1666 if (oinp) {
1667 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1668 otp->t_state == TCPS_TIME_WAIT &&
1669 ((int)(tcp_now - otp->t_starttime)) < tcp_msl &&
1670 (otp->t_flags & TF_RCVD_CC)) {
1671 otp = tcp_close(otp);
1672 } else {
1673 error = EADDRINUSE;
1674 goto done;
1675 }
1676 }
1677#if SKYWALK
1678 if (!NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1679 error = netns_reserve_in6(token: &inp->inp_netns_token,
1680 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1681 addr6 : inp->in6p_laddr,
1682 IPPROTO_TCP, port: inp->inp_lport, NETNS_BSD, NULL);
1683 if (error) {
1684 goto done;
1685 }
1686 }
1687#endif /* SKYWALK */
1688 if (!lck_rw_try_lock_exclusive(lck: &inp->inp_pcbinfo->ipi_lock)) {
1689 /*lock inversion issue, mostly with udp multicast packets */
1690 socket_unlock(so: inp->inp_socket, refcount: 0);
1691 lck_rw_lock_exclusive(lck: &inp->inp_pcbinfo->ipi_lock);
1692 socket_lock(so: inp->inp_socket, refcount: 0);
1693 }
1694 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1695 inp->in6p_laddr = addr6;
1696 inp->in6p_last_outifp = outif; /* no reference needed */
1697 inp->inp_lifscope = lifscope;
1698 in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1699#if SKYWALK
1700 if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1701 netns_set_ifnet(token: &inp->inp_netns_token, ifp: inp->in6p_last_outifp);
1702 }
1703#endif /* SKYWALK */
1704 inp->in6p_flags |= INP_IN6ADDR_ANY;
1705 }
1706 inp->in6p_faddr = sin6->sin6_addr;
1707 inp->inp_fport = sin6->sin6_port;
1708 inp->inp_fifscope = sin6->sin6_scope_id;
1709 in6_verify_ifscope(&inp->in6p_faddr, inp->inp_fifscope);
1710 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0) {
1711 inp->inp_flow = sin6->sin6_flowinfo;
1712 }
1713 in_pcbrehash(inp);
1714 lck_rw_done(lck: &inp->inp_pcbinfo->ipi_lock);
1715
1716 if (inp->inp_flowhash == 0) {
1717 inp_calc_flowhash(inp);
1718 ASSERT(inp->inp_flowhash != 0);
1719 }
1720 /* update flowinfo - RFC 6437 */
1721 if (inp->inp_flow == 0 && inp->in6p_flags & IN6P_AUTOFLOWLABEL) {
1722 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1723 inp->inp_flow |=
1724 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1725 }
1726
1727 tcp_set_max_rwinscale(tp, so);
1728
1729 soisconnecting(so);
1730 tcpstat.tcps_connattempt++;
1731 TCP_LOG_STATE(tp, TCPS_SYN_SENT);
1732 tp->t_state = TCPS_SYN_SENT;
1733 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
1734 TCP_CONN_KEEPINIT(tp));
1735 tp->iss = tcp_new_isn(tp);
1736 tcp_sendseqinit(tp);
1737 tp->t_connect_time = tcp_now;
1738 if (nstat_collect) {
1739 nstat_pcb_event(inp, event: NSTAT_EVENT_SRC_FLOW_STATE_OUTBOUND);
1740 nstat_route_connect_attempt(rte: inp->inp_route.ro_rt);
1741 }
1742
1743 tcp_add_fsw_flow(tp, outif);
1744
1745done:
1746 if (outif != NULL) {
1747 ifnet_release(interface: outif);
1748 }
1749
1750 return error;
1751}
1752
1753/*
1754 * Export TCP internal state information via a struct tcp_info
1755 */
1756void
1757tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1758{
1759 struct inpcb *inp = tp->t_inpcb;
1760
1761 bzero(s: ti, n: sizeof(*ti));
1762
1763 ti->tcpi_state = (uint8_t)tp->t_state;
1764 ti->tcpi_flowhash = inp != NULL ? inp->inp_flowhash: 0;
1765
1766 if (TSTMP_SUPPORTED(tp)) {
1767 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1768 }
1769 if (SACK_ENABLED(tp)) {
1770 ti->tcpi_options |= TCPI_OPT_SACK;
1771 }
1772 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
1773 ti->tcpi_options |= TCPI_OPT_WSCALE;
1774 ti->tcpi_snd_wscale = tp->snd_scale;
1775 ti->tcpi_rcv_wscale = tp->rcv_scale;
1776 }
1777 if (TCP_ECN_ENABLED(tp)) {
1778 ti->tcpi_options |= TCPI_OPT_ECN;
1779 }
1780
1781 /* Are we in retranmission episode */
1782 if (IN_FASTRECOVERY(tp) || tp->t_rxtshift > 0) {
1783 ti->tcpi_flags |= TCPI_FLAG_LOSSRECOVERY;
1784 }
1785
1786 if (tp->t_flags & TF_STREAMING_ON) {
1787 ti->tcpi_flags |= TCPI_FLAG_STREAMING_ON;
1788 }
1789
1790 ti->tcpi_rto = tp->t_timer[TCPT_REXMT] ? tp->t_rxtcur : 0;
1791 ti->tcpi_snd_mss = tp->t_maxseg;
1792 ti->tcpi_rcv_mss = tp->t_maxseg;
1793
1794 ti->tcpi_rttcur = tp->t_rttcur;
1795 ti->tcpi_srtt = tp->t_srtt >> TCP_RTT_SHIFT;
1796 ti->tcpi_rcv_srtt = tp->rcv_srtt >> TCP_RTT_SHIFT;
1797 ti->tcpi_rttvar = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
1798 ti->tcpi_rttbest = tp->t_rttbest >> TCP_RTT_SHIFT;
1799
1800 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1801 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1802 if (inp != NULL && inp->inp_socket != NULL) {
1803 ti->tcpi_snd_sbbytes = inp->inp_socket->so_snd.sb_cc;
1804 }
1805
1806 ti->tcpi_rcv_space = tp->rcv_adv > tp->rcv_nxt ?
1807 tp->rcv_adv - tp->rcv_nxt : 0;
1808
1809 ti->tcpi_snd_wnd = tp->snd_wnd;
1810 ti->tcpi_snd_nxt = tp->snd_nxt;
1811 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1812
1813 /* convert bytes/msec to bits/sec */
1814 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
1815 tp->t_bwmeas != NULL) {
1816 ti->tcpi_snd_bw = (tp->t_bwmeas->bw_sndbw * 8000);
1817 }
1818
1819 ti->tcpi_txpackets = inp != NULL ? inp->inp_stat->txpackets : 0;
1820 ti->tcpi_txbytes = inp != NULL ? inp->inp_stat->txbytes : 0;
1821 ti->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes;
1822 ti->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts;
1823 ti->tcpi_txunacked = tp->snd_max - tp->snd_una;
1824
1825 ti->tcpi_rxpackets = inp != NULL ? inp->inp_stat->rxpackets : 0;
1826 ti->tcpi_rxbytes = inp != NULL ? inp->inp_stat->rxbytes : 0;
1827 ti->tcpi_rxduplicatebytes = tp->t_stat.rxduplicatebytes;
1828 ti->tcpi_rxoutoforderbytes = tp->t_stat.rxoutoforderbytes;
1829
1830 if (tp->t_state > TCPS_LISTEN) {
1831 ti->tcpi_synrexmits = (uint8_t)tp->t_stat.rxmitsyns;
1832 }
1833 if (inp != NULL) {
1834 ti->tcpi_cell_rxpackets = inp->inp_cstat->rxpackets;
1835 ti->tcpi_cell_rxbytes = inp->inp_cstat->rxbytes;
1836 ti->tcpi_cell_txpackets = inp->inp_cstat->txpackets;
1837 ti->tcpi_cell_txbytes = inp->inp_cstat->txbytes;
1838
1839 ti->tcpi_wifi_rxpackets = inp->inp_wstat->rxpackets;
1840 ti->tcpi_wifi_rxbytes = inp->inp_wstat->rxbytes;
1841 ti->tcpi_wifi_txpackets = inp->inp_wstat->txpackets;
1842 ti->tcpi_wifi_txbytes = inp->inp_wstat->txbytes;
1843
1844 ti->tcpi_wired_rxpackets = inp->inp_Wstat->rxpackets;
1845 ti->tcpi_wired_rxbytes = inp->inp_Wstat->rxbytes;
1846 ti->tcpi_wired_txpackets = inp->inp_Wstat->txpackets;
1847 ti->tcpi_wired_txbytes = inp->inp_Wstat->txbytes;
1848 }
1849 tcp_get_connectivity_status(tp, &ti->tcpi_connstatus);
1850
1851 ti->tcpi_tfo_syn_data_rcv = !!(tp->t_tfo_stats & TFO_S_SYNDATA_RCV);
1852 ti->tcpi_tfo_cookie_req_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIEREQ_RECV);
1853 ti->tcpi_tfo_cookie_sent = !!(tp->t_tfo_stats & TFO_S_COOKIE_SENT);
1854 ti->tcpi_tfo_cookie_invalid = !!(tp->t_tfo_stats & TFO_S_COOKIE_INVALID);
1855
1856 ti->tcpi_tfo_cookie_req = !!(tp->t_tfo_stats & TFO_S_COOKIE_REQ);
1857 ti->tcpi_tfo_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIE_RCV);
1858 ti->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT);
1859 ti->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED);
1860 ti->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS);
1861 ti->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG);
1862 ti->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV);
1863 ti->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE);
1864 ti->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE);
1865 ti->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE);
1866 ti->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY);
1867
1868 ti->tcpi_ecn_client_setup = !!(tp->ecn_flags & (TE_SETUPSENT | TE_ACE_SETUPSENT));
1869 ti->tcpi_ecn_server_setup = !!(tp->ecn_flags & (TE_SETUPRECEIVED | TE_ACE_SETUPRECEIVED));
1870 ti->tcpi_ecn_success = (TCP_ECN_ENABLED(tp) || TCP_ACC_ECN_ON(tp)) ? 1 : 0;
1871 ti->tcpi_ecn_lost_syn = !!(tp->ecn_flags & TE_LOST_SYN);
1872 ti->tcpi_ecn_lost_synack = !!(tp->ecn_flags & TE_LOST_SYNACK);
1873
1874 ti->tcpi_local_peer = !!(tp->t_flags & TF_LOCAL);
1875
1876 if (inp != NULL && inp->inp_last_outifp != NULL) {
1877 ti->tcpi_last_outif = inp->inp_last_outifp->if_index;
1878
1879 if (IFNET_IS_CELLULAR(inp->inp_last_outifp)) {
1880 ti->tcpi_if_cell = 1;
1881 }
1882 if (IFNET_IS_WIFI(inp->inp_last_outifp)) {
1883 ti->tcpi_if_wifi = 1;
1884 }
1885 if (IFNET_IS_WIRED(inp->inp_last_outifp)) {
1886 ti->tcpi_if_wired = 1;
1887 }
1888 if (IFNET_IS_WIFI_INFRA(inp->inp_last_outifp)) {
1889 ti->tcpi_if_wifi_infra = 1;
1890 }
1891 if (inp->inp_last_outifp->if_eflags & IFEF_AWDL) {
1892 ti->tcpi_if_wifi_awdl = 1;
1893 }
1894 }
1895 if (tp->tcp_cc_index == TCP_CC_ALGO_BACKGROUND_INDEX) {
1896 ti->tcpi_snd_background = 1;
1897 }
1898 if (tcp_recv_bg == 1 || (inp != NULL && inp->inp_socket != NULL &&
1899 IS_TCP_RECV_BG(inp->inp_socket))) {
1900 ti->tcpi_rcv_background = 1;
1901 }
1902
1903 ti->tcpi_ecn_recv_ce = tp->t_ecn_recv_ce;
1904 ti->tcpi_ecn_recv_cwr = tp->t_ecn_recv_cwr;
1905
1906 ti->tcpi_rcvoopack = tp->t_rcvoopack;
1907 ti->tcpi_pawsdrop = tp->t_pawsdrop;
1908 ti->tcpi_sack_recovery_episode = tp->t_sack_recovery_episode;
1909 ti->tcpi_reordered_pkts = tp->t_reordered_pkts;
1910 ti->tcpi_dsack_sent = tp->t_dsack_sent;
1911 ti->tcpi_dsack_recvd = tp->t_dsack_recvd;
1912
1913 ti->tcpi_client_accecn_state = tp->t_client_accecn_state;
1914 ti->tcpi_server_accecn_state = tp->t_server_accecn_state;
1915 ti->tcpi_ecn_capable_packets_sent = tp->t_ecn_capable_packets_sent;
1916 ti->tcpi_ecn_capable_packets_acked = tp->t_ecn_capable_packets_acked;
1917 ti->tcpi_ecn_capable_packets_marked = tp->t_ecn_capable_packets_marked;
1918 ti->tcpi_ecn_capable_packets_lost = tp->t_ecn_capable_packets_lost;
1919
1920 ti->tcpi_flow_control_total_time = inp->inp_fadv_total_time;
1921 ti->tcpi_rcvwnd_limited_total_time = tp->t_rcvwnd_limited_total_time;
1922}
1923
1924__private_extern__ errno_t
1925tcp_fill_info_for_info_tuple(struct info_tuple *itpl, struct tcp_info *ti)
1926{
1927 struct inpcbinfo *pcbinfo = NULL;
1928 struct inpcb *inp = NULL;
1929 struct socket *so;
1930 struct tcpcb *tp;
1931
1932 if (itpl->itpl_proto == IPPROTO_TCP) {
1933 pcbinfo = &tcbinfo;
1934 } else {
1935 return EINVAL;
1936 }
1937
1938 if (itpl->itpl_local_sa.sa_family == AF_INET &&
1939 itpl->itpl_remote_sa.sa_family == AF_INET) {
1940 inp = in_pcblookup_hash(pcbinfo,
1941 itpl->itpl_remote_sin.sin_addr,
1942 itpl->itpl_remote_sin.sin_port,
1943 itpl->itpl_local_sin.sin_addr,
1944 itpl->itpl_local_sin.sin_port,
1945 0, NULL);
1946 } else if (itpl->itpl_local_sa.sa_family == AF_INET6 &&
1947 itpl->itpl_remote_sa.sa_family == AF_INET6) {
1948 struct in6_addr ina6_local;
1949 struct in6_addr ina6_remote;
1950
1951 ina6_local = itpl->itpl_local_sin6.sin6_addr;
1952 if (in6_embedded_scope && IN6_IS_SCOPE_LINKLOCAL(&ina6_local) &&
1953 itpl->itpl_local_sin6.sin6_scope_id) {
1954 ina6_local.s6_addr16[1] = htons((uint16_t)itpl->itpl_local_sin6.sin6_scope_id);
1955 }
1956
1957 ina6_remote = itpl->itpl_remote_sin6.sin6_addr;
1958 if (in6_embedded_scope && IN6_IS_SCOPE_LINKLOCAL(&ina6_remote) &&
1959 itpl->itpl_remote_sin6.sin6_scope_id) {
1960 ina6_remote.s6_addr16[1] = htons((uint16_t)itpl->itpl_remote_sin6.sin6_scope_id);
1961 }
1962
1963 inp = in6_pcblookup_hash(pcbinfo,
1964 &ina6_remote,
1965 itpl->itpl_remote_sin6.sin6_port,
1966 itpl->itpl_remote_sin6.sin6_scope_id,
1967 &ina6_local,
1968 itpl->itpl_local_sin6.sin6_port,
1969 itpl->itpl_local_sin6.sin6_scope_id,
1970 0, NULL);
1971 } else {
1972 return EINVAL;
1973 }
1974
1975 if (inp != NULL) {
1976 if ((so = inp->inp_socket) == NULL) {
1977 return ENOENT;
1978 }
1979 socket_lock(so, refcount: 0);
1980 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
1981 socket_unlock(so, refcount: 0);
1982 return ENOENT;
1983 }
1984 tp = intotcpcb(inp);
1985
1986 tcp_fill_info(tp, ti);
1987 socket_unlock(so, refcount: 0);
1988
1989 return 0;
1990 }
1991#if SKYWALK
1992 else {
1993 /* if no pcb found, check for flowswitch for uTCP flow */
1994 int error;
1995 struct nexus_mib_filter nmf = {
1996 .nmf_type = NXMIB_FLOW,
1997 .nmf_bitmap = NXMIB_FILTER_INFO_TUPLE,
1998 .nmf_info_tuple = *itpl,
1999 };
2000 struct sk_stats_flow sf;
2001 size_t len = sizeof(sf);
2002 error = kernel_sysctlbyname(SK_STATS_FLOW, &sf, &len, &nmf, sizeof(nmf));
2003 if (error != 0) {
2004 printf("kernel_sysctlbyname err %d\n", error);
2005 return error;
2006 }
2007 if (len != sizeof(sf)) {
2008 printf("kernel_sysctlbyname invalid len %zu\n", len);
2009 return ENOENT;
2010 }
2011
2012 /*
2013 * This is what flow tracker can offer right now, which is good
2014 * for mDNS TCP keep alive offload.
2015 */
2016 ti->tcpi_snd_nxt = sf.sf_lseq;
2017 ti->tcpi_rcv_nxt = sf.sf_rseq;
2018 ti->tcpi_rcv_space = (uint32_t)(sf.sf_lmax_win << sf.sf_lwscale);
2019 ti->tcpi_rcv_wscale = sf.sf_lwscale;
2020 ti->tcpi_last_outif = (int32_t)sf.sf_if_index;
2021
2022 return 0;
2023 }
2024#endif /* SKYWALK */
2025
2026 return ENOENT;
2027}
2028
2029static void
2030tcp_connection_fill_info(struct tcpcb *tp, struct tcp_connection_info *tci)
2031{
2032 struct inpcb *inp = tp->t_inpcb;
2033
2034 bzero(s: tci, n: sizeof(*tci));
2035 tci->tcpi_state = (uint8_t)tp->t_state;
2036
2037 if (TSTMP_SUPPORTED(tp)) {
2038 tci->tcpi_options |= TCPCI_OPT_TIMESTAMPS;
2039 }
2040 if (SACK_ENABLED(tp)) {
2041 tci->tcpi_options |= TCPCI_OPT_SACK;
2042 }
2043 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
2044 tci->tcpi_options |= TCPCI_OPT_WSCALE;
2045 tci->tcpi_snd_wscale = tp->snd_scale;
2046 tci->tcpi_rcv_wscale = tp->rcv_scale;
2047 }
2048 if (TCP_ECN_ENABLED(tp)) {
2049 tci->tcpi_options |= TCPCI_OPT_ECN;
2050 }
2051 if (IN_FASTRECOVERY(tp) || tp->t_rxtshift > 0) {
2052 tci->tcpi_flags |= TCPCI_FLAG_LOSSRECOVERY;
2053 }
2054 if (tp->t_flagsext & TF_PKTS_REORDERED) {
2055 tci->tcpi_flags |= TCPCI_FLAG_REORDERING_DETECTED;
2056 }
2057 tci->tcpi_rto = tp->t_timer[TCPT_REXMT] > 0 ? tp->t_rxtcur : 0;
2058 tci->tcpi_maxseg = tp->t_maxseg;
2059 tci->tcpi_snd_ssthresh = tp->snd_ssthresh;
2060 tci->tcpi_snd_cwnd = tp->snd_cwnd;
2061 tci->tcpi_snd_wnd = tp->snd_wnd;
2062 if (inp != NULL && inp->inp_socket != NULL) {
2063 tci->tcpi_snd_sbbytes = inp->inp_socket->so_snd.sb_cc;
2064 }
2065 tci->tcpi_rcv_wnd = tp->rcv_adv > tp->rcv_nxt ? tp->rcv_adv - tp->rcv_nxt : 0;
2066 tci->tcpi_rttcur = tp->t_rttcur;
2067 tci->tcpi_srtt = (tp->t_srtt >> TCP_RTT_SHIFT);
2068 tci->tcpi_rttvar = (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
2069 tci->tcpi_txpackets = inp != NULL ? inp->inp_stat->txpackets : 0;
2070 tci->tcpi_txbytes = inp != NULL ? inp->inp_stat->txbytes : 0;
2071 tci->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes;
2072 tci->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts;
2073 tci->tcpi_rxpackets = inp != NULL ? inp->inp_stat->rxpackets : 0;
2074 tci->tcpi_rxbytes = inp != NULL ? inp->inp_stat->rxbytes : 0;
2075 tci->tcpi_rxoutoforderbytes = tp->t_stat.rxoutoforderbytes;
2076
2077 tci->tcpi_tfo_syn_data_rcv = !!(tp->t_tfo_stats & TFO_S_SYNDATA_RCV);
2078 tci->tcpi_tfo_cookie_req_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIEREQ_RECV);
2079 tci->tcpi_tfo_cookie_sent = !!(tp->t_tfo_stats & TFO_S_COOKIE_SENT);
2080 tci->tcpi_tfo_cookie_invalid = !!(tp->t_tfo_stats & TFO_S_COOKIE_INVALID);
2081 tci->tcpi_tfo_cookie_req = !!(tp->t_tfo_stats & TFO_S_COOKIE_REQ);
2082 tci->tcpi_tfo_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIE_RCV);
2083 tci->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT);
2084 tci->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED);
2085 tci->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS);
2086 tci->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG);
2087 tci->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV);
2088 tci->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE);
2089 tci->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE);
2090 tci->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE);
2091 tci->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY);
2092}
2093
2094
2095__private_extern__ int
2096tcp_sysctl_info(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2097{
2098 int error;
2099 struct tcp_info ti = {};
2100 struct info_tuple itpl;
2101
2102 if (req->newptr == USER_ADDR_NULL) {
2103 return EINVAL;
2104 }
2105 if (req->newlen < sizeof(struct info_tuple)) {
2106 return EINVAL;
2107 }
2108 error = SYSCTL_IN(req, &itpl, sizeof(struct info_tuple));
2109 if (error != 0) {
2110 return error;
2111 }
2112 error = tcp_fill_info_for_info_tuple(itpl: &itpl, ti: &ti);
2113 if (error != 0) {
2114 return error;
2115 }
2116 error = SYSCTL_OUT(req, &ti, sizeof(struct tcp_info));
2117 if (error != 0) {
2118 return error;
2119 }
2120
2121 return 0;
2122}
2123
2124static int
2125tcp_lookup_peer_pid_locked(struct socket *so, pid_t *out_pid)
2126{
2127 int error = EHOSTUNREACH;
2128 *out_pid = -1;
2129 if ((so->so_state & SS_ISCONNECTED) == 0) {
2130 return ENOTCONN;
2131 }
2132
2133 struct inpcb *inp = (struct inpcb*)so->so_pcb;
2134 uint16_t lport = inp->inp_lport;
2135 uint16_t fport = inp->inp_fport;
2136 uint32_t fifscope = inp->inp_fifscope;
2137 uint32_t lifscope = inp->inp_lifscope;
2138
2139 struct inpcb *finp = NULL;
2140 struct in6_addr laddr6, faddr6;
2141 struct in_addr laddr4, faddr4;
2142
2143 if (inp->inp_vflag & INP_IPV6) {
2144 laddr6 = inp->in6p_laddr;
2145 faddr6 = inp->in6p_faddr;
2146 } else if (inp->inp_vflag & INP_IPV4) {
2147 laddr4 = inp->inp_laddr;
2148 faddr4 = inp->inp_faddr;
2149 }
2150
2151 socket_unlock(so, refcount: 0);
2152 if (inp->inp_vflag & INP_IPV6) {
2153 finp = in6_pcblookup_hash(&tcbinfo, &laddr6, lport, lifscope, &faddr6, fport, fifscope, 0, NULL);
2154 } else if (inp->inp_vflag & INP_IPV4) {
2155 finp = in_pcblookup_hash(&tcbinfo, laddr4, lport, faddr4, fport, 0, NULL);
2156 }
2157
2158 if (finp) {
2159 *out_pid = finp->inp_socket->last_pid;
2160 error = 0;
2161 in_pcb_checkstate(finp, WNT_RELEASE, 0);
2162 }
2163 socket_lock(so, refcount: 0);
2164
2165 return error;
2166}
2167
2168void
2169tcp_getconninfo(struct socket *so, struct conninfo_tcp *tcp_ci)
2170{
2171 tcp_fill_info(sototcpcb(so), ti: &tcp_ci->tcpci_tcp_info);
2172}
2173
2174void
2175tcp_clear_keep_alive_offload(struct socket *so)
2176{
2177 struct inpcb *inp;
2178 struct ifnet *ifp;
2179
2180 inp = sotoinpcb(so);
2181 if (inp == NULL) {
2182 return;
2183 }
2184
2185 if ((inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD) == 0) {
2186 return;
2187 }
2188
2189 ifp = inp->inp_boundifp != NULL ? inp->inp_boundifp :
2190 inp->inp_last_outifp;
2191 if (ifp == NULL) {
2192 panic("%s: so %p inp %p ifp NULL",
2193 __func__, so, inp);
2194 }
2195
2196 ifnet_lock_exclusive(ifp);
2197
2198 if (ifp->if_tcp_kao_cnt == 0) {
2199 panic("%s: so %p inp %p ifp %p if_tcp_kao_cnt == 0",
2200 __func__, so, inp, ifp);
2201 }
2202 ifp->if_tcp_kao_cnt--;
2203 inp->inp_flags2 &= ~INP2_KEEPALIVE_OFFLOAD;
2204
2205 ifnet_lock_done(ifp);
2206}
2207
2208static int
2209tcp_set_keep_alive_offload(struct socket *so, struct proc *proc)
2210{
2211 int error = 0;
2212 struct inpcb *inp;
2213 struct ifnet *ifp;
2214
2215 inp = sotoinpcb(so);
2216 if (inp == NULL) {
2217 return ECONNRESET;
2218 }
2219 if ((inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD) != 0) {
2220 return 0;
2221 }
2222
2223 ifp = inp->inp_boundifp != NULL ? inp->inp_boundifp :
2224 inp->inp_last_outifp;
2225 if (ifp == NULL) {
2226 error = ENXIO;
2227 os_log_info(OS_LOG_DEFAULT,
2228 "%s: error %d for proc %s[%u] out ifp is not set\n",
2229 __func__, error,
2230 proc != NULL ? proc->p_comm : "kernel",
2231 proc != NULL ? proc_getpid(proc) : 0);
2232 return ENXIO;
2233 }
2234
2235 error = if_get_tcp_kao_max(ifp);
2236 if (error != 0) {
2237 return error;
2238 }
2239
2240 ifnet_lock_exclusive(ifp);
2241 if (ifp->if_tcp_kao_cnt < ifp->if_tcp_kao_max) {
2242 ifp->if_tcp_kao_cnt++;
2243 inp->inp_flags2 |= INP2_KEEPALIVE_OFFLOAD;
2244 } else {
2245 error = ETOOMANYREFS;
2246 os_log_info(OS_LOG_DEFAULT,
2247 "%s: error %d for proc %s[%u] if_tcp_kao_max %u\n",
2248 __func__, error,
2249 proc != NULL ? proc->p_comm : "kernel",
2250 proc != NULL ? proc_getpid(proc) : 0,
2251 ifp->if_tcp_kao_max);
2252 }
2253 ifnet_lock_done(ifp);
2254
2255 return error;
2256}
2257
2258/*
2259 * The new sockopt interface makes it possible for us to block in the
2260 * copyin/out step (if we take a page fault). Taking a page fault at
2261 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
2262 * use TSM, there probably isn't any need for this function to run at
2263 * splnet() any more. This needs more examination.)
2264 */
2265int
2266tcp_ctloutput(struct socket *so, struct sockopt *sopt)
2267{
2268 int error = 0, opt = 0, optval = 0;
2269 struct inpcb *inp;
2270 struct tcpcb *tp;
2271
2272 inp = sotoinpcb(so);
2273 if (inp == NULL) {
2274 return ECONNRESET;
2275 }
2276 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
2277 if (sopt->sopt_level != IPPROTO_TCP &&
2278 !(sopt->sopt_level == SOL_SOCKET && (sopt->sopt_name == SO_FLUSH ||
2279 sopt->sopt_name == SO_TRAFFIC_MGT_BACKGROUND))) {
2280 if (SOCK_CHECK_DOM(so, PF_INET6)) {
2281 error = ip6_ctloutput(so, sopt);
2282 } else {
2283 error = ip_ctloutput(so, sopt);
2284 }
2285 return error;
2286 }
2287 tp = intotcpcb(inp);
2288 if (tp == NULL) {
2289 return ECONNRESET;
2290 }
2291
2292 calculate_tcp_clock();
2293
2294 switch (sopt->sopt_dir) {
2295 case SOPT_SET:
2296 switch (sopt->sopt_name) {
2297 case TCP_NODELAY:
2298 case TCP_NOOPT:
2299 case TCP_NOPUSH:
2300 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2301 minlen: sizeof optval);
2302 if (error) {
2303 break;
2304 }
2305
2306 switch (sopt->sopt_name) {
2307 case TCP_NODELAY:
2308 opt = TF_NODELAY;
2309 break;
2310 case TCP_NOOPT:
2311 opt = TF_NOOPT;
2312 break;
2313 case TCP_NOPUSH:
2314 opt = TF_NOPUSH;
2315 break;
2316 default:
2317 opt = 0; /* dead code to fool gcc */
2318 break;
2319 }
2320
2321 if (optval) {
2322 tp->t_flags |= opt;
2323 } else {
2324 tp->t_flags &= ~opt;
2325 }
2326 break;
2327 case TCP_RXT_FINDROP:
2328 case TCP_NOTIMEWAIT:
2329 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2330 minlen: sizeof optval);
2331 if (error) {
2332 break;
2333 }
2334 switch (sopt->sopt_name) {
2335 case TCP_RXT_FINDROP:
2336 opt = TF_RXTFINDROP;
2337 break;
2338 case TCP_NOTIMEWAIT:
2339 opt = TF_NOTIMEWAIT;
2340 break;
2341 default:
2342 opt = 0;
2343 break;
2344 }
2345 if (optval) {
2346 tp->t_flagsext |= opt;
2347 } else {
2348 tp->t_flagsext &= ~opt;
2349 }
2350 break;
2351 case TCP_MEASURE_SND_BW:
2352 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2353 minlen: sizeof optval);
2354 if (error) {
2355 break;
2356 }
2357 opt = TF_MEASURESNDBW;
2358 if (optval) {
2359 if (tp->t_bwmeas == NULL) {
2360 tp->t_bwmeas = tcp_bwmeas_alloc(tp);
2361 if (tp->t_bwmeas == NULL) {
2362 error = ENOMEM;
2363 break;
2364 }
2365 }
2366 tp->t_flagsext |= opt;
2367 } else {
2368 tp->t_flagsext &= ~opt;
2369 /* Reset snd bw measurement state */
2370 tp->t_flagsext &= ~(TF_BWMEAS_INPROGRESS);
2371 if (tp->t_bwmeas != NULL) {
2372 tcp_bwmeas_free(tp);
2373 }
2374 }
2375 break;
2376 case TCP_MEASURE_BW_BURST: {
2377 struct tcp_measure_bw_burst in;
2378 uint32_t minpkts, maxpkts;
2379 bzero(s: &in, n: sizeof(in));
2380
2381 error = sooptcopyin(sopt, &in, len: sizeof(in),
2382 minlen: sizeof(in));
2383 if (error) {
2384 break;
2385 }
2386 if ((tp->t_flagsext & TF_MEASURESNDBW) == 0 ||
2387 tp->t_bwmeas == NULL) {
2388 error = EINVAL;
2389 break;
2390 }
2391 minpkts = (in.min_burst_size != 0) ? in.min_burst_size :
2392 tp->t_bwmeas->bw_minsizepkts;
2393 maxpkts = (in.max_burst_size != 0) ? in.max_burst_size :
2394 tp->t_bwmeas->bw_maxsizepkts;
2395 if (minpkts > maxpkts) {
2396 error = EINVAL;
2397 break;
2398 }
2399 tp->t_bwmeas->bw_minsizepkts = minpkts;
2400 tp->t_bwmeas->bw_maxsizepkts = maxpkts;
2401 tp->t_bwmeas->bw_minsize = (minpkts * tp->t_maxseg);
2402 tp->t_bwmeas->bw_maxsize = (maxpkts * tp->t_maxseg);
2403 break;
2404 }
2405 case TCP_MAXSEG:
2406 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2407 minlen: sizeof optval);
2408 if (error) {
2409 break;
2410 }
2411
2412 if (optval > 0 && optval <= tp->t_maxseg &&
2413 optval + 40 >= tcp_minmss) {
2414 tp->t_maxseg = optval;
2415 } else {
2416 error = EINVAL;
2417 }
2418 break;
2419
2420 case TCP_KEEPALIVE:
2421 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2422 minlen: sizeof optval);
2423 if (error) {
2424 break;
2425 }
2426 if (optval < 0 || optval > UINT32_MAX / TCP_RETRANSHZ) {
2427 error = EINVAL;
2428 } else {
2429 tp->t_keepidle = optval * TCP_RETRANSHZ;
2430 /* reset the timer to new value */
2431 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2432 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
2433 TCP_CONN_KEEPIDLE(tp));
2434 tcp_check_timer_state(tp);
2435 }
2436 }
2437 break;
2438
2439 case TCP_CONNECTIONTIMEOUT:
2440 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2441 minlen: sizeof optval);
2442 if (error) {
2443 break;
2444 }
2445 if (optval < 0 || optval > UINT32_MAX / TCP_RETRANSHZ) {
2446 error = EINVAL;
2447 } else {
2448 tp->t_keepinit = optval * TCP_RETRANSHZ;
2449 if (tp->t_state == TCPS_SYN_RECEIVED ||
2450 tp->t_state == TCPS_SYN_SENT) {
2451 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
2452 TCP_CONN_KEEPINIT(tp));
2453 tcp_check_timer_state(tp);
2454 }
2455 }
2456 break;
2457
2458 case TCP_KEEPINTVL:
2459 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2460 minlen: sizeof(optval));
2461 if (error) {
2462 break;
2463 }
2464 if (optval < 0 || optval > UINT32_MAX / TCP_RETRANSHZ) {
2465 error = EINVAL;
2466 } else {
2467 tp->t_keepintvl = optval * TCP_RETRANSHZ;
2468 if (tp->t_state == TCPS_FIN_WAIT_2 &&
2469 TCP_CONN_MAXIDLE(tp) > 0) {
2470 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
2471 TCP_CONN_MAXIDLE(tp));
2472 tcp_check_timer_state(tp);
2473 }
2474 }
2475 break;
2476
2477 case TCP_KEEPCNT:
2478 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2479 minlen: sizeof(optval));
2480 if (error) {
2481 break;
2482 }
2483 if (optval < 0 || optval > INT32_MAX) {
2484 error = EINVAL;
2485 } else {
2486 tp->t_keepcnt = optval;
2487 if (tp->t_state == TCPS_FIN_WAIT_2 &&
2488 TCP_CONN_MAXIDLE(tp) > 0) {
2489 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
2490 TCP_CONN_MAXIDLE(tp));
2491 tcp_check_timer_state(tp);
2492 }
2493 }
2494 break;
2495
2496 case TCP_KEEPALIVE_OFFLOAD:
2497 if ((error = priv_check_cred(cred: kauth_cred_get(),
2498 PRIV_NETINET_TCP_KA_OFFLOAD, flags: 0)) != 0) {
2499 break;
2500 }
2501 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2502 minlen: sizeof(optval));
2503 if (error) {
2504 break;
2505 }
2506 if (optval < 0 || optval > INT32_MAX) {
2507 error = EINVAL;
2508 break;
2509 }
2510 if (optval != 0) {
2511 error = tcp_set_keep_alive_offload(so,
2512 proc: sopt->sopt_p);
2513 } else {
2514 tcp_clear_keep_alive_offload(so);
2515 }
2516 break;
2517
2518 case PERSIST_TIMEOUT:
2519 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2520 minlen: sizeof optval);
2521 if (error) {
2522 break;
2523 }
2524 if (optval < 0) {
2525 error = EINVAL;
2526 } else {
2527 tp->t_persist_timeout = optval * TCP_RETRANSHZ;
2528 }
2529 break;
2530 case TCP_RXT_CONNDROPTIME:
2531 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2532 minlen: sizeof(optval));
2533 if (error) {
2534 break;
2535 }
2536 if (optval < 0) {
2537 error = EINVAL;
2538 } else {
2539 tp->t_rxt_conndroptime = optval * TCP_RETRANSHZ;
2540 }
2541 break;
2542 case TCP_NOTSENT_LOWAT:
2543 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2544 minlen: sizeof(optval));
2545 if (error) {
2546 break;
2547 }
2548 if (optval < 0) {
2549 error = EINVAL;
2550 break;
2551 } else {
2552 if (optval == 0) {
2553 so->so_flags &= ~(SOF_NOTSENT_LOWAT);
2554 tp->t_notsent_lowat = 0;
2555 } else {
2556 so->so_flags |= SOF_NOTSENT_LOWAT;
2557 tp->t_notsent_lowat = optval;
2558 }
2559 }
2560 break;
2561 case TCP_ADAPTIVE_READ_TIMEOUT:
2562 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2563 minlen: sizeof(optval));
2564 if (error) {
2565 break;
2566 }
2567 if (optval < 0 ||
2568 optval > TCP_ADAPTIVE_TIMEOUT_MAX) {
2569 error = EINVAL;
2570 break;
2571 } else if (optval == 0) {
2572 tp->t_adaptive_rtimo = 0;
2573 tcp_keepalive_reset(tp);
2574
2575 if (tp->t_mpsub) {
2576 mptcp_reset_keepalive(tp);
2577 }
2578 } else {
2579 tp->t_adaptive_rtimo = (uint8_t)optval;
2580 }
2581 break;
2582 case TCP_ADAPTIVE_WRITE_TIMEOUT:
2583 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2584 minlen: sizeof(optval));
2585 if (error) {
2586 break;
2587 }
2588 if (optval < 0 ||
2589 optval > TCP_ADAPTIVE_TIMEOUT_MAX) {
2590 error = EINVAL;
2591 break;
2592 } else {
2593 tp->t_adaptive_wtimo = (uint8_t)optval;
2594 }
2595 break;
2596 case TCP_SENDMOREACKS:
2597 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2598 minlen: sizeof(optval));
2599 if (error) {
2600 break;
2601 }
2602 if (optval < 0 || optval > 1) {
2603 error = EINVAL;
2604 } else if (optval == 0) {
2605 tp->t_flagsext &= ~(TF_NOSTRETCHACK);
2606 } else {
2607 tp->t_flagsext |= TF_NOSTRETCHACK;
2608 }
2609 break;
2610 case TCP_DISABLE_BLACKHOLE_DETECTION:
2611 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2612 minlen: sizeof(optval));
2613 if (error) {
2614 break;
2615 }
2616 if (optval < 0 || optval > 1) {
2617 error = EINVAL;
2618 } else if (optval == 0) {
2619 tp->t_flagsext &= ~TF_NOBLACKHOLE_DETECTION;
2620 } else {
2621 tp->t_flagsext |= TF_NOBLACKHOLE_DETECTION;
2622 if ((tp->t_flags & TF_BLACKHOLE) &&
2623 tp->t_pmtud_saved_maxopd > 0) {
2624 tcp_pmtud_revert_segment_size(tp);
2625 }
2626 }
2627 break;
2628 case TCP_FASTOPEN:
2629 if (!(tcp_fastopen & TCP_FASTOPEN_SERVER)) {
2630 error = ENOTSUP;
2631 break;
2632 }
2633
2634 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2635 minlen: sizeof(optval));
2636 if (error) {
2637 break;
2638 }
2639 if (optval < 0 || optval > 1) {
2640 error = EINVAL;
2641 break;
2642 }
2643 if (tp->t_state != TCPS_LISTEN) {
2644 error = EINVAL;
2645 break;
2646 }
2647 if (optval) {
2648 tp->t_flagsext |= TF_FASTOPEN;
2649 } else {
2650 tcp_disable_tfo(tp);
2651 }
2652 break;
2653 case TCP_FASTOPEN_FORCE_HEURISTICS:
2654
2655 break;
2656 case TCP_FASTOPEN_FORCE_ENABLE:
2657 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2658 minlen: sizeof(optval));
2659
2660 if (error) {
2661 break;
2662 }
2663 if (optval < 0 || optval > 1) {
2664 error = EINVAL;
2665 break;
2666 }
2667
2668 if (tp->t_state != TCPS_CLOSED) {
2669 error = EINVAL;
2670 break;
2671 }
2672 if (optval) {
2673 tp->t_flagsext |= TF_FASTOPEN_FORCE_ENABLE;
2674 } else {
2675 tp->t_flagsext &= ~TF_FASTOPEN_FORCE_ENABLE;
2676 }
2677
2678 break;
2679 case TCP_ENABLE_ECN:
2680 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2681 minlen: sizeof optval);
2682 if (error) {
2683 break;
2684 }
2685 if (optval) {
2686 tp->ecn_flags |= TE_ECN_MODE_ENABLE;
2687 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2688 } else {
2689 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2690 tp->ecn_flags |= TE_ECN_MODE_DISABLE;
2691 }
2692 break;
2693 case TCP_ECN_MODE:
2694 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2695 minlen: sizeof optval);
2696 if (error) {
2697 break;
2698 }
2699 if (optval == ECN_MODE_DEFAULT) {
2700 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2701 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2702 } else if (optval == ECN_MODE_ENABLE) {
2703 tp->ecn_flags |= TE_ECN_MODE_ENABLE;
2704 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2705 } else if (optval == ECN_MODE_DISABLE) {
2706 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2707 tp->ecn_flags |= TE_ECN_MODE_DISABLE;
2708 } else {
2709 error = EINVAL;
2710 }
2711 break;
2712 case TCP_ENABLE_L4S:
2713 error = sooptcopyin(sopt, &optval, len: sizeof optval,
2714 minlen: sizeof optval);
2715 if (error) {
2716 break;
2717 }
2718 if (optval < 0 || optval > 1) {
2719 error = EINVAL;
2720 break;
2721 }
2722 if (tp->t_state != TCPS_CLOSED) {
2723 error = EINVAL;
2724 break;
2725 }
2726 if (optval == 1) {
2727 tp->t_flagsext |= TF_L4S_ENABLED;
2728 tp->t_flagsext &= ~TF_L4S_DISABLED;
2729 } else {
2730 tp->t_flagsext &= ~TF_L4S_ENABLED;
2731 tp->t_flagsext |= TF_L4S_DISABLED;
2732 }
2733 break;
2734 case TCP_NOTIFY_ACKNOWLEDGEMENT:
2735 error = sooptcopyin(sopt, &optval,
2736 len: sizeof(optval), minlen: sizeof(optval));
2737 if (error) {
2738 break;
2739 }
2740 if (optval <= 0) {
2741 error = EINVAL;
2742 break;
2743 }
2744 if (tp->t_notify_ack_count >= TCP_MAX_NOTIFY_ACK) {
2745 error = ETOOMANYREFS;
2746 break;
2747 }
2748
2749 /*
2750 * validate that the given marker id is not
2751 * a duplicate to avoid ambiguity
2752 */
2753 if ((error = tcp_notify_ack_id_valid(tp, so,
2754 optval)) != 0) {
2755 break;
2756 }
2757 error = tcp_add_notify_ack_marker(tp, optval);
2758 break;
2759 case SO_FLUSH:
2760 if ((error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2761 minlen: sizeof(optval))) != 0) {
2762 break;
2763 }
2764
2765 error = inp_flush(inp, optval);
2766 break;
2767
2768 case SO_TRAFFIC_MGT_BACKGROUND:
2769 if ((error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2770 minlen: sizeof(optval))) != 0) {
2771 break;
2772 }
2773
2774 if (optval) {
2775 socket_set_traffic_mgt_flags_locked(so,
2776 TRAFFIC_MGT_SO_BACKGROUND);
2777 } else {
2778 socket_clear_traffic_mgt_flags_locked(so,
2779 TRAFFIC_MGT_SO_BACKGROUND);
2780 }
2781 break;
2782 case TCP_RXT_MINIMUM_TIMEOUT:
2783 error = sooptcopyin(sopt, &optval, len: sizeof(optval),
2784 minlen: sizeof(optval));
2785 if (error) {
2786 break;
2787 }
2788 if (optval < 0) {
2789 error = EINVAL;
2790 break;
2791 }
2792 if (optval == 0) {
2793 tp->t_rxt_minimum_timeout = 0;
2794 } else {
2795 tp->t_rxt_minimum_timeout = min(a: optval,
2796 TCP_RXT_MINIMUM_TIMEOUT_LIMIT);
2797 /* convert to milliseconds */
2798 tp->t_rxt_minimum_timeout *= TCP_RETRANSHZ;
2799 }
2800 break;
2801 default:
2802 error = ENOPROTOOPT;
2803 break;
2804 }
2805 break;
2806
2807 case SOPT_GET:
2808 switch (sopt->sopt_name) {
2809 case TCP_NODELAY:
2810 optval = tp->t_flags & TF_NODELAY;
2811 break;
2812 case TCP_MAXSEG:
2813 optval = tp->t_maxseg;
2814 break;
2815 case TCP_KEEPALIVE:
2816 if (tp->t_keepidle > 0) {
2817 optval = tp->t_keepidle / TCP_RETRANSHZ;
2818 } else {
2819 optval = tcp_keepidle / TCP_RETRANSHZ;
2820 }
2821 break;
2822 case TCP_KEEPINTVL:
2823 if (tp->t_keepintvl > 0) {
2824 optval = tp->t_keepintvl / TCP_RETRANSHZ;
2825 } else {
2826 optval = tcp_keepintvl / TCP_RETRANSHZ;
2827 }
2828 break;
2829 case TCP_KEEPCNT:
2830 if (tp->t_keepcnt > 0) {
2831 optval = tp->t_keepcnt;
2832 } else {
2833 optval = tcp_keepcnt;
2834 }
2835 break;
2836 case TCP_KEEPALIVE_OFFLOAD:
2837 optval = !!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD);
2838 break;
2839 case TCP_NOOPT:
2840 optval = tp->t_flags & TF_NOOPT;
2841 break;
2842 case TCP_NOPUSH:
2843 optval = tp->t_flags & TF_NOPUSH;
2844 break;
2845 case TCP_ENABLE_ECN:
2846 optval = (tp->ecn_flags & TE_ECN_MODE_ENABLE) ? 1 : 0;
2847 break;
2848 case TCP_ECN_MODE:
2849 if (tp->ecn_flags & TE_ECN_MODE_ENABLE) {
2850 optval = ECN_MODE_ENABLE;
2851 } else if (tp->ecn_flags & TE_ECN_MODE_DISABLE) {
2852 optval = ECN_MODE_DISABLE;
2853 } else {
2854 optval = ECN_MODE_DEFAULT;
2855 }
2856 break;
2857 case TCP_ENABLE_L4S:
2858 optval = (tp->t_flagsext & TF_L4S_ENABLED) ? 1 : 0;
2859 break;
2860 case TCP_CONNECTIONTIMEOUT:
2861 optval = tp->t_keepinit / TCP_RETRANSHZ;
2862 break;
2863 case PERSIST_TIMEOUT:
2864 optval = tp->t_persist_timeout / TCP_RETRANSHZ;
2865 break;
2866 case TCP_RXT_CONNDROPTIME:
2867 optval = tp->t_rxt_conndroptime / TCP_RETRANSHZ;
2868 break;
2869 case TCP_RXT_FINDROP:
2870 optval = tp->t_flagsext & TF_RXTFINDROP;
2871 break;
2872 case TCP_NOTIMEWAIT:
2873 optval = (tp->t_flagsext & TF_NOTIMEWAIT) ? 1 : 0;
2874 break;
2875 case TCP_FASTOPEN:
2876 if (tp->t_state != TCPS_LISTEN ||
2877 !(tcp_fastopen & TCP_FASTOPEN_SERVER)) {
2878 error = ENOTSUP;
2879 break;
2880 }
2881 optval = tfo_enabled(tp);
2882 break;
2883 case TCP_FASTOPEN_FORCE_HEURISTICS:
2884 optval = 0;
2885 break;
2886 case TCP_FASTOPEN_FORCE_ENABLE:
2887 optval = (tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) ? 1 : 0;
2888 break;
2889 case TCP_MEASURE_SND_BW:
2890 optval = tp->t_flagsext & TF_MEASURESNDBW;
2891 break;
2892 case TCP_INFO: {
2893 struct tcp_info ti;
2894
2895 tcp_fill_info(tp, ti: &ti);
2896 error = sooptcopyout(sopt, data: &ti, len: sizeof(struct tcp_info));
2897 goto done;
2898 /* NOT REACHED */
2899 }
2900 case TCP_CONNECTION_INFO: {
2901 struct tcp_connection_info tci;
2902 tcp_connection_fill_info(tp, tci: &tci);
2903 error = sooptcopyout(sopt, data: &tci,
2904 len: sizeof(struct tcp_connection_info));
2905 goto done;
2906 }
2907 case TCP_MEASURE_BW_BURST: {
2908 struct tcp_measure_bw_burst out = {};
2909 if ((tp->t_flagsext & TF_MEASURESNDBW) == 0 ||
2910 tp->t_bwmeas == NULL) {
2911 error = EINVAL;
2912 break;
2913 }
2914 out.min_burst_size = tp->t_bwmeas->bw_minsizepkts;
2915 out.max_burst_size = tp->t_bwmeas->bw_maxsizepkts;
2916 error = sooptcopyout(sopt, data: &out, len: sizeof(out));
2917 goto done;
2918 }
2919 case TCP_NOTSENT_LOWAT:
2920 if ((so->so_flags & SOF_NOTSENT_LOWAT) != 0) {
2921 optval = tp->t_notsent_lowat;
2922 } else {
2923 optval = 0;
2924 }
2925 break;
2926 case TCP_SENDMOREACKS:
2927 if (tp->t_flagsext & TF_NOSTRETCHACK) {
2928 optval = 1;
2929 } else {
2930 optval = 0;
2931 }
2932 break;
2933 case TCP_DISABLE_BLACKHOLE_DETECTION:
2934 if (tp->t_flagsext & TF_NOBLACKHOLE_DETECTION) {
2935 optval = 1;
2936 } else {
2937 optval = 0;
2938 }
2939 break;
2940 case TCP_PEER_PID: {
2941 pid_t pid;
2942 error = tcp_lookup_peer_pid_locked(so, out_pid: &pid);
2943 if (error == 0) {
2944 error = sooptcopyout(sopt, data: &pid, len: sizeof(pid));
2945 }
2946 goto done;
2947 }
2948 case TCP_ADAPTIVE_READ_TIMEOUT:
2949 optval = tp->t_adaptive_rtimo;
2950 break;
2951 case TCP_ADAPTIVE_WRITE_TIMEOUT:
2952 optval = tp->t_adaptive_wtimo;
2953 break;
2954 case SO_TRAFFIC_MGT_BACKGROUND:
2955 optval = (so->so_flags1 &
2956 SOF1_TRAFFIC_MGT_SO_BACKGROUND) ? 1 : 0;
2957 break;
2958 case TCP_NOTIFY_ACKNOWLEDGEMENT: {
2959 struct tcp_notify_ack_complete retid;
2960
2961 if (sopt->sopt_valsize != sizeof(retid)) {
2962 error = EINVAL;
2963 break;
2964 }
2965 bzero(s: &retid, n: sizeof(retid));
2966 tcp_get_notify_ack_count(tp, &retid);
2967 if (retid.notify_complete_count > 0) {
2968 tcp_get_notify_ack_ids(tp, &retid);
2969 }
2970
2971 error = sooptcopyout(sopt, data: &retid, len: sizeof(retid));
2972 goto done;
2973 }
2974 case TCP_RXT_MINIMUM_TIMEOUT:
2975 optval = tp->t_rxt_minimum_timeout / TCP_RETRANSHZ;
2976 break;
2977 default:
2978 error = ENOPROTOOPT;
2979 break;
2980 }
2981 if (error == 0) {
2982 error = sooptcopyout(sopt, data: &optval, len: sizeof optval);
2983 }
2984 break;
2985 }
2986done:
2987 return error;
2988}
2989
2990/*
2991 * tcp_sendspace and tcp_recvspace are the default send and receive window
2992 * sizes, respectively. These are obsolescent (this information should
2993 * be set by the route).
2994 */
2995uint32_t tcp_sendspace = 1448 * 256;
2996uint32_t tcp_recvspace = 1448 * 384;
2997
2998/* During attach, the size of socket buffer allocated is limited to
2999 * sb_max in sbreserve. Disallow setting the tcp send and recv space
3000 * to be more than sb_max because that will cause tcp_attach to fail
3001 * (see radar 5713060)
3002 */
3003static int
3004sysctl_tcp_sospace(struct sysctl_oid *oidp, __unused void *arg1,
3005 int arg2, struct sysctl_req *req)
3006{
3007#pragma unused(arg2)
3008 u_int32_t new_value = 0, *space_p = NULL;
3009 int changed = 0, error = 0;
3010
3011 switch (oidp->oid_number) {
3012 case TCPCTL_SENDSPACE:
3013 space_p = &tcp_sendspace;
3014 break;
3015 case TCPCTL_RECVSPACE:
3016 space_p = &tcp_recvspace;
3017 break;
3018 default:
3019 return EINVAL;
3020 }
3021 error = sysctl_io_number(req, bigValue: *space_p, valueSize: sizeof(u_int32_t),
3022 pValue: &new_value, changed: &changed);
3023 if (changed) {
3024 if (new_value > 0 && new_value <= sb_max) {
3025 *space_p = new_value;
3026 SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2, new_value);
3027 } else {
3028 error = ERANGE;
3029 }
3030 }
3031 return error;
3032}
3033
3034#if SYSCTL_SKMEM
3035SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
3036 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &tcp_sendspace,
3037 offsetof(skmem_sysctl, tcp.sendspace), sysctl_tcp_sospace,
3038 "IU", "Maximum outgoing TCP datagram size");
3039SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
3040 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &tcp_recvspace,
3041 offsetof(skmem_sysctl, tcp.recvspace), sysctl_tcp_sospace,
3042 "IU", "Maximum incoming TCP datagram size");
3043#else /* SYSCTL_SKMEM */
3044SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
3045 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN,
3046 &tcp_sendspace, 0, &sysctl_tcp_sospace, "IU", "Maximum outgoing TCP datagram size");
3047SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
3048 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN,
3049 &tcp_recvspace, 0, &sysctl_tcp_sospace, "IU", "Maximum incoming TCP datagram size");
3050#endif /* SYSCTL_SKMEM */
3051
3052/*
3053 * Attach TCP protocol to socket, allocating
3054 * internet protocol control block, tcp control block,
3055 * bufer space, and entering LISTEN state if to accept connections.
3056 *
3057 * Returns: 0 Success
3058 * in_pcballoc:ENOBUFS
3059 * in_pcballoc:ENOMEM
3060 * in_pcballoc:??? [IPSEC specific]
3061 * soreserve:ENOBUFS
3062 */
3063static int
3064tcp_attach(struct socket *so, struct proc *p)
3065{
3066 struct tcpcb *tp;
3067 struct inpcb *inp;
3068 int error;
3069 int isipv6 = SOCK_CHECK_DOM(so, PF_INET6) != 0;
3070
3071 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
3072 error = soreserve(so, sndcc: tcp_sendspace, rcvcc: tcp_recvspace);
3073 if (error) {
3074 return error;
3075 }
3076 }
3077
3078 error = in_pcballoc(so, &tcbinfo, p);
3079 if (error) {
3080 return error;
3081 }
3082
3083 inp = sotoinpcb(so);
3084
3085 if (so->so_snd.sb_preconn_hiwat == 0) {
3086 soreserve_preconnect(so, pre_cc: 2048);
3087 }
3088
3089 if ((so->so_rcv.sb_flags & SB_USRSIZE) == 0) {
3090 so->so_rcv.sb_flags |= SB_AUTOSIZE;
3091 }
3092 if ((so->so_snd.sb_flags & SB_USRSIZE) == 0) {
3093 so->so_snd.sb_flags |= SB_AUTOSIZE;
3094 }
3095
3096 if (isipv6) {
3097 inp->inp_vflag |= INP_IPV6;
3098 inp->in6p_hops = -1; /* use kernel default */
3099 } else {
3100 inp->inp_vflag |= INP_IPV4;
3101 }
3102 tp = tcp_newtcpcb(inp);
3103 if (tp == NULL) {
3104 short nofd = so->so_state & SS_NOFDREF; /* XXX */
3105
3106 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
3107 if (isipv6) {
3108 in6_pcbdetach(inp);
3109 } else {
3110 in_pcbdetach(inp);
3111 }
3112 so->so_state |= nofd;
3113 return ENOBUFS;
3114 }
3115 if (nstat_collect) {
3116 nstat_tcp_new_pcb(inp);
3117 }
3118 TCP_LOG_STATE(tp, TCPS_CLOSED);
3119 tp->t_state = TCPS_CLOSED;
3120 return 0;
3121}
3122
3123/*
3124 * Initiate (or continue) disconnect.
3125 * If embryonic state, just send reset (once).
3126 * If in ``let data drain'' option and linger null, just drop.
3127 * Otherwise (hard), mark socket disconnecting and drop
3128 * current input data; switch states based on user close, and
3129 * send segment to peer (with FIN).
3130 */
3131static struct tcpcb *
3132tcp_disconnect(struct tcpcb *tp)
3133{
3134 struct socket *so = tp->t_inpcb->inp_socket;
3135
3136 if (so->so_rcv.sb_cc != 0 || tp->t_reassqlen != 0 ||
3137 so->so_flags1 & SOF1_DEFUNCTINPROG) {
3138 return tcp_drop(tp, 0);
3139 }
3140
3141 if (tp->t_state < TCPS_ESTABLISHED) {
3142 tp = tcp_close(tp);
3143 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
3144 tp = tcp_drop(tp, 0);
3145 } else {
3146 soisdisconnecting(so);
3147 sbflush(sb: &so->so_rcv);
3148 tp = tcp_usrclosed(tp);
3149#if MPTCP
3150 /* A reset has been sent but socket exists, do not send FIN */
3151 if ((so->so_flags & SOF_MP_SUBFLOW) &&
3152 (tp) && (tp->t_mpflags & TMPF_RESET)) {
3153 return tp;
3154 }
3155#endif
3156 if (tp) {
3157 (void) tcp_output(tp);
3158 }
3159 }
3160 return tp;
3161}
3162
3163/*
3164 * User issued close, and wish to trail through shutdown states:
3165 * if never received SYN, just forget it. If got a SYN from peer,
3166 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3167 * If already got a FIN from peer, then almost done; go to LAST_ACK
3168 * state. In all other cases, have already sent FIN to peer (e.g.
3169 * after PRU_SHUTDOWN), and just have to play tedious game waiting
3170 * for peer to send FIN or not respond to keep-alives, etc.
3171 * We can let the user exit from the close as soon as the FIN is acked.
3172 */
3173static struct tcpcb *
3174tcp_usrclosed(struct tcpcb *tp)
3175{
3176 switch (tp->t_state) {
3177 case TCPS_CLOSED:
3178 case TCPS_LISTEN:
3179 case TCPS_SYN_SENT:
3180 tp = tcp_close(tp);
3181 break;
3182
3183 case TCPS_SYN_RECEIVED:
3184 tp->t_flags |= TF_NEEDFIN;
3185 break;
3186
3187 case TCPS_ESTABLISHED:
3188 DTRACE_TCP4(state__change, void, NULL,
3189 struct inpcb *, tp->t_inpcb,
3190 struct tcpcb *, tp,
3191 int32_t, TCPS_FIN_WAIT_1);
3192 TCP_LOG_STATE(tp, TCPS_FIN_WAIT_1);
3193 tp->t_state = TCPS_FIN_WAIT_1;
3194 TCP_LOG_CONNECTION_SUMMARY(tp);
3195 break;
3196
3197 case TCPS_CLOSE_WAIT:
3198 DTRACE_TCP4(state__change, void, NULL,
3199 struct inpcb *, tp->t_inpcb,
3200 struct tcpcb *, tp,
3201 int32_t, TCPS_LAST_ACK);
3202 TCP_LOG_STATE(tp, TCPS_LAST_ACK);
3203 tp->t_state = TCPS_LAST_ACK;
3204 TCP_LOG_CONNECTION_SUMMARY(tp);
3205 break;
3206 }
3207 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
3208 soisdisconnected(so: tp->t_inpcb->inp_socket);
3209 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
3210 if (tp->t_state == TCPS_FIN_WAIT_2) {
3211 tcp_set_finwait_timeout(tp);
3212 }
3213 }
3214 return tp;
3215}
3216
3217void
3218tcp_in_cksum_stats(u_int32_t len)
3219{
3220 tcpstat.tcps_rcv_swcsum++;
3221 tcpstat.tcps_rcv_swcsum_bytes += len;
3222}
3223
3224void
3225tcp_out_cksum_stats(u_int32_t len)
3226{
3227 tcpstat.tcps_snd_swcsum++;
3228 tcpstat.tcps_snd_swcsum_bytes += len;
3229}
3230
3231void
3232tcp_in6_cksum_stats(u_int32_t len)
3233{
3234 tcpstat.tcps_rcv6_swcsum++;
3235 tcpstat.tcps_rcv6_swcsum_bytes += len;
3236}
3237
3238void
3239tcp_out6_cksum_stats(u_int32_t len)
3240{
3241 tcpstat.tcps_snd6_swcsum++;
3242 tcpstat.tcps_snd6_swcsum_bytes += len;
3243}
3244
3245int
3246tcp_get_mpkl_send_info(struct mbuf *control,
3247 struct so_mpkl_send_info *mpkl_send_info)
3248{
3249 struct cmsghdr *cm;
3250
3251 if (control == NULL || mpkl_send_info == NULL) {
3252 return EINVAL;
3253 }
3254
3255 for (cm = M_FIRST_CMSGHDR(control); cm;
3256 cm = M_NXT_CMSGHDR(control, cm)) {
3257 if (cm->cmsg_len < sizeof(struct cmsghdr) ||
3258 cm->cmsg_len > control->m_len) {
3259 return EINVAL;
3260 }
3261 if (cm->cmsg_level != SOL_SOCKET ||
3262 cm->cmsg_type != SCM_MPKL_SEND_INFO) {
3263 continue;
3264 }
3265 if (cm->cmsg_len != CMSG_LEN(sizeof(struct so_mpkl_send_info))) {
3266 return EINVAL;
3267 }
3268 memcpy(dst: mpkl_send_info, CMSG_DATA(cm),
3269 n: sizeof(struct so_mpkl_send_info));
3270 return 0;
3271 }
3272 return ENOMSG;
3273}
3274
3275/*
3276 * tcp socket options.
3277 *
3278 * The switch statement below does nothing at runtime, as it serves as a
3279 * compile time check to ensure that all of the tcp socket options are
3280 * unique. This works as long as this routine gets updated each time a
3281 * new tcp socket option gets added.
3282 *
3283 * Any failures at compile time indicates duplicated tcp socket option
3284 * values.
3285 */
3286static __attribute__((unused)) void
3287tcpsockopt_cassert(void)
3288{
3289 /*
3290 * This is equivalent to _CASSERT() and the compiler wouldn't
3291 * generate any instructions, thus for compile time only.
3292 */
3293 switch ((int)0) {
3294 case 0:
3295
3296 /* bsd/netinet/tcp.h */
3297 case TCP_NODELAY:
3298 case TCP_MAXSEG:
3299 case TCP_NOPUSH:
3300 case TCP_NOOPT:
3301 case TCP_KEEPALIVE:
3302 case TCP_CONNECTIONTIMEOUT:
3303 case PERSIST_TIMEOUT:
3304 case TCP_RXT_CONNDROPTIME:
3305 case TCP_RXT_FINDROP:
3306 case TCP_KEEPINTVL:
3307 case TCP_KEEPCNT:
3308 case TCP_SENDMOREACKS:
3309 case TCP_ENABLE_ECN:
3310 case TCP_FASTOPEN:
3311 case TCP_CONNECTION_INFO:
3312 case TCP_NOTSENT_LOWAT:
3313
3314 /* bsd/netinet/tcp_private.h */
3315 case TCP_INFO:
3316 case TCP_MEASURE_SND_BW:
3317 case TCP_MEASURE_BW_BURST:
3318 case TCP_PEER_PID:
3319 case TCP_ADAPTIVE_READ_TIMEOUT:
3320 case TCP_OPTION_UNUSED_0:
3321 case TCP_ADAPTIVE_WRITE_TIMEOUT:
3322 case TCP_NOTIMEWAIT:
3323 case TCP_DISABLE_BLACKHOLE_DETECTION:
3324 case TCP_ECN_MODE:
3325 case TCP_KEEPALIVE_OFFLOAD:
3326 ;
3327 }
3328}
3329