xref: /freebsd/sys/netinet/tcp_usrreq.c (revision d1401c9000732247c8f8db3b6abe7a2e53393d5d)
1c398230bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
291fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
336a800098SYoshinobu Inoue #include "opt_ipsec.h"
341cfd4b53SBruce M Simpson #include "opt_inet.h"
35fb59c426SYoshinobu Inoue #include "opt_inet6.h"
360cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
370cc12cc5SJoerg Wunsch 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
41c7a82f90SGarrett Wollman #include <sys/kernel.h>
4298163b98SPoul-Henning Kamp #include <sys/sysctl.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44fb59c426SYoshinobu Inoue #ifdef INET6
45fb59c426SYoshinobu Inoue #include <sys/domain.h>
46fb59c426SYoshinobu Inoue #endif /* INET6 */
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5091421ba2SRobert Watson #include <sys/proc.h>
5191421ba2SRobert Watson #include <sys/jail.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 
8956dc72c3SPawel Jakub Dawidek static int	tcp_attach(struct socket *);
904d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
914d77a549SAlfred Perlstein 		    struct thread *td);
92fb59c426SYoshinobu Inoue #ifdef INET6
934d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
944d77a549SAlfred Perlstein 		    struct thread *td);
95fb59c426SYoshinobu Inoue #endif /* INET6 */
960312fbe9SPoul-Henning Kamp static struct tcpcb *
974d77a549SAlfred Perlstein 		tcp_disconnect(struct tcpcb *);
980312fbe9SPoul-Henning Kamp static struct tcpcb *
994d77a549SAlfred Perlstein 		tcp_usrclosed(struct tcpcb *);
100b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1012c37256eSGarrett Wollman 
1022c37256eSGarrett Wollman #ifdef TCPDEBUG
1031db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1042c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1054cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1064cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1072c37256eSGarrett Wollman #else
1082c37256eSGarrett Wollman #define	TCPDEBUG0
1092c37256eSGarrett Wollman #define	TCPDEBUG1()
1102c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1112c37256eSGarrett Wollman #endif
1122c37256eSGarrett Wollman 
1132c37256eSGarrett Wollman /*
1142c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1152c37256eSGarrett Wollman  * and an internet control block.
1162c37256eSGarrett Wollman  */
1172c37256eSGarrett Wollman static int
118b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1192c37256eSGarrett Wollman {
1202c37256eSGarrett Wollman 	int error;
121f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1222c37256eSGarrett Wollman 	struct tcpcb *tp = 0;
1232c37256eSGarrett Wollman 	TCPDEBUG0;
1242c37256eSGarrett Wollman 
125f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
1262c37256eSGarrett Wollman 	TCPDEBUG1();
127f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
1282c37256eSGarrett Wollman 	if (inp) {
1292c37256eSGarrett Wollman 		error = EISCONN;
1302c37256eSGarrett Wollman 		goto out;
1312c37256eSGarrett Wollman 	}
1322c37256eSGarrett Wollman 
13356dc72c3SPawel Jakub Dawidek 	error = tcp_attach(so);
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;
139f76fcf6dSJeffrey Hsu 
140f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
141f76fcf6dSJeffrey Hsu 	tp = intotcpcb(inp);
1422c37256eSGarrett Wollman out:
1432c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
144f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
1452c37256eSGarrett Wollman 	return error;
1462c37256eSGarrett Wollman }
1472c37256eSGarrett Wollman 
1482c37256eSGarrett Wollman /*
1492c37256eSGarrett Wollman  * pru_detach() detaches the TCP protocol from the socket.
1502c37256eSGarrett Wollman  * If the protocol state is non-embryonic, then can't
1512c37256eSGarrett Wollman  * do this directly: have to initiate a pru_disconnect(),
1522c37256eSGarrett Wollman  * which may finish later; embryonic TCB's can just
1532c37256eSGarrett Wollman  * be discarded here.
1542c37256eSGarrett Wollman  */
1552c37256eSGarrett Wollman static int
1562c37256eSGarrett Wollman tcp_usr_detach(struct socket *so)
1572c37256eSGarrett Wollman {
1582c37256eSGarrett Wollman 	int error = 0;
159f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1602c37256eSGarrett Wollman 	struct tcpcb *tp;
1612c37256eSGarrett Wollman 	TCPDEBUG0;
1622c37256eSGarrett Wollman 
163f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
164f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
1650aa8ce50SJohn-Mark Gurney 	if (inp == NULL) {
166f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
1670aa8ce50SJohn-Mark Gurney 		return error;
1682c37256eSGarrett Wollman 	}
169f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1702c37256eSGarrett Wollman 	tp = intotcpcb(inp);
1712c37256eSGarrett Wollman 	TCPDEBUG1();
1722c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
1732c37256eSGarrett Wollman 
1742c37256eSGarrett Wollman 	TCPDEBUG2(PRU_DETACH);
175f76fcf6dSJeffrey Hsu 	if (tp)
176f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
177f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
1782c37256eSGarrett Wollman 	return error;
1792c37256eSGarrett Wollman }
1802c37256eSGarrett Wollman 
181f76fcf6dSJeffrey Hsu #define INI_NOLOCK	0
182f76fcf6dSJeffrey Hsu #define INI_READ	1
183f76fcf6dSJeffrey Hsu #define INI_WRITE	2
184f76fcf6dSJeffrey Hsu 
185f76fcf6dSJeffrey Hsu #define	COMMON_START()						\
186f76fcf6dSJeffrey Hsu 	TCPDEBUG0;						\
1872c37256eSGarrett Wollman 	do {							\
188f76fcf6dSJeffrey Hsu 		if (inirw == INI_READ)				\
189f76fcf6dSJeffrey Hsu 			INP_INFO_RLOCK(&tcbinfo);		\
190f76fcf6dSJeffrey Hsu 		else if (inirw == INI_WRITE)			\
191f76fcf6dSJeffrey Hsu 			INP_INFO_WLOCK(&tcbinfo);		\
192f76fcf6dSJeffrey Hsu 		inp = sotoinpcb(so);				\
1932c37256eSGarrett Wollman 		if (inp == 0) {					\
194f76fcf6dSJeffrey Hsu 			if (inirw == INI_READ)			\
195f76fcf6dSJeffrey Hsu 				INP_INFO_RUNLOCK(&tcbinfo);	\
196f76fcf6dSJeffrey Hsu 			else if (inirw == INI_WRITE)		\
197f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);	\
1982c37256eSGarrett Wollman 			return EINVAL;				\
1992c37256eSGarrett Wollman 		}						\
200f76fcf6dSJeffrey Hsu 		INP_LOCK(inp);					\
201f76fcf6dSJeffrey Hsu 		if (inirw == INI_READ)				\
202f76fcf6dSJeffrey Hsu 			INP_INFO_RUNLOCK(&tcbinfo);		\
2032c37256eSGarrett Wollman 		tp = intotcpcb(inp);				\
2042c37256eSGarrett Wollman 		TCPDEBUG1();					\
2052c37256eSGarrett Wollman } while(0)
2062c37256eSGarrett Wollman 
207f76fcf6dSJeffrey Hsu #define COMMON_END(req)						\
208f76fcf6dSJeffrey Hsu out:	TCPDEBUG2(req);						\
209f76fcf6dSJeffrey Hsu 	do {							\
210f76fcf6dSJeffrey Hsu 		if (tp)						\
211f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);			\
212f76fcf6dSJeffrey Hsu 		if (inirw == INI_WRITE)				\
213f76fcf6dSJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);		\
214f76fcf6dSJeffrey Hsu 		return error;					\
215f76fcf6dSJeffrey Hsu 		goto out;					\
216f76fcf6dSJeffrey Hsu } while(0)
2172c37256eSGarrett Wollman 
2182c37256eSGarrett Wollman /*
2192c37256eSGarrett Wollman  * Give the socket an address.
2202c37256eSGarrett Wollman  */
2212c37256eSGarrett Wollman static int
222b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2232c37256eSGarrett Wollman {
2242c37256eSGarrett Wollman 	int error = 0;
225f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
2262c37256eSGarrett Wollman 	struct tcpcb *tp;
2272c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
228edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
2292c37256eSGarrett Wollman 
23052710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
23152710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
23252710de1SPawel Jakub Dawidek 		return (EINVAL);
2332c37256eSGarrett Wollman 	/*
2342c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2352c37256eSGarrett Wollman 	 * to them.
2362c37256eSGarrett Wollman 	 */
2372c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
23852710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
23952710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
24052710de1SPawel Jakub Dawidek 
24152710de1SPawel Jakub Dawidek 	COMMON_START();
242b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
2432c37256eSGarrett Wollman 	if (error)
2442c37256eSGarrett Wollman 		goto out;
2452c37256eSGarrett Wollman 	COMMON_END(PRU_BIND);
2462c37256eSGarrett Wollman }
2472c37256eSGarrett Wollman 
248fb59c426SYoshinobu Inoue #ifdef INET6
249fb59c426SYoshinobu Inoue static int
250b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
251fb59c426SYoshinobu Inoue {
252fb59c426SYoshinobu Inoue 	int error = 0;
253f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
254fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
255fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
256edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
257fb59c426SYoshinobu Inoue 
25852710de1SPawel Jakub Dawidek 	sin6p = (struct sockaddr_in6 *)nam;
25952710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sin6p))
26052710de1SPawel Jakub Dawidek 		return (EINVAL);
261fb59c426SYoshinobu Inoue 	/*
262fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
263fb59c426SYoshinobu Inoue 	 * to them.
264fb59c426SYoshinobu Inoue 	 */
265fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6 &&
26652710de1SPawel Jakub Dawidek 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
26752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
26852710de1SPawel Jakub Dawidek 
26952710de1SPawel Jakub Dawidek 	COMMON_START();
270fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
271fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
27266ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
273fb59c426SYoshinobu Inoue 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
274fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
275fb59c426SYoshinobu Inoue 		else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
276fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
277fb59c426SYoshinobu Inoue 
278fb59c426SYoshinobu Inoue 			in6_sin6_2_sin(&sin, sin6p);
279fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
280fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
281b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
282b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
283fb59c426SYoshinobu Inoue 			goto out;
284fb59c426SYoshinobu Inoue 		}
285fb59c426SYoshinobu Inoue 	}
286b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
287fb59c426SYoshinobu Inoue 	if (error)
288fb59c426SYoshinobu Inoue 		goto out;
289fb59c426SYoshinobu Inoue 	COMMON_END(PRU_BIND);
290fb59c426SYoshinobu Inoue }
291fb59c426SYoshinobu Inoue #endif /* INET6 */
292fb59c426SYoshinobu Inoue 
2932c37256eSGarrett Wollman /*
2942c37256eSGarrett Wollman  * Prepare to accept connections.
2952c37256eSGarrett Wollman  */
2962c37256eSGarrett Wollman static int
297b40ce416SJulian Elischer tcp_usr_listen(struct socket *so, struct thread *td)
2982c37256eSGarrett Wollman {
2992c37256eSGarrett Wollman 	int error = 0;
300f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3012c37256eSGarrett Wollman 	struct tcpcb *tp;
302edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
3032c37256eSGarrett Wollman 
3042c37256eSGarrett Wollman 	COMMON_START();
3050daccb9cSRobert Watson 	SOCK_LOCK(so);
3060daccb9cSRobert Watson 	error = solisten_proto_check(so);
3070daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
308b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3090daccb9cSRobert Watson 	if (error == 0) {
3102c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
3110daccb9cSRobert Watson 		solisten_proto(so);
3120daccb9cSRobert Watson 	}
3130daccb9cSRobert Watson 	SOCK_UNLOCK(so);
3142c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
3152c37256eSGarrett Wollman }
3162c37256eSGarrett Wollman 
317fb59c426SYoshinobu Inoue #ifdef INET6
318fb59c426SYoshinobu Inoue static int
319b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td)
320fb59c426SYoshinobu Inoue {
321fb59c426SYoshinobu Inoue 	int error = 0;
322f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
323fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
324edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
325fb59c426SYoshinobu Inoue 
326fb59c426SYoshinobu Inoue 	COMMON_START();
3270daccb9cSRobert Watson 	SOCK_LOCK(so);
3280daccb9cSRobert Watson 	error = solisten_proto_check(so);
3290daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
330fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
33166ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
332fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
333b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
334fb59c426SYoshinobu Inoue 	}
3350daccb9cSRobert Watson 	if (error == 0) {
336fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
3370daccb9cSRobert Watson 		solisten_proto(so);
3380daccb9cSRobert Watson 	}
3390daccb9cSRobert Watson 	SOCK_UNLOCK(so);
340fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
341fb59c426SYoshinobu Inoue }
342fb59c426SYoshinobu Inoue #endif /* INET6 */
343fb59c426SYoshinobu Inoue 
3442c37256eSGarrett Wollman /*
3452c37256eSGarrett Wollman  * Initiate connection to peer.
3462c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3472c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3482c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3492c37256eSGarrett Wollman  * Send initial segment on connection.
3502c37256eSGarrett Wollman  */
3512c37256eSGarrett Wollman static int
352b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
3532c37256eSGarrett Wollman {
3542c37256eSGarrett Wollman 	int error = 0;
355f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3562c37256eSGarrett Wollman 	struct tcpcb *tp;
3572c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
358f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
3592c37256eSGarrett Wollman 
36057bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
361e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
362e29ef13fSDon Lewis 		return (EINVAL);
36352710de1SPawel Jakub Dawidek 	/*
36452710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
36552710de1SPawel Jakub Dawidek 	 */
3662c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
36752710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
36852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
369812d8653SSam Leffler 	if (jailed(td->td_ucred))
370a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
37175c13541SPoul-Henning Kamp 
37252710de1SPawel Jakub Dawidek 	COMMON_START();
373b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
3742c37256eSGarrett Wollman 		goto out;
3752c37256eSGarrett Wollman 	error = tcp_output(tp);
3762c37256eSGarrett Wollman 	COMMON_END(PRU_CONNECT);
3772c37256eSGarrett Wollman }
3782c37256eSGarrett Wollman 
379fb59c426SYoshinobu Inoue #ifdef INET6
380fb59c426SYoshinobu Inoue static int
381b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
382fb59c426SYoshinobu Inoue {
383fb59c426SYoshinobu Inoue 	int error = 0;
384f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
385fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
386fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
387f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
388fb59c426SYoshinobu Inoue 
389fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
390e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
391e29ef13fSDon Lewis 		return (EINVAL);
39252710de1SPawel Jakub Dawidek 	/*
39352710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
39452710de1SPawel Jakub Dawidek 	 */
395fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
39652710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
39752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
398fb59c426SYoshinobu Inoue 
39952710de1SPawel Jakub Dawidek 	COMMON_START();
40033841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
401fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
402fb59c426SYoshinobu Inoue 
403d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
404d46a5312SMaxim Konovalov 			error = EINVAL;
405d46a5312SMaxim Konovalov 			goto out;
406d46a5312SMaxim Konovalov 		}
40733841545SHajimu UMEMOTO 
408fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
409fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
410fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
411b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
412fb59c426SYoshinobu Inoue 			goto out;
413fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
414fb59c426SYoshinobu Inoue 		goto out;
415fb59c426SYoshinobu Inoue 	}
416fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
417fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
418b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
419b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
420fb59c426SYoshinobu Inoue 		goto out;
421fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
422fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
423fb59c426SYoshinobu Inoue }
424fb59c426SYoshinobu Inoue #endif /* INET6 */
425fb59c426SYoshinobu Inoue 
4262c37256eSGarrett Wollman /*
4272c37256eSGarrett Wollman  * Initiate disconnect from peer.
4282c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
4292c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
4302c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
4312c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
4322c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
4332c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
4342c37256eSGarrett Wollman  *
4352c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
4362c37256eSGarrett Wollman  */
4372c37256eSGarrett Wollman static int
4382c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
4392c37256eSGarrett Wollman {
4402c37256eSGarrett Wollman 	int error = 0;
441f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
4422c37256eSGarrett Wollman 	struct tcpcb *tp;
443f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
4442c37256eSGarrett Wollman 
4452c37256eSGarrett Wollman 	COMMON_START();
4462c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4472c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4482c37256eSGarrett Wollman }
4492c37256eSGarrett Wollman 
4502c37256eSGarrett Wollman /*
4512c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4522c37256eSGarrett Wollman  * done at higher levels; just return the address
4532c37256eSGarrett Wollman  * of the peer, storing through addr.
4542c37256eSGarrett Wollman  */
4552c37256eSGarrett Wollman static int
45657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4572c37256eSGarrett Wollman {
4582c37256eSGarrett Wollman 	int error = 0;
459f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
4601db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
46126ef6ac4SDon Lewis 	struct in_addr addr;
46226ef6ac4SDon Lewis 	in_port_t port = 0;
4631db24ffbSJonathan Lemon 	TCPDEBUG0;
4642c37256eSGarrett Wollman 
465c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
466c0647e0dSJonathan Lemon 		error = ECONNABORTED;
467c0647e0dSJonathan Lemon 		goto out;
468c0647e0dSJonathan Lemon 	}
469f76fcf6dSJeffrey Hsu 
470f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
471f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
472f76fcf6dSJeffrey Hsu 	if (!inp) {
473f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
4741db24ffbSJonathan Lemon 		return (EINVAL);
4751db24ffbSJonathan Lemon 	}
476f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
477f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
4781db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4791db24ffbSJonathan Lemon 	TCPDEBUG1();
480f76fcf6dSJeffrey Hsu 
481f76fcf6dSJeffrey Hsu 	/*
48226ef6ac4SDon Lewis 	 * We inline in_setpeeraddr and COMMON_END here, so that we can
48326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
48426ef6ac4SDon Lewis 	 * release the lock.
485f76fcf6dSJeffrey Hsu 	 */
48626ef6ac4SDon Lewis 	port = inp->inp_fport;
48726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
488f76fcf6dSJeffrey Hsu 
48926ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
49026ef6ac4SDon Lewis 	if (tp)
49126ef6ac4SDon Lewis 		INP_UNLOCK(inp);
49226ef6ac4SDon Lewis 	if (error == 0)
49326ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
49426ef6ac4SDon Lewis 	return error;
4952c37256eSGarrett Wollman }
4962c37256eSGarrett Wollman 
497fb59c426SYoshinobu Inoue #ifdef INET6
498fb59c426SYoshinobu Inoue static int
499fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
500fb59c426SYoshinobu Inoue {
501f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
502fb59c426SYoshinobu Inoue 	int error = 0;
5031db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
50426ef6ac4SDon Lewis 	struct in_addr addr;
50526ef6ac4SDon Lewis 	struct in6_addr addr6;
50626ef6ac4SDon Lewis 	in_port_t port = 0;
50726ef6ac4SDon Lewis 	int v4 = 0;
5081db24ffbSJonathan Lemon 	TCPDEBUG0;
509fb59c426SYoshinobu Inoue 
510c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
511c0647e0dSJonathan Lemon 		error = ECONNABORTED;
512c0647e0dSJonathan Lemon 		goto out;
513c0647e0dSJonathan Lemon 	}
514f76fcf6dSJeffrey Hsu 
515f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
516f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
5171db24ffbSJonathan Lemon 	if (inp == 0) {
518f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
5191db24ffbSJonathan Lemon 		return (EINVAL);
5201db24ffbSJonathan Lemon 	}
521f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
522f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
5231db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
5241db24ffbSJonathan Lemon 	TCPDEBUG1();
52526ef6ac4SDon Lewis 	/*
52626ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
52726ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
52826ef6ac4SDon Lewis 	 * release the lock.
52926ef6ac4SDon Lewis 	 */
53026ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
53126ef6ac4SDon Lewis 		v4 = 1;
53226ef6ac4SDon Lewis 		port = inp->inp_fport;
53326ef6ac4SDon Lewis 		addr = inp->inp_faddr;
53426ef6ac4SDon Lewis 	} else {
53526ef6ac4SDon Lewis 		port = inp->inp_fport;
53626ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
53726ef6ac4SDon Lewis 	}
53826ef6ac4SDon Lewis 
53926ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
54026ef6ac4SDon Lewis 	if (tp)
54126ef6ac4SDon Lewis 		INP_UNLOCK(inp);
54226ef6ac4SDon Lewis 	if (error == 0) {
54326ef6ac4SDon Lewis 		if (v4)
54426ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
54526ef6ac4SDon Lewis 		else
54626ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
54726ef6ac4SDon Lewis 	}
54826ef6ac4SDon Lewis 	return error;
549fb59c426SYoshinobu Inoue }
550fb59c426SYoshinobu Inoue #endif /* INET6 */
551f76fcf6dSJeffrey Hsu 
552f76fcf6dSJeffrey Hsu /*
553f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
554f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
555f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
556f76fcf6dSJeffrey Hsu  */
557f76fcf6dSJeffrey Hsu static int
558f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
559f76fcf6dSJeffrey Hsu {
560f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
561f76fcf6dSJeffrey Hsu }
562f76fcf6dSJeffrey Hsu 
563f76fcf6dSJeffrey Hsu /*
564f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
565f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
566f76fcf6dSJeffrey Hsu  */
567f76fcf6dSJeffrey Hsu static int
568f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
569f76fcf6dSJeffrey Hsu {
570f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
571f76fcf6dSJeffrey Hsu }
572f76fcf6dSJeffrey Hsu 
5732c37256eSGarrett Wollman /*
5742c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
5752c37256eSGarrett Wollman  */
5762c37256eSGarrett Wollman static int
5772c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
5782c37256eSGarrett Wollman {
5792c37256eSGarrett Wollman 	int error = 0;
580f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
5812c37256eSGarrett Wollman 	struct tcpcb *tp;
582f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
5832c37256eSGarrett Wollman 
5842c37256eSGarrett Wollman 	COMMON_START();
5852c37256eSGarrett Wollman 	socantsendmore(so);
5862c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
5872c37256eSGarrett Wollman 	if (tp)
5882c37256eSGarrett Wollman 		error = tcp_output(tp);
5892c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
5902c37256eSGarrett Wollman }
5912c37256eSGarrett Wollman 
5922c37256eSGarrett Wollman /*
5932c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
5942c37256eSGarrett Wollman  */
5952c37256eSGarrett Wollman static int
5962c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
5972c37256eSGarrett Wollman {
5982c37256eSGarrett Wollman 	int error = 0;
599f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6002c37256eSGarrett Wollman 	struct tcpcb *tp;
601f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
6022c37256eSGarrett Wollman 
6032c37256eSGarrett Wollman 	COMMON_START();
6042c37256eSGarrett Wollman 	tcp_output(tp);
6052c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
6062c37256eSGarrett Wollman }
6072c37256eSGarrett Wollman 
6082c37256eSGarrett Wollman /*
6092c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
6109c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
6119c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
6129c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
6139c9906e9SPeter Wemm  * generally are caller-frees.
6142c37256eSGarrett Wollman  */
6152c37256eSGarrett Wollman static int
61657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
617b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
6182c37256eSGarrett Wollman {
6192c37256eSGarrett Wollman 	int error = 0;
620f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6212c37256eSGarrett Wollman 	struct tcpcb *tp;
622f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
623d1401c90SRobert Watson 	int unlocked = 0;
624fb59c426SYoshinobu Inoue #ifdef INET6
625fb59c426SYoshinobu Inoue 	int isipv6;
626fb59c426SYoshinobu Inoue #endif
6279c9906e9SPeter Wemm 	TCPDEBUG0;
6282c37256eSGarrett Wollman 
629f76fcf6dSJeffrey Hsu 	/*
630f76fcf6dSJeffrey Hsu 	 * Need write lock here because this function might call
631f76fcf6dSJeffrey Hsu 	 * tcp_connect or tcp_usrclosed.
632f76fcf6dSJeffrey Hsu 	 * We really want to have to this function upgrade from read lock
633f76fcf6dSJeffrey Hsu 	 * to write lock.  XXX
634f76fcf6dSJeffrey Hsu 	 */
635f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
636f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
6379c9906e9SPeter Wemm 	if (inp == NULL) {
6389c9906e9SPeter Wemm 		/*
6399c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
640c0b99ffaSRobert Watson 		 * we checked SBS_CANTSENDMORE, eg: while doing uiomove or a
6419c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
6429c9906e9SPeter Wemm 		 */
6439c9906e9SPeter Wemm 		if (m)
6449c9906e9SPeter Wemm 			m_freem(m);
6459c9906e9SPeter Wemm 		if (control)
6469c9906e9SPeter Wemm 			m_freem(control);
6479c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
64845d3a132SPeter Wemm 		tp = NULL;
64945d3a132SPeter Wemm 		TCPDEBUG1();
6509c9906e9SPeter Wemm 		goto out;
6519c9906e9SPeter Wemm 	}
652f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
653fb59c426SYoshinobu Inoue #ifdef INET6
654fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
655fb59c426SYoshinobu Inoue #endif /* INET6 */
6569c9906e9SPeter Wemm 	tp = intotcpcb(inp);
6579c9906e9SPeter Wemm 	TCPDEBUG1();
6589c9906e9SPeter Wemm 	if (control) {
6599c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
6609c9906e9SPeter Wemm 		if (control->m_len) {
6619c9906e9SPeter Wemm 			m_freem(control);
6622c37256eSGarrett Wollman 			if (m)
6632c37256eSGarrett Wollman 				m_freem(m);
664744f87eaSDavid Greenman 			error = EINVAL;
665744f87eaSDavid Greenman 			goto out;
6662c37256eSGarrett Wollman 		}
6679c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
6689c9906e9SPeter Wemm 	}
6692c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
670395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
6712c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
6722c37256eSGarrett Wollman 			/*
6732c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
6742c37256eSGarrett Wollman 			 * initialize window to default value, and
6752c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
6762c37256eSGarrett Wollman 			 * MSS.
6772c37256eSGarrett Wollman 			 */
678fb59c426SYoshinobu Inoue #ifdef INET6
679fb59c426SYoshinobu Inoue 			if (isipv6)
680b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
681fb59c426SYoshinobu Inoue 			else
682fb59c426SYoshinobu Inoue #endif /* INET6 */
683b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
6842c37256eSGarrett Wollman 			if (error)
6852c37256eSGarrett Wollman 				goto out;
6862c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
6872c37256eSGarrett Wollman 			tcp_mss(tp, -1);
6882c37256eSGarrett Wollman 		}
6892c37256eSGarrett Wollman 
6902c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
6912c37256eSGarrett Wollman 			/*
6922c37256eSGarrett Wollman 			 * Close the send side of the connection after
6932c37256eSGarrett Wollman 			 * the data is sent.
6942c37256eSGarrett Wollman 			 */
6952c37256eSGarrett Wollman 			socantsendmore(so);
6962c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
6972c37256eSGarrett Wollman 		}
698d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
699d1401c90SRobert Watson 		unlocked = 1;
700b0acefa8SBill Fenner 		if (tp != NULL) {
701b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
702b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
7032c37256eSGarrett Wollman 			error = tcp_output(tp);
704b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
705b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
706b0acefa8SBill Fenner 		}
7072c37256eSGarrett Wollman 	} else {
708d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
7092c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
710d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
7112c37256eSGarrett Wollman 			m_freem(m);
7122c37256eSGarrett Wollman 			error = ENOBUFS;
7132c37256eSGarrett Wollman 			goto out;
7142c37256eSGarrett Wollman 		}
7152c37256eSGarrett Wollman 		/*
7162c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
7172c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
7182c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
7192c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
7202c37256eSGarrett Wollman 		 * of data past the urgent section.
7212c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
7222c37256eSGarrett Wollman 		 */
723d2bc35abSRobert Watson 		sbappendstream_locked(&so->so_snd, m);
724d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
725ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
726ef53690bSGarrett Wollman 			/*
727ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
728ef53690bSGarrett Wollman 			 * initialize window to default value, and
729ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
730ef53690bSGarrett Wollman 			 * MSS.
731ef53690bSGarrett Wollman 			 */
732fb59c426SYoshinobu Inoue #ifdef INET6
733fb59c426SYoshinobu Inoue 			if (isipv6)
734b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
735fb59c426SYoshinobu Inoue 			else
736fb59c426SYoshinobu Inoue #endif /* INET6 */
737b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
738ef53690bSGarrett Wollman 			if (error)
739ef53690bSGarrett Wollman 				goto out;
740ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
741ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
742ef53690bSGarrett Wollman 		}
743d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
744d1401c90SRobert Watson 		unlocked = 1;
7452c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
7462c37256eSGarrett Wollman 		tp->t_force = 1;
7472c37256eSGarrett Wollman 		error = tcp_output(tp);
7482c37256eSGarrett Wollman 		tp->t_force = 0;
7492c37256eSGarrett Wollman 	}
750d1401c90SRobert Watson out:
751d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
7522c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
753d1401c90SRobert Watson 	if (tp)
754d1401c90SRobert Watson 		INP_UNLOCK(inp);
755d1401c90SRobert Watson 	if (!unlocked)
756d1401c90SRobert Watson 		INP_INFO_WUNLOCK(&tcbinfo);
757d1401c90SRobert Watson 	return (error):
7582c37256eSGarrett Wollman }
7592c37256eSGarrett Wollman 
7602c37256eSGarrett Wollman /*
7612c37256eSGarrett Wollman  * Abort the TCP.
7622c37256eSGarrett Wollman  */
7632c37256eSGarrett Wollman static int
7642c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
7652c37256eSGarrett Wollman {
7662c37256eSGarrett Wollman 	int error = 0;
767f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7682c37256eSGarrett Wollman 	struct tcpcb *tp;
769f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
7702c37256eSGarrett Wollman 
7712c37256eSGarrett Wollman 	COMMON_START();
7722c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
7732c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
7742c37256eSGarrett Wollman }
7752c37256eSGarrett Wollman 
7762c37256eSGarrett Wollman /*
7772c37256eSGarrett Wollman  * Receive out-of-band data.
7782c37256eSGarrett Wollman  */
7792c37256eSGarrett Wollman static int
7802c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
7812c37256eSGarrett Wollman {
7822c37256eSGarrett Wollman 	int error = 0;
783f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7842c37256eSGarrett Wollman 	struct tcpcb *tp;
785f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
7862c37256eSGarrett Wollman 
7872c37256eSGarrett Wollman 	COMMON_START();
7882c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
789c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
7904cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
7914cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
7922c37256eSGarrett Wollman 		error = EINVAL;
7932c37256eSGarrett Wollman 		goto out;
7942c37256eSGarrett Wollman 	}
7952c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
7962c37256eSGarrett Wollman 		error = EWOULDBLOCK;
7972c37256eSGarrett Wollman 		goto out;
7982c37256eSGarrett Wollman 	}
7992c37256eSGarrett Wollman 	m->m_len = 1;
8002c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
8012c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
8022c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
8032c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
8042c37256eSGarrett Wollman }
8052c37256eSGarrett Wollman 
8062c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
807756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
808756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
809756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
810756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
811756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
812756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
813756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
814756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
815756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
816756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		tcp_peeraddr,
817756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
818756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
819756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
820756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
821756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		tcp_sockaddr,
822756d52a1SPoul-Henning Kamp 	.pru_sosetlabel =	in_pcbsosetlabel
8232c37256eSGarrett Wollman };
824df8bae1dSRodney W. Grimes 
825fb59c426SYoshinobu Inoue #ifdef INET6
826fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
827756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
828756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
829756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
830756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
831756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
832756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
833756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
834756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
835756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
836756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
837756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
838756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
839756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
840756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
841756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
842756d52a1SPoul-Henning Kamp  	.pru_sosetlabel =	in_pcbsosetlabel
843fb59c426SYoshinobu Inoue };
844fb59c426SYoshinobu Inoue #endif /* INET6 */
845fb59c426SYoshinobu Inoue 
846a0292f23SGarrett Wollman /*
847a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
848a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
8495200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
8505200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
8515200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
8525200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
853a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
854a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
855a0292f23SGarrett Wollman  */
8560312fbe9SPoul-Henning Kamp static int
857b40ce416SJulian Elischer tcp_connect(tp, nam, td)
858a0292f23SGarrett Wollman 	register struct tcpcb *tp;
85957bf258eSGarrett Wollman 	struct sockaddr *nam;
860b40ce416SJulian Elischer 	struct thread *td;
861a0292f23SGarrett Wollman {
862a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
863a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
8645200e00eSIan Dowse 	struct in_addr laddr;
8655200e00eSIan Dowse 	u_short lport;
866c3229e05SDavid Greenman 	int error;
867a0292f23SGarrett Wollman 
868a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
869b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
870a0292f23SGarrett Wollman 		if (error)
871a0292f23SGarrett Wollman 			return error;
872a0292f23SGarrett Wollman 	}
873a0292f23SGarrett Wollman 
874a0292f23SGarrett Wollman 	/*
875a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
876a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
877a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
878a0292f23SGarrett Wollman 	 */
8795200e00eSIan Dowse 	laddr = inp->inp_laddr;
8805200e00eSIan Dowse 	lport = inp->inp_lport;
8815200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
882b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
8835200e00eSIan Dowse 	if (error && oinp == NULL)
884d3628763SRodney W. Grimes 		return error;
885c94c54e4SAndre Oppermann 	if (oinp)
886a0292f23SGarrett Wollman 		return EADDRINUSE;
8875200e00eSIan Dowse 	inp->inp_laddr = laddr;
88815bd2b43SDavid Greenman 	in_pcbrehash(inp);
889a0292f23SGarrett Wollman 
890a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
891a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
892a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
893a0292f23SGarrett Wollman 		tp->request_r_scale++;
894a0292f23SGarrett Wollman 
895a0292f23SGarrett Wollman 	soisconnecting(so);
896a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
897a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
8989b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
899b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
9001fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
901a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
902a45d2726SAndras Olah 
903a0292f23SGarrett Wollman 	return 0;
904a0292f23SGarrett Wollman }
905a0292f23SGarrett Wollman 
906fb59c426SYoshinobu Inoue #ifdef INET6
907fb59c426SYoshinobu Inoue static int
908b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
909fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
910fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
911b40ce416SJulian Elischer 	struct thread *td;
912fb59c426SYoshinobu Inoue {
913fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
914fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
915fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
916fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
917fb59c426SYoshinobu Inoue 	int error;
918fb59c426SYoshinobu Inoue 
919fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
920b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
921fb59c426SYoshinobu Inoue 		if (error)
922fb59c426SYoshinobu Inoue 			return error;
923fb59c426SYoshinobu Inoue 	}
924fb59c426SYoshinobu Inoue 
925fb59c426SYoshinobu Inoue 	/*
926fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
927fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
928fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
929fb59c426SYoshinobu Inoue 	 */
930fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
931fb59c426SYoshinobu Inoue 	if (error)
932fb59c426SYoshinobu Inoue 		return error;
933fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
934fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
935fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
936fb59c426SYoshinobu Inoue 				  ? addr6
937fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
938fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
939c94c54e4SAndre Oppermann 	if (oinp)
940fb59c426SYoshinobu Inoue 		return EADDRINUSE;
941fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
942fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
943fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
944fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
9458a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
9468a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
9478a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
9488a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
9498a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
950fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
951fb59c426SYoshinobu Inoue 
952fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
953fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
954fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
955fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
956fb59c426SYoshinobu Inoue 
957fb59c426SYoshinobu Inoue 	soisconnecting(so);
958fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
959fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
960fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
961b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
9621fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
963fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
964fb59c426SYoshinobu Inoue 
965fb59c426SYoshinobu Inoue 	return 0;
966fb59c426SYoshinobu Inoue }
967fb59c426SYoshinobu Inoue #endif /* INET6 */
968fb59c426SYoshinobu Inoue 
969cfe8b629SGarrett Wollman /*
970b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
971b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
972b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
973b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
974b8af5dfaSRobert Watson  * from Linux.
975b8af5dfaSRobert Watson  */
976b8af5dfaSRobert Watson static void
977b8af5dfaSRobert Watson tcp_fill_info(tp, ti)
978b8af5dfaSRobert Watson 	struct tcpcb *tp;
979b8af5dfaSRobert Watson 	struct tcp_info *ti;
980b8af5dfaSRobert Watson {
981b8af5dfaSRobert Watson 
982b8af5dfaSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
983b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
984b8af5dfaSRobert Watson 
985b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
986b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
987b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
988b8af5dfaSRobert Watson 	if (tp->sack_enable)
989b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
990b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
991b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
992b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
993b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
994b8af5dfaSRobert Watson 	}
995b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
996b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
997b8af5dfaSRobert Watson 
998b8af5dfaSRobert Watson 	/*
999b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1000b8af5dfaSRobert Watson 	 */
1001c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1002b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
1003b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
1004b8af5dfaSRobert Watson }
1005b8af5dfaSRobert Watson 
1006b8af5dfaSRobert Watson /*
1007cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
1008cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
1009cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
1010cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
1011cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
1012b8af5dfaSRobert Watson  *
1013b8af5dfaSRobert Watson  * XXXRW: The locking here is wrong; we may take a page fault while holding
1014b8af5dfaSRobert Watson  * the inpcb lock.
1015cfe8b629SGarrett Wollman  */
1016df8bae1dSRodney W. Grimes int
1017cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
1018df8bae1dSRodney W. Grimes 	struct socket *so;
1019cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1020df8bae1dSRodney W. Grimes {
10213f9d1ef9SRobert Watson 	int	error, opt, optval;
1022df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1023cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
1024b8af5dfaSRobert Watson 	struct	tcp_info ti;
1025df8bae1dSRodney W. Grimes 
1026cfe8b629SGarrett Wollman 	error = 0;
1027f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
1028df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1029df8bae1dSRodney W. Grimes 	if (inp == NULL) {
1030f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
1031df8bae1dSRodney W. Grimes 		return (ECONNRESET);
1032df8bae1dSRodney W. Grimes 	}
1033f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1034f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
1035cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
10364e397bc5SRobert Watson 		INP_UNLOCK(inp);
1037fb59c426SYoshinobu Inoue #ifdef INET6
1038fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1039fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1040fb59c426SYoshinobu Inoue 		else
1041fb59c426SYoshinobu Inoue #endif /* INET6 */
1042cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1043df8bae1dSRodney W. Grimes 		return (error);
1044df8bae1dSRodney W. Grimes 	}
1045df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1046df8bae1dSRodney W. Grimes 
1047cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1048cfe8b629SGarrett Wollman 	case SOPT_SET:
1049cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
10501cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
105188f6b043SBruce M Simpson 		case TCP_MD5SIG:
10521cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
10531cfd4b53SBruce M Simpson 					    sizeof optval);
10541cfd4b53SBruce M Simpson 			if (error)
10551cfd4b53SBruce M Simpson 				break;
10561cfd4b53SBruce M Simpson 
10571cfd4b53SBruce M Simpson 			if (optval > 0)
10581cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
10591cfd4b53SBruce M Simpson 			else
10601cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
10611cfd4b53SBruce M Simpson 			break;
10621cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1063df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1064cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1065cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1066cfe8b629SGarrett Wollman 					    sizeof optval);
1067cfe8b629SGarrett Wollman 			if (error)
1068cfe8b629SGarrett Wollman 				break;
1069cfe8b629SGarrett Wollman 
1070cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1071cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1072cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1073cfe8b629SGarrett Wollman 				break;
1074cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1075cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1076cfe8b629SGarrett Wollman 				break;
1077cfe8b629SGarrett Wollman 			default:
1078cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1079cfe8b629SGarrett Wollman 				break;
1080cfe8b629SGarrett Wollman 			}
1081cfe8b629SGarrett Wollman 
1082cfe8b629SGarrett Wollman 			if (optval)
1083cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1084df8bae1dSRodney W. Grimes 			else
1085cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1086df8bae1dSRodney W. Grimes 			break;
1087df8bae1dSRodney W. Grimes 
1088007581c0SJonathan Lemon 		case TCP_NOPUSH:
1089007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1090007581c0SJonathan Lemon 					    sizeof optval);
1091007581c0SJonathan Lemon 			if (error)
1092007581c0SJonathan Lemon 				break;
1093007581c0SJonathan Lemon 
1094007581c0SJonathan Lemon 			if (optval)
1095007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1096007581c0SJonathan Lemon 			else {
1097007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1098007581c0SJonathan Lemon 				error = tcp_output(tp);
1099007581c0SJonathan Lemon 			}
1100007581c0SJonathan Lemon 			break;
1101007581c0SJonathan Lemon 
1102df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1103cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1104cfe8b629SGarrett Wollman 					    sizeof optval);
1105cfe8b629SGarrett Wollman 			if (error)
1106df8bae1dSRodney W. Grimes 				break;
1107df8bae1dSRodney W. Grimes 
110853369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
110953369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1110cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1111a0292f23SGarrett Wollman 			else
1112a0292f23SGarrett Wollman 				error = EINVAL;
1113a0292f23SGarrett Wollman 			break;
1114a0292f23SGarrett Wollman 
1115b8af5dfaSRobert Watson 		case TCP_INFO:
1116b8af5dfaSRobert Watson 			error = EINVAL;
1117b8af5dfaSRobert Watson 			break;
1118b8af5dfaSRobert Watson 
1119df8bae1dSRodney W. Grimes 		default:
1120df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1121df8bae1dSRodney W. Grimes 			break;
1122df8bae1dSRodney W. Grimes 		}
1123df8bae1dSRodney W. Grimes 		break;
1124df8bae1dSRodney W. Grimes 
1125cfe8b629SGarrett Wollman 	case SOPT_GET:
1126cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
11271cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
112888f6b043SBruce M Simpson 		case TCP_MD5SIG:
11291cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1130b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
11311cfd4b53SBruce M Simpson 			break;
1132265ed012SBruce M Simpson #endif
1133df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1134cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1135b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1136df8bae1dSRodney W. Grimes 			break;
1137df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1138cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1139b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1140df8bae1dSRodney W. Grimes 			break;
1141a0292f23SGarrett Wollman 		case TCP_NOOPT:
1142cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1143b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1144a0292f23SGarrett Wollman 			break;
1145a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1146cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1147b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1148b8af5dfaSRobert Watson 			break;
1149b8af5dfaSRobert Watson 		case TCP_INFO:
1150b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
1151b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1152a0292f23SGarrett Wollman 			break;
1153df8bae1dSRodney W. Grimes 		default:
1154df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1155df8bae1dSRodney W. Grimes 			break;
1156df8bae1dSRodney W. Grimes 		}
1157df8bae1dSRodney W. Grimes 		break;
1158df8bae1dSRodney W. Grimes 	}
1159f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1160df8bae1dSRodney W. Grimes 	return (error);
1161df8bae1dSRodney W. Grimes }
1162df8bae1dSRodney W. Grimes 
116326e30fbbSDavid Greenman /*
116426e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
116526e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
116626e30fbbSDavid Greenman  * be set by the route).
116726e30fbbSDavid Greenman  */
116881e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
11693d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
11703d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
117181e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
11723d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
11733d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1174df8bae1dSRodney W. Grimes 
1175df8bae1dSRodney W. Grimes /*
1176df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1177df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1178df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1179df8bae1dSRodney W. Grimes  */
11800312fbe9SPoul-Henning Kamp static int
118156dc72c3SPawel Jakub Dawidek tcp_attach(so)
1182df8bae1dSRodney W. Grimes 	struct socket *so;
1183df8bae1dSRodney W. Grimes {
1184df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1185df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1186df8bae1dSRodney W. Grimes 	int error;
1187fb59c426SYoshinobu Inoue #ifdef INET6
11884a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1189fb59c426SYoshinobu Inoue #endif
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1192df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1193df8bae1dSRodney W. Grimes 		if (error)
1194df8bae1dSRodney W. Grimes 			return (error);
1195df8bae1dSRodney W. Grimes 	}
11966823b823SPawel Jakub Dawidek 	error = in_pcballoc(so, &tcbinfo, "tcpinp");
1197df8bae1dSRodney W. Grimes 	if (error)
1198df8bae1dSRodney W. Grimes 		return (error);
1199df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1200fb59c426SYoshinobu Inoue #ifdef INET6
1201fb59c426SYoshinobu Inoue 	if (isipv6) {
1202fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1203fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1204fb59c426SYoshinobu Inoue 	}
1205fb59c426SYoshinobu Inoue 	else
1206fb59c426SYoshinobu Inoue #endif
1207cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1208df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1209df8bae1dSRodney W. Grimes 	if (tp == 0) {
12104cc20ab1SSeigo Tanimura 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1211df8bae1dSRodney W. Grimes 
1212df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1213fb59c426SYoshinobu Inoue #ifdef INET6
1214fb59c426SYoshinobu Inoue 		if (isipv6)
1215fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1216fb59c426SYoshinobu Inoue 		else
1217fb59c426SYoshinobu Inoue #endif
1218df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1219df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1220df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1221df8bae1dSRodney W. Grimes 	}
1222df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1223df8bae1dSRodney W. Grimes 	return (0);
1224df8bae1dSRodney W. Grimes }
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes /*
1227df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1228df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1229df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1230df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1231df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1232df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1233df8bae1dSRodney W. Grimes  */
12340312fbe9SPoul-Henning Kamp static struct tcpcb *
1235df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1236df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1237df8bae1dSRodney W. Grimes {
1238df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1241df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
12424cc20ab1SSeigo Tanimura 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1243243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
12444cc20ab1SSeigo Tanimura 	else {
1245df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1246df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1247df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1248df8bae1dSRodney W. Grimes 		if (tp)
1249df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1250df8bae1dSRodney W. Grimes 	}
1251df8bae1dSRodney W. Grimes 	return (tp);
1252df8bae1dSRodney W. Grimes }
1253df8bae1dSRodney W. Grimes 
1254df8bae1dSRodney W. Grimes /*
1255df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1256df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1257df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1258df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1259df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1260df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1261df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1262df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1263df8bae1dSRodney W. Grimes  */
12640312fbe9SPoul-Henning Kamp static struct tcpcb *
1265df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1266df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1267df8bae1dSRodney W. Grimes {
1268df8bae1dSRodney W. Grimes 
1269df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1270df8bae1dSRodney W. Grimes 
1271df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1272df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1273df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1274df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1275df8bae1dSRodney W. Grimes 		break;
1276df8bae1dSRodney W. Grimes 
1277a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1278df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1279a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1280a0292f23SGarrett Wollman 		break;
1281a0292f23SGarrett Wollman 
1282df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1283df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1284df8bae1dSRodney W. Grimes 		break;
1285df8bae1dSRodney W. Grimes 
1286df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1287df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1288df8bae1dSRodney W. Grimes 		break;
1289df8bae1dSRodney W. Grimes 	}
1290b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1291df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1292b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1293b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
12949b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
12959b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1296b6239c4aSAndras Olah 	}
1297df8bae1dSRodney W. Grimes 	return (tp);
1298df8bae1dSRodney W. Grimes }
1299