]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
55 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/sysctl.h>
65 #include <sys/domain.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/protosw.h>
72 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
77 #include <netinet/ip6.h>
79 #include <netinet/in_pcb.h>
81 #include <netinet6/in6_pcb.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip_var.h>
86 #include <netinet6/ip6_var.h>
88 #include <netinet/tcp.h>
89 #include <netinet/tcp_fsm.h>
90 #include <netinet/tcp_seq.h>
91 #include <netinet/tcp_timer.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/tcpip.h>
95 #include <netinet/tcp_debug.h>
99 #include <netinet6/ipsec.h>
103 * TCP protocol interface to socket abstraction.
105 extern char *tcpstates
[]; /* XXX ??? */
107 static int tcp_attach(struct socket
*, struct proc
*);
108 static int tcp_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
110 static int tcp6_connect(struct tcpcb
*, struct sockaddr
*, struct proc
*);
112 static struct tcpcb
*
113 tcp_disconnect(struct tcpcb
*);
114 static struct tcpcb
*
115 tcp_usrclosed(struct tcpcb
*);
118 #define TCPDEBUG0 int ostate = 0
119 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
120 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
121 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
125 #define TCPDEBUG2(req)
129 * TCP attaches to socket via pru_attach(), reserving space,
130 * and an internet control block.
133 tcp_usr_attach(struct socket
*so
, int proto
, struct proc
*p
)
136 struct inpcb
*inp
= sotoinpcb(so
);
137 struct tcpcb
*tp
= 0;
146 error
= tcp_attach(so
, p
);
150 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
151 so
->so_linger
= TCP_LINGERTIME
* hz
;
154 TCPDEBUG2(PRU_ATTACH
);
159 * pru_detach() detaches the TCP protocol from the socket.
160 * If the protocol state is non-embryonic, then can't
161 * do this directly: have to initiate a pru_disconnect(),
162 * which may finish later; embryonic TCB's can just
166 tcp_usr_detach(struct socket
*so
)
169 struct inpcb
*inp
= sotoinpcb(so
);
173 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
174 return EINVAL
; /* XXX */
177 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
180 /* In case we got disconnected from the peer */
184 tp
= tcp_disconnect(tp
);
186 TCPDEBUG2(PRU_DETACH
);
190 #define COMMON_START() TCPDEBUG0; \
192 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { \
195 tp = intotcpcb(inp); \
199 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
203 * Give the socket an address.
206 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
209 struct inpcb
*inp
= sotoinpcb(so
);
211 struct sockaddr_in
*sinp
;
216 * Must check for multicast addresses and disallow binding
219 sinp
= (struct sockaddr_in
*)nam
;
220 if (sinp
->sin_family
== AF_INET
&&
221 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
222 error
= EAFNOSUPPORT
;
225 error
= in_pcbbind(inp
, nam
, p
);
228 COMMON_END(PRU_BIND
);
234 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
237 struct inpcb
*inp
= sotoinpcb(so
);
239 struct sockaddr_in6
*sin6p
;
244 * Must check for multicast addresses and disallow binding
247 sin6p
= (struct sockaddr_in6
*)nam
;
248 if (sin6p
->sin6_family
== AF_INET6
&&
249 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
250 error
= EAFNOSUPPORT
;
253 inp
->inp_vflag
&= ~INP_IPV4
;
254 inp
->inp_vflag
|= INP_IPV6
;
255 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
256 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
257 inp
->inp_vflag
|= INP_IPV4
;
258 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
259 struct sockaddr_in sin
;
261 in6_sin6_2_sin(&sin
, sin6p
);
262 inp
->inp_vflag
|= INP_IPV4
;
263 inp
->inp_vflag
&= ~INP_IPV6
;
264 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
268 error
= in6_pcbbind(inp
, nam
, p
);
271 COMMON_END(PRU_BIND
);
276 * Prepare to accept connections.
279 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
282 struct inpcb
*inp
= sotoinpcb(so
);
286 if (inp
->inp_lport
== 0)
287 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
289 tp
->t_state
= TCPS_LISTEN
;
290 COMMON_END(PRU_LISTEN
);
295 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
298 struct inpcb
*inp
= sotoinpcb(so
);
302 if (inp
->inp_lport
== 0) {
303 inp
->inp_vflag
&= ~INP_IPV4
;
304 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
305 inp
->inp_vflag
|= INP_IPV4
;
306 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
309 tp
->t_state
= TCPS_LISTEN
;
310 COMMON_END(PRU_LISTEN
);
315 * Initiate connection to peer.
316 * Create a template for use in transmissions on this connection.
317 * Enter SYN_SENT state, and mark socket as connecting.
318 * Start keep-alive timer, and seed output sequence space.
319 * Send initial segment on connection.
322 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
325 struct inpcb
*inp
= sotoinpcb(so
);
327 struct sockaddr_in
*sinp
;
332 * Must disallow TCP ``connections'' to multicast addresses.
334 sinp
= (struct sockaddr_in
*)nam
;
335 if (sinp
->sin_family
== AF_INET
336 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
337 error
= EAFNOSUPPORT
;
342 prison_remote_ip(p
, 0, &sinp
->sin_addr
.s_addr
);
345 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
347 error
= tcp_output(tp
);
348 COMMON_END(PRU_CONNECT
);
353 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
356 struct inpcb
*inp
= sotoinpcb(so
);
358 struct sockaddr_in6
*sin6p
;
363 * Must disallow TCP ``connections'' to multicast addresses.
365 sin6p
= (struct sockaddr_in6
*)nam
;
366 if (sin6p
->sin6_family
== AF_INET6
367 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
368 error
= EAFNOSUPPORT
;
372 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
373 struct sockaddr_in sin
;
375 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
378 in6_sin6_2_sin(&sin
, sin6p
);
379 inp
->inp_vflag
|= INP_IPV4
;
380 inp
->inp_vflag
&= ~INP_IPV6
;
381 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
383 error
= tcp_output(tp
);
386 inp
->inp_vflag
&= ~INP_IPV4
;
387 inp
->inp_vflag
|= INP_IPV6
;
388 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
390 error
= tcp_output(tp
);
393 COMMON_END(PRU_CONNECT
);
398 * Initiate disconnect from peer.
399 * If connection never passed embryonic stage, just drop;
400 * else if don't need to let data drain, then can just drop anyways,
401 * else have to begin TCP shutdown process: mark socket disconnecting,
402 * drain unread data, state switch to reflect user close, and
403 * send segment (e.g. FIN) to peer. Socket will be really disconnected
404 * when peer sends FIN and acks ours.
406 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
409 tcp_usr_disconnect(struct socket
*so
)
412 struct inpcb
*inp
= sotoinpcb(so
);
416 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
419 /* In case we got disconnected from the peer */
422 tp
= tcp_disconnect(tp
);
423 COMMON_END(PRU_DISCONNECT
);
427 * Accept a connection. Essentially all the work is
428 * done at higher levels; just return the address
429 * of the peer, storing through addr.
432 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
435 struct inpcb
*inp
= sotoinpcb(so
);
436 struct tcpcb
*tp
= NULL
;
439 if (so
->so_state
& SS_ISDISCONNECTED
) {
440 error
= ECONNABORTED
;
443 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
448 in_setpeeraddr(so
, nam
);
449 COMMON_END(PRU_ACCEPT
);
454 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
457 struct inpcb
*inp
= sotoinpcb(so
);
458 struct tcpcb
*tp
= NULL
;
461 if (so
->so_state
& SS_ISDISCONNECTED
) {
462 error
= ECONNABORTED
;
465 if (inp
== 0 || (inp
->inp_state
== INPCB_STATE_DEAD
)) {
470 in6_mapped_peeraddr(so
, nam
);
471 COMMON_END(PRU_ACCEPT
);
475 * Mark the connection as being incapable of further output.
478 tcp_usr_shutdown(struct socket
*so
)
481 struct inpcb
*inp
= sotoinpcb(so
);
486 /* In case we got disconnected from the peer */
489 tp
= tcp_usrclosed(tp
);
491 error
= tcp_output(tp
);
492 COMMON_END(PRU_SHUTDOWN
);
496 * After a receive, possibly send window update to peer.
499 tcp_usr_rcvd(struct socket
*so
, int flags
)
502 struct inpcb
*inp
= sotoinpcb(so
);
506 /* In case we got disconnected from the peer */
510 COMMON_END(PRU_RCVD
);
514 * Do a send by putting data in output queue and updating urgent
515 * marker if URG set. Possibly send more data. Unlike the other
516 * pru_*() routines, the mbuf chains are our responsibility. We
517 * must either enqueue them or free them. The other pru_* routines
518 * generally are caller-frees.
521 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
522 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
525 struct inpcb
*inp
= sotoinpcb(so
);
532 if (inp
== NULL
|| inp
->inp_state
== INPCB_STATE_DEAD
) {
534 * OOPS! we lost a race, the TCP session got reset after
535 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
536 * network interrupt in the non-splnet() section of sosend().
542 error
= ECONNRESET
; /* XXX EPIPE? */
548 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
553 /* TCP doesn't do control messages (rights, creds, etc) */
554 if (control
->m_len
) {
561 m_freem(control
); /* empty control, just free it */
563 if(!(flags
& PRUS_OOB
)) {
564 sbappend(&so
->so_snd
, m
);
565 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
567 * Do implied connect if not yet connected,
568 * initialize window to default value, and
569 * initialize maxseg/maxopd using peer's cached
574 error
= tcp6_connect(tp
, nam
, p
);
577 error
= tcp_connect(tp
, nam
, p
);
580 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
584 if (flags
& PRUS_EOF
) {
586 * Close the send side of the connection after
590 tp
= tcp_usrclosed(tp
);
593 if (flags
& PRUS_MORETOCOME
)
594 tp
->t_flags
|= TF_MORETOCOME
;
595 error
= tcp_output(tp
);
596 if (flags
& PRUS_MORETOCOME
)
597 tp
->t_flags
&= ~TF_MORETOCOME
;
600 if (sbspace(&so
->so_snd
) < -512) {
606 * According to RFC961 (Assigned Protocols),
607 * the urgent pointer points to the last octet
608 * of urgent data. We continue, however,
609 * to consider it to indicate the first octet
610 * of data past the urgent section.
611 * Otherwise, snd_up should be one lower.
613 sbappend(&so
->so_snd
, m
);
614 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
616 * Do implied connect if not yet connected,
617 * initialize window to default value, and
618 * initialize maxseg/maxopd using peer's cached
623 error
= tcp6_connect(tp
, nam
, p
);
626 error
= tcp_connect(tp
, nam
, p
);
629 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
632 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
634 error
= tcp_output(tp
);
637 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
638 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
645 tcp_usr_abort(struct socket
*so
)
648 struct inpcb
*inp
= sotoinpcb(so
);
652 /* In case we got disconnected from the peer */
655 tp
= tcp_drop(tp
, ECONNABORTED
);
657 COMMON_END(PRU_ABORT
);
661 * Receive out-of-band data.
664 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
667 struct inpcb
*inp
= sotoinpcb(so
);
671 if ((so
->so_oobmark
== 0 &&
672 (so
->so_state
& SS_RCVATMARK
) == 0) ||
673 so
->so_options
& SO_OOBINLINE
||
674 tp
->t_oobflags
& TCPOOB_HADDATA
) {
678 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
683 *mtod(m
, caddr_t
) = tp
->t_iobc
;
684 if ((flags
& MSG_PEEK
) == 0)
685 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
686 COMMON_END(PRU_RCVOOB
);
689 /* xxx - should be const */
690 struct pr_usrreqs tcp_usrreqs
= {
691 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
692 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
693 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
694 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
695 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
699 struct pr_usrreqs tcp6_usrreqs
= {
700 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
701 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
702 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
703 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
704 in6_mapped_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
709 * Common subroutine to open a TCP connection to remote host specified
710 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
711 * port number if needed. Call in_pcbladdr to do the routing and to choose
712 * a local host address (interface). If there is an existing incarnation
713 * of the same connection in TIME-WAIT state and if the remote host was
714 * sending CC options and if the connection duration was < MSL, then
715 * truncate the previous TIME-WAIT state and proceed.
716 * Initialize connection parameters and enter SYN-SENT state.
719 tcp_connect(tp
, nam
, p
)
720 register struct tcpcb
*tp
;
721 struct sockaddr
*nam
;
724 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
725 struct socket
*so
= inp
->inp_socket
;
727 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
728 struct sockaddr_in
*ifaddr
;
729 struct rmxp_tao
*taop
;
730 struct rmxp_tao tao_noncached
;
733 if (inp
->inp_lport
== 0) {
734 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
740 * Cannot simply call in_pcbconnect, because there might be an
741 * earlier incarnation of this same connection still in
742 * TIME_WAIT state, creating an ADDRINUSE error.
744 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
748 tcp_unlock(inp
->inp_socket
, 0, 0);
749 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
750 sin
->sin_addr
, sin
->sin_port
,
751 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
753 inp
->inp_lport
, 0, NULL
);
755 tcp_lock(inp
->inp_socket
, 0, 0);
757 if (oinp
!= inp
) /* 4143933: avoid deadlock if inp == oinp */
758 tcp_lock(oinp
->inp_socket
, 1, 0);
759 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
761 tcp_unlock(oinp
->inp_socket
, 1, 0);
765 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
766 otp
->t_state
== TCPS_TIME_WAIT
&&
767 otp
->t_starttime
< tcp_msl
&&
768 (otp
->t_flags
& TF_RCVD_CC
))
769 otp
= tcp_close(otp
);
771 printf("tcp_connect: inp=%x err=EADDRINUSE\n", inp
);
773 tcp_unlock(oinp
->inp_socket
, 1, 0);
777 tcp_unlock(oinp
->inp_socket
, 1, 0);
780 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
781 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
782 inp
->inp_lport
== sin
->sin_port
)
784 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
785 /*lock inversion issue, mostly with udp multicast packets */
786 socket_unlock(inp
->inp_socket
, 0);
787 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
788 socket_lock(inp
->inp_socket
, 0);
790 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
791 inp
->inp_laddr
= ifaddr
->sin_addr
;
792 inp
->inp_faddr
= sin
->sin_addr
;
793 inp
->inp_fport
= sin
->sin_port
;
795 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
797 /* Compute window scaling to request. */
798 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
799 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
800 tp
->request_r_scale
++;
803 tcpstat
.tcps_connattempt
++;
804 tp
->t_state
= TCPS_SYN_SENT
;
805 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
806 tp
->iss
= tcp_new_isn(tp
);
810 * Generate a CC value for this connection and
811 * check whether CC or CCnew should be used.
813 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
814 taop
= &tao_noncached
;
815 bzero(taop
, sizeof(*taop
));
818 tp
->cc_send
= CC_INC(tcp_ccgen
);
819 if (taop
->tao_ccsent
!= 0 &&
820 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
821 taop
->tao_ccsent
= tp
->cc_send
;
823 taop
->tao_ccsent
= 0;
824 tp
->t_flags
|= TF_SENDCCNEW
;
832 tcp6_connect(tp
, nam
, p
)
833 register struct tcpcb
*tp
;
834 struct sockaddr
*nam
;
837 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
838 struct socket
*so
= inp
->inp_socket
;
840 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
841 struct in6_addr addr6
;
842 struct rmxp_tao
*taop
;
843 struct rmxp_tao tao_noncached
;
846 if (inp
->inp_lport
== 0) {
847 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
853 * Cannot simply call in_pcbconnect, because there might be an
854 * earlier incarnation of this same connection still in
855 * TIME_WAIT state, creating an ADDRINUSE error.
857 error
= in6_pcbladdr(inp
, nam
, &addr6
);
860 tcp_unlock(inp
->inp_socket
, 0, 0);
861 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
862 &sin6
->sin6_addr
, sin6
->sin6_port
,
863 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
866 inp
->inp_lport
, 0, NULL
);
867 tcp_lock(inp
->inp_socket
, 0, 0);
869 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
870 otp
->t_state
== TCPS_TIME_WAIT
&&
871 otp
->t_starttime
< tcp_msl
&&
872 (otp
->t_flags
& TF_RCVD_CC
))
873 otp
= tcp_close(otp
);
877 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
878 /*lock inversion issue, mostly with udp multicast packets */
879 socket_unlock(inp
->inp_socket
, 0);
880 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
881 socket_lock(inp
->inp_socket
, 0);
883 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
884 inp
->in6p_laddr
= addr6
;
885 inp
->in6p_faddr
= sin6
->sin6_addr
;
886 inp
->inp_fport
= sin6
->sin6_port
;
887 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != NULL
)
888 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
890 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
892 /* Compute window scaling to request. */
893 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
894 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
895 tp
->request_r_scale
++;
898 tcpstat
.tcps_connattempt
++;
899 tp
->t_state
= TCPS_SYN_SENT
;
900 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
901 tp
->iss
= tcp_new_isn(tp
);
905 * Generate a CC value for this connection and
906 * check whether CC or CCnew should be used.
908 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
909 taop
= &tao_noncached
;
910 bzero(taop
, sizeof(*taop
));
913 tp
->cc_send
= CC_INC(tcp_ccgen
);
914 if (taop
->tao_ccsent
!= 0 &&
915 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
916 taop
->tao_ccsent
= tp
->cc_send
;
918 taop
->tao_ccsent
= 0;
919 tp
->t_flags
|= TF_SENDCCNEW
;
927 * The new sockopt interface makes it possible for us to block in the
928 * copyin/out step (if we take a page fault). Taking a page fault at
929 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
930 * use TSM, there probably isn't any need for this function to run at
931 * splnet() any more. This needs more examination.)
934 tcp_ctloutput(so
, sopt
)
936 struct sockopt
*sopt
;
938 int error
, opt
, optval
;
947 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
949 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
950 error
= ip6_ctloutput(so
, sopt
);
953 error
= ip_ctloutput(so
, sopt
);
961 switch (sopt
->sopt_dir
) {
963 switch (sopt
->sopt_name
) {
967 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
972 switch (sopt
->sopt_name
) {
983 opt
= 0; /* dead code to fool gcc */
994 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
999 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
1000 optval
+ 40 >= tcp_minmss
)
1001 tp
->t_maxseg
= optval
;
1007 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1014 tp
->t_keepidle
= optval
* PR_SLOWHZ
;
1018 error
= ENOPROTOOPT
;
1024 switch (sopt
->sopt_name
) {
1026 optval
= tp
->t_flags
& TF_NODELAY
;
1029 optval
= tp
->t_maxseg
;
1032 optval
= tp
->t_keepidle
/ PR_SLOWHZ
;
1035 optval
= tp
->t_flags
& TF_NOOPT
;
1038 optval
= tp
->t_flags
& TF_NOPUSH
;
1041 error
= ENOPROTOOPT
;
1045 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1052 * tcp_sendspace and tcp_recvspace are the default send and receive window
1053 * sizes, respectively. These are obsolescent (this information should
1054 * be set by the route).
1056 u_long tcp_sendspace
= 1024*16;
1057 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1058 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1059 u_long tcp_recvspace
= 1024*16;
1060 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1061 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1063 __private_extern__
int tcp_sockthreshold
= 256;
1064 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sockthreshold
, CTLFLAG_RW
,
1065 &tcp_sockthreshold
, 0, "TCP Socket size increased if less than threshold");
1067 #define TCP_INCREASED_SPACE 65535 /* Automatically increase tcp send/rcv space to this value */
1069 * Attach TCP protocol to socket, allocating
1070 * internet protocol control block, tcp control block,
1071 * bufer space, and entering LISTEN state if to accept connections.
1078 register struct tcpcb
*tp
;
1082 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != NULL
;
1085 error
= in_pcballoc(so
, &tcbinfo
, p
);
1089 inp
= sotoinpcb(so
);
1091 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1093 * The goal is to let clients have large send/rcv default windows (TCP_INCREASED_SPACE)
1094 * while not hogging mbuf space for servers. This is done by watching a threshold
1095 * of tcpcbs in use and bumping the default send and rcvspace only if under that threshold.
1096 * The theory being that busy servers have a lot more active tcpcbs and don't want the potential
1097 * memory penalty of having much larger sockbuffs. The sysctl allows to fine tune that threshold value. */
1099 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)
1100 error
= soreserve(so
, MAX(TCP_INCREASED_SPACE
, tcp_sendspace
), MAX(TCP_INCREASED_SPACE
,tcp_recvspace
));
1102 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1109 inp
->inp_vflag
|= INP_IPV6
;
1110 inp
->in6p_hops
= -1; /* use kernel default */
1114 inp
->inp_vflag
|= INP_IPV4
;
1115 tp
= tcp_newtcpcb(inp
);
1117 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1119 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1126 so
->so_state
|= nofd
;
1129 tp
->t_state
= TCPS_CLOSED
;
1134 * Initiate (or continue) disconnect.
1135 * If embryonic state, just send reset (once).
1136 * If in ``let data drain'' option and linger null, just drop.
1137 * Otherwise (hard), mark socket disconnecting and drop
1138 * current input data; switch states based on user close, and
1139 * send segment to peer (with FIN).
1141 static struct tcpcb
*
1143 register struct tcpcb
*tp
;
1145 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1147 if (tp
->t_state
< TCPS_ESTABLISHED
)
1149 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1150 tp
= tcp_drop(tp
, 0);
1152 soisdisconnecting(so
);
1153 sbflush(&so
->so_rcv
);
1154 tp
= tcp_usrclosed(tp
);
1156 (void) tcp_output(tp
);
1162 * User issued close, and wish to trail through shutdown states:
1163 * if never received SYN, just forget it. If got a SYN from peer,
1164 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1165 * If already got a FIN from peer, then almost done; go to LAST_ACK
1166 * state. In all other cases, have already sent FIN to peer (e.g.
1167 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1168 * for peer to send FIN or not respond to keep-alives, etc.
1169 * We can let the user exit from the close as soon as the FIN is acked.
1171 static struct tcpcb
*
1173 register struct tcpcb
*tp
;
1176 switch (tp
->t_state
) {
1180 tp
->t_state
= TCPS_CLOSED
;
1185 case TCPS_SYN_RECEIVED
:
1186 tp
->t_flags
|= TF_NEEDFIN
;
1189 case TCPS_ESTABLISHED
:
1190 tp
->t_state
= TCPS_FIN_WAIT_1
;
1193 case TCPS_CLOSE_WAIT
:
1194 tp
->t_state
= TCPS_LAST_ACK
;
1197 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1198 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1199 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1200 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1201 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;