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
122 extern char *proc_name_address(void *p);
123
124 errno_t tcp_fill_info_for_info_tuple(struct info_tuple *, struct tcp_info *);
125
126 static int tcp_sysctl_info(struct sysctl_oid *, void *, int, struct sysctl_req *);
127 static void tcp_connection_fill_info(struct tcpcb *tp,
128 struct tcp_connection_info *tci);
129 static int tcp_get_mpkl_send_info(struct mbuf *, struct so_mpkl_send_info *);
130
131 /*
132 * TCP protocol interface to socket abstraction.
133 */
134 static int tcp_attach(struct socket *, struct proc *);
135 static int tcp_connect(struct tcpcb *, struct sockaddr *, struct proc *);
136 static int tcp6_connect(struct tcpcb *, struct sockaddr *, struct proc *);
137 static int tcp6_usr_connect(struct socket *, struct sockaddr *,
138 struct proc *);
139 static struct tcpcb *tcp_disconnect(struct tcpcb *);
140 static struct tcpcb *tcp_usrclosed(struct tcpcb *);
141 extern 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
154 SYSCTL_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
158 int faster_mcopy = 1;
159 SYSCTL_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 */
173 static int
tcp_usr_attach(struct socket * so,__unused int proto,struct proc * p)174 tcp_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);
202 out:
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 */
214 static int
tcp_usr_detach(struct socket * so)215 tcp_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);
236 out:
237 TCPDEBUG2(PRU_DETACH);
238 return error;
239 }
240
241 #if NECP
242 #define COMMON_START_ALLOW_FLOW_DIVERT(allow) TCPDEBUG0; \
243 do { \
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; \
254 do { \
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 */
281 static int
tcp_usr_bind(struct socket * so,struct sockaddr * nam,struct proc * p)282 tcp_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(inp->necp_client_uuid)) {
316 socket_unlock(so, 0);
317 necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
318 socket_lock(so, 0);
319 }
320 #endif /* NECP */
321
322 COMMON_END(PRU_BIND);
323 }
324
325 static int
tcp6_usr_bind(struct socket * so,struct sockaddr * nam,struct proc * p)326 tcp6_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, 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 */
406 static int
tcp_usr_listen(struct socket * so,struct proc * p)407 tcp_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, NSTAT_EVENT_SRC_FLOW_STATE_LISTEN);
422 }
423 }
424 TCP_LOG_LISTEN(tp, error);
425 COMMON_END(PRU_LISTEN);
426 }
427
428 static int
tcp6_usr_listen(struct socket * so,struct proc * p)429 tcp6_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, NSTAT_EVENT_SRC_FLOW_STATE_LISTEN);
448 }
449 }
450 TCP_LOG_LISTEN(tp, error);
451 COMMON_END(PRU_LISTEN);
452 }
453
454 static int
tcp_connect_complete(struct socket * so)455 tcp_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, 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(inp->necp_client_uuid)) {
481 socket_unlock(so, 0);
482 necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp);
483 socket_lock(so, 0);
484 }
485 #endif /* NECP */
486
487 return error;
488 }
489
490 __attribute__((noinline))
491 static void
tcp_log_address_error(int error,struct sockaddr * nam,struct proc * p)492 tcp_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 */
516 static int
tcp_usr_connect_common(struct socket * so,struct tcpcb * tp,struct sockaddr * nam,struct proc * p,bool isipv6,bool need_connect_complete)517 tcp_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, 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 }
603 out:
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 */
621 static int
tcp_usr_connect(struct socket * so,struct sockaddr * nam,struct proc * p)622 tcp_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, 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, 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
672 static int
tcp_usr_connectx_common(struct socket * so,int af,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * auio,user_ssize_t * bytes_written)673 tcp_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, src, 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, 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, 0);
735
736 VERIFY(bytes_written != NULL);
737
738 datalen = uio_resid(auio);
739 error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL,
740 (uio_t)auio, NULL, NULL, 0);
741 socket_lock(so, 0);
742
743 if (error == 0 || error == EWOULDBLOCK) {
744 *bytes_written = datalen - uio_resid(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 }
763 done:
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
772 static int
tcp_usr_connectx(struct socket * so,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * uio,user_ssize_t * bytes_written)773 tcp_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, uio, bytes_written);
780 }
781
782 static int
tcp6_usr_connect(struct socket * so,struct sockaddr * nam,struct proc * p)783 tcp6_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, 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, 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
835 static int
tcp6_usr_connectx(struct socket * so,struct sockaddr * src,struct sockaddr * dst,struct proc * p,uint32_t ifscope,sae_associd_t aid,sae_connid_t * pcid,uint32_t flags,void * arg,uint32_t arglen,struct uio * uio,user_ssize_t * bytes_written)836 tcp6_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, 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 */
856 static int
tcp_usr_disconnect(struct socket * so)857 tcp_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 */
876 static int
tcp_usr_disconnectx(struct socket * so,sae_associd_t aid,sae_connid_t cid)877 tcp_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 */
892 static int
tcp_usr_accept(struct socket * so,struct sockaddr ** nam)893 tcp_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
926 static int
tcp6_usr_accept(struct socket * so,struct sockaddr ** nam)927 tcp6_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 */
976 static int
tcp_usr_shutdown(struct socket * so)977 tcp_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(&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 */
1034 static int
tcp_usr_rcvd(struct socket * so,int flags)1035 tcp_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, &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(&so->so_rcv);
1063 #endif /* CONTENT_FILTER */
1064
1065 COMMON_END(PRU_RCVD);
1066 }
1067
1068 __attribute__((noinline))
1069 static int
tcp_send_implied_connect(struct socket * so,struct tcpcb * tp,struct sockaddr * nam,struct proc * p,bool isipv6)1070 tcp_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);
1087 out:
1088 return error;
1089 }
1090
1091 __attribute__((noinline))
1092 static void
mpkl_tcp_send(struct socket * so,struct tcpcb * tp,uint32_t mpkl_seq,uint32_t mpkl_len,struct so_mpkl_send_info * mpkl_send_info)1093 mpkl_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 */
1149 static int
tcp_usr_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)1150 tcp_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 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD
1164 #if NECP
1165 || (necp_socket_should_use_flow_divert(inp))
1166 #endif /* NECP */
1167 ) {
1168 /*
1169 * OOPS! we lost a race, the TCP session got reset after
1170 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
1171 * network interrupt in the non-splnet() section of sosend().
1172 */
1173 if (m != NULL) {
1174 m_freem(m);
1175 }
1176 if (control != NULL) {
1177 m_freem(control);
1178 control = NULL;
1179 }
1180
1181 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
1182 error = ECONNRESET; /* XXX EPIPE? */
1183 } else {
1184 error = EPROTOTYPE;
1185 }
1186 tp = NULL;
1187 TCPDEBUG1();
1188 goto out;
1189 }
1190 isipv6 = nam && nam->sa_family == AF_INET6 ? true : false;
1191 tp = intotcpcb(inp);
1192 TCPDEBUG1();
1193
1194 calculate_tcp_clock();
1195
1196 if (net_mpklog_enabled) {
1197 mpkl_seq = tp->snd_una + so->so_snd.sb_cc;
1198 if (m) {
1199 mpkl_len = m_length(m);
1200 }
1201 if (so->so_flags1 & SOF1_MPKL_SEND_INFO) {
1202 uuid_copy(mpkl_send_info.mpkl_uuid, so->so_mpkl_send_uuid);
1203 mpkl_send_info.mpkl_proto = so->so_mpkl_send_proto;
1204 }
1205 }
1206
1207 if (control != NULL) {
1208 if (control->m_len > 0 && net_mpklog_enabled) {
1209 error = tcp_get_mpkl_send_info(control, &mpkl_send_info);
1210 /*
1211 * Intepretation of the returned code:
1212 * 0: client wants us to use value passed in SCM_MPKL_SEND_INFO
1213 * 1: SCM_MPKL_SEND_INFO was not present
1214 * other: failure
1215 */
1216 if (error != 0 && error != ENOMSG) {
1217 m_freem(control);
1218 if (m != NULL) {
1219 m_freem(m);
1220 }
1221 control = NULL;
1222 m = NULL;
1223 goto out;
1224 }
1225 }
1226 /*
1227 * Silently drop unsupported ancillary data messages
1228 */
1229 m_freem(control);
1230 control = NULL;
1231 }
1232
1233 /* MPTCP sublow socket buffers must not be compressed */
1234 VERIFY(!(so->so_flags & SOF_MP_SUBFLOW) ||
1235 (so->so_snd.sb_flags & SB_NOCOMPRESS));
1236
1237 if (!(flags & PRUS_OOB) || (so->so_flags1 & SOF1_PRECONNECT_DATA)) {
1238 sbappendstream(&so->so_snd, m);
1239
1240 if (nam && tp->t_state < TCPS_SYN_SENT) {
1241 /*
1242 * Do implied connect if not yet connected,
1243 */
1244 error = tcp_send_implied_connect(so, tp, nam, p, isipv6);
1245 if (error != 0) {
1246 goto out;
1247 }
1248 /* The sequence number of the data is past the SYN */
1249 mpkl_seq = tp->iss + 1;
1250 }
1251
1252 if (flags & PRUS_EOF) {
1253 /*
1254 * Close the send side of the connection after
1255 * the data is sent.
1256 */
1257 socantsendmore(so);
1258 tp = tcp_usrclosed(tp);
1259 }
1260 if (tp != NULL) {
1261 if (flags & PRUS_MORETOCOME) {
1262 tp->t_flags |= TF_MORETOCOME;
1263 }
1264 tp->t_flagsext |= TF_USR_OUTPUT;
1265 error = tcp_output(tp);
1266 tp->t_flagsext &= ~TF_USR_OUTPUT;
1267 if (flags & PRUS_MORETOCOME) {
1268 tp->t_flags &= ~TF_MORETOCOME;
1269 }
1270 }
1271 } else {
1272 if (sbspace(&so->so_snd) == 0) {
1273 /* if no space is left in sockbuf,
1274 * do not try to squeeze in OOB traffic */
1275 m_freem(m);
1276 error = ENOBUFS;
1277 goto out;
1278 }
1279 /*
1280 * According to RFC961 (Assigned Protocols),
1281 * the urgent pointer points to the last octet
1282 * of urgent data. We continue, however,
1283 * to consider it to indicate the first octet
1284 * of data past the urgent section.
1285 * Otherwise, snd_up should be one lower.
1286 */
1287 sbappendstream(&so->so_snd, m);
1288 if (nam && tp->t_state < TCPS_SYN_SENT) {
1289 /*
1290 * Do implied connect if not yet connected,
1291 * initialize window to default value, and
1292 * initialize maxseg/maxopd using peer's cached
1293 * MSS.
1294 */
1295 error = tcp_send_implied_connect(so, tp, nam, p, isipv6);
1296 if (error != 0) {
1297 goto out;
1298 }
1299 }
1300 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
1301 tp->t_flagsext |= TF_FORCE;
1302 tp->t_flagsext |= TF_USR_OUTPUT;
1303 error = tcp_output(tp);
1304 tp->t_flagsext &= ~TF_USR_OUTPUT;
1305 tp->t_flagsext &= ~TF_FORCE;
1306 }
1307
1308 if (net_mpklog_enabled) {
1309 mpkl_tcp_send(so, tp, mpkl_seq, mpkl_len, &mpkl_send_info);
1310 }
1311
1312 /*
1313 * We wait for the socket to successfully connect before returning.
1314 * This allows us to signal a timeout to the application.
1315 */
1316 if (so->so_state & SS_ISCONNECTING) {
1317 if (so->so_state & SS_NBIO) {
1318 error = EWOULDBLOCK;
1319 } else {
1320 error = sbwait(&so->so_snd);
1321 }
1322 }
1323
1324 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
1325 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1326 }
1327
1328 /*
1329 * Abort the TCP.
1330 */
1331 static int
tcp_usr_abort(struct socket * so)1332 tcp_usr_abort(struct socket *so)
1333 {
1334 int error = 0;
1335 struct inpcb *inp = sotoinpcb(so);
1336 struct tcpcb *tp;
1337
1338 COMMON_START();
1339 /* In case we got disconnected from the peer */
1340 if (tp == NULL) {
1341 goto out;
1342 }
1343 tp = tcp_drop(tp, ECONNABORTED);
1344 VERIFY(so->so_usecount > 0);
1345 so->so_usecount--;
1346 COMMON_END(PRU_ABORT);
1347 }
1348
1349 /*
1350 * Receive out-of-band data.
1351 *
1352 * Returns: 0 Success
1353 * EINVAL [COMMON_START]
1354 * EINVAL
1355 * EWOULDBLOCK
1356 */
1357 static int
tcp_usr_rcvoob(struct socket * so,struct mbuf * m,int flags)1358 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
1359 {
1360 int error = 0;
1361 struct inpcb *inp = sotoinpcb(so);
1362 struct tcpcb *tp;
1363
1364 COMMON_START();
1365 if ((so->so_oobmark == 0 &&
1366 (so->so_state & SS_RCVATMARK) == 0) ||
1367 so->so_options & SO_OOBINLINE ||
1368 tp->t_oobflags & TCPOOB_HADDATA) {
1369 error = EINVAL;
1370 goto out;
1371 }
1372 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
1373 error = EWOULDBLOCK;
1374 goto out;
1375 }
1376 m->m_len = 1;
1377 *mtod(m, caddr_t) = tp->t_iobc;
1378 so->so_state &= ~SS_RCVATMARK;
1379 if ((flags & MSG_PEEK) == 0) {
1380 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1381 }
1382 COMMON_END(PRU_RCVOOB);
1383 }
1384
1385 static int
tcp_usr_preconnect(struct socket * so)1386 tcp_usr_preconnect(struct socket *so)
1387 {
1388 struct inpcb *inp = sotoinpcb(so);
1389 int error = 0;
1390
1391 #if NECP
1392 if (necp_socket_should_use_flow_divert(inp)) {
1393 /* May happen, if in tcp_usr_connect we did not had a chance
1394 * to set the usrreqs (due to some error). So, let's get out
1395 * of here.
1396 */
1397 goto out;
1398 }
1399 #endif /* NECP */
1400
1401 error = tcp_output(sototcpcb(so));
1402
1403 soclearfastopen(so);
1404
1405 COMMON_END(PRU_PRECONNECT);
1406 }
1407
1408 /* xxx - should be const */
1409 struct pr_usrreqs tcp_usrreqs = {
1410 .pru_abort = tcp_usr_abort,
1411 .pru_accept = tcp_usr_accept,
1412 .pru_attach = tcp_usr_attach,
1413 .pru_bind = tcp_usr_bind,
1414 .pru_connect = tcp_usr_connect,
1415 .pru_connectx = tcp_usr_connectx,
1416 .pru_control = in_control,
1417 .pru_detach = tcp_usr_detach,
1418 .pru_disconnect = tcp_usr_disconnect,
1419 .pru_disconnectx = tcp_usr_disconnectx,
1420 .pru_listen = tcp_usr_listen,
1421 .pru_peeraddr = in_getpeeraddr,
1422 .pru_rcvd = tcp_usr_rcvd,
1423 .pru_rcvoob = tcp_usr_rcvoob,
1424 .pru_send = tcp_usr_send,
1425 .pru_shutdown = tcp_usr_shutdown,
1426 .pru_sockaddr = in_getsockaddr,
1427 .pru_sosend = sosend,
1428 .pru_soreceive = soreceive,
1429 .pru_preconnect = tcp_usr_preconnect,
1430 };
1431
1432 struct pr_usrreqs tcp6_usrreqs = {
1433 .pru_abort = tcp_usr_abort,
1434 .pru_accept = tcp6_usr_accept,
1435 .pru_attach = tcp_usr_attach,
1436 .pru_bind = tcp6_usr_bind,
1437 .pru_connect = tcp6_usr_connect,
1438 .pru_connectx = tcp6_usr_connectx,
1439 .pru_control = in6_control,
1440 .pru_detach = tcp_usr_detach,
1441 .pru_disconnect = tcp_usr_disconnect,
1442 .pru_disconnectx = tcp_usr_disconnectx,
1443 .pru_listen = tcp6_usr_listen,
1444 .pru_peeraddr = in6_mapped_peeraddr,
1445 .pru_rcvd = tcp_usr_rcvd,
1446 .pru_rcvoob = tcp_usr_rcvoob,
1447 .pru_send = tcp_usr_send,
1448 .pru_shutdown = tcp_usr_shutdown,
1449 .pru_sockaddr = in6_mapped_sockaddr,
1450 .pru_sosend = sosend,
1451 .pru_soreceive = soreceive,
1452 .pru_preconnect = tcp_usr_preconnect,
1453 };
1454
1455 /*
1456 * Common subroutine to open a TCP connection to remote host specified
1457 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1458 * port number if needed. Call in_pcbladdr to do the routing and to choose
1459 * a local host address (interface). If there is an existing incarnation
1460 * of the same connection in TIME-WAIT state and if the remote host was
1461 * sending CC options and if the connection duration was < MSL, then
1462 * truncate the previous TIME-WAIT state and proceed.
1463 * Initialize connection parameters and enter SYN-SENT state.
1464 *
1465 * Returns: 0 Success
1466 * EADDRINUSE
1467 * EINVAL
1468 * in_pcbbind:EADDRNOTAVAIL Address not available.
1469 * in_pcbbind:EINVAL Invalid argument
1470 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1471 * in_pcbbind:EACCES Permission denied
1472 * in_pcbbind:EADDRINUSE Address in use
1473 * in_pcbbind:EAGAIN Resource unavailable, try again
1474 * in_pcbbind:EPERM Operation not permitted
1475 * in_pcbladdr:EINVAL Invalid argument
1476 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1477 * in_pcbladdr:EADDRNOTAVAIL Address not available
1478 */
1479 static int
tcp_connect(struct tcpcb * tp,struct sockaddr * nam,struct proc * p)1480 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p)
1481 {
1482 struct inpcb *inp = tp->t_inpcb, *oinp;
1483 struct socket *so = inp->inp_socket;
1484 struct tcpcb *otp;
1485 struct sockaddr_in *sin = SIN(nam);
1486 struct in_addr laddr;
1487 int error = 0;
1488 struct ifnet *outif = NULL;
1489
1490 if (inp->inp_lport == 0) {
1491 error = in_pcbbind(inp, NULL, p);
1492 if (error) {
1493 goto done;
1494 }
1495 }
1496
1497 /*
1498 * Cannot simply call in_pcbconnect, because there might be an
1499 * earlier incarnation of this same connection still in
1500 * TIME_WAIT state, creating an ADDRINUSE error.
1501 */
1502 error = in_pcbladdr(inp, nam, &laddr, IFSCOPE_NONE, &outif, 0);
1503 if (error) {
1504 goto done;
1505 }
1506
1507 socket_unlock(inp->inp_socket, 0);
1508 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
1509 sin->sin_addr, sin->sin_port,
1510 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr : laddr,
1511 inp->inp_lport, 0, NULL);
1512
1513 socket_lock(inp->inp_socket, 0);
1514 if (oinp) {
1515 if (oinp != inp) { /* 4143933: avoid deadlock if inp == oinp */
1516 socket_lock(oinp->inp_socket, 1);
1517 }
1518 if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) {
1519 if (oinp != inp) {
1520 socket_unlock(oinp->inp_socket, 1);
1521 }
1522 goto skip_oinp;
1523 }
1524
1525 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1526 otp->t_state == TCPS_TIME_WAIT &&
1527 ((int)(tcp_now - otp->t_starttime)) < tcp_msl &&
1528 (otp->t_flags & TF_RCVD_CC)) {
1529 otp = tcp_close(otp);
1530 } else {
1531 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1532 (uint64_t)VM_KERNEL_ADDRPERM(inp));
1533 if (oinp != inp) {
1534 socket_unlock(oinp->inp_socket, 1);
1535 }
1536 error = EADDRINUSE;
1537 goto done;
1538 }
1539 if (oinp != inp) {
1540 socket_unlock(oinp->inp_socket, 1);
1541 }
1542 }
1543 skip_oinp:
1544 if ((inp->inp_laddr.s_addr == INADDR_ANY ? laddr.s_addr :
1545 inp->inp_laddr.s_addr) == sin->sin_addr.s_addr &&
1546 inp->inp_lport == sin->sin_port) {
1547 error = EINVAL;
1548 goto done;
1549 }
1550 #if SKYWALK
1551 if (!NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1552 error = netns_reserve_in(&inp->inp_netns_token,
1553 inp->inp_laddr.s_addr != INADDR_ANY ?
1554 inp->inp_laddr : laddr,
1555 IPPROTO_TCP, inp->inp_lport, NETNS_BSD, NULL);
1556 if (error) {
1557 goto done;
1558 }
1559 }
1560 #endif /* SKYWALK */
1561 if (!lck_rw_try_lock_exclusive(&inp->inp_pcbinfo->ipi_lock)) {
1562 /*lock inversion issue, mostly with udp multicast packets */
1563 socket_unlock(inp->inp_socket, 0);
1564 lck_rw_lock_exclusive(&inp->inp_pcbinfo->ipi_lock);
1565 socket_lock(inp->inp_socket, 0);
1566 }
1567 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1568 inp->inp_laddr = laddr;
1569 /* no reference needed */
1570 inp->inp_last_outifp = outif;
1571 #if SKYWALK
1572 if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1573 netns_set_ifnet(&inp->inp_netns_token, inp->inp_last_outifp);
1574 }
1575 #endif /* SKYWALK */
1576
1577 inp->inp_flags |= INP_INADDR_ANY;
1578 }
1579 inp->inp_faddr = sin->sin_addr;
1580 inp->inp_fport = sin->sin_port;
1581 in_pcbrehash(inp);
1582 lck_rw_done(&inp->inp_pcbinfo->ipi_lock);
1583
1584 if (inp->inp_flowhash == 0) {
1585 inp_calc_flowhash(inp);
1586 ASSERT(inp->inp_flowhash != 0);
1587 }
1588
1589 tcp_set_max_rwinscale(tp, so);
1590
1591 soisconnecting(so);
1592 tcpstat.tcps_connattempt++;
1593 TCP_LOG_STATE(tp, TCPS_SYN_SENT);
1594 tp->t_state = TCPS_SYN_SENT;
1595 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp, TCP_CONN_KEEPINIT(tp));
1596 tp->iss = tcp_new_isn(tp);
1597 tcp_sendseqinit(tp);
1598 tp->t_connect_time = tcp_now;
1599 if (nstat_collect) {
1600 nstat_pcb_event(inp, NSTAT_EVENT_SRC_FLOW_STATE_OUTBOUND);
1601 nstat_route_connect_attempt(inp->inp_route.ro_rt);
1602 }
1603
1604 tcp_add_fsw_flow(tp, outif);
1605
1606 done:
1607 if (outif != NULL) {
1608 ifnet_release(outif);
1609 }
1610
1611 return error;
1612 }
1613
1614 static int
tcp6_connect(struct tcpcb * tp,struct sockaddr * nam,struct proc * p)1615 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct proc *p)
1616 {
1617 struct inpcb *inp = tp->t_inpcb, *oinp;
1618 struct socket *so = inp->inp_socket;
1619 struct tcpcb *otp;
1620 struct sockaddr_in6 *sin6 = SIN6(nam);
1621 struct in6_addr addr6;
1622 int error = 0;
1623 struct ifnet *outif = NULL;
1624
1625 if (inp->inp_lport == 0) {
1626 error = in6_pcbbind(inp, NULL, p);
1627 if (error) {
1628 goto done;
1629 }
1630 }
1631
1632 /*
1633 * Cannot simply call in_pcbconnect, because there might be an
1634 * earlier incarnation of this same connection still in
1635 * TIME_WAIT state, creating an ADDRINUSE error.
1636 *
1637 * in6_pcbladdr() might return an ifp with its reference held
1638 * even in the error case, so make sure that it's released
1639 * whenever it's non-NULL.
1640 */
1641 error = in6_pcbladdr(inp, nam, &addr6, &outif);
1642 if (error) {
1643 goto done;
1644 }
1645 socket_unlock(inp->inp_socket, 0);
1646
1647 uint32_t lifscope = IFSCOPE_NONE;
1648 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1649 lifscope = inp->inp_lifscope;
1650 } else if (sin6->sin6_scope_id != IFSCOPE_NONE) {
1651 lifscope = sin6->sin6_scope_id;
1652 } else if (outif != NULL) {
1653 lifscope = outif->if_index;
1654 }
1655 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1656 &sin6->sin6_addr, sin6->sin6_port, sin6->sin6_scope_id,
1657 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1658 ? &addr6
1659 : &inp->in6p_laddr,
1660 inp->inp_lport, lifscope, 0, NULL);
1661 socket_lock(inp->inp_socket, 0);
1662 if (oinp) {
1663 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1664 otp->t_state == TCPS_TIME_WAIT &&
1665 ((int)(tcp_now - otp->t_starttime)) < tcp_msl &&
1666 (otp->t_flags & TF_RCVD_CC)) {
1667 otp = tcp_close(otp);
1668 } else {
1669 error = EADDRINUSE;
1670 goto done;
1671 }
1672 }
1673 #if SKYWALK
1674 if (!NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1675 error = netns_reserve_in6(&inp->inp_netns_token,
1676 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
1677 addr6 : inp->in6p_laddr,
1678 IPPROTO_TCP, inp->inp_lport, NETNS_BSD, NULL);
1679 if (error) {
1680 goto done;
1681 }
1682 }
1683 #endif /* SKYWALK */
1684 if (!lck_rw_try_lock_exclusive(&inp->inp_pcbinfo->ipi_lock)) {
1685 /*lock inversion issue, mostly with udp multicast packets */
1686 socket_unlock(inp->inp_socket, 0);
1687 lck_rw_lock_exclusive(&inp->inp_pcbinfo->ipi_lock);
1688 socket_lock(inp->inp_socket, 0);
1689 }
1690 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1691 inp->in6p_laddr = addr6;
1692 inp->in6p_last_outifp = outif; /* no reference needed */
1693 inp->inp_lifscope = lifscope;
1694 in6_verify_ifscope(&inp->in6p_laddr, inp->inp_lifscope);
1695 #if SKYWALK
1696 if (NETNS_TOKEN_VALID(&inp->inp_netns_token)) {
1697 netns_set_ifnet(&inp->inp_netns_token, inp->in6p_last_outifp);
1698 }
1699 #endif /* SKYWALK */
1700 inp->in6p_flags |= INP_IN6ADDR_ANY;
1701 }
1702 inp->in6p_faddr = sin6->sin6_addr;
1703 inp->inp_fport = sin6->sin6_port;
1704 inp->inp_fifscope = sin6->sin6_scope_id;
1705 in6_verify_ifscope(&inp->in6p_faddr, inp->inp_fifscope);
1706 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0) {
1707 inp->inp_flow = sin6->sin6_flowinfo;
1708 }
1709 in_pcbrehash(inp);
1710 lck_rw_done(&inp->inp_pcbinfo->ipi_lock);
1711
1712 if (inp->inp_flowhash == 0) {
1713 inp_calc_flowhash(inp);
1714 ASSERT(inp->inp_flowhash != 0);
1715 }
1716 /* update flowinfo - RFC 6437 */
1717 if (inp->inp_flow == 0 && inp->in6p_flags & IN6P_AUTOFLOWLABEL) {
1718 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1719 inp->inp_flow |=
1720 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1721 }
1722
1723 tcp_set_max_rwinscale(tp, so);
1724
1725 soisconnecting(so);
1726 tcpstat.tcps_connattempt++;
1727 TCP_LOG_STATE(tp, TCPS_SYN_SENT);
1728 tp->t_state = TCPS_SYN_SENT;
1729 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
1730 TCP_CONN_KEEPINIT(tp));
1731 tp->iss = tcp_new_isn(tp);
1732 tcp_sendseqinit(tp);
1733 tp->t_connect_time = tcp_now;
1734 if (nstat_collect) {
1735 nstat_pcb_event(inp, NSTAT_EVENT_SRC_FLOW_STATE_OUTBOUND);
1736 nstat_route_connect_attempt(inp->inp_route.ro_rt);
1737 }
1738
1739 tcp_add_fsw_flow(tp, outif);
1740
1741 done:
1742 if (outif != NULL) {
1743 ifnet_release(outif);
1744 }
1745
1746 return error;
1747 }
1748
1749 /*
1750 * Export TCP internal state information via a struct tcp_info
1751 */
1752 void
tcp_fill_info(struct tcpcb * tp,struct tcp_info * ti)1753 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1754 {
1755 struct inpcb *inp = tp->t_inpcb;
1756
1757 bzero(ti, sizeof(*ti));
1758
1759 ti->tcpi_state = (uint8_t)tp->t_state;
1760 ti->tcpi_flowhash = inp != NULL ? inp->inp_flowhash: 0;
1761
1762 if (TSTMP_SUPPORTED(tp)) {
1763 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1764 }
1765 if (SACK_ENABLED(tp)) {
1766 ti->tcpi_options |= TCPI_OPT_SACK;
1767 }
1768 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
1769 ti->tcpi_options |= TCPI_OPT_WSCALE;
1770 ti->tcpi_snd_wscale = tp->snd_scale;
1771 ti->tcpi_rcv_wscale = tp->rcv_scale;
1772 }
1773 if (TCP_ECN_ENABLED(tp)) {
1774 ti->tcpi_options |= TCPI_OPT_ECN;
1775 }
1776
1777 /* Are we in retranmission episode */
1778 if (IN_FASTRECOVERY(tp) || tp->t_rxtshift > 0) {
1779 ti->tcpi_flags |= TCPI_FLAG_LOSSRECOVERY;
1780 }
1781
1782 if (tp->t_flags & TF_STREAMING_ON) {
1783 ti->tcpi_flags |= TCPI_FLAG_STREAMING_ON;
1784 }
1785
1786 ti->tcpi_rto = tp->t_timer[TCPT_REXMT] ? tp->t_rxtcur : 0;
1787 ti->tcpi_snd_mss = tp->t_maxseg;
1788 ti->tcpi_rcv_mss = tp->t_maxseg;
1789
1790 ti->tcpi_rttcur = tp->t_rttcur;
1791 ti->tcpi_srtt = tp->t_srtt >> TCP_RTT_SHIFT;
1792 ti->tcpi_rcv_srtt = tp->rcv_srtt >> TCP_RTT_SHIFT;
1793 ti->tcpi_rttvar = tp->t_rttvar >> TCP_RTTVAR_SHIFT;
1794 ti->tcpi_rttbest = tp->t_rttbest >> TCP_RTT_SHIFT;
1795
1796 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1797 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1798 if (inp != NULL && inp->inp_socket != NULL) {
1799 ti->tcpi_snd_sbbytes = inp->inp_socket->so_snd.sb_cc;
1800 }
1801
1802 ti->tcpi_rcv_space = tp->rcv_adv > tp->rcv_nxt ?
1803 tp->rcv_adv - tp->rcv_nxt : 0;
1804
1805 ti->tcpi_snd_wnd = tp->snd_wnd;
1806 ti->tcpi_snd_nxt = tp->snd_nxt;
1807 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1808
1809 /* convert bytes/msec to bits/sec */
1810 if ((tp->t_flagsext & TF_MEASURESNDBW) != 0 &&
1811 tp->t_bwmeas != NULL) {
1812 ti->tcpi_snd_bw = (tp->t_bwmeas->bw_sndbw * 8000);
1813 }
1814
1815 ti->tcpi_txpackets = inp != NULL ? inp->inp_stat->txpackets : 0;
1816 ti->tcpi_txbytes = inp != NULL ? inp->inp_stat->txbytes : 0;
1817 ti->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes;
1818 ti->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts;
1819 ti->tcpi_txunacked = tp->snd_max - tp->snd_una;
1820
1821 ti->tcpi_rxpackets = inp != NULL ? inp->inp_stat->rxpackets : 0;
1822 ti->tcpi_rxbytes = inp != NULL ? inp->inp_stat->rxbytes : 0;
1823 ti->tcpi_rxduplicatebytes = tp->t_stat.rxduplicatebytes;
1824 ti->tcpi_rxoutoforderbytes = tp->t_stat.rxoutoforderbytes;
1825
1826 if (tp->t_state > TCPS_LISTEN) {
1827 ti->tcpi_synrexmits = (uint8_t)tp->t_stat.rxmitsyns;
1828 }
1829 if (inp != NULL) {
1830 ti->tcpi_cell_rxpackets = inp->inp_cstat->rxpackets;
1831 ti->tcpi_cell_rxbytes = inp->inp_cstat->rxbytes;
1832 ti->tcpi_cell_txpackets = inp->inp_cstat->txpackets;
1833 ti->tcpi_cell_txbytes = inp->inp_cstat->txbytes;
1834
1835 ti->tcpi_wifi_rxpackets = inp->inp_wstat->rxpackets;
1836 ti->tcpi_wifi_rxbytes = inp->inp_wstat->rxbytes;
1837 ti->tcpi_wifi_txpackets = inp->inp_wstat->txpackets;
1838 ti->tcpi_wifi_txbytes = inp->inp_wstat->txbytes;
1839
1840 ti->tcpi_wired_rxpackets = inp->inp_Wstat->rxpackets;
1841 ti->tcpi_wired_rxbytes = inp->inp_Wstat->rxbytes;
1842 ti->tcpi_wired_txpackets = inp->inp_Wstat->txpackets;
1843 ti->tcpi_wired_txbytes = inp->inp_Wstat->txbytes;
1844 }
1845 tcp_get_connectivity_status(tp, &ti->tcpi_connstatus);
1846
1847 ti->tcpi_tfo_syn_data_rcv = !!(tp->t_tfo_stats & TFO_S_SYNDATA_RCV);
1848 ti->tcpi_tfo_cookie_req_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIEREQ_RECV);
1849 ti->tcpi_tfo_cookie_sent = !!(tp->t_tfo_stats & TFO_S_COOKIE_SENT);
1850 ti->tcpi_tfo_cookie_invalid = !!(tp->t_tfo_stats & TFO_S_COOKIE_INVALID);
1851
1852 ti->tcpi_tfo_cookie_req = !!(tp->t_tfo_stats & TFO_S_COOKIE_REQ);
1853 ti->tcpi_tfo_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIE_RCV);
1854 ti->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT);
1855 ti->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED);
1856 ti->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS);
1857 ti->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG);
1858 ti->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV);
1859 ti->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE);
1860 ti->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE);
1861 ti->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE);
1862 ti->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY);
1863
1864 ti->tcpi_ecn_client_setup = !!(tp->ecn_flags & (TE_SETUPSENT | TE_ACE_SETUPSENT));
1865 ti->tcpi_ecn_server_setup = !!(tp->ecn_flags & (TE_SETUPRECEIVED | TE_ACE_SETUPRECEIVED));
1866 ti->tcpi_ecn_success = (TCP_ECN_ENABLED(tp) || TCP_ACC_ECN_ON(tp)) ? 1 : 0;
1867 ti->tcpi_ecn_lost_syn = !!(tp->ecn_flags & TE_LOST_SYN);
1868 ti->tcpi_ecn_lost_synack = !!(tp->ecn_flags & TE_LOST_SYNACK);
1869
1870 ti->tcpi_local_peer = !!(tp->t_flags & TF_LOCAL);
1871
1872 if (inp != NULL && inp->inp_last_outifp != NULL) {
1873 ti->tcpi_last_outif = inp->inp_last_outifp->if_index;
1874
1875 if (IFNET_IS_CELLULAR(inp->inp_last_outifp)) {
1876 ti->tcpi_if_cell = 1;
1877 }
1878 if (IFNET_IS_WIFI(inp->inp_last_outifp)) {
1879 ti->tcpi_if_wifi = 1;
1880 }
1881 if (IFNET_IS_WIRED(inp->inp_last_outifp)) {
1882 ti->tcpi_if_wired = 1;
1883 }
1884 if (IFNET_IS_WIFI_INFRA(inp->inp_last_outifp)) {
1885 ti->tcpi_if_wifi_infra = 1;
1886 }
1887 if (inp->inp_last_outifp->if_eflags & IFEF_AWDL) {
1888 ti->tcpi_if_wifi_awdl = 1;
1889 }
1890 }
1891 if (tp->tcp_cc_index == TCP_CC_ALGO_BACKGROUND_INDEX) {
1892 ti->tcpi_snd_background = 1;
1893 }
1894 if (tcp_recv_bg == 1 || (inp != NULL && inp->inp_socket != NULL &&
1895 IS_TCP_RECV_BG(inp->inp_socket))) {
1896 ti->tcpi_rcv_background = 1;
1897 }
1898
1899 ti->tcpi_ecn_recv_ce = tp->t_ecn_recv_ce;
1900 ti->tcpi_ecn_recv_cwr = tp->t_ecn_recv_cwr;
1901
1902 ti->tcpi_rcvoopack = tp->t_rcvoopack;
1903 ti->tcpi_pawsdrop = tp->t_pawsdrop;
1904 ti->tcpi_sack_recovery_episode = tp->t_sack_recovery_episode;
1905 ti->tcpi_reordered_pkts = tp->t_reordered_pkts;
1906 ti->tcpi_dsack_sent = tp->t_dsack_sent;
1907 ti->tcpi_dsack_recvd = tp->t_dsack_recvd;
1908
1909 ti->tcpi_client_accecn_state = tp->t_client_accecn_state;
1910 ti->tcpi_server_accecn_state = tp->t_server_accecn_state;
1911 ti->tcpi_ecn_capable_packets_sent = tp->t_ecn_capable_packets_sent;
1912 ti->tcpi_ecn_capable_packets_acked = tp->t_ecn_capable_packets_acked;
1913 ti->tcpi_ecn_capable_packets_marked = tp->t_ecn_capable_packets_marked;
1914 ti->tcpi_ecn_capable_packets_lost = tp->t_ecn_capable_packets_lost;
1915
1916 ti->tcpi_flow_control_total_time = inp->inp_fadv_total_time;
1917 ti->tcpi_rcvwnd_limited_total_time = tp->t_rcvwnd_limited_total_time;
1918 }
1919
1920 __private_extern__ errno_t
tcp_fill_info_for_info_tuple(struct info_tuple * itpl,struct tcp_info * ti)1921 tcp_fill_info_for_info_tuple(struct info_tuple *itpl, struct tcp_info *ti)
1922 {
1923 struct inpcbinfo *pcbinfo = NULL;
1924 struct inpcb *inp = NULL;
1925 struct socket *so;
1926 struct tcpcb *tp;
1927
1928 if (itpl->itpl_proto == IPPROTO_TCP) {
1929 pcbinfo = &tcbinfo;
1930 } else {
1931 return EINVAL;
1932 }
1933
1934 if (itpl->itpl_local_sa.sa_family == AF_INET &&
1935 itpl->itpl_remote_sa.sa_family == AF_INET) {
1936 inp = in_pcblookup_hash(pcbinfo,
1937 itpl->itpl_remote_sin.sin_addr,
1938 itpl->itpl_remote_sin.sin_port,
1939 itpl->itpl_local_sin.sin_addr,
1940 itpl->itpl_local_sin.sin_port,
1941 0, NULL);
1942 } else if (itpl->itpl_local_sa.sa_family == AF_INET6 &&
1943 itpl->itpl_remote_sa.sa_family == AF_INET6) {
1944 struct in6_addr ina6_local;
1945 struct in6_addr ina6_remote;
1946
1947 ina6_local = itpl->itpl_local_sin6.sin6_addr;
1948 if (in6_embedded_scope && IN6_IS_SCOPE_LINKLOCAL(&ina6_local) &&
1949 itpl->itpl_local_sin6.sin6_scope_id) {
1950 ina6_local.s6_addr16[1] = htons((uint16_t)itpl->itpl_local_sin6.sin6_scope_id);
1951 }
1952
1953 ina6_remote = itpl->itpl_remote_sin6.sin6_addr;
1954 if (in6_embedded_scope && IN6_IS_SCOPE_LINKLOCAL(&ina6_remote) &&
1955 itpl->itpl_remote_sin6.sin6_scope_id) {
1956 ina6_remote.s6_addr16[1] = htons((uint16_t)itpl->itpl_remote_sin6.sin6_scope_id);
1957 }
1958
1959 inp = in6_pcblookup_hash(pcbinfo,
1960 &ina6_remote,
1961 itpl->itpl_remote_sin6.sin6_port,
1962 itpl->itpl_remote_sin6.sin6_scope_id,
1963 &ina6_local,
1964 itpl->itpl_local_sin6.sin6_port,
1965 itpl->itpl_local_sin6.sin6_scope_id,
1966 0, NULL);
1967 } else {
1968 return EINVAL;
1969 }
1970
1971 if (inp != NULL) {
1972 if ((so = inp->inp_socket) == NULL) {
1973 return ENOENT;
1974 }
1975 socket_lock(so, 0);
1976 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) {
1977 socket_unlock(so, 0);
1978 return ENOENT;
1979 }
1980 tp = intotcpcb(inp);
1981
1982 tcp_fill_info(tp, ti);
1983 socket_unlock(so, 0);
1984
1985 return 0;
1986 }
1987 #if SKYWALK
1988 else {
1989 /* if no pcb found, check for flowswitch for uTCP flow */
1990 int error;
1991 struct nexus_mib_filter nmf = {
1992 .nmf_type = NXMIB_FLOW,
1993 .nmf_bitmap = NXMIB_FILTER_INFO_TUPLE,
1994 .nmf_info_tuple = *itpl,
1995 };
1996 struct sk_stats_flow sf;
1997 size_t len = sizeof(sf);
1998 error = kernel_sysctlbyname(SK_STATS_FLOW, &sf, &len, &nmf, sizeof(nmf));
1999 if (error != 0) {
2000 printf("kernel_sysctlbyname err %d\n", error);
2001 return error;
2002 }
2003 if (len != sizeof(sf)) {
2004 printf("kernel_sysctlbyname invalid len %zu\n", len);
2005 return ENOENT;
2006 }
2007
2008 /*
2009 * This is what flow tracker can offer right now, which is good
2010 * for mDNS TCP keep alive offload.
2011 */
2012 ti->tcpi_snd_nxt = sf.sf_lseq;
2013 ti->tcpi_rcv_nxt = sf.sf_rseq;
2014 ti->tcpi_rcv_space = (uint32_t)(sf.sf_lmax_win << sf.sf_lwscale);
2015 ti->tcpi_rcv_wscale = sf.sf_lwscale;
2016 ti->tcpi_last_outif = (int32_t)sf.sf_if_index;
2017
2018 return 0;
2019 }
2020 #endif /* SKYWALK */
2021
2022 return ENOENT;
2023 }
2024
2025 static void
tcp_connection_fill_info(struct tcpcb * tp,struct tcp_connection_info * tci)2026 tcp_connection_fill_info(struct tcpcb *tp, struct tcp_connection_info *tci)
2027 {
2028 struct inpcb *inp = tp->t_inpcb;
2029
2030 bzero(tci, sizeof(*tci));
2031 tci->tcpi_state = (uint8_t)tp->t_state;
2032
2033 if (TSTMP_SUPPORTED(tp)) {
2034 tci->tcpi_options |= TCPCI_OPT_TIMESTAMPS;
2035 }
2036 if (SACK_ENABLED(tp)) {
2037 tci->tcpi_options |= TCPCI_OPT_SACK;
2038 }
2039 if (TCP_WINDOW_SCALE_ENABLED(tp)) {
2040 tci->tcpi_options |= TCPCI_OPT_WSCALE;
2041 tci->tcpi_snd_wscale = tp->snd_scale;
2042 tci->tcpi_rcv_wscale = tp->rcv_scale;
2043 }
2044 if (TCP_ECN_ENABLED(tp)) {
2045 tci->tcpi_options |= TCPCI_OPT_ECN;
2046 }
2047 if (IN_FASTRECOVERY(tp) || tp->t_rxtshift > 0) {
2048 tci->tcpi_flags |= TCPCI_FLAG_LOSSRECOVERY;
2049 }
2050 if (tp->t_flagsext & TF_PKTS_REORDERED) {
2051 tci->tcpi_flags |= TCPCI_FLAG_REORDERING_DETECTED;
2052 }
2053 tci->tcpi_rto = tp->t_timer[TCPT_REXMT] > 0 ? tp->t_rxtcur : 0;
2054 tci->tcpi_maxseg = tp->t_maxseg;
2055 tci->tcpi_snd_ssthresh = tp->snd_ssthresh;
2056 tci->tcpi_snd_cwnd = tp->snd_cwnd;
2057 tci->tcpi_snd_wnd = tp->snd_wnd;
2058 if (inp != NULL && inp->inp_socket != NULL) {
2059 tci->tcpi_snd_sbbytes = inp->inp_socket->so_snd.sb_cc;
2060 }
2061 tci->tcpi_rcv_wnd = tp->rcv_adv > tp->rcv_nxt ? tp->rcv_adv - tp->rcv_nxt : 0;
2062 tci->tcpi_rttcur = tp->t_rttcur;
2063 tci->tcpi_srtt = (tp->t_srtt >> TCP_RTT_SHIFT);
2064 tci->tcpi_rttvar = (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
2065 tci->tcpi_txpackets = inp != NULL ? inp->inp_stat->txpackets : 0;
2066 tci->tcpi_txbytes = inp != NULL ? inp->inp_stat->txbytes : 0;
2067 tci->tcpi_txretransmitbytes = tp->t_stat.txretransmitbytes;
2068 tci->tcpi_txretransmitpackets = tp->t_stat.rxmitpkts;
2069 tci->tcpi_rxpackets = inp != NULL ? inp->inp_stat->rxpackets : 0;
2070 tci->tcpi_rxbytes = inp != NULL ? inp->inp_stat->rxbytes : 0;
2071 tci->tcpi_rxoutoforderbytes = tp->t_stat.rxoutoforderbytes;
2072
2073 tci->tcpi_tfo_syn_data_rcv = !!(tp->t_tfo_stats & TFO_S_SYNDATA_RCV);
2074 tci->tcpi_tfo_cookie_req_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIEREQ_RECV);
2075 tci->tcpi_tfo_cookie_sent = !!(tp->t_tfo_stats & TFO_S_COOKIE_SENT);
2076 tci->tcpi_tfo_cookie_invalid = !!(tp->t_tfo_stats & TFO_S_COOKIE_INVALID);
2077 tci->tcpi_tfo_cookie_req = !!(tp->t_tfo_stats & TFO_S_COOKIE_REQ);
2078 tci->tcpi_tfo_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_COOKIE_RCV);
2079 tci->tcpi_tfo_syn_data_sent = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_SENT);
2080 tci->tcpi_tfo_syn_data_acked = !!(tp->t_tfo_stats & TFO_S_SYN_DATA_ACKED);
2081 tci->tcpi_tfo_syn_loss = !!(tp->t_tfo_stats & TFO_S_SYN_LOSS);
2082 tci->tcpi_tfo_cookie_wrong = !!(tp->t_tfo_stats & TFO_S_COOKIE_WRONG);
2083 tci->tcpi_tfo_no_cookie_rcv = !!(tp->t_tfo_stats & TFO_S_NO_COOKIE_RCV);
2084 tci->tcpi_tfo_heuristics_disable = !!(tp->t_tfo_stats & TFO_S_HEURISTICS_DISABLE);
2085 tci->tcpi_tfo_send_blackhole = !!(tp->t_tfo_stats & TFO_S_SEND_BLACKHOLE);
2086 tci->tcpi_tfo_recv_blackhole = !!(tp->t_tfo_stats & TFO_S_RECV_BLACKHOLE);
2087 tci->tcpi_tfo_onebyte_proxy = !!(tp->t_tfo_stats & TFO_S_ONE_BYTE_PROXY);
2088 }
2089
2090
2091 __private_extern__ int
tcp_sysctl_info(__unused struct sysctl_oid * oidp,__unused void * arg1,__unused int arg2,struct sysctl_req * req)2092 tcp_sysctl_info(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)
2093 {
2094 int error;
2095 struct tcp_info ti = {};
2096 struct info_tuple itpl;
2097
2098 if (req->newptr == USER_ADDR_NULL) {
2099 return EINVAL;
2100 }
2101 if (req->newlen < sizeof(struct info_tuple)) {
2102 return EINVAL;
2103 }
2104 error = SYSCTL_IN(req, &itpl, sizeof(struct info_tuple));
2105 if (error != 0) {
2106 return error;
2107 }
2108 error = tcp_fill_info_for_info_tuple(&itpl, &ti);
2109 if (error != 0) {
2110 return error;
2111 }
2112 error = SYSCTL_OUT(req, &ti, sizeof(struct tcp_info));
2113 if (error != 0) {
2114 return error;
2115 }
2116
2117 return 0;
2118 }
2119
2120 static int
tcp_lookup_peer_pid_locked(struct socket * so,pid_t * out_pid)2121 tcp_lookup_peer_pid_locked(struct socket *so, pid_t *out_pid)
2122 {
2123 int error = EHOSTUNREACH;
2124 *out_pid = -1;
2125 if ((so->so_state & SS_ISCONNECTED) == 0) {
2126 return ENOTCONN;
2127 }
2128
2129 struct inpcb *inp = (struct inpcb*)so->so_pcb;
2130 uint16_t lport = inp->inp_lport;
2131 uint16_t fport = inp->inp_fport;
2132 uint32_t fifscope = inp->inp_fifscope;
2133 uint32_t lifscope = inp->inp_lifscope;
2134
2135 struct inpcb *finp = NULL;
2136 struct in6_addr laddr6, faddr6;
2137 struct in_addr laddr4, faddr4;
2138
2139 if (inp->inp_vflag & INP_IPV6) {
2140 laddr6 = inp->in6p_laddr;
2141 faddr6 = inp->in6p_faddr;
2142 } else if (inp->inp_vflag & INP_IPV4) {
2143 laddr4 = inp->inp_laddr;
2144 faddr4 = inp->inp_faddr;
2145 }
2146
2147 socket_unlock(so, 0);
2148 if (inp->inp_vflag & INP_IPV6) {
2149 finp = in6_pcblookup_hash(&tcbinfo, &laddr6, lport, lifscope, &faddr6, fport, fifscope, 0, NULL);
2150 } else if (inp->inp_vflag & INP_IPV4) {
2151 finp = in_pcblookup_hash(&tcbinfo, laddr4, lport, faddr4, fport, 0, NULL);
2152 }
2153
2154 if (finp) {
2155 *out_pid = finp->inp_socket->last_pid;
2156 error = 0;
2157 in_pcb_checkstate(finp, WNT_RELEASE, 0);
2158 }
2159 socket_lock(so, 0);
2160
2161 return error;
2162 }
2163
2164 void
tcp_getconninfo(struct socket * so,struct conninfo_tcp * tcp_ci)2165 tcp_getconninfo(struct socket *so, struct conninfo_tcp *tcp_ci)
2166 {
2167 tcp_fill_info(sototcpcb(so), &tcp_ci->tcpci_tcp_info);
2168 }
2169
2170 void
tcp_clear_keep_alive_offload(struct socket * so)2171 tcp_clear_keep_alive_offload(struct socket *so)
2172 {
2173 struct inpcb *inp;
2174 struct ifnet *ifp;
2175
2176 inp = sotoinpcb(so);
2177 if (inp == NULL) {
2178 return;
2179 }
2180
2181 if ((inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD) == 0) {
2182 return;
2183 }
2184
2185 ifp = inp->inp_boundifp != NULL ? inp->inp_boundifp :
2186 inp->inp_last_outifp;
2187 if (ifp == NULL) {
2188 panic("%s: so %p inp %p ifp NULL",
2189 __func__, so, inp);
2190 }
2191
2192 ifnet_lock_exclusive(ifp);
2193
2194 if (ifp->if_tcp_kao_cnt == 0) {
2195 panic("%s: so %p inp %p ifp %p if_tcp_kao_cnt == 0",
2196 __func__, so, inp, ifp);
2197 }
2198 ifp->if_tcp_kao_cnt--;
2199 inp->inp_flags2 &= ~INP2_KEEPALIVE_OFFLOAD;
2200
2201 ifnet_lock_done(ifp);
2202 }
2203
2204 static int
tcp_set_keep_alive_offload(struct socket * so,struct proc * proc)2205 tcp_set_keep_alive_offload(struct socket *so, struct proc *proc)
2206 {
2207 int error = 0;
2208 struct inpcb *inp;
2209 struct ifnet *ifp;
2210
2211 inp = sotoinpcb(so);
2212 if (inp == NULL) {
2213 return ECONNRESET;
2214 }
2215 if ((inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD) != 0) {
2216 return 0;
2217 }
2218
2219 ifp = inp->inp_boundifp != NULL ? inp->inp_boundifp :
2220 inp->inp_last_outifp;
2221 if (ifp == NULL) {
2222 error = ENXIO;
2223 os_log_info(OS_LOG_DEFAULT,
2224 "%s: error %d for proc %s[%u] out ifp is not set\n",
2225 __func__, error,
2226 proc != NULL ? proc->p_comm : "kernel",
2227 proc != NULL ? proc_getpid(proc) : 0);
2228 return ENXIO;
2229 }
2230
2231 error = if_get_tcp_kao_max(ifp);
2232 if (error != 0) {
2233 return error;
2234 }
2235
2236 ifnet_lock_exclusive(ifp);
2237 if (ifp->if_tcp_kao_cnt < ifp->if_tcp_kao_max) {
2238 ifp->if_tcp_kao_cnt++;
2239 inp->inp_flags2 |= INP2_KEEPALIVE_OFFLOAD;
2240 } else {
2241 error = ETOOMANYREFS;
2242 os_log_info(OS_LOG_DEFAULT,
2243 "%s: error %d for proc %s[%u] if_tcp_kao_max %u\n",
2244 __func__, error,
2245 proc != NULL ? proc->p_comm : "kernel",
2246 proc != NULL ? proc_getpid(proc) : 0,
2247 ifp->if_tcp_kao_max);
2248 }
2249 ifnet_lock_done(ifp);
2250
2251 return error;
2252 }
2253
2254 /*
2255 * The new sockopt interface makes it possible for us to block in the
2256 * copyin/out step (if we take a page fault). Taking a page fault at
2257 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
2258 * use TSM, there probably isn't any need for this function to run at
2259 * splnet() any more. This needs more examination.)
2260 */
2261 int
tcp_ctloutput(struct socket * so,struct sockopt * sopt)2262 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
2263 {
2264 int error = 0, opt = 0, optval = 0;
2265 struct inpcb *inp;
2266 struct tcpcb *tp;
2267
2268 inp = sotoinpcb(so);
2269 if (inp == NULL) {
2270 return ECONNRESET;
2271 }
2272 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
2273 if (sopt->sopt_level != IPPROTO_TCP &&
2274 !(sopt->sopt_level == SOL_SOCKET && (sopt->sopt_name == SO_FLUSH ||
2275 sopt->sopt_name == SO_TRAFFIC_MGT_BACKGROUND))) {
2276 if (SOCK_CHECK_DOM(so, PF_INET6)) {
2277 error = ip6_ctloutput(so, sopt);
2278 } else {
2279 error = ip_ctloutput(so, sopt);
2280 }
2281 return error;
2282 }
2283 tp = intotcpcb(inp);
2284 if (tp == NULL) {
2285 return ECONNRESET;
2286 }
2287
2288 calculate_tcp_clock();
2289
2290 switch (sopt->sopt_dir) {
2291 case SOPT_SET:
2292 switch (sopt->sopt_name) {
2293 case TCP_NODELAY:
2294 case TCP_NOOPT:
2295 case TCP_NOPUSH:
2296 error = sooptcopyin(sopt, &optval, sizeof optval,
2297 sizeof optval);
2298 if (error) {
2299 break;
2300 }
2301
2302 switch (sopt->sopt_name) {
2303 case TCP_NODELAY:
2304 opt = TF_NODELAY;
2305 break;
2306 case TCP_NOOPT:
2307 opt = TF_NOOPT;
2308 break;
2309 case TCP_NOPUSH:
2310 opt = TF_NOPUSH;
2311 break;
2312 default:
2313 opt = 0; /* dead code to fool gcc */
2314 break;
2315 }
2316
2317 if (optval) {
2318 tp->t_flags |= opt;
2319 } else {
2320 tp->t_flags &= ~opt;
2321 }
2322 break;
2323 case TCP_RXT_FINDROP:
2324 case TCP_NOTIMEWAIT:
2325 error = sooptcopyin(sopt, &optval, sizeof optval,
2326 sizeof optval);
2327 if (error) {
2328 break;
2329 }
2330 switch (sopt->sopt_name) {
2331 case TCP_RXT_FINDROP:
2332 opt = TF_RXTFINDROP;
2333 break;
2334 case TCP_NOTIMEWAIT:
2335 opt = TF_NOTIMEWAIT;
2336 break;
2337 default:
2338 opt = 0;
2339 break;
2340 }
2341 if (optval) {
2342 tp->t_flagsext |= opt;
2343 } else {
2344 tp->t_flagsext &= ~opt;
2345 }
2346 break;
2347 case TCP_MEASURE_SND_BW:
2348 error = sooptcopyin(sopt, &optval, sizeof optval,
2349 sizeof optval);
2350 if (error) {
2351 break;
2352 }
2353 opt = TF_MEASURESNDBW;
2354 if (optval) {
2355 if (tp->t_bwmeas == NULL) {
2356 tp->t_bwmeas = tcp_bwmeas_alloc(tp);
2357 if (tp->t_bwmeas == NULL) {
2358 error = ENOMEM;
2359 break;
2360 }
2361 }
2362 tp->t_flagsext |= opt;
2363 } else {
2364 tp->t_flagsext &= ~opt;
2365 /* Reset snd bw measurement state */
2366 tp->t_flagsext &= ~(TF_BWMEAS_INPROGRESS);
2367 if (tp->t_bwmeas != NULL) {
2368 tcp_bwmeas_free(tp);
2369 }
2370 }
2371 break;
2372 case TCP_MEASURE_BW_BURST: {
2373 struct tcp_measure_bw_burst in;
2374 uint32_t minpkts, maxpkts;
2375 bzero(&in, sizeof(in));
2376
2377 error = sooptcopyin(sopt, &in, sizeof(in),
2378 sizeof(in));
2379 if (error) {
2380 break;
2381 }
2382 if ((tp->t_flagsext & TF_MEASURESNDBW) == 0 ||
2383 tp->t_bwmeas == NULL) {
2384 error = EINVAL;
2385 break;
2386 }
2387 minpkts = (in.min_burst_size != 0) ? in.min_burst_size :
2388 tp->t_bwmeas->bw_minsizepkts;
2389 maxpkts = (in.max_burst_size != 0) ? in.max_burst_size :
2390 tp->t_bwmeas->bw_maxsizepkts;
2391 if (minpkts > maxpkts) {
2392 error = EINVAL;
2393 break;
2394 }
2395 tp->t_bwmeas->bw_minsizepkts = minpkts;
2396 tp->t_bwmeas->bw_maxsizepkts = maxpkts;
2397 tp->t_bwmeas->bw_minsize = (minpkts * tp->t_maxseg);
2398 tp->t_bwmeas->bw_maxsize = (maxpkts * tp->t_maxseg);
2399 break;
2400 }
2401 case TCP_MAXSEG:
2402 error = sooptcopyin(sopt, &optval, sizeof optval,
2403 sizeof optval);
2404 if (error) {
2405 break;
2406 }
2407
2408 if (optval > 0 && optval <= tp->t_maxseg &&
2409 optval + 40 >= tcp_minmss) {
2410 tp->t_maxseg = optval;
2411 } else {
2412 error = EINVAL;
2413 }
2414 break;
2415
2416 case TCP_KEEPALIVE:
2417 error = sooptcopyin(sopt, &optval, sizeof optval,
2418 sizeof optval);
2419 if (error) {
2420 break;
2421 }
2422 if (optval < 0 || optval > UINT32_MAX / TCP_RETRANSHZ) {
2423 error = EINVAL;
2424 } else {
2425 tp->t_keepidle = optval * TCP_RETRANSHZ;
2426 /* reset the timer to new value */
2427 if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2428 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
2429 TCP_CONN_KEEPIDLE(tp));
2430 tcp_check_timer_state(tp);
2431 }
2432 }
2433 break;
2434
2435 case TCP_CONNECTIONTIMEOUT:
2436 error = sooptcopyin(sopt, &optval, sizeof optval,
2437 sizeof optval);
2438 if (error) {
2439 break;
2440 }
2441 if (optval < 0 || optval > UINT32_MAX / TCP_RETRANSHZ) {
2442 error = EINVAL;
2443 } else {
2444 tp->t_keepinit = optval * TCP_RETRANSHZ;
2445 if (tp->t_state == TCPS_SYN_RECEIVED ||
2446 tp->t_state == TCPS_SYN_SENT) {
2447 tp->t_timer[TCPT_KEEP] = OFFSET_FROM_START(tp,
2448 TCP_CONN_KEEPINIT(tp));
2449 tcp_check_timer_state(tp);
2450 }
2451 }
2452 break;
2453
2454 case TCP_KEEPINTVL:
2455 error = sooptcopyin(sopt, &optval, sizeof(optval),
2456 sizeof(optval));
2457 if (error) {
2458 break;
2459 }
2460 if (optval < 0 || optval > UINT32_MAX / TCP_RETRANSHZ) {
2461 error = EINVAL;
2462 } else {
2463 tp->t_keepintvl = optval * TCP_RETRANSHZ;
2464 if (tp->t_state == TCPS_FIN_WAIT_2 &&
2465 TCP_CONN_MAXIDLE(tp) > 0) {
2466 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
2467 TCP_CONN_MAXIDLE(tp));
2468 tcp_check_timer_state(tp);
2469 }
2470 }
2471 break;
2472
2473 case TCP_KEEPCNT:
2474 error = sooptcopyin(sopt, &optval, sizeof(optval),
2475 sizeof(optval));
2476 if (error) {
2477 break;
2478 }
2479 if (optval < 0 || optval > INT32_MAX) {
2480 error = EINVAL;
2481 } else {
2482 tp->t_keepcnt = optval;
2483 if (tp->t_state == TCPS_FIN_WAIT_2 &&
2484 TCP_CONN_MAXIDLE(tp) > 0) {
2485 tp->t_timer[TCPT_2MSL] = OFFSET_FROM_START(tp,
2486 TCP_CONN_MAXIDLE(tp));
2487 tcp_check_timer_state(tp);
2488 }
2489 }
2490 break;
2491
2492 case TCP_KEEPALIVE_OFFLOAD:
2493 if ((error = priv_check_cred(kauth_cred_get(),
2494 PRIV_NETINET_TCP_KA_OFFLOAD, 0)) != 0) {
2495 break;
2496 }
2497 error = sooptcopyin(sopt, &optval, sizeof(optval),
2498 sizeof(optval));
2499 if (error) {
2500 break;
2501 }
2502 if (optval < 0 || optval > INT32_MAX) {
2503 error = EINVAL;
2504 break;
2505 }
2506 if (optval != 0) {
2507 error = tcp_set_keep_alive_offload(so,
2508 sopt->sopt_p);
2509 } else {
2510 tcp_clear_keep_alive_offload(so);
2511 }
2512 break;
2513
2514 case PERSIST_TIMEOUT:
2515 error = sooptcopyin(sopt, &optval, sizeof optval,
2516 sizeof optval);
2517 if (error) {
2518 break;
2519 }
2520 if (optval < 0) {
2521 error = EINVAL;
2522 } else {
2523 tp->t_persist_timeout = optval * TCP_RETRANSHZ;
2524 }
2525 break;
2526 case TCP_RXT_CONNDROPTIME:
2527 error = sooptcopyin(sopt, &optval, sizeof(optval),
2528 sizeof(optval));
2529 if (error) {
2530 break;
2531 }
2532 if (optval < 0) {
2533 error = EINVAL;
2534 } else {
2535 tp->t_rxt_conndroptime = optval * TCP_RETRANSHZ;
2536 }
2537 break;
2538 case TCP_NOTSENT_LOWAT:
2539 error = sooptcopyin(sopt, &optval, sizeof(optval),
2540 sizeof(optval));
2541 if (error) {
2542 break;
2543 }
2544 if (optval < 0) {
2545 error = EINVAL;
2546 break;
2547 } else {
2548 if (optval == 0) {
2549 so->so_flags &= ~(SOF_NOTSENT_LOWAT);
2550 tp->t_notsent_lowat = 0;
2551 } else {
2552 so->so_flags |= SOF_NOTSENT_LOWAT;
2553 tp->t_notsent_lowat = optval;
2554 }
2555 }
2556 break;
2557 case TCP_ADAPTIVE_READ_TIMEOUT:
2558 error = sooptcopyin(sopt, &optval, sizeof(optval),
2559 sizeof(optval));
2560 if (error) {
2561 break;
2562 }
2563 if (optval < 0 ||
2564 optval > TCP_ADAPTIVE_TIMEOUT_MAX) {
2565 error = EINVAL;
2566 break;
2567 } else if (optval == 0) {
2568 tp->t_adaptive_rtimo = 0;
2569 tcp_keepalive_reset(tp);
2570
2571 if (tp->t_mpsub) {
2572 mptcp_reset_keepalive(tp);
2573 }
2574 } else {
2575 tp->t_adaptive_rtimo = (uint8_t)optval;
2576 }
2577 break;
2578 case TCP_ADAPTIVE_WRITE_TIMEOUT:
2579 error = sooptcopyin(sopt, &optval, sizeof(optval),
2580 sizeof(optval));
2581 if (error) {
2582 break;
2583 }
2584 if (optval < 0 ||
2585 optval > TCP_ADAPTIVE_TIMEOUT_MAX) {
2586 error = EINVAL;
2587 break;
2588 } else {
2589 tp->t_adaptive_wtimo = (uint8_t)optval;
2590 }
2591 break;
2592 case TCP_SENDMOREACKS:
2593 error = sooptcopyin(sopt, &optval, sizeof(optval),
2594 sizeof(optval));
2595 if (error) {
2596 break;
2597 }
2598 if (optval < 0 || optval > 1) {
2599 error = EINVAL;
2600 } else if (optval == 0) {
2601 tp->t_flagsext &= ~(TF_NOSTRETCHACK);
2602 } else {
2603 tp->t_flagsext |= TF_NOSTRETCHACK;
2604 }
2605 break;
2606 case TCP_DISABLE_BLACKHOLE_DETECTION:
2607 error = sooptcopyin(sopt, &optval, sizeof(optval),
2608 sizeof(optval));
2609 if (error) {
2610 break;
2611 }
2612 if (optval < 0 || optval > 1) {
2613 error = EINVAL;
2614 } else if (optval == 0) {
2615 tp->t_flagsext &= ~TF_NOBLACKHOLE_DETECTION;
2616 } else {
2617 tp->t_flagsext |= TF_NOBLACKHOLE_DETECTION;
2618 if ((tp->t_flags & TF_BLACKHOLE) &&
2619 tp->t_pmtud_saved_maxopd > 0) {
2620 tcp_pmtud_revert_segment_size(tp);
2621 }
2622 }
2623 break;
2624 case TCP_FASTOPEN:
2625 if (!(tcp_fastopen & TCP_FASTOPEN_SERVER)) {
2626 error = ENOTSUP;
2627 break;
2628 }
2629
2630 error = sooptcopyin(sopt, &optval, sizeof(optval),
2631 sizeof(optval));
2632 if (error) {
2633 break;
2634 }
2635 if (optval < 0 || optval > 1) {
2636 error = EINVAL;
2637 break;
2638 }
2639 if (tp->t_state != TCPS_LISTEN) {
2640 error = EINVAL;
2641 break;
2642 }
2643 if (optval) {
2644 tp->t_flagsext |= TF_FASTOPEN;
2645 } else {
2646 tcp_disable_tfo(tp);
2647 }
2648 break;
2649 case TCP_FASTOPEN_FORCE_HEURISTICS:
2650
2651 break;
2652 case TCP_FASTOPEN_FORCE_ENABLE:
2653 error = sooptcopyin(sopt, &optval, sizeof(optval),
2654 sizeof(optval));
2655
2656 if (error) {
2657 break;
2658 }
2659 if (optval < 0 || optval > 1) {
2660 error = EINVAL;
2661 break;
2662 }
2663
2664 if (tp->t_state != TCPS_CLOSED) {
2665 error = EINVAL;
2666 break;
2667 }
2668 if (optval) {
2669 tp->t_flagsext |= TF_FASTOPEN_FORCE_ENABLE;
2670 } else {
2671 tp->t_flagsext &= ~TF_FASTOPEN_FORCE_ENABLE;
2672 }
2673
2674 break;
2675 case TCP_ENABLE_ECN:
2676 error = sooptcopyin(sopt, &optval, sizeof optval,
2677 sizeof optval);
2678 if (error) {
2679 break;
2680 }
2681 if (optval) {
2682 tp->ecn_flags |= TE_ECN_MODE_ENABLE;
2683 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2684 } else {
2685 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2686 tp->ecn_flags |= TE_ECN_MODE_DISABLE;
2687 }
2688 break;
2689 case TCP_ECN_MODE:
2690 error = sooptcopyin(sopt, &optval, sizeof optval,
2691 sizeof optval);
2692 if (error) {
2693 break;
2694 }
2695 if (optval == ECN_MODE_DEFAULT) {
2696 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2697 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2698 } else if (optval == ECN_MODE_ENABLE) {
2699 tp->ecn_flags |= TE_ECN_MODE_ENABLE;
2700 tp->ecn_flags &= ~TE_ECN_MODE_DISABLE;
2701 } else if (optval == ECN_MODE_DISABLE) {
2702 tp->ecn_flags &= ~TE_ECN_MODE_ENABLE;
2703 tp->ecn_flags |= TE_ECN_MODE_DISABLE;
2704 } else {
2705 error = EINVAL;
2706 }
2707 break;
2708 case TCP_ENABLE_L4S:
2709 error = sooptcopyin(sopt, &optval, sizeof optval,
2710 sizeof optval);
2711 if (error) {
2712 break;
2713 }
2714 if (optval < 0 || optval > 1) {
2715 error = EINVAL;
2716 break;
2717 }
2718 if (tp->t_state != TCPS_CLOSED) {
2719 error = EINVAL;
2720 break;
2721 }
2722 if (optval == 1) {
2723 tp->t_flagsext |= TF_L4S_ENABLED;
2724 tp->t_flagsext &= ~TF_L4S_DISABLED;
2725 } else {
2726 tp->t_flagsext &= ~TF_L4S_ENABLED;
2727 tp->t_flagsext |= TF_L4S_DISABLED;
2728 }
2729 break;
2730 case TCP_NOTIFY_ACKNOWLEDGEMENT:
2731 error = sooptcopyin(sopt, &optval,
2732 sizeof(optval), sizeof(optval));
2733 if (error) {
2734 break;
2735 }
2736 if (optval <= 0) {
2737 error = EINVAL;
2738 break;
2739 }
2740 if (tp->t_notify_ack_count >= TCP_MAX_NOTIFY_ACK) {
2741 error = ETOOMANYREFS;
2742 break;
2743 }
2744
2745 /*
2746 * validate that the given marker id is not
2747 * a duplicate to avoid ambiguity
2748 */
2749 if ((error = tcp_notify_ack_id_valid(tp, so,
2750 optval)) != 0) {
2751 break;
2752 }
2753 error = tcp_add_notify_ack_marker(tp, optval);
2754 break;
2755 case SO_FLUSH:
2756 if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
2757 sizeof(optval))) != 0) {
2758 break;
2759 }
2760
2761 error = inp_flush(inp, optval);
2762 break;
2763
2764 case SO_TRAFFIC_MGT_BACKGROUND:
2765 if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
2766 sizeof(optval))) != 0) {
2767 break;
2768 }
2769
2770 if (optval) {
2771 socket_set_traffic_mgt_flags_locked(so,
2772 TRAFFIC_MGT_SO_BACKGROUND);
2773 } else {
2774 socket_clear_traffic_mgt_flags_locked(so,
2775 TRAFFIC_MGT_SO_BACKGROUND);
2776 }
2777 break;
2778 case TCP_RXT_MINIMUM_TIMEOUT:
2779 error = sooptcopyin(sopt, &optval, sizeof(optval),
2780 sizeof(optval));
2781 if (error) {
2782 break;
2783 }
2784 if (optval < 0) {
2785 error = EINVAL;
2786 break;
2787 }
2788 if (optval == 0) {
2789 tp->t_rxt_minimum_timeout = 0;
2790 } else {
2791 tp->t_rxt_minimum_timeout = min(optval,
2792 TCP_RXT_MINIMUM_TIMEOUT_LIMIT);
2793 /* convert to milliseconds */
2794 tp->t_rxt_minimum_timeout *= TCP_RETRANSHZ;
2795 }
2796 break;
2797 default:
2798 error = ENOPROTOOPT;
2799 break;
2800 }
2801 break;
2802
2803 case SOPT_GET:
2804 switch (sopt->sopt_name) {
2805 case TCP_NODELAY:
2806 optval = tp->t_flags & TF_NODELAY;
2807 break;
2808 case TCP_MAXSEG:
2809 optval = tp->t_maxseg;
2810 break;
2811 case TCP_KEEPALIVE:
2812 if (tp->t_keepidle > 0) {
2813 optval = tp->t_keepidle / TCP_RETRANSHZ;
2814 } else {
2815 optval = tcp_keepidle / TCP_RETRANSHZ;
2816 }
2817 break;
2818 case TCP_KEEPINTVL:
2819 if (tp->t_keepintvl > 0) {
2820 optval = tp->t_keepintvl / TCP_RETRANSHZ;
2821 } else {
2822 optval = tcp_keepintvl / TCP_RETRANSHZ;
2823 }
2824 break;
2825 case TCP_KEEPCNT:
2826 if (tp->t_keepcnt > 0) {
2827 optval = tp->t_keepcnt;
2828 } else {
2829 optval = tcp_keepcnt;
2830 }
2831 break;
2832 case TCP_KEEPALIVE_OFFLOAD:
2833 optval = !!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD);
2834 break;
2835 case TCP_NOOPT:
2836 optval = tp->t_flags & TF_NOOPT;
2837 break;
2838 case TCP_NOPUSH:
2839 optval = tp->t_flags & TF_NOPUSH;
2840 break;
2841 case TCP_ENABLE_ECN:
2842 optval = (tp->ecn_flags & TE_ECN_MODE_ENABLE) ? 1 : 0;
2843 break;
2844 case TCP_ECN_MODE:
2845 if (tp->ecn_flags & TE_ECN_MODE_ENABLE) {
2846 optval = ECN_MODE_ENABLE;
2847 } else if (tp->ecn_flags & TE_ECN_MODE_DISABLE) {
2848 optval = ECN_MODE_DISABLE;
2849 } else {
2850 optval = ECN_MODE_DEFAULT;
2851 }
2852 break;
2853 case TCP_ENABLE_L4S:
2854 optval = (tp->t_flagsext & TF_L4S_ENABLED) ? 1 : 0;
2855 break;
2856 case TCP_CONNECTIONTIMEOUT:
2857 optval = tp->t_keepinit / TCP_RETRANSHZ;
2858 break;
2859 case PERSIST_TIMEOUT:
2860 optval = tp->t_persist_timeout / TCP_RETRANSHZ;
2861 break;
2862 case TCP_RXT_CONNDROPTIME:
2863 optval = tp->t_rxt_conndroptime / TCP_RETRANSHZ;
2864 break;
2865 case TCP_RXT_FINDROP:
2866 optval = tp->t_flagsext & TF_RXTFINDROP;
2867 break;
2868 case TCP_NOTIMEWAIT:
2869 optval = (tp->t_flagsext & TF_NOTIMEWAIT) ? 1 : 0;
2870 break;
2871 case TCP_FASTOPEN:
2872 if (tp->t_state != TCPS_LISTEN ||
2873 !(tcp_fastopen & TCP_FASTOPEN_SERVER)) {
2874 error = ENOTSUP;
2875 break;
2876 }
2877 optval = tfo_enabled(tp);
2878 break;
2879 case TCP_FASTOPEN_FORCE_HEURISTICS:
2880 optval = 0;
2881 break;
2882 case TCP_FASTOPEN_FORCE_ENABLE:
2883 optval = (tp->t_flagsext & TF_FASTOPEN_FORCE_ENABLE) ? 1 : 0;
2884 break;
2885 case TCP_MEASURE_SND_BW:
2886 optval = tp->t_flagsext & TF_MEASURESNDBW;
2887 break;
2888 case TCP_INFO: {
2889 struct tcp_info ti;
2890
2891 tcp_fill_info(tp, &ti);
2892 error = sooptcopyout(sopt, &ti, sizeof(struct tcp_info));
2893 goto done;
2894 /* NOT REACHED */
2895 }
2896 case TCP_CONNECTION_INFO: {
2897 struct tcp_connection_info tci;
2898 tcp_connection_fill_info(tp, &tci);
2899 error = sooptcopyout(sopt, &tci,
2900 sizeof(struct tcp_connection_info));
2901 goto done;
2902 }
2903 case TCP_MEASURE_BW_BURST: {
2904 struct tcp_measure_bw_burst out = {};
2905 if ((tp->t_flagsext & TF_MEASURESNDBW) == 0 ||
2906 tp->t_bwmeas == NULL) {
2907 error = EINVAL;
2908 break;
2909 }
2910 out.min_burst_size = tp->t_bwmeas->bw_minsizepkts;
2911 out.max_burst_size = tp->t_bwmeas->bw_maxsizepkts;
2912 error = sooptcopyout(sopt, &out, sizeof(out));
2913 goto done;
2914 }
2915 case TCP_NOTSENT_LOWAT:
2916 if ((so->so_flags & SOF_NOTSENT_LOWAT) != 0) {
2917 optval = tp->t_notsent_lowat;
2918 } else {
2919 optval = 0;
2920 }
2921 break;
2922 case TCP_SENDMOREACKS:
2923 if (tp->t_flagsext & TF_NOSTRETCHACK) {
2924 optval = 1;
2925 } else {
2926 optval = 0;
2927 }
2928 break;
2929 case TCP_DISABLE_BLACKHOLE_DETECTION:
2930 if (tp->t_flagsext & TF_NOBLACKHOLE_DETECTION) {
2931 optval = 1;
2932 } else {
2933 optval = 0;
2934 }
2935 break;
2936 case TCP_PEER_PID: {
2937 pid_t pid;
2938 error = tcp_lookup_peer_pid_locked(so, &pid);
2939 if (error == 0) {
2940 error = sooptcopyout(sopt, &pid, sizeof(pid));
2941 }
2942 goto done;
2943 }
2944 case TCP_ADAPTIVE_READ_TIMEOUT:
2945 optval = tp->t_adaptive_rtimo;
2946 break;
2947 case TCP_ADAPTIVE_WRITE_TIMEOUT:
2948 optval = tp->t_adaptive_wtimo;
2949 break;
2950 case SO_TRAFFIC_MGT_BACKGROUND:
2951 optval = (so->so_flags1 &
2952 SOF1_TRAFFIC_MGT_SO_BACKGROUND) ? 1 : 0;
2953 break;
2954 case TCP_NOTIFY_ACKNOWLEDGEMENT: {
2955 struct tcp_notify_ack_complete retid;
2956
2957 if (sopt->sopt_valsize != sizeof(retid)) {
2958 error = EINVAL;
2959 break;
2960 }
2961 bzero(&retid, sizeof(retid));
2962 tcp_get_notify_ack_count(tp, &retid);
2963 if (retid.notify_complete_count > 0) {
2964 tcp_get_notify_ack_ids(tp, &retid);
2965 }
2966
2967 error = sooptcopyout(sopt, &retid, sizeof(retid));
2968 goto done;
2969 }
2970 case TCP_RXT_MINIMUM_TIMEOUT:
2971 optval = tp->t_rxt_minimum_timeout / TCP_RETRANSHZ;
2972 break;
2973 default:
2974 error = ENOPROTOOPT;
2975 break;
2976 }
2977 if (error == 0) {
2978 error = sooptcopyout(sopt, &optval, sizeof optval);
2979 }
2980 break;
2981 }
2982 done:
2983 return error;
2984 }
2985
2986 /*
2987 * tcp_sendspace and tcp_recvspace are the default send and receive window
2988 * sizes, respectively. These are obsolescent (this information should
2989 * be set by the route).
2990 */
2991 uint32_t tcp_sendspace = 1448 * 256;
2992 uint32_t tcp_recvspace = 1448 * 384;
2993
2994 /* During attach, the size of socket buffer allocated is limited to
2995 * sb_max in sbreserve. Disallow setting the tcp send and recv space
2996 * to be more than sb_max because that will cause tcp_attach to fail
2997 * (see radar 5713060)
2998 */
2999 static int
sysctl_tcp_sospace(struct sysctl_oid * oidp,__unused void * arg1,int arg2,struct sysctl_req * req)3000 sysctl_tcp_sospace(struct sysctl_oid *oidp, __unused void *arg1,
3001 int arg2, struct sysctl_req *req)
3002 {
3003 #pragma unused(arg2)
3004 u_int32_t new_value = 0, *space_p = NULL;
3005 int changed = 0, error = 0;
3006
3007 switch (oidp->oid_number) {
3008 case TCPCTL_SENDSPACE:
3009 space_p = &tcp_sendspace;
3010 break;
3011 case TCPCTL_RECVSPACE:
3012 space_p = &tcp_recvspace;
3013 break;
3014 default:
3015 return EINVAL;
3016 }
3017 error = sysctl_io_number(req, *space_p, sizeof(u_int32_t),
3018 &new_value, &changed);
3019 if (changed) {
3020 if (new_value > 0 && new_value <= sb_max) {
3021 *space_p = new_value;
3022 SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2, new_value);
3023 } else {
3024 error = ERANGE;
3025 }
3026 }
3027 return error;
3028 }
3029
3030 #if SYSCTL_SKMEM
3031 SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
3032 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &tcp_sendspace,
3033 offsetof(skmem_sysctl, tcp.sendspace), sysctl_tcp_sospace,
3034 "IU", "Maximum outgoing TCP datagram size");
3035 SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
3036 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN, &tcp_recvspace,
3037 offsetof(skmem_sysctl, tcp.recvspace), sysctl_tcp_sospace,
3038 "IU", "Maximum incoming TCP datagram size");
3039 #else /* SYSCTL_SKMEM */
3040 SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
3041 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN,
3042 &tcp_sendspace, 0, &sysctl_tcp_sospace, "IU", "Maximum outgoing TCP datagram size");
3043 SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
3044 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN,
3045 &tcp_recvspace, 0, &sysctl_tcp_sospace, "IU", "Maximum incoming TCP datagram size");
3046 #endif /* SYSCTL_SKMEM */
3047
3048 /*
3049 * Attach TCP protocol to socket, allocating
3050 * internet protocol control block, tcp control block,
3051 * bufer space, and entering LISTEN state if to accept connections.
3052 *
3053 * Returns: 0 Success
3054 * in_pcballoc:ENOBUFS
3055 * in_pcballoc:ENOMEM
3056 * in_pcballoc:??? [IPSEC specific]
3057 * soreserve:ENOBUFS
3058 */
3059 static int
tcp_attach(struct socket * so,struct proc * p)3060 tcp_attach(struct socket *so, struct proc *p)
3061 {
3062 struct tcpcb *tp;
3063 struct inpcb *inp;
3064 int error;
3065 int isipv6 = SOCK_CHECK_DOM(so, PF_INET6) != 0;
3066
3067 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
3068 error = soreserve(so, tcp_sendspace, tcp_recvspace);
3069 if (error) {
3070 return error;
3071 }
3072 }
3073
3074 error = in_pcballoc(so, &tcbinfo, p);
3075 if (error) {
3076 return error;
3077 }
3078
3079 inp = sotoinpcb(so);
3080
3081 if (so->so_snd.sb_preconn_hiwat == 0) {
3082 soreserve_preconnect(so, 2048);
3083 }
3084
3085 if ((so->so_rcv.sb_flags & SB_USRSIZE) == 0) {
3086 so->so_rcv.sb_flags |= SB_AUTOSIZE;
3087 }
3088 if ((so->so_snd.sb_flags & SB_USRSIZE) == 0) {
3089 so->so_snd.sb_flags |= SB_AUTOSIZE;
3090 }
3091
3092 if (isipv6) {
3093 inp->inp_vflag |= INP_IPV6;
3094 inp->in6p_hops = -1; /* use kernel default */
3095 } else {
3096 inp->inp_vflag |= INP_IPV4;
3097 }
3098 tp = tcp_newtcpcb(inp);
3099 if (tp == NULL) {
3100 short nofd = so->so_state & SS_NOFDREF; /* XXX */
3101
3102 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
3103 if (isipv6) {
3104 in6_pcbdetach(inp);
3105 } else {
3106 in_pcbdetach(inp);
3107 }
3108 so->so_state |= nofd;
3109 return ENOBUFS;
3110 }
3111 if (nstat_collect) {
3112 nstat_tcp_new_pcb(inp);
3113 }
3114 TCP_LOG_STATE(tp, TCPS_CLOSED);
3115 tp->t_state = TCPS_CLOSED;
3116 return 0;
3117 }
3118
3119 /*
3120 * Initiate (or continue) disconnect.
3121 * If embryonic state, just send reset (once).
3122 * If in ``let data drain'' option and linger null, just drop.
3123 * Otherwise (hard), mark socket disconnecting and drop
3124 * current input data; switch states based on user close, and
3125 * send segment to peer (with FIN).
3126 */
3127 static struct tcpcb *
tcp_disconnect(struct tcpcb * tp)3128 tcp_disconnect(struct tcpcb *tp)
3129 {
3130 struct socket *so = tp->t_inpcb->inp_socket;
3131
3132 if (so->so_rcv.sb_cc != 0 || tp->t_reassqlen != 0 ||
3133 so->so_flags1 & SOF1_DEFUNCTINPROG) {
3134 return tcp_drop(tp, 0);
3135 }
3136
3137 if (tp->t_state < TCPS_ESTABLISHED) {
3138 tp = tcp_close(tp);
3139 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
3140 tp = tcp_drop(tp, 0);
3141 } else {
3142 soisdisconnecting(so);
3143 sbflush(&so->so_rcv);
3144 tp = tcp_usrclosed(tp);
3145 #if MPTCP
3146 /* A reset has been sent but socket exists, do not send FIN */
3147 if ((so->so_flags & SOF_MP_SUBFLOW) &&
3148 (tp) && (tp->t_mpflags & TMPF_RESET)) {
3149 return tp;
3150 }
3151 #endif
3152 if (tp) {
3153 (void) tcp_output(tp);
3154 }
3155 }
3156 return tp;
3157 }
3158
3159 /*
3160 * User issued close, and wish to trail through shutdown states:
3161 * if never received SYN, just forget it. If got a SYN from peer,
3162 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
3163 * If already got a FIN from peer, then almost done; go to LAST_ACK
3164 * state. In all other cases, have already sent FIN to peer (e.g.
3165 * after PRU_SHUTDOWN), and just have to play tedious game waiting
3166 * for peer to send FIN or not respond to keep-alives, etc.
3167 * We can let the user exit from the close as soon as the FIN is acked.
3168 */
3169 static struct tcpcb *
tcp_usrclosed(struct tcpcb * tp)3170 tcp_usrclosed(struct tcpcb *tp)
3171 {
3172 switch (tp->t_state) {
3173 case TCPS_CLOSED:
3174 case TCPS_LISTEN:
3175 case TCPS_SYN_SENT:
3176 tp = tcp_close(tp);
3177 break;
3178
3179 case TCPS_SYN_RECEIVED:
3180 tp->t_flags |= TF_NEEDFIN;
3181 break;
3182
3183 case TCPS_ESTABLISHED:
3184 DTRACE_TCP4(state__change, void, NULL,
3185 struct inpcb *, tp->t_inpcb,
3186 struct tcpcb *, tp,
3187 int32_t, TCPS_FIN_WAIT_1);
3188 TCP_LOG_STATE(tp, TCPS_FIN_WAIT_1);
3189 tp->t_state = TCPS_FIN_WAIT_1;
3190 TCP_LOG_CONNECTION_SUMMARY(tp);
3191 break;
3192
3193 case TCPS_CLOSE_WAIT:
3194 DTRACE_TCP4(state__change, void, NULL,
3195 struct inpcb *, tp->t_inpcb,
3196 struct tcpcb *, tp,
3197 int32_t, TCPS_LAST_ACK);
3198 TCP_LOG_STATE(tp, TCPS_LAST_ACK);
3199 tp->t_state = TCPS_LAST_ACK;
3200 TCP_LOG_CONNECTION_SUMMARY(tp);
3201 break;
3202 }
3203 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
3204 soisdisconnected(tp->t_inpcb->inp_socket);
3205 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
3206 if (tp->t_state == TCPS_FIN_WAIT_2) {
3207 tcp_set_finwait_timeout(tp);
3208 }
3209 }
3210 return tp;
3211 }
3212
3213 void
tcp_in_cksum_stats(u_int32_t len)3214 tcp_in_cksum_stats(u_int32_t len)
3215 {
3216 tcpstat.tcps_rcv_swcsum++;
3217 tcpstat.tcps_rcv_swcsum_bytes += len;
3218 }
3219
3220 void
tcp_out_cksum_stats(u_int32_t len)3221 tcp_out_cksum_stats(u_int32_t len)
3222 {
3223 tcpstat.tcps_snd_swcsum++;
3224 tcpstat.tcps_snd_swcsum_bytes += len;
3225 }
3226
3227 void
tcp_in6_cksum_stats(u_int32_t len)3228 tcp_in6_cksum_stats(u_int32_t len)
3229 {
3230 tcpstat.tcps_rcv6_swcsum++;
3231 tcpstat.tcps_rcv6_swcsum_bytes += len;
3232 }
3233
3234 void
tcp_out6_cksum_stats(u_int32_t len)3235 tcp_out6_cksum_stats(u_int32_t len)
3236 {
3237 tcpstat.tcps_snd6_swcsum++;
3238 tcpstat.tcps_snd6_swcsum_bytes += len;
3239 }
3240
3241 int
tcp_get_mpkl_send_info(struct mbuf * control,struct so_mpkl_send_info * mpkl_send_info)3242 tcp_get_mpkl_send_info(struct mbuf *control,
3243 struct so_mpkl_send_info *mpkl_send_info)
3244 {
3245 struct cmsghdr *cm;
3246
3247 if (control == NULL || mpkl_send_info == NULL) {
3248 return EINVAL;
3249 }
3250
3251 for (cm = M_FIRST_CMSGHDR(control); cm;
3252 cm = M_NXT_CMSGHDR(control, cm)) {
3253 if (cm->cmsg_len < sizeof(struct cmsghdr) ||
3254 cm->cmsg_len > control->m_len) {
3255 return EINVAL;
3256 }
3257 if (cm->cmsg_level != SOL_SOCKET ||
3258 cm->cmsg_type != SCM_MPKL_SEND_INFO) {
3259 continue;
3260 }
3261 if (cm->cmsg_len != CMSG_LEN(sizeof(struct so_mpkl_send_info))) {
3262 return EINVAL;
3263 }
3264 memcpy(mpkl_send_info, CMSG_DATA(cm),
3265 sizeof(struct so_mpkl_send_info));
3266 return 0;
3267 }
3268 return ENOMSG;
3269 }
3270
3271 /*
3272 * tcp socket options.
3273 *
3274 * The switch statement below does nothing at runtime, as it serves as a
3275 * compile time check to ensure that all of the tcp socket options are
3276 * unique. This works as long as this routine gets updated each time a
3277 * new tcp socket option gets added.
3278 *
3279 * Any failures at compile time indicates duplicated tcp socket option
3280 * values.
3281 */
3282 static __attribute__((unused)) void
tcpsockopt_cassert(void)3283 tcpsockopt_cassert(void)
3284 {
3285 /*
3286 * This is equivalent to _CASSERT() and the compiler wouldn't
3287 * generate any instructions, thus for compile time only.
3288 */
3289 switch ((int)0) {
3290 case 0:
3291
3292 /* bsd/netinet/tcp.h */
3293 case TCP_NODELAY:
3294 case TCP_MAXSEG:
3295 case TCP_NOPUSH:
3296 case TCP_NOOPT:
3297 case TCP_KEEPALIVE:
3298 case TCP_CONNECTIONTIMEOUT:
3299 case PERSIST_TIMEOUT:
3300 case TCP_RXT_CONNDROPTIME:
3301 case TCP_RXT_FINDROP:
3302 case TCP_KEEPINTVL:
3303 case TCP_KEEPCNT:
3304 case TCP_SENDMOREACKS:
3305 case TCP_ENABLE_ECN:
3306 case TCP_FASTOPEN:
3307 case TCP_CONNECTION_INFO:
3308 case TCP_NOTSENT_LOWAT:
3309
3310 /* bsd/netinet/tcp_private.h */
3311 case TCP_INFO:
3312 case TCP_MEASURE_SND_BW:
3313 case TCP_MEASURE_BW_BURST:
3314 case TCP_PEER_PID:
3315 case TCP_ADAPTIVE_READ_TIMEOUT:
3316 case TCP_OPTION_UNUSED_0:
3317 case TCP_ADAPTIVE_WRITE_TIMEOUT:
3318 case TCP_NOTIMEWAIT:
3319 case TCP_DISABLE_BLACKHOLE_DETECTION:
3320 case TCP_ECN_MODE:
3321 case TCP_KEEPALIVE_OFFLOAD:
3322 ;
3323 }
3324 }
3325