xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 52710de1cbf22f18044f9a9180955a2313009df5)
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 
9356dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
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 
13756dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
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 
24152710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
24252710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
24352710de1SPawel Jakub Dawidek 		return (EINVAL);
2442c37256eSGarrett Wollman 	/*
2452c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2462c37256eSGarrett Wollman 	 * to them.
2472c37256eSGarrett Wollman 	 */
2482c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
24952710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
25052710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
25152710de1SPawel Jakub Dawidek 
25252710de1SPawel Jakub Dawidek 	COMMON_START();
253b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
2542c37256eSGarrett Wollman 	if (error)
2552c37256eSGarrett Wollman 		goto out;
2562c37256eSGarrett Wollman 	COMMON_END(PRU_BIND);
2572c37256eSGarrett Wollman }
2582c37256eSGarrett Wollman 
259fb59c426SYoshinobu Inoue #ifdef INET6
260fb59c426SYoshinobu Inoue static int
261b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
262fb59c426SYoshinobu Inoue {
263fb59c426SYoshinobu Inoue 	int s = splnet();
264fb59c426SYoshinobu Inoue 	int error = 0;
265f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
266fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
267fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
268edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
269fb59c426SYoshinobu Inoue 
27052710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
27152710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
27252710de1SPawel Jakub Dawidek 		return (EINVAL);
273fb59c426SYoshinobu Inoue 	/*
274fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
275fb59c426SYoshinobu Inoue 	 * to them.
276fb59c426SYoshinobu Inoue 	 */
277fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
27852710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
27952710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
28052710de1SPawel Jakub Dawidek 
28152710de1SPawel Jakub Dawidek 	COMMON_START();
282fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
283fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
28466ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
285fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
286fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
287fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
288fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
289fb59c426SYoshinobu Inoue 
290fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
291fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
292fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
293b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
294b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
295fb59c426SYoshinobu Inoue 			goto out;
296fb59c426SYoshinobu Inoue 		}
297fb59c426SYoshinobu Inoue 	}
298b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
299fb59c426SYoshinobu Inoue 	if (error)
300fb59c426SYoshinobu Inoue 		goto out;
301fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
302fb59c426SYoshinobu Inoue }
303fb59c426SYoshinobu Inoue #endif /* INET6 */
304fb59c426SYoshinobu Inoue 
3052c37256eSGarrett Wollman /*
3062c37256eSGarrett Wollman  * Prepare to accept connections.
3072c37256eSGarrett Wollman  */
3082c37256eSGarrett Wollman static int
309b40ce416SJulian Elischer tcp_usr_listen(struct socket *so, struct thread *td)
3102c37256eSGarrett Wollman {
3112c37256eSGarrett Wollman 	int s = splnet();
3122c37256eSGarrett Wollman 	int error = 0;
313f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3142c37256eSGarrett Wollman 	struct tcpcb *tp;
315edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
3162c37256eSGarrett Wollman 
3172c37256eSGarrett Wollman 	COMMON_START();
3182c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
319b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3202c37256eSGarrett Wollman 	if (error == 0)
3212c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
3222c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
3232c37256eSGarrett Wollman }
3242c37256eSGarrett Wollman 
325fb59c426SYoshinobu Inoue #ifdef INET6
326fb59c426SYoshinobu Inoue static int
327b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td)
328fb59c426SYoshinobu Inoue {
329fb59c426SYoshinobu Inoue 	int s = splnet();
330fb59c426SYoshinobu Inoue 	int error = 0;
331f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
332fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
333edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
334fb59c426SYoshinobu Inoue 
335fb59c426SYoshinobu Inoue 	COMMON_START();
336fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
337fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
33866ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
339fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
340b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
341fb59c426SYoshinobu Inoue 	}
342fb59c426SYoshinobu Inoue 	if (error == 0)
343fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
344fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
345fb59c426SYoshinobu Inoue }
346fb59c426SYoshinobu Inoue #endif /* INET6 */
347fb59c426SYoshinobu Inoue 
3482c37256eSGarrett Wollman /*
3492c37256eSGarrett Wollman  * Initiate connection to peer.
3502c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3512c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3522c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3532c37256eSGarrett Wollman  * Send initial segment on connection.
3542c37256eSGarrett Wollman  */
3552c37256eSGarrett Wollman static int
356b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
3572c37256eSGarrett Wollman {
3582c37256eSGarrett Wollman 	int s = splnet();
3592c37256eSGarrett Wollman 	int error = 0;
360f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3612c37256eSGarrett Wollman 	struct tcpcb *tp;
3622c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
363f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
3642c37256eSGarrett Wollman 
36557bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
366e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
367e29ef13fSDon Lewis 		return (EINVAL);
36852710de1SPawel Jakub Dawidek 	/*
36952710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
37052710de1SPawel Jakub Dawidek 	 */
3712c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
37252710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
37352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
374a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
375a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
37675c13541SPoul-Henning Kamp 
37752710de1SPawel Jakub Dawidek 	COMMON_START();
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 	sin6p = (struct sockaddr_in6 *)nam;
396e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
397e29ef13fSDon Lewis 		return (EINVAL);
39852710de1SPawel Jakub Dawidek 	/*
39952710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
40052710de1SPawel Jakub Dawidek 	 */
401fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
40252710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
40352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
404fb59c426SYoshinobu Inoue 
40552710de1SPawel Jakub Dawidek 	COMMON_START();
40633841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
407fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
408fb59c426SYoshinobu Inoue 
409d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
410d46a5312SMaxim Konovalov 			error = EINVAL;
411d46a5312SMaxim Konovalov 			goto out;
412d46a5312SMaxim Konovalov 		}
41333841545SHajimu UMEMOTO 
414fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
415fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
416fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
417b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
418fb59c426SYoshinobu Inoue 			goto out;
419fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
420fb59c426SYoshinobu Inoue 		goto out;
421fb59c426SYoshinobu Inoue 	}
422fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
423fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
424b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
425b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
426fb59c426SYoshinobu Inoue 		goto out;
427fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
428fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
429fb59c426SYoshinobu Inoue }
430fb59c426SYoshinobu Inoue #endif /* INET6 */
431fb59c426SYoshinobu Inoue 
4322c37256eSGarrett Wollman /*
4332c37256eSGarrett Wollman  * Initiate disconnect from peer.
4342c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
4352c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
4362c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
4372c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
4382c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
4392c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
4402c37256eSGarrett Wollman  *
4412c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
4422c37256eSGarrett Wollman  */
4432c37256eSGarrett Wollman static int
4442c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
4452c37256eSGarrett Wollman {
4462c37256eSGarrett Wollman 	int s = splnet();
4472c37256eSGarrett Wollman 	int error = 0;
448f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
4492c37256eSGarrett Wollman 	struct tcpcb *tp;
450f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
4512c37256eSGarrett Wollman 
4522c37256eSGarrett Wollman 	COMMON_START();
4532c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4542c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4552c37256eSGarrett Wollman }
4562c37256eSGarrett Wollman 
4572c37256eSGarrett Wollman /*
4582c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4592c37256eSGarrett Wollman  * done at higher levels; just return the address
4602c37256eSGarrett Wollman  * of the peer, storing through addr.
4612c37256eSGarrett Wollman  */
4622c37256eSGarrett Wollman static int
46357bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4642c37256eSGarrett Wollman {
465f76fcf6dSJeffrey Hsu 	int s;
4662c37256eSGarrett Wollman 	int error = 0;
467f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
4681db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
46926ef6ac4SDon Lewis 	struct in_addr addr;
47026ef6ac4SDon Lewis 	in_port_t port = 0;
4711db24ffbSJonathan Lemon 	TCPDEBUG0;
4722c37256eSGarrett Wollman 
473c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
474c0647e0dSJonathan Lemon 		error = ECONNABORTED;
475c0647e0dSJonathan Lemon 		goto out;
476c0647e0dSJonathan Lemon 	}
477f76fcf6dSJeffrey Hsu 
478f76fcf6dSJeffrey Hsu 	s = splnet();
479f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
480f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
481f76fcf6dSJeffrey Hsu 	if (!inp) {
482f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
4831db24ffbSJonathan Lemon 		splx(s);
4841db24ffbSJonathan Lemon 		return (EINVAL);
4851db24ffbSJonathan Lemon 	}
486f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
487f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
4881db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4891db24ffbSJonathan Lemon 	TCPDEBUG1();
490f76fcf6dSJeffrey Hsu 
491f76fcf6dSJeffrey Hsu 	/*
49226ef6ac4SDon Lewis 	 * We inline in_setpeeraddr and COMMON_END here, so that we can
49326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
49426ef6ac4SDon Lewis 	 * release the lock.
495f76fcf6dSJeffrey Hsu 	 */
49626ef6ac4SDon Lewis 	port = inp->inp_fport;
49726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
498f76fcf6dSJeffrey Hsu 
49926ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
50026ef6ac4SDon Lewis 	if (tp)
50126ef6ac4SDon Lewis 		INP_UNLOCK(inp);
50226ef6ac4SDon Lewis 	splx(s);
50326ef6ac4SDon Lewis 	if (error == 0)
50426ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
50526ef6ac4SDon Lewis 	return error;
5062c37256eSGarrett Wollman }
5072c37256eSGarrett Wollman 
508fb59c426SYoshinobu Inoue #ifdef INET6
509fb59c426SYoshinobu Inoue static int
510fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
511fb59c426SYoshinobu Inoue {
512f76fcf6dSJeffrey Hsu 	int s;
513f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
514fb59c426SYoshinobu Inoue 	int error = 0;
5151db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
51626ef6ac4SDon Lewis 	struct in_addr addr;
51726ef6ac4SDon Lewis 	struct in6_addr addr6;
51826ef6ac4SDon Lewis 	in_port_t port = 0;
51926ef6ac4SDon Lewis 	int v4 = 0;
5201db24ffbSJonathan Lemon 	TCPDEBUG0;
521fb59c426SYoshinobu Inoue 
522c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
523c0647e0dSJonathan Lemon 		error = ECONNABORTED;
524c0647e0dSJonathan Lemon 		goto out;
525c0647e0dSJonathan Lemon 	}
526f76fcf6dSJeffrey Hsu 
527f76fcf6dSJeffrey Hsu 	s = splnet();
528f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
529f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
5301db24ffbSJonathan Lemon 	if (inp == 0) {
531f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
5321db24ffbSJonathan Lemon 		splx(s);
5331db24ffbSJonathan Lemon 		return (EINVAL);
5341db24ffbSJonathan Lemon 	}
535f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
536f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
5371db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
5381db24ffbSJonathan Lemon 	TCPDEBUG1();
53926ef6ac4SDon Lewis 	/*
54026ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
54126ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
54226ef6ac4SDon Lewis 	 * release the lock.
54326ef6ac4SDon Lewis 	 */
54426ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
54526ef6ac4SDon Lewis 		v4 = 1;
54626ef6ac4SDon Lewis 		port = inp->inp_fport;
54726ef6ac4SDon Lewis 		addr = inp->inp_faddr;
54826ef6ac4SDon Lewis 	} else {
54926ef6ac4SDon Lewis 		port = inp->inp_fport;
55026ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
55126ef6ac4SDon Lewis 	}
55226ef6ac4SDon Lewis 
55326ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
55426ef6ac4SDon Lewis 	if (tp)
55526ef6ac4SDon Lewis 		INP_UNLOCK(inp);
55626ef6ac4SDon Lewis 	splx(s);
55726ef6ac4SDon Lewis 	if (error == 0) {
55826ef6ac4SDon Lewis 		if (v4)
55926ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
56026ef6ac4SDon Lewis 		else
56126ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
56226ef6ac4SDon Lewis 	}
56326ef6ac4SDon Lewis 	return error;
564fb59c426SYoshinobu Inoue }
565fb59c426SYoshinobu Inoue #endif /* INET6 */
566f76fcf6dSJeffrey Hsu 
567f76fcf6dSJeffrey Hsu /*
568f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
569f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
570f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
571f76fcf6dSJeffrey Hsu  */
572f76fcf6dSJeffrey Hsu static int
573f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
574f76fcf6dSJeffrey Hsu {
575f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
576f76fcf6dSJeffrey Hsu }
577f76fcf6dSJeffrey Hsu 
578f76fcf6dSJeffrey Hsu /*
579f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
580f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
581f76fcf6dSJeffrey Hsu  */
582f76fcf6dSJeffrey Hsu static int
583f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
584f76fcf6dSJeffrey Hsu {
585f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
586f76fcf6dSJeffrey Hsu }
587f76fcf6dSJeffrey Hsu 
5882c37256eSGarrett Wollman /*
5892c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
5902c37256eSGarrett Wollman  */
5912c37256eSGarrett Wollman static int
5922c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
5932c37256eSGarrett Wollman {
5942c37256eSGarrett Wollman 	int s = splnet();
5952c37256eSGarrett Wollman 	int error = 0;
596f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
5972c37256eSGarrett Wollman 	struct tcpcb *tp;
598f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
5992c37256eSGarrett Wollman 
6002c37256eSGarrett Wollman 	COMMON_START();
6012c37256eSGarrett Wollman 	socantsendmore(so);
6022c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
6032c37256eSGarrett Wollman 	if (tp)
6042c37256eSGarrett Wollman 		error = tcp_output(tp);
6052c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
6062c37256eSGarrett Wollman }
6072c37256eSGarrett Wollman 
6082c37256eSGarrett Wollman /*
6092c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
6102c37256eSGarrett Wollman  */
6112c37256eSGarrett Wollman static int
6122c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
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_READ;
6192c37256eSGarrett Wollman 
6202c37256eSGarrett Wollman 	COMMON_START();
6212c37256eSGarrett Wollman 	tcp_output(tp);
6222c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
6232c37256eSGarrett Wollman }
6242c37256eSGarrett Wollman 
6252c37256eSGarrett Wollman /*
6262c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
6279c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
6289c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
6299c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
6309c9906e9SPeter Wemm  * generally are caller-frees.
6312c37256eSGarrett Wollman  */
6322c37256eSGarrett Wollman static int
63357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
634b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
6352c37256eSGarrett Wollman {
6362c37256eSGarrett Wollman 	int s = splnet();
6372c37256eSGarrett Wollman 	int error = 0;
638f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6392c37256eSGarrett Wollman 	struct tcpcb *tp;
640f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
641fb59c426SYoshinobu Inoue #ifdef INET6
642fb59c426SYoshinobu Inoue 	int isipv6;
643fb59c426SYoshinobu Inoue #endif
6449c9906e9SPeter Wemm 	TCPDEBUG0;
6452c37256eSGarrett Wollman 
646f76fcf6dSJeffrey Hsu 	/*
647f76fcf6dSJeffrey Hsu 	 * Need write lock here because this function might call
648f76fcf6dSJeffrey Hsu 	 * tcp_connect or tcp_usrclosed.
649f76fcf6dSJeffrey Hsu 	 * We really want to have to this function upgrade from read lock
650f76fcf6dSJeffrey Hsu 	 * to write lock.  XXX
651f76fcf6dSJeffrey Hsu 	 */
652f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
653f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
6549c9906e9SPeter Wemm 	if (inp == NULL) {
6559c9906e9SPeter Wemm 		/*
6569c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
6579c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
6589c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
6599c9906e9SPeter Wemm 		 */
6609c9906e9SPeter Wemm 		if (m)
6619c9906e9SPeter Wemm 			m_freem(m);
6629c9906e9SPeter Wemm 		if (control)
6639c9906e9SPeter Wemm 			m_freem(control);
6649c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
66545d3a132SPeter Wemm 		tp = NULL;
66645d3a132SPeter Wemm 		TCPDEBUG1();
6679c9906e9SPeter Wemm 		goto out;
6689c9906e9SPeter Wemm 	}
669f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
670fb59c426SYoshinobu Inoue #ifdef INET6
671fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
672fb59c426SYoshinobu Inoue #endif /* INET6 */
6739c9906e9SPeter Wemm 	tp = intotcpcb(inp);
6749c9906e9SPeter Wemm 	TCPDEBUG1();
6759c9906e9SPeter Wemm 	if (control) {
6769c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
6779c9906e9SPeter Wemm 		if (control->m_len) {
6789c9906e9SPeter Wemm 			m_freem(control);
6792c37256eSGarrett Wollman 			if (m)
6802c37256eSGarrett Wollman 				m_freem(m);
681744f87eaSDavid Greenman 			error = EINVAL;
682744f87eaSDavid Greenman 			goto out;
6832c37256eSGarrett Wollman 		}
6849c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
6859c9906e9SPeter Wemm 	}
6862c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
687395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
6882c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
6892c37256eSGarrett Wollman 			/*
6902c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
6912c37256eSGarrett Wollman 			 * initialize window to default value, and
6922c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
6932c37256eSGarrett Wollman 			 * MSS.
6942c37256eSGarrett Wollman 			 */
695fb59c426SYoshinobu Inoue #ifdef INET6
696fb59c426SYoshinobu Inoue 			if (isipv6)
697b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
698fb59c426SYoshinobu Inoue 			else
699fb59c426SYoshinobu Inoue #endif /* INET6 */
700b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
7012c37256eSGarrett Wollman 			if (error)
7022c37256eSGarrett Wollman 				goto out;
7032c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
7042c37256eSGarrett Wollman 			tcp_mss(tp, -1);
7052c37256eSGarrett Wollman 		}
7062c37256eSGarrett Wollman 
7072c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
7082c37256eSGarrett Wollman 			/*
7092c37256eSGarrett Wollman 			 * Close the send side of the connection after
7102c37256eSGarrett Wollman 			 * the data is sent.
7112c37256eSGarrett Wollman 			 */
7122c37256eSGarrett Wollman 			socantsendmore(so);
7132c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
7142c37256eSGarrett Wollman 		}
715b0acefa8SBill Fenner 		if (tp != NULL) {
716b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
717b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
7182c37256eSGarrett Wollman 			error = tcp_output(tp);
719b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
720b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
721b0acefa8SBill Fenner 		}
7222c37256eSGarrett Wollman 	} else {
7232c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
7242c37256eSGarrett Wollman 			m_freem(m);
7252c37256eSGarrett Wollman 			error = ENOBUFS;
7262c37256eSGarrett Wollman 			goto out;
7272c37256eSGarrett Wollman 		}
7282c37256eSGarrett Wollman 		/*
7292c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
7302c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
7312c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
7322c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
7332c37256eSGarrett Wollman 		 * of data past the urgent section.
7342c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
7352c37256eSGarrett Wollman 		 */
736395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
737ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
738ef53690bSGarrett Wollman 			/*
739ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
740ef53690bSGarrett Wollman 			 * initialize window to default value, and
741ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
742ef53690bSGarrett Wollman 			 * MSS.
743ef53690bSGarrett Wollman 			 */
744fb59c426SYoshinobu Inoue #ifdef INET6
745fb59c426SYoshinobu Inoue 			if (isipv6)
746b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
747fb59c426SYoshinobu Inoue 			else
748fb59c426SYoshinobu Inoue #endif /* INET6 */
749b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
750ef53690bSGarrett Wollman 			if (error)
751ef53690bSGarrett Wollman 				goto out;
752ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
753ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
754ef53690bSGarrett Wollman 		}
7552c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
7562c37256eSGarrett Wollman 		tp->t_force = 1;
7572c37256eSGarrett Wollman 		error = tcp_output(tp);
7582c37256eSGarrett Wollman 		tp->t_force = 0;
7592c37256eSGarrett Wollman 	}
7602c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
7612c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
7622c37256eSGarrett Wollman }
7632c37256eSGarrett Wollman 
7642c37256eSGarrett Wollman /*
7652c37256eSGarrett Wollman  * Abort the TCP.
7662c37256eSGarrett Wollman  */
7672c37256eSGarrett Wollman static int
7682c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
7692c37256eSGarrett Wollman {
7702c37256eSGarrett Wollman 	int s = splnet();
7712c37256eSGarrett Wollman 	int error = 0;
772f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7732c37256eSGarrett Wollman 	struct tcpcb *tp;
774f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
7752c37256eSGarrett Wollman 
7762c37256eSGarrett Wollman 	COMMON_START();
7772c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
7782c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
7792c37256eSGarrett Wollman }
7802c37256eSGarrett Wollman 
7812c37256eSGarrett Wollman /*
7822c37256eSGarrett Wollman  * Receive out-of-band data.
7832c37256eSGarrett Wollman  */
7842c37256eSGarrett Wollman static int
7852c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
7862c37256eSGarrett Wollman {
7872c37256eSGarrett Wollman 	int s = splnet();
7882c37256eSGarrett Wollman 	int error = 0;
789f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7902c37256eSGarrett Wollman 	struct tcpcb *tp;
791f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
7922c37256eSGarrett Wollman 
7932c37256eSGarrett Wollman 	COMMON_START();
7942c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
7952c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
7964cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
7974cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
7982c37256eSGarrett Wollman 		error = EINVAL;
7992c37256eSGarrett Wollman 		goto out;
8002c37256eSGarrett Wollman 	}
8012c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
8022c37256eSGarrett Wollman 		error = EWOULDBLOCK;
8032c37256eSGarrett Wollman 		goto out;
8042c37256eSGarrett Wollman 	}
8052c37256eSGarrett Wollman 	m->m_len = 1;
8062c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
8072c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
8082c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
8092c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
8102c37256eSGarrett Wollman }
8112c37256eSGarrett Wollman 
8122c37256eSGarrett Wollman /* xxx - should be const */
8132c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
8142c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
815117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
816f76fcf6dSJeffrey Hsu 	tcp_usr_disconnect, tcp_usr_listen, tcp_peeraddr, tcp_usr_rcvd,
817117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
818a557af22SRobert Watson 	tcp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
8192c37256eSGarrett Wollman };
820df8bae1dSRodney W. Grimes 
821fb59c426SYoshinobu Inoue #ifdef INET6
822fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
823fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
824fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
825fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
826fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
827a557af22SRobert Watson 	in6_mapped_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
828fb59c426SYoshinobu Inoue };
829fb59c426SYoshinobu Inoue #endif /* INET6 */
830fb59c426SYoshinobu Inoue 
831a0292f23SGarrett Wollman /*
832a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
833a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
8345200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
8355200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
8365200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
8375200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
838a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
839a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
840a0292f23SGarrett Wollman  */
8410312fbe9SPoul-Henning Kamp static int
842b40ce416SJulian Elischer tcp_connect(tp, nam, td)
843a0292f23SGarrett Wollman 	register struct tcpcb *tp;
84457bf258eSGarrett Wollman 	struct sockaddr *nam;
845b40ce416SJulian Elischer 	struct thread *td;
846a0292f23SGarrett Wollman {
847a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
848a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
849a3b6edc3SJonathan Lemon 	struct tcptw *otw;
85097d8d152SAndre Oppermann 	struct rmxp_tao tao;
8515200e00eSIan Dowse 	struct in_addr laddr;
8525200e00eSIan Dowse 	u_short lport;
853c3229e05SDavid Greenman 	int error;
854a0292f23SGarrett Wollman 
85597d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
85697d8d152SAndre Oppermann 
857a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
858b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
859a0292f23SGarrett Wollman 		if (error)
860a0292f23SGarrett Wollman 			return error;
861a0292f23SGarrett Wollman 	}
862a0292f23SGarrett Wollman 
863a0292f23SGarrett Wollman 	/*
864a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
865a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
866a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
867a0292f23SGarrett Wollman 	 */
8685200e00eSIan Dowse 	laddr = inp->inp_laddr;
8695200e00eSIan Dowse 	lport = inp->inp_lport;
8705200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
871b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
8725200e00eSIan Dowse 	if (error && oinp == NULL)
873d3628763SRodney W. Grimes 		return error;
874a0292f23SGarrett Wollman 	if (oinp) {
875a3b6edc3SJonathan Lemon 		if (oinp != inp &&
876a3b6edc3SJonathan Lemon 		    (oinp->inp_vflag & INP_TIMEWAIT) &&
877a3b6edc3SJonathan Lemon 		    (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl &&
878a3b6edc3SJonathan Lemon 		    otw->cc_recv != 0) {
879efac726eSIan Dowse 			inp->inp_faddr = oinp->inp_faddr;
880efac726eSIan Dowse 			inp->inp_fport = oinp->inp_fport;
881a3b6edc3SJonathan Lemon 			(void) tcp_twclose(otw, 0);
882efac726eSIan Dowse 		} else
883a0292f23SGarrett Wollman 			return EADDRINUSE;
884a0292f23SGarrett Wollman 	}
8855200e00eSIan Dowse 	inp->inp_laddr = laddr;
88615bd2b43SDavid Greenman 	in_pcbrehash(inp);
887a0292f23SGarrett Wollman 
888a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
889a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
890a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
891a0292f23SGarrett Wollman 		tp->request_r_scale++;
892a0292f23SGarrett Wollman 
893a0292f23SGarrett Wollman 	soisconnecting(so);
894a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
895a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
8969b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
897b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
8981fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
899a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
900a45d2726SAndras Olah 
901a45d2726SAndras Olah 	/*
902a45d2726SAndras Olah 	 * Generate a CC value for this connection and
903a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
904a45d2726SAndras Olah 	 */
90597d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
90697d8d152SAndre Oppermann 		tcp_hc_gettao(&inp->inp_inc, &tao);
907a45d2726SAndras Olah 
908a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
90997d8d152SAndre Oppermann 	if (tao.tao_ccsent != 0 &&
91097d8d152SAndre Oppermann 	    CC_GEQ(tp->cc_send, tao.tao_ccsent)) {
91197d8d152SAndre Oppermann 		tao.tao_ccsent = tp->cc_send;
912a45d2726SAndras Olah 	} else {
91397d8d152SAndre Oppermann 		tao.tao_ccsent = 0;
914a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
915a45d2726SAndras Olah 	}
916a0292f23SGarrett Wollman 
91797d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
91897d8d152SAndre Oppermann 		tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT,
91997d8d152SAndre Oppermann 				 tao.tao_ccsent, 0);
92097d8d152SAndre Oppermann 
921a0292f23SGarrett Wollman 	return 0;
922a0292f23SGarrett Wollman }
923a0292f23SGarrett Wollman 
924fb59c426SYoshinobu Inoue #ifdef INET6
925fb59c426SYoshinobu Inoue static int
926b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
927fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
928fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
929b40ce416SJulian Elischer 	struct thread *td;
930fb59c426SYoshinobu Inoue {
931fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
932fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
933a3b6edc3SJonathan Lemon 	struct tcptw *otw;
934fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
935fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
93697d8d152SAndre Oppermann 	struct rmxp_tao tao;
937fb59c426SYoshinobu Inoue 	int error;
938fb59c426SYoshinobu Inoue 
93997d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
94097d8d152SAndre Oppermann 
941fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
942b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
943fb59c426SYoshinobu Inoue 		if (error)
944fb59c426SYoshinobu Inoue 			return error;
945fb59c426SYoshinobu Inoue 	}
946fb59c426SYoshinobu Inoue 
947fb59c426SYoshinobu Inoue 	/*
948fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
949fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
950fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
951fb59c426SYoshinobu Inoue 	 */
952fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
953fb59c426SYoshinobu Inoue 	if (error)
954fb59c426SYoshinobu Inoue 		return error;
955fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
956fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
957fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
958fb59c426SYoshinobu Inoue 				  ? addr6
959fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
960fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
961fb59c426SYoshinobu Inoue 	if (oinp) {
962a3b6edc3SJonathan Lemon 		if (oinp != inp &&
963a3b6edc3SJonathan Lemon 		    (oinp->inp_vflag & INP_TIMEWAIT) &&
964a3b6edc3SJonathan Lemon 		    (ticks - (otw = intotw(oinp))->t_starttime) < tcp_msl &&
965a3b6edc3SJonathan Lemon 		    otw->cc_recv != 0) {
966a3b6edc3SJonathan Lemon 			inp->inp_faddr = oinp->inp_faddr;
967a3b6edc3SJonathan Lemon 			inp->inp_fport = oinp->inp_fport;
968a3b6edc3SJonathan Lemon 			(void) tcp_twclose(otw, 0);
969a3b6edc3SJonathan Lemon 		} else
970fb59c426SYoshinobu Inoue 			return EADDRINUSE;
971fb59c426SYoshinobu Inoue 	}
972fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
973fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
974fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
975fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
9764a6a94d8SArchie Cobbs 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
977fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
978fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
979fb59c426SYoshinobu Inoue 
980fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
981fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
982fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
983fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
984fb59c426SYoshinobu Inoue 
985fb59c426SYoshinobu Inoue 	soisconnecting(so);
986fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
987fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
988fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
989b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
9901fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
991fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
992fb59c426SYoshinobu Inoue 
993fb59c426SYoshinobu Inoue 	/*
994fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
995fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
996fb59c426SYoshinobu Inoue 	 */
99797d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
99897d8d152SAndre Oppermann 		tcp_hc_gettao(&inp->inp_inc, &tao);
999fb59c426SYoshinobu Inoue 
1000fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
100197d8d152SAndre Oppermann 	if (tao.tao_ccsent != 0 &&
100297d8d152SAndre Oppermann 	    CC_GEQ(tp->cc_send, tao.tao_ccsent)) {
100397d8d152SAndre Oppermann 		tao.tao_ccsent = tp->cc_send;
1004fb59c426SYoshinobu Inoue 	} else {
100597d8d152SAndre Oppermann 		tao.tao_ccsent = 0;
1006fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
1007fb59c426SYoshinobu Inoue 	}
100897d8d152SAndre Oppermann 	if (tcp_do_rfc1644)
100997d8d152SAndre Oppermann 		tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT,
101097d8d152SAndre Oppermann 				 tao.tao_ccsent, 0);
1011fb59c426SYoshinobu Inoue 
1012fb59c426SYoshinobu Inoue 	return 0;
1013fb59c426SYoshinobu Inoue }
1014fb59c426SYoshinobu Inoue #endif /* INET6 */
1015fb59c426SYoshinobu Inoue 
1016cfe8b629SGarrett Wollman /*
1017cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1018cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1019cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1020cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1021cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1022cfe8b629SGarrett Wollman  */
1023df8bae1dSRodney W. Grimes int
1024cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
1025df8bae1dSRodney W. Grimes 	struct socket *so;
1026cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1027df8bae1dSRodney W. Grimes {
1028cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
1029df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1030cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1031df8bae1dSRodney W. Grimes 
1032cfe8b629SGarrett Wollman 	error = 0;
1033cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
1034f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
1035df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1036df8bae1dSRodney W. Grimes 	if (inp == NULL) {
1037f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
1038df8bae1dSRodney W. Grimes 		splx(s);
1039df8bae1dSRodney W. Grimes 		return (ECONNRESET);
1040df8bae1dSRodney W. Grimes 	}
1041f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1042f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
1043cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1044fb59c426SYoshinobu Inoue #ifdef INET6
1045fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1046fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1047fb59c426SYoshinobu Inoue 		else
1048fb59c426SYoshinobu Inoue #endif /* INET6 */
1049cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1050f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
1051df8bae1dSRodney W. Grimes 		splx(s);
1052df8bae1dSRodney W. Grimes 		return (error);
1053df8bae1dSRodney W. Grimes 	}
1054df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1055df8bae1dSRodney W. Grimes 
1056cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1057cfe8b629SGarrett Wollman 	case SOPT_SET:
1058cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
10591cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
106088f6b043SBruce M Simpson 		case TCP_MD5SIG:
10611cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
10621cfd4b53SBruce M Simpson 					    sizeof optval);
10631cfd4b53SBruce M Simpson 			if (error)
10641cfd4b53SBruce M Simpson 				break;
10651cfd4b53SBruce M Simpson 
10661cfd4b53SBruce M Simpson 			if (optval > 0)
10671cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
10681cfd4b53SBruce M Simpson 			else
10691cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
10701cfd4b53SBruce M Simpson 			break;
10711cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1072df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1073cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1074cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1075cfe8b629SGarrett Wollman 					    sizeof optval);
1076cfe8b629SGarrett Wollman 			if (error)
1077cfe8b629SGarrett Wollman 				break;
1078cfe8b629SGarrett Wollman 
1079cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1080cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1081cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1082cfe8b629SGarrett Wollman 				break;
1083cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1084cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1085cfe8b629SGarrett Wollman 				break;
1086cfe8b629SGarrett Wollman 			default:
1087cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1088cfe8b629SGarrett Wollman 				break;
1089cfe8b629SGarrett Wollman 			}
1090cfe8b629SGarrett Wollman 
1091cfe8b629SGarrett Wollman 			if (optval)
1092cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1093df8bae1dSRodney W. Grimes 			else
1094cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1095df8bae1dSRodney W. Grimes 			break;
1096df8bae1dSRodney W. Grimes 
1097007581c0SJonathan Lemon 		case TCP_NOPUSH:
1098007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1099007581c0SJonathan Lemon 					    sizeof optval);
1100007581c0SJonathan Lemon 			if (error)
1101007581c0SJonathan Lemon 				break;
1102007581c0SJonathan Lemon 
1103007581c0SJonathan Lemon 			if (optval)
1104007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1105007581c0SJonathan Lemon 			else {
1106007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1107007581c0SJonathan Lemon 				error = tcp_output(tp);
1108007581c0SJonathan Lemon 			}
1109007581c0SJonathan Lemon 			break;
1110007581c0SJonathan Lemon 
1111df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1112cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1113cfe8b629SGarrett Wollman 					    sizeof optval);
1114cfe8b629SGarrett Wollman 			if (error)
1115df8bae1dSRodney W. Grimes 				break;
1116df8bae1dSRodney W. Grimes 
111753369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
111853369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1119cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1120a0292f23SGarrett Wollman 			else
1121a0292f23SGarrett Wollman 				error = EINVAL;
1122a0292f23SGarrett Wollman 			break;
1123a0292f23SGarrett Wollman 
1124df8bae1dSRodney W. Grimes 		default:
1125df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1126df8bae1dSRodney W. Grimes 			break;
1127df8bae1dSRodney W. Grimes 		}
1128df8bae1dSRodney W. Grimes 		break;
1129df8bae1dSRodney W. Grimes 
1130cfe8b629SGarrett Wollman 	case SOPT_GET:
1131cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
11321cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
113388f6b043SBruce M Simpson 		case TCP_MD5SIG:
11341cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
11351cfd4b53SBruce M Simpson 			break;
1136265ed012SBruce M Simpson #endif
1137df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1138cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1139df8bae1dSRodney W. Grimes 			break;
1140df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1141cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1142df8bae1dSRodney W. Grimes 			break;
1143a0292f23SGarrett Wollman 		case TCP_NOOPT:
1144cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1145a0292f23SGarrett Wollman 			break;
1146a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1147cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1148a0292f23SGarrett Wollman 			break;
1149df8bae1dSRodney W. Grimes 		default:
1150df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1151df8bae1dSRodney W. Grimes 			break;
1152df8bae1dSRodney W. Grimes 		}
1153cfe8b629SGarrett Wollman 		if (error == 0)
1154cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1155df8bae1dSRodney W. Grimes 		break;
1156df8bae1dSRodney W. Grimes 	}
1157f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1158df8bae1dSRodney W. Grimes 	splx(s);
1159df8bae1dSRodney W. Grimes 	return (error);
1160df8bae1dSRodney W. Grimes }
1161df8bae1dSRodney W. Grimes 
116226e30fbbSDavid Greenman /*
116326e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
116426e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
116526e30fbbSDavid Greenman  * be set by the route).
116626e30fbbSDavid Greenman  */
116781e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
11683d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
11693d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
117081e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
11713d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
11723d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1173df8bae1dSRodney W. Grimes 
1174df8bae1dSRodney W. Grimes /*
1175df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1176df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1177df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1178df8bae1dSRodney W. Grimes  */
11790312fbe9SPoul-Henning Kamp static int
118056dc72c3SPawel Jakub Dawidek tcp_attach(so)
1181df8bae1dSRodney W. Grimes 	struct socket *so;
1182df8bae1dSRodney W. Grimes {
1183df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1184df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1185df8bae1dSRodney W. Grimes 	int error;
1186fb59c426SYoshinobu Inoue #ifdef INET6
11874a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1188fb59c426SYoshinobu Inoue #endif
1189df8bae1dSRodney W. Grimes 
1190df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1191df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1192df8bae1dSRodney W. Grimes 		if (error)
1193df8bae1dSRodney W. Grimes 			return (error);
1194df8bae1dSRodney W. Grimes 	}
11956823b823SPawel Jakub Dawidek 	error = in_pcballoc(so, &tcbinfo, "tcpinp");
1196df8bae1dSRodney W. Grimes 	if (error)
1197df8bae1dSRodney W. Grimes 		return (error);
1198df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1199fb59c426SYoshinobu Inoue #ifdef INET6
1200fb59c426SYoshinobu Inoue 	if (isipv6) {
1201fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1202fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1203fb59c426SYoshinobu Inoue 	}
1204fb59c426SYoshinobu Inoue 	else
1205fb59c426SYoshinobu Inoue #endif
1206cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1207df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1208df8bae1dSRodney W. Grimes 	if (tp == 0) {
12094cc20ab1SSeigo Tanimura 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1212fb59c426SYoshinobu Inoue #ifdef INET6
1213fb59c426SYoshinobu Inoue 		if (isipv6)
1214fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1215fb59c426SYoshinobu Inoue 		else
1216fb59c426SYoshinobu Inoue #endif
1217df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1218df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1219df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1220df8bae1dSRodney W. Grimes 	}
1221df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1222df8bae1dSRodney W. Grimes 	return (0);
1223df8bae1dSRodney W. Grimes }
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes /*
1226df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1227df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1228df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1229df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1230df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1231df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1232df8bae1dSRodney W. Grimes  */
12330312fbe9SPoul-Henning Kamp static struct tcpcb *
1234df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1235df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1236df8bae1dSRodney W. Grimes {
1237df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1240df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
12414cc20ab1SSeigo Tanimura 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1242243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
12434cc20ab1SSeigo Tanimura 	else {
1244df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1245df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1246df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1247df8bae1dSRodney W. Grimes 		if (tp)
1248df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1249df8bae1dSRodney W. Grimes 	}
1250df8bae1dSRodney W. Grimes 	return (tp);
1251df8bae1dSRodney W. Grimes }
1252df8bae1dSRodney W. Grimes 
1253df8bae1dSRodney W. Grimes /*
1254df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1255df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1256df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1257df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1258df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1259df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1260df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1261df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1262df8bae1dSRodney W. Grimes  */
12630312fbe9SPoul-Henning Kamp static struct tcpcb *
1264df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1265df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1266df8bae1dSRodney W. Grimes {
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1269df8bae1dSRodney W. Grimes 
1270df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1271df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1272df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1273df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1274df8bae1dSRodney W. Grimes 		break;
1275df8bae1dSRodney W. Grimes 
1276a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1277df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1278a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1279a0292f23SGarrett Wollman 		break;
1280a0292f23SGarrett Wollman 
1281df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1282df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1283df8bae1dSRodney W. Grimes 		break;
1284df8bae1dSRodney W. Grimes 
1285df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1286df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1287df8bae1dSRodney W. Grimes 		break;
1288df8bae1dSRodney W. Grimes 	}
1289b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1290df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1291b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1292b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
12939b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
12949b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1295b6239c4aSAndras Olah 	}
1296df8bae1dSRodney W. Grimes 	return (tp);
1297df8bae1dSRodney W. Grimes }
1298a0292f23SGarrett Wollman 
1299