xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 33841545909f4a4ee94aa148b3a9cbcdc1abb02a)
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;
24333841545SHajimu UMEMOTO 	if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
244fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
245fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
246fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
247fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
248fb59c426SYoshinobu Inoue 
249fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
250fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
251fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
252fb59c426SYoshinobu Inoue 			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
253fb59c426SYoshinobu Inoue 			goto out;
254fb59c426SYoshinobu Inoue 		}
255fb59c426SYoshinobu Inoue 	}
256fb59c426SYoshinobu Inoue 	error = in6_pcbbind(inp, nam, p);
257fb59c426SYoshinobu Inoue 	if (error)
258fb59c426SYoshinobu Inoue 		goto out;
259fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
260fb59c426SYoshinobu Inoue }
261fb59c426SYoshinobu Inoue #endif /* INET6 */
262fb59c426SYoshinobu Inoue 
2632c37256eSGarrett Wollman /*
2642c37256eSGarrett Wollman  * Prepare to accept connections.
2652c37256eSGarrett Wollman  */
2662c37256eSGarrett Wollman static int
267a29f300eSGarrett Wollman tcp_usr_listen(struct socket *so, struct proc *p)
2682c37256eSGarrett Wollman {
2692c37256eSGarrett Wollman 	int s = splnet();
2702c37256eSGarrett Wollman 	int error = 0;
2712c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
2722c37256eSGarrett Wollman 	struct tcpcb *tp;
2732c37256eSGarrett Wollman 
2742c37256eSGarrett Wollman 	COMMON_START();
2752c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
27657bf258eSGarrett Wollman 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
2772c37256eSGarrett Wollman 	if (error == 0)
2782c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
2792c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
2802c37256eSGarrett Wollman }
2812c37256eSGarrett Wollman 
282fb59c426SYoshinobu Inoue #ifdef INET6
283fb59c426SYoshinobu Inoue static int
284fb59c426SYoshinobu Inoue tcp6_usr_listen(struct socket *so, struct proc *p)
285fb59c426SYoshinobu Inoue {
286fb59c426SYoshinobu Inoue 	int s = splnet();
287fb59c426SYoshinobu Inoue 	int error = 0;
288fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
289fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
290fb59c426SYoshinobu Inoue 
291fb59c426SYoshinobu Inoue 	COMMON_START();
292fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
293fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
29433841545SHajimu UMEMOTO 		if (ip6_mapped_addr_on &&
29533841545SHajimu UMEMOTO 		    (inp->inp_flags & IN6P_IPV6_V6ONLY) == 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 
36433841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
365fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
366fb59c426SYoshinobu Inoue 
36733841545SHajimu UMEMOTO 		if (!ip6_mapped_addr_on ||
36833841545SHajimu UMEMOTO 		    (inp->inp_flags & IN6P_IPV6_V6ONLY))
36933841545SHajimu UMEMOTO 			return(EINVAL);
37033841545SHajimu UMEMOTO 
371fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
372fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
373fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
374fb59c426SYoshinobu Inoue 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
375fb59c426SYoshinobu Inoue 			goto out;
376fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
377fb59c426SYoshinobu Inoue 		goto out;
378fb59c426SYoshinobu Inoue 	}
379fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
380fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
381fb59c426SYoshinobu Inoue 	if ((error = tcp6_connect(tp, nam, p)) != 0)
382fb59c426SYoshinobu Inoue 		goto out;
383fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
384fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
385fb59c426SYoshinobu Inoue }
386fb59c426SYoshinobu Inoue #endif /* INET6 */
387fb59c426SYoshinobu Inoue 
3882c37256eSGarrett Wollman /*
3892c37256eSGarrett Wollman  * Initiate disconnect from peer.
3902c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
3912c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
3922c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
3932c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
3942c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
3952c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
3962c37256eSGarrett Wollman  *
3972c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
3982c37256eSGarrett Wollman  */
3992c37256eSGarrett Wollman static int
4002c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
4012c37256eSGarrett Wollman {
4022c37256eSGarrett Wollman 	int s = splnet();
4032c37256eSGarrett Wollman 	int error = 0;
4042c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4052c37256eSGarrett Wollman 	struct tcpcb *tp;
4062c37256eSGarrett Wollman 
4072c37256eSGarrett Wollman 	COMMON_START();
4082c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4092c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4102c37256eSGarrett Wollman }
4112c37256eSGarrett Wollman 
4122c37256eSGarrett Wollman /*
4132c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4142c37256eSGarrett Wollman  * done at higher levels; just return the address
4152c37256eSGarrett Wollman  * of the peer, storing through addr.
4162c37256eSGarrett Wollman  */
4172c37256eSGarrett Wollman static int
41857bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4192c37256eSGarrett Wollman {
4202c37256eSGarrett Wollman 	int s = splnet();
4212c37256eSGarrett Wollman 	int error = 0;
4222c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4231db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
4241db24ffbSJonathan Lemon 	TCPDEBUG0;
4252c37256eSGarrett Wollman 
426c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
427c0647e0dSJonathan Lemon 		error = ECONNABORTED;
428c0647e0dSJonathan Lemon 		goto out;
429c0647e0dSJonathan Lemon 	}
4301db24ffbSJonathan Lemon 	if (inp == 0) {
4311db24ffbSJonathan Lemon 		splx(s);
4321db24ffbSJonathan Lemon 		return (EINVAL);
4331db24ffbSJonathan Lemon 	}
4341db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4351db24ffbSJonathan Lemon 	TCPDEBUG1();
436117bcae7SGarrett Wollman 	in_setpeeraddr(so, nam);
4372c37256eSGarrett Wollman 	COMMON_END(PRU_ACCEPT);
4382c37256eSGarrett Wollman }
4392c37256eSGarrett Wollman 
440fb59c426SYoshinobu Inoue #ifdef INET6
441fb59c426SYoshinobu Inoue static int
442fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
443fb59c426SYoshinobu Inoue {
444fb59c426SYoshinobu Inoue 	int s = splnet();
445fb59c426SYoshinobu Inoue 	int error = 0;
446fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
4471db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
4481db24ffbSJonathan Lemon 	TCPDEBUG0;
449fb59c426SYoshinobu Inoue 
450c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
451c0647e0dSJonathan Lemon 		error = ECONNABORTED;
452c0647e0dSJonathan Lemon 		goto out;
453c0647e0dSJonathan Lemon 	}
4541db24ffbSJonathan Lemon 	if (inp == 0) {
4551db24ffbSJonathan Lemon 		splx(s);
4561db24ffbSJonathan Lemon 		return (EINVAL);
4571db24ffbSJonathan Lemon 	}
4581db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4591db24ffbSJonathan Lemon 	TCPDEBUG1();
460fb59c426SYoshinobu Inoue 	in6_mapped_peeraddr(so, nam);
461fb59c426SYoshinobu Inoue 	COMMON_END(PRU_ACCEPT);
462fb59c426SYoshinobu Inoue }
463fb59c426SYoshinobu Inoue #endif /* INET6 */
4642c37256eSGarrett Wollman /*
4652c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
4662c37256eSGarrett Wollman  */
4672c37256eSGarrett Wollman static int
4682c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
4692c37256eSGarrett Wollman {
4702c37256eSGarrett Wollman 	int s = splnet();
4712c37256eSGarrett Wollman 	int error = 0;
4722c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4732c37256eSGarrett Wollman 	struct tcpcb *tp;
4742c37256eSGarrett Wollman 
4752c37256eSGarrett Wollman 	COMMON_START();
4762c37256eSGarrett Wollman 	socantsendmore(so);
4772c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
4782c37256eSGarrett Wollman 	if (tp)
4792c37256eSGarrett Wollman 		error = tcp_output(tp);
4802c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
4812c37256eSGarrett Wollman }
4822c37256eSGarrett Wollman 
4832c37256eSGarrett Wollman /*
4842c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
4852c37256eSGarrett Wollman  */
4862c37256eSGarrett Wollman static int
4872c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
4882c37256eSGarrett Wollman {
4892c37256eSGarrett Wollman 	int s = splnet();
4902c37256eSGarrett Wollman 	int error = 0;
4912c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4922c37256eSGarrett Wollman 	struct tcpcb *tp;
4932c37256eSGarrett Wollman 
4942c37256eSGarrett Wollman 	COMMON_START();
4952c37256eSGarrett Wollman 	tcp_output(tp);
4962c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
4972c37256eSGarrett Wollman }
4982c37256eSGarrett Wollman 
4992c37256eSGarrett Wollman /*
5002c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
5019c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
5029c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
5039c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
5049c9906e9SPeter Wemm  * generally are caller-frees.
5052c37256eSGarrett Wollman  */
5062c37256eSGarrett Wollman static int
50757bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
50857bf258eSGarrett Wollman 	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
5092c37256eSGarrett Wollman {
5102c37256eSGarrett Wollman 	int s = splnet();
5112c37256eSGarrett Wollman 	int error = 0;
5122c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
5132c37256eSGarrett Wollman 	struct tcpcb *tp;
514fb59c426SYoshinobu Inoue #ifdef INET6
515fb59c426SYoshinobu Inoue 	int isipv6;
516fb59c426SYoshinobu Inoue #endif
5179c9906e9SPeter Wemm 	TCPDEBUG0;
5182c37256eSGarrett Wollman 
5199c9906e9SPeter Wemm 	if (inp == NULL) {
5209c9906e9SPeter Wemm 		/*
5219c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
5229c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
5239c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
5249c9906e9SPeter Wemm 		 */
5259c9906e9SPeter Wemm 		if (m)
5269c9906e9SPeter Wemm 			m_freem(m);
5279c9906e9SPeter Wemm 		if (control)
5289c9906e9SPeter Wemm 			m_freem(control);
5299c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
53045d3a132SPeter Wemm 		tp = NULL;
53145d3a132SPeter Wemm 		TCPDEBUG1();
5329c9906e9SPeter Wemm 		goto out;
5339c9906e9SPeter Wemm 	}
534fb59c426SYoshinobu Inoue #ifdef INET6
535fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
536fb59c426SYoshinobu Inoue #endif /* INET6 */
5379c9906e9SPeter Wemm 	tp = intotcpcb(inp);
5389c9906e9SPeter Wemm 	TCPDEBUG1();
5399c9906e9SPeter Wemm 	if (control) {
5409c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
5419c9906e9SPeter Wemm 		if (control->m_len) {
5429c9906e9SPeter Wemm 			m_freem(control);
5432c37256eSGarrett Wollman 			if (m)
5442c37256eSGarrett Wollman 				m_freem(m);
545744f87eaSDavid Greenman 			error = EINVAL;
546744f87eaSDavid Greenman 			goto out;
5472c37256eSGarrett Wollman 		}
5489c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
5499c9906e9SPeter Wemm 	}
5502c37256eSGarrett Wollman 	if(!(flags & PRUS_OOB)) {
5512c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
5522c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
5532c37256eSGarrett Wollman 			/*
5542c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
5552c37256eSGarrett Wollman 			 * initialize window to default value, and
5562c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
5572c37256eSGarrett Wollman 			 * MSS.
5582c37256eSGarrett Wollman 			 */
559fb59c426SYoshinobu Inoue #ifdef INET6
560fb59c426SYoshinobu Inoue 			if (isipv6)
561fb59c426SYoshinobu Inoue 				error = tcp6_connect(tp, nam, p);
562fb59c426SYoshinobu Inoue 			else
563fb59c426SYoshinobu Inoue #endif /* INET6 */
564a29f300eSGarrett Wollman 			error = tcp_connect(tp, nam, p);
5652c37256eSGarrett Wollman 			if (error)
5662c37256eSGarrett Wollman 				goto out;
5672c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
5682c37256eSGarrett Wollman 			tcp_mss(tp, -1);
5692c37256eSGarrett Wollman 		}
5702c37256eSGarrett Wollman 
5712c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
5722c37256eSGarrett Wollman 			/*
5732c37256eSGarrett Wollman 			 * Close the send side of the connection after
5742c37256eSGarrett Wollman 			 * the data is sent.
5752c37256eSGarrett Wollman 			 */
5762c37256eSGarrett Wollman 			socantsendmore(so);
5772c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
5782c37256eSGarrett Wollman 		}
579b0acefa8SBill Fenner 		if (tp != NULL) {
580b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
581b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
5822c37256eSGarrett Wollman 			error = tcp_output(tp);
583b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
584b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
585b0acefa8SBill Fenner 		}
5862c37256eSGarrett Wollman 	} else {
5872c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
5882c37256eSGarrett Wollman 			m_freem(m);
5892c37256eSGarrett Wollman 			error = ENOBUFS;
5902c37256eSGarrett Wollman 			goto out;
5912c37256eSGarrett Wollman 		}
5922c37256eSGarrett Wollman 		/*
5932c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
5942c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
5952c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
5962c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
5972c37256eSGarrett Wollman 		 * of data past the urgent section.
5982c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
5992c37256eSGarrett Wollman 		 */
6002c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
601ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
602ef53690bSGarrett Wollman 			/*
603ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
604ef53690bSGarrett Wollman 			 * initialize window to default value, and
605ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
606ef53690bSGarrett Wollman 			 * MSS.
607ef53690bSGarrett Wollman 			 */
608fb59c426SYoshinobu Inoue #ifdef INET6
609fb59c426SYoshinobu Inoue 			if (isipv6)
610fb59c426SYoshinobu Inoue 				error = tcp6_connect(tp, nam, p);
611fb59c426SYoshinobu Inoue 			else
612fb59c426SYoshinobu Inoue #endif /* INET6 */
613a29f300eSGarrett Wollman 			error = tcp_connect(tp, nam, p);
614ef53690bSGarrett Wollman 			if (error)
615ef53690bSGarrett Wollman 				goto out;
616ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
617ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
618ef53690bSGarrett Wollman 		}
6192c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
6202c37256eSGarrett Wollman 		tp->t_force = 1;
6212c37256eSGarrett Wollman 		error = tcp_output(tp);
6222c37256eSGarrett Wollman 		tp->t_force = 0;
6232c37256eSGarrett Wollman 	}
6242c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
6252c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
6262c37256eSGarrett Wollman }
6272c37256eSGarrett Wollman 
6282c37256eSGarrett Wollman /*
6292c37256eSGarrett Wollman  * Abort the TCP.
6302c37256eSGarrett Wollman  */
6312c37256eSGarrett Wollman static int
6322c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
6332c37256eSGarrett Wollman {
6342c37256eSGarrett Wollman 	int s = splnet();
6352c37256eSGarrett Wollman 	int error = 0;
6362c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6372c37256eSGarrett Wollman 	struct tcpcb *tp;
6382c37256eSGarrett Wollman 
6392c37256eSGarrett Wollman 	COMMON_START();
6402c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
6412c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
6422c37256eSGarrett Wollman }
6432c37256eSGarrett Wollman 
6442c37256eSGarrett Wollman /*
6452c37256eSGarrett Wollman  * Receive out-of-band data.
6462c37256eSGarrett Wollman  */
6472c37256eSGarrett Wollman static int
6482c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
6492c37256eSGarrett Wollman {
6502c37256eSGarrett Wollman 	int s = splnet();
6512c37256eSGarrett Wollman 	int error = 0;
6522c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6532c37256eSGarrett Wollman 	struct tcpcb *tp;
6542c37256eSGarrett Wollman 
6552c37256eSGarrett Wollman 	COMMON_START();
6562c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
6572c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
6582c37256eSGarrett Wollman 	    so->so_options & SO_OOBINLINE ||
6592c37256eSGarrett Wollman 	    tp->t_oobflags & TCPOOB_HADDATA) {
6602c37256eSGarrett Wollman 		error = EINVAL;
6612c37256eSGarrett Wollman 		goto out;
6622c37256eSGarrett Wollman 	}
6632c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
6642c37256eSGarrett Wollman 		error = EWOULDBLOCK;
6652c37256eSGarrett Wollman 		goto out;
6662c37256eSGarrett Wollman 	}
6672c37256eSGarrett Wollman 	m->m_len = 1;
6682c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
6692c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
6702c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
6712c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
6722c37256eSGarrett Wollman }
6732c37256eSGarrett Wollman 
6742c37256eSGarrett Wollman /* xxx - should be const */
6752c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
6762c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
677117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
678117bcae7SGarrett Wollman 	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
679117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
680f8f6cbbaSPeter Wemm 	in_setsockaddr, sosend, soreceive, sopoll
6812c37256eSGarrett Wollman };
682df8bae1dSRodney W. Grimes 
683fb59c426SYoshinobu Inoue #ifdef INET6
684fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
685fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
686fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
687fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
688fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
689fb59c426SYoshinobu Inoue 	in6_mapped_sockaddr, sosend, soreceive, sopoll
690fb59c426SYoshinobu Inoue };
691fb59c426SYoshinobu Inoue #endif /* INET6 */
692fb59c426SYoshinobu Inoue 
693a0292f23SGarrett Wollman /*
694a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
695a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
696a0292f23SGarrett Wollman  * port number if needed.  Call in_pcbladdr to do the routing and to choose
697a0292f23SGarrett Wollman  * a local host address (interface).  If there is an existing incarnation
698a0292f23SGarrett Wollman  * of the same connection in TIME-WAIT state and if the remote host was
699a0292f23SGarrett Wollman  * sending CC options and if the connection duration was < MSL, then
700a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
701a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
702a0292f23SGarrett Wollman  */
7030312fbe9SPoul-Henning Kamp static int
704a29f300eSGarrett Wollman tcp_connect(tp, nam, p)
705a0292f23SGarrett Wollman 	register struct tcpcb *tp;
70657bf258eSGarrett Wollman 	struct sockaddr *nam;
707a29f300eSGarrett Wollman 	struct proc *p;
708a0292f23SGarrett Wollman {
709a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
710a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
711a0292f23SGarrett Wollman 	struct tcpcb *otp;
71257bf258eSGarrett Wollman 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
713a0292f23SGarrett Wollman 	struct sockaddr_in *ifaddr;
714a45d2726SAndras Olah 	struct rmxp_tao *taop;
715a45d2726SAndras Olah 	struct rmxp_tao tao_noncached;
716c3229e05SDavid Greenman 	int error;
717a0292f23SGarrett Wollman 
718a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
71957bf258eSGarrett Wollman 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
720a0292f23SGarrett Wollman 		if (error)
721a0292f23SGarrett Wollman 			return error;
722a0292f23SGarrett Wollman 	}
723a0292f23SGarrett Wollman 
724a0292f23SGarrett Wollman 	/*
725a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
726a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
727a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
728a0292f23SGarrett Wollman 	 */
729a0292f23SGarrett Wollman 	error = in_pcbladdr(inp, nam, &ifaddr);
730d3628763SRodney W. Grimes 	if (error)
731d3628763SRodney W. Grimes 		return error;
732c3229e05SDavid Greenman 	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
733a0292f23SGarrett Wollman 	    sin->sin_addr, sin->sin_port,
734a0292f23SGarrett Wollman 	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
735a0292f23SGarrett Wollman 						: ifaddr->sin_addr,
736cfa1ca9dSYoshinobu Inoue 	    inp->inp_lport,  0, NULL);
737a0292f23SGarrett Wollman 	if (oinp) {
738a0292f23SGarrett Wollman 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
739a0292f23SGarrett Wollman 		otp->t_state == TCPS_TIME_WAIT &&
7409b8b58e0SJonathan Lemon 		    (ticks - otp->t_starttime) < tcp_msl &&
741a0292f23SGarrett Wollman 		    (otp->t_flags & TF_RCVD_CC))
742a0292f23SGarrett Wollman 			otp = tcp_close(otp);
743a0292f23SGarrett Wollman 		else
744a0292f23SGarrett Wollman 			return EADDRINUSE;
745a0292f23SGarrett Wollman 	}
746a0292f23SGarrett Wollman 	if (inp->inp_laddr.s_addr == INADDR_ANY)
747a0292f23SGarrett Wollman 		inp->inp_laddr = ifaddr->sin_addr;
748a0292f23SGarrett Wollman 	inp->inp_faddr = sin->sin_addr;
749a0292f23SGarrett Wollman 	inp->inp_fport = sin->sin_port;
75015bd2b43SDavid Greenman 	in_pcbrehash(inp);
751a0292f23SGarrett Wollman 
752a0292f23SGarrett Wollman 	tp->t_template = tcp_template(tp);
753a0292f23SGarrett Wollman 	if (tp->t_template == 0) {
754a0292f23SGarrett Wollman 		in_pcbdisconnect(inp);
755a0292f23SGarrett Wollman 		return ENOBUFS;
756a0292f23SGarrett Wollman 	}
757a0292f23SGarrett Wollman 
758a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
759a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
760a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
761a0292f23SGarrett Wollman 		tp->request_r_scale++;
762a0292f23SGarrett Wollman 
763a0292f23SGarrett Wollman 	soisconnecting(so);
764a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
765a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
7669b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
767f0a04f3fSKris Kennaway 	tp->iss = tcp_rndiss_next();
768a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
769a45d2726SAndras Olah 
770a45d2726SAndras Olah 	/*
771a45d2726SAndras Olah 	 * Generate a CC value for this connection and
772a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
773a45d2726SAndras Olah 	 */
774a45d2726SAndras Olah 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
775a45d2726SAndras Olah 		taop = &tao_noncached;
776a45d2726SAndras Olah 		bzero(taop, sizeof(*taop));
777a45d2726SAndras Olah 	}
778a45d2726SAndras Olah 
779a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
780a45d2726SAndras Olah 	if (taop->tao_ccsent != 0 &&
781a45d2726SAndras Olah 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
782a45d2726SAndras Olah 		taop->tao_ccsent = tp->cc_send;
783a45d2726SAndras Olah 	} else {
784a45d2726SAndras Olah 		taop->tao_ccsent = 0;
785a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
786a45d2726SAndras Olah 	}
787a0292f23SGarrett Wollman 
788a0292f23SGarrett Wollman 	return 0;
789a0292f23SGarrett Wollman }
790a0292f23SGarrett Wollman 
791fb59c426SYoshinobu Inoue #ifdef INET6
792fb59c426SYoshinobu Inoue static int
793fb59c426SYoshinobu Inoue tcp6_connect(tp, nam, p)
794fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
795fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
796fb59c426SYoshinobu Inoue 	struct proc *p;
797fb59c426SYoshinobu Inoue {
798fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
799fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
800fb59c426SYoshinobu Inoue 	struct tcpcb *otp;
801fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
802fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
803fb59c426SYoshinobu Inoue 	struct rmxp_tao *taop;
804fb59c426SYoshinobu Inoue 	struct rmxp_tao tao_noncached;
805fb59c426SYoshinobu Inoue 	int error;
806fb59c426SYoshinobu Inoue 
807fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
808fb59c426SYoshinobu Inoue 		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
809fb59c426SYoshinobu Inoue 		if (error)
810fb59c426SYoshinobu Inoue 			return error;
811fb59c426SYoshinobu Inoue 	}
812fb59c426SYoshinobu Inoue 
813fb59c426SYoshinobu Inoue 	/*
814fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
815fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
816fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
817fb59c426SYoshinobu Inoue 	 */
818fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
819fb59c426SYoshinobu Inoue 	if (error)
820fb59c426SYoshinobu Inoue 		return error;
821fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
822fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
823fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
824fb59c426SYoshinobu Inoue 				  ? addr6
825fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
826fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
827fb59c426SYoshinobu Inoue 	if (oinp) {
828fb59c426SYoshinobu Inoue 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
829fb59c426SYoshinobu Inoue 		    otp->t_state == TCPS_TIME_WAIT &&
830fb59c426SYoshinobu Inoue 		    (ticks - otp->t_starttime) < tcp_msl &&
831fb59c426SYoshinobu Inoue 		    (otp->t_flags & TF_RCVD_CC))
832fb59c426SYoshinobu Inoue 			otp = tcp_close(otp);
833fb59c426SYoshinobu Inoue 		else
834fb59c426SYoshinobu Inoue 			return EADDRINUSE;
835fb59c426SYoshinobu Inoue 	}
836fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
837fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
838fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
839fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
840fb59c426SYoshinobu Inoue 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
841fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
842fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
843fb59c426SYoshinobu Inoue 
844fb59c426SYoshinobu Inoue 	tp->t_template = tcp_template(tp);
845fb59c426SYoshinobu Inoue 	if (tp->t_template == 0) {
846fb59c426SYoshinobu Inoue 		in6_pcbdisconnect(inp);
847fb59c426SYoshinobu Inoue 		return ENOBUFS;
848fb59c426SYoshinobu Inoue 	}
849fb59c426SYoshinobu Inoue 
850fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
851fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
852fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
853fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
854fb59c426SYoshinobu Inoue 
855fb59c426SYoshinobu Inoue 	soisconnecting(so);
856fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
857fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
858fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
859f0a04f3fSKris Kennaway 	tp->iss = tcp_rndiss_next();
860fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
861fb59c426SYoshinobu Inoue 
862fb59c426SYoshinobu Inoue 	/*
863fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
864fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
865fb59c426SYoshinobu Inoue 	 */
866fb59c426SYoshinobu Inoue 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
867fb59c426SYoshinobu Inoue 		taop = &tao_noncached;
868fb59c426SYoshinobu Inoue 		bzero(taop, sizeof(*taop));
869fb59c426SYoshinobu Inoue 	}
870fb59c426SYoshinobu Inoue 
871fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
872fb59c426SYoshinobu Inoue 	if (taop->tao_ccsent != 0 &&
873fb59c426SYoshinobu Inoue 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
874fb59c426SYoshinobu Inoue 		taop->tao_ccsent = tp->cc_send;
875fb59c426SYoshinobu Inoue 	} else {
876fb59c426SYoshinobu Inoue 		taop->tao_ccsent = 0;
877fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
878fb59c426SYoshinobu Inoue 	}
879fb59c426SYoshinobu Inoue 
880fb59c426SYoshinobu Inoue 	return 0;
881fb59c426SYoshinobu Inoue }
882fb59c426SYoshinobu Inoue #endif /* INET6 */
883fb59c426SYoshinobu Inoue 
884cfe8b629SGarrett Wollman /*
885cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
886cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
887cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
888cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
889cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
890cfe8b629SGarrett Wollman  */
891df8bae1dSRodney W. Grimes int
892cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
893df8bae1dSRodney W. Grimes 	struct socket *so;
894cfe8b629SGarrett Wollman 	struct sockopt *sopt;
895df8bae1dSRodney W. Grimes {
896cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
897df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
898cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
899df8bae1dSRodney W. Grimes 
900cfe8b629SGarrett Wollman 	error = 0;
901cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
902df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
903df8bae1dSRodney W. Grimes 	if (inp == NULL) {
904df8bae1dSRodney W. Grimes 		splx(s);
905df8bae1dSRodney W. Grimes 		return (ECONNRESET);
906df8bae1dSRodney W. Grimes 	}
907cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
908fb59c426SYoshinobu Inoue #ifdef INET6
909fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
910fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
911fb59c426SYoshinobu Inoue 		else
912fb59c426SYoshinobu Inoue #endif /* INET6 */
913cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
914df8bae1dSRodney W. Grimes 		splx(s);
915df8bae1dSRodney W. Grimes 		return (error);
916df8bae1dSRodney W. Grimes 	}
917df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
918df8bae1dSRodney W. Grimes 
919cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
920cfe8b629SGarrett Wollman 	case SOPT_SET:
921cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
922df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
923cfe8b629SGarrett Wollman 		case TCP_NOOPT:
924cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
925cfe8b629SGarrett Wollman 					    sizeof optval);
926cfe8b629SGarrett Wollman 			if (error)
927cfe8b629SGarrett Wollman 				break;
928cfe8b629SGarrett Wollman 
929cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
930cfe8b629SGarrett Wollman 			case TCP_NODELAY:
931cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
932cfe8b629SGarrett Wollman 				break;
933cfe8b629SGarrett Wollman 			case TCP_NOOPT:
934cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
935cfe8b629SGarrett Wollman 				break;
936cfe8b629SGarrett Wollman 			default:
937cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
938cfe8b629SGarrett Wollman 				break;
939cfe8b629SGarrett Wollman 			}
940cfe8b629SGarrett Wollman 
941cfe8b629SGarrett Wollman 			if (optval)
942cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
943df8bae1dSRodney W. Grimes 			else
944cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
945df8bae1dSRodney W. Grimes 			break;
946df8bae1dSRodney W. Grimes 
947007581c0SJonathan Lemon 		case TCP_NOPUSH:
948007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
949007581c0SJonathan Lemon 					    sizeof optval);
950007581c0SJonathan Lemon 			if (error)
951007581c0SJonathan Lemon 				break;
952007581c0SJonathan Lemon 
953007581c0SJonathan Lemon 			if (optval)
954007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
955007581c0SJonathan Lemon 			else {
956007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
957007581c0SJonathan Lemon 				error = tcp_output(tp);
958007581c0SJonathan Lemon 			}
959007581c0SJonathan Lemon 			break;
960007581c0SJonathan Lemon 
961df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
962cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
963cfe8b629SGarrett Wollman 					    sizeof optval);
964cfe8b629SGarrett Wollman 			if (error)
965df8bae1dSRodney W. Grimes 				break;
966df8bae1dSRodney W. Grimes 
967cfe8b629SGarrett Wollman 			if (optval > 0 && optval <= tp->t_maxseg)
968cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
969a0292f23SGarrett Wollman 			else
970a0292f23SGarrett Wollman 				error = EINVAL;
971a0292f23SGarrett Wollman 			break;
972a0292f23SGarrett Wollman 
973df8bae1dSRodney W. Grimes 		default:
974df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
975df8bae1dSRodney W. Grimes 			break;
976df8bae1dSRodney W. Grimes 		}
977df8bae1dSRodney W. Grimes 		break;
978df8bae1dSRodney W. Grimes 
979cfe8b629SGarrett Wollman 	case SOPT_GET:
980cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
981df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
982cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
983df8bae1dSRodney W. Grimes 			break;
984df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
985cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
986df8bae1dSRodney W. Grimes 			break;
987a0292f23SGarrett Wollman 		case TCP_NOOPT:
988cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
989a0292f23SGarrett Wollman 			break;
990a0292f23SGarrett Wollman 		case TCP_NOPUSH:
991cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
992a0292f23SGarrett Wollman 			break;
993df8bae1dSRodney W. Grimes 		default:
994df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
995df8bae1dSRodney W. Grimes 			break;
996df8bae1dSRodney W. Grimes 		}
997cfe8b629SGarrett Wollman 		if (error == 0)
998cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
999df8bae1dSRodney W. Grimes 		break;
1000df8bae1dSRodney W. Grimes 	}
1001df8bae1dSRodney W. Grimes 	splx(s);
1002df8bae1dSRodney W. Grimes 	return (error);
1003df8bae1dSRodney W. Grimes }
1004df8bae1dSRodney W. Grimes 
100526e30fbbSDavid Greenman /*
100626e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
100726e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
100826e30fbbSDavid Greenman  * be set by the route).
100926e30fbbSDavid Greenman  */
101026e30fbbSDavid Greenman u_long	tcp_sendspace = 1024*16;
10113d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
10123d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
101326e30fbbSDavid Greenman u_long	tcp_recvspace = 1024*16;
10143d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
10153d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1016df8bae1dSRodney W. Grimes 
1017df8bae1dSRodney W. Grimes /*
1018df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1019df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1020df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1021df8bae1dSRodney W. Grimes  */
10220312fbe9SPoul-Henning Kamp static int
1023a29f300eSGarrett Wollman tcp_attach(so, p)
1024df8bae1dSRodney W. Grimes 	struct socket *so;
1025a29f300eSGarrett Wollman 	struct proc *p;
1026df8bae1dSRodney W. Grimes {
1027df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1028df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1029df8bae1dSRodney W. Grimes 	int error;
1030fb59c426SYoshinobu Inoue #ifdef INET6
1031fb59c426SYoshinobu Inoue 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1032fb59c426SYoshinobu Inoue #endif
1033df8bae1dSRodney W. Grimes 
1034df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1035df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1036df8bae1dSRodney W. Grimes 		if (error)
1037df8bae1dSRodney W. Grimes 			return (error);
1038df8bae1dSRodney W. Grimes 	}
1039a29f300eSGarrett Wollman 	error = in_pcballoc(so, &tcbinfo, p);
1040df8bae1dSRodney W. Grimes 	if (error)
1041df8bae1dSRodney W. Grimes 		return (error);
1042df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
10436a800098SYoshinobu Inoue #ifdef IPSEC
10446a800098SYoshinobu Inoue 	error = ipsec_init_policy(so, &inp->inp_sp);
10456a800098SYoshinobu Inoue 	if (error) {
1046fb59c426SYoshinobu Inoue #ifdef INET6
1047fb59c426SYoshinobu Inoue 		if (isipv6)
1048fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1049fb59c426SYoshinobu Inoue 		else
1050fb59c426SYoshinobu Inoue #endif
10516a800098SYoshinobu Inoue 		in_pcbdetach(inp);
10526a800098SYoshinobu Inoue 		return (error);
10536a800098SYoshinobu Inoue 	}
10546a800098SYoshinobu Inoue #endif /*IPSEC*/
1055fb59c426SYoshinobu Inoue #ifdef INET6
1056fb59c426SYoshinobu Inoue 	if (isipv6) {
1057fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1058fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1059fb59c426SYoshinobu Inoue 	}
1060fb59c426SYoshinobu Inoue 	else
1061fb59c426SYoshinobu Inoue #endif
1062cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1063df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1064df8bae1dSRodney W. Grimes 	if (tp == 0) {
1065df8bae1dSRodney W. Grimes 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1068fb59c426SYoshinobu Inoue #ifdef INET6
1069fb59c426SYoshinobu Inoue 		if (isipv6)
1070fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1071fb59c426SYoshinobu Inoue 		else
1072fb59c426SYoshinobu Inoue #endif
1073df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1074df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1075df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1076df8bae1dSRodney W. Grimes 	}
1077df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1078df8bae1dSRodney W. Grimes 	return (0);
1079df8bae1dSRodney W. Grimes }
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes /*
1082df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1083df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1084df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1085df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1086df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1087df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1088df8bae1dSRodney W. Grimes  */
10890312fbe9SPoul-Henning Kamp static struct tcpcb *
1090df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1091df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1092df8bae1dSRodney W. Grimes {
1093df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1094df8bae1dSRodney W. Grimes 
1095df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1096df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1097df8bae1dSRodney W. Grimes 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1098df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, 0);
1099df8bae1dSRodney W. Grimes 	else {
1100df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1101df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1102df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1103df8bae1dSRodney W. Grimes 		if (tp)
1104df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1105df8bae1dSRodney W. Grimes 	}
1106df8bae1dSRodney W. Grimes 	return (tp);
1107df8bae1dSRodney W. Grimes }
1108df8bae1dSRodney W. Grimes 
1109df8bae1dSRodney W. Grimes /*
1110df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1111df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1112df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1113df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1114df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1115df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1116df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1117df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1118df8bae1dSRodney W. Grimes  */
11190312fbe9SPoul-Henning Kamp static struct tcpcb *
1120df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1121df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1122df8bae1dSRodney W. Grimes {
1123df8bae1dSRodney W. Grimes 
1124df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1127df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1128df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1129df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1130df8bae1dSRodney W. Grimes 		break;
1131df8bae1dSRodney W. Grimes 
1132a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1133df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1134a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1135a0292f23SGarrett Wollman 		break;
1136a0292f23SGarrett Wollman 
1137df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1138df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1139df8bae1dSRodney W. Grimes 		break;
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1142df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1143df8bae1dSRodney W. Grimes 		break;
1144df8bae1dSRodney W. Grimes 	}
1145b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1146df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1147b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1148b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
11499b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
11509b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1151b6239c4aSAndras Olah 	}
1152df8bae1dSRodney W. Grimes 	return (tp);
1153df8bae1dSRodney W. Grimes }
1154a0292f23SGarrett Wollman 
1155