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