xref: /freebsd/sys/netinet/tcp_usrreq.c (revision bd4f986644b3804e19b92d39f1f90ffabff83e14)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
5623dce13SRobert Watson  *	The Regents of the University of California.
6497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
7fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8623dce13SRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
10fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
11fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
12fa046d87SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
371fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
404b421e2dSMike Silbersack #include <sys/cdefs.h>
414b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
424b421e2dSMike Silbersack 
43497057eeSRobert Watson #include "opt_ddb.h"
441cfd4b53SBruce M Simpson #include "opt_inet.h"
45fb59c426SYoshinobu Inoue #include "opt_inet6.h"
46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
47b2e60773SJohn Baldwin #include "opt_kern_tls.h"
480cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
490cc12cc5SJoerg Wunsch 
50df8bae1dSRodney W. Grimes #include <sys/param.h>
51df8bae1dSRodney W. Grimes #include <sys/systm.h>
52adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
539077f387SGleb Smirnoff #include <sys/limits.h>
54f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5555bceb1eSRandall Stewart #include <sys/refcount.h>
56c7a82f90SGarrett Wollman #include <sys/kernel.h>
57b2e60773SJohn Baldwin #include <sys/ktls.h>
58adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5998163b98SPoul-Henning Kamp #include <sys/sysctl.h>
60df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
61fb59c426SYoshinobu Inoue #ifdef INET6
62fb59c426SYoshinobu Inoue #include <sys/domain.h>
63fb59c426SYoshinobu Inoue #endif /* INET6 */
64df8bae1dSRodney W. Grimes #include <sys/socket.h>
65df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
66df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6791421ba2SRobert Watson #include <sys/proc.h>
6891421ba2SRobert Watson #include <sys/jail.h>
69adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
70df8bae1dSRodney W. Grimes 
71497057eeSRobert Watson #ifdef DDB
72497057eeSRobert Watson #include <ddb/ddb.h>
73497057eeSRobert Watson #endif
74497057eeSRobert Watson 
75df8bae1dSRodney W. Grimes #include <net/if.h>
7676039bc8SGleb Smirnoff #include <net/if_var.h>
77df8bae1dSRodney W. Grimes #include <net/route.h>
78530c0060SRobert Watson #include <net/vnet.h>
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes #include <netinet/in.h>
815d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
82df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
83b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
84b5e8ce9fSBruce Evans #include <netinet/in_var.h>
853b0ee680SRichard Scheffenegger #include <netinet/ip.h>
86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
87fb59c426SYoshinobu Inoue #ifdef INET6
88b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
89b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
90fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
91a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
92fb59c426SYoshinobu Inoue #endif
932de3e790SGleb Smirnoff #include <netinet/tcp.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
95df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
96df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
97df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
982529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
99df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1004644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
101c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
102fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
10386a996e6SHiren Panchasara #ifdef TCPPCAP
10486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10586a996e6SHiren Panchasara #endif
106df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
10709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
108bc65987aSKip Macy #include <netinet/tcp_offload.h>
10909fe6320SNavdeep Parhar #endif
110fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
111df8bae1dSRodney W. Grimes 
112adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
113adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
114adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
115adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
116adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
117adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
118adc56f5aSEdward Tomasz Napierala 
119df8bae1dSRodney W. Grimes /*
120df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
121df8bae1dSRodney W. Grimes  */
122b287c6c7SBjoern A. Zeeb #ifdef INET
1234d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1244d77a549SAlfred Perlstein 		    struct thread *td);
125b287c6c7SBjoern A. Zeeb #endif /* INET */
126fb59c426SYoshinobu Inoue #ifdef INET6
1274d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1284d77a549SAlfred Perlstein 		    struct thread *td);
129fb59c426SYoshinobu Inoue #endif /* INET6 */
130623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
131623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
132b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1332c37256eSGarrett Wollman 
134d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
135d3b6c96bSRandall Stewart 
1362c37256eSGarrett Wollman #ifdef TCPDEBUG
1371db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1382c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1394cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1404cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1412c37256eSGarrett Wollman #else
1422c37256eSGarrett Wollman #define	TCPDEBUG0
1432c37256eSGarrett Wollman #define	TCPDEBUG1()
1442c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1452c37256eSGarrett Wollman #endif
1462c37256eSGarrett Wollman 
1472c37256eSGarrett Wollman /*
14825102351SMike Karels  * tcp_require_unique port requires a globally-unique source port for each
14925102351SMike Karels  * outgoing connection.  The default is to require the 4-tuple to be unique.
15025102351SMike Karels  */
15125102351SMike Karels VNET_DEFINE(int, tcp_require_unique_port) = 0;
15225102351SMike Karels SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
15325102351SMike Karels     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
15425102351SMike Karels     "Require globally-unique ephemeral port for outgoing connections");
15525102351SMike Karels #define	V_tcp_require_unique_port	VNET(tcp_require_unique_port)
15625102351SMike Karels 
15725102351SMike Karels /*
1582c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1592c37256eSGarrett Wollman  * and an internet control block.
1602c37256eSGarrett Wollman  */
1612c37256eSGarrett Wollman static int
162b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1632c37256eSGarrett Wollman {
164f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
165623dce13SRobert Watson 	struct tcpcb *tp = NULL;
166623dce13SRobert Watson 	int error;
1672c37256eSGarrett Wollman 	TCPDEBUG0;
1682c37256eSGarrett Wollman 
169623dce13SRobert Watson 	inp = sotoinpcb(so);
170623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1712c37256eSGarrett Wollman 	TCPDEBUG1();
1722c37256eSGarrett Wollman 
1730f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1742c37256eSGarrett Wollman 	if (error)
1752c37256eSGarrett Wollman 		goto out;
1762c37256eSGarrett Wollman 
1770f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1780f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1790f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1807669c586SGleb Smirnoff 	if (error)
1810f6385e7SGleb Smirnoff 		goto out;
1820f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1830f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1840f6385e7SGleb Smirnoff 	if (tp == NULL) {
1857669c586SGleb Smirnoff 		error = ENOBUFS;
1860f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1870f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1880f6385e7SGleb Smirnoff 		goto out;
1890f6385e7SGleb Smirnoff 	}
1900f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
1910f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1920f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1932c37256eSGarrett Wollman out:
1942c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1955d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1960f6385e7SGleb Smirnoff 	return (error);
1972c37256eSGarrett Wollman }
1982c37256eSGarrett Wollman 
1992c37256eSGarrett Wollman /*
2003fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
201a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
202a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
203a152f8a3SRobert Watson  * inpcb state: time wait.
2042c37256eSGarrett Wollman  */
205bc725eafSRobert Watson static void
2063fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2072c37256eSGarrett Wollman {
2083fed74e9SGleb Smirnoff 	struct inpcb *inp;
2092c37256eSGarrett Wollman 	struct tcpcb *tp;
2102c37256eSGarrett Wollman 
2113fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2123fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2133fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2143fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2153fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
216953b5606SRobert Watson 
217a152f8a3SRobert Watson 	tp = intotcpcb(inp);
218a152f8a3SRobert Watson 
2191b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2201b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2211b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2229c3507f9SGleb Smirnoff 
223623dce13SRobert Watson 	tcp_discardcb(tp);
224623dce13SRobert Watson 	in_pcbdetach(inp);
2250206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
226623dce13SRobert Watson }
227c78cbc7bSRobert Watson 
228b287c6c7SBjoern A. Zeeb #ifdef INET
2292c37256eSGarrett Wollman /*
2302c37256eSGarrett Wollman  * Give the socket an address.
2312c37256eSGarrett Wollman  */
2322c37256eSGarrett Wollman static int
233b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2342c37256eSGarrett Wollman {
2352c37256eSGarrett Wollman 	int error = 0;
236f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
237b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
238623dce13SRobert Watson 	struct tcpcb *tp = NULL;
239b338b1fdSMateusz Guzik #endif
2402c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2412c37256eSGarrett Wollman 
24252710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
243f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
244f96603b5SMark Johnston 		/*
245f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
246f96603b5SMark Johnston 		 */
247f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2483f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
249f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
250f161d294SMark Johnston 			return (EAFNOSUPPORT);
251f96603b5SMark Johnston 		nam->sa_family = AF_INET;
252f96603b5SMark Johnston 	}
25352710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof(*sinp))
25452710de1SPawel Jakub Dawidek 		return (EINVAL);
255f161d294SMark Johnston 
2562c37256eSGarrett Wollman 	/*
2572c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2582c37256eSGarrett Wollman 	 * to them.
2592c37256eSGarrett Wollman 	 */
260f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
26152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
26252710de1SPawel Jakub Dawidek 
263623dce13SRobert Watson 	TCPDEBUG0;
264623dce13SRobert Watson 	inp = sotoinpcb(so);
265623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2668501a69cSRobert Watson 	INP_WLOCK(inp);
26753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
268623dce13SRobert Watson 		error = EINVAL;
2692c37256eSGarrett Wollman 		goto out;
270623dce13SRobert Watson 	}
271b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
272623dce13SRobert Watson 	tp = intotcpcb(inp);
273b338b1fdSMateusz Guzik #endif
274623dce13SRobert Watson 	TCPDEBUG1();
275fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
276623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
277fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
278623dce13SRobert Watson out:
279623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
2805d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2818501a69cSRobert Watson 	INP_WUNLOCK(inp);
282623dce13SRobert Watson 
283623dce13SRobert Watson 	return (error);
2842c37256eSGarrett Wollman }
285b287c6c7SBjoern A. Zeeb #endif /* INET */
2862c37256eSGarrett Wollman 
287fb59c426SYoshinobu Inoue #ifdef INET6
288fb59c426SYoshinobu Inoue static int
289b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
290fb59c426SYoshinobu Inoue {
291fb59c426SYoshinobu Inoue 	int error = 0;
292f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
293b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
294623dce13SRobert Watson 	struct tcpcb *tp = NULL;
295b338b1fdSMateusz Guzik #endif
2960ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2974a91aa8fSMichael Tuexen 	u_char vflagsav;
298fb59c426SYoshinobu Inoue 
2990ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
300f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
301f161d294SMark Johnston 		return (EAFNOSUPPORT);
3020ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof(*sin6))
30352710de1SPawel Jakub Dawidek 		return (EINVAL);
304f161d294SMark Johnston 
305fb59c426SYoshinobu Inoue 	/*
306fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
307fb59c426SYoshinobu Inoue 	 * to them.
308fb59c426SYoshinobu Inoue 	 */
309f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
31052710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
31152710de1SPawel Jakub Dawidek 
312623dce13SRobert Watson 	TCPDEBUG0;
313623dce13SRobert Watson 	inp = sotoinpcb(so);
314623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3158501a69cSRobert Watson 	INP_WLOCK(inp);
3164a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
31753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
318623dce13SRobert Watson 		error = EINVAL;
319623dce13SRobert Watson 		goto out;
320623dce13SRobert Watson 	}
321b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
322623dce13SRobert Watson 	tp = intotcpcb(inp);
323b338b1fdSMateusz Guzik #endif
324623dce13SRobert Watson 	TCPDEBUG1();
325fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
326fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
327fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
328b287c6c7SBjoern A. Zeeb #ifdef INET
32966ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3300ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
331fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3320ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
333fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
334fb59c426SYoshinobu Inoue 
3350ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
336888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
337888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
338888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
339888973f5SMichael Tuexen 				goto out;
340888973f5SMichael Tuexen 			}
341fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
342fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
343b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
344b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
345fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
346fb59c426SYoshinobu Inoue 			goto out;
347fb59c426SYoshinobu Inoue 		}
348fb59c426SYoshinobu Inoue 	}
349b287c6c7SBjoern A. Zeeb #endif
350b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
351fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
352623dce13SRobert Watson out:
3534a91aa8fSMichael Tuexen 	if (error != 0)
3544a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
355623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3565d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3578501a69cSRobert Watson 	INP_WUNLOCK(inp);
358623dce13SRobert Watson 	return (error);
359fb59c426SYoshinobu Inoue }
360fb59c426SYoshinobu Inoue #endif /* INET6 */
361fb59c426SYoshinobu Inoue 
362b287c6c7SBjoern A. Zeeb #ifdef INET
3632c37256eSGarrett Wollman /*
3642c37256eSGarrett Wollman  * Prepare to accept connections.
3652c37256eSGarrett Wollman  */
3662c37256eSGarrett Wollman static int
367d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3682c37256eSGarrett Wollman {
3692c37256eSGarrett Wollman 	int error = 0;
370f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
371623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3722c37256eSGarrett Wollman 
373623dce13SRobert Watson 	TCPDEBUG0;
374623dce13SRobert Watson 	inp = sotoinpcb(so);
375623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3768501a69cSRobert Watson 	INP_WLOCK(inp);
37753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
378623dce13SRobert Watson 		error = EINVAL;
379623dce13SRobert Watson 		goto out;
380623dce13SRobert Watson 	}
381623dce13SRobert Watson 	tp = intotcpcb(inp);
382623dce13SRobert Watson 	TCPDEBUG1();
3830daccb9cSRobert Watson 	SOCK_LOCK(so);
3840daccb9cSRobert Watson 	error = solisten_proto_check(so);
385bd4a39ccSMark Johnston 	if (error != 0) {
386bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
387bd4a39ccSMark Johnston 		goto out;
388bd4a39ccSMark Johnston 	}
389bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
390fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
391bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
392fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
393bd4a39ccSMark Johnston 	}
3940daccb9cSRobert Watson 	if (error == 0) {
39557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
396d374e81eSRobert Watson 		solisten_proto(so, backlog);
39709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
39837cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
39909fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
40009fe6320SNavdeep Parhar #endif
401bd4a39ccSMark Johnston 	} else {
402bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4030daccb9cSRobert Watson 	}
4040daccb9cSRobert Watson 	SOCK_UNLOCK(so);
405623dce13SRobert Watson 
40668bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
407281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
40818a75309SPatrick Kelsey 
409623dce13SRobert Watson out:
410623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4115d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4128501a69cSRobert Watson 	INP_WUNLOCK(inp);
413623dce13SRobert Watson 	return (error);
4142c37256eSGarrett Wollman }
415b287c6c7SBjoern A. Zeeb #endif /* INET */
4162c37256eSGarrett Wollman 
417fb59c426SYoshinobu Inoue #ifdef INET6
418fb59c426SYoshinobu Inoue static int
419d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
420fb59c426SYoshinobu Inoue {
421fb59c426SYoshinobu Inoue 	int error = 0;
422f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
423623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4244a91aa8fSMichael Tuexen 	u_char vflagsav;
425fb59c426SYoshinobu Inoue 
426623dce13SRobert Watson 	TCPDEBUG0;
427623dce13SRobert Watson 	inp = sotoinpcb(so);
428623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4298501a69cSRobert Watson 	INP_WLOCK(inp);
43053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
431623dce13SRobert Watson 		error = EINVAL;
432623dce13SRobert Watson 		goto out;
433623dce13SRobert Watson 	}
4344a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
435623dce13SRobert Watson 	tp = intotcpcb(inp);
436623dce13SRobert Watson 	TCPDEBUG1();
4370daccb9cSRobert Watson 	SOCK_LOCK(so);
4380daccb9cSRobert Watson 	error = solisten_proto_check(so);
439bd4a39ccSMark Johnston 	if (error != 0) {
440bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
441bd4a39ccSMark Johnston 		goto out;
442bd4a39ccSMark Johnston 	}
443fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
444bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
445fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
44666ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
447fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
448bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
449fb59c426SYoshinobu Inoue 	}
450fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4510daccb9cSRobert Watson 	if (error == 0) {
45257f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
453d374e81eSRobert Watson 		solisten_proto(so, backlog);
45409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
45537cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45609fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45709fe6320SNavdeep Parhar #endif
458bd4a39ccSMark Johnston 	} else {
459bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4600daccb9cSRobert Watson 	}
4610daccb9cSRobert Watson 	SOCK_UNLOCK(so);
462623dce13SRobert Watson 
46368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
464281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
46518a75309SPatrick Kelsey 
4664a91aa8fSMichael Tuexen 	if (error != 0)
4674a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4684a91aa8fSMichael Tuexen 
469623dce13SRobert Watson out:
470623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4715d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4728501a69cSRobert Watson 	INP_WUNLOCK(inp);
473623dce13SRobert Watson 	return (error);
474fb59c426SYoshinobu Inoue }
475fb59c426SYoshinobu Inoue #endif /* INET6 */
476fb59c426SYoshinobu Inoue 
477b287c6c7SBjoern A. Zeeb #ifdef INET
4782c37256eSGarrett Wollman /*
4792c37256eSGarrett Wollman  * Initiate connection to peer.
4802c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4812c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4822c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4832c37256eSGarrett Wollman  * Send initial segment on connection.
4842c37256eSGarrett Wollman  */
4852c37256eSGarrett Wollman static int
486b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4872c37256eSGarrett Wollman {
488109eb549SGleb Smirnoff 	struct epoch_tracker et;
4892c37256eSGarrett Wollman 	int error = 0;
490f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
491623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4922c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4932c37256eSGarrett Wollman 
49457bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
495f161d294SMark Johnston 	if (nam->sa_family != AF_INET)
496f161d294SMark Johnston 		return (EAFNOSUPPORT);
497e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
498e29ef13fSDon Lewis 		return (EINVAL);
499f161d294SMark Johnston 
50052710de1SPawel Jakub Dawidek 	/*
50152710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
50252710de1SPawel Jakub Dawidek 	 */
503f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
50452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
505f161d294SMark Johnston 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
506f903a308SMichael Tuexen 		return (EACCES);
507b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
508b89e82ddSJamie Gritton 		return (error);
50975c13541SPoul-Henning Kamp 
510623dce13SRobert Watson 	TCPDEBUG0;
511623dce13SRobert Watson 	inp = sotoinpcb(so);
512623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5138501a69cSRobert Watson 	INP_WLOCK(inp);
514eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
515eb96dc33SJulien Charbon 		error = ECONNREFUSED;
516623dce13SRobert Watson 		goto out;
517623dce13SRobert Watson 	}
518bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
519bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
520bd4a39ccSMark Johnston 		goto out;
521bd4a39ccSMark Johnston 	}
522623dce13SRobert Watson 	tp = intotcpcb(inp);
523623dce13SRobert Watson 	TCPDEBUG1();
524c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
525b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
526c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
52809fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
52937cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
53009fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
531c1604fe4SGleb Smirnoff 		goto out_in_epoch;
53209fe6320SNavdeep Parhar #endif
53309fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
53440fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5351d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5361d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
537c1604fe4SGleb Smirnoff out_in_epoch:
538109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
539623dce13SRobert Watson out:
540623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
541e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5428501a69cSRobert Watson 	INP_WUNLOCK(inp);
543623dce13SRobert Watson 	return (error);
5442c37256eSGarrett Wollman }
545b287c6c7SBjoern A. Zeeb #endif /* INET */
5462c37256eSGarrett Wollman 
547fb59c426SYoshinobu Inoue #ifdef INET6
548fb59c426SYoshinobu Inoue static int
549b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
550fb59c426SYoshinobu Inoue {
551109eb549SGleb Smirnoff 	struct epoch_tracker et;
552fb59c426SYoshinobu Inoue 	int error = 0;
553f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
554623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5550ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5564a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5574a91aa8fSMichael Tuexen 	u_char vflagsav;
558623dce13SRobert Watson 
559623dce13SRobert Watson 	TCPDEBUG0;
560fb59c426SYoshinobu Inoue 
5610ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
562f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
563f161d294SMark Johnston 		return (EAFNOSUPPORT);
5640ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
565e29ef13fSDon Lewis 		return (EINVAL);
566f161d294SMark Johnston 
56752710de1SPawel Jakub Dawidek 	/*
56852710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
56952710de1SPawel Jakub Dawidek 	 */
570f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
57152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
572fb59c426SYoshinobu Inoue 
573623dce13SRobert Watson 	inp = sotoinpcb(so);
574623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5758501a69cSRobert Watson 	INP_WLOCK(inp);
5764a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5774a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
578eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
579eb96dc33SJulien Charbon 		error = ECONNREFUSED;
580623dce13SRobert Watson 		goto out;
581623dce13SRobert Watson 	}
582bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
583bd4a39ccSMark Johnston 		error = EINVAL;
584bd4a39ccSMark Johnston 		goto out;
585bd4a39ccSMark Johnston 	}
586623dce13SRobert Watson 	tp = intotcpcb(inp);
587623dce13SRobert Watson 	TCPDEBUG1();
588b287c6c7SBjoern A. Zeeb #ifdef INET
589fa046d87SRobert Watson 	/*
590fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
591fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
592fa046d87SRobert Watson 	 * Is this a significant problem?
593fa046d87SRobert Watson 	 */
5940ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
595fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
596fb59c426SYoshinobu Inoue 
597d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
598d46a5312SMaxim Konovalov 			error = EINVAL;
599d46a5312SMaxim Konovalov 			goto out;
600d46a5312SMaxim Konovalov 		}
6015dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6025dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6035dba6adaSMichael Tuexen 			goto out;
6045dba6adaSMichael Tuexen 		}
60533841545SHajimu UMEMOTO 
6060ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
607888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
608888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
609888973f5SMichael Tuexen 			goto out;
610888973f5SMichael Tuexen 		}
611f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
612f903a308SMichael Tuexen 			error = EACCES;
6132cf21ae5SRandall Stewart 			goto out;
6142cf21ae5SRandall Stewart 		}
615b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
616b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
617413628a7SBjoern A. Zeeb 			goto out;
6184a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6194a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
620c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
621b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
622c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62409fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
625adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62609fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
627c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62809fe6320SNavdeep Parhar #endif
62940fa3e40SGleb Smirnoff 		error = tcp_output(tp);
630c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6315dba6adaSMichael Tuexen 	} else {
6325dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6335dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6345dba6adaSMichael Tuexen 			goto out;
6355dba6adaSMichael Tuexen 		}
636fb59c426SYoshinobu Inoue 	}
637b287c6c7SBjoern A. Zeeb #endif
6384a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6394a91aa8fSMichael Tuexen 		goto out;
640fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
641fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
642dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6430773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
644b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
6450773b44eSGleb Smirnoff 		goto out_in_epoch;
64609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
648adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
64909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6500773b44eSGleb Smirnoff 		goto out_in_epoch;
65109fe6320SNavdeep Parhar #endif
65209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
65340fa3e40SGleb Smirnoff 	error = tcp_output(tp);
654c1604fe4SGleb Smirnoff out_in_epoch:
655109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
656623dce13SRobert Watson out:
6571d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6581d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6594a91aa8fSMichael Tuexen 	/*
6604a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6614a91aa8fSMichael Tuexen 	 * the flags we modified.
6624a91aa8fSMichael Tuexen 	 */
6634a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6644a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6654a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6664a91aa8fSMichael Tuexen 	}
6674a91aa8fSMichael Tuexen 
668623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
6695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6708501a69cSRobert Watson 	INP_WUNLOCK(inp);
671623dce13SRobert Watson 	return (error);
672fb59c426SYoshinobu Inoue }
673fb59c426SYoshinobu Inoue #endif /* INET6 */
674fb59c426SYoshinobu Inoue 
6752c37256eSGarrett Wollman /*
6762c37256eSGarrett Wollman  * Initiate disconnect from peer.
6772c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6782c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6792c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6802c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6812c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6822c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6832c37256eSGarrett Wollman  *
6842c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6852c37256eSGarrett Wollman  */
6862c37256eSGarrett Wollman static int
6872c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6882c37256eSGarrett Wollman {
689f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
690623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6916573d758SMatt Macy 	struct epoch_tracker et;
692623dce13SRobert Watson 	int error = 0;
6932c37256eSGarrett Wollman 
694623dce13SRobert Watson 	TCPDEBUG0;
69597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
696623dce13SRobert Watson 	inp = sotoinpcb(so);
697623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6988501a69cSRobert Watson 	INP_WLOCK(inp);
699489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
70021367f63SSam Leffler 		error = ECONNRESET;
701623dce13SRobert Watson 		goto out;
702623dce13SRobert Watson 	}
703623dce13SRobert Watson 	tp = intotcpcb(inp);
704623dce13SRobert Watson 	TCPDEBUG1();
705623dce13SRobert Watson 	tcp_disconnect(tp);
706623dce13SRobert Watson out:
707623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7085d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7098501a69cSRobert Watson 	INP_WUNLOCK(inp);
71097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
711623dce13SRobert Watson 	return (error);
7122c37256eSGarrett Wollman }
7132c37256eSGarrett Wollman 
714b287c6c7SBjoern A. Zeeb #ifdef INET
7152c37256eSGarrett Wollman /*
7168296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7178296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7182c37256eSGarrett Wollman  */
7192c37256eSGarrett Wollman static int
72057bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7212c37256eSGarrett Wollman {
7222c37256eSGarrett Wollman 	int error = 0;
723f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
724b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7251db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
726b338b1fdSMateusz Guzik #endif
72726ef6ac4SDon Lewis 	struct in_addr addr;
72826ef6ac4SDon Lewis 	in_port_t port = 0;
7291db24ffbSJonathan Lemon 	TCPDEBUG0;
7302c37256eSGarrett Wollman 
7313d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7323d2d3ef4SRobert Watson 		return (ECONNABORTED);
733f76fcf6dSJeffrey Hsu 
734f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
735623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7368501a69cSRobert Watson 	INP_WLOCK(inp);
73753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
7383d2d3ef4SRobert Watson 		error = ECONNABORTED;
739623dce13SRobert Watson 		goto out;
740623dce13SRobert Watson 	}
741b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7421db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
743b338b1fdSMateusz Guzik #endif
7441db24ffbSJonathan Lemon 	TCPDEBUG1();
745f76fcf6dSJeffrey Hsu 
746f76fcf6dSJeffrey Hsu 	/*
74754d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
74826ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
74926ef6ac4SDon Lewis 	 * release the lock.
750f76fcf6dSJeffrey Hsu 	 */
75126ef6ac4SDon Lewis 	port = inp->inp_fport;
75226ef6ac4SDon Lewis 	addr = inp->inp_faddr;
753f76fcf6dSJeffrey Hsu 
754623dce13SRobert Watson out:
755623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7565d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7578501a69cSRobert Watson 	INP_WUNLOCK(inp);
75826ef6ac4SDon Lewis 	if (error == 0)
75926ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
76026ef6ac4SDon Lewis 	return error;
7612c37256eSGarrett Wollman }
762b287c6c7SBjoern A. Zeeb #endif /* INET */
7632c37256eSGarrett Wollman 
764fb59c426SYoshinobu Inoue #ifdef INET6
765fb59c426SYoshinobu Inoue static int
766fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
767fb59c426SYoshinobu Inoue {
768f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
769fb59c426SYoshinobu Inoue 	int error = 0;
770b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7711db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
772b338b1fdSMateusz Guzik #endif
77326ef6ac4SDon Lewis 	struct in_addr addr;
77426ef6ac4SDon Lewis 	struct in6_addr addr6;
7756573d758SMatt Macy 	struct epoch_tracker et;
77626ef6ac4SDon Lewis 	in_port_t port = 0;
77726ef6ac4SDon Lewis 	int v4 = 0;
7781db24ffbSJonathan Lemon 	TCPDEBUG0;
779fb59c426SYoshinobu Inoue 
780b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
781b4470c16SRobert Watson 		return (ECONNABORTED);
782f76fcf6dSJeffrey Hsu 
783f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
784623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
78597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
7868501a69cSRobert Watson 	INP_WLOCK(inp);
78753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
78821367f63SSam Leffler 		error = ECONNABORTED;
789623dce13SRobert Watson 		goto out;
790623dce13SRobert Watson 	}
791b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7921db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
793b338b1fdSMateusz Guzik #endif
7941db24ffbSJonathan Lemon 	TCPDEBUG1();
795623dce13SRobert Watson 
79626ef6ac4SDon Lewis 	/*
79726ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
79826ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
79926ef6ac4SDon Lewis 	 * release the lock.
80026ef6ac4SDon Lewis 	 */
80126ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
80226ef6ac4SDon Lewis 		v4 = 1;
80326ef6ac4SDon Lewis 		port = inp->inp_fport;
80426ef6ac4SDon Lewis 		addr = inp->inp_faddr;
80526ef6ac4SDon Lewis 	} else {
80626ef6ac4SDon Lewis 		port = inp->inp_fport;
80726ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
80826ef6ac4SDon Lewis 	}
80926ef6ac4SDon Lewis 
810623dce13SRobert Watson out:
811623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8125d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8138501a69cSRobert Watson 	INP_WUNLOCK(inp);
81497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
81526ef6ac4SDon Lewis 	if (error == 0) {
81626ef6ac4SDon Lewis 		if (v4)
81726ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
81826ef6ac4SDon Lewis 		else
81926ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
82026ef6ac4SDon Lewis 	}
82126ef6ac4SDon Lewis 	return error;
822fb59c426SYoshinobu Inoue }
823fb59c426SYoshinobu Inoue #endif /* INET6 */
824f76fcf6dSJeffrey Hsu 
825f76fcf6dSJeffrey Hsu /*
8262c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8272c37256eSGarrett Wollman  */
8282c37256eSGarrett Wollman static int
8292c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8302c37256eSGarrett Wollman {
8312c37256eSGarrett Wollman 	int error = 0;
832f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
833623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8346573d758SMatt Macy 	struct epoch_tracker et;
8352c37256eSGarrett Wollman 
836623dce13SRobert Watson 	TCPDEBUG0;
837623dce13SRobert Watson 	inp = sotoinpcb(so);
838623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8398501a69cSRobert Watson 	INP_WLOCK(inp);
84053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
8410af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
8420af4ce45SGleb Smirnoff 		return (ECONNRESET);
843623dce13SRobert Watson 	}
8440af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
8450af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
846623dce13SRobert Watson 	TCPDEBUG1();
8472c37256eSGarrett Wollman 	socantsendmore(so);
848623dce13SRobert Watson 	tcp_usrclosed(tp);
849ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
850f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
851623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8525d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
853f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
85497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
855623dce13SRobert Watson 
856623dce13SRobert Watson 	return (error);
8572c37256eSGarrett Wollman }
8582c37256eSGarrett Wollman 
8592c37256eSGarrett Wollman /*
8602c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8612c37256eSGarrett Wollman  */
8622c37256eSGarrett Wollman static int
8632c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8642c37256eSGarrett Wollman {
865109eb549SGleb Smirnoff 	struct epoch_tracker et;
866f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
867623dce13SRobert Watson 	struct tcpcb *tp = NULL;
868f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8692c37256eSGarrett Wollman 
870623dce13SRobert Watson 	TCPDEBUG0;
871623dce13SRobert Watson 	inp = sotoinpcb(so);
872623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8738501a69cSRobert Watson 	INP_WLOCK(inp);
87453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
87537a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
87637a7f557SGleb Smirnoff 		return (ECONNRESET);
877623dce13SRobert Watson 	}
87837a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
87937a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
880623dce13SRobert Watson 	TCPDEBUG1();
881281a0fd4SPatrick Kelsey 	/*
882281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
883281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
884281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
885281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
886281a0fd4SPatrick Kelsey 	 * expires.
887281a0fd4SPatrick Kelsey 	 */
88868bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
889281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
890281a0fd4SPatrick Kelsey 		goto out;
89109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
89209fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
89309fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
894460cf046SNavdeep Parhar 	else
89509fe6320SNavdeep Parhar #endif
896f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
897623dce13SRobert Watson out:
898623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
8995d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
900f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
901f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
902623dce13SRobert Watson 	return (error);
9032c37256eSGarrett Wollman }
9042c37256eSGarrett Wollman 
9052c37256eSGarrett Wollman /*
9062c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9079c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9089c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9099c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9109c9906e9SPeter Wemm  * generally are caller-frees.
9112c37256eSGarrett Wollman  */
9122c37256eSGarrett Wollman static int
91357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
914b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9152c37256eSGarrett Wollman {
91697a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9172c37256eSGarrett Wollman 	int error = 0;
918f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
919623dce13SRobert Watson 	struct tcpcb *tp = NULL;
920888973f5SMichael Tuexen #ifdef INET
92151e08d53SMichael Tuexen #ifdef INET6
92251e08d53SMichael Tuexen 	struct sockaddr_in sin;
92351e08d53SMichael Tuexen #endif
92451e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
925888973f5SMichael Tuexen #endif
926fb59c426SYoshinobu Inoue #ifdef INET6
927fb59c426SYoshinobu Inoue 	int isipv6;
928fb59c426SYoshinobu Inoue #endif
9294a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9304a91aa8fSMichael Tuexen 	u_char vflagsav;
9314a91aa8fSMichael Tuexen 	bool restoreflags;
9329c9906e9SPeter Wemm 	TCPDEBUG0;
9332c37256eSGarrett Wollman 
934d8acd268SMark Johnston 	if (control != NULL) {
935d8acd268SMark Johnston 		/* TCP doesn't do control messages (rights, creds, etc) */
936d8acd268SMark Johnston 		if (control->m_len) {
937d8acd268SMark Johnston 			m_freem(control);
9384287aa56SGleb Smirnoff 			return (EINVAL);
939d8acd268SMark Johnston 		}
940d8acd268SMark Johnston 		m_freem(control);	/* empty control, just free it */
941d8acd268SMark Johnston 	}
9424287aa56SGleb Smirnoff 
9434287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9444287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9454287aa56SGleb Smirnoff 	INP_WLOCK(inp);
94653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9474287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9484287aa56SGleb Smirnoff 			m_freem(m);
9494287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9504287aa56SGleb Smirnoff 		return (ECONNRESET);
9514287aa56SGleb Smirnoff 	}
9524287aa56SGleb Smirnoff 
9534287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9544287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9554287aa56SGleb Smirnoff 	restoreflags = false;
9564287aa56SGleb Smirnoff 	tp = intotcpcb(inp);
9574287aa56SGleb Smirnoff 
9584287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
9597d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9607d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
961d3b6c96bSRandall Stewart 		goto out;
9627d2608a5SMark Johnston 
9639c9906e9SPeter Wemm 	TCPDEBUG1();
964888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
965bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
966bd4a39ccSMark Johnston 			error = EINVAL;
967bd4a39ccSMark Johnston 			goto out;
968bd4a39ccSMark Johnston 		}
969888973f5SMichael Tuexen 		switch (nam->sa_family) {
970888973f5SMichael Tuexen #ifdef INET
971888973f5SMichael Tuexen 		case AF_INET:
972888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
973888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
974888973f5SMichael Tuexen 				error = EINVAL;
975888973f5SMichael Tuexen 				goto out;
976888973f5SMichael Tuexen 			}
977888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
978888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
979888973f5SMichael Tuexen 				goto out;
980888973f5SMichael Tuexen 			}
981888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
982888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
983888973f5SMichael Tuexen 				goto out;
984888973f5SMichael Tuexen 			}
985f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
986f903a308SMichael Tuexen 				error = EACCES;
9872cf21ae5SRandall Stewart 				goto out;
9882cf21ae5SRandall Stewart 			}
989888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9907d2608a5SMark Johnston 			    &sinp->sin_addr)))
991888973f5SMichael Tuexen 				goto out;
992888973f5SMichael Tuexen #ifdef INET6
993888973f5SMichael Tuexen 			isipv6 = 0;
994888973f5SMichael Tuexen #endif
995888973f5SMichael Tuexen 			break;
996888973f5SMichael Tuexen #endif /* INET */
997888973f5SMichael Tuexen #ifdef INET6
998888973f5SMichael Tuexen 		case AF_INET6:
999888973f5SMichael Tuexen 		{
10000ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1001888973f5SMichael Tuexen 
10020ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10030ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1004888973f5SMichael Tuexen 				error = EINVAL;
1005888973f5SMichael Tuexen 				goto out;
1006888973f5SMichael Tuexen 			}
1007e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1008e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1009e240ce42SMichael Tuexen 				goto out;
1010e240ce42SMichael Tuexen 			}
10110ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1012888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1013888973f5SMichael Tuexen 				goto out;
1014888973f5SMichael Tuexen 			}
10150ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1016888973f5SMichael Tuexen #ifdef INET
1017888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1018888973f5SMichael Tuexen 					error = EINVAL;
1019888973f5SMichael Tuexen 					goto out;
1020888973f5SMichael Tuexen 				}
1021888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1022888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1023888973f5SMichael Tuexen 					goto out;
1024888973f5SMichael Tuexen 				}
10254a91aa8fSMichael Tuexen 				restoreflags = true;
1026888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1027888973f5SMichael Tuexen 				sinp = &sin;
10280ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1029888973f5SMichael Tuexen 				if (IN_MULTICAST(
1030888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1031888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1032888973f5SMichael Tuexen 					goto out;
1033888973f5SMichael Tuexen 				}
1034888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10357d2608a5SMark Johnston 				    &sinp->sin_addr)))
1036888973f5SMichael Tuexen 					goto out;
1037888973f5SMichael Tuexen 				isipv6 = 0;
1038888973f5SMichael Tuexen #else /* !INET */
1039888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1040888973f5SMichael Tuexen 				goto out;
1041888973f5SMichael Tuexen #endif /* INET */
1042888973f5SMichael Tuexen 			} else {
1043888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1044888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1045888973f5SMichael Tuexen 					goto out;
1046888973f5SMichael Tuexen 				}
10474a91aa8fSMichael Tuexen 				restoreflags = true;
1048888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1049888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1050888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10517d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1052888973f5SMichael Tuexen 					goto out;
1053888973f5SMichael Tuexen 				isipv6 = 1;
1054888973f5SMichael Tuexen 			}
1055888973f5SMichael Tuexen 			break;
1056888973f5SMichael Tuexen 		}
1057888973f5SMichael Tuexen #endif /* INET6 */
1058888973f5SMichael Tuexen 		default:
1059888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1060888973f5SMichael Tuexen 			goto out;
1061888973f5SMichael Tuexen 		}
1062888973f5SMichael Tuexen 	}
10632c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
106408af8aacSRandall Stewart 		if (tp->t_acktime == 0)
106508af8aacSRandall Stewart 			tp->t_acktime = ticks;
1066651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10677d2608a5SMark Johnston 		m = NULL;
10682c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1069bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1070bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1071bd4a39ccSMark Johnston 
10722c37256eSGarrett Wollman 			/*
10732c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10742c37256eSGarrett Wollman 			 * initialize window to default value, and
10750c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10762c37256eSGarrett Wollman 			 */
1077fb59c426SYoshinobu Inoue #ifdef INET6
1078fb59c426SYoshinobu Inoue 			if (isipv6)
1079b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1080fb59c426SYoshinobu Inoue #endif /* INET6 */
1081b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1082b287c6c7SBjoern A. Zeeb 			else
1083b287c6c7SBjoern A. Zeeb #endif
1084b287c6c7SBjoern A. Zeeb #ifdef INET
1085888973f5SMichael Tuexen 				error = tcp_connect(tp,
1086888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1087b287c6c7SBjoern A. Zeeb #endif
10884a91aa8fSMichael Tuexen 			/*
10894a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10904a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10914a91aa8fSMichael Tuexen 			 * operations fail.
10924a91aa8fSMichael Tuexen 			 */
10934a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10944a91aa8fSMichael Tuexen 				restoreflags = false;
10954a91aa8fSMichael Tuexen 
10967d2608a5SMark Johnston 			if (error) {
10977d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10987d2608a5SMark Johnston 				sbflush(&so->so_snd);
10992c37256eSGarrett Wollman 				goto out;
11007d2608a5SMark Johnston 			}
1101c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1102c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
110318a75309SPatrick Kelsey 			else {
11042c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11052c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11062c37256eSGarrett Wollman 			}
1107c560df6fSPatrick Kelsey 		}
11082c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11092c37256eSGarrett Wollman 			/*
11102c37256eSGarrett Wollman 			 * Close the send side of the connection after
11112c37256eSGarrett Wollman 			 * the data is sent.
11122c37256eSGarrett Wollman 			 */
11132c37256eSGarrett Wollman 			socantsendmore(so);
1114623dce13SRobert Watson 			tcp_usrclosed(tp);
11152c37256eSGarrett Wollman 		}
1116e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1117e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1118e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1119e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1120e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1121e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1122e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1123e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1124e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1125e854dd38SRandall Stewart 		}
11262cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11272cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1128b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1129b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1130f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1131b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1132b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1133b0acefa8SBill Fenner 		}
11342c37256eSGarrett Wollman 	} else {
1135623dce13SRobert Watson 		/*
1136623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1137623dce13SRobert Watson 		 */
1138d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11392c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1140d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11412c37256eSGarrett Wollman 			error = ENOBUFS;
11422c37256eSGarrett Wollman 			goto out;
11432c37256eSGarrett Wollman 		}
11442c37256eSGarrett Wollman 		/*
11452c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11462c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11472c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11482c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11492c37256eSGarrett Wollman 		 * of data past the urgent section.
11502c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11512c37256eSGarrett Wollman 		 */
115208af8aacSRandall Stewart 		if (tp->t_acktime == 0)
115308af8aacSRandall Stewart 			tp->t_acktime = ticks;
1154651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1155d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
11567d2608a5SMark Johnston 		m = NULL;
1157ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1158ef53690bSGarrett Wollman 			/*
1159ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1160ef53690bSGarrett Wollman 			 * initialize window to default value, and
11610c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1162ef53690bSGarrett Wollman 			 */
116318a75309SPatrick Kelsey 
1164c560df6fSPatrick Kelsey 			/*
1165c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1166c560df6fSPatrick Kelsey 			 */
1167c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1168c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1169fb59c426SYoshinobu Inoue #ifdef INET6
1170fb59c426SYoshinobu Inoue 			if (isipv6)
1171b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1172fb59c426SYoshinobu Inoue #endif /* INET6 */
1173b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1174b287c6c7SBjoern A. Zeeb 			else
1175b287c6c7SBjoern A. Zeeb #endif
1176b287c6c7SBjoern A. Zeeb #ifdef INET
1177888973f5SMichael Tuexen 				error = tcp_connect(tp,
1178888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1179b287c6c7SBjoern A. Zeeb #endif
11804a91aa8fSMichael Tuexen 			/*
11814a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11824a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11834a91aa8fSMichael Tuexen 			 * operations fail.
11844a91aa8fSMichael Tuexen 			 */
11854a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11864a91aa8fSMichael Tuexen 				restoreflags = false;
11874a91aa8fSMichael Tuexen 
11887d2608a5SMark Johnston 			if (error != 0) {
11897d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11907d2608a5SMark Johnston 				sbflush(&so->so_snd);
1191ef53690bSGarrett Wollman 				goto out;
11927d2608a5SMark Johnston 			}
1193ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1194ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1195623dce13SRobert Watson 		}
1196300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11977d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11982cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1199f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
12002cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12012c37256eSGarrett Wollman 		}
12022cbcd3c1SGleb Smirnoff 	}
12032529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12042529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12052529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12062529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12072529f56eSJonathan T. Looney 	    0, NULL, false);
12087d2608a5SMark Johnston 
1209d1401c90SRobert Watson out:
12104a91aa8fSMichael Tuexen 	/*
12117d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
12127d2608a5SMark Johnston 	 * responsible for freeing memory.
12137d2608a5SMark Johnston 	 */
12147d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12157d2608a5SMark Johnston 		m_freem(m);
12167d2608a5SMark Johnston 
12177d2608a5SMark Johnston 	/*
12184a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12194a91aa8fSMichael Tuexen 	 * restore the original flags.
12204a91aa8fSMichael Tuexen 	 */
12214a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12224a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12234a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12244a91aa8fSMichael Tuexen 	}
1225d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12262c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12275d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12285d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1229f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
123097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
123173fddedaSPeter Grehan 	return (error);
12322c37256eSGarrett Wollman }
12332c37256eSGarrett Wollman 
12342cbcd3c1SGleb Smirnoff static int
12352cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12362cbcd3c1SGleb Smirnoff {
1237109eb549SGleb Smirnoff 	struct epoch_tracker et;
12382cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12392cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12402cbcd3c1SGleb Smirnoff 	int error;
12412cbcd3c1SGleb Smirnoff 
12422cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12432cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
124453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12452cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
124682334850SJohn Baldwin 		mb_free_notready(m, count);
12472cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12482cbcd3c1SGleb Smirnoff 	}
12492cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12502cbcd3c1SGleb Smirnoff 
12512cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12522cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12532cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1254f64dc2abSGleb Smirnoff 	if (error) {
12552cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1256f64dc2abSGleb Smirnoff 		return (error);
1257f64dc2abSGleb Smirnoff 	}
1258f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1259f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1260f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12612cbcd3c1SGleb Smirnoff 
12622cbcd3c1SGleb Smirnoff 	return (error);
12632cbcd3c1SGleb Smirnoff }
12642cbcd3c1SGleb Smirnoff 
12652c37256eSGarrett Wollman /*
1266a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12672c37256eSGarrett Wollman  */
1268ac45e92fSRobert Watson static void
12692c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12702c37256eSGarrett Wollman {
1271f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1272a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12736573d758SMatt Macy 	struct epoch_tracker et;
1274623dce13SRobert Watson 	TCPDEBUG0;
1275c78cbc7bSRobert Watson 
1276ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1277c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1278c78cbc7bSRobert Watson 
127997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12808501a69cSRobert Watson 	INP_WLOCK(inp);
1281c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1282c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1283c78cbc7bSRobert Watson 
1284c78cbc7bSRobert Watson 	/*
1285a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1286c78cbc7bSRobert Watson 	 */
128753af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1288c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1289a152f8a3SRobert Watson 		TCPDEBUG1();
12908fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12918fa799bdSJonathan T. Looney 		if (tp == NULL)
12928fa799bdSJonathan T. Looney 			goto dropped;
1293a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
12945d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1295c78cbc7bSRobert Watson 	}
1296ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1297d8596171SGleb Smirnoff 		soref(so);
1298ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1299a152f8a3SRobert Watson 	}
13008501a69cSRobert Watson 	INP_WUNLOCK(inp);
13018fa799bdSJonathan T. Looney dropped:
130297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1303a152f8a3SRobert Watson }
1304a152f8a3SRobert Watson 
1305a152f8a3SRobert Watson /*
1306a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1307a152f8a3SRobert Watson  */
1308a152f8a3SRobert Watson static void
1309a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1310a152f8a3SRobert Watson {
1311a152f8a3SRobert Watson 	struct inpcb *inp;
1312a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13136573d758SMatt Macy 	struct epoch_tracker et;
1314a152f8a3SRobert Watson 	TCPDEBUG0;
1315a152f8a3SRobert Watson 
1316a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1317a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1318a152f8a3SRobert Watson 
131997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13208501a69cSRobert Watson 	INP_WLOCK(inp);
1321a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1322a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1323a152f8a3SRobert Watson 
1324a152f8a3SRobert Watson 	/*
1325a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1326a152f8a3SRobert Watson 	 * a disconnect.
1327a152f8a3SRobert Watson 	 */
132853af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1329a152f8a3SRobert Watson 		tp = intotcpcb(inp);
133074703901SGleb Smirnoff 		tp->t_flags |= TF_CLOSED;
1331a152f8a3SRobert Watson 		TCPDEBUG1();
1332a152f8a3SRobert Watson 		tcp_disconnect(tp);
1333a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
13345d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1335a152f8a3SRobert Watson 	}
1336ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1337d8596171SGleb Smirnoff 		soref(so);
1338ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1339a152f8a3SRobert Watson 	}
13408501a69cSRobert Watson 	INP_WUNLOCK(inp);
134197a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13422c37256eSGarrett Wollman }
13432c37256eSGarrett Wollman 
1344d3b6c96bSRandall Stewart static int
1345d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1346d3b6c96bSRandall Stewart {
1347d3b6c96bSRandall Stewart 	/*
1348d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1349d3b6c96bSRandall Stewart 	 * specified then it does not always support
1350d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1351d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1352d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1353d3b6c96bSRandall Stewart 	 */
1354d3b6c96bSRandall Stewart 	int ret = 0;
1355d3b6c96bSRandall Stewart 
1356d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1357d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1358d3b6c96bSRandall Stewart 	}
1359d3b6c96bSRandall Stewart 	return (ret);
1360d3b6c96bSRandall Stewart }
1361d3b6c96bSRandall Stewart 
13622c37256eSGarrett Wollman /*
13632c37256eSGarrett Wollman  * Receive out-of-band data.
13642c37256eSGarrett Wollman  */
13652c37256eSGarrett Wollman static int
13662c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13672c37256eSGarrett Wollman {
13682c37256eSGarrett Wollman 	int error = 0;
1369f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1370623dce13SRobert Watson 	struct tcpcb *tp = NULL;
13712c37256eSGarrett Wollman 
1372623dce13SRobert Watson 	TCPDEBUG0;
1373623dce13SRobert Watson 	inp = sotoinpcb(so);
1374623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13758501a69cSRobert Watson 	INP_WLOCK(inp);
137653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
137721367f63SSam Leffler 		error = ECONNRESET;
1378623dce13SRobert Watson 		goto out;
1379623dce13SRobert Watson 	}
1380623dce13SRobert Watson 	tp = intotcpcb(inp);
1381d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1382d3b6c96bSRandall Stewart 	if (error) {
1383d3b6c96bSRandall Stewart 		goto out;
1384d3b6c96bSRandall Stewart 	}
1385623dce13SRobert Watson 	TCPDEBUG1();
13862c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1387c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13884cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13894cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13902c37256eSGarrett Wollman 		error = EINVAL;
13912c37256eSGarrett Wollman 		goto out;
13922c37256eSGarrett Wollman 	}
13932c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13942c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13952c37256eSGarrett Wollman 		goto out;
13962c37256eSGarrett Wollman 	}
13972c37256eSGarrett Wollman 	m->m_len = 1;
13982c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13992c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
14002c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1401623dce13SRobert Watson 
1402623dce13SRobert Watson out:
1403623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14045d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14058501a69cSRobert Watson 	INP_WUNLOCK(inp);
1406623dce13SRobert Watson 	return (error);
14072c37256eSGarrett Wollman }
14082c37256eSGarrett Wollman 
1409b287c6c7SBjoern A. Zeeb #ifdef INET
1410e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1411e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1412e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1413e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1414e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1415e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1416e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1417e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1418e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1419e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1420e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1421e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1422e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1423e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1424e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1425e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1426e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1427e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1428e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1429e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1430e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1431e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1432e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1433e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14342c37256eSGarrett Wollman };
1435b287c6c7SBjoern A. Zeeb #endif /* INET */
1436df8bae1dSRodney W. Grimes 
1437fb59c426SYoshinobu Inoue #ifdef INET6
1438e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1439e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1440e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1441e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1442e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1443e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1444e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1445e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1446e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1447e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1448e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1449e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1450e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1451e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1452e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1453e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1454e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1455e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1456e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1457e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1458e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1459e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1460e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1461e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1462fb59c426SYoshinobu Inoue };
1463fb59c426SYoshinobu Inoue #endif /* INET6 */
1464fb59c426SYoshinobu Inoue 
1465b287c6c7SBjoern A. Zeeb #ifdef INET
1466a0292f23SGarrett Wollman /*
1467a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1468a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
14695200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
14705200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
14715200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
14725200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1473a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1474a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1475a0292f23SGarrett Wollman  */
14760312fbe9SPoul-Henning Kamp static int
1477ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1478a0292f23SGarrett Wollman {
14799eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp), *oinp;
14809eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
14815200e00eSIan Dowse 	struct in_addr laddr;
14825200e00eSIan Dowse 	u_short lport;
1483c3229e05SDavid Greenman 	int error;
1484a0292f23SGarrett Wollman 
1485c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14868501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1487fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1488623dce13SRobert Watson 
148925102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
14904616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
14914616026fSErmal Luçi 		if (error)
1492fa046d87SRobert Watson 			goto out;
1493a0292f23SGarrett Wollman 	}
1494a0292f23SGarrett Wollman 
1495a0292f23SGarrett Wollman 	/*
1496a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1497a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1498a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1499a0292f23SGarrett Wollman 	 */
15005200e00eSIan Dowse 	laddr = inp->inp_laddr;
15015200e00eSIan Dowse 	lport = inp->inp_lport;
15025200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1503b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
15045200e00eSIan Dowse 	if (error && oinp == NULL)
1505fa046d87SRobert Watson 		goto out;
1506fa046d87SRobert Watson 	if (oinp) {
1507fa046d87SRobert Watson 		error = EADDRINUSE;
1508fa046d87SRobert Watson 		goto out;
1509fa046d87SRobert Watson 	}
151025102351SMike Karels 	/* Handle initial bind if it hadn't been done in advance. */
151125102351SMike Karels 	if (inp->inp_lport == 0) {
151225102351SMike Karels 		inp->inp_lport = lport;
151325102351SMike Karels 		if (in_pcbinshash(inp) != 0) {
151425102351SMike Karels 			inp->inp_lport = 0;
151525102351SMike Karels 			error = EAGAIN;
151625102351SMike Karels 			goto out;
151725102351SMike Karels 		}
151825102351SMike Karels 	}
15195200e00eSIan Dowse 	inp->inp_laddr = laddr;
152015bd2b43SDavid Greenman 	in_pcbrehash(inp);
1521fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1522a0292f23SGarrett Wollman 
1523087b55eaSAndre Oppermann 	/*
1524087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1525087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1526087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1527087b55eaSAndre Oppermann 	 */
1528a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15299b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1530a0292f23SGarrett Wollman 		tp->request_r_scale++;
1531a0292f23SGarrett Wollman 
1532a0292f23SGarrett Wollman 	soisconnecting(so);
153378b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
153457f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15358e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15368e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15378e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1538a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1539a45d2726SAndras Olah 
1540a0292f23SGarrett Wollman 	return 0;
1541fa046d87SRobert Watson 
1542fa046d87SRobert Watson out:
1543fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1544fa046d87SRobert Watson 	return (error);
1545a0292f23SGarrett Wollman }
1546b287c6c7SBjoern A. Zeeb #endif /* INET */
1547a0292f23SGarrett Wollman 
1548fb59c426SYoshinobu Inoue #ifdef INET6
1549fb59c426SYoshinobu Inoue static int
1550ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1551fb59c426SYoshinobu Inoue {
15529eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1553fb59c426SYoshinobu Inoue 	int error;
1554fb59c426SYoshinobu Inoue 
15558501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1556fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1557623dce13SRobert Watson 
155825102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
15594616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15604616026fSErmal Luçi 		if (error)
1561fa046d87SRobert Watson 			goto out;
1562fb59c426SYoshinobu Inoue 	}
1563a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1564a7e201bbSAndrey V. Elsukov 	if (error != 0)
1565b598155aSRobert Watson 		goto out;
1566fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1567fb59c426SYoshinobu Inoue 
1568fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1569fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1570970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1571fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1572fb59c426SYoshinobu Inoue 
1573a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
157478b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
157557f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15768e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15778e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15788e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1579fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1580fb59c426SYoshinobu Inoue 
1581fb59c426SYoshinobu Inoue 	return 0;
1582fa046d87SRobert Watson 
1583fa046d87SRobert Watson out:
1584fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1585fa046d87SRobert Watson 	return error;
1586fb59c426SYoshinobu Inoue }
1587fb59c426SYoshinobu Inoue #endif /* INET6 */
1588fb59c426SYoshinobu Inoue 
1589cfe8b629SGarrett Wollman /*
1590b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1591b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1592b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1593b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1594b8af5dfaSRobert Watson  * from Linux.
1595b8af5dfaSRobert Watson  */
1596b8af5dfaSRobert Watson static void
1597ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1598b8af5dfaSRobert Watson {
1599b8af5dfaSRobert Watson 
16009eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1601b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1602b8af5dfaSRobert Watson 
1603b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1604b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1605b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
16063529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1607b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1608b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1609b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1610b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1611b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1612b8af5dfaSRobert Watson 	}
16133f169c54SRichard Scheffenegger 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
16145a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
16151baaf834SBruce M Simpson 
161643d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
16173ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
16181baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
16191baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16201baaf834SBruce M Simpson 
1621b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1622b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1623b8af5dfaSRobert Watson 
1624b8af5dfaSRobert Watson 	/*
1625b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1626b8af5dfaSRobert Watson 	 */
1627c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1628535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1629b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16301c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1631535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
163243d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
163343d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1634f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1635f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1636f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1637a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1638a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1639a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1640a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1641a6456410SNavdeep Parhar 	}
1642a6456410SNavdeep Parhar #endif
164322c81cc5SRichard Scheffenegger 	/*
164422c81cc5SRichard Scheffenegger 	 * AccECN related counters.
164522c81cc5SRichard Scheffenegger 	 */
164622c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
164722c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
164822c81cc5SRichard Scheffenegger 		/*
164922c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
165022c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
165122c81cc5SRichard Scheffenegger 		 */
165222c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
165322c81cc5SRichard Scheffenegger 	else
165422c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
165522c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1656b8af5dfaSRobert Watson }
1657b8af5dfaSRobert Watson 
1658b8af5dfaSRobert Watson /*
16591e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16601e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16611e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16621e8f5ffaSRobert Watson  * option.
1663cfe8b629SGarrett Wollman  */
1664bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16658501a69cSRobert Watson 	INP_WLOCK(inp);							\
166653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16678501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1668bac5bedfSConrad Meyer 		cleanup;						\
16691e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16701e8f5ffaSRobert Watson 	}								\
16711e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16721e8f5ffaSRobert Watson } while(0)
1673bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16741e8f5ffaSRobert Watson 
1675fd7daa72SMichael Tuexen int
1676fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1677df8bae1dSRodney W. Grimes {
1678fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1679fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1680fc4d53ccSGleb Smirnoff 	int error = 0;
1681df8bae1dSRodney W. Grimes 
1682fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16833b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
168453af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1685fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1686fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1687fc4d53ccSGleb Smirnoff 
1688cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16893b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1690fb59c426SYoshinobu Inoue #ifdef INET6
1691de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1692fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1693de156263SGleb Smirnoff #endif
1694de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1695de156263SGleb Smirnoff 		else
1696de156263SGleb Smirnoff #endif
1697de156263SGleb Smirnoff #ifdef INET
1698fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1699de156263SGleb Smirnoff #endif
17005dff1c38SMichael Tuexen 		/*
1701de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1702de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1703de156263SGleb Smirnoff 		 * IP level returned.
17045dff1c38SMichael Tuexen 		 */
1705de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1706de156263SGleb Smirnoff #ifdef INET6
1707de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1708de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1709de156263SGleb Smirnoff 				return (error);
1710de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1711de156263SGleb Smirnoff 			case IPV6_TCLASS:
1712de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1713de156263SGleb Smirnoff 				break;
1714de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1715f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1716f581a26eSGleb Smirnoff 				break;
1717de156263SGleb Smirnoff 			default:
1718de156263SGleb Smirnoff 				return (error);
17195dff1c38SMichael Tuexen 			}
1720de156263SGleb Smirnoff 			break;
1721b287c6c7SBjoern A. Zeeb #endif
1722b287c6c7SBjoern A. Zeeb #ifdef INET
1723de156263SGleb Smirnoff 		case IPPROTO_IP:
1724de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1725de156263SGleb Smirnoff 			case IP_TOS:
17263b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
17273b0ee680SRichard Scheffenegger 				break;
1728de156263SGleb Smirnoff 			case IP_TTL:
1729de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1730de156263SGleb Smirnoff 				break;
1731de156263SGleb Smirnoff 			default:
1732df8bae1dSRodney W. Grimes 				return (error);
1733de156263SGleb Smirnoff 			}
1734de156263SGleb Smirnoff 			break;
1735de156263SGleb Smirnoff #endif
1736de156263SGleb Smirnoff 		default:
1737de156263SGleb Smirnoff 			return (error);
1738de156263SGleb Smirnoff 		}
17393b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
174053af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
17413b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
17423b3c08c1SMichael Tuexen 			return (ECONNRESET);
17433b3c08c1SMichael Tuexen 		}
1744fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1745fc4d53ccSGleb Smirnoff 		/*
1746fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1747fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1748fc4d53ccSGleb Smirnoff 		 */
1749fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1750fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
1751fc4d53ccSGleb Smirnoff 
17523b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1753fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1754fc4d53ccSGleb Smirnoff 		if (error)
1755fc4d53ccSGleb Smirnoff 			return (error);
1756fc4d53ccSGleb Smirnoff 
175768cea2b1SJohn Baldwin 		INP_WLOCK(inp);
175853af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
17598501a69cSRobert Watson 			INP_WUNLOCK(inp);
17601e8f5ffaSRobert Watson 			return (ECONNRESET);
1761623dce13SRobert Watson 		}
176255bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1763fc4d53ccSGleb Smirnoff 
176455bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
176555bceb1eSRandall Stewart 		if (blk == NULL) {
176655bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
176755bceb1eSRandall Stewart 			return (ENOENT);
176855bceb1eSRandall Stewart 		}
1769587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1770587d67c0SRandall Stewart 			/* You already have this */
1771587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1772587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1773587d67c0SRandall Stewart 			return (0);
1774587d67c0SRandall Stewart 		}
1775587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1776587d67c0SRandall Stewart 			/*
1777587d67c0SRandall Stewart 			 * The user has advanced the state
1778587d67c0SRandall Stewart 			 * past the initial point, we may not
1779587d67c0SRandall Stewart 			 * be able to switch.
1780587d67c0SRandall Stewart 			 */
1781587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1782587d67c0SRandall Stewart 				/*
1783587d67c0SRandall Stewart 				 * Does the stack provide a
1784587d67c0SRandall Stewart 				 * query mechanism, if so it may
1785587d67c0SRandall Stewart 				 * still be possible?
1786587d67c0SRandall Stewart 				 */
1787587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1788c6c0be27SMichael Tuexen 			} else
1789c6c0be27SMichael Tuexen 				error = EINVAL;
1790587d67c0SRandall Stewart 			if (error) {
1791587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1792587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1793587d67c0SRandall Stewart 				return(error);
1794587d67c0SRandall Stewart 			}
1795587d67c0SRandall Stewart 		}
179655bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
179755bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
179855bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
179955bceb1eSRandall Stewart 			return (ENOENT);
180055bceb1eSRandall Stewart 		}
180155bceb1eSRandall Stewart 		/*
180255bceb1eSRandall Stewart 		 * Release the old refcnt, the
1803587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1804587d67c0SRandall Stewart 		 * new one already.
180555bceb1eSRandall Stewart 		 */
1806587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1807086a3556SAndrew Gallatin 			struct epoch_tracker et;
1808587d67c0SRandall Stewart 			/*
1809587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1810587d67c0SRandall Stewart 			 * the tcb is not going away.
1811587d67c0SRandall Stewart 			 */
1812086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1813587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1814086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1815587d67c0SRandall Stewart 		}
18163ee9c3c4SRandall Stewart #ifdef TCPHPTS
18173ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
18189eb0e832SGleb Smirnoff 		tcp_hpts_remove(tptoinpcb(tp));
18193ee9c3c4SRandall Stewart #endif
18203ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
18213ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
18223ee9c3c4SRandall Stewart 			if (error) {
18233ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
18243ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
18253ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
18263ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
18273ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
1828fd7daa72SMichael Tuexen 						soabort(so);
18293ee9c3c4SRandall Stewart 						return (error);
18303ee9c3c4SRandall Stewart 					}
18313ee9c3c4SRandall Stewart 				}
18323ee9c3c4SRandall Stewart 				goto err_out;
18333ee9c3c4SRandall Stewart 			}
18343ee9c3c4SRandall Stewart 		}
183555bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
183655bceb1eSRandall Stewart 		tp->t_fb = blk;
183755bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
183855bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
183955bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
184055bceb1eSRandall Stewart 			     sopt->sopt_name);
184155bceb1eSRandall Stewart 		}
184255bceb1eSRandall Stewart #endif
18433ee9c3c4SRandall Stewart err_out:
184455bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
184555bceb1eSRandall Stewart 		return (error);
1846fc4d53ccSGleb Smirnoff 	}
1847fc4d53ccSGleb Smirnoff 
18483b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
18493b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1850fc4d53ccSGleb Smirnoff }
1851fc4d53ccSGleb Smirnoff 
1852fc4d53ccSGleb Smirnoff static int
1853fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1854fc4d53ccSGleb Smirnoff {
1855fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1856fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1857fc4d53ccSGleb Smirnoff 	int error = 0;
1858fc4d53ccSGleb Smirnoff 
1859fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18603b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
186153af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1862fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1863fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1864fc4d53ccSGleb Smirnoff 
1865fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18663b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1867fc4d53ccSGleb Smirnoff #ifdef INET6
1868fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1869fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1870fc4d53ccSGleb Smirnoff #endif /* INET6 */
1871fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1872fc4d53ccSGleb Smirnoff 		else
1873fc4d53ccSGleb Smirnoff #endif
1874fc4d53ccSGleb Smirnoff #ifdef INET
1875fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1876fc4d53ccSGleb Smirnoff #endif
1877fc4d53ccSGleb Smirnoff 		return (error);
1878fc4d53ccSGleb Smirnoff 	}
1879fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1880e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1881fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1882fc4d53ccSGleb Smirnoff 
1883e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1884e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1885e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1886e2833083SPeter Lei 		} else {
1887e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1888e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1889c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1890c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1891e2833083SPeter Lei 		}
189255bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
189355bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
189455bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
189555bceb1eSRandall Stewart 		return (error);
189655bceb1eSRandall Stewart 	}
1897fc4d53ccSGleb Smirnoff 
18983b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
18993b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1900fc4d53ccSGleb Smirnoff }
1901fc4d53ccSGleb Smirnoff 
1902fc4d53ccSGleb Smirnoff int
1903fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1904fc4d53ccSGleb Smirnoff {
1905fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1906fc4d53ccSGleb Smirnoff 
1907fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1908fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1909fc4d53ccSGleb Smirnoff 
19103b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
191153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
19123b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
19133b3c08c1SMichael Tuexen 		return (ECONNRESET);
19143b3c08c1SMichael Tuexen 	}
1915fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1916fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1917fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1918fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1919fc4d53ccSGleb Smirnoff 	else
1920fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
192155bceb1eSRandall Stewart }
192255bceb1eSRandall Stewart 
19232529f56eSJonathan T. Looney /*
19242529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
19252529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
19262529f56eSJonathan T. Looney  */
19272529f56eSJonathan T. Looney #ifdef CTASSERT
19282529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
19292529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
19302529f56eSJonathan T. Looney #endif
19312529f56eSJonathan T. Looney 
1932ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1933ec1db6e1SJohn Baldwin static int
1934ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1935ec1db6e1SJohn Baldwin {
1936ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1937ec1db6e1SJohn Baldwin 	int error;
1938ec1db6e1SJohn Baldwin 
1939ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1940ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1941ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1942ec1db6e1SJohn Baldwin 		if (error)
1943ec1db6e1SJohn Baldwin 			return (error);
1944ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1945ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1946ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1947ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1948ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1949ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1950ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1951ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1952ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1953ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1954ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1955ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1956ec1db6e1SJohn Baldwin 		return (0);
1957ec1db6e1SJohn Baldwin 	}
1958ec1db6e1SJohn Baldwin 
1959ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1960ec1db6e1SJohn Baldwin }
1961ec1db6e1SJohn Baldwin #endif
1962ec1db6e1SJohn Baldwin 
1963b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1964b8d60729SRandall Stewart 
1965b8d60729SRandall Stewart static int
1966ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1967b8d60729SRandall Stewart {
1968b8d60729SRandall Stewart 	struct cc_algo *algo;
1969b8d60729SRandall Stewart 	void *ptr = NULL;
19703b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1971b8d60729SRandall Stewart 	struct cc_var cc_mem;
1972b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1973b8d60729SRandall Stewart 	size_t mem_sz;
1974b8d60729SRandall Stewart 	int error;
1975b8d60729SRandall Stewart 
1976b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1977b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1978b8d60729SRandall Stewart 	if (error)
1979b8d60729SRandall Stewart 		return(error);
1980b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1981b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1982ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1983b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1984b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1985b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1986b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1987b8d60729SRandall Stewart 				continue;
1988b8d60729SRandall Stewart 			}
1989b8d60729SRandall Stewart 			break;
1990b8d60729SRandall Stewart 		}
1991ea9017fbSRandall Stewart 	}
1992b8d60729SRandall Stewart 	if (algo == NULL) {
1993b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1994b8d60729SRandall Stewart 		return(ESRCH);
1995b8d60729SRandall Stewart 	}
1996ea9017fbSRandall Stewart 	/*
1997ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1998ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1999ea9017fbSRandall Stewart 	 */
2000ea9017fbSRandall Stewart 	cc_refer(algo);
2001ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2002b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
2003b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
2004b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
2005b8d60729SRandall Stewart 		if (mem_sz == 0) {
2006b8d60729SRandall Stewart 			goto no_mem_needed;
2007b8d60729SRandall Stewart 		}
2008b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
2009b8d60729SRandall Stewart 	} else {
2010b8d60729SRandall Stewart no_mem_needed:
2011b8d60729SRandall Stewart 		mem_sz = 0;
2012b8d60729SRandall Stewart 		ptr = NULL;
2013b8d60729SRandall Stewart 	}
2014b8d60729SRandall Stewart 	/*
2015b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
2016b8d60729SRandall Stewart 	 * back the inplock.
2017b8d60729SRandall Stewart 	 */
2018b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
2019df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
202053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
2021df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
2022ea9017fbSRandall Stewart 		if (ptr)
2023df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
2024ea9017fbSRandall Stewart 		/* Release our temp reference */
2025ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
2026ea9017fbSRandall Stewart 		cc_release(algo);
2027ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
2028df07bfdaSMichael Tuexen 		return (ECONNRESET);
2029df07bfdaSMichael Tuexen 	}
2030df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
2031df07bfdaSMichael Tuexen 	if (ptr != NULL)
2032b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
2033b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
2034b8d60729SRandall Stewart 	/*
2035b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
2036b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
2037b8d60729SRandall Stewart 	 * Note here we init into stack memory.
2038b8d60729SRandall Stewart 	 */
2039b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
2040b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
2041b8d60729SRandall Stewart 	else
2042b8d60729SRandall Stewart 		error = 0;
2043b8d60729SRandall Stewart 	/*
2044b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
2045b8d60729SRandall Stewart 	 * should not fail we could in theory have a
2046b8d60729SRandall Stewart 	 * KASSERT here.
2047b8d60729SRandall Stewart 	 */
2048b8d60729SRandall Stewart 	if (error == 0) {
2049b8d60729SRandall Stewart 		/*
2050b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
2051b8d60729SRandall Stewart 		 * connection to the new CC module by
2052b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
2053b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
2054b8d60729SRandall Stewart 		 */
2055b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
2056b8d60729SRandall Stewart 			CC_ALGO(tp)->cb_destroy(tp->ccv);
2057ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
2058ea9017fbSRandall Stewart 		cc_detach(tp);
2059ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
2060b8d60729SRandall Stewart 		memcpy(tp->ccv, &cc_mem, sizeof(struct cc_var));
2061ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
2062ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2063b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2064b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2065b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2066b8d60729SRandall Stewart 			CC_ALGO(tp)->conn_init(tp->ccv);
2067b8d60729SRandall Stewart 		}
2068b8d60729SRandall Stewart 	} else if (ptr)
2069b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2070b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2071ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2072ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2073ea9017fbSRandall Stewart 	cc_release(algo);
2074ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2075b8d60729SRandall Stewart 	return (error);
2076b8d60729SRandall Stewart }
2077b8d60729SRandall Stewart 
207855bceb1eSRandall Stewart int
20793b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
208055bceb1eSRandall Stewart {
2081fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
208255bceb1eSRandall Stewart 	int	error, opt, optval;
208355bceb1eSRandall Stewart 	u_int	ui;
208455bceb1eSRandall Stewart 	struct	tcp_info ti;
2085b2e60773SJohn Baldwin #ifdef KERN_TLS
2086b2e60773SJohn Baldwin 	struct tls_enable tls;
2087528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2088b2e60773SJohn Baldwin #endif
20892529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2090adc56f5aSEdward Tomasz Napierala #ifdef STATS
2091adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2092adc56f5aSEdward Tomasz Napierala #endif
2093af6fef3aSGleb Smirnoff 	size_t	len;
2094df8bae1dSRodney W. Grimes 
2095f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
209653af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2097fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2098528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2099f581a26eSGleb Smirnoff 
2100f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2101f581a26eSGleb Smirnoff #ifdef INET6
2102f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2103f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2104f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2105f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2106f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2107f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2108f581a26eSGleb Smirnoff 		}
2109f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2110f581a26eSGleb Smirnoff 		return (0);
2111f581a26eSGleb Smirnoff #endif
2112f581a26eSGleb Smirnoff #ifdef INET
2113f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2114f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2115f581a26eSGleb Smirnoff 		return (0);
2116f581a26eSGleb Smirnoff #endif
2117f581a26eSGleb Smirnoff 	}
2118f581a26eSGleb Smirnoff 
2119d519cedbSGleb Smirnoff 	/*
2120d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2121d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2122d519cedbSGleb Smirnoff 	 */
2123d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2124d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2125d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2126c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2127c8b53cedSMichael Tuexen 			return (EINVAL);
2128af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2129af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2130d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2131d519cedbSGleb Smirnoff 		if (error) {
2132af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2133d519cedbSGleb Smirnoff 			return (error);
2134d519cedbSGleb Smirnoff 		}
2135bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2136d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2137af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
2138d519cedbSGleb Smirnoff 		else
2139d519cedbSGleb Smirnoff 			error = ENOENT;
2140d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2141d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2142af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2143af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2144d519cedbSGleb Smirnoff 		return (error);
2145d519cedbSGleb Smirnoff 	}
2146d519cedbSGleb Smirnoff 
2147cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2148cfe8b629SGarrett Wollman 	case SOPT_SET:
2149cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2150fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
215188f6b043SBruce M Simpson 		case TCP_MD5SIG:
21528501a69cSRobert Watson 			INP_WUNLOCK(inp);
215397453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2154fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2155fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
21561cfd4b53SBruce M Simpson 			if (error)
21571e8f5ffaSRobert Watson 				return (error);
215897453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
215909fe6320SNavdeep Parhar 			goto unlock_and_done;
2160fcf59617SAndrey V. Elsukov #endif /* IPSEC */
216109fe6320SNavdeep Parhar 
2162df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2163cfe8b629SGarrett Wollman 		case TCP_NOOPT:
21640471a8c7SRichard Scheffenegger 		case TCP_LRD:
21658501a69cSRobert Watson 			INP_WUNLOCK(inp);
2166cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2167cfe8b629SGarrett Wollman 			    sizeof optval);
2168cfe8b629SGarrett Wollman 			if (error)
21691e8f5ffaSRobert Watson 				return (error);
2170cfe8b629SGarrett Wollman 
21718501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2172cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2173cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2174cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2175cfe8b629SGarrett Wollman 				break;
2176cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2177cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2178cfe8b629SGarrett Wollman 				break;
21790471a8c7SRichard Scheffenegger 			case TCP_LRD:
21800471a8c7SRichard Scheffenegger 				opt = TF_LRD;
21810471a8c7SRichard Scheffenegger 				break;
2182cfe8b629SGarrett Wollman 			default:
2183cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2184cfe8b629SGarrett Wollman 				break;
2185cfe8b629SGarrett Wollman 			}
2186cfe8b629SGarrett Wollman 
2187cfe8b629SGarrett Wollman 			if (optval)
2188cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2189df8bae1dSRodney W. Grimes 			else
2190cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
219109fe6320SNavdeep Parhar unlock_and_done:
219209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
219309fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
219409fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
219509fe6320SNavdeep Parhar 				    sopt->sopt_name);
219609fe6320SNavdeep Parhar 			}
219709fe6320SNavdeep Parhar #endif
21988501a69cSRobert Watson 			INP_WUNLOCK(inp);
2199df8bae1dSRodney W. Grimes 			break;
2200df8bae1dSRodney W. Grimes 
2201007581c0SJonathan Lemon 		case TCP_NOPUSH:
22028501a69cSRobert Watson 			INP_WUNLOCK(inp);
2203007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2204007581c0SJonathan Lemon 			    sizeof optval);
2205007581c0SJonathan Lemon 			if (error)
22061e8f5ffaSRobert Watson 				return (error);
2207007581c0SJonathan Lemon 
22088501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2209007581c0SJonathan Lemon 			if (optval)
2210007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2211d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2212007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2213109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2214109eb549SGleb Smirnoff 					struct epoch_tracker et;
2215109eb549SGleb Smirnoff 
2216109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2217f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2218109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2219109eb549SGleb Smirnoff 				}
2220007581c0SJonathan Lemon 			}
222109fe6320SNavdeep Parhar 			goto unlock_and_done;
2222007581c0SJonathan Lemon 
22239e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
22249e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
22259e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
22269e644c23SMichael Tuexen 			    sizeof optval);
22279e644c23SMichael Tuexen 			if (error)
22289e644c23SMichael Tuexen 				return (error);
22299e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
22309e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
22319e644c23SMichael Tuexen 				/* Its got to be in range */
22329e644c23SMichael Tuexen 				return (EINVAL);
22339e644c23SMichael Tuexen 			}
22349e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
22359e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
22369e644c23SMichael Tuexen 				return (EINVAL);
22379e644c23SMichael Tuexen 			}
22389e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
22399e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
22409e644c23SMichael Tuexen 				/* You can't change after you are connected */
22419e644c23SMichael Tuexen 				error = EINVAL;
22429e644c23SMichael Tuexen 			} else {
22439e644c23SMichael Tuexen 				/* Ok we are all good set the port */
22449e644c23SMichael Tuexen 				tp->t_port = htons(optval);
22459e644c23SMichael Tuexen 			}
22469e644c23SMichael Tuexen 			goto unlock_and_done;
22479e644c23SMichael Tuexen 
2248df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
22498501a69cSRobert Watson 			INP_WUNLOCK(inp);
2250cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2251cfe8b629SGarrett Wollman 			    sizeof optval);
2252cfe8b629SGarrett Wollman 			if (error)
22531e8f5ffaSRobert Watson 				return (error);
2254df8bae1dSRodney W. Grimes 
22558501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
225653369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2257603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2258cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2259a0292f23SGarrett Wollman 			else
2260a0292f23SGarrett Wollman 				error = EINVAL;
226109fe6320SNavdeep Parhar 			goto unlock_and_done;
2262a0292f23SGarrett Wollman 
2263b8af5dfaSRobert Watson 		case TCP_INFO:
22648501a69cSRobert Watson 			INP_WUNLOCK(inp);
2265b8af5dfaSRobert Watson 			error = EINVAL;
2266b8af5dfaSRobert Watson 			break;
2267b8af5dfaSRobert Watson 
2268adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2269adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2270adc56f5aSEdward Tomasz Napierala #ifdef STATS
2271adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2272adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2273adc56f5aSEdward Tomasz Napierala 			if (error)
2274adc56f5aSEdward Tomasz Napierala 				return (error);
2275adc56f5aSEdward Tomasz Napierala 
2276adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2277adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2278adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2279adc56f5aSEdward Tomasz Napierala 			else
2280adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2281adc56f5aSEdward Tomasz Napierala 
2282adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2283adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2284adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2285adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2286adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2287adc56f5aSEdward Tomasz Napierala 				sbp = t;
2288adc56f5aSEdward Tomasz Napierala 			}
2289adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2290adc56f5aSEdward Tomasz Napierala 
2291adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2292adc56f5aSEdward Tomasz Napierala #else
2293adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2294adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2295adc56f5aSEdward Tomasz Napierala 			break;
2296adc56f5aSEdward Tomasz Napierala 
2297dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2298ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
229973e263b1SGleb Smirnoff 			break;
2300dbc42409SLawrence Stewart 
2301a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2302a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2303a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2304a034518aSAndrew Gallatin 			    sizeof(optval));
2305a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2306a034518aSAndrew Gallatin 			if (!error)
2307a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2308a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2309a034518aSAndrew Gallatin 			break;
2310a034518aSAndrew Gallatin 
2311b2e60773SJohn Baldwin #ifdef KERN_TLS
2312b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2313b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2314ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2315b2e60773SJohn Baldwin 			if (error)
2316b2e60773SJohn Baldwin 				break;
2317fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2318b2e60773SJohn Baldwin 			break;
2319b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2320b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2321b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2322b2e60773SJohn Baldwin 			if (error)
2323b2e60773SJohn Baldwin 				return (error);
2324b2e60773SJohn Baldwin 
2325b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2326fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2327b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2328b2e60773SJohn Baldwin 			break;
2329f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2330f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2331f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2332f1f93475SJohn Baldwin 			    sizeof(tls));
2333f1f93475SJohn Baldwin 			if (error)
2334f1f93475SJohn Baldwin 				break;
2335fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2336f1f93475SJohn Baldwin 			break;
2337b2e60773SJohn Baldwin #endif
233808af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
23399077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
23409077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
23419077f387SGleb Smirnoff 		case TCP_KEEPINIT:
23429077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
23439077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
23449077f387SGleb Smirnoff 			if (error)
23459077f387SGleb Smirnoff 				return (error);
23469077f387SGleb Smirnoff 
23479077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
23489077f387SGleb Smirnoff 				error = EINVAL;
23499077f387SGleb Smirnoff 				break;
23509077f387SGleb Smirnoff 			}
23519077f387SGleb Smirnoff 			ui *= hz;
23529077f387SGleb Smirnoff 
23539077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
23549077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
235508af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
235608af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
235708af8aacSRandall Stewart 				break;
235808af8aacSRandall Stewart 
23599077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23609077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23619077f387SGleb Smirnoff 				/*
23629077f387SGleb Smirnoff 				 * XXX: better check current remaining
23639077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
23649077f387SGleb Smirnoff 				 */
23659077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
23669077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
23679077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23689077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23699077f387SGleb Smirnoff 				break;
23709077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23719077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23729077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23739077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23749077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23759077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23769077f387SGleb Smirnoff 				break;
23779077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23789077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23799077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23809077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23819077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23829077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23839077f387SGleb Smirnoff 				break;
23849077f387SGleb Smirnoff 			}
238509fe6320SNavdeep Parhar 			goto unlock_and_done;
23869077f387SGleb Smirnoff 
238785c05144SGleb Smirnoff 		case TCP_KEEPCNT:
238885c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
238985c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
239085c05144SGleb Smirnoff 			if (error)
239185c05144SGleb Smirnoff 				return (error);
239285c05144SGleb Smirnoff 
239385c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
239485c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
239585c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
239685c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
239785c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
239885c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
239985c05144SGleb Smirnoff 			goto unlock_and_done;
240085c05144SGleb Smirnoff 
240186a996e6SHiren Panchasara #ifdef TCPPCAP
240286a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
240386a996e6SHiren Panchasara 		case TCP_PCAP_IN:
240486a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
240586a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
240686a996e6SHiren Panchasara 			    sizeof optval);
240786a996e6SHiren Panchasara 			if (error)
240886a996e6SHiren Panchasara 				return (error);
240986a996e6SHiren Panchasara 
241086a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
241186a996e6SHiren Panchasara 			if (optval >= 0)
241286a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
241386a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
241486a996e6SHiren Panchasara 					optval);
241586a996e6SHiren Panchasara 			else
241686a996e6SHiren Panchasara 				error = EINVAL;
241786a996e6SHiren Panchasara 			goto unlock_and_done;
241886a996e6SHiren Panchasara #endif
241986a996e6SHiren Panchasara 
2420c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2421c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2422c560df6fSPatrick Kelsey 
2423281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2424c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2425c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2426281a0fd4SPatrick Kelsey 				return (EPERM);
2427281a0fd4SPatrick Kelsey 
2428c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2429c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2430281a0fd4SPatrick Kelsey 			if (error)
2431281a0fd4SPatrick Kelsey 				return (error);
2432281a0fd4SPatrick Kelsey 
2433281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2434d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2435d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2436d442a657SMichael Tuexen 				error = EINVAL;
2437d442a657SMichael Tuexen 				goto unlock_and_done;
2438d442a657SMichael Tuexen 			}
2439c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2440c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2441c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2442c560df6fSPatrick Kelsey 						error = EPERM;
2443c560df6fSPatrick Kelsey 						goto unlock_and_done;
2444c560df6fSPatrick Kelsey 					}
2445c560df6fSPatrick Kelsey 
2446c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2447281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2448281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2449c560df6fSPatrick Kelsey 				} else {
2450c560df6fSPatrick Kelsey 					/*
2451c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2452c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2453c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2454c560df6fSPatrick Kelsey 					 * connect.
2455c560df6fSPatrick Kelsey 					 */
2456c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2457c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2458c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2459c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2460c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2461c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2462c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2463c560df6fSPatrick Kelsey 					}
2464c560df6fSPatrick Kelsey 				}
2465d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2466281a0fd4SPatrick Kelsey 			} else
2467281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2468281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2469c560df6fSPatrick Kelsey 		}
2470281a0fd4SPatrick Kelsey 
2471e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24722529f56eSJonathan T. Looney 		case TCP_LOG:
24732529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24742529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24752529f56eSJonathan T. Looney 			    sizeof optval);
24762529f56eSJonathan T. Looney 			if (error)
24772529f56eSJonathan T. Looney 				return (error);
24782529f56eSJonathan T. Looney 
24792529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24802529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24812529f56eSJonathan T. Looney 			goto unlock_and_done;
24822529f56eSJonathan T. Looney 
24832529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24842529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24852529f56eSJonathan T. Looney 			error = EINVAL;
24862529f56eSJonathan T. Looney 			break;
24872529f56eSJonathan T. Looney 
24882529f56eSJonathan T. Looney 		case TCP_LOGID:
24892529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24902529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24912529f56eSJonathan T. Looney 			if (error)
24922529f56eSJonathan T. Looney 				break;
24932529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24942529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24952529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24962529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24972529f56eSJonathan T. Looney 			break;
24982529f56eSJonathan T. Looney 
24992529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
25002529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
25012529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25022529f56eSJonathan T. Looney 			error =
25032529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
25042529f56eSJonathan T. Looney 			if (error)
25052529f56eSJonathan T. Looney 				break;
25062529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
25072529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
25082529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
25092529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
25102529f56eSJonathan T. Looney 				    M_WAITOK, true);
25112529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
25122529f56eSJonathan T. Looney 			} else {
25132529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
25142529f56eSJonathan T. Looney 				/*
25152529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
25162529f56eSJonathan T. Looney 				 * INP lock.
25172529f56eSJonathan T. Looney 				 */
25182529f56eSJonathan T. Looney 			}
25192529f56eSJonathan T. Looney 			break;
2520e24e5683SJonathan T. Looney #endif
25212529f56eSJonathan T. Looney 
2522df8bae1dSRodney W. Grimes 		default:
25238501a69cSRobert Watson 			INP_WUNLOCK(inp);
2524df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2525df8bae1dSRodney W. Grimes 			break;
2526df8bae1dSRodney W. Grimes 		}
2527df8bae1dSRodney W. Grimes 		break;
2528df8bae1dSRodney W. Grimes 
2529cfe8b629SGarrett Wollman 	case SOPT_GET:
25301e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2531cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2532fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
253388f6b043SBruce M Simpson 		case TCP_MD5SIG:
25348501a69cSRobert Watson 			INP_WUNLOCK(inp);
253597453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2536fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2537fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
25381cfd4b53SBruce M Simpson 			break;
2539265ed012SBruce M Simpson #endif
25401e8f5ffaSRobert Watson 
2541df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2542cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
25438501a69cSRobert Watson 			INP_WUNLOCK(inp);
2544b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2545df8bae1dSRodney W. Grimes 			break;
2546df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2547cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
25488501a69cSRobert Watson 			INP_WUNLOCK(inp);
2549b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2550df8bae1dSRodney W. Grimes 			break;
25519e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
25529e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
25539e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
25549e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25559e644c23SMichael Tuexen 			break;
2556a0292f23SGarrett Wollman 		case TCP_NOOPT:
2557cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
25588501a69cSRobert Watson 			INP_WUNLOCK(inp);
2559b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2560a0292f23SGarrett Wollman 			break;
2561a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2562cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
25638501a69cSRobert Watson 			INP_WUNLOCK(inp);
2564b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2565b8af5dfaSRobert Watson 			break;
2566b8af5dfaSRobert Watson 		case TCP_INFO:
2567b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
25688501a69cSRobert Watson 			INP_WUNLOCK(inp);
2569b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2570a0292f23SGarrett Wollman 			break;
2571adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2572adc56f5aSEdward Tomasz Napierala 			{
2573adc56f5aSEdward Tomasz Napierala #ifdef STATS
2574adc56f5aSEdward Tomasz Napierala 			int nheld;
2575adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2576adc56f5aSEdward Tomasz Napierala 
2577adc56f5aSEdward Tomasz Napierala 			error = 0;
2578adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2579adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2580adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2581adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2582adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2583adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2584adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2585adc56f5aSEdward Tomasz Napierala 			else
2586adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2587adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2588adc56f5aSEdward Tomasz Napierala 			if (error)
2589adc56f5aSEdward Tomasz Napierala 				break;
2590adc56f5aSEdward Tomasz Napierala 
2591adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2592adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2593adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2594adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2595adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2596adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2597adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2598adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2599adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2600adc56f5aSEdward Tomasz Napierala 				break;
2601adc56f5aSEdward Tomasz Napierala 			}
2602adc56f5aSEdward Tomasz Napierala 
2603adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2604adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2605adc56f5aSEdward Tomasz Napierala 				goto unhold;
2606adc56f5aSEdward Tomasz Napierala 
2607adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2608adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2609adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2610adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2611adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2612adc56f5aSEdward Tomasz Napierala unhold:
2613adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2614adc56f5aSEdward Tomasz Napierala #else
2615adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2616adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2617adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2618adc56f5aSEdward Tomasz Napierala 			break;
2619adc56f5aSEdward Tomasz Napierala 			}
2620dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2621af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2622dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2623af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2624dbc42409SLawrence Stewart 			break;
262508af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
26262f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
26272f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
26282f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
26292f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
26302f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
263108af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
263208af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
263308af8aacSRandall Stewart 				break;
26342f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
26355a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
26362f3eb7f4SGleb Smirnoff 				break;
26372f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
26385a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
26392f3eb7f4SGleb Smirnoff 				break;
26402f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
26415a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
26422f3eb7f4SGleb Smirnoff 				break;
26432f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
26445a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
26452f3eb7f4SGleb Smirnoff 				break;
26462f3eb7f4SGleb Smirnoff 			}
26472f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
26482f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
26492f3eb7f4SGleb Smirnoff 			break;
265086a996e6SHiren Panchasara #ifdef TCPPCAP
265186a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
265286a996e6SHiren Panchasara 		case TCP_PCAP_IN:
265386a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
265486a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
265586a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
265686a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
265786a996e6SHiren Panchasara 			break;
265886a996e6SHiren Panchasara #endif
2659281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2660281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2661281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2662281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2663281a0fd4SPatrick Kelsey 			break;
2664e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
26652529f56eSJonathan T. Looney 		case TCP_LOG:
26662529f56eSJonathan T. Looney 			optval = tp->t_logstate;
26672529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26682529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
26692529f56eSJonathan T. Looney 			break;
26702529f56eSJonathan T. Looney 		case TCP_LOGBUF:
26712529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
26722529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26732529f56eSJonathan T. Looney 			break;
26742529f56eSJonathan T. Looney 		case TCP_LOGID:
26752529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26762529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26772529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26782529f56eSJonathan T. Looney 			break;
26792529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26802529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26812529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26822529f56eSJonathan T. Looney 			error = EINVAL;
26832529f56eSJonathan T. Looney 			break;
2684e24e5683SJonathan T. Looney #endif
2685b2e60773SJohn Baldwin #ifdef KERN_TLS
2686b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2687fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2688b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2689bf256782SMark Johnston 			if (error == 0)
2690bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2691bf256782SMark Johnston 				    sizeof(optval));
2692b2e60773SJohn Baldwin 			break;
2693f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2694fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2695f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2696bf256782SMark Johnston 			if (error == 0)
2697bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2698bf256782SMark Johnston 				    sizeof(optval));
2699f1f93475SJohn Baldwin 			break;
2700b2e60773SJohn Baldwin #endif
27010471a8c7SRichard Scheffenegger 		case TCP_LRD:
27020471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
27030471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
27040471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
27050471a8c7SRichard Scheffenegger 			break;
2706df8bae1dSRodney W. Grimes 		default:
27078501a69cSRobert Watson 			INP_WUNLOCK(inp);
2708df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2709df8bae1dSRodney W. Grimes 			break;
2710df8bae1dSRodney W. Grimes 		}
2711df8bae1dSRodney W. Grimes 		break;
2712df8bae1dSRodney W. Grimes 	}
2713df8bae1dSRodney W. Grimes 	return (error);
2714df8bae1dSRodney W. Grimes }
27158501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2716bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2717df8bae1dSRodney W. Grimes 
271826e30fbbSDavid Greenman /*
2719df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2720df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2721df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2722df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2723df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2724df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2725df8bae1dSRodney W. Grimes  */
2726623dce13SRobert Watson static void
2727ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2728df8bae1dSRodney W. Grimes {
27299eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
27309eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2731e6e0b5ffSRobert Watson 
273297a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27338501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2734df8bae1dSRodney W. Grimes 
2735623dce13SRobert Watson 	/*
2736623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2737623dce13SRobert Watson 	 * socket is still open.
2738623dce13SRobert Watson 	 */
27398db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
27408db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2741df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2742623dce13SRobert Watson 		KASSERT(tp != NULL,
2743623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2744623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2745243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2746623dce13SRobert Watson 		KASSERT(tp != NULL,
2747623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2748623dce13SRobert Watson 	} else {
2749df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2750df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2751623dce13SRobert Watson 		tcp_usrclosed(tp);
2752ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2753f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2754f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2755df8bae1dSRodney W. Grimes 	}
2756df8bae1dSRodney W. Grimes }
2757df8bae1dSRodney W. Grimes 
2758df8bae1dSRodney W. Grimes /*
2759df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2760df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2761df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2762df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2763df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2764df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2765df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2766df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2767df8bae1dSRodney W. Grimes  */
2768623dce13SRobert Watson static void
2769ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2770df8bae1dSRodney W. Grimes {
2771df8bae1dSRodney W. Grimes 
277297a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27739eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2774e6e0b5ffSRobert Watson 
2775df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2776df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
277709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
277809fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
277909fe6320SNavdeep Parhar #endif
2780550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2781bc65987aSKip Macy 		/* FALLTHROUGH */
2782bc65987aSKip Macy 	case TCPS_CLOSED:
2783df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2784623dce13SRobert Watson 		/*
2785623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2786623dce13SRobert Watson 		 * still open.
2787623dce13SRobert Watson 		 */
2788623dce13SRobert Watson 		KASSERT(tp != NULL,
2789623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2790df8bae1dSRodney W. Grimes 		break;
2791df8bae1dSRodney W. Grimes 
2792a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2793df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2794a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2795a0292f23SGarrett Wollman 		break;
2796a0292f23SGarrett Wollman 
2797df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
279857f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2799df8bae1dSRodney W. Grimes 		break;
2800df8bae1dSRodney W. Grimes 
2801df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
280257f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2803df8bae1dSRodney W. Grimes 		break;
2804df8bae1dSRodney W. Grimes 	}
280508af8aacSRandall Stewart 	if (tp->t_acktime == 0)
280608af8aacSRandall Stewart 		tp->t_acktime = ticks;
2807abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
28089eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2809abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
28107c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
28117c72af87SMohan Srinivasan 			int timeout;
28127c72af87SMohan Srinivasan 
28137c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
28149077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2815b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2816b6239c4aSAndras Olah 		}
2817df8bae1dSRodney W. Grimes 	}
28187c72af87SMohan Srinivasan }
2819497057eeSRobert Watson 
2820497057eeSRobert Watson #ifdef DDB
2821497057eeSRobert Watson static void
2822497057eeSRobert Watson db_print_indent(int indent)
2823497057eeSRobert Watson {
2824497057eeSRobert Watson 	int i;
2825497057eeSRobert Watson 
2826497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2827497057eeSRobert Watson 		db_printf(" ");
2828497057eeSRobert Watson }
2829497057eeSRobert Watson 
2830497057eeSRobert Watson static void
2831497057eeSRobert Watson db_print_tstate(int t_state)
2832497057eeSRobert Watson {
2833497057eeSRobert Watson 
2834497057eeSRobert Watson 	switch (t_state) {
2835497057eeSRobert Watson 	case TCPS_CLOSED:
2836497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2837497057eeSRobert Watson 		return;
2838497057eeSRobert Watson 
2839497057eeSRobert Watson 	case TCPS_LISTEN:
2840497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2841497057eeSRobert Watson 		return;
2842497057eeSRobert Watson 
2843497057eeSRobert Watson 	case TCPS_SYN_SENT:
2844497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2845497057eeSRobert Watson 		return;
2846497057eeSRobert Watson 
2847497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2848497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2849497057eeSRobert Watson 		return;
2850497057eeSRobert Watson 
2851497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2852497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2853497057eeSRobert Watson 		return;
2854497057eeSRobert Watson 
2855497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2856497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2857497057eeSRobert Watson 		return;
2858497057eeSRobert Watson 
2859497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2860497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2861497057eeSRobert Watson 		return;
2862497057eeSRobert Watson 
2863497057eeSRobert Watson 	case TCPS_CLOSING:
2864497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2865497057eeSRobert Watson 		return;
2866497057eeSRobert Watson 
2867497057eeSRobert Watson 	case TCPS_LAST_ACK:
2868497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2869497057eeSRobert Watson 		return;
2870497057eeSRobert Watson 
2871497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2872497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2873497057eeSRobert Watson 		return;
2874497057eeSRobert Watson 
2875497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2876497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2877497057eeSRobert Watson 		return;
2878497057eeSRobert Watson 
2879497057eeSRobert Watson 	default:
2880497057eeSRobert Watson 		db_printf("unknown");
2881497057eeSRobert Watson 		return;
2882497057eeSRobert Watson 	}
2883497057eeSRobert Watson }
2884497057eeSRobert Watson 
2885497057eeSRobert Watson static void
2886497057eeSRobert Watson db_print_tflags(u_int t_flags)
2887497057eeSRobert Watson {
2888497057eeSRobert Watson 	int comma;
2889497057eeSRobert Watson 
2890497057eeSRobert Watson 	comma = 0;
2891497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2892497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2893497057eeSRobert Watson 		comma = 1;
2894497057eeSRobert Watson 	}
2895497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2896497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2897497057eeSRobert Watson 		comma = 1;
2898497057eeSRobert Watson 	}
2899497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2900497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2901497057eeSRobert Watson 		comma = 1;
2902497057eeSRobert Watson 	}
2903497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2904497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2905497057eeSRobert Watson 		comma = 1;
2906497057eeSRobert Watson 	}
2907497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2908497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2909497057eeSRobert Watson 		comma = 1;
2910497057eeSRobert Watson 	}
2911497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2912497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2913497057eeSRobert Watson 		comma = 1;
2914497057eeSRobert Watson 	}
2915497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2916497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2917497057eeSRobert Watson 		comma = 1;
2918497057eeSRobert Watson 	}
2919497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2920497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2921497057eeSRobert Watson 		comma = 1;
2922497057eeSRobert Watson 	}
2923497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2924497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2925497057eeSRobert Watson 		comma = 1;
2926497057eeSRobert Watson 	}
2927497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2928497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2929497057eeSRobert Watson 		comma = 1;
2930497057eeSRobert Watson 	}
2931497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2932497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2933497057eeSRobert Watson 		comma = 1;
2934497057eeSRobert Watson 	}
2935497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2936497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2937497057eeSRobert Watson 		comma = 1;
2938497057eeSRobert Watson 	}
2939497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2940497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2941497057eeSRobert Watson 		comma = 1;
2942497057eeSRobert Watson 	}
29433f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
29443f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
29453f169c54SRichard Scheffenegger 		comma = 1;
29463f169c54SRichard Scheffenegger 	}
2947497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2948497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2949497057eeSRobert Watson 		comma = 1;
2950497057eeSRobert Watson 	}
2951493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2952493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2953497057eeSRobert Watson 		comma = 1;
2954497057eeSRobert Watson 	}
2955497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2956497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2957497057eeSRobert Watson 		comma = 1;
2958497057eeSRobert Watson 	}
2959497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2960497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2961497057eeSRobert Watson 		comma = 1;
2962497057eeSRobert Watson 	}
2963497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2964497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2965497057eeSRobert Watson 		comma = 1;
2966497057eeSRobert Watson 	}
2967dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2968dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2969dbc42409SLawrence Stewart 		comma = 1;
2970dbc42409SLawrence Stewart 	}
2971497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2972497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2973497057eeSRobert Watson 		comma = 1;
2974497057eeSRobert Watson 	}
29753f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
29763f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
29773f169c54SRichard Scheffenegger 		comma = 1;
29783f169c54SRichard Scheffenegger 	}
2979497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2980497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2981497057eeSRobert Watson 		comma = 1;
2982497057eeSRobert Watson 	}
2983497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2984497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2985497057eeSRobert Watson 		comma = 1;
2986497057eeSRobert Watson 	}
2987497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2988497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2989497057eeSRobert Watson 		comma = 1;
2990497057eeSRobert Watson 	}
2991281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2992281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2993281a0fd4SPatrick Kelsey 		comma = 1;
2994281a0fd4SPatrick Kelsey 	}
2995497057eeSRobert Watson }
2996497057eeSRobert Watson 
2997497057eeSRobert Watson static void
29983cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29993cf38784SMichael Tuexen {
30003cf38784SMichael Tuexen 	int comma;
30013cf38784SMichael Tuexen 
30023cf38784SMichael Tuexen 	comma = 0;
30033f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
30043f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
30053f169c54SRichard Scheffenegger 		comma = 1;
30063f169c54SRichard Scheffenegger 	}
30073f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
30083f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
30093f169c54SRichard Scheffenegger 		comma = 1;
30103f169c54SRichard Scheffenegger 	}
30113f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
30123f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
30133f169c54SRichard Scheffenegger 		comma = 1;
30143f169c54SRichard Scheffenegger 	}
30153f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
30163f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
30173f169c54SRichard Scheffenegger 		comma = 1;
30183f169c54SRichard Scheffenegger 	}
30193f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
30203f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
30213f169c54SRichard Scheffenegger 		comma = 1;
30223f169c54SRichard Scheffenegger 	}
30233cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
30243cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
30253cf38784SMichael Tuexen 		comma = 1;
30263cf38784SMichael Tuexen 	}
30273f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
30283f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
30293f169c54SRichard Scheffenegger 		comma = 1;
30303f169c54SRichard Scheffenegger 	}
30313f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
30323f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
30333f169c54SRichard Scheffenegger 		comma = 1;
30343f169c54SRichard Scheffenegger 	}
30353f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
30363f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
30373f169c54SRichard Scheffenegger 		comma = 1;
30383f169c54SRichard Scheffenegger 	}
30393f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
30403f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30413f169c54SRichard Scheffenegger 		comma = 1;
30423f169c54SRichard Scheffenegger 	}
30433cf38784SMichael Tuexen }
30443cf38784SMichael Tuexen 
30453cf38784SMichael Tuexen static void
3046497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3047497057eeSRobert Watson {
3048497057eeSRobert Watson 	int comma;
3049497057eeSRobert Watson 
3050497057eeSRobert Watson 	comma = 0;
3051497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3052497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3053497057eeSRobert Watson 		comma = 1;
3054497057eeSRobert Watson 	}
3055497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3056497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3057497057eeSRobert Watson 		comma = 1;
3058497057eeSRobert Watson 	}
3059497057eeSRobert Watson }
3060497057eeSRobert Watson 
3061497057eeSRobert Watson static void
3062497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3063497057eeSRobert Watson {
3064497057eeSRobert Watson 
3065497057eeSRobert Watson 	db_print_indent(indent);
3066497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3067497057eeSRobert Watson 
3068497057eeSRobert Watson 	indent += 2;
3069497057eeSRobert Watson 
3070497057eeSRobert Watson 	db_print_indent(indent);
3071497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3072c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3073497057eeSRobert Watson 
3074497057eeSRobert Watson 	db_print_indent(indent);
307585d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
3076e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
3077497057eeSRobert Watson 
3078497057eeSRobert Watson 	db_print_indent(indent);
3079e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
3080e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
3081497057eeSRobert Watson 
3082497057eeSRobert Watson 	db_print_indent(indent);
3083497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3084497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3085497057eeSRobert Watson 	db_printf(")\n");
3086497057eeSRobert Watson 
3087497057eeSRobert Watson 	db_print_indent(indent);
3088497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3089497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3090497057eeSRobert Watson 	db_printf(")\n");
3091497057eeSRobert Watson 
3092497057eeSRobert Watson 	db_print_indent(indent);
30933cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30943cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30953cf38784SMichael Tuexen 	db_printf(")\n");
30963cf38784SMichael Tuexen 
30973cf38784SMichael Tuexen 	db_print_indent(indent);
3098497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
3099497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3100497057eeSRobert Watson 
3101497057eeSRobert Watson 	db_print_indent(indent);
3102497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3103497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3104497057eeSRobert Watson 
3105497057eeSRobert Watson 	db_print_indent(indent);
3106497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3107497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3108497057eeSRobert Watson 
3109497057eeSRobert Watson 	db_print_indent(indent);
31103ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3111497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3112497057eeSRobert Watson 
3113497057eeSRobert Watson 	db_print_indent(indent);
31143ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
31151c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3116497057eeSRobert Watson 
3117497057eeSRobert Watson 	db_print_indent(indent);
31183ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
31191c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3120497057eeSRobert Watson 
3121497057eeSRobert Watson 	db_print_indent(indent);
31220c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
31230c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3124497057eeSRobert Watson 
3125497057eeSRobert Watson 	db_print_indent(indent);
31261c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
31271c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3128497057eeSRobert Watson 
3129497057eeSRobert Watson 	db_print_indent(indent);
31301c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
31311c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3132497057eeSRobert Watson 
3133497057eeSRobert Watson 	db_print_indent(indent);
3134*bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3135*bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3136497057eeSRobert Watson 
3137497057eeSRobert Watson 	db_print_indent(indent);
31383ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
3139497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3140497057eeSRobert Watson 
3141497057eeSRobert Watson 	db_print_indent(indent);
3142497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3143497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3144497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3145497057eeSRobert Watson 
3146497057eeSRobert Watson 	db_print_indent(indent);
3147497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3148497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3149497057eeSRobert Watson 
3150497057eeSRobert Watson 	db_print_indent(indent);
31519f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31521a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3153497057eeSRobert Watson 
3154497057eeSRobert Watson 	db_print_indent(indent);
3155497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31563ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3157497057eeSRobert Watson 
3158497057eeSRobert Watson 	db_print_indent(indent);
31593ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31609f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3161497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3162497057eeSRobert Watson 
3163497057eeSRobert Watson 	db_print_indent(indent);
31643529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31653529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3166497057eeSRobert Watson 
3167497057eeSRobert Watson 	db_print_indent(indent);
3168a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3169a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3170497057eeSRobert Watson 
3171497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3172497057eeSRobert Watson 
3173497057eeSRobert Watson 	db_print_indent(indent);
3174497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3175497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3176497057eeSRobert Watson }
3177497057eeSRobert Watson 
3178497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3179497057eeSRobert Watson {
3180497057eeSRobert Watson 	struct tcpcb *tp;
3181497057eeSRobert Watson 
3182497057eeSRobert Watson 	if (!have_addr) {
3183497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3184497057eeSRobert Watson 		return;
3185497057eeSRobert Watson 	}
3186497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3187497057eeSRobert Watson 
3188497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3189497057eeSRobert Watson }
3190497057eeSRobert Watson #endif
3191