2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
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 $
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
71 #include <sys/domain.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/protosw.h>
77 #include <sys/syslog.h>
80 #include <net/route.h>
81 #include <net/ntstat.h>
82 #include <net/content_filter.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
87 #include <netinet/ip6.h>
89 #include <netinet/in_pcb.h>
91 #include <netinet6/in6_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
96 #include <netinet6/ip6_var.h>
98 #include <netinet/tcp.h>
99 #include <netinet/tcp_fsm.h>
100 #include <netinet/tcp_seq.h>
101 #include <netinet/tcp_timer.h>
102 #include <netinet/tcp_var.h>
103 #include <netinet/tcpip.h>
104 #include <mach/sdt.h>
106 #include <netinet/tcp_debug.h>
109 #include <netinet/mptcp_var.h>
113 #include <netinet6/ipsec.h>
117 #include <netinet/flow_divert.h>
118 #endif /* FLOW_DIVERT */
120 void tcp_fill_info(struct tcpcb
*, struct tcp_info
*);
121 errno_t
tcp_fill_info_for_info_tuple(struct info_tuple
*, struct tcp_info
*);
123 int tcp_sysctl_info(struct sysctl_oid
*, void *, int , struct sysctl_req
*);
124 static void tcp_connection_fill_info(struct tcpcb
*tp
,
125 struct tcp_connection_info
*tci
);
128 * TCP protocol interface to socket abstraction.
130 extern char *tcpstates
[]; /* XXX ??? */
132 static int tcp_attach(struct socket
*, struct proc
*);
133 static int tcp_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
135 static int tcp6_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
136 static int tcp6_usr_connect(struct socket
*, struct sockaddr
*,
139 static struct tcpcb
*
140 tcp_disconnect(struct tcpcb
*);
141 static struct tcpcb
*
142 tcp_usrclosed(struct tcpcb
*);
144 extern uint32_t tcp_autorcvbuf_max
;
146 extern void tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sb
);
149 #define TCPDEBUG0 int ostate = 0
150 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
151 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
152 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
156 #define TCPDEBUG2(req)
159 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, info
,
160 CTLFLAG_RW
| CTLFLAG_LOCKED
| CTLFLAG_ANYBODY
| CTLFLAG_KERN
,
161 0 , 0, tcp_sysctl_info
, "S", "TCP info per tuple");
164 * TCP attaches to socket via pru_attach(), reserving space,
165 * and an internet control block.
171 * tcp_attach:??? [IPSEC specific]
174 tcp_usr_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
177 struct inpcb
*inp
= sotoinpcb(so
);
178 struct tcpcb
*tp
= 0;
187 error
= tcp_attach(so
, p
);
191 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
192 so
->so_linger
= TCP_LINGERTIME
* hz
;
195 TCPDEBUG2(PRU_ATTACH
);
200 * pru_detach() detaches the TCP protocol from the socket.
201 * If the protocol state is non-embryonic, then can't
202 * do this directly: have to initiate a pru_disconnect(),
203 * which may finish later; embryonic TCB's can just
207 tcp_usr_detach(struct socket
*so
)
210 struct inpcb
*inp
= sotoinpcb(so
);
214 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
215 return EINVAL
; /* XXX */
217 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
219 /* In case we got disconnected from the peer */
224 calculate_tcp_clock();
226 tp
= tcp_disconnect(tp
);
228 TCPDEBUG2(PRU_DETACH
);
233 #define COMMON_START() TCPDEBUG0; \
235 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
237 if (necp_socket_should_use_flow_divert(inp)) \
238 return (EPROTOTYPE); \
239 tp = intotcpcb(inp); \
241 calculate_tcp_clock(); \
244 #define COMMON_START() TCPDEBUG0; \
246 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
248 tp = intotcpcb(inp); \
250 calculate_tcp_clock(); \
254 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
258 * Give the socket an address.
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
272 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
275 struct inpcb
*inp
= sotoinpcb(so
);
277 struct sockaddr_in
*sinp
;
281 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
282 error
= EAFNOSUPPORT
;
287 * Must check for multicast addresses and disallow binding
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
;
296 error
= in_pcbbind(inp
, nam
, p
);
299 COMMON_END(PRU_BIND
);
305 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
308 struct inpcb
*inp
= sotoinpcb(so
);
310 struct sockaddr_in6
*sin6p
;
314 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
315 error
= EAFNOSUPPORT
;
320 * Must check for multicast addresses and disallow binding
323 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
324 if (sin6p
->sin6_family
== AF_INET6
&&
325 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
326 error
= EAFNOSUPPORT
;
329 inp
->inp_vflag
&= ~INP_IPV4
;
330 inp
->inp_vflag
|= INP_IPV6
;
331 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
332 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
333 inp
->inp_vflag
|= INP_IPV4
;
334 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
335 struct sockaddr_in sin
;
337 in6_sin6_2_sin(&sin
, sin6p
);
338 inp
->inp_vflag
|= INP_IPV4
;
339 inp
->inp_vflag
&= ~INP_IPV6
;
340 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
344 error
= in6_pcbbind(inp
, nam
, p
);
347 COMMON_END(PRU_BIND
);
352 * Prepare to accept connections.
355 * EINVAL [COMMON_START]
356 * in_pcbbind:EADDRNOTAVAIL Address not available.
357 * in_pcbbind:EINVAL Invalid argument
358 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
359 * in_pcbbind:EACCES Permission denied
360 * in_pcbbind:EADDRINUSE Address in use
361 * in_pcbbind:EAGAIN Resource unavailable, try again
362 * in_pcbbind:EPERM Operation not permitted
365 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
368 struct inpcb
*inp
= sotoinpcb(so
);
372 if (inp
->inp_lport
== 0)
373 error
= in_pcbbind(inp
, NULL
, p
);
375 tp
->t_state
= TCPS_LISTEN
;
376 COMMON_END(PRU_LISTEN
);
381 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
384 struct inpcb
*inp
= sotoinpcb(so
);
388 if (inp
->inp_lport
== 0) {
389 inp
->inp_vflag
&= ~INP_IPV4
;
390 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
391 inp
->inp_vflag
|= INP_IPV4
;
392 error
= in6_pcbbind(inp
, NULL
, p
);
395 tp
->t_state
= TCPS_LISTEN
;
396 COMMON_END(PRU_LISTEN
);
401 tcp_connect_complete(struct socket
*so
)
403 struct tcpcb
*tp
= sototcpcb(so
);
406 /* TFO delays the tcp_output until later, when the app calls write() */
407 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
408 if (!necp_socket_is_allowed_to_send_recv(sotoinpcb(so
), NULL
, NULL
))
409 return (EHOSTUNREACH
);
411 /* Initialize enough state so that we can actually send data */
412 tcp_mss(tp
, -1, IFSCOPE_NONE
);
413 tp
->snd_wnd
= tp
->t_maxseg
;
415 error
= tcp_output(tp
);
422 * Initiate connection to peer.
423 * Create a template for use in transmissions on this connection.
424 * Enter SYN_SENT state, and mark socket as connecting.
425 * Start keep-alive timer, and seed output sequence space.
426 * Send initial segment on connection.
429 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
432 struct inpcb
*inp
= sotoinpcb(so
);
434 struct sockaddr_in
*sinp
;
439 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
441 error
= so
->so_error
;
449 else if (necp_socket_should_use_flow_divert(inp
)) {
450 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
451 if (fd_ctl_unit
> 0) {
452 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
454 error
= flow_divert_connect_out(so
, nam
, p
);
460 /* Disable PRECONNECT_DATA, as we don't need to send a SYN anymore. */
461 so
->so_flags1
&= ~SOF1_PRECONNECT_DATA
;
464 #endif /* FLOW_DIVERT */
466 error
= cfil_sock_attach(so
);
469 #endif /* CONTENT_FILTER */
474 calculate_tcp_clock();
476 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
477 error
= EAFNOSUPPORT
;
481 * Must disallow TCP ``connections'' to multicast addresses.
483 sinp
= (struct sockaddr_in
*)(void *)nam
;
484 if (sinp
->sin_family
== AF_INET
485 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
486 error
= EAFNOSUPPORT
;
490 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
493 error
= tcp_connect_complete(so
);
495 COMMON_END(PRU_CONNECT
);
499 tcp_usr_connectx_common(struct socket
*so
, int af
,
500 struct sockaddr_list
**src_sl
, struct sockaddr_list
**dst_sl
,
501 struct proc
*p
, uint32_t ifscope
, sae_associd_t aid
, sae_connid_t
*pcid
,
502 uint32_t flags
, void *arg
, uint32_t arglen
, struct uio
*auio
,
503 user_ssize_t
*bytes_written
)
507 #pragma unused(flags, arg, arglen)
509 struct sockaddr_entry
*src_se
= NULL
, *dst_se
= NULL
;
510 struct inpcb
*inp
= sotoinpcb(so
);
512 user_ssize_t datalen
= 0;
517 VERIFY(dst_sl
!= NULL
);
519 /* select source (if specified) and destination addresses */
520 error
= in_selectaddrs(af
, src_sl
, &src_se
, dst_sl
, &dst_se
);
524 VERIFY(*dst_sl
!= NULL
&& dst_se
!= NULL
);
525 VERIFY(src_se
== NULL
|| *src_sl
!= NULL
);
526 VERIFY(dst_se
->se_addr
->sa_family
== af
);
527 VERIFY(src_se
== NULL
|| src_se
->se_addr
->sa_family
== af
);
530 inp_update_necp_policy(inp
, src_se
? src_se
->se_addr
: NULL
, dst_se
? dst_se
->se_addr
: NULL
, ifscope
);
533 if ((so
->so_flags1
& SOF1_DATA_IDEMPOTENT
) &&
534 (tcp_fastopen
& TCP_FASTOPEN_CLIENT
))
535 sototcpcb(so
)->t_flagsext
|= TF_FASTOPEN
;
538 * We get here for 2 cases:
540 * a. From MPTCP, to connect a subflow. There is no need to
541 * bind the socket to the source address and/or interface,
542 * since everything has been taken care of by MPTCP. We
543 * simply check whether or not this is for the initial
544 * MPTCP connection attempt, or to join an existing one.
546 * b. From the socket layer, to connect a TCP. Perform the
547 * bind to source address and/or interface as necessary.
550 if (flags
& CONNREQF_MPTCP
) {
551 struct mptsub_connreq
*mpcr
= arg
;
553 /* Check to make sure this came down from MPTCP */
554 if (arg
== NULL
|| arglen
!= sizeof (*mpcr
))
557 switch (mpcr
->mpcr_type
) {
558 case MPTSUB_CONNREQ_MP_ENABLE
:
560 case MPTSUB_CONNREQ_MP_ADD
:
568 /* bind socket to the specified interface, if requested */
569 if (ifscope
!= IFSCOPE_NONE
&&
570 (error
= inp_bindif(inp
, ifscope
, NULL
)) != 0)
573 /* if source address and/or port is specified, bind to it */
574 if (src_se
!= NULL
) {
575 struct sockaddr
*sa
= src_se
->se_addr
;
576 error
= sobindlock(so
, sa
, 0); /* already locked */
584 error
= tcp_usr_connect(so
, dst_se
->se_addr
, p
);
588 error
= tcp6_usr_connect(so
, dst_se
->se_addr
, p
);
599 /* if there is data, copy it */
601 socket_unlock(so
, 0);
603 VERIFY(bytes_written
!= NULL
);
605 datalen
= uio_resid(auio
);
606 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, NULL
,
607 (uio_t
)auio
, NULL
, NULL
, 0);
610 if (error
== 0 || error
== EWOULDBLOCK
)
611 *bytes_written
= datalen
- uio_resid(auio
);
614 * sosend returns EWOULDBLOCK if it's a non-blocking
615 * socket or a timeout occured (this allows to return
616 * the amount of queued data through sendit()).
618 * However, connectx() returns EINPROGRESS in case of a
619 * blocking socket. So we change the return value here.
621 if (error
== EWOULDBLOCK
)
625 if (error
== 0 && pcid
!= NULL
)
626 *pcid
= 1; /* there is only one connection in regular TCP */
632 tcp_usr_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
633 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
634 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
635 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
637 return (tcp_usr_connectx_common(so
, AF_INET
, src_sl
, dst_sl
,
638 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
, uio
,
644 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
647 struct inpcb
*inp
= sotoinpcb(so
);
649 struct sockaddr_in6
*sin6p
;
654 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
656 error
= so
->so_error
;
664 else if (necp_socket_should_use_flow_divert(inp
)) {
665 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
666 if (fd_ctl_unit
> 0) {
667 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
669 error
= flow_divert_connect_out(so
, nam
, p
);
675 /* Disable PRECONNECT_DATA, as we don't need to send a SYN anymore. */
676 so
->so_flags1
&= ~SOF1_PRECONNECT_DATA
;
679 #endif /* FLOW_DIVERT */
681 error
= cfil_sock_attach(so
);
684 #endif /* CONTENT_FILTER */
690 calculate_tcp_clock();
692 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
693 error
= EAFNOSUPPORT
;
698 * Must disallow TCP ``connections'' to multicast addresses.
700 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
701 if (sin6p
->sin6_family
== AF_INET6
702 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
703 error
= EAFNOSUPPORT
;
707 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
708 struct sockaddr_in sin
;
710 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
713 in6_sin6_2_sin(&sin
, sin6p
);
714 inp
->inp_vflag
|= INP_IPV4
;
715 inp
->inp_vflag
&= ~INP_IPV6
;
716 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
719 error
= tcp_connect_complete(so
);
722 inp
->inp_vflag
&= ~INP_IPV4
;
723 inp
->inp_vflag
|= INP_IPV6
;
724 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
727 error
= tcp_connect_complete(so
);
728 COMMON_END(PRU_CONNECT
);
732 tcp6_usr_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
733 struct sockaddr_list
**dst_sl
, 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
)
737 return (tcp_usr_connectx_common(so
, AF_INET6
, src_sl
, dst_sl
,
738 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
, uio
,
744 * Initiate disconnect from peer.
745 * If connection never passed embryonic stage, just drop;
746 * else if don't need to let data drain, then can just drop anyways,
747 * else have to begin TCP shutdown process: mark socket disconnecting,
748 * drain unread data, state switch to reflect user close, and
749 * send segment (e.g. FIN) to peer. Socket will be really disconnected
750 * when peer sends FIN and acks ours.
752 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
755 tcp_usr_disconnect(struct socket
*so
)
758 struct inpcb
*inp
= sotoinpcb(so
);
761 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
762 LCK_MTX_ASSERT_OWNED
);
764 /* In case we got disconnected from the peer */
767 tp
= tcp_disconnect(tp
);
768 COMMON_END(PRU_DISCONNECT
);
772 * User-protocol pru_disconnectx callback.
775 tcp_usr_disconnectx(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
778 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
781 return (tcp_usr_disconnect(so
));
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.
790 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
793 struct inpcb
*inp
= sotoinpcb(so
);
794 struct tcpcb
*tp
= NULL
;
797 in_getpeeraddr(so
, nam
);
799 if (so
->so_state
& SS_ISDISCONNECTED
) {
800 error
= ECONNABORTED
;
803 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
806 else if (necp_socket_should_use_flow_divert(inp
))
809 error
= cfil_sock_attach(so
);
812 #endif /* CONTENT_FILTER */
818 calculate_tcp_clock();
820 COMMON_END(PRU_ACCEPT
);
825 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
828 struct inpcb
*inp
= sotoinpcb(so
);
829 struct tcpcb
*tp
= NULL
;
832 if (so
->so_state
& SS_ISDISCONNECTED
) {
833 error
= ECONNABORTED
;
836 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
839 else if (necp_socket_should_use_flow_divert(inp
))
842 error
= cfil_sock_attach(so
);
845 #endif /* CONTENT_FILTER */
851 calculate_tcp_clock();
853 in6_mapped_peeraddr(so
, nam
);
854 COMMON_END(PRU_ACCEPT
);
859 * Mark the connection as being incapable of further output.
862 * EINVAL [COMMON_START]
863 * tcp_output:EADDRNOTAVAIL
865 * tcp_output:EMSGSIZE
866 * tcp_output:EHOSTUNREACH
867 * tcp_output:ENETUNREACH
868 * tcp_output:ENETDOWN
871 * tcp_output:EMSGSIZE
873 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
876 tcp_usr_shutdown(struct socket
*so
)
879 struct inpcb
*inp
= sotoinpcb(so
);
883 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
889 * In case we got disconnected from the peer, or if this is
890 * a socket that is to be flow-diverted (but not yet).
897 || (necp_socket_should_use_flow_divert(inp
))
905 calculate_tcp_clock();
907 tp
= tcp_usrclosed(tp
);
909 /* A reset has been sent but socket exists, do not send FIN */
910 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
911 (tp
) && (tp
->t_mpflags
& TMPF_RESET
)) {
916 /* Don't send a FIN yet */
917 if (tp
&& !(so
->so_state
& SS_ISDISCONNECTED
) &&
918 cfil_sock_data_pending(&so
->so_snd
))
920 #endif /* CONTENT_FILTER */
922 error
= tcp_output(tp
);
923 COMMON_END(PRU_SHUTDOWN
);
927 * After a receive, possibly send window update to peer.
930 tcp_usr_rcvd(struct socket
*so
, __unused
int flags
)
933 struct inpcb
*inp
= sotoinpcb(so
);
937 /* In case we got disconnected from the peer */
940 tcp_sbrcv_trim(tp
, &so
->so_rcv
);
943 * This tcp_output is solely there to trigger window-updates.
944 * However, we really do not want these window-updates while we
945 * are still in SYN_SENT or SYN_RECEIVED.
947 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
951 cfil_sock_buf_update(&so
->so_rcv
);
952 #endif /* CONTENT_FILTER */
954 COMMON_END(PRU_RCVD
);
958 * Do a send by putting data in output queue and updating urgent
959 * marker if URG set. Possibly send more data. Unlike the other
960 * pru_*() routines, the mbuf chains are our responsibility. We
961 * must either enqueue them or free them. The other pru_* routines
962 * generally are caller-frees.
968 * tcp_connect:EADDRINUSE Address in use
969 * tcp_connect:EADDRNOTAVAIL Address not available.
970 * tcp_connect:EINVAL Invalid argument
971 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
972 * tcp_connect:EACCES Permission denied
973 * tcp_connect:EAGAIN Resource unavailable, try again
974 * tcp_connect:EPERM Operation not permitted
975 * tcp_output:EADDRNOTAVAIL
977 * tcp_output:EMSGSIZE
978 * tcp_output:EHOSTUNREACH
979 * tcp_output:ENETUNREACH
980 * tcp_output:ENETDOWN
983 * tcp_output:EMSGSIZE
985 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
986 * tcp6_connect:??? [IPV6 only]
989 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
990 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
993 struct inpcb
*inp
= sotoinpcb(so
);
995 uint32_t msgpri
= MSG_PRI_DEFAULT
;
1001 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
1003 || (necp_socket_should_use_flow_divert(inp
))
1007 * OOPS! we lost a race, the TCP session got reset after
1008 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
1009 * network interrupt in the non-splnet() section of sosend().
1013 if (control
!= NULL
) {
1019 error
= ECONNRESET
; /* XXX EPIPE? */
1027 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
1029 tp
= intotcpcb(inp
);
1032 calculate_tcp_clock();
1034 if (control
!= NULL
) {
1035 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1036 /* Get the msg priority from control mbufs */
1037 error
= tcp_get_msg_priority(control
, &msgpri
);
1048 } else if (control
->m_len
) {
1050 * if not unordered, TCP should not have
1063 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1064 VERIFY(m
->m_flags
& M_PKTHDR
);
1065 m
->m_pkthdr
.msg_pri
= msgpri
;
1068 /* MPTCP sublow socket buffers must not be compressed */
1069 VERIFY(!(so
->so_flags
& SOF_MP_SUBFLOW
) ||
1070 (so
->so_snd
.sb_flags
& SB_NOCOMPRESS
));
1072 if(!(flags
& PRUS_OOB
) || (so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
1073 /* Call msg send if message delivery is enabled */
1074 if (so
->so_flags
& SOF_ENABLE_MSGS
)
1075 sbappendmsg_snd(&so
->so_snd
, m
);
1077 sbappendstream(&so
->so_snd
, m
);
1079 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1082 * Do implied connect if not yet connected,
1083 * initialize window to default value, and
1084 * initialize maxseg/maxopd using peer's cached
1089 error
= tcp6_connect(tp
, nam
, p
);
1092 error
= tcp_connect(tp
, nam
, p
);
1095 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1096 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1099 if (flags
& PRUS_EOF
) {
1101 * Close the send side of the connection after
1105 tp
= tcp_usrclosed(tp
);
1108 if (flags
& PRUS_MORETOCOME
)
1109 tp
->t_flags
|= TF_MORETOCOME
;
1110 error
= tcp_output(tp
);
1111 if (flags
& PRUS_MORETOCOME
)
1112 tp
->t_flags
&= ~TF_MORETOCOME
;
1115 if (sbspace(&so
->so_snd
) == 0) {
1116 /* if no space is left in sockbuf,
1117 * do not try to squeeze in OOB traffic */
1123 * According to RFC961 (Assigned Protocols),
1124 * the urgent pointer points to the last octet
1125 * of urgent data. We continue, however,
1126 * to consider it to indicate the first octet
1127 * of data past the urgent section.
1128 * Otherwise, snd_up should be one lower.
1130 sbappendstream(&so
->so_snd
, m
);
1131 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1133 * Do implied connect if not yet connected,
1134 * initialize window to default value, and
1135 * initialize maxseg/maxopd using peer's cached
1140 error
= tcp6_connect(tp
, nam
, p
);
1143 error
= tcp_connect(tp
, nam
, p
);
1146 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1147 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1149 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
1150 tp
->t_flagsext
|= TF_FORCE
;
1151 error
= tcp_output(tp
);
1152 tp
->t_flagsext
&= ~TF_FORCE
;
1157 * We wait for the socket to successfully connect before returning.
1158 * This allows us to signal a timeout to the application.
1160 if (so
->so_state
& SS_ISCONNECTING
) {
1161 if (so
->so_state
& SS_NBIO
)
1162 error
= EWOULDBLOCK
;
1164 error
= sbwait(&so
->so_snd
);
1167 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
1168 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
1175 tcp_usr_abort(struct socket
*so
)
1178 struct inpcb
*inp
= sotoinpcb(so
);
1182 /* In case we got disconnected from the peer */
1185 tp
= tcp_drop(tp
, ECONNABORTED
);
1187 COMMON_END(PRU_ABORT
);
1191 * Receive out-of-band data.
1193 * Returns: 0 Success
1194 * EINVAL [COMMON_START]
1199 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
1202 struct inpcb
*inp
= sotoinpcb(so
);
1206 if ((so
->so_oobmark
== 0 &&
1207 (so
->so_state
& SS_RCVATMARK
) == 0) ||
1208 so
->so_options
& SO_OOBINLINE
||
1209 tp
->t_oobflags
& TCPOOB_HADDATA
) {
1213 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
1214 error
= EWOULDBLOCK
;
1218 *mtod(m
, caddr_t
) = tp
->t_iobc
;
1219 if ((flags
& MSG_PEEK
) == 0)
1220 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
1221 COMMON_END(PRU_RCVOOB
);
1225 tcp_usr_preconnect(struct socket
*so
)
1227 int error
= tcp_output(sototcpcb(so
));
1229 /* One read has been done. This was enough. Get back to "normal" behavior. */
1230 so
->so_flags1
&= ~SOF1_PRECONNECT_DATA
;
1232 COMMON_END(PRU_PRECONNECT
);
1235 /* xxx - should be const */
1236 struct pr_usrreqs tcp_usrreqs
= {
1237 .pru_abort
= tcp_usr_abort
,
1238 .pru_accept
= tcp_usr_accept
,
1239 .pru_attach
= tcp_usr_attach
,
1240 .pru_bind
= tcp_usr_bind
,
1241 .pru_connect
= tcp_usr_connect
,
1242 .pru_connectx
= tcp_usr_connectx
,
1243 .pru_control
= in_control
,
1244 .pru_detach
= tcp_usr_detach
,
1245 .pru_disconnect
= tcp_usr_disconnect
,
1246 .pru_disconnectx
= tcp_usr_disconnectx
,
1247 .pru_listen
= tcp_usr_listen
,
1248 .pru_peeraddr
= in_getpeeraddr
,
1249 .pru_rcvd
= tcp_usr_rcvd
,
1250 .pru_rcvoob
= tcp_usr_rcvoob
,
1251 .pru_send
= tcp_usr_send
,
1252 .pru_shutdown
= tcp_usr_shutdown
,
1253 .pru_sockaddr
= in_getsockaddr
,
1254 .pru_sosend
= sosend
,
1255 .pru_soreceive
= soreceive
,
1256 .pru_preconnect
= tcp_usr_preconnect
,
1260 struct pr_usrreqs tcp6_usrreqs
= {
1261 .pru_abort
= tcp_usr_abort
,
1262 .pru_accept
= tcp6_usr_accept
,
1263 .pru_attach
= tcp_usr_attach
,
1264 .pru_bind
= tcp6_usr_bind
,
1265 .pru_connect
= tcp6_usr_connect
,
1266 .pru_connectx
= tcp6_usr_connectx
,
1267 .pru_control
= in6_control
,
1268 .pru_detach
= tcp_usr_detach
,
1269 .pru_disconnect
= tcp_usr_disconnect
,
1270 .pru_disconnectx
= tcp_usr_disconnectx
,
1271 .pru_listen
= tcp6_usr_listen
,
1272 .pru_peeraddr
= in6_mapped_peeraddr
,
1273 .pru_rcvd
= tcp_usr_rcvd
,
1274 .pru_rcvoob
= tcp_usr_rcvoob
,
1275 .pru_send
= tcp_usr_send
,
1276 .pru_shutdown
= tcp_usr_shutdown
,
1277 .pru_sockaddr
= in6_mapped_sockaddr
,
1278 .pru_sosend
= sosend
,
1279 .pru_soreceive
= soreceive
,
1280 .pru_preconnect
= tcp_usr_preconnect
,
1285 * Common subroutine to open a TCP connection to remote host specified
1286 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1287 * port number if needed. Call in_pcbladdr to do the routing and to choose
1288 * a local host address (interface). If there is an existing incarnation
1289 * of the same connection in TIME-WAIT state and if the remote host was
1290 * sending CC options and if the connection duration was < MSL, then
1291 * truncate the previous TIME-WAIT state and proceed.
1292 * Initialize connection parameters and enter SYN-SENT state.
1294 * Returns: 0 Success
1297 * in_pcbbind:EADDRNOTAVAIL Address not available.
1298 * in_pcbbind:EINVAL Invalid argument
1299 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1300 * in_pcbbind:EACCES Permission denied
1301 * in_pcbbind:EADDRINUSE Address in use
1302 * in_pcbbind:EAGAIN Resource unavailable, try again
1303 * in_pcbbind:EPERM Operation not permitted
1304 * in_pcbladdr:EINVAL Invalid argument
1305 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1306 * in_pcbladdr:EADDRNOTAVAIL Address not available
1309 tcp_connect(tp
, nam
, p
)
1310 register struct tcpcb
*tp
;
1311 struct sockaddr
*nam
;
1314 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1315 struct socket
*so
= inp
->inp_socket
;
1317 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)nam
;
1318 struct in_addr laddr
;
1319 struct rmxp_tao
*taop
;
1320 struct rmxp_tao tao_noncached
;
1322 struct ifnet
*outif
= NULL
;
1324 if (inp
->inp_lport
== 0) {
1325 error
= in_pcbbind(inp
, NULL
, p
);
1331 * Cannot simply call in_pcbconnect, because there might be an
1332 * earlier incarnation of this same connection still in
1333 * TIME_WAIT state, creating an ADDRINUSE error.
1335 error
= in_pcbladdr(inp
, nam
, &laddr
, IFSCOPE_NONE
, &outif
);
1339 tcp_unlock(inp
->inp_socket
, 0, 0);
1340 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
1341 sin
->sin_addr
, sin
->sin_port
,
1342 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
: laddr
,
1343 inp
->inp_lport
, 0, NULL
);
1345 tcp_lock(inp
->inp_socket
, 0, 0);
1347 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
1348 tcp_lock(oinp
->inp_socket
, 1, 0);
1349 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1351 tcp_unlock(oinp
->inp_socket
, 1, 0);
1355 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1356 otp
->t_state
== TCPS_TIME_WAIT
&&
1357 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1358 (otp
->t_flags
& TF_RCVD_CC
)) {
1359 otp
= tcp_close(otp
);
1361 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1362 (uint64_t)VM_KERNEL_ADDRPERM(inp
));
1364 tcp_unlock(oinp
->inp_socket
, 1, 0);
1369 tcp_unlock(oinp
->inp_socket
, 1, 0);
1372 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? laddr
.s_addr
:
1373 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
1374 inp
->inp_lport
== sin
->sin_port
) {
1378 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1379 /*lock inversion issue, mostly with udp multicast packets */
1380 socket_unlock(inp
->inp_socket
, 0);
1381 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1382 socket_lock(inp
->inp_socket
, 0);
1384 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1385 inp
->inp_laddr
= laddr
;
1386 /* no reference needed */
1387 inp
->inp_last_outifp
= outif
;
1388 inp
->inp_flags
|= INP_INADDR_ANY
;
1390 inp
->inp_faddr
= sin
->sin_addr
;
1391 inp
->inp_fport
= sin
->sin_port
;
1393 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1395 if (inp
->inp_flowhash
== 0)
1396 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1398 tcp_set_max_rwinscale(tp
, so
);
1401 tcpstat
.tcps_connattempt
++;
1402 tp
->t_state
= TCPS_SYN_SENT
;
1403 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, TCP_CONN_KEEPINIT(tp
));
1404 tp
->iss
= tcp_new_isn(tp
);
1405 tcp_sendseqinit(tp
);
1407 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1410 * Generate a CC value for this connection and
1411 * check whether CC or CCnew should be used.
1413 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
1414 taop
= &tao_noncached
;
1415 bzero(taop
, sizeof(*taop
));
1418 tp
->cc_send
= CC_INC(tcp_ccgen
);
1419 if (taop
->tao_ccsent
!= 0 &&
1420 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1421 taop
->tao_ccsent
= tp
->cc_send
;
1423 taop
->tao_ccsent
= 0;
1424 tp
->t_flags
|= TF_SENDCCNEW
;
1429 ifnet_release(outif
);
1436 tcp6_connect(tp
, nam
, p
)
1437 register struct tcpcb
*tp
;
1438 struct sockaddr
*nam
;
1441 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1442 struct socket
*so
= inp
->inp_socket
;
1444 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)nam
;
1445 struct in6_addr addr6
;
1446 struct rmxp_tao
*taop
;
1447 struct rmxp_tao tao_noncached
;
1449 struct ifnet
*outif
= NULL
;
1451 if (inp
->inp_lport
== 0) {
1452 error
= in6_pcbbind(inp
, NULL
, p
);
1458 * Cannot simply call in_pcbconnect, because there might be an
1459 * earlier incarnation of this same connection still in
1460 * TIME_WAIT state, creating an ADDRINUSE error.
1462 * in6_pcbladdr() might return an ifp with its reference held
1463 * even in the error case, so make sure that it's released
1464 * whenever it's non-NULL.
1466 error
= in6_pcbladdr(inp
, nam
, &addr6
, &outif
);
1469 tcp_unlock(inp
->inp_socket
, 0, 0);
1470 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1471 &sin6
->sin6_addr
, sin6
->sin6_port
,
1472 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1475 inp
->inp_lport
, 0, NULL
);
1476 tcp_lock(inp
->inp_socket
, 0, 0);
1478 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1479 otp
->t_state
== TCPS_TIME_WAIT
&&
1480 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1481 (otp
->t_flags
& TF_RCVD_CC
)) {
1482 otp
= tcp_close(otp
);
1488 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1489 /*lock inversion issue, mostly with udp multicast packets */
1490 socket_unlock(inp
->inp_socket
, 0);
1491 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1492 socket_lock(inp
->inp_socket
, 0);
1494 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
1495 inp
->in6p_laddr
= addr6
;
1496 inp
->in6p_last_outifp
= outif
; /* no reference needed */
1497 inp
->in6p_flags
|= INP_IN6ADDR_ANY
;
1499 inp
->in6p_faddr
= sin6
->sin6_addr
;
1500 inp
->inp_fport
= sin6
->sin6_port
;
1501 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1502 inp
->inp_flow
= sin6
->sin6_flowinfo
;
1504 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1506 if (inp
->inp_flowhash
== 0)
1507 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1508 /* update flowinfo - RFC 6437 */
1509 if (inp
->inp_flow
== 0 && inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
1510 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
1512 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
1515 tcp_set_max_rwinscale(tp
, so
);
1518 tcpstat
.tcps_connattempt
++;
1519 tp
->t_state
= TCPS_SYN_SENT
;
1520 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1521 TCP_CONN_KEEPINIT(tp
));
1522 tp
->iss
= tcp_new_isn(tp
);
1523 tcp_sendseqinit(tp
);
1525 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1528 * Generate a CC value for this connection and
1529 * check whether CC or CCnew should be used.
1531 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
1532 taop
= &tao_noncached
;
1533 bzero(taop
, sizeof(*taop
));
1536 tp
->cc_send
= CC_INC(tcp_ccgen
);
1537 if (taop
->tao_ccsent
!= 0 &&
1538 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1539 taop
->tao_ccsent
= tp
->cc_send
;
1541 taop
->tao_ccsent
= 0;
1542 tp
->t_flags
|= TF_SENDCCNEW
;
1547 ifnet_release(outif
);
1554 * Export TCP internal state information via a struct tcp_info
1556 __private_extern__
void
1557 tcp_fill_info(struct tcpcb
*tp
, struct tcp_info
*ti
)
1559 struct inpcb
*inp
= tp
->t_inpcb
;
1561 bzero(ti
, sizeof(*ti
));
1563 ti
->tcpi_state
= tp
->t_state
;
1564 ti
->tcpi_flowhash
= inp
->inp_flowhash
;
1566 if (tp
->t_state
> TCPS_LISTEN
) {
1567 if (TSTMP_SUPPORTED(tp
))
1568 ti
->tcpi_options
|= TCPI_OPT_TIMESTAMPS
;
1569 if (SACK_ENABLED(tp
))
1570 ti
->tcpi_options
|= TCPI_OPT_SACK
;
1571 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1572 ti
->tcpi_options
|= TCPI_OPT_WSCALE
;
1573 ti
->tcpi_snd_wscale
= tp
->snd_scale
;
1574 ti
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1576 if (TCP_ECN_ENABLED(tp
))
1577 ti
->tcpi_options
|= TCPI_OPT_ECN
;
1579 /* Are we in retranmission episode */
1580 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1581 ti
->tcpi_flags
|= TCPI_FLAG_LOSSRECOVERY
;
1583 ti
->tcpi_rto
= tp
->t_timer
[TCPT_REXMT
] ? tp
->t_rxtcur
: 0;
1584 ti
->tcpi_snd_mss
= tp
->t_maxseg
;
1585 ti
->tcpi_rcv_mss
= tp
->t_maxseg
;
1587 ti
->tcpi_rttcur
= tp
->t_rttcur
;
1588 ti
->tcpi_srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
1589 ti
->tcpi_rttvar
= tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
;
1590 ti
->tcpi_rttbest
= tp
->t_rttbest
>> TCP_RTT_SHIFT
;
1592 ti
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1593 ti
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1594 ti
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1596 ti
->tcpi_rcv_space
= tp
->rcv_wnd
;
1598 ti
->tcpi_snd_wnd
= tp
->snd_wnd
;
1599 ti
->tcpi_snd_nxt
= tp
->snd_nxt
;
1600 ti
->tcpi_rcv_nxt
= tp
->rcv_nxt
;
1602 /* convert bytes/msec to bits/sec */
1603 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1604 tp
->t_bwmeas
!= NULL
) {
1605 ti
->tcpi_snd_bw
= (tp
->t_bwmeas
->bw_sndbw
* 8000);
1608 ti
->tcpi_last_outif
= (tp
->t_inpcb
->inp_last_outifp
== NULL
) ? 0 :
1609 tp
->t_inpcb
->inp_last_outifp
->if_index
;
1611 //atomic_get_64(ti->tcpi_txbytes, &inp->inp_stat->txbytes);
1612 ti
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1613 ti
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1614 ti
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1615 ti
->tcpi_txunacked
= tp
->snd_max
- tp
->snd_una
;
1617 //atomic_get_64(ti->tcpi_rxbytes, &inp->inp_stat->rxbytes);
1618 ti
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1619 ti
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1620 ti
->tcpi_rxduplicatebytes
= tp
->t_stat
.rxduplicatebytes
;
1621 ti
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1623 if (tp
->t_state
> TCPS_LISTEN
) {
1624 ti
->tcpi_synrexmits
= tp
->t_stat
.synrxtshift
;
1626 ti
->tcpi_cell_rxpackets
= inp
->inp_cstat
->rxpackets
;
1627 ti
->tcpi_cell_rxbytes
= inp
->inp_cstat
->rxbytes
;
1628 ti
->tcpi_cell_txpackets
= inp
->inp_cstat
->txpackets
;
1629 ti
->tcpi_cell_txbytes
= inp
->inp_cstat
->txbytes
;
1631 ti
->tcpi_wifi_rxpackets
= inp
->inp_wstat
->rxpackets
;
1632 ti
->tcpi_wifi_rxbytes
= inp
->inp_wstat
->rxbytes
;
1633 ti
->tcpi_wifi_txpackets
= inp
->inp_wstat
->txpackets
;
1634 ti
->tcpi_wifi_txbytes
= inp
->inp_wstat
->txbytes
;
1636 ti
->tcpi_wired_rxpackets
= inp
->inp_Wstat
->rxpackets
;
1637 ti
->tcpi_wired_rxbytes
= inp
->inp_Wstat
->rxbytes
;
1638 ti
->tcpi_wired_txpackets
= inp
->inp_Wstat
->txpackets
;
1639 ti
->tcpi_wired_txbytes
= inp
->inp_Wstat
->txbytes
;
1640 tcp_get_connectivity_status(tp
, &ti
->tcpi_connstatus
);
1642 ti
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1643 ti
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1644 ti
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1645 ti
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1647 ti
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1648 ti
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1649 ti
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1650 ti
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1651 ti
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1653 ti
->tcpi_ecn_client_setup
= !!(tp
->ecn_flags
& TE_SETUPSENT
);
1654 ti
->tcpi_ecn_server_setup
= !!(tp
->ecn_flags
& TE_SETUPRECEIVED
);
1655 ti
->tcpi_ecn_success
= (tp
->ecn_flags
& TE_ECN_ON
) == TE_ECN_ON
? 1 : 0;
1656 ti
->tcpi_ecn_lost_syn
= !!(tp
->ecn_flags
& TE_LOST_SYN
);
1657 ti
->tcpi_ecn_lost_synack
= !!(tp
->ecn_flags
& TE_LOST_SYNACK
);
1659 ti
->tcpi_local_peer
= !!(tp
->t_flags
& TF_LOCAL
);
1661 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
) {
1662 if (IFNET_IS_CELLULAR(tp
->t_inpcb
->inp_last_outifp
))
1663 ti
->tcpi_if_cell
= 1;
1664 else if (IFNET_IS_WIFI(tp
->t_inpcb
->inp_last_outifp
))
1665 ti
->tcpi_if_wifi
= 1;
1668 ti
->tcpi_ecn_recv_ce
= tp
->t_ecn_recv_ce
;
1669 ti
->tcpi_ecn_recv_cwr
= tp
->t_ecn_recv_cwr
;
1671 ti
->tcpi_rcvoopack
= tp
->t_rcvoopack
;
1672 ti
->tcpi_pawsdrop
= tp
->t_pawsdrop
;
1673 ti
->tcpi_sack_recovery_episode
= tp
->t_sack_recovery_episode
;
1674 ti
->tcpi_reordered_pkts
= tp
->t_reordered_pkts
;
1675 ti
->tcpi_dsack_sent
= tp
->t_dsack_sent
;
1676 ti
->tcpi_dsack_recvd
= tp
->t_dsack_recvd
;
1680 __private_extern__ errno_t
1681 tcp_fill_info_for_info_tuple(struct info_tuple
*itpl
, struct tcp_info
*ti
)
1683 struct inpcbinfo
*pcbinfo
= NULL
;
1684 struct inpcb
*inp
= NULL
;
1688 if (itpl
->itpl_proto
== IPPROTO_TCP
)
1693 if (itpl
->itpl_local_sa
.sa_family
== AF_INET
&&
1694 itpl
->itpl_remote_sa
.sa_family
== AF_INET
) {
1695 inp
= in_pcblookup_hash(pcbinfo
,
1696 itpl
->itpl_remote_sin
.sin_addr
,
1697 itpl
->itpl_remote_sin
.sin_port
,
1698 itpl
->itpl_local_sin
.sin_addr
,
1699 itpl
->itpl_local_sin
.sin_port
,
1701 } else if (itpl
->itpl_local_sa
.sa_family
== AF_INET6
&&
1702 itpl
->itpl_remote_sa
.sa_family
== AF_INET6
) {
1703 struct in6_addr ina6_local
;
1704 struct in6_addr ina6_remote
;
1706 ina6_local
= itpl
->itpl_local_sin6
.sin6_addr
;
1707 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local
) &&
1708 itpl
->itpl_local_sin6
.sin6_scope_id
)
1709 ina6_local
.s6_addr16
[1] = htons(itpl
->itpl_local_sin6
.sin6_scope_id
);
1711 ina6_remote
= itpl
->itpl_remote_sin6
.sin6_addr
;
1712 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote
) &&
1713 itpl
->itpl_remote_sin6
.sin6_scope_id
)
1714 ina6_remote
.s6_addr16
[1] = htons(itpl
->itpl_remote_sin6
.sin6_scope_id
);
1716 inp
= in6_pcblookup_hash(pcbinfo
,
1718 itpl
->itpl_remote_sin6
.sin6_port
,
1720 itpl
->itpl_local_sin6
.sin6_port
,
1725 if (inp
== NULL
|| (so
= inp
->inp_socket
) == NULL
)
1729 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1730 socket_unlock(so
, 0);
1733 tp
= intotcpcb(inp
);
1735 tcp_fill_info(tp
, ti
);
1736 socket_unlock(so
, 0);
1742 tcp_connection_fill_info(struct tcpcb
*tp
, struct tcp_connection_info
*tci
)
1744 struct inpcb
*inp
= tp
->t_inpcb
;
1746 bzero(tci
, sizeof(*tci
));
1747 tci
->tcpi_state
= tp
->t_state
;
1748 if (tp
->t_state
> TCPS_LISTEN
) {
1749 if (TSTMP_SUPPORTED(tp
))
1750 tci
->tcpi_options
|= TCPCI_OPT_TIMESTAMPS
;
1751 if (SACK_ENABLED(tp
))
1752 tci
->tcpi_options
|= TCPCI_OPT_SACK
;
1753 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1754 tci
->tcpi_options
|= TCPCI_OPT_WSCALE
;
1755 tci
->tcpi_snd_wscale
= tp
->snd_scale
;
1756 tci
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1758 if (TCP_ECN_ENABLED(tp
))
1759 tci
->tcpi_options
|= TCPCI_OPT_ECN
;
1760 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1761 tci
->tcpi_flags
|= TCPCI_FLAG_LOSSRECOVERY
;
1762 if (tp
->t_flagsext
& TF_PKTS_REORDERED
)
1763 tci
->tcpi_flags
|= TCPCI_FLAG_REORDERING_DETECTED
;
1764 tci
->tcpi_rto
= (tp
->t_timer
[TCPT_REXMT
] > 0) ?
1766 tci
->tcpi_maxseg
= tp
->t_maxseg
;
1767 tci
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1768 tci
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1769 tci
->tcpi_snd_wnd
= tp
->snd_wnd
;
1770 tci
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1771 tci
->tcpi_rcv_wnd
= tp
->rcv_wnd
;
1772 tci
->tcpi_rttcur
= tp
->t_rttcur
;
1773 tci
->tcpi_srtt
= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1774 tci
->tcpi_rttvar
= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
1775 tci
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1776 tci
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1777 tci
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1778 tci
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1779 tci
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1780 tci
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1782 tci
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1783 tci
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1784 tci
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1785 tci
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1786 tci
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1787 tci
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1788 tci
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1789 tci
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1790 tci
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1795 __private_extern__
int
1796 tcp_sysctl_info(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
1800 struct info_tuple itpl
;
1801 proc_t caller
= PROC_NULL
;
1802 proc_t caller_parent
= PROC_NULL
;
1803 char command_name
[MAXCOMLEN
+ 1] = "";
1804 char parent_name
[MAXCOMLEN
+ 1] = "";
1806 if ((caller
= proc_self()) != PROC_NULL
) {
1807 /* get process name */
1808 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
1810 /* get parent process name if possible */
1811 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
1812 strlcpy(parent_name
, caller_parent
->p_comm
,
1813 sizeof(parent_name
));
1814 proc_rele(caller_parent
);
1817 if ((escape_str(command_name
, strlen(command_name
),
1818 sizeof(command_name
)) == 0) &&
1819 (escape_str(parent_name
, strlen(parent_name
),
1820 sizeof(parent_name
)) == 0)) {
1821 kern_asl_msg(LOG_DEBUG
, "messagetracer",
1823 "com.apple.message.domain",
1824 "com.apple.kernel.tcpstat", /* 1 */
1825 "com.apple.message.signature",
1827 "com.apple.message.signature2", command_name
, /* 3 */
1828 "com.apple.message.signature3", parent_name
, /* 4 */
1829 "com.apple.message.summarize", "YES", /* 5 */
1834 if (caller
!= PROC_NULL
)
1837 if (req
->newptr
== USER_ADDR_NULL
) {
1840 if (req
->newlen
< sizeof(struct info_tuple
)) {
1843 error
= SYSCTL_IN(req
, &itpl
, sizeof(struct info_tuple
));
1847 error
= tcp_fill_info_for_info_tuple(&itpl
, &ti
);
1851 error
= SYSCTL_OUT(req
, &ti
, sizeof(struct tcp_info
));
1860 tcp_lookup_peer_pid_locked(struct socket
*so
, pid_t
*out_pid
)
1862 int error
= EHOSTUNREACH
;
1864 if ((so
->so_state
& SS_ISCONNECTED
) == 0) return ENOTCONN
;
1866 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
1867 uint16_t lport
= inp
->inp_lport
;
1868 uint16_t fport
= inp
->inp_fport
;
1869 struct inpcb
*finp
= NULL
;
1871 if (inp
->inp_vflag
& INP_IPV6
) {
1872 struct in6_addr laddr6
= inp
->in6p_laddr
;
1873 struct in6_addr faddr6
= inp
->in6p_faddr
;
1874 socket_unlock(so
, 0);
1875 finp
= in6_pcblookup_hash(&tcbinfo
, &laddr6
, lport
, &faddr6
, fport
, 0, NULL
);
1877 } else if (inp
->inp_vflag
& INP_IPV4
) {
1878 struct in_addr laddr4
= inp
->inp_laddr
;
1879 struct in_addr faddr4
= inp
->inp_faddr
;
1880 socket_unlock(so
, 0);
1881 finp
= in_pcblookup_hash(&tcbinfo
, laddr4
, lport
, faddr4
, fport
, 0, NULL
);
1886 *out_pid
= finp
->inp_socket
->last_pid
;
1888 in_pcb_checkstate(finp
, WNT_RELEASE
, 0);
1895 tcp_getconninfo(struct socket
*so
, struct conninfo_tcp
*tcp_ci
)
1897 (void) tcp_lookup_peer_pid_locked(so
, &tcp_ci
->tcpci_peer_pid
);
1898 tcp_fill_info(sototcpcb(so
), &tcp_ci
->tcpci_tcp_info
);
1902 * The new sockopt interface makes it possible for us to block in the
1903 * copyin/out step (if we take a page fault). Taking a page fault at
1904 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1905 * use TSM, there probably isn't any need for this function to run at
1906 * splnet() any more. This needs more examination.)
1909 tcp_ctloutput(so
, sopt
)
1911 struct sockopt
*sopt
;
1913 int error
, opt
, optval
;
1918 inp
= sotoinpcb(so
);
1920 return (ECONNRESET
);
1922 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
1923 if (sopt
->sopt_level
!= IPPROTO_TCP
&&
1924 !(sopt
->sopt_level
== SOL_SOCKET
&& (sopt
->sopt_name
== SO_FLUSH
||
1925 sopt
->sopt_name
== SO_TRAFFIC_MGT_BACKGROUND
))) {
1927 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1928 error
= ip6_ctloutput(so
, sopt
);
1931 error
= ip_ctloutput(so
, sopt
);
1934 tp
= intotcpcb(inp
);
1936 return (ECONNRESET
);
1939 calculate_tcp_clock();
1941 switch (sopt
->sopt_dir
) {
1943 switch (sopt
->sopt_name
) {
1947 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1952 switch (sopt
->sopt_name
) {
1963 opt
= 0; /* dead code to fool gcc */
1970 tp
->t_flags
&= ~opt
;
1972 case TCP_RXT_FINDROP
:
1973 case TCP_NOTIMEWAIT
:
1974 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1978 switch (sopt
->sopt_name
) {
1979 case TCP_RXT_FINDROP
:
1980 opt
= TF_RXTFINDROP
;
1982 case TCP_NOTIMEWAIT
:
1983 opt
= TF_NOTIMEWAIT
;
1990 tp
->t_flagsext
|= opt
;
1992 tp
->t_flagsext
&= ~opt
;
1994 case TCP_MEASURE_SND_BW
:
1995 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1999 opt
= TF_MEASURESNDBW
;
2001 if (tp
->t_bwmeas
== NULL
) {
2002 tp
->t_bwmeas
= tcp_bwmeas_alloc(tp
);
2003 if (tp
->t_bwmeas
== NULL
) {
2008 tp
->t_flagsext
|= opt
;
2010 tp
->t_flagsext
&= ~opt
;
2011 /* Reset snd bw measurement state */
2012 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
2013 if (tp
->t_bwmeas
!= NULL
) {
2014 tcp_bwmeas_free(tp
);
2018 case TCP_MEASURE_BW_BURST
: {
2019 struct tcp_measure_bw_burst in
;
2020 uint32_t minpkts
, maxpkts
;
2021 bzero(&in
, sizeof(in
));
2023 error
= sooptcopyin(sopt
, &in
, sizeof(in
),
2027 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2028 tp
->t_bwmeas
== NULL
) {
2032 minpkts
= (in
.min_burst_size
!= 0) ? in
.min_burst_size
:
2033 tp
->t_bwmeas
->bw_minsizepkts
;
2034 maxpkts
= (in
.max_burst_size
!= 0) ? in
.max_burst_size
:
2035 tp
->t_bwmeas
->bw_maxsizepkts
;
2036 if (minpkts
> maxpkts
) {
2040 tp
->t_bwmeas
->bw_minsizepkts
= minpkts
;
2041 tp
->t_bwmeas
->bw_maxsizepkts
= maxpkts
;
2042 tp
->t_bwmeas
->bw_minsize
= (minpkts
* tp
->t_maxseg
);
2043 tp
->t_bwmeas
->bw_maxsize
= (maxpkts
* tp
->t_maxseg
);
2047 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2052 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
2053 optval
+ 40 >= tcp_minmss
)
2054 tp
->t_maxseg
= optval
;
2060 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2064 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2067 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
2068 /* reset the timer to new value */
2069 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2070 TCP_CONN_KEEPIDLE(tp
));
2071 tcp_check_timer_state(tp
);
2075 case TCP_CONNECTIONTIMEOUT
:
2076 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2080 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2083 tp
->t_keepinit
= optval
* TCP_RETRANSHZ
;
2084 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
2085 tp
->t_state
== TCPS_SYN_SENT
) {
2086 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2087 TCP_CONN_KEEPINIT(tp
));
2088 tcp_check_timer_state(tp
);
2094 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2098 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2101 tp
->t_keepintvl
= optval
* TCP_RETRANSHZ
;
2102 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2103 TCP_CONN_MAXIDLE(tp
) > 0) {
2104 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2105 TCP_CONN_MAXIDLE(tp
));
2106 tcp_check_timer_state(tp
);
2112 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2116 if (optval
< 0 || optval
> INT32_MAX
) {
2119 tp
->t_keepcnt
= optval
;
2120 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2121 TCP_CONN_MAXIDLE(tp
) > 0) {
2122 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2123 TCP_CONN_MAXIDLE(tp
));
2124 tcp_check_timer_state(tp
);
2129 case PERSIST_TIMEOUT
:
2130 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2137 tp
->t_persist_timeout
= optval
* TCP_RETRANSHZ
;
2139 case TCP_RXT_CONNDROPTIME
:
2140 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2147 tp
->t_rxt_conndroptime
= optval
* TCP_RETRANSHZ
;
2149 case TCP_NOTSENT_LOWAT
:
2150 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2159 so
->so_flags
&= ~(SOF_NOTSENT_LOWAT
);
2160 tp
->t_notsent_lowat
= 0;
2162 so
->so_flags
|= SOF_NOTSENT_LOWAT
;
2163 tp
->t_notsent_lowat
= optval
;
2167 case TCP_ADAPTIVE_READ_TIMEOUT
:
2168 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2173 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2176 } else if (optval
== 0) {
2177 tp
->t_adaptive_rtimo
= 0;
2178 tcp_keepalive_reset(tp
);
2180 tp
->t_adaptive_rtimo
= optval
;
2183 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2184 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2189 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2193 tp
->t_adaptive_wtimo
= optval
;
2196 case TCP_ENABLE_MSGS
:
2197 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2201 if (optval
< 0 || optval
> 1) {
2203 } else if (optval
== 1) {
2205 * Check if messages option is already
2206 * enabled, if so return.
2208 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2209 VERIFY(so
->so_msg_state
!= NULL
);
2214 * allocate memory for storing message
2217 VERIFY(so
->so_msg_state
== NULL
);
2218 MALLOC(so
->so_msg_state
,
2220 sizeof(struct msg_state
),
2221 M_TEMP
, M_WAITOK
| M_ZERO
);
2222 if (so
->so_msg_state
== NULL
) {
2227 /* Enable message delivery */
2228 so
->so_flags
|= SOF_ENABLE_MSGS
;
2231 * Can't disable message delivery on socket
2232 * because of restrictions imposed by
2238 case TCP_SENDMOREACKS
:
2239 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2243 if (optval
< 0 || optval
> 1) {
2245 } else if (optval
== 0) {
2246 tp
->t_flagsext
&= ~(TF_NOSTRETCHACK
);
2248 tp
->t_flagsext
|= TF_NOSTRETCHACK
;
2251 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2252 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2256 if (optval
< 0 || optval
> 1) {
2258 } else if (optval
== 0) {
2259 tp
->t_flagsext
&= ~TF_NOBLACKHOLE_DETECTION
;
2261 tp
->t_flagsext
|= TF_NOBLACKHOLE_DETECTION
;
2262 if ((tp
->t_flags
& TF_BLACKHOLE
) &&
2263 tp
->t_pmtud_saved_maxopd
> 0)
2264 tcp_pmtud_revert_segment_size(tp
);
2268 if (!(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2273 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2277 if (optval
< 0 || optval
> 1) {
2281 if (tp
->t_state
!= TCPS_LISTEN
) {
2286 tp
->t_flagsext
|= TF_FASTOPEN
;
2288 tcp_disable_tfo(tp
);
2290 case TCP_ENABLE_ECN
:
2291 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2296 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2297 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2299 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2303 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2307 if (optval
== ECN_MODE_DEFAULT
) {
2308 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2309 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2310 } else if (optval
== ECN_MODE_ENABLE
) {
2311 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2312 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2313 } else if (optval
== ECN_MODE_DISABLE
) {
2314 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2315 tp
->ecn_flags
|= TE_ECN_MODE_DISABLE
;
2321 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2322 sizeof (optval
))) != 0)
2325 error
= inp_flush(inp
, optval
);
2328 case SO_TRAFFIC_MGT_BACKGROUND
:
2329 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2330 sizeof (optval
))) != 0)
2334 socket_set_traffic_mgt_flags_locked(so
,
2335 TRAFFIC_MGT_SO_BACKGROUND
);
2337 socket_clear_traffic_mgt_flags_locked(so
,
2338 TRAFFIC_MGT_SO_BACKGROUND
);
2343 error
= ENOPROTOOPT
;
2349 switch (sopt
->sopt_name
) {
2351 optval
= tp
->t_flags
& TF_NODELAY
;
2354 optval
= tp
->t_maxseg
;
2357 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
2360 optval
= tp
->t_keepintvl
/ TCP_RETRANSHZ
;
2363 optval
= tp
->t_keepcnt
;
2366 optval
= tp
->t_flags
& TF_NOOPT
;
2369 optval
= tp
->t_flags
& TF_NOPUSH
;
2371 case TCP_ENABLE_ECN
:
2372 optval
= (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
) ? 1 : 0;
2375 if (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
)
2376 optval
= ECN_MODE_ENABLE
;
2377 else if (tp
->ecn_flags
& TE_ECN_MODE_DISABLE
)
2378 optval
= ECN_MODE_DISABLE
;
2380 optval
= ECN_MODE_DEFAULT
;
2382 case TCP_CONNECTIONTIMEOUT
:
2383 optval
= tp
->t_keepinit
/ TCP_RETRANSHZ
;
2385 case PERSIST_TIMEOUT
:
2386 optval
= tp
->t_persist_timeout
/ TCP_RETRANSHZ
;
2388 case TCP_RXT_CONNDROPTIME
:
2389 optval
= tp
->t_rxt_conndroptime
/ TCP_RETRANSHZ
;
2391 case TCP_RXT_FINDROP
:
2392 optval
= tp
->t_flagsext
& TF_RXTFINDROP
;
2394 case TCP_NOTIMEWAIT
:
2395 optval
= (tp
->t_flagsext
& TF_NOTIMEWAIT
) ? 1 : 0;
2398 if (tp
->t_state
!= TCPS_LISTEN
||
2399 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2403 optval
= tfo_enabled(tp
);
2405 case TCP_MEASURE_SND_BW
:
2406 optval
= tp
->t_flagsext
& TF_MEASURESNDBW
;
2411 tcp_fill_info(tp
, &ti
);
2412 error
= sooptcopyout(sopt
, &ti
, sizeof(struct tcp_info
));
2416 case TCP_CONNECTION_INFO
: {
2417 struct tcp_connection_info tci
;
2418 tcp_connection_fill_info(tp
, &tci
);
2419 error
= sooptcopyout(sopt
, &tci
,
2420 sizeof(struct tcp_connection_info
));
2423 case TCP_MEASURE_BW_BURST
: {
2424 struct tcp_measure_bw_burst out
;
2425 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2426 tp
->t_bwmeas
== NULL
) {
2430 out
.min_burst_size
= tp
->t_bwmeas
->bw_minsizepkts
;
2431 out
.max_burst_size
= tp
->t_bwmeas
->bw_maxsizepkts
;
2432 error
= sooptcopyout(sopt
, &out
, sizeof(out
));
2435 case TCP_NOTSENT_LOWAT
:
2436 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0) {
2437 optval
= tp
->t_notsent_lowat
;
2443 case TCP_ENABLE_MSGS
:
2444 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2450 case TCP_SENDMOREACKS
:
2451 if (tp
->t_flagsext
& TF_NOSTRETCHACK
)
2456 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2457 if (tp
->t_flagsext
& TF_NOBLACKHOLE_DETECTION
)
2462 case TCP_PEER_PID
: {
2464 error
= tcp_lookup_peer_pid_locked(so
, &pid
);
2466 error
= sooptcopyout(sopt
, &pid
, sizeof(pid
));
2469 case TCP_ADAPTIVE_READ_TIMEOUT
:
2470 optval
= tp
->t_adaptive_rtimo
;
2472 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2473 optval
= tp
->t_adaptive_wtimo
;
2475 case SO_TRAFFIC_MGT_BACKGROUND
:
2476 optval
= (so
->so_traffic_mgt_flags
&
2477 TRAFFIC_MGT_SO_BACKGROUND
) ? 1 : 0;
2480 error
= ENOPROTOOPT
;
2484 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2492 * tcp_sendspace and tcp_recvspace are the default send and receive window
2493 * sizes, respectively. These are obsolescent (this information should
2494 * be set by the route).
2496 u_int32_t tcp_sendspace
= 1448*256;
2497 u_int32_t tcp_recvspace
= 1448*384;
2499 /* During attach, the size of socket buffer allocated is limited to
2500 * sb_max in sbreserve. Disallow setting the tcp send and recv space
2501 * to be more than sb_max because that will cause tcp_attach to fail
2502 * (see radar 5713060)
2505 sysctl_tcp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
2506 __unused
int arg2
, struct sysctl_req
*req
) {
2507 u_int32_t new_value
= 0, *space_p
= NULL
;
2508 int changed
= 0, error
= 0;
2509 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
2511 switch (oidp
->oid_number
) {
2512 case TCPCTL_SENDSPACE
:
2513 space_p
= &tcp_sendspace
;
2515 case TCPCTL_RECVSPACE
:
2516 space_p
= &tcp_recvspace
;
2521 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
2522 &new_value
, &changed
);
2524 if (new_value
> 0 && new_value
<= sb_effective_max
) {
2525 *space_p
= new_value
;
2533 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2534 &tcp_sendspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum outgoing TCP datagram size");
2535 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2536 &tcp_recvspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum incoming TCP datagram size");
2538 /* Sysctl for testing and tuning the connectx with data api */
2539 #define TCP_PRECONNECT_SBSZ_MAX 1460
2540 #define TCP_PRECONNECT_SBSZ_MIN (TCP_MSS)
2541 #define TCP_PRECONNECT_SBSZ_DEF (TCP6_MSS)
2542 static int tcp_preconnect_sbspace
= TCP_PRECONNECT_SBSZ_DEF
;
2543 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, preconn_sbsz
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2544 &tcp_preconnect_sbspace
, 0, "Maximum preconnect space");
2548 * Attach TCP protocol to socket, allocating
2549 * internet protocol control block, tcp control block,
2550 * bufer space, and entering LISTEN state if to accept connections.
2552 * Returns: 0 Success
2553 * in_pcballoc:ENOBUFS
2554 * in_pcballoc:ENOMEM
2555 * in_pcballoc:??? [IPSEC specific]
2563 register struct tcpcb
*tp
;
2567 int isipv6
= SOCK_CHECK_DOM(so
, PF_INET6
) != 0;
2570 error
= in_pcballoc(so
, &tcbinfo
, p
);
2574 inp
= sotoinpcb(so
);
2576 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
2577 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
2582 if (so
->so_snd
.sb_preconn_hiwat
== 0) {
2583 soreserve_preconnect(so
, imin(TCP_PRECONNECT_SBSZ_MAX
,
2584 imax(tcp_preconnect_sbspace
, TCP_PRECONNECT_SBSZ_MIN
)));
2587 if ((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0)
2588 so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
2589 if ((so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0)
2590 so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
2594 inp
->inp_vflag
|= INP_IPV6
;
2595 inp
->in6p_hops
= -1; /* use kernel default */
2599 inp
->inp_vflag
|= INP_IPV4
;
2600 tp
= tcp_newtcpcb(inp
);
2602 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
2604 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
2611 so
->so_state
|= nofd
;
2615 nstat_tcp_new_pcb(inp
);
2616 tp
->t_state
= TCPS_CLOSED
;
2621 * Initiate (or continue) disconnect.
2622 * If embryonic state, just send reset (once).
2623 * If in ``let data drain'' option and linger null, just drop.
2624 * Otherwise (hard), mark socket disconnecting and drop
2625 * current input data; switch states based on user close, and
2626 * send segment to peer (with FIN).
2628 static struct tcpcb
*
2630 register struct tcpcb
*tp
;
2632 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2634 if (tp
->t_state
< TCPS_ESTABLISHED
)
2636 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
2637 tp
= tcp_drop(tp
, 0);
2639 soisdisconnecting(so
);
2640 sbflush(&so
->so_rcv
);
2641 tp
= tcp_usrclosed(tp
);
2643 /* A reset has been sent but socket exists, do not send FIN */
2644 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
2645 (tp
) && (tp
->t_mpflags
& TMPF_RESET
))
2649 (void) tcp_output(tp
);
2655 * User issued close, and wish to trail through shutdown states:
2656 * if never received SYN, just forget it. If got a SYN from peer,
2657 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2658 * If already got a FIN from peer, then almost done; go to LAST_ACK
2659 * state. In all other cases, have already sent FIN to peer (e.g.
2660 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2661 * for peer to send FIN or not respond to keep-alives, etc.
2662 * We can let the user exit from the close as soon as the FIN is acked.
2664 static struct tcpcb
*
2666 register struct tcpcb
*tp
;
2669 switch (tp
->t_state
) {
2677 case TCPS_SYN_RECEIVED
:
2678 tp
->t_flags
|= TF_NEEDFIN
;
2681 case TCPS_ESTABLISHED
:
2682 DTRACE_TCP4(state__change
, void, NULL
,
2683 struct inpcb
*, tp
->t_inpcb
,
2685 int32_t, TCPS_FIN_WAIT_1
);
2686 tp
->t_state
= TCPS_FIN_WAIT_1
;
2689 case TCPS_CLOSE_WAIT
:
2690 DTRACE_TCP4(state__change
, void, NULL
,
2691 struct inpcb
*, tp
->t_inpcb
,
2693 int32_t, TCPS_LAST_ACK
);
2694 tp
->t_state
= TCPS_LAST_ACK
;
2697 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
2698 soisdisconnected(tp
->t_inpcb
->inp_socket
);
2699 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
2700 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
2701 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2702 TCP_CONN_MAXIDLE(tp
));
2708 tcp_in_cksum_stats(u_int32_t len
)
2710 tcpstat
.tcps_rcv_swcsum
++;
2711 tcpstat
.tcps_rcv_swcsum_bytes
+= len
;
2715 tcp_out_cksum_stats(u_int32_t len
)
2717 tcpstat
.tcps_snd_swcsum
++;
2718 tcpstat
.tcps_snd_swcsum_bytes
+= len
;
2723 tcp_in6_cksum_stats(u_int32_t len
)
2725 tcpstat
.tcps_rcv6_swcsum
++;
2726 tcpstat
.tcps_rcv6_swcsum_bytes
+= len
;
2730 tcp_out6_cksum_stats(u_int32_t len
)
2732 tcpstat
.tcps_snd6_swcsum
++;
2733 tcpstat
.tcps_snd6_swcsum_bytes
+= len
;
2737 * When messages are enabled on a TCP socket, the message priority
2738 * is sent as a control message. This function will extract it.
2741 tcp_get_msg_priority(struct mbuf
*control
, uint32_t *msgpri
)
2744 if (control
== NULL
)
2747 for (cm
= M_FIRST_CMSGHDR(control
); cm
;
2748 cm
= M_NXT_CMSGHDR(control
, cm
)) {
2749 if (cm
->cmsg_len
< sizeof(struct cmsghdr
) ||
2750 cm
->cmsg_len
> control
->m_len
) {
2753 if (cm
->cmsg_level
== SOL_SOCKET
&&
2754 cm
->cmsg_type
== SCM_MSG_PRIORITY
) {
2755 *msgpri
= *(unsigned int *)(void *)CMSG_DATA(cm
);
2760 VERIFY(*msgpri
>= MSG_PRI_MIN
&& *msgpri
<= MSG_PRI_MAX
);