]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
d29331e289be53d6734058c868803e3b5ca86e7d
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 tcp_lock(oinp
->inp_socket
, 1, 0);
758 if (in_pcb_checkstate(oinp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
759 tcp_unlock(oinp
->inp_socket
, 1, 0);
763 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
764 otp
->t_state
== TCPS_TIME_WAIT
&&
765 otp
->t_starttime
< tcp_msl
&&
766 (otp
->t_flags
& TF_RCVD_CC
))
767 otp
= tcp_close(otp
);
769 printf("tcp_connect: inp=%x err=EADDRINUSE\n", inp
);
770 tcp_unlock(oinp
->inp_socket
, 1, 0);
773 tcp_unlock(oinp
->inp_socket
, 1, 0);
776 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
777 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
778 inp
->inp_lport
== sin
->sin_port
)
780 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
781 /*lock inversion issue, mostly with udp multicast packets */
782 socket_unlock(inp
->inp_socket
, 0);
783 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
784 socket_lock(inp
->inp_socket
, 0);
786 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
787 inp
->inp_laddr
= ifaddr
->sin_addr
;
788 inp
->inp_faddr
= sin
->sin_addr
;
789 inp
->inp_fport
= sin
->sin_port
;
791 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
793 /* Compute window scaling to request. */
794 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
795 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
796 tp
->request_r_scale
++;
799 tcpstat
.tcps_connattempt
++;
800 tp
->t_state
= TCPS_SYN_SENT
;
801 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
802 tp
->iss
= tcp_new_isn(tp
);
806 * Generate a CC value for this connection and
807 * check whether CC or CCnew should be used.
809 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
810 taop
= &tao_noncached
;
811 bzero(taop
, sizeof(*taop
));
814 tp
->cc_send
= CC_INC(tcp_ccgen
);
815 if (taop
->tao_ccsent
!= 0 &&
816 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
817 taop
->tao_ccsent
= tp
->cc_send
;
819 taop
->tao_ccsent
= 0;
820 tp
->t_flags
|= TF_SENDCCNEW
;
828 tcp6_connect(tp
, nam
, p
)
829 register struct tcpcb
*tp
;
830 struct sockaddr
*nam
;
833 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
834 struct socket
*so
= inp
->inp_socket
;
836 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
837 struct in6_addr addr6
;
838 struct rmxp_tao
*taop
;
839 struct rmxp_tao tao_noncached
;
842 if (inp
->inp_lport
== 0) {
843 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
849 * Cannot simply call in_pcbconnect, because there might be an
850 * earlier incarnation of this same connection still in
851 * TIME_WAIT state, creating an ADDRINUSE error.
853 error
= in6_pcbladdr(inp
, nam
, &addr6
);
856 tcp_unlock(inp
->inp_socket
, 0, 0);
857 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
858 &sin6
->sin6_addr
, sin6
->sin6_port
,
859 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
862 inp
->inp_lport
, 0, NULL
);
863 tcp_lock(inp
->inp_socket
, 0, 0);
865 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
866 otp
->t_state
== TCPS_TIME_WAIT
&&
867 otp
->t_starttime
< tcp_msl
&&
868 (otp
->t_flags
& TF_RCVD_CC
))
869 otp
= tcp_close(otp
);
873 if (!lck_rw_try_lock_exclusive(inp
->inp_pcbinfo
->mtx
)) {
874 /*lock inversion issue, mostly with udp multicast packets */
875 socket_unlock(inp
->inp_socket
, 0);
876 lck_rw_lock_exclusive(inp
->inp_pcbinfo
->mtx
);
877 socket_lock(inp
->inp_socket
, 0);
879 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
880 inp
->in6p_laddr
= addr6
;
881 inp
->in6p_faddr
= sin6
->sin6_addr
;
882 inp
->inp_fport
= sin6
->sin6_port
;
883 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != NULL
)
884 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
886 lck_rw_done(inp
->inp_pcbinfo
->mtx
);
888 /* Compute window scaling to request. */
889 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
890 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
891 tp
->request_r_scale
++;
894 tcpstat
.tcps_connattempt
++;
895 tp
->t_state
= TCPS_SYN_SENT
;
896 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
897 tp
->iss
= tcp_new_isn(tp
);
901 * Generate a CC value for this connection and
902 * check whether CC or CCnew should be used.
904 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
905 taop
= &tao_noncached
;
906 bzero(taop
, sizeof(*taop
));
909 tp
->cc_send
= CC_INC(tcp_ccgen
);
910 if (taop
->tao_ccsent
!= 0 &&
911 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
912 taop
->tao_ccsent
= tp
->cc_send
;
914 taop
->tao_ccsent
= 0;
915 tp
->t_flags
|= TF_SENDCCNEW
;
923 * The new sockopt interface makes it possible for us to block in the
924 * copyin/out step (if we take a page fault). Taking a page fault at
925 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
926 * use TSM, there probably isn't any need for this function to run at
927 * splnet() any more. This needs more examination.)
930 tcp_ctloutput(so
, sopt
)
932 struct sockopt
*sopt
;
934 int error
, opt
, optval
;
943 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
945 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
946 error
= ip6_ctloutput(so
, sopt
);
949 error
= ip_ctloutput(so
, sopt
);
957 switch (sopt
->sopt_dir
) {
959 switch (sopt
->sopt_name
) {
963 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
968 switch (sopt
->sopt_name
) {
979 opt
= 0; /* dead code to fool gcc */
990 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
995 if (optval
> 0 && optval
<= tp
->t_maxseg
&&
996 optval
+ 40 >= tcp_minmss
)
997 tp
->t_maxseg
= optval
;
1003 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1010 tp
->t_keepidle
= optval
* PR_SLOWHZ
;
1014 error
= ENOPROTOOPT
;
1020 switch (sopt
->sopt_name
) {
1022 optval
= tp
->t_flags
& TF_NODELAY
;
1025 optval
= tp
->t_maxseg
;
1028 optval
= tp
->t_keepidle
/ PR_SLOWHZ
;
1031 optval
= tp
->t_flags
& TF_NOOPT
;
1034 optval
= tp
->t_flags
& TF_NOPUSH
;
1037 error
= ENOPROTOOPT
;
1041 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1048 * tcp_sendspace and tcp_recvspace are the default send and receive window
1049 * sizes, respectively. These are obsolescent (this information should
1050 * be set by the route).
1052 u_long tcp_sendspace
= 1024*16;
1053 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1054 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1055 u_long tcp_recvspace
= 1024*16;
1056 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1057 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1059 __private_extern__
int tcp_sockthreshold
= 256;
1060 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sockthreshold
, CTLFLAG_RW
,
1061 &tcp_sockthreshold
, 0, "TCP Socket size increased if less than threshold");
1063 #define TCP_INCREASED_SPACE 65535 /* Automatically increase tcp send/rcv space to this value */
1065 * Attach TCP protocol to socket, allocating
1066 * internet protocol control block, tcp control block,
1067 * bufer space, and entering LISTEN state if to accept connections.
1074 register struct tcpcb
*tp
;
1078 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != NULL
;
1081 error
= in_pcballoc(so
, &tcbinfo
, p
);
1085 inp
= sotoinpcb(so
);
1087 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1089 * The goal is to let clients have large send/rcv default windows (TCP_INCREASED_SPACE)
1090 * while not hogging mbuf space for servers. This is done by watching a threshold
1091 * of tcpcbs in use and bumping the default send and rcvspace only if under that threshold.
1092 * The theory being that busy servers have a lot more active tcpcbs and don't want the potential
1093 * memory penalty of having much larger sockbuffs. The sysctl allows to fine tune that threshold value. */
1095 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)
1096 error
= soreserve(so
, MAX(TCP_INCREASED_SPACE
, tcp_sendspace
), MAX(TCP_INCREASED_SPACE
,tcp_recvspace
));
1098 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1105 inp
->inp_vflag
|= INP_IPV6
;
1106 inp
->in6p_hops
= -1; /* use kernel default */
1110 inp
->inp_vflag
|= INP_IPV4
;
1111 tp
= tcp_newtcpcb(inp
);
1113 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1115 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1122 so
->so_state
|= nofd
;
1125 tp
->t_state
= TCPS_CLOSED
;
1130 * Initiate (or continue) disconnect.
1131 * If embryonic state, just send reset (once).
1132 * If in ``let data drain'' option and linger null, just drop.
1133 * Otherwise (hard), mark socket disconnecting and drop
1134 * current input data; switch states based on user close, and
1135 * send segment to peer (with FIN).
1137 static struct tcpcb
*
1139 register struct tcpcb
*tp
;
1141 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1143 if (tp
->t_state
< TCPS_ESTABLISHED
)
1145 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1146 tp
= tcp_drop(tp
, 0);
1148 soisdisconnecting(so
);
1149 sbflush(&so
->so_rcv
);
1150 tp
= tcp_usrclosed(tp
);
1152 (void) tcp_output(tp
);
1158 * User issued close, and wish to trail through shutdown states:
1159 * if never received SYN, just forget it. If got a SYN from peer,
1160 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1161 * If already got a FIN from peer, then almost done; go to LAST_ACK
1162 * state. In all other cases, have already sent FIN to peer (e.g.
1163 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1164 * for peer to send FIN or not respond to keep-alives, etc.
1165 * We can let the user exit from the close as soon as the FIN is acked.
1167 static struct tcpcb
*
1169 register struct tcpcb
*tp
;
1172 switch (tp
->t_state
) {
1176 tp
->t_state
= TCPS_CLOSED
;
1181 case TCPS_SYN_RECEIVED
:
1182 tp
->t_flags
|= TF_NEEDFIN
;
1185 case TCPS_ESTABLISHED
:
1186 tp
->t_state
= TCPS_FIN_WAIT_1
;
1189 case TCPS_CLOSE_WAIT
:
1190 tp
->t_state
= TCPS_LAST_ACK
;
1193 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1194 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1195 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1196 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1197 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;