2 * Copyright (c) 2000-2008 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>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/protosw.h>
78 #include <net/route.h>
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
83 #include <netinet/ip6.h>
85 #include <netinet/in_pcb.h>
87 #include <netinet6/in6_pcb.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip_var.h>
92 #include <netinet6/ip6_var.h>
94 #include <netinet/tcp.h>
95 #include <netinet/tcp_fsm.h>
96 #include <netinet/tcp_seq.h>
97 #include <netinet/tcp_timer.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcpip.h>
101 #include <netinet/tcp_debug.h>
105 #include <netinet6/ipsec.h>
109 * TCP protocol interface to socket abstraction.
111 extern char *tcpstates
[]; /* XXX ??? */
113 static int tcp_attach(struct socket
*, struct proc
*);
114 static int tcp_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
116 static int tcp6_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
118 static struct tcpcb
*
119 tcp_disconnect(struct tcpcb
*);
120 static struct tcpcb
*
121 tcp_usrclosed(struct tcpcb
*);
123 __private_extern__
int tcp_win_scale
= 3;
124 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, win_scale_factor
, CTLFLAG_RW
,
125 &tcp_win_scale
, 0, "Window scaling factor");
127 static u_int32_t tcps_in_sw_cksum
;
128 SYSCTL_UINT(_net_inet_tcp
, OID_AUTO
, in_sw_cksum
, CTLFLAG_RD
,
129 &tcps_in_sw_cksum
, 0,
130 "Number of received packets checksummed in software");
132 static u_int64_t tcps_in_sw_cksum_bytes
;
133 SYSCTL_QUAD(_net_inet_tcp
, OID_AUTO
, in_sw_cksum_bytes
, CTLFLAG_RD
,
134 &tcps_in_sw_cksum_bytes
,
135 "Amount of received data checksummed in software");
137 static u_int32_t tcps_out_sw_cksum
;
138 SYSCTL_UINT(_net_inet_tcp
, OID_AUTO
, out_sw_cksum
, CTLFLAG_RD
,
139 &tcps_out_sw_cksum
, 0,
140 "Number of transmitted packets checksummed in software");
142 static u_int64_t tcps_out_sw_cksum_bytes
;
143 SYSCTL_QUAD(_net_inet_tcp
, OID_AUTO
, out_sw_cksum_bytes
, CTLFLAG_RD
,
144 &tcps_out_sw_cksum_bytes
,
145 "Amount of transmitted data checksummed in software");
148 #define TCPDEBUG0 int ostate = 0
149 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
150 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
151 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
155 #define TCPDEBUG2(req)
158 #if CONFIG_USESOCKTHRESHOLD
159 __private_extern__
unsigned int tcp_sockthreshold
= 64;
161 __private_extern__
unsigned int tcp_sockthreshold
= 0;
163 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sockthreshold
, CTLFLAG_RW
,
164 &tcp_sockthreshold
, 0, "TCP Socket size increased if less than threshold");
167 * TCP attaches to socket via pru_attach(), reserving space,
168 * and an internet control block.
174 * tcp_attach:??? [IPSEC specific]
177 tcp_usr_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
180 struct inpcb
*inp
= sotoinpcb(so
);
181 struct tcpcb
*tp
= 0;
190 error
= tcp_attach(so
, p
);
194 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
195 so
->so_linger
= TCP_LINGERTIME
* hz
;
198 TCPDEBUG2(PRU_ATTACH
);
203 * pru_detach() detaches the TCP protocol from the socket.
204 * If the protocol state is non-embryonic, then can't
205 * do this directly: have to initiate a pru_disconnect(),
206 * which may finish later; embryonic TCB's can just
210 tcp_usr_detach(struct socket
*so
)
213 struct inpcb
*inp
= sotoinpcb(so
);
217 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
218 return EINVAL
; /* XXX */
221 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
224 /* In case we got disconnected from the peer */
228 tp
= tcp_disconnect(tp
);
230 TCPDEBUG2(PRU_DETACH
);
234 #define COMMON_START() TCPDEBUG0; \
236 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { \
239 tp = intotcpcb(inp); \
243 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
247 * Give the socket an address.
250 * EINVAL Invalid argument [COMMON_START]
251 * EAFNOSUPPORT Address family not supported
252 * in_pcbbind:EADDRNOTAVAIL Address not available.
253 * in_pcbbind:EINVAL Invalid argument
254 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
255 * in_pcbbind:EACCES Permission denied
256 * in_pcbbind:EADDRINUSE Address in use
257 * in_pcbbind:EAGAIN Resource unavailable, try again
258 * in_pcbbind:EPERM Operation not permitted
261 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
264 struct inpcb
*inp
= sotoinpcb(so
);
266 struct sockaddr_in
*sinp
;
270 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
271 error
= EAFNOSUPPORT
;
276 * Must check for multicast addresses and disallow binding
279 sinp
= (struct sockaddr_in
*)nam
;
280 if (sinp
->sin_family
== AF_INET
&&
281 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
282 error
= EAFNOSUPPORT
;
285 error
= in_pcbbind(inp
, nam
, p
);
288 COMMON_END(PRU_BIND
);
294 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
297 struct inpcb
*inp
= sotoinpcb(so
);
299 struct sockaddr_in6
*sin6p
;
303 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
304 error
= EAFNOSUPPORT
;
309 * Must check for multicast addresses and disallow binding
312 sin6p
= (struct sockaddr_in6
*)nam
;
313 if (sin6p
->sin6_family
== AF_INET6
&&
314 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
315 error
= EAFNOSUPPORT
;
318 inp
->inp_vflag
&= ~INP_IPV4
;
319 inp
->inp_vflag
|= INP_IPV6
;
320 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
321 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
322 inp
->inp_vflag
|= INP_IPV4
;
323 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
324 struct sockaddr_in sin
;
326 in6_sin6_2_sin(&sin
, sin6p
);
327 inp
->inp_vflag
|= INP_IPV4
;
328 inp
->inp_vflag
&= ~INP_IPV6
;
329 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
333 error
= in6_pcbbind(inp
, nam
, p
);
336 COMMON_END(PRU_BIND
);
341 * Prepare to accept connections.
344 * EINVAL [COMMON_START]
345 * in_pcbbind:EADDRNOTAVAIL Address not available.
346 * in_pcbbind:EINVAL Invalid argument
347 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
348 * in_pcbbind:EACCES Permission denied
349 * in_pcbbind:EADDRINUSE Address in use
350 * in_pcbbind:EAGAIN Resource unavailable, try again
351 * in_pcbbind:EPERM Operation not permitted
354 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
357 struct inpcb
*inp
= sotoinpcb(so
);
361 if (inp
->inp_lport
== 0)
362 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
364 tp
->t_state
= TCPS_LISTEN
;
365 COMMON_END(PRU_LISTEN
);
370 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
373 struct inpcb
*inp
= sotoinpcb(so
);
377 if (inp
->inp_lport
== 0) {
378 inp
->inp_vflag
&= ~INP_IPV4
;
379 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
380 inp
->inp_vflag
|= INP_IPV4
;
381 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
384 tp
->t_state
= TCPS_LISTEN
;
385 COMMON_END(PRU_LISTEN
);
390 * Initiate connection to peer.
391 * Create a template for use in transmissions on this connection.
392 * Enter SYN_SENT state, and mark socket as connecting.
393 * Start keep-alive timer, and seed output sequence space.
394 * Send initial segment on connection.
397 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
400 struct inpcb
*inp
= sotoinpcb(so
);
402 struct sockaddr_in
*sinp
;
407 else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
409 error
= so
->so_error
;
418 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
419 error
= EAFNOSUPPORT
;
423 * Must disallow TCP ``connections'' to multicast addresses.
425 sinp
= (struct sockaddr_in
*)nam
;
426 if (sinp
->sin_family
== AF_INET
427 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
428 error
= EAFNOSUPPORT
;
433 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
435 error
= tcp_output(tp
);
436 COMMON_END(PRU_CONNECT
);
441 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
444 struct inpcb
*inp
= sotoinpcb(so
);
446 struct sockaddr_in6
*sin6p
;
450 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
451 error
= EAFNOSUPPORT
;
456 * Must disallow TCP ``connections'' to multicast addresses.
458 sin6p
= (struct sockaddr_in6
*)nam
;
459 if (sin6p
->sin6_family
== AF_INET6
460 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
461 error
= EAFNOSUPPORT
;
465 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
466 struct sockaddr_in sin
;
468 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
471 in6_sin6_2_sin(&sin
, sin6p
);
472 inp
->inp_vflag
|= INP_IPV4
;
473 inp
->inp_vflag
&= ~INP_IPV6
;
474 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
476 error
= tcp_output(tp
);
479 inp
->inp_vflag
&= ~INP_IPV4
;
480 inp
->inp_vflag
|= INP_IPV6
;
481 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
483 error
= tcp_output(tp
);
486 COMMON_END(PRU_CONNECT
);
491 * Initiate disconnect from peer.
492 * If connection never passed embryonic stage, just drop;
493 * else if don't need to let data drain, then can just drop anyways,
494 * else have to begin TCP shutdown process: mark socket disconnecting,
495 * drain unread data, state switch to reflect user close, and
496 * send segment (e.g. FIN) to peer. Socket will be really disconnected
497 * when peer sends FIN and acks ours.
499 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
502 tcp_usr_disconnect(struct socket
*so
)
505 struct inpcb
*inp
= sotoinpcb(so
);
509 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
512 /* In case we got disconnected from the peer */
515 tp
= tcp_disconnect(tp
);
516 COMMON_END(PRU_DISCONNECT
);
520 * Accept a connection. Essentially all the work is
521 * done at higher levels; just return the address
522 * of the peer, storing through addr.
525 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
528 struct inpcb
*inp
= sotoinpcb(so
);
529 struct tcpcb
*tp
= NULL
;
532 if (so
->so_state
& SS_ISDISCONNECTED
) {
533 error
= ECONNABORTED
;
536 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
541 in_setpeeraddr(so
, nam
);
542 COMMON_END(PRU_ACCEPT
);
547 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
550 struct inpcb
*inp
= sotoinpcb(so
);
551 struct tcpcb
*tp
= NULL
;
554 if (so
->so_state
& SS_ISDISCONNECTED
) {
555 error
= ECONNABORTED
;
558 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
563 in6_mapped_peeraddr(so
, nam
);
564 COMMON_END(PRU_ACCEPT
);
569 * Mark the connection as being incapable of further output.
572 * EINVAL [COMMON_START]
573 * tcp_output:EADDRNOTAVAIL
575 * tcp_output:EMSGSIZE
576 * tcp_output:EHOSTUNREACH
577 * tcp_output:ENETUNREACH
578 * tcp_output:ENETDOWN
581 * tcp_output:EMSGSIZE
583 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
586 tcp_usr_shutdown(struct socket
*so
)
589 struct inpcb
*inp
= sotoinpcb(so
);
594 /* In case we got disconnected from the peer */
597 tp
= tcp_usrclosed(tp
);
599 error
= tcp_output(tp
);
600 COMMON_END(PRU_SHUTDOWN
);
604 * After a receive, possibly send window update to peer.
607 tcp_usr_rcvd(struct socket
*so
, __unused
int flags
)
610 struct inpcb
*inp
= sotoinpcb(so
);
614 /* In case we got disconnected from the peer */
618 COMMON_END(PRU_RCVD
);
622 * Do a send by putting data in output queue and updating urgent
623 * marker if URG set. Possibly send more data. Unlike the other
624 * pru_*() routines, the mbuf chains are our responsibility. We
625 * must either enqueue them or free them. The other pru_* routines
626 * generally are caller-frees.
632 * tcp_connect:EADDRINUSE Address in use
633 * tcp_connect:EADDRNOTAVAIL Address not available.
634 * tcp_connect:EINVAL Invalid argument
635 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
636 * tcp_connect:EACCES Permission denied
637 * tcp_connect:EAGAIN Resource unavailable, try again
638 * tcp_connect:EPERM Operation not permitted
639 * tcp_output:EADDRNOTAVAIL
641 * tcp_output:EMSGSIZE
642 * tcp_output:EHOSTUNREACH
643 * tcp_output:ENETUNREACH
644 * tcp_output:ENETDOWN
647 * tcp_output:EMSGSIZE
649 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
650 * tcp6_connect:??? [IPV6 only]
653 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
654 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
657 struct inpcb
*inp
= sotoinpcb(so
);
664 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
666 * OOPS! we lost a race, the TCP session got reset after
667 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
668 * network interrupt in the non-splnet() section of sosend().
674 error
= ECONNRESET
; /* XXX EPIPE? */
680 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
685 /* TCP doesn't do control messages (rights, creds, etc) */
686 if (control
->m_len
) {
693 m_freem(control
); /* empty control, just free it */
695 if(!(flags
& PRUS_OOB
)) {
696 sbappendstream(&so
->so_snd
, m
);
697 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
699 * Do implied connect if not yet connected,
700 * initialize window to default value, and
701 * initialize maxseg/maxopd using peer's cached
706 error
= tcp6_connect(tp
, nam
, p
);
709 error
= tcp_connect(tp
, nam
, p
);
712 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
713 tcp_mss(tp
, -1, IFSCOPE_NONE
);
716 if (flags
& PRUS_EOF
) {
718 * Close the send side of the connection after
722 tp
= tcp_usrclosed(tp
);
725 if (flags
& PRUS_MORETOCOME
)
726 tp
->t_flags
|= TF_MORETOCOME
;
727 error
= tcp_output(tp
);
728 if (flags
& PRUS_MORETOCOME
)
729 tp
->t_flags
&= ~TF_MORETOCOME
;
732 if (sbspace(&so
->so_snd
) == 0) {
733 /* if no space is left in sockbuf,
734 * do not try to squeeze in OOB traffic */
740 * According to RFC961 (Assigned Protocols),
741 * the urgent pointer points to the last octet
742 * of urgent data. We continue, however,
743 * to consider it to indicate the first octet
744 * of data past the urgent section.
745 * Otherwise, snd_up should be one lower.
747 sbappendstream(&so
->so_snd
, m
);
748 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
750 * Do implied connect if not yet connected,
751 * initialize window to default value, and
752 * initialize maxseg/maxopd using peer's cached
757 error
= tcp6_connect(tp
, nam
, p
);
760 error
= tcp_connect(tp
, nam
, p
);
763 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
764 tcp_mss(tp
, -1, IFSCOPE_NONE
);
766 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
768 error
= tcp_output(tp
);
771 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
772 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
779 tcp_usr_abort(struct socket
*so
)
782 struct inpcb
*inp
= sotoinpcb(so
);
786 /* In case we got disconnected from the peer */
789 tp
= tcp_drop(tp
, ECONNABORTED
);
791 COMMON_END(PRU_ABORT
);
795 * Receive out-of-band data.
798 * EINVAL [COMMON_START]
803 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
806 struct inpcb
*inp
= sotoinpcb(so
);
810 if ((so
->so_oobmark
== 0 &&
811 (so
->so_state
& SS_RCVATMARK
) == 0) ||
812 so
->so_options
& SO_OOBINLINE
||
813 tp
->t_oobflags
& TCPOOB_HADDATA
) {
817 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
822 *mtod(m
, caddr_t
) = tp
->t_iobc
;
823 if ((flags
& MSG_PEEK
) == 0)
824 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
825 COMMON_END(PRU_RCVOOB
);
828 /* xxx - should be const */
829 struct pr_usrreqs tcp_usrreqs
= {
830 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
831 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
832 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
833 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
834 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
838 struct pr_usrreqs tcp6_usrreqs
= {
839 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
840 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
841 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
842 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
843 in6_mapped_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
848 * Common subroutine to open a TCP connection to remote host specified
849 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
850 * port number if needed. Call in_pcbladdr to do the routing and to choose
851 * a local host address (interface). If there is an existing incarnation
852 * of the same connection in TIME-WAIT state and if the remote host was
853 * sending CC options and if the connection duration was < MSL, then
854 * truncate the previous TIME-WAIT state and proceed.
855 * Initialize connection parameters and enter SYN-SENT state.
860 * in_pcbbind:EADDRNOTAVAIL Address not available.
861 * in_pcbbind:EINVAL Invalid argument
862 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
863 * in_pcbbind:EACCES Permission denied
864 * in_pcbbind:EADDRINUSE Address in use
865 * in_pcbbind:EAGAIN Resource unavailable, try again
866 * in_pcbbind:EPERM Operation not permitted
867 * in_pcbladdr:EINVAL Invalid argument
868 * in_pcbladdr:EAFNOSUPPORT Address family not supported
869 * in_pcbladdr:EADDRNOTAVAIL Address not available
872 tcp_connect(tp
, nam
, p
)
873 register struct tcpcb
*tp
;
874 struct sockaddr
*nam
;
877 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
878 struct socket
*so
= inp
->inp_socket
;
880 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
881 struct sockaddr_in
*ifaddr
;
882 struct rmxp_tao
*taop
;
883 struct rmxp_tao tao_noncached
;
886 if (inp
->inp_lport
== 0) {
887 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
893 * Cannot simply call in_pcbconnect, because there might be an
894 * earlier incarnation of this same connection still in
895 * TIME_WAIT state, creating an ADDRINUSE error.
897 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
901 tcp_unlock(inp
->inp_socket
, 0, 0);
902 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
903 sin
->sin_addr
, sin
->sin_port
,
904 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
906 inp
->inp_lport
, 0, NULL
);
908 tcp_lock(inp
->inp_socket
, 0, 0);
910 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
911 tcp_lock(oinp
->inp_socket
, 1, 0);
912 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
914 tcp_unlock(oinp
->inp_socket
, 1, 0);
918 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
919 otp
->t_state
== TCPS_TIME_WAIT
&&
920 otp
->t_starttime
< (u_int32_t
)tcp_msl
&&
921 (otp
->t_flags
& TF_RCVD_CC
))
922 otp
= tcp_close(otp
);
924 printf("tcp_connect: inp=%p err=EADDRINUSE\n", inp
);
926 tcp_unlock(oinp
->inp_socket
, 1, 0);
930 tcp_unlock(oinp
->inp_socket
, 1, 0);
933 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
934 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
935 inp
->inp_lport
== sin
->sin_port
)
937 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
938 /*lock inversion issue, mostly with udp multicast packets */
939 socket_unlock(inp
->inp_socket
, 0);
940 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
941 socket_lock(inp
->inp_socket
, 0);
943 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
944 inp
->inp_laddr
= ifaddr
->sin_addr
;
945 inp
->inp_faddr
= sin
->sin_addr
;
946 inp
->inp_fport
= sin
->sin_port
;
948 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
950 /* Compute window scaling to requesti according to sb_hiwat
951 * or leave us some room to increase potentially increase the window size depending
952 * on the default win scale
954 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
955 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
956 tp
->request_r_scale
++;
959 * Inflate window size only if no setsockopt was performed on the recv sockbuf and
960 * if we're not over our number of active pcbs.
963 if (((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0) && (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)) {
964 tp
->request_r_scale
= max(tcp_win_scale
, tp
->request_r_scale
);
965 so
->so_rcv
.sb_hiwat
= min(TCP_MAXWIN
<< tp
->request_r_scale
, (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
);
969 tcpstat
.tcps_connattempt
++;
970 tp
->t_state
= TCPS_SYN_SENT
;
971 tp
->t_timer
[TCPT_KEEP
] = tp
->t_keepinit
? tp
->t_keepinit
: tcp_keepinit
;
972 tp
->iss
= tcp_new_isn(tp
);
976 * Generate a CC value for this connection and
977 * check whether CC or CCnew should be used.
979 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
980 taop
= &tao_noncached
;
981 bzero(taop
, sizeof(*taop
));
984 tp
->cc_send
= CC_INC(tcp_ccgen
);
985 if (taop
->tao_ccsent
!= 0 &&
986 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
987 taop
->tao_ccsent
= tp
->cc_send
;
989 taop
->tao_ccsent
= 0;
990 tp
->t_flags
|= TF_SENDCCNEW
;
998 tcp6_connect(tp
, nam
, p
)
999 register struct tcpcb
*tp
;
1000 struct sockaddr
*nam
;
1003 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1004 struct socket
*so
= inp
->inp_socket
;
1006 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
1007 struct in6_addr addr6
;
1008 struct rmxp_tao
*taop
;
1009 struct rmxp_tao tao_noncached
;
1012 if (inp
->inp_lport
== 0) {
1013 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
1019 * Cannot simply call in_pcbconnect, because there might be an
1020 * earlier incarnation of this same connection still in
1021 * TIME_WAIT state, creating an ADDRINUSE error.
1023 error
= in6_pcbladdr(inp
, nam
, &addr6
);
1026 tcp_unlock(inp
->inp_socket
, 0, 0);
1027 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1028 &sin6
->sin6_addr
, sin6
->sin6_port
,
1029 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1032 inp
->inp_lport
, 0, NULL
);
1033 tcp_lock(inp
->inp_socket
, 0, 0);
1035 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1036 otp
->t_state
== TCPS_TIME_WAIT
&&
1037 otp
->t_starttime
< (u_int32_t
)tcp_msl
&&
1038 (otp
->t_flags
& TF_RCVD_CC
))
1039 otp
= tcp_close(otp
);
1043 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
1044 /*lock inversion issue, mostly with udp multicast packets */
1045 socket_unlock(inp
->inp_socket
, 0);
1046 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
1047 socket_lock(inp
->inp_socket
, 0);
1049 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1050 inp
->in6p_laddr
= addr6
;
1051 inp
->in6p_faddr
= sin6
->sin6_addr
;
1052 inp
->inp_fport
= sin6
->sin6_port
;
1053 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1054 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
1056 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
1058 /* Compute window scaling to request. */
1059 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1060 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
1061 tp
->request_r_scale
++;
1064 tcpstat
.tcps_connattempt
++;
1065 tp
->t_state
= TCPS_SYN_SENT
;
1066 tp
->t_timer
[TCPT_KEEP
] = tp
->t_keepinit
? tp
->t_keepinit
: tcp_keepinit
;
1067 tp
->iss
= tcp_new_isn(tp
);
1068 tcp_sendseqinit(tp
);
1071 * Generate a CC value for this connection and
1072 * check whether CC or CCnew should be used.
1074 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
1075 taop
= &tao_noncached
;
1076 bzero(taop
, sizeof(*taop
));
1079 tp
->cc_send
= CC_INC(tcp_ccgen
);
1080 if (taop
->tao_ccsent
!= 0 &&
1081 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1082 taop
->tao_ccsent
= tp
->cc_send
;
1084 taop
->tao_ccsent
= 0;
1085 tp
->t_flags
|= TF_SENDCCNEW
;
1093 * The new sockopt interface makes it possible for us to block in the
1094 * copyin/out step (if we take a page fault). Taking a page fault at
1095 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1096 * use TSM, there probably isn't any need for this function to run at
1097 * splnet() any more. This needs more examination.)
1100 tcp_ctloutput(so
, sopt
)
1102 struct sockopt
*sopt
;
1104 int error
, opt
, optval
;
1109 inp
= sotoinpcb(so
);
1111 return (ECONNRESET
);
1113 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1115 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
1116 error
= ip6_ctloutput(so
, sopt
);
1119 error
= ip_ctloutput(so
, sopt
);
1122 tp
= intotcpcb(inp
);
1124 return (ECONNRESET
);
1127 switch (sopt
->sopt_dir
) {
1129 switch (sopt
->sopt_name
) {
1133 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1138 switch (sopt
->sopt_name
) {
1149 opt
= 0; /* dead code to fool gcc */
1156 tp
->t_flags
&= ~opt
;
1160 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1165 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
1166 optval
+ 40 >= tcp_minmss
)
1167 tp
->t_maxseg
= optval
;
1173 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1180 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
1181 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
); /* reset the timer to new value */
1185 case TCP_CONNECTIONTIMEOUT
:
1186 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1193 tp
->t_keepinit
= optval
* TCP_RETRANSHZ
;
1197 error
= ENOPROTOOPT
;
1203 switch (sopt
->sopt_name
) {
1205 optval
= tp
->t_flags
& TF_NODELAY
;
1208 optval
= tp
->t_maxseg
;
1211 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
1214 optval
= tp
->t_flags
& TF_NOOPT
;
1217 optval
= tp
->t_flags
& TF_NOPUSH
;
1219 case TCP_CONNECTIONTIMEOUT
:
1220 optval
= tp
->t_keepinit
/ TCP_RETRANSHZ
;
1223 error
= ENOPROTOOPT
;
1227 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1234 * tcp_sendspace and tcp_recvspace are the default send and receive window
1235 * sizes, respectively. These are obsolescent (this information should
1236 * be set by the route).
1238 u_int32_t tcp_sendspace
= 1448*256;
1239 u_int32_t tcp_recvspace
= 1448*384;
1241 /* During attach, the size of socket buffer allocated is limited to
1242 * sb_max in sbreserve. Disallow setting the tcp send and recv space
1243 * to be more than sb_max because that will cause tcp_attach to fail
1244 * (see radar 5713060)
1247 sysctl_tcp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
1248 __unused
int arg2
, struct sysctl_req
*req
) {
1249 u_int32_t new_value
= 0, *space_p
= NULL
;
1250 int changed
= 0, error
= 0;
1251 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
1253 switch (oidp
->oid_number
) {
1254 case TCPCTL_SENDSPACE
:
1255 space_p
= &tcp_sendspace
;
1257 case TCPCTL_RECVSPACE
:
1258 space_p
= &tcp_recvspace
;
1263 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
1264 &new_value
, &changed
);
1266 if (new_value
> 0 && new_value
<= sb_effective_max
) {
1267 *space_p
= new_value
;
1275 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLTYPE_INT
| CTLFLAG_RW
,
1276 &tcp_sendspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum outgoing TCP datagram size");
1277 SYSCTL_PROC(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
,
1278 &tcp_recvspace
, 0, &sysctl_tcp_sospace
, "IU", "Maximum incoming TCP datagram size");
1282 * Attach TCP protocol to socket, allocating
1283 * internet protocol control block, tcp control block,
1284 * bufer space, and entering LISTEN state if to accept connections.
1286 * Returns: 0 Success
1287 * in_pcballoc:ENOBUFS
1288 * in_pcballoc:ENOMEM
1289 * in_pcballoc:??? [IPSEC specific]
1297 register struct tcpcb
*tp
;
1300 u_long sb_effective_max
;
1302 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != 0;
1305 error
= in_pcballoc(so
, &tcbinfo
, p
);
1309 inp
= sotoinpcb(so
);
1311 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1313 * The goal is to let clients machines use large send/rcv default windows to compensate for link
1314 * latency and make sure the receiver is not constraining the sender window.
1315 * But we doon't want to have a few connections use all our mbuf space for servers.
1316 * This is done by watching a threshold of tcpcbs in use and bumping the default send and rcvspace
1317 * only if that threshold isn't reached.
1318 * We're also advertising a much bigger window size (tuneable by sysctl) in correlation with * the max socket buffer size if
1319 * we consider that we have enough ressources for it. This window will be adjusted depending on the
1320 * global socket layer buffer use with the use of tcp_sbpace
1323 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
) {
1324 sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
1325 error
= soreserve(so
, max(min((TCP_MAXWIN
<< tcp_win_scale
)/4, sb_effective_max
), tcp_sendspace
),
1326 max(min((TCP_MAXWIN
<< tcp_win_scale
)/2, sb_effective_max
), tcp_recvspace
));
1329 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1336 inp
->inp_vflag
|= INP_IPV6
;
1337 inp
->in6p_hops
= -1; /* use kernel default */
1341 inp
->inp_vflag
|= INP_IPV4
;
1342 tp
= tcp_newtcpcb(inp
);
1344 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1346 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1353 so
->so_state
|= nofd
;
1356 tp
->t_state
= TCPS_CLOSED
;
1361 * Initiate (or continue) disconnect.
1362 * If embryonic state, just send reset (once).
1363 * If in ``let data drain'' option and linger null, just drop.
1364 * Otherwise (hard), mark socket disconnecting and drop
1365 * current input data; switch states based on user close, and
1366 * send segment to peer (with FIN).
1368 static struct tcpcb
*
1370 register struct tcpcb
*tp
;
1372 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1374 if (tp
->t_state
< TCPS_ESTABLISHED
)
1376 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1377 tp
= tcp_drop(tp
, 0);
1379 soisdisconnecting(so
);
1380 sbflush(&so
->so_rcv
);
1381 tp
= tcp_usrclosed(tp
);
1383 (void) tcp_output(tp
);
1389 * User issued close, and wish to trail through shutdown states:
1390 * if never received SYN, just forget it. If got a SYN from peer,
1391 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1392 * If already got a FIN from peer, then almost done; go to LAST_ACK
1393 * state. In all other cases, have already sent FIN to peer (e.g.
1394 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1395 * for peer to send FIN or not respond to keep-alives, etc.
1396 * We can let the user exit from the close as soon as the FIN is acked.
1398 static struct tcpcb
*
1400 register struct tcpcb
*tp
;
1403 switch (tp
->t_state
) {
1407 tp
->t_state
= TCPS_CLOSED
;
1412 case TCPS_SYN_RECEIVED
:
1413 tp
->t_flags
|= TF_NEEDFIN
;
1416 case TCPS_ESTABLISHED
:
1417 tp
->t_state
= TCPS_FIN_WAIT_1
;
1420 case TCPS_CLOSE_WAIT
:
1421 tp
->t_state
= TCPS_LAST_ACK
;
1424 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1425 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1426 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1427 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1428 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
1434 tcp_in_cksum_stats(u_int32_t len
)
1437 tcps_in_sw_cksum_bytes
+= len
;
1441 tcp_out_cksum_stats(u_int32_t len
)
1443 tcps_out_sw_cksum
++;
1444 tcps_out_sw_cksum_bytes
+= len
;