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
);
462 #endif /* FLOW_DIVERT */
464 error
= cfil_sock_attach(so
);
467 #endif /* CONTENT_FILTER */
472 calculate_tcp_clock();
474 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
475 error
= EAFNOSUPPORT
;
479 * Must disallow TCP ``connections'' to multicast addresses.
481 sinp
= (struct sockaddr_in
*)(void *)nam
;
482 if (sinp
->sin_family
== AF_INET
483 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
484 error
= EAFNOSUPPORT
;
488 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
491 error
= tcp_connect_complete(so
);
493 COMMON_END(PRU_CONNECT
);
497 tcp_usr_connectx_common(struct socket
*so
, int af
,
498 struct sockaddr_list
**src_sl
, struct sockaddr_list
**dst_sl
,
499 struct proc
*p
, uint32_t ifscope
, sae_associd_t aid
, sae_connid_t
*pcid
,
500 uint32_t flags
, void *arg
, uint32_t arglen
, struct uio
*auio
,
501 user_ssize_t
*bytes_written
)
505 #pragma unused(flags, arg, arglen)
507 struct sockaddr_entry
*src_se
= NULL
, *dst_se
= NULL
;
508 struct inpcb
*inp
= sotoinpcb(so
);
510 user_ssize_t datalen
= 0;
515 VERIFY(dst_sl
!= NULL
);
517 /* select source (if specified) and destination addresses */
518 error
= in_selectaddrs(af
, src_sl
, &src_se
, dst_sl
, &dst_se
);
522 VERIFY(*dst_sl
!= NULL
&& dst_se
!= NULL
);
523 VERIFY(src_se
== NULL
|| *src_sl
!= NULL
);
524 VERIFY(dst_se
->se_addr
->sa_family
== af
);
525 VERIFY(src_se
== NULL
|| src_se
->se_addr
->sa_family
== af
);
528 inp_update_necp_policy(inp
, src_se
? src_se
->se_addr
: NULL
, dst_se
? dst_se
->se_addr
: NULL
, ifscope
);
531 if ((so
->so_flags1
& SOF1_DATA_IDEMPOTENT
) &&
532 (tcp_fastopen
& TCP_FASTOPEN_CLIENT
))
533 sototcpcb(so
)->t_flagsext
|= TF_FASTOPEN
;
536 * We get here for 2 cases:
538 * a. From MPTCP, to connect a subflow. There is no need to
539 * bind the socket to the source address and/or interface,
540 * since everything has been taken care of by MPTCP. We
541 * simply check whether or not this is for the initial
542 * MPTCP connection attempt, or to join an existing one.
544 * b. From the socket layer, to connect a TCP. Perform the
545 * bind to source address and/or interface as necessary.
548 if (flags
& CONNREQF_MPTCP
) {
549 struct mptsub_connreq
*mpcr
= arg
;
551 /* Check to make sure this came down from MPTCP */
552 if (arg
== NULL
|| arglen
!= sizeof (*mpcr
))
555 switch (mpcr
->mpcr_type
) {
556 case MPTSUB_CONNREQ_MP_ENABLE
:
558 case MPTSUB_CONNREQ_MP_ADD
:
566 /* bind socket to the specified interface, if requested */
567 if (ifscope
!= IFSCOPE_NONE
&&
568 (error
= inp_bindif(inp
, ifscope
, NULL
)) != 0)
571 /* if source address and/or port is specified, bind to it */
572 if (src_se
!= NULL
) {
573 struct sockaddr
*sa
= src_se
->se_addr
;
574 error
= sobindlock(so
, sa
, 0); /* already locked */
582 error
= tcp_usr_connect(so
, dst_se
->se_addr
, p
);
586 error
= tcp6_usr_connect(so
, dst_se
->se_addr
, p
);
597 /* if there is data, copy it */
599 socket_unlock(so
, 0);
601 VERIFY(bytes_written
!= NULL
);
603 datalen
= uio_resid(auio
);
604 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, NULL
,
605 (uio_t
)auio
, NULL
, NULL
, 0);
608 if (error
== 0 || error
== EWOULDBLOCK
)
609 *bytes_written
= datalen
- uio_resid(auio
);
612 * sosend returns EWOULDBLOCK if it's a non-blocking
613 * socket or a timeout occured (this allows to return
614 * the amount of queued data through sendit()).
616 * However, connectx() returns EINPROGRESS in case of a
617 * blocking socket. So we change the return value here.
619 if (error
== EWOULDBLOCK
)
623 if (error
== 0 && pcid
!= NULL
)
624 *pcid
= 1; /* there is only one connection in regular TCP */
630 tcp_usr_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
631 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
632 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
633 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
635 return (tcp_usr_connectx_common(so
, AF_INET
, src_sl
, dst_sl
,
636 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
, uio
,
642 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
645 struct inpcb
*inp
= sotoinpcb(so
);
647 struct sockaddr_in6
*sin6p
;
652 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
654 error
= so
->so_error
;
662 else if (necp_socket_should_use_flow_divert(inp
)) {
663 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
664 if (fd_ctl_unit
> 0) {
665 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
667 error
= flow_divert_connect_out(so
, nam
, p
);
675 #endif /* FLOW_DIVERT */
677 error
= cfil_sock_attach(so
);
680 #endif /* CONTENT_FILTER */
686 calculate_tcp_clock();
688 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
689 error
= EAFNOSUPPORT
;
694 * Must disallow TCP ``connections'' to multicast addresses.
696 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
697 if (sin6p
->sin6_family
== AF_INET6
698 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
699 error
= EAFNOSUPPORT
;
703 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
704 struct sockaddr_in sin
;
706 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
709 in6_sin6_2_sin(&sin
, sin6p
);
710 inp
->inp_vflag
|= INP_IPV4
;
711 inp
->inp_vflag
&= ~INP_IPV6
;
712 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
715 error
= tcp_connect_complete(so
);
718 inp
->inp_vflag
&= ~INP_IPV4
;
719 inp
->inp_vflag
|= INP_IPV6
;
720 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
723 error
= tcp_connect_complete(so
);
724 COMMON_END(PRU_CONNECT
);
728 tcp6_usr_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
729 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
730 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
731 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
733 return (tcp_usr_connectx_common(so
, AF_INET6
, src_sl
, dst_sl
,
734 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
, uio
,
740 * Initiate disconnect from peer.
741 * If connection never passed embryonic stage, just drop;
742 * else if don't need to let data drain, then can just drop anyways,
743 * else have to begin TCP shutdown process: mark socket disconnecting,
744 * drain unread data, state switch to reflect user close, and
745 * send segment (e.g. FIN) to peer. Socket will be really disconnected
746 * when peer sends FIN and acks ours.
748 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
751 tcp_usr_disconnect(struct socket
*so
)
754 struct inpcb
*inp
= sotoinpcb(so
);
757 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
758 LCK_MTX_ASSERT_OWNED
);
760 /* In case we got disconnected from the peer */
763 tp
= tcp_disconnect(tp
);
764 COMMON_END(PRU_DISCONNECT
);
768 * User-protocol pru_disconnectx callback.
771 tcp_usr_disconnectx(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
774 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
777 return (tcp_usr_disconnect(so
));
781 * Accept a connection. Essentially all the work is
782 * done at higher levels; just return the address
783 * of the peer, storing through addr.
786 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
789 struct inpcb
*inp
= sotoinpcb(so
);
790 struct tcpcb
*tp
= NULL
;
793 in_getpeeraddr(so
, nam
);
795 if (so
->so_state
& SS_ISDISCONNECTED
) {
796 error
= ECONNABORTED
;
799 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
802 else if (necp_socket_should_use_flow_divert(inp
))
805 error
= cfil_sock_attach(so
);
808 #endif /* CONTENT_FILTER */
814 calculate_tcp_clock();
816 COMMON_END(PRU_ACCEPT
);
821 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
824 struct inpcb
*inp
= sotoinpcb(so
);
825 struct tcpcb
*tp
= NULL
;
828 if (so
->so_state
& SS_ISDISCONNECTED
) {
829 error
= ECONNABORTED
;
832 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
835 else if (necp_socket_should_use_flow_divert(inp
))
838 error
= cfil_sock_attach(so
);
841 #endif /* CONTENT_FILTER */
847 calculate_tcp_clock();
849 in6_mapped_peeraddr(so
, nam
);
850 COMMON_END(PRU_ACCEPT
);
855 * Mark the connection as being incapable of further output.
858 * EINVAL [COMMON_START]
859 * tcp_output:EADDRNOTAVAIL
861 * tcp_output:EMSGSIZE
862 * tcp_output:EHOSTUNREACH
863 * tcp_output:ENETUNREACH
864 * tcp_output:ENETDOWN
867 * tcp_output:EMSGSIZE
869 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
872 tcp_usr_shutdown(struct socket
*so
)
875 struct inpcb
*inp
= sotoinpcb(so
);
879 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
885 * In case we got disconnected from the peer, or if this is
886 * a socket that is to be flow-diverted (but not yet).
893 || (necp_socket_should_use_flow_divert(inp
))
901 calculate_tcp_clock();
903 tp
= tcp_usrclosed(tp
);
905 /* A reset has been sent but socket exists, do not send FIN */
906 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
907 (tp
) && (tp
->t_mpflags
& TMPF_RESET
)) {
912 /* Don't send a FIN yet */
913 if (tp
&& !(so
->so_state
& SS_ISDISCONNECTED
) &&
914 cfil_sock_data_pending(&so
->so_snd
))
916 #endif /* CONTENT_FILTER */
918 error
= tcp_output(tp
);
919 COMMON_END(PRU_SHUTDOWN
);
923 * After a receive, possibly send window update to peer.
926 tcp_usr_rcvd(struct socket
*so
, __unused
int flags
)
929 struct inpcb
*inp
= sotoinpcb(so
);
933 /* In case we got disconnected from the peer */
936 tcp_sbrcv_trim(tp
, &so
->so_rcv
);
939 * This tcp_output is solely there to trigger window-updates.
940 * However, we really do not want these window-updates while we
941 * are still in SYN_SENT or SYN_RECEIVED.
943 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
947 cfil_sock_buf_update(&so
->so_rcv
);
948 #endif /* CONTENT_FILTER */
950 COMMON_END(PRU_RCVD
);
954 * Do a send by putting data in output queue and updating urgent
955 * marker if URG set. Possibly send more data. Unlike the other
956 * pru_*() routines, the mbuf chains are our responsibility. We
957 * must either enqueue them or free them. The other pru_* routines
958 * generally are caller-frees.
964 * tcp_connect:EADDRINUSE Address in use
965 * tcp_connect:EADDRNOTAVAIL Address not available.
966 * tcp_connect:EINVAL Invalid argument
967 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
968 * tcp_connect:EACCES Permission denied
969 * tcp_connect:EAGAIN Resource unavailable, try again
970 * tcp_connect:EPERM Operation not permitted
971 * tcp_output:EADDRNOTAVAIL
973 * tcp_output:EMSGSIZE
974 * tcp_output:EHOSTUNREACH
975 * tcp_output:ENETUNREACH
976 * tcp_output:ENETDOWN
979 * tcp_output:EMSGSIZE
981 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
982 * tcp6_connect:??? [IPV6 only]
985 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
986 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
989 struct inpcb
*inp
= sotoinpcb(so
);
991 uint32_t msgpri
= MSG_PRI_DEFAULT
;
997 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
999 || (necp_socket_should_use_flow_divert(inp
))
1003 * OOPS! we lost a race, the TCP session got reset after
1004 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
1005 * network interrupt in the non-splnet() section of sosend().
1009 if (control
!= NULL
) {
1015 error
= ECONNRESET
; /* XXX EPIPE? */
1023 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
1025 tp
= intotcpcb(inp
);
1028 calculate_tcp_clock();
1030 if (control
!= NULL
) {
1031 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1032 /* Get the msg priority from control mbufs */
1033 error
= tcp_get_msg_priority(control
, &msgpri
);
1044 } else if (control
->m_len
) {
1046 * if not unordered, TCP should not have
1059 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1060 VERIFY(m
->m_flags
& M_PKTHDR
);
1061 m
->m_pkthdr
.msg_pri
= msgpri
;
1064 /* MPTCP sublow socket buffers must not be compressed */
1065 VERIFY(!(so
->so_flags
& SOF_MP_SUBFLOW
) ||
1066 (so
->so_snd
.sb_flags
& SB_NOCOMPRESS
));
1068 if(!(flags
& PRUS_OOB
) || (so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
1069 /* Call msg send if message delivery is enabled */
1070 if (so
->so_flags
& SOF_ENABLE_MSGS
)
1071 sbappendmsg_snd(&so
->so_snd
, m
);
1073 sbappendstream(&so
->so_snd
, m
);
1075 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1078 * Do implied connect if not yet connected,
1079 * initialize window to default value, and
1080 * initialize maxseg/maxopd using peer's cached
1085 error
= tcp6_connect(tp
, nam
, p
);
1088 error
= tcp_connect(tp
, nam
, p
);
1091 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1092 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1095 if (flags
& PRUS_EOF
) {
1097 * Close the send side of the connection after
1101 tp
= tcp_usrclosed(tp
);
1104 if (flags
& PRUS_MORETOCOME
)
1105 tp
->t_flags
|= TF_MORETOCOME
;
1106 error
= tcp_output(tp
);
1107 if (flags
& PRUS_MORETOCOME
)
1108 tp
->t_flags
&= ~TF_MORETOCOME
;
1111 if (sbspace(&so
->so_snd
) == 0) {
1112 /* if no space is left in sockbuf,
1113 * do not try to squeeze in OOB traffic */
1119 * According to RFC961 (Assigned Protocols),
1120 * the urgent pointer points to the last octet
1121 * of urgent data. We continue, however,
1122 * to consider it to indicate the first octet
1123 * of data past the urgent section.
1124 * Otherwise, snd_up should be one lower.
1126 sbappendstream(&so
->so_snd
, m
);
1127 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1129 * Do implied connect if not yet connected,
1130 * initialize window to default value, and
1131 * initialize maxseg/maxopd using peer's cached
1136 error
= tcp6_connect(tp
, nam
, p
);
1139 error
= tcp_connect(tp
, nam
, p
);
1142 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1143 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1145 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
1146 tp
->t_flagsext
|= TF_FORCE
;
1147 error
= tcp_output(tp
);
1148 tp
->t_flagsext
&= ~TF_FORCE
;
1153 * We wait for the socket to successfully connect before returning.
1154 * This allows us to signal a timeout to the application.
1156 if (so
->so_state
& SS_ISCONNECTING
) {
1157 if (so
->so_state
& SS_NBIO
)
1158 error
= EWOULDBLOCK
;
1160 error
= sbwait(&so
->so_snd
);
1163 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
1164 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
1171 tcp_usr_abort(struct socket
*so
)
1174 struct inpcb
*inp
= sotoinpcb(so
);
1178 /* In case we got disconnected from the peer */
1181 tp
= tcp_drop(tp
, ECONNABORTED
);
1183 COMMON_END(PRU_ABORT
);
1187 * Receive out-of-band data.
1189 * Returns: 0 Success
1190 * EINVAL [COMMON_START]
1195 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
1198 struct inpcb
*inp
= sotoinpcb(so
);
1202 if ((so
->so_oobmark
== 0 &&
1203 (so
->so_state
& SS_RCVATMARK
) == 0) ||
1204 so
->so_options
& SO_OOBINLINE
||
1205 tp
->t_oobflags
& TCPOOB_HADDATA
) {
1209 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
1210 error
= EWOULDBLOCK
;
1214 *mtod(m
, caddr_t
) = tp
->t_iobc
;
1215 if ((flags
& MSG_PEEK
) == 0)
1216 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
1217 COMMON_END(PRU_RCVOOB
);
1221 tcp_usr_preconnect(struct socket
*so
)
1223 struct inpcb
*inp
= sotoinpcb(so
);
1227 if (necp_socket_should_use_flow_divert(inp
)) {
1228 /* May happen, if in tcp_usr_connect we did not had a chance
1229 * to set the usrreqs (due to some error). So, let's get out
1236 error
= tcp_output(sototcpcb(so
));
1238 /* One read has been done. This was enough. Get back to "normal" behavior. */
1239 so
->so_flags1
&= ~SOF1_PRECONNECT_DATA
;
1241 COMMON_END(PRU_PRECONNECT
);
1244 /* xxx - should be const */
1245 struct pr_usrreqs tcp_usrreqs
= {
1246 .pru_abort
= tcp_usr_abort
,
1247 .pru_accept
= tcp_usr_accept
,
1248 .pru_attach
= tcp_usr_attach
,
1249 .pru_bind
= tcp_usr_bind
,
1250 .pru_connect
= tcp_usr_connect
,
1251 .pru_connectx
= tcp_usr_connectx
,
1252 .pru_control
= in_control
,
1253 .pru_detach
= tcp_usr_detach
,
1254 .pru_disconnect
= tcp_usr_disconnect
,
1255 .pru_disconnectx
= tcp_usr_disconnectx
,
1256 .pru_listen
= tcp_usr_listen
,
1257 .pru_peeraddr
= in_getpeeraddr
,
1258 .pru_rcvd
= tcp_usr_rcvd
,
1259 .pru_rcvoob
= tcp_usr_rcvoob
,
1260 .pru_send
= tcp_usr_send
,
1261 .pru_shutdown
= tcp_usr_shutdown
,
1262 .pru_sockaddr
= in_getsockaddr
,
1263 .pru_sosend
= sosend
,
1264 .pru_soreceive
= soreceive
,
1265 .pru_preconnect
= tcp_usr_preconnect
,
1269 struct pr_usrreqs tcp6_usrreqs
= {
1270 .pru_abort
= tcp_usr_abort
,
1271 .pru_accept
= tcp6_usr_accept
,
1272 .pru_attach
= tcp_usr_attach
,
1273 .pru_bind
= tcp6_usr_bind
,
1274 .pru_connect
= tcp6_usr_connect
,
1275 .pru_connectx
= tcp6_usr_connectx
,
1276 .pru_control
= in6_control
,
1277 .pru_detach
= tcp_usr_detach
,
1278 .pru_disconnect
= tcp_usr_disconnect
,
1279 .pru_disconnectx
= tcp_usr_disconnectx
,
1280 .pru_listen
= tcp6_usr_listen
,
1281 .pru_peeraddr
= in6_mapped_peeraddr
,
1282 .pru_rcvd
= tcp_usr_rcvd
,
1283 .pru_rcvoob
= tcp_usr_rcvoob
,
1284 .pru_send
= tcp_usr_send
,
1285 .pru_shutdown
= tcp_usr_shutdown
,
1286 .pru_sockaddr
= in6_mapped_sockaddr
,
1287 .pru_sosend
= sosend
,
1288 .pru_soreceive
= soreceive
,
1289 .pru_preconnect
= tcp_usr_preconnect
,
1294 * Common subroutine to open a TCP connection to remote host specified
1295 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1296 * port number if needed. Call in_pcbladdr to do the routing and to choose
1297 * a local host address (interface). If there is an existing incarnation
1298 * of the same connection in TIME-WAIT state and if the remote host was
1299 * sending CC options and if the connection duration was < MSL, then
1300 * truncate the previous TIME-WAIT state and proceed.
1301 * Initialize connection parameters and enter SYN-SENT state.
1303 * Returns: 0 Success
1306 * in_pcbbind:EADDRNOTAVAIL Address not available.
1307 * in_pcbbind:EINVAL Invalid argument
1308 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1309 * in_pcbbind:EACCES Permission denied
1310 * in_pcbbind:EADDRINUSE Address in use
1311 * in_pcbbind:EAGAIN Resource unavailable, try again
1312 * in_pcbbind:EPERM Operation not permitted
1313 * in_pcbladdr:EINVAL Invalid argument
1314 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1315 * in_pcbladdr:EADDRNOTAVAIL Address not available
1318 tcp_connect(tp
, nam
, p
)
1319 register struct tcpcb
*tp
;
1320 struct sockaddr
*nam
;
1323 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1324 struct socket
*so
= inp
->inp_socket
;
1326 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)nam
;
1327 struct in_addr laddr
;
1329 struct ifnet
*outif
= NULL
;
1331 if (inp
->inp_lport
== 0) {
1332 error
= in_pcbbind(inp
, NULL
, p
);
1338 * Cannot simply call in_pcbconnect, because there might be an
1339 * earlier incarnation of this same connection still in
1340 * TIME_WAIT state, creating an ADDRINUSE error.
1342 error
= in_pcbladdr(inp
, nam
, &laddr
, IFSCOPE_NONE
, &outif
);
1346 tcp_unlock(inp
->inp_socket
, 0, 0);
1347 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
1348 sin
->sin_addr
, sin
->sin_port
,
1349 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
: laddr
,
1350 inp
->inp_lport
, 0, NULL
);
1352 tcp_lock(inp
->inp_socket
, 0, 0);
1354 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
1355 tcp_lock(oinp
->inp_socket
, 1, 0);
1356 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1358 tcp_unlock(oinp
->inp_socket
, 1, 0);
1362 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1363 otp
->t_state
== TCPS_TIME_WAIT
&&
1364 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1365 (otp
->t_flags
& TF_RCVD_CC
)) {
1366 otp
= tcp_close(otp
);
1368 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1369 (uint64_t)VM_KERNEL_ADDRPERM(inp
));
1371 tcp_unlock(oinp
->inp_socket
, 1, 0);
1376 tcp_unlock(oinp
->inp_socket
, 1, 0);
1379 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? laddr
.s_addr
:
1380 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
1381 inp
->inp_lport
== sin
->sin_port
) {
1385 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1386 /*lock inversion issue, mostly with udp multicast packets */
1387 socket_unlock(inp
->inp_socket
, 0);
1388 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1389 socket_lock(inp
->inp_socket
, 0);
1391 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1392 inp
->inp_laddr
= laddr
;
1393 /* no reference needed */
1394 inp
->inp_last_outifp
= outif
;
1395 inp
->inp_flags
|= INP_INADDR_ANY
;
1397 inp
->inp_faddr
= sin
->sin_addr
;
1398 inp
->inp_fport
= sin
->sin_port
;
1400 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1402 if (inp
->inp_flowhash
== 0)
1403 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1405 tcp_set_max_rwinscale(tp
, so
);
1408 tcpstat
.tcps_connattempt
++;
1409 tp
->t_state
= TCPS_SYN_SENT
;
1410 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, TCP_CONN_KEEPINIT(tp
));
1411 tp
->iss
= tcp_new_isn(tp
);
1412 tcp_sendseqinit(tp
);
1414 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1418 ifnet_release(outif
);
1425 tcp6_connect(tp
, nam
, p
)
1426 register struct tcpcb
*tp
;
1427 struct sockaddr
*nam
;
1430 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1431 struct socket
*so
= inp
->inp_socket
;
1433 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)nam
;
1434 struct in6_addr addr6
;
1436 struct ifnet
*outif
= NULL
;
1438 if (inp
->inp_lport
== 0) {
1439 error
= in6_pcbbind(inp
, NULL
, p
);
1445 * Cannot simply call in_pcbconnect, because there might be an
1446 * earlier incarnation of this same connection still in
1447 * TIME_WAIT state, creating an ADDRINUSE error.
1449 * in6_pcbladdr() might return an ifp with its reference held
1450 * even in the error case, so make sure that it's released
1451 * whenever it's non-NULL.
1453 error
= in6_pcbladdr(inp
, nam
, &addr6
, &outif
);
1456 tcp_unlock(inp
->inp_socket
, 0, 0);
1457 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1458 &sin6
->sin6_addr
, sin6
->sin6_port
,
1459 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1462 inp
->inp_lport
, 0, NULL
);
1463 tcp_lock(inp
->inp_socket
, 0, 0);
1465 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1466 otp
->t_state
== TCPS_TIME_WAIT
&&
1467 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1468 (otp
->t_flags
& TF_RCVD_CC
)) {
1469 otp
= tcp_close(otp
);
1475 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1476 /*lock inversion issue, mostly with udp multicast packets */
1477 socket_unlock(inp
->inp_socket
, 0);
1478 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1479 socket_lock(inp
->inp_socket
, 0);
1481 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
1482 inp
->in6p_laddr
= addr6
;
1483 inp
->in6p_last_outifp
= outif
; /* no reference needed */
1484 inp
->in6p_flags
|= INP_IN6ADDR_ANY
;
1486 inp
->in6p_faddr
= sin6
->sin6_addr
;
1487 inp
->inp_fport
= sin6
->sin6_port
;
1488 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1489 inp
->inp_flow
= sin6
->sin6_flowinfo
;
1491 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1493 if (inp
->inp_flowhash
== 0)
1494 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1495 /* update flowinfo - RFC 6437 */
1496 if (inp
->inp_flow
== 0 && inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
1497 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
1499 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
1502 tcp_set_max_rwinscale(tp
, so
);
1505 tcpstat
.tcps_connattempt
++;
1506 tp
->t_state
= TCPS_SYN_SENT
;
1507 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1508 TCP_CONN_KEEPINIT(tp
));
1509 tp
->iss
= tcp_new_isn(tp
);
1510 tcp_sendseqinit(tp
);
1512 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1516 ifnet_release(outif
);
1523 * Export TCP internal state information via a struct tcp_info
1525 __private_extern__
void
1526 tcp_fill_info(struct tcpcb
*tp
, struct tcp_info
*ti
)
1528 struct inpcb
*inp
= tp
->t_inpcb
;
1530 bzero(ti
, sizeof(*ti
));
1532 ti
->tcpi_state
= tp
->t_state
;
1533 ti
->tcpi_flowhash
= inp
->inp_flowhash
;
1535 if (tp
->t_state
> TCPS_LISTEN
) {
1536 if (TSTMP_SUPPORTED(tp
))
1537 ti
->tcpi_options
|= TCPI_OPT_TIMESTAMPS
;
1538 if (SACK_ENABLED(tp
))
1539 ti
->tcpi_options
|= TCPI_OPT_SACK
;
1540 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1541 ti
->tcpi_options
|= TCPI_OPT_WSCALE
;
1542 ti
->tcpi_snd_wscale
= tp
->snd_scale
;
1543 ti
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1545 if (TCP_ECN_ENABLED(tp
))
1546 ti
->tcpi_options
|= TCPI_OPT_ECN
;
1548 /* Are we in retranmission episode */
1549 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1550 ti
->tcpi_flags
|= TCPI_FLAG_LOSSRECOVERY
;
1552 ti
->tcpi_rto
= tp
->t_timer
[TCPT_REXMT
] ? tp
->t_rxtcur
: 0;
1553 ti
->tcpi_snd_mss
= tp
->t_maxseg
;
1554 ti
->tcpi_rcv_mss
= tp
->t_maxseg
;
1556 ti
->tcpi_rttcur
= tp
->t_rttcur
;
1557 ti
->tcpi_srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
1558 ti
->tcpi_rttvar
= tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
;
1559 ti
->tcpi_rttbest
= tp
->t_rttbest
>> TCP_RTT_SHIFT
;
1561 ti
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1562 ti
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1563 ti
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1565 ti
->tcpi_rcv_space
= tp
->rcv_wnd
;
1567 ti
->tcpi_snd_wnd
= tp
->snd_wnd
;
1568 ti
->tcpi_snd_nxt
= tp
->snd_nxt
;
1569 ti
->tcpi_rcv_nxt
= tp
->rcv_nxt
;
1571 /* convert bytes/msec to bits/sec */
1572 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1573 tp
->t_bwmeas
!= NULL
) {
1574 ti
->tcpi_snd_bw
= (tp
->t_bwmeas
->bw_sndbw
* 8000);
1577 ti
->tcpi_last_outif
= (tp
->t_inpcb
->inp_last_outifp
== NULL
) ? 0 :
1578 tp
->t_inpcb
->inp_last_outifp
->if_index
;
1580 //atomic_get_64(ti->tcpi_txbytes, &inp->inp_stat->txbytes);
1581 ti
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1582 ti
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1583 ti
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1584 ti
->tcpi_txunacked
= tp
->snd_max
- tp
->snd_una
;
1586 //atomic_get_64(ti->tcpi_rxbytes, &inp->inp_stat->rxbytes);
1587 ti
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1588 ti
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1589 ti
->tcpi_rxduplicatebytes
= tp
->t_stat
.rxduplicatebytes
;
1590 ti
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1592 if (tp
->t_state
> TCPS_LISTEN
) {
1593 ti
->tcpi_synrexmits
= tp
->t_stat
.synrxtshift
;
1595 ti
->tcpi_cell_rxpackets
= inp
->inp_cstat
->rxpackets
;
1596 ti
->tcpi_cell_rxbytes
= inp
->inp_cstat
->rxbytes
;
1597 ti
->tcpi_cell_txpackets
= inp
->inp_cstat
->txpackets
;
1598 ti
->tcpi_cell_txbytes
= inp
->inp_cstat
->txbytes
;
1600 ti
->tcpi_wifi_rxpackets
= inp
->inp_wstat
->rxpackets
;
1601 ti
->tcpi_wifi_rxbytes
= inp
->inp_wstat
->rxbytes
;
1602 ti
->tcpi_wifi_txpackets
= inp
->inp_wstat
->txpackets
;
1603 ti
->tcpi_wifi_txbytes
= inp
->inp_wstat
->txbytes
;
1605 ti
->tcpi_wired_rxpackets
= inp
->inp_Wstat
->rxpackets
;
1606 ti
->tcpi_wired_rxbytes
= inp
->inp_Wstat
->rxbytes
;
1607 ti
->tcpi_wired_txpackets
= inp
->inp_Wstat
->txpackets
;
1608 ti
->tcpi_wired_txbytes
= inp
->inp_Wstat
->txbytes
;
1609 tcp_get_connectivity_status(tp
, &ti
->tcpi_connstatus
);
1611 ti
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1612 ti
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1613 ti
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1614 ti
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1616 ti
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1617 ti
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1618 ti
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1619 ti
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1620 ti
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1622 ti
->tcpi_ecn_client_setup
= !!(tp
->ecn_flags
& TE_SETUPSENT
);
1623 ti
->tcpi_ecn_server_setup
= !!(tp
->ecn_flags
& TE_SETUPRECEIVED
);
1624 ti
->tcpi_ecn_success
= (tp
->ecn_flags
& TE_ECN_ON
) == TE_ECN_ON
? 1 : 0;
1625 ti
->tcpi_ecn_lost_syn
= !!(tp
->ecn_flags
& TE_LOST_SYN
);
1626 ti
->tcpi_ecn_lost_synack
= !!(tp
->ecn_flags
& TE_LOST_SYNACK
);
1628 ti
->tcpi_local_peer
= !!(tp
->t_flags
& TF_LOCAL
);
1630 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
) {
1631 if (IFNET_IS_CELLULAR(tp
->t_inpcb
->inp_last_outifp
))
1632 ti
->tcpi_if_cell
= 1;
1633 else if (IFNET_IS_WIFI(tp
->t_inpcb
->inp_last_outifp
))
1634 ti
->tcpi_if_wifi
= 1;
1637 ti
->tcpi_ecn_recv_ce
= tp
->t_ecn_recv_ce
;
1638 ti
->tcpi_ecn_recv_cwr
= tp
->t_ecn_recv_cwr
;
1640 ti
->tcpi_rcvoopack
= tp
->t_rcvoopack
;
1641 ti
->tcpi_pawsdrop
= tp
->t_pawsdrop
;
1642 ti
->tcpi_sack_recovery_episode
= tp
->t_sack_recovery_episode
;
1643 ti
->tcpi_reordered_pkts
= tp
->t_reordered_pkts
;
1644 ti
->tcpi_dsack_sent
= tp
->t_dsack_sent
;
1645 ti
->tcpi_dsack_recvd
= tp
->t_dsack_recvd
;
1649 __private_extern__ errno_t
1650 tcp_fill_info_for_info_tuple(struct info_tuple
*itpl
, struct tcp_info
*ti
)
1652 struct inpcbinfo
*pcbinfo
= NULL
;
1653 struct inpcb
*inp
= NULL
;
1657 if (itpl
->itpl_proto
== IPPROTO_TCP
)
1662 if (itpl
->itpl_local_sa
.sa_family
== AF_INET
&&
1663 itpl
->itpl_remote_sa
.sa_family
== AF_INET
) {
1664 inp
= in_pcblookup_hash(pcbinfo
,
1665 itpl
->itpl_remote_sin
.sin_addr
,
1666 itpl
->itpl_remote_sin
.sin_port
,
1667 itpl
->itpl_local_sin
.sin_addr
,
1668 itpl
->itpl_local_sin
.sin_port
,
1670 } else if (itpl
->itpl_local_sa
.sa_family
== AF_INET6
&&
1671 itpl
->itpl_remote_sa
.sa_family
== AF_INET6
) {
1672 struct in6_addr ina6_local
;
1673 struct in6_addr ina6_remote
;
1675 ina6_local
= itpl
->itpl_local_sin6
.sin6_addr
;
1676 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local
) &&
1677 itpl
->itpl_local_sin6
.sin6_scope_id
)
1678 ina6_local
.s6_addr16
[1] = htons(itpl
->itpl_local_sin6
.sin6_scope_id
);
1680 ina6_remote
= itpl
->itpl_remote_sin6
.sin6_addr
;
1681 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote
) &&
1682 itpl
->itpl_remote_sin6
.sin6_scope_id
)
1683 ina6_remote
.s6_addr16
[1] = htons(itpl
->itpl_remote_sin6
.sin6_scope_id
);
1685 inp
= in6_pcblookup_hash(pcbinfo
,
1687 itpl
->itpl_remote_sin6
.sin6_port
,
1689 itpl
->itpl_local_sin6
.sin6_port
,
1694 if (inp
== NULL
|| (so
= inp
->inp_socket
) == NULL
)
1698 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1699 socket_unlock(so
, 0);
1702 tp
= intotcpcb(inp
);
1704 tcp_fill_info(tp
, ti
);
1705 socket_unlock(so
, 0);
1711 tcp_connection_fill_info(struct tcpcb
*tp
, struct tcp_connection_info
*tci
)
1713 struct inpcb
*inp
= tp
->t_inpcb
;
1715 bzero(tci
, sizeof(*tci
));
1716 tci
->tcpi_state
= tp
->t_state
;
1717 if (tp
->t_state
> TCPS_LISTEN
) {
1718 if (TSTMP_SUPPORTED(tp
))
1719 tci
->tcpi_options
|= TCPCI_OPT_TIMESTAMPS
;
1720 if (SACK_ENABLED(tp
))
1721 tci
->tcpi_options
|= TCPCI_OPT_SACK
;
1722 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1723 tci
->tcpi_options
|= TCPCI_OPT_WSCALE
;
1724 tci
->tcpi_snd_wscale
= tp
->snd_scale
;
1725 tci
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1727 if (TCP_ECN_ENABLED(tp
))
1728 tci
->tcpi_options
|= TCPCI_OPT_ECN
;
1729 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1730 tci
->tcpi_flags
|= TCPCI_FLAG_LOSSRECOVERY
;
1731 if (tp
->t_flagsext
& TF_PKTS_REORDERED
)
1732 tci
->tcpi_flags
|= TCPCI_FLAG_REORDERING_DETECTED
;
1733 tci
->tcpi_rto
= (tp
->t_timer
[TCPT_REXMT
] > 0) ?
1735 tci
->tcpi_maxseg
= tp
->t_maxseg
;
1736 tci
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1737 tci
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1738 tci
->tcpi_snd_wnd
= tp
->snd_wnd
;
1739 tci
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1740 tci
->tcpi_rcv_wnd
= tp
->rcv_wnd
;
1741 tci
->tcpi_rttcur
= tp
->t_rttcur
;
1742 tci
->tcpi_srtt
= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1743 tci
->tcpi_rttvar
= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
1744 tci
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1745 tci
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1746 tci
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1747 tci
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1748 tci
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1749 tci
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1751 tci
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1752 tci
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1753 tci
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1754 tci
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1755 tci
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1756 tci
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1757 tci
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1758 tci
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1759 tci
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1764 __private_extern__
int
1765 tcp_sysctl_info(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
1769 struct info_tuple itpl
;
1770 proc_t caller
= PROC_NULL
;
1771 proc_t caller_parent
= PROC_NULL
;
1772 char command_name
[MAXCOMLEN
+ 1] = "";
1773 char parent_name
[MAXCOMLEN
+ 1] = "";
1775 if ((caller
= proc_self()) != PROC_NULL
) {
1776 /* get process name */
1777 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
1779 /* get parent process name if possible */
1780 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
1781 strlcpy(parent_name
, caller_parent
->p_comm
,
1782 sizeof(parent_name
));
1783 proc_rele(caller_parent
);
1786 if ((escape_str(command_name
, strlen(command_name
),
1787 sizeof(command_name
)) == 0) &&
1788 (escape_str(parent_name
, strlen(parent_name
),
1789 sizeof(parent_name
)) == 0)) {
1790 kern_asl_msg(LOG_DEBUG
, "messagetracer",
1792 "com.apple.message.domain",
1793 "com.apple.kernel.tcpstat", /* 1 */
1794 "com.apple.message.signature",
1796 "com.apple.message.signature2", command_name
, /* 3 */
1797 "com.apple.message.signature3", parent_name
, /* 4 */
1798 "com.apple.message.summarize", "YES", /* 5 */
1803 if (caller
!= PROC_NULL
)
1806 if (req
->newptr
== USER_ADDR_NULL
) {
1809 if (req
->newlen
< sizeof(struct info_tuple
)) {
1812 error
= SYSCTL_IN(req
, &itpl
, sizeof(struct info_tuple
));
1816 error
= tcp_fill_info_for_info_tuple(&itpl
, &ti
);
1820 error
= SYSCTL_OUT(req
, &ti
, sizeof(struct tcp_info
));
1829 tcp_lookup_peer_pid_locked(struct socket
*so
, pid_t
*out_pid
)
1831 int error
= EHOSTUNREACH
;
1833 if ((so
->so_state
& SS_ISCONNECTED
) == 0) return ENOTCONN
;
1835 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
1836 uint16_t lport
= inp
->inp_lport
;
1837 uint16_t fport
= inp
->inp_fport
;
1838 struct inpcb
*finp
= NULL
;
1840 if (inp
->inp_vflag
& INP_IPV6
) {
1841 struct in6_addr laddr6
= inp
->in6p_laddr
;
1842 struct in6_addr faddr6
= inp
->in6p_faddr
;
1843 socket_unlock(so
, 0);
1844 finp
= in6_pcblookup_hash(&tcbinfo
, &laddr6
, lport
, &faddr6
, fport
, 0, NULL
);
1846 } else if (inp
->inp_vflag
& INP_IPV4
) {
1847 struct in_addr laddr4
= inp
->inp_laddr
;
1848 struct in_addr faddr4
= inp
->inp_faddr
;
1849 socket_unlock(so
, 0);
1850 finp
= in_pcblookup_hash(&tcbinfo
, laddr4
, lport
, faddr4
, fport
, 0, NULL
);
1855 *out_pid
= finp
->inp_socket
->last_pid
;
1857 in_pcb_checkstate(finp
, WNT_RELEASE
, 0);
1864 tcp_getconninfo(struct socket
*so
, struct conninfo_tcp
*tcp_ci
)
1866 (void) tcp_lookup_peer_pid_locked(so
, &tcp_ci
->tcpci_peer_pid
);
1867 tcp_fill_info(sototcpcb(so
), &tcp_ci
->tcpci_tcp_info
);
1871 * The new sockopt interface makes it possible for us to block in the
1872 * copyin/out step (if we take a page fault). Taking a page fault at
1873 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1874 * use TSM, there probably isn't any need for this function to run at
1875 * splnet() any more. This needs more examination.)
1878 tcp_ctloutput(so
, sopt
)
1880 struct sockopt
*sopt
;
1882 int error
, opt
, optval
;
1887 inp
= sotoinpcb(so
);
1889 return (ECONNRESET
);
1891 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
1892 if (sopt
->sopt_level
!= IPPROTO_TCP
&&
1893 !(sopt
->sopt_level
== SOL_SOCKET
&& (sopt
->sopt_name
== SO_FLUSH
||
1894 sopt
->sopt_name
== SO_TRAFFIC_MGT_BACKGROUND
))) {
1896 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1897 error
= ip6_ctloutput(so
, sopt
);
1900 error
= ip_ctloutput(so
, sopt
);
1903 tp
= intotcpcb(inp
);
1905 return (ECONNRESET
);
1908 calculate_tcp_clock();
1910 switch (sopt
->sopt_dir
) {
1912 switch (sopt
->sopt_name
) {
1916 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1921 switch (sopt
->sopt_name
) {
1932 opt
= 0; /* dead code to fool gcc */
1939 tp
->t_flags
&= ~opt
;
1941 case TCP_RXT_FINDROP
:
1942 case TCP_NOTIMEWAIT
:
1943 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1947 switch (sopt
->sopt_name
) {
1948 case TCP_RXT_FINDROP
:
1949 opt
= TF_RXTFINDROP
;
1951 case TCP_NOTIMEWAIT
:
1952 opt
= TF_NOTIMEWAIT
;
1959 tp
->t_flagsext
|= opt
;
1961 tp
->t_flagsext
&= ~opt
;
1963 case TCP_MEASURE_SND_BW
:
1964 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1968 opt
= TF_MEASURESNDBW
;
1970 if (tp
->t_bwmeas
== NULL
) {
1971 tp
->t_bwmeas
= tcp_bwmeas_alloc(tp
);
1972 if (tp
->t_bwmeas
== NULL
) {
1977 tp
->t_flagsext
|= opt
;
1979 tp
->t_flagsext
&= ~opt
;
1980 /* Reset snd bw measurement state */
1981 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
1982 if (tp
->t_bwmeas
!= NULL
) {
1983 tcp_bwmeas_free(tp
);
1987 case TCP_MEASURE_BW_BURST
: {
1988 struct tcp_measure_bw_burst in
;
1989 uint32_t minpkts
, maxpkts
;
1990 bzero(&in
, sizeof(in
));
1992 error
= sooptcopyin(sopt
, &in
, sizeof(in
),
1996 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
1997 tp
->t_bwmeas
== NULL
) {
2001 minpkts
= (in
.min_burst_size
!= 0) ? in
.min_burst_size
:
2002 tp
->t_bwmeas
->bw_minsizepkts
;
2003 maxpkts
= (in
.max_burst_size
!= 0) ? in
.max_burst_size
:
2004 tp
->t_bwmeas
->bw_maxsizepkts
;
2005 if (minpkts
> maxpkts
) {
2009 tp
->t_bwmeas
->bw_minsizepkts
= minpkts
;
2010 tp
->t_bwmeas
->bw_maxsizepkts
= maxpkts
;
2011 tp
->t_bwmeas
->bw_minsize
= (minpkts
* tp
->t_maxseg
);
2012 tp
->t_bwmeas
->bw_maxsize
= (maxpkts
* tp
->t_maxseg
);
2016 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2021 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
2022 optval
+ 40 >= tcp_minmss
)
2023 tp
->t_maxseg
= optval
;
2029 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2033 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2036 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
2037 /* reset the timer to new value */
2038 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2039 TCP_CONN_KEEPIDLE(tp
));
2040 tcp_check_timer_state(tp
);
2044 case TCP_CONNECTIONTIMEOUT
:
2045 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2049 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2052 tp
->t_keepinit
= optval
* TCP_RETRANSHZ
;
2053 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
2054 tp
->t_state
== TCPS_SYN_SENT
) {
2055 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2056 TCP_CONN_KEEPINIT(tp
));
2057 tcp_check_timer_state(tp
);
2063 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2067 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2070 tp
->t_keepintvl
= optval
* TCP_RETRANSHZ
;
2071 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2072 TCP_CONN_MAXIDLE(tp
) > 0) {
2073 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2074 TCP_CONN_MAXIDLE(tp
));
2075 tcp_check_timer_state(tp
);
2081 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2085 if (optval
< 0 || optval
> INT32_MAX
) {
2088 tp
->t_keepcnt
= optval
;
2089 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2090 TCP_CONN_MAXIDLE(tp
) > 0) {
2091 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2092 TCP_CONN_MAXIDLE(tp
));
2093 tcp_check_timer_state(tp
);
2098 case PERSIST_TIMEOUT
:
2099 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2106 tp
->t_persist_timeout
= optval
* TCP_RETRANSHZ
;
2108 case TCP_RXT_CONNDROPTIME
:
2109 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2116 tp
->t_rxt_conndroptime
= optval
* TCP_RETRANSHZ
;
2118 case TCP_NOTSENT_LOWAT
:
2119 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2128 so
->so_flags
&= ~(SOF_NOTSENT_LOWAT
);
2129 tp
->t_notsent_lowat
= 0;
2131 so
->so_flags
|= SOF_NOTSENT_LOWAT
;
2132 tp
->t_notsent_lowat
= optval
;
2136 case TCP_ADAPTIVE_READ_TIMEOUT
:
2137 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2142 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2145 } else if (optval
== 0) {
2146 tp
->t_adaptive_rtimo
= 0;
2147 tcp_keepalive_reset(tp
);
2149 tp
->t_adaptive_rtimo
= optval
;
2152 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2153 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2158 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2162 tp
->t_adaptive_wtimo
= optval
;
2165 case TCP_ENABLE_MSGS
:
2166 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2170 if (optval
< 0 || optval
> 1) {
2172 } else if (optval
== 1) {
2174 * Check if messages option is already
2175 * enabled, if so return.
2177 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2178 VERIFY(so
->so_msg_state
!= NULL
);
2183 * allocate memory for storing message
2186 VERIFY(so
->so_msg_state
== NULL
);
2187 MALLOC(so
->so_msg_state
,
2189 sizeof(struct msg_state
),
2190 M_TEMP
, M_WAITOK
| M_ZERO
);
2191 if (so
->so_msg_state
== NULL
) {
2196 /* Enable message delivery */
2197 so
->so_flags
|= SOF_ENABLE_MSGS
;
2200 * Can't disable message delivery on socket
2201 * because of restrictions imposed by
2207 case TCP_SENDMOREACKS
:
2208 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2212 if (optval
< 0 || optval
> 1) {
2214 } else if (optval
== 0) {
2215 tp
->t_flagsext
&= ~(TF_NOSTRETCHACK
);
2217 tp
->t_flagsext
|= TF_NOSTRETCHACK
;
2220 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2221 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2225 if (optval
< 0 || optval
> 1) {
2227 } else if (optval
== 0) {
2228 tp
->t_flagsext
&= ~TF_NOBLACKHOLE_DETECTION
;
2230 tp
->t_flagsext
|= TF_NOBLACKHOLE_DETECTION
;
2231 if ((tp
->t_flags
& TF_BLACKHOLE
) &&
2232 tp
->t_pmtud_saved_maxopd
> 0)
2233 tcp_pmtud_revert_segment_size(tp
);
2237 if (!(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2242 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2246 if (optval
< 0 || optval
> 1) {
2250 if (tp
->t_state
!= TCPS_LISTEN
) {
2255 tp
->t_flagsext
|= TF_FASTOPEN
;
2257 tcp_disable_tfo(tp
);
2259 case TCP_ENABLE_ECN
:
2260 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2265 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2266 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2268 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2272 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2276 if (optval
== ECN_MODE_DEFAULT
) {
2277 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2278 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2279 } else if (optval
== ECN_MODE_ENABLE
) {
2280 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2281 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2282 } else if (optval
== ECN_MODE_DISABLE
) {
2283 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2284 tp
->ecn_flags
|= TE_ECN_MODE_DISABLE
;
2290 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2291 sizeof (optval
))) != 0)
2294 error
= inp_flush(inp
, optval
);
2297 case SO_TRAFFIC_MGT_BACKGROUND
:
2298 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2299 sizeof (optval
))) != 0)
2303 socket_set_traffic_mgt_flags_locked(so
,
2304 TRAFFIC_MGT_SO_BACKGROUND
);
2306 socket_clear_traffic_mgt_flags_locked(so
,
2307 TRAFFIC_MGT_SO_BACKGROUND
);
2312 error
= ENOPROTOOPT
;
2318 switch (sopt
->sopt_name
) {
2320 optval
= tp
->t_flags
& TF_NODELAY
;
2323 optval
= tp
->t_maxseg
;
2326 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
2329 optval
= tp
->t_keepintvl
/ TCP_RETRANSHZ
;
2332 optval
= tp
->t_keepcnt
;
2335 optval
= tp
->t_flags
& TF_NOOPT
;
2338 optval
= tp
->t_flags
& TF_NOPUSH
;
2340 case TCP_ENABLE_ECN
:
2341 optval
= (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
) ? 1 : 0;
2344 if (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
)
2345 optval
= ECN_MODE_ENABLE
;
2346 else if (tp
->ecn_flags
& TE_ECN_MODE_DISABLE
)
2347 optval
= ECN_MODE_DISABLE
;
2349 optval
= ECN_MODE_DEFAULT
;
2351 case TCP_CONNECTIONTIMEOUT
:
2352 optval
= tp
->t_keepinit
/ TCP_RETRANSHZ
;
2354 case PERSIST_TIMEOUT
:
2355 optval
= tp
->t_persist_timeout
/ TCP_RETRANSHZ
;
2357 case TCP_RXT_CONNDROPTIME
:
2358 optval
= tp
->t_rxt_conndroptime
/ TCP_RETRANSHZ
;
2360 case TCP_RXT_FINDROP
:
2361 optval
= tp
->t_flagsext
& TF_RXTFINDROP
;
2363 case TCP_NOTIMEWAIT
:
2364 optval
= (tp
->t_flagsext
& TF_NOTIMEWAIT
) ? 1 : 0;
2367 if (tp
->t_state
!= TCPS_LISTEN
||
2368 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2372 optval
= tfo_enabled(tp
);
2374 case TCP_MEASURE_SND_BW
:
2375 optval
= tp
->t_flagsext
& TF_MEASURESNDBW
;
2380 tcp_fill_info(tp
, &ti
);
2381 error
= sooptcopyout(sopt
, &ti
, sizeof(struct tcp_info
));
2385 case TCP_CONNECTION_INFO
: {
2386 struct tcp_connection_info tci
;
2387 tcp_connection_fill_info(tp
, &tci
);
2388 error
= sooptcopyout(sopt
, &tci
,
2389 sizeof(struct tcp_connection_info
));
2392 case TCP_MEASURE_BW_BURST
: {
2393 struct tcp_measure_bw_burst out
;
2394 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2395 tp
->t_bwmeas
== NULL
) {
2399 out
.min_burst_size
= tp
->t_bwmeas
->bw_minsizepkts
;
2400 out
.max_burst_size
= tp
->t_bwmeas
->bw_maxsizepkts
;
2401 error
= sooptcopyout(sopt
, &out
, sizeof(out
));
2404 case TCP_NOTSENT_LOWAT
:
2405 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0) {
2406 optval
= tp
->t_notsent_lowat
;
2412 case TCP_ENABLE_MSGS
:
2413 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2419 case TCP_SENDMOREACKS
:
2420 if (tp
->t_flagsext
& TF_NOSTRETCHACK
)
2425 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2426 if (tp
->t_flagsext
& TF_NOBLACKHOLE_DETECTION
)
2431 case TCP_PEER_PID
: {
2433 error
= tcp_lookup_peer_pid_locked(so
, &pid
);
2435 error
= sooptcopyout(sopt
, &pid
, sizeof(pid
));
2438 case TCP_ADAPTIVE_READ_TIMEOUT
:
2439 optval
= tp
->t_adaptive_rtimo
;
2441 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2442 optval
= tp
->t_adaptive_wtimo
;
2444 case SO_TRAFFIC_MGT_BACKGROUND
:
2445 optval
= (so
->so_traffic_mgt_flags
&
2446 TRAFFIC_MGT_SO_BACKGROUND
) ? 1 : 0;
2449 error
= ENOPROTOOPT
;
2453 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2461 * tcp_sendspace and tcp_recvspace are the default send and receive window
2462 * sizes, respectively. These are obsolescent (this information should
2463 * be set by the route).
2465 u_int32_t tcp_sendspace
= 1448*256;
2466 u_int32_t tcp_recvspace
= 1448*384;
2468 /* During attach, the size of socket buffer allocated is limited to
2469 * sb_max in sbreserve. Disallow setting the tcp send and recv space
2470 * to be more than sb_max because that will cause tcp_attach to fail
2471 * (see radar 5713060)
2474 sysctl_tcp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
2475 __unused
int arg2
, struct sysctl_req
*req
) {
2476 u_int32_t new_value
= 0, *space_p
= NULL
;
2477 int changed
= 0, error
= 0;
2478 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
2480 switch (oidp
->oid_number
) {
2481 case TCPCTL_SENDSPACE
:
2482 space_p
= &tcp_sendspace
;
2484 case TCPCTL_RECVSPACE
:
2485 space_p
= &tcp_recvspace
;
2490 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
2491 &new_value
, &changed
);
2493 if (new_value
> 0 && new_value
<= sb_effective_max
) {
2494 *space_p
= new_value
;
2502 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2503 &tcp_sendspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum outgoing TCP datagram size");
2504 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2505 &tcp_recvspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum incoming TCP datagram size");
2507 /* Sysctl for testing and tuning the connectx with data api */
2508 #define TCP_PRECONNECT_SBSZ_MAX 1460
2509 #define TCP_PRECONNECT_SBSZ_MIN (TCP_MSS)
2510 #define TCP_PRECONNECT_SBSZ_DEF (TCP6_MSS)
2511 static int tcp_preconnect_sbspace
= TCP_PRECONNECT_SBSZ_DEF
;
2512 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, preconn_sbsz
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
2513 &tcp_preconnect_sbspace
, 0, "Maximum preconnect space");
2517 * Attach TCP protocol to socket, allocating
2518 * internet protocol control block, tcp control block,
2519 * bufer space, and entering LISTEN state if to accept connections.
2521 * Returns: 0 Success
2522 * in_pcballoc:ENOBUFS
2523 * in_pcballoc:ENOMEM
2524 * in_pcballoc:??? [IPSEC specific]
2532 register struct tcpcb
*tp
;
2536 int isipv6
= SOCK_CHECK_DOM(so
, PF_INET6
) != 0;
2539 error
= in_pcballoc(so
, &tcbinfo
, p
);
2543 inp
= sotoinpcb(so
);
2545 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
2546 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
2551 if (so
->so_snd
.sb_preconn_hiwat
== 0) {
2552 soreserve_preconnect(so
, imin(TCP_PRECONNECT_SBSZ_MAX
,
2553 imax(tcp_preconnect_sbspace
, TCP_PRECONNECT_SBSZ_MIN
)));
2556 if ((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0)
2557 so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
2558 if ((so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0)
2559 so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
2563 inp
->inp_vflag
|= INP_IPV6
;
2564 inp
->in6p_hops
= -1; /* use kernel default */
2568 inp
->inp_vflag
|= INP_IPV4
;
2569 tp
= tcp_newtcpcb(inp
);
2571 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
2573 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
2580 so
->so_state
|= nofd
;
2584 nstat_tcp_new_pcb(inp
);
2585 tp
->t_state
= TCPS_CLOSED
;
2590 * Initiate (or continue) disconnect.
2591 * If embryonic state, just send reset (once).
2592 * If in ``let data drain'' option and linger null, just drop.
2593 * Otherwise (hard), mark socket disconnecting and drop
2594 * current input data; switch states based on user close, and
2595 * send segment to peer (with FIN).
2597 static struct tcpcb
*
2599 register struct tcpcb
*tp
;
2601 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2603 if (tp
->t_state
< TCPS_ESTABLISHED
)
2605 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
2606 tp
= tcp_drop(tp
, 0);
2608 soisdisconnecting(so
);
2609 sbflush(&so
->so_rcv
);
2610 tp
= tcp_usrclosed(tp
);
2612 /* A reset has been sent but socket exists, do not send FIN */
2613 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
2614 (tp
) && (tp
->t_mpflags
& TMPF_RESET
))
2618 (void) tcp_output(tp
);
2624 * User issued close, and wish to trail through shutdown states:
2625 * if never received SYN, just forget it. If got a SYN from peer,
2626 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2627 * If already got a FIN from peer, then almost done; go to LAST_ACK
2628 * state. In all other cases, have already sent FIN to peer (e.g.
2629 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2630 * for peer to send FIN or not respond to keep-alives, etc.
2631 * We can let the user exit from the close as soon as the FIN is acked.
2633 static struct tcpcb
*
2635 register struct tcpcb
*tp
;
2638 switch (tp
->t_state
) {
2646 case TCPS_SYN_RECEIVED
:
2647 tp
->t_flags
|= TF_NEEDFIN
;
2650 case TCPS_ESTABLISHED
:
2651 DTRACE_TCP4(state__change
, void, NULL
,
2652 struct inpcb
*, tp
->t_inpcb
,
2654 int32_t, TCPS_FIN_WAIT_1
);
2655 tp
->t_state
= TCPS_FIN_WAIT_1
;
2658 case TCPS_CLOSE_WAIT
:
2659 DTRACE_TCP4(state__change
, void, NULL
,
2660 struct inpcb
*, tp
->t_inpcb
,
2662 int32_t, TCPS_LAST_ACK
);
2663 tp
->t_state
= TCPS_LAST_ACK
;
2666 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
2667 soisdisconnected(tp
->t_inpcb
->inp_socket
);
2668 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
2669 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
2670 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2671 TCP_CONN_MAXIDLE(tp
));
2677 tcp_in_cksum_stats(u_int32_t len
)
2679 tcpstat
.tcps_rcv_swcsum
++;
2680 tcpstat
.tcps_rcv_swcsum_bytes
+= len
;
2684 tcp_out_cksum_stats(u_int32_t len
)
2686 tcpstat
.tcps_snd_swcsum
++;
2687 tcpstat
.tcps_snd_swcsum_bytes
+= len
;
2692 tcp_in6_cksum_stats(u_int32_t len
)
2694 tcpstat
.tcps_rcv6_swcsum
++;
2695 tcpstat
.tcps_rcv6_swcsum_bytes
+= len
;
2699 tcp_out6_cksum_stats(u_int32_t len
)
2701 tcpstat
.tcps_snd6_swcsum
++;
2702 tcpstat
.tcps_snd6_swcsum_bytes
+= len
;
2706 * When messages are enabled on a TCP socket, the message priority
2707 * is sent as a control message. This function will extract it.
2710 tcp_get_msg_priority(struct mbuf
*control
, uint32_t *msgpri
)
2713 if (control
== NULL
)
2716 for (cm
= M_FIRST_CMSGHDR(control
); cm
;
2717 cm
= M_NXT_CMSGHDR(control
, cm
)) {
2718 if (cm
->cmsg_len
< sizeof(struct cmsghdr
) ||
2719 cm
->cmsg_len
> control
->m_len
) {
2722 if (cm
->cmsg_level
== SOL_SOCKET
&&
2723 cm
->cmsg_type
== SCM_MSG_PRIORITY
) {
2724 *msgpri
= *(unsigned int *)(void *)CMSG_DATA(cm
);
2729 VERIFY(*msgpri
>= MSG_PRI_MIN
&& *msgpri
<= MSG_PRI_MAX
);