]>
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 * 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 (ip6_mapped_addr_on
&& (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 (ip6_mapped_addr_on
&&
317 (inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
318 inp
->inp_vflag
|= INP_IPV4
;
319 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
322 tp
->t_state
= TCPS_LISTEN
;
323 COMMON_END(PRU_LISTEN
);
328 * Initiate connection to peer.
329 * Create a template for use in transmissions on this connection.
330 * Enter SYN_SENT state, and mark socket as connecting.
331 * Start keep-alive timer, and seed output sequence space.
332 * Send initial segment on connection.
335 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
339 struct inpcb
*inp
= sotoinpcb(so
);
341 struct sockaddr_in
*sinp
;
346 * Must disallow TCP ``connections'' to multicast addresses.
348 sinp
= (struct sockaddr_in
*)nam
;
349 if (sinp
->sin_family
== AF_INET
350 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
351 error
= EAFNOSUPPORT
;
356 prison_remote_ip(p
, 0, &sinp
->sin_addr
.s_addr
);
359 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
361 error
= tcp_output(tp
);
362 COMMON_END(PRU_CONNECT
);
367 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
371 struct inpcb
*inp
= sotoinpcb(so
);
373 struct sockaddr_in6
*sin6p
;
378 * Must disallow TCP ``connections'' to multicast addresses.
380 sin6p
= (struct sockaddr_in6
*)nam
;
381 if (sin6p
->sin6_family
== AF_INET6
382 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
383 error
= EAFNOSUPPORT
;
387 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
388 struct sockaddr_in sin
;
390 if (!ip6_mapped_addr_on
||
391 (inp
->inp_flags
& IN6P_IPV6_V6ONLY
))
394 in6_sin6_2_sin(&sin
, sin6p
);
395 inp
->inp_vflag
|= INP_IPV4
;
396 inp
->inp_vflag
&= ~INP_IPV6
;
397 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
399 error
= tcp_output(tp
);
402 inp
->inp_vflag
&= ~INP_IPV4
;
403 inp
->inp_vflag
|= INP_IPV6
;
404 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
406 error
= tcp_output(tp
);
409 COMMON_END(PRU_CONNECT
);
414 * Initiate disconnect from peer.
415 * If connection never passed embryonic stage, just drop;
416 * else if don't need to let data drain, then can just drop anyways,
417 * else have to begin TCP shutdown process: mark socket disconnecting,
418 * drain unread data, state switch to reflect user close, and
419 * send segment (e.g. FIN) to peer. Socket will be really disconnected
420 * when peer sends FIN and acks ours.
422 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
425 tcp_usr_disconnect(struct socket
*so
)
429 struct inpcb
*inp
= sotoinpcb(so
);
433 /* In case we got disconnected from the peer */
436 tp
= tcp_disconnect(tp
);
437 COMMON_END(PRU_DISCONNECT
);
441 * Accept a connection. Essentially all the work is
442 * done at higher levels; just return the address
443 * of the peer, storing through addr.
446 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
450 struct inpcb
*inp
= sotoinpcb(so
);
451 struct tcpcb
*tp
= NULL
;
454 if (so
->so_state
& SS_ISDISCONNECTED
) {
455 error
= ECONNABORTED
;
464 in_setpeeraddr(so
, nam
);
465 COMMON_END(PRU_ACCEPT
);
470 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
474 struct inpcb
*inp
= sotoinpcb(so
);
475 struct tcpcb
*tp
= NULL
;
478 if (so
->so_state
& SS_ISDISCONNECTED
) {
479 error
= ECONNABORTED
;
488 in6_mapped_peeraddr(so
, nam
);
489 COMMON_END(PRU_ACCEPT
);
493 * Mark the connection as being incapable of further output.
496 tcp_usr_shutdown(struct socket
*so
)
500 struct inpcb
*inp
= sotoinpcb(so
);
505 /* In case we got disconnected from the peer */
508 tp
= tcp_usrclosed(tp
);
510 error
= tcp_output(tp
);
511 COMMON_END(PRU_SHUTDOWN
);
515 * After a receive, possibly send window update to peer.
518 tcp_usr_rcvd(struct socket
*so
, int flags
)
522 struct inpcb
*inp
= sotoinpcb(so
);
526 /* In case we got disconnected from the peer */
530 COMMON_END(PRU_RCVD
);
534 * Do a send by putting data in output queue and updating urgent
535 * marker if URG set. Possibly send more data. Unlike the other
536 * pru_*() routines, the mbuf chains are our responsibility. We
537 * must either enqueue them or free them. The other pru_* routines
538 * generally are caller-frees.
541 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
542 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
546 struct inpcb
*inp
= sotoinpcb(so
);
555 * OOPS! we lost a race, the TCP session got reset after
556 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
557 * network interrupt in the non-splnet() section of sosend().
563 error
= ECONNRESET
; /* XXX EPIPE? */
569 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
574 /* TCP doesn't do control messages (rights, creds, etc) */
575 if (control
->m_len
) {
582 m_freem(control
); /* empty control, just free it */
584 if(!(flags
& PRUS_OOB
)) {
585 sbappend(&so
->so_snd
, m
);
586 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
588 * Do implied connect if not yet connected,
589 * initialize window to default value, and
590 * initialize maxseg/maxopd using peer's cached
595 error
= tcp6_connect(tp
, nam
, p
);
598 error
= tcp_connect(tp
, nam
, p
);
601 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
605 if (flags
& PRUS_EOF
) {
607 * Close the send side of the connection after
611 tp
= tcp_usrclosed(tp
);
614 if (flags
& PRUS_MORETOCOME
)
615 tp
->t_flags
|= TF_MORETOCOME
;
616 error
= tcp_output(tp
);
617 if (flags
& PRUS_MORETOCOME
)
618 tp
->t_flags
&= ~TF_MORETOCOME
;
621 if (sbspace(&so
->so_snd
) < -512) {
627 * According to RFC961 (Assigned Protocols),
628 * the urgent pointer points to the last octet
629 * of urgent data. We continue, however,
630 * to consider it to indicate the first octet
631 * of data past the urgent section.
632 * Otherwise, snd_up should be one lower.
634 sbappend(&so
->so_snd
, m
);
635 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
637 * Do implied connect if not yet connected,
638 * initialize window to default value, and
639 * initialize maxseg/maxopd using peer's cached
644 error
= tcp6_connect(tp
, nam
, p
);
647 error
= tcp_connect(tp
, nam
, p
);
650 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
653 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
655 error
= tcp_output(tp
);
658 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
659 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
666 tcp_usr_abort(struct socket
*so
)
670 struct inpcb
*inp
= sotoinpcb(so
);
674 /* In case we got disconnected from the peer */
677 tp
= tcp_drop(tp
, ECONNABORTED
);
678 COMMON_END(PRU_ABORT
);
682 * Receive out-of-band data.
685 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
689 struct inpcb
*inp
= sotoinpcb(so
);
693 if ((so
->so_oobmark
== 0 &&
694 (so
->so_state
& SS_RCVATMARK
) == 0) ||
695 so
->so_options
& SO_OOBINLINE
||
696 tp
->t_oobflags
& TCPOOB_HADDATA
) {
700 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
705 *mtod(m
, caddr_t
) = tp
->t_iobc
;
706 if ((flags
& MSG_PEEK
) == 0)
707 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
708 COMMON_END(PRU_RCVOOB
);
711 /* xxx - should be const */
712 struct pr_usrreqs tcp_usrreqs
= {
713 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
714 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
715 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
716 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
717 in_setsockaddr
, sosend
, soreceive
, sopoll
721 struct pr_usrreqs tcp6_usrreqs
= {
722 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
723 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
724 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
725 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
726 in6_mapped_sockaddr
, sosend
, soreceive
, sopoll
731 * Common subroutine to open a TCP connection to remote host specified
732 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
733 * port number if needed. Call in_pcbladdr to do the routing and to choose
734 * a local host address (interface). If there is an existing incarnation
735 * of the same connection in TIME-WAIT state and if the remote host was
736 * sending CC options and if the connection duration was < MSL, then
737 * truncate the previous TIME-WAIT state and proceed.
738 * Initialize connection parameters and enter SYN-SENT state.
741 tcp_connect(tp
, nam
, p
)
742 register struct tcpcb
*tp
;
743 struct sockaddr
*nam
;
746 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
747 struct socket
*so
= inp
->inp_socket
;
749 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
750 struct sockaddr_in
*ifaddr
;
751 struct rmxp_tao
*taop
;
752 struct rmxp_tao tao_noncached
;
755 if (inp
->inp_lport
== 0) {
756 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
762 * Cannot simply call in_pcbconnect, because there might be an
763 * earlier incarnation of this same connection still in
764 * TIME_WAIT state, creating an ADDRINUSE error.
766 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
769 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
770 sin
->sin_addr
, sin
->sin_port
,
771 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
773 inp
->inp_lport
, 0, NULL
);
775 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
776 otp
->t_state
== TCPS_TIME_WAIT
&&
777 otp
->t_starttime
< tcp_msl
&&
778 (otp
->t_flags
& TF_RCVD_CC
))
779 otp
= tcp_close(otp
);
783 if ((inp
->inp_laddr
.s_addr
== INADDR_ANY
? ifaddr
->sin_addr
.s_addr
:
784 inp
->inp_laddr
.s_addr
) == sin
->sin_addr
.s_addr
&&
785 inp
->inp_lport
== sin
->sin_port
)
787 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
788 inp
->inp_laddr
= ifaddr
->sin_addr
;
789 inp
->inp_faddr
= sin
->sin_addr
;
790 inp
->inp_fport
= sin
->sin_port
;
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 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
857 &sin6
->sin6_addr
, sin6
->sin6_port
,
858 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
861 inp
->inp_lport
, 0, NULL
);
863 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
864 otp
->t_state
== TCPS_TIME_WAIT
&&
865 otp
->t_starttime
< tcp_msl
&&
866 (otp
->t_flags
& TF_RCVD_CC
))
867 otp
= tcp_close(otp
);
871 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
872 inp
->in6p_laddr
= *addr6
;
873 inp
->in6p_faddr
= sin6
->sin6_addr
;
874 inp
->inp_fport
= sin6
->sin6_port
;
875 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != NULL
)
876 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
879 /* Compute window scaling to request. */
880 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
881 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
882 tp
->request_r_scale
++;
885 tcpstat
.tcps_connattempt
++;
886 tp
->t_state
= TCPS_SYN_SENT
;
887 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
888 tp
->iss
= tcp_new_isn(tp
);
892 * Generate a CC value for this connection and
893 * check whether CC or CCnew should be used.
895 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
896 taop
= &tao_noncached
;
897 bzero(taop
, sizeof(*taop
));
900 tp
->cc_send
= CC_INC(tcp_ccgen
);
901 if (taop
->tao_ccsent
!= 0 &&
902 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
903 taop
->tao_ccsent
= tp
->cc_send
;
905 taop
->tao_ccsent
= 0;
906 tp
->t_flags
|= TF_SENDCCNEW
;
914 * The new sockopt interface makes it possible for us to block in the
915 * copyin/out step (if we take a page fault). Taking a page fault at
916 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
917 * use TSM, there probably isn't any need for this function to run at
918 * splnet() any more. This needs more examination.)
921 tcp_ctloutput(so
, sopt
)
923 struct sockopt
*sopt
;
925 int error
, opt
, optval
, s
;
930 s
= splnet(); /* XXX */
936 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
938 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
939 error
= ip6_ctloutput(so
, sopt
);
942 error
= ip_ctloutput(so
, sopt
);
952 switch (sopt
->sopt_dir
) {
954 switch (sopt
->sopt_name
) {
958 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
963 switch (sopt
->sopt_name
) {
974 opt
= 0; /* dead code to fool gcc */
985 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
990 if (optval
> 0 && optval
<= tp
->t_maxseg
)
991 tp
->t_maxseg
= optval
;
1003 switch (sopt
->sopt_name
) {
1005 optval
= tp
->t_flags
& TF_NODELAY
;
1008 optval
= tp
->t_maxseg
;
1011 optval
= tp
->t_flags
& TF_NOOPT
;
1014 optval
= tp
->t_flags
& TF_NOPUSH
;
1017 error
= ENOPROTOOPT
;
1021 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1029 * tcp_sendspace and tcp_recvspace are the default send and receive window
1030 * sizes, respectively. These are obsolescent (this information should
1031 * be set by the route).
1033 u_long tcp_sendspace
= 1024*16;
1034 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1035 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1036 u_long tcp_recvspace
= 1024*16;
1037 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1038 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1041 * Attach TCP protocol to socket, allocating
1042 * internet protocol control block, tcp control block,
1043 * bufer space, and entering LISTEN state if to accept connections.
1050 register struct tcpcb
*tp
;
1054 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != NULL
;
1057 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1058 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1062 error
= in_pcballoc(so
, &tcbinfo
, p
);
1065 inp
= sotoinpcb(so
);
1068 inp
->inp_vflag
|= INP_IPV6
;
1069 inp
->in6p_hops
= -1; /* use kernel default */
1073 inp
->inp_vflag
|= INP_IPV4
;
1074 tp
= tcp_newtcpcb(inp
);
1076 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1078 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1085 so
->so_state
|= nofd
;
1088 tp
->t_state
= TCPS_CLOSED
;
1093 * Initiate (or continue) disconnect.
1094 * If embryonic state, just send reset (once).
1095 * If in ``let data drain'' option and linger null, just drop.
1096 * Otherwise (hard), mark socket disconnecting and drop
1097 * current input data; switch states based on user close, and
1098 * send segment to peer (with FIN).
1100 static struct tcpcb
*
1102 register struct tcpcb
*tp
;
1104 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1106 if (tp
->t_state
< TCPS_ESTABLISHED
)
1108 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1109 tp
= tcp_drop(tp
, 0);
1111 soisdisconnecting(so
);
1112 sbflush(&so
->so_rcv
);
1113 tp
= tcp_usrclosed(tp
);
1115 (void) tcp_output(tp
);
1121 * User issued close, and wish to trail through shutdown states:
1122 * if never received SYN, just forget it. If got a SYN from peer,
1123 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1124 * If already got a FIN from peer, then almost done; go to LAST_ACK
1125 * state. In all other cases, have already sent FIN to peer (e.g.
1126 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1127 * for peer to send FIN or not respond to keep-alives, etc.
1128 * We can let the user exit from the close as soon as the FIN is acked.
1130 static struct tcpcb
*
1132 register struct tcpcb
*tp
;
1135 switch (tp
->t_state
) {
1139 tp
->t_state
= TCPS_CLOSED
;
1144 case TCPS_SYN_RECEIVED
:
1145 tp
->t_flags
|= TF_NEEDFIN
;
1148 case TCPS_ESTABLISHED
:
1149 tp
->t_state
= TCPS_FIN_WAIT_1
;
1152 case TCPS_CLOSE_WAIT
:
1153 tp
->t_state
= TCPS_LAST_ACK
;
1156 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1157 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1158 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1159 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1160 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;