2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
61 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
71 #include <sys/domain.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/protosw.h>
77 #include <sys/syslog.h>
80 #include <net/route.h>
81 #include <net/ntstat.h>
82 #include <net/content_filter.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
87 #include <netinet/ip6.h>
89 #include <netinet/in_pcb.h>
91 #include <netinet6/in6_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
96 #include <netinet6/ip6_var.h>
98 #include <netinet/tcp.h>
99 #include <netinet/tcp_fsm.h>
100 #include <netinet/tcp_seq.h>
101 #include <netinet/tcp_timer.h>
102 #include <netinet/tcp_var.h>
103 #include <netinet/tcpip.h>
104 #include <mach/sdt.h>
106 #include <netinet/tcp_debug.h>
109 #include <netinet/mptcp_var.h>
113 #include <netinet6/ipsec.h>
117 #include <netinet/flow_divert.h>
118 #endif /* FLOW_DIVERT */
120 void tcp_fill_info(struct tcpcb
*, struct tcp_info
*);
121 errno_t
tcp_fill_info_for_info_tuple(struct info_tuple
*, struct tcp_info
*);
123 int tcp_sysctl_info(struct sysctl_oid
*, void *, int , struct sysctl_req
*);
126 * TCP protocol interface to socket abstraction.
128 extern char *tcpstates
[]; /* XXX ??? */
130 static int tcp_attach(struct socket
*, struct proc
*);
131 static int tcp_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
133 static int tcp6_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
134 static int tcp6_usr_connect(struct socket
*, struct sockaddr
*,
137 static struct tcpcb
*
138 tcp_disconnect(struct tcpcb
*);
139 static struct tcpcb
*
140 tcp_usrclosed(struct tcpcb
*);
142 extern uint32_t tcp_autorcvbuf_max
;
144 extern void tcp_sbrcv_trim(struct tcpcb
*tp
, struct sockbuf
*sb
);
147 #define TCPDEBUG0 int ostate = 0
148 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
149 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
150 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
154 #define TCPDEBUG2(req)
157 SYSCTL_PROC(_net_inet_tcp
, OID_AUTO
, info
,
158 CTLFLAG_RW
| CTLFLAG_LOCKED
| CTLFLAG_ANYBODY
| CTLFLAG_KERN
,
159 0 , 0, tcp_sysctl_info
, "S", "TCP info per tuple");
162 * TCP attaches to socket via pru_attach(), reserving space,
163 * and an internet control block.
169 * tcp_attach:??? [IPSEC specific]
172 tcp_usr_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
175 struct inpcb
*inp
= sotoinpcb(so
);
176 struct tcpcb
*tp
= 0;
185 error
= tcp_attach(so
, p
);
189 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
190 so
->so_linger
= TCP_LINGERTIME
* hz
;
193 TCPDEBUG2(PRU_ATTACH
);
198 * pru_detach() detaches the TCP protocol from the socket.
199 * If the protocol state is non-embryonic, then can't
200 * do this directly: have to initiate a pru_disconnect(),
201 * which may finish later; embryonic TCB's can just
205 tcp_usr_detach(struct socket
*so
)
208 struct inpcb
*inp
= sotoinpcb(so
);
212 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
213 return EINVAL
; /* XXX */
215 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
217 /* In case we got disconnected from the peer */
222 calculate_tcp_clock();
224 tp
= tcp_disconnect(tp
);
226 TCPDEBUG2(PRU_DETACH
);
231 #define COMMON_START() TCPDEBUG0; \
233 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
235 if (necp_socket_should_use_flow_divert(inp)) \
236 return (EPROTOTYPE); \
237 tp = intotcpcb(inp); \
239 calculate_tcp_clock(); \
242 #define COMMON_START() TCPDEBUG0; \
244 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) \
246 tp = intotcpcb(inp); \
248 calculate_tcp_clock(); \
252 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
256 * Give the socket an address.
259 * EINVAL Invalid argument [COMMON_START]
260 * EAFNOSUPPORT Address family not supported
261 * in_pcbbind:EADDRNOTAVAIL Address not available.
262 * in_pcbbind:EINVAL Invalid argument
263 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
264 * in_pcbbind:EACCES Permission denied
265 * in_pcbbind:EADDRINUSE Address in use
266 * in_pcbbind:EAGAIN Resource unavailable, try again
267 * in_pcbbind:EPERM Operation not permitted
270 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
273 struct inpcb
*inp
= sotoinpcb(so
);
275 struct sockaddr_in
*sinp
;
279 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
280 error
= EAFNOSUPPORT
;
285 * Must check for multicast addresses and disallow binding
288 sinp
= (struct sockaddr_in
*)(void *)nam
;
289 if (sinp
->sin_family
== AF_INET
&&
290 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
291 error
= EAFNOSUPPORT
;
294 error
= in_pcbbind(inp
, nam
, p
);
297 COMMON_END(PRU_BIND
);
303 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
306 struct inpcb
*inp
= sotoinpcb(so
);
308 struct sockaddr_in6
*sin6p
;
312 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
313 error
= EAFNOSUPPORT
;
318 * Must check for multicast addresses and disallow binding
321 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
322 if (sin6p
->sin6_family
== AF_INET6
&&
323 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
324 error
= EAFNOSUPPORT
;
327 inp
->inp_vflag
&= ~INP_IPV4
;
328 inp
->inp_vflag
|= INP_IPV6
;
329 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
330 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
331 inp
->inp_vflag
|= INP_IPV4
;
332 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
333 struct sockaddr_in sin
;
335 in6_sin6_2_sin(&sin
, sin6p
);
336 inp
->inp_vflag
|= INP_IPV4
;
337 inp
->inp_vflag
&= ~INP_IPV6
;
338 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
342 error
= in6_pcbbind(inp
, nam
, p
);
345 COMMON_END(PRU_BIND
);
350 * Prepare to accept connections.
353 * EINVAL [COMMON_START]
354 * in_pcbbind:EADDRNOTAVAIL Address not available.
355 * in_pcbbind:EINVAL Invalid argument
356 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
357 * in_pcbbind:EACCES Permission denied
358 * in_pcbbind:EADDRINUSE Address in use
359 * in_pcbbind:EAGAIN Resource unavailable, try again
360 * in_pcbbind:EPERM Operation not permitted
363 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
366 struct inpcb
*inp
= sotoinpcb(so
);
370 if (inp
->inp_lport
== 0)
371 error
= in_pcbbind(inp
, NULL
, p
);
373 tp
->t_state
= TCPS_LISTEN
;
374 COMMON_END(PRU_LISTEN
);
379 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
382 struct inpcb
*inp
= sotoinpcb(so
);
386 if (inp
->inp_lport
== 0) {
387 inp
->inp_vflag
&= ~INP_IPV4
;
388 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
389 inp
->inp_vflag
|= INP_IPV4
;
390 error
= in6_pcbbind(inp
, NULL
, p
);
393 tp
->t_state
= TCPS_LISTEN
;
394 COMMON_END(PRU_LISTEN
);
399 * Initiate connection to peer.
400 * Create a template for use in transmissions on this connection.
401 * Enter SYN_SENT state, and mark socket as connecting.
402 * Start keep-alive timer, and seed output sequence space.
403 * Send initial segment on connection.
406 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
409 struct inpcb
*inp
= sotoinpcb(so
);
411 struct sockaddr_in
*sinp
;
416 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
418 error
= so
->so_error
;
426 else if (necp_socket_should_use_flow_divert(inp
)) {
427 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
428 if (fd_ctl_unit
> 0) {
429 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
431 error
= flow_divert_connect_out(so
, nam
, p
);
438 #endif /* FLOW_DIVERT */
440 error
= cfil_sock_attach(so
);
443 #endif /* CONTENT_FILTER */
448 calculate_tcp_clock();
450 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
451 error
= EAFNOSUPPORT
;
455 * Must disallow TCP ``connections'' to multicast addresses.
457 sinp
= (struct sockaddr_in
*)(void *)nam
;
458 if (sinp
->sin_family
== AF_INET
459 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
460 error
= EAFNOSUPPORT
;
464 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
466 error
= tcp_output(tp
);
467 COMMON_END(PRU_CONNECT
);
471 tcp_usr_connectx_common(struct socket
*so
, int af
,
472 struct sockaddr_list
**src_sl
, struct sockaddr_list
**dst_sl
,
473 struct proc
*p
, uint32_t ifscope
, associd_t aid
, connid_t
*pcid
,
474 uint32_t flags
, void *arg
, uint32_t arglen
)
478 #pragma unused(flags, arg, arglen)
480 struct sockaddr_entry
*src_se
= NULL
, *dst_se
= NULL
;
481 struct inpcb
*inp
= sotoinpcb(so
);
487 VERIFY(dst_sl
!= NULL
);
489 /* select source (if specified) and destination addresses */
490 error
= in_selectaddrs(af
, src_sl
, &src_se
, dst_sl
, &dst_se
);
494 VERIFY(*dst_sl
!= NULL
&& dst_se
!= NULL
);
495 VERIFY(src_se
== NULL
|| *src_sl
!= NULL
);
496 VERIFY(dst_se
->se_addr
->sa_family
== af
);
497 VERIFY(src_se
== NULL
|| src_se
->se_addr
->sa_family
== af
);
500 inp_update_necp_policy(inp
, src_se
? src_se
->se_addr
: NULL
, dst_se
? dst_se
->se_addr
: NULL
, ifscope
);
504 * We get here for 2 cases:
506 * a. From MPTCP, to connect a subflow. There is no need to
507 * bind the socket to the source address and/or interface,
508 * since everything has been taken care of by MPTCP. We
509 * simply check whether or not this is for the initial
510 * MPTCP connection attempt, or to join an existing one.
512 * b. From the socket layer, to connect a TCP. Perform the
513 * bind to source address and/or interface as necessary.
516 if (flags
& TCP_CONNREQF_MPTCP
) {
517 struct mptsub_connreq
*mpcr
= arg
;
519 /* Check to make sure this came down from MPTCP */
520 if (arg
== NULL
|| arglen
!= sizeof (*mpcr
))
523 switch (mpcr
->mpcr_type
) {
524 case MPTSUB_CONNREQ_MP_ENABLE
:
526 case MPTSUB_CONNREQ_MP_ADD
:
534 /* bind socket to the specified interface, if requested */
535 if (ifscope
!= IFSCOPE_NONE
&&
536 (error
= inp_bindif(inp
, ifscope
, NULL
)) != 0)
539 /* if source address and/or port is specified, bind to it */
540 if (src_se
!= NULL
) {
541 struct sockaddr
*sa
= src_se
->se_addr
;
542 error
= sobindlock(so
, sa
, 0); /* already locked */
550 error
= tcp_usr_connect(so
, dst_se
->se_addr
, p
);
554 error
= tcp6_usr_connect(so
, dst_se
->se_addr
, p
);
562 if (error
== 0 && pcid
!= NULL
)
563 *pcid
= 1; /* there is only 1 connection for a TCP */
569 tcp_usr_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
570 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
571 associd_t aid
, connid_t
*pcid
, uint32_t flags
, void *arg
,
574 return (tcp_usr_connectx_common(so
, AF_INET
, src_sl
, dst_sl
,
575 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
));
580 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
583 struct inpcb
*inp
= sotoinpcb(so
);
585 struct sockaddr_in6
*sin6p
;
590 } else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
592 error
= so
->so_error
;
600 else if (necp_socket_should_use_flow_divert(inp
)) {
601 uint32_t fd_ctl_unit
= necp_socket_get_flow_divert_control_unit(inp
);
602 if (fd_ctl_unit
> 0) {
603 error
= flow_divert_pcb_init(so
, fd_ctl_unit
);
605 error
= flow_divert_connect_out(so
, nam
, p
);
612 #endif /* FLOW_DIVERT */
614 error
= cfil_sock_attach(so
);
617 #endif /* CONTENT_FILTER */
623 calculate_tcp_clock();
625 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
626 error
= EAFNOSUPPORT
;
631 * Must disallow TCP ``connections'' to multicast addresses.
633 sin6p
= (struct sockaddr_in6
*)(void *)nam
;
634 if (sin6p
->sin6_family
== AF_INET6
635 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
636 error
= EAFNOSUPPORT
;
640 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
641 struct sockaddr_in sin
;
643 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
646 in6_sin6_2_sin(&sin
, sin6p
);
647 inp
->inp_vflag
|= INP_IPV4
;
648 inp
->inp_vflag
&= ~INP_IPV6
;
649 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
651 error
= tcp_output(tp
);
654 inp
->inp_vflag
&= ~INP_IPV4
;
655 inp
->inp_vflag
|= INP_IPV6
;
656 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
658 error
= tcp_output(tp
);
661 COMMON_END(PRU_CONNECT
);
665 tcp6_usr_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
666 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
667 associd_t aid
, connid_t
*pcid
, uint32_t flags
, void *arg
,
670 return (tcp_usr_connectx_common(so
, AF_INET6
, src_sl
, dst_sl
,
671 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
));
676 * Initiate disconnect from peer.
677 * If connection never passed embryonic stage, just drop;
678 * else if don't need to let data drain, then can just drop anyways,
679 * else have to begin TCP shutdown process: mark socket disconnecting,
680 * drain unread data, state switch to reflect user close, and
681 * send segment (e.g. FIN) to peer. Socket will be really disconnected
682 * when peer sends FIN and acks ours.
684 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
687 tcp_usr_disconnect(struct socket
*so
)
690 struct inpcb
*inp
= sotoinpcb(so
);
693 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
694 LCK_MTX_ASSERT_OWNED
);
696 /* In case we got disconnected from the peer */
699 tp
= tcp_disconnect(tp
);
700 COMMON_END(PRU_DISCONNECT
);
704 * User-protocol pru_disconnectx callback.
707 tcp_usr_disconnectx(struct socket
*so
, associd_t aid
, connid_t cid
)
710 if (aid
!= ASSOCID_ANY
&& aid
!= ASSOCID_ALL
)
713 return (tcp_usr_disconnect(so
));
717 * Accept a connection. Essentially all the work is
718 * done at higher levels; just return the address
719 * of the peer, storing through addr.
722 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
725 struct inpcb
*inp
= sotoinpcb(so
);
726 struct tcpcb
*tp
= NULL
;
729 in_getpeeraddr(so
, nam
);
731 if (so
->so_state
& SS_ISDISCONNECTED
) {
732 error
= ECONNABORTED
;
735 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
738 else if (necp_socket_should_use_flow_divert(inp
))
741 error
= cfil_sock_attach(so
);
744 #endif /* CONTENT_FILTER */
750 calculate_tcp_clock();
752 COMMON_END(PRU_ACCEPT
);
757 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
760 struct inpcb
*inp
= sotoinpcb(so
);
761 struct tcpcb
*tp
= NULL
;
764 if (so
->so_state
& SS_ISDISCONNECTED
) {
765 error
= ECONNABORTED
;
768 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
771 else if (necp_socket_should_use_flow_divert(inp
))
774 error
= cfil_sock_attach(so
);
777 #endif /* CONTENT_FILTER */
783 calculate_tcp_clock();
785 in6_mapped_peeraddr(so
, nam
);
786 COMMON_END(PRU_ACCEPT
);
791 * Mark the connection as being incapable of further output.
794 * EINVAL [COMMON_START]
795 * tcp_output:EADDRNOTAVAIL
797 * tcp_output:EMSGSIZE
798 * tcp_output:EHOSTUNREACH
799 * tcp_output:ENETUNREACH
800 * tcp_output:ENETDOWN
803 * tcp_output:EMSGSIZE
805 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
808 tcp_usr_shutdown(struct socket
*so
)
811 struct inpcb
*inp
= sotoinpcb(so
);
815 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
)
821 * In case we got disconnected from the peer, or if this is
822 * a socket that is to be flow-diverted (but not yet).
829 || (necp_socket_should_use_flow_divert(inp
))
837 calculate_tcp_clock();
839 tp
= tcp_usrclosed(tp
);
841 /* A reset has been sent but socket exists, do not send FIN */
842 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
843 (tp
) && (tp
->t_mpflags
& TMPF_RESET
)) {
848 /* Don't send a FIN yet */
849 if (tp
&& !(so
->so_state
& SS_ISDISCONNECTED
) &&
850 cfil_sock_data_pending(&so
->so_snd
))
852 #endif /* CONTENT_FILTER */
854 error
= tcp_output(tp
);
855 COMMON_END(PRU_SHUTDOWN
);
859 * After a receive, possibly send window update to peer.
862 tcp_usr_rcvd(struct socket
*so
, __unused
int flags
)
865 struct inpcb
*inp
= sotoinpcb(so
);
869 /* In case we got disconnected from the peer */
872 tcp_sbrcv_trim(tp
, &so
->so_rcv
);
877 cfil_sock_buf_update(&so
->so_rcv
);
878 #endif /* CONTENT_FILTER */
880 COMMON_END(PRU_RCVD
);
884 * Do a send by putting data in output queue and updating urgent
885 * marker if URG set. Possibly send more data. Unlike the other
886 * pru_*() routines, the mbuf chains are our responsibility. We
887 * must either enqueue them or free them. The other pru_* routines
888 * generally are caller-frees.
894 * tcp_connect:EADDRINUSE Address in use
895 * tcp_connect:EADDRNOTAVAIL Address not available.
896 * tcp_connect:EINVAL Invalid argument
897 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
898 * tcp_connect:EACCES Permission denied
899 * tcp_connect:EAGAIN Resource unavailable, try again
900 * tcp_connect:EPERM Operation not permitted
901 * tcp_output:EADDRNOTAVAIL
903 * tcp_output:EMSGSIZE
904 * tcp_output:EHOSTUNREACH
905 * tcp_output:ENETUNREACH
906 * tcp_output:ENETDOWN
909 * tcp_output:EMSGSIZE
911 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
912 * tcp6_connect:??? [IPV6 only]
915 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
916 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
919 struct inpcb
*inp
= sotoinpcb(so
);
921 uint32_t msgpri
= MSG_PRI_DEFAULT
;
927 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
929 || (necp_socket_should_use_flow_divert(inp
))
933 * OOPS! we lost a race, the TCP session got reset after
934 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
935 * network interrupt in the non-splnet() section of sosend().
939 if (control
!= NULL
) {
945 error
= ECONNRESET
; /* XXX EPIPE? */
953 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
958 calculate_tcp_clock();
960 if (control
!= NULL
) {
961 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
962 /* Get the msg priority from control mbufs */
963 error
= tcp_get_msg_priority(control
, &msgpri
);
974 } else if (control
->m_len
) {
976 * if not unordered, TCP should not have
989 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
990 VERIFY(m
->m_flags
& M_PKTHDR
);
991 m
->m_pkthdr
.msg_pri
= msgpri
;
994 /* MPTCP sublow socket buffers must not be compressed */
995 VERIFY(!(so
->so_flags
& SOF_MP_SUBFLOW
) ||
996 (so
->so_snd
.sb_flags
& SB_NOCOMPRESS
));
998 if(!(flags
& PRUS_OOB
)) {
999 /* Call msg send if message delivery is enabled */
1000 if (so
->so_flags
& SOF_ENABLE_MSGS
)
1001 sbappendmsg_snd(&so
->so_snd
, m
);
1003 sbappendstream(&so
->so_snd
, m
);
1005 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1007 * Do implied connect if not yet connected,
1008 * initialize window to default value, and
1009 * initialize maxseg/maxopd using peer's cached
1014 error
= tcp6_connect(tp
, nam
, p
);
1017 error
= tcp_connect(tp
, nam
, p
);
1020 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1021 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1024 if (flags
& PRUS_EOF
) {
1026 * Close the send side of the connection after
1030 tp
= tcp_usrclosed(tp
);
1033 if (flags
& PRUS_MORETOCOME
)
1034 tp
->t_flags
|= TF_MORETOCOME
;
1035 error
= tcp_output(tp
);
1036 if (flags
& PRUS_MORETOCOME
)
1037 tp
->t_flags
&= ~TF_MORETOCOME
;
1040 if (sbspace(&so
->so_snd
) == 0) {
1041 /* if no space is left in sockbuf,
1042 * do not try to squeeze in OOB traffic */
1048 * According to RFC961 (Assigned Protocols),
1049 * the urgent pointer points to the last octet
1050 * of urgent data. We continue, however,
1051 * to consider it to indicate the first octet
1052 * of data past the urgent section.
1053 * Otherwise, snd_up should be one lower.
1055 sbappendstream(&so
->so_snd
, m
);
1056 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
1058 * Do implied connect if not yet connected,
1059 * initialize window to default value, and
1060 * initialize maxseg/maxopd using peer's cached
1065 error
= tcp6_connect(tp
, nam
, p
);
1068 error
= tcp_connect(tp
, nam
, p
);
1071 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
1072 tcp_mss(tp
, -1, IFSCOPE_NONE
);
1074 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
1075 tp
->t_flagsext
|= TF_FORCE
;
1076 error
= tcp_output(tp
);
1077 tp
->t_flagsext
&= ~TF_FORCE
;
1079 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
1080 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
1087 tcp_usr_abort(struct socket
*so
)
1090 struct inpcb
*inp
= sotoinpcb(so
);
1094 /* In case we got disconnected from the peer */
1097 tp
= tcp_drop(tp
, ECONNABORTED
);
1099 COMMON_END(PRU_ABORT
);
1103 * Receive out-of-band data.
1105 * Returns: 0 Success
1106 * EINVAL [COMMON_START]
1111 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
1114 struct inpcb
*inp
= sotoinpcb(so
);
1118 if ((so
->so_oobmark
== 0 &&
1119 (so
->so_state
& SS_RCVATMARK
) == 0) ||
1120 so
->so_options
& SO_OOBINLINE
||
1121 tp
->t_oobflags
& TCPOOB_HADDATA
) {
1125 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
1126 error
= EWOULDBLOCK
;
1130 *mtod(m
, caddr_t
) = tp
->t_iobc
;
1131 if ((flags
& MSG_PEEK
) == 0)
1132 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
1133 COMMON_END(PRU_RCVOOB
);
1136 /* xxx - should be const */
1137 struct pr_usrreqs tcp_usrreqs
= {
1138 .pru_abort
= tcp_usr_abort
,
1139 .pru_accept
= tcp_usr_accept
,
1140 .pru_attach
= tcp_usr_attach
,
1141 .pru_bind
= tcp_usr_bind
,
1142 .pru_connect
= tcp_usr_connect
,
1143 .pru_connectx
= tcp_usr_connectx
,
1144 .pru_control
= in_control
,
1145 .pru_detach
= tcp_usr_detach
,
1146 .pru_disconnect
= tcp_usr_disconnect
,
1147 .pru_disconnectx
= tcp_usr_disconnectx
,
1148 .pru_listen
= tcp_usr_listen
,
1149 .pru_peeraddr
= in_getpeeraddr
,
1150 .pru_rcvd
= tcp_usr_rcvd
,
1151 .pru_rcvoob
= tcp_usr_rcvoob
,
1152 .pru_send
= tcp_usr_send
,
1153 .pru_shutdown
= tcp_usr_shutdown
,
1154 .pru_sockaddr
= in_getsockaddr
,
1155 .pru_sosend
= sosend
,
1156 .pru_soreceive
= soreceive
,
1160 struct pr_usrreqs tcp6_usrreqs
= {
1161 .pru_abort
= tcp_usr_abort
,
1162 .pru_accept
= tcp6_usr_accept
,
1163 .pru_attach
= tcp_usr_attach
,
1164 .pru_bind
= tcp6_usr_bind
,
1165 .pru_connect
= tcp6_usr_connect
,
1166 .pru_connectx
= tcp6_usr_connectx
,
1167 .pru_control
= in6_control
,
1168 .pru_detach
= tcp_usr_detach
,
1169 .pru_disconnect
= tcp_usr_disconnect
,
1170 .pru_disconnectx
= tcp_usr_disconnectx
,
1171 .pru_listen
= tcp6_usr_listen
,
1172 .pru_peeraddr
= in6_mapped_peeraddr
,
1173 .pru_rcvd
= tcp_usr_rcvd
,
1174 .pru_rcvoob
= tcp_usr_rcvoob
,
1175 .pru_send
= tcp_usr_send
,
1176 .pru_shutdown
= tcp_usr_shutdown
,
1177 .pru_sockaddr
= in6_mapped_sockaddr
,
1178 .pru_sosend
= sosend
,
1179 .pru_soreceive
= soreceive
,
1184 * Common subroutine to open a TCP connection to remote host specified
1185 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1186 * port number if needed. Call in_pcbladdr to do the routing and to choose
1187 * a local host address (interface). If there is an existing incarnation
1188 * of the same connection in TIME-WAIT state and if the remote host was
1189 * sending CC options and if the connection duration was < MSL, then
1190 * truncate the previous TIME-WAIT state and proceed.
1191 * Initialize connection parameters and enter SYN-SENT state.
1193 * Returns: 0 Success
1196 * in_pcbbind:EADDRNOTAVAIL Address not available.
1197 * in_pcbbind:EINVAL Invalid argument
1198 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
1199 * in_pcbbind:EACCES Permission denied
1200 * in_pcbbind:EADDRINUSE Address in use
1201 * in_pcbbind:EAGAIN Resource unavailable, try again
1202 * in_pcbbind:EPERM Operation not permitted
1203 * in_pcbladdr:EINVAL Invalid argument
1204 * in_pcbladdr:EAFNOSUPPORT Address family not supported
1205 * in_pcbladdr:EADDRNOTAVAIL Address not available
1208 tcp_connect(tp
, nam
, p
)
1209 register struct tcpcb
*tp
;
1210 struct sockaddr
*nam
;
1213 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1214 struct socket
*so
= inp
->inp_socket
;
1216 struct sockaddr_in
*sin
= (struct sockaddr_in
*)(void *)nam
;
1217 struct in_addr laddr
;
1218 struct rmxp_tao
*taop
;
1219 struct rmxp_tao tao_noncached
;
1221 struct ifnet
*outif
= NULL
;
1223 if (inp
->inp_lport
== 0) {
1224 error
= in_pcbbind(inp
, NULL
, p
);
1230 * Cannot simply call in_pcbconnect, because there might be an
1231 * earlier incarnation of this same connection still in
1232 * TIME_WAIT state, creating an ADDRINUSE error.
1234 error
= in_pcbladdr(inp
, nam
, &laddr
, IFSCOPE_NONE
, &outif
);
1238 tcp_unlock(inp
->inp_socket
, 0, 0);
1239 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
1240 sin
->sin_addr
, sin
->sin_port
,
1241 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
: laddr
,
1242 inp
->inp_lport
, 0, NULL
);
1244 tcp_lock(inp
->inp_socket
, 0, 0);
1246 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
1247 tcp_lock(oinp
->inp_socket
, 1, 0);
1248 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1250 tcp_unlock(oinp
->inp_socket
, 1, 0);
1254 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1255 otp
->t_state
== TCPS_TIME_WAIT
&&
1256 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1257 (otp
->t_flags
& TF_RCVD_CC
)) {
1258 otp
= tcp_close(otp
);
1260 printf("tcp_connect: inp=0x%llx err=EADDRINUSE\n",
1261 (uint64_t)VM_KERNEL_ADDRPERM(inp
));
1263 tcp_unlock(oinp
->inp_socket
, 1, 0);
1268 tcp_unlock(oinp
->inp_socket
, 1, 0);
1271 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? laddr
.s_addr
:
1272 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
1273 inp
->inp_lport
== sin
->sin_port
) {
1277 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1278 /*lock inversion issue, mostly with udp multicast packets */
1279 socket_unlock(inp
->inp_socket
, 0);
1280 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1281 socket_lock(inp
->inp_socket
, 0);
1283 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1284 inp
->inp_laddr
= laddr
;
1285 /* no reference needed */
1286 inp
->inp_last_outifp
= outif
;
1287 inp
->inp_flags
|= INP_INADDR_ANY
;
1289 inp
->inp_faddr
= sin
->sin_addr
;
1290 inp
->inp_fport
= sin
->sin_port
;
1292 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1294 if (inp
->inp_flowhash
== 0)
1295 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1297 tcp_set_max_rwinscale(tp
, so
);
1300 tcpstat
.tcps_connattempt
++;
1301 tp
->t_state
= TCPS_SYN_SENT
;
1302 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
, TCP_CONN_KEEPINIT(tp
));
1303 tp
->iss
= tcp_new_isn(tp
);
1304 tcp_sendseqinit(tp
);
1306 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1309 * Generate a CC value for this connection and
1310 * check whether CC or CCnew should be used.
1312 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
1313 taop
= &tao_noncached
;
1314 bzero(taop
, sizeof(*taop
));
1317 tp
->cc_send
= CC_INC(tcp_ccgen
);
1318 if (taop
->tao_ccsent
!= 0 &&
1319 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1320 taop
->tao_ccsent
= tp
->cc_send
;
1322 taop
->tao_ccsent
= 0;
1323 tp
->t_flags
|= TF_SENDCCNEW
;
1328 ifnet_release(outif
);
1335 tcp6_connect(tp
, nam
, p
)
1336 register struct tcpcb
*tp
;
1337 struct sockaddr
*nam
;
1340 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1341 struct socket
*so
= inp
->inp_socket
;
1343 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)(void *)nam
;
1344 struct in6_addr addr6
;
1345 struct rmxp_tao
*taop
;
1346 struct rmxp_tao tao_noncached
;
1348 struct ifnet
*outif
= NULL
;
1350 if (inp
->inp_lport
== 0) {
1351 error
= in6_pcbbind(inp
, NULL
, p
);
1357 * Cannot simply call in_pcbconnect, because there might be an
1358 * earlier incarnation of this same connection still in
1359 * TIME_WAIT state, creating an ADDRINUSE error.
1361 * in6_pcbladdr() might return an ifp with its reference held
1362 * even in the error case, so make sure that it's released
1363 * whenever it's non-NULL.
1365 error
= in6_pcbladdr(inp
, nam
, &addr6
, &outif
);
1368 tcp_unlock(inp
->inp_socket
, 0, 0);
1369 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1370 &sin6
->sin6_addr
, sin6
->sin6_port
,
1371 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1374 inp
->inp_lport
, 0, NULL
);
1375 tcp_lock(inp
->inp_socket
, 0, 0);
1377 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1378 otp
->t_state
== TCPS_TIME_WAIT
&&
1379 ((int)(tcp_now
- otp
->t_starttime
)) < tcp_msl
&&
1380 (otp
->t_flags
& TF_RCVD_CC
)) {
1381 otp
= tcp_close(otp
);
1387 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
)) {
1388 /*lock inversion issue, mostly with udp multicast packets */
1389 socket_unlock(inp
->inp_socket
, 0);
1390 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->ipi_lock
);
1391 socket_lock(inp
->inp_socket
, 0);
1393 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
1394 inp
->in6p_laddr
= addr6
;
1395 inp
->in6p_last_outifp
= outif
; /* no reference needed */
1396 inp
->in6p_flags
|= INP_IN6ADDR_ANY
;
1398 inp
->in6p_faddr
= sin6
->sin6_addr
;
1399 inp
->inp_fport
= sin6
->sin6_port
;
1400 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1401 inp
->inp_flow
= sin6
->sin6_flowinfo
;
1403 lck_rw_done(inp
->inp_pcbinfo
->ipi_lock
);
1405 if (inp
->inp_flowhash
== 0)
1406 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1407 /* update flowinfo - RFC 6437 */
1408 if (inp
->inp_flow
== 0 && inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
1409 inp
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
1411 (htonl(inp
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
1414 tcp_set_max_rwinscale(tp
, so
);
1417 tcpstat
.tcps_connattempt
++;
1418 tp
->t_state
= TCPS_SYN_SENT
;
1419 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1420 TCP_CONN_KEEPINIT(tp
));
1421 tp
->iss
= tcp_new_isn(tp
);
1422 tcp_sendseqinit(tp
);
1424 nstat_route_connect_attempt(inp
->inp_route
.ro_rt
);
1427 * Generate a CC value for this connection and
1428 * check whether CC or CCnew should be used.
1430 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
1431 taop
= &tao_noncached
;
1432 bzero(taop
, sizeof(*taop
));
1435 tp
->cc_send
= CC_INC(tcp_ccgen
);
1436 if (taop
->tao_ccsent
!= 0 &&
1437 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1438 taop
->tao_ccsent
= tp
->cc_send
;
1440 taop
->tao_ccsent
= 0;
1441 tp
->t_flags
|= TF_SENDCCNEW
;
1446 ifnet_release(outif
);
1453 * Export TCP internal state information via a struct tcp_info
1455 __private_extern__
void
1456 tcp_fill_info(struct tcpcb
*tp
, struct tcp_info
*ti
)
1458 struct inpcb
*inp
= tp
->t_inpcb
;
1460 bzero(ti
, sizeof(*ti
));
1462 ti
->tcpi_state
= tp
->t_state
;
1464 if (tp
->t_state
> TCPS_LISTEN
) {
1465 if ((tp
->t_flags
& TF_REQ_TSTMP
) && (tp
->t_flags
& TF_RCVD_TSTMP
))
1466 ti
->tcpi_options
|= TCPI_OPT_TIMESTAMPS
;
1467 if (tp
->t_flags
& TF_SACK_PERMIT
)
1468 ti
->tcpi_options
|= TCPI_OPT_SACK
;
1469 if ((tp
->t_flags
& TF_REQ_SCALE
) && (tp
->t_flags
& TF_RCVD_SCALE
)) {
1470 ti
->tcpi_options
|= TCPI_OPT_WSCALE
;
1471 ti
->tcpi_snd_wscale
= tp
->snd_scale
;
1472 ti
->tcpi_rcv_wscale
= tp
->rcv_scale
;
1475 /* Are we in retranmission episode */
1476 if (tp
->snd_max
!= tp
->snd_nxt
)
1477 ti
->tcpi_flags
|= TCPI_FLAG_LOSSRECOVERY
;
1479 ti
->tcpi_flags
&= ~TCPI_FLAG_LOSSRECOVERY
;
1481 ti
->tcpi_rto
= tp
->t_timer
[TCPT_REXMT
] ? tp
->t_rxtcur
: 0;
1482 ti
->tcpi_snd_mss
= tp
->t_maxseg
;
1483 ti
->tcpi_rcv_mss
= tp
->t_maxseg
;
1485 ti
->tcpi_rttcur
= tp
->t_rttcur
;
1486 ti
->tcpi_srtt
= tp
->t_srtt
>> TCP_RTT_SHIFT
;
1487 ti
->tcpi_rttvar
= tp
->t_rttvar
>> TCP_RTTVAR_SHIFT
;
1488 ti
->tcpi_rttbest
= tp
->t_rttbest
>> TCP_RTT_SHIFT
;
1490 ti
->tcpi_snd_ssthresh
= tp
->snd_ssthresh
;
1491 ti
->tcpi_snd_cwnd
= tp
->snd_cwnd
;
1492 ti
->tcpi_snd_sbbytes
= tp
->t_inpcb
->inp_socket
->so_snd
.sb_cc
;
1494 ti
->tcpi_rcv_space
= tp
->rcv_wnd
;
1496 ti
->tcpi_snd_wnd
= tp
->snd_wnd
;
1497 ti
->tcpi_snd_nxt
= tp
->snd_nxt
;
1498 ti
->tcpi_rcv_nxt
= tp
->rcv_nxt
;
1500 /* convert bytes/msec to bits/sec */
1501 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) != 0 &&
1502 tp
->t_bwmeas
!= NULL
) {
1503 ti
->tcpi_snd_bw
= (tp
->t_bwmeas
->bw_sndbw
* 8000);
1506 ti
->tcpi_last_outif
= (tp
->t_inpcb
->inp_last_outifp
== NULL
) ? 0 :
1507 tp
->t_inpcb
->inp_last_outifp
->if_index
;
1509 //atomic_get_64(ti->tcpi_txbytes, &inp->inp_stat->txbytes);
1510 ti
->tcpi_txpackets
= inp
->inp_stat
->txpackets
;
1511 ti
->tcpi_txbytes
= inp
->inp_stat
->txbytes
;
1512 ti
->tcpi_txretransmitbytes
= tp
->t_stat
.txretransmitbytes
;
1513 ti
->tcpi_txunacked
= tp
->snd_max
- tp
->snd_una
;
1515 //atomic_get_64(ti->tcpi_rxbytes, &inp->inp_stat->rxbytes);
1516 ti
->tcpi_rxpackets
= inp
->inp_stat
->rxpackets
;
1517 ti
->tcpi_rxbytes
= inp
->inp_stat
->rxbytes
;
1518 ti
->tcpi_rxduplicatebytes
= tp
->t_stat
.rxduplicatebytes
;
1519 ti
->tcpi_rxoutoforderbytes
= tp
->t_stat
.rxoutoforderbytes
;
1521 if (tp
->t_state
> TCPS_LISTEN
) {
1522 ti
->tcpi_synrexmits
= tp
->t_stat
.synrxtshift
;
1524 ti
->tcpi_cell_rxpackets
= inp
->inp_cstat
->rxpackets
;
1525 ti
->tcpi_cell_rxbytes
= inp
->inp_cstat
->rxbytes
;
1526 ti
->tcpi_cell_txpackets
= inp
->inp_cstat
->txpackets
;
1527 ti
->tcpi_cell_txbytes
= inp
->inp_cstat
->txbytes
;
1529 ti
->tcpi_wifi_rxpackets
= inp
->inp_wstat
->rxpackets
;
1530 ti
->tcpi_wifi_rxbytes
= inp
->inp_wstat
->rxbytes
;
1531 ti
->tcpi_wifi_txpackets
= inp
->inp_wstat
->txpackets
;
1532 ti
->tcpi_wifi_txbytes
= inp
->inp_wstat
->txbytes
;
1534 ti
->tcpi_wired_rxpackets
= inp
->inp_Wstat
->rxpackets
;
1535 ti
->tcpi_wired_rxbytes
= inp
->inp_Wstat
->rxbytes
;
1536 ti
->tcpi_wired_txpackets
= inp
->inp_Wstat
->txpackets
;
1537 ti
->tcpi_wired_txbytes
= inp
->inp_Wstat
->txbytes
;
1541 __private_extern__ errno_t
1542 tcp_fill_info_for_info_tuple(struct info_tuple
*itpl
, struct tcp_info
*ti
)
1544 struct inpcbinfo
*pcbinfo
= NULL
;
1545 struct inpcb
*inp
= NULL
;
1549 if (itpl
->itpl_proto
== IPPROTO_TCP
)
1554 if (itpl
->itpl_local_sa
.sa_family
== AF_INET
&&
1555 itpl
->itpl_remote_sa
.sa_family
== AF_INET
) {
1556 inp
= in_pcblookup_hash(pcbinfo
,
1557 itpl
->itpl_remote_sin
.sin_addr
,
1558 itpl
->itpl_remote_sin
.sin_port
,
1559 itpl
->itpl_local_sin
.sin_addr
,
1560 itpl
->itpl_local_sin
.sin_port
,
1562 } else if (itpl
->itpl_local_sa
.sa_family
== AF_INET6
&&
1563 itpl
->itpl_remote_sa
.sa_family
== AF_INET6
) {
1564 struct in6_addr ina6_local
;
1565 struct in6_addr ina6_remote
;
1567 ina6_local
= itpl
->itpl_local_sin6
.sin6_addr
;
1568 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_local
) &&
1569 itpl
->itpl_local_sin6
.sin6_scope_id
)
1570 ina6_local
.s6_addr16
[1] = htons(itpl
->itpl_local_sin6
.sin6_scope_id
);
1572 ina6_remote
= itpl
->itpl_remote_sin6
.sin6_addr
;
1573 if (IN6_IS_SCOPE_LINKLOCAL(&ina6_remote
) &&
1574 itpl
->itpl_remote_sin6
.sin6_scope_id
)
1575 ina6_remote
.s6_addr16
[1] = htons(itpl
->itpl_remote_sin6
.sin6_scope_id
);
1577 inp
= in6_pcblookup_hash(pcbinfo
,
1579 itpl
->itpl_remote_sin6
.sin6_port
,
1581 itpl
->itpl_local_sin6
.sin6_port
,
1586 if (inp
== NULL
|| (so
= inp
->inp_socket
) == NULL
)
1590 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
1591 socket_unlock(so
, 0);
1594 tp
= intotcpcb(inp
);
1596 tcp_fill_info(tp
, ti
);
1597 socket_unlock(so
, 0);
1603 __private_extern__
int
1604 tcp_sysctl_info(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
, __unused
int arg2
, struct sysctl_req
*req
)
1608 struct info_tuple itpl
;
1609 proc_t caller
= PROC_NULL
;
1610 proc_t caller_parent
= PROC_NULL
;
1611 char command_name
[MAXCOMLEN
+ 1] = "";
1612 char parent_name
[MAXCOMLEN
+ 1] = "";
1614 if ((caller
= proc_self()) != PROC_NULL
) {
1615 /* get process name */
1616 strlcpy(command_name
, caller
->p_comm
, sizeof(command_name
));
1618 /* get parent process name if possible */
1619 if ((caller_parent
= proc_find(caller
->p_ppid
)) != PROC_NULL
) {
1620 strlcpy(parent_name
, caller_parent
->p_comm
,
1621 sizeof(parent_name
));
1622 proc_rele(caller_parent
);
1625 if ((escape_str(command_name
, strlen(command_name
),
1626 sizeof(command_name
)) == 0) &&
1627 (escape_str(parent_name
, strlen(parent_name
),
1628 sizeof(parent_name
)) == 0)) {
1629 kern_asl_msg(LOG_DEBUG
, "messagetracer",
1631 "com.apple.message.domain",
1632 "com.apple.kernel.tcpstat", /* 1 */
1633 "com.apple.message.signature",
1635 "com.apple.message.signature2", command_name
, /* 3 */
1636 "com.apple.message.signature3", parent_name
, /* 4 */
1637 "com.apple.message.summarize", "YES", /* 5 */
1642 if (caller
!= PROC_NULL
)
1645 if (req
->newptr
== USER_ADDR_NULL
) {
1648 if (req
->newlen
< sizeof(struct info_tuple
)) {
1651 error
= SYSCTL_IN(req
, &itpl
, sizeof(struct info_tuple
));
1655 error
= tcp_fill_info_for_info_tuple(&itpl
, &ti
);
1659 error
= SYSCTL_OUT(req
, &ti
, sizeof(struct tcp_info
));
1668 tcp_lookup_peer_pid_locked(struct socket
*so
, pid_t
*out_pid
)
1670 int error
= EHOSTUNREACH
;
1672 if ((so
->so_state
& SS_ISCONNECTED
) == 0) return ENOTCONN
;
1674 struct inpcb
*inp
= (struct inpcb
*)so
->so_pcb
;
1675 uint16_t lport
= inp
->inp_lport
;
1676 uint16_t fport
= inp
->inp_fport
;
1677 struct inpcb
*finp
= NULL
;
1679 if (inp
->inp_vflag
& INP_IPV6
) {
1680 struct in6_addr laddr6
= inp
->in6p_laddr
;
1681 struct in6_addr faddr6
= inp
->in6p_faddr
;
1682 socket_unlock(so
, 0);
1683 finp
= in6_pcblookup_hash(&tcbinfo
, &laddr6
, lport
, &faddr6
, fport
, 0, NULL
);
1685 } else if (inp
->inp_vflag
& INP_IPV4
) {
1686 struct in_addr laddr4
= inp
->inp_laddr
;
1687 struct in_addr faddr4
= inp
->inp_faddr
;
1688 socket_unlock(so
, 0);
1689 finp
= in_pcblookup_hash(&tcbinfo
, laddr4
, lport
, faddr4
, fport
, 0, NULL
);
1694 *out_pid
= finp
->inp_socket
->last_pid
;
1696 in_pcb_checkstate(finp
, WNT_RELEASE
, 0);
1703 tcp_getconninfo(struct socket
*so
, struct conninfo_tcp
*tcp_ci
)
1705 (void) tcp_lookup_peer_pid_locked(so
, &tcp_ci
->tcpci_peer_pid
);
1706 tcp_fill_info(sototcpcb(so
), &tcp_ci
->tcpci_tcp_info
);
1710 * The new sockopt interface makes it possible for us to block in the
1711 * copyin/out step (if we take a page fault). Taking a page fault at
1712 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1713 * use TSM, there probably isn't any need for this function to run at
1714 * splnet() any more. This needs more examination.)
1717 tcp_ctloutput(so
, sopt
)
1719 struct sockopt
*sopt
;
1721 int error
, opt
, optval
;
1726 inp
= sotoinpcb(so
);
1728 return (ECONNRESET
);
1730 /* Allow <SOL_SOCKET,SO_FLUSH/SO_TRAFFIC_MGT_BACKGROUND> at this level */
1731 if (sopt
->sopt_level
!= IPPROTO_TCP
&&
1732 !(sopt
->sopt_level
== SOL_SOCKET
&& (sopt
->sopt_name
== SO_FLUSH
||
1733 sopt
->sopt_name
== SO_TRAFFIC_MGT_BACKGROUND
))) {
1735 if (SOCK_CHECK_DOM(so
, PF_INET6
))
1736 error
= ip6_ctloutput(so
, sopt
);
1739 error
= ip_ctloutput(so
, sopt
);
1742 tp
= intotcpcb(inp
);
1744 return (ECONNRESET
);
1747 calculate_tcp_clock();
1749 switch (sopt
->sopt_dir
) {
1751 switch (sopt
->sopt_name
) {
1755 case TCP_ENABLE_ECN
:
1756 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1761 switch (sopt
->sopt_name
) {
1771 case TCP_ENABLE_ECN
:
1772 opt
= TF_ENABLE_ECN
;
1775 opt
= 0; /* dead code to fool gcc */
1782 tp
->t_flags
&= ~opt
;
1784 case TCP_RXT_FINDROP
:
1785 case TCP_NOTIMEWAIT
:
1786 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1790 switch (sopt
->sopt_name
) {
1791 case TCP_RXT_FINDROP
:
1792 opt
= TF_RXTFINDROP
;
1794 case TCP_NOTIMEWAIT
:
1795 opt
= TF_NOTIMEWAIT
;
1802 tp
->t_flagsext
|= opt
;
1804 tp
->t_flagsext
&= ~opt
;
1806 case TCP_MEASURE_SND_BW
:
1807 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1811 opt
= TF_MEASURESNDBW
;
1813 if (tp
->t_bwmeas
== NULL
) {
1814 tp
->t_bwmeas
= tcp_bwmeas_alloc(tp
);
1815 if (tp
->t_bwmeas
== NULL
) {
1820 tp
->t_flagsext
|= opt
;
1822 tp
->t_flagsext
&= ~opt
;
1823 /* Reset snd bw measurement state */
1824 tp
->t_flagsext
&= ~(TF_BWMEAS_INPROGRESS
);
1825 if (tp
->t_bwmeas
!= NULL
) {
1826 tcp_bwmeas_free(tp
);
1830 case TCP_MEASURE_BW_BURST
: {
1831 struct tcp_measure_bw_burst in
;
1832 uint32_t minpkts
, maxpkts
;
1833 bzero(&in
, sizeof(in
));
1835 error
= sooptcopyin(sopt
, &in
, sizeof(in
),
1839 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
1840 tp
->t_bwmeas
== NULL
) {
1844 minpkts
= (in
.min_burst_size
!= 0) ? in
.min_burst_size
:
1845 tp
->t_bwmeas
->bw_minsizepkts
;
1846 maxpkts
= (in
.max_burst_size
!= 0) ? in
.max_burst_size
:
1847 tp
->t_bwmeas
->bw_maxsizepkts
;
1848 if (minpkts
> maxpkts
) {
1852 tp
->t_bwmeas
->bw_minsizepkts
= minpkts
;
1853 tp
->t_bwmeas
->bw_maxsizepkts
= maxpkts
;
1854 tp
->t_bwmeas
->bw_minsize
= (minpkts
* tp
->t_maxseg
);
1855 tp
->t_bwmeas
->bw_maxsize
= (maxpkts
* tp
->t_maxseg
);
1859 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1864 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
1865 optval
+ 40 >= tcp_minmss
)
1866 tp
->t_maxseg
= optval
;
1872 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1876 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
1879 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
1880 /* reset the timer to new value */
1881 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1882 TCP_CONN_KEEPIDLE(tp
));
1883 tcp_check_timer_state(tp
);
1887 case TCP_CONNECTIONTIMEOUT
:
1888 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1892 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
1895 tp
->t_keepinit
= optval
* TCP_RETRANSHZ
;
1896 if (tp
->t_state
== TCPS_SYN_RECEIVED
||
1897 tp
->t_state
== TCPS_SYN_SENT
) {
1898 tp
->t_timer
[TCPT_KEEP
] = OFFSET_FROM_START(tp
,
1899 TCP_CONN_KEEPINIT(tp
));
1900 tcp_check_timer_state(tp
);
1906 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1910 if (optval
< 0 || optval
> UINT32_MAX
/TCP_RETRANSHZ
) {
1913 tp
->t_keepintvl
= optval
* TCP_RETRANSHZ
;
1914 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
1915 TCP_CONN_MAXIDLE(tp
) > 0) {
1916 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
1917 TCP_CONN_MAXIDLE(tp
));
1918 tcp_check_timer_state(tp
);
1924 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1928 if (optval
< 0 || optval
> INT32_MAX
) {
1931 tp
->t_keepcnt
= optval
;
1932 if (tp
->t_state
== TCPS_FIN_WAIT_2
&&
1933 TCP_CONN_MAXIDLE(tp
) > 0) {
1934 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
1935 TCP_CONN_MAXIDLE(tp
));
1936 tcp_check_timer_state(tp
);
1941 case PERSIST_TIMEOUT
:
1942 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1949 tp
->t_persist_timeout
= optval
* TCP_RETRANSHZ
;
1951 case TCP_RXT_CONNDROPTIME
:
1952 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1959 tp
->t_rxt_conndroptime
= optval
* TCP_RETRANSHZ
;
1961 case TCP_NOTSENT_LOWAT
:
1962 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
1971 so
->so_flags
&= ~(SOF_NOTSENT_LOWAT
);
1972 tp
->t_notsent_lowat
= 0;
1974 so
->so_flags
|= SOF_NOTSENT_LOWAT
;
1975 tp
->t_notsent_lowat
= optval
;
1979 case TCP_ADAPTIVE_READ_TIMEOUT
:
1980 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
1985 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
1988 } else if (optval
== 0) {
1989 tp
->t_adaptive_rtimo
= 0;
1990 tcp_keepalive_reset(tp
);
1992 tp
->t_adaptive_rtimo
= optval
;
1995 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
1996 error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2001 optval
> TCP_ADAPTIVE_TIMEOUT_MAX
) {
2005 tp
->t_adaptive_wtimo
= optval
;
2008 case TCP_ENABLE_MSGS
:
2009 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2013 if (optval
< 0 || optval
> 1) {
2015 } else if (optval
== 1) {
2017 * Check if messages option is already
2018 * enabled, if so return.
2020 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2021 VERIFY(so
->so_msg_state
!= NULL
);
2026 * allocate memory for storing message
2029 VERIFY(so
->so_msg_state
== NULL
);
2030 MALLOC(so
->so_msg_state
,
2032 sizeof(struct msg_state
),
2033 M_TEMP
, M_WAITOK
| M_ZERO
);
2034 if (so
->so_msg_state
== NULL
) {
2039 /* Enable message delivery */
2040 so
->so_flags
|= SOF_ENABLE_MSGS
;
2043 * Can't disable message delivery on socket
2044 * because of restrictions imposed by
2050 case TCP_SENDMOREACKS
:
2051 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2055 if (optval
< 0 || optval
> 1) {
2057 } else if (optval
== 0) {
2058 tp
->t_flagsext
&= ~(TF_NOSTRETCHACK
);
2060 tp
->t_flagsext
|= TF_NOSTRETCHACK
;
2063 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2064 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2068 if (optval
< 0 || optval
> 1) {
2070 } else if (optval
== 0) {
2071 tp
->t_flagsext
&= ~TF_NOBLACKHOLE_DETECTION
;
2073 tp
->t_flagsext
|= TF_NOBLACKHOLE_DETECTION
;
2074 if ((tp
->t_flags
& TF_BLACKHOLE
) &&
2075 tp
->t_pmtud_saved_maxopd
> 0)
2076 tcp_pmtud_revert_segment_size(tp
);
2080 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2081 sizeof (optval
))) != 0)
2084 error
= inp_flush(inp
, optval
);
2087 case SO_TRAFFIC_MGT_BACKGROUND
:
2088 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
2089 sizeof (optval
))) != 0)
2093 socket_set_traffic_mgt_flags_locked(so
,
2094 TRAFFIC_MGT_SO_BACKGROUND
);
2096 socket_clear_traffic_mgt_flags_locked(so
,
2097 TRAFFIC_MGT_SO_BACKGROUND
);
2102 error
= ENOPROTOOPT
;
2108 switch (sopt
->sopt_name
) {
2110 optval
= tp
->t_flags
& TF_NODELAY
;
2113 optval
= tp
->t_maxseg
;
2116 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
2119 optval
= tp
->t_keepintvl
/ TCP_RETRANSHZ
;
2122 optval
= tp
->t_keepcnt
;
2125 optval
= tp
->t_flags
& TF_NOOPT
;
2128 optval
= tp
->t_flags
& TF_NOPUSH
;
2130 case TCP_ENABLE_ECN
:
2131 optval
= (tp
->t_flags
& TF_ENABLE_ECN
) ? 1 : 0;
2133 case TCP_CONNECTIONTIMEOUT
:
2134 optval
= tp
->t_keepinit
/ TCP_RETRANSHZ
;
2136 case PERSIST_TIMEOUT
:
2137 optval
= tp
->t_persist_timeout
/ TCP_RETRANSHZ
;
2139 case TCP_RXT_CONNDROPTIME
:
2140 optval
= tp
->t_rxt_conndroptime
/ TCP_RETRANSHZ
;
2142 case TCP_RXT_FINDROP
:
2143 optval
= tp
->t_flagsext
& TF_RXTFINDROP
;
2145 case TCP_NOTIMEWAIT
:
2146 optval
= (tp
->t_flagsext
& TF_NOTIMEWAIT
) ? 1 : 0;
2148 case TCP_MEASURE_SND_BW
:
2149 optval
= tp
->t_flagsext
& TF_MEASURESNDBW
;
2154 tcp_fill_info(tp
, &ti
);
2155 error
= sooptcopyout(sopt
, &ti
, sizeof(struct tcp_info
));
2159 case TCP_MEASURE_BW_BURST
: {
2160 struct tcp_measure_bw_burst out
;
2161 if ((tp
->t_flagsext
& TF_MEASURESNDBW
) == 0 ||
2162 tp
->t_bwmeas
== NULL
) {
2166 out
.min_burst_size
= tp
->t_bwmeas
->bw_minsizepkts
;
2167 out
.max_burst_size
= tp
->t_bwmeas
->bw_maxsizepkts
;
2168 error
= sooptcopyout(sopt
, &out
, sizeof(out
));
2171 case TCP_NOTSENT_LOWAT
:
2172 if ((so
->so_flags
& SOF_NOTSENT_LOWAT
) != 0) {
2173 optval
= tp
->t_notsent_lowat
;
2179 case TCP_ENABLE_MSGS
:
2180 if (so
->so_flags
& SOF_ENABLE_MSGS
) {
2186 case TCP_SENDMOREACKS
:
2187 if (tp
->t_flagsext
& TF_NOSTRETCHACK
)
2192 case TCP_DISABLE_BLACKHOLE_DETECTION
:
2193 if (tp
->t_flagsext
& TF_NOBLACKHOLE_DETECTION
)
2198 case TCP_PEER_PID
: {
2200 error
= tcp_lookup_peer_pid_locked(so
, &pid
);
2202 error
= sooptcopyout(sopt
, &pid
, sizeof(pid
));
2205 case TCP_ADAPTIVE_READ_TIMEOUT
:
2206 optval
= tp
->t_adaptive_rtimo
;
2208 case TCP_ADAPTIVE_WRITE_TIMEOUT
:
2209 optval
= tp
->t_adaptive_wtimo
;
2211 case SO_TRAFFIC_MGT_BACKGROUND
:
2212 optval
= (so
->so_traffic_mgt_flags
&
2213 TRAFFIC_MGT_SO_BACKGROUND
) ? 1 : 0;
2216 error
= ENOPROTOOPT
;
2220 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2228 * tcp_sendspace and tcp_recvspace are the default send and receive window
2229 * sizes, respectively. These are obsolescent (this information should
2230 * be set by the route).
2232 u_int32_t tcp_sendspace
= 1448*256;
2233 u_int32_t tcp_recvspace
= 1448*384;
2235 /* During attach, the size of socket buffer allocated is limited to
2236 * sb_max in sbreserve. Disallow setting the tcp send and recv space
2237 * to be more than sb_max because that will cause tcp_attach to fail
2238 * (see radar 5713060)
2241 sysctl_tcp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
2242 __unused
int arg2
, struct sysctl_req
*req
) {
2243 u_int32_t new_value
= 0, *space_p
= NULL
;
2244 int changed
= 0, error
= 0;
2245 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
2247 switch (oidp
->oid_number
) {
2248 case TCPCTL_SENDSPACE
:
2249 space_p
= &tcp_sendspace
;
2251 case TCPCTL_RECVSPACE
:
2252 space_p
= &tcp_recvspace
;
2257 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
2258 &new_value
, &changed
);
2260 if (new_value
> 0 && new_value
<= sb_effective_max
) {
2261 *space_p
= new_value
;
2269 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2270 &tcp_sendspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum outgoing TCP datagram size");
2271 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
2272 &tcp_recvspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum incoming TCP datagram size");
2276 * Attach TCP protocol to socket, allocating
2277 * internet protocol control block, tcp control block,
2278 * bufer space, and entering LISTEN state if to accept connections.
2280 * Returns: 0 Success
2281 * in_pcballoc:ENOBUFS
2282 * in_pcballoc:ENOMEM
2283 * in_pcballoc:??? [IPSEC specific]
2291 register struct tcpcb
*tp
;
2295 int isipv6
= SOCK_CHECK_DOM(so
, PF_INET6
) != 0;
2298 error
= in_pcballoc(so
, &tcbinfo
, p
);
2302 inp
= sotoinpcb(so
);
2304 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
2305 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
2309 if ((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0)
2310 so
->so_rcv
.sb_flags
|= SB_AUTOSIZE
;
2311 if ((so
->so_snd
.sb_flags
& SB_USRSIZE
) == 0)
2312 so
->so_snd
.sb_flags
|= SB_AUTOSIZE
;
2316 inp
->inp_vflag
|= INP_IPV6
;
2317 inp
->in6p_hops
= -1; /* use kernel default */
2321 inp
->inp_vflag
|= INP_IPV4
;
2322 tp
= tcp_newtcpcb(inp
);
2324 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
2326 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
2333 so
->so_state
|= nofd
;
2337 nstat_tcp_new_pcb(inp
);
2338 tp
->t_state
= TCPS_CLOSED
;
2343 * Initiate (or continue) disconnect.
2344 * If embryonic state, just send reset (once).
2345 * If in ``let data drain'' option and linger null, just drop.
2346 * Otherwise (hard), mark socket disconnecting and drop
2347 * current input data; switch states based on user close, and
2348 * send segment to peer (with FIN).
2350 static struct tcpcb
*
2352 register struct tcpcb
*tp
;
2354 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
2356 if (tp
->t_state
< TCPS_ESTABLISHED
)
2358 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
2359 tp
= tcp_drop(tp
, 0);
2361 soisdisconnecting(so
);
2362 sbflush(&so
->so_rcv
);
2363 tp
= tcp_usrclosed(tp
);
2365 /* A reset has been sent but socket exists, do not send FIN */
2366 if ((so
->so_flags
& SOF_MP_SUBFLOW
) &&
2367 (tp
) && (tp
->t_mpflags
& TMPF_RESET
))
2371 (void) tcp_output(tp
);
2377 * User issued close, and wish to trail through shutdown states:
2378 * if never received SYN, just forget it. If got a SYN from peer,
2379 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2380 * If already got a FIN from peer, then almost done; go to LAST_ACK
2381 * state. In all other cases, have already sent FIN to peer (e.g.
2382 * after PRU_SHUTDOWN), and just have to play tedious game waiting
2383 * for peer to send FIN or not respond to keep-alives, etc.
2384 * We can let the user exit from the close as soon as the FIN is acked.
2386 static struct tcpcb
*
2388 register struct tcpcb
*tp
;
2391 switch (tp
->t_state
) {
2399 case TCPS_SYN_RECEIVED
:
2400 tp
->t_flags
|= TF_NEEDFIN
;
2403 case TCPS_ESTABLISHED
:
2404 DTRACE_TCP4(state__change
, void, NULL
,
2405 struct inpcb
*, tp
->t_inpcb
,
2407 int32_t, TCPS_FIN_WAIT_1
);
2408 tp
->t_state
= TCPS_FIN_WAIT_1
;
2411 case TCPS_CLOSE_WAIT
:
2412 DTRACE_TCP4(state__change
, void, NULL
,
2413 struct inpcb
*, tp
->t_inpcb
,
2415 int32_t, TCPS_LAST_ACK
);
2416 tp
->t_state
= TCPS_LAST_ACK
;
2419 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
2420 soisdisconnected(tp
->t_inpcb
->inp_socket
);
2421 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
2422 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
2423 tp
->t_timer
[TCPT_2MSL
] = OFFSET_FROM_START(tp
,
2424 TCP_CONN_MAXIDLE(tp
));
2430 tcp_in_cksum_stats(u_int32_t len
)
2432 tcpstat
.tcps_rcv_swcsum
++;
2433 tcpstat
.tcps_rcv_swcsum_bytes
+= len
;
2437 tcp_out_cksum_stats(u_int32_t len
)
2439 tcpstat
.tcps_snd_swcsum
++;
2440 tcpstat
.tcps_snd_swcsum_bytes
+= len
;
2445 tcp_in6_cksum_stats(u_int32_t len
)
2447 tcpstat
.tcps_rcv6_swcsum
++;
2448 tcpstat
.tcps_rcv6_swcsum_bytes
+= len
;
2452 tcp_out6_cksum_stats(u_int32_t len
)
2454 tcpstat
.tcps_snd6_swcsum
++;
2455 tcpstat
.tcps_snd6_swcsum_bytes
+= len
;
2459 * When messages are enabled on a TCP socket, the message priority
2460 * is sent as a control message. This function will extract it.
2463 tcp_get_msg_priority(struct mbuf
*control
, uint32_t *msgpri
)
2466 if (control
== NULL
)
2469 for (cm
= M_FIRST_CMSGHDR(control
); cm
;
2470 cm
= M_NXT_CMSGHDR(control
, cm
)) {
2471 if (cm
->cmsg_len
< sizeof(struct cmsghdr
) ||
2472 cm
->cmsg_len
> control
->m_len
) {
2475 if (cm
->cmsg_level
== SOL_SOCKET
&&
2476 cm
->cmsg_type
== SCM_MSG_PRIORITY
) {
2477 *msgpri
= *(unsigned int *)(void *)CMSG_DATA(cm
);
2482 VERIFY(*msgpri
>= MSG_PRI_MIN
&& *msgpri
<= MSG_PRI_MAX
);