2 * Copyright (c) 2000-2007 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
;
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
) < -512) {
738 * According to RFC961 (Assigned Protocols),
739 * the urgent pointer points to the last octet
740 * of urgent data. We continue, however,
741 * to consider it to indicate the first octet
742 * of data past the urgent section.
743 * Otherwise, snd_up should be one lower.
745 sbappendstream(&so
->so_snd
, m
);
746 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
748 * Do implied connect if not yet connected,
749 * initialize window to default value, and
750 * initialize maxseg/maxopd using peer's cached
755 error
= tcp6_connect(tp
, nam
, p
);
758 error
= tcp_connect(tp
, nam
, p
);
761 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
764 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
766 error
= tcp_output(tp
);
769 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
770 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
777 tcp_usr_abort(struct socket
*so
)
780 struct inpcb
*inp
= sotoinpcb(so
);
784 /* In case we got disconnected from the peer */
787 tp
= tcp_drop(tp
, ECONNABORTED
);
789 COMMON_END(PRU_ABORT
);
793 * Receive out-of-band data.
796 * EINVAL [COMMON_START]
801 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
804 struct inpcb
*inp
= sotoinpcb(so
);
808 if ((so
->so_oobmark
== 0 &&
809 (so
->so_state
& SS_RCVATMARK
) == 0) ||
810 so
->so_options
& SO_OOBINLINE
||
811 tp
->t_oobflags
& TCPOOB_HADDATA
) {
815 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
820 *mtod(m
, caddr_t
) = tp
->t_iobc
;
821 if ((flags
& MSG_PEEK
) == 0)
822 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
823 COMMON_END(PRU_RCVOOB
);
826 /* xxx - should be const */
827 struct pr_usrreqs tcp_usrreqs
= {
828 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
829 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
830 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
831 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
832 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
836 struct pr_usrreqs tcp6_usrreqs
= {
837 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
838 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
839 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
840 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
841 in6_mapped_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
846 * Common subroutine to open a TCP connection to remote host specified
847 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
848 * port number if needed. Call in_pcbladdr to do the routing and to choose
849 * a local host address (interface). If there is an existing incarnation
850 * of the same connection in TIME-WAIT state and if the remote host was
851 * sending CC options and if the connection duration was < MSL, then
852 * truncate the previous TIME-WAIT state and proceed.
853 * Initialize connection parameters and enter SYN-SENT state.
858 * in_pcbbind:EADDRNOTAVAIL Address not available.
859 * in_pcbbind:EINVAL Invalid argument
860 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
861 * in_pcbbind:EACCES Permission denied
862 * in_pcbbind:EADDRINUSE Address in use
863 * in_pcbbind:EAGAIN Resource unavailable, try again
864 * in_pcbbind:EPERM Operation not permitted
865 * in_pcbladdr:EINVAL Invalid argument
866 * in_pcbladdr:EAFNOSUPPORT Address family not supported
867 * in_pcbladdr:EADDRNOTAVAIL Address not available
870 tcp_connect(tp
, nam
, p
)
871 register struct tcpcb
*tp
;
872 struct sockaddr
*nam
;
875 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
876 struct socket
*so
= inp
->inp_socket
;
878 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
879 struct sockaddr_in
*ifaddr
;
880 struct rmxp_tao
*taop
;
881 struct rmxp_tao tao_noncached
;
884 if (inp
->inp_lport
== 0) {
885 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
891 * Cannot simply call in_pcbconnect, because there might be an
892 * earlier incarnation of this same connection still in
893 * TIME_WAIT state, creating an ADDRINUSE error.
895 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
899 tcp_unlock(inp
->inp_socket
, 0, 0);
900 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
901 sin
->sin_addr
, sin
->sin_port
,
902 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
904 inp
->inp_lport
, 0, NULL
);
906 tcp_lock(inp
->inp_socket
, 0, 0);
908 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
909 tcp_lock(oinp
->inp_socket
, 1, 0);
910 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
912 tcp_unlock(oinp
->inp_socket
, 1, 0);
916 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
917 otp
->t_state
== TCPS_TIME_WAIT
&&
918 otp
->t_starttime
< (u_long
)tcp_msl
&&
919 (otp
->t_flags
& TF_RCVD_CC
))
920 otp
= tcp_close(otp
);
922 printf("tcp_connect: inp=%p err=EADDRINUSE\n", inp
);
924 tcp_unlock(oinp
->inp_socket
, 1, 0);
928 tcp_unlock(oinp
->inp_socket
, 1, 0);
931 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
932 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
933 inp
->inp_lport
== sin
->sin_port
)
935 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
936 /*lock inversion issue, mostly with udp multicast packets */
937 socket_unlock(inp
->inp_socket
, 0);
938 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
939 socket_lock(inp
->inp_socket
, 0);
941 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
942 inp
->inp_laddr
= ifaddr
->sin_addr
;
943 inp
->inp_faddr
= sin
->sin_addr
;
944 inp
->inp_fport
= sin
->sin_port
;
946 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
948 /* Compute window scaling to requesti according to sb_hiwat
949 * or leave us some room to increase potentially increase the window size depending
950 * on the default win scale
952 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
953 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
954 tp
->request_r_scale
++;
957 * Inflate window size only if no setsockopt was performed on the recv sockbuf and
958 * if we're not over our number of active pcbs.
961 if (((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0) && (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)) {
962 tp
->request_r_scale
= max(tcp_win_scale
, tp
->request_r_scale
);
963 so
->so_rcv
.sb_hiwat
= min(TCP_MAXWIN
<< tp
->request_r_scale
, (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
);
967 tcpstat
.tcps_connattempt
++;
968 tp
->t_state
= TCPS_SYN_SENT
;
969 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
970 tp
->iss
= tcp_new_isn(tp
);
974 * Generate a CC value for this connection and
975 * check whether CC or CCnew should be used.
977 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
978 taop
= &tao_noncached
;
979 bzero(taop
, sizeof(*taop
));
982 tp
->cc_send
= CC_INC(tcp_ccgen
);
983 if (taop
->tao_ccsent
!= 0 &&
984 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
985 taop
->tao_ccsent
= tp
->cc_send
;
987 taop
->tao_ccsent
= 0;
988 tp
->t_flags
|= TF_SENDCCNEW
;
996 tcp6_connect(tp
, nam
, p
)
997 register struct tcpcb
*tp
;
998 struct sockaddr
*nam
;
1001 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
1002 struct socket
*so
= inp
->inp_socket
;
1004 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
1005 struct in6_addr addr6
;
1006 struct rmxp_tao
*taop
;
1007 struct rmxp_tao tao_noncached
;
1010 if (inp
->inp_lport
== 0) {
1011 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
1017 * Cannot simply call in_pcbconnect, because there might be an
1018 * earlier incarnation of this same connection still in
1019 * TIME_WAIT state, creating an ADDRINUSE error.
1021 error
= in6_pcbladdr(inp
, nam
, &addr6
);
1024 tcp_unlock(inp
->inp_socket
, 0, 0);
1025 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1026 &sin6
->sin6_addr
, sin6
->sin6_port
,
1027 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1030 inp
->inp_lport
, 0, NULL
);
1031 tcp_lock(inp
->inp_socket
, 0, 0);
1033 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1034 otp
->t_state
== TCPS_TIME_WAIT
&&
1035 otp
->t_starttime
< (u_long
)tcp_msl
&&
1036 (otp
->t_flags
& TF_RCVD_CC
))
1037 otp
= tcp_close(otp
);
1041 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
1042 /*lock inversion issue, mostly with udp multicast packets */
1043 socket_unlock(inp
->inp_socket
, 0);
1044 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
1045 socket_lock(inp
->inp_socket
, 0);
1047 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1048 inp
->in6p_laddr
= addr6
;
1049 inp
->in6p_faddr
= sin6
->sin6_addr
;
1050 inp
->inp_fport
= sin6
->sin6_port
;
1051 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1052 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
1054 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
1056 /* Compute window scaling to request. */
1057 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1058 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
1059 tp
->request_r_scale
++;
1062 tcpstat
.tcps_connattempt
++;
1063 tp
->t_state
= TCPS_SYN_SENT
;
1064 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1065 tp
->iss
= tcp_new_isn(tp
);
1066 tcp_sendseqinit(tp
);
1069 * Generate a CC value for this connection and
1070 * check whether CC or CCnew should be used.
1072 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
1073 taop
= &tao_noncached
;
1074 bzero(taop
, sizeof(*taop
));
1077 tp
->cc_send
= CC_INC(tcp_ccgen
);
1078 if (taop
->tao_ccsent
!= 0 &&
1079 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1080 taop
->tao_ccsent
= tp
->cc_send
;
1082 taop
->tao_ccsent
= 0;
1083 tp
->t_flags
|= TF_SENDCCNEW
;
1091 * The new sockopt interface makes it possible for us to block in the
1092 * copyin/out step (if we take a page fault). Taking a page fault at
1093 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1094 * use TSM, there probably isn't any need for this function to run at
1095 * splnet() any more. This needs more examination.)
1098 tcp_ctloutput(so
, sopt
)
1100 struct sockopt
*sopt
;
1102 int error
, opt
, optval
;
1107 inp
= sotoinpcb(so
);
1109 return (ECONNRESET
);
1111 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1113 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
1114 error
= ip6_ctloutput(so
, sopt
);
1117 error
= ip_ctloutput(so
, sopt
);
1120 tp
= intotcpcb(inp
);
1122 return (ECONNRESET
);
1125 switch (sopt
->sopt_dir
) {
1127 switch (sopt
->sopt_name
) {
1131 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1136 switch (sopt
->sopt_name
) {
1147 opt
= 0; /* dead code to fool gcc */
1154 tp
->t_flags
&= ~opt
;
1158 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1163 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
1164 optval
+ 40 >= tcp_minmss
)
1165 tp
->t_maxseg
= optval
;
1171 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1178 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
1179 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
); /* reset the timer to new value */
1184 error
= ENOPROTOOPT
;
1190 switch (sopt
->sopt_name
) {
1192 optval
= tp
->t_flags
& TF_NODELAY
;
1195 optval
= tp
->t_maxseg
;
1198 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
1201 optval
= tp
->t_flags
& TF_NOOPT
;
1204 optval
= tp
->t_flags
& TF_NOPUSH
;
1207 error
= ENOPROTOOPT
;
1211 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1218 * tcp_sendspace and tcp_recvspace are the default send and receive window
1219 * sizes, respectively. These are obsolescent (this information should
1220 * be set by the route).
1222 u_long tcp_sendspace
= 1448*256;
1223 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1224 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1225 u_long tcp_recvspace
= 1448*384;
1226 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1227 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1231 * Attach TCP protocol to socket, allocating
1232 * internet protocol control block, tcp control block,
1233 * bufer space, and entering LISTEN state if to accept connections.
1235 * Returns: 0 Success
1236 * in_pcballoc:ENOBUFS
1237 * in_pcballoc:ENOMEM
1238 * in_pcballoc:??? [IPSEC specific]
1246 register struct tcpcb
*tp
;
1249 u_long sb_effective_max
;
1251 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != 0;
1254 error
= in_pcballoc(so
, &tcbinfo
, p
);
1258 inp
= sotoinpcb(so
);
1260 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1262 * The goal is to let clients machines use large send/rcv default windows to compensate for link
1263 * latency and make sure the receiver is not constraining the sender window.
1264 * But we doon't want to have a few connections use all our mbuf space for servers.
1265 * This is done by watching a threshold of tcpcbs in use and bumping the default send and rcvspace
1266 * only if that threshold isn't reached.
1267 * We're also advertising a much bigger window size (tuneable by sysctl) in correlation with * the max socket buffer size if
1268 * we consider that we have enough ressources for it. This window will be adjusted depending on the
1269 * global socket layer buffer use with the use of tcp_sbpace
1272 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
) {
1273 sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
1274 error
= soreserve(so
, max(min((TCP_MAXWIN
<< tcp_win_scale
)/4, sb_effective_max
), tcp_sendspace
),
1275 max(min((TCP_MAXWIN
<< tcp_win_scale
)/2, sb_effective_max
), tcp_recvspace
));
1278 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1285 inp
->inp_vflag
|= INP_IPV6
;
1286 inp
->in6p_hops
= -1; /* use kernel default */
1290 inp
->inp_vflag
|= INP_IPV4
;
1291 tp
= tcp_newtcpcb(inp
);
1293 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1295 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1302 so
->so_state
|= nofd
;
1305 tp
->t_state
= TCPS_CLOSED
;
1310 * Initiate (or continue) disconnect.
1311 * If embryonic state, just send reset (once).
1312 * If in ``let data drain'' option and linger null, just drop.
1313 * Otherwise (hard), mark socket disconnecting and drop
1314 * current input data; switch states based on user close, and
1315 * send segment to peer (with FIN).
1317 static struct tcpcb
*
1319 register struct tcpcb
*tp
;
1321 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1323 if (tp
->t_state
< TCPS_ESTABLISHED
)
1325 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1326 tp
= tcp_drop(tp
, 0);
1328 soisdisconnecting(so
);
1329 sbflush(&so
->so_rcv
);
1330 tp
= tcp_usrclosed(tp
);
1332 (void) tcp_output(tp
);
1338 * User issued close, and wish to trail through shutdown states:
1339 * if never received SYN, just forget it. If got a SYN from peer,
1340 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1341 * If already got a FIN from peer, then almost done; go to LAST_ACK
1342 * state. In all other cases, have already sent FIN to peer (e.g.
1343 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1344 * for peer to send FIN or not respond to keep-alives, etc.
1345 * We can let the user exit from the close as soon as the FIN is acked.
1347 static struct tcpcb
*
1349 register struct tcpcb
*tp
;
1352 switch (tp
->t_state
) {
1356 tp
->t_state
= TCPS_CLOSED
;
1361 case TCPS_SYN_RECEIVED
:
1362 tp
->t_flags
|= TF_NEEDFIN
;
1365 case TCPS_ESTABLISHED
:
1366 tp
->t_state
= TCPS_FIN_WAIT_1
;
1369 case TCPS_CLOSE_WAIT
:
1370 tp
->t_state
= TCPS_LAST_ACK
;
1373 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1374 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1375 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1376 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1377 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
1383 tcp_in_cksum_stats(u_int32_t len
)
1386 tcps_in_sw_cksum_bytes
+= len
;
1390 tcp_out_cksum_stats(u_int32_t len
)
1392 tcps_out_sw_cksum
++;
1393 tcps_out_sw_cksum_bytes
+= len
;