xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 1db24ffb98a74acab6daaddc9445870f4a069dc9)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
331fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
38fb59c426SYoshinobu Inoue #include "opt_inet6.h"
390cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
400cc12cc5SJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43c7a82f90SGarrett Wollman #include <sys/kernel.h>
4498163b98SPoul-Henning Kamp #include <sys/sysctl.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46fb59c426SYoshinobu Inoue #ifdef INET6
47fb59c426SYoshinobu Inoue #include <sys/domain.h>
48fb59c426SYoshinobu Inoue #endif /* INET6 */
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5291421ba2SRobert Watson #include <sys/proc.h>
5391421ba2SRobert Watson #include <sys/jail.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
56df8bae1dSRodney W. Grimes #include <net/route.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
60fb59c426SYoshinobu Inoue #ifdef INET6
61fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
62fb59c426SYoshinobu Inoue #endif
63df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
64fb59c426SYoshinobu Inoue #ifdef INET6
65fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
66fb59c426SYoshinobu Inoue #endif
67b5e8ce9fSBruce Evans #include <netinet/in_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
69fb59c426SYoshinobu Inoue #ifdef INET6
70fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
71fb59c426SYoshinobu Inoue #endif
72df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
74df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
75df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
78610ee2f9SDavid Greenman #ifdef TCPDEBUG
79df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
80610ee2f9SDavid Greenman #endif
81df8bae1dSRodney W. Grimes 
826a800098SYoshinobu Inoue #ifdef IPSEC
836a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
846a800098SYoshinobu Inoue #endif /*IPSEC*/
856a800098SYoshinobu Inoue 
86df8bae1dSRodney W. Grimes /*
87df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
88df8bae1dSRodney W. Grimes  */
89117bcae7SGarrett Wollman extern	char *tcpstates[];	/* XXX ??? */
90df8bae1dSRodney W. Grimes 
91a29f300eSGarrett Wollman static int	tcp_attach __P((struct socket *, struct proc *));
9257bf258eSGarrett Wollman static int	tcp_connect __P((struct tcpcb *, struct sockaddr *,
93a29f300eSGarrett Wollman 				 struct proc *));
94fb59c426SYoshinobu Inoue #ifdef INET6
95fb59c426SYoshinobu Inoue static int	tcp6_connect __P((struct tcpcb *, struct sockaddr *,
96fb59c426SYoshinobu Inoue 				 struct proc *));
97fb59c426SYoshinobu Inoue #endif /* INET6 */
980312fbe9SPoul-Henning Kamp static struct tcpcb *
990312fbe9SPoul-Henning Kamp 		tcp_disconnect __P((struct tcpcb *));
1000312fbe9SPoul-Henning Kamp static struct tcpcb *
1010312fbe9SPoul-Henning Kamp 		tcp_usrclosed __P((struct tcpcb *));
1022c37256eSGarrett Wollman 
1032c37256eSGarrett Wollman #ifdef TCPDEBUG
1041db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1052c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
106af7a2999SDavid Greenman #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
107fb59c426SYoshinobu Inoue 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1082c37256eSGarrett Wollman #else
1092c37256eSGarrett Wollman #define	TCPDEBUG0
1102c37256eSGarrett Wollman #define	TCPDEBUG1()
1112c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1122c37256eSGarrett Wollman #endif
1132c37256eSGarrett Wollman 
1142c37256eSGarrett Wollman /*
1152c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1162c37256eSGarrett Wollman  * and an internet control block.
1172c37256eSGarrett Wollman  */
1182c37256eSGarrett Wollman static int
119a29f300eSGarrett Wollman tcp_usr_attach(struct socket *so, int proto, struct proc *p)
1202c37256eSGarrett Wollman {
1212c37256eSGarrett Wollman 	int s = splnet();
1222c37256eSGarrett Wollman 	int error;
1232c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1242c37256eSGarrett Wollman 	struct tcpcb *tp = 0;
1252c37256eSGarrett Wollman 	TCPDEBUG0;
1262c37256eSGarrett Wollman 
1272c37256eSGarrett Wollman 	TCPDEBUG1();
1282c37256eSGarrett Wollman 	if (inp) {
1292c37256eSGarrett Wollman 		error = EISCONN;
1302c37256eSGarrett Wollman 		goto out;
1312c37256eSGarrett Wollman 	}
1322c37256eSGarrett Wollman 
133a29f300eSGarrett Wollman 	error = tcp_attach(so, p);
1342c37256eSGarrett Wollman 	if (error)
1352c37256eSGarrett Wollman 		goto out;
1362c37256eSGarrett Wollman 
1372c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1383879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
1392c37256eSGarrett Wollman 	tp = sototcpcb(so);
1402c37256eSGarrett Wollman out:
1412c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1422c37256eSGarrett Wollman 	splx(s);
1432c37256eSGarrett Wollman 	return error;
1442c37256eSGarrett Wollman }
1452c37256eSGarrett Wollman 
1462c37256eSGarrett Wollman /*
1472c37256eSGarrett Wollman  * pru_detach() detaches the TCP protocol from the socket.
1482c37256eSGarrett Wollman  * If the protocol state is non-embryonic, then can't
1492c37256eSGarrett Wollman  * do this directly: have to initiate a pru_disconnect(),
1502c37256eSGarrett Wollman  * which may finish later; embryonic TCB's can just
1512c37256eSGarrett Wollman  * be discarded here.
1522c37256eSGarrett Wollman  */
1532c37256eSGarrett Wollman static int
1542c37256eSGarrett Wollman tcp_usr_detach(struct socket *so)
1552c37256eSGarrett Wollman {
1562c37256eSGarrett Wollman 	int s = splnet();
1572c37256eSGarrett Wollman 	int error = 0;
1582c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1592c37256eSGarrett Wollman 	struct tcpcb *tp;
1602c37256eSGarrett Wollman 	TCPDEBUG0;
1612c37256eSGarrett Wollman 
1622c37256eSGarrett Wollman 	if (inp == 0) {
1632c37256eSGarrett Wollman 		splx(s);
1642c37256eSGarrett Wollman 		return EINVAL;	/* XXX */
1652c37256eSGarrett Wollman 	}
1662c37256eSGarrett Wollman 	tp = intotcpcb(inp);
1672c37256eSGarrett Wollman 	TCPDEBUG1();
1682c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
1692c37256eSGarrett Wollman 
1702c37256eSGarrett Wollman 	TCPDEBUG2(PRU_DETACH);
1712c37256eSGarrett Wollman 	splx(s);
1722c37256eSGarrett Wollman 	return error;
1732c37256eSGarrett Wollman }
1742c37256eSGarrett Wollman 
1752c37256eSGarrett Wollman #define	COMMON_START()	TCPDEBUG0; \
1762c37256eSGarrett Wollman 			do { \
1772c37256eSGarrett Wollman 				     if (inp == 0) { \
1782c37256eSGarrett Wollman 					     splx(s); \
1792c37256eSGarrett Wollman 					     return EINVAL; \
1802c37256eSGarrett Wollman 				     } \
1812c37256eSGarrett Wollman 				     tp = intotcpcb(inp); \
1822c37256eSGarrett Wollman 				     TCPDEBUG1(); \
1832c37256eSGarrett Wollman 		     } while(0)
1842c37256eSGarrett Wollman 
1852c37256eSGarrett Wollman #define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
1862c37256eSGarrett Wollman 
1872c37256eSGarrett Wollman 
1882c37256eSGarrett Wollman /*
1892c37256eSGarrett Wollman  * Give the socket an address.
1902c37256eSGarrett Wollman  */
1912c37256eSGarrett Wollman static int
19257bf258eSGarrett Wollman tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1932c37256eSGarrett Wollman {
1942c37256eSGarrett Wollman 	int s = splnet();
1952c37256eSGarrett Wollman 	int error = 0;
1962c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1972c37256eSGarrett Wollman 	struct tcpcb *tp;
1982c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
1992c37256eSGarrett Wollman 
2002c37256eSGarrett Wollman 	COMMON_START();
2012c37256eSGarrett Wollman 
2022c37256eSGarrett Wollman 	/*
2032c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2042c37256eSGarrett Wollman 	 * to them.
2052c37256eSGarrett Wollman 	 */
20657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
2072c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
2082c37256eSGarrett Wollman 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
2092c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
2102c37256eSGarrett Wollman 		goto out;
2112c37256eSGarrett Wollman 	}
212a29f300eSGarrett Wollman 	error = in_pcbbind(inp, nam, p);
2132c37256eSGarrett Wollman 	if (error)
2142c37256eSGarrett Wollman 		goto out;
2152c37256eSGarrett Wollman 	COMMON_END(PRU_BIND);
2162c37256eSGarrett Wollman 
2172c37256eSGarrett Wollman }
2182c37256eSGarrett Wollman 
219fb59c426SYoshinobu Inoue #ifdef INET6
220fb59c426SYoshinobu Inoue static int
221fb59c426SYoshinobu Inoue tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
222fb59c426SYoshinobu Inoue {
223fb59c426SYoshinobu Inoue 	int s = splnet();
224fb59c426SYoshinobu Inoue 	int error = 0;
225fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
226fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
227fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
228fb59c426SYoshinobu Inoue 
229fb59c426SYoshinobu Inoue 	COMMON_START();
230fb59c426SYoshinobu Inoue 
231fb59c426SYoshinobu Inoue 	/*
232fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
233fb59c426SYoshinobu Inoue 	 * to them.
234fb59c426SYoshinobu Inoue 	 */
235fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
236fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
237fb59c426SYoshinobu Inoue 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
238fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
239fb59c426SYoshinobu Inoue 		goto out;
240fb59c426SYoshinobu Inoue 	}
241fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
242fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
243fdaf052eSYoshinobu Inoue 	if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
244fb59c426SYoshinobu Inoue 
245fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
246fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
247fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
248fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
249fb59c426SYoshinobu Inoue 
250fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
251fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
252fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
253fb59c426SYoshinobu Inoue 			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
254fb59c426SYoshinobu Inoue 			goto out;
255fb59c426SYoshinobu Inoue 		}
256fb59c426SYoshinobu Inoue 	}
257fb59c426SYoshinobu Inoue 	error = in6_pcbbind(inp, nam, p);
258fb59c426SYoshinobu Inoue 	if (error)
259fb59c426SYoshinobu Inoue 		goto out;
260fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
261fb59c426SYoshinobu Inoue }
262fb59c426SYoshinobu Inoue #endif /* INET6 */
263fb59c426SYoshinobu Inoue 
2642c37256eSGarrett Wollman /*
2652c37256eSGarrett Wollman  * Prepare to accept connections.
2662c37256eSGarrett Wollman  */
2672c37256eSGarrett Wollman static int
268a29f300eSGarrett Wollman tcp_usr_listen(struct socket *so, struct proc *p)
2692c37256eSGarrett Wollman {
2702c37256eSGarrett Wollman 	int s = splnet();
2712c37256eSGarrett Wollman 	int error = 0;
2722c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
2732c37256eSGarrett Wollman 	struct tcpcb *tp;
2742c37256eSGarrett Wollman 
2752c37256eSGarrett Wollman 	COMMON_START();
2762c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
27757bf258eSGarrett Wollman 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
2782c37256eSGarrett Wollman 	if (error == 0)
2792c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
2802c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
2812c37256eSGarrett Wollman }
2822c37256eSGarrett Wollman 
283fb59c426SYoshinobu Inoue #ifdef INET6
284fb59c426SYoshinobu Inoue static int
285fb59c426SYoshinobu Inoue tcp6_usr_listen(struct socket *so, struct proc *p)
286fb59c426SYoshinobu Inoue {
287fb59c426SYoshinobu Inoue 	int s = splnet();
288fb59c426SYoshinobu Inoue 	int error = 0;
289fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
290fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
291fb59c426SYoshinobu Inoue 
292fb59c426SYoshinobu Inoue 	COMMON_START();
293fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
294fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
295fdaf052eSYoshinobu Inoue 		if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0)
296fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
297fb59c426SYoshinobu Inoue 		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
298fb59c426SYoshinobu Inoue 	}
299fb59c426SYoshinobu Inoue 	if (error == 0)
300fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
301fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
302fb59c426SYoshinobu Inoue }
303fb59c426SYoshinobu Inoue #endif /* INET6 */
304fb59c426SYoshinobu Inoue 
3052c37256eSGarrett Wollman /*
3062c37256eSGarrett Wollman  * Initiate connection to peer.
3072c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3082c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3092c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3102c37256eSGarrett Wollman  * Send initial segment on connection.
3112c37256eSGarrett Wollman  */
3122c37256eSGarrett Wollman static int
31357bf258eSGarrett Wollman tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
3142c37256eSGarrett Wollman {
3152c37256eSGarrett Wollman 	int s = splnet();
3162c37256eSGarrett Wollman 	int error = 0;
3172c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
3182c37256eSGarrett Wollman 	struct tcpcb *tp;
3192c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3202c37256eSGarrett Wollman 
3212c37256eSGarrett Wollman 	COMMON_START();
3222c37256eSGarrett Wollman 
3232c37256eSGarrett Wollman 	/*
3242c37256eSGarrett Wollman 	 * Must disallow TCP ``connections'' to multicast addresses.
3252c37256eSGarrett Wollman 	 */
32657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
3272c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
3282c37256eSGarrett Wollman 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
3292c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
3302c37256eSGarrett Wollman 		goto out;
3312c37256eSGarrett Wollman 	}
3322c37256eSGarrett Wollman 
33391421ba2SRobert Watson 	if (p && jailed(p->p_ucred))
33491421ba2SRobert Watson 		prison_remote_ip(p->p_ucred, 0, &sinp->sin_addr.s_addr);
33575c13541SPoul-Henning Kamp 
336a29f300eSGarrett Wollman 	if ((error = tcp_connect(tp, nam, p)) != 0)
3372c37256eSGarrett Wollman 		goto out;
3382c37256eSGarrett Wollman 	error = tcp_output(tp);
3392c37256eSGarrett Wollman 	COMMON_END(PRU_CONNECT);
3402c37256eSGarrett Wollman }
3412c37256eSGarrett Wollman 
342fb59c426SYoshinobu Inoue #ifdef INET6
343fb59c426SYoshinobu Inoue static int
344fb59c426SYoshinobu Inoue tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
345fb59c426SYoshinobu Inoue {
346fb59c426SYoshinobu Inoue 	int s = splnet();
347fb59c426SYoshinobu Inoue 	int error = 0;
348fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
349fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
350fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
351fb59c426SYoshinobu Inoue 
352fb59c426SYoshinobu Inoue 	COMMON_START();
353fb59c426SYoshinobu Inoue 
354fb59c426SYoshinobu Inoue 	/*
355fb59c426SYoshinobu Inoue 	 * Must disallow TCP ``connections'' to multicast addresses.
356fb59c426SYoshinobu Inoue 	 */
357fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
358fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
359fb59c426SYoshinobu Inoue 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
360fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
361fb59c426SYoshinobu Inoue 		goto out;
362fb59c426SYoshinobu Inoue 	}
363fb59c426SYoshinobu Inoue 
364fdaf052eSYoshinobu Inoue 	if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0 &&
365fb59c426SYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
366fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
367fb59c426SYoshinobu Inoue 
368fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
369fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
370fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
371fb59c426SYoshinobu Inoue 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
372fb59c426SYoshinobu Inoue 			goto out;
373fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
374fb59c426SYoshinobu Inoue 		goto out;
375fb59c426SYoshinobu Inoue 	}
376fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
377fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
378fb59c426SYoshinobu Inoue 	if ((error = tcp6_connect(tp, nam, p)) != 0)
379fb59c426SYoshinobu Inoue 		goto out;
380fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
381fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
382fb59c426SYoshinobu Inoue }
383fb59c426SYoshinobu Inoue #endif /* INET6 */
384fb59c426SYoshinobu Inoue 
3852c37256eSGarrett Wollman /*
3862c37256eSGarrett Wollman  * Initiate disconnect from peer.
3872c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
3882c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
3892c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
3902c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
3912c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
3922c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
3932c37256eSGarrett Wollman  *
3942c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
3952c37256eSGarrett Wollman  */
3962c37256eSGarrett Wollman static int
3972c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
3982c37256eSGarrett Wollman {
3992c37256eSGarrett Wollman 	int s = splnet();
4002c37256eSGarrett Wollman 	int error = 0;
4012c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4022c37256eSGarrett Wollman 	struct tcpcb *tp;
4032c37256eSGarrett Wollman 
4042c37256eSGarrett Wollman 	COMMON_START();
4052c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4062c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4072c37256eSGarrett Wollman }
4082c37256eSGarrett Wollman 
4092c37256eSGarrett Wollman /*
4102c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4112c37256eSGarrett Wollman  * done at higher levels; just return the address
4122c37256eSGarrett Wollman  * of the peer, storing through addr.
4132c37256eSGarrett Wollman  */
4142c37256eSGarrett Wollman static int
41557bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4162c37256eSGarrett Wollman {
4172c37256eSGarrett Wollman 	int s = splnet();
4182c37256eSGarrett Wollman 	int error = 0;
4192c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4201db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
4211db24ffbSJonathan Lemon 	TCPDEBUG0;
4222c37256eSGarrett Wollman 
423c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
424c0647e0dSJonathan Lemon 		error = ECONNABORTED;
425c0647e0dSJonathan Lemon 		goto out;
426c0647e0dSJonathan Lemon 	}
4271db24ffbSJonathan Lemon 	if (inp == 0) {
4281db24ffbSJonathan Lemon 		splx(s);
4291db24ffbSJonathan Lemon 		return (EINVAL);
4301db24ffbSJonathan Lemon 	}
4311db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4321db24ffbSJonathan Lemon 	TCPDEBUG1();
433117bcae7SGarrett Wollman 	in_setpeeraddr(so, nam);
4342c37256eSGarrett Wollman 	COMMON_END(PRU_ACCEPT);
4352c37256eSGarrett Wollman }
4362c37256eSGarrett Wollman 
437fb59c426SYoshinobu Inoue #ifdef INET6
438fb59c426SYoshinobu Inoue static int
439fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
440fb59c426SYoshinobu Inoue {
441fb59c426SYoshinobu Inoue 	int s = splnet();
442fb59c426SYoshinobu Inoue 	int error = 0;
443fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
4441db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
4451db24ffbSJonathan Lemon 	TCPDEBUG0;
446fb59c426SYoshinobu Inoue 
447c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
448c0647e0dSJonathan Lemon 		error = ECONNABORTED;
449c0647e0dSJonathan Lemon 		goto out;
450c0647e0dSJonathan Lemon 	}
4511db24ffbSJonathan Lemon 	if (inp == 0) {
4521db24ffbSJonathan Lemon 		splx(s);
4531db24ffbSJonathan Lemon 		return (EINVAL);
4541db24ffbSJonathan Lemon 	}
4551db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4561db24ffbSJonathan Lemon 	TCPDEBUG1();
457fb59c426SYoshinobu Inoue 	in6_mapped_peeraddr(so, nam);
458fb59c426SYoshinobu Inoue 	COMMON_END(PRU_ACCEPT);
459fb59c426SYoshinobu Inoue }
460fb59c426SYoshinobu Inoue #endif /* INET6 */
4612c37256eSGarrett Wollman /*
4622c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
4632c37256eSGarrett Wollman  */
4642c37256eSGarrett Wollman static int
4652c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
4662c37256eSGarrett Wollman {
4672c37256eSGarrett Wollman 	int s = splnet();
4682c37256eSGarrett Wollman 	int error = 0;
4692c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4702c37256eSGarrett Wollman 	struct tcpcb *tp;
4712c37256eSGarrett Wollman 
4722c37256eSGarrett Wollman 	COMMON_START();
4732c37256eSGarrett Wollman 	socantsendmore(so);
4742c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
4752c37256eSGarrett Wollman 	if (tp)
4762c37256eSGarrett Wollman 		error = tcp_output(tp);
4772c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
4782c37256eSGarrett Wollman }
4792c37256eSGarrett Wollman 
4802c37256eSGarrett Wollman /*
4812c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
4822c37256eSGarrett Wollman  */
4832c37256eSGarrett Wollman static int
4842c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
4852c37256eSGarrett Wollman {
4862c37256eSGarrett Wollman 	int s = splnet();
4872c37256eSGarrett Wollman 	int error = 0;
4882c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4892c37256eSGarrett Wollman 	struct tcpcb *tp;
4902c37256eSGarrett Wollman 
4912c37256eSGarrett Wollman 	COMMON_START();
4922c37256eSGarrett Wollman 	tcp_output(tp);
4932c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
4942c37256eSGarrett Wollman }
4952c37256eSGarrett Wollman 
4962c37256eSGarrett Wollman /*
4972c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
4989c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
4999c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
5009c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
5019c9906e9SPeter Wemm  * generally are caller-frees.
5022c37256eSGarrett Wollman  */
5032c37256eSGarrett Wollman static int
50457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
50557bf258eSGarrett Wollman 	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
5062c37256eSGarrett Wollman {
5072c37256eSGarrett Wollman 	int s = splnet();
5082c37256eSGarrett Wollman 	int error = 0;
5092c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
5102c37256eSGarrett Wollman 	struct tcpcb *tp;
511fb59c426SYoshinobu Inoue #ifdef INET6
512fb59c426SYoshinobu Inoue 	int isipv6;
513fb59c426SYoshinobu Inoue #endif
5149c9906e9SPeter Wemm 	TCPDEBUG0;
5152c37256eSGarrett Wollman 
5169c9906e9SPeter Wemm 	if (inp == NULL) {
5179c9906e9SPeter Wemm 		/*
5189c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
5199c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
5209c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
5219c9906e9SPeter Wemm 		 */
5229c9906e9SPeter Wemm 		if (m)
5239c9906e9SPeter Wemm 			m_freem(m);
5249c9906e9SPeter Wemm 		if (control)
5259c9906e9SPeter Wemm 			m_freem(control);
5269c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
52745d3a132SPeter Wemm 		tp = NULL;
52845d3a132SPeter Wemm 		TCPDEBUG1();
5299c9906e9SPeter Wemm 		goto out;
5309c9906e9SPeter Wemm 	}
531fb59c426SYoshinobu Inoue #ifdef INET6
532fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
533fb59c426SYoshinobu Inoue #endif /* INET6 */
5349c9906e9SPeter Wemm 	tp = intotcpcb(inp);
5359c9906e9SPeter Wemm 	TCPDEBUG1();
5369c9906e9SPeter Wemm 	if (control) {
5379c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
5389c9906e9SPeter Wemm 		if (control->m_len) {
5399c9906e9SPeter Wemm 			m_freem(control);
5402c37256eSGarrett Wollman 			if (m)
5412c37256eSGarrett Wollman 				m_freem(m);
542744f87eaSDavid Greenman 			error = EINVAL;
543744f87eaSDavid Greenman 			goto out;
5442c37256eSGarrett Wollman 		}
5459c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
5469c9906e9SPeter Wemm 	}
5472c37256eSGarrett Wollman 	if(!(flags & PRUS_OOB)) {
5482c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
5492c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
5502c37256eSGarrett Wollman 			/*
5512c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
5522c37256eSGarrett Wollman 			 * initialize window to default value, and
5532c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
5542c37256eSGarrett Wollman 			 * MSS.
5552c37256eSGarrett Wollman 			 */
556fb59c426SYoshinobu Inoue #ifdef INET6
557fb59c426SYoshinobu Inoue 			if (isipv6)
558fb59c426SYoshinobu Inoue 				error = tcp6_connect(tp, nam, p);
559fb59c426SYoshinobu Inoue 			else
560fb59c426SYoshinobu Inoue #endif /* INET6 */
561a29f300eSGarrett Wollman 			error = tcp_connect(tp, nam, p);
5622c37256eSGarrett Wollman 			if (error)
5632c37256eSGarrett Wollman 				goto out;
5642c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
5652c37256eSGarrett Wollman 			tcp_mss(tp, -1);
5662c37256eSGarrett Wollman 		}
5672c37256eSGarrett Wollman 
5682c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
5692c37256eSGarrett Wollman 			/*
5702c37256eSGarrett Wollman 			 * Close the send side of the connection after
5712c37256eSGarrett Wollman 			 * the data is sent.
5722c37256eSGarrett Wollman 			 */
5732c37256eSGarrett Wollman 			socantsendmore(so);
5742c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
5752c37256eSGarrett Wollman 		}
576b0acefa8SBill Fenner 		if (tp != NULL) {
577b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
578b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
5792c37256eSGarrett Wollman 			error = tcp_output(tp);
580b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
581b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
582b0acefa8SBill Fenner 		}
5832c37256eSGarrett Wollman 	} else {
5842c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
5852c37256eSGarrett Wollman 			m_freem(m);
5862c37256eSGarrett Wollman 			error = ENOBUFS;
5872c37256eSGarrett Wollman 			goto out;
5882c37256eSGarrett Wollman 		}
5892c37256eSGarrett Wollman 		/*
5902c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
5912c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
5922c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
5932c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
5942c37256eSGarrett Wollman 		 * of data past the urgent section.
5952c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
5962c37256eSGarrett Wollman 		 */
5972c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
598ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
599ef53690bSGarrett Wollman 			/*
600ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
601ef53690bSGarrett Wollman 			 * initialize window to default value, and
602ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
603ef53690bSGarrett Wollman 			 * MSS.
604ef53690bSGarrett Wollman 			 */
605fb59c426SYoshinobu Inoue #ifdef INET6
606fb59c426SYoshinobu Inoue 			if (isipv6)
607fb59c426SYoshinobu Inoue 				error = tcp6_connect(tp, nam, p);
608fb59c426SYoshinobu Inoue 			else
609fb59c426SYoshinobu Inoue #endif /* INET6 */
610a29f300eSGarrett Wollman 			error = tcp_connect(tp, nam, p);
611ef53690bSGarrett Wollman 			if (error)
612ef53690bSGarrett Wollman 				goto out;
613ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
614ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
615ef53690bSGarrett Wollman 		}
6162c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
6172c37256eSGarrett Wollman 		tp->t_force = 1;
6182c37256eSGarrett Wollman 		error = tcp_output(tp);
6192c37256eSGarrett Wollman 		tp->t_force = 0;
6202c37256eSGarrett Wollman 	}
6212c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
6222c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
6232c37256eSGarrett Wollman }
6242c37256eSGarrett Wollman 
6252c37256eSGarrett Wollman /*
6262c37256eSGarrett Wollman  * Abort the TCP.
6272c37256eSGarrett Wollman  */
6282c37256eSGarrett Wollman static int
6292c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
6302c37256eSGarrett Wollman {
6312c37256eSGarrett Wollman 	int s = splnet();
6322c37256eSGarrett Wollman 	int error = 0;
6332c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6342c37256eSGarrett Wollman 	struct tcpcb *tp;
6352c37256eSGarrett Wollman 
6362c37256eSGarrett Wollman 	COMMON_START();
6372c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
6382c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
6392c37256eSGarrett Wollman }
6402c37256eSGarrett Wollman 
6412c37256eSGarrett Wollman /*
6422c37256eSGarrett Wollman  * Receive out-of-band data.
6432c37256eSGarrett Wollman  */
6442c37256eSGarrett Wollman static int
6452c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
6462c37256eSGarrett Wollman {
6472c37256eSGarrett Wollman 	int s = splnet();
6482c37256eSGarrett Wollman 	int error = 0;
6492c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6502c37256eSGarrett Wollman 	struct tcpcb *tp;
6512c37256eSGarrett Wollman 
6522c37256eSGarrett Wollman 	COMMON_START();
6532c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
6542c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
6552c37256eSGarrett Wollman 	    so->so_options & SO_OOBINLINE ||
6562c37256eSGarrett Wollman 	    tp->t_oobflags & TCPOOB_HADDATA) {
6572c37256eSGarrett Wollman 		error = EINVAL;
6582c37256eSGarrett Wollman 		goto out;
6592c37256eSGarrett Wollman 	}
6602c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
6612c37256eSGarrett Wollman 		error = EWOULDBLOCK;
6622c37256eSGarrett Wollman 		goto out;
6632c37256eSGarrett Wollman 	}
6642c37256eSGarrett Wollman 	m->m_len = 1;
6652c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
6662c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
6672c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
6682c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
6692c37256eSGarrett Wollman }
6702c37256eSGarrett Wollman 
6712c37256eSGarrett Wollman /* xxx - should be const */
6722c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
6732c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
674117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
675117bcae7SGarrett Wollman 	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
676117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
677f8f6cbbaSPeter Wemm 	in_setsockaddr, sosend, soreceive, sopoll
6782c37256eSGarrett Wollman };
679df8bae1dSRodney W. Grimes 
680fb59c426SYoshinobu Inoue #ifdef INET6
681fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
682fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
683fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
684fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
685fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
686fb59c426SYoshinobu Inoue 	in6_mapped_sockaddr, sosend, soreceive, sopoll
687fb59c426SYoshinobu Inoue };
688fb59c426SYoshinobu Inoue #endif /* INET6 */
689fb59c426SYoshinobu Inoue 
690a0292f23SGarrett Wollman /*
691a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
692a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
693a0292f23SGarrett Wollman  * port number if needed.  Call in_pcbladdr to do the routing and to choose
694a0292f23SGarrett Wollman  * a local host address (interface).  If there is an existing incarnation
695a0292f23SGarrett Wollman  * of the same connection in TIME-WAIT state and if the remote host was
696a0292f23SGarrett Wollman  * sending CC options and if the connection duration was < MSL, then
697a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
698a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
699a0292f23SGarrett Wollman  */
7000312fbe9SPoul-Henning Kamp static int
701a29f300eSGarrett Wollman tcp_connect(tp, nam, p)
702a0292f23SGarrett Wollman 	register struct tcpcb *tp;
70357bf258eSGarrett Wollman 	struct sockaddr *nam;
704a29f300eSGarrett Wollman 	struct proc *p;
705a0292f23SGarrett Wollman {
706a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
707a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
708a0292f23SGarrett Wollman 	struct tcpcb *otp;
70957bf258eSGarrett Wollman 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
710a0292f23SGarrett Wollman 	struct sockaddr_in *ifaddr;
711a45d2726SAndras Olah 	struct rmxp_tao *taop;
712a45d2726SAndras Olah 	struct rmxp_tao tao_noncached;
713c3229e05SDavid Greenman 	int error;
714a0292f23SGarrett Wollman 
715a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
71657bf258eSGarrett Wollman 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
717a0292f23SGarrett Wollman 		if (error)
718a0292f23SGarrett Wollman 			return error;
719a0292f23SGarrett Wollman 	}
720a0292f23SGarrett Wollman 
721a0292f23SGarrett Wollman 	/*
722a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
723a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
724a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
725a0292f23SGarrett Wollman 	 */
726a0292f23SGarrett Wollman 	error = in_pcbladdr(inp, nam, &ifaddr);
727d3628763SRodney W. Grimes 	if (error)
728d3628763SRodney W. Grimes 		return error;
729c3229e05SDavid Greenman 	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
730a0292f23SGarrett Wollman 	    sin->sin_addr, sin->sin_port,
731a0292f23SGarrett Wollman 	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
732a0292f23SGarrett Wollman 						: ifaddr->sin_addr,
733cfa1ca9dSYoshinobu Inoue 	    inp->inp_lport,  0, NULL);
734a0292f23SGarrett Wollman 	if (oinp) {
735a0292f23SGarrett Wollman 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
736a0292f23SGarrett Wollman 		otp->t_state == TCPS_TIME_WAIT &&
7379b8b58e0SJonathan Lemon 		    (ticks - otp->t_starttime) < tcp_msl &&
738a0292f23SGarrett Wollman 		    (otp->t_flags & TF_RCVD_CC))
739a0292f23SGarrett Wollman 			otp = tcp_close(otp);
740a0292f23SGarrett Wollman 		else
741a0292f23SGarrett Wollman 			return EADDRINUSE;
742a0292f23SGarrett Wollman 	}
743a0292f23SGarrett Wollman 	if (inp->inp_laddr.s_addr == INADDR_ANY)
744a0292f23SGarrett Wollman 		inp->inp_laddr = ifaddr->sin_addr;
745a0292f23SGarrett Wollman 	inp->inp_faddr = sin->sin_addr;
746a0292f23SGarrett Wollman 	inp->inp_fport = sin->sin_port;
74715bd2b43SDavid Greenman 	in_pcbrehash(inp);
748a0292f23SGarrett Wollman 
749a0292f23SGarrett Wollman 	tp->t_template = tcp_template(tp);
750a0292f23SGarrett Wollman 	if (tp->t_template == 0) {
751a0292f23SGarrett Wollman 		in_pcbdisconnect(inp);
752a0292f23SGarrett Wollman 		return ENOBUFS;
753a0292f23SGarrett Wollman 	}
754a0292f23SGarrett Wollman 
755a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
756a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
757a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
758a0292f23SGarrett Wollman 		tp->request_r_scale++;
759a0292f23SGarrett Wollman 
760a0292f23SGarrett Wollman 	soisconnecting(so);
761a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
762a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
7639b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
764a0292f23SGarrett Wollman 	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
765a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
766a45d2726SAndras Olah 
767a45d2726SAndras Olah 	/*
768a45d2726SAndras Olah 	 * Generate a CC value for this connection and
769a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
770a45d2726SAndras Olah 	 */
771a45d2726SAndras Olah 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
772a45d2726SAndras Olah 		taop = &tao_noncached;
773a45d2726SAndras Olah 		bzero(taop, sizeof(*taop));
774a45d2726SAndras Olah 	}
775a45d2726SAndras Olah 
776a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
777a45d2726SAndras Olah 	if (taop->tao_ccsent != 0 &&
778a45d2726SAndras Olah 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
779a45d2726SAndras Olah 		taop->tao_ccsent = tp->cc_send;
780a45d2726SAndras Olah 	} else {
781a45d2726SAndras Olah 		taop->tao_ccsent = 0;
782a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
783a45d2726SAndras Olah 	}
784a0292f23SGarrett Wollman 
785a0292f23SGarrett Wollman 	return 0;
786a0292f23SGarrett Wollman }
787a0292f23SGarrett Wollman 
788fb59c426SYoshinobu Inoue #ifdef INET6
789fb59c426SYoshinobu Inoue static int
790fb59c426SYoshinobu Inoue tcp6_connect(tp, nam, p)
791fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
792fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
793fb59c426SYoshinobu Inoue 	struct proc *p;
794fb59c426SYoshinobu Inoue {
795fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
796fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
797fb59c426SYoshinobu Inoue 	struct tcpcb *otp;
798fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
799fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
800fb59c426SYoshinobu Inoue 	struct rmxp_tao *taop;
801fb59c426SYoshinobu Inoue 	struct rmxp_tao tao_noncached;
802fb59c426SYoshinobu Inoue 	int error;
803fb59c426SYoshinobu Inoue 
804fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
805fb59c426SYoshinobu Inoue 		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
806fb59c426SYoshinobu Inoue 		if (error)
807fb59c426SYoshinobu Inoue 			return error;
808fb59c426SYoshinobu Inoue 	}
809fb59c426SYoshinobu Inoue 
810fb59c426SYoshinobu Inoue 	/*
811fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
812fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
813fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
814fb59c426SYoshinobu Inoue 	 */
815fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
816fb59c426SYoshinobu Inoue 	if (error)
817fb59c426SYoshinobu Inoue 		return error;
818fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
819fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
820fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
821fb59c426SYoshinobu Inoue 				  ? addr6
822fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
823fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
824fb59c426SYoshinobu Inoue 	if (oinp) {
825fb59c426SYoshinobu Inoue 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
826fb59c426SYoshinobu Inoue 		    otp->t_state == TCPS_TIME_WAIT &&
827fb59c426SYoshinobu Inoue 		    (ticks - otp->t_starttime) < tcp_msl &&
828fb59c426SYoshinobu Inoue 		    (otp->t_flags & TF_RCVD_CC))
829fb59c426SYoshinobu Inoue 			otp = tcp_close(otp);
830fb59c426SYoshinobu Inoue 		else
831fb59c426SYoshinobu Inoue 			return EADDRINUSE;
832fb59c426SYoshinobu Inoue 	}
833fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
834fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
835fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
836fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
837fb59c426SYoshinobu Inoue 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
838fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
839fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
840fb59c426SYoshinobu Inoue 
841fb59c426SYoshinobu Inoue 	tp->t_template = tcp_template(tp);
842fb59c426SYoshinobu Inoue 	if (tp->t_template == 0) {
843fb59c426SYoshinobu Inoue 		in6_pcbdisconnect(inp);
844fb59c426SYoshinobu Inoue 		return ENOBUFS;
845fb59c426SYoshinobu Inoue 	}
846fb59c426SYoshinobu Inoue 
847fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
848fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
849fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
850fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
851fb59c426SYoshinobu Inoue 
852fb59c426SYoshinobu Inoue 	soisconnecting(so);
853fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
854fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
855fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
856fb59c426SYoshinobu Inoue 	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
857fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
858fb59c426SYoshinobu Inoue 
859fb59c426SYoshinobu Inoue 	/*
860fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
861fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
862fb59c426SYoshinobu Inoue 	 */
863fb59c426SYoshinobu Inoue 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
864fb59c426SYoshinobu Inoue 		taop = &tao_noncached;
865fb59c426SYoshinobu Inoue 		bzero(taop, sizeof(*taop));
866fb59c426SYoshinobu Inoue 	}
867fb59c426SYoshinobu Inoue 
868fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
869fb59c426SYoshinobu Inoue 	if (taop->tao_ccsent != 0 &&
870fb59c426SYoshinobu Inoue 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
871fb59c426SYoshinobu Inoue 		taop->tao_ccsent = tp->cc_send;
872fb59c426SYoshinobu Inoue 	} else {
873fb59c426SYoshinobu Inoue 		taop->tao_ccsent = 0;
874fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
875fb59c426SYoshinobu Inoue 	}
876fb59c426SYoshinobu Inoue 
877fb59c426SYoshinobu Inoue 	return 0;
878fb59c426SYoshinobu Inoue }
879fb59c426SYoshinobu Inoue #endif /* INET6 */
880fb59c426SYoshinobu Inoue 
881cfe8b629SGarrett Wollman /*
882cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
883cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
884cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
885cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
886cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
887cfe8b629SGarrett Wollman  */
888df8bae1dSRodney W. Grimes int
889cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
890df8bae1dSRodney W. Grimes 	struct socket *so;
891cfe8b629SGarrett Wollman 	struct sockopt *sopt;
892df8bae1dSRodney W. Grimes {
893cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
894df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
895cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
896df8bae1dSRodney W. Grimes 
897cfe8b629SGarrett Wollman 	error = 0;
898cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
899df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
900df8bae1dSRodney W. Grimes 	if (inp == NULL) {
901df8bae1dSRodney W. Grimes 		splx(s);
902df8bae1dSRodney W. Grimes 		return (ECONNRESET);
903df8bae1dSRodney W. Grimes 	}
904cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
905fb59c426SYoshinobu Inoue #ifdef INET6
906fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
907fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
908fb59c426SYoshinobu Inoue 		else
909fb59c426SYoshinobu Inoue #endif /* INET6 */
910cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
911df8bae1dSRodney W. Grimes 		splx(s);
912df8bae1dSRodney W. Grimes 		return (error);
913df8bae1dSRodney W. Grimes 	}
914df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
915df8bae1dSRodney W. Grimes 
916cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
917cfe8b629SGarrett Wollman 	case SOPT_SET:
918cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
919df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
920cfe8b629SGarrett Wollman 		case TCP_NOOPT:
921cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
922cfe8b629SGarrett Wollman 					    sizeof optval);
923cfe8b629SGarrett Wollman 			if (error)
924cfe8b629SGarrett Wollman 				break;
925cfe8b629SGarrett Wollman 
926cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
927cfe8b629SGarrett Wollman 			case TCP_NODELAY:
928cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
929cfe8b629SGarrett Wollman 				break;
930cfe8b629SGarrett Wollman 			case TCP_NOOPT:
931cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
932cfe8b629SGarrett Wollman 				break;
933cfe8b629SGarrett Wollman 			default:
934cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
935cfe8b629SGarrett Wollman 				break;
936cfe8b629SGarrett Wollman 			}
937cfe8b629SGarrett Wollman 
938cfe8b629SGarrett Wollman 			if (optval)
939cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
940df8bae1dSRodney W. Grimes 			else
941cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
942df8bae1dSRodney W. Grimes 			break;
943df8bae1dSRodney W. Grimes 
944007581c0SJonathan Lemon 		case TCP_NOPUSH:
945007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
946007581c0SJonathan Lemon 					    sizeof optval);
947007581c0SJonathan Lemon 			if (error)
948007581c0SJonathan Lemon 				break;
949007581c0SJonathan Lemon 
950007581c0SJonathan Lemon 			if (optval)
951007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
952007581c0SJonathan Lemon 			else {
953007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
954007581c0SJonathan Lemon 				error = tcp_output(tp);
955007581c0SJonathan Lemon 			}
956007581c0SJonathan Lemon 			break;
957007581c0SJonathan Lemon 
958df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
959cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
960cfe8b629SGarrett Wollman 					    sizeof optval);
961cfe8b629SGarrett Wollman 			if (error)
962df8bae1dSRodney W. Grimes 				break;
963df8bae1dSRodney W. Grimes 
964cfe8b629SGarrett Wollman 			if (optval > 0 && optval <= tp->t_maxseg)
965cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
966a0292f23SGarrett Wollman 			else
967a0292f23SGarrett Wollman 				error = EINVAL;
968a0292f23SGarrett Wollman 			break;
969a0292f23SGarrett Wollman 
970df8bae1dSRodney W. Grimes 		default:
971df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
972df8bae1dSRodney W. Grimes 			break;
973df8bae1dSRodney W. Grimes 		}
974df8bae1dSRodney W. Grimes 		break;
975df8bae1dSRodney W. Grimes 
976cfe8b629SGarrett Wollman 	case SOPT_GET:
977cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
978df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
979cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
980df8bae1dSRodney W. Grimes 			break;
981df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
982cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
983df8bae1dSRodney W. Grimes 			break;
984a0292f23SGarrett Wollman 		case TCP_NOOPT:
985cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
986a0292f23SGarrett Wollman 			break;
987a0292f23SGarrett Wollman 		case TCP_NOPUSH:
988cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
989a0292f23SGarrett Wollman 			break;
990df8bae1dSRodney W. Grimes 		default:
991df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
992df8bae1dSRodney W. Grimes 			break;
993df8bae1dSRodney W. Grimes 		}
994cfe8b629SGarrett Wollman 		if (error == 0)
995cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
996df8bae1dSRodney W. Grimes 		break;
997df8bae1dSRodney W. Grimes 	}
998df8bae1dSRodney W. Grimes 	splx(s);
999df8bae1dSRodney W. Grimes 	return (error);
1000df8bae1dSRodney W. Grimes }
1001df8bae1dSRodney W. Grimes 
100226e30fbbSDavid Greenman /*
100326e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
100426e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
100526e30fbbSDavid Greenman  * be set by the route).
100626e30fbbSDavid Greenman  */
100726e30fbbSDavid Greenman u_long	tcp_sendspace = 1024*16;
10083d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
10093d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
101026e30fbbSDavid Greenman u_long	tcp_recvspace = 1024*16;
10113d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
10123d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1013df8bae1dSRodney W. Grimes 
1014df8bae1dSRodney W. Grimes /*
1015df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1016df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1017df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1018df8bae1dSRodney W. Grimes  */
10190312fbe9SPoul-Henning Kamp static int
1020a29f300eSGarrett Wollman tcp_attach(so, p)
1021df8bae1dSRodney W. Grimes 	struct socket *so;
1022a29f300eSGarrett Wollman 	struct proc *p;
1023df8bae1dSRodney W. Grimes {
1024df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1025df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1026df8bae1dSRodney W. Grimes 	int error;
1027fb59c426SYoshinobu Inoue #ifdef INET6
1028fb59c426SYoshinobu Inoue 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1029fb59c426SYoshinobu Inoue #endif
1030df8bae1dSRodney W. Grimes 
1031df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1032df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1033df8bae1dSRodney W. Grimes 		if (error)
1034df8bae1dSRodney W. Grimes 			return (error);
1035df8bae1dSRodney W. Grimes 	}
1036a29f300eSGarrett Wollman 	error = in_pcballoc(so, &tcbinfo, p);
1037df8bae1dSRodney W. Grimes 	if (error)
1038df8bae1dSRodney W. Grimes 		return (error);
1039df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
10406a800098SYoshinobu Inoue #ifdef IPSEC
10416a800098SYoshinobu Inoue 	error = ipsec_init_policy(so, &inp->inp_sp);
10426a800098SYoshinobu Inoue 	if (error) {
1043fb59c426SYoshinobu Inoue #ifdef INET6
1044fb59c426SYoshinobu Inoue 		if (isipv6)
1045fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1046fb59c426SYoshinobu Inoue 		else
1047fb59c426SYoshinobu Inoue #endif
10486a800098SYoshinobu Inoue 		in_pcbdetach(inp);
10496a800098SYoshinobu Inoue 		return (error);
10506a800098SYoshinobu Inoue 	}
10516a800098SYoshinobu Inoue #endif /*IPSEC*/
1052fb59c426SYoshinobu Inoue #ifdef INET6
1053fb59c426SYoshinobu Inoue 	if (isipv6) {
1054fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1055fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1056fb59c426SYoshinobu Inoue 	}
1057fb59c426SYoshinobu Inoue 	else
1058fb59c426SYoshinobu Inoue #endif
1059cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1060df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1061df8bae1dSRodney W. Grimes 	if (tp == 0) {
1062df8bae1dSRodney W. Grimes 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1065fb59c426SYoshinobu Inoue #ifdef INET6
1066fb59c426SYoshinobu Inoue 		if (isipv6)
1067fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1068fb59c426SYoshinobu Inoue 		else
1069fb59c426SYoshinobu Inoue #endif
1070df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1071df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1072df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1073df8bae1dSRodney W. Grimes 	}
1074df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1075df8bae1dSRodney W. Grimes 	return (0);
1076df8bae1dSRodney W. Grimes }
1077df8bae1dSRodney W. Grimes 
1078df8bae1dSRodney W. Grimes /*
1079df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1080df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1081df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1082df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1083df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1084df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1085df8bae1dSRodney W. Grimes  */
10860312fbe9SPoul-Henning Kamp static struct tcpcb *
1087df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1088df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1089df8bae1dSRodney W. Grimes {
1090df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1091df8bae1dSRodney W. Grimes 
1092df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1093df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1094df8bae1dSRodney W. Grimes 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1095df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, 0);
1096df8bae1dSRodney W. Grimes 	else {
1097df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1098df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1099df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1100df8bae1dSRodney W. Grimes 		if (tp)
1101df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1102df8bae1dSRodney W. Grimes 	}
1103df8bae1dSRodney W. Grimes 	return (tp);
1104df8bae1dSRodney W. Grimes }
1105df8bae1dSRodney W. Grimes 
1106df8bae1dSRodney W. Grimes /*
1107df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1108df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1109df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1110df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1111df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1112df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1113df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1114df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1115df8bae1dSRodney W. Grimes  */
11160312fbe9SPoul-Henning Kamp static struct tcpcb *
1117df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1118df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1119df8bae1dSRodney W. Grimes {
1120df8bae1dSRodney W. Grimes 
1121df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1124df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1125df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1126df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1127df8bae1dSRodney W. Grimes 		break;
1128df8bae1dSRodney W. Grimes 
1129a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1130df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1131a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1132a0292f23SGarrett Wollman 		break;
1133a0292f23SGarrett Wollman 
1134df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1135df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1136df8bae1dSRodney W. Grimes 		break;
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1139df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1140df8bae1dSRodney W. Grimes 		break;
1141df8bae1dSRodney W. Grimes 	}
1142b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1143df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1144b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1145b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
11469b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
11479b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1148b6239c4aSAndras Olah 	}
1149df8bae1dSRodney W. Grimes 	return (tp);
1150df8bae1dSRodney W. Grimes }
1151a0292f23SGarrett Wollman 
1152