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