xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 243917fe3b5e36464ab72473e872da9acd44aa1c)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
331fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
38fb59c426SYoshinobu Inoue #include "opt_inet6.h"
390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
400cc12cc5SJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43c7a82f90SGarrett Wollman #include <sys/kernel.h>
4498163b98SPoul-Henning Kamp #include <sys/sysctl.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46fb59c426SYoshinobu Inoue #ifdef INET6
47fb59c426SYoshinobu Inoue #include <sys/domain.h>
48fb59c426SYoshinobu Inoue #endif /* INET6 */
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5291421ba2SRobert Watson #include <sys/proc.h>
5391421ba2SRobert Watson #include <sys/jail.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
56df8bae1dSRodney W. Grimes #include <net/route.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
60fb59c426SYoshinobu Inoue #ifdef INET6
61fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
62fb59c426SYoshinobu Inoue #endif
63df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
64fb59c426SYoshinobu Inoue #ifdef INET6
65fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
66fb59c426SYoshinobu Inoue #endif
67b5e8ce9fSBruce Evans #include <netinet/in_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
69fb59c426SYoshinobu Inoue #ifdef INET6
70fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
71fb59c426SYoshinobu Inoue #endif
72df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
74df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
75df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
78610ee2f9SDavid Greenman #ifdef TCPDEBUG
79df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
80610ee2f9SDavid Greenman #endif
81df8bae1dSRodney W. Grimes 
826a800098SYoshinobu Inoue #ifdef IPSEC
836a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
846a800098SYoshinobu Inoue #endif /*IPSEC*/
856a800098SYoshinobu Inoue 
86df8bae1dSRodney W. Grimes /*
87df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
88df8bae1dSRodney W. Grimes  */
89117bcae7SGarrett Wollman extern	char *tcpstates[];	/* XXX ??? */
90df8bae1dSRodney W. Grimes 
914d77a549SAlfred Perlstein static int	tcp_attach(struct socket *, struct thread *td);
924d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
934d77a549SAlfred Perlstein 		    struct thread *td);
94fb59c426SYoshinobu Inoue #ifdef INET6
954d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
964d77a549SAlfred Perlstein 		    struct thread *td);
97fb59c426SYoshinobu Inoue #endif /* INET6 */
980312fbe9SPoul-Henning Kamp static struct tcpcb *
994d77a549SAlfred Perlstein 		tcp_disconnect(struct tcpcb *);
1000312fbe9SPoul-Henning Kamp static struct tcpcb *
1014d77a549SAlfred Perlstein 		tcp_usrclosed(struct tcpcb *);
1022c37256eSGarrett Wollman 
1032c37256eSGarrett Wollman #ifdef TCPDEBUG
1041db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1052c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
106243917feSSeigo Tanimura #define	TCPDEBUG2(req)								\
107243917feSSeigo Tanimura 	do {									\
108243917feSSeigo Tanimura 		if (tp != 0) {							\
109243917feSSeigo Tanimura 			SOCK_LOCK(so);						\
110243917feSSeigo Tanimura 			if (so->so_options & SO_DEBUG) {			\
111243917feSSeigo Tanimura 				SOCK_UNLOCK(so);				\
112243917feSSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req);	\
113243917feSSeigo Tanimura 			} else							\
114243917feSSeigo Tanimura 				SOCK_UNLOCK(so);				\
115243917feSSeigo Tanimura 		}								\
116243917feSSeigo Tanimura 	} while(0)
1172c37256eSGarrett Wollman #else
1182c37256eSGarrett Wollman #define	TCPDEBUG0
1192c37256eSGarrett Wollman #define	TCPDEBUG1()
1202c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1212c37256eSGarrett Wollman #endif
1222c37256eSGarrett Wollman 
1232c37256eSGarrett Wollman /*
1242c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1252c37256eSGarrett Wollman  * and an internet control block.
1262c37256eSGarrett Wollman  */
1272c37256eSGarrett Wollman static int
128b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1292c37256eSGarrett Wollman {
1302c37256eSGarrett Wollman 	int s = splnet();
1312c37256eSGarrett Wollman 	int error;
1322c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1332c37256eSGarrett Wollman 	struct tcpcb *tp = 0;
1342c37256eSGarrett Wollman 	TCPDEBUG0;
1352c37256eSGarrett Wollman 
1362c37256eSGarrett Wollman 	TCPDEBUG1();
1372c37256eSGarrett Wollman 	if (inp) {
1382c37256eSGarrett Wollman 		error = EISCONN;
1392c37256eSGarrett Wollman 		goto out;
1402c37256eSGarrett Wollman 	}
1412c37256eSGarrett Wollman 
142b40ce416SJulian Elischer 	error = tcp_attach(so, td);
1432c37256eSGarrett Wollman 	if (error)
1442c37256eSGarrett Wollman 		goto out;
1452c37256eSGarrett Wollman 
146243917feSSeigo Tanimura 	SOCK_LOCK(so);
1472c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1483879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
149243917feSSeigo Tanimura 	SOCK_UNLOCK(so);
1502c37256eSGarrett Wollman 	tp = sototcpcb(so);
1512c37256eSGarrett Wollman out:
1522c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1532c37256eSGarrett Wollman 	splx(s);
1542c37256eSGarrett Wollman 	return error;
1552c37256eSGarrett Wollman }
1562c37256eSGarrett Wollman 
1572c37256eSGarrett Wollman /*
1582c37256eSGarrett Wollman  * pru_detach() detaches the TCP protocol from the socket.
1592c37256eSGarrett Wollman  * If the protocol state is non-embryonic, then can't
1602c37256eSGarrett Wollman  * do this directly: have to initiate a pru_disconnect(),
1612c37256eSGarrett Wollman  * which may finish later; embryonic TCB's can just
1622c37256eSGarrett Wollman  * be discarded here.
1632c37256eSGarrett Wollman  */
1642c37256eSGarrett Wollman static int
1652c37256eSGarrett Wollman tcp_usr_detach(struct socket *so)
1662c37256eSGarrett Wollman {
1672c37256eSGarrett Wollman 	int s = splnet();
1682c37256eSGarrett Wollman 	int error = 0;
1692c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1702c37256eSGarrett Wollman 	struct tcpcb *tp;
1712c37256eSGarrett Wollman 	TCPDEBUG0;
1722c37256eSGarrett Wollman 
1732c37256eSGarrett Wollman 	if (inp == 0) {
1742c37256eSGarrett Wollman 		splx(s);
1752c37256eSGarrett Wollman 		return EINVAL;	/* XXX */
1762c37256eSGarrett Wollman 	}
1772c37256eSGarrett Wollman 	tp = intotcpcb(inp);
1782c37256eSGarrett Wollman 	TCPDEBUG1();
1792c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
1802c37256eSGarrett Wollman 
1812c37256eSGarrett Wollman 	TCPDEBUG2(PRU_DETACH);
1822c37256eSGarrett Wollman 	splx(s);
1832c37256eSGarrett Wollman 	return error;
1842c37256eSGarrett Wollman }
1852c37256eSGarrett Wollman 
1862c37256eSGarrett Wollman #define	COMMON_START()	TCPDEBUG0; \
1872c37256eSGarrett Wollman 			do { \
1882c37256eSGarrett Wollman 				     if (inp == 0) { \
1892c37256eSGarrett Wollman 					     splx(s); \
1902c37256eSGarrett Wollman 					     return EINVAL; \
1912c37256eSGarrett Wollman 				     } \
1922c37256eSGarrett Wollman 				     tp = intotcpcb(inp); \
1932c37256eSGarrett Wollman 				     TCPDEBUG1(); \
1942c37256eSGarrett Wollman 		     } while(0)
1952c37256eSGarrett Wollman 
1962c37256eSGarrett Wollman #define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
1972c37256eSGarrett Wollman 
1982c37256eSGarrett Wollman 
1992c37256eSGarrett Wollman /*
2002c37256eSGarrett Wollman  * Give the socket an address.
2012c37256eSGarrett Wollman  */
2022c37256eSGarrett Wollman static int
203b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2042c37256eSGarrett Wollman {
2052c37256eSGarrett Wollman 	int s = splnet();
2062c37256eSGarrett Wollman 	int error = 0;
2072c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
2082c37256eSGarrett Wollman 	struct tcpcb *tp;
2092c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2102c37256eSGarrett Wollman 
2112c37256eSGarrett Wollman 	COMMON_START();
2122c37256eSGarrett Wollman 
2132c37256eSGarrett Wollman 	/*
2142c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2152c37256eSGarrett Wollman 	 * to them.
2162c37256eSGarrett Wollman 	 */
21757bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
2182c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
2192c37256eSGarrett Wollman 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
2202c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
2212c37256eSGarrett Wollman 		goto out;
2222c37256eSGarrett Wollman 	}
223b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
2242c37256eSGarrett Wollman 	if (error)
2252c37256eSGarrett Wollman 		goto out;
2262c37256eSGarrett Wollman 	COMMON_END(PRU_BIND);
2272c37256eSGarrett Wollman 
2282c37256eSGarrett Wollman }
2292c37256eSGarrett Wollman 
230fb59c426SYoshinobu Inoue #ifdef INET6
231fb59c426SYoshinobu Inoue static int
232b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
233fb59c426SYoshinobu Inoue {
234fb59c426SYoshinobu Inoue 	int s = splnet();
235fb59c426SYoshinobu Inoue 	int error = 0;
236fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
237fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
238fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
239fb59c426SYoshinobu Inoue 
240fb59c426SYoshinobu Inoue 	COMMON_START();
241fb59c426SYoshinobu Inoue 
242fb59c426SYoshinobu Inoue 	/*
243fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
244fb59c426SYoshinobu Inoue 	 * to them.
245fb59c426SYoshinobu Inoue 	 */
246fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
247fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
248fb59c426SYoshinobu Inoue 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
249fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
250fb59c426SYoshinobu Inoue 		goto out;
251fb59c426SYoshinobu Inoue 	}
252fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
253fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
25433841545SHajimu UMEMOTO 	if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
255fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
256fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
257fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
258fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
259fb59c426SYoshinobu Inoue 
260fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
261fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
262fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
263b40ce416SJulian Elischer 			error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
264fb59c426SYoshinobu Inoue 			goto out;
265fb59c426SYoshinobu Inoue 		}
266fb59c426SYoshinobu Inoue 	}
267b40ce416SJulian Elischer 	error = in6_pcbbind(inp, nam, td);
268fb59c426SYoshinobu Inoue 	if (error)
269fb59c426SYoshinobu Inoue 		goto out;
270fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
271fb59c426SYoshinobu Inoue }
272fb59c426SYoshinobu Inoue #endif /* INET6 */
273fb59c426SYoshinobu Inoue 
2742c37256eSGarrett Wollman /*
2752c37256eSGarrett Wollman  * Prepare to accept connections.
2762c37256eSGarrett Wollman  */
2772c37256eSGarrett Wollman static int
278b40ce416SJulian Elischer tcp_usr_listen(struct socket *so, struct thread *td)
2792c37256eSGarrett Wollman {
2802c37256eSGarrett Wollman 	int s = splnet();
2812c37256eSGarrett Wollman 	int error = 0;
2822c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
2832c37256eSGarrett Wollman 	struct tcpcb *tp;
2842c37256eSGarrett Wollman 
2852c37256eSGarrett Wollman 	COMMON_START();
2862c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
287b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)0, td);
2882c37256eSGarrett Wollman 	if (error == 0)
2892c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
2902c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
2912c37256eSGarrett Wollman }
2922c37256eSGarrett Wollman 
293fb59c426SYoshinobu Inoue #ifdef INET6
294fb59c426SYoshinobu Inoue static int
295b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td)
296fb59c426SYoshinobu Inoue {
297fb59c426SYoshinobu Inoue 	int s = splnet();
298fb59c426SYoshinobu Inoue 	int error = 0;
299fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
300fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
301fb59c426SYoshinobu Inoue 
302fb59c426SYoshinobu Inoue 	COMMON_START();
303fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
304fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
30533841545SHajimu UMEMOTO 		if (ip6_mapped_addr_on &&
30633841545SHajimu UMEMOTO 		    (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
307fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
308b40ce416SJulian Elischer 		error = in6_pcbbind(inp, (struct sockaddr *)0, td);
309fb59c426SYoshinobu Inoue 	}
310fb59c426SYoshinobu Inoue 	if (error == 0)
311fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
312fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
313fb59c426SYoshinobu Inoue }
314fb59c426SYoshinobu Inoue #endif /* INET6 */
315fb59c426SYoshinobu Inoue 
3162c37256eSGarrett Wollman /*
3172c37256eSGarrett Wollman  * Initiate connection to peer.
3182c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3192c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3202c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3212c37256eSGarrett Wollman  * Send initial segment on connection.
3222c37256eSGarrett Wollman  */
3232c37256eSGarrett Wollman static int
324b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
3252c37256eSGarrett Wollman {
3262c37256eSGarrett Wollman 	int s = splnet();
3272c37256eSGarrett Wollman 	int error = 0;
3282c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
3292c37256eSGarrett Wollman 	struct tcpcb *tp;
3302c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3312c37256eSGarrett Wollman 
3322c37256eSGarrett Wollman 	COMMON_START();
3332c37256eSGarrett Wollman 
3342c37256eSGarrett Wollman 	/*
3352c37256eSGarrett Wollman 	 * Must disallow TCP ``connections'' to multicast addresses.
3362c37256eSGarrett Wollman 	 */
33757bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
3382c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
3392c37256eSGarrett Wollman 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
3402c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
3412c37256eSGarrett Wollman 		goto out;
3422c37256eSGarrett Wollman 	}
3432c37256eSGarrett Wollman 
344a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
345a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
34675c13541SPoul-Henning Kamp 
347b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
3482c37256eSGarrett Wollman 		goto out;
3492c37256eSGarrett Wollman 	error = tcp_output(tp);
3502c37256eSGarrett Wollman 	COMMON_END(PRU_CONNECT);
3512c37256eSGarrett Wollman }
3522c37256eSGarrett Wollman 
353fb59c426SYoshinobu Inoue #ifdef INET6
354fb59c426SYoshinobu Inoue static int
355b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
356fb59c426SYoshinobu Inoue {
357fb59c426SYoshinobu Inoue 	int s = splnet();
358fb59c426SYoshinobu Inoue 	int error = 0;
359fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
360fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
361fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
362fb59c426SYoshinobu Inoue 
363fb59c426SYoshinobu Inoue 	COMMON_START();
364fb59c426SYoshinobu Inoue 
365fb59c426SYoshinobu Inoue 	/*
366fb59c426SYoshinobu Inoue 	 * Must disallow TCP ``connections'' to multicast addresses.
367fb59c426SYoshinobu Inoue 	 */
368fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
369fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
370fb59c426SYoshinobu Inoue 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
371fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
372fb59c426SYoshinobu Inoue 		goto out;
373fb59c426SYoshinobu Inoue 	}
374fb59c426SYoshinobu Inoue 
37533841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
376fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
377fb59c426SYoshinobu Inoue 
37833841545SHajimu UMEMOTO 		if (!ip6_mapped_addr_on ||
37933841545SHajimu UMEMOTO 		    (inp->inp_flags & IN6P_IPV6_V6ONLY))
38033841545SHajimu UMEMOTO 			return(EINVAL);
38133841545SHajimu UMEMOTO 
382fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
383fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
384fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
385b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
386fb59c426SYoshinobu Inoue 			goto out;
387fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
388fb59c426SYoshinobu Inoue 		goto out;
389fb59c426SYoshinobu Inoue 	}
390fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
391fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
392b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
393b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
394fb59c426SYoshinobu Inoue 		goto out;
395fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
396fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
397fb59c426SYoshinobu Inoue }
398fb59c426SYoshinobu Inoue #endif /* INET6 */
399fb59c426SYoshinobu Inoue 
4002c37256eSGarrett Wollman /*
4012c37256eSGarrett Wollman  * Initiate disconnect from peer.
4022c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
4032c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
4042c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
4052c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
4062c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
4072c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
4082c37256eSGarrett Wollman  *
4092c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
4102c37256eSGarrett Wollman  */
4112c37256eSGarrett Wollman static int
4122c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
4132c37256eSGarrett Wollman {
4142c37256eSGarrett Wollman 	int s = splnet();
4152c37256eSGarrett Wollman 	int error = 0;
4162c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4172c37256eSGarrett Wollman 	struct tcpcb *tp;
4182c37256eSGarrett Wollman 
4192c37256eSGarrett Wollman 	COMMON_START();
4202c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4212c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4222c37256eSGarrett Wollman }
4232c37256eSGarrett Wollman 
4242c37256eSGarrett Wollman /*
4252c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4262c37256eSGarrett Wollman  * done at higher levels; just return the address
4272c37256eSGarrett Wollman  * of the peer, storing through addr.
4282c37256eSGarrett Wollman  */
4292c37256eSGarrett Wollman static int
43057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4312c37256eSGarrett Wollman {
4322c37256eSGarrett Wollman 	int s = splnet();
4332c37256eSGarrett Wollman 	int error = 0;
4342c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4351db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
4361db24ffbSJonathan Lemon 	TCPDEBUG0;
4372c37256eSGarrett Wollman 
438243917feSSeigo Tanimura 	SOCK_LOCK(so);
439c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
440243917feSSeigo Tanimura 		SOCK_UNLOCK(so);
441c0647e0dSJonathan Lemon 		error = ECONNABORTED;
442c0647e0dSJonathan Lemon 		goto out;
443c0647e0dSJonathan Lemon 	}
444243917feSSeigo Tanimura 	SOCK_UNLOCK(so);
4451db24ffbSJonathan Lemon 	if (inp == 0) {
4461db24ffbSJonathan Lemon 		splx(s);
4471db24ffbSJonathan Lemon 		return (EINVAL);
4481db24ffbSJonathan Lemon 	}
4491db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4501db24ffbSJonathan Lemon 	TCPDEBUG1();
451117bcae7SGarrett Wollman 	in_setpeeraddr(so, nam);
4522c37256eSGarrett Wollman 	COMMON_END(PRU_ACCEPT);
4532c37256eSGarrett Wollman }
4542c37256eSGarrett Wollman 
455fb59c426SYoshinobu Inoue #ifdef INET6
456fb59c426SYoshinobu Inoue static int
457fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
458fb59c426SYoshinobu Inoue {
459fb59c426SYoshinobu Inoue 	int s = splnet();
460fb59c426SYoshinobu Inoue 	int error = 0;
461fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
4621db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
4631db24ffbSJonathan Lemon 	TCPDEBUG0;
464fb59c426SYoshinobu Inoue 
465243917feSSeigo Tanimura 	SOCK_LOCK(so);
466c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
467243917feSSeigo Tanimura 		SOCK_UNLOCK(so);
468c0647e0dSJonathan Lemon 		error = ECONNABORTED;
469c0647e0dSJonathan Lemon 		goto out;
470c0647e0dSJonathan Lemon 	}
471243917feSSeigo Tanimura 	SOCK_UNLOCK(so);
4721db24ffbSJonathan Lemon 	if (inp == 0) {
4731db24ffbSJonathan Lemon 		splx(s);
4741db24ffbSJonathan Lemon 		return (EINVAL);
4751db24ffbSJonathan Lemon 	}
4761db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4771db24ffbSJonathan Lemon 	TCPDEBUG1();
478fb59c426SYoshinobu Inoue 	in6_mapped_peeraddr(so, nam);
479fb59c426SYoshinobu Inoue 	COMMON_END(PRU_ACCEPT);
480fb59c426SYoshinobu Inoue }
481fb59c426SYoshinobu Inoue #endif /* INET6 */
4822c37256eSGarrett Wollman /*
4832c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
4842c37256eSGarrett Wollman  */
4852c37256eSGarrett Wollman static int
4862c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
4872c37256eSGarrett Wollman {
4882c37256eSGarrett Wollman 	int s = splnet();
4892c37256eSGarrett Wollman 	int error = 0;
4902c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4912c37256eSGarrett Wollman 	struct tcpcb *tp;
4922c37256eSGarrett Wollman 
4932c37256eSGarrett Wollman 	COMMON_START();
4942c37256eSGarrett Wollman 	socantsendmore(so);
4952c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
4962c37256eSGarrett Wollman 	if (tp)
4972c37256eSGarrett Wollman 		error = tcp_output(tp);
4982c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
4992c37256eSGarrett Wollman }
5002c37256eSGarrett Wollman 
5012c37256eSGarrett Wollman /*
5022c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
5032c37256eSGarrett Wollman  */
5042c37256eSGarrett Wollman static int
5052c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
5062c37256eSGarrett Wollman {
5072c37256eSGarrett Wollman 	int s = splnet();
5082c37256eSGarrett Wollman 	int error = 0;
5092c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
5102c37256eSGarrett Wollman 	struct tcpcb *tp;
5112c37256eSGarrett Wollman 
5122c37256eSGarrett Wollman 	COMMON_START();
5132c37256eSGarrett Wollman 	tcp_output(tp);
5142c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
5152c37256eSGarrett Wollman }
5162c37256eSGarrett Wollman 
5172c37256eSGarrett Wollman /*
5182c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
5199c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
5209c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
5219c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
5229c9906e9SPeter Wemm  * generally are caller-frees.
5232c37256eSGarrett Wollman  */
5242c37256eSGarrett Wollman static int
52557bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
526b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
5272c37256eSGarrett Wollman {
5282c37256eSGarrett Wollman 	int s = splnet();
5292c37256eSGarrett Wollman 	int error = 0;
5302c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
5312c37256eSGarrett Wollman 	struct tcpcb *tp;
532fb59c426SYoshinobu Inoue #ifdef INET6
533fb59c426SYoshinobu Inoue 	int isipv6;
534fb59c426SYoshinobu Inoue #endif
5359c9906e9SPeter Wemm 	TCPDEBUG0;
5362c37256eSGarrett Wollman 
5379c9906e9SPeter Wemm 	if (inp == NULL) {
5389c9906e9SPeter Wemm 		/*
5399c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
5409c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
5419c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
5429c9906e9SPeter Wemm 		 */
5439c9906e9SPeter Wemm 		if (m)
5449c9906e9SPeter Wemm 			m_freem(m);
5459c9906e9SPeter Wemm 		if (control)
5469c9906e9SPeter Wemm 			m_freem(control);
5479c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
54845d3a132SPeter Wemm 		tp = NULL;
54945d3a132SPeter Wemm 		TCPDEBUG1();
5509c9906e9SPeter Wemm 		goto out;
5519c9906e9SPeter Wemm 	}
552fb59c426SYoshinobu Inoue #ifdef INET6
553fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
554fb59c426SYoshinobu Inoue #endif /* INET6 */
5559c9906e9SPeter Wemm 	tp = intotcpcb(inp);
5569c9906e9SPeter Wemm 	TCPDEBUG1();
5579c9906e9SPeter Wemm 	if (control) {
5589c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
5599c9906e9SPeter Wemm 		if (control->m_len) {
5609c9906e9SPeter Wemm 			m_freem(control);
5612c37256eSGarrett Wollman 			if (m)
5622c37256eSGarrett Wollman 				m_freem(m);
563744f87eaSDavid Greenman 			error = EINVAL;
564744f87eaSDavid Greenman 			goto out;
5652c37256eSGarrett Wollman 		}
5669c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
5679c9906e9SPeter Wemm 	}
5682c37256eSGarrett Wollman 	if(!(flags & PRUS_OOB)) {
5692c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
5702c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
5712c37256eSGarrett Wollman 			/*
5722c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
5732c37256eSGarrett Wollman 			 * initialize window to default value, and
5742c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
5752c37256eSGarrett Wollman 			 * MSS.
5762c37256eSGarrett Wollman 			 */
577fb59c426SYoshinobu Inoue #ifdef INET6
578fb59c426SYoshinobu Inoue 			if (isipv6)
579b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
580fb59c426SYoshinobu Inoue 			else
581fb59c426SYoshinobu Inoue #endif /* INET6 */
582b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
5832c37256eSGarrett Wollman 			if (error)
5842c37256eSGarrett Wollman 				goto out;
5852c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
5862c37256eSGarrett Wollman 			tcp_mss(tp, -1);
5872c37256eSGarrett Wollman 		}
5882c37256eSGarrett Wollman 
5892c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
5902c37256eSGarrett Wollman 			/*
5912c37256eSGarrett Wollman 			 * Close the send side of the connection after
5922c37256eSGarrett Wollman 			 * the data is sent.
5932c37256eSGarrett Wollman 			 */
5942c37256eSGarrett Wollman 			socantsendmore(so);
5952c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
5962c37256eSGarrett Wollman 		}
597b0acefa8SBill Fenner 		if (tp != NULL) {
598b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
599b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
6002c37256eSGarrett Wollman 			error = tcp_output(tp);
601b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
602b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
603b0acefa8SBill Fenner 		}
6042c37256eSGarrett Wollman 	} else {
6052c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
6062c37256eSGarrett Wollman 			m_freem(m);
6072c37256eSGarrett Wollman 			error = ENOBUFS;
6082c37256eSGarrett Wollman 			goto out;
6092c37256eSGarrett Wollman 		}
6102c37256eSGarrett Wollman 		/*
6112c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
6122c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
6132c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
6142c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
6152c37256eSGarrett Wollman 		 * of data past the urgent section.
6162c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
6172c37256eSGarrett Wollman 		 */
6182c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
619ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
620ef53690bSGarrett Wollman 			/*
621ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
622ef53690bSGarrett Wollman 			 * initialize window to default value, and
623ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
624ef53690bSGarrett Wollman 			 * MSS.
625ef53690bSGarrett Wollman 			 */
626fb59c426SYoshinobu Inoue #ifdef INET6
627fb59c426SYoshinobu Inoue 			if (isipv6)
628b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
629fb59c426SYoshinobu Inoue 			else
630fb59c426SYoshinobu Inoue #endif /* INET6 */
631b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
632ef53690bSGarrett Wollman 			if (error)
633ef53690bSGarrett Wollman 				goto out;
634ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
635ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
636ef53690bSGarrett Wollman 		}
6372c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
6382c37256eSGarrett Wollman 		tp->t_force = 1;
6392c37256eSGarrett Wollman 		error = tcp_output(tp);
6402c37256eSGarrett Wollman 		tp->t_force = 0;
6412c37256eSGarrett Wollman 	}
6422c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
6432c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
6442c37256eSGarrett Wollman }
6452c37256eSGarrett Wollman 
6462c37256eSGarrett Wollman /*
6472c37256eSGarrett Wollman  * Abort the TCP.
6482c37256eSGarrett Wollman  */
6492c37256eSGarrett Wollman static int
6502c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
6512c37256eSGarrett Wollman {
6522c37256eSGarrett Wollman 	int s = splnet();
6532c37256eSGarrett Wollman 	int error = 0;
6542c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6552c37256eSGarrett Wollman 	struct tcpcb *tp;
6562c37256eSGarrett Wollman 
6572c37256eSGarrett Wollman 	COMMON_START();
6582c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
6592c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
6602c37256eSGarrett Wollman }
6612c37256eSGarrett Wollman 
6622c37256eSGarrett Wollman /*
6632c37256eSGarrett Wollman  * Receive out-of-band data.
6642c37256eSGarrett Wollman  */
6652c37256eSGarrett Wollman static int
6662c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
6672c37256eSGarrett Wollman {
6682c37256eSGarrett Wollman 	int s = splnet();
6692c37256eSGarrett Wollman 	int error = 0;
6702c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6712c37256eSGarrett Wollman 	struct tcpcb *tp;
6722c37256eSGarrett Wollman 
6732c37256eSGarrett Wollman 	COMMON_START();
674243917feSSeigo Tanimura 	SOCK_LOCK(so);
6752c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
6762c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
677243917feSSeigo Tanimura 	    so->so_options & SO_OOBINLINE) {
678243917feSSeigo Tanimura 		SOCK_UNLOCK(so);
679243917feSSeigo Tanimura 		error = EINVAL;
680243917feSSeigo Tanimura 		goto out;
681243917feSSeigo Tanimura 	}
682243917feSSeigo Tanimura 	SOCK_UNLOCK(so);
683243917feSSeigo Tanimura 	if (tp->t_oobflags & TCPOOB_HADDATA) {
6842c37256eSGarrett Wollman 		error = EINVAL;
6852c37256eSGarrett Wollman 		goto out;
6862c37256eSGarrett Wollman 	}
6872c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
6882c37256eSGarrett Wollman 		error = EWOULDBLOCK;
6892c37256eSGarrett Wollman 		goto out;
6902c37256eSGarrett Wollman 	}
6912c37256eSGarrett Wollman 	m->m_len = 1;
6922c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
6932c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
6942c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
6952c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
6962c37256eSGarrett Wollman }
6972c37256eSGarrett Wollman 
6982c37256eSGarrett Wollman /* xxx - should be const */
6992c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
7002c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
701117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
702117bcae7SGarrett Wollman 	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
703117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
704f8f6cbbaSPeter Wemm 	in_setsockaddr, sosend, soreceive, sopoll
7052c37256eSGarrett Wollman };
706df8bae1dSRodney W. Grimes 
707fb59c426SYoshinobu Inoue #ifdef INET6
708fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
709fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
710fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
711fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
712fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
713fb59c426SYoshinobu Inoue 	in6_mapped_sockaddr, sosend, soreceive, sopoll
714fb59c426SYoshinobu Inoue };
715fb59c426SYoshinobu Inoue #endif /* INET6 */
716fb59c426SYoshinobu Inoue 
717a0292f23SGarrett Wollman /*
718a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
719a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
720a0292f23SGarrett Wollman  * port number if needed.  Call in_pcbladdr to do the routing and to choose
721a0292f23SGarrett Wollman  * a local host address (interface).  If there is an existing incarnation
722a0292f23SGarrett Wollman  * of the same connection in TIME-WAIT state and if the remote host was
723a0292f23SGarrett Wollman  * sending CC options and if the connection duration was < MSL, then
724a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
725a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
726a0292f23SGarrett Wollman  */
7270312fbe9SPoul-Henning Kamp static int
728b40ce416SJulian Elischer tcp_connect(tp, nam, td)
729a0292f23SGarrett Wollman 	register struct tcpcb *tp;
73057bf258eSGarrett Wollman 	struct sockaddr *nam;
731b40ce416SJulian Elischer 	struct thread *td;
732a0292f23SGarrett Wollman {
733a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
734a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
735a0292f23SGarrett Wollman 	struct tcpcb *otp;
73657bf258eSGarrett Wollman 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
737a0292f23SGarrett Wollman 	struct sockaddr_in *ifaddr;
738a45d2726SAndras Olah 	struct rmxp_tao *taop;
739a45d2726SAndras Olah 	struct rmxp_tao tao_noncached;
740c3229e05SDavid Greenman 	int error;
741a0292f23SGarrett Wollman 
742a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
743b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)0, td);
744a0292f23SGarrett Wollman 		if (error)
745a0292f23SGarrett Wollman 			return error;
746a0292f23SGarrett Wollman 	}
747a0292f23SGarrett Wollman 
748a0292f23SGarrett Wollman 	/*
749a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
750a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
751a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
752a0292f23SGarrett Wollman 	 */
753a0292f23SGarrett Wollman 	error = in_pcbladdr(inp, nam, &ifaddr);
754d3628763SRodney W. Grimes 	if (error)
755d3628763SRodney W. Grimes 		return error;
756c3229e05SDavid Greenman 	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
757a0292f23SGarrett Wollman 	    sin->sin_addr, sin->sin_port,
758a0292f23SGarrett Wollman 	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
759a0292f23SGarrett Wollman 						: ifaddr->sin_addr,
760cfa1ca9dSYoshinobu Inoue 	    inp->inp_lport,  0, NULL);
761a0292f23SGarrett Wollman 	if (oinp) {
762a0292f23SGarrett Wollman 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
763a0292f23SGarrett Wollman 		otp->t_state == TCPS_TIME_WAIT &&
7649b8b58e0SJonathan Lemon 		    (ticks - otp->t_starttime) < tcp_msl &&
765a0292f23SGarrett Wollman 		    (otp->t_flags & TF_RCVD_CC))
766a0292f23SGarrett Wollman 			otp = tcp_close(otp);
767a0292f23SGarrett Wollman 		else
768a0292f23SGarrett Wollman 			return EADDRINUSE;
769a0292f23SGarrett Wollman 	}
770a0292f23SGarrett Wollman 	if (inp->inp_laddr.s_addr == INADDR_ANY)
771a0292f23SGarrett Wollman 		inp->inp_laddr = ifaddr->sin_addr;
772a0292f23SGarrett Wollman 	inp->inp_faddr = sin->sin_addr;
773a0292f23SGarrett Wollman 	inp->inp_fport = sin->sin_port;
77415bd2b43SDavid Greenman 	in_pcbrehash(inp);
775a0292f23SGarrett Wollman 
776a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
777a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
778a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
779a0292f23SGarrett Wollman 		tp->request_r_scale++;
780a0292f23SGarrett Wollman 
781243917feSSeigo Tanimura 	SOCK_LOCK(so);
782a0292f23SGarrett Wollman 	soisconnecting(so);
783243917feSSeigo Tanimura 	SOCK_UNLOCK(so);
784a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
785a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
7869b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
787b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
788a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
789a45d2726SAndras Olah 
790a45d2726SAndras Olah 	/*
791a45d2726SAndras Olah 	 * Generate a CC value for this connection and
792a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
793a45d2726SAndras Olah 	 */
794be2ac88cSJonathan Lemon 	if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
795a45d2726SAndras Olah 		taop = &tao_noncached;
796a45d2726SAndras Olah 		bzero(taop, sizeof(*taop));
797a45d2726SAndras Olah 	}
798a45d2726SAndras Olah 
799a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
800a45d2726SAndras Olah 	if (taop->tao_ccsent != 0 &&
801a45d2726SAndras Olah 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
802a45d2726SAndras Olah 		taop->tao_ccsent = tp->cc_send;
803a45d2726SAndras Olah 	} else {
804a45d2726SAndras Olah 		taop->tao_ccsent = 0;
805a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
806a45d2726SAndras Olah 	}
807a0292f23SGarrett Wollman 
808a0292f23SGarrett Wollman 	return 0;
809a0292f23SGarrett Wollman }
810a0292f23SGarrett Wollman 
811fb59c426SYoshinobu Inoue #ifdef INET6
812fb59c426SYoshinobu Inoue static int
813b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
814fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
815fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
816b40ce416SJulian Elischer 	struct thread *td;
817fb59c426SYoshinobu Inoue {
818fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
819fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
820fb59c426SYoshinobu Inoue 	struct tcpcb *otp;
821fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
822fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
823fb59c426SYoshinobu Inoue 	struct rmxp_tao *taop;
824fb59c426SYoshinobu Inoue 	struct rmxp_tao tao_noncached;
825fb59c426SYoshinobu Inoue 	int error;
826fb59c426SYoshinobu Inoue 
827fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
828b40ce416SJulian Elischer 		error = in6_pcbbind(inp, (struct sockaddr *)0, td);
829fb59c426SYoshinobu Inoue 		if (error)
830fb59c426SYoshinobu Inoue 			return error;
831fb59c426SYoshinobu Inoue 	}
832fb59c426SYoshinobu Inoue 
833fb59c426SYoshinobu Inoue 	/*
834fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
835fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
836fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
837fb59c426SYoshinobu Inoue 	 */
838fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
839fb59c426SYoshinobu Inoue 	if (error)
840fb59c426SYoshinobu Inoue 		return error;
841fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
842fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
843fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
844fb59c426SYoshinobu Inoue 				  ? addr6
845fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
846fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
847fb59c426SYoshinobu Inoue 	if (oinp) {
848fb59c426SYoshinobu Inoue 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
849fb59c426SYoshinobu Inoue 		    otp->t_state == TCPS_TIME_WAIT &&
850fb59c426SYoshinobu Inoue 		    (ticks - otp->t_starttime) < tcp_msl &&
851fb59c426SYoshinobu Inoue 		    (otp->t_flags & TF_RCVD_CC))
852fb59c426SYoshinobu Inoue 			otp = tcp_close(otp);
853fb59c426SYoshinobu Inoue 		else
854fb59c426SYoshinobu Inoue 			return EADDRINUSE;
855fb59c426SYoshinobu Inoue 	}
856fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
857fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
858fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
859fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
860fb59c426SYoshinobu Inoue 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
861fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
862fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
863fb59c426SYoshinobu Inoue 
864fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
865fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
866fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
867fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
868fb59c426SYoshinobu Inoue 
869243917feSSeigo Tanimura 	SOCK_LOCK(so);
870fb59c426SYoshinobu Inoue 	soisconnecting(so);
871243917feSSeigo Tanimura 	SOCK_UNLOCK(so);
872fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
873fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
874fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
875b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
876fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
877fb59c426SYoshinobu Inoue 
878fb59c426SYoshinobu Inoue 	/*
879fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
880fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
881fb59c426SYoshinobu Inoue 	 */
882be2ac88cSJonathan Lemon 	if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
883fb59c426SYoshinobu Inoue 		taop = &tao_noncached;
884fb59c426SYoshinobu Inoue 		bzero(taop, sizeof(*taop));
885fb59c426SYoshinobu Inoue 	}
886fb59c426SYoshinobu Inoue 
887fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
888fb59c426SYoshinobu Inoue 	if (taop->tao_ccsent != 0 &&
889fb59c426SYoshinobu Inoue 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
890fb59c426SYoshinobu Inoue 		taop->tao_ccsent = tp->cc_send;
891fb59c426SYoshinobu Inoue 	} else {
892fb59c426SYoshinobu Inoue 		taop->tao_ccsent = 0;
893fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
894fb59c426SYoshinobu Inoue 	}
895fb59c426SYoshinobu Inoue 
896fb59c426SYoshinobu Inoue 	return 0;
897fb59c426SYoshinobu Inoue }
898fb59c426SYoshinobu Inoue #endif /* INET6 */
899fb59c426SYoshinobu Inoue 
900cfe8b629SGarrett Wollman /*
901cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
902cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
903cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
904cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
905cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
906cfe8b629SGarrett Wollman  */
907df8bae1dSRodney W. Grimes int
908cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
909df8bae1dSRodney W. Grimes 	struct socket *so;
910cfe8b629SGarrett Wollman 	struct sockopt *sopt;
911df8bae1dSRodney W. Grimes {
912cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
913df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
914cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
915df8bae1dSRodney W. Grimes 
916cfe8b629SGarrett Wollman 	error = 0;
917cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
918df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
919df8bae1dSRodney W. Grimes 	if (inp == NULL) {
920df8bae1dSRodney W. Grimes 		splx(s);
921df8bae1dSRodney W. Grimes 		return (ECONNRESET);
922df8bae1dSRodney W. Grimes 	}
923cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
924fb59c426SYoshinobu Inoue #ifdef INET6
925fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
926fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
927fb59c426SYoshinobu Inoue 		else
928fb59c426SYoshinobu Inoue #endif /* INET6 */
929cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
930df8bae1dSRodney W. Grimes 		splx(s);
931df8bae1dSRodney W. Grimes 		return (error);
932df8bae1dSRodney W. Grimes 	}
933df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
934df8bae1dSRodney W. Grimes 
935cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
936cfe8b629SGarrett Wollman 	case SOPT_SET:
937cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
938df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
939cfe8b629SGarrett Wollman 		case TCP_NOOPT:
940cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
941cfe8b629SGarrett Wollman 					    sizeof optval);
942cfe8b629SGarrett Wollman 			if (error)
943cfe8b629SGarrett Wollman 				break;
944cfe8b629SGarrett Wollman 
945cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
946cfe8b629SGarrett Wollman 			case TCP_NODELAY:
947cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
948cfe8b629SGarrett Wollman 				break;
949cfe8b629SGarrett Wollman 			case TCP_NOOPT:
950cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
951cfe8b629SGarrett Wollman 				break;
952cfe8b629SGarrett Wollman 			default:
953cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
954cfe8b629SGarrett Wollman 				break;
955cfe8b629SGarrett Wollman 			}
956cfe8b629SGarrett Wollman 
957cfe8b629SGarrett Wollman 			if (optval)
958cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
959df8bae1dSRodney W. Grimes 			else
960cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
961df8bae1dSRodney W. Grimes 			break;
962df8bae1dSRodney W. Grimes 
963007581c0SJonathan Lemon 		case TCP_NOPUSH:
964007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
965007581c0SJonathan Lemon 					    sizeof optval);
966007581c0SJonathan Lemon 			if (error)
967007581c0SJonathan Lemon 				break;
968007581c0SJonathan Lemon 
969007581c0SJonathan Lemon 			if (optval)
970007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
971007581c0SJonathan Lemon 			else {
972007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
973007581c0SJonathan Lemon 				error = tcp_output(tp);
974007581c0SJonathan Lemon 			}
975007581c0SJonathan Lemon 			break;
976007581c0SJonathan Lemon 
977df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
978cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
979cfe8b629SGarrett Wollman 					    sizeof optval);
980cfe8b629SGarrett Wollman 			if (error)
981df8bae1dSRodney W. Grimes 				break;
982df8bae1dSRodney W. Grimes 
983cfe8b629SGarrett Wollman 			if (optval > 0 && optval <= tp->t_maxseg)
984cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
985a0292f23SGarrett Wollman 			else
986a0292f23SGarrett Wollman 				error = EINVAL;
987a0292f23SGarrett Wollman 			break;
988a0292f23SGarrett Wollman 
989df8bae1dSRodney W. Grimes 		default:
990df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
991df8bae1dSRodney W. Grimes 			break;
992df8bae1dSRodney W. Grimes 		}
993df8bae1dSRodney W. Grimes 		break;
994df8bae1dSRodney W. Grimes 
995cfe8b629SGarrett Wollman 	case SOPT_GET:
996cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
997df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
998cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
999df8bae1dSRodney W. Grimes 			break;
1000df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1001cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1002df8bae1dSRodney W. Grimes 			break;
1003a0292f23SGarrett Wollman 		case TCP_NOOPT:
1004cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1005a0292f23SGarrett Wollman 			break;
1006a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1007cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1008a0292f23SGarrett Wollman 			break;
1009df8bae1dSRodney W. Grimes 		default:
1010df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1011df8bae1dSRodney W. Grimes 			break;
1012df8bae1dSRodney W. Grimes 		}
1013cfe8b629SGarrett Wollman 		if (error == 0)
1014cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1015df8bae1dSRodney W. Grimes 		break;
1016df8bae1dSRodney W. Grimes 	}
1017df8bae1dSRodney W. Grimes 	splx(s);
1018df8bae1dSRodney W. Grimes 	return (error);
1019df8bae1dSRodney W. Grimes }
1020df8bae1dSRodney W. Grimes 
102126e30fbbSDavid Greenman /*
102226e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
102326e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
102426e30fbbSDavid Greenman  * be set by the route).
102526e30fbbSDavid Greenman  */
102681e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
10273d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
10283d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
102981e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
10303d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
10313d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1032df8bae1dSRodney W. Grimes 
1033df8bae1dSRodney W. Grimes /*
1034df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1035df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1036df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1037df8bae1dSRodney W. Grimes  */
10380312fbe9SPoul-Henning Kamp static int
1039b40ce416SJulian Elischer tcp_attach(so, td)
1040df8bae1dSRodney W. Grimes 	struct socket *so;
1041b40ce416SJulian Elischer 	struct thread *td;
1042df8bae1dSRodney W. Grimes {
1043df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1044df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1045df8bae1dSRodney W. Grimes 	int error;
1046fb59c426SYoshinobu Inoue #ifdef INET6
1047fb59c426SYoshinobu Inoue 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1048fb59c426SYoshinobu Inoue #endif
1049df8bae1dSRodney W. Grimes 
1050df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1051df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1052df8bae1dSRodney W. Grimes 		if (error)
1053df8bae1dSRodney W. Grimes 			return (error);
1054df8bae1dSRodney W. Grimes 	}
1055b40ce416SJulian Elischer 	error = in_pcballoc(so, &tcbinfo, td);
1056df8bae1dSRodney W. Grimes 	if (error)
1057df8bae1dSRodney W. Grimes 		return (error);
1058df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1059fb59c426SYoshinobu Inoue #ifdef INET6
1060fb59c426SYoshinobu Inoue 	if (isipv6) {
1061fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1062fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1063fb59c426SYoshinobu Inoue 	}
1064fb59c426SYoshinobu Inoue 	else
1065fb59c426SYoshinobu Inoue #endif
1066cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1067df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1068df8bae1dSRodney W. Grimes 	if (tp == 0) {
1069243917feSSeigo Tanimura 		int nofd;
1070df8bae1dSRodney W. Grimes 
1071243917feSSeigo Tanimura 		SOCK_LOCK(so);
1072243917feSSeigo Tanimura 		nofd = so->so_state & SS_NOFDREF;	/* XXX */
1073df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1074243917feSSeigo Tanimura 		SOCK_UNLOCK(so);
1075fb59c426SYoshinobu Inoue #ifdef INET6
1076fb59c426SYoshinobu Inoue 		if (isipv6)
1077fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1078fb59c426SYoshinobu Inoue 		else
1079fb59c426SYoshinobu Inoue #endif
1080df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1081243917feSSeigo Tanimura 		SOCK_LOCK(so);
1082df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1083243917feSSeigo Tanimura 		SOCK_UNLOCK(so);
1084df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1085df8bae1dSRodney W. Grimes 	}
1086df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1087df8bae1dSRodney W. Grimes 	return (0);
1088df8bae1dSRodney W. Grimes }
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes /*
1091df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1092df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1093df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1094df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1095df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1096df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1097df8bae1dSRodney W. Grimes  */
10980312fbe9SPoul-Henning Kamp static struct tcpcb *
1099df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1100df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1101df8bae1dSRodney W. Grimes {
1102df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1103df8bae1dSRodney W. Grimes 
1104df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1105df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1106df8bae1dSRodney W. Grimes 	else {
1107243917feSSeigo Tanimura 		SOCK_LOCK(so);
1108243917feSSeigo Tanimura 		if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1109243917feSSeigo Tanimura 			SOCK_UNLOCK(so);
1110243917feSSeigo Tanimura 			tp = tcp_drop(tp, 0);
1111243917feSSeigo Tanimura 		} else {
1112df8bae1dSRodney W. Grimes 			soisdisconnecting(so);
1113243917feSSeigo Tanimura 			SOCK_UNLOCK(so);
1114df8bae1dSRodney W. Grimes 			sbflush(&so->so_rcv);
1115df8bae1dSRodney W. Grimes 			tp = tcp_usrclosed(tp);
1116df8bae1dSRodney W. Grimes 			if (tp)
1117df8bae1dSRodney W. Grimes 				(void) tcp_output(tp);
1118df8bae1dSRodney W. Grimes 		}
1119243917feSSeigo Tanimura 	}
1120df8bae1dSRodney W. Grimes 	return (tp);
1121df8bae1dSRodney W. Grimes }
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes /*
1124df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1125df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1126df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1127df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1128df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1129df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1130df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1131df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1132df8bae1dSRodney W. Grimes  */
11330312fbe9SPoul-Henning Kamp static struct tcpcb *
1134df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1135df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1136df8bae1dSRodney W. Grimes {
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1139df8bae1dSRodney W. Grimes 
1140df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1141df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1142df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1143df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1144df8bae1dSRodney W. Grimes 		break;
1145df8bae1dSRodney W. Grimes 
1146a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1147df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1148a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1149a0292f23SGarrett Wollman 		break;
1150a0292f23SGarrett Wollman 
1151df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1152df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1153df8bae1dSRodney W. Grimes 		break;
1154df8bae1dSRodney W. Grimes 
1155df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1156df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1157df8bae1dSRodney W. Grimes 		break;
1158df8bae1dSRodney W. Grimes 	}
1159b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1160243917feSSeigo Tanimura 		SOCK_LOCK(tp->t_inpcb->inp_socket);
1161df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1162243917feSSeigo Tanimura 		SOCK_UNLOCK(tp->t_inpcb->inp_socket);
1163b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1164b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
11659b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
11669b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1167b6239c4aSAndras Olah 	}
1168df8bae1dSRodney W. Grimes 	return (tp);
1169df8bae1dSRodney W. Grimes }
1170a0292f23SGarrett Wollman 
1171