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