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