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
);
416 /* Update NECP client with connected five-tuple */
417 if (!uuid_is_null(inp
->necp_client_uuid
)) {
418 socket_unlock(so
, 0);
419 necp_client_assign_from_socket(so
->last_pid
, inp
->necp_client_uuid
, inp
);
424 /* TFO delays the tcp_output until later, when the app calls write() */
425 if (so
->so_flags1
& SOF1_PRECONNECT_DATA
) {
426 if (!necp_socket_is_allowed_to_send_recv(sotoinpcb(so
), NULL
, NULL
))
427 return (EHOSTUNREACH
);
429 /* Initialize enough state so that we can actually send data */
430 tcp_mss(tp
, -1, IFSCOPE_NONE
);
431 tp
->snd_wnd
= tp
->t_maxseg
;
433 error
= tcp_output(tp
);
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 inp
->inp_flags2
&= ~INP2_CONNECT_IN_PROGRESS
;
609 tcp_usr_connectx(struct socket
*so
, struct sockaddr
*src
,
610 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
611 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
612 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
614 return (tcp_usr_connectx_common(so
, AF_INET
, src
, dst
, p
, ifscope
, aid
,
615 pcid
, flags
, arg
, arglen
, uio
, bytes_written
));
620 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
623 struct inpcb
*inp
= sotoinpcb(so
);
625 struct sockaddr_in6
*sin6p
;
630 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
632 error
= so
->so_error
;
640 else if (necp_socket_should_use_flow_divert(inp
)) {
641 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
642 if (fd_ctl_unit
> 0) {
643 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
645 error
= flow_divert_connect_out(so
, nam
, p
);
653 #endif /* FLOW_DIVERT */
655 error
= cfil_sock_attach(so
);
658 #endif /* CONTENT_FILTER */
664 calculate_tcp_clock();
666 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
667 error
= EAFNOSUPPORT
;
672 * Must disallow TCP ``connections'' to multicast addresses.
674 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
675 if (sin6p
->sin6_family
== AF_INET6
676 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
677 error
= EAFNOSUPPORT
;
681 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
682 struct sockaddr_in sin
;
684 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
687 in6_sin6_2_sin(&sin
, sin6p
);
688 inp
->inp_vflag
|= INP_IPV4
;
689 inp
->inp_vflag
&= ~INP_IPV6
;
690 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
693 error
= tcp_connect_complete(so
);
696 inp
->inp_vflag
&= ~INP_IPV4
;
697 inp
->inp_vflag
|= INP_IPV6
;
698 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
701 error
= tcp_connect_complete(so
);
702 COMMON_END(PRU_CONNECT
);
706 tcp6_usr_connectx(struct socket
*so
, struct sockaddr
*src
,
707 struct sockaddr
*dst
, struct proc
*p
, uint32_t ifscope
,
708 sae_associd_t aid
, sae_connid_t
*pcid
, uint32_t flags
, void *arg
,
709 uint32_t arglen
, struct uio
*uio
, user_ssize_t
*bytes_written
)
711 return (tcp_usr_connectx_common(so
, AF_INET6
, src
, dst
, p
, ifscope
, aid
,
712 pcid
, flags
, arg
, arglen
, uio
, bytes_written
));
717 * Initiate disconnect from peer.
718 * If connection never passed embryonic stage, just drop;
719 * else if don't need to let data drain, then can just drop anyways,
720 * else have to begin TCP shutdown process: mark socket disconnecting,
721 * drain unread data, state switch to reflect user close, and
722 * send segment (e.g. FIN) to peer. Socket will be really disconnected
723 * when peer sends FIN and acks ours.
725 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
728 tcp_usr_disconnect(struct socket
*so
)
731 struct inpcb
*inp
= sotoinpcb(so
);
734 socket_lock_assert_owned(so
);
736 /* In case we got disconnected from the peer */
739 tp
= tcp_disconnect(tp
);
740 COMMON_END(PRU_DISCONNECT
);
744 * User-protocol pru_disconnectx callback.
747 tcp_usr_disconnectx(struct socket
*so
, sae_associd_t aid
, sae_connid_t cid
)
750 if (aid
!= SAE_ASSOCID_ANY
&& aid
!= SAE_ASSOCID_ALL
)
753 return (tcp_usr_disconnect(so
));
757 * Accept a connection. Essentially all the work is
758 * done at higher levels; just return the address
759 * of the peer, storing through addr.
762 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
765 struct inpcb
*inp
= sotoinpcb(so
);
766 struct tcpcb
*tp
= NULL
;
769 in_getpeeraddr(so
, nam
);
771 if (so
->so_state
& SS_ISDISCONNECTED
) {
772 error
= ECONNABORTED
;
775 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
778 else if (necp_socket_should_use_flow_divert(inp
))
781 error
= cfil_sock_attach(so
);
784 #endif /* CONTENT_FILTER */
790 calculate_tcp_clock();
792 COMMON_END(PRU_ACCEPT
);
797 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
800 struct inpcb
*inp
= sotoinpcb(so
);
801 struct tcpcb
*tp
= NULL
;
804 if (so
->so_state
& SS_ISDISCONNECTED
) {
805 error
= ECONNABORTED
;
808 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
811 else if (necp_socket_should_use_flow_divert(inp
))
814 error
= cfil_sock_attach(so
);
817 #endif /* CONTENT_FILTER */
823 calculate_tcp_clock();
825 in6_mapped_peeraddr(so
, nam
);
826 COMMON_END(PRU_ACCEPT
);
831 * Mark the connection as being incapable of further output.
834 * EINVAL [COMMON_START]
835 * tcp_output:EADDRNOTAVAIL
837 * tcp_output:EMSGSIZE
838 * tcp_output:EHOSTUNREACH
839 * tcp_output:ENETUNREACH
840 * tcp_output:ENETDOWN
843 * tcp_output:EMSGSIZE
845 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
848 tcp_usr_shutdown(struct socket
*so
)
851 struct inpcb
*inp
= sotoinpcb(so
);
855 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
861 * In case we got disconnected from the peer, or if this is
862 * a socket that is to be flow-diverted (but not yet).
869 || (necp_socket_should_use_flow_divert(inp
))
877 calculate_tcp_clock();
879 tp
= tcp_usrclosed(tp
);
881 /* A reset has been sent but socket exists, do not send FIN */
882 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
883 (tp
) && (tp
->t_mpflags
& TMPF_RESET
)) {
888 /* Don't send a FIN yet */
889 if (tp
&& !(so
->so_state
& SS_ISDISCONNECTED
) &&
890 cfil_sock_data_pending(&so
->so_snd
))
892 #endif /* CONTENT_FILTER */
894 error
= tcp_output(tp
);
895 COMMON_END(PRU_SHUTDOWN
);
899 * After a receive, possibly send window update to peer.
902 tcp_usr_rcvd(struct socket
*so
, __unused
int flags
)
905 struct inpcb
*inp
= sotoinpcb(so
);
909 /* In case we got disconnected from the peer */
912 tcp_sbrcv_trim(tp
, &so
->so_rcv
);
915 * This tcp_output is solely there to trigger window-updates.
916 * However, we really do not want these window-updates while we
917 * are still in SYN_SENT or SYN_RECEIVED.
919 if (TCPS_HAVEESTABLISHED(tp
->t_state
))
923 cfil_sock_buf_update(&so
->so_rcv
);
924 #endif /* CONTENT_FILTER */
926 COMMON_END(PRU_RCVD
);
930 * Do a send by putting data in output queue and updating urgent
931 * marker if URG set. Possibly send more data. Unlike the other
932 * pru_*() routines, the mbuf chains are our responsibility. We
933 * must either enqueue them or free them. The other pru_* routines
934 * generally are caller-frees.
940 * tcp_connect:EADDRINUSE Address in use
941 * tcp_connect:EADDRNOTAVAIL Address not available.
942 * tcp_connect:EINVAL Invalid argument
943 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
944 * tcp_connect:EACCES Permission denied
945 * tcp_connect:EAGAIN Resource unavailable, try again
946 * tcp_connect:EPERM Operation not permitted
947 * tcp_output:EADDRNOTAVAIL
949 * tcp_output:EMSGSIZE
950 * tcp_output:EHOSTUNREACH
951 * tcp_output:ENETUNREACH
952 * tcp_output:ENETDOWN
955 * tcp_output:EMSGSIZE
957 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
958 * tcp6_connect:??? [IPV6 only]
961 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
962 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
965 struct inpcb
*inp
= sotoinpcb(so
);
967 uint32_t msgpri
= MSG_PRI_DEFAULT
;
973 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
975 || (necp_socket_should_use_flow_divert(inp
))
979 * OOPS! we lost a race, the TCP session got reset after
980 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
981 * network interrupt in the non-splnet() section of sosend().
985 if (control
!= NULL
) {
991 error
= ECONNRESET
; /* XXX EPIPE? */
999 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
1001 tp
= intotcpcb(inp
);
1004 calculate_tcp_clock();
1006 if (control
!= NULL
) {
1007 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1008 /* Get the msg priority from control mbufs */
1009 error
= tcp_get_msg_priority(control
, &msgpri
);
1020 } else if (control
->m_len
) {
1022 * if not unordered, TCP should not have
1035 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
1036 VERIFY(m
->m_flags
& M_PKTHDR
);
1037 m
->m_pkthdr
.msg_pri
= msgpri
;
1040 /* MPTCP sublow socket buffers must not be compressed */
1041 VERIFY(!(so
->so_flags
& SOF_MP_SUBFLOW
) ||
1042 (so
->so_snd
.sb_flags
& SB_NOCOMPRESS
));
1044 if(!(flags
& PRUS_OOB
) || (so
->so_flags1
& SOF1_PRECONNECT_DATA
)) {
1045 /* Call msg send if message delivery is enabled */
1046 if (so
->so_flags
& SOF_ENABLE_MSGS
)
1047 sbappendmsg_snd(&so
->so_snd
, m
);
1049 sbappendstream(&so
->so_snd
, m
);
1051 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1054 * Do implied connect if not yet connected,
1055 * initialize window to default value, and
1056 * initialize maxseg/maxopd using peer's cached
1061 error
= tcp6_connect(tp
, nam
, p
);
1064 error
= tcp_connect(tp
, nam
, p
);
1067 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1068 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1071 if (flags
& PRUS_EOF
) {
1073 * Close the send side of the connection after
1077 tp
= tcp_usrclosed(tp
);
1080 if (flags
& PRUS_MORETOCOME
)
1081 tp
->t_flags
|= TF_MORETOCOME
;
1082 error
= tcp_output(tp
);
1083 if (flags
& PRUS_MORETOCOME
)
1084 tp
->t_flags
&= ~TF_MORETOCOME
;
1087 if (sbspace(&so
->so_snd
) == 0) {
1088 /* if no space is left in sockbuf,
1089 * do not try to squeeze in OOB traffic */
1095 * According to RFC961 (Assigned Protocols),
1096 * the urgent pointer points to the last octet
1097 * of urgent data. We continue, however,
1098 * to consider it to indicate the first octet
1099 * of data past the urgent section.
1100 * Otherwise, snd_up should be one lower.
1102 sbappendstream(&so
->so_snd
, m
);
1103 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1105 * Do implied connect if not yet connected,
1106 * initialize window to default value, and
1107 * initialize maxseg/maxopd using peer's cached
1112 error
= tcp6_connect(tp
, nam
, p
);
1115 error
= tcp_connect(tp
, nam
, p
);
1118 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1119 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1121 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
1122 tp
->t_flagsext
|= TF_FORCE
;
1123 error
= tcp_output(tp
);
1124 tp
->t_flagsext
&= ~TF_FORCE
;
1129 * We wait for the socket to successfully connect before returning.
1130 * This allows us to signal a timeout to the application.
1132 if (so
->so_state
& SS_ISCONNECTING
) {
1133 if (so
->so_state
& SS_NBIO
)
1134 error
= EWOULDBLOCK
;
1136 error
= sbwait(&so
->so_snd
);
1139 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
1140 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
1147 tcp_usr_abort(struct socket
*so
)
1150 struct inpcb
*inp
= sotoinpcb(so
);
1154 /* In case we got disconnected from the peer */
1157 tp
= tcp_drop(tp
, ECONNABORTED
);
1158 VERIFY(so
->so_usecount
> 0);
1160 COMMON_END(PRU_ABORT
);
1164 * Receive out-of-band data.
1166 * Returns: 0 Success
1167 * EINVAL [COMMON_START]
1172 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
1175 struct inpcb
*inp
= sotoinpcb(so
);
1179 if ((so
->so_oobmark
== 0 &&
1180 (so
->so_state
& SS_RCVATMARK
) == 0) ||
1181 so
->so_options
& SO_OOBINLINE
||
1182 tp
->t_oobflags
& TCPOOB_HADDATA
) {
1186 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
1187 error
= EWOULDBLOCK
;
1191 *mtod(m
, caddr_t
) = tp
->t_iobc
;
1192 so
->so_state
&= ~SS_RCVATMARK
;
1193 if ((flags
& MSG_PEEK
) == 0)
1194 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
1195 COMMON_END(PRU_RCVOOB
);
1199 tcp_usr_preconnect(struct socket
*so
)
1201 struct inpcb
*inp
= sotoinpcb(so
);
1205 if (necp_socket_should_use_flow_divert(inp
)) {
1206 /* May happen, if in tcp_usr_connect we did not had a chance
1207 * to set the usrreqs (due to some error). So, let's get out
1214 error
= tcp_output(sototcpcb(so
));
1216 soclearfastopen(so
);
1218 COMMON_END(PRU_PRECONNECT
);
1221 /* xxx - should be const */
1222 struct pr_usrreqs tcp_usrreqs
= {
1223 .pru_abort
= tcp_usr_abort
,
1224 .pru_accept
= tcp_usr_accept
,
1225 .pru_attach
= tcp_usr_attach
,
1226 .pru_bind
= tcp_usr_bind
,
1227 .pru_connect
= tcp_usr_connect
,
1228 .pru_connectx
= tcp_usr_connectx
,
1229 .pru_control
= in_control
,
1230 .pru_detach
= tcp_usr_detach
,
1231 .pru_disconnect
= tcp_usr_disconnect
,
1232 .pru_disconnectx
= tcp_usr_disconnectx
,
1233 .pru_listen
= tcp_usr_listen
,
1234 .pru_peeraddr
= in_getpeeraddr
,
1235 .pru_rcvd
= tcp_usr_rcvd
,
1236 .pru_rcvoob
= tcp_usr_rcvoob
,
1237 .pru_send
= tcp_usr_send
,
1238 .pru_shutdown
= tcp_usr_shutdown
,
1239 .pru_sockaddr
= in_getsockaddr
,
1240 .pru_sosend
= sosend
,
1241 .pru_soreceive
= soreceive
,
1242 .pru_preconnect
= tcp_usr_preconnect
,
1246 struct pr_usrreqs tcp6_usrreqs
= {
1247 .pru_abort
= tcp_usr_abort
,
1248 .pru_accept
= tcp6_usr_accept
,
1249 .pru_attach
= tcp_usr_attach
,
1250 .pru_bind
= tcp6_usr_bind
,
1251 .pru_connect
= tcp6_usr_connect
,
1252 .pru_connectx
= tcp6_usr_connectx
,
1253 .pru_control
= in6_control
,
1254 .pru_detach
= tcp_usr_detach
,
1255 .pru_disconnect
= tcp_usr_disconnect
,
1256 .pru_disconnectx
= tcp_usr_disconnectx
,
1257 .pru_listen
= tcp6_usr_listen
,
1258 .pru_peeraddr
= in6_mapped_peeraddr
,
1259 .pru_rcvd
= tcp_usr_rcvd
,
1260 .pru_rcvoob
= tcp_usr_rcvoob
,
1261 .pru_send
= tcp_usr_send
,
1262 .pru_shutdown
= tcp_usr_shutdown
,
1263 .pru_sockaddr
= in6_mapped_sockaddr
,
1264 .pru_sosend
= sosend
,
1265 .pru_soreceive
= soreceive
,
1266 .pru_preconnect
= tcp_usr_preconnect
,
1271 * Common subroutine to open a TCP connection to remote host specified
1272 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1273 * port number if needed. Call in_pcbladdr to do the routing and to choose
1274 * a local host address (interface). If there is an existing incarnation
1275 * of the same connection in TIME-WAIT state and if the remote host was
1276 * sending CC options and if the connection duration was < MSL, then
1277 * truncate the previous TIME-WAIT state and proceed.
1278 * Initialize connection parameters and enter SYN-SENT state.
1280 * Returns: 0 Success
1283 * in_pcbbind:EADDRNOTAVAIL Address not available.
1284 * in_pcbbind:EINVAL Invalid argument
1285 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1286 * in_pcbbind:EACCES Permission denied
1287 * in_pcbbind:EADDRINUSE Address in use
1288 * in_pcbbind:EAGAIN Resource unavailable, try again
1289 * in_pcbbind:EPERM Operation not permitted
1290 * in_pcbladdr:EINVAL Invalid argument
1291 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1292 * in_pcbladdr:EADDRNOTAVAIL Address not available
1295 tcp_connect(struct tcpcb
*tp
, struct sockaddr
*nam
, struct proc
*p
)
1297 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1298 struct socket
*so
= inp
->inp_socket
;
1300 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)nam
;
1301 struct in_addr laddr
;
1303 struct ifnet
*outif
= NULL
;
1305 if (inp
->inp_lport
== 0) {
1306 error
= in_pcbbind(inp
, NULL
, p
);
1312 * Cannot simply call in_pcbconnect, because there might be an
1313 * earlier incarnation of this same connection still in
1314 * TIME_WAIT state, creating an ADDRINUSE error.
1316 error
= in_pcbladdr(inp
, nam
, &laddr
, IFSCOPE_NONE
, &outif
, 0);
1320 socket_unlock(inp
->inp_socket
, 0);
1321 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
1322 sin
->sin_addr
, sin
->sin_port
,
1323 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
: laddr
,
1324 inp
->inp_lport
, 0, NULL
);
1326 socket_lock(inp
->inp_socket
, 0);
1328 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
1329 socket_lock(oinp
->inp_socket
, 1);
1330 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1332 socket_unlock(oinp
->inp_socket
, 1);
1336 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1337 otp
->t_state
== TCPS_TIME_WAIT
&&
1338 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1339 (otp
->t_flags
& TF_RCVD_CC
)) {
1340 otp
= tcp_close(otp
);
1342 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1343 (uint64_t)VM_KERNEL_ADDRPERM(inp
));
1345 socket_unlock(oinp
->inp_socket
, 1);
1350 socket_unlock(oinp
->inp_socket
, 1);
1353 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? laddr
.s_addr
:
1354 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
1355 inp
->inp_lport
== sin
->sin_port
) {
1359 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1360 /*lock inversion issue, mostly with udp multicast packets */
1361 socket_unlock(inp
->inp_socket
, 0);
1362 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1363 socket_lock(inp
->inp_socket
, 0);
1365 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1366 inp
->inp_laddr
= laddr
;
1367 /* no reference needed */
1368 inp
->inp_last_outifp
= outif
;
1370 inp
->inp_flags
|= INP_INADDR_ANY
;
1372 inp
->inp_faddr
= sin
->sin_addr
;
1373 inp
->inp_fport
= sin
->sin_port
;
1375 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1377 if (inp
->inp_flowhash
== 0)
1378 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1380 tcp_set_max_rwinscale(tp
, so
, TCP_AUTORCVBUF_MAX(outif
));
1383 tcpstat
.tcps_connattempt
++;
1384 tp
->t_state
= TCPS_SYN_SENT
;
1385 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, TCP_CONN_KEEPINIT(tp
));
1386 tp
->iss
= tcp_new_isn(tp
);
1387 tcp_sendseqinit(tp
);
1389 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1393 ifnet_release(outif
);
1400 tcp6_connect(struct tcpcb
*tp
, struct sockaddr
*nam
, struct proc
*p
)
1402 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1403 struct socket
*so
= inp
->inp_socket
;
1405 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)nam
;
1406 struct in6_addr addr6
;
1408 struct ifnet
*outif
= NULL
;
1410 if (inp
->inp_lport
== 0) {
1411 error
= in6_pcbbind(inp
, NULL
, p
);
1417 * Cannot simply call in_pcbconnect, because there might be an
1418 * earlier incarnation of this same connection still in
1419 * TIME_WAIT state, creating an ADDRINUSE error.
1421 * in6_pcbladdr() might return an ifp with its reference held
1422 * even in the error case, so make sure that it's released
1423 * whenever it's non-NULL.
1425 error
= in6_pcbladdr(inp
, nam
, &addr6
, &outif
);
1428 socket_unlock(inp
->inp_socket
, 0);
1429 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1430 &sin6
->sin6_addr
, sin6
->sin6_port
,
1431 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1434 inp
->inp_lport
, 0, NULL
);
1435 socket_lock(inp
->inp_socket
, 0);
1437 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1438 otp
->t_state
== TCPS_TIME_WAIT
&&
1439 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1440 (otp
->t_flags
& TF_RCVD_CC
)) {
1441 otp
= tcp_close(otp
);
1447 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1448 /*lock inversion issue, mostly with udp multicast packets */
1449 socket_unlock(inp
->inp_socket
, 0);
1450 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1451 socket_lock(inp
->inp_socket
, 0);
1453 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
1454 inp
->in6p_laddr
= addr6
;
1455 inp
->in6p_last_outifp
= outif
; /* no reference needed */
1456 inp
->in6p_flags
|= INP_IN6ADDR_ANY
;
1458 inp
->in6p_faddr
= sin6
->sin6_addr
;
1459 inp
->inp_fport
= sin6
->sin6_port
;
1460 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1461 inp
->inp_flow
= sin6
->sin6_flowinfo
;
1463 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1465 if (inp
->inp_flowhash
== 0)
1466 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1467 /* update flowinfo - RFC 6437 */
1468 if (inp
->inp_flow
== 0 && inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
1469 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
1471 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
1474 tcp_set_max_rwinscale(tp
, so
, TCP_AUTORCVBUF_MAX(outif
));
1477 tcpstat
.tcps_connattempt
++;
1478 tp
->t_state
= TCPS_SYN_SENT
;
1479 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1480 TCP_CONN_KEEPINIT(tp
));
1481 tp
->iss
= tcp_new_isn(tp
);
1482 tcp_sendseqinit(tp
);
1484 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1488 ifnet_release(outif
);
1495 * Export TCP internal state information via a struct tcp_info
1498 tcp_fill_info(struct tcpcb
*tp
, struct tcp_info
*ti
)
1500 struct inpcb
*inp
= tp
->t_inpcb
;
1502 bzero(ti
, sizeof(*ti
));
1504 ti
->tcpi_state
= tp
->t_state
;
1505 ti
->tcpi_flowhash
= inp
->inp_flowhash
;
1507 if (tp
->t_state
> TCPS_LISTEN
) {
1508 if (TSTMP_SUPPORTED(tp
))
1509 ti
->tcpi_options
|= TCPI_OPT_TIMESTAMPS
;
1510 if (SACK_ENABLED(tp
))
1511 ti
->tcpi_options
|= TCPI_OPT_SACK
;
1512 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1513 ti
->tcpi_options
|= TCPI_OPT_WSCALE
;
1514 ti
->tcpi_snd_wscale
= tp
->snd_scale
;
1515 ti
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1517 if (TCP_ECN_ENABLED(tp
))
1518 ti
->tcpi_options
|= TCPI_OPT_ECN
;
1520 /* Are we in retranmission episode */
1521 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1522 ti
->tcpi_flags
|= TCPI_FLAG_LOSSRECOVERY
;
1524 if (tp
->t_flags
& TF_STREAMING_ON
)
1525 ti
->tcpi_flags
|= TCPI_FLAG_STREAMING_ON
;
1527 ti
->tcpi_rto
= tp
->t_timer
[TCPT_REXMT
] ? tp
->t_rxtcur
: 0;
1528 ti
->tcpi_snd_mss
= tp
->t_maxseg
;
1529 ti
->tcpi_rcv_mss
= tp
->t_maxseg
;
1531 ti
->tcpi_rttcur
= tp
->t_rttcur
;
1532 ti
->tcpi_srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
1533 ti
->tcpi_rttvar
= tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
;
1534 ti
->tcpi_rttbest
= tp
->t_rttbest
>> TCP_RTT_SHIFT
;
1536 ti
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1537 ti
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1538 ti
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1540 ti
->tcpi_rcv_space
= tp
->rcv_wnd
;
1542 ti
->tcpi_snd_wnd
= tp
->snd_wnd
;
1543 ti
->tcpi_snd_nxt
= tp
->snd_nxt
;
1544 ti
->tcpi_rcv_nxt
= tp
->rcv_nxt
;
1546 /* convert bytes/msec to bits/sec */
1547 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1548 tp
->t_bwmeas
!= NULL
) {
1549 ti
->tcpi_snd_bw
= (tp
->t_bwmeas
->bw_sndbw
* 8000);
1552 ti
->tcpi_last_outif
= (tp
->t_inpcb
->inp_last_outifp
== NULL
) ? 0 :
1553 tp
->t_inpcb
->inp_last_outifp
->if_index
;
1555 //atomic_get_64(ti->tcpi_txbytes, &inp->inp_stat->txbytes);
1556 ti
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1557 ti
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1558 ti
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1559 ti
->tcpi_txretransmitpackets
= tp
->t_stat
.rxmitpkts
;
1560 ti
->tcpi_txunacked
= tp
->snd_max
- tp
->snd_una
;
1562 //atomic_get_64(ti->tcpi_rxbytes, &inp->inp_stat->rxbytes);
1563 ti
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1564 ti
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1565 ti
->tcpi_rxduplicatebytes
= tp
->t_stat
.rxduplicatebytes
;
1566 ti
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1568 if (tp
->t_state
> TCPS_LISTEN
) {
1569 ti
->tcpi_synrexmits
= tp
->t_stat
.synrxtshift
;
1571 ti
->tcpi_cell_rxpackets
= inp
->inp_cstat
->rxpackets
;
1572 ti
->tcpi_cell_rxbytes
= inp
->inp_cstat
->rxbytes
;
1573 ti
->tcpi_cell_txpackets
= inp
->inp_cstat
->txpackets
;
1574 ti
->tcpi_cell_txbytes
= inp
->inp_cstat
->txbytes
;
1576 ti
->tcpi_wifi_rxpackets
= inp
->inp_wstat
->rxpackets
;
1577 ti
->tcpi_wifi_rxbytes
= inp
->inp_wstat
->rxbytes
;
1578 ti
->tcpi_wifi_txpackets
= inp
->inp_wstat
->txpackets
;
1579 ti
->tcpi_wifi_txbytes
= inp
->inp_wstat
->txbytes
;
1581 ti
->tcpi_wired_rxpackets
= inp
->inp_Wstat
->rxpackets
;
1582 ti
->tcpi_wired_rxbytes
= inp
->inp_Wstat
->rxbytes
;
1583 ti
->tcpi_wired_txpackets
= inp
->inp_Wstat
->txpackets
;
1584 ti
->tcpi_wired_txbytes
= inp
->inp_Wstat
->txbytes
;
1585 tcp_get_connectivity_status(tp
, &ti
->tcpi_connstatus
);
1587 ti
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1588 ti
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1589 ti
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1590 ti
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1592 ti
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1593 ti
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1594 ti
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1595 ti
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1596 ti
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1597 ti
->tcpi_tfo_cookie_wrong
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_WRONG
);
1598 ti
->tcpi_tfo_no_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_NO_COOKIE_RCV
);
1599 ti
->tcpi_tfo_heuristics_disable
= !!(tp
->t_tfo_stats
& TFO_S_HEURISTICS_DISABLE
);
1600 ti
->tcpi_tfo_send_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_SEND_BLACKHOLE
);
1601 ti
->tcpi_tfo_recv_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_RECV_BLACKHOLE
);
1602 ti
->tcpi_tfo_onebyte_proxy
= !!(tp
->t_tfo_stats
& TFO_S_ONE_BYTE_PROXY
);
1604 ti
->tcpi_ecn_client_setup
= !!(tp
->ecn_flags
& TE_SETUPSENT
);
1605 ti
->tcpi_ecn_server_setup
= !!(tp
->ecn_flags
& TE_SETUPRECEIVED
);
1606 ti
->tcpi_ecn_success
= (tp
->ecn_flags
& TE_ECN_ON
) == TE_ECN_ON
? 1 : 0;
1607 ti
->tcpi_ecn_lost_syn
= !!(tp
->ecn_flags
& TE_LOST_SYN
);
1608 ti
->tcpi_ecn_lost_synack
= !!(tp
->ecn_flags
& TE_LOST_SYNACK
);
1610 ti
->tcpi_local_peer
= !!(tp
->t_flags
& TF_LOCAL
);
1612 if (tp
->t_inpcb
->inp_last_outifp
!= NULL
) {
1613 if (IFNET_IS_CELLULAR(tp
->t_inpcb
->inp_last_outifp
))
1614 ti
->tcpi_if_cell
= 1;
1615 if (IFNET_IS_WIFI(tp
->t_inpcb
->inp_last_outifp
))
1616 ti
->tcpi_if_wifi
= 1;
1617 if (IFNET_IS_WIRED(tp
->t_inpcb
->inp_last_outifp
))
1618 ti
->tcpi_if_wired
= 1;
1619 if (IFNET_IS_WIFI_INFRA(tp
->t_inpcb
->inp_last_outifp
))
1620 ti
->tcpi_if_wifi_infra
= 1;
1621 if (tp
->t_inpcb
->inp_last_outifp
->if_eflags
& IFEF_AWDL
)
1622 ti
->tcpi_if_wifi_awdl
= 1;
1624 if (tp
->tcp_cc_index
== TCP_CC_ALGO_BACKGROUND_INDEX
)
1625 ti
->tcpi_snd_background
= 1;
1626 if (tcp_recv_bg
== 1 ||
1627 IS_TCP_RECV_BG(tp
->t_inpcb
->inp_socket
))
1628 ti
->tcpi_rcv_background
= 1;
1630 ti
->tcpi_ecn_recv_ce
= tp
->t_ecn_recv_ce
;
1631 ti
->tcpi_ecn_recv_cwr
= tp
->t_ecn_recv_cwr
;
1633 ti
->tcpi_rcvoopack
= tp
->t_rcvoopack
;
1634 ti
->tcpi_pawsdrop
= tp
->t_pawsdrop
;
1635 ti
->tcpi_sack_recovery_episode
= tp
->t_sack_recovery_episode
;
1636 ti
->tcpi_reordered_pkts
= tp
->t_reordered_pkts
;
1637 ti
->tcpi_dsack_sent
= tp
->t_dsack_sent
;
1638 ti
->tcpi_dsack_recvd
= tp
->t_dsack_recvd
;
1642 __private_extern__ errno_t
1643 tcp_fill_info_for_info_tuple(struct info_tuple
*itpl
, struct tcp_info
*ti
)
1645 struct inpcbinfo
*pcbinfo
= NULL
;
1646 struct inpcb
*inp
= NULL
;
1650 if (itpl
->itpl_proto
== IPPROTO_TCP
)
1655 if (itpl
->itpl_local_sa
.sa_family
== AF_INET
&&
1656 itpl
->itpl_remote_sa
.sa_family
== AF_INET
) {
1657 inp
= in_pcblookup_hash(pcbinfo
,
1658 itpl
->itpl_remote_sin
.sin_addr
,
1659 itpl
->itpl_remote_sin
.sin_port
,
1660 itpl
->itpl_local_sin
.sin_addr
,
1661 itpl
->itpl_local_sin
.sin_port
,
1663 } else if (itpl
->itpl_local_sa
.sa_family
== AF_INET6
&&
1664 itpl
->itpl_remote_sa
.sa_family
== AF_INET6
) {
1665 struct in6_addr ina6_local
;
1666 struct in6_addr ina6_remote
;
1668 ina6_local
= itpl
->itpl_local_sin6
.sin6_addr
;
1669 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local
) &&
1670 itpl
->itpl_local_sin6
.sin6_scope_id
)
1671 ina6_local
.s6_addr16
[1] = htons(itpl
->itpl_local_sin6
.sin6_scope_id
);
1673 ina6_remote
= itpl
->itpl_remote_sin6
.sin6_addr
;
1674 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote
) &&
1675 itpl
->itpl_remote_sin6
.sin6_scope_id
)
1676 ina6_remote
.s6_addr16
[1] = htons(itpl
->itpl_remote_sin6
.sin6_scope_id
);
1678 inp
= in6_pcblookup_hash(pcbinfo
,
1680 itpl
->itpl_remote_sin6
.sin6_port
,
1682 itpl
->itpl_local_sin6
.sin6_port
,
1687 if (inp
== NULL
|| (so
= inp
->inp_socket
) == NULL
)
1691 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1692 socket_unlock(so
, 0);
1695 tp
= intotcpcb(inp
);
1697 tcp_fill_info(tp
, ti
);
1698 socket_unlock(so
, 0);
1704 tcp_connection_fill_info(struct tcpcb
*tp
, struct tcp_connection_info
*tci
)
1706 struct inpcb
*inp
= tp
->t_inpcb
;
1708 bzero(tci
, sizeof(*tci
));
1709 tci
->tcpi_state
= tp
->t_state
;
1710 if (tp
->t_state
> TCPS_LISTEN
) {
1711 if (TSTMP_SUPPORTED(tp
))
1712 tci
->tcpi_options
|= TCPCI_OPT_TIMESTAMPS
;
1713 if (SACK_ENABLED(tp
))
1714 tci
->tcpi_options
|= TCPCI_OPT_SACK
;
1715 if (TCP_WINDOW_SCALE_ENABLED(tp
)) {
1716 tci
->tcpi_options
|= TCPCI_OPT_WSCALE
;
1717 tci
->tcpi_snd_wscale
= tp
->snd_scale
;
1718 tci
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1720 if (TCP_ECN_ENABLED(tp
))
1721 tci
->tcpi_options
|= TCPCI_OPT_ECN
;
1722 if (IN_FASTRECOVERY(tp
) || tp
->t_rxtshift
> 0)
1723 tci
->tcpi_flags
|= TCPCI_FLAG_LOSSRECOVERY
;
1724 if (tp
->t_flagsext
& TF_PKTS_REORDERED
)
1725 tci
->tcpi_flags
|= TCPCI_FLAG_REORDERING_DETECTED
;
1726 tci
->tcpi_rto
= (tp
->t_timer
[TCPT_REXMT
] > 0) ?
1728 tci
->tcpi_maxseg
= tp
->t_maxseg
;
1729 tci
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1730 tci
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1731 tci
->tcpi_snd_wnd
= tp
->snd_wnd
;
1732 tci
->tcpi_snd_sbbytes
= inp
->inp_socket
->so_snd
.sb_cc
;
1733 tci
->tcpi_rcv_wnd
= tp
->rcv_wnd
;
1734 tci
->tcpi_rttcur
= tp
->t_rttcur
;
1735 tci
->tcpi_srtt
= (tp
->t_srtt
>> TCP_RTT_SHIFT
);
1736 tci
->tcpi_rttvar
= (tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
);
1737 tci
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1738 tci
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1739 tci
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1740 tci
->tcpi_txretransmitpackets
= tp
->t_stat
.rxmitpkts
;
1741 tci
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1742 tci
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1743 tci
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1745 tci
->tcpi_tfo_syn_data_rcv
= !!(tp
->t_tfo_stats
& TFO_S_SYNDATA_RCV
);
1746 tci
->tcpi_tfo_cookie_req_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIEREQ_RECV
);
1747 tci
->tcpi_tfo_cookie_sent
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_SENT
);
1748 tci
->tcpi_tfo_cookie_invalid
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_INVALID
);
1749 tci
->tcpi_tfo_cookie_req
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_REQ
);
1750 tci
->tcpi_tfo_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_RCV
);
1751 tci
->tcpi_tfo_syn_data_sent
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_SENT
);
1752 tci
->tcpi_tfo_syn_data_acked
= !!(tp
->t_tfo_stats
& TFO_S_SYN_DATA_ACKED
);
1753 tci
->tcpi_tfo_syn_loss
= !!(tp
->t_tfo_stats
& TFO_S_SYN_LOSS
);
1754 tci
->tcpi_tfo_cookie_wrong
= !!(tp
->t_tfo_stats
& TFO_S_COOKIE_WRONG
);
1755 tci
->tcpi_tfo_no_cookie_rcv
= !!(tp
->t_tfo_stats
& TFO_S_NO_COOKIE_RCV
);
1756 tci
->tcpi_tfo_heuristics_disable
= !!(tp
->t_tfo_stats
& TFO_S_HEURISTICS_DISABLE
);
1757 tci
->tcpi_tfo_send_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_SEND_BLACKHOLE
);
1758 tci
->tcpi_tfo_recv_blackhole
= !!(tp
->t_tfo_stats
& TFO_S_RECV_BLACKHOLE
);
1759 tci
->tcpi_tfo_onebyte_proxy
= !!(tp
->t_tfo_stats
& TFO_S_ONE_BYTE_PROXY
);
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 #if !CONFIG_EMBEDDED
1771 proc_t caller
= PROC_NULL
;
1772 proc_t caller_parent
= PROC_NULL
;
1773 char command_name
[MAXCOMLEN
+ 1] = "";
1774 char parent_name
[MAXCOMLEN
+ 1] = "";
1776 if ((caller
= proc_self()) != PROC_NULL
) {
1777 /* get process name */
1778 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
1780 /* get parent process name if possible */
1781 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
1782 strlcpy(parent_name
, caller_parent
->p_comm
,
1783 sizeof(parent_name
));
1784 proc_rele(caller_parent
);
1787 if ((escape_str(command_name
, strlen(command_name
),
1788 sizeof(command_name
)) == 0) &&
1789 (escape_str(parent_name
, strlen(parent_name
),
1790 sizeof(parent_name
)) == 0)) {
1791 kern_asl_msg(LOG_DEBUG
, "messagetracer",
1793 "com.apple.message.domain",
1794 "com.apple.kernel.tcpstat", /* 1 */
1795 "com.apple.message.signature",
1797 "com.apple.message.signature2", command_name
, /* 3 */
1798 "com.apple.message.signature3", parent_name
, /* 4 */
1799 "com.apple.message.summarize", "YES", /* 5 */
1804 if (caller
!= PROC_NULL
)
1806 #endif /* !CONFIG_EMBEDDED */
1808 if (req
->newptr
== USER_ADDR_NULL
) {
1811 if (req
->newlen
< sizeof(struct info_tuple
)) {
1814 error
= SYSCTL_IN(req
, &itpl
, sizeof(struct info_tuple
));
1818 error
= tcp_fill_info_for_info_tuple(&itpl
, &ti
);
1822 error
= SYSCTL_OUT(req
, &ti
, sizeof(struct tcp_info
));
1831 tcp_lookup_peer_pid_locked(struct socket
*so
, pid_t
*out_pid
)
1833 int error
= EHOSTUNREACH
;
1835 if ((so
->so_state
& SS_ISCONNECTED
) == 0) return ENOTCONN
;
1837 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
1838 uint16_t lport
= inp
->inp_lport
;
1839 uint16_t fport
= inp
->inp_fport
;
1840 struct inpcb
*finp
= NULL
;
1842 if (inp
->inp_vflag
& INP_IPV6
) {
1843 struct in6_addr laddr6
= inp
->in6p_laddr
;
1844 struct in6_addr faddr6
= inp
->in6p_faddr
;
1845 socket_unlock(so
, 0);
1846 finp
= in6_pcblookup_hash(&tcbinfo
, &laddr6
, lport
, &faddr6
, fport
, 0, NULL
);
1848 } else if (inp
->inp_vflag
& INP_IPV4
) {
1849 struct in_addr laddr4
= inp
->inp_laddr
;
1850 struct in_addr faddr4
= inp
->inp_faddr
;
1851 socket_unlock(so
, 0);
1852 finp
= in_pcblookup_hash(&tcbinfo
, laddr4
, lport
, faddr4
, fport
, 0, NULL
);
1857 *out_pid
= finp
->inp_socket
->last_pid
;
1859 /* Avoid deadlock due to same inpcb for loopback socket */
1861 in_pcb_checkstate(finp
, WNT_RELEASE
, 1);
1863 in_pcb_checkstate(finp
, WNT_RELEASE
, 0);
1870 tcp_getconninfo(struct socket
*so
, struct conninfo_tcp
*tcp_ci
)
1872 (void) tcp_lookup_peer_pid_locked(so
, &tcp_ci
->tcpci_peer_pid
);
1873 tcp_fill_info(sototcpcb(so
), &tcp_ci
->tcpci_tcp_info
);
1877 * The new sockopt interface makes it possible for us to block in the
1878 * copyin/out step (if we take a page fault). Taking a page fault at
1879 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1880 * use TSM, there probably isn't any need for this function to run at
1881 * splnet() any more. This needs more examination.)
1884 tcp_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1886 int error
, opt
, optval
;
1891 inp
= sotoinpcb(so
);
1893 return (ECONNRESET
);
1895 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
1896 if (sopt
->sopt_level
!= IPPROTO_TCP
&&
1897 !(sopt
->sopt_level
== SOL_SOCKET
&& (sopt
->sopt_name
== SO_FLUSH
||
1898 sopt
->sopt_name
== SO_TRAFFIC_MGT_BACKGROUND
))) {
1900 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1901 error
= ip6_ctloutput(so
, sopt
);
1904 error
= ip_ctloutput(so
, sopt
);
1907 tp
= intotcpcb(inp
);
1909 return (ECONNRESET
);
1912 calculate_tcp_clock();
1914 switch (sopt
->sopt_dir
) {
1916 switch (sopt
->sopt_name
) {
1920 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1925 switch (sopt
->sopt_name
) {
1936 opt
= 0; /* dead code to fool gcc */
1943 tp
->t_flags
&= ~opt
;
1945 case TCP_RXT_FINDROP
:
1946 case TCP_NOTIMEWAIT
:
1947 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1951 switch (sopt
->sopt_name
) {
1952 case TCP_RXT_FINDROP
:
1953 opt
= TF_RXTFINDROP
;
1955 case TCP_NOTIMEWAIT
:
1956 opt
= TF_NOTIMEWAIT
;
1963 tp
->t_flagsext
|= opt
;
1965 tp
->t_flagsext
&= ~opt
;
1967 case TCP_MEASURE_SND_BW
:
1968 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1972 opt
= TF_MEASURESNDBW
;
1974 if (tp
->t_bwmeas
== NULL
) {
1975 tp
->t_bwmeas
= tcp_bwmeas_alloc(tp
);
1976 if (tp
->t_bwmeas
== NULL
) {
1981 tp
->t_flagsext
|= opt
;
1983 tp
->t_flagsext
&= ~opt
;
1984 /* Reset snd bw measurement state */
1985 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
1986 if (tp
->t_bwmeas
!= NULL
) {
1987 tcp_bwmeas_free(tp
);
1991 case TCP_MEASURE_BW_BURST
: {
1992 struct tcp_measure_bw_burst in
;
1993 uint32_t minpkts
, maxpkts
;
1994 bzero(&in
, sizeof(in
));
1996 error
= sooptcopyin(sopt
, &in
, sizeof(in
),
2000 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2001 tp
->t_bwmeas
== NULL
) {
2005 minpkts
= (in
.min_burst_size
!= 0) ? in
.min_burst_size
:
2006 tp
->t_bwmeas
->bw_minsizepkts
;
2007 maxpkts
= (in
.max_burst_size
!= 0) ? in
.max_burst_size
:
2008 tp
->t_bwmeas
->bw_maxsizepkts
;
2009 if (minpkts
> maxpkts
) {
2013 tp
->t_bwmeas
->bw_minsizepkts
= minpkts
;
2014 tp
->t_bwmeas
->bw_maxsizepkts
= maxpkts
;
2015 tp
->t_bwmeas
->bw_minsize
= (minpkts
* tp
->t_maxseg
);
2016 tp
->t_bwmeas
->bw_maxsize
= (maxpkts
* tp
->t_maxseg
);
2020 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2025 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
2026 optval
+ 40 >= tcp_minmss
)
2027 tp
->t_maxseg
= optval
;
2033 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2037 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2040 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
2041 /* reset the timer to new value */
2042 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2043 TCP_CONN_KEEPIDLE(tp
));
2044 tcp_check_timer_state(tp
);
2048 case TCP_CONNECTIONTIMEOUT
:
2049 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2053 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2056 tp
->t_keepinit
= optval
* TCP_RETRANSHZ
;
2057 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
2058 tp
->t_state
== TCPS_SYN_SENT
) {
2059 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
2060 TCP_CONN_KEEPINIT(tp
));
2061 tcp_check_timer_state(tp
);
2067 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2071 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
2074 tp
->t_keepintvl
= optval
* TCP_RETRANSHZ
;
2075 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2076 TCP_CONN_MAXIDLE(tp
) > 0) {
2077 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2078 TCP_CONN_MAXIDLE(tp
));
2079 tcp_check_timer_state(tp
);
2085 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2089 if (optval
< 0 || optval
> INT32_MAX
) {
2092 tp
->t_keepcnt
= optval
;
2093 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
2094 TCP_CONN_MAXIDLE(tp
) > 0) {
2095 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2096 TCP_CONN_MAXIDLE(tp
));
2097 tcp_check_timer_state(tp
);
2102 case TCP_KEEPALIVE_OFFLOAD
:
2103 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2107 if (optval
< 0 || optval
> INT32_MAX
) {
2112 inp
->inp_flags2
|= INP2_KEEPALIVE_OFFLOAD
;
2114 inp
->inp_flags2
&= ~INP2_KEEPALIVE_OFFLOAD
;
2117 case PERSIST_TIMEOUT
:
2118 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2125 tp
->t_persist_timeout
= optval
* TCP_RETRANSHZ
;
2127 case TCP_RXT_CONNDROPTIME
:
2128 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2135 tp
->t_rxt_conndroptime
= optval
* TCP_RETRANSHZ
;
2137 case TCP_NOTSENT_LOWAT
:
2138 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2147 so
->so_flags
&= ~(SOF_NOTSENT_LOWAT
);
2148 tp
->t_notsent_lowat
= 0;
2150 so
->so_flags
|= SOF_NOTSENT_LOWAT
;
2151 tp
->t_notsent_lowat
= optval
;
2155 case TCP_ADAPTIVE_READ_TIMEOUT
:
2156 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2161 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2164 } else if (optval
== 0) {
2165 tp
->t_adaptive_rtimo
= 0;
2166 tcp_keepalive_reset(tp
);
2169 mptcp_reset_keepalive(tp
);
2171 tp
->t_adaptive_rtimo
= optval
;
2174 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2175 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2180 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2184 tp
->t_adaptive_wtimo
= optval
;
2187 case TCP_ENABLE_MSGS
:
2188 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2192 if (optval
< 0 || optval
> 1) {
2194 } else if (optval
== 1) {
2196 * Check if messages option is already
2197 * enabled, if so return.
2199 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2200 VERIFY(so
->so_msg_state
!= NULL
);
2205 * allocate memory for storing message
2208 VERIFY(so
->so_msg_state
== NULL
);
2209 MALLOC(so
->so_msg_state
,
2211 sizeof(struct msg_state
),
2212 M_TEMP
, M_WAITOK
| M_ZERO
);
2213 if (so
->so_msg_state
== NULL
) {
2218 /* Enable message delivery */
2219 so
->so_flags
|= SOF_ENABLE_MSGS
;
2222 * Can't disable message delivery on socket
2223 * because of restrictions imposed by
2229 case TCP_SENDMOREACKS
:
2230 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2234 if (optval
< 0 || optval
> 1) {
2236 } else if (optval
== 0) {
2237 tp
->t_flagsext
&= ~(TF_NOSTRETCHACK
);
2239 tp
->t_flagsext
|= TF_NOSTRETCHACK
;
2242 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2243 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2247 if (optval
< 0 || optval
> 1) {
2249 } else if (optval
== 0) {
2250 tp
->t_flagsext
&= ~TF_NOBLACKHOLE_DETECTION
;
2252 tp
->t_flagsext
|= TF_NOBLACKHOLE_DETECTION
;
2253 if ((tp
->t_flags
& TF_BLACKHOLE
) &&
2254 tp
->t_pmtud_saved_maxopd
> 0)
2255 tcp_pmtud_revert_segment_size(tp
);
2259 if (!(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2264 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2268 if (optval
< 0 || optval
> 1) {
2272 if (tp
->t_state
!= TCPS_LISTEN
) {
2277 tp
->t_flagsext
|= TF_FASTOPEN
;
2279 tcp_disable_tfo(tp
);
2281 case TCP_FASTOPEN_FORCE_HEURISTICS
:
2282 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2287 if (optval
< 0 || optval
> 1) {
2292 if (tp
->t_state
!= TCPS_CLOSED
) {
2297 tp
->t_flagsext
|= TF_FASTOPEN_HEUR
;
2299 tp
->t_flagsext
&= ~TF_FASTOPEN_HEUR
;
2302 case TCP_ENABLE_ECN
:
2303 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2308 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2309 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2311 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2312 tp
->ecn_flags
|= TE_ECN_MODE_DISABLE
;
2316 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
2320 if (optval
== ECN_MODE_DEFAULT
) {
2321 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2322 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2323 } else if (optval
== ECN_MODE_ENABLE
) {
2324 tp
->ecn_flags
|= TE_ECN_MODE_ENABLE
;
2325 tp
->ecn_flags
&= ~TE_ECN_MODE_DISABLE
;
2326 } else if (optval
== ECN_MODE_DISABLE
) {
2327 tp
->ecn_flags
&= ~TE_ECN_MODE_ENABLE
;
2328 tp
->ecn_flags
|= TE_ECN_MODE_DISABLE
;
2333 case TCP_NOTIFY_ACKNOWLEDGEMENT
:
2334 error
= sooptcopyin(sopt
, &optval
,
2335 sizeof(optval
), sizeof(optval
));
2342 if (tp
->t_notify_ack_count
>= TCP_MAX_NOTIFY_ACK
) {
2343 error
= ETOOMANYREFS
;
2348 * validate that the given marker id is not
2349 * a duplicate to avoid ambiguity
2351 if ((error
= tcp_notify_ack_id_valid(tp
, so
,
2355 error
= tcp_add_notify_ack_marker(tp
, optval
);
2358 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2359 sizeof (optval
))) != 0)
2362 error
= inp_flush(inp
, optval
);
2365 case SO_TRAFFIC_MGT_BACKGROUND
:
2366 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2367 sizeof (optval
))) != 0)
2371 socket_set_traffic_mgt_flags_locked(so
,
2372 TRAFFIC_MGT_SO_BACKGROUND
);
2374 socket_clear_traffic_mgt_flags_locked(so
,
2375 TRAFFIC_MGT_SO_BACKGROUND
);
2378 case TCP_RXT_MINIMUM_TIMEOUT
:
2379 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2388 tp
->t_rxt_minimum_timeout
= 0;
2390 tp
->t_rxt_minimum_timeout
= min(optval
,
2391 TCP_RXT_MINIMUM_TIMEOUT_LIMIT
);
2392 /* convert to milliseconds */
2393 tp
->t_rxt_minimum_timeout
*= TCP_RETRANSHZ
;
2397 error
= ENOPROTOOPT
;
2403 switch (sopt
->sopt_name
) {
2405 optval
= tp
->t_flags
& TF_NODELAY
;
2408 optval
= tp
->t_maxseg
;
2411 if (tp
->t_keepidle
> 0)
2412 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
2414 optval
= tcp_keepidle
/ TCP_RETRANSHZ
;
2417 if (tp
->t_keepintvl
> 0)
2418 optval
= tp
->t_keepintvl
/ TCP_RETRANSHZ
;
2420 optval
= tcp_keepintvl
/ TCP_RETRANSHZ
;
2423 if (tp
->t_keepcnt
> 0)
2424 optval
= tp
->t_keepcnt
;
2426 optval
= tcp_keepcnt
;
2428 case TCP_KEEPALIVE_OFFLOAD
:
2429 optval
= !!(inp
->inp_flags2
& INP2_KEEPALIVE_OFFLOAD
);
2432 optval
= tp
->t_flags
& TF_NOOPT
;
2435 optval
= tp
->t_flags
& TF_NOPUSH
;
2437 case TCP_ENABLE_ECN
:
2438 optval
= (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
) ? 1 : 0;
2441 if (tp
->ecn_flags
& TE_ECN_MODE_ENABLE
)
2442 optval
= ECN_MODE_ENABLE
;
2443 else if (tp
->ecn_flags
& TE_ECN_MODE_DISABLE
)
2444 optval
= ECN_MODE_DISABLE
;
2446 optval
= ECN_MODE_DEFAULT
;
2448 case TCP_CONNECTIONTIMEOUT
:
2449 optval
= tp
->t_keepinit
/ TCP_RETRANSHZ
;
2451 case PERSIST_TIMEOUT
:
2452 optval
= tp
->t_persist_timeout
/ TCP_RETRANSHZ
;
2454 case TCP_RXT_CONNDROPTIME
:
2455 optval
= tp
->t_rxt_conndroptime
/ TCP_RETRANSHZ
;
2457 case TCP_RXT_FINDROP
:
2458 optval
= tp
->t_flagsext
& TF_RXTFINDROP
;
2460 case TCP_NOTIMEWAIT
:
2461 optval
= (tp
->t_flagsext
& TF_NOTIMEWAIT
) ? 1 : 0;
2464 if (tp
->t_state
!= TCPS_LISTEN
||
2465 !(tcp_fastopen
& TCP_FASTOPEN_SERVER
)) {
2469 optval
= tfo_enabled(tp
);
2471 case TCP_FASTOPEN_FORCE_HEURISTICS
:
2472 optval
= (tp
->t_flagsext
& TF_FASTOPEN_HEUR
) ? 1 : 0;
2474 case TCP_MEASURE_SND_BW
:
2475 optval
= tp
->t_flagsext
& TF_MEASURESNDBW
;
2480 tcp_fill_info(tp
, &ti
);
2481 error
= sooptcopyout(sopt
, &ti
, sizeof(struct tcp_info
));
2485 case TCP_CONNECTION_INFO
: {
2486 struct tcp_connection_info tci
;
2487 tcp_connection_fill_info(tp
, &tci
);
2488 error
= sooptcopyout(sopt
, &tci
,
2489 sizeof(struct tcp_connection_info
));
2492 case TCP_MEASURE_BW_BURST
: {
2493 struct tcp_measure_bw_burst out
;
2494 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2495 tp
->t_bwmeas
== NULL
) {
2499 out
.min_burst_size
= tp
->t_bwmeas
->bw_minsizepkts
;
2500 out
.max_burst_size
= tp
->t_bwmeas
->bw_maxsizepkts
;
2501 error
= sooptcopyout(sopt
, &out
, sizeof(out
));
2504 case TCP_NOTSENT_LOWAT
:
2505 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0) {
2506 optval
= tp
->t_notsent_lowat
;
2512 case TCP_ENABLE_MSGS
:
2513 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2519 case TCP_SENDMOREACKS
:
2520 if (tp
->t_flagsext
& TF_NOSTRETCHACK
)
2525 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2526 if (tp
->t_flagsext
& TF_NOBLACKHOLE_DETECTION
)
2531 case TCP_PEER_PID
: {
2533 error
= tcp_lookup_peer_pid_locked(so
, &pid
);
2535 error
= sooptcopyout(sopt
, &pid
, sizeof(pid
));
2538 case TCP_ADAPTIVE_READ_TIMEOUT
:
2539 optval
= tp
->t_adaptive_rtimo
;
2541 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2542 optval
= tp
->t_adaptive_wtimo
;
2544 case SO_TRAFFIC_MGT_BACKGROUND
:
2545 optval
= (so
->so_flags1
&
2546 SOF1_TRAFFIC_MGT_SO_BACKGROUND
) ? 1 : 0;
2548 case TCP_NOTIFY_ACKNOWLEDGEMENT
: {
2549 struct tcp_notify_ack_complete retid
;
2551 if (sopt
->sopt_valsize
!= sizeof (retid
)) {
2555 bzero(&retid
, sizeof (retid
));
2556 tcp_get_notify_ack_count(tp
, &retid
);
2557 if (retid
.notify_complete_count
> 0)
2558 tcp_get_notify_ack_ids(tp
, &retid
);
2560 error
= sooptcopyout(sopt
, &retid
, sizeof (retid
));
2563 case TCP_RXT_MINIMUM_TIMEOUT
:
2564 optval
= tp
->t_rxt_minimum_timeout
/ TCP_RETRANSHZ
;
2567 error
= ENOPROTOOPT
;
2571 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2579 * tcp_sendspace and tcp_recvspace are the default send and receive window
2580 * sizes, respectively. These are obsolescent (this information should
2581 * be set by the route).
2583 u_int32_t tcp_sendspace
= 1448*256;
2584 u_int32_t tcp_recvspace
= 1448*384;
2586 /* During attach, the size of socket buffer allocated is limited to
2587 * sb_max in sbreserve. Disallow setting the tcp send and recv space
2588 * to be more than sb_max because that will cause tcp_attach to fail
2589 * (see radar 5713060)
2592 sysctl_tcp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
2593 int arg2
, struct sysctl_req
*req
)
2595 #pragma unused(arg2)
2596 u_int32_t new_value
= 0, *space_p
= NULL
;
2597 int changed
= 0, error
= 0;
2598 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
2600 switch (oidp
->oid_number
) {
2601 case TCPCTL_SENDSPACE
:
2602 space_p
= &tcp_sendspace
;
2604 case TCPCTL_RECVSPACE
:
2605 space_p
= &tcp_recvspace
;
2610 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
2611 &new_value
, &changed
);
2613 if (new_value
> 0 && new_value
<= sb_effective_max
) {
2614 *space_p
= new_value
;
2615 SYSCTL_SKMEM_UPDATE_AT_OFFSET(arg2
, new_value
);
2624 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
,
2625 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_sendspace
,
2626 offsetof(skmem_sysctl
, tcp
.sendspace
), sysctl_tcp_sospace
,
2627 "IU", "Maximum outgoing TCP datagram size");
2628 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
,
2629 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &tcp_recvspace
,
2630 offsetof(skmem_sysctl
, tcp
.recvspace
), sysctl_tcp_sospace
,
2631 "IU", "Maximum incoming TCP datagram size");
2632 #else /* SYSCTL_SKMEM */
2633 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2634 &tcp_sendspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum outgoing TCP datagram size");
2635 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2636 &tcp_recvspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum incoming TCP datagram size");
2637 #endif /* SYSCTL_SKMEM */
2640 * Attach TCP protocol to socket, allocating
2641 * internet protocol control block, tcp control block,
2642 * bufer space, and entering LISTEN state if to accept connections.
2644 * Returns: 0 Success
2645 * in_pcballoc:ENOBUFS
2646 * in_pcballoc:ENOMEM
2647 * in_pcballoc:??? [IPSEC specific]
2651 tcp_attach(struct socket
*so
, struct proc
*p
)
2657 int isipv6
= SOCK_CHECK_DOM(so
, PF_INET6
) != 0;
2660 error
= in_pcballoc(so
, &tcbinfo
, p
);
2664 inp
= sotoinpcb(so
);
2666 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
2667 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
2672 if (so
->so_snd
.sb_preconn_hiwat
== 0) {
2673 soreserve_preconnect(so
, 2048);
2676 if ((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0)
2677 so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
2678 if ((so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0)
2679 so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
2683 inp
->inp_vflag
|= INP_IPV6
;
2684 inp
->in6p_hops
= -1; /* use kernel default */
2688 inp
->inp_vflag
|= INP_IPV4
;
2689 tp
= tcp_newtcpcb(inp
);
2691 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
2693 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
2700 so
->so_state
|= nofd
;
2704 nstat_tcp_new_pcb(inp
);
2705 tp
->t_state
= TCPS_CLOSED
;
2710 * Initiate (or continue) disconnect.
2711 * If embryonic state, just send reset (once).
2712 * If in ``let data drain'' option and linger null, just drop.
2713 * Otherwise (hard), mark socket disconnecting and drop
2714 * current input data; switch states based on user close, and
2715 * send segment to peer (with FIN).
2717 static struct tcpcb
*
2718 tcp_disconnect(struct tcpcb
*tp
)
2720 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2722 if (so
->so_rcv
.sb_cc
!= 0 || tp
->t_reassqlen
!= 0)
2723 return tcp_drop(tp
, 0);
2725 if (tp
->t_state
< TCPS_ESTABLISHED
)
2727 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
2728 tp
= tcp_drop(tp
, 0);
2730 soisdisconnecting(so
);
2731 sbflush(&so
->so_rcv
);
2732 tp
= tcp_usrclosed(tp
);
2734 /* A reset has been sent but socket exists, do not send FIN */
2735 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
2736 (tp
) && (tp
->t_mpflags
& TMPF_RESET
))
2740 (void) tcp_output(tp
);
2746 * User issued close, and wish to trail through shutdown states:
2747 * if never received SYN, just forget it. If got a SYN from peer,
2748 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2749 * If already got a FIN from peer, then almost done; go to LAST_ACK
2750 * state. In all other cases, have already sent FIN to peer (e.g.
2751 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2752 * for peer to send FIN or not respond to keep-alives, etc.
2753 * We can let the user exit from the close as soon as the FIN is acked.
2755 static struct tcpcb
*
2756 tcp_usrclosed(struct tcpcb
*tp
)
2758 switch (tp
->t_state
) {
2766 case TCPS_SYN_RECEIVED
:
2767 tp
->t_flags
|= TF_NEEDFIN
;
2770 case TCPS_ESTABLISHED
:
2771 DTRACE_TCP4(state__change
, void, NULL
,
2772 struct inpcb
*, tp
->t_inpcb
,
2774 int32_t, TCPS_FIN_WAIT_1
);
2775 tp
->t_state
= TCPS_FIN_WAIT_1
;
2778 case TCPS_CLOSE_WAIT
:
2779 DTRACE_TCP4(state__change
, void, NULL
,
2780 struct inpcb
*, tp
->t_inpcb
,
2782 int32_t, TCPS_LAST_ACK
);
2783 tp
->t_state
= TCPS_LAST_ACK
;
2786 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
2787 soisdisconnected(tp
->t_inpcb
->inp_socket
);
2788 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
2789 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
2790 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2791 TCP_CONN_MAXIDLE(tp
));
2797 tcp_in_cksum_stats(u_int32_t len
)
2799 tcpstat
.tcps_rcv_swcsum
++;
2800 tcpstat
.tcps_rcv_swcsum_bytes
+= len
;
2804 tcp_out_cksum_stats(u_int32_t len
)
2806 tcpstat
.tcps_snd_swcsum
++;
2807 tcpstat
.tcps_snd_swcsum_bytes
+= len
;
2812 tcp_in6_cksum_stats(u_int32_t len
)
2814 tcpstat
.tcps_rcv6_swcsum
++;
2815 tcpstat
.tcps_rcv6_swcsum_bytes
+= len
;
2819 tcp_out6_cksum_stats(u_int32_t len
)
2821 tcpstat
.tcps_snd6_swcsum
++;
2822 tcpstat
.tcps_snd6_swcsum_bytes
+= len
;
2826 * When messages are enabled on a TCP socket, the message priority
2827 * is sent as a control message. This function will extract it.
2830 tcp_get_msg_priority(struct mbuf
*control
, uint32_t *msgpri
)
2833 if (control
== NULL
)
2836 for (cm
= M_FIRST_CMSGHDR(control
); cm
;
2837 cm
= M_NXT_CMSGHDR(control
, cm
)) {
2838 if (cm
->cmsg_len
< sizeof(struct cmsghdr
) ||
2839 cm
->cmsg_len
> control
->m_len
) {
2842 if (cm
->cmsg_level
== SOL_SOCKET
&&
2843 cm
->cmsg_type
== SCM_MSG_PRIORITY
) {
2844 *msgpri
= *(unsigned int *)(void *)CMSG_DATA(cm
);
2849 VERIFY(*msgpri
>= MSG_PRI_MIN
&& *msgpri
<= MSG_PRI_MAX
);