xref: /freebsd/sys/netinet/tcp_usrreq.c (revision b8af5dfa81f1fdca5ed77f8f339a5b522d10e714)
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  * 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();
3052c37256eSGarrett Wollman 	if (inp->inp_lport == 0)
306b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
3072c37256eSGarrett Wollman 	if (error == 0)
3082c37256eSGarrett Wollman 		tp->t_state = TCPS_LISTEN;
3092c37256eSGarrett Wollman 	COMMON_END(PRU_LISTEN);
3102c37256eSGarrett Wollman }
3112c37256eSGarrett Wollman 
312fb59c426SYoshinobu Inoue #ifdef INET6
313fb59c426SYoshinobu Inoue static int
314b40ce416SJulian Elischer tcp6_usr_listen(struct socket *so, struct thread *td)
315fb59c426SYoshinobu Inoue {
316fb59c426SYoshinobu Inoue 	int error = 0;
317f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
318fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
319edf02ff1SJeffrey Hsu 	const int inirw = INI_WRITE;
320fb59c426SYoshinobu Inoue 
321fb59c426SYoshinobu Inoue 	COMMON_START();
322fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
323fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
32466ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
325fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
326b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
327fb59c426SYoshinobu Inoue 	}
328fb59c426SYoshinobu Inoue 	if (error == 0)
329fb59c426SYoshinobu Inoue 		tp->t_state = TCPS_LISTEN;
330fb59c426SYoshinobu Inoue 	COMMON_END(PRU_LISTEN);
331fb59c426SYoshinobu Inoue }
332fb59c426SYoshinobu Inoue #endif /* INET6 */
333fb59c426SYoshinobu Inoue 
3342c37256eSGarrett Wollman /*
3352c37256eSGarrett Wollman  * Initiate connection to peer.
3362c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
3372c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
3382c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
3392c37256eSGarrett Wollman  * Send initial segment on connection.
3402c37256eSGarrett Wollman  */
3412c37256eSGarrett Wollman static int
342b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
3432c37256eSGarrett Wollman {
3442c37256eSGarrett Wollman 	int error = 0;
345f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
3462c37256eSGarrett Wollman 	struct tcpcb *tp;
3472c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
348f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
3492c37256eSGarrett Wollman 
35057bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
351e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
352e29ef13fSDon Lewis 		return (EINVAL);
35352710de1SPawel Jakub Dawidek 	/*
35452710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
35552710de1SPawel Jakub Dawidek 	 */
3562c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
35752710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
35852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
359a854ed98SJohn Baldwin 	if (td && jailed(td->td_ucred))
360a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
36175c13541SPoul-Henning Kamp 
36252710de1SPawel Jakub Dawidek 	COMMON_START();
363b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
3642c37256eSGarrett Wollman 		goto out;
3652c37256eSGarrett Wollman 	error = tcp_output(tp);
3662c37256eSGarrett Wollman 	COMMON_END(PRU_CONNECT);
3672c37256eSGarrett Wollman }
3682c37256eSGarrett Wollman 
369fb59c426SYoshinobu Inoue #ifdef INET6
370fb59c426SYoshinobu Inoue static int
371b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
372fb59c426SYoshinobu Inoue {
373fb59c426SYoshinobu Inoue 	int error = 0;
374f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
375fb59c426SYoshinobu Inoue 	struct tcpcb *tp;
376fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6p;
377f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
378fb59c426SYoshinobu Inoue 
379fb59c426SYoshinobu Inoue 	sin6p = (struct sockaddr_in6 *)nam;
380e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sin6p))
381e29ef13fSDon Lewis 		return (EINVAL);
38252710de1SPawel Jakub Dawidek 	/*
38352710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
38452710de1SPawel Jakub Dawidek 	 */
385fb59c426SYoshinobu Inoue 	if (sin6p->sin6_family == AF_INET6
38652710de1SPawel Jakub Dawidek 	    && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
38752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
388fb59c426SYoshinobu Inoue 
38952710de1SPawel Jakub Dawidek 	COMMON_START();
39033841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
391fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
392fb59c426SYoshinobu Inoue 
393d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
394d46a5312SMaxim Konovalov 			error = EINVAL;
395d46a5312SMaxim Konovalov 			goto out;
396d46a5312SMaxim Konovalov 		}
39733841545SHajimu UMEMOTO 
398fb59c426SYoshinobu Inoue 		in6_sin6_2_sin(&sin, sin6p);
399fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV4;
400fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV6;
401b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
402fb59c426SYoshinobu Inoue 			goto out;
403fb59c426SYoshinobu Inoue 		error = tcp_output(tp);
404fb59c426SYoshinobu Inoue 		goto out;
405fb59c426SYoshinobu Inoue 	}
406fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
407fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
408b7d6d952SHajimu UMEMOTO 	inp->inp_inc.inc_isipv6 = 1;
409b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
410fb59c426SYoshinobu Inoue 		goto out;
411fb59c426SYoshinobu Inoue 	error = tcp_output(tp);
412fb59c426SYoshinobu Inoue 	COMMON_END(PRU_CONNECT);
413fb59c426SYoshinobu Inoue }
414fb59c426SYoshinobu Inoue #endif /* INET6 */
415fb59c426SYoshinobu Inoue 
4162c37256eSGarrett Wollman /*
4172c37256eSGarrett Wollman  * Initiate disconnect from peer.
4182c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
4192c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
4202c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
4212c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
4222c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
4232c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
4242c37256eSGarrett Wollman  *
4252c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
4262c37256eSGarrett Wollman  */
4272c37256eSGarrett Wollman static int
4282c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
4292c37256eSGarrett Wollman {
4302c37256eSGarrett Wollman 	int error = 0;
431f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
4322c37256eSGarrett Wollman 	struct tcpcb *tp;
433f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
4342c37256eSGarrett Wollman 
4352c37256eSGarrett Wollman 	COMMON_START();
4362c37256eSGarrett Wollman 	tp = tcp_disconnect(tp);
4372c37256eSGarrett Wollman 	COMMON_END(PRU_DISCONNECT);
4382c37256eSGarrett Wollman }
4392c37256eSGarrett Wollman 
4402c37256eSGarrett Wollman /*
4412c37256eSGarrett Wollman  * Accept a connection.  Essentially all the work is
4422c37256eSGarrett Wollman  * done at higher levels; just return the address
4432c37256eSGarrett Wollman  * of the peer, storing through addr.
4442c37256eSGarrett Wollman  */
4452c37256eSGarrett Wollman static int
44657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
4472c37256eSGarrett Wollman {
4482c37256eSGarrett Wollman 	int error = 0;
449f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
4501db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
45126ef6ac4SDon Lewis 	struct in_addr addr;
45226ef6ac4SDon Lewis 	in_port_t port = 0;
4531db24ffbSJonathan Lemon 	TCPDEBUG0;
4542c37256eSGarrett Wollman 
455c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
456c0647e0dSJonathan Lemon 		error = ECONNABORTED;
457c0647e0dSJonathan Lemon 		goto out;
458c0647e0dSJonathan Lemon 	}
459f76fcf6dSJeffrey Hsu 
460f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
461f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
462f76fcf6dSJeffrey Hsu 	if (!inp) {
463f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
4641db24ffbSJonathan Lemon 		return (EINVAL);
4651db24ffbSJonathan Lemon 	}
466f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
467f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
4681db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
4691db24ffbSJonathan Lemon 	TCPDEBUG1();
470f76fcf6dSJeffrey Hsu 
471f76fcf6dSJeffrey Hsu 	/*
47226ef6ac4SDon Lewis 	 * We inline in_setpeeraddr and COMMON_END here, so that we can
47326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
47426ef6ac4SDon Lewis 	 * release the lock.
475f76fcf6dSJeffrey Hsu 	 */
47626ef6ac4SDon Lewis 	port = inp->inp_fport;
47726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
478f76fcf6dSJeffrey Hsu 
47926ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
48026ef6ac4SDon Lewis 	if (tp)
48126ef6ac4SDon Lewis 		INP_UNLOCK(inp);
48226ef6ac4SDon Lewis 	if (error == 0)
48326ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
48426ef6ac4SDon Lewis 	return error;
4852c37256eSGarrett Wollman }
4862c37256eSGarrett Wollman 
487fb59c426SYoshinobu Inoue #ifdef INET6
488fb59c426SYoshinobu Inoue static int
489fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
490fb59c426SYoshinobu Inoue {
491f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
492fb59c426SYoshinobu Inoue 	int error = 0;
4931db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
49426ef6ac4SDon Lewis 	struct in_addr addr;
49526ef6ac4SDon Lewis 	struct in6_addr addr6;
49626ef6ac4SDon Lewis 	in_port_t port = 0;
49726ef6ac4SDon Lewis 	int v4 = 0;
4981db24ffbSJonathan Lemon 	TCPDEBUG0;
499fb59c426SYoshinobu Inoue 
500c0647e0dSJonathan Lemon 	if (so->so_state & SS_ISDISCONNECTED) {
501c0647e0dSJonathan Lemon 		error = ECONNABORTED;
502c0647e0dSJonathan Lemon 		goto out;
503c0647e0dSJonathan Lemon 	}
504f76fcf6dSJeffrey Hsu 
505f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
506f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
5071db24ffbSJonathan Lemon 	if (inp == 0) {
508f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
5091db24ffbSJonathan Lemon 		return (EINVAL);
5101db24ffbSJonathan Lemon 	}
511f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
512f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
5131db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
5141db24ffbSJonathan Lemon 	TCPDEBUG1();
51526ef6ac4SDon Lewis 	/*
51626ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
51726ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
51826ef6ac4SDon Lewis 	 * release the lock.
51926ef6ac4SDon Lewis 	 */
52026ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
52126ef6ac4SDon Lewis 		v4 = 1;
52226ef6ac4SDon Lewis 		port = inp->inp_fport;
52326ef6ac4SDon Lewis 		addr = inp->inp_faddr;
52426ef6ac4SDon Lewis 	} else {
52526ef6ac4SDon Lewis 		port = inp->inp_fport;
52626ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
52726ef6ac4SDon Lewis 	}
52826ef6ac4SDon Lewis 
52926ef6ac4SDon Lewis out:	TCPDEBUG2(PRU_ACCEPT);
53026ef6ac4SDon Lewis 	if (tp)
53126ef6ac4SDon Lewis 		INP_UNLOCK(inp);
53226ef6ac4SDon Lewis 	if (error == 0) {
53326ef6ac4SDon Lewis 		if (v4)
53426ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
53526ef6ac4SDon Lewis 		else
53626ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
53726ef6ac4SDon Lewis 	}
53826ef6ac4SDon Lewis 	return error;
539fb59c426SYoshinobu Inoue }
540fb59c426SYoshinobu Inoue #endif /* INET6 */
541f76fcf6dSJeffrey Hsu 
542f76fcf6dSJeffrey Hsu /*
543f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setsockaddr. We just pass down
544f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
545f76fcf6dSJeffrey Hsu  * here because in_setsockaddr will call malloc and can block.
546f76fcf6dSJeffrey Hsu  */
547f76fcf6dSJeffrey Hsu static int
548f76fcf6dSJeffrey Hsu tcp_sockaddr(struct socket *so, struct sockaddr **nam)
549f76fcf6dSJeffrey Hsu {
550f76fcf6dSJeffrey Hsu 	return (in_setsockaddr(so, nam, &tcbinfo));
551f76fcf6dSJeffrey Hsu }
552f76fcf6dSJeffrey Hsu 
553f76fcf6dSJeffrey Hsu /*
554f76fcf6dSJeffrey Hsu  * This is the wrapper function for in_setpeeraddr. We just pass down
555f76fcf6dSJeffrey Hsu  * the pcbinfo for in_setpeeraddr to lock.
556f76fcf6dSJeffrey Hsu  */
557f76fcf6dSJeffrey Hsu static int
558f76fcf6dSJeffrey Hsu tcp_peeraddr(struct socket *so, struct sockaddr **nam)
559f76fcf6dSJeffrey Hsu {
560f76fcf6dSJeffrey Hsu 	return (in_setpeeraddr(so, nam, &tcbinfo));
561f76fcf6dSJeffrey Hsu }
562f76fcf6dSJeffrey Hsu 
5632c37256eSGarrett Wollman /*
5642c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
5652c37256eSGarrett Wollman  */
5662c37256eSGarrett Wollman static int
5672c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
5682c37256eSGarrett Wollman {
5692c37256eSGarrett Wollman 	int error = 0;
570f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
5712c37256eSGarrett Wollman 	struct tcpcb *tp;
572f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
5732c37256eSGarrett Wollman 
5742c37256eSGarrett Wollman 	COMMON_START();
5752c37256eSGarrett Wollman 	socantsendmore(so);
5762c37256eSGarrett Wollman 	tp = tcp_usrclosed(tp);
5772c37256eSGarrett Wollman 	if (tp)
5782c37256eSGarrett Wollman 		error = tcp_output(tp);
5792c37256eSGarrett Wollman 	COMMON_END(PRU_SHUTDOWN);
5802c37256eSGarrett Wollman }
5812c37256eSGarrett Wollman 
5822c37256eSGarrett Wollman /*
5832c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
5842c37256eSGarrett Wollman  */
5852c37256eSGarrett Wollman static int
5862c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
5872c37256eSGarrett Wollman {
5882c37256eSGarrett Wollman 	int error = 0;
589f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
5902c37256eSGarrett Wollman 	struct tcpcb *tp;
591f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
5922c37256eSGarrett Wollman 
5932c37256eSGarrett Wollman 	COMMON_START();
5942c37256eSGarrett Wollman 	tcp_output(tp);
5952c37256eSGarrett Wollman 	COMMON_END(PRU_RCVD);
5962c37256eSGarrett Wollman }
5972c37256eSGarrett Wollman 
5982c37256eSGarrett Wollman /*
5992c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
6009c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
6019c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
6029c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
6039c9906e9SPeter Wemm  * generally are caller-frees.
6042c37256eSGarrett Wollman  */
6052c37256eSGarrett Wollman static int
60657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
607b40ce416SJulian Elischer 	     struct sockaddr *nam, struct mbuf *control, struct thread *td)
6082c37256eSGarrett Wollman {
6092c37256eSGarrett Wollman 	int error = 0;
610f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
6112c37256eSGarrett Wollman 	struct tcpcb *tp;
612f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
613fb59c426SYoshinobu Inoue #ifdef INET6
614fb59c426SYoshinobu Inoue 	int isipv6;
615fb59c426SYoshinobu Inoue #endif
6169c9906e9SPeter Wemm 	TCPDEBUG0;
6172c37256eSGarrett Wollman 
618f76fcf6dSJeffrey Hsu 	/*
619f76fcf6dSJeffrey Hsu 	 * Need write lock here because this function might call
620f76fcf6dSJeffrey Hsu 	 * tcp_connect or tcp_usrclosed.
621f76fcf6dSJeffrey Hsu 	 * We really want to have to this function upgrade from read lock
622f76fcf6dSJeffrey Hsu 	 * to write lock.  XXX
623f76fcf6dSJeffrey Hsu 	 */
624f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
625f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
6269c9906e9SPeter Wemm 	if (inp == NULL) {
6279c9906e9SPeter Wemm 		/*
6289c9906e9SPeter Wemm 		 * OOPS! we lost a race, the TCP session got reset after
629c0b99ffaSRobert Watson 		 * we checked SBS_CANTSENDMORE, eg: while doing uiomove or a
6309c9906e9SPeter Wemm 		 * network interrupt in the non-splnet() section of sosend().
6319c9906e9SPeter Wemm 		 */
6329c9906e9SPeter Wemm 		if (m)
6339c9906e9SPeter Wemm 			m_freem(m);
6349c9906e9SPeter Wemm 		if (control)
6359c9906e9SPeter Wemm 			m_freem(control);
6369c9906e9SPeter Wemm 		error = ECONNRESET;	/* XXX EPIPE? */
63745d3a132SPeter Wemm 		tp = NULL;
63845d3a132SPeter Wemm 		TCPDEBUG1();
6399c9906e9SPeter Wemm 		goto out;
6409c9906e9SPeter Wemm 	}
641f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
642fb59c426SYoshinobu Inoue #ifdef INET6
643fb59c426SYoshinobu Inoue 	isipv6 = nam && nam->sa_family == AF_INET6;
644fb59c426SYoshinobu Inoue #endif /* INET6 */
6459c9906e9SPeter Wemm 	tp = intotcpcb(inp);
6469c9906e9SPeter Wemm 	TCPDEBUG1();
6479c9906e9SPeter Wemm 	if (control) {
6489c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
6499c9906e9SPeter Wemm 		if (control->m_len) {
6509c9906e9SPeter Wemm 			m_freem(control);
6512c37256eSGarrett Wollman 			if (m)
6522c37256eSGarrett Wollman 				m_freem(m);
653744f87eaSDavid Greenman 			error = EINVAL;
654744f87eaSDavid Greenman 			goto out;
6552c37256eSGarrett Wollman 		}
6569c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
6579c9906e9SPeter Wemm 	}
6582c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
659395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
6602c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
6612c37256eSGarrett Wollman 			/*
6622c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
6632c37256eSGarrett Wollman 			 * initialize window to default value, and
6642c37256eSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
6652c37256eSGarrett Wollman 			 * MSS.
6662c37256eSGarrett Wollman 			 */
667fb59c426SYoshinobu Inoue #ifdef INET6
668fb59c426SYoshinobu Inoue 			if (isipv6)
669b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
670fb59c426SYoshinobu Inoue 			else
671fb59c426SYoshinobu Inoue #endif /* INET6 */
672b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
6732c37256eSGarrett Wollman 			if (error)
6742c37256eSGarrett Wollman 				goto out;
6752c37256eSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
6762c37256eSGarrett Wollman 			tcp_mss(tp, -1);
6772c37256eSGarrett Wollman 		}
6782c37256eSGarrett Wollman 
6792c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
6802c37256eSGarrett Wollman 			/*
6812c37256eSGarrett Wollman 			 * Close the send side of the connection after
6822c37256eSGarrett Wollman 			 * the data is sent.
6832c37256eSGarrett Wollman 			 */
6842c37256eSGarrett Wollman 			socantsendmore(so);
6852c37256eSGarrett Wollman 			tp = tcp_usrclosed(tp);
6862c37256eSGarrett Wollman 		}
687b0acefa8SBill Fenner 		if (tp != NULL) {
688b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
689b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
6902c37256eSGarrett Wollman 			error = tcp_output(tp);
691b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
692b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
693b0acefa8SBill Fenner 		}
6942c37256eSGarrett Wollman 	} else {
6952c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
6962c37256eSGarrett Wollman 			m_freem(m);
6972c37256eSGarrett Wollman 			error = ENOBUFS;
6982c37256eSGarrett Wollman 			goto out;
6992c37256eSGarrett Wollman 		}
7002c37256eSGarrett Wollman 		/*
7012c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
7022c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
7032c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
7042c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
7052c37256eSGarrett Wollman 		 * of data past the urgent section.
7062c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
7072c37256eSGarrett Wollman 		 */
708395bb186SSam Leffler 		sbappendstream(&so->so_snd, m);
709ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
710ef53690bSGarrett Wollman 			/*
711ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
712ef53690bSGarrett Wollman 			 * initialize window to default value, and
713ef53690bSGarrett Wollman 			 * initialize maxseg/maxopd using peer's cached
714ef53690bSGarrett Wollman 			 * MSS.
715ef53690bSGarrett Wollman 			 */
716fb59c426SYoshinobu Inoue #ifdef INET6
717fb59c426SYoshinobu Inoue 			if (isipv6)
718b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
719fb59c426SYoshinobu Inoue 			else
720fb59c426SYoshinobu Inoue #endif /* INET6 */
721b40ce416SJulian Elischer 			error = tcp_connect(tp, nam, td);
722ef53690bSGarrett Wollman 			if (error)
723ef53690bSGarrett Wollman 				goto out;
724ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
725ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
726ef53690bSGarrett Wollman 		}
7272c37256eSGarrett Wollman 		tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
7282c37256eSGarrett Wollman 		tp->t_force = 1;
7292c37256eSGarrett Wollman 		error = tcp_output(tp);
7302c37256eSGarrett Wollman 		tp->t_force = 0;
7312c37256eSGarrett Wollman 	}
7322c37256eSGarrett Wollman 	COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
7332c37256eSGarrett Wollman 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
7342c37256eSGarrett Wollman }
7352c37256eSGarrett Wollman 
7362c37256eSGarrett Wollman /*
7372c37256eSGarrett Wollman  * Abort the TCP.
7382c37256eSGarrett Wollman  */
7392c37256eSGarrett Wollman static int
7402c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
7412c37256eSGarrett Wollman {
7422c37256eSGarrett Wollman 	int error = 0;
743f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7442c37256eSGarrett Wollman 	struct tcpcb *tp;
745f76fcf6dSJeffrey Hsu 	const int inirw = INI_WRITE;
7462c37256eSGarrett Wollman 
7472c37256eSGarrett Wollman 	COMMON_START();
7482c37256eSGarrett Wollman 	tp = tcp_drop(tp, ECONNABORTED);
7492c37256eSGarrett Wollman 	COMMON_END(PRU_ABORT);
7502c37256eSGarrett Wollman }
7512c37256eSGarrett Wollman 
7522c37256eSGarrett Wollman /*
7532c37256eSGarrett Wollman  * Receive out-of-band data.
7542c37256eSGarrett Wollman  */
7552c37256eSGarrett Wollman static int
7562c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
7572c37256eSGarrett Wollman {
7582c37256eSGarrett Wollman 	int error = 0;
759f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
7602c37256eSGarrett Wollman 	struct tcpcb *tp;
761f76fcf6dSJeffrey Hsu 	const int inirw = INI_READ;
7622c37256eSGarrett Wollman 
7632c37256eSGarrett Wollman 	COMMON_START();
7642c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
765c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
7664cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
7674cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
7682c37256eSGarrett Wollman 		error = EINVAL;
7692c37256eSGarrett Wollman 		goto out;
7702c37256eSGarrett Wollman 	}
7712c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
7722c37256eSGarrett Wollman 		error = EWOULDBLOCK;
7732c37256eSGarrett Wollman 		goto out;
7742c37256eSGarrett Wollman 	}
7752c37256eSGarrett Wollman 	m->m_len = 1;
7762c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
7772c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
7782c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
7792c37256eSGarrett Wollman 	COMMON_END(PRU_RCVOOB);
7802c37256eSGarrett Wollman }
7812c37256eSGarrett Wollman 
7822c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
783756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
784756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
785756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
786756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
787756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
788756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
789756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
790756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
791756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
792756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		tcp_peeraddr,
793756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
794756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
795756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
796756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
797756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		tcp_sockaddr,
798756d52a1SPoul-Henning Kamp 	.pru_sosetlabel =	in_pcbsosetlabel
7992c37256eSGarrett Wollman };
800df8bae1dSRodney W. Grimes 
801fb59c426SYoshinobu Inoue #ifdef INET6
802fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
803756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
804756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
805756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
806756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
807756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
808756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
809756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
810756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
811756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
812756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
813756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
814756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
815756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
816756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
817756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
818756d52a1SPoul-Henning Kamp  	.pru_sosetlabel =	in_pcbsosetlabel
819fb59c426SYoshinobu Inoue };
820fb59c426SYoshinobu Inoue #endif /* INET6 */
821fb59c426SYoshinobu Inoue 
822a0292f23SGarrett Wollman /*
823a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
824a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
8255200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
8265200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
8275200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
8285200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
829a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
830a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
831a0292f23SGarrett Wollman  */
8320312fbe9SPoul-Henning Kamp static int
833b40ce416SJulian Elischer tcp_connect(tp, nam, td)
834a0292f23SGarrett Wollman 	register struct tcpcb *tp;
83557bf258eSGarrett Wollman 	struct sockaddr *nam;
836b40ce416SJulian Elischer 	struct thread *td;
837a0292f23SGarrett Wollman {
838a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
839a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
8405200e00eSIan Dowse 	struct in_addr laddr;
8415200e00eSIan Dowse 	u_short lport;
842c3229e05SDavid Greenman 	int error;
843a0292f23SGarrett Wollman 
844a0292f23SGarrett Wollman 	if (inp->inp_lport == 0) {
845b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
846a0292f23SGarrett Wollman 		if (error)
847a0292f23SGarrett Wollman 			return error;
848a0292f23SGarrett Wollman 	}
849a0292f23SGarrett Wollman 
850a0292f23SGarrett Wollman 	/*
851a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
852a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
853a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
854a0292f23SGarrett Wollman 	 */
8555200e00eSIan Dowse 	laddr = inp->inp_laddr;
8565200e00eSIan Dowse 	lport = inp->inp_lport;
8575200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
858b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
8595200e00eSIan Dowse 	if (error && oinp == NULL)
860d3628763SRodney W. Grimes 		return error;
861c94c54e4SAndre Oppermann 	if (oinp)
862a0292f23SGarrett Wollman 		return EADDRINUSE;
8635200e00eSIan Dowse 	inp->inp_laddr = laddr;
86415bd2b43SDavid Greenman 	in_pcbrehash(inp);
865a0292f23SGarrett Wollman 
866a0292f23SGarrett Wollman 	/* Compute window scaling to request.  */
867a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
868a0292f23SGarrett Wollman 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
869a0292f23SGarrett Wollman 		tp->request_r_scale++;
870a0292f23SGarrett Wollman 
871a0292f23SGarrett Wollman 	soisconnecting(so);
872a0292f23SGarrett Wollman 	tcpstat.tcps_connattempt++;
873a0292f23SGarrett Wollman 	tp->t_state = TCPS_SYN_SENT;
8749b8b58e0SJonathan Lemon 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
875b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
8761fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
877a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
878a45d2726SAndras Olah 
879a0292f23SGarrett Wollman 	return 0;
880a0292f23SGarrett Wollman }
881a0292f23SGarrett Wollman 
882fb59c426SYoshinobu Inoue #ifdef INET6
883fb59c426SYoshinobu Inoue static int
884b40ce416SJulian Elischer tcp6_connect(tp, nam, td)
885fb59c426SYoshinobu Inoue 	register struct tcpcb *tp;
886fb59c426SYoshinobu Inoue 	struct sockaddr *nam;
887b40ce416SJulian Elischer 	struct thread *td;
888fb59c426SYoshinobu Inoue {
889fb59c426SYoshinobu Inoue 	struct inpcb *inp = tp->t_inpcb, *oinp;
890fb59c426SYoshinobu Inoue 	struct socket *so = inp->inp_socket;
891fb59c426SYoshinobu Inoue 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
892fb59c426SYoshinobu Inoue 	struct in6_addr *addr6;
893fb59c426SYoshinobu Inoue 	int error;
894fb59c426SYoshinobu Inoue 
895fb59c426SYoshinobu Inoue 	if (inp->inp_lport == 0) {
896b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
897fb59c426SYoshinobu Inoue 		if (error)
898fb59c426SYoshinobu Inoue 			return error;
899fb59c426SYoshinobu Inoue 	}
900fb59c426SYoshinobu Inoue 
901fb59c426SYoshinobu Inoue 	/*
902fb59c426SYoshinobu Inoue 	 * Cannot simply call in_pcbconnect, because there might be an
903fb59c426SYoshinobu Inoue 	 * earlier incarnation of this same connection still in
904fb59c426SYoshinobu Inoue 	 * TIME_WAIT state, creating an ADDRINUSE error.
905fb59c426SYoshinobu Inoue 	 */
906fb59c426SYoshinobu Inoue 	error = in6_pcbladdr(inp, nam, &addr6);
907fb59c426SYoshinobu Inoue 	if (error)
908fb59c426SYoshinobu Inoue 		return error;
909fb59c426SYoshinobu Inoue 	oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
910fb59c426SYoshinobu Inoue 				  &sin6->sin6_addr, sin6->sin6_port,
911fb59c426SYoshinobu Inoue 				  IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
912fb59c426SYoshinobu Inoue 				  ? addr6
913fb59c426SYoshinobu Inoue 				  : &inp->in6p_laddr,
914fb59c426SYoshinobu Inoue 				  inp->inp_lport,  0, NULL);
915c94c54e4SAndre Oppermann 	if (oinp)
916fb59c426SYoshinobu Inoue 		return EADDRINUSE;
917fb59c426SYoshinobu Inoue 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
918fb59c426SYoshinobu Inoue 		inp->in6p_laddr = *addr6;
919fb59c426SYoshinobu Inoue 	inp->in6p_faddr = sin6->sin6_addr;
920fb59c426SYoshinobu Inoue 	inp->inp_fport = sin6->sin6_port;
9218a59da30SHajimu UMEMOTO 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
9228a59da30SHajimu UMEMOTO 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
9238a59da30SHajimu UMEMOTO 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
9248a59da30SHajimu UMEMOTO 		inp->in6p_flowinfo |=
9258a59da30SHajimu UMEMOTO 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
926fb59c426SYoshinobu Inoue 	in_pcbrehash(inp);
927fb59c426SYoshinobu Inoue 
928fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
929fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
930fb59c426SYoshinobu Inoue 	    (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
931fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
932fb59c426SYoshinobu Inoue 
933fb59c426SYoshinobu Inoue 	soisconnecting(so);
934fb59c426SYoshinobu Inoue 	tcpstat.tcps_connattempt++;
935fb59c426SYoshinobu Inoue 	tp->t_state = TCPS_SYN_SENT;
936fb59c426SYoshinobu Inoue 	callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
937b0e3ad75SMike Silbersack 	tp->iss = tcp_new_isn(tp);
9381fcc99b5SMatthew Dillon 	tp->t_bw_rtseq = tp->iss;
939fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
940fb59c426SYoshinobu Inoue 
941fb59c426SYoshinobu Inoue 	return 0;
942fb59c426SYoshinobu Inoue }
943fb59c426SYoshinobu Inoue #endif /* INET6 */
944fb59c426SYoshinobu Inoue 
945cfe8b629SGarrett Wollman /*
946b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
947b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
948b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
949b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
950b8af5dfaSRobert Watson  * from Linux.
951b8af5dfaSRobert Watson  */
952b8af5dfaSRobert Watson static void
953b8af5dfaSRobert Watson tcp_fill_info(tp, ti)
954b8af5dfaSRobert Watson 	struct tcpcb *tp;
955b8af5dfaSRobert Watson 	struct tcp_info *ti;
956b8af5dfaSRobert Watson {
957b8af5dfaSRobert Watson 
958b8af5dfaSRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
959b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
960b8af5dfaSRobert Watson 
961b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
962b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
963b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
964b8af5dfaSRobert Watson 	if (tp->sack_enable)
965b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
966b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
967b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
968b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
969b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
970b8af5dfaSRobert Watson 	}
971b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
972b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
973b8af5dfaSRobert Watson 
974b8af5dfaSRobert Watson 	/*
975b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
976b8af5dfaSRobert Watson 	 */
977b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
978b8af5dfaSRobert Watson 	ti->tcpi_snd_bwnd = tp->snd_bwnd;
979b8af5dfaSRobert Watson }
980b8af5dfaSRobert Watson 
981b8af5dfaSRobert Watson /*
982cfe8b629SGarrett Wollman  * The new sockopt interface makes it possible for us to block in the
983cfe8b629SGarrett Wollman  * copyin/out step (if we take a page fault).  Taking a page fault at
984cfe8b629SGarrett Wollman  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
985cfe8b629SGarrett Wollman  * use TSM, there probably isn't any need for this function to run at
986cfe8b629SGarrett Wollman  * splnet() any more.  This needs more examination.)
987b8af5dfaSRobert Watson  *
988b8af5dfaSRobert Watson  * XXXRW: The locking here is wrong; we may take a page fault while holding
989b8af5dfaSRobert Watson  * the inpcb lock.
990cfe8b629SGarrett Wollman  */
991df8bae1dSRodney W. Grimes int
992cfe8b629SGarrett Wollman tcp_ctloutput(so, sopt)
993df8bae1dSRodney W. Grimes 	struct socket *so;
994cfe8b629SGarrett Wollman 	struct sockopt *sopt;
995df8bae1dSRodney W. Grimes {
9963f9d1ef9SRobert Watson 	int	error, opt, optval;
997df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
998cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
999b8af5dfaSRobert Watson 	struct	tcp_info ti;
1000df8bae1dSRodney W. Grimes 
1001cfe8b629SGarrett Wollman 	error = 0;
1002f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&tcbinfo);
1003df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1004df8bae1dSRodney W. Grimes 	if (inp == NULL) {
1005f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&tcbinfo);
1006df8bae1dSRodney W. Grimes 		return (ECONNRESET);
1007df8bae1dSRodney W. Grimes 	}
1008f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
1009f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&tcbinfo);
1010cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
10114e397bc5SRobert Watson 		INP_UNLOCK(inp);
1012fb59c426SYoshinobu Inoue #ifdef INET6
1013fb59c426SYoshinobu Inoue 		if (INP_CHECK_SOCKAF(so, AF_INET6))
1014fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
1015fb59c426SYoshinobu Inoue 		else
1016fb59c426SYoshinobu Inoue #endif /* INET6 */
1017cfe8b629SGarrett Wollman 		error = ip_ctloutput(so, sopt);
1018df8bae1dSRodney W. Grimes 		return (error);
1019df8bae1dSRodney W. Grimes 	}
1020df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1021df8bae1dSRodney W. Grimes 
1022cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1023cfe8b629SGarrett Wollman 	case SOPT_SET:
1024cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
10251cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
102688f6b043SBruce M Simpson 		case TCP_MD5SIG:
10271cfd4b53SBruce M Simpson 			error = sooptcopyin(sopt, &optval, sizeof optval,
10281cfd4b53SBruce M Simpson 					    sizeof optval);
10291cfd4b53SBruce M Simpson 			if (error)
10301cfd4b53SBruce M Simpson 				break;
10311cfd4b53SBruce M Simpson 
10321cfd4b53SBruce M Simpson 			if (optval > 0)
10331cfd4b53SBruce M Simpson 				tp->t_flags |= TF_SIGNATURE;
10341cfd4b53SBruce M Simpson 			else
10351cfd4b53SBruce M Simpson 				tp->t_flags &= ~TF_SIGNATURE;
10361cfd4b53SBruce M Simpson 			break;
10371cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */
1038df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1039cfe8b629SGarrett Wollman 		case TCP_NOOPT:
1040cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1041cfe8b629SGarrett Wollman 					    sizeof optval);
1042cfe8b629SGarrett Wollman 			if (error)
1043cfe8b629SGarrett Wollman 				break;
1044cfe8b629SGarrett Wollman 
1045cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1046cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1047cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1048cfe8b629SGarrett Wollman 				break;
1049cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1050cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1051cfe8b629SGarrett Wollman 				break;
1052cfe8b629SGarrett Wollman 			default:
1053cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1054cfe8b629SGarrett Wollman 				break;
1055cfe8b629SGarrett Wollman 			}
1056cfe8b629SGarrett Wollman 
1057cfe8b629SGarrett Wollman 			if (optval)
1058cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1059df8bae1dSRodney W. Grimes 			else
1060cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
1061df8bae1dSRodney W. Grimes 			break;
1062df8bae1dSRodney W. Grimes 
1063007581c0SJonathan Lemon 		case TCP_NOPUSH:
1064007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
1065007581c0SJonathan Lemon 					    sizeof optval);
1066007581c0SJonathan Lemon 			if (error)
1067007581c0SJonathan Lemon 				break;
1068007581c0SJonathan Lemon 
1069007581c0SJonathan Lemon 			if (optval)
1070007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
1071007581c0SJonathan Lemon 			else {
1072007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
1073007581c0SJonathan Lemon 				error = tcp_output(tp);
1074007581c0SJonathan Lemon 			}
1075007581c0SJonathan Lemon 			break;
1076007581c0SJonathan Lemon 
1077df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1078cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1079cfe8b629SGarrett Wollman 					    sizeof optval);
1080cfe8b629SGarrett Wollman 			if (error)
1081df8bae1dSRodney W. Grimes 				break;
1082df8bae1dSRodney W. Grimes 
108353369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
108453369ac9SAndre Oppermann 			    optval + 40 >= tcp_minmss)
1085cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
1086a0292f23SGarrett Wollman 			else
1087a0292f23SGarrett Wollman 				error = EINVAL;
1088a0292f23SGarrett Wollman 			break;
1089a0292f23SGarrett Wollman 
1090b8af5dfaSRobert Watson 		case TCP_INFO:
1091b8af5dfaSRobert Watson 			error = EINVAL;
1092b8af5dfaSRobert Watson 			break;
1093b8af5dfaSRobert Watson 
1094df8bae1dSRodney W. Grimes 		default:
1095df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1096df8bae1dSRodney W. Grimes 			break;
1097df8bae1dSRodney W. Grimes 		}
1098df8bae1dSRodney W. Grimes 		break;
1099df8bae1dSRodney W. Grimes 
1100cfe8b629SGarrett Wollman 	case SOPT_GET:
1101cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
11021cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
110388f6b043SBruce M Simpson 		case TCP_MD5SIG:
11041cfd4b53SBruce M Simpson 			optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1105b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
11061cfd4b53SBruce M Simpson 			break;
1107265ed012SBruce M Simpson #endif
1108df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1109cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
1110b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1111df8bae1dSRodney W. Grimes 			break;
1112df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
1113cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
1114b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1115df8bae1dSRodney W. Grimes 			break;
1116a0292f23SGarrett Wollman 		case TCP_NOOPT:
1117cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
1118b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1119a0292f23SGarrett Wollman 			break;
1120a0292f23SGarrett Wollman 		case TCP_NOPUSH:
1121cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
1122b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
1123b8af5dfaSRobert Watson 			break;
1124b8af5dfaSRobert Watson 		case TCP_INFO:
1125b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
1126b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
1127a0292f23SGarrett Wollman 			break;
1128df8bae1dSRodney W. Grimes 		default:
1129df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1130df8bae1dSRodney W. Grimes 			break;
1131df8bae1dSRodney W. Grimes 		}
1132df8bae1dSRodney W. Grimes 		break;
1133df8bae1dSRodney W. Grimes 	}
1134f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
1135df8bae1dSRodney W. Grimes 	return (error);
1136df8bae1dSRodney W. Grimes }
1137df8bae1dSRodney W. Grimes 
113826e30fbbSDavid Greenman /*
113926e30fbbSDavid Greenman  * tcp_sendspace and tcp_recvspace are the default send and receive window
114026e30fbbSDavid Greenman  * sizes, respectively.  These are obsolescent (this information should
114126e30fbbSDavid Greenman  * be set by the route).
114226e30fbbSDavid Greenman  */
114381e561cdSDavid E. O'Brien u_long	tcp_sendspace = 1024*32;
11443d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
11453d177f46SBill Fumerola     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
114681e561cdSDavid E. O'Brien u_long	tcp_recvspace = 1024*64;
11473d177f46SBill Fumerola SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
11483d177f46SBill Fumerola     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes /*
1151df8bae1dSRodney W. Grimes  * Attach TCP protocol to socket, allocating
1152df8bae1dSRodney W. Grimes  * internet protocol control block, tcp control block,
1153df8bae1dSRodney W. Grimes  * bufer space, and entering LISTEN state if to accept connections.
1154df8bae1dSRodney W. Grimes  */
11550312fbe9SPoul-Henning Kamp static int
115656dc72c3SPawel Jakub Dawidek tcp_attach(so)
1157df8bae1dSRodney W. Grimes 	struct socket *so;
1158df8bae1dSRodney W. Grimes {
1159df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1160df8bae1dSRodney W. Grimes 	struct inpcb *inp;
1161df8bae1dSRodney W. Grimes 	int error;
1162fb59c426SYoshinobu Inoue #ifdef INET6
11634a6a94d8SArchie Cobbs 	int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1164fb59c426SYoshinobu Inoue #endif
1165df8bae1dSRodney W. Grimes 
1166df8bae1dSRodney W. Grimes 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1167df8bae1dSRodney W. Grimes 		error = soreserve(so, tcp_sendspace, tcp_recvspace);
1168df8bae1dSRodney W. Grimes 		if (error)
1169df8bae1dSRodney W. Grimes 			return (error);
1170df8bae1dSRodney W. Grimes 	}
11716823b823SPawel Jakub Dawidek 	error = in_pcballoc(so, &tcbinfo, "tcpinp");
1172df8bae1dSRodney W. Grimes 	if (error)
1173df8bae1dSRodney W. Grimes 		return (error);
1174df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1175fb59c426SYoshinobu Inoue #ifdef INET6
1176fb59c426SYoshinobu Inoue 	if (isipv6) {
1177fb59c426SYoshinobu Inoue 		inp->inp_vflag |= INP_IPV6;
1178fb59c426SYoshinobu Inoue 		inp->in6p_hops = -1;	/* use kernel default */
1179fb59c426SYoshinobu Inoue 	}
1180fb59c426SYoshinobu Inoue 	else
1181fb59c426SYoshinobu Inoue #endif
1182cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1183df8bae1dSRodney W. Grimes 	tp = tcp_newtcpcb(inp);
1184df8bae1dSRodney W. Grimes 	if (tp == 0) {
11854cc20ab1SSeigo Tanimura 		int nofd = so->so_state & SS_NOFDREF;	/* XXX */
1186df8bae1dSRodney W. Grimes 
1187df8bae1dSRodney W. Grimes 		so->so_state &= ~SS_NOFDREF;	/* don't free the socket yet */
1188fb59c426SYoshinobu Inoue #ifdef INET6
1189fb59c426SYoshinobu Inoue 		if (isipv6)
1190fb59c426SYoshinobu Inoue 			in6_pcbdetach(inp);
1191fb59c426SYoshinobu Inoue 		else
1192fb59c426SYoshinobu Inoue #endif
1193df8bae1dSRodney W. Grimes 		in_pcbdetach(inp);
1194df8bae1dSRodney W. Grimes 		so->so_state |= nofd;
1195df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1196df8bae1dSRodney W. Grimes 	}
1197df8bae1dSRodney W. Grimes 	tp->t_state = TCPS_CLOSED;
1198df8bae1dSRodney W. Grimes 	return (0);
1199df8bae1dSRodney W. Grimes }
1200df8bae1dSRodney W. Grimes 
1201df8bae1dSRodney W. Grimes /*
1202df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
1203df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
1204df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
1205df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
1206df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
1207df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
1208df8bae1dSRodney W. Grimes  */
12090312fbe9SPoul-Henning Kamp static struct tcpcb *
1210df8bae1dSRodney W. Grimes tcp_disconnect(tp)
1211df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1212df8bae1dSRodney W. Grimes {
1213df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
1214df8bae1dSRodney W. Grimes 
1215df8bae1dSRodney W. Grimes 	if (tp->t_state < TCPS_ESTABLISHED)
1216df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
12174cc20ab1SSeigo Tanimura 	else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1218243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
12194cc20ab1SSeigo Tanimura 	else {
1220df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
1221df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
1222df8bae1dSRodney W. Grimes 		tp = tcp_usrclosed(tp);
1223df8bae1dSRodney W. Grimes 		if (tp)
1224df8bae1dSRodney W. Grimes 			(void) tcp_output(tp);
1225df8bae1dSRodney W. Grimes 	}
1226df8bae1dSRodney W. Grimes 	return (tp);
1227df8bae1dSRodney W. Grimes }
1228df8bae1dSRodney W. Grimes 
1229df8bae1dSRodney W. Grimes /*
1230df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
1231df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
1232df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1233df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
1234df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
1235df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1236df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
1237df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
1238df8bae1dSRodney W. Grimes  */
12390312fbe9SPoul-Henning Kamp static struct tcpcb *
1240df8bae1dSRodney W. Grimes tcp_usrclosed(tp)
1241df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
1242df8bae1dSRodney W. Grimes {
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1245df8bae1dSRodney W. Grimes 
1246df8bae1dSRodney W. Grimes 	case TCPS_CLOSED:
1247df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
1248df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_CLOSED;
1249df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1250df8bae1dSRodney W. Grimes 		break;
1251df8bae1dSRodney W. Grimes 
1252a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
1253df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1254a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
1255a0292f23SGarrett Wollman 		break;
1256a0292f23SGarrett Wollman 
1257df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1258df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_FIN_WAIT_1;
1259df8bae1dSRodney W. Grimes 		break;
1260df8bae1dSRodney W. Grimes 
1261df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1262df8bae1dSRodney W. Grimes 		tp->t_state = TCPS_LAST_ACK;
1263df8bae1dSRodney W. Grimes 		break;
1264df8bae1dSRodney W. Grimes 	}
1265b6239c4aSAndras Olah 	if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1266df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
1267b6239c4aSAndras Olah 		/* To prevent the connection hanging in FIN_WAIT_2 forever. */
1268b6239c4aSAndras Olah 		if (tp->t_state == TCPS_FIN_WAIT_2)
12699b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, tcp_maxidle,
12709b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1271b6239c4aSAndras Olah 	}
1272df8bae1dSRodney W. Grimes 	return (tp);
1273df8bae1dSRodney W. Grimes }
1274a0292f23SGarrett Wollman 
1275