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 __private_extern__
unsigned int tcp_sockthreshold
= 64;
159 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sockthreshold
, CTLFLAG_RW
,
160 &tcp_sockthreshold
, 0, "TCP Socket size increased if less than threshold");
163 * TCP attaches to socket via pru_attach(), reserving space,
164 * and an internet control block.
170 * tcp_attach:??? [IPSEC specific]
173 tcp_usr_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
176 struct inpcb
*inp
= sotoinpcb(so
);
177 struct tcpcb
*tp
= 0;
186 error
= tcp_attach(so
, p
);
190 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
191 so
->so_linger
= TCP_LINGERTIME
* hz
;
194 TCPDEBUG2(PRU_ATTACH
);
199 * pru_detach() detaches the TCP protocol from the socket.
200 * If the protocol state is non-embryonic, then can't
201 * do this directly: have to initiate a pru_disconnect(),
202 * which may finish later; embryonic TCB's can just
206 tcp_usr_detach(struct socket
*so
)
209 struct inpcb
*inp
= sotoinpcb(so
);
213 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
214 return EINVAL
; /* XXX */
217 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
220 /* In case we got disconnected from the peer */
224 tp
= tcp_disconnect(tp
);
226 TCPDEBUG2(PRU_DETACH
);
230 #define COMMON_START() TCPDEBUG0; \
232 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { \
235 tp = intotcpcb(inp); \
239 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
243 * Give the socket an address.
246 * EINVAL Invalid argument [COMMON_START]
247 * EAFNOSUPPORT Address family not supported
248 * in_pcbbind:EADDRNOTAVAIL Address not available.
249 * in_pcbbind:EINVAL Invalid argument
250 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
251 * in_pcbbind:EACCES Permission denied
252 * in_pcbbind:EADDRINUSE Address in use
253 * in_pcbbind:EAGAIN Resource unavailable, try again
254 * in_pcbbind:EPERM Operation not permitted
257 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
260 struct inpcb
*inp
= sotoinpcb(so
);
262 struct sockaddr_in
*sinp
;
266 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
267 error
= EAFNOSUPPORT
;
272 * Must check for multicast addresses and disallow binding
275 sinp
= (struct sockaddr_in
*)nam
;
276 if (sinp
->sin_family
== AF_INET
&&
277 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
278 error
= EAFNOSUPPORT
;
281 error
= in_pcbbind(inp
, nam
, p
);
284 COMMON_END(PRU_BIND
);
290 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
293 struct inpcb
*inp
= sotoinpcb(so
);
295 struct sockaddr_in6
*sin6p
;
299 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
300 error
= EAFNOSUPPORT
;
305 * Must check for multicast addresses and disallow binding
308 sin6p
= (struct sockaddr_in6
*)nam
;
309 if (sin6p
->sin6_family
== AF_INET6
&&
310 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
311 error
= EAFNOSUPPORT
;
314 inp
->inp_vflag
&= ~INP_IPV4
;
315 inp
->inp_vflag
|= INP_IPV6
;
316 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
317 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
318 inp
->inp_vflag
|= INP_IPV4
;
319 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
320 struct sockaddr_in sin
;
322 in6_sin6_2_sin(&sin
, sin6p
);
323 inp
->inp_vflag
|= INP_IPV4
;
324 inp
->inp_vflag
&= ~INP_IPV6
;
325 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
329 error
= in6_pcbbind(inp
, nam
, p
);
332 COMMON_END(PRU_BIND
);
337 * Prepare to accept connections.
340 * EINVAL [COMMON_START]
341 * in_pcbbind:EADDRNOTAVAIL Address not available.
342 * in_pcbbind:EINVAL Invalid argument
343 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
344 * in_pcbbind:EACCES Permission denied
345 * in_pcbbind:EADDRINUSE Address in use
346 * in_pcbbind:EAGAIN Resource unavailable, try again
347 * in_pcbbind:EPERM Operation not permitted
350 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
353 struct inpcb
*inp
= sotoinpcb(so
);
357 if (inp
->inp_lport
== 0)
358 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
360 tp
->t_state
= TCPS_LISTEN
;
361 COMMON_END(PRU_LISTEN
);
366 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
369 struct inpcb
*inp
= sotoinpcb(so
);
373 if (inp
->inp_lport
== 0) {
374 inp
->inp_vflag
&= ~INP_IPV4
;
375 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
376 inp
->inp_vflag
|= INP_IPV4
;
377 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
380 tp
->t_state
= TCPS_LISTEN
;
381 COMMON_END(PRU_LISTEN
);
386 * Initiate connection to peer.
387 * Create a template for use in transmissions on this connection.
388 * Enter SYN_SENT state, and mark socket as connecting.
389 * Start keep-alive timer, and seed output sequence space.
390 * Send initial segment on connection.
393 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
396 struct inpcb
*inp
= sotoinpcb(so
);
398 struct sockaddr_in
*sinp
;
403 else if (inp
->inp_state
== INPCB_STATE_DEAD
) {
405 error
= so
->so_error
;
414 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
) {
415 error
= EAFNOSUPPORT
;
419 * Must disallow TCP ``connections'' to multicast addresses.
421 sinp
= (struct sockaddr_in
*)nam
;
422 if (sinp
->sin_family
== AF_INET
423 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
424 error
= EAFNOSUPPORT
;
429 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
431 error
= tcp_output(tp
);
432 COMMON_END(PRU_CONNECT
);
437 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
440 struct inpcb
*inp
= sotoinpcb(so
);
442 struct sockaddr_in6
*sin6p
;
446 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET6
) {
447 error
= EAFNOSUPPORT
;
452 * Must disallow TCP ``connections'' to multicast addresses.
454 sin6p
= (struct sockaddr_in6
*)nam
;
455 if (sin6p
->sin6_family
== AF_INET6
456 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
457 error
= EAFNOSUPPORT
;
461 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
462 struct sockaddr_in sin
;
464 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
467 in6_sin6_2_sin(&sin
, sin6p
);
468 inp
->inp_vflag
|= INP_IPV4
;
469 inp
->inp_vflag
&= ~INP_IPV6
;
470 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
472 error
= tcp_output(tp
);
475 inp
->inp_vflag
&= ~INP_IPV4
;
476 inp
->inp_vflag
|= INP_IPV6
;
477 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
479 error
= tcp_output(tp
);
482 COMMON_END(PRU_CONNECT
);
487 * Initiate disconnect from peer.
488 * If connection never passed embryonic stage, just drop;
489 * else if don't need to let data drain, then can just drop anyways,
490 * else have to begin TCP shutdown process: mark socket disconnecting,
491 * drain unread data, state switch to reflect user close, and
492 * send segment (e.g. FIN) to peer. Socket will be really disconnected
493 * when peer sends FIN and acks ours.
495 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
498 tcp_usr_disconnect(struct socket
*so
)
501 struct inpcb
*inp
= sotoinpcb(so
);
505 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
508 /* In case we got disconnected from the peer */
511 tp
= tcp_disconnect(tp
);
512 COMMON_END(PRU_DISCONNECT
);
516 * Accept a connection. Essentially all the work is
517 * done at higher levels; just return the address
518 * of the peer, storing through addr.
521 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
524 struct inpcb
*inp
= sotoinpcb(so
);
525 struct tcpcb
*tp
= NULL
;
528 if (so
->so_state
& SS_ISDISCONNECTED
) {
529 error
= ECONNABORTED
;
532 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
537 in_setpeeraddr(so
, nam
);
538 COMMON_END(PRU_ACCEPT
);
543 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
546 struct inpcb
*inp
= sotoinpcb(so
);
547 struct tcpcb
*tp
= NULL
;
550 if (so
->so_state
& SS_ISDISCONNECTED
) {
551 error
= ECONNABORTED
;
554 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
559 in6_mapped_peeraddr(so
, nam
);
560 COMMON_END(PRU_ACCEPT
);
565 * Mark the connection as being incapable of further output.
568 * EINVAL [COMMON_START]
569 * tcp_output:EADDRNOTAVAIL
571 * tcp_output:EMSGSIZE
572 * tcp_output:EHOSTUNREACH
573 * tcp_output:ENETUNREACH
574 * tcp_output:ENETDOWN
577 * tcp_output:EMSGSIZE
579 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
582 tcp_usr_shutdown(struct socket
*so
)
585 struct inpcb
*inp
= sotoinpcb(so
);
590 /* In case we got disconnected from the peer */
593 tp
= tcp_usrclosed(tp
);
595 error
= tcp_output(tp
);
596 COMMON_END(PRU_SHUTDOWN
);
600 * After a receive, possibly send window update to peer.
603 tcp_usr_rcvd(struct socket
*so
, __unused
int flags
)
606 struct inpcb
*inp
= sotoinpcb(so
);
610 /* In case we got disconnected from the peer */
614 COMMON_END(PRU_RCVD
);
618 * Do a send by putting data in output queue and updating urgent
619 * marker if URG set. Possibly send more data. Unlike the other
620 * pru_*() routines, the mbuf chains are our responsibility. We
621 * must either enqueue them or free them. The other pru_* routines
622 * generally are caller-frees.
628 * tcp_connect:EADDRINUSE Address in use
629 * tcp_connect:EADDRNOTAVAIL Address not available.
630 * tcp_connect:EINVAL Invalid argument
631 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
632 * tcp_connect:EACCES Permission denied
633 * tcp_connect:EAGAIN Resource unavailable, try again
634 * tcp_connect:EPERM Operation not permitted
635 * tcp_output:EADDRNOTAVAIL
637 * tcp_output:EMSGSIZE
638 * tcp_output:EHOSTUNREACH
639 * tcp_output:ENETUNREACH
640 * tcp_output:ENETDOWN
643 * tcp_output:EMSGSIZE
645 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
646 * tcp6_connect:??? [IPV6 only]
649 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
650 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
653 struct inpcb
*inp
= sotoinpcb(so
);
660 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
662 * OOPS! we lost a race, the TCP session got reset after
663 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
664 * network interrupt in the non-splnet() section of sosend().
670 error
= ECONNRESET
; /* XXX EPIPE? */
676 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
681 /* TCP doesn't do control messages (rights, creds, etc) */
682 if (control
->m_len
) {
689 m_freem(control
); /* empty control, just free it */
691 if(!(flags
& PRUS_OOB
)) {
692 sbappendstream(&so
->so_snd
, m
);
693 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
695 * Do implied connect if not yet connected,
696 * initialize window to default value, and
697 * initialize maxseg/maxopd using peer's cached
702 error
= tcp6_connect(tp
, nam
, p
);
705 error
= tcp_connect(tp
, nam
, p
);
708 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
712 if (flags
& PRUS_EOF
) {
714 * Close the send side of the connection after
718 tp
= tcp_usrclosed(tp
);
721 if (flags
& PRUS_MORETOCOME
)
722 tp
->t_flags
|= TF_MORETOCOME
;
723 error
= tcp_output(tp
);
724 if (flags
& PRUS_MORETOCOME
)
725 tp
->t_flags
&= ~TF_MORETOCOME
;
728 if (sbspace(&so
->so_snd
) < -512) {
734 * According to RFC961 (Assigned Protocols),
735 * the urgent pointer points to the last octet
736 * of urgent data. We continue, however,
737 * to consider it to indicate the first octet
738 * of data past the urgent section.
739 * Otherwise, snd_up should be one lower.
741 sbappendstream(&so
->so_snd
, m
);
742 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
744 * Do implied connect if not yet connected,
745 * initialize window to default value, and
746 * initialize maxseg/maxopd using peer's cached
751 error
= tcp6_connect(tp
, nam
, p
);
754 error
= tcp_connect(tp
, nam
, p
);
757 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
760 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
762 error
= tcp_output(tp
);
765 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
766 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
773 tcp_usr_abort(struct socket
*so
)
776 struct inpcb
*inp
= sotoinpcb(so
);
780 /* In case we got disconnected from the peer */
783 tp
= tcp_drop(tp
, ECONNABORTED
);
785 COMMON_END(PRU_ABORT
);
789 * Receive out-of-band data.
792 * EINVAL [COMMON_START]
797 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
800 struct inpcb
*inp
= sotoinpcb(so
);
804 if ((so
->so_oobmark
== 0 &&
805 (so
->so_state
& SS_RCVATMARK
) == 0) ||
806 so
->so_options
& SO_OOBINLINE
||
807 tp
->t_oobflags
& TCPOOB_HADDATA
) {
811 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
816 *mtod(m
, caddr_t
) = tp
->t_iobc
;
817 if ((flags
& MSG_PEEK
) == 0)
818 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
819 COMMON_END(PRU_RCVOOB
);
822 /* xxx - should be const */
823 struct pr_usrreqs tcp_usrreqs
= {
824 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
825 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
826 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
827 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
828 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
832 struct pr_usrreqs tcp6_usrreqs
= {
833 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
834 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
835 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
836 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
837 in6_mapped_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
842 * Common subroutine to open a TCP connection to remote host specified
843 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
844 * port number if needed. Call in_pcbladdr to do the routing and to choose
845 * a local host address (interface). If there is an existing incarnation
846 * of the same connection in TIME-WAIT state and if the remote host was
847 * sending CC options and if the connection duration was < MSL, then
848 * truncate the previous TIME-WAIT state and proceed.
849 * Initialize connection parameters and enter SYN-SENT state.
854 * in_pcbbind:EADDRNOTAVAIL Address not available.
855 * in_pcbbind:EINVAL Invalid argument
856 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
857 * in_pcbbind:EACCES Permission denied
858 * in_pcbbind:EADDRINUSE Address in use
859 * in_pcbbind:EAGAIN Resource unavailable, try again
860 * in_pcbbind:EPERM Operation not permitted
861 * in_pcbladdr:EINVAL Invalid argument
862 * in_pcbladdr:EAFNOSUPPORT Address family not supported
863 * in_pcbladdr:EADDRNOTAVAIL Address not available
866 tcp_connect(tp
, nam
, p
)
867 register struct tcpcb
*tp
;
868 struct sockaddr
*nam
;
871 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
872 struct socket
*so
= inp
->inp_socket
;
874 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
875 struct sockaddr_in
*ifaddr
;
876 struct rmxp_tao
*taop
;
877 struct rmxp_tao tao_noncached
;
880 if (inp
->inp_lport
== 0) {
881 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
887 * Cannot simply call in_pcbconnect, because there might be an
888 * earlier incarnation of this same connection still in
889 * TIME_WAIT state, creating an ADDRINUSE error.
891 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
895 tcp_unlock(inp
->inp_socket
, 0, 0);
896 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
897 sin
->sin_addr
, sin
->sin_port
,
898 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
900 inp
->inp_lport
, 0, NULL
);
902 tcp_lock(inp
->inp_socket
, 0, 0);
904 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
905 tcp_lock(oinp
->inp_socket
, 1, 0);
906 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
908 tcp_unlock(oinp
->inp_socket
, 1, 0);
912 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
913 otp
->t_state
== TCPS_TIME_WAIT
&&
914 otp
->t_starttime
< (u_long
)tcp_msl
&&
915 (otp
->t_flags
& TF_RCVD_CC
))
916 otp
= tcp_close(otp
);
918 printf("tcp_connect: inp=%p err=EADDRINUSE\n", inp
);
920 tcp_unlock(oinp
->inp_socket
, 1, 0);
924 tcp_unlock(oinp
->inp_socket
, 1, 0);
927 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
928 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
929 inp
->inp_lport
== sin
->sin_port
)
931 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
932 /*lock inversion issue, mostly with udp multicast packets */
933 socket_unlock(inp
->inp_socket
, 0);
934 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
935 socket_lock(inp
->inp_socket
, 0);
937 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
938 inp
->inp_laddr
= ifaddr
->sin_addr
;
939 inp
->inp_faddr
= sin
->sin_addr
;
940 inp
->inp_fport
= sin
->sin_port
;
942 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
944 /* Compute window scaling to requesti according to sb_hiwat
945 * or leave us some room to increase potentially increase the window size depending
946 * on the default win scale
948 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
949 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
950 tp
->request_r_scale
++;
953 * Inflate window size only if no setsockopt was performed on the recv sockbuf and
954 * if we're not over our number of active pcbs.
957 if (((so
->so_rcv
.sb_flags
& SB_USRSIZE
) == 0) && (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)) {
958 tp
->request_r_scale
= max(tcp_win_scale
, tp
->request_r_scale
);
959 so
->so_rcv
.sb_hiwat
= min(TCP_MAXWIN
<< tp
->request_r_scale
, (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
);
963 tcpstat
.tcps_connattempt
++;
964 tp
->t_state
= TCPS_SYN_SENT
;
965 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
966 tp
->iss
= tcp_new_isn(tp
);
970 * Generate a CC value for this connection and
971 * check whether CC or CCnew should be used.
973 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
974 taop
= &tao_noncached
;
975 bzero(taop
, sizeof(*taop
));
978 tp
->cc_send
= CC_INC(tcp_ccgen
);
979 if (taop
->tao_ccsent
!= 0 &&
980 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
981 taop
->tao_ccsent
= tp
->cc_send
;
983 taop
->tao_ccsent
= 0;
984 tp
->t_flags
|= TF_SENDCCNEW
;
992 tcp6_connect(tp
, nam
, p
)
993 register struct tcpcb
*tp
;
994 struct sockaddr
*nam
;
997 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
998 struct socket
*so
= inp
->inp_socket
;
1000 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
1001 struct in6_addr addr6
;
1002 struct rmxp_tao
*taop
;
1003 struct rmxp_tao tao_noncached
;
1006 if (inp
->inp_lport
== 0) {
1007 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
1013 * Cannot simply call in_pcbconnect, because there might be an
1014 * earlier incarnation of this same connection still in
1015 * TIME_WAIT state, creating an ADDRINUSE error.
1017 error
= in6_pcbladdr(inp
, nam
, &addr6
);
1020 tcp_unlock(inp
->inp_socket
, 0, 0);
1021 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
1022 &sin6
->sin6_addr
, sin6
->sin6_port
,
1023 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
1026 inp
->inp_lport
, 0, NULL
);
1027 tcp_lock(inp
->inp_socket
, 0, 0);
1029 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
1030 otp
->t_state
== TCPS_TIME_WAIT
&&
1031 otp
->t_starttime
< (u_long
)tcp_msl
&&
1032 (otp
->t_flags
& TF_RCVD_CC
))
1033 otp
= tcp_close(otp
);
1037 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
1038 /*lock inversion issue, mostly with udp multicast packets */
1039 socket_unlock(inp
->inp_socket
, 0);
1040 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
1041 socket_lock(inp
->inp_socket
, 0);
1043 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1044 inp
->in6p_laddr
= addr6
;
1045 inp
->in6p_faddr
= sin6
->sin6_addr
;
1046 inp
->inp_fport
= sin6
->sin6_port
;
1047 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != 0)
1048 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
1050 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
1052 /* Compute window scaling to request. */
1053 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
1054 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
1055 tp
->request_r_scale
++;
1058 tcpstat
.tcps_connattempt
++;
1059 tp
->t_state
= TCPS_SYN_SENT
;
1060 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
1061 tp
->iss
= tcp_new_isn(tp
);
1062 tcp_sendseqinit(tp
);
1065 * Generate a CC value for this connection and
1066 * check whether CC or CCnew should be used.
1068 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
1069 taop
= &tao_noncached
;
1070 bzero(taop
, sizeof(*taop
));
1073 tp
->cc_send
= CC_INC(tcp_ccgen
);
1074 if (taop
->tao_ccsent
!= 0 &&
1075 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
1076 taop
->tao_ccsent
= tp
->cc_send
;
1078 taop
->tao_ccsent
= 0;
1079 tp
->t_flags
|= TF_SENDCCNEW
;
1087 * The new sockopt interface makes it possible for us to block in the
1088 * copyin/out step (if we take a page fault). Taking a page fault at
1089 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1090 * use TSM, there probably isn't any need for this function to run at
1091 * splnet() any more. This needs more examination.)
1094 tcp_ctloutput(so
, sopt
)
1096 struct sockopt
*sopt
;
1098 int error
, opt
, optval
;
1103 inp
= sotoinpcb(so
);
1105 return (ECONNRESET
);
1107 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
1109 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
1110 error
= ip6_ctloutput(so
, sopt
);
1113 error
= ip_ctloutput(so
, sopt
);
1116 tp
= intotcpcb(inp
);
1118 return (ECONNRESET
);
1121 switch (sopt
->sopt_dir
) {
1123 switch (sopt
->sopt_name
) {
1127 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1132 switch (sopt
->sopt_name
) {
1143 opt
= 0; /* dead code to fool gcc */
1150 tp
->t_flags
&= ~opt
;
1154 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1159 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
1160 optval
+ 40 >= tcp_minmss
)
1161 tp
->t_maxseg
= optval
;
1167 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1174 tp
->t_keepidle
= optval
* TCP_RETRANSHZ
;
1175 tp
->t_timer
[TCPT_KEEP
] = TCP_KEEPIDLE(tp
); /* reset the timer to new value */
1180 error
= ENOPROTOOPT
;
1186 switch (sopt
->sopt_name
) {
1188 optval
= tp
->t_flags
& TF_NODELAY
;
1191 optval
= tp
->t_maxseg
;
1194 optval
= tp
->t_keepidle
/ TCP_RETRANSHZ
;
1197 optval
= tp
->t_flags
& TF_NOOPT
;
1200 optval
= tp
->t_flags
& TF_NOPUSH
;
1203 error
= ENOPROTOOPT
;
1207 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1214 * tcp_sendspace and tcp_recvspace are the default send and receive window
1215 * sizes, respectively. These are obsolescent (this information should
1216 * be set by the route).
1218 u_long tcp_sendspace
= 1448*256;
1219 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1220 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1221 u_long tcp_recvspace
= 1448*384;
1222 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1223 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1227 * Attach TCP protocol to socket, allocating
1228 * internet protocol control block, tcp control block,
1229 * bufer space, and entering LISTEN state if to accept connections.
1231 * Returns: 0 Success
1232 * in_pcballoc:ENOBUFS
1233 * in_pcballoc:ENOMEM
1234 * in_pcballoc:??? [IPSEC specific]
1242 register struct tcpcb
*tp
;
1245 u_long sb_effective_max
;
1247 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != 0;
1250 error
= in_pcballoc(so
, &tcbinfo
, p
);
1254 inp
= sotoinpcb(so
);
1256 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1258 * The goal is to let clients machines use large send/rcv default windows to compensate for link
1259 * latency and make sure the receiver is not constraining the sender window.
1260 * But we doon't want to have a few connections use all our mbuf space for servers.
1261 * This is done by watching a threshold of tcpcbs in use and bumping the default send and rcvspace
1262 * only if that threshold isn't reached.
1263 * We're also advertising a much bigger window size (tuneable by sysctl) in correlation with * the max socket buffer size if
1264 * we consider that we have enough ressources for it. This window will be adjusted depending on the
1265 * global socket layer buffer use with the use of tcp_sbpace
1268 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
) {
1269 sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
1270 error
= soreserve(so
, max(min((TCP_MAXWIN
<< tcp_win_scale
)/4, sb_effective_max
), tcp_sendspace
),
1271 max(min((TCP_MAXWIN
<< tcp_win_scale
)/2, sb_effective_max
), tcp_recvspace
));
1274 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1281 inp
->inp_vflag
|= INP_IPV6
;
1282 inp
->in6p_hops
= -1; /* use kernel default */
1286 inp
->inp_vflag
|= INP_IPV4
;
1287 tp
= tcp_newtcpcb(inp
);
1289 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1291 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1298 so
->so_state
|= nofd
;
1301 tp
->t_state
= TCPS_CLOSED
;
1306 * Initiate (or continue) disconnect.
1307 * If embryonic state, just send reset (once).
1308 * If in ``let data drain'' option and linger null, just drop.
1309 * Otherwise (hard), mark socket disconnecting and drop
1310 * current input data; switch states based on user close, and
1311 * send segment to peer (with FIN).
1313 static struct tcpcb
*
1315 register struct tcpcb
*tp
;
1317 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1319 if (tp
->t_state
< TCPS_ESTABLISHED
)
1321 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1322 tp
= tcp_drop(tp
, 0);
1324 soisdisconnecting(so
);
1325 sbflush(&so
->so_rcv
);
1326 tp
= tcp_usrclosed(tp
);
1328 (void) tcp_output(tp
);
1334 * User issued close, and wish to trail through shutdown states:
1335 * if never received SYN, just forget it. If got a SYN from peer,
1336 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1337 * If already got a FIN from peer, then almost done; go to LAST_ACK
1338 * state. In all other cases, have already sent FIN to peer (e.g.
1339 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1340 * for peer to send FIN or not respond to keep-alives, etc.
1341 * We can let the user exit from the close as soon as the FIN is acked.
1343 static struct tcpcb
*
1345 register struct tcpcb
*tp
;
1348 switch (tp
->t_state
) {
1352 tp
->t_state
= TCPS_CLOSED
;
1357 case TCPS_SYN_RECEIVED
:
1358 tp
->t_flags
|= TF_NEEDFIN
;
1361 case TCPS_ESTABLISHED
:
1362 tp
->t_state
= TCPS_FIN_WAIT_1
;
1365 case TCPS_CLOSE_WAIT
:
1366 tp
->t_state
= TCPS_LAST_ACK
;
1369 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1370 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1371 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1372 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1373 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;
1379 tcp_in_cksum_stats(u_int32_t len
)
1382 tcps_in_sw_cksum_bytes
+= len
;
1386 tcp_out_cksum_stats(u_int32_t len
)
1388 tcps_out_sw_cksum
++;
1389 tcps_out_sw_cksum_bytes
+= len
;