xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 007581c0d8e5cb76c655a68b458940d410b680df)
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>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes #include <net/if.h>
54df8bae1dSRodney W. Grimes #include <net/route.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <netinet/in.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
58fb59c426SYoshinobu Inoue #ifdef INET6
59fb59c426SYoshinobu Inoue #include <netinet/ip6.h>
60fb59c426SYoshinobu Inoue #endif
61df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
62fb59c426SYoshinobu Inoue #ifdef INET6
63fb59c426SYoshinobu Inoue #include <netinet6/in6_pcb.h>
64fb59c426SYoshinobu Inoue #endif
65b5e8ce9fSBruce Evans #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
67fb59c426SYoshinobu Inoue #ifdef INET6
68fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
69fb59c426SYoshinobu Inoue #endif
70df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
71df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
72df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
73df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
74df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
75df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
76610ee2f9SDavid Greenman #ifdef TCPDEBUG
77df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
78610ee2f9SDavid Greenman #endif
79df8bae1dSRodney W. Grimes 
806a800098SYoshinobu Inoue #ifdef IPSEC
816a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
826a800098SYoshinobu Inoue #endif /*IPSEC*/
836a800098SYoshinobu Inoue 
84df8bae1dSRodney W. Grimes /*
85df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
86df8bae1dSRodney W. Grimes  */
87117bcae7SGarrett Wollman extern	char *tcpstates[];	/* XXX ??? */
88df8bae1dSRodney W. Grimes 
89a29f300eSGarrett Wollman static int	tcp_attach __P((struct socket *, struct proc *));
9057bf258eSGarrett Wollman static int	tcp_connect __P((struct tcpcb *, struct sockaddr *,
91a29f300eSGarrett Wollman 				 struct proc *));
92fb59c426SYoshinobu Inoue #ifdef INET6
93fb59c426SYoshinobu Inoue static int	tcp6_connect __P((struct tcpcb *, struct sockaddr *,
94fb59c426SYoshinobu Inoue 				 struct proc *));
95fb59c426SYoshinobu Inoue #endif /* INET6 */
960312fbe9SPoul-Henning Kamp static struct tcpcb *
970312fbe9SPoul-Henning Kamp 		tcp_disconnect __P((struct tcpcb *));
980312fbe9SPoul-Henning Kamp static struct tcpcb *
990312fbe9SPoul-Henning Kamp 		tcp_usrclosed __P((struct tcpcb *));
1002c37256eSGarrett Wollman 
1012c37256eSGarrett Wollman #ifdef TCPDEBUG
1022c37256eSGarrett Wollman #define	TCPDEBUG0	int ostate
1032c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
104af7a2999SDavid Greenman #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
105fb59c426SYoshinobu Inoue 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1062c37256eSGarrett Wollman #else
1072c37256eSGarrett Wollman #define	TCPDEBUG0
1082c37256eSGarrett Wollman #define	TCPDEBUG1()
1092c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1102c37256eSGarrett Wollman #endif
1112c37256eSGarrett Wollman 
1122c37256eSGarrett Wollman /*
1132c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1142c37256eSGarrett Wollman  * and an internet control block.
1152c37256eSGarrett Wollman  */
1162c37256eSGarrett Wollman static int
117a29f300eSGarrett Wollman tcp_usr_attach(struct socket *so, int proto, struct proc *p)
1182c37256eSGarrett Wollman {
1192c37256eSGarrett Wollman 	int s = splnet();
1202c37256eSGarrett Wollman 	int error;
1212c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1222c37256eSGarrett Wollman 	struct tcpcb *tp = 0;
1232c37256eSGarrett Wollman 	TCPDEBUG0;
1242c37256eSGarrett Wollman 
1252c37256eSGarrett Wollman 	TCPDEBUG1();
1262c37256eSGarrett Wollman 	if (inp) {
1272c37256eSGarrett Wollman 		error = EISCONN;
1282c37256eSGarrett Wollman 		goto out;
1292c37256eSGarrett Wollman 	}
1302c37256eSGarrett Wollman 
131a29f300eSGarrett Wollman 	error = tcp_attach(so, p);
1322c37256eSGarrett Wollman 	if (error)
1332c37256eSGarrett Wollman 		goto out;
1342c37256eSGarrett Wollman 
1352c37256eSGarrett Wollman 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1363879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
1372c37256eSGarrett Wollman 	tp = sototcpcb(so);
1382c37256eSGarrett Wollman out:
1392c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1402c37256eSGarrett Wollman 	splx(s);
1412c37256eSGarrett Wollman 	return error;
1422c37256eSGarrett Wollman }
1432c37256eSGarrett Wollman 
1442c37256eSGarrett Wollman /*
1452c37256eSGarrett Wollman  * pru_detach() detaches the TCP protocol from the socket.
1462c37256eSGarrett Wollman  * If the protocol state is non-embryonic, then can't
1472c37256eSGarrett Wollman  * do this directly: have to initiate a pru_disconnect(),
1482c37256eSGarrett Wollman  * which may finish later; embryonic TCB's can just
1492c37256eSGarrett Wollman  * be discarded here.
1502c37256eSGarrett Wollman  */
1512c37256eSGarrett Wollman static int
1522c37256eSGarrett Wollman tcp_usr_detach(struct socket *so)
1532c37256eSGarrett Wollman {
1542c37256eSGarrett Wollman 	int s = splnet();
1552c37256eSGarrett Wollman 	int error = 0;
1562c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1572c37256eSGarrett Wollman 	struct tcpcb *tp;
1582c37256eSGarrett Wollman 	TCPDEBUG0;
1592c37256eSGarrett Wollman 
1602c37256eSGarrett Wollman 	if (inp == 0) {
1612c37256eSGarrett Wollman 		splx(s);
1622c37256eSGarrett Wollman 		return EINVAL;	/* XXX */
1632c37256eSGarrett Wollman 	}
1642c37256eSGarrett Wollman 	tp = intotcpcb(inp);
1652c37256eSGarrett Wollman 	TCPDEBUG1();
1662c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
1672c37256eSGarrett Wollman 
1682c37256eSGarrett Wollman 	TCPDEBUG2(PRU_DETACH);
1692c37256eSGarrett Wollman 	splx(s);
1702c37256eSGarrett Wollman 	return error;
1712c37256eSGarrett Wollman }
1722c37256eSGarrett Wollman 
1732c37256eSGarrett Wollman #define	COMMON_START()	TCPDEBUG0; \
1742c37256eSGarrett Wollman 			do { \
1752c37256eSGarrett Wollman 				     if (inp == 0) { \
1762c37256eSGarrett Wollman 					     splx(s); \
1772c37256eSGarrett Wollman 					     return EINVAL; \
1782c37256eSGarrett Wollman 				     } \
1792c37256eSGarrett Wollman 				     tp = intotcpcb(inp); \
1802c37256eSGarrett Wollman 				     TCPDEBUG1(); \
1812c37256eSGarrett Wollman 		     } while(0)
1822c37256eSGarrett Wollman 
1832c37256eSGarrett Wollman #define COMMON_END(req)	out: TCPDEBUG2(req); splx(s); return error; goto out
1842c37256eSGarrett Wollman 
1852c37256eSGarrett Wollman 
1862c37256eSGarrett Wollman /*
1872c37256eSGarrett Wollman  * Give the socket an address.
1882c37256eSGarrett Wollman  */
1892c37256eSGarrett Wollman static int
19057bf258eSGarrett Wollman tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1912c37256eSGarrett Wollman {
1922c37256eSGarrett Wollman 	int s = splnet();
1932c37256eSGarrett Wollman 	int error = 0;
1942c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1952c37256eSGarrett Wollman 	struct tcpcb *tp;
1962c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
1972c37256eSGarrett Wollman 
1982c37256eSGarrett Wollman 	COMMON_START();
1992c37256eSGarrett Wollman 
2002c37256eSGarrett Wollman 	/*
2012c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2022c37256eSGarrett Wollman 	 * to them.
2032c37256eSGarrett Wollman 	 */
20457bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
2052c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
2062c37256eSGarrett Wollman 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
2072c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
2082c37256eSGarrett Wollman 		goto out;
2092c37256eSGarrett Wollman 	}
210a29f300eSGarrett Wollman 	error = in_pcbbind(inp, nam, p);
2112c37256eSGarrett Wollman 	if (error)
2122c37256eSGarrett Wollman 		goto out;
2132c37256eSGarrett Wollman 	COMMON_END(PRU_BIND);
2142c37256eSGarrett Wollman 
2152c37256eSGarrett Wollman }
2162c37256eSGarrett Wollman 
217fb59c426SYoshinobu Inoue #ifdef INET6
218fb59c426SYoshinobu Inoue static int
219fb59c426SYoshinobu Inoue tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
220fb59c426SYoshinobu Inoue {
221fb59c426SYoshinobu Inoue 	int s = splnet();
222fb59c426SYoshinobu Inoue 	int error = 0;
223fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
224fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
225fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
226fb59c426SYoshinobu Inoue 
227fb59c426SYoshinobu Inoue 	COMMON_START();
228fb59c426SYoshinobu Inoue 
229fb59c426SYoshinobu Inoue 	/*
230fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
231fb59c426SYoshinobu Inoue 	 * to them.
232fb59c426SYoshinobu Inoue 	 */
233fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
234fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
235fb59c426SYoshinobu Inoue 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
236fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
237fb59c426SYoshinobu Inoue 		goto out;
238fb59c426SYoshinobu Inoue 	}
239fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
240fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
241fdaf052eSYoshinobu Inoue 	if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
242fb59c426SYoshinobu Inoue 
243fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
244fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
245fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
246fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
247fb59c426SYoshinobu Inoue 
248fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
249fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
250fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
251fb59c426SYoshinobu Inoue 			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
252fb59c426SYoshinobu Inoue 			goto out;
253fb59c426SYoshinobu Inoue 		}
254fb59c426SYoshinobu Inoue 	}
255fb59c426SYoshinobu Inoue 	error = in6_pcbbind(inp, nam, p);
256fb59c426SYoshinobu Inoue 	if (error)
257fb59c426SYoshinobu Inoue 		goto out;
258fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
259fb59c426SYoshinobu Inoue }
260fb59c426SYoshinobu Inoue #endif /* INET6 */
261fb59c426SYoshinobu Inoue 
2622c37256eSGarrett Wollman /*
2632c37256eSGarrett Wollman  * Prepare to accept connections.
2642c37256eSGarrett Wollman  */
2652c37256eSGarrett Wollman static int
266a29f300eSGarrett Wollman tcp_usr_listen(struct socket *so, struct proc *p)
2672c37256eSGarrett Wollman {
2682c37256eSGarrett Wollman 	int s = splnet();
2692c37256eSGarrett Wollman 	int error = 0;
2702c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
2712c37256eSGarrett Wollman 	struct tcpcb *tp;
2722c37256eSGarrett Wollman 
2732c37256eSGarrett Wollman 	COMMON_START();
2742c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
27557bf258eSGarrett Wollman 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
2762c37256eSGarrett Wollman 	if (error == 0)
2772c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
2782c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
2792c37256eSGarrett Wollman }
2802c37256eSGarrett Wollman 
281fb59c426SYoshinobu Inoue #ifdef INET6
282fb59c426SYoshinobu Inoue static int
283fb59c426SYoshinobu Inoue tcp6_usr_listen(struct socket *so, struct proc *p)
284fb59c426SYoshinobu Inoue {
285fb59c426SYoshinobu Inoue 	int s = splnet();
286fb59c426SYoshinobu Inoue 	int error = 0;
287fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
288fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
289fb59c426SYoshinobu Inoue 
290fb59c426SYoshinobu Inoue 	COMMON_START();
291fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
292fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
293fdaf052eSYoshinobu Inoue 		if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0)
294fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
295fb59c426SYoshinobu Inoue 		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
296fb59c426SYoshinobu Inoue 	}
297fb59c426SYoshinobu Inoue 	if (error == 0)
298fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
299fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
300fb59c426SYoshinobu Inoue }
301fb59c426SYoshinobu Inoue #endif /* INET6 */
302fb59c426SYoshinobu Inoue 
3032c37256eSGarrett Wollman /*
3042c37256eSGarrett Wollman  * Initiate connection to peer.
3052c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3062c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3072c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3082c37256eSGarrett Wollman  * Send initial segment on connection.
3092c37256eSGarrett Wollman  */
3102c37256eSGarrett Wollman static int
31157bf258eSGarrett Wollman tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
3122c37256eSGarrett Wollman {
3132c37256eSGarrett Wollman 	int s = splnet();
3142c37256eSGarrett Wollman 	int error = 0;
3152c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
3162c37256eSGarrett Wollman 	struct tcpcb *tp;
3172c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3182c37256eSGarrett Wollman 
3192c37256eSGarrett Wollman 	COMMON_START();
3202c37256eSGarrett Wollman 
3212c37256eSGarrett Wollman 	/*
3222c37256eSGarrett Wollman 	 * Must disallow TCP ``connections'' to multicast addresses.
3232c37256eSGarrett Wollman 	 */
32457bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
3252c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
3262c37256eSGarrett Wollman 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
3272c37256eSGarrett Wollman 		error = EAFNOSUPPORT;
3282c37256eSGarrett Wollman 		goto out;
3292c37256eSGarrett Wollman 	}
3302c37256eSGarrett Wollman 
33175c13541SPoul-Henning Kamp 	prison_remote_ip(p, 0, &sinp->sin_addr.s_addr);
33275c13541SPoul-Henning Kamp 
333a29f300eSGarrett Wollman 	if ((error = tcp_connect(tp, nam, p)) != 0)
3342c37256eSGarrett Wollman 		goto out;
3352c37256eSGarrett Wollman 	error = tcp_output(tp);
3362c37256eSGarrett Wollman 	COMMON_END(PRU_CONNECT);
3372c37256eSGarrett Wollman }
3382c37256eSGarrett Wollman 
339fb59c426SYoshinobu Inoue #ifdef INET6
340fb59c426SYoshinobu Inoue static int
341fb59c426SYoshinobu Inoue tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
342fb59c426SYoshinobu Inoue {
343fb59c426SYoshinobu Inoue 	int s = splnet();
344fb59c426SYoshinobu Inoue 	int error = 0;
345fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
346fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
347fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
348fb59c426SYoshinobu Inoue 
349fb59c426SYoshinobu Inoue 	COMMON_START();
350fb59c426SYoshinobu Inoue 
351fb59c426SYoshinobu Inoue 	/*
352fb59c426SYoshinobu Inoue 	 * Must disallow TCP ``connections'' to multicast addresses.
353fb59c426SYoshinobu Inoue 	 */
354fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
355fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
356fb59c426SYoshinobu Inoue 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
357fb59c426SYoshinobu Inoue 		error = EAFNOSUPPORT;
358fb59c426SYoshinobu Inoue 		goto out;
359fb59c426SYoshinobu Inoue 	}
360fb59c426SYoshinobu Inoue 
361fdaf052eSYoshinobu Inoue 	if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0 &&
362fb59c426SYoshinobu Inoue 	    IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
363fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
364fb59c426SYoshinobu Inoue 
365fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
366fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
367fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
368fb59c426SYoshinobu Inoue 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
369fb59c426SYoshinobu Inoue 			goto out;
370fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
371fb59c426SYoshinobu Inoue 		goto out;
372fb59c426SYoshinobu Inoue 	}
373fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
374fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
375fb59c426SYoshinobu Inoue 	if ((error = tcp6_connect(tp, nam, p)) != 0)
376fb59c426SYoshinobu Inoue 		goto out;
377fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
378fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
379fb59c426SYoshinobu Inoue }
380fb59c426SYoshinobu Inoue #endif /* INET6 */
381fb59c426SYoshinobu Inoue 
3822c37256eSGarrett Wollman /*
3832c37256eSGarrett Wollman  * Initiate disconnect from peer.
3842c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
3852c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
3862c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
3872c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
3882c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
3892c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
3902c37256eSGarrett Wollman  *
3912c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
3922c37256eSGarrett Wollman  */
3932c37256eSGarrett Wollman static int
3942c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
3952c37256eSGarrett Wollman {
3962c37256eSGarrett Wollman 	int s = splnet();
3972c37256eSGarrett Wollman 	int error = 0;
3982c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
3992c37256eSGarrett Wollman 	struct tcpcb *tp;
4002c37256eSGarrett Wollman 
4012c37256eSGarrett Wollman 	COMMON_START();
4022c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4032c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4042c37256eSGarrett Wollman }
4052c37256eSGarrett Wollman 
4062c37256eSGarrett Wollman /*
4072c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4082c37256eSGarrett Wollman  * done at higher levels; just return the address
4092c37256eSGarrett Wollman  * of the peer, storing through addr.
4102c37256eSGarrett Wollman  */
4112c37256eSGarrett Wollman static int
41257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4132c37256eSGarrett Wollman {
4142c37256eSGarrett Wollman 	int s = splnet();
4152c37256eSGarrett Wollman 	int error = 0;
4162c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4172c37256eSGarrett Wollman 	struct tcpcb *tp;
4182c37256eSGarrett Wollman 
4192c37256eSGarrett Wollman 	COMMON_START();
420117bcae7SGarrett Wollman 	in_setpeeraddr(so, nam);
4212c37256eSGarrett Wollman 	COMMON_END(PRU_ACCEPT);
4222c37256eSGarrett Wollman }
4232c37256eSGarrett Wollman 
424fb59c426SYoshinobu Inoue #ifdef INET6
425fb59c426SYoshinobu Inoue static int
426fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
427fb59c426SYoshinobu Inoue {
428fb59c426SYoshinobu Inoue 	int s = splnet();
429fb59c426SYoshinobu Inoue 	int error = 0;
430fb59c426SYoshinobu Inoue 	struct inpcb *inp = sotoinpcb(so);
431fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
432fb59c426SYoshinobu Inoue 
433fb59c426SYoshinobu Inoue 	COMMON_START();
434fb59c426SYoshinobu Inoue 	in6_mapped_peeraddr(so, nam);
435fb59c426SYoshinobu Inoue 	COMMON_END(PRU_ACCEPT);
436fb59c426SYoshinobu Inoue }
437fb59c426SYoshinobu Inoue #endif /* INET6 */
4382c37256eSGarrett Wollman /*
4392c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
4402c37256eSGarrett Wollman  */
4412c37256eSGarrett Wollman static int
4422c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
4432c37256eSGarrett Wollman {
4442c37256eSGarrett Wollman 	int s = splnet();
4452c37256eSGarrett Wollman 	int error = 0;
4462c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4472c37256eSGarrett Wollman 	struct tcpcb *tp;
4482c37256eSGarrett Wollman 
4492c37256eSGarrett Wollman 	COMMON_START();
4502c37256eSGarrett Wollman 	socantsendmore(so);
4512c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
4522c37256eSGarrett Wollman 	if (tp)
4532c37256eSGarrett Wollman 		error = tcp_output(tp);
4542c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
4552c37256eSGarrett Wollman }
4562c37256eSGarrett Wollman 
4572c37256eSGarrett Wollman /*
4582c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
4592c37256eSGarrett Wollman  */
4602c37256eSGarrett Wollman static int
4612c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
4622c37256eSGarrett Wollman {
4632c37256eSGarrett Wollman 	int s = splnet();
4642c37256eSGarrett Wollman 	int error = 0;
4652c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4662c37256eSGarrett Wollman 	struct tcpcb *tp;
4672c37256eSGarrett Wollman 
4682c37256eSGarrett Wollman 	COMMON_START();
4692c37256eSGarrett Wollman 	tcp_output(tp);
4702c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
4712c37256eSGarrett Wollman }
4722c37256eSGarrett Wollman 
4732c37256eSGarrett Wollman /*
4742c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
4759c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
4769c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
4779c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
4789c9906e9SPeter Wemm  * generally are caller-frees.
4792c37256eSGarrett Wollman  */
4802c37256eSGarrett Wollman static int
48157bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
48257bf258eSGarrett Wollman 	     struct sockaddr *nam, struct mbuf *control, struct proc *p)
4832c37256eSGarrett Wollman {
4842c37256eSGarrett Wollman 	int s = splnet();
4852c37256eSGarrett Wollman 	int error = 0;
4862c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
4872c37256eSGarrett Wollman 	struct tcpcb *tp;
488fb59c426SYoshinobu Inoue #ifdef INET6
489fb59c426SYoshinobu Inoue 	int isipv6;
490fb59c426SYoshinobu Inoue #endif
4919c9906e9SPeter Wemm 	TCPDEBUG0;
4922c37256eSGarrett Wollman 
4939c9906e9SPeter Wemm 	if (inp == NULL) {
4949c9906e9SPeter Wemm 		/*
4959c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
4969c9906e9SPeter Wemm 		 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
4979c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
4989c9906e9SPeter Wemm 		 */
4999c9906e9SPeter Wemm 		if (m)
5009c9906e9SPeter Wemm 			m_freem(m);
5019c9906e9SPeter Wemm 		if (control)
5029c9906e9SPeter Wemm 			m_freem(control);
5039c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
50445d3a132SPeter Wemm 		tp = NULL;
50545d3a132SPeter Wemm 		TCPDEBUG1();
5069c9906e9SPeter Wemm 		goto out;
5079c9906e9SPeter Wemm 	}
508fb59c426SYoshinobu Inoue #ifdef INET6
509fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
510fb59c426SYoshinobu Inoue #endif /* INET6 */
5119c9906e9SPeter Wemm 	tp = intotcpcb(inp);
5129c9906e9SPeter Wemm 	TCPDEBUG1();
5139c9906e9SPeter Wemm 	if (control) {
5149c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
5159c9906e9SPeter Wemm 		if (control->m_len) {
5169c9906e9SPeter Wemm 			m_freem(control);
5172c37256eSGarrett Wollman 			if (m)
5182c37256eSGarrett Wollman 				m_freem(m);
519744f87eaSDavid Greenman 			error = EINVAL;
520744f87eaSDavid Greenman 			goto out;
5212c37256eSGarrett Wollman 		}
5229c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
5239c9906e9SPeter Wemm 	}
5242c37256eSGarrett Wollman 	if(!(flags & PRUS_OOB)) {
5252c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
5262c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
5272c37256eSGarrett Wollman 			/*
5282c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
5292c37256eSGarrett Wollman 			 * initialize window to default value, and
5302c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
5312c37256eSGarrett Wollman 			 * MSS.
5322c37256eSGarrett Wollman 			 */
533fb59c426SYoshinobu Inoue #ifdef INET6
534fb59c426SYoshinobu Inoue 			if (isipv6)
535fb59c426SYoshinobu Inoue 				error = tcp6_connect(tp, nam, p);
536fb59c426SYoshinobu Inoue 			else
537fb59c426SYoshinobu Inoue #endif /* INET6 */
538a29f300eSGarrett Wollman 			error = tcp_connect(tp, nam, p);
5392c37256eSGarrett Wollman 			if (error)
5402c37256eSGarrett Wollman 				goto out;
5412c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
5422c37256eSGarrett Wollman 			tcp_mss(tp, -1);
5432c37256eSGarrett Wollman 		}
5442c37256eSGarrett Wollman 
5452c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
5462c37256eSGarrett Wollman 			/*
5472c37256eSGarrett Wollman 			 * Close the send side of the connection after
5482c37256eSGarrett Wollman 			 * the data is sent.
5492c37256eSGarrett Wollman 			 */
5502c37256eSGarrett Wollman 			socantsendmore(so);
5512c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
5522c37256eSGarrett Wollman 		}
553b0acefa8SBill Fenner 		if (tp != NULL) {
554b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
555b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
5562c37256eSGarrett Wollman 			error = tcp_output(tp);
557b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
558b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
559b0acefa8SBill Fenner 		}
5602c37256eSGarrett Wollman 	} else {
5612c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
5622c37256eSGarrett Wollman 			m_freem(m);
5632c37256eSGarrett Wollman 			error = ENOBUFS;
5642c37256eSGarrett Wollman 			goto out;
5652c37256eSGarrett Wollman 		}
5662c37256eSGarrett Wollman 		/*
5672c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
5682c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
5692c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
5702c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
5712c37256eSGarrett Wollman 		 * of data past the urgent section.
5722c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
5732c37256eSGarrett Wollman 		 */
5742c37256eSGarrett Wollman 		sbappend(&so->so_snd, m);
575ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
576ef53690bSGarrett Wollman 			/*
577ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
578ef53690bSGarrett Wollman 			 * initialize window to default value, and
579ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
580ef53690bSGarrett Wollman 			 * MSS.
581ef53690bSGarrett Wollman 			 */
582fb59c426SYoshinobu Inoue #ifdef INET6
583fb59c426SYoshinobu Inoue 			if (isipv6)
584fb59c426SYoshinobu Inoue 				error = tcp6_connect(tp, nam, p);
585fb59c426SYoshinobu Inoue 			else
586fb59c426SYoshinobu Inoue #endif /* INET6 */
587a29f300eSGarrett Wollman 			error = tcp_connect(tp, nam, p);
588ef53690bSGarrett Wollman 			if (error)
589ef53690bSGarrett Wollman 				goto out;
590ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
591ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
592ef53690bSGarrett Wollman 		}
5932c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
5942c37256eSGarrett Wollman 		tp->t_force = 1;
5952c37256eSGarrett Wollman 		error = tcp_output(tp);
5962c37256eSGarrett Wollman 		tp->t_force = 0;
5972c37256eSGarrett Wollman 	}
5982c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
5992c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
6002c37256eSGarrett Wollman }
6012c37256eSGarrett Wollman 
6022c37256eSGarrett Wollman /*
6032c37256eSGarrett Wollman  * Abort the TCP.
6042c37256eSGarrett Wollman  */
6052c37256eSGarrett Wollman static int
6062c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
6072c37256eSGarrett Wollman {
6082c37256eSGarrett Wollman 	int s = splnet();
6092c37256eSGarrett Wollman 	int error = 0;
6102c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6112c37256eSGarrett Wollman 	struct tcpcb *tp;
6122c37256eSGarrett Wollman 
6132c37256eSGarrett Wollman 	COMMON_START();
6142c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
6152c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
6162c37256eSGarrett Wollman }
6172c37256eSGarrett Wollman 
6182c37256eSGarrett Wollman /*
6192c37256eSGarrett Wollman  * Receive out-of-band data.
6202c37256eSGarrett Wollman  */
6212c37256eSGarrett Wollman static int
6222c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
6232c37256eSGarrett Wollman {
6242c37256eSGarrett Wollman 	int s = splnet();
6252c37256eSGarrett Wollman 	int error = 0;
6262c37256eSGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
6272c37256eSGarrett Wollman 	struct tcpcb *tp;
6282c37256eSGarrett Wollman 
6292c37256eSGarrett Wollman 	COMMON_START();
6302c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
6312c37256eSGarrett Wollman 	     (so->so_state & SS_RCVATMARK) == 0) ||
6322c37256eSGarrett Wollman 	    so->so_options & SO_OOBINLINE ||
6332c37256eSGarrett Wollman 	    tp->t_oobflags & TCPOOB_HADDATA) {
6342c37256eSGarrett Wollman 		error = EINVAL;
6352c37256eSGarrett Wollman 		goto out;
6362c37256eSGarrett Wollman 	}
6372c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
6382c37256eSGarrett Wollman 		error = EWOULDBLOCK;
6392c37256eSGarrett Wollman 		goto out;
6402c37256eSGarrett Wollman 	}
6412c37256eSGarrett Wollman 	m->m_len = 1;
6422c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
6432c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
6442c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
6452c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
6462c37256eSGarrett Wollman }
6472c37256eSGarrett Wollman 
6482c37256eSGarrett Wollman /* xxx - should be const */
6492c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
6502c37256eSGarrett Wollman 	tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
651117bcae7SGarrett Wollman 	tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
652117bcae7SGarrett Wollman 	tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
653117bcae7SGarrett Wollman 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
654f8f6cbbaSPeter Wemm 	in_setsockaddr, sosend, soreceive, sopoll
6552c37256eSGarrett Wollman };
656df8bae1dSRodney W. Grimes 
657fb59c426SYoshinobu Inoue #ifdef INET6
658fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
659fb59c426SYoshinobu Inoue 	tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
660fb59c426SYoshinobu Inoue 	tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
661fb59c426SYoshinobu Inoue 	tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
662fb59c426SYoshinobu Inoue 	tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
663fb59c426SYoshinobu Inoue 	in6_mapped_sockaddr, sosend, soreceive, sopoll
664fb59c426SYoshinobu Inoue };
665fb59c426SYoshinobu Inoue #endif /* INET6 */
666fb59c426SYoshinobu Inoue 
667a0292f23SGarrett Wollman /*
668a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
669a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
670a0292f23SGarrett Wollman  * port number if needed.  Call in_pcbladdr to do the routing and to choose
671a0292f23SGarrett Wollman  * a local host address (interface).  If there is an existing incarnation
672a0292f23SGarrett Wollman  * of the same connection in TIME-WAIT state and if the remote host was
673a0292f23SGarrett Wollman  * sending CC options and if the connection duration was < MSL, then
674a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
675a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
676a0292f23SGarrett Wollman  */
6770312fbe9SPoul-Henning Kamp static int
678a29f300eSGarrett Wollman tcp_connect(tp, nam, p)
679a0292f23SGarrett Wollman 	register struct tcpcb *tp;
68057bf258eSGarrett Wollman 	struct sockaddr *nam;
681a29f300eSGarrett Wollman 	struct proc *p;
682a0292f23SGarrett Wollman {
683a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
684a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
685a0292f23SGarrett Wollman 	struct tcpcb *otp;
68657bf258eSGarrett Wollman 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
687a0292f23SGarrett Wollman 	struct sockaddr_in *ifaddr;
688a45d2726SAndras Olah 	struct rmxp_tao *taop;
689a45d2726SAndras Olah 	struct rmxp_tao tao_noncached;
690c3229e05SDavid Greenman 	int error;
691a0292f23SGarrett Wollman 
692a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
69357bf258eSGarrett Wollman 		error = in_pcbbind(inp, (struct sockaddr *)0, p);
694a0292f23SGarrett Wollman 		if (error)
695a0292f23SGarrett Wollman 			return error;
696a0292f23SGarrett Wollman 	}
697a0292f23SGarrett Wollman 
698a0292f23SGarrett Wollman 	/*
699a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
700a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
701a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
702a0292f23SGarrett Wollman 	 */
703a0292f23SGarrett Wollman 	error = in_pcbladdr(inp, nam, &ifaddr);
704d3628763SRodney W. Grimes 	if (error)
705d3628763SRodney W. Grimes 		return error;
706c3229e05SDavid Greenman 	oinp = in_pcblookup_hash(inp->inp_pcbinfo,
707a0292f23SGarrett Wollman 	    sin->sin_addr, sin->sin_port,
708a0292f23SGarrett Wollman 	    inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
709a0292f23SGarrett Wollman 						: ifaddr->sin_addr,
710cfa1ca9dSYoshinobu Inoue 	    inp->inp_lport,  0, NULL);
711a0292f23SGarrett Wollman 	if (oinp) {
712a0292f23SGarrett Wollman 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
713a0292f23SGarrett Wollman 		otp->t_state == TCPS_TIME_WAIT &&
7149b8b58e0SJonathan Lemon 		    (ticks - otp->t_starttime) < tcp_msl &&
715a0292f23SGarrett Wollman 		    (otp->t_flags & TF_RCVD_CC))
716a0292f23SGarrett Wollman 			otp = tcp_close(otp);
717a0292f23SGarrett Wollman 		else
718a0292f23SGarrett Wollman 			return EADDRINUSE;
719a0292f23SGarrett Wollman 	}
720a0292f23SGarrett Wollman 	if (inp->inp_laddr.s_addr == INADDR_ANY)
721a0292f23SGarrett Wollman 		inp->inp_laddr = ifaddr->sin_addr;
722a0292f23SGarrett Wollman 	inp->inp_faddr = sin->sin_addr;
723a0292f23SGarrett Wollman 	inp->inp_fport = sin->sin_port;
72415bd2b43SDavid Greenman 	in_pcbrehash(inp);
725a0292f23SGarrett Wollman 
726a0292f23SGarrett Wollman 	tp->t_template = tcp_template(tp);
727a0292f23SGarrett Wollman 	if (tp->t_template == 0) {
728a0292f23SGarrett Wollman 		in_pcbdisconnect(inp);
729a0292f23SGarrett Wollman 		return ENOBUFS;
730a0292f23SGarrett Wollman 	}
731a0292f23SGarrett Wollman 
732a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
733a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
734a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
735a0292f23SGarrett Wollman 		tp->request_r_scale++;
736a0292f23SGarrett Wollman 
737a0292f23SGarrett Wollman 	soisconnecting(so);
738a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
739a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
7409b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
741a0292f23SGarrett Wollman 	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
742a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
743a45d2726SAndras Olah 
744a45d2726SAndras Olah 	/*
745a45d2726SAndras Olah 	 * Generate a CC value for this connection and
746a45d2726SAndras Olah 	 * check whether CC or CCnew should be used.
747a45d2726SAndras Olah 	 */
748a45d2726SAndras Olah 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
749a45d2726SAndras Olah 		taop = &tao_noncached;
750a45d2726SAndras Olah 		bzero(taop, sizeof(*taop));
751a45d2726SAndras Olah 	}
752a45d2726SAndras Olah 
753a0292f23SGarrett Wollman 	tp->cc_send = CC_INC(tcp_ccgen);
754a45d2726SAndras Olah 	if (taop->tao_ccsent != 0 &&
755a45d2726SAndras Olah 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
756a45d2726SAndras Olah 		taop->tao_ccsent = tp->cc_send;
757a45d2726SAndras Olah 	} else {
758a45d2726SAndras Olah 		taop->tao_ccsent = 0;
759a45d2726SAndras Olah 		tp->t_flags |= TF_SENDCCNEW;
760a45d2726SAndras Olah 	}
761a0292f23SGarrett Wollman 
762a0292f23SGarrett Wollman 	return 0;
763a0292f23SGarrett Wollman }
764a0292f23SGarrett Wollman 
765fb59c426SYoshinobu Inoue #ifdef INET6
766fb59c426SYoshinobu Inoue static int
767fb59c426SYoshinobu Inoue tcp6_connect(tp, nam, p)
768fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
769fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
770fb59c426SYoshinobu Inoue 	struct proc *p;
771fb59c426SYoshinobu Inoue {
772fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
773fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
774fb59c426SYoshinobu Inoue 	struct tcpcb *otp;
775fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
776fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
777fb59c426SYoshinobu Inoue 	struct rmxp_tao *taop;
778fb59c426SYoshinobu Inoue 	struct rmxp_tao tao_noncached;
779fb59c426SYoshinobu Inoue 	int error;
780fb59c426SYoshinobu Inoue 
781fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
782fb59c426SYoshinobu Inoue 		error = in6_pcbbind(inp, (struct sockaddr *)0, p);
783fb59c426SYoshinobu Inoue 		if (error)
784fb59c426SYoshinobu Inoue 			return error;
785fb59c426SYoshinobu Inoue 	}
786fb59c426SYoshinobu Inoue 
787fb59c426SYoshinobu Inoue 	/*
788fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
789fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
790fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
791fb59c426SYoshinobu Inoue 	 */
792fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
793fb59c426SYoshinobu Inoue 	if (error)
794fb59c426SYoshinobu Inoue 		return error;
795fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
796fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
797fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
798fb59c426SYoshinobu Inoue 				  ? addr6
799fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
800fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
801fb59c426SYoshinobu Inoue 	if (oinp) {
802fb59c426SYoshinobu Inoue 		if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
803fb59c426SYoshinobu Inoue 		    otp->t_state == TCPS_TIME_WAIT &&
804fb59c426SYoshinobu Inoue 		    (ticks - otp->t_starttime) < tcp_msl &&
805fb59c426SYoshinobu Inoue 		    (otp->t_flags & TF_RCVD_CC))
806fb59c426SYoshinobu Inoue 			otp = tcp_close(otp);
807fb59c426SYoshinobu Inoue 		else
808fb59c426SYoshinobu Inoue 			return EADDRINUSE;
809fb59c426SYoshinobu Inoue 	}
810fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
811fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
812fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
813fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
814fb59c426SYoshinobu Inoue 	if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
815fb59c426SYoshinobu Inoue 		inp->in6p_flowinfo = sin6->sin6_flowinfo;
816fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
817fb59c426SYoshinobu Inoue 
818fb59c426SYoshinobu Inoue 	tp->t_template = tcp_template(tp);
819fb59c426SYoshinobu Inoue 	if (tp->t_template == 0) {
820fb59c426SYoshinobu Inoue 		in6_pcbdisconnect(inp);
821fb59c426SYoshinobu Inoue 		return ENOBUFS;
822fb59c426SYoshinobu Inoue 	}
823fb59c426SYoshinobu Inoue 
824fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
825fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
826fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
827fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
828fb59c426SYoshinobu Inoue 
829fb59c426SYoshinobu Inoue 	soisconnecting(so);
830fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
831fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
832fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
833fb59c426SYoshinobu Inoue 	tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
834fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
835fb59c426SYoshinobu Inoue 
836fb59c426SYoshinobu Inoue 	/*
837fb59c426SYoshinobu Inoue 	 * Generate a CC value for this connection and
838fb59c426SYoshinobu Inoue 	 * check whether CC or CCnew should be used.
839fb59c426SYoshinobu Inoue 	 */
840fb59c426SYoshinobu Inoue 	if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
841fb59c426SYoshinobu Inoue 		taop = &tao_noncached;
842fb59c426SYoshinobu Inoue 		bzero(taop, sizeof(*taop));
843fb59c426SYoshinobu Inoue 	}
844fb59c426SYoshinobu Inoue 
845fb59c426SYoshinobu Inoue 	tp->cc_send = CC_INC(tcp_ccgen);
846fb59c426SYoshinobu Inoue 	if (taop->tao_ccsent != 0 &&
847fb59c426SYoshinobu Inoue 	    CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
848fb59c426SYoshinobu Inoue 		taop->tao_ccsent = tp->cc_send;
849fb59c426SYoshinobu Inoue 	} else {
850fb59c426SYoshinobu Inoue 		taop->tao_ccsent = 0;
851fb59c426SYoshinobu Inoue 		tp->t_flags |= TF_SENDCCNEW;
852fb59c426SYoshinobu Inoue 	}
853fb59c426SYoshinobu Inoue 
854fb59c426SYoshinobu Inoue 	return 0;
855fb59c426SYoshinobu Inoue }
856fb59c426SYoshinobu Inoue #endif /* INET6 */
857fb59c426SYoshinobu Inoue 
858cfe8b629SGarrett Wollman /*
859cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
860cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
861cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
862cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
863cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
864cfe8b629SGarrett Wollman  */
865df8bae1dSRodney W. Grimes int
866cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
867df8bae1dSRodney W. Grimes 	struct socket *so;
868cfe8b629SGarrett Wollman 	struct sockopt *sopt;
869df8bae1dSRodney W. Grimes {
870cfe8b629SGarrett Wollman 	int	error, opt, optval, s;
871df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
872cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
873df8bae1dSRodney W. Grimes 
874cfe8b629SGarrett Wollman 	error = 0;
875cfe8b629SGarrett Wollman 	s = splnet();		/* XXX */
876df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
877df8bae1dSRodney W. Grimes 	if (inp == NULL) {
878df8bae1dSRodney W. Grimes 		splx(s);
879df8bae1dSRodney W. Grimes 		return (ECONNRESET);
880df8bae1dSRodney W. Grimes 	}
881cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
882fb59c426SYoshinobu Inoue #ifdef INET6
883fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
884fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
885fb59c426SYoshinobu Inoue 		else
886fb59c426SYoshinobu Inoue #endif /* INET6 */
887cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
888df8bae1dSRodney W. Grimes 		splx(s);
889df8bae1dSRodney W. Grimes 		return (error);
890df8bae1dSRodney W. Grimes 	}
891df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
892df8bae1dSRodney W. Grimes 
893cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
894cfe8b629SGarrett Wollman 	case SOPT_SET:
895cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
896df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
897cfe8b629SGarrett Wollman 		case TCP_NOOPT:
898cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
899cfe8b629SGarrett Wollman 					    sizeof optval);
900cfe8b629SGarrett Wollman 			if (error)
901cfe8b629SGarrett Wollman 				break;
902cfe8b629SGarrett Wollman 
903cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
904cfe8b629SGarrett Wollman 			case TCP_NODELAY:
905cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
906cfe8b629SGarrett Wollman 				break;
907cfe8b629SGarrett Wollman 			case TCP_NOOPT:
908cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
909cfe8b629SGarrett Wollman 				break;
910cfe8b629SGarrett Wollman 			default:
911cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
912cfe8b629SGarrett Wollman 				break;
913cfe8b629SGarrett Wollman 			}
914cfe8b629SGarrett Wollman 
915cfe8b629SGarrett Wollman 			if (optval)
916cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
917df8bae1dSRodney W. Grimes 			else
918cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
919df8bae1dSRodney W. Grimes 			break;
920df8bae1dSRodney W. Grimes 
921007581c0SJonathan Lemon 		case TCP_NOPUSH:
922007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
923007581c0SJonathan Lemon 					    sizeof optval);
924007581c0SJonathan Lemon 			if (error)
925007581c0SJonathan Lemon 				break;
926007581c0SJonathan Lemon 
927007581c0SJonathan Lemon 			if (optval)
928007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
929007581c0SJonathan Lemon 			else {
930007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
931007581c0SJonathan Lemon 				error = tcp_output(tp);
932007581c0SJonathan Lemon 			}
933007581c0SJonathan Lemon 			break;
934007581c0SJonathan Lemon 
935df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
936cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
937cfe8b629SGarrett Wollman 					    sizeof optval);
938cfe8b629SGarrett Wollman 			if (error)
939df8bae1dSRodney W. Grimes 				break;
940df8bae1dSRodney W. Grimes 
941cfe8b629SGarrett Wollman 			if (optval > 0 && optval <= tp->t_maxseg)
942cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
943a0292f23SGarrett Wollman 			else
944a0292f23SGarrett Wollman 				error = EINVAL;
945a0292f23SGarrett Wollman 			break;
946a0292f23SGarrett Wollman 
947df8bae1dSRodney W. Grimes 		default:
948df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
949df8bae1dSRodney W. Grimes 			break;
950df8bae1dSRodney W. Grimes 		}
951df8bae1dSRodney W. Grimes 		break;
952df8bae1dSRodney W. Grimes 
953cfe8b629SGarrett Wollman 	case SOPT_GET:
954cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
955df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
956cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
957df8bae1dSRodney W. Grimes 			break;
958df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
959cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
960df8bae1dSRodney W. Grimes 			break;
961a0292f23SGarrett Wollman 		case TCP_NOOPT:
962cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
963a0292f23SGarrett Wollman 			break;
964a0292f23SGarrett Wollman 		case TCP_NOPUSH:
965cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
966a0292f23SGarrett Wollman 			break;
967df8bae1dSRodney W. Grimes 		default:
968df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
969df8bae1dSRodney W. Grimes 			break;
970df8bae1dSRodney W. Grimes 		}
971cfe8b629SGarrett Wollman 		if (error == 0)
972cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
973df8bae1dSRodney W. Grimes 		break;
974df8bae1dSRodney W. Grimes 	}
975df8bae1dSRodney W. Grimes 	splx(s);
976df8bae1dSRodney W. Grimes 	return (error);
977df8bae1dSRodney W. Grimes }
978df8bae1dSRodney W. Grimes 
97926e30fbbSDavid Greenman /*
98026e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
98126e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
98226e30fbbSDavid Greenman  * be set by the route).
98326e30fbbSDavid Greenman  */
98426e30fbbSDavid Greenman u_long	tcp_sendspace = 1024*16;
9853d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
9863d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
98726e30fbbSDavid Greenman u_long	tcp_recvspace = 1024*16;
9883d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
9893d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
990df8bae1dSRodney W. Grimes 
991df8bae1dSRodney W. Grimes /*
992df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
993df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
994df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
995df8bae1dSRodney W. Grimes  */
9960312fbe9SPoul-Henning Kamp static int
997a29f300eSGarrett Wollman tcp_attach(so, p)
998df8bae1dSRodney W. Grimes 	struct socket *so;
999a29f300eSGarrett Wollman 	struct proc *p;
1000df8bae1dSRodney W. Grimes {
1001df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1002df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1003df8bae1dSRodney W. Grimes 	int error;
1004fb59c426SYoshinobu Inoue #ifdef INET6
1005fb59c426SYoshinobu Inoue 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1006fb59c426SYoshinobu Inoue #endif
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1009df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1010df8bae1dSRodney W. Grimes 		if (error)
1011df8bae1dSRodney W. Grimes 			return (error);
1012df8bae1dSRodney W. Grimes 	}
1013a29f300eSGarrett Wollman 	error = in_pcballoc(so, &tcbinfo, p);
1014df8bae1dSRodney W. Grimes 	if (error)
1015df8bae1dSRodney W. Grimes 		return (error);
1016df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
10176a800098SYoshinobu Inoue #ifdef IPSEC
10186a800098SYoshinobu Inoue 	error = ipsec_init_policy(so, &inp->inp_sp);
10196a800098SYoshinobu Inoue 	if (error) {
1020fb59c426SYoshinobu Inoue #ifdef INET6
1021fb59c426SYoshinobu Inoue 		if (isipv6)
1022fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1023fb59c426SYoshinobu Inoue 		else
1024fb59c426SYoshinobu Inoue #endif
10256a800098SYoshinobu Inoue 		in_pcbdetach(inp);
10266a800098SYoshinobu Inoue 		return (error);
10276a800098SYoshinobu Inoue 	}
10286a800098SYoshinobu Inoue #endif /*IPSEC*/
1029fb59c426SYoshinobu Inoue #ifdef INET6
1030fb59c426SYoshinobu Inoue 	if (isipv6) {
1031fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1032fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1033fb59c426SYoshinobu Inoue 	}
1034fb59c426SYoshinobu Inoue 	else
1035fb59c426SYoshinobu Inoue #endif
1036cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1037df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1038df8bae1dSRodney W. Grimes 	if (tp == 0) {
1039df8bae1dSRodney W. Grimes 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1040df8bae1dSRodney W. Grimes 
1041df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1042fb59c426SYoshinobu Inoue #ifdef INET6
1043fb59c426SYoshinobu Inoue 		if (isipv6)
1044fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1045fb59c426SYoshinobu Inoue 		else
1046fb59c426SYoshinobu Inoue #endif
1047df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1048df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1049df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1050df8bae1dSRodney W. Grimes 	}
1051df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1052df8bae1dSRodney W. Grimes 	return (0);
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes /*
1056df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1057df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1058df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1059df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1060df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1061df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1062df8bae1dSRodney W. Grimes  */
10630312fbe9SPoul-Henning Kamp static struct tcpcb *
1064df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1065df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1066df8bae1dSRodney W. Grimes {
1067df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1068df8bae1dSRodney W. Grimes 
1069df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1070df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1071df8bae1dSRodney W. Grimes 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1072df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, 0);
1073df8bae1dSRodney W. Grimes 	else {
1074df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1075df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1076df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1077df8bae1dSRodney W. Grimes 		if (tp)
1078df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1079df8bae1dSRodney W. Grimes 	}
1080df8bae1dSRodney W. Grimes 	return (tp);
1081df8bae1dSRodney W. Grimes }
1082df8bae1dSRodney W. Grimes 
1083df8bae1dSRodney W. Grimes /*
1084df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1085df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1086df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1087df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1088df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1089df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1090df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1091df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1092df8bae1dSRodney W. Grimes  */
10930312fbe9SPoul-Henning Kamp static struct tcpcb *
1094df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1095df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1096df8bae1dSRodney W. Grimes {
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1099df8bae1dSRodney W. Grimes 
1100df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1101df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1102df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1103df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1104df8bae1dSRodney W. Grimes 		break;
1105df8bae1dSRodney W. Grimes 
1106a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1107df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1108a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1109a0292f23SGarrett Wollman 		break;
1110a0292f23SGarrett Wollman 
1111df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1112df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1113df8bae1dSRodney W. Grimes 		break;
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1116df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1117df8bae1dSRodney W. Grimes 		break;
1118df8bae1dSRodney W. Grimes 	}
1119b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1120df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1121b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1122b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
11239b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
11249b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1125b6239c4aSAndras Olah 	}
1126df8bae1dSRodney W. Grimes 	return (tp);
1127df8bae1dSRodney W. Grimes }
1128a0292f23SGarrett Wollman 
1129