xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 26ef6ac4df851b90d6844f7ab7294f842f7c0450)
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>
43f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
44c7a82f90SGarrett Wollman #include <sys/kernel.h>
4598163b98SPoul-Henning Kamp #include <sys/sysctl.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47fb59c426SYoshinobu Inoue #ifdef INET6
48fb59c426SYoshinobu Inoue #include <sys/domain.h>
49fb59c426SYoshinobu Inoue #endif /* INET6 */
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5391421ba2SRobert Watson #include <sys/proc.h>
5491421ba2SRobert Watson #include <sys/jail.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <netinet/in.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
61fb59c426SYoshinobu Inoue #ifdef INET6
62fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
63fb59c426SYoshinobu Inoue #endif
64df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
65fb59c426SYoshinobu Inoue #ifdef INET6
66fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
67fb59c426SYoshinobu Inoue #endif
68b5e8ce9fSBruce Evans #include <netinet/in_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
70fb59c426SYoshinobu Inoue #ifdef INET6
71fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
72fb59c426SYoshinobu Inoue #endif
73df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
74df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
75df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
79610ee2f9SDavid Greenman #ifdef TCPDEBUG
80df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
81610ee2f9SDavid Greenman #endif
82df8bae1dSRodney W. Grimes 
836a800098SYoshinobu Inoue #ifdef IPSEC
846a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
856a800098SYoshinobu Inoue #endif /*IPSEC*/
866a800098SYoshinobu Inoue 
87df8bae1dSRodney W. Grimes /*
88df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
89df8bae1dSRodney W. Grimes  */
90117bcae7SGarrett Wollman extern	char *tcpstates[];	/* XXX ??? */
91df8bae1dSRodney W. Grimes 
924d77a549SAlfred Perlstein static int	tcp_attach(struct socket *, struct thread *td);
934d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
944d77a549SAlfred Perlstein 		    struct thread *td);
95fb59c426SYoshinobu Inoue #ifdef INET6
964d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
974d77a549SAlfred Perlstein 		    struct thread *td);
98fb59c426SYoshinobu Inoue #endif /* INET6 */
990312fbe9SPoul-Henning Kamp static struct tcpcb *
1004d77a549SAlfred Perlstein 		tcp_disconnect(struct tcpcb *);
1010312fbe9SPoul-Henning Kamp static struct tcpcb *
1024d77a549SAlfred Perlstein 		tcp_usrclosed(struct tcpcb *);
1032c37256eSGarrett Wollman 
1042c37256eSGarrett Wollman #ifdef TCPDEBUG
1051db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1062c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1074cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1084cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1092c37256eSGarrett Wollman #else
1102c37256eSGarrett Wollman #define	TCPDEBUG0
1112c37256eSGarrett Wollman #define	TCPDEBUG1()
1122c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1132c37256eSGarrett Wollman #endif
1142c37256eSGarrett Wollman 
1152c37256eSGarrett Wollman /*
1162c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1172c37256eSGarrett Wollman  * and an internet control block.
1182c37256eSGarrett Wollman  */
1192c37256eSGarrett Wollman static int
120b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1212c37256eSGarrett Wollman {
1222c37256eSGarrett Wollman 	int s = splnet();
1232c37256eSGarrett Wollman 	int error;
124f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1252c37256eSGarrett Wollman 	struct tcpcb *tp = 0;
1262c37256eSGarrett Wollman 	TCPDEBUG0;
1272c37256eSGarrett Wollman 
128f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
1292c37256eSGarrett Wollman 	TCPDEBUG1();
130f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
1312c37256eSGarrett Wollman 	if (inp) {
1322c37256eSGarrett Wollman 		error = EISCONN;
1332c37256eSGarrett Wollman 		goto out;
1342c37256eSGarrett Wollman 	}
1352c37256eSGarrett Wollman 
136b40ce416SJulian Elischer 	error = tcp_attach(so, td);
1372c37256eSGarrett Wollman 	if (error)
1382c37256eSGarrett Wollman 		goto out;
1392c37256eSGarrett Wollman 
1402c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1413879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
142f76fcf6dSJeffrey Hsu 
143f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
144f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1452c37256eSGarrett Wollman out:
1462c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
147f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
1482c37256eSGarrett Wollman 	splx(s);
1492c37256eSGarrett Wollman 	return error;
1502c37256eSGarrett Wollman }
1512c37256eSGarrett Wollman 
1522c37256eSGarrett Wollman /*
1532c37256eSGarrett Wollman  * pru_detach() detaches the TCP protocol from the socket.
1542c37256eSGarrett Wollman  * If the protocol state is non-embryonic, then can't
1552c37256eSGarrett Wollman  * do this directly: have to initiate a pru_disconnect(),
1562c37256eSGarrett Wollman  * which may finish later; embryonic TCB's can just
1572c37256eSGarrett Wollman  * be discarded here.
1582c37256eSGarrett Wollman  */
1592c37256eSGarrett Wollman static int
1602c37256eSGarrett Wollman tcp_usr_detach(struct socket *so)
1612c37256eSGarrett Wollman {
1622c37256eSGarrett Wollman 	int s = splnet();
1632c37256eSGarrett Wollman 	int error = 0;
164f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1652c37256eSGarrett Wollman 	struct tcpcb *tp;
1662c37256eSGarrett Wollman 	TCPDEBUG0;
1672c37256eSGarrett Wollman 
168f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
169f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
1702c37256eSGarrett Wollman 	if (inp == 0) {
171f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
1722c37256eSGarrett Wollman 		splx(s);
1732c37256eSGarrett Wollman 		return EINVAL;	/* XXX */
1742c37256eSGarrett Wollman 	}
175f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1762c37256eSGarrett Wollman 	tp = intotcpcb(inp);
1772c37256eSGarrett Wollman 	TCPDEBUG1();
1782c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
1792c37256eSGarrett Wollman 
1802c37256eSGarrett Wollman 	TCPDEBUG2(PRU_DETACH);
181f76fcf6dSJeffrey Hsu 	if (tp)
182f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
183f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
1842c37256eSGarrett Wollman 	splx(s);
1852c37256eSGarrett Wollman 	return error;
1862c37256eSGarrett Wollman }
1872c37256eSGarrett Wollman 
188f76fcf6dSJeffrey Hsu #define INI_NOLOCK	0
189f76fcf6dSJeffrey Hsu #define INI_READ	1
190f76fcf6dSJeffrey Hsu #define INI_WRITE	2
191f76fcf6dSJeffrey Hsu 
192f76fcf6dSJeffrey Hsu #define	COMMON_START()						\
193f76fcf6dSJeffrey Hsu 	TCPDEBUG0;						\
1942c37256eSGarrett Wollman 	do {							\
195f76fcf6dSJeffrey Hsu 		if (inirw == INI_READ)				\
196f76fcf6dSJeffrey Hsu 			INP_INFO_RLOCK(&tcbinfo);		\
197f76fcf6dSJeffrey Hsu 		else if (inirw == INI_WRITE)			\
198f76fcf6dSJeffrey Hsu 			INP_INFO_WLOCK(&tcbinfo);		\
199f76fcf6dSJeffrey Hsu 		inp = sotoinpcb(so);				\
2002c37256eSGarrett Wollman 		if (inp == 0) {					\
201f76fcf6dSJeffrey Hsu 			if (inirw == INI_READ)			\
202f76fcf6dSJeffrey Hsu 				INP_INFO_RUNLOCK(&tcbinfo);	\
203f76fcf6dSJeffrey Hsu 			else if (inirw == INI_WRITE)		\
204f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);	\
2052c37256eSGarrett Wollman 			splx(s);				\
2062c37256eSGarrett Wollman 			return EINVAL;				\
2072c37256eSGarrett Wollman 		}						\
208f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);					\
209f76fcf6dSJeffrey Hsu 		if (inirw == INI_READ)				\
210f76fcf6dSJeffrey Hsu 			INP_INFO_RUNLOCK(&tcbinfo);		\
2112c37256eSGarrett Wollman 		tp = intotcpcb(inp);				\
2122c37256eSGarrett Wollman 		TCPDEBUG1();					\
2132c37256eSGarrett Wollman } while(0)
2142c37256eSGarrett Wollman 
215f76fcf6dSJeffrey Hsu #define COMMON_END(req)						\
216f76fcf6dSJeffrey Hsu out:	TCPDEBUG2(req);						\
217f76fcf6dSJeffrey Hsu 	do {							\
218f76fcf6dSJeffrey Hsu 		if (tp)						\
219f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);			\
220f76fcf6dSJeffrey Hsu 		if (inirw == INI_WRITE)				\
221f76fcf6dSJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);		\
222f76fcf6dSJeffrey Hsu 		splx(s);					\
223f76fcf6dSJeffrey Hsu 		return error;					\
224f76fcf6dSJeffrey Hsu 		goto out;					\
225f76fcf6dSJeffrey Hsu } while(0)
2262c37256eSGarrett Wollman 
2272c37256eSGarrett Wollman /*
2282c37256eSGarrett Wollman  * Give the socket an address.
2292c37256eSGarrett Wollman  */
2302c37256eSGarrett Wollman static int
231b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2322c37256eSGarrett Wollman {
2332c37256eSGarrett Wollman 	int s = splnet();
2342c37256eSGarrett Wollman 	int error = 0;
235f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
2362c37256eSGarrett Wollman 	struct tcpcb *tp;
2372c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
238f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
2392c37256eSGarrett Wollman 
2402c37256eSGarrett Wollman 	COMMON_START();
2412c37256eSGarrett Wollman 
2422c37256eSGarrett Wollman 	/*
2432c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2442c37256eSGarrett Wollman 	 * to them.
2452c37256eSGarrett Wollman 	 */
24657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
2472c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
2482c37256eSGarrett Wollman 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
2492c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
2502c37256eSGarrett Wollman 		goto out;
2512c37256eSGarrett Wollman 	}
252b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
2532c37256eSGarrett Wollman 	if (error)
2542c37256eSGarrett Wollman 		goto out;
2552c37256eSGarrett Wollman 	COMMON_END(PRU_BIND);
2562c37256eSGarrett Wollman }
2572c37256eSGarrett Wollman 
258fb59c426SYoshinobu Inoue #ifdef INET6
259fb59c426SYoshinobu Inoue static int
260b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
261fb59c426SYoshinobu Inoue {
262fb59c426SYoshinobu Inoue 	int s = splnet();
263fb59c426SYoshinobu Inoue 	int error = 0;
264f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
265fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
266fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
267f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
268fb59c426SYoshinobu Inoue 
269fb59c426SYoshinobu Inoue 	COMMON_START();
270fb59c426SYoshinobu Inoue 
271fb59c426SYoshinobu Inoue 	/*
272fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
273fb59c426SYoshinobu Inoue 	 * to them.
274fb59c426SYoshinobu Inoue 	 */
275fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
276fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
277fb59c426SYoshinobu Inoue 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
278fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
279fb59c426SYoshinobu Inoue 		goto out;
280fb59c426SYoshinobu Inoue 	}
281fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
282fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
28366ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
284fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
285fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
286fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
287fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
288fb59c426SYoshinobu Inoue 
289fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
290fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
291fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
292b40ce416SJulian Elischer 			error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
293fb59c426SYoshinobu Inoue 			goto out;
294fb59c426SYoshinobu Inoue 		}
295fb59c426SYoshinobu Inoue 	}
296b40ce416SJulian Elischer 	error = in6_pcbbind(inp, nam, td);
297fb59c426SYoshinobu Inoue 	if (error)
298fb59c426SYoshinobu Inoue 		goto out;
299fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
300fb59c426SYoshinobu Inoue }
301fb59c426SYoshinobu Inoue #endif /* INET6 */
302fb59c426SYoshinobu Inoue 
3032c37256eSGarrett Wollman /*
3042c37256eSGarrett Wollman  * Prepare to accept connections.
3052c37256eSGarrett Wollman  */
3062c37256eSGarrett Wollman static int
307b40ce416SJulian Elischer tcp_usr_listen(struct socket *so, struct thread *td)
3082c37256eSGarrett Wollman {
3092c37256eSGarrett Wollman 	int s = splnet();
3102c37256eSGarrett Wollman 	int error = 0;
311f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3122c37256eSGarrett Wollman 	struct tcpcb *tp;
313f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
3142c37256eSGarrett Wollman 
3152c37256eSGarrett Wollman 	COMMON_START();
3162c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
317b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)0, td);
3182c37256eSGarrett Wollman 	if (error == 0)
3192c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
3202c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
3212c37256eSGarrett Wollman }
3222c37256eSGarrett Wollman 
323fb59c426SYoshinobu Inoue #ifdef INET6
324fb59c426SYoshinobu Inoue static int
325b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td)
326fb59c426SYoshinobu Inoue {
327fb59c426SYoshinobu Inoue 	int s = splnet();
328fb59c426SYoshinobu Inoue 	int error = 0;
329f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
330fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
331f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
332fb59c426SYoshinobu Inoue 
333fb59c426SYoshinobu Inoue 	COMMON_START();
334fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
335fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
33666ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
337fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
338b40ce416SJulian Elischer 		error = in6_pcbbind(inp, (struct sockaddr *)0, td);
339fb59c426SYoshinobu Inoue 	}
340fb59c426SYoshinobu Inoue 	if (error == 0)
341fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
342fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
343fb59c426SYoshinobu Inoue }
344fb59c426SYoshinobu Inoue #endif /* INET6 */
345fb59c426SYoshinobu Inoue 
3462c37256eSGarrett Wollman /*
3472c37256eSGarrett Wollman  * Initiate connection to peer.
3482c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3492c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3502c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3512c37256eSGarrett Wollman  * Send initial segment on connection.
3522c37256eSGarrett Wollman  */
3532c37256eSGarrett Wollman static int
354b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
3552c37256eSGarrett Wollman {
3562c37256eSGarrett Wollman 	int s = splnet();
3572c37256eSGarrett Wollman 	int error = 0;
358f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3592c37256eSGarrett Wollman 	struct tcpcb *tp;
3602c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
361f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
3622c37256eSGarrett Wollman 
3632c37256eSGarrett Wollman 	COMMON_START();
3642c37256eSGarrett Wollman 
3652c37256eSGarrett Wollman 	/*
3662c37256eSGarrett Wollman 	 * Must disallow TCP ``connections'' to multicast addresses.
3672c37256eSGarrett Wollman 	 */
36857bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
3692c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
3702c37256eSGarrett Wollman 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
3712c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
3722c37256eSGarrett Wollman 		goto out;
3732c37256eSGarrett Wollman 	}
3742c37256eSGarrett Wollman 
375a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
376a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
37775c13541SPoul-Henning Kamp 
378b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
3792c37256eSGarrett Wollman 		goto out;
3802c37256eSGarrett Wollman 	error = tcp_output(tp);
3812c37256eSGarrett Wollman 	COMMON_END(PRU_CONNECT);
3822c37256eSGarrett Wollman }
3832c37256eSGarrett Wollman 
384fb59c426SYoshinobu Inoue #ifdef INET6
385fb59c426SYoshinobu Inoue static int
386b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
387fb59c426SYoshinobu Inoue {
388fb59c426SYoshinobu Inoue 	int s = splnet();
389fb59c426SYoshinobu Inoue 	int error = 0;
390f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
391fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
392fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
393f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
394fb59c426SYoshinobu Inoue 
395fb59c426SYoshinobu Inoue 	COMMON_START();
396fb59c426SYoshinobu Inoue 
397fb59c426SYoshinobu Inoue 	/*
398fb59c426SYoshinobu Inoue 	 * Must disallow TCP ``connections'' to multicast addresses.
399fb59c426SYoshinobu Inoue 	 */
400fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
401fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
402fb59c426SYoshinobu Inoue 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
403fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
404fb59c426SYoshinobu Inoue 		goto out;
405fb59c426SYoshinobu Inoue 	}
406fb59c426SYoshinobu Inoue 
40733841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
408fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
409fb59c426SYoshinobu Inoue 
410d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
411d46a5312SMaxim Konovalov 			error = EINVAL;
412d46a5312SMaxim Konovalov 			goto out;
413d46a5312SMaxim Konovalov 		}
41433841545SHajimu UMEMOTO 
415fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
416fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
417fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
418b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
419fb59c426SYoshinobu Inoue 			goto out;
420fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
421fb59c426SYoshinobu Inoue 		goto out;
422fb59c426SYoshinobu Inoue 	}
423fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
424fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
425b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
426b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
427fb59c426SYoshinobu Inoue 		goto out;
428fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
429fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
430fb59c426SYoshinobu Inoue }
431fb59c426SYoshinobu Inoue #endif /* INET6 */
432fb59c426SYoshinobu Inoue 
4332c37256eSGarrett Wollman /*
4342c37256eSGarrett Wollman  * Initiate disconnect from peer.
4352c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
4362c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
4372c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
4382c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
4392c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
4402c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
4412c37256eSGarrett Wollman  *
4422c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
4432c37256eSGarrett Wollman  */
4442c37256eSGarrett Wollman static int
4452c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
4462c37256eSGarrett Wollman {
4472c37256eSGarrett Wollman 	int s = splnet();
4482c37256eSGarrett Wollman 	int error = 0;
449f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
4502c37256eSGarrett Wollman 	struct tcpcb *tp;
451f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
4522c37256eSGarrett Wollman 
4532c37256eSGarrett Wollman 	COMMON_START();
4542c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4552c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4562c37256eSGarrett Wollman }
4572c37256eSGarrett Wollman 
4582c37256eSGarrett Wollman /*
4592c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4602c37256eSGarrett Wollman  * done at higher levels; just return the address
4612c37256eSGarrett Wollman  * of the peer, storing through addr.
4622c37256eSGarrett Wollman  */
4632c37256eSGarrett Wollman static int
46457bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4652c37256eSGarrett Wollman {
466f76fcf6dSJeffrey Hsu 	int s;
4672c37256eSGarrett Wollman 	int error = 0;
468f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
4691db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
47026ef6ac4SDon Lewis 	struct in_addr addr;
47126ef6ac4SDon Lewis 	in_port_t port = 0;
4721db24ffbSJonathan Lemon 	TCPDEBUG0;
4732c37256eSGarrett Wollman 
474c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
475c0647e0dSJonathan Lemon 		error = ECONNABORTED;
476c0647e0dSJonathan Lemon 		goto out;
477c0647e0dSJonathan Lemon 	}
478f76fcf6dSJeffrey Hsu 
479f76fcf6dSJeffrey Hsu 	s = splnet();
480f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
481f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
482f76fcf6dSJeffrey Hsu 	if (!inp) {
483f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
4841db24ffbSJonathan Lemon 		splx(s);
4851db24ffbSJonathan Lemon 		return (EINVAL);
4861db24ffbSJonathan Lemon 	}
487f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
488f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
4891db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4901db24ffbSJonathan Lemon 	TCPDEBUG1();
491f76fcf6dSJeffrey Hsu 
492f76fcf6dSJeffrey Hsu 	/*
49326ef6ac4SDon Lewis 	 * We inline in_setpeeraddr and COMMON_END here, so that we can
49426ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
49526ef6ac4SDon Lewis 	 * release the lock.
496f76fcf6dSJeffrey Hsu 	 */
49726ef6ac4SDon Lewis 	port = inp->inp_fport;
49826ef6ac4SDon Lewis 	addr = inp->inp_faddr;
499f76fcf6dSJeffrey Hsu 
50026ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
50126ef6ac4SDon Lewis 	if (tp)
50226ef6ac4SDon Lewis 		INP_UNLOCK(inp);
50326ef6ac4SDon Lewis 	splx(s);
50426ef6ac4SDon Lewis 	if (error == 0)
50526ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
50626ef6ac4SDon Lewis 	return error;
5072c37256eSGarrett Wollman }
5082c37256eSGarrett Wollman 
509fb59c426SYoshinobu Inoue #ifdef INET6
510fb59c426SYoshinobu Inoue static int
511fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
512fb59c426SYoshinobu Inoue {
513f76fcf6dSJeffrey Hsu 	int s;
514f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
515fb59c426SYoshinobu Inoue 	int error = 0;
5161db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
51726ef6ac4SDon Lewis 	struct in_addr addr;
51826ef6ac4SDon Lewis 	struct in6_addr addr6;
51926ef6ac4SDon Lewis 	in_port_t port = 0;
52026ef6ac4SDon Lewis 	int v4 = 0;
5211db24ffbSJonathan Lemon 	TCPDEBUG0;
522fb59c426SYoshinobu Inoue 
523c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
524c0647e0dSJonathan Lemon 		error = ECONNABORTED;
525c0647e0dSJonathan Lemon 		goto out;
526c0647e0dSJonathan Lemon 	}
527f76fcf6dSJeffrey Hsu 
528f76fcf6dSJeffrey Hsu 	s = splnet();
529f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
530f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
5311db24ffbSJonathan Lemon 	if (inp == 0) {
532f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
5331db24ffbSJonathan Lemon 		splx(s);
5341db24ffbSJonathan Lemon 		return (EINVAL);
5351db24ffbSJonathan Lemon 	}
536f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
537f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
5381db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
5391db24ffbSJonathan Lemon 	TCPDEBUG1();
54026ef6ac4SDon Lewis 	/*
54126ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
54226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
54326ef6ac4SDon Lewis 	 * release the lock.
54426ef6ac4SDon Lewis 	 */
54526ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
54626ef6ac4SDon Lewis 		v4 = 1;
54726ef6ac4SDon Lewis 		port = inp->inp_fport;
54826ef6ac4SDon Lewis 		addr = inp->inp_faddr;
54926ef6ac4SDon Lewis 	} else {
55026ef6ac4SDon Lewis 		port = inp->inp_fport;
55126ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
55226ef6ac4SDon Lewis 	}
55326ef6ac4SDon Lewis 
55426ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
55526ef6ac4SDon Lewis 	if (tp)
55626ef6ac4SDon Lewis 		INP_UNLOCK(inp);
55726ef6ac4SDon Lewis 	splx(s);
55826ef6ac4SDon Lewis 	if (error == 0) {
55926ef6ac4SDon Lewis 		if (v4)
56026ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
56126ef6ac4SDon Lewis 		else
56226ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
56326ef6ac4SDon Lewis 	}
56426ef6ac4SDon Lewis 	return error;
565fb59c426SYoshinobu Inoue }
566fb59c426SYoshinobu Inoue #endif /* INET6 */
567f76fcf6dSJeffrey Hsu 
568f76fcf6dSJeffrey Hsu /*
569f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
570f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
571f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
572f76fcf6dSJeffrey Hsu  */
573f76fcf6dSJeffrey Hsu static int
574f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
575f76fcf6dSJeffrey Hsu {
576f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
577f76fcf6dSJeffrey Hsu }
578f76fcf6dSJeffrey Hsu 
579f76fcf6dSJeffrey Hsu /*
580f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
581f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
582f76fcf6dSJeffrey Hsu  */
583f76fcf6dSJeffrey Hsu static int
584f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
585f76fcf6dSJeffrey Hsu {
586f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
587f76fcf6dSJeffrey Hsu }
588f76fcf6dSJeffrey Hsu 
5892c37256eSGarrett Wollman /*
5902c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
5912c37256eSGarrett Wollman  */
5922c37256eSGarrett Wollman static int
5932c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
5942c37256eSGarrett Wollman {
5952c37256eSGarrett Wollman 	int s = splnet();
5962c37256eSGarrett Wollman 	int error = 0;
597f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
5982c37256eSGarrett Wollman 	struct tcpcb *tp;
599f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
6002c37256eSGarrett Wollman 
6012c37256eSGarrett Wollman 	COMMON_START();
6022c37256eSGarrett Wollman 	socantsendmore(so);
6032c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
6042c37256eSGarrett Wollman 	if (tp)
6052c37256eSGarrett Wollman 		error = tcp_output(tp);
6062c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
6072c37256eSGarrett Wollman }
6082c37256eSGarrett Wollman 
6092c37256eSGarrett Wollman /*
6102c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
6112c37256eSGarrett Wollman  */
6122c37256eSGarrett Wollman static int
6132c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
6142c37256eSGarrett Wollman {
6152c37256eSGarrett Wollman 	int s = splnet();
6162c37256eSGarrett Wollman 	int error = 0;
617f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6182c37256eSGarrett Wollman 	struct tcpcb *tp;
619f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
6202c37256eSGarrett Wollman 
6212c37256eSGarrett Wollman 	COMMON_START();
6222c37256eSGarrett Wollman 	tcp_output(tp);
6232c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
6242c37256eSGarrett Wollman }
6252c37256eSGarrett Wollman 
6262c37256eSGarrett Wollman /*
6272c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
6289c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
6299c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
6309c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
6319c9906e9SPeter Wemm  * generally are caller-frees.
6322c37256eSGarrett Wollman  */
6332c37256eSGarrett Wollman static int
63457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
635b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
6362c37256eSGarrett Wollman {
6372c37256eSGarrett Wollman 	int s = splnet();
6382c37256eSGarrett Wollman 	int error = 0;
639f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6402c37256eSGarrett Wollman 	struct tcpcb *tp;
641f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
642fb59c426SYoshinobu Inoue #ifdef INET6
643fb59c426SYoshinobu Inoue 	int isipv6;
644fb59c426SYoshinobu Inoue #endif
6459c9906e9SPeter Wemm 	TCPDEBUG0;
6462c37256eSGarrett Wollman 
647f76fcf6dSJeffrey Hsu 	/*
648f76fcf6dSJeffrey Hsu 	 * Need write lock here because this function might call
649f76fcf6dSJeffrey Hsu 	 * tcp_connect or tcp_usrclosed.
650f76fcf6dSJeffrey Hsu 	 * We really want to have to this function upgrade from read lock
651f76fcf6dSJeffrey Hsu 	 * to write lock.  XXX
652f76fcf6dSJeffrey Hsu 	 */
653f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
654f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
6559c9906e9SPeter Wemm 	if (inp == NULL) {
6569c9906e9SPeter Wemm 		/*
6579c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
6589c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
6599c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
6609c9906e9SPeter Wemm 		 */
6619c9906e9SPeter Wemm 		if (m)
6629c9906e9SPeter Wemm 			m_freem(m);
6639c9906e9SPeter Wemm 		if (control)
6649c9906e9SPeter Wemm 			m_freem(control);
6659c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
66645d3a132SPeter Wemm 		tp = NULL;
66745d3a132SPeter Wemm 		TCPDEBUG1();
6689c9906e9SPeter Wemm 		goto out;
6699c9906e9SPeter Wemm 	}
670f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
671fb59c426SYoshinobu Inoue #ifdef INET6
672fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
673fb59c426SYoshinobu Inoue #endif /* INET6 */
6749c9906e9SPeter Wemm 	tp = intotcpcb(inp);
6759c9906e9SPeter Wemm 	TCPDEBUG1();
6769c9906e9SPeter Wemm 	if (control) {
6779c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
6789c9906e9SPeter Wemm 		if (control->m_len) {
6799c9906e9SPeter Wemm 			m_freem(control);
6802c37256eSGarrett Wollman 			if (m)
6812c37256eSGarrett Wollman 				m_freem(m);
682744f87eaSDavid Greenman 			error = EINVAL;
683744f87eaSDavid Greenman 			goto out;
6842c37256eSGarrett Wollman 		}
6859c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
6869c9906e9SPeter Wemm 	}
6872c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
6882c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
6892c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
6902c37256eSGarrett Wollman 			/*
6912c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
6922c37256eSGarrett Wollman 			 * initialize window to default value, and
6932c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
6942c37256eSGarrett Wollman 			 * MSS.
6952c37256eSGarrett Wollman 			 */
696fb59c426SYoshinobu Inoue #ifdef INET6
697fb59c426SYoshinobu Inoue 			if (isipv6)
698b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
699fb59c426SYoshinobu Inoue 			else
700fb59c426SYoshinobu Inoue #endif /* INET6 */
701b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
7022c37256eSGarrett Wollman 			if (error)
7032c37256eSGarrett Wollman 				goto out;
7042c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
7052c37256eSGarrett Wollman 			tcp_mss(tp, -1);
7062c37256eSGarrett Wollman 		}
7072c37256eSGarrett Wollman 
7082c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
7092c37256eSGarrett Wollman 			/*
7102c37256eSGarrett Wollman 			 * Close the send side of the connection after
7112c37256eSGarrett Wollman 			 * the data is sent.
7122c37256eSGarrett Wollman 			 */
7132c37256eSGarrett Wollman 			socantsendmore(so);
7142c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
7152c37256eSGarrett Wollman 		}
716b0acefa8SBill Fenner 		if (tp != NULL) {
717b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
718b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
7192c37256eSGarrett Wollman 			error = tcp_output(tp);
720b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
721b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
722b0acefa8SBill Fenner 		}
7232c37256eSGarrett Wollman 	} else {
7242c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
7252c37256eSGarrett Wollman 			m_freem(m);
7262c37256eSGarrett Wollman 			error = ENOBUFS;
7272c37256eSGarrett Wollman 			goto out;
7282c37256eSGarrett Wollman 		}
7292c37256eSGarrett Wollman 		/*
7302c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
7312c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
7322c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
7332c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
7342c37256eSGarrett Wollman 		 * of data past the urgent section.
7352c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
7362c37256eSGarrett Wollman 		 */
7372c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
738ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
739ef53690bSGarrett Wollman 			/*
740ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
741ef53690bSGarrett Wollman 			 * initialize window to default value, and
742ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
743ef53690bSGarrett Wollman 			 * MSS.
744ef53690bSGarrett Wollman 			 */
745fb59c426SYoshinobu Inoue #ifdef INET6
746fb59c426SYoshinobu Inoue 			if (isipv6)
747b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
748fb59c426SYoshinobu Inoue 			else
749fb59c426SYoshinobu Inoue #endif /* INET6 */
750b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
751ef53690bSGarrett Wollman 			if (error)
752ef53690bSGarrett Wollman 				goto out;
753ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
754ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
755ef53690bSGarrett Wollman 		}
7562c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
7572c37256eSGarrett Wollman 		tp->t_force = 1;
7582c37256eSGarrett Wollman 		error = tcp_output(tp);
7592c37256eSGarrett Wollman 		tp->t_force = 0;
7602c37256eSGarrett Wollman 	}
7612c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
7622c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
7632c37256eSGarrett Wollman }
7642c37256eSGarrett Wollman 
7652c37256eSGarrett Wollman /*
7662c37256eSGarrett Wollman  * Abort the TCP.
7672c37256eSGarrett Wollman  */
7682c37256eSGarrett Wollman static int
7692c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
7702c37256eSGarrett Wollman {
7712c37256eSGarrett Wollman 	int s = splnet();
7722c37256eSGarrett Wollman 	int error = 0;
773f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7742c37256eSGarrett Wollman 	struct tcpcb *tp;
775f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
7762c37256eSGarrett Wollman 
7772c37256eSGarrett Wollman 	COMMON_START();
7782c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
7792c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
7802c37256eSGarrett Wollman }
7812c37256eSGarrett Wollman 
7822c37256eSGarrett Wollman /*
7832c37256eSGarrett Wollman  * Receive out-of-band data.
7842c37256eSGarrett Wollman  */
7852c37256eSGarrett Wollman static int
7862c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
7872c37256eSGarrett Wollman {
7882c37256eSGarrett Wollman 	int s = splnet();
7892c37256eSGarrett Wollman 	int error = 0;
790f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7912c37256eSGarrett Wollman 	struct tcpcb *tp;
792f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
7932c37256eSGarrett Wollman 
7942c37256eSGarrett Wollman 	COMMON_START();
7952c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
7962c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
7974cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
7984cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
7992c37256eSGarrett Wollman 		error = EINVAL;
8002c37256eSGarrett Wollman 		goto out;
8012c37256eSGarrett Wollman 	}
8022c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
8032c37256eSGarrett Wollman 		error = EWOULDBLOCK;
8042c37256eSGarrett Wollman 		goto out;
8052c37256eSGarrett Wollman 	}
8062c37256eSGarrett Wollman 	m->m_len = 1;
8072c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
8082c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
8092c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
8102c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
8112c37256eSGarrett Wollman }
8122c37256eSGarrett Wollman 
8132c37256eSGarrett Wollman /* xxx - should be const */
8142c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
8152c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
816117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
817f76fcf6dSJeffrey Hsu 	tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd,
818117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
819f76fcf6dSJeffrey Hsu 	tcp_sockaddr, sosend, soreceive, sopoll
8202c37256eSGarrett Wollman };
821df8bae1dSRodney W. Grimes 
822fb59c426SYoshinobu Inoue #ifdef INET6
823fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
824fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
825fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
826fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
827fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
828fb59c426SYoshinobu Inoue 	in6_mapped_sockaddr, sosend, soreceive, sopoll
829fb59c426SYoshinobu Inoue };
830fb59c426SYoshinobu Inoue #endif /* INET6 */
831fb59c426SYoshinobu Inoue 
832a0292f23SGarrett Wollman /*
833a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
834a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
835a0292f23SGarrett Wollman  * port number if needed.  Call in_pcbladdr to do the routing and to choose
836a0292f23SGarrett Wollman  * a local host address (interface).  If there is an existing incarnation
837a0292f23SGarrett Wollman  * of the same connection in TIME-WAIT state and if the remote host was
838a0292f23SGarrett Wollman  * sending CC options and if the connection duration was < MSL, then
839a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
840a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
841a0292f23SGarrett Wollman  */
8420312fbe9SPoul-Henning Kamp static int
843b40ce416SJulian Elischer tcp_connect(tp, nam, td)
844a0292f23SGarrett Wollman 	register struct tcpcb *tp;
84557bf258eSGarrett Wollman 	struct sockaddr *nam;
846b40ce416SJulian Elischer 	struct thread *td;
847a0292f23SGarrett Wollman {
848a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
849a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
850a0292f23SGarrett Wollman 	struct tcpcb *otp;
85157bf258eSGarrett Wollman 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
852a0292f23SGarrett Wollman 	struct sockaddr_in *ifaddr;
853a45d2726SAndras Olah 	struct rmxp_tao *taop;
854a45d2726SAndras Olah 	struct rmxp_tao tao_noncached;
855c3229e05SDavid Greenman 	int error;
856a0292f23SGarrett Wollman 
857a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
858b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)0, td);
859a0292f23SGarrett Wollman 		if (error)
860a0292f23SGarrett Wollman 			return error;
861a0292f23SGarrett Wollman 	}
862a0292f23SGarrett Wollman 
863a0292f23SGarrett Wollman 	/*
864a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
865a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
866a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
867a0292f23SGarrett Wollman 	 */
868a0292f23SGarrett Wollman 	error = in_pcbladdr(inp, nam, &ifaddr);
869d3628763SRodney W. Grimes 	if (error)
870d3628763SRodney W. Grimes 		return error;
871c3229e05SDavid Greenman 	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
872a0292f23SGarrett Wollman 	    sin->sin_addr, sin->sin_port,
873a0292f23SGarrett Wollman 	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
874a0292f23SGarrett Wollman 						: ifaddr->sin_addr,
875cfa1ca9dSYoshinobu Inoue 	    inp->inp_lport,  0, NULL);
876a0292f23SGarrett Wollman 	if (oinp) {
877a0292f23SGarrett Wollman 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
878a0292f23SGarrett Wollman 		otp->t_state == TCPS_TIME_WAIT &&
8799b8b58e0SJonathan Lemon 		    (ticks - otp->t_starttime) < tcp_msl &&
880a0292f23SGarrett Wollman 		    (otp->t_flags & TF_RCVD_CC))
881a0292f23SGarrett Wollman 			otp = tcp_close(otp);
882a0292f23SGarrett Wollman 		else
883a0292f23SGarrett Wollman 			return EADDRINUSE;
884a0292f23SGarrett Wollman 	}
885a0292f23SGarrett Wollman 	if (inp->inp_laddr.s_addr == INADDR_ANY)
886a0292f23SGarrett Wollman 		inp->inp_laddr = ifaddr->sin_addr;
887a0292f23SGarrett Wollman 	inp->inp_faddr = sin->sin_addr;
888a0292f23SGarrett Wollman 	inp->inp_fport = sin->sin_port;
88915bd2b43SDavid Greenman 	in_pcbrehash(inp);
890a0292f23SGarrett Wollman 
891a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
892a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
893a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
894a0292f23SGarrett Wollman 		tp->request_r_scale++;
895a0292f23SGarrett Wollman 
896a0292f23SGarrett Wollman 	soisconnecting(so);
897a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
898a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
8999b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
900b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
9011fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
902a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
903a45d2726SAndras Olah 
904a45d2726SAndras Olah 	/*
905a45d2726SAndras Olah 	 * Generate a CC value for this connection and
906a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
907a45d2726SAndras Olah 	 */
908be2ac88cSJonathan Lemon 	if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
909a45d2726SAndras Olah 		taop = &tao_noncached;
910a45d2726SAndras Olah 		bzero(taop, sizeof(*taop));
911a45d2726SAndras Olah 	}
912a45d2726SAndras Olah 
913a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
914a45d2726SAndras Olah 	if (taop->tao_ccsent != 0 &&
915a45d2726SAndras Olah 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
916a45d2726SAndras Olah 		taop->tao_ccsent = tp->cc_send;
917a45d2726SAndras Olah 	} else {
918a45d2726SAndras Olah 		taop->tao_ccsent = 0;
919a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
920a45d2726SAndras Olah 	}
921a0292f23SGarrett Wollman 
922a0292f23SGarrett Wollman 	return 0;
923a0292f23SGarrett Wollman }
924a0292f23SGarrett Wollman 
925fb59c426SYoshinobu Inoue #ifdef INET6
926fb59c426SYoshinobu Inoue static int
927b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
928fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
929fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
930b40ce416SJulian Elischer 	struct thread *td;
931fb59c426SYoshinobu Inoue {
932fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
933fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
934fb59c426SYoshinobu Inoue 	struct tcpcb *otp;
935fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
936fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
937fb59c426SYoshinobu Inoue 	struct rmxp_tao *taop;
938fb59c426SYoshinobu Inoue 	struct rmxp_tao tao_noncached;
939fb59c426SYoshinobu Inoue 	int error;
940fb59c426SYoshinobu Inoue 
941fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
942b40ce416SJulian Elischer 		error = in6_pcbbind(inp, (struct sockaddr *)0, td);
943fb59c426SYoshinobu Inoue 		if (error)
944fb59c426SYoshinobu Inoue 			return error;
945fb59c426SYoshinobu Inoue 	}
946fb59c426SYoshinobu Inoue 
947fb59c426SYoshinobu Inoue 	/*
948fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
949fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
950fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
951fb59c426SYoshinobu Inoue 	 */
952fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
953fb59c426SYoshinobu Inoue 	if (error)
954fb59c426SYoshinobu Inoue 		return error;
955fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
956fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
957fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
958fb59c426SYoshinobu Inoue 				  ? addr6
959fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
960fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
961fb59c426SYoshinobu Inoue 	if (oinp) {
962fb59c426SYoshinobu Inoue 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
963fb59c426SYoshinobu Inoue 		    otp->t_state == TCPS_TIME_WAIT &&
964fb59c426SYoshinobu Inoue 		    (ticks - otp->t_starttime) < tcp_msl &&
965fb59c426SYoshinobu Inoue 		    (otp->t_flags & TF_RCVD_CC))
966fb59c426SYoshinobu Inoue 			otp = tcp_close(otp);
967fb59c426SYoshinobu Inoue 		else
968fb59c426SYoshinobu Inoue 			return EADDRINUSE;
969fb59c426SYoshinobu Inoue 	}
970fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
971fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
972fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
973fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
974fb59c426SYoshinobu Inoue 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
975fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
976fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
977fb59c426SYoshinobu Inoue 
978fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
979fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
980fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
981fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
982fb59c426SYoshinobu Inoue 
983fb59c426SYoshinobu Inoue 	soisconnecting(so);
984fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
985fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
986fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
987b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
9881fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
989fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
990fb59c426SYoshinobu Inoue 
991fb59c426SYoshinobu Inoue 	/*
992fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
993fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
994fb59c426SYoshinobu Inoue 	 */
995be2ac88cSJonathan Lemon 	if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
996fb59c426SYoshinobu Inoue 		taop = &tao_noncached;
997fb59c426SYoshinobu Inoue 		bzero(taop, sizeof(*taop));
998fb59c426SYoshinobu Inoue 	}
999fb59c426SYoshinobu Inoue 
1000fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
1001fb59c426SYoshinobu Inoue 	if (taop->tao_ccsent != 0 &&
1002fb59c426SYoshinobu Inoue 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1003fb59c426SYoshinobu Inoue 		taop->tao_ccsent = tp->cc_send;
1004fb59c426SYoshinobu Inoue 	} else {
1005fb59c426SYoshinobu Inoue 		taop->tao_ccsent = 0;
1006fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
1007fb59c426SYoshinobu Inoue 	}
1008fb59c426SYoshinobu Inoue 
1009fb59c426SYoshinobu Inoue 	return 0;
1010fb59c426SYoshinobu Inoue }
1011fb59c426SYoshinobu Inoue #endif /* INET6 */
1012fb59c426SYoshinobu Inoue 
1013cfe8b629SGarrett Wollman /*
1014cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1015cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1016cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1017cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1018cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1019cfe8b629SGarrett Wollman  */
1020df8bae1dSRodney W. Grimes int
1021cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
1022df8bae1dSRodney W. Grimes 	struct socket *so;
1023cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1024df8bae1dSRodney W. Grimes {
1025cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
1026df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1027cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1028df8bae1dSRodney W. Grimes 
1029cfe8b629SGarrett Wollman 	error = 0;
1030cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
1031f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
1032df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1033df8bae1dSRodney W. Grimes 	if (inp == NULL) {
1034f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
1035df8bae1dSRodney W. Grimes 		splx(s);
1036df8bae1dSRodney W. Grimes 		return (ECONNRESET);
1037df8bae1dSRodney W. Grimes 	}
1038f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1039f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
1040cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1041fb59c426SYoshinobu Inoue #ifdef INET6
1042fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1043fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1044fb59c426SYoshinobu Inoue 		else
1045fb59c426SYoshinobu Inoue #endif /* INET6 */
1046cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1047f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1048df8bae1dSRodney W. Grimes 		splx(s);
1049df8bae1dSRodney W. Grimes 		return (error);
1050df8bae1dSRodney W. Grimes 	}
1051df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1052df8bae1dSRodney W. Grimes 
1053cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1054cfe8b629SGarrett Wollman 	case SOPT_SET:
1055cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1056df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1057cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1058cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1059cfe8b629SGarrett Wollman 					    sizeof optval);
1060cfe8b629SGarrett Wollman 			if (error)
1061cfe8b629SGarrett Wollman 				break;
1062cfe8b629SGarrett Wollman 
1063cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1064cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1065cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1066cfe8b629SGarrett Wollman 				break;
1067cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1068cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1069cfe8b629SGarrett Wollman 				break;
1070cfe8b629SGarrett Wollman 			default:
1071cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1072cfe8b629SGarrett Wollman 				break;
1073cfe8b629SGarrett Wollman 			}
1074cfe8b629SGarrett Wollman 
1075cfe8b629SGarrett Wollman 			if (optval)
1076cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1077df8bae1dSRodney W. Grimes 			else
1078cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1079df8bae1dSRodney W. Grimes 			break;
1080df8bae1dSRodney W. Grimes 
1081007581c0SJonathan Lemon 		case TCP_NOPUSH:
1082007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1083007581c0SJonathan Lemon 					    sizeof optval);
1084007581c0SJonathan Lemon 			if (error)
1085007581c0SJonathan Lemon 				break;
1086007581c0SJonathan Lemon 
1087007581c0SJonathan Lemon 			if (optval)
1088007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1089007581c0SJonathan Lemon 			else {
1090007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1091007581c0SJonathan Lemon 				error = tcp_output(tp);
1092007581c0SJonathan Lemon 			}
1093007581c0SJonathan Lemon 			break;
1094007581c0SJonathan Lemon 
1095df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1096cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1097cfe8b629SGarrett Wollman 					    sizeof optval);
1098cfe8b629SGarrett Wollman 			if (error)
1099df8bae1dSRodney W. Grimes 				break;
1100df8bae1dSRodney W. Grimes 
1101cfe8b629SGarrett Wollman 			if (optval > 0 && optval <= tp->t_maxseg)
1102cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1103a0292f23SGarrett Wollman 			else
1104a0292f23SGarrett Wollman 				error = EINVAL;
1105a0292f23SGarrett Wollman 			break;
1106a0292f23SGarrett Wollman 
1107df8bae1dSRodney W. Grimes 		default:
1108df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1109df8bae1dSRodney W. Grimes 			break;
1110df8bae1dSRodney W. Grimes 		}
1111df8bae1dSRodney W. Grimes 		break;
1112df8bae1dSRodney W. Grimes 
1113cfe8b629SGarrett Wollman 	case SOPT_GET:
1114cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1115df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1116cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1117df8bae1dSRodney W. Grimes 			break;
1118df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1119cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1120df8bae1dSRodney W. Grimes 			break;
1121a0292f23SGarrett Wollman 		case TCP_NOOPT:
1122cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1123a0292f23SGarrett Wollman 			break;
1124a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1125cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1126a0292f23SGarrett Wollman 			break;
1127df8bae1dSRodney W. Grimes 		default:
1128df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1129df8bae1dSRodney W. Grimes 			break;
1130df8bae1dSRodney W. Grimes 		}
1131cfe8b629SGarrett Wollman 		if (error == 0)
1132cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1133df8bae1dSRodney W. Grimes 		break;
1134df8bae1dSRodney W. Grimes 	}
1135f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1136df8bae1dSRodney W. Grimes 	splx(s);
1137df8bae1dSRodney W. Grimes 	return (error);
1138df8bae1dSRodney W. Grimes }
1139df8bae1dSRodney W. Grimes 
114026e30fbbSDavid Greenman /*
114126e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
114226e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
114326e30fbbSDavid Greenman  * be set by the route).
114426e30fbbSDavid Greenman  */
114581e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
11463d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
11473d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
114881e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
11493d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
11503d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1151df8bae1dSRodney W. Grimes 
1152df8bae1dSRodney W. Grimes /*
1153df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1154df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1155df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1156df8bae1dSRodney W. Grimes  */
11570312fbe9SPoul-Henning Kamp static int
1158b40ce416SJulian Elischer tcp_attach(so, td)
1159df8bae1dSRodney W. Grimes 	struct socket *so;
1160b40ce416SJulian Elischer 	struct thread *td;
1161df8bae1dSRodney W. Grimes {
1162df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1163df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1164df8bae1dSRodney W. Grimes 	int error;
1165fb59c426SYoshinobu Inoue #ifdef INET6
1166fb59c426SYoshinobu Inoue 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1167fb59c426SYoshinobu Inoue #endif
1168df8bae1dSRodney W. Grimes 
1169df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1170df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1171df8bae1dSRodney W. Grimes 		if (error)
1172df8bae1dSRodney W. Grimes 			return (error);
1173df8bae1dSRodney W. Grimes 	}
1174b40ce416SJulian Elischer 	error = in_pcballoc(so, &tcbinfo, td);
1175df8bae1dSRodney W. Grimes 	if (error)
1176df8bae1dSRodney W. Grimes 		return (error);
1177df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1178fb59c426SYoshinobu Inoue #ifdef INET6
1179fb59c426SYoshinobu Inoue 	if (isipv6) {
1180fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1181fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1182fb59c426SYoshinobu Inoue 	}
1183fb59c426SYoshinobu Inoue 	else
1184fb59c426SYoshinobu Inoue #endif
1185cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1186df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1187df8bae1dSRodney W. Grimes 	if (tp == 0) {
11884cc20ab1SSeigo Tanimura 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1189df8bae1dSRodney W. Grimes 
1190df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1191fb59c426SYoshinobu Inoue #ifdef INET6
1192fb59c426SYoshinobu Inoue 		if (isipv6)
1193fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1194fb59c426SYoshinobu Inoue 		else
1195fb59c426SYoshinobu Inoue #endif
1196df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1197df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1198df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1199df8bae1dSRodney W. Grimes 	}
1200df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1201df8bae1dSRodney W. Grimes 	return (0);
1202df8bae1dSRodney W. Grimes }
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes /*
1205df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1206df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1207df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1208df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1209df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1210df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1211df8bae1dSRodney W. Grimes  */
12120312fbe9SPoul-Henning Kamp static struct tcpcb *
1213df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1214df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1215df8bae1dSRodney W. Grimes {
1216df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1217df8bae1dSRodney W. Grimes 
1218df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1219df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
12204cc20ab1SSeigo Tanimura 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1221243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
12224cc20ab1SSeigo Tanimura 	else {
1223df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1224df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1225df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1226df8bae1dSRodney W. Grimes 		if (tp)
1227df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1228df8bae1dSRodney W. Grimes 	}
1229df8bae1dSRodney W. Grimes 	return (tp);
1230df8bae1dSRodney W. Grimes }
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes /*
1233df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1234df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1235df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1236df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1237df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1238df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1239df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1240df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1241df8bae1dSRodney W. Grimes  */
12420312fbe9SPoul-Henning Kamp static struct tcpcb *
1243df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1244df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1245df8bae1dSRodney W. Grimes {
1246df8bae1dSRodney W. Grimes 
1247df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1248df8bae1dSRodney W. Grimes 
1249df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1250df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1251df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1252df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1253df8bae1dSRodney W. Grimes 		break;
1254df8bae1dSRodney W. Grimes 
1255a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1256df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1257a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1258a0292f23SGarrett Wollman 		break;
1259a0292f23SGarrett Wollman 
1260df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1261df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1262df8bae1dSRodney W. Grimes 		break;
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1265df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1266df8bae1dSRodney W. Grimes 		break;
1267df8bae1dSRodney W. Grimes 	}
1268b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1269df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1270b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1271b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
12729b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
12739b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1274b6239c4aSAndras Olah 	}
1275df8bae1dSRodney W. Grimes 	return (tp);
1276df8bae1dSRodney W. Grimes }
1277a0292f23SGarrett Wollman 
1278