]>
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
__P((struct socket
*, struct proc
*));
108 static int tcp_connect
__P((struct tcpcb
*, struct sockaddr
*,
111 static int tcp6_connect
__P((struct tcpcb
*, struct sockaddr
*,
114 static struct tcpcb
*
115 tcp_disconnect
__P((struct tcpcb
*));
116 static struct tcpcb
*
117 tcp_usrclosed
__P((struct tcpcb
*));
120 #define TCPDEBUG0 int ostate = 0
121 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
122 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
123 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
127 #define TCPDEBUG2(req)
131 * TCP attaches to socket via pru_attach(), reserving space,
132 * and an internet control block.
135 tcp_usr_attach(struct socket
*so
, int proto
, struct proc
*p
)
139 struct inpcb
*inp
= sotoinpcb(so
);
140 struct tcpcb
*tp
= 0;
149 error
= tcp_attach(so
, p
);
153 if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
154 so
->so_linger
= TCP_LINGERTIME
* hz
;
157 TCPDEBUG2(PRU_ATTACH
);
163 * pru_detach() detaches the TCP protocol from the socket.
164 * If the protocol state is non-embryonic, then can't
165 * do this directly: have to initiate a pru_disconnect(),
166 * which may finish later; embryonic TCB's can just
170 tcp_usr_detach(struct socket
*so
)
174 struct inpcb
*inp
= sotoinpcb(so
);
180 return EINVAL
; /* XXX */
183 /* In case we got disconnected from the peer */
187 tp
= tcp_disconnect(tp
);
189 TCPDEBUG2(PRU_DETACH
);
194 #define COMMON_START() TCPDEBUG0; \
200 tp = intotcpcb(inp); \
204 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
208 * Give the socket an address.
211 tcp_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
215 struct inpcb
*inp
= sotoinpcb(so
);
217 struct sockaddr_in
*sinp
;
222 * Must check for multicast addresses and disallow binding
225 sinp
= (struct sockaddr_in
*)nam
;
226 if (sinp
->sin_family
== AF_INET
&&
227 IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
228 error
= EAFNOSUPPORT
;
231 error
= in_pcbbind(inp
, nam
, p
);
234 COMMON_END(PRU_BIND
);
240 tcp6_usr_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
244 struct inpcb
*inp
= sotoinpcb(so
);
246 struct sockaddr_in6
*sin6p
;
251 * Must check for multicast addresses and disallow binding
254 sin6p
= (struct sockaddr_in6
*)nam
;
255 if (sin6p
->sin6_family
== AF_INET6
&&
256 IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
257 error
= EAFNOSUPPORT
;
260 inp
->inp_vflag
&= ~INP_IPV4
;
261 inp
->inp_vflag
|= INP_IPV6
;
262 if (ip6_mapped_addr_on
&& (inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
263 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p
->sin6_addr
))
264 inp
->inp_vflag
|= INP_IPV4
;
265 else if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
266 struct sockaddr_in sin
;
268 in6_sin6_2_sin(&sin
, sin6p
);
269 inp
->inp_vflag
|= INP_IPV4
;
270 inp
->inp_vflag
&= ~INP_IPV6
;
271 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
275 error
= in6_pcbbind(inp
, nam
, p
);
278 COMMON_END(PRU_BIND
);
283 * Prepare to accept connections.
286 tcp_usr_listen(struct socket
*so
, struct proc
*p
)
290 struct inpcb
*inp
= sotoinpcb(so
);
294 if (inp
->inp_lport
== 0)
295 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
297 tp
->t_state
= TCPS_LISTEN
;
298 COMMON_END(PRU_LISTEN
);
303 tcp6_usr_listen(struct socket
*so
, struct proc
*p
)
307 struct inpcb
*inp
= sotoinpcb(so
);
311 if (inp
->inp_lport
== 0) {
312 inp
->inp_vflag
&= ~INP_IPV4
;
313 if (ip6_mapped_addr_on
&&
314 (inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0)
315 inp
->inp_vflag
|= INP_IPV4
;
316 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
319 tp
->t_state
= TCPS_LISTEN
;
320 COMMON_END(PRU_LISTEN
);
325 * Initiate connection to peer.
326 * Create a template for use in transmissions on this connection.
327 * Enter SYN_SENT state, and mark socket as connecting.
328 * Start keep-alive timer, and seed output sequence space.
329 * Send initial segment on connection.
332 tcp_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
336 struct inpcb
*inp
= sotoinpcb(so
);
338 struct sockaddr_in
*sinp
;
343 * Must disallow TCP ``connections'' to multicast addresses.
345 sinp
= (struct sockaddr_in
*)nam
;
346 if (sinp
->sin_family
== AF_INET
347 && IN_MULTICAST(ntohl(sinp
->sin_addr
.s_addr
))) {
348 error
= EAFNOSUPPORT
;
353 prison_remote_ip(p
, 0, &sinp
->sin_addr
.s_addr
);
356 if ((error
= tcp_connect(tp
, nam
, p
)) != 0)
358 error
= tcp_output(tp
);
359 COMMON_END(PRU_CONNECT
);
364 tcp6_usr_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
368 struct inpcb
*inp
= sotoinpcb(so
);
370 struct sockaddr_in6
*sin6p
;
375 * Must disallow TCP ``connections'' to multicast addresses.
377 sin6p
= (struct sockaddr_in6
*)nam
;
378 if (sin6p
->sin6_family
== AF_INET6
379 && IN6_IS_ADDR_MULTICAST(&sin6p
->sin6_addr
)) {
380 error
= EAFNOSUPPORT
;
384 if (IN6_IS_ADDR_V4MAPPED(&sin6p
->sin6_addr
)) {
385 struct sockaddr_in sin
;
387 if (!ip6_mapped_addr_on
||
388 (inp
->inp_flags
& IN6P_IPV6_V6ONLY
))
391 in6_sin6_2_sin(&sin
, sin6p
);
392 inp
->inp_vflag
|= INP_IPV4
;
393 inp
->inp_vflag
&= ~INP_IPV6
;
394 if ((error
= tcp_connect(tp
, (struct sockaddr
*)&sin
, p
)) != 0)
396 error
= tcp_output(tp
);
399 inp
->inp_vflag
&= ~INP_IPV4
;
400 inp
->inp_vflag
|= INP_IPV6
;
401 if ((error
= tcp6_connect(tp
, nam
, p
)) != 0)
403 error
= tcp_output(tp
);
406 COMMON_END(PRU_CONNECT
);
411 * Initiate disconnect from peer.
412 * If connection never passed embryonic stage, just drop;
413 * else if don't need to let data drain, then can just drop anyways,
414 * else have to begin TCP shutdown process: mark socket disconnecting,
415 * drain unread data, state switch to reflect user close, and
416 * send segment (e.g. FIN) to peer. Socket will be really disconnected
417 * when peer sends FIN and acks ours.
419 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
422 tcp_usr_disconnect(struct socket
*so
)
426 struct inpcb
*inp
= sotoinpcb(so
);
430 /* In case we got disconnected from the peer */
433 tp
= tcp_disconnect(tp
);
434 COMMON_END(PRU_DISCONNECT
);
438 * Accept a connection. Essentially all the work is
439 * done at higher levels; just return the address
440 * of the peer, storing through addr.
443 tcp_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
447 struct inpcb
*inp
= sotoinpcb(so
);
448 struct tcpcb
*tp
= NULL
;
451 if (so
->so_state
& SS_ISDISCONNECTED
) {
452 error
= ECONNABORTED
;
461 in_setpeeraddr(so
, nam
);
462 COMMON_END(PRU_ACCEPT
);
467 tcp6_usr_accept(struct socket
*so
, struct sockaddr
**nam
)
471 struct inpcb
*inp
= sotoinpcb(so
);
472 struct tcpcb
*tp
= NULL
;
475 if (so
->so_state
& SS_ISDISCONNECTED
) {
476 error
= ECONNABORTED
;
485 in6_mapped_peeraddr(so
, nam
);
486 COMMON_END(PRU_ACCEPT
);
490 * Mark the connection as being incapable of further output.
493 tcp_usr_shutdown(struct socket
*so
)
497 struct inpcb
*inp
= sotoinpcb(so
);
502 /* In case we got disconnected from the peer */
505 tp
= tcp_usrclosed(tp
);
507 error
= tcp_output(tp
);
508 COMMON_END(PRU_SHUTDOWN
);
512 * After a receive, possibly send window update to peer.
515 tcp_usr_rcvd(struct socket
*so
, int flags
)
519 struct inpcb
*inp
= sotoinpcb(so
);
523 /* In case we got disconnected from the peer */
527 COMMON_END(PRU_RCVD
);
531 * Do a send by putting data in output queue and updating urgent
532 * marker if URG set. Possibly send more data. Unlike the other
533 * pru_*() routines, the mbuf chains are our responsibility. We
534 * must either enqueue them or free them. The other pru_* routines
535 * generally are caller-frees.
538 tcp_usr_send(struct socket
*so
, int flags
, struct mbuf
*m
,
539 struct sockaddr
*nam
, struct mbuf
*control
, struct proc
*p
)
543 struct inpcb
*inp
= sotoinpcb(so
);
552 * OOPS! we lost a race, the TCP session got reset after
553 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
554 * network interrupt in the non-splnet() section of sosend().
560 error
= ECONNRESET
; /* XXX EPIPE? */
566 isipv6
= nam
&& nam
->sa_family
== AF_INET6
;
571 /* TCP doesn't do control messages (rights, creds, etc) */
572 if (control
->m_len
) {
579 m_freem(control
); /* empty control, just free it */
581 if(!(flags
& PRUS_OOB
)) {
582 sbappend(&so
->so_snd
, m
);
583 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
585 * Do implied connect if not yet connected,
586 * initialize window to default value, and
587 * initialize maxseg/maxopd using peer's cached
592 error
= tcp6_connect(tp
, nam
, p
);
595 error
= tcp_connect(tp
, nam
, p
);
598 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
602 if (flags
& PRUS_EOF
) {
604 * Close the send side of the connection after
608 tp
= tcp_usrclosed(tp
);
611 if (flags
& PRUS_MORETOCOME
)
612 tp
->t_flags
|= TF_MORETOCOME
;
613 error
= tcp_output(tp
);
614 if (flags
& PRUS_MORETOCOME
)
615 tp
->t_flags
&= ~TF_MORETOCOME
;
618 if (sbspace(&so
->so_snd
) < -512) {
624 * According to RFC961 (Assigned Protocols),
625 * the urgent pointer points to the last octet
626 * of urgent data. We continue, however,
627 * to consider it to indicate the first octet
628 * of data past the urgent section.
629 * Otherwise, snd_up should be one lower.
631 sbappend(&so
->so_snd
, m
);
632 if (nam
&& tp
->t_state
< TCPS_SYN_SENT
) {
634 * Do implied connect if not yet connected,
635 * initialize window to default value, and
636 * initialize maxseg/maxopd using peer's cached
641 error
= tcp6_connect(tp
, nam
, p
);
644 error
= tcp_connect(tp
, nam
, p
);
647 tp
->snd_wnd
= TTCP_CLIENT_SND_WND
;
650 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
652 error
= tcp_output(tp
);
655 COMMON_END((flags
& PRUS_OOB
) ? PRU_SENDOOB
:
656 ((flags
& PRUS_EOF
) ? PRU_SEND_EOF
: PRU_SEND
));
663 tcp_usr_abort(struct socket
*so
)
667 struct inpcb
*inp
= sotoinpcb(so
);
671 /* In case we got disconnected from the peer */
674 tp
= tcp_drop(tp
, ECONNABORTED
);
675 COMMON_END(PRU_ABORT
);
679 * Receive out-of-band data.
682 tcp_usr_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
686 struct inpcb
*inp
= sotoinpcb(so
);
690 if ((so
->so_oobmark
== 0 &&
691 (so
->so_state
& SS_RCVATMARK
) == 0) ||
692 so
->so_options
& SO_OOBINLINE
||
693 tp
->t_oobflags
& TCPOOB_HADDATA
) {
697 if ((tp
->t_oobflags
& TCPOOB_HAVEDATA
) == 0) {
702 *mtod(m
, caddr_t
) = tp
->t_iobc
;
703 if ((flags
& MSG_PEEK
) == 0)
704 tp
->t_oobflags
^= (TCPOOB_HAVEDATA
| TCPOOB_HADDATA
);
705 COMMON_END(PRU_RCVOOB
);
708 /* xxx - should be const */
709 struct pr_usrreqs tcp_usrreqs
= {
710 tcp_usr_abort
, tcp_usr_accept
, tcp_usr_attach
, tcp_usr_bind
,
711 tcp_usr_connect
, pru_connect2_notsupp
, in_control
, tcp_usr_detach
,
712 tcp_usr_disconnect
, tcp_usr_listen
, in_setpeeraddr
, tcp_usr_rcvd
,
713 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
714 in_setsockaddr
, sosend
, soreceive
, sopoll
718 struct pr_usrreqs tcp6_usrreqs
= {
719 tcp_usr_abort
, tcp6_usr_accept
, tcp_usr_attach
, tcp6_usr_bind
,
720 tcp6_usr_connect
, pru_connect2_notsupp
, in6_control
, tcp_usr_detach
,
721 tcp_usr_disconnect
, tcp6_usr_listen
, in6_mapped_peeraddr
, tcp_usr_rcvd
,
722 tcp_usr_rcvoob
, tcp_usr_send
, pru_sense_null
, tcp_usr_shutdown
,
723 in6_mapped_sockaddr
, sosend
, soreceive
, sopoll
728 * Common subroutine to open a TCP connection to remote host specified
729 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
730 * port number if needed. Call in_pcbladdr to do the routing and to choose
731 * a local host address (interface). If there is an existing incarnation
732 * of the same connection in TIME-WAIT state and if the remote host was
733 * sending CC options and if the connection duration was < MSL, then
734 * truncate the previous TIME-WAIT state and proceed.
735 * Initialize connection parameters and enter SYN-SENT state.
738 tcp_connect(tp
, nam
, p
)
739 register struct tcpcb
*tp
;
740 struct sockaddr
*nam
;
743 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
744 struct socket
*so
= inp
->inp_socket
;
746 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
747 struct sockaddr_in
*ifaddr
;
748 struct rmxp_tao
*taop
;
749 struct rmxp_tao tao_noncached
;
752 if (inp
->inp_lport
== 0) {
753 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
759 * Cannot simply call in_pcbconnect, because there might be an
760 * earlier incarnation of this same connection still in
761 * TIME_WAIT state, creating an ADDRINUSE error.
763 error
= in_pcbladdr(inp
, nam
, &ifaddr
);
766 oinp
= in_pcblookup_hash(inp
->inp_pcbinfo
,
767 sin
->sin_addr
, sin
->sin_port
,
768 inp
->inp_laddr
.s_addr
!= INADDR_ANY
? inp
->inp_laddr
770 inp
->inp_lport
, 0, NULL
);
772 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
773 otp
->t_state
== TCPS_TIME_WAIT
&&
774 otp
->t_starttime
< tcp_msl
&&
775 (otp
->t_flags
& TF_RCVD_CC
))
776 otp
= tcp_close(otp
);
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 (inp
->inp_laddr
.s_addr
== INADDR_ANY
)
785 inp
->inp_laddr
= ifaddr
->sin_addr
;
786 inp
->inp_faddr
= sin
->sin_addr
;
787 inp
->inp_fport
= sin
->sin_port
;
790 /* Compute window scaling to request. */
791 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
792 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
793 tp
->request_r_scale
++;
796 tcpstat
.tcps_connattempt
++;
797 tp
->t_state
= TCPS_SYN_SENT
;
798 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
799 tp
->iss
= tcp_new_isn(tp
);
803 * Generate a CC value for this connection and
804 * check whether CC or CCnew should be used.
806 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
807 taop
= &tao_noncached
;
808 bzero(taop
, sizeof(*taop
));
811 tp
->cc_send
= CC_INC(tcp_ccgen
);
812 if (taop
->tao_ccsent
!= 0 &&
813 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
814 taop
->tao_ccsent
= tp
->cc_send
;
816 taop
->tao_ccsent
= 0;
817 tp
->t_flags
|= TF_SENDCCNEW
;
825 tcp6_connect(tp
, nam
, p
)
826 register struct tcpcb
*tp
;
827 struct sockaddr
*nam
;
830 struct inpcb
*inp
= tp
->t_inpcb
, *oinp
;
831 struct socket
*so
= inp
->inp_socket
;
833 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
834 struct in6_addr
*addr6
;
835 struct rmxp_tao
*taop
;
836 struct rmxp_tao tao_noncached
;
839 if (inp
->inp_lport
== 0) {
840 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
846 * Cannot simply call in_pcbconnect, because there might be an
847 * earlier incarnation of this same connection still in
848 * TIME_WAIT state, creating an ADDRINUSE error.
850 error
= in6_pcbladdr(inp
, nam
, &addr6
);
853 oinp
= in6_pcblookup_hash(inp
->inp_pcbinfo
,
854 &sin6
->sin6_addr
, sin6
->sin6_port
,
855 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
858 inp
->inp_lport
, 0, NULL
);
860 if (oinp
!= inp
&& (otp
= intotcpcb(oinp
)) != NULL
&&
861 otp
->t_state
== TCPS_TIME_WAIT
&&
862 otp
->t_starttime
< tcp_msl
&&
863 (otp
->t_flags
& TF_RCVD_CC
))
864 otp
= tcp_close(otp
);
868 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
869 inp
->in6p_laddr
= *addr6
;
870 inp
->in6p_faddr
= sin6
->sin6_addr
;
871 inp
->inp_fport
= sin6
->sin6_port
;
872 if ((sin6
->sin6_flowinfo
& IPV6_FLOWINFO_MASK
) != NULL
)
873 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
876 /* Compute window scaling to request. */
877 while (tp
->request_r_scale
< TCP_MAX_WINSHIFT
&&
878 (TCP_MAXWIN
<< tp
->request_r_scale
) < so
->so_rcv
.sb_hiwat
)
879 tp
->request_r_scale
++;
882 tcpstat
.tcps_connattempt
++;
883 tp
->t_state
= TCPS_SYN_SENT
;
884 tp
->t_timer
[TCPT_KEEP
] = tcp_keepinit
;
885 tp
->iss
= tcp_new_isn(tp
);
889 * Generate a CC value for this connection and
890 * check whether CC or CCnew should be used.
892 if ((taop
= tcp_gettaocache(tp
->t_inpcb
)) == NULL
) {
893 taop
= &tao_noncached
;
894 bzero(taop
, sizeof(*taop
));
897 tp
->cc_send
= CC_INC(tcp_ccgen
);
898 if (taop
->tao_ccsent
!= 0 &&
899 CC_GEQ(tp
->cc_send
, taop
->tao_ccsent
)) {
900 taop
->tao_ccsent
= tp
->cc_send
;
902 taop
->tao_ccsent
= 0;
903 tp
->t_flags
|= TF_SENDCCNEW
;
911 * The new sockopt interface makes it possible for us to block in the
912 * copyin/out step (if we take a page fault). Taking a page fault at
913 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
914 * use TSM, there probably isn't any need for this function to run at
915 * splnet() any more. This needs more examination.)
918 tcp_ctloutput(so
, sopt
)
920 struct sockopt
*sopt
;
922 int error
, opt
, optval
, s
;
927 s
= splnet(); /* XXX */
933 if (sopt
->sopt_level
!= IPPROTO_TCP
) {
935 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
936 error
= ip6_ctloutput(so
, sopt
);
939 error
= ip_ctloutput(so
, sopt
);
949 switch (sopt
->sopt_dir
) {
951 switch (sopt
->sopt_name
) {
955 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
960 switch (sopt
->sopt_name
) {
971 opt
= 0; /* dead code to fool gcc */
982 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
987 if (optval
> 0 && optval
<= tp
->t_maxseg
)
988 tp
->t_maxseg
= optval
;
1000 switch (sopt
->sopt_name
) {
1002 optval
= tp
->t_flags
& TF_NODELAY
;
1005 optval
= tp
->t_maxseg
;
1008 optval
= tp
->t_flags
& TF_NOOPT
;
1011 optval
= tp
->t_flags
& TF_NOPUSH
;
1014 error
= ENOPROTOOPT
;
1018 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1026 * tcp_sendspace and tcp_recvspace are the default send and receive window
1027 * sizes, respectively. These are obsolescent (this information should
1028 * be set by the route).
1030 u_long tcp_sendspace
= 1024*16;
1031 SYSCTL_INT(_net_inet_tcp
, TCPCTL_SENDSPACE
, sendspace
, CTLFLAG_RW
,
1032 &tcp_sendspace
, 0, "Maximum outgoing TCP datagram size");
1033 u_long tcp_recvspace
= 1024*16;
1034 SYSCTL_INT(_net_inet_tcp
, TCPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1035 &tcp_recvspace
, 0, "Maximum incoming TCP datagram size");
1038 * Attach TCP protocol to socket, allocating
1039 * internet protocol control block, tcp control block,
1040 * bufer space, and entering LISTEN state if to accept connections.
1047 register struct tcpcb
*tp
;
1051 int isipv6
= INP_CHECK_SOCKAF(so
, AF_INET6
) != NULL
;
1054 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
1055 error
= soreserve(so
, tcp_sendspace
, tcp_recvspace
);
1059 error
= in_pcballoc(so
, &tcbinfo
, p
);
1062 inp
= sotoinpcb(so
);
1065 inp
->inp_vflag
|= INP_IPV6
;
1066 inp
->in6p_hops
= -1; /* use kernel default */
1070 inp
->inp_vflag
|= INP_IPV4
;
1071 tp
= tcp_newtcpcb(inp
);
1073 int nofd
= so
->so_state
& SS_NOFDREF
; /* XXX */
1075 so
->so_state
&= ~SS_NOFDREF
; /* don't free the socket yet */
1082 so
->so_state
|= nofd
;
1085 tp
->t_state
= TCPS_CLOSED
;
1090 * Initiate (or continue) disconnect.
1091 * If embryonic state, just send reset (once).
1092 * If in ``let data drain'' option and linger null, just drop.
1093 * Otherwise (hard), mark socket disconnecting and drop
1094 * current input data; switch states based on user close, and
1095 * send segment to peer (with FIN).
1097 static struct tcpcb
*
1099 register struct tcpcb
*tp
;
1101 struct socket
*so
= tp
->t_inpcb
->inp_socket
;
1103 if (tp
->t_state
< TCPS_ESTABLISHED
)
1105 else if ((so
->so_options
& SO_LINGER
) && so
->so_linger
== 0)
1106 tp
= tcp_drop(tp
, 0);
1108 soisdisconnecting(so
);
1109 sbflush(&so
->so_rcv
);
1110 tp
= tcp_usrclosed(tp
);
1112 (void) tcp_output(tp
);
1118 * User issued close, and wish to trail through shutdown states:
1119 * if never received SYN, just forget it. If got a SYN from peer,
1120 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1121 * If already got a FIN from peer, then almost done; go to LAST_ACK
1122 * state. In all other cases, have already sent FIN to peer (e.g.
1123 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1124 * for peer to send FIN or not respond to keep-alives, etc.
1125 * We can let the user exit from the close as soon as the FIN is acked.
1127 static struct tcpcb
*
1129 register struct tcpcb
*tp
;
1132 switch (tp
->t_state
) {
1136 tp
->t_state
= TCPS_CLOSED
;
1141 case TCPS_SYN_RECEIVED
:
1142 tp
->t_flags
|= TF_NEEDFIN
;
1145 case TCPS_ESTABLISHED
:
1146 tp
->t_state
= TCPS_FIN_WAIT_1
;
1149 case TCPS_CLOSE_WAIT
:
1150 tp
->t_state
= TCPS_LAST_ACK
;
1153 if (tp
&& tp
->t_state
>= TCPS_FIN_WAIT_2
) {
1154 soisdisconnected(tp
->t_inpcb
->inp_socket
);
1155 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1156 if (tp
->t_state
== TCPS_FIN_WAIT_2
)
1157 tp
->t_timer
[TCPT_2MSL
] = tcp_maxidle
;