2 * Copyright (c) 2000-2017 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>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/protosw.h>
79 #include <sys/syslog.h>
82 #include <net/route.h>
83 #include <net/ntstat.h>
84 #include <net/content_filter.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
89 #include <netinet/ip6.h>
91 #include <netinet/in_pcb.h>
93 #include <netinet6/in6_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
98 #include <netinet6/ip6_var.h>
100 #include <netinet/tcp.h>
101 #include <netinet/tcp_fsm.h>
102 #include <netinet/tcp_seq.h>
103 #include <netinet/tcp_timer.h>
104 #include <netinet/tcp_var.h>
105 #include <netinet/tcpip.h>
106 #include <netinet/tcp_cc.h>
107 #include <mach/sdt.h>
109 #include <netinet/tcp_debug.h>
112 #include <netinet/mptcp_var.h>
116 #include <netinet6/ipsec.h>
120 #include <netinet/flow_divert.h>
121 #endif /* FLOW_DIVERT */
123 errno_t
tcp_fill_info_for_info_tuple(struct info_tuple
*, struct tcp_info
*);
125 int tcp_sysctl_info(struct sysctl_oid
*, void *, int , struct sysctl_req
*);
126 static void tcp_connection_fill_info(struct tcpcb
*tp
,
127 struct tcp_connection_info
*tci
);
130 * TCP protocol interface to socket abstraction.
132 extern char *tcpstates
[]; /* XXX ??? */
134 static int tcp_attach(struct socket
*, struct proc
*);
135 static int tcp_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
137 static int tcp6_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
138 static int tcp6_usr_connect(struct socket
*, struct sockaddr
*,
141 static struct tcpcb
*tcp_disconnect(struct tcpcb
*);
142 static struct tcpcb
*tcp_usrclosed(struct tcpcb
*);
143 extern void tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sb
);
146 #define TCPDEBUG0 int ostate = 0
147 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
148 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
149 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
153 #define TCPDEBUG2(req)
156 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, info
,
157 CTLFLAG_RW
| CTLFLAG_LOCKED
| CTLFLAG_ANYBODY
| CTLFLAG_KERN
,
158 0 , 0, tcp_sysctl_info
, "S", "TCP info per tuple");
161 * TCP attaches to socket via pru_attach(), reserving space,
162 * and an internet control block.
168 * tcp_attach:??? [IPSEC specific]
171 tcp_usr_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
174 struct inpcb
*inp
= sotoinpcb(so
);
175 struct tcpcb
*tp
= 0;
184 error
= tcp_attach(so
, p
);
188 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
189 so
->so_linger
= TCP_LINGERTIME
* hz
;
192 TCPDEBUG2(PRU_ATTACH
);
197 * pru_detach() detaches the TCP protocol from the socket.
198 * If the protocol state is non-embryonic, then can't
199 * do this directly: have to initiate a pru_disconnect(),
200 * which may finish later; embryonic TCB's can just
204 tcp_usr_detach(struct socket
*so
)
207 struct inpcb
*inp
= sotoinpcb(so
);
211 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
212 return EINVAL
; /* XXX */
214 socket_lock_assert_owned(so
);
216 /* In case we got disconnected from the peer */
221 calculate_tcp_clock();
223 tp
= tcp_disconnect(tp
);
225 TCPDEBUG2(PRU_DETACH
);
230 #define COMMON_START() TCPDEBUG0; \
232 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
234 if (necp_socket_should_use_flow_divert(inp)) \
235 return (EPROTOTYPE); \
236 tp = intotcpcb(inp); \
238 calculate_tcp_clock(); \
241 #define COMMON_START() TCPDEBUG0; \
243 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
245 tp = intotcpcb(inp); \
247 calculate_tcp_clock(); \
251 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
255 * Give the socket an address.
258 * EINVAL Invalid argument [COMMON_START]
259 * EAFNOSUPPORT Address family not supported
260 * in_pcbbind:EADDRNOTAVAIL Address not available.
261 * in_pcbbind:EINVAL Invalid argument
262 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
263 * in_pcbbind:EACCES Permission denied
264 * in_pcbbind:EADDRINUSE Address in use
265 * in_pcbbind:EAGAIN Resource unavailable, try again
266 * in_pcbbind:EPERM Operation not permitted
269 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
272 struct inpcb
*inp
= sotoinpcb(so
);
274 struct sockaddr_in
*sinp
;
278 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
279 error
= EAFNOSUPPORT
;
284 * Must check for multicast addresses and disallow binding
287 sinp
= (struct sockaddr_in
*)(void *)nam
;
288 if (sinp
->sin_family
== AF_INET
&&
289 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
290 error
= EAFNOSUPPORT
;
293 error
= in_pcbbind(inp
, nam
, p
);
298 /* Update NECP client with bind result if not in middle of connect */
299 if ((inp
->inp_flags2
& INP2_CONNECT_IN_PROGRESS
) &&
300 !uuid_is_null(inp
->necp_client_uuid
)) {
301 socket_unlock(so
, 0);
302 necp_client_assign_from_socket(so
->last_pid
, inp
->necp_client_uuid
, inp
);
307 COMMON_END(PRU_BIND
);
313 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
316 struct inpcb
*inp
= sotoinpcb(so
);
318 struct sockaddr_in6
*sin6p
;
322 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
323 error
= EAFNOSUPPORT
;
328 * Must check for multicast addresses and disallow binding
331 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
332 if (sin6p
->sin6_family
== AF_INET6
&&
333 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
334 error
= EAFNOSUPPORT
;
337 inp
->inp_vflag
&= ~INP_IPV4
;
338 inp
->inp_vflag
|= INP_IPV6
;
339 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
340 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
341 inp
->inp_vflag
|= INP_IPV4
;
342 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
343 struct sockaddr_in sin
;
345 in6_sin6_2_sin(&sin
, sin6p
);
346 inp
->inp_vflag
|= INP_IPV4
;
347 inp
->inp_vflag
&= ~INP_IPV6
;
348 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
352 error
= in6_pcbbind(inp
, nam
, p
);
355 COMMON_END(PRU_BIND
);
360 * Prepare to accept connections.
363 * EINVAL [COMMON_START]
364 * in_pcbbind:EADDRNOTAVAIL Address not available.
365 * in_pcbbind:EINVAL Invalid argument
366 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
367 * in_pcbbind:EACCES Permission denied
368 * in_pcbbind:EADDRINUSE Address in use
369 * in_pcbbind:EAGAIN Resource unavailable, try again
370 * in_pcbbind:EPERM Operation not permitted
373 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
376 struct inpcb
*inp
= sotoinpcb(so
);
380 if (inp
->inp_lport
== 0)
381 error
= in_pcbbind(inp
, NULL
, p
);
383 tp
->t_state
= TCPS_LISTEN
;
384 COMMON_END(PRU_LISTEN
);
389 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
392 struct inpcb
*inp
= sotoinpcb(so
);
396 if (inp
->inp_lport
== 0) {
397 inp
->inp_vflag
&= ~INP_IPV4
;
398 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
399 inp
->inp_vflag
|= INP_IPV4
;
400 error
= in6_pcbbind(inp
, NULL
, p
);
403 tp
->t_state
= TCPS_LISTEN
;
404 COMMON_END(PRU_LISTEN
);
409 tcp_connect_complete(struct socket
*so
)
411 struct tcpcb
*tp
= sototcpcb(so
);
412 struct inpcb
*inp
= sotoinpcb(so
);
415 /* TFO delays the tcp_output until later, when the app calls write() */
416 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
417 if (!necp_socket_is_allowed_to_send_recv(sotoinpcb(so
), NULL
, NULL
))
418 return (EHOSTUNREACH
);
420 /* Initialize enough state so that we can actually send data */
421 tcp_mss(tp
, -1, IFSCOPE_NONE
);
422 tp
->snd_wnd
= tp
->t_maxseg
;
424 error
= tcp_output(tp
);
428 /* Update NECP client with connected five-tuple */
429 if (error
== 0 && !uuid_is_null(inp
->necp_client_uuid
)) {
430 socket_unlock(so
, 0);
431 necp_client_assign_from_socket(so
->last_pid
, inp
->necp_client_uuid
, inp
);
440 * Initiate connection to peer.
441 * Create a template for use in transmissions on this connection.
442 * Enter SYN_SENT state, and mark socket as connecting.
443 * Start keep-alive timer, and seed output sequence space.
444 * Send initial segment on connection.
447 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
450 struct inpcb
*inp
= sotoinpcb(so
);
452 struct sockaddr_in
*sinp
;
457 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
459 error
= so
->so_error
;
467 else if (necp_socket_should_use_flow_divert(inp
)) {
468 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
469 if (fd_ctl_unit
> 0) {
470 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
472 error
= flow_divert_connect_out(so
, nam
, p
);
480 #endif /* FLOW_DIVERT */
482 error
= cfil_sock_attach(so
);
485 #endif /* CONTENT_FILTER */
490 calculate_tcp_clock();
492 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
493 error
= EAFNOSUPPORT
;
497 * Must disallow TCP ``connections'' to multicast addresses.
499 sinp
= (struct sockaddr_in
*)(void *)nam
;
500 if (sinp
->sin_family
== AF_INET
501 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
502 error
= EAFNOSUPPORT
;
506 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
509 error
= tcp_connect_complete(so
);
511 COMMON_END(PRU_CONNECT
);
515 tcp_usr_connectx_common(struct socket
*so
, int af
,
516 struct sockaddr
*src
, struct sockaddr
*dst
,
517 struct proc
*p
, uint32_t ifscope
, sae_associd_t aid
, sae_connid_t
*pcid
,
518 uint32_t flags
, void *arg
, uint32_t arglen
, struct uio
*auio
,
519 user_ssize_t
*bytes_written
)
521 #pragma unused(aid, flags, arg, arglen)
522 struct inpcb
*inp
= sotoinpcb(so
);
524 user_ssize_t datalen
= 0;
531 ASSERT(!(inp
->inp_flags2
& INP2_CONNECT_IN_PROGRESS
));
532 inp
->inp_flags2
|= INP2_CONNECT_IN_PROGRESS
;
535 inp_update_necp_policy(inp
, src
, dst
, ifscope
);
538 if ((so
->so_flags1
& SOF1_DATA_IDEMPOTENT
) &&
539 (tcp_fastopen
& TCP_FASTOPEN_CLIENT
))
540 sototcpcb(so
)->t_flagsext
|= TF_FASTOPEN
;
542 /* bind socket to the specified interface, if requested */
543 if (ifscope
!= IFSCOPE_NONE
&&
544 (error
= inp_bindif(inp
, ifscope
, NULL
)) != 0) {
548 /* if source address and/or port is specified, bind to it */
550 error
= sobindlock(so
, src
, 0); /* already locked */
558 error
= tcp_usr_connect(so
, dst
, p
);
562 error
= tcp6_usr_connect(so
, dst
, p
);
574 /* if there is data, copy it */
576 socket_unlock(so
, 0);
578 VERIFY(bytes_written
!= NULL
);
580 datalen
= uio_resid(auio
);
581 error
= so
->so_proto
->pr_usrreqs
->pru_sosend(so
, NULL
,
582 (uio_t
)auio
, NULL
, NULL
, 0);
585 if (error
== 0 || error
== EWOULDBLOCK
)
586 *bytes_written
= datalen
- uio_resid(auio
);
589 * sosend returns EWOULDBLOCK if it's a non-blocking
590 * socket or a timeout occured (this allows to return
591 * the amount of queued data through sendit()).
593 * However, connectx() returns EINPROGRESS in case of a
594 * blocking socket. So we change the return value here.
596 if (error
== EWOULDBLOCK
)
600 if (error
== 0 && pcid
!= NULL
)
601 *pcid
= 1; /* there is only one connection in regular TCP */
604 if (error
&& error
!= EINPROGRESS
)
605 so
->so_flags1
&= ~SOF1_PRECONNECT_DATA
;
607 inp
->inp_flags2
&= ~INP2_CONNECT_IN_PROGRESS
;
612 tcp_usr_connectx(struct socket
*so
, struct sockaddr
*src
,
613 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
614 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
615 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
617 return (tcp_usr_connectx_common(so
, AF_INET
, src
, dst
, p
, ifscope
, aid
,
618 pcid
, flags
, arg
, arglen
, uio
, bytes_written
));
623 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
626 struct inpcb
*inp
= sotoinpcb(so
);
628 struct sockaddr_in6
*sin6p
;
633 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
635 error
= so
->so_error
;
643 else if (necp_socket_should_use_flow_divert(inp
)) {
644 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
645 if (fd_ctl_unit
> 0) {
646 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
648 error
= flow_divert_connect_out(so
, nam
, p
);
656 #endif /* FLOW_DIVERT */
658 error
= cfil_sock_attach(so
);
661 #endif /* CONTENT_FILTER */
667 calculate_tcp_clock();
669 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
670 error
= EAFNOSUPPORT
;
675 * Must disallow TCP ``connections'' to multicast addresses.
677 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
678 if (sin6p
->sin6_family
== AF_INET6
679 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
680 error
= EAFNOSUPPORT
;
684 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
685 struct sockaddr_in sin
;
687 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
690 in6_sin6_2_sin(&sin
, sin6p
);
691 inp
->inp_vflag
|= INP_IPV4
;
692 inp
->inp_vflag
&= ~INP_IPV6
;
693 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
696 error
= tcp_connect_complete(so
);
699 inp
->inp_vflag
&= ~INP_IPV4
;
700 inp
->inp_vflag
|= INP_IPV6
;
701 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
704 error
= tcp_connect_complete(so
);
705 COMMON_END(PRU_CONNECT
);
709 tcp6_usr_connectx(struct socket
*so
, struct sockaddr
*src
,
710 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
711 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
712 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
714 return (tcp_usr_connectx_common(so
, AF_INET6
, src
, dst
, p
, ifscope
, aid
,
715 pcid
, flags
, arg
, arglen
, uio
, bytes_written
));
720 * Initiate disconnect from peer.
721 * If connection never passed embryonic stage, just drop;
722 * else if don't need to let data drain, then can just drop anyways,
723 * else have to begin TCP shutdown process: mark socket disconnecting,
724 * drain unread data, state switch to reflect user close, and
725 * send segment (e.g. FIN) to peer. Socket will be really disconnected
726 * when peer sends FIN and acks ours.
728 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
731 tcp_usr_disconnect(struct socket
*so
)
734 struct inpcb
*inp
= sotoinpcb(so
);
737 socket_lock_assert_owned(so
);
739 /* In case we got disconnected from the peer */
742 tp
= tcp_disconnect(tp
);
743 COMMON_END(PRU_DISCONNECT
);
747 * User-protocol pru_disconnectx callback.
750 tcp_usr_disconnectx(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
753 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
756 return (tcp_usr_disconnect(so
));
760 * Accept a connection. Essentially all the work is
761 * done at higher levels; just return the address
762 * of the peer, storing through addr.
765 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
768 struct inpcb
*inp
= sotoinpcb(so
);
769 struct tcpcb
*tp
= NULL
;
772 in_getpeeraddr(so
, nam
);
774 if (so
->so_state
& SS_ISDISCONNECTED
) {
775 error
= ECONNABORTED
;
778 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
781 else if (necp_socket_should_use_flow_divert(inp
))
784 error
= cfil_sock_attach(so
);
787 #endif /* CONTENT_FILTER */
793 calculate_tcp_clock();
795 COMMON_END(PRU_ACCEPT
);
800 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
803 struct inpcb
*inp
= sotoinpcb(so
);
804 struct tcpcb
*tp
= NULL
;
807 if (so
->so_state
& SS_ISDISCONNECTED
) {
808 error
= ECONNABORTED
;
811 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
814 else if (necp_socket_should_use_flow_divert(inp
))
817 error
= cfil_sock_attach(so
);
820 #endif /* CONTENT_FILTER */
826 calculate_tcp_clock();
828 in6_mapped_peeraddr(so
, nam
);
829 COMMON_END(PRU_ACCEPT
);
834 * Mark the connection as being incapable of further output.
837 * EINVAL [COMMON_START]
838 * tcp_output:EADDRNOTAVAIL
840 * tcp_output:EMSGSIZE
841 * tcp_output:EHOSTUNREACH
842 * tcp_output:ENETUNREACH
843 * tcp_output:ENETDOWN
846 * tcp_output:EMSGSIZE
848 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
851 tcp_usr_shutdown(struct socket
*so
)
854 struct inpcb
*inp
= sotoinpcb(so
);
858 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
864 * In case we got disconnected from the peer, or if this is
865 * a socket that is to be flow-diverted (but not yet).
872 || (necp_socket_should_use_flow_divert(inp
))
880 calculate_tcp_clock();
882 tp
= tcp_usrclosed(tp
);
884 /* A reset has been sent but socket exists, do not send FIN */
885 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
886 (tp
) && (tp
->t_mpflags
& TMPF_RESET
)) {
891 /* Don't send a FIN yet */
892 if (tp
&& !(so
->so_state
& SS_ISDISCONNECTED
) &&
893 cfil_sock_data_pending(&so
->so_snd
))
895 #endif /* CONTENT_FILTER */
897 error
= tcp_output(tp
);
898 COMMON_END(PRU_SHUTDOWN
);
902 * After a receive, possibly send window update to peer.
905 tcp_usr_rcvd(struct socket
*so
, __unused
int flags
)
908 struct inpcb
*inp
= sotoinpcb(so
);
912 /* In case we got disconnected from the peer */
915 tcp_sbrcv_trim(tp
, &so
->so_rcv
);
918 * This tcp_output is solely there to trigger window-updates.
919 * However, we really do not want these window-updates while we
920 * are still in SYN_SENT or SYN_RECEIVED.
922 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
926 cfil_sock_buf_update(&so
->so_rcv
);
927 #endif /* CONTENT_FILTER */
929 COMMON_END(PRU_RCVD
);
933 * Do a send by putting data in output queue and updating urgent
934 * marker if URG set. Possibly send more data. Unlike the other
935 * pru_*() routines, the mbuf chains are our responsibility. We
936 * must either enqueue them or free them. The other pru_* routines
937 * generally are caller-frees.
943 * tcp_connect:EADDRINUSE Address in use
944 * tcp_connect:EADDRNOTAVAIL Address not available.
945 * tcp_connect:EINVAL Invalid argument
946 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
947 * tcp_connect:EACCES Permission denied
948 * tcp_connect:EAGAIN Resource unavailable, try again
949 * tcp_connect:EPERM Operation not permitted
950 * tcp_output:EADDRNOTAVAIL
952 * tcp_output:EMSGSIZE
953 * tcp_output:EHOSTUNREACH
954 * tcp_output:ENETUNREACH
955 * tcp_output:ENETDOWN
958 * tcp_output:EMSGSIZE
960 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
961 * tcp6_connect:??? [IPV6 only]
964 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
965 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
968 struct inpcb
*inp
= sotoinpcb(so
);
970 uint32_t msgpri
= MSG_PRI_DEFAULT
;
976 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
978 || (necp_socket_should_use_flow_divert(inp
))
982 * OOPS! we lost a race, the TCP session got reset after
983 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
984 * network interrupt in the non-splnet() section of sosend().
988 if (control
!= NULL
) {
994 error
= ECONNRESET
; /* XXX EPIPE? */
1002 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
1004 tp
= intotcpcb(inp
);
1007 calculate_tcp_clock();
1009 if (control
!= NULL
) {
1010 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1011 /* Get the msg priority from control mbufs */
1012 error
= tcp_get_msg_priority(control
, &msgpri
);
1023 } else if (control
->m_len
) {
1025 * if not unordered, TCP should not have
1038 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1039 VERIFY(m
->m_flags
& M_PKTHDR
);
1040 m
->m_pkthdr
.msg_pri
= msgpri
;
1043 /* MPTCP sublow socket buffers must not be compressed */
1044 VERIFY(!(so
->so_flags
& SOF_MP_SUBFLOW
) ||
1045 (so
->so_snd
.sb_flags
& SB_NOCOMPRESS
));
1047 if(!(flags
& PRUS_OOB
) || (so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
1048 /* Call msg send if message delivery is enabled */
1049 if (so
->so_flags
& SOF_ENABLE_MSGS
)
1050 sbappendmsg_snd(&so
->so_snd
, m
);
1052 sbappendstream(&so
->so_snd
, m
);
1054 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1057 * Do implied connect if not yet connected,
1058 * initialize window to default value, and
1059 * initialize maxseg/maxopd using peer's cached
1064 error
= tcp6_connect(tp
, nam
, p
);
1067 error
= tcp_connect(tp
, nam
, p
);
1070 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1071 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1074 if (flags
& PRUS_EOF
) {
1076 * Close the send side of the connection after
1080 tp
= tcp_usrclosed(tp
);
1083 if (flags
& PRUS_MORETOCOME
)
1084 tp
->t_flags
|= TF_MORETOCOME
;
1085 error
= tcp_output(tp
);
1086 if (flags
& PRUS_MORETOCOME
)
1087 tp
->t_flags
&= ~TF_MORETOCOME
;
1090 if (sbspace(&so
->so_snd
) == 0) {
1091 /* if no space is left in sockbuf,
1092 * do not try to squeeze in OOB traffic */
1098 * According to RFC961 (Assigned Protocols),
1099 * the urgent pointer points to the last octet
1100 * of urgent data. We continue, however,
1101 * to consider it to indicate the first octet
1102 * of data past the urgent section.
1103 * Otherwise, snd_up should be one lower.
1105 sbappendstream(&so
->so_snd
, m
);
1106 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1108 * Do implied connect if not yet connected,
1109 * initialize window to default value, and
1110 * initialize maxseg/maxopd using peer's cached
1115 error
= tcp6_connect(tp
, nam
, p
);
1118 error
= tcp_connect(tp
, nam
, p
);
1121 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1122 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1124 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
1125 tp
->t_flagsext
|= TF_FORCE
;
1126 error
= tcp_output(tp
);
1127 tp
->t_flagsext
&= ~TF_FORCE
;
1132 * We wait for the socket to successfully connect before returning.
1133 * This allows us to signal a timeout to the application.
1135 if (so
->so_state
& SS_ISCONNECTING
) {
1136 if (so
->so_state
& SS_NBIO
)
1137 error
= EWOULDBLOCK
;
1139 error
= sbwait(&so
->so_snd
);
1142 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
1143 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
1150 tcp_usr_abort(struct socket
*so
)
1153 struct inpcb
*inp
= sotoinpcb(so
);
1157 /* In case we got disconnected from the peer */
1160 tp
= tcp_drop(tp
, ECONNABORTED
);
1161 VERIFY(so
->so_usecount
> 0);
1163 COMMON_END(PRU_ABORT
);
1167 * Receive out-of-band data.
1169 * Returns: 0 Success
1170 * EINVAL [COMMON_START]
1175 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
1178 struct inpcb
*inp
= sotoinpcb(so
);
1182 if ((so
->so_oobmark
== 0 &&
1183 (so
->so_state
& SS_RCVATMARK
) == 0) ||
1184 so
->so_options
& SO_OOBINLINE
||
1185 tp
->t_oobflags
& TCPOOB_HADDATA
) {
1189 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
1190 error
= EWOULDBLOCK
;
1194 *mtod(m
, caddr_t
) = tp
->t_iobc
;
1195 so
->so_state
&= ~SS_RCVATMARK
;
1196 if ((flags
& MSG_PEEK
) == 0)
1197 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
1198 COMMON_END(PRU_RCVOOB
);
1202 tcp_usr_preconnect(struct socket
*so
)
1204 struct inpcb
*inp
= sotoinpcb(so
);
1208 if (necp_socket_should_use_flow_divert(inp
)) {
1209 /* May happen, if in tcp_usr_connect we did not had a chance
1210 * to set the usrreqs (due to some error). So, let's get out
1217 error
= tcp_output(sototcpcb(so
));
1219 soclearfastopen(so
);
1221 COMMON_END(PRU_PRECONNECT
);
1224 /* xxx - should be const */
1225 struct pr_usrreqs tcp_usrreqs
= {
1226 .pru_abort
= tcp_usr_abort
,
1227 .pru_accept
= tcp_usr_accept
,
1228 .pru_attach
= tcp_usr_attach
,
1229 .pru_bind
= tcp_usr_bind
,
1230 .pru_connect
= tcp_usr_connect
,
1231 .pru_connectx
= tcp_usr_connectx
,
1232 .pru_control
= in_control
,
1233 .pru_detach
= tcp_usr_detach
,
1234 .pru_disconnect
= tcp_usr_disconnect
,
1235 .pru_disconnectx
= tcp_usr_disconnectx
,
1236 .pru_listen
= tcp_usr_listen
,
1237 .pru_peeraddr
= in_getpeeraddr
,
1238 .pru_rcvd
= tcp_usr_rcvd
,
1239 .pru_rcvoob
= tcp_usr_rcvoob
,
1240 .pru_send
= tcp_usr_send
,
1241 .pru_shutdown
= tcp_usr_shutdown
,
1242 .pru_sockaddr
= in_getsockaddr
,
1243 .pru_sosend
= sosend
,
1244 .pru_soreceive
= soreceive
,
1245 .pru_preconnect
= tcp_usr_preconnect
,
1249 struct pr_usrreqs tcp6_usrreqs
= {
1250 .pru_abort
= tcp_usr_abort
,
1251 .pru_accept
= tcp6_usr_accept
,
1252 .pru_attach
= tcp_usr_attach
,
1253 .pru_bind
= tcp6_usr_bind
,
1254 .pru_connect
= tcp6_usr_connect
,
1255 .pru_connectx
= tcp6_usr_connectx
,
1256 .pru_control
= in6_control
,
1257 .pru_detach
= tcp_usr_detach
,
1258 .pru_disconnect
= tcp_usr_disconnect
,
1259 .pru_disconnectx
= tcp_usr_disconnectx
,
1260 .pru_listen
= tcp6_usr_listen
,
1261 .pru_peeraddr
= in6_mapped_peeraddr
,
1262 .pru_rcvd
= tcp_usr_rcvd
,
1263 .pru_rcvoob
= tcp_usr_rcvoob
,
1264 .pru_send
= tcp_usr_send
,
1265 .pru_shutdown
= tcp_usr_shutdown
,
1266 .pru_sockaddr
= in6_mapped_sockaddr
,
1267 .pru_sosend
= sosend
,
1268 .pru_soreceive
= soreceive
,
1269 .pru_preconnect
= tcp_usr_preconnect
,
1274 * Common subroutine to open a TCP connection to remote host specified
1275 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1276 * port number if needed. Call in_pcbladdr to do the routing and to choose
1277 * a local host address (interface). If there is an existing incarnation
1278 * of the same connection in TIME-WAIT state and if the remote host was
1279 * sending CC options and if the connection duration was < MSL, then
1280 * truncate the previous TIME-WAIT state and proceed.
1281 * Initialize connection parameters and enter SYN-SENT state.
1283 * Returns: 0 Success
1286 * in_pcbbind:EADDRNOTAVAIL Address not available.
1287 * in_pcbbind:EINVAL Invalid argument
1288 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1289 * in_pcbbind:EACCES Permission denied
1290 * in_pcbbind:EADDRINUSE Address in use
1291 * in_pcbbind:EAGAIN Resource unavailable, try again
1292 * in_pcbbind:EPERM Operation not permitted
1293 * in_pcbladdr:EINVAL Invalid argument
1294 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1295 * in_pcbladdr:EADDRNOTAVAIL Address not available
1298 tcp_connect(struct tcpcb
*tp
, struct sockaddr
*nam
, struct proc
*p
)
1300 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1301 struct socket
*so
= inp
->inp_socket
;
1303 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)nam
;
1304 struct in_addr laddr
;
1306 struct ifnet
*outif
= NULL
;
1308 if (inp
->inp_lport
== 0) {
1309 error
= in_pcbbind(inp
, NULL
, p
);
1315 * Cannot simply call in_pcbconnect, because there might be an
1316 * earlier incarnation of this same connection still in
1317 * TIME_WAIT state, creating an ADDRINUSE error.
1319 error
= in_pcbladdr(inp
, nam
, &laddr
, IFSCOPE_NONE
, &outif
, 0);
1323 socket_unlock(inp
->inp_socket
, 0);
1324 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
1325 sin
->sin_addr
, sin
->sin_port
,
1326 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
: laddr
,
1327 inp
->inp_lport
, 0, NULL
);
1329 socket_lock(inp
->inp_socket
, 0);
1331 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
1332 socket_lock(oinp
->inp_socket
, 1);
1333 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1335 socket_unlock(oinp
->inp_socket
, 1);
1339 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1340 otp
->t_state
== TCPS_TIME_WAIT
&&
1341 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1342 (otp
->t_flags
& TF_RCVD_CC
)) {
1343 otp
= tcp_close(otp
);
1345 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1346 (uint64_t)VM_KERNEL_ADDRPERM(inp
));
1348 socket_unlock(oinp
->inp_socket
, 1);
1353 socket_unlock(oinp
->inp_socket
, 1);
1356 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? laddr
.s_addr
:
1357 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
1358 inp
->inp_lport
== sin
->sin_port
) {
1362 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1363 /*lock inversion issue, mostly with udp multicast packets */
1364 socket_unlock(inp
->inp_socket
, 0);
1365 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1366 socket_lock(inp
->inp_socket
, 0);
1368 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1369 inp
->inp_laddr
= laddr
;
1370 /* no reference needed */
1371 inp
->inp_last_outifp
= outif
;
1373 inp
->inp_flags
|= INP_INADDR_ANY
;
1375 inp
->inp_faddr
= sin
->sin_addr
;
1376 inp
->inp_fport
= sin
->sin_port
;
1378 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1380 if (inp
->inp_flowhash
== 0)
1381 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1383 tcp_set_max_rwinscale(tp
, so
, TCP_AUTORCVBUF_MAX(outif
));
1386 tcpstat
.tcps_connattempt
++;
1387 tp
->t_state
= TCPS_SYN_SENT
;
1388 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, TCP_CONN_KEEPINIT(tp
));
1389 tp
->iss
= tcp_new_isn(tp
);
1390 tcp_sendseqinit(tp
);
1392 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1396 ifnet_release(outif
);
1403 tcp6_connect(struct tcpcb
*tp
, struct sockaddr
*nam
, struct proc
*p
)
1405 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1406 struct socket
*so
= inp
->inp_socket
;
1408 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)nam
;
1409 struct in6_addr addr6
;
1411 struct ifnet
*outif
= NULL
;
1413 if (inp
->inp_lport
== 0) {
1414 error
= in6_pcbbind(inp
, NULL
, p
);
1420 * Cannot simply call in_pcbconnect, because there might be an
1421 * earlier incarnation of this same connection still in
1422 * TIME_WAIT state, creating an ADDRINUSE error.
1424 * in6_pcbladdr() might return an ifp with its reference held
1425 * even in the error case, so make sure that it's released
1426 * whenever it's non-NULL.
1428 error
= in6_pcbladdr(inp
, nam
, &addr6
, &outif
);
1431 socket_unlock(inp
->inp_socket
, 0);
1432 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1433 &sin6
->sin6_addr
, sin6
->sin6_port
,
1434 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1437 inp
->inp_lport
, 0, NULL
);
1438 socket_lock(inp
->inp_socket
, 0);
1440 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1441 otp
->t_state
== TCPS_TIME_WAIT
&&
1442 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1443 (otp
->t_flags
& TF_RCVD_CC
)) {
1444 otp
= tcp_close(otp
);
1450 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1451 /*lock inversion issue, mostly with udp multicast packets */
1452 socket_unlock(inp
->inp_socket
, 0);
1453 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1454 socket_lock(inp
->inp_socket
, 0);
1456 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
1457 inp
->in6p_laddr
= addr6
;
1458 inp
->in6p_last_outifp
= outif
; /* no reference needed */
1459 inp
->in6p_flags
|= INP_IN6ADDR_ANY
;
1461 inp
->in6p_faddr
= sin6
->sin6_addr
;
1462 inp
->inp_fport
= sin6
->sin6_port
;
1463 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1464 inp
->inp_flow
= sin6
->sin6_flowinfo
;
1466 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1468 if (inp
->inp_flowhash
== 0)
1469 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1470 /* update flowinfo - RFC 6437 */
1471 if (inp
->inp_flow
== 0 && inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
1472 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
1474 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
1477 tcp_set_max_rwinscale(tp
, so
, TCP_AUTORCVBUF_MAX(outif
));
1480 tcpstat
.tcps_connattempt
++;
1481 tp
->t_state
= TCPS_SYN_SENT
;
1482 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1483 TCP_CONN_KEEPINIT(tp
));
1484 tp
->iss
= tcp_new_isn(tp
);
1485 tcp_sendseqinit(tp
);
1487 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1491 ifnet_release(outif
);
1498 * Export TCP internal state information via a struct tcp_info
1501 tcp_fill_info(struct tcpcb
*tp
, struct tcp_info
*ti
)
1503 struct inpcb
*inp
= tp
->t_inpcb
;
1505 bzero(ti
, sizeof(*ti
));
1507 ti
->tcpi_state
= tp
->t_state
;
1508 ti
->tcpi_flowhash
= inp
->inp_flowhash
;
1510 if (tp
->t_state
> TCPS_LISTEN
) {
1511 if (TSTMP_SUPPORTED(tp
))
1512 ti
->tcpi_options
|= TCPI_OPT_TIMESTAMPS
;
1513 if (SACK_ENABLED(tp
))
1514 ti
->tcpi_options
|= TCPI_OPT_SACK
;
1515 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1516 ti
->tcpi_options
|= TCPI_OPT_WSCALE
;
1517 ti
->tcpi_snd_wscale
= tp
->snd_scale
;
1518 ti
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1520 if (TCP_ECN_ENABLED(tp
))
1521 ti
->tcpi_options
|= TCPI_OPT_ECN
;
1523 /* Are we in retranmission episode */
1524 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1525 ti
->tcpi_flags
|= TCPI_FLAG_LOSSRECOVERY
;
1527 if (tp
->t_flags
& TF_STREAMING_ON
)
1528 ti
->tcpi_flags
|= TCPI_FLAG_STREAMING_ON
;
1530 ti
->tcpi_rto
= tp
->t_timer
[TCPT_REXMT
] ? tp
->t_rxtcur
: 0;
1531 ti
->tcpi_snd_mss
= tp
->t_maxseg
;
1532 ti
->tcpi_rcv_mss
= tp
->t_maxseg
;
1534 ti
->tcpi_rttcur
= tp
->t_rttcur
;
1535 ti
->tcpi_srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
1536 ti
->tcpi_rttvar
= tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
;
1537 ti
->tcpi_rttbest
= tp
->t_rttbest
>> TCP_RTT_SHIFT
;
1539 ti
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1540 ti
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1541 ti
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1543 ti
->tcpi_rcv_space
= tp
->rcv_wnd
;
1545 ti
->tcpi_snd_wnd
= tp
->snd_wnd
;
1546 ti
->tcpi_snd_nxt
= tp
->snd_nxt
;
1547 ti
->tcpi_rcv_nxt
= tp
->rcv_nxt
;
1549 /* convert bytes/msec to bits/sec */
1550 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1551 tp
->t_bwmeas
!= NULL
) {
1552 ti
->tcpi_snd_bw
= (tp
->t_bwmeas
->bw_sndbw
* 8000);
1555 ti
->tcpi_last_outif
= (tp
->t_inpcb
->inp_last_outifp
== NULL
) ? 0 :
1556 tp
->t_inpcb
->inp_last_outifp
->if_index
;
1558 //atomic_get_64(ti->tcpi_txbytes, &inp->inp_stat->txbytes);
1559 ti
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1560 ti
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1561 ti
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1562 ti
->tcpi_txretransmitpackets
= tp
->t_stat
.rxmitpkts
;
1563 ti
->tcpi_txunacked
= tp
->snd_max
- tp
->snd_una
;
1565 //atomic_get_64(ti->tcpi_rxbytes, &inp->inp_stat->rxbytes);
1566 ti
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1567 ti
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1568 ti
->tcpi_rxduplicatebytes
= tp
->t_stat
.rxduplicatebytes
;
1569 ti
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1571 if (tp
->t_state
> TCPS_LISTEN
) {
1572 ti
->tcpi_synrexmits
= tp
->t_stat
.synrxtshift
;
1574 ti
->tcpi_cell_rxpackets
= inp
->inp_cstat
->rxpackets
;
1575 ti
->tcpi_cell_rxbytes
= inp
->inp_cstat
->rxbytes
;
1576 ti
->tcpi_cell_txpackets
= inp
->inp_cstat
->txpackets
;
1577 ti
->tcpi_cell_txbytes
= inp
->inp_cstat
->txbytes
;
1579 ti
->tcpi_wifi_rxpackets
= inp
->inp_wstat
->rxpackets
;
1580 ti
->tcpi_wifi_rxbytes
= inp
->inp_wstat
->rxbytes
;
1581 ti
->tcpi_wifi_txpackets
= inp
->inp_wstat
->txpackets
;
1582 ti
->tcpi_wifi_txbytes
= inp
->inp_wstat
->txbytes
;
1584 ti
->tcpi_wired_rxpackets
= inp
->inp_Wstat
->rxpackets
;
1585 ti
->tcpi_wired_rxbytes
= inp
->inp_Wstat
->rxbytes
;
1586 ti
->tcpi_wired_txpackets
= inp
->inp_Wstat
->txpackets
;
1587 ti
->tcpi_wired_txbytes
= inp
->inp_Wstat
->txbytes
;
1588 tcp_get_connectivity_status(tp
, &ti
->tcpi_connstatus
);
1590 ti
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1591 ti
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1592 ti
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1593 ti
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1595 ti
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1596 ti
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1597 ti
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1598 ti
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1599 ti
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1600 ti
->tcpi_tfo_cookie_wrong
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_WRONG
);
1601 ti
->tcpi_tfo_no_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_NO_COOKIE_RCV
);
1602 ti
->tcpi_tfo_heuristics_disable
= !!(tp
->t_tfo_stats
& TFO_S_HEURISTICS_DISABLE
);
1603 ti
->tcpi_tfo_send_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_SEND_BLACKHOLE
);
1604 ti
->tcpi_tfo_recv_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_RECV_BLACKHOLE
);
1605 ti
->tcpi_tfo_onebyte_proxy
= !!(tp
->t_tfo_stats
& TFO_S_ONE_BYTE_PROXY
);
1607 ti
->tcpi_ecn_client_setup
= !!(tp
->ecn_flags
& TE_SETUPSENT
);
1608 ti
->tcpi_ecn_server_setup
= !!(tp
->ecn_flags
& TE_SETUPRECEIVED
);
1609 ti
->tcpi_ecn_success
= (tp
->ecn_flags
& TE_ECN_ON
) == TE_ECN_ON
? 1 : 0;
1610 ti
->tcpi_ecn_lost_syn
= !!(tp
->ecn_flags
& TE_LOST_SYN
);
1611 ti
->tcpi_ecn_lost_synack
= !!(tp
->ecn_flags
& TE_LOST_SYNACK
);
1613 ti
->tcpi_local_peer
= !!(tp
->t_flags
& TF_LOCAL
);
1615 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
) {
1616 if (IFNET_IS_CELLULAR(tp
->t_inpcb
->inp_last_outifp
))
1617 ti
->tcpi_if_cell
= 1;
1618 if (IFNET_IS_WIFI(tp
->t_inpcb
->inp_last_outifp
))
1619 ti
->tcpi_if_wifi
= 1;
1620 if (IFNET_IS_WIRED(tp
->t_inpcb
->inp_last_outifp
))
1621 ti
->tcpi_if_wired
= 1;
1622 if (IFNET_IS_WIFI_INFRA(tp
->t_inpcb
->inp_last_outifp
))
1623 ti
->tcpi_if_wifi_infra
= 1;
1624 if (tp
->t_inpcb
->inp_last_outifp
->if_eflags
& IFEF_AWDL
)
1625 ti
->tcpi_if_wifi_awdl
= 1;
1627 if (tp
->tcp_cc_index
== TCP_CC_ALGO_BACKGROUND_INDEX
)
1628 ti
->tcpi_snd_background
= 1;
1629 if (tcp_recv_bg
== 1 ||
1630 IS_TCP_RECV_BG(tp
->t_inpcb
->inp_socket
))
1631 ti
->tcpi_rcv_background
= 1;
1633 ti
->tcpi_ecn_recv_ce
= tp
->t_ecn_recv_ce
;
1634 ti
->tcpi_ecn_recv_cwr
= tp
->t_ecn_recv_cwr
;
1636 ti
->tcpi_rcvoopack
= tp
->t_rcvoopack
;
1637 ti
->tcpi_pawsdrop
= tp
->t_pawsdrop
;
1638 ti
->tcpi_sack_recovery_episode
= tp
->t_sack_recovery_episode
;
1639 ti
->tcpi_reordered_pkts
= tp
->t_reordered_pkts
;
1640 ti
->tcpi_dsack_sent
= tp
->t_dsack_sent
;
1641 ti
->tcpi_dsack_recvd
= tp
->t_dsack_recvd
;
1645 __private_extern__ errno_t
1646 tcp_fill_info_for_info_tuple(struct info_tuple
*itpl
, struct tcp_info
*ti
)
1648 struct inpcbinfo
*pcbinfo
= NULL
;
1649 struct inpcb
*inp
= NULL
;
1653 if (itpl
->itpl_proto
== IPPROTO_TCP
)
1658 if (itpl
->itpl_local_sa
.sa_family
== AF_INET
&&
1659 itpl
->itpl_remote_sa
.sa_family
== AF_INET
) {
1660 inp
= in_pcblookup_hash(pcbinfo
,
1661 itpl
->itpl_remote_sin
.sin_addr
,
1662 itpl
->itpl_remote_sin
.sin_port
,
1663 itpl
->itpl_local_sin
.sin_addr
,
1664 itpl
->itpl_local_sin
.sin_port
,
1666 } else if (itpl
->itpl_local_sa
.sa_family
== AF_INET6
&&
1667 itpl
->itpl_remote_sa
.sa_family
== AF_INET6
) {
1668 struct in6_addr ina6_local
;
1669 struct in6_addr ina6_remote
;
1671 ina6_local
= itpl
->itpl_local_sin6
.sin6_addr
;
1672 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local
) &&
1673 itpl
->itpl_local_sin6
.sin6_scope_id
)
1674 ina6_local
.s6_addr16
[1] = htons(itpl
->itpl_local_sin6
.sin6_scope_id
);
1676 ina6_remote
= itpl
->itpl_remote_sin6
.sin6_addr
;
1677 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote
) &&
1678 itpl
->itpl_remote_sin6
.sin6_scope_id
)
1679 ina6_remote
.s6_addr16
[1] = htons(itpl
->itpl_remote_sin6
.sin6_scope_id
);
1681 inp
= in6_pcblookup_hash(pcbinfo
,
1683 itpl
->itpl_remote_sin6
.sin6_port
,
1685 itpl
->itpl_local_sin6
.sin6_port
,
1690 if (inp
== NULL
|| (so
= inp
->inp_socket
) == NULL
)
1694 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1695 socket_unlock(so
, 0);
1698 tp
= intotcpcb(inp
);
1700 tcp_fill_info(tp
, ti
);
1701 socket_unlock(so
, 0);
1707 tcp_connection_fill_info(struct tcpcb
*tp
, struct tcp_connection_info
*tci
)
1709 struct inpcb
*inp
= tp
->t_inpcb
;
1711 bzero(tci
, sizeof(*tci
));
1712 tci
->tcpi_state
= tp
->t_state
;
1713 if (tp
->t_state
> TCPS_LISTEN
) {
1714 if (TSTMP_SUPPORTED(tp
))
1715 tci
->tcpi_options
|= TCPCI_OPT_TIMESTAMPS
;
1716 if (SACK_ENABLED(tp
))
1717 tci
->tcpi_options
|= TCPCI_OPT_SACK
;
1718 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1719 tci
->tcpi_options
|= TCPCI_OPT_WSCALE
;
1720 tci
->tcpi_snd_wscale
= tp
->snd_scale
;
1721 tci
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1723 if (TCP_ECN_ENABLED(tp
))
1724 tci
->tcpi_options
|= TCPCI_OPT_ECN
;
1725 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1726 tci
->tcpi_flags
|= TCPCI_FLAG_LOSSRECOVERY
;
1727 if (tp
->t_flagsext
& TF_PKTS_REORDERED
)
1728 tci
->tcpi_flags
|= TCPCI_FLAG_REORDERING_DETECTED
;
1729 tci
->tcpi_rto
= (tp
->t_timer
[TCPT_REXMT
] > 0) ?
1731 tci
->tcpi_maxseg
= tp
->t_maxseg
;
1732 tci
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1733 tci
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1734 tci
->tcpi_snd_wnd
= tp
->snd_wnd
;
1735 tci
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1736 tci
->tcpi_rcv_wnd
= tp
->rcv_wnd
;
1737 tci
->tcpi_rttcur
= tp
->t_rttcur
;
1738 tci
->tcpi_srtt
= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1739 tci
->tcpi_rttvar
= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
1740 tci
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1741 tci
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1742 tci
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1743 tci
->tcpi_txretransmitpackets
= tp
->t_stat
.rxmitpkts
;
1744 tci
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1745 tci
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1746 tci
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1748 tci
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1749 tci
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1750 tci
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1751 tci
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1752 tci
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1753 tci
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1754 tci
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1755 tci
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1756 tci
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1757 tci
->tcpi_tfo_cookie_wrong
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_WRONG
);
1758 tci
->tcpi_tfo_no_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_NO_COOKIE_RCV
);
1759 tci
->tcpi_tfo_heuristics_disable
= !!(tp
->t_tfo_stats
& TFO_S_HEURISTICS_DISABLE
);
1760 tci
->tcpi_tfo_send_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_SEND_BLACKHOLE
);
1761 tci
->tcpi_tfo_recv_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_RECV_BLACKHOLE
);
1762 tci
->tcpi_tfo_onebyte_proxy
= !!(tp
->t_tfo_stats
& TFO_S_ONE_BYTE_PROXY
);
1767 __private_extern__
int
1768 tcp_sysctl_info(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
1771 struct tcp_info ti
= {};
1772 struct info_tuple itpl
;
1773 #if !CONFIG_EMBEDDED
1774 proc_t caller
= PROC_NULL
;
1775 proc_t caller_parent
= PROC_NULL
;
1776 char command_name
[MAXCOMLEN
+ 1] = "";
1777 char parent_name
[MAXCOMLEN
+ 1] = "";
1779 if ((caller
= proc_self()) != PROC_NULL
) {
1780 /* get process name */
1781 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
1783 /* get parent process name if possible */
1784 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
1785 strlcpy(parent_name
, caller_parent
->p_comm
,
1786 sizeof(parent_name
));
1787 proc_rele(caller_parent
);
1790 if ((escape_str(command_name
, strlen(command_name
) + 1,
1791 sizeof(command_name
)) == 0) &&
1792 (escape_str(parent_name
, strlen(parent_name
) + 1,
1793 sizeof(parent_name
)) == 0)) {
1794 kern_asl_msg(LOG_DEBUG
, "messagetracer",
1796 "com.apple.message.domain",
1797 "com.apple.kernel.tcpstat", /* 1 */
1798 "com.apple.message.signature",
1800 "com.apple.message.signature2", command_name
, /* 3 */
1801 "com.apple.message.signature3", parent_name
, /* 4 */
1802 "com.apple.message.summarize", "YES", /* 5 */
1807 if (caller
!= PROC_NULL
)
1809 #endif /* !CONFIG_EMBEDDED */
1811 if (req
->newptr
== USER_ADDR_NULL
) {
1814 if (req
->newlen
< sizeof(struct info_tuple
)) {
1817 error
= SYSCTL_IN(req
, &itpl
, sizeof(struct info_tuple
));
1821 error
= tcp_fill_info_for_info_tuple(&itpl
, &ti
);
1825 error
= SYSCTL_OUT(req
, &ti
, sizeof(struct tcp_info
));
1834 tcp_lookup_peer_pid_locked(struct socket
*so
, pid_t
*out_pid
)
1836 int error
= EHOSTUNREACH
;
1838 if ((so
->so_state
& SS_ISCONNECTED
) == 0) return ENOTCONN
;
1840 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
1841 uint16_t lport
= inp
->inp_lport
;
1842 uint16_t fport
= inp
->inp_fport
;
1843 struct inpcb
*finp
= NULL
;
1844 struct in6_addr laddr6
, faddr6
;
1845 struct in_addr laddr4
, faddr4
;
1847 if (inp
->inp_vflag
& INP_IPV6
) {
1848 laddr6
= inp
->in6p_laddr
;
1849 faddr6
= inp
->in6p_faddr
;
1850 } else if (inp
->inp_vflag
& INP_IPV4
) {
1851 laddr4
= inp
->inp_laddr
;
1852 faddr4
= inp
->inp_faddr
;
1855 socket_unlock(so
, 0);
1856 if (inp
->inp_vflag
& INP_IPV6
) {
1857 finp
= in6_pcblookup_hash(&tcbinfo
, &laddr6
, lport
, &faddr6
, fport
, 0, NULL
);
1858 } else if (inp
->inp_vflag
& INP_IPV4
) {
1859 finp
= in_pcblookup_hash(&tcbinfo
, laddr4
, lport
, faddr4
, fport
, 0, NULL
);
1863 *out_pid
= finp
->inp_socket
->last_pid
;
1865 in_pcb_checkstate(finp
, WNT_RELEASE
, 0);
1873 tcp_getconninfo(struct socket
*so
, struct conninfo_tcp
*tcp_ci
)
1875 (void) tcp_lookup_peer_pid_locked(so
, &tcp_ci
->tcpci_peer_pid
);
1876 tcp_fill_info(sototcpcb(so
), &tcp_ci
->tcpci_tcp_info
);
1880 * The new sockopt interface makes it possible for us to block in the
1881 * copyin/out step (if we take a page fault). Taking a page fault at
1882 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1883 * use TSM, there probably isn't any need for this function to run at
1884 * splnet() any more. This needs more examination.)
1887 tcp_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1889 int error
= 0, opt
= 0, optval
= 0;
1893 inp
= sotoinpcb(so
);
1895 return (ECONNRESET
);
1897 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
1898 if (sopt
->sopt_level
!= IPPROTO_TCP
&&
1899 !(sopt
->sopt_level
== SOL_SOCKET
&& (sopt
->sopt_name
== SO_FLUSH
||
1900 sopt
->sopt_name
== SO_TRAFFIC_MGT_BACKGROUND
))) {
1902 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1903 error
= ip6_ctloutput(so
, sopt
);
1906 error
= ip_ctloutput(so
, sopt
);
1909 tp
= intotcpcb(inp
);
1911 return (ECONNRESET
);
1914 calculate_tcp_clock();
1916 switch (sopt
->sopt_dir
) {
1918 switch (sopt
->sopt_name
) {
1922 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1927 switch (sopt
->sopt_name
) {
1938 opt
= 0; /* dead code to fool gcc */
1945 tp
->t_flags
&= ~opt
;
1947 case TCP_RXT_FINDROP
:
1948 case TCP_NOTIMEWAIT
:
1949 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1953 switch (sopt
->sopt_name
) {
1954 case TCP_RXT_FINDROP
:
1955 opt
= TF_RXTFINDROP
;
1957 case TCP_NOTIMEWAIT
:
1958 opt
= TF_NOTIMEWAIT
;
1965 tp
->t_flagsext
|= opt
;
1967 tp
->t_flagsext
&= ~opt
;
1969 case TCP_MEASURE_SND_BW
:
1970 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1974 opt
= TF_MEASURESNDBW
;
1976 if (tp
->t_bwmeas
== NULL
) {
1977 tp
->t_bwmeas
= tcp_bwmeas_alloc(tp
);
1978 if (tp
->t_bwmeas
== NULL
) {
1983 tp
->t_flagsext
|= opt
;
1985 tp
->t_flagsext
&= ~opt
;
1986 /* Reset snd bw measurement state */
1987 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
1988 if (tp
->t_bwmeas
!= NULL
) {
1989 tcp_bwmeas_free(tp
);
1993 case TCP_MEASURE_BW_BURST
: {
1994 struct tcp_measure_bw_burst in
;
1995 uint32_t minpkts
, maxpkts
;
1996 bzero(&in
, sizeof(in
));
1998 error
= sooptcopyin(sopt
, &in
, sizeof(in
),
2002 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2003 tp
->t_bwmeas
== NULL
) {
2007 minpkts
= (in
.min_burst_size
!= 0) ? in
.min_burst_size
:
2008 tp
->t_bwmeas
->bw_minsizepkts
;
2009 maxpkts
= (in
.max_burst_size
!= 0) ? in
.max_burst_size
:
2010 tp
->t_bwmeas
->bw_maxsizepkts
;
2011 if (minpkts
> maxpkts
) {
2015 tp
->t_bwmeas
->bw_minsizepkts
= minpkts
;
2016 tp
->t_bwmeas
->bw_maxsizepkts
= maxpkts
;
2017 tp
->t_bwmeas
->bw_minsize
= (minpkts
* tp
->t_maxseg
);
2018 tp
->t_bwmeas
->bw_maxsize
= (maxpkts
* tp
->t_maxseg
);
2022 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2027 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
2028 optval
+ 40 >= tcp_minmss
)
2029 tp
->t_maxseg
= optval
;
2035 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2039 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2042 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
2043 /* reset the timer to new value */
2044 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2045 TCP_CONN_KEEPIDLE(tp
));
2046 tcp_check_timer_state(tp
);
2050 case TCP_CONNECTIONTIMEOUT
:
2051 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2055 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2058 tp
->t_keepinit
= optval
* TCP_RETRANSHZ
;
2059 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
2060 tp
->t_state
== TCPS_SYN_SENT
) {
2061 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2062 TCP_CONN_KEEPINIT(tp
));
2063 tcp_check_timer_state(tp
);
2069 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2073 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2076 tp
->t_keepintvl
= optval
* TCP_RETRANSHZ
;
2077 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2078 TCP_CONN_MAXIDLE(tp
) > 0) {
2079 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2080 TCP_CONN_MAXIDLE(tp
));
2081 tcp_check_timer_state(tp
);
2087 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2091 if (optval
< 0 || optval
> INT32_MAX
) {
2094 tp
->t_keepcnt
= optval
;
2095 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2096 TCP_CONN_MAXIDLE(tp
) > 0) {
2097 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2098 TCP_CONN_MAXIDLE(tp
));
2099 tcp_check_timer_state(tp
);
2104 case TCP_KEEPALIVE_OFFLOAD
:
2105 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2109 if (optval
< 0 || optval
> INT32_MAX
) {
2114 inp
->inp_flags2
|= INP2_KEEPALIVE_OFFLOAD
;
2116 inp
->inp_flags2
&= ~INP2_KEEPALIVE_OFFLOAD
;
2119 case PERSIST_TIMEOUT
:
2120 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2127 tp
->t_persist_timeout
= optval
* TCP_RETRANSHZ
;
2129 case TCP_RXT_CONNDROPTIME
:
2130 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2137 tp
->t_rxt_conndroptime
= optval
* TCP_RETRANSHZ
;
2139 case TCP_NOTSENT_LOWAT
:
2140 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2149 so
->so_flags
&= ~(SOF_NOTSENT_LOWAT
);
2150 tp
->t_notsent_lowat
= 0;
2152 so
->so_flags
|= SOF_NOTSENT_LOWAT
;
2153 tp
->t_notsent_lowat
= optval
;
2157 case TCP_ADAPTIVE_READ_TIMEOUT
:
2158 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2163 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2166 } else if (optval
== 0) {
2167 tp
->t_adaptive_rtimo
= 0;
2168 tcp_keepalive_reset(tp
);
2171 mptcp_reset_keepalive(tp
);
2173 tp
->t_adaptive_rtimo
= optval
;
2176 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2177 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2182 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2186 tp
->t_adaptive_wtimo
= optval
;
2189 case TCP_ENABLE_MSGS
:
2190 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2194 if (optval
< 0 || optval
> 1) {
2196 } else if (optval
== 1) {
2198 * Check if messages option is already
2199 * enabled, if so return.
2201 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2202 VERIFY(so
->so_msg_state
!= NULL
);
2207 * allocate memory for storing message
2210 VERIFY(so
->so_msg_state
== NULL
);
2211 MALLOC(so
->so_msg_state
,
2213 sizeof(struct msg_state
),
2214 M_TEMP
, M_WAITOK
| M_ZERO
);
2215 if (so
->so_msg_state
== NULL
) {
2220 /* Enable message delivery */
2221 so
->so_flags
|= SOF_ENABLE_MSGS
;
2224 * Can't disable message delivery on socket
2225 * because of restrictions imposed by
2231 case TCP_SENDMOREACKS
:
2232 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2236 if (optval
< 0 || optval
> 1) {
2238 } else if (optval
== 0) {
2239 tp
->t_flagsext
&= ~(TF_NOSTRETCHACK
);
2241 tp
->t_flagsext
|= TF_NOSTRETCHACK
;
2244 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2245 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2249 if (optval
< 0 || optval
> 1) {
2251 } else if (optval
== 0) {
2252 tp
->t_flagsext
&= ~TF_NOBLACKHOLE_DETECTION
;
2254 tp
->t_flagsext
|= TF_NOBLACKHOLE_DETECTION
;
2255 if ((tp
->t_flags
& TF_BLACKHOLE
) &&
2256 tp
->t_pmtud_saved_maxopd
> 0)
2257 tcp_pmtud_revert_segment_size(tp
);
2261 if (!(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2266 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2270 if (optval
< 0 || optval
> 1) {
2274 if (tp
->t_state
!= TCPS_LISTEN
) {
2279 tp
->t_flagsext
|= TF_FASTOPEN
;
2281 tcp_disable_tfo(tp
);
2283 case TCP_FASTOPEN_FORCE_HEURISTICS
:
2284 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2289 if (optval
< 0 || optval
> 1) {
2294 if (tp
->t_state
!= TCPS_CLOSED
) {
2299 tp
->t_flagsext
|= TF_FASTOPEN_HEUR
;
2301 tp
->t_flagsext
&= ~TF_FASTOPEN_HEUR
;
2304 case TCP_ENABLE_ECN
:
2305 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2310 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2311 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2313 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2314 tp
->ecn_flags
|= TE_ECN_MODE_DISABLE
;
2318 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2322 if (optval
== ECN_MODE_DEFAULT
) {
2323 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2324 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2325 } else if (optval
== ECN_MODE_ENABLE
) {
2326 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2327 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2328 } else if (optval
== ECN_MODE_DISABLE
) {
2329 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2330 tp
->ecn_flags
|= TE_ECN_MODE_DISABLE
;
2335 case TCP_NOTIFY_ACKNOWLEDGEMENT
:
2336 error
= sooptcopyin(sopt
, &optval
,
2337 sizeof(optval
), sizeof(optval
));
2344 if (tp
->t_notify_ack_count
>= TCP_MAX_NOTIFY_ACK
) {
2345 error
= ETOOMANYREFS
;
2350 * validate that the given marker id is not
2351 * a duplicate to avoid ambiguity
2353 if ((error
= tcp_notify_ack_id_valid(tp
, so
,
2357 error
= tcp_add_notify_ack_marker(tp
, optval
);
2360 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2361 sizeof (optval
))) != 0)
2364 error
= inp_flush(inp
, optval
);
2367 case SO_TRAFFIC_MGT_BACKGROUND
:
2368 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2369 sizeof (optval
))) != 0)
2373 socket_set_traffic_mgt_flags_locked(so
,
2374 TRAFFIC_MGT_SO_BACKGROUND
);
2376 socket_clear_traffic_mgt_flags_locked(so
,
2377 TRAFFIC_MGT_SO_BACKGROUND
);
2380 case TCP_RXT_MINIMUM_TIMEOUT
:
2381 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2390 tp
->t_rxt_minimum_timeout
= 0;
2392 tp
->t_rxt_minimum_timeout
= min(optval
,
2393 TCP_RXT_MINIMUM_TIMEOUT_LIMIT
);
2394 /* convert to milliseconds */
2395 tp
->t_rxt_minimum_timeout
*= TCP_RETRANSHZ
;
2399 error
= ENOPROTOOPT
;
2405 switch (sopt
->sopt_name
) {
2407 optval
= tp
->t_flags
& TF_NODELAY
;
2410 optval
= tp
->t_maxseg
;
2413 if (tp
->t_keepidle
> 0)
2414 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
2416 optval
= tcp_keepidle
/ TCP_RETRANSHZ
;
2419 if (tp
->t_keepintvl
> 0)
2420 optval
= tp
->t_keepintvl
/ TCP_RETRANSHZ
;
2422 optval
= tcp_keepintvl
/ TCP_RETRANSHZ
;
2425 if (tp
->t_keepcnt
> 0)
2426 optval
= tp
->t_keepcnt
;
2428 optval
= tcp_keepcnt
;
2430 case TCP_KEEPALIVE_OFFLOAD
:
2431 optval
= !!(inp
->inp_flags2
& INP2_KEEPALIVE_OFFLOAD
);
2434 optval
= tp
->t_flags
& TF_NOOPT
;
2437 optval
= tp
->t_flags
& TF_NOPUSH
;
2439 case TCP_ENABLE_ECN
:
2440 optval
= (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
) ? 1 : 0;
2443 if (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
)
2444 optval
= ECN_MODE_ENABLE
;
2445 else if (tp
->ecn_flags
& TE_ECN_MODE_DISABLE
)
2446 optval
= ECN_MODE_DISABLE
;
2448 optval
= ECN_MODE_DEFAULT
;
2450 case TCP_CONNECTIONTIMEOUT
:
2451 optval
= tp
->t_keepinit
/ TCP_RETRANSHZ
;
2453 case PERSIST_TIMEOUT
:
2454 optval
= tp
->t_persist_timeout
/ TCP_RETRANSHZ
;
2456 case TCP_RXT_CONNDROPTIME
:
2457 optval
= tp
->t_rxt_conndroptime
/ TCP_RETRANSHZ
;
2459 case TCP_RXT_FINDROP
:
2460 optval
= tp
->t_flagsext
& TF_RXTFINDROP
;
2462 case TCP_NOTIMEWAIT
:
2463 optval
= (tp
->t_flagsext
& TF_NOTIMEWAIT
) ? 1 : 0;
2466 if (tp
->t_state
!= TCPS_LISTEN
||
2467 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2471 optval
= tfo_enabled(tp
);
2473 case TCP_FASTOPEN_FORCE_HEURISTICS
:
2474 optval
= (tp
->t_flagsext
& TF_FASTOPEN_HEUR
) ? 1 : 0;
2476 case TCP_MEASURE_SND_BW
:
2477 optval
= tp
->t_flagsext
& TF_MEASURESNDBW
;
2482 tcp_fill_info(tp
, &ti
);
2483 error
= sooptcopyout(sopt
, &ti
, sizeof(struct tcp_info
));
2487 case TCP_CONNECTION_INFO
: {
2488 struct tcp_connection_info tci
;
2489 tcp_connection_fill_info(tp
, &tci
);
2490 error
= sooptcopyout(sopt
, &tci
,
2491 sizeof(struct tcp_connection_info
));
2494 case TCP_MEASURE_BW_BURST
: {
2495 struct tcp_measure_bw_burst out
= {};
2496 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2497 tp
->t_bwmeas
== NULL
) {
2501 out
.min_burst_size
= tp
->t_bwmeas
->bw_minsizepkts
;
2502 out
.max_burst_size
= tp
->t_bwmeas
->bw_maxsizepkts
;
2503 error
= sooptcopyout(sopt
, &out
, sizeof(out
));
2506 case TCP_NOTSENT_LOWAT
:
2507 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0) {
2508 optval
= tp
->t_notsent_lowat
;
2514 case TCP_ENABLE_MSGS
:
2515 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2521 case TCP_SENDMOREACKS
:
2522 if (tp
->t_flagsext
& TF_NOSTRETCHACK
)
2527 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2528 if (tp
->t_flagsext
& TF_NOBLACKHOLE_DETECTION
)
2533 case TCP_PEER_PID
: {
2535 error
= tcp_lookup_peer_pid_locked(so
, &pid
);
2537 error
= sooptcopyout(sopt
, &pid
, sizeof(pid
));
2540 case TCP_ADAPTIVE_READ_TIMEOUT
:
2541 optval
= tp
->t_adaptive_rtimo
;
2543 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2544 optval
= tp
->t_adaptive_wtimo
;
2546 case SO_TRAFFIC_MGT_BACKGROUND
:
2547 optval
= (so
->so_flags1
&
2548 SOF1_TRAFFIC_MGT_SO_BACKGROUND
) ? 1 : 0;
2550 case TCP_NOTIFY_ACKNOWLEDGEMENT
: {
2551 struct tcp_notify_ack_complete retid
;
2553 if (sopt
->sopt_valsize
!= sizeof (retid
)) {
2557 bzero(&retid
, sizeof (retid
));
2558 tcp_get_notify_ack_count(tp
, &retid
);
2559 if (retid
.notify_complete_count
> 0)
2560 tcp_get_notify_ack_ids(tp
, &retid
);
2562 error
= sooptcopyout(sopt
, &retid
, sizeof (retid
));
2565 case TCP_RXT_MINIMUM_TIMEOUT
:
2566 optval
= tp
->t_rxt_minimum_timeout
/ TCP_RETRANSHZ
;
2569 error
= ENOPROTOOPT
;
2573 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2581 * tcp_sendspace and tcp_recvspace are the default send and receive window
2582 * sizes, respectively. These are obsolescent (this information should
2583 * be set by the route).
2585 u_int32_t tcp_sendspace
= 1448*256;
2586 u_int32_t tcp_recvspace
= 1448*384;
2588 /* During attach, the size of socket buffer allocated is limited to
2589 * sb_max in sbreserve. Disallow setting the tcp send and recv space
2590 * to be more than sb_max because that will cause tcp_attach to fail
2591 * (see radar 5713060)
2594 sysctl_tcp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
2595 int arg2
, struct sysctl_req
*req
)
2597 #pragma unused(arg2)
2598 u_int32_t new_value
= 0, *space_p
= NULL
;
2599 int changed
= 0, error
= 0;
2600 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
2602 switch (oidp
->oid_number
) {
2603 case TCPCTL_SENDSPACE
:
2604 space_p
= &tcp_sendspace
;
2606 case TCPCTL_RECVSPACE
:
2607 space_p
= &tcp_recvspace
;
2612 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
2613 &new_value
, &changed
);
2615 if (new_value
> 0 && new_value
<= sb_effective_max
) {
2616 *space_p
= new_value
;
2617 SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2
, new_value
);
2626 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
,
2627 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_sendspace
,
2628 offsetof(skmem_sysctl
, tcp
.sendspace
), sysctl_tcp_sospace
,
2629 "IU", "Maximum outgoing TCP datagram size");
2630 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
,
2631 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_recvspace
,
2632 offsetof(skmem_sysctl
, tcp
.recvspace
), sysctl_tcp_sospace
,
2633 "IU", "Maximum incoming TCP datagram size");
2634 #else /* SYSCTL_SKMEM */
2635 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2636 &tcp_sendspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum outgoing TCP datagram size");
2637 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2638 &tcp_recvspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum incoming TCP datagram size");
2639 #endif /* SYSCTL_SKMEM */
2642 * Attach TCP protocol to socket, allocating
2643 * internet protocol control block, tcp control block,
2644 * bufer space, and entering LISTEN state if to accept connections.
2646 * Returns: 0 Success
2647 * in_pcballoc:ENOBUFS
2648 * in_pcballoc:ENOMEM
2649 * in_pcballoc:??? [IPSEC specific]
2653 tcp_attach(struct socket
*so
, struct proc
*p
)
2659 int isipv6
= SOCK_CHECK_DOM(so
, PF_INET6
) != 0;
2662 error
= in_pcballoc(so
, &tcbinfo
, p
);
2666 inp
= sotoinpcb(so
);
2668 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
2669 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
2674 if (so
->so_snd
.sb_preconn_hiwat
== 0) {
2675 soreserve_preconnect(so
, 2048);
2678 if ((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0)
2679 so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
2680 if ((so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0)
2681 so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
2685 inp
->inp_vflag
|= INP_IPV6
;
2686 inp
->in6p_hops
= -1; /* use kernel default */
2690 inp
->inp_vflag
|= INP_IPV4
;
2691 tp
= tcp_newtcpcb(inp
);
2693 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
2695 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
2702 so
->so_state
|= nofd
;
2706 nstat_tcp_new_pcb(inp
);
2707 tp
->t_state
= TCPS_CLOSED
;
2712 * Initiate (or continue) disconnect.
2713 * If embryonic state, just send reset (once).
2714 * If in ``let data drain'' option and linger null, just drop.
2715 * Otherwise (hard), mark socket disconnecting and drop
2716 * current input data; switch states based on user close, and
2717 * send segment to peer (with FIN).
2719 static struct tcpcb
*
2720 tcp_disconnect(struct tcpcb
*tp
)
2722 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2724 if (so
->so_rcv
.sb_cc
!= 0 || tp
->t_reassqlen
!= 0)
2725 return tcp_drop(tp
, 0);
2727 if (tp
->t_state
< TCPS_ESTABLISHED
)
2729 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
2730 tp
= tcp_drop(tp
, 0);
2732 soisdisconnecting(so
);
2733 sbflush(&so
->so_rcv
);
2734 tp
= tcp_usrclosed(tp
);
2736 /* A reset has been sent but socket exists, do not send FIN */
2737 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
2738 (tp
) && (tp
->t_mpflags
& TMPF_RESET
))
2742 (void) tcp_output(tp
);
2748 * User issued close, and wish to trail through shutdown states:
2749 * if never received SYN, just forget it. If got a SYN from peer,
2750 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2751 * If already got a FIN from peer, then almost done; go to LAST_ACK
2752 * state. In all other cases, have already sent FIN to peer (e.g.
2753 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2754 * for peer to send FIN or not respond to keep-alives, etc.
2755 * We can let the user exit from the close as soon as the FIN is acked.
2757 static struct tcpcb
*
2758 tcp_usrclosed(struct tcpcb
*tp
)
2760 switch (tp
->t_state
) {
2768 case TCPS_SYN_RECEIVED
:
2769 tp
->t_flags
|= TF_NEEDFIN
;
2772 case TCPS_ESTABLISHED
:
2773 DTRACE_TCP4(state__change
, void, NULL
,
2774 struct inpcb
*, tp
->t_inpcb
,
2776 int32_t, TCPS_FIN_WAIT_1
);
2777 tp
->t_state
= TCPS_FIN_WAIT_1
;
2780 case TCPS_CLOSE_WAIT
:
2781 DTRACE_TCP4(state__change
, void, NULL
,
2782 struct inpcb
*, tp
->t_inpcb
,
2784 int32_t, TCPS_LAST_ACK
);
2785 tp
->t_state
= TCPS_LAST_ACK
;
2788 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
2789 soisdisconnected(tp
->t_inpcb
->inp_socket
);
2790 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
2791 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
2792 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2793 TCP_CONN_MAXIDLE(tp
));
2799 tcp_in_cksum_stats(u_int32_t len
)
2801 tcpstat
.tcps_rcv_swcsum
++;
2802 tcpstat
.tcps_rcv_swcsum_bytes
+= len
;
2806 tcp_out_cksum_stats(u_int32_t len
)
2808 tcpstat
.tcps_snd_swcsum
++;
2809 tcpstat
.tcps_snd_swcsum_bytes
+= len
;
2814 tcp_in6_cksum_stats(u_int32_t len
)
2816 tcpstat
.tcps_rcv6_swcsum
++;
2817 tcpstat
.tcps_rcv6_swcsum_bytes
+= len
;
2821 tcp_out6_cksum_stats(u_int32_t len
)
2823 tcpstat
.tcps_snd6_swcsum
++;
2824 tcpstat
.tcps_snd6_swcsum_bytes
+= len
;
2828 * When messages are enabled on a TCP socket, the message priority
2829 * is sent as a control message. This function will extract it.
2832 tcp_get_msg_priority(struct mbuf
*control
, uint32_t *msgpri
)
2835 if (control
== NULL
)
2838 for (cm
= M_FIRST_CMSGHDR(control
); cm
;
2839 cm
= M_NXT_CMSGHDR(control
, cm
)) {
2840 if (cm
->cmsg_len
< sizeof(struct cmsghdr
) ||
2841 cm
->cmsg_len
> control
->m_len
) {
2844 if (cm
->cmsg_level
== SOL_SOCKET
&&
2845 cm
->cmsg_type
== SCM_MSG_PRIORITY
) {
2846 *msgpri
= *(unsigned int *)(void *)CMSG_DATA(cm
);
2851 VERIFY(*msgpri
>= MSG_PRI_MIN
&& *msgpri
<= MSG_PRI_MAX
);