xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 265ed01285f178a5421761c0546791f994a5b652)
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"
381cfd4b53SBruce M Simpson #include "opt_inet.h"
39fb59c426SYoshinobu Inoue #include "opt_inet6.h"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
45c7a82f90SGarrett Wollman #include <sys/kernel.h>
4698163b98SPoul-Henning Kamp #include <sys/sysctl.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48fb59c426SYoshinobu Inoue #ifdef INET6
49fb59c426SYoshinobu Inoue #include <sys/domain.h>
50fb59c426SYoshinobu Inoue #endif /* INET6 */
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5491421ba2SRobert Watson #include <sys/proc.h>
5591421ba2SRobert Watson #include <sys/jail.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <net/if.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <netinet/in.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
62fb59c426SYoshinobu Inoue #ifdef INET6
63fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
64fb59c426SYoshinobu Inoue #endif
65df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
66fb59c426SYoshinobu Inoue #ifdef INET6
67fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
68fb59c426SYoshinobu Inoue #endif
69b5e8ce9fSBruce Evans #include <netinet/in_var.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
71fb59c426SYoshinobu Inoue #ifdef INET6
72fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
73fb59c426SYoshinobu Inoue #endif
74df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
75df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
80610ee2f9SDavid Greenman #ifdef TCPDEBUG
81df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
82610ee2f9SDavid Greenman #endif
83df8bae1dSRodney W. Grimes 
846a800098SYoshinobu Inoue #ifdef IPSEC
856a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
866a800098SYoshinobu Inoue #endif /*IPSEC*/
876a800098SYoshinobu Inoue 
88df8bae1dSRodney W. Grimes /*
89df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
90df8bae1dSRodney W. Grimes  */
91117bcae7SGarrett Wollman extern	char *tcpstates[];	/* XXX ??? */
92df8bae1dSRodney W. Grimes 
934d77a549SAlfred Perlstein static int	tcp_attach(struct socket *, struct thread *td);
944d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
954d77a549SAlfred Perlstein 		    struct thread *td);
96fb59c426SYoshinobu Inoue #ifdef INET6
974d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
984d77a549SAlfred Perlstein 		    struct thread *td);
99fb59c426SYoshinobu Inoue #endif /* INET6 */
1000312fbe9SPoul-Henning Kamp static struct tcpcb *
1014d77a549SAlfred Perlstein 		tcp_disconnect(struct tcpcb *);
1020312fbe9SPoul-Henning Kamp static struct tcpcb *
1034d77a549SAlfred Perlstein 		tcp_usrclosed(struct tcpcb *);
1042c37256eSGarrett Wollman 
1052c37256eSGarrett Wollman #ifdef TCPDEBUG
1061db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1072c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1084cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1094cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1102c37256eSGarrett Wollman #else
1112c37256eSGarrett Wollman #define	TCPDEBUG0
1122c37256eSGarrett Wollman #define	TCPDEBUG1()
1132c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1142c37256eSGarrett Wollman #endif
1152c37256eSGarrett Wollman 
1162c37256eSGarrett Wollman /*
1172c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1182c37256eSGarrett Wollman  * and an internet control block.
1192c37256eSGarrett Wollman  */
1202c37256eSGarrett Wollman static int
121b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1222c37256eSGarrett Wollman {
1232c37256eSGarrett Wollman 	int s = splnet();
1242c37256eSGarrett Wollman 	int error;
125f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1262c37256eSGarrett Wollman 	struct tcpcb *tp = 0;
1272c37256eSGarrett Wollman 	TCPDEBUG0;
1282c37256eSGarrett Wollman 
129f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
1302c37256eSGarrett Wollman 	TCPDEBUG1();
131f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
1322c37256eSGarrett Wollman 	if (inp) {
1332c37256eSGarrett Wollman 		error = EISCONN;
1342c37256eSGarrett Wollman 		goto out;
1352c37256eSGarrett Wollman 	}
1362c37256eSGarrett Wollman 
137b40ce416SJulian Elischer 	error = tcp_attach(so, td);
1382c37256eSGarrett Wollman 	if (error)
1392c37256eSGarrett Wollman 		goto out;
1402c37256eSGarrett Wollman 
1412c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1423879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
143f76fcf6dSJeffrey Hsu 
144f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
145f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1462c37256eSGarrett Wollman out:
1472c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
148f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
1492c37256eSGarrett Wollman 	splx(s);
1502c37256eSGarrett Wollman 	return error;
1512c37256eSGarrett Wollman }
1522c37256eSGarrett Wollman 
1532c37256eSGarrett Wollman /*
1542c37256eSGarrett Wollman  * pru_detach() detaches the TCP protocol from the socket.
1552c37256eSGarrett Wollman  * If the protocol state is non-embryonic, then can't
1562c37256eSGarrett Wollman  * do this directly: have to initiate a pru_disconnect(),
1572c37256eSGarrett Wollman  * which may finish later; embryonic TCB's can just
1582c37256eSGarrett Wollman  * be discarded here.
1592c37256eSGarrett Wollman  */
1602c37256eSGarrett Wollman static int
1612c37256eSGarrett Wollman tcp_usr_detach(struct socket *so)
1622c37256eSGarrett Wollman {
1632c37256eSGarrett Wollman 	int s = splnet();
1642c37256eSGarrett Wollman 	int error = 0;
165f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1662c37256eSGarrett Wollman 	struct tcpcb *tp;
1672c37256eSGarrett Wollman 	TCPDEBUG0;
1682c37256eSGarrett Wollman 
169f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
170f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
1712c37256eSGarrett Wollman 	if (inp == 0) {
172f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
1732c37256eSGarrett Wollman 		splx(s);
1742c37256eSGarrett Wollman 		return EINVAL;	/* XXX */
1752c37256eSGarrett Wollman 	}
176f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1772c37256eSGarrett Wollman 	tp = intotcpcb(inp);
1782c37256eSGarrett Wollman 	TCPDEBUG1();
1792c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
1802c37256eSGarrett Wollman 
1812c37256eSGarrett Wollman 	TCPDEBUG2(PRU_DETACH);
182f76fcf6dSJeffrey Hsu 	if (tp)
183f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
184f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
1852c37256eSGarrett Wollman 	splx(s);
1862c37256eSGarrett Wollman 	return error;
1872c37256eSGarrett Wollman }
1882c37256eSGarrett Wollman 
189f76fcf6dSJeffrey Hsu #define INI_NOLOCK	0
190f76fcf6dSJeffrey Hsu #define INI_READ	1
191f76fcf6dSJeffrey Hsu #define INI_WRITE	2
192f76fcf6dSJeffrey Hsu 
193f76fcf6dSJeffrey Hsu #define	COMMON_START()						\
194f76fcf6dSJeffrey Hsu 	TCPDEBUG0;						\
1952c37256eSGarrett Wollman 	do {							\
196f76fcf6dSJeffrey Hsu 		if (inirw == INI_READ)				\
197f76fcf6dSJeffrey Hsu 			INP_INFO_RLOCK(&tcbinfo);		\
198f76fcf6dSJeffrey Hsu 		else if (inirw == INI_WRITE)			\
199f76fcf6dSJeffrey Hsu 			INP_INFO_WLOCK(&tcbinfo);		\
200f76fcf6dSJeffrey Hsu 		inp = sotoinpcb(so);				\
2012c37256eSGarrett Wollman 		if (inp == 0) {					\
202f76fcf6dSJeffrey Hsu 			if (inirw == INI_READ)			\
203f76fcf6dSJeffrey Hsu 				INP_INFO_RUNLOCK(&tcbinfo);	\
204f76fcf6dSJeffrey Hsu 			else if (inirw == INI_WRITE)		\
205f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);	\
2062c37256eSGarrett Wollman 			splx(s);				\
2072c37256eSGarrett Wollman 			return EINVAL;				\
2082c37256eSGarrett Wollman 		}						\
209f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);					\
210f76fcf6dSJeffrey Hsu 		if (inirw == INI_READ)				\
211f76fcf6dSJeffrey Hsu 			INP_INFO_RUNLOCK(&tcbinfo);		\
2122c37256eSGarrett Wollman 		tp = intotcpcb(inp);				\
2132c37256eSGarrett Wollman 		TCPDEBUG1();					\
2142c37256eSGarrett Wollman } while(0)
2152c37256eSGarrett Wollman 
216f76fcf6dSJeffrey Hsu #define COMMON_END(req)						\
217f76fcf6dSJeffrey Hsu out:	TCPDEBUG2(req);						\
218f76fcf6dSJeffrey Hsu 	do {							\
219f76fcf6dSJeffrey Hsu 		if (tp)						\
220f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);			\
221f76fcf6dSJeffrey Hsu 		if (inirw == INI_WRITE)				\
222f76fcf6dSJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);		\
223f76fcf6dSJeffrey Hsu 		splx(s);					\
224f76fcf6dSJeffrey Hsu 		return error;					\
225f76fcf6dSJeffrey Hsu 		goto out;					\
226f76fcf6dSJeffrey Hsu } while(0)
2272c37256eSGarrett Wollman 
2282c37256eSGarrett Wollman /*
2292c37256eSGarrett Wollman  * Give the socket an address.
2302c37256eSGarrett Wollman  */
2312c37256eSGarrett Wollman static int
232b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2332c37256eSGarrett Wollman {
2342c37256eSGarrett Wollman 	int s = splnet();
2352c37256eSGarrett Wollman 	int error = 0;
236f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
2372c37256eSGarrett Wollman 	struct tcpcb *tp;
2382c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
239edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
2402c37256eSGarrett Wollman 
2412c37256eSGarrett Wollman 	COMMON_START();
2422c37256eSGarrett Wollman 
2432c37256eSGarrett Wollman 	/*
2442c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2452c37256eSGarrett Wollman 	 * to them.
2462c37256eSGarrett Wollman 	 */
24757bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
248e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
249e29ef13fSDon Lewis 		return (EINVAL);
2502c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
2512c37256eSGarrett Wollman 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
2522c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
2532c37256eSGarrett Wollman 		goto out;
2542c37256eSGarrett Wollman 	}
255b40ce416SJulian Elischer 	error = in_pcbbind(inp, nam, td);
2562c37256eSGarrett Wollman 	if (error)
2572c37256eSGarrett Wollman 		goto out;
2582c37256eSGarrett Wollman 	COMMON_END(PRU_BIND);
2592c37256eSGarrett Wollman }
2602c37256eSGarrett Wollman 
261fb59c426SYoshinobu Inoue #ifdef INET6
262fb59c426SYoshinobu Inoue static int
263b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
264fb59c426SYoshinobu Inoue {
265fb59c426SYoshinobu Inoue 	int s = splnet();
266fb59c426SYoshinobu Inoue 	int error = 0;
267f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
268fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
269fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
270edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
271fb59c426SYoshinobu Inoue 
272fb59c426SYoshinobu Inoue 	COMMON_START();
273fb59c426SYoshinobu Inoue 
274fb59c426SYoshinobu Inoue 	/*
275fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
276fb59c426SYoshinobu Inoue 	 * to them.
277fb59c426SYoshinobu Inoue 	 */
278fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
279e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
280e29ef13fSDon Lewis 		return (EINVAL);
281fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
282fb59c426SYoshinobu Inoue 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
283fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
284fb59c426SYoshinobu Inoue 		goto out;
285fb59c426SYoshinobu Inoue 	}
286fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
287fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
28866ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
289fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
290fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
291fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
292fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
293fb59c426SYoshinobu Inoue 
294fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
295fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
296fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
297b40ce416SJulian Elischer 			error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
298fb59c426SYoshinobu Inoue 			goto out;
299fb59c426SYoshinobu Inoue 		}
300fb59c426SYoshinobu Inoue 	}
301b40ce416SJulian Elischer 	error = in6_pcbbind(inp, nam, td);
302fb59c426SYoshinobu Inoue 	if (error)
303fb59c426SYoshinobu Inoue 		goto out;
304fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
305fb59c426SYoshinobu Inoue }
306fb59c426SYoshinobu Inoue #endif /* INET6 */
307fb59c426SYoshinobu Inoue 
3082c37256eSGarrett Wollman /*
3092c37256eSGarrett Wollman  * Prepare to accept connections.
3102c37256eSGarrett Wollman  */
3112c37256eSGarrett Wollman static int
312b40ce416SJulian Elischer tcp_usr_listen(struct socket *so, struct thread *td)
3132c37256eSGarrett Wollman {
3142c37256eSGarrett Wollman 	int s = splnet();
3152c37256eSGarrett Wollman 	int error = 0;
316f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3172c37256eSGarrett Wollman 	struct tcpcb *tp;
318edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
3192c37256eSGarrett Wollman 
3202c37256eSGarrett Wollman 	COMMON_START();
3212c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
322b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)0, td);
3232c37256eSGarrett Wollman 	if (error == 0)
3242c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
3252c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
3262c37256eSGarrett Wollman }
3272c37256eSGarrett Wollman 
328fb59c426SYoshinobu Inoue #ifdef INET6
329fb59c426SYoshinobu Inoue static int
330b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td)
331fb59c426SYoshinobu Inoue {
332fb59c426SYoshinobu Inoue 	int s = splnet();
333fb59c426SYoshinobu Inoue 	int error = 0;
334f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
335fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
336edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
337fb59c426SYoshinobu Inoue 
338fb59c426SYoshinobu Inoue 	COMMON_START();
339fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
340fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
34166ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
342fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
343b40ce416SJulian Elischer 		error = in6_pcbbind(inp, (struct sockaddr *)0, td);
344fb59c426SYoshinobu Inoue 	}
345fb59c426SYoshinobu Inoue 	if (error == 0)
346fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
347fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
348fb59c426SYoshinobu Inoue }
349fb59c426SYoshinobu Inoue #endif /* INET6 */
350fb59c426SYoshinobu Inoue 
3512c37256eSGarrett Wollman /*
3522c37256eSGarrett Wollman  * Initiate connection to peer.
3532c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3542c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3552c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3562c37256eSGarrett Wollman  * Send initial segment on connection.
3572c37256eSGarrett Wollman  */
3582c37256eSGarrett Wollman static int
359b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
3602c37256eSGarrett Wollman {
3612c37256eSGarrett Wollman 	int s = splnet();
3622c37256eSGarrett Wollman 	int error = 0;
363f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3642c37256eSGarrett Wollman 	struct tcpcb *tp;
3652c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
366f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
3672c37256eSGarrett Wollman 
3682c37256eSGarrett Wollman 	COMMON_START();
3692c37256eSGarrett Wollman 
3702c37256eSGarrett Wollman 	/*
3712c37256eSGarrett Wollman 	 * Must disallow TCP ``connections'' to multicast addresses.
3722c37256eSGarrett Wollman 	 */
37357bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
374e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
375e29ef13fSDon Lewis 		return (EINVAL);
3762c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
3772c37256eSGarrett Wollman 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
3782c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
3792c37256eSGarrett Wollman 		goto out;
3802c37256eSGarrett Wollman 	}
3812c37256eSGarrett Wollman 
382a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
383a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
38475c13541SPoul-Henning Kamp 
385b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
3862c37256eSGarrett Wollman 		goto out;
3872c37256eSGarrett Wollman 	error = tcp_output(tp);
3882c37256eSGarrett Wollman 	COMMON_END(PRU_CONNECT);
3892c37256eSGarrett Wollman }
3902c37256eSGarrett Wollman 
391fb59c426SYoshinobu Inoue #ifdef INET6
392fb59c426SYoshinobu Inoue static int
393b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
394fb59c426SYoshinobu Inoue {
395fb59c426SYoshinobu Inoue 	int s = splnet();
396fb59c426SYoshinobu Inoue 	int error = 0;
397f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
398fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
399fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
400f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
401fb59c426SYoshinobu Inoue 
402fb59c426SYoshinobu Inoue 	COMMON_START();
403fb59c426SYoshinobu Inoue 
404fb59c426SYoshinobu Inoue 	/*
405fb59c426SYoshinobu Inoue 	 * Must disallow TCP ``connections'' to multicast addresses.
406fb59c426SYoshinobu Inoue 	 */
407fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
408e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
409e29ef13fSDon Lewis 		return (EINVAL);
410fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
411fb59c426SYoshinobu Inoue 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
412fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
413fb59c426SYoshinobu Inoue 		goto out;
414fb59c426SYoshinobu Inoue 	}
415fb59c426SYoshinobu Inoue 
41633841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
417fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
418fb59c426SYoshinobu Inoue 
419d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
420d46a5312SMaxim Konovalov 			error = EINVAL;
421d46a5312SMaxim Konovalov 			goto out;
422d46a5312SMaxim Konovalov 		}
42333841545SHajimu UMEMOTO 
424fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
425fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
426fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
427b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
428fb59c426SYoshinobu Inoue 			goto out;
429fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
430fb59c426SYoshinobu Inoue 		goto out;
431fb59c426SYoshinobu Inoue 	}
432fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
433fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
434b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
435b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
436fb59c426SYoshinobu Inoue 		goto out;
437fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
438fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
439fb59c426SYoshinobu Inoue }
440fb59c426SYoshinobu Inoue #endif /* INET6 */
441fb59c426SYoshinobu Inoue 
4422c37256eSGarrett Wollman /*
4432c37256eSGarrett Wollman  * Initiate disconnect from peer.
4442c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
4452c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
4462c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
4472c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
4482c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
4492c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
4502c37256eSGarrett Wollman  *
4512c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
4522c37256eSGarrett Wollman  */
4532c37256eSGarrett Wollman static int
4542c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
4552c37256eSGarrett Wollman {
4562c37256eSGarrett Wollman 	int s = splnet();
4572c37256eSGarrett Wollman 	int error = 0;
458f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
4592c37256eSGarrett Wollman 	struct tcpcb *tp;
460f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
4612c37256eSGarrett Wollman 
4622c37256eSGarrett Wollman 	COMMON_START();
4632c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4642c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4652c37256eSGarrett Wollman }
4662c37256eSGarrett Wollman 
4672c37256eSGarrett Wollman /*
4682c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4692c37256eSGarrett Wollman  * done at higher levels; just return the address
4702c37256eSGarrett Wollman  * of the peer, storing through addr.
4712c37256eSGarrett Wollman  */
4722c37256eSGarrett Wollman static int
47357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4742c37256eSGarrett Wollman {
475f76fcf6dSJeffrey Hsu 	int s;
4762c37256eSGarrett Wollman 	int error = 0;
477f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
4781db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
47926ef6ac4SDon Lewis 	struct in_addr addr;
48026ef6ac4SDon Lewis 	in_port_t port = 0;
4811db24ffbSJonathan Lemon 	TCPDEBUG0;
4822c37256eSGarrett Wollman 
483c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
484c0647e0dSJonathan Lemon 		error = ECONNABORTED;
485c0647e0dSJonathan Lemon 		goto out;
486c0647e0dSJonathan Lemon 	}
487f76fcf6dSJeffrey Hsu 
488f76fcf6dSJeffrey Hsu 	s = splnet();
489f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
490f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
491f76fcf6dSJeffrey Hsu 	if (!inp) {
492f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
4931db24ffbSJonathan Lemon 		splx(s);
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 	/*
50226ef6ac4SDon Lewis 	 * We inline in_setpeeraddr and COMMON_END here, so that we can
50326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
50426ef6ac4SDon Lewis 	 * release the lock.
505f76fcf6dSJeffrey Hsu 	 */
50626ef6ac4SDon Lewis 	port = inp->inp_fport;
50726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
508f76fcf6dSJeffrey Hsu 
50926ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
51026ef6ac4SDon Lewis 	if (tp)
51126ef6ac4SDon Lewis 		INP_UNLOCK(inp);
51226ef6ac4SDon Lewis 	splx(s);
51326ef6ac4SDon Lewis 	if (error == 0)
51426ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
51526ef6ac4SDon Lewis 	return error;
5162c37256eSGarrett Wollman }
5172c37256eSGarrett Wollman 
518fb59c426SYoshinobu Inoue #ifdef INET6
519fb59c426SYoshinobu Inoue static int
520fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
521fb59c426SYoshinobu Inoue {
522f76fcf6dSJeffrey Hsu 	int s;
523f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
524fb59c426SYoshinobu Inoue 	int error = 0;
5251db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
52626ef6ac4SDon Lewis 	struct in_addr addr;
52726ef6ac4SDon Lewis 	struct in6_addr addr6;
52826ef6ac4SDon Lewis 	in_port_t port = 0;
52926ef6ac4SDon Lewis 	int v4 = 0;
5301db24ffbSJonathan Lemon 	TCPDEBUG0;
531fb59c426SYoshinobu Inoue 
532c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
533c0647e0dSJonathan Lemon 		error = ECONNABORTED;
534c0647e0dSJonathan Lemon 		goto out;
535c0647e0dSJonathan Lemon 	}
536f76fcf6dSJeffrey Hsu 
537f76fcf6dSJeffrey Hsu 	s = splnet();
538f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
539f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
5401db24ffbSJonathan Lemon 	if (inp == 0) {
541f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
5421db24ffbSJonathan Lemon 		splx(s);
5431db24ffbSJonathan Lemon 		return (EINVAL);
5441db24ffbSJonathan Lemon 	}
545f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
546f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
5471db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
5481db24ffbSJonathan Lemon 	TCPDEBUG1();
54926ef6ac4SDon Lewis 	/*
55026ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
55126ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
55226ef6ac4SDon Lewis 	 * release the lock.
55326ef6ac4SDon Lewis 	 */
55426ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
55526ef6ac4SDon Lewis 		v4 = 1;
55626ef6ac4SDon Lewis 		port = inp->inp_fport;
55726ef6ac4SDon Lewis 		addr = inp->inp_faddr;
55826ef6ac4SDon Lewis 	} else {
55926ef6ac4SDon Lewis 		port = inp->inp_fport;
56026ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
56126ef6ac4SDon Lewis 	}
56226ef6ac4SDon Lewis 
56326ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
56426ef6ac4SDon Lewis 	if (tp)
56526ef6ac4SDon Lewis 		INP_UNLOCK(inp);
56626ef6ac4SDon Lewis 	splx(s);
56726ef6ac4SDon Lewis 	if (error == 0) {
56826ef6ac4SDon Lewis 		if (v4)
56926ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
57026ef6ac4SDon Lewis 		else
57126ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
57226ef6ac4SDon Lewis 	}
57326ef6ac4SDon Lewis 	return error;
574fb59c426SYoshinobu Inoue }
575fb59c426SYoshinobu Inoue #endif /* INET6 */
576f76fcf6dSJeffrey Hsu 
577f76fcf6dSJeffrey Hsu /*
578f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
579f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
580f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
581f76fcf6dSJeffrey Hsu  */
582f76fcf6dSJeffrey Hsu static int
583f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
584f76fcf6dSJeffrey Hsu {
585f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
586f76fcf6dSJeffrey Hsu }
587f76fcf6dSJeffrey Hsu 
588f76fcf6dSJeffrey Hsu /*
589f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
590f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
591f76fcf6dSJeffrey Hsu  */
592f76fcf6dSJeffrey Hsu static int
593f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
594f76fcf6dSJeffrey Hsu {
595f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
596f76fcf6dSJeffrey Hsu }
597f76fcf6dSJeffrey Hsu 
5982c37256eSGarrett Wollman /*
5992c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
6002c37256eSGarrett Wollman  */
6012c37256eSGarrett Wollman static int
6022c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
6032c37256eSGarrett Wollman {
6042c37256eSGarrett Wollman 	int s = splnet();
6052c37256eSGarrett Wollman 	int error = 0;
606f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6072c37256eSGarrett Wollman 	struct tcpcb *tp;
608f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
6092c37256eSGarrett Wollman 
6102c37256eSGarrett Wollman 	COMMON_START();
6112c37256eSGarrett Wollman 	socantsendmore(so);
6122c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
6132c37256eSGarrett Wollman 	if (tp)
6142c37256eSGarrett Wollman 		error = tcp_output(tp);
6152c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
6162c37256eSGarrett Wollman }
6172c37256eSGarrett Wollman 
6182c37256eSGarrett Wollman /*
6192c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
6202c37256eSGarrett Wollman  */
6212c37256eSGarrett Wollman static int
6222c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
6232c37256eSGarrett Wollman {
6242c37256eSGarrett Wollman 	int s = splnet();
6252c37256eSGarrett Wollman 	int error = 0;
626f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6272c37256eSGarrett Wollman 	struct tcpcb *tp;
628f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
6292c37256eSGarrett Wollman 
6302c37256eSGarrett Wollman 	COMMON_START();
6312c37256eSGarrett Wollman 	tcp_output(tp);
6322c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
6332c37256eSGarrett Wollman }
6342c37256eSGarrett Wollman 
6352c37256eSGarrett Wollman /*
6362c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
6379c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
6389c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
6399c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
6409c9906e9SPeter Wemm  * generally are caller-frees.
6412c37256eSGarrett Wollman  */
6422c37256eSGarrett Wollman static int
64357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
644b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
6452c37256eSGarrett Wollman {
6462c37256eSGarrett Wollman 	int s = splnet();
6472c37256eSGarrett Wollman 	int error = 0;
648f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6492c37256eSGarrett Wollman 	struct tcpcb *tp;
650f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
651fb59c426SYoshinobu Inoue #ifdef INET6
652fb59c426SYoshinobu Inoue 	int isipv6;
653fb59c426SYoshinobu Inoue #endif
6549c9906e9SPeter Wemm 	TCPDEBUG0;
6552c37256eSGarrett Wollman 
656f76fcf6dSJeffrey Hsu 	/*
657f76fcf6dSJeffrey Hsu 	 * Need write lock here because this function might call
658f76fcf6dSJeffrey Hsu 	 * tcp_connect or tcp_usrclosed.
659f76fcf6dSJeffrey Hsu 	 * We really want to have to this function upgrade from read lock
660f76fcf6dSJeffrey Hsu 	 * to write lock.  XXX
661f76fcf6dSJeffrey Hsu 	 */
662f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
663f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
6649c9906e9SPeter Wemm 	if (inp == NULL) {
6659c9906e9SPeter Wemm 		/*
6669c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
6679c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
6689c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
6699c9906e9SPeter Wemm 		 */
6709c9906e9SPeter Wemm 		if (m)
6719c9906e9SPeter Wemm 			m_freem(m);
6729c9906e9SPeter Wemm 		if (control)
6739c9906e9SPeter Wemm 			m_freem(control);
6749c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
67545d3a132SPeter Wemm 		tp = NULL;
67645d3a132SPeter Wemm 		TCPDEBUG1();
6779c9906e9SPeter Wemm 		goto out;
6789c9906e9SPeter Wemm 	}
679f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
680fb59c426SYoshinobu Inoue #ifdef INET6
681fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
682fb59c426SYoshinobu Inoue #endif /* INET6 */
6839c9906e9SPeter Wemm 	tp = intotcpcb(inp);
6849c9906e9SPeter Wemm 	TCPDEBUG1();
6859c9906e9SPeter Wemm 	if (control) {
6869c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
6879c9906e9SPeter Wemm 		if (control->m_len) {
6889c9906e9SPeter Wemm 			m_freem(control);
6892c37256eSGarrett Wollman 			if (m)
6902c37256eSGarrett Wollman 				m_freem(m);
691744f87eaSDavid Greenman 			error = EINVAL;
692744f87eaSDavid Greenman 			goto out;
6932c37256eSGarrett Wollman 		}
6949c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
6959c9906e9SPeter Wemm 	}
6962c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
697395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
6982c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
6992c37256eSGarrett Wollman 			/*
7002c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
7012c37256eSGarrett Wollman 			 * initialize window to default value, and
7022c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
7032c37256eSGarrett Wollman 			 * MSS.
7042c37256eSGarrett Wollman 			 */
705fb59c426SYoshinobu Inoue #ifdef INET6
706fb59c426SYoshinobu Inoue 			if (isipv6)
707b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
708fb59c426SYoshinobu Inoue 			else
709fb59c426SYoshinobu Inoue #endif /* INET6 */
710b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
7112c37256eSGarrett Wollman 			if (error)
7122c37256eSGarrett Wollman 				goto out;
7132c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
7142c37256eSGarrett Wollman 			tcp_mss(tp, -1);
7152c37256eSGarrett Wollman 		}
7162c37256eSGarrett Wollman 
7172c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
7182c37256eSGarrett Wollman 			/*
7192c37256eSGarrett Wollman 			 * Close the send side of the connection after
7202c37256eSGarrett Wollman 			 * the data is sent.
7212c37256eSGarrett Wollman 			 */
7222c37256eSGarrett Wollman 			socantsendmore(so);
7232c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
7242c37256eSGarrett Wollman 		}
725b0acefa8SBill Fenner 		if (tp != NULL) {
726b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
727b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
7282c37256eSGarrett Wollman 			error = tcp_output(tp);
729b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
730b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
731b0acefa8SBill Fenner 		}
7322c37256eSGarrett Wollman 	} else {
7332c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
7342c37256eSGarrett Wollman 			m_freem(m);
7352c37256eSGarrett Wollman 			error = ENOBUFS;
7362c37256eSGarrett Wollman 			goto out;
7372c37256eSGarrett Wollman 		}
7382c37256eSGarrett Wollman 		/*
7392c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
7402c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
7412c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
7422c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
7432c37256eSGarrett Wollman 		 * of data past the urgent section.
7442c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
7452c37256eSGarrett Wollman 		 */
746395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
747ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
748ef53690bSGarrett Wollman 			/*
749ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
750ef53690bSGarrett Wollman 			 * initialize window to default value, and
751ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
752ef53690bSGarrett Wollman 			 * MSS.
753ef53690bSGarrett Wollman 			 */
754fb59c426SYoshinobu Inoue #ifdef INET6
755fb59c426SYoshinobu Inoue 			if (isipv6)
756b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
757fb59c426SYoshinobu Inoue 			else
758fb59c426SYoshinobu Inoue #endif /* INET6 */
759b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
760ef53690bSGarrett Wollman 			if (error)
761ef53690bSGarrett Wollman 				goto out;
762ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
763ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
764ef53690bSGarrett Wollman 		}
7652c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
7662c37256eSGarrett Wollman 		tp->t_force = 1;
7672c37256eSGarrett Wollman 		error = tcp_output(tp);
7682c37256eSGarrett Wollman 		tp->t_force = 0;
7692c37256eSGarrett Wollman 	}
7702c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
7712c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
7722c37256eSGarrett Wollman }
7732c37256eSGarrett Wollman 
7742c37256eSGarrett Wollman /*
7752c37256eSGarrett Wollman  * Abort the TCP.
7762c37256eSGarrett Wollman  */
7772c37256eSGarrett Wollman static int
7782c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
7792c37256eSGarrett Wollman {
7802c37256eSGarrett Wollman 	int s = splnet();
7812c37256eSGarrett Wollman 	int error = 0;
782f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7832c37256eSGarrett Wollman 	struct tcpcb *tp;
784f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
7852c37256eSGarrett Wollman 
7862c37256eSGarrett Wollman 	COMMON_START();
7872c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
7882c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
7892c37256eSGarrett Wollman }
7902c37256eSGarrett Wollman 
7912c37256eSGarrett Wollman /*
7922c37256eSGarrett Wollman  * Receive out-of-band data.
7932c37256eSGarrett Wollman  */
7942c37256eSGarrett Wollman static int
7952c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
7962c37256eSGarrett Wollman {
7972c37256eSGarrett Wollman 	int s = splnet();
7982c37256eSGarrett Wollman 	int error = 0;
799f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
8002c37256eSGarrett Wollman 	struct tcpcb *tp;
801f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
8022c37256eSGarrett Wollman 
8032c37256eSGarrett Wollman 	COMMON_START();
8042c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
8052c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
8064cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
8074cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
8082c37256eSGarrett Wollman 		error = EINVAL;
8092c37256eSGarrett Wollman 		goto out;
8102c37256eSGarrett Wollman 	}
8112c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
8122c37256eSGarrett Wollman 		error = EWOULDBLOCK;
8132c37256eSGarrett Wollman 		goto out;
8142c37256eSGarrett Wollman 	}
8152c37256eSGarrett Wollman 	m->m_len = 1;
8162c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
8172c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
8182c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
8192c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
8202c37256eSGarrett Wollman }
8212c37256eSGarrett Wollman 
8222c37256eSGarrett Wollman /* xxx - should be const */
8232c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
8242c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
825117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
826f76fcf6dSJeffrey Hsu 	tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd,
827117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
828a557af22SRobert Watson 	tcp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
8292c37256eSGarrett Wollman };
830df8bae1dSRodney W. Grimes 
831fb59c426SYoshinobu Inoue #ifdef INET6
832fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
833fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
834fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
835fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
836fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
837a557af22SRobert Watson 	in6_mapped_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
838fb59c426SYoshinobu Inoue };
839fb59c426SYoshinobu Inoue #endif /* INET6 */
840fb59c426SYoshinobu Inoue 
841a0292f23SGarrett Wollman /*
842a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
843a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
8445200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
8455200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
8465200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
8475200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
848a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
849a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
850a0292f23SGarrett Wollman  */
8510312fbe9SPoul-Henning Kamp static int
852b40ce416SJulian Elischer tcp_connect(tp, nam, td)
853a0292f23SGarrett Wollman 	register struct tcpcb *tp;
85457bf258eSGarrett Wollman 	struct sockaddr *nam;
855b40ce416SJulian Elischer 	struct thread *td;
856a0292f23SGarrett Wollman {
857a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
858a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
859a3b6edc3SJonathan Lemon 	struct tcptw *otw;
86097d8d152SAndre Oppermann 	struct rmxp_tao tao;
8615200e00eSIan Dowse 	struct in_addr laddr;
8625200e00eSIan Dowse 	u_short lport;
863c3229e05SDavid Greenman 	int error;
864a0292f23SGarrett Wollman 
86597d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
86697d8d152SAndre Oppermann 
867a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
868b40ce416SJulian Elischer 		error = in_pcbbind(inp, (struct sockaddr *)0, td);
869a0292f23SGarrett Wollman 		if (error)
870a0292f23SGarrett Wollman 			return error;
871a0292f23SGarrett Wollman 	}
872a0292f23SGarrett Wollman 
873a0292f23SGarrett Wollman 	/*
874a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
875a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
876a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
877a0292f23SGarrett Wollman 	 */
8785200e00eSIan Dowse 	laddr = inp->inp_laddr;
8795200e00eSIan Dowse 	lport = inp->inp_lport;
8805200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
8815200e00eSIan Dowse 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td);
8825200e00eSIan Dowse 	if (error && oinp == NULL)
883d3628763SRodney W. Grimes 		return error;
884a0292f23SGarrett Wollman 	if (oinp) {
885a3b6edc3SJonathan Lemon 		if (oinp != inp &&
886a3b6edc3SJonathan Lemon 		    (oinp->inp_vflag & INP_TIMEWAIT) &&
887a3b6edc3SJonathan Lemon 		    (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl &&
888a3b6edc3SJonathan Lemon 		    otw->cc_recv != 0) {
889efac726eSIan Dowse 			inp->inp_faddr = oinp->inp_faddr;
890efac726eSIan Dowse 			inp->inp_fport = oinp->inp_fport;
891a3b6edc3SJonathan Lemon 			(void) tcp_twclose(otw, 0);
892efac726eSIan Dowse 		} else
893a0292f23SGarrett Wollman 			return EADDRINUSE;
894a0292f23SGarrett Wollman 	}
8955200e00eSIan Dowse 	inp->inp_laddr = laddr;
89615bd2b43SDavid Greenman 	in_pcbrehash(inp);
897a0292f23SGarrett Wollman 
898a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
899a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
900a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
901a0292f23SGarrett Wollman 		tp->request_r_scale++;
902a0292f23SGarrett Wollman 
903a0292f23SGarrett Wollman 	soisconnecting(so);
904a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
905a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
9069b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
907b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
9081fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
909a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
910a45d2726SAndras Olah 
911a45d2726SAndras Olah 	/*
912a45d2726SAndras Olah 	 * Generate a CC value for this connection and
913a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
914a45d2726SAndras Olah 	 */
91597d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
91697d8d152SAndre Oppermann 		tcp_hc_gettao(&inp->inp_inc, &tao);
917a45d2726SAndras Olah 
918a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
91997d8d152SAndre Oppermann 	if (tao.tao_ccsent != 0 &&
92097d8d152SAndre Oppermann 	    CC_GEQ(tp->cc_send, tao.tao_ccsent)) {
92197d8d152SAndre Oppermann 		tao.tao_ccsent = tp->cc_send;
922a45d2726SAndras Olah 	} else {
92397d8d152SAndre Oppermann 		tao.tao_ccsent = 0;
924a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
925a45d2726SAndras Olah 	}
926a0292f23SGarrett Wollman 
92797d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
92897d8d152SAndre Oppermann 		tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT,
92997d8d152SAndre Oppermann 				 tao.tao_ccsent, 0);
93097d8d152SAndre Oppermann 
931a0292f23SGarrett Wollman 	return 0;
932a0292f23SGarrett Wollman }
933a0292f23SGarrett Wollman 
934fb59c426SYoshinobu Inoue #ifdef INET6
935fb59c426SYoshinobu Inoue static int
936b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
937fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
938fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
939b40ce416SJulian Elischer 	struct thread *td;
940fb59c426SYoshinobu Inoue {
941fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
942fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
943a3b6edc3SJonathan Lemon 	struct tcptw *otw;
944fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
945fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
94697d8d152SAndre Oppermann 	struct rmxp_tao tao;
947fb59c426SYoshinobu Inoue 	int error;
948fb59c426SYoshinobu Inoue 
94997d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
95097d8d152SAndre Oppermann 
951fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
952b40ce416SJulian Elischer 		error = in6_pcbbind(inp, (struct sockaddr *)0, td);
953fb59c426SYoshinobu Inoue 		if (error)
954fb59c426SYoshinobu Inoue 			return error;
955fb59c426SYoshinobu Inoue 	}
956fb59c426SYoshinobu Inoue 
957fb59c426SYoshinobu Inoue 	/*
958fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
959fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
960fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
961fb59c426SYoshinobu Inoue 	 */
962fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
963fb59c426SYoshinobu Inoue 	if (error)
964fb59c426SYoshinobu Inoue 		return error;
965fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
966fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
967fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
968fb59c426SYoshinobu Inoue 				  ? addr6
969fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
970fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
971fb59c426SYoshinobu Inoue 	if (oinp) {
972a3b6edc3SJonathan Lemon 		if (oinp != inp &&
973a3b6edc3SJonathan Lemon 		    (oinp->inp_vflag & INP_TIMEWAIT) &&
974a3b6edc3SJonathan Lemon 		    (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl &&
975a3b6edc3SJonathan Lemon 		    otw->cc_recv != 0) {
976a3b6edc3SJonathan Lemon 			inp->inp_faddr = oinp->inp_faddr;
977a3b6edc3SJonathan Lemon 			inp->inp_fport = oinp->inp_fport;
978a3b6edc3SJonathan Lemon 			(void) tcp_twclose(otw, 0);
979a3b6edc3SJonathan Lemon 		} else
980fb59c426SYoshinobu Inoue 			return EADDRINUSE;
981fb59c426SYoshinobu Inoue 	}
982fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
983fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
984fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
985fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
9864a6a94d8SArchie Cobbs 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
987fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
988fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
989fb59c426SYoshinobu Inoue 
990fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
991fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
992fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
993fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
994fb59c426SYoshinobu Inoue 
995fb59c426SYoshinobu Inoue 	soisconnecting(so);
996fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
997fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
998fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
999b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
10001fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
1001fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1002fb59c426SYoshinobu Inoue 
1003fb59c426SYoshinobu Inoue 	/*
1004fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
1005fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
1006fb59c426SYoshinobu Inoue 	 */
100797d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
100897d8d152SAndre Oppermann 		tcp_hc_gettao(&inp->inp_inc, &tao);
1009fb59c426SYoshinobu Inoue 
1010fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
101197d8d152SAndre Oppermann 	if (tao.tao_ccsent != 0 &&
101297d8d152SAndre Oppermann 	    CC_GEQ(tp->cc_send, tao.tao_ccsent)) {
101397d8d152SAndre Oppermann 		tao.tao_ccsent = tp->cc_send;
1014fb59c426SYoshinobu Inoue 	} else {
101597d8d152SAndre Oppermann 		tao.tao_ccsent = 0;
1016fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
1017fb59c426SYoshinobu Inoue 	}
101897d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
101997d8d152SAndre Oppermann 		tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT,
102097d8d152SAndre Oppermann 				 tao.tao_ccsent, 0);
1021fb59c426SYoshinobu Inoue 
1022fb59c426SYoshinobu Inoue 	return 0;
1023fb59c426SYoshinobu Inoue }
1024fb59c426SYoshinobu Inoue #endif /* INET6 */
1025fb59c426SYoshinobu Inoue 
1026cfe8b629SGarrett Wollman /*
1027cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1028cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1029cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1030cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1031cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1032cfe8b629SGarrett Wollman  */
1033df8bae1dSRodney W. Grimes int
1034cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
1035df8bae1dSRodney W. Grimes 	struct socket *so;
1036cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1037df8bae1dSRodney W. Grimes {
1038cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
1039df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1040cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1041df8bae1dSRodney W. Grimes 
1042cfe8b629SGarrett Wollman 	error = 0;
1043cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
1044f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
1045df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1046df8bae1dSRodney W. Grimes 	if (inp == NULL) {
1047f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
1048df8bae1dSRodney W. Grimes 		splx(s);
1049df8bae1dSRodney W. Grimes 		return (ECONNRESET);
1050df8bae1dSRodney W. Grimes 	}
1051f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1052f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
1053cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1054fb59c426SYoshinobu Inoue #ifdef INET6
1055fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1056fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1057fb59c426SYoshinobu Inoue 		else
1058fb59c426SYoshinobu Inoue #endif /* INET6 */
1059cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1060f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1061df8bae1dSRodney W. Grimes 		splx(s);
1062df8bae1dSRodney W. Grimes 		return (error);
1063df8bae1dSRodney W. Grimes 	}
1064df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1065df8bae1dSRodney W. Grimes 
1066cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1067cfe8b629SGarrett Wollman 	case SOPT_SET:
1068cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
10691cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
10701cfd4b53SBruce M Simpson 		case TCP_SIGNATURE_ENABLE:
10711cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
10721cfd4b53SBruce M Simpson 					    sizeof optval);
10731cfd4b53SBruce M Simpson 			if (error)
10741cfd4b53SBruce M Simpson 				break;
10751cfd4b53SBruce M Simpson 
10761cfd4b53SBruce M Simpson 			if (optval > 0)
10771cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
10781cfd4b53SBruce M Simpson 			else
10791cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
10801cfd4b53SBruce M Simpson 			break;
10811cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1082df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1083cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1084cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1085cfe8b629SGarrett Wollman 					    sizeof optval);
1086cfe8b629SGarrett Wollman 			if (error)
1087cfe8b629SGarrett Wollman 				break;
1088cfe8b629SGarrett Wollman 
1089cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1090cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1091cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1092cfe8b629SGarrett Wollman 				break;
1093cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1094cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1095cfe8b629SGarrett Wollman 				break;
1096cfe8b629SGarrett Wollman 			default:
1097cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1098cfe8b629SGarrett Wollman 				break;
1099cfe8b629SGarrett Wollman 			}
1100cfe8b629SGarrett Wollman 
1101cfe8b629SGarrett Wollman 			if (optval)
1102cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1103df8bae1dSRodney W. Grimes 			else
1104cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1105df8bae1dSRodney W. Grimes 			break;
1106df8bae1dSRodney W. Grimes 
1107007581c0SJonathan Lemon 		case TCP_NOPUSH:
1108007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1109007581c0SJonathan Lemon 					    sizeof optval);
1110007581c0SJonathan Lemon 			if (error)
1111007581c0SJonathan Lemon 				break;
1112007581c0SJonathan Lemon 
1113007581c0SJonathan Lemon 			if (optval)
1114007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1115007581c0SJonathan Lemon 			else {
1116007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1117007581c0SJonathan Lemon 				error = tcp_output(tp);
1118007581c0SJonathan Lemon 			}
1119007581c0SJonathan Lemon 			break;
1120007581c0SJonathan Lemon 
1121df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1122cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1123cfe8b629SGarrett Wollman 					    sizeof optval);
1124cfe8b629SGarrett Wollman 			if (error)
1125df8bae1dSRodney W. Grimes 				break;
1126df8bae1dSRodney W. Grimes 
112753369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
112853369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1129cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1130a0292f23SGarrett Wollman 			else
1131a0292f23SGarrett Wollman 				error = EINVAL;
1132a0292f23SGarrett Wollman 			break;
1133a0292f23SGarrett Wollman 
1134df8bae1dSRodney W. Grimes 		default:
1135df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1136df8bae1dSRodney W. Grimes 			break;
1137df8bae1dSRodney W. Grimes 		}
1138df8bae1dSRodney W. Grimes 		break;
1139df8bae1dSRodney W. Grimes 
1140cfe8b629SGarrett Wollman 	case SOPT_GET:
1141cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
11421cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
11431cfd4b53SBruce M Simpson 		case TCP_SIGNATURE_ENABLE:
11441cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
11451cfd4b53SBruce M Simpson 			break;
1146265ed012SBruce M Simpson #endif
1147df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1148cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1149df8bae1dSRodney W. Grimes 			break;
1150df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1151cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1152df8bae1dSRodney W. Grimes 			break;
1153a0292f23SGarrett Wollman 		case TCP_NOOPT:
1154cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1155a0292f23SGarrett Wollman 			break;
1156a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1157cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1158a0292f23SGarrett Wollman 			break;
1159df8bae1dSRodney W. Grimes 		default:
1160df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1161df8bae1dSRodney W. Grimes 			break;
1162df8bae1dSRodney W. Grimes 		}
1163cfe8b629SGarrett Wollman 		if (error == 0)
1164cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1165df8bae1dSRodney W. Grimes 		break;
1166df8bae1dSRodney W. Grimes 	}
1167f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1168df8bae1dSRodney W. Grimes 	splx(s);
1169df8bae1dSRodney W. Grimes 	return (error);
1170df8bae1dSRodney W. Grimes }
1171df8bae1dSRodney W. Grimes 
117226e30fbbSDavid Greenman /*
117326e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
117426e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
117526e30fbbSDavid Greenman  * be set by the route).
117626e30fbbSDavid Greenman  */
117781e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
11783d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
11793d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
118081e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
11813d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
11823d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1183df8bae1dSRodney W. Grimes 
1184df8bae1dSRodney W. Grimes /*
1185df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1186df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1187df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1188df8bae1dSRodney W. Grimes  */
11890312fbe9SPoul-Henning Kamp static int
1190b40ce416SJulian Elischer tcp_attach(so, td)
1191df8bae1dSRodney W. Grimes 	struct socket *so;
1192b40ce416SJulian Elischer 	struct thread *td;
1193df8bae1dSRodney W. Grimes {
1194df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1195df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1196df8bae1dSRodney W. Grimes 	int error;
1197fb59c426SYoshinobu Inoue #ifdef INET6
11984a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1199fb59c426SYoshinobu Inoue #endif
1200df8bae1dSRodney W. Grimes 
1201df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1202df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1203df8bae1dSRodney W. Grimes 		if (error)
1204df8bae1dSRodney W. Grimes 			return (error);
1205df8bae1dSRodney W. Grimes 	}
12065bd311a5SSam Leffler 	error = in_pcballoc(so, &tcbinfo, td, "tcpinp");
1207df8bae1dSRodney W. Grimes 	if (error)
1208df8bae1dSRodney W. Grimes 		return (error);
1209df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1210fb59c426SYoshinobu Inoue #ifdef INET6
1211fb59c426SYoshinobu Inoue 	if (isipv6) {
1212fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1213fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1214fb59c426SYoshinobu Inoue 	}
1215fb59c426SYoshinobu Inoue 	else
1216fb59c426SYoshinobu Inoue #endif
1217cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1218df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1219df8bae1dSRodney W. Grimes 	if (tp == 0) {
12204cc20ab1SSeigo Tanimura 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1223fb59c426SYoshinobu Inoue #ifdef INET6
1224fb59c426SYoshinobu Inoue 		if (isipv6)
1225fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1226fb59c426SYoshinobu Inoue 		else
1227fb59c426SYoshinobu Inoue #endif
1228df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1229df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1230df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1231df8bae1dSRodney W. Grimes 	}
1232df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1233df8bae1dSRodney W. Grimes 	return (0);
1234df8bae1dSRodney W. Grimes }
1235df8bae1dSRodney W. Grimes 
1236df8bae1dSRodney W. Grimes /*
1237df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1238df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1239df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1240df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1241df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1242df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1243df8bae1dSRodney W. Grimes  */
12440312fbe9SPoul-Henning Kamp static struct tcpcb *
1245df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1246df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1247df8bae1dSRodney W. Grimes {
1248df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1251df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
12524cc20ab1SSeigo Tanimura 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1253243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
12544cc20ab1SSeigo Tanimura 	else {
1255df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1256df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1257df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1258df8bae1dSRodney W. Grimes 		if (tp)
1259df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1260df8bae1dSRodney W. Grimes 	}
1261df8bae1dSRodney W. Grimes 	return (tp);
1262df8bae1dSRodney W. Grimes }
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes /*
1265df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1266df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1267df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1268df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1269df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1270df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1271df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1272df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1273df8bae1dSRodney W. Grimes  */
12740312fbe9SPoul-Henning Kamp static struct tcpcb *
1275df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1276df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1277df8bae1dSRodney W. Grimes {
1278df8bae1dSRodney W. Grimes 
1279df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1280df8bae1dSRodney W. Grimes 
1281df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1282df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1283df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1284df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1285df8bae1dSRodney W. Grimes 		break;
1286df8bae1dSRodney W. Grimes 
1287a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1288df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1289a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1290a0292f23SGarrett Wollman 		break;
1291a0292f23SGarrett Wollman 
1292df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1293df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1294df8bae1dSRodney W. Grimes 		break;
1295df8bae1dSRodney W. Grimes 
1296df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1297df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1298df8bae1dSRodney W. Grimes 		break;
1299df8bae1dSRodney W. Grimes 	}
1300b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1301df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1302b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1303b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
13049b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
13059b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1306b6239c4aSAndras Olah 	}
1307df8bae1dSRodney W. Grimes 	return (tp);
1308df8bae1dSRodney W. Grimes }
1309a0292f23SGarrett Wollman 
1310