]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
fd2f7cad3d137a79f5a27b9b6ad4149c6317f679
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1982, 1986, 1988, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
56 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/sysctl.h>
66 #include <sys/domain.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/protosw.h>
73 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
78 #include <netinet/ip6.h>
80 #include <netinet/in_pcb.h>
82 #include <netinet6/in6_pcb.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip_var.h>
87 #include <netinet6/ip6_var.h>
89 #include <netinet/tcp.h>
90 #include <netinet/tcp_fsm.h>
91 #include <netinet/tcp_seq.h>
92 #include <netinet/tcp_timer.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet/tcpip.h>
96 #include <netinet/tcp_debug.h>
100 #include <netinet6/ipsec.h>
104 * TCP protocol interface to socket abstraction.
106 extern char *tcpstates
[]; /* XXX ??? */
108 static int tcp_attach(struct socket
*, struct proc
*);
109 static int tcp_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
111 static int tcp6_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
113 static struct tcpcb
*
114 tcp_disconnect(struct tcpcb
*);
115 static struct tcpcb
*
116 tcp_usrclosed(struct tcpcb
*);
119 #define TCPDEBUG0 int ostate = 0
120 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
121 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
122 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
126 #define TCPDEBUG2(req)
130 * TCP attaches to socket via pru_attach(), reserving space,
131 * and an internet control block.
134 tcp_usr_attach(struct socket
*so
, int proto
, struct proc
*p
)
137 struct inpcb
*inp
= sotoinpcb(so
);
138 struct tcpcb
*tp
= 0;
147 error
= tcp_attach(so
, p
);
151 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
152 so
->so_linger
= TCP_LINGERTIME
* hz
;
155 TCPDEBUG2(PRU_ATTACH
);
160 * pru_detach() detaches the TCP protocol from the socket.
161 * If the protocol state is non-embryonic, then can't
162 * do this directly: have to initiate a pru_disconnect(),
163 * which may finish later; embryonic TCB's can just
167 tcp_usr_detach(struct socket
*so
)
170 struct inpcb
*inp
= sotoinpcb(so
);
174 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
175 return EINVAL
; /* XXX */
178 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
181 /* In case we got disconnected from the peer */
185 tp
= tcp_disconnect(tp
);
187 TCPDEBUG2(PRU_DETACH
);
191 #define COMMON_START() TCPDEBUG0; \
193 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { \
196 tp = intotcpcb(inp); \
200 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
204 * Give the socket an address.
207 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
210 struct inpcb
*inp
= sotoinpcb(so
);
212 struct sockaddr_in
*sinp
;
217 * Must check for multicast addresses and disallow binding
220 sinp
= (struct sockaddr_in
*)nam
;
221 if (sinp
->sin_family
== AF_INET
&&
222 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
223 error
= EAFNOSUPPORT
;
226 error
= in_pcbbind(inp
, nam
, p
);
229 COMMON_END(PRU_BIND
);
235 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
238 struct inpcb
*inp
= sotoinpcb(so
);
240 struct sockaddr_in6
*sin6p
;
245 * Must check for multicast addresses and disallow binding
248 sin6p
= (struct sockaddr_in6
*)nam
;
249 if (sin6p
->sin6_family
== AF_INET6
&&
250 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
251 error
= EAFNOSUPPORT
;
254 inp
->inp_vflag
&= ~INP_IPV4
;
255 inp
->inp_vflag
|= INP_IPV6
;
256 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
257 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
258 inp
->inp_vflag
|= INP_IPV4
;
259 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
260 struct sockaddr_in sin
;
262 in6_sin6_2_sin(&sin
, sin6p
);
263 inp
->inp_vflag
|= INP_IPV4
;
264 inp
->inp_vflag
&= ~INP_IPV6
;
265 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
269 error
= in6_pcbbind(inp
, nam
, p
);
272 COMMON_END(PRU_BIND
);
277 * Prepare to accept connections.
280 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
283 struct inpcb
*inp
= sotoinpcb(so
);
287 if (inp
->inp_lport
== 0)
288 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
290 tp
->t_state
= TCPS_LISTEN
;
291 COMMON_END(PRU_LISTEN
);
296 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
299 struct inpcb
*inp
= sotoinpcb(so
);
303 if (inp
->inp_lport
== 0) {
304 inp
->inp_vflag
&= ~INP_IPV4
;
305 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
306 inp
->inp_vflag
|= INP_IPV4
;
307 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
310 tp
->t_state
= TCPS_LISTEN
;
311 COMMON_END(PRU_LISTEN
);
316 * Initiate connection to peer.
317 * Create a template for use in transmissions on this connection.
318 * Enter SYN_SENT state, and mark socket as connecting.
319 * Start keep-alive timer, and seed output sequence space.
320 * Send initial segment on connection.
323 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
326 struct inpcb
*inp
= sotoinpcb(so
);
328 struct sockaddr_in
*sinp
;
333 * Must disallow TCP ``connections'' to multicast addresses.
335 sinp
= (struct sockaddr_in
*)nam
;
336 if (sinp
->sin_family
== AF_INET
337 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
338 error
= EAFNOSUPPORT
;
343 prison_remote_ip(p
, 0, &sinp
->sin_addr
.s_addr
);
346 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
348 error
= tcp_output(tp
);
349 COMMON_END(PRU_CONNECT
);
354 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
357 struct inpcb
*inp
= sotoinpcb(so
);
359 struct sockaddr_in6
*sin6p
;
364 * Must disallow TCP ``connections'' to multicast addresses.
366 sin6p
= (struct sockaddr_in6
*)nam
;
367 if (sin6p
->sin6_family
== AF_INET6
368 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
369 error
= EAFNOSUPPORT
;
373 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
374 struct sockaddr_in sin
;
376 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
379 in6_sin6_2_sin(&sin
, sin6p
);
380 inp
->inp_vflag
|= INP_IPV4
;
381 inp
->inp_vflag
&= ~INP_IPV6
;
382 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
384 error
= tcp_output(tp
);
387 inp
->inp_vflag
&= ~INP_IPV4
;
388 inp
->inp_vflag
|= INP_IPV6
;
389 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
391 error
= tcp_output(tp
);
394 COMMON_END(PRU_CONNECT
);
399 * Initiate disconnect from peer.
400 * If connection never passed embryonic stage, just drop;
401 * else if don't need to let data drain, then can just drop anyways,
402 * else have to begin TCP shutdown process: mark socket disconnecting,
403 * drain unread data, state switch to reflect user close, and
404 * send segment (e.g. FIN) to peer. Socket will be really disconnected
405 * when peer sends FIN and acks ours.
407 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
410 tcp_usr_disconnect(struct socket
*so
)
413 struct inpcb
*inp
= sotoinpcb(so
);
417 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
420 /* In case we got disconnected from the peer */
423 tp
= tcp_disconnect(tp
);
424 COMMON_END(PRU_DISCONNECT
);
428 * Accept a connection. Essentially all the work is
429 * done at higher levels; just return the address
430 * of the peer, storing through addr.
433 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
436 struct inpcb
*inp
= sotoinpcb(so
);
437 struct tcpcb
*tp
= NULL
;
440 if (so
->so_state
& SS_ISDISCONNECTED
) {
441 error
= ECONNABORTED
;
444 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
449 in_setpeeraddr(so
, nam
);
450 COMMON_END(PRU_ACCEPT
);
455 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
458 struct inpcb
*inp
= sotoinpcb(so
);
459 struct tcpcb
*tp
= NULL
;
462 if (so
->so_state
& SS_ISDISCONNECTED
) {
463 error
= ECONNABORTED
;
466 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
471 in6_mapped_peeraddr(so
, nam
);
472 COMMON_END(PRU_ACCEPT
);
476 * Mark the connection as being incapable of further output.
479 tcp_usr_shutdown(struct socket
*so
)
482 struct inpcb
*inp
= sotoinpcb(so
);
487 /* In case we got disconnected from the peer */
490 tp
= tcp_usrclosed(tp
);
492 error
= tcp_output(tp
);
493 COMMON_END(PRU_SHUTDOWN
);
497 * After a receive, possibly send window update to peer.
500 tcp_usr_rcvd(struct socket
*so
, int flags
)
503 struct inpcb
*inp
= sotoinpcb(so
);
507 /* In case we got disconnected from the peer */
511 COMMON_END(PRU_RCVD
);
515 * Do a send by putting data in output queue and updating urgent
516 * marker if URG set. Possibly send more data. Unlike the other
517 * pru_*() routines, the mbuf chains are our responsibility. We
518 * must either enqueue them or free them. The other pru_* routines
519 * generally are caller-frees.
522 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
523 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
526 struct inpcb
*inp
= sotoinpcb(so
);
533 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
535 * OOPS! we lost a race, the TCP session got reset after
536 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
537 * network interrupt in the non-splnet() section of sosend().
543 error
= ECONNRESET
; /* XXX EPIPE? */
549 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
554 /* TCP doesn't do control messages (rights, creds, etc) */
555 if (control
->m_len
) {
562 m_freem(control
); /* empty control, just free it */
564 if(!(flags
& PRUS_OOB
)) {
565 sbappend(&so
->so_snd
, m
);
566 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
568 * Do implied connect if not yet connected,
569 * initialize window to default value, and
570 * initialize maxseg/maxopd using peer's cached
575 error
= tcp6_connect(tp
, nam
, p
);
578 error
= tcp_connect(tp
, nam
, p
);
581 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
585 if (flags
& PRUS_EOF
) {
587 * Close the send side of the connection after
591 tp
= tcp_usrclosed(tp
);
594 if (flags
& PRUS_MORETOCOME
)
595 tp
->t_flags
|= TF_MORETOCOME
;
596 error
= tcp_output(tp
);
597 if (flags
& PRUS_MORETOCOME
)
598 tp
->t_flags
&= ~TF_MORETOCOME
;
601 if (sbspace(&so
->so_snd
) < -512) {
607 * According to RFC961 (Assigned Protocols),
608 * the urgent pointer points to the last octet
609 * of urgent data. We continue, however,
610 * to consider it to indicate the first octet
611 * of data past the urgent section.
612 * Otherwise, snd_up should be one lower.
614 sbappend(&so
->so_snd
, m
);
615 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
617 * Do implied connect if not yet connected,
618 * initialize window to default value, and
619 * initialize maxseg/maxopd using peer's cached
624 error
= tcp6_connect(tp
, nam
, p
);
627 error
= tcp_connect(tp
, nam
, p
);
630 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
633 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
635 error
= tcp_output(tp
);
638 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
639 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
646 tcp_usr_abort(struct socket
*so
)
649 struct inpcb
*inp
= sotoinpcb(so
);
653 /* In case we got disconnected from the peer */
656 tp
= tcp_drop(tp
, ECONNABORTED
);
658 COMMON_END(PRU_ABORT
);
662 * Receive out-of-band data.
665 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
668 struct inpcb
*inp
= sotoinpcb(so
);
672 if ((so
->so_oobmark
== 0 &&
673 (so
->so_state
& SS_RCVATMARK
) == 0) ||
674 so
->so_options
& SO_OOBINLINE
||
675 tp
->t_oobflags
& TCPOOB_HADDATA
) {
679 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
684 *mtod(m
, caddr_t
) = tp
->t_iobc
;
685 if ((flags
& MSG_PEEK
) == 0)
686 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
687 COMMON_END(PRU_RCVOOB
);
690 /* xxx - should be const */
691 struct pr_usrreqs tcp_usrreqs
= {
692 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
693 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
694 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
695 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
696 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
700 struct pr_usrreqs tcp6_usrreqs
= {
701 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
702 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
703 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
704 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
705 in6_mapped_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
710 * Common subroutine to open a TCP connection to remote host specified
711 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
712 * port number if needed. Call in_pcbladdr to do the routing and to choose
713 * a local host address (interface). If there is an existing incarnation
714 * of the same connection in TIME-WAIT state and if the remote host was
715 * sending CC options and if the connection duration was < MSL, then
716 * truncate the previous TIME-WAIT state and proceed.
717 * Initialize connection parameters and enter SYN-SENT state.
720 tcp_connect(tp
, nam
, p
)
721 register struct tcpcb
*tp
;
722 struct sockaddr
*nam
;
725 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
726 struct socket
*so
= inp
->inp_socket
;
728 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
729 struct sockaddr_in
*ifaddr
;
730 struct rmxp_tao
*taop
;
731 struct rmxp_tao tao_noncached
;
734 if (inp
->inp_lport
== 0) {
735 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
741 * Cannot simply call in_pcbconnect, because there might be an
742 * earlier incarnation of this same connection still in
743 * TIME_WAIT state, creating an ADDRINUSE error.
745 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
749 tcp_unlock(inp
->inp_socket
, 0, 0);
750 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
751 sin
->sin_addr
, sin
->sin_port
,
752 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
754 inp
->inp_lport
, 0, NULL
);
756 tcp_lock(inp
->inp_socket
, 0, 0);
758 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
759 tcp_lock(oinp
->inp_socket
, 1, 0);
760 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
762 tcp_unlock(oinp
->inp_socket
, 1, 0);
766 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
767 otp
->t_state
== TCPS_TIME_WAIT
&&
768 otp
->t_starttime
< tcp_msl
&&
769 (otp
->t_flags
& TF_RCVD_CC
))
770 otp
= tcp_close(otp
);
772 printf("tcp_connect: inp=%x err=EADDRINUSE\n", inp
);
774 tcp_unlock(oinp
->inp_socket
, 1, 0);
778 tcp_unlock(oinp
->inp_socket
, 1, 0);
781 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
782 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
783 inp
->inp_lport
== sin
->sin_port
)
785 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
786 /*lock inversion issue, mostly with udp multicast packets */
787 socket_unlock(inp
->inp_socket
, 0);
788 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
789 socket_lock(inp
->inp_socket
, 0);
791 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
792 inp
->inp_laddr
= ifaddr
->sin_addr
;
793 inp
->inp_faddr
= sin
->sin_addr
;
794 inp
->inp_fport
= sin
->sin_port
;
796 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
798 /* Compute window scaling to request. */
799 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
800 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
801 tp
->request_r_scale
++;
804 tcpstat
.tcps_connattempt
++;
805 tp
->t_state
= TCPS_SYN_SENT
;
806 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
807 tp
->iss
= tcp_new_isn(tp
);
811 * Generate a CC value for this connection and
812 * check whether CC or CCnew should be used.
814 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
815 taop
= &tao_noncached
;
816 bzero(taop
, sizeof(*taop
));
819 tp
->cc_send
= CC_INC(tcp_ccgen
);
820 if (taop
->tao_ccsent
!= 0 &&
821 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
822 taop
->tao_ccsent
= tp
->cc_send
;
824 taop
->tao_ccsent
= 0;
825 tp
->t_flags
|= TF_SENDCCNEW
;
833 tcp6_connect(tp
, nam
, p
)
834 register struct tcpcb
*tp
;
835 struct sockaddr
*nam
;
838 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
839 struct socket
*so
= inp
->inp_socket
;
841 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
842 struct in6_addr addr6
;
843 struct rmxp_tao
*taop
;
844 struct rmxp_tao tao_noncached
;
847 if (inp
->inp_lport
== 0) {
848 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
854 * Cannot simply call in_pcbconnect, because there might be an
855 * earlier incarnation of this same connection still in
856 * TIME_WAIT state, creating an ADDRINUSE error.
858 error
= in6_pcbladdr(inp
, nam
, &addr6
);
861 tcp_unlock(inp
->inp_socket
, 0, 0);
862 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
863 &sin6
->sin6_addr
, sin6
->sin6_port
,
864 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
867 inp
->inp_lport
, 0, NULL
);
868 tcp_lock(inp
->inp_socket
, 0, 0);
870 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
871 otp
->t_state
== TCPS_TIME_WAIT
&&
872 otp
->t_starttime
< tcp_msl
&&
873 (otp
->t_flags
& TF_RCVD_CC
))
874 otp
= tcp_close(otp
);
878 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
879 /*lock inversion issue, mostly with udp multicast packets */
880 socket_unlock(inp
->inp_socket
, 0);
881 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
882 socket_lock(inp
->inp_socket
, 0);
884 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
885 inp
->in6p_laddr
= addr6
;
886 inp
->in6p_faddr
= sin6
->sin6_addr
;
887 inp
->inp_fport
= sin6
->sin6_port
;
888 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != NULL
)
889 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
891 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
893 /* Compute window scaling to request. */
894 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
895 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
896 tp
->request_r_scale
++;
899 tcpstat
.tcps_connattempt
++;
900 tp
->t_state
= TCPS_SYN_SENT
;
901 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
902 tp
->iss
= tcp_new_isn(tp
);
906 * Generate a CC value for this connection and
907 * check whether CC or CCnew should be used.
909 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
910 taop
= &tao_noncached
;
911 bzero(taop
, sizeof(*taop
));
914 tp
->cc_send
= CC_INC(tcp_ccgen
);
915 if (taop
->tao_ccsent
!= 0 &&
916 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
917 taop
->tao_ccsent
= tp
->cc_send
;
919 taop
->tao_ccsent
= 0;
920 tp
->t_flags
|= TF_SENDCCNEW
;
928 * The new sockopt interface makes it possible for us to block in the
929 * copyin/out step (if we take a page fault). Taking a page fault at
930 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
931 * use TSM, there probably isn't any need for this function to run at
932 * splnet() any more. This needs more examination.)
935 tcp_ctloutput(so
, sopt
)
937 struct sockopt
*sopt
;
939 int error
, opt
, optval
;
948 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
950 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
951 error
= ip6_ctloutput(so
, sopt
);
954 error
= ip_ctloutput(so
, sopt
);
962 switch (sopt
->sopt_dir
) {
964 switch (sopt
->sopt_name
) {
968 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
973 switch (sopt
->sopt_name
) {
984 opt
= 0; /* dead code to fool gcc */
995 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1000 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
1001 optval
+ 40 >= tcp_minmss
)
1002 tp
->t_maxseg
= optval
;
1008 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1015 tp
->t_keepidle
= optval
* PR_SLOWHZ
;
1019 error
= ENOPROTOOPT
;
1025 switch (sopt
->sopt_name
) {
1027 optval
= tp
->t_flags
& TF_NODELAY
;
1030 optval
= tp
->t_maxseg
;
1033 optval
= tp
->t_keepidle
/ PR_SLOWHZ
;
1036 optval
= tp
->t_flags
& TF_NOOPT
;
1039 optval
= tp
->t_flags
& TF_NOPUSH
;
1042 error
= ENOPROTOOPT
;
1046 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1053 * tcp_sendspace and tcp_recvspace are the default send and receive window
1054 * sizes, respectively. These are obsolescent (this information should
1055 * be set by the route).
1057 u_long tcp_sendspace
= 1024*16;
1058 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1059 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1060 u_long tcp_recvspace
= 1024*16;
1061 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1062 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1064 __private_extern__
int tcp_sockthreshold
= 256;
1065 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sockthreshold
, CTLFLAG_RW
,
1066 &tcp_sockthreshold
, 0, "TCP Socket size increased if less than threshold");
1068 #define TCP_INCREASED_SPACE 65535 /* Automatically increase tcp send/rcv space to this value */
1070 * Attach TCP protocol to socket, allocating
1071 * internet protocol control block, tcp control block,
1072 * bufer space, and entering LISTEN state if to accept connections.
1079 register struct tcpcb
*tp
;
1083 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != NULL
;
1086 error
= in_pcballoc(so
, &tcbinfo
, p
);
1090 inp
= sotoinpcb(so
);
1092 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1094 * The goal is to let clients have large send/rcv default windows (TCP_INCREASED_SPACE)
1095 * while not hogging mbuf space for servers. This is done by watching a threshold
1096 * of tcpcbs in use and bumping the default send and rcvspace only if under that threshold.
1097 * The theory being that busy servers have a lot more active tcpcbs and don't want the potential
1098 * memory penalty of having much larger sockbuffs. The sysctl allows to fine tune that threshold value. */
1100 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)
1101 error
= soreserve(so
, MAX(TCP_INCREASED_SPACE
, tcp_sendspace
), MAX(TCP_INCREASED_SPACE
,tcp_recvspace
));
1103 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1110 inp
->inp_vflag
|= INP_IPV6
;
1111 inp
->in6p_hops
= -1; /* use kernel default */
1115 inp
->inp_vflag
|= INP_IPV4
;
1116 tp
= tcp_newtcpcb(inp
);
1118 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1120 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1127 so
->so_state
|= nofd
;
1130 tp
->t_state
= TCPS_CLOSED
;
1135 * Initiate (or continue) disconnect.
1136 * If embryonic state, just send reset (once).
1137 * If in ``let data drain'' option and linger null, just drop.
1138 * Otherwise (hard), mark socket disconnecting and drop
1139 * current input data; switch states based on user close, and
1140 * send segment to peer (with FIN).
1142 static struct tcpcb
*
1144 register struct tcpcb
*tp
;
1146 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1148 if (tp
->t_state
< TCPS_ESTABLISHED
)
1150 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1151 tp
= tcp_drop(tp
, 0);
1153 soisdisconnecting(so
);
1154 sbflush(&so
->so_rcv
);
1155 tp
= tcp_usrclosed(tp
);
1157 (void) tcp_output(tp
);
1163 * User issued close, and wish to trail through shutdown states:
1164 * if never received SYN, just forget it. If got a SYN from peer,
1165 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1166 * If already got a FIN from peer, then almost done; go to LAST_ACK
1167 * state. In all other cases, have already sent FIN to peer (e.g.
1168 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1169 * for peer to send FIN or not respond to keep-alives, etc.
1170 * We can let the user exit from the close as soon as the FIN is acked.
1172 static struct tcpcb
*
1174 register struct tcpcb
*tp
;
1177 switch (tp
->t_state
) {
1181 tp
->t_state
= TCPS_CLOSED
;
1186 case TCPS_SYN_RECEIVED
:
1187 tp
->t_flags
|= TF_NEEDFIN
;
1190 case TCPS_ESTABLISHED
:
1191 tp
->t_state
= TCPS_FIN_WAIT_1
;
1194 case TCPS_CLOSE_WAIT
:
1195 tp
->t_state
= TCPS_LAST_ACK
;
1198 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1199 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1200 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1201 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1202 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;