]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
e14fc8ca8551c9e068071eeb556b051c2f9a971d
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1986, 1988, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
58 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/sysctl.h>
68 #include <sys/domain.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/protosw.h>
75 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
80 #include <netinet/ip6.h>
82 #include <netinet/in_pcb.h>
84 #include <netinet6/in6_pcb.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip_var.h>
89 #include <netinet6/ip6_var.h>
91 #include <netinet/tcp.h>
92 #include <netinet/tcp_fsm.h>
93 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_timer.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/tcpip.h>
98 #include <netinet/tcp_debug.h>
102 #include <netinet6/ipsec.h>
106 * TCP protocol interface to socket abstraction.
108 extern char *tcpstates
[]; /* XXX ??? */
110 static int tcp_attach
__P((struct socket
*, struct proc
*));
111 static int tcp_connect
__P((struct tcpcb
*, struct sockaddr
*,
114 static int tcp6_connect
__P((struct tcpcb
*, struct sockaddr
*,
117 static struct tcpcb
*
118 tcp_disconnect
__P((struct tcpcb
*));
119 static struct tcpcb
*
120 tcp_usrclosed
__P((struct tcpcb
*));
123 #define TCPDEBUG0 int ostate = 0
124 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
125 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
126 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
130 #define TCPDEBUG2(req)
134 * TCP attaches to socket via pru_attach(), reserving space,
135 * and an internet control block.
138 tcp_usr_attach(struct socket
*so
, int proto
, struct proc
*p
)
142 struct inpcb
*inp
= sotoinpcb(so
);
143 struct tcpcb
*tp
= 0;
152 error
= tcp_attach(so
, p
);
156 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
157 so
->so_linger
= TCP_LINGERTIME
* hz
;
160 TCPDEBUG2(PRU_ATTACH
);
166 * pru_detach() detaches the TCP protocol from the socket.
167 * If the protocol state is non-embryonic, then can't
168 * do this directly: have to initiate a pru_disconnect(),
169 * which may finish later; embryonic TCB's can just
173 tcp_usr_detach(struct socket
*so
)
177 struct inpcb
*inp
= sotoinpcb(so
);
183 return EINVAL
; /* XXX */
186 /* In case we got disconnected from the peer */
190 tp
= tcp_disconnect(tp
);
192 TCPDEBUG2(PRU_DETACH
);
197 #define COMMON_START() TCPDEBUG0; \
203 tp = intotcpcb(inp); \
207 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
211 * Give the socket an address.
214 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
218 struct inpcb
*inp
= sotoinpcb(so
);
220 struct sockaddr_in
*sinp
;
225 * Must check for multicast addresses and disallow binding
228 sinp
= (struct sockaddr_in
*)nam
;
229 if (sinp
->sin_family
== AF_INET
&&
230 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
231 error
= EAFNOSUPPORT
;
234 error
= in_pcbbind(inp
, nam
, p
);
237 COMMON_END(PRU_BIND
);
243 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
247 struct inpcb
*inp
= sotoinpcb(so
);
249 struct sockaddr_in6
*sin6p
;
254 * Must check for multicast addresses and disallow binding
257 sin6p
= (struct sockaddr_in6
*)nam
;
258 if (sin6p
->sin6_family
== AF_INET6
&&
259 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
260 error
= EAFNOSUPPORT
;
263 inp
->inp_vflag
&= ~INP_IPV4
;
264 inp
->inp_vflag
|= INP_IPV6
;
265 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
266 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
267 inp
->inp_vflag
|= INP_IPV4
;
268 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
269 struct sockaddr_in sin
;
271 in6_sin6_2_sin(&sin
, sin6p
);
272 inp
->inp_vflag
|= INP_IPV4
;
273 inp
->inp_vflag
&= ~INP_IPV6
;
274 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
278 error
= in6_pcbbind(inp
, nam
, p
);
281 COMMON_END(PRU_BIND
);
286 * Prepare to accept connections.
289 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
293 struct inpcb
*inp
= sotoinpcb(so
);
297 if (inp
->inp_lport
== 0)
298 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
300 tp
->t_state
= TCPS_LISTEN
;
301 COMMON_END(PRU_LISTEN
);
306 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
310 struct inpcb
*inp
= sotoinpcb(so
);
314 if (inp
->inp_lport
== 0) {
315 inp
->inp_vflag
&= ~INP_IPV4
;
316 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
317 inp
->inp_vflag
|= INP_IPV4
;
318 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
321 tp
->t_state
= TCPS_LISTEN
;
322 COMMON_END(PRU_LISTEN
);
327 * Initiate connection to peer.
328 * Create a template for use in transmissions on this connection.
329 * Enter SYN_SENT state, and mark socket as connecting.
330 * Start keep-alive timer, and seed output sequence space.
331 * Send initial segment on connection.
334 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
338 struct inpcb
*inp
= sotoinpcb(so
);
340 struct sockaddr_in
*sinp
;
345 * Must disallow TCP ``connections'' to multicast addresses.
347 sinp
= (struct sockaddr_in
*)nam
;
348 if (sinp
->sin_family
== AF_INET
349 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
350 error
= EAFNOSUPPORT
;
355 prison_remote_ip(p
, 0, &sinp
->sin_addr
.s_addr
);
358 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
360 error
= tcp_output(tp
);
361 COMMON_END(PRU_CONNECT
);
366 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
370 struct inpcb
*inp
= sotoinpcb(so
);
372 struct sockaddr_in6
*sin6p
;
377 * Must disallow TCP ``connections'' to multicast addresses.
379 sin6p
= (struct sockaddr_in6
*)nam
;
380 if (sin6p
->sin6_family
== AF_INET6
381 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
382 error
= EAFNOSUPPORT
;
386 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
387 struct sockaddr_in sin
;
389 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) != 0)
392 in6_sin6_2_sin(&sin
, sin6p
);
393 inp
->inp_vflag
|= INP_IPV4
;
394 inp
->inp_vflag
&= ~INP_IPV6
;
395 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
397 error
= tcp_output(tp
);
400 inp
->inp_vflag
&= ~INP_IPV4
;
401 inp
->inp_vflag
|= INP_IPV6
;
402 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
404 error
= tcp_output(tp
);
407 COMMON_END(PRU_CONNECT
);
412 * Initiate disconnect from peer.
413 * If connection never passed embryonic stage, just drop;
414 * else if don't need to let data drain, then can just drop anyways,
415 * else have to begin TCP shutdown process: mark socket disconnecting,
416 * drain unread data, state switch to reflect user close, and
417 * send segment (e.g. FIN) to peer. Socket will be really disconnected
418 * when peer sends FIN and acks ours.
420 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
423 tcp_usr_disconnect(struct socket
*so
)
427 struct inpcb
*inp
= sotoinpcb(so
);
431 /* In case we got disconnected from the peer */
434 tp
= tcp_disconnect(tp
);
435 COMMON_END(PRU_DISCONNECT
);
439 * Accept a connection. Essentially all the work is
440 * done at higher levels; just return the address
441 * of the peer, storing through addr.
444 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
448 struct inpcb
*inp
= sotoinpcb(so
);
449 struct tcpcb
*tp
= NULL
;
452 if (so
->so_state
& SS_ISDISCONNECTED
) {
453 error
= ECONNABORTED
;
462 in_setpeeraddr(so
, nam
);
463 COMMON_END(PRU_ACCEPT
);
468 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
472 struct inpcb
*inp
= sotoinpcb(so
);
473 struct tcpcb
*tp
= NULL
;
476 if (so
->so_state
& SS_ISDISCONNECTED
) {
477 error
= ECONNABORTED
;
486 in6_mapped_peeraddr(so
, nam
);
487 COMMON_END(PRU_ACCEPT
);
491 * Mark the connection as being incapable of further output.
494 tcp_usr_shutdown(struct socket
*so
)
498 struct inpcb
*inp
= sotoinpcb(so
);
503 /* In case we got disconnected from the peer */
506 tp
= tcp_usrclosed(tp
);
508 error
= tcp_output(tp
);
509 COMMON_END(PRU_SHUTDOWN
);
513 * After a receive, possibly send window update to peer.
516 tcp_usr_rcvd(struct socket
*so
, int flags
)
520 struct inpcb
*inp
= sotoinpcb(so
);
524 /* In case we got disconnected from the peer */
528 COMMON_END(PRU_RCVD
);
532 * Do a send by putting data in output queue and updating urgent
533 * marker if URG set. Possibly send more data. Unlike the other
534 * pru_*() routines, the mbuf chains are our responsibility. We
535 * must either enqueue them or free them. The other pru_* routines
536 * generally are caller-frees.
539 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
540 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
544 struct inpcb
*inp
= sotoinpcb(so
);
553 * OOPS! we lost a race, the TCP session got reset after
554 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
555 * network interrupt in the non-splnet() section of sosend().
561 error
= ECONNRESET
; /* XXX EPIPE? */
567 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
572 /* TCP doesn't do control messages (rights, creds, etc) */
573 if (control
->m_len
) {
580 m_freem(control
); /* empty control, just free it */
582 if(!(flags
& PRUS_OOB
)) {
583 sbappend(&so
->so_snd
, m
);
584 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
586 * Do implied connect if not yet connected,
587 * initialize window to default value, and
588 * initialize maxseg/maxopd using peer's cached
593 error
= tcp6_connect(tp
, nam
, p
);
596 error
= tcp_connect(tp
, nam
, p
);
599 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
603 if (flags
& PRUS_EOF
) {
605 * Close the send side of the connection after
609 tp
= tcp_usrclosed(tp
);
612 if (flags
& PRUS_MORETOCOME
)
613 tp
->t_flags
|= TF_MORETOCOME
;
614 error
= tcp_output(tp
);
615 if (flags
& PRUS_MORETOCOME
)
616 tp
->t_flags
&= ~TF_MORETOCOME
;
619 if (sbspace(&so
->so_snd
) < -512) {
625 * According to RFC961 (Assigned Protocols),
626 * the urgent pointer points to the last octet
627 * of urgent data. We continue, however,
628 * to consider it to indicate the first octet
629 * of data past the urgent section.
630 * Otherwise, snd_up should be one lower.
632 sbappend(&so
->so_snd
, m
);
633 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
635 * Do implied connect if not yet connected,
636 * initialize window to default value, and
637 * initialize maxseg/maxopd using peer's cached
642 error
= tcp6_connect(tp
, nam
, p
);
645 error
= tcp_connect(tp
, nam
, p
);
648 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
651 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
653 error
= tcp_output(tp
);
656 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
657 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
664 tcp_usr_abort(struct socket
*so
)
668 struct inpcb
*inp
= sotoinpcb(so
);
672 /* In case we got disconnected from the peer */
675 tp
= tcp_drop(tp
, ECONNABORTED
);
676 COMMON_END(PRU_ABORT
);
680 * Receive out-of-band data.
683 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
687 struct inpcb
*inp
= sotoinpcb(so
);
691 if ((so
->so_oobmark
== 0 &&
692 (so
->so_state
& SS_RCVATMARK
) == 0) ||
693 so
->so_options
& SO_OOBINLINE
||
694 tp
->t_oobflags
& TCPOOB_HADDATA
) {
698 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
703 *mtod(m
, caddr_t
) = tp
->t_iobc
;
704 if ((flags
& MSG_PEEK
) == 0)
705 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
706 COMMON_END(PRU_RCVOOB
);
709 /* xxx - should be const */
710 struct pr_usrreqs tcp_usrreqs
= {
711 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
712 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
713 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
714 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
715 in_setsockaddr
, sosend
, soreceive
, sopoll
719 struct pr_usrreqs tcp6_usrreqs
= {
720 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
721 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
722 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
723 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
724 in6_mapped_sockaddr
, sosend
, soreceive
, sopoll
729 * Common subroutine to open a TCP connection to remote host specified
730 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
731 * port number if needed. Call in_pcbladdr to do the routing and to choose
732 * a local host address (interface). If there is an existing incarnation
733 * of the same connection in TIME-WAIT state and if the remote host was
734 * sending CC options and if the connection duration was < MSL, then
735 * truncate the previous TIME-WAIT state and proceed.
736 * Initialize connection parameters and enter SYN-SENT state.
739 tcp_connect(tp
, nam
, p
)
740 register struct tcpcb
*tp
;
741 struct sockaddr
*nam
;
744 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
745 struct socket
*so
= inp
->inp_socket
;
747 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
748 struct sockaddr_in
*ifaddr
;
749 struct rmxp_tao
*taop
;
750 struct rmxp_tao tao_noncached
;
753 if (inp
->inp_lport
== 0) {
754 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
760 * Cannot simply call in_pcbconnect, because there might be an
761 * earlier incarnation of this same connection still in
762 * TIME_WAIT state, creating an ADDRINUSE error.
764 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
767 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
768 sin
->sin_addr
, sin
->sin_port
,
769 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
771 inp
->inp_lport
, 0, NULL
);
773 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
774 otp
->t_state
== TCPS_TIME_WAIT
&&
775 otp
->t_starttime
< tcp_msl
&&
776 (otp
->t_flags
& TF_RCVD_CC
))
777 otp
= tcp_close(otp
);
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 (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
786 inp
->inp_laddr
= ifaddr
->sin_addr
;
787 inp
->inp_faddr
= sin
->sin_addr
;
788 inp
->inp_fport
= sin
->sin_port
;
791 /* Compute window scaling to request. */
792 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
793 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
794 tp
->request_r_scale
++;
797 tcpstat
.tcps_connattempt
++;
798 tp
->t_state
= TCPS_SYN_SENT
;
799 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
800 tp
->iss
= tcp_new_isn(tp
);
804 * Generate a CC value for this connection and
805 * check whether CC or CCnew should be used.
807 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
808 taop
= &tao_noncached
;
809 bzero(taop
, sizeof(*taop
));
812 tp
->cc_send
= CC_INC(tcp_ccgen
);
813 if (taop
->tao_ccsent
!= 0 &&
814 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
815 taop
->tao_ccsent
= tp
->cc_send
;
817 taop
->tao_ccsent
= 0;
818 tp
->t_flags
|= TF_SENDCCNEW
;
826 tcp6_connect(tp
, nam
, p
)
827 register struct tcpcb
*tp
;
828 struct sockaddr
*nam
;
831 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
832 struct socket
*so
= inp
->inp_socket
;
834 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
835 struct in6_addr
*addr6
;
836 struct rmxp_tao
*taop
;
837 struct rmxp_tao tao_noncached
;
840 if (inp
->inp_lport
== 0) {
841 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
847 * Cannot simply call in_pcbconnect, because there might be an
848 * earlier incarnation of this same connection still in
849 * TIME_WAIT state, creating an ADDRINUSE error.
851 error
= in6_pcbladdr(inp
, nam
, &addr6
);
854 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
855 &sin6
->sin6_addr
, sin6
->sin6_port
,
856 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
859 inp
->inp_lport
, 0, NULL
);
861 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
862 otp
->t_state
== TCPS_TIME_WAIT
&&
863 otp
->t_starttime
< tcp_msl
&&
864 (otp
->t_flags
& TF_RCVD_CC
))
865 otp
= tcp_close(otp
);
869 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
870 inp
->in6p_laddr
= *addr6
;
871 inp
->in6p_faddr
= sin6
->sin6_addr
;
872 inp
->inp_fport
= sin6
->sin6_port
;
873 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != NULL
)
874 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
877 /* Compute window scaling to request. */
878 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
879 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
880 tp
->request_r_scale
++;
883 tcpstat
.tcps_connattempt
++;
884 tp
->t_state
= TCPS_SYN_SENT
;
885 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
886 tp
->iss
= tcp_new_isn(tp
);
890 * Generate a CC value for this connection and
891 * check whether CC or CCnew should be used.
893 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
894 taop
= &tao_noncached
;
895 bzero(taop
, sizeof(*taop
));
898 tp
->cc_send
= CC_INC(tcp_ccgen
);
899 if (taop
->tao_ccsent
!= 0 &&
900 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
901 taop
->tao_ccsent
= tp
->cc_send
;
903 taop
->tao_ccsent
= 0;
904 tp
->t_flags
|= TF_SENDCCNEW
;
912 * The new sockopt interface makes it possible for us to block in the
913 * copyin/out step (if we take a page fault). Taking a page fault at
914 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
915 * use TSM, there probably isn't any need for this function to run at
916 * splnet() any more. This needs more examination.)
919 tcp_ctloutput(so
, sopt
)
921 struct sockopt
*sopt
;
923 int error
, opt
, optval
, s
;
928 s
= splnet(); /* XXX */
934 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
936 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
937 error
= ip6_ctloutput(so
, sopt
);
940 error
= ip_ctloutput(so
, sopt
);
950 switch (sopt
->sopt_dir
) {
952 switch (sopt
->sopt_name
) {
956 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
961 switch (sopt
->sopt_name
) {
972 opt
= 0; /* dead code to fool gcc */
983 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
988 if (optval
> 0 && optval
<= tp
->t_maxseg
)
989 tp
->t_maxseg
= optval
;
995 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1002 tp
->t_keepidle
= optval
* PR_SLOWHZ
;
1006 error
= ENOPROTOOPT
;
1012 switch (sopt
->sopt_name
) {
1014 optval
= tp
->t_flags
& TF_NODELAY
;
1017 optval
= tp
->t_maxseg
;
1020 optval
= tp
->t_keepidle
/ PR_SLOWHZ
;
1023 optval
= tp
->t_flags
& TF_NOOPT
;
1026 optval
= tp
->t_flags
& TF_NOPUSH
;
1029 error
= ENOPROTOOPT
;
1033 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1041 * tcp_sendspace and tcp_recvspace are the default send and receive window
1042 * sizes, respectively. These are obsolescent (this information should
1043 * be set by the route).
1045 u_long tcp_sendspace
= 1024*16;
1046 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1047 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1048 u_long tcp_recvspace
= 1024*16;
1049 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1050 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1052 __private_extern__
int tcp_sockthreshold
= 256;
1053 SYSCTL_INT(_net_inet_tcp
, OID_AUTO
, sockthreshold
, CTLFLAG_RW
,
1054 &tcp_sockthreshold
, 0, "TCP Socket size increased if less than threshold");
1056 #define TCP_INCREASED_SPACE 65535 /* Automatically increase tcp send/rcv space to this value */
1058 * Attach TCP protocol to socket, allocating
1059 * internet protocol control block, tcp control block,
1060 * bufer space, and entering LISTEN state if to accept connections.
1067 register struct tcpcb
*tp
;
1071 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != NULL
;
1074 error
= in_pcballoc(so
, &tcbinfo
, p
);
1078 inp
= sotoinpcb(so
);
1080 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1082 * The goal is to let clients have large send/rcv default windows (TCP_INCREASED_SPACE)
1083 * while not hogging mbuf space for servers. This is done by watching a threshold
1084 * of tcpcbs in use and bumping the default send and rcvspace only if under that threshold.
1085 * The theory being that busy servers have a lot more active tcpcbs and don't want the potential
1086 * memory penalty of having much larger sockbuffs. The sysctl allows to fine tune that threshold value. */
1088 if (inp
->inp_pcbinfo
->ipi_count
< tcp_sockthreshold
)
1089 error
= soreserve(so
, MAX(TCP_INCREASED_SPACE
, tcp_sendspace
), MAX(TCP_INCREASED_SPACE
,tcp_recvspace
));
1091 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1098 inp
->inp_vflag
|= INP_IPV6
;
1099 inp
->in6p_hops
= -1; /* use kernel default */
1103 inp
->inp_vflag
|= INP_IPV4
;
1104 tp
= tcp_newtcpcb(inp
);
1106 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1108 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1115 so
->so_state
|= nofd
;
1118 tp
->t_state
= TCPS_CLOSED
;
1123 * Initiate (or continue) disconnect.
1124 * If embryonic state, just send reset (once).
1125 * If in ``let data drain'' option and linger null, just drop.
1126 * Otherwise (hard), mark socket disconnecting and drop
1127 * current input data; switch states based on user close, and
1128 * send segment to peer (with FIN).
1130 static struct tcpcb
*
1132 register struct tcpcb
*tp
;
1134 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1136 if (tp
->t_state
< TCPS_ESTABLISHED
)
1138 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1139 tp
= tcp_drop(tp
, 0);
1141 soisdisconnecting(so
);
1142 sbflush(&so
->so_rcv
);
1143 tp
= tcp_usrclosed(tp
);
1145 (void) tcp_output(tp
);
1151 * User issued close, and wish to trail through shutdown states:
1152 * if never received SYN, just forget it. If got a SYN from peer,
1153 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1154 * If already got a FIN from peer, then almost done; go to LAST_ACK
1155 * state. In all other cases, have already sent FIN to peer (e.g.
1156 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1157 * for peer to send FIN or not respond to keep-alives, etc.
1158 * We can let the user exit from the close as soon as the FIN is acked.
1160 static struct tcpcb
*
1162 register struct tcpcb
*tp
;
1165 switch (tp
->t_state
) {
1169 tp
->t_state
= TCPS_CLOSED
;
1174 case TCPS_SYN_RECEIVED
:
1175 tp
->t_flags
|= TF_NEEDFIN
;
1178 case TCPS_ESTABLISHED
:
1179 tp
->t_state
= TCPS_FIN_WAIT_1
;
1182 case TCPS_CLOSE_WAIT
:
1183 tp
->t_state
= TCPS_LAST_ACK
;
1186 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1187 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1188 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1189 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1190 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;