xref: /freebsd/sys/netinet/tcp_usrreq.c (revision d46a53126ccd583de1c3f4b6e927c89e1f09f09d)
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;
470f76fcf6dSJeffrey Hsu 	struct sockaddr_in *sin;
471f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
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 	/*
480f76fcf6dSJeffrey Hsu 	 * Do the malloc first in case it blocks.
481f76fcf6dSJeffrey Hsu 	 */
482f76fcf6dSJeffrey Hsu 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
483f76fcf6dSJeffrey Hsu 		M_WAITOK | M_ZERO);
484f76fcf6dSJeffrey Hsu 	sin->sin_family = AF_INET;
485f76fcf6dSJeffrey Hsu 	sin->sin_len = sizeof(*sin);
486f76fcf6dSJeffrey Hsu 
487f76fcf6dSJeffrey Hsu 	s = splnet();
488f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
489f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
490f76fcf6dSJeffrey Hsu 	if (!inp) {
491f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
4921db24ffbSJonathan Lemon 		splx(s);
493f76fcf6dSJeffrey Hsu 		free(sin, M_SONAME);
4941db24ffbSJonathan Lemon 		return (EINVAL);
4951db24ffbSJonathan Lemon 	}
496f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
497f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
4981db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4991db24ffbSJonathan Lemon 	TCPDEBUG1();
500f76fcf6dSJeffrey Hsu 
501f76fcf6dSJeffrey Hsu 	/*
502f76fcf6dSJeffrey Hsu 	 * We inline in_setpeeraddr here, because we have already done
503f76fcf6dSJeffrey Hsu 	 * the locking and the malloc.
504f76fcf6dSJeffrey Hsu 	 */
505f76fcf6dSJeffrey Hsu 	sin->sin_port = inp->inp_fport;
506f76fcf6dSJeffrey Hsu 	sin->sin_addr = inp->inp_faddr;
507f76fcf6dSJeffrey Hsu 	*nam = (struct sockaddr *)sin;
508f76fcf6dSJeffrey Hsu 
5092c37256eSGarrett Wollman 	COMMON_END(PRU_ACCEPT);
5102c37256eSGarrett Wollman }
5112c37256eSGarrett Wollman 
512fb59c426SYoshinobu Inoue #ifdef INET6
513fb59c426SYoshinobu Inoue static int
514fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
515fb59c426SYoshinobu Inoue {
516f76fcf6dSJeffrey Hsu 	int s;
517f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
518fb59c426SYoshinobu Inoue 	int error = 0;
5191db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
520f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
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();
540fb59c426SYoshinobu Inoue 	in6_mapped_peeraddr(so, nam);
541fb59c426SYoshinobu Inoue 	COMMON_END(PRU_ACCEPT);
542fb59c426SYoshinobu Inoue }
543fb59c426SYoshinobu Inoue #endif /* INET6 */
544f76fcf6dSJeffrey Hsu 
545f76fcf6dSJeffrey Hsu /*
546f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
547f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
548f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
549f76fcf6dSJeffrey Hsu  */
550f76fcf6dSJeffrey Hsu static int
551f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
552f76fcf6dSJeffrey Hsu {
553f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
554f76fcf6dSJeffrey Hsu }
555f76fcf6dSJeffrey Hsu 
556f76fcf6dSJeffrey Hsu /*
557f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
558f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
559f76fcf6dSJeffrey Hsu  */
560f76fcf6dSJeffrey Hsu static int
561f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
562f76fcf6dSJeffrey Hsu {
563f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
564f76fcf6dSJeffrey Hsu }
565f76fcf6dSJeffrey Hsu 
5662c37256eSGarrett Wollman /*
5672c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
5682c37256eSGarrett Wollman  */
5692c37256eSGarrett Wollman static int
5702c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
5712c37256eSGarrett Wollman {
5722c37256eSGarrett Wollman 	int s = splnet();
5732c37256eSGarrett Wollman 	int error = 0;
574f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
5752c37256eSGarrett Wollman 	struct tcpcb *tp;
576f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
5772c37256eSGarrett Wollman 
5782c37256eSGarrett Wollman 	COMMON_START();
5792c37256eSGarrett Wollman 	socantsendmore(so);
5802c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
5812c37256eSGarrett Wollman 	if (tp)
5822c37256eSGarrett Wollman 		error = tcp_output(tp);
5832c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
5842c37256eSGarrett Wollman }
5852c37256eSGarrett Wollman 
5862c37256eSGarrett Wollman /*
5872c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
5882c37256eSGarrett Wollman  */
5892c37256eSGarrett Wollman static int
5902c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
5912c37256eSGarrett Wollman {
5922c37256eSGarrett Wollman 	int s = splnet();
5932c37256eSGarrett Wollman 	int error = 0;
594f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
5952c37256eSGarrett Wollman 	struct tcpcb *tp;
596f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
5972c37256eSGarrett Wollman 
5982c37256eSGarrett Wollman 	COMMON_START();
5992c37256eSGarrett Wollman 	tcp_output(tp);
6002c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
6012c37256eSGarrett Wollman }
6022c37256eSGarrett Wollman 
6032c37256eSGarrett Wollman /*
6042c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
6059c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
6069c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
6079c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
6089c9906e9SPeter Wemm  * generally are caller-frees.
6092c37256eSGarrett Wollman  */
6102c37256eSGarrett Wollman static int
61157bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
612b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
6132c37256eSGarrett Wollman {
6142c37256eSGarrett Wollman 	int s = splnet();
6152c37256eSGarrett Wollman 	int error = 0;
616f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6172c37256eSGarrett Wollman 	struct tcpcb *tp;
618f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
619fb59c426SYoshinobu Inoue #ifdef INET6
620fb59c426SYoshinobu Inoue 	int isipv6;
621fb59c426SYoshinobu Inoue #endif
6229c9906e9SPeter Wemm 	TCPDEBUG0;
6232c37256eSGarrett Wollman 
624f76fcf6dSJeffrey Hsu 	/*
625f76fcf6dSJeffrey Hsu 	 * Need write lock here because this function might call
626f76fcf6dSJeffrey Hsu 	 * tcp_connect or tcp_usrclosed.
627f76fcf6dSJeffrey Hsu 	 * We really want to have to this function upgrade from read lock
628f76fcf6dSJeffrey Hsu 	 * to write lock.  XXX
629f76fcf6dSJeffrey Hsu 	 */
630f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
631f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
6329c9906e9SPeter Wemm 	if (inp == NULL) {
6339c9906e9SPeter Wemm 		/*
6349c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
6359c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
6369c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
6379c9906e9SPeter Wemm 		 */
6389c9906e9SPeter Wemm 		if (m)
6399c9906e9SPeter Wemm 			m_freem(m);
6409c9906e9SPeter Wemm 		if (control)
6419c9906e9SPeter Wemm 			m_freem(control);
6429c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
64345d3a132SPeter Wemm 		tp = NULL;
64445d3a132SPeter Wemm 		TCPDEBUG1();
6459c9906e9SPeter Wemm 		goto out;
6469c9906e9SPeter Wemm 	}
647f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
648fb59c426SYoshinobu Inoue #ifdef INET6
649fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
650fb59c426SYoshinobu Inoue #endif /* INET6 */
6519c9906e9SPeter Wemm 	tp = intotcpcb(inp);
6529c9906e9SPeter Wemm 	TCPDEBUG1();
6539c9906e9SPeter Wemm 	if (control) {
6549c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
6559c9906e9SPeter Wemm 		if (control->m_len) {
6569c9906e9SPeter Wemm 			m_freem(control);
6572c37256eSGarrett Wollman 			if (m)
6582c37256eSGarrett Wollman 				m_freem(m);
659744f87eaSDavid Greenman 			error = EINVAL;
660744f87eaSDavid Greenman 			goto out;
6612c37256eSGarrett Wollman 		}
6629c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
6639c9906e9SPeter Wemm 	}
6642c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
6652c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
6662c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
6672c37256eSGarrett Wollman 			/*
6682c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
6692c37256eSGarrett Wollman 			 * initialize window to default value, and
6702c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
6712c37256eSGarrett Wollman 			 * MSS.
6722c37256eSGarrett Wollman 			 */
673fb59c426SYoshinobu Inoue #ifdef INET6
674fb59c426SYoshinobu Inoue 			if (isipv6)
675b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
676fb59c426SYoshinobu Inoue 			else
677fb59c426SYoshinobu Inoue #endif /* INET6 */
678b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
6792c37256eSGarrett Wollman 			if (error)
6802c37256eSGarrett Wollman 				goto out;
6812c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
6822c37256eSGarrett Wollman 			tcp_mss(tp, -1);
6832c37256eSGarrett Wollman 		}
6842c37256eSGarrett Wollman 
6852c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
6862c37256eSGarrett Wollman 			/*
6872c37256eSGarrett Wollman 			 * Close the send side of the connection after
6882c37256eSGarrett Wollman 			 * the data is sent.
6892c37256eSGarrett Wollman 			 */
6902c37256eSGarrett Wollman 			socantsendmore(so);
6912c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
6922c37256eSGarrett Wollman 		}
693b0acefa8SBill Fenner 		if (tp != NULL) {
694b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
695b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
6962c37256eSGarrett Wollman 			error = tcp_output(tp);
697b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
698b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
699b0acefa8SBill Fenner 		}
7002c37256eSGarrett Wollman 	} else {
7012c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
7022c37256eSGarrett Wollman 			m_freem(m);
7032c37256eSGarrett Wollman 			error = ENOBUFS;
7042c37256eSGarrett Wollman 			goto out;
7052c37256eSGarrett Wollman 		}
7062c37256eSGarrett Wollman 		/*
7072c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
7082c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
7092c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
7102c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
7112c37256eSGarrett Wollman 		 * of data past the urgent section.
7122c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
7132c37256eSGarrett Wollman 		 */
7142c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
715ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
716ef53690bSGarrett Wollman 			/*
717ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
718ef53690bSGarrett Wollman 			 * initialize window to default value, and
719ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
720ef53690bSGarrett Wollman 			 * MSS.
721ef53690bSGarrett Wollman 			 */
722fb59c426SYoshinobu Inoue #ifdef INET6
723fb59c426SYoshinobu Inoue 			if (isipv6)
724b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
725fb59c426SYoshinobu Inoue 			else
726fb59c426SYoshinobu Inoue #endif /* INET6 */
727b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
728ef53690bSGarrett Wollman 			if (error)
729ef53690bSGarrett Wollman 				goto out;
730ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
731ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
732ef53690bSGarrett Wollman 		}
7332c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
7342c37256eSGarrett Wollman 		tp->t_force = 1;
7352c37256eSGarrett Wollman 		error = tcp_output(tp);
7362c37256eSGarrett Wollman 		tp->t_force = 0;
7372c37256eSGarrett Wollman 	}
7382c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
7392c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
7402c37256eSGarrett Wollman }
7412c37256eSGarrett Wollman 
7422c37256eSGarrett Wollman /*
7432c37256eSGarrett Wollman  * Abort the TCP.
7442c37256eSGarrett Wollman  */
7452c37256eSGarrett Wollman static int
7462c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
7472c37256eSGarrett Wollman {
7482c37256eSGarrett Wollman 	int s = splnet();
7492c37256eSGarrett Wollman 	int error = 0;
750f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7512c37256eSGarrett Wollman 	struct tcpcb *tp;
752f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
7532c37256eSGarrett Wollman 
7542c37256eSGarrett Wollman 	COMMON_START();
7552c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
7562c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
7572c37256eSGarrett Wollman }
7582c37256eSGarrett Wollman 
7592c37256eSGarrett Wollman /*
7602c37256eSGarrett Wollman  * Receive out-of-band data.
7612c37256eSGarrett Wollman  */
7622c37256eSGarrett Wollman static int
7632c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
7642c37256eSGarrett Wollman {
7652c37256eSGarrett Wollman 	int s = splnet();
7662c37256eSGarrett Wollman 	int error = 0;
767f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7682c37256eSGarrett Wollman 	struct tcpcb *tp;
769f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
7702c37256eSGarrett Wollman 
7712c37256eSGarrett Wollman 	COMMON_START();
7722c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
7732c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
7744cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
7754cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
7762c37256eSGarrett Wollman 		error = EINVAL;
7772c37256eSGarrett Wollman 		goto out;
7782c37256eSGarrett Wollman 	}
7792c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
7802c37256eSGarrett Wollman 		error = EWOULDBLOCK;
7812c37256eSGarrett Wollman 		goto out;
7822c37256eSGarrett Wollman 	}
7832c37256eSGarrett Wollman 	m->m_len = 1;
7842c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
7852c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
7862c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
7872c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
7882c37256eSGarrett Wollman }
7892c37256eSGarrett Wollman 
7902c37256eSGarrett Wollman /* xxx - should be const */
7912c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
7922c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
793117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
794f76fcf6dSJeffrey Hsu 	tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd,
795117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
796f76fcf6dSJeffrey Hsu 	tcp_sockaddr, sosend, soreceive, sopoll
7972c37256eSGarrett Wollman };
798df8bae1dSRodney W. Grimes 
799fb59c426SYoshinobu Inoue #ifdef INET6
800fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
801fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
802fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
803fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
804fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
805fb59c426SYoshinobu Inoue 	in6_mapped_sockaddr, sosend, soreceive, sopoll
806fb59c426SYoshinobu Inoue };
807fb59c426SYoshinobu Inoue #endif /* INET6 */
808fb59c426SYoshinobu Inoue 
809a0292f23SGarrett Wollman /*
810a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
811a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
812a0292f23SGarrett Wollman  * port number if needed.  Call in_pcbladdr to do the routing and to choose
813a0292f23SGarrett Wollman  * a local host address (interface).  If there is an existing incarnation
814a0292f23SGarrett Wollman  * of the same connection in TIME-WAIT state and if the remote host was
815a0292f23SGarrett Wollman  * sending CC options and if the connection duration was < MSL, then
816a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
817a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
818a0292f23SGarrett Wollman  */
8190312fbe9SPoul-Henning Kamp static int
820b40ce416SJulian Elischer tcp_connect(tp, nam, td)
821a0292f23SGarrett Wollman 	register struct tcpcb *tp;
82257bf258eSGarrett Wollman 	struct sockaddr *nam;
823b40ce416SJulian Elischer 	struct thread *td;
824a0292f23SGarrett Wollman {
825a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
826a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
827a0292f23SGarrett Wollman 	struct tcpcb *otp;
82857bf258eSGarrett Wollman 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
829a0292f23SGarrett Wollman 	struct sockaddr_in *ifaddr;
830a45d2726SAndras Olah 	struct rmxp_tao *taop;
831a45d2726SAndras Olah 	struct rmxp_tao tao_noncached;
832c3229e05SDavid Greenman 	int error;
833a0292f23SGarrett Wollman 
834a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
835b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)0, td);
836a0292f23SGarrett Wollman 		if (error)
837a0292f23SGarrett Wollman 			return error;
838a0292f23SGarrett Wollman 	}
839a0292f23SGarrett Wollman 
840a0292f23SGarrett Wollman 	/*
841a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
842a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
843a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
844a0292f23SGarrett Wollman 	 */
845a0292f23SGarrett Wollman 	error = in_pcbladdr(inp, nam, &ifaddr);
846d3628763SRodney W. Grimes 	if (error)
847d3628763SRodney W. Grimes 		return error;
848c3229e05SDavid Greenman 	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
849a0292f23SGarrett Wollman 	    sin->sin_addr, sin->sin_port,
850a0292f23SGarrett Wollman 	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
851a0292f23SGarrett Wollman 						: ifaddr->sin_addr,
852cfa1ca9dSYoshinobu Inoue 	    inp->inp_lport,  0, NULL);
853a0292f23SGarrett Wollman 	if (oinp) {
854a0292f23SGarrett Wollman 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
855a0292f23SGarrett Wollman 		otp->t_state == TCPS_TIME_WAIT &&
8569b8b58e0SJonathan Lemon 		    (ticks - otp->t_starttime) < tcp_msl &&
857a0292f23SGarrett Wollman 		    (otp->t_flags & TF_RCVD_CC))
858a0292f23SGarrett Wollman 			otp = tcp_close(otp);
859a0292f23SGarrett Wollman 		else
860a0292f23SGarrett Wollman 			return EADDRINUSE;
861a0292f23SGarrett Wollman 	}
862a0292f23SGarrett Wollman 	if (inp->inp_laddr.s_addr == INADDR_ANY)
863a0292f23SGarrett Wollman 		inp->inp_laddr = ifaddr->sin_addr;
864a0292f23SGarrett Wollman 	inp->inp_faddr = sin->sin_addr;
865a0292f23SGarrett Wollman 	inp->inp_fport = sin->sin_port;
86615bd2b43SDavid Greenman 	in_pcbrehash(inp);
867a0292f23SGarrett Wollman 
868a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
869a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
870a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
871a0292f23SGarrett Wollman 		tp->request_r_scale++;
872a0292f23SGarrett Wollman 
873a0292f23SGarrett Wollman 	soisconnecting(so);
874a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
875a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
8769b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
877b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
878a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
879a45d2726SAndras Olah 
880a45d2726SAndras Olah 	/*
881a45d2726SAndras Olah 	 * Generate a CC value for this connection and
882a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
883a45d2726SAndras Olah 	 */
884be2ac88cSJonathan Lemon 	if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
885a45d2726SAndras Olah 		taop = &tao_noncached;
886a45d2726SAndras Olah 		bzero(taop, sizeof(*taop));
887a45d2726SAndras Olah 	}
888a45d2726SAndras Olah 
889a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
890a45d2726SAndras Olah 	if (taop->tao_ccsent != 0 &&
891a45d2726SAndras Olah 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
892a45d2726SAndras Olah 		taop->tao_ccsent = tp->cc_send;
893a45d2726SAndras Olah 	} else {
894a45d2726SAndras Olah 		taop->tao_ccsent = 0;
895a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
896a45d2726SAndras Olah 	}
897a0292f23SGarrett Wollman 
898a0292f23SGarrett Wollman 	return 0;
899a0292f23SGarrett Wollman }
900a0292f23SGarrett Wollman 
901fb59c426SYoshinobu Inoue #ifdef INET6
902fb59c426SYoshinobu Inoue static int
903b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
904fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
905fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
906b40ce416SJulian Elischer 	struct thread *td;
907fb59c426SYoshinobu Inoue {
908fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
909fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
910fb59c426SYoshinobu Inoue 	struct tcpcb *otp;
911fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
912fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
913fb59c426SYoshinobu Inoue 	struct rmxp_tao *taop;
914fb59c426SYoshinobu Inoue 	struct rmxp_tao tao_noncached;
915fb59c426SYoshinobu Inoue 	int error;
916fb59c426SYoshinobu Inoue 
917fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
918b40ce416SJulian Elischer 		error = in6_pcbbind(inp, (struct sockaddr *)0, td);
919fb59c426SYoshinobu Inoue 		if (error)
920fb59c426SYoshinobu Inoue 			return error;
921fb59c426SYoshinobu Inoue 	}
922fb59c426SYoshinobu Inoue 
923fb59c426SYoshinobu Inoue 	/*
924fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
925fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
926fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
927fb59c426SYoshinobu Inoue 	 */
928fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
929fb59c426SYoshinobu Inoue 	if (error)
930fb59c426SYoshinobu Inoue 		return error;
931fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
932fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
933fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
934fb59c426SYoshinobu Inoue 				  ? addr6
935fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
936fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
937fb59c426SYoshinobu Inoue 	if (oinp) {
938fb59c426SYoshinobu Inoue 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
939fb59c426SYoshinobu Inoue 		    otp->t_state == TCPS_TIME_WAIT &&
940fb59c426SYoshinobu Inoue 		    (ticks - otp->t_starttime) < tcp_msl &&
941fb59c426SYoshinobu Inoue 		    (otp->t_flags & TF_RCVD_CC))
942fb59c426SYoshinobu Inoue 			otp = tcp_close(otp);
943fb59c426SYoshinobu Inoue 		else
944fb59c426SYoshinobu Inoue 			return EADDRINUSE;
945fb59c426SYoshinobu Inoue 	}
946fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
947fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
948fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
949fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
950fb59c426SYoshinobu Inoue 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
951fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
952fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
953fb59c426SYoshinobu Inoue 
954fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
955fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
956fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
957fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
958fb59c426SYoshinobu Inoue 
959fb59c426SYoshinobu Inoue 	soisconnecting(so);
960fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
961fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
962fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
963b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
964fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
965fb59c426SYoshinobu Inoue 
966fb59c426SYoshinobu Inoue 	/*
967fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
968fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
969fb59c426SYoshinobu Inoue 	 */
970be2ac88cSJonathan Lemon 	if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
971fb59c426SYoshinobu Inoue 		taop = &tao_noncached;
972fb59c426SYoshinobu Inoue 		bzero(taop, sizeof(*taop));
973fb59c426SYoshinobu Inoue 	}
974fb59c426SYoshinobu Inoue 
975fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
976fb59c426SYoshinobu Inoue 	if (taop->tao_ccsent != 0 &&
977fb59c426SYoshinobu Inoue 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
978fb59c426SYoshinobu Inoue 		taop->tao_ccsent = tp->cc_send;
979fb59c426SYoshinobu Inoue 	} else {
980fb59c426SYoshinobu Inoue 		taop->tao_ccsent = 0;
981fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
982fb59c426SYoshinobu Inoue 	}
983fb59c426SYoshinobu Inoue 
984fb59c426SYoshinobu Inoue 	return 0;
985fb59c426SYoshinobu Inoue }
986fb59c426SYoshinobu Inoue #endif /* INET6 */
987fb59c426SYoshinobu Inoue 
988cfe8b629SGarrett Wollman /*
989cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
990cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
991cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
992cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
993cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
994cfe8b629SGarrett Wollman  */
995df8bae1dSRodney W. Grimes int
996cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
997df8bae1dSRodney W. Grimes 	struct socket *so;
998cfe8b629SGarrett Wollman 	struct sockopt *sopt;
999df8bae1dSRodney W. Grimes {
1000cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
1001df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1002cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1003df8bae1dSRodney W. Grimes 
1004cfe8b629SGarrett Wollman 	error = 0;
1005cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
1006f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
1007df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1008df8bae1dSRodney W. Grimes 	if (inp == NULL) {
1009f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
1010df8bae1dSRodney W. Grimes 		splx(s);
1011df8bae1dSRodney W. Grimes 		return (ECONNRESET);
1012df8bae1dSRodney W. Grimes 	}
1013f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1014f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
1015cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1016fb59c426SYoshinobu Inoue #ifdef INET6
1017fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1018fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1019fb59c426SYoshinobu Inoue 		else
1020fb59c426SYoshinobu Inoue #endif /* INET6 */
1021cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1022f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1023df8bae1dSRodney W. Grimes 		splx(s);
1024df8bae1dSRodney W. Grimes 		return (error);
1025df8bae1dSRodney W. Grimes 	}
1026df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1027df8bae1dSRodney W. Grimes 
1028cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1029cfe8b629SGarrett Wollman 	case SOPT_SET:
1030cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1031df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1032cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1033cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1034cfe8b629SGarrett Wollman 					    sizeof optval);
1035cfe8b629SGarrett Wollman 			if (error)
1036cfe8b629SGarrett Wollman 				break;
1037cfe8b629SGarrett Wollman 
1038cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1039cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1040cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1041cfe8b629SGarrett Wollman 				break;
1042cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1043cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1044cfe8b629SGarrett Wollman 				break;
1045cfe8b629SGarrett Wollman 			default:
1046cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1047cfe8b629SGarrett Wollman 				break;
1048cfe8b629SGarrett Wollman 			}
1049cfe8b629SGarrett Wollman 
1050cfe8b629SGarrett Wollman 			if (optval)
1051cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1052df8bae1dSRodney W. Grimes 			else
1053cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1054df8bae1dSRodney W. Grimes 			break;
1055df8bae1dSRodney W. Grimes 
1056007581c0SJonathan Lemon 		case TCP_NOPUSH:
1057007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1058007581c0SJonathan Lemon 					    sizeof optval);
1059007581c0SJonathan Lemon 			if (error)
1060007581c0SJonathan Lemon 				break;
1061007581c0SJonathan Lemon 
1062007581c0SJonathan Lemon 			if (optval)
1063007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1064007581c0SJonathan Lemon 			else {
1065007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1066007581c0SJonathan Lemon 				error = tcp_output(tp);
1067007581c0SJonathan Lemon 			}
1068007581c0SJonathan Lemon 			break;
1069007581c0SJonathan Lemon 
1070df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1071cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1072cfe8b629SGarrett Wollman 					    sizeof optval);
1073cfe8b629SGarrett Wollman 			if (error)
1074df8bae1dSRodney W. Grimes 				break;
1075df8bae1dSRodney W. Grimes 
1076cfe8b629SGarrett Wollman 			if (optval > 0 && optval <= tp->t_maxseg)
1077cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1078a0292f23SGarrett Wollman 			else
1079a0292f23SGarrett Wollman 				error = EINVAL;
1080a0292f23SGarrett Wollman 			break;
1081a0292f23SGarrett Wollman 
1082df8bae1dSRodney W. Grimes 		default:
1083df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1084df8bae1dSRodney W. Grimes 			break;
1085df8bae1dSRodney W. Grimes 		}
1086df8bae1dSRodney W. Grimes 		break;
1087df8bae1dSRodney W. Grimes 
1088cfe8b629SGarrett Wollman 	case SOPT_GET:
1089cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1090df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1091cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1092df8bae1dSRodney W. Grimes 			break;
1093df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1094cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1095df8bae1dSRodney W. Grimes 			break;
1096a0292f23SGarrett Wollman 		case TCP_NOOPT:
1097cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1098a0292f23SGarrett Wollman 			break;
1099a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1100cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1101a0292f23SGarrett Wollman 			break;
1102df8bae1dSRodney W. Grimes 		default:
1103df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1104df8bae1dSRodney W. Grimes 			break;
1105df8bae1dSRodney W. Grimes 		}
1106cfe8b629SGarrett Wollman 		if (error == 0)
1107cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1108df8bae1dSRodney W. Grimes 		break;
1109df8bae1dSRodney W. Grimes 	}
1110f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1111df8bae1dSRodney W. Grimes 	splx(s);
1112df8bae1dSRodney W. Grimes 	return (error);
1113df8bae1dSRodney W. Grimes }
1114df8bae1dSRodney W. Grimes 
111526e30fbbSDavid Greenman /*
111626e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
111726e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
111826e30fbbSDavid Greenman  * be set by the route).
111926e30fbbSDavid Greenman  */
112081e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
11213d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
11223d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
112381e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
11243d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
11253d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1126df8bae1dSRodney W. Grimes 
1127df8bae1dSRodney W. Grimes /*
1128df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1129df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1130df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1131df8bae1dSRodney W. Grimes  */
11320312fbe9SPoul-Henning Kamp static int
1133b40ce416SJulian Elischer tcp_attach(so, td)
1134df8bae1dSRodney W. Grimes 	struct socket *so;
1135b40ce416SJulian Elischer 	struct thread *td;
1136df8bae1dSRodney W. Grimes {
1137df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1138df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1139df8bae1dSRodney W. Grimes 	int error;
1140fb59c426SYoshinobu Inoue #ifdef INET6
1141fb59c426SYoshinobu Inoue 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1142fb59c426SYoshinobu Inoue #endif
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1145df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1146df8bae1dSRodney W. Grimes 		if (error)
1147df8bae1dSRodney W. Grimes 			return (error);
1148df8bae1dSRodney W. Grimes 	}
1149b40ce416SJulian Elischer 	error = in_pcballoc(so, &tcbinfo, td);
1150df8bae1dSRodney W. Grimes 	if (error)
1151df8bae1dSRodney W. Grimes 		return (error);
1152df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1153fb59c426SYoshinobu Inoue #ifdef INET6
1154fb59c426SYoshinobu Inoue 	if (isipv6) {
1155fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1156fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1157fb59c426SYoshinobu Inoue 	}
1158fb59c426SYoshinobu Inoue 	else
1159fb59c426SYoshinobu Inoue #endif
1160cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1161df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1162df8bae1dSRodney W. Grimes 	if (tp == 0) {
11634cc20ab1SSeigo Tanimura 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1164df8bae1dSRodney W. Grimes 
1165df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1166fb59c426SYoshinobu Inoue #ifdef INET6
1167fb59c426SYoshinobu Inoue 		if (isipv6)
1168fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1169fb59c426SYoshinobu Inoue 		else
1170fb59c426SYoshinobu Inoue #endif
1171df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1172df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1173df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1174df8bae1dSRodney W. Grimes 	}
1175df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1176df8bae1dSRodney W. Grimes 	return (0);
1177df8bae1dSRodney W. Grimes }
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes /*
1180df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1181df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1182df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1183df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1184df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1185df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1186df8bae1dSRodney W. Grimes  */
11870312fbe9SPoul-Henning Kamp static struct tcpcb *
1188df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1189df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1190df8bae1dSRodney W. Grimes {
1191df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1192df8bae1dSRodney W. Grimes 
1193df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1194df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
11954cc20ab1SSeigo Tanimura 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1196243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
11974cc20ab1SSeigo Tanimura 	else {
1198df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1199df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1200df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1201df8bae1dSRodney W. Grimes 		if (tp)
1202df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1203df8bae1dSRodney W. Grimes 	}
1204df8bae1dSRodney W. Grimes 	return (tp);
1205df8bae1dSRodney W. Grimes }
1206df8bae1dSRodney W. Grimes 
1207df8bae1dSRodney W. Grimes /*
1208df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1209df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1210df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1211df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1212df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1213df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1214df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1215df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1216df8bae1dSRodney W. Grimes  */
12170312fbe9SPoul-Henning Kamp static struct tcpcb *
1218df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1219df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1220df8bae1dSRodney W. Grimes {
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1223df8bae1dSRodney W. Grimes 
1224df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1225df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1226df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1227df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1228df8bae1dSRodney W. Grimes 		break;
1229df8bae1dSRodney W. Grimes 
1230a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1231df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1232a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1233a0292f23SGarrett Wollman 		break;
1234a0292f23SGarrett Wollman 
1235df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1236df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1237df8bae1dSRodney W. Grimes 		break;
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1240df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1241df8bae1dSRodney W. Grimes 		break;
1242df8bae1dSRodney W. Grimes 	}
1243b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1244df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1245b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1246b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
12479b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
12489b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1249b6239c4aSAndras Olah 	}
1250df8bae1dSRodney W. Grimes 	return (tp);
1251df8bae1dSRodney W. Grimes }
1252a0292f23SGarrett Wollman 
1253