xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 1b91978f6375023b00c7d2b49a778765ce4ee6b8)
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
106610ee2f9SDavid Greenman #ifdef TCPDEBUG
107df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
108610ee2f9SDavid Greenman #endif
10909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
110bc65987aSKip Macy #include <netinet/tcp_offload.h>
11109fe6320SNavdeep Parhar #endif
112fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
113df8bae1dSRodney W. Grimes 
114adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
115adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
116adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
117adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
118adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
119adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
120adc56f5aSEdward Tomasz Napierala 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
123df8bae1dSRodney W. Grimes  */
124b287c6c7SBjoern A. Zeeb #ifdef INET
1254d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1264d77a549SAlfred Perlstein 		    struct thread *td);
127b287c6c7SBjoern A. Zeeb #endif /* INET */
128fb59c426SYoshinobu Inoue #ifdef INET6
1294d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1304d77a549SAlfred Perlstein 		    struct thread *td);
131fb59c426SYoshinobu Inoue #endif /* INET6 */
132623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
133623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
134b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1352c37256eSGarrett Wollman 
136d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
137d3b6c96bSRandall Stewart 
1382c37256eSGarrett Wollman #ifdef TCPDEBUG
1391db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1402c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1414cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1424cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1432c37256eSGarrett Wollman #else
1442c37256eSGarrett Wollman #define	TCPDEBUG0
1452c37256eSGarrett Wollman #define	TCPDEBUG1()
1462c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1472c37256eSGarrett Wollman #endif
1482c37256eSGarrett Wollman 
1492c37256eSGarrett Wollman /*
15025102351SMike Karels  * tcp_require_unique port requires a globally-unique source port for each
15125102351SMike Karels  * outgoing connection.  The default is to require the 4-tuple to be unique.
15225102351SMike Karels  */
15325102351SMike Karels VNET_DEFINE(int, tcp_require_unique_port) = 0;
15425102351SMike Karels SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
15525102351SMike Karels     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
15625102351SMike Karels     "Require globally-unique ephemeral port for outgoing connections");
15725102351SMike Karels #define	V_tcp_require_unique_port	VNET(tcp_require_unique_port)
15825102351SMike Karels 
15925102351SMike Karels /*
1602c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1612c37256eSGarrett Wollman  * and an internet control block.
1622c37256eSGarrett Wollman  */
1632c37256eSGarrett Wollman static int
164b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1652c37256eSGarrett Wollman {
166f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
167623dce13SRobert Watson 	struct tcpcb *tp = NULL;
168623dce13SRobert Watson 	int error;
1692c37256eSGarrett Wollman 	TCPDEBUG0;
1702c37256eSGarrett Wollman 
171623dce13SRobert Watson 	inp = sotoinpcb(so);
172623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1732c37256eSGarrett Wollman 	TCPDEBUG1();
1742c37256eSGarrett Wollman 
1750f6385e7SGleb Smirnoff 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1760f6385e7SGleb Smirnoff 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1772c37256eSGarrett Wollman 		if (error)
1782c37256eSGarrett Wollman 			goto out;
1790f6385e7SGleb Smirnoff 	}
1802c37256eSGarrett Wollman 
1810f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1820f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1830f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1847669c586SGleb Smirnoff 	if (error)
1850f6385e7SGleb Smirnoff 		goto out;
1860f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1870f6385e7SGleb Smirnoff #ifdef INET6
1880f6385e7SGleb Smirnoff 	if (inp->inp_vflag & INP_IPV6PROTO) {
1890f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV6;
1900f6385e7SGleb Smirnoff 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1910f6385e7SGleb Smirnoff 			inp->inp_vflag |= INP_IPV4;
1920f6385e7SGleb Smirnoff 		inp->in6p_hops = -1;	/* use kernel default */
1930f6385e7SGleb Smirnoff 	}
1940f6385e7SGleb Smirnoff 	else
1950f6385e7SGleb Smirnoff #endif
1960f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV4;
1970f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1980f6385e7SGleb Smirnoff 	if (tp == NULL) {
1997669c586SGleb Smirnoff 		error = ENOBUFS;
2000f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
2010f6385e7SGleb Smirnoff 		in_pcbfree(inp);
2020f6385e7SGleb Smirnoff 		goto out;
2030f6385e7SGleb Smirnoff 	}
2040f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
2050f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
2060f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
2072c37256eSGarrett Wollman out:
2082c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
2095d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
2100f6385e7SGleb Smirnoff 	return (error);
2112c37256eSGarrett Wollman }
2122c37256eSGarrett Wollman 
2132c37256eSGarrett Wollman /*
2143fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
215a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
216a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
217a152f8a3SRobert Watson  * inpcb state: time wait.
2182c37256eSGarrett Wollman  */
219bc725eafSRobert Watson static void
2203fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2212c37256eSGarrett Wollman {
2223fed74e9SGleb Smirnoff 	struct inpcb *inp;
2232c37256eSGarrett Wollman 	struct tcpcb *tp;
2242c37256eSGarrett Wollman 
2253fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2263fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2273fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2283fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2293fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
230953b5606SRobert Watson 
231a152f8a3SRobert Watson 	tp = intotcpcb(inp);
232a152f8a3SRobert Watson 
233ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
234623dce13SRobert Watson 		/*
235a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
236a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
237a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
238a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
239a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
240a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
241a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
242a152f8a3SRobert Watson 		 * present until timewait ends.
243623dce13SRobert Watson 		 *
244a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
245cea40c48SJulien Charbon 		 *
246cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
247dd388cfdSGleb Smirnoff 		 * four cases to consider:
248cea40c48SJulien Charbon 		 *
2493fed74e9SGleb Smirnoff 		 * #1 tcp_usr_detach is called at tcptw creation time by
250cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
251cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
2523fed74e9SGleb Smirnoff 		 * #2 tcp_usr_detach is called at tcptw creation time by
253dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
254dd388cfdSGleb Smirnoff 		 *  discarded immediately
2553fed74e9SGleb Smirnoff 		 * #3 tcp_usr_detach is called at timewait end (or reuse) by
256cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
257ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
2583fed74e9SGleb Smirnoff 		 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
259cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
260ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
261cea40c48SJulien Charbon 		 *
262cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
263623dce13SRobert Watson 		 */
264ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
265ef396441SGleb Smirnoff 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
266ef396441SGleb Smirnoff 			    "INP_DROPPED && tp != NULL"));
267623dce13SRobert Watson 			in_pcbdetach(inp);
2680206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
2690206cdb8SBjoern A. Zeeb 		} else {
270623dce13SRobert Watson 			in_pcbdetach(inp);
2718501a69cSRobert Watson 			INP_WUNLOCK(inp);
272623dce13SRobert Watson 		}
273623dce13SRobert Watson 	} else {
274e6e65783SRobert Watson 		/*
275*1b91978fSGleb Smirnoff 		 * If the connection is not in timewait, it must be either
276*1b91978fSGleb Smirnoff 		 * dropped or embryonic.
277e6e65783SRobert Watson 		 */
278*1b91978fSGleb Smirnoff 		KASSERT(inp->inp_flags & INP_DROPPED ||
279*1b91978fSGleb Smirnoff 		    tp->t_state < TCPS_SYN_SENT,
280*1b91978fSGleb Smirnoff 		    ("%s: inp %p not dropped or embryonic", __func__, inp));
281623dce13SRobert Watson 		tcp_discardcb(tp);
282623dce13SRobert Watson 		in_pcbdetach(inp);
2830206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
284623dce13SRobert Watson 	}
285623dce13SRobert Watson }
286c78cbc7bSRobert Watson 
287b287c6c7SBjoern A. Zeeb #ifdef INET
2882c37256eSGarrett Wollman /*
2892c37256eSGarrett Wollman  * Give the socket an address.
2902c37256eSGarrett Wollman  */
2912c37256eSGarrett Wollman static int
292b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2932c37256eSGarrett Wollman {
2942c37256eSGarrett Wollman 	int error = 0;
295f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
296b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
297623dce13SRobert Watson 	struct tcpcb *tp = NULL;
298b338b1fdSMateusz Guzik #endif
2992c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3002c37256eSGarrett Wollman 
30152710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
302f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
303f96603b5SMark Johnston 		/*
304f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
305f96603b5SMark Johnston 		 */
306f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
3073f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
308f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
309f161d294SMark Johnston 			return (EAFNOSUPPORT);
310f96603b5SMark Johnston 		nam->sa_family = AF_INET;
311f96603b5SMark Johnston 	}
31252710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof(*sinp))
31352710de1SPawel Jakub Dawidek 		return (EINVAL);
314f161d294SMark Johnston 
3152c37256eSGarrett Wollman 	/*
3162c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3172c37256eSGarrett Wollman 	 * to them.
3182c37256eSGarrett Wollman 	 */
319f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
32052710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
32152710de1SPawel Jakub Dawidek 
322623dce13SRobert Watson 	TCPDEBUG0;
323623dce13SRobert Watson 	inp = sotoinpcb(so);
324623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3258501a69cSRobert Watson 	INP_WLOCK(inp);
326ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
327623dce13SRobert Watson 		error = EINVAL;
3282c37256eSGarrett Wollman 		goto out;
329623dce13SRobert Watson 	}
330b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
331623dce13SRobert Watson 	tp = intotcpcb(inp);
332b338b1fdSMateusz Guzik #endif
333623dce13SRobert Watson 	TCPDEBUG1();
334fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
335623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
336fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
337623dce13SRobert Watson out:
338623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3395d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3408501a69cSRobert Watson 	INP_WUNLOCK(inp);
341623dce13SRobert Watson 
342623dce13SRobert Watson 	return (error);
3432c37256eSGarrett Wollman }
344b287c6c7SBjoern A. Zeeb #endif /* INET */
3452c37256eSGarrett Wollman 
346fb59c426SYoshinobu Inoue #ifdef INET6
347fb59c426SYoshinobu Inoue static int
348b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
349fb59c426SYoshinobu Inoue {
350fb59c426SYoshinobu Inoue 	int error = 0;
351f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
352b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
353623dce13SRobert Watson 	struct tcpcb *tp = NULL;
354b338b1fdSMateusz Guzik #endif
3550ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
3564a91aa8fSMichael Tuexen 	u_char vflagsav;
357fb59c426SYoshinobu Inoue 
3580ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
359f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
360f161d294SMark Johnston 		return (EAFNOSUPPORT);
3610ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof(*sin6))
36252710de1SPawel Jakub Dawidek 		return (EINVAL);
363f161d294SMark Johnston 
364fb59c426SYoshinobu Inoue 	/*
365fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
366fb59c426SYoshinobu Inoue 	 * to them.
367fb59c426SYoshinobu Inoue 	 */
368f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
36952710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
37052710de1SPawel Jakub Dawidek 
371623dce13SRobert Watson 	TCPDEBUG0;
372623dce13SRobert Watson 	inp = sotoinpcb(so);
373623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3748501a69cSRobert Watson 	INP_WLOCK(inp);
3754a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
376ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
377623dce13SRobert Watson 		error = EINVAL;
378623dce13SRobert Watson 		goto out;
379623dce13SRobert Watson 	}
380b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
381623dce13SRobert Watson 	tp = intotcpcb(inp);
382b338b1fdSMateusz Guzik #endif
383623dce13SRobert Watson 	TCPDEBUG1();
384fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
385fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
386fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
387b287c6c7SBjoern A. Zeeb #ifdef INET
38866ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3890ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
390fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3910ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
392fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
393fb59c426SYoshinobu Inoue 
3940ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
395888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
396888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
397888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
398888973f5SMichael Tuexen 				goto out;
399888973f5SMichael Tuexen 			}
400fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
401fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
402b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
403b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
404fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
405fb59c426SYoshinobu Inoue 			goto out;
406fb59c426SYoshinobu Inoue 		}
407fb59c426SYoshinobu Inoue 	}
408b287c6c7SBjoern A. Zeeb #endif
409b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
410fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
411623dce13SRobert Watson out:
4124a91aa8fSMichael Tuexen 	if (error != 0)
4134a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
414623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
4155d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4168501a69cSRobert Watson 	INP_WUNLOCK(inp);
417623dce13SRobert Watson 	return (error);
418fb59c426SYoshinobu Inoue }
419fb59c426SYoshinobu Inoue #endif /* INET6 */
420fb59c426SYoshinobu Inoue 
421b287c6c7SBjoern A. Zeeb #ifdef INET
4222c37256eSGarrett Wollman /*
4232c37256eSGarrett Wollman  * Prepare to accept connections.
4242c37256eSGarrett Wollman  */
4252c37256eSGarrett Wollman static int
426d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4272c37256eSGarrett Wollman {
4282c37256eSGarrett Wollman 	int error = 0;
429f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
430623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4312c37256eSGarrett Wollman 
432623dce13SRobert Watson 	TCPDEBUG0;
433623dce13SRobert Watson 	inp = sotoinpcb(so);
434623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4358501a69cSRobert Watson 	INP_WLOCK(inp);
436ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
437623dce13SRobert Watson 		error = EINVAL;
438623dce13SRobert Watson 		goto out;
439623dce13SRobert Watson 	}
440623dce13SRobert Watson 	tp = intotcpcb(inp);
441623dce13SRobert Watson 	TCPDEBUG1();
4420daccb9cSRobert Watson 	SOCK_LOCK(so);
4430daccb9cSRobert Watson 	error = solisten_proto_check(so);
444bd4a39ccSMark Johnston 	if (error != 0) {
445bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
446bd4a39ccSMark Johnston 		goto out;
447bd4a39ccSMark Johnston 	}
448bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
449fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
450bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
451fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
452bd4a39ccSMark Johnston 	}
4530daccb9cSRobert Watson 	if (error == 0) {
45457f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
455d374e81eSRobert Watson 		solisten_proto(so, backlog);
45609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
45737cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45809fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45909fe6320SNavdeep Parhar #endif
460bd4a39ccSMark Johnston 	} else {
461bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4620daccb9cSRobert Watson 	}
4630daccb9cSRobert Watson 	SOCK_UNLOCK(so);
464623dce13SRobert Watson 
46568bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
466281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
46718a75309SPatrick Kelsey 
468623dce13SRobert Watson out:
469623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4705d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4718501a69cSRobert Watson 	INP_WUNLOCK(inp);
472623dce13SRobert Watson 	return (error);
4732c37256eSGarrett Wollman }
474b287c6c7SBjoern A. Zeeb #endif /* INET */
4752c37256eSGarrett Wollman 
476fb59c426SYoshinobu Inoue #ifdef INET6
477fb59c426SYoshinobu Inoue static int
478d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
479fb59c426SYoshinobu Inoue {
480fb59c426SYoshinobu Inoue 	int error = 0;
481f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
482623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4834a91aa8fSMichael Tuexen 	u_char vflagsav;
484fb59c426SYoshinobu Inoue 
485623dce13SRobert Watson 	TCPDEBUG0;
486623dce13SRobert Watson 	inp = sotoinpcb(so);
487623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4888501a69cSRobert Watson 	INP_WLOCK(inp);
489ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
490623dce13SRobert Watson 		error = EINVAL;
491623dce13SRobert Watson 		goto out;
492623dce13SRobert Watson 	}
4934a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
494623dce13SRobert Watson 	tp = intotcpcb(inp);
495623dce13SRobert Watson 	TCPDEBUG1();
4960daccb9cSRobert Watson 	SOCK_LOCK(so);
4970daccb9cSRobert Watson 	error = solisten_proto_check(so);
498bd4a39ccSMark Johnston 	if (error != 0) {
499bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
500bd4a39ccSMark Johnston 		goto out;
501bd4a39ccSMark Johnston 	}
502fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
503bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
504fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
50566ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
506fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
507bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
508fb59c426SYoshinobu Inoue 	}
509fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
5100daccb9cSRobert Watson 	if (error == 0) {
51157f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
512d374e81eSRobert Watson 		solisten_proto(so, backlog);
51309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
51437cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
51509fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
51609fe6320SNavdeep Parhar #endif
517bd4a39ccSMark Johnston 	} else {
518bd4a39ccSMark Johnston 		solisten_proto_abort(so);
5190daccb9cSRobert Watson 	}
5200daccb9cSRobert Watson 	SOCK_UNLOCK(so);
521623dce13SRobert Watson 
52268bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
523281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
52418a75309SPatrick Kelsey 
5254a91aa8fSMichael Tuexen 	if (error != 0)
5264a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
5274a91aa8fSMichael Tuexen 
528623dce13SRobert Watson out:
529623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
5305d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
5318501a69cSRobert Watson 	INP_WUNLOCK(inp);
532623dce13SRobert Watson 	return (error);
533fb59c426SYoshinobu Inoue }
534fb59c426SYoshinobu Inoue #endif /* INET6 */
535fb59c426SYoshinobu Inoue 
536b287c6c7SBjoern A. Zeeb #ifdef INET
5372c37256eSGarrett Wollman /*
5382c37256eSGarrett Wollman  * Initiate connection to peer.
5392c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5402c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5412c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5422c37256eSGarrett Wollman  * Send initial segment on connection.
5432c37256eSGarrett Wollman  */
5442c37256eSGarrett Wollman static int
545b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5462c37256eSGarrett Wollman {
547109eb549SGleb Smirnoff 	struct epoch_tracker et;
5482c37256eSGarrett Wollman 	int error = 0;
549f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
550623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5512c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5522c37256eSGarrett Wollman 
55357bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
554f161d294SMark Johnston 	if (nam->sa_family != AF_INET)
555f161d294SMark Johnston 		return (EAFNOSUPPORT);
556e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
557e29ef13fSDon Lewis 		return (EINVAL);
558f161d294SMark Johnston 
55952710de1SPawel Jakub Dawidek 	/*
56052710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
56152710de1SPawel Jakub Dawidek 	 */
562f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
56352710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
564f161d294SMark Johnston 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
565f903a308SMichael Tuexen 		return (EACCES);
566b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
567b89e82ddSJamie Gritton 		return (error);
56875c13541SPoul-Henning Kamp 
569623dce13SRobert Watson 	TCPDEBUG0;
570623dce13SRobert Watson 	inp = sotoinpcb(so);
571623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5728501a69cSRobert Watson 	INP_WLOCK(inp);
573eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
574eb96dc33SJulien Charbon 		error = EADDRINUSE;
575eb96dc33SJulien Charbon 		goto out;
576eb96dc33SJulien Charbon 	}
577eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
578eb96dc33SJulien Charbon 		error = ECONNREFUSED;
579623dce13SRobert Watson 		goto out;
580623dce13SRobert Watson 	}
581bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
582bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
583bd4a39ccSMark Johnston 		goto out;
584bd4a39ccSMark Johnston 	}
585623dce13SRobert Watson 	tp = intotcpcb(inp);
586623dce13SRobert Watson 	TCPDEBUG1();
587c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
588b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
589c1604fe4SGleb Smirnoff 		goto out_in_epoch;
59009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
59109fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
59237cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
59309fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
594c1604fe4SGleb Smirnoff 		goto out_in_epoch;
59509fe6320SNavdeep Parhar #endif
59609fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
59740fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5981d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5991d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
600c1604fe4SGleb Smirnoff out_in_epoch:
601109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
602623dce13SRobert Watson out:
603623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
604e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6058501a69cSRobert Watson 	INP_WUNLOCK(inp);
606623dce13SRobert Watson 	return (error);
6072c37256eSGarrett Wollman }
608b287c6c7SBjoern A. Zeeb #endif /* INET */
6092c37256eSGarrett Wollman 
610fb59c426SYoshinobu Inoue #ifdef INET6
611fb59c426SYoshinobu Inoue static int
612b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
613fb59c426SYoshinobu Inoue {
614109eb549SGleb Smirnoff 	struct epoch_tracker et;
615fb59c426SYoshinobu Inoue 	int error = 0;
616f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
617623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6180ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
6194a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
6204a91aa8fSMichael Tuexen 	u_char vflagsav;
621623dce13SRobert Watson 
622623dce13SRobert Watson 	TCPDEBUG0;
623fb59c426SYoshinobu Inoue 
6240ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
625f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
626f161d294SMark Johnston 		return (EAFNOSUPPORT);
6270ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
628e29ef13fSDon Lewis 		return (EINVAL);
629f161d294SMark Johnston 
63052710de1SPawel Jakub Dawidek 	/*
63152710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
63252710de1SPawel Jakub Dawidek 	 */
633f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
63452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
635fb59c426SYoshinobu Inoue 
636623dce13SRobert Watson 	inp = sotoinpcb(so);
637623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6388501a69cSRobert Watson 	INP_WLOCK(inp);
6394a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6404a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
641eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
642eb96dc33SJulien Charbon 		error = EADDRINUSE;
643eb96dc33SJulien Charbon 		goto out;
644eb96dc33SJulien Charbon 	}
645eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
646eb96dc33SJulien Charbon 		error = ECONNREFUSED;
647623dce13SRobert Watson 		goto out;
648623dce13SRobert Watson 	}
649bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
650bd4a39ccSMark Johnston 		error = EINVAL;
651bd4a39ccSMark Johnston 		goto out;
652bd4a39ccSMark Johnston 	}
653623dce13SRobert Watson 	tp = intotcpcb(inp);
654623dce13SRobert Watson 	TCPDEBUG1();
655b287c6c7SBjoern A. Zeeb #ifdef INET
656fa046d87SRobert Watson 	/*
657fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
658fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
659fa046d87SRobert Watson 	 * Is this a significant problem?
660fa046d87SRobert Watson 	 */
6610ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
662fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
663fb59c426SYoshinobu Inoue 
664d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
665d46a5312SMaxim Konovalov 			error = EINVAL;
666d46a5312SMaxim Konovalov 			goto out;
667d46a5312SMaxim Konovalov 		}
6685dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6695dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6705dba6adaSMichael Tuexen 			goto out;
6715dba6adaSMichael Tuexen 		}
67233841545SHajimu UMEMOTO 
6730ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
674888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
675888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
676888973f5SMichael Tuexen 			goto out;
677888973f5SMichael Tuexen 		}
678f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
679f903a308SMichael Tuexen 			error = EACCES;
6802cf21ae5SRandall Stewart 			goto out;
6812cf21ae5SRandall Stewart 		}
682b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
683b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
684413628a7SBjoern A. Zeeb 			goto out;
6854a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6864a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
687c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
688b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
689c1604fe4SGleb Smirnoff 			goto out_in_epoch;
69009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
69109fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
692adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
69309fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
694c1604fe4SGleb Smirnoff 			goto out_in_epoch;
69509fe6320SNavdeep Parhar #endif
69640fa3e40SGleb Smirnoff 		error = tcp_output(tp);
697c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6985dba6adaSMichael Tuexen 	} else {
6995dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
7005dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
7015dba6adaSMichael Tuexen 			goto out;
7025dba6adaSMichael Tuexen 		}
703fb59c426SYoshinobu Inoue 	}
704b287c6c7SBjoern A. Zeeb #endif
7054a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
7064a91aa8fSMichael Tuexen 		goto out;
707fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
708fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
709dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
710b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
711fb59c426SYoshinobu Inoue 		goto out;
71209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
71309fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
714adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
71509fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
71609fe6320SNavdeep Parhar 		goto out;
71709fe6320SNavdeep Parhar #endif
71809fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
719109eb549SGleb Smirnoff 	NET_EPOCH_ENTER(et);
72040fa3e40SGleb Smirnoff 	error = tcp_output(tp);
7217754e281SBjoern A. Zeeb #ifdef INET
722c1604fe4SGleb Smirnoff out_in_epoch:
7237754e281SBjoern A. Zeeb #endif
724109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
725623dce13SRobert Watson out:
7261d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
7271d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
7284a91aa8fSMichael Tuexen 	/*
7294a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
7304a91aa8fSMichael Tuexen 	 * the flags we modified.
7314a91aa8fSMichael Tuexen 	 */
7324a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
7334a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
7344a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
7354a91aa8fSMichael Tuexen 	}
7364a91aa8fSMichael Tuexen 
737623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
7385d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
7398501a69cSRobert Watson 	INP_WUNLOCK(inp);
740623dce13SRobert Watson 	return (error);
741fb59c426SYoshinobu Inoue }
742fb59c426SYoshinobu Inoue #endif /* INET6 */
743fb59c426SYoshinobu Inoue 
7442c37256eSGarrett Wollman /*
7452c37256eSGarrett Wollman  * Initiate disconnect from peer.
7462c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7472c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7482c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7492c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7502c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7512c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7522c37256eSGarrett Wollman  *
7532c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7542c37256eSGarrett Wollman  */
7552c37256eSGarrett Wollman static int
7562c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7572c37256eSGarrett Wollman {
758f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
759623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7606573d758SMatt Macy 	struct epoch_tracker et;
761623dce13SRobert Watson 	int error = 0;
7622c37256eSGarrett Wollman 
763623dce13SRobert Watson 	TCPDEBUG0;
76497a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
765623dce13SRobert Watson 	inp = sotoinpcb(so);
766623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7678501a69cSRobert Watson 	INP_WLOCK(inp);
768489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
769489dcc92SJulien Charbon 		goto out;
770489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
77121367f63SSam Leffler 		error = ECONNRESET;
772623dce13SRobert Watson 		goto out;
773623dce13SRobert Watson 	}
774623dce13SRobert Watson 	tp = intotcpcb(inp);
775623dce13SRobert Watson 	TCPDEBUG1();
776623dce13SRobert Watson 	tcp_disconnect(tp);
777623dce13SRobert Watson out:
778623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7795d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7808501a69cSRobert Watson 	INP_WUNLOCK(inp);
78197a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
782623dce13SRobert Watson 	return (error);
7832c37256eSGarrett Wollman }
7842c37256eSGarrett Wollman 
785b287c6c7SBjoern A. Zeeb #ifdef INET
7862c37256eSGarrett Wollman /*
7878296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7888296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7892c37256eSGarrett Wollman  */
7902c37256eSGarrett Wollman static int
79157bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7922c37256eSGarrett Wollman {
7932c37256eSGarrett Wollman 	int error = 0;
794f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
795b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7961db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
797b338b1fdSMateusz Guzik #endif
79826ef6ac4SDon Lewis 	struct in_addr addr;
79926ef6ac4SDon Lewis 	in_port_t port = 0;
8001db24ffbSJonathan Lemon 	TCPDEBUG0;
8012c37256eSGarrett Wollman 
8023d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
8033d2d3ef4SRobert Watson 		return (ECONNABORTED);
804f76fcf6dSJeffrey Hsu 
805f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
806623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
8078501a69cSRobert Watson 	INP_WLOCK(inp);
808ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
8093d2d3ef4SRobert Watson 		error = ECONNABORTED;
810623dce13SRobert Watson 		goto out;
811623dce13SRobert Watson 	}
812b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
8131db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
814b338b1fdSMateusz Guzik #endif
8151db24ffbSJonathan Lemon 	TCPDEBUG1();
816f76fcf6dSJeffrey Hsu 
817f76fcf6dSJeffrey Hsu 	/*
81854d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
81926ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
82026ef6ac4SDon Lewis 	 * release the lock.
821f76fcf6dSJeffrey Hsu 	 */
82226ef6ac4SDon Lewis 	port = inp->inp_fport;
82326ef6ac4SDon Lewis 	addr = inp->inp_faddr;
824f76fcf6dSJeffrey Hsu 
825623dce13SRobert Watson out:
826623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8275d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8288501a69cSRobert Watson 	INP_WUNLOCK(inp);
82926ef6ac4SDon Lewis 	if (error == 0)
83026ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
83126ef6ac4SDon Lewis 	return error;
8322c37256eSGarrett Wollman }
833b287c6c7SBjoern A. Zeeb #endif /* INET */
8342c37256eSGarrett Wollman 
835fb59c426SYoshinobu Inoue #ifdef INET6
836fb59c426SYoshinobu Inoue static int
837fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
838fb59c426SYoshinobu Inoue {
839f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
840fb59c426SYoshinobu Inoue 	int error = 0;
841b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
8421db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
843b338b1fdSMateusz Guzik #endif
84426ef6ac4SDon Lewis 	struct in_addr addr;
84526ef6ac4SDon Lewis 	struct in6_addr addr6;
8466573d758SMatt Macy 	struct epoch_tracker et;
84726ef6ac4SDon Lewis 	in_port_t port = 0;
84826ef6ac4SDon Lewis 	int v4 = 0;
8491db24ffbSJonathan Lemon 	TCPDEBUG0;
850fb59c426SYoshinobu Inoue 
851b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
852b4470c16SRobert Watson 		return (ECONNABORTED);
853f76fcf6dSJeffrey Hsu 
854f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
855623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
85697a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8578501a69cSRobert Watson 	INP_WLOCK(inp);
858ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
85921367f63SSam Leffler 		error = ECONNABORTED;
860623dce13SRobert Watson 		goto out;
861623dce13SRobert Watson 	}
862b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
8631db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
864b338b1fdSMateusz Guzik #endif
8651db24ffbSJonathan Lemon 	TCPDEBUG1();
866623dce13SRobert Watson 
86726ef6ac4SDon Lewis 	/*
86826ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
86926ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
87026ef6ac4SDon Lewis 	 * release the lock.
87126ef6ac4SDon Lewis 	 */
87226ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
87326ef6ac4SDon Lewis 		v4 = 1;
87426ef6ac4SDon Lewis 		port = inp->inp_fport;
87526ef6ac4SDon Lewis 		addr = inp->inp_faddr;
87626ef6ac4SDon Lewis 	} else {
87726ef6ac4SDon Lewis 		port = inp->inp_fport;
87826ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
87926ef6ac4SDon Lewis 	}
88026ef6ac4SDon Lewis 
881623dce13SRobert Watson out:
882623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8835d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8848501a69cSRobert Watson 	INP_WUNLOCK(inp);
88597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
88626ef6ac4SDon Lewis 	if (error == 0) {
88726ef6ac4SDon Lewis 		if (v4)
88826ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
88926ef6ac4SDon Lewis 		else
89026ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
89126ef6ac4SDon Lewis 	}
89226ef6ac4SDon Lewis 	return error;
893fb59c426SYoshinobu Inoue }
894fb59c426SYoshinobu Inoue #endif /* INET6 */
895f76fcf6dSJeffrey Hsu 
896f76fcf6dSJeffrey Hsu /*
8972c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8982c37256eSGarrett Wollman  */
8992c37256eSGarrett Wollman static int
9002c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
9012c37256eSGarrett Wollman {
9022c37256eSGarrett Wollman 	int error = 0;
903f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
904623dce13SRobert Watson 	struct tcpcb *tp = NULL;
9056573d758SMatt Macy 	struct epoch_tracker et;
9062c37256eSGarrett Wollman 
907623dce13SRobert Watson 	TCPDEBUG0;
908623dce13SRobert Watson 	inp = sotoinpcb(so);
909623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
9108501a69cSRobert Watson 	INP_WLOCK(inp);
911ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9120af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
9130af4ce45SGleb Smirnoff 		return (ECONNRESET);
914623dce13SRobert Watson 	}
9150af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
9160af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
917623dce13SRobert Watson 	TCPDEBUG1();
9182c37256eSGarrett Wollman 	socantsendmore(so);
919623dce13SRobert Watson 	tcp_usrclosed(tp);
920ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
921f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
922623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
9235d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
924f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
92597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
926623dce13SRobert Watson 
927623dce13SRobert Watson 	return (error);
9282c37256eSGarrett Wollman }
9292c37256eSGarrett Wollman 
9302c37256eSGarrett Wollman /*
9312c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
9322c37256eSGarrett Wollman  */
9332c37256eSGarrett Wollman static int
9342c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
9352c37256eSGarrett Wollman {
936109eb549SGleb Smirnoff 	struct epoch_tracker et;
937f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
938623dce13SRobert Watson 	struct tcpcb *tp = NULL;
939f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
9402c37256eSGarrett Wollman 
941623dce13SRobert Watson 	TCPDEBUG0;
942623dce13SRobert Watson 	inp = sotoinpcb(so);
943623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
9448501a69cSRobert Watson 	INP_WLOCK(inp);
945ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
94637a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
94737a7f557SGleb Smirnoff 		return (ECONNRESET);
948623dce13SRobert Watson 	}
94937a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
95037a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
951623dce13SRobert Watson 	TCPDEBUG1();
952281a0fd4SPatrick Kelsey 	/*
953281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
954281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
955281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
956281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
957281a0fd4SPatrick Kelsey 	 * expires.
958281a0fd4SPatrick Kelsey 	 */
95968bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
960281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
961281a0fd4SPatrick Kelsey 		goto out;
96209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
96309fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
96409fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
965460cf046SNavdeep Parhar 	else
96609fe6320SNavdeep Parhar #endif
967f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
968623dce13SRobert Watson out:
969623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9705d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
971f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
972f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
973623dce13SRobert Watson 	return (error);
9742c37256eSGarrett Wollman }
9752c37256eSGarrett Wollman 
9762c37256eSGarrett Wollman /*
9772c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9789c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9799c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9809c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9819c9906e9SPeter Wemm  * generally are caller-frees.
9822c37256eSGarrett Wollman  */
9832c37256eSGarrett Wollman static int
98457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
985b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9862c37256eSGarrett Wollman {
98797a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9882c37256eSGarrett Wollman 	int error = 0;
989f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
990623dce13SRobert Watson 	struct tcpcb *tp = NULL;
991888973f5SMichael Tuexen #ifdef INET
99251e08d53SMichael Tuexen #ifdef INET6
99351e08d53SMichael Tuexen 	struct sockaddr_in sin;
99451e08d53SMichael Tuexen #endif
99551e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
996888973f5SMichael Tuexen #endif
997fb59c426SYoshinobu Inoue #ifdef INET6
998fb59c426SYoshinobu Inoue 	int isipv6;
999fb59c426SYoshinobu Inoue #endif
10004a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
10014a91aa8fSMichael Tuexen 	u_char vflagsav;
10024a91aa8fSMichael Tuexen 	bool restoreflags;
10039c9906e9SPeter Wemm 	TCPDEBUG0;
10042c37256eSGarrett Wollman 
1005d8acd268SMark Johnston 	if (control != NULL) {
1006d8acd268SMark Johnston 		/* TCP doesn't do control messages (rights, creds, etc) */
1007d8acd268SMark Johnston 		if (control->m_len) {
1008d8acd268SMark Johnston 			m_freem(control);
10094287aa56SGleb Smirnoff 			return (EINVAL);
1010d8acd268SMark Johnston 		}
1011d8acd268SMark Johnston 		m_freem(control);	/* empty control, just free it */
1012d8acd268SMark Johnston 	}
10134287aa56SGleb Smirnoff 
10144287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
10154287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
10164287aa56SGleb Smirnoff 	INP_WLOCK(inp);
10174287aa56SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
10184287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
10194287aa56SGleb Smirnoff 			m_freem(m);
10204287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
10214287aa56SGleb Smirnoff 		return (ECONNRESET);
10224287aa56SGleb Smirnoff 	}
10234287aa56SGleb Smirnoff 
10244287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
10254287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
10264287aa56SGleb Smirnoff 	restoreflags = false;
10274287aa56SGleb Smirnoff 	tp = intotcpcb(inp);
10284287aa56SGleb Smirnoff 
10294287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
10307d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
10317d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
1032d3b6c96bSRandall Stewart 		goto out;
10337d2608a5SMark Johnston 
10349c9906e9SPeter Wemm 	TCPDEBUG1();
1035888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1036bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
1037bd4a39ccSMark Johnston 			error = EINVAL;
1038bd4a39ccSMark Johnston 			goto out;
1039bd4a39ccSMark Johnston 		}
1040888973f5SMichael Tuexen 		switch (nam->sa_family) {
1041888973f5SMichael Tuexen #ifdef INET
1042888973f5SMichael Tuexen 		case AF_INET:
1043888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
1044888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1045888973f5SMichael Tuexen 				error = EINVAL;
1046888973f5SMichael Tuexen 				goto out;
1047888973f5SMichael Tuexen 			}
1048888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
1049888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1050888973f5SMichael Tuexen 				goto out;
1051888973f5SMichael Tuexen 			}
1052888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1053888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1054888973f5SMichael Tuexen 				goto out;
1055888973f5SMichael Tuexen 			}
1056f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
1057f903a308SMichael Tuexen 				error = EACCES;
10582cf21ae5SRandall Stewart 				goto out;
10592cf21ae5SRandall Stewart 			}
1060888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
10617d2608a5SMark Johnston 			    &sinp->sin_addr)))
1062888973f5SMichael Tuexen 				goto out;
1063888973f5SMichael Tuexen #ifdef INET6
1064888973f5SMichael Tuexen 			isipv6 = 0;
1065888973f5SMichael Tuexen #endif
1066888973f5SMichael Tuexen 			break;
1067888973f5SMichael Tuexen #endif /* INET */
1068888973f5SMichael Tuexen #ifdef INET6
1069888973f5SMichael Tuexen 		case AF_INET6:
1070888973f5SMichael Tuexen 		{
10710ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1072888973f5SMichael Tuexen 
10730ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10740ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1075888973f5SMichael Tuexen 				error = EINVAL;
1076888973f5SMichael Tuexen 				goto out;
1077888973f5SMichael Tuexen 			}
1078e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1079e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1080e240ce42SMichael Tuexen 				goto out;
1081e240ce42SMichael Tuexen 			}
10820ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1083888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1084888973f5SMichael Tuexen 				goto out;
1085888973f5SMichael Tuexen 			}
10860ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1087888973f5SMichael Tuexen #ifdef INET
1088888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1089888973f5SMichael Tuexen 					error = EINVAL;
1090888973f5SMichael Tuexen 					goto out;
1091888973f5SMichael Tuexen 				}
1092888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1093888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1094888973f5SMichael Tuexen 					goto out;
1095888973f5SMichael Tuexen 				}
10964a91aa8fSMichael Tuexen 				restoreflags = true;
1097888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1098888973f5SMichael Tuexen 				sinp = &sin;
10990ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1100888973f5SMichael Tuexen 				if (IN_MULTICAST(
1101888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1102888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1103888973f5SMichael Tuexen 					goto out;
1104888973f5SMichael Tuexen 				}
1105888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
11067d2608a5SMark Johnston 				    &sinp->sin_addr)))
1107888973f5SMichael Tuexen 					goto out;
1108888973f5SMichael Tuexen 				isipv6 = 0;
1109888973f5SMichael Tuexen #else /* !INET */
1110888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1111888973f5SMichael Tuexen 				goto out;
1112888973f5SMichael Tuexen #endif /* INET */
1113888973f5SMichael Tuexen 			} else {
1114888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1115888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1116888973f5SMichael Tuexen 					goto out;
1117888973f5SMichael Tuexen 				}
11184a91aa8fSMichael Tuexen 				restoreflags = true;
1119888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1120888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1121888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
11227d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1123888973f5SMichael Tuexen 					goto out;
1124888973f5SMichael Tuexen 				isipv6 = 1;
1125888973f5SMichael Tuexen 			}
1126888973f5SMichael Tuexen 			break;
1127888973f5SMichael Tuexen 		}
1128888973f5SMichael Tuexen #endif /* INET6 */
1129888973f5SMichael Tuexen 		default:
1130888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1131888973f5SMichael Tuexen 			goto out;
1132888973f5SMichael Tuexen 		}
1133888973f5SMichael Tuexen 	}
11342c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1135651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
11367d2608a5SMark Johnston 		m = NULL;
11372c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1138bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1139bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1140bd4a39ccSMark Johnston 
11412c37256eSGarrett Wollman 			/*
11422c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11432c37256eSGarrett Wollman 			 * initialize window to default value, and
11440c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11452c37256eSGarrett Wollman 			 */
1146fb59c426SYoshinobu Inoue #ifdef INET6
1147fb59c426SYoshinobu Inoue 			if (isipv6)
1148b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1149fb59c426SYoshinobu Inoue #endif /* INET6 */
1150b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1151b287c6c7SBjoern A. Zeeb 			else
1152b287c6c7SBjoern A. Zeeb #endif
1153b287c6c7SBjoern A. Zeeb #ifdef INET
1154888973f5SMichael Tuexen 				error = tcp_connect(tp,
1155888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1156b287c6c7SBjoern A. Zeeb #endif
11574a91aa8fSMichael Tuexen 			/*
11584a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11594a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11604a91aa8fSMichael Tuexen 			 * operations fail.
11614a91aa8fSMichael Tuexen 			 */
11624a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11634a91aa8fSMichael Tuexen 				restoreflags = false;
11644a91aa8fSMichael Tuexen 
11657d2608a5SMark Johnston 			if (error) {
11667d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11677d2608a5SMark Johnston 				sbflush(&so->so_snd);
11682c37256eSGarrett Wollman 				goto out;
11697d2608a5SMark Johnston 			}
1170c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1171c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
117218a75309SPatrick Kelsey 			else {
11732c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11742c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11752c37256eSGarrett Wollman 			}
1176c560df6fSPatrick Kelsey 		}
11772c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11782c37256eSGarrett Wollman 			/*
11792c37256eSGarrett Wollman 			 * Close the send side of the connection after
11802c37256eSGarrett Wollman 			 * the data is sent.
11812c37256eSGarrett Wollman 			 */
11822c37256eSGarrett Wollman 			socantsendmore(so);
1183623dce13SRobert Watson 			tcp_usrclosed(tp);
11842c37256eSGarrett Wollman 		}
1185e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1186e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1187e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1188e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1189e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1190e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1191e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1192e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1193e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1194e854dd38SRandall Stewart 		}
11952cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11962cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1197b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1198b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1199f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1200b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1201b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1202b0acefa8SBill Fenner 		}
12032c37256eSGarrett Wollman 	} else {
1204623dce13SRobert Watson 		/*
1205623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1206623dce13SRobert Watson 		 */
1207d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
12082c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1209d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
12102c37256eSGarrett Wollman 			error = ENOBUFS;
12112c37256eSGarrett Wollman 			goto out;
12122c37256eSGarrett Wollman 		}
12132c37256eSGarrett Wollman 		/*
12142c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
12152c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
12162c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
12172c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
12182c37256eSGarrett Wollman 		 * of data past the urgent section.
12192c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
12202c37256eSGarrett Wollman 		 */
1221651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1222d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
12237d2608a5SMark Johnston 		m = NULL;
1224ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1225ef53690bSGarrett Wollman 			/*
1226ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1227ef53690bSGarrett Wollman 			 * initialize window to default value, and
12280c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1229ef53690bSGarrett Wollman 			 */
123018a75309SPatrick Kelsey 
1231c560df6fSPatrick Kelsey 			/*
1232c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1233c560df6fSPatrick Kelsey 			 */
1234c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1235c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1236fb59c426SYoshinobu Inoue #ifdef INET6
1237fb59c426SYoshinobu Inoue 			if (isipv6)
1238b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1239fb59c426SYoshinobu Inoue #endif /* INET6 */
1240b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1241b287c6c7SBjoern A. Zeeb 			else
1242b287c6c7SBjoern A. Zeeb #endif
1243b287c6c7SBjoern A. Zeeb #ifdef INET
1244888973f5SMichael Tuexen 				error = tcp_connect(tp,
1245888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1246b287c6c7SBjoern A. Zeeb #endif
12474a91aa8fSMichael Tuexen 			/*
12484a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12494a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12504a91aa8fSMichael Tuexen 			 * operations fail.
12514a91aa8fSMichael Tuexen 			 */
12524a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12534a91aa8fSMichael Tuexen 				restoreflags = false;
12544a91aa8fSMichael Tuexen 
12557d2608a5SMark Johnston 			if (error != 0) {
12567d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
12577d2608a5SMark Johnston 				sbflush(&so->so_snd);
1258ef53690bSGarrett Wollman 				goto out;
12597d2608a5SMark Johnston 			}
1260ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1261ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1262623dce13SRobert Watson 		}
1263300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12647d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
12652cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1266f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
12672cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12682c37256eSGarrett Wollman 		}
12692cbcd3c1SGleb Smirnoff 	}
12702529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12712529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12722529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12732529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12742529f56eSJonathan T. Looney 	    0, NULL, false);
12757d2608a5SMark Johnston 
1276d1401c90SRobert Watson out:
12774a91aa8fSMichael Tuexen 	/*
12787d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
12797d2608a5SMark Johnston 	 * responsible for freeing memory.
12807d2608a5SMark Johnston 	 */
12817d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12827d2608a5SMark Johnston 		m_freem(m);
12837d2608a5SMark Johnston 
12847d2608a5SMark Johnston 	/*
12854a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12864a91aa8fSMichael Tuexen 	 * restore the original flags.
12874a91aa8fSMichael Tuexen 	 */
12884a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12894a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12904a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12914a91aa8fSMichael Tuexen 	}
1292d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12932c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12945d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12955d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1296f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
129797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
129873fddedaSPeter Grehan 	return (error);
12992c37256eSGarrett Wollman }
13002c37256eSGarrett Wollman 
13012cbcd3c1SGleb Smirnoff static int
13022cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
13032cbcd3c1SGleb Smirnoff {
1304109eb549SGleb Smirnoff 	struct epoch_tracker et;
13052cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
13062cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
13072cbcd3c1SGleb Smirnoff 	int error;
13082cbcd3c1SGleb Smirnoff 
13092cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
13102cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
13112cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
13122cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
131382334850SJohn Baldwin 		mb_free_notready(m, count);
13142cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
13152cbcd3c1SGleb Smirnoff 	}
13162cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
13172cbcd3c1SGleb Smirnoff 
13182cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
13192cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
13202cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1321f64dc2abSGleb Smirnoff 	if (error) {
13222cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1323f64dc2abSGleb Smirnoff 		return (error);
1324f64dc2abSGleb Smirnoff 	}
1325f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1326f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1327f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
13282cbcd3c1SGleb Smirnoff 
13292cbcd3c1SGleb Smirnoff 	return (error);
13302cbcd3c1SGleb Smirnoff }
13312cbcd3c1SGleb Smirnoff 
13322c37256eSGarrett Wollman /*
1333a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
13342c37256eSGarrett Wollman  */
1335ac45e92fSRobert Watson static void
13362c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
13372c37256eSGarrett Wollman {
1338f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1339a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13406573d758SMatt Macy 	struct epoch_tracker et;
1341623dce13SRobert Watson 	TCPDEBUG0;
1342c78cbc7bSRobert Watson 
1343ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1344c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1345c78cbc7bSRobert Watson 
134697a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13478501a69cSRobert Watson 	INP_WLOCK(inp);
1348c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1349c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1350c78cbc7bSRobert Watson 
1351c78cbc7bSRobert Watson 	/*
1352a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1353c78cbc7bSRobert Watson 	 */
1354ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1355ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1356c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1357a152f8a3SRobert Watson 		TCPDEBUG1();
13588fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13598fa799bdSJonathan T. Looney 		if (tp == NULL)
13608fa799bdSJonathan T. Looney 			goto dropped;
1361a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13625d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1363c78cbc7bSRobert Watson 	}
1364ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1365d8596171SGleb Smirnoff 		soref(so);
1366ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1367a152f8a3SRobert Watson 	}
13688501a69cSRobert Watson 	INP_WUNLOCK(inp);
13698fa799bdSJonathan T. Looney dropped:
137097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1371a152f8a3SRobert Watson }
1372a152f8a3SRobert Watson 
1373a152f8a3SRobert Watson /*
1374a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1375a152f8a3SRobert Watson  */
1376a152f8a3SRobert Watson static void
1377a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1378a152f8a3SRobert Watson {
1379a152f8a3SRobert Watson 	struct inpcb *inp;
1380a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13816573d758SMatt Macy 	struct epoch_tracker et;
1382a152f8a3SRobert Watson 	TCPDEBUG0;
1383a152f8a3SRobert Watson 
1384a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1385a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1386a152f8a3SRobert Watson 
138797a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13888501a69cSRobert Watson 	INP_WLOCK(inp);
1389a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1390a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1391a152f8a3SRobert Watson 
1392a152f8a3SRobert Watson 	/*
1393a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1394a152f8a3SRobert Watson 	 * a disconnect.
1395a152f8a3SRobert Watson 	 */
1396ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1397ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1398a152f8a3SRobert Watson 		tp = intotcpcb(inp);
139974703901SGleb Smirnoff 		tp->t_flags |= TF_CLOSED;
1400a152f8a3SRobert Watson 		TCPDEBUG1();
1401a152f8a3SRobert Watson 		tcp_disconnect(tp);
1402a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
14035d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1404a152f8a3SRobert Watson 	}
1405ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1406d8596171SGleb Smirnoff 		soref(so);
1407ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1408a152f8a3SRobert Watson 	}
14098501a69cSRobert Watson 	INP_WUNLOCK(inp);
141097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
14112c37256eSGarrett Wollman }
14122c37256eSGarrett Wollman 
1413d3b6c96bSRandall Stewart static int
1414d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1415d3b6c96bSRandall Stewart {
1416d3b6c96bSRandall Stewart 	/*
1417d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1418d3b6c96bSRandall Stewart 	 * specified then it does not always support
1419d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1420d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1421d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1422d3b6c96bSRandall Stewart 	 */
1423d3b6c96bSRandall Stewart 	int ret = 0;
1424d3b6c96bSRandall Stewart 
1425d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1426d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1427d3b6c96bSRandall Stewart 	}
1428d3b6c96bSRandall Stewart 	return (ret);
1429d3b6c96bSRandall Stewart }
1430d3b6c96bSRandall Stewart 
14312c37256eSGarrett Wollman /*
14322c37256eSGarrett Wollman  * Receive out-of-band data.
14332c37256eSGarrett Wollman  */
14342c37256eSGarrett Wollman static int
14352c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
14362c37256eSGarrett Wollman {
14372c37256eSGarrett Wollman 	int error = 0;
1438f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1439623dce13SRobert Watson 	struct tcpcb *tp = NULL;
14402c37256eSGarrett Wollman 
1441623dce13SRobert Watson 	TCPDEBUG0;
1442623dce13SRobert Watson 	inp = sotoinpcb(so);
1443623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
14448501a69cSRobert Watson 	INP_WLOCK(inp);
1445ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
144621367f63SSam Leffler 		error = ECONNRESET;
1447623dce13SRobert Watson 		goto out;
1448623dce13SRobert Watson 	}
1449623dce13SRobert Watson 	tp = intotcpcb(inp);
1450d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1451d3b6c96bSRandall Stewart 	if (error) {
1452d3b6c96bSRandall Stewart 		goto out;
1453d3b6c96bSRandall Stewart 	}
1454623dce13SRobert Watson 	TCPDEBUG1();
14552c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1456c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
14574cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
14584cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
14592c37256eSGarrett Wollman 		error = EINVAL;
14602c37256eSGarrett Wollman 		goto out;
14612c37256eSGarrett Wollman 	}
14622c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
14632c37256eSGarrett Wollman 		error = EWOULDBLOCK;
14642c37256eSGarrett Wollman 		goto out;
14652c37256eSGarrett Wollman 	}
14662c37256eSGarrett Wollman 	m->m_len = 1;
14672c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
14682c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
14692c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1470623dce13SRobert Watson 
1471623dce13SRobert Watson out:
1472623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14735d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14748501a69cSRobert Watson 	INP_WUNLOCK(inp);
1475623dce13SRobert Watson 	return (error);
14762c37256eSGarrett Wollman }
14772c37256eSGarrett Wollman 
1478b287c6c7SBjoern A. Zeeb #ifdef INET
14792c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1480756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1481756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1482756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1483756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1484756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1485756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1486756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1487756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1488756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
148954d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1490756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1491756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1492756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14932cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1494756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
149554d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1496a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1497a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
14982c37256eSGarrett Wollman };
1499b287c6c7SBjoern A. Zeeb #endif /* INET */
1500df8bae1dSRodney W. Grimes 
1501fb59c426SYoshinobu Inoue #ifdef INET6
1502fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1503756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1504756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1505756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1506756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1507756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1508756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1509756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1510756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1511756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1512756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1513756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1514756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1515756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
15162cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1517756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1518756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1519a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1520a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1521fb59c426SYoshinobu Inoue };
1522fb59c426SYoshinobu Inoue #endif /* INET6 */
1523fb59c426SYoshinobu Inoue 
1524b287c6c7SBjoern A. Zeeb #ifdef INET
1525a0292f23SGarrett Wollman /*
1526a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1527a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
15285200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
15295200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
15305200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
15315200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1532a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1533a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1534a0292f23SGarrett Wollman  */
15350312fbe9SPoul-Henning Kamp static int
1536ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1537a0292f23SGarrett Wollman {
1538a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1539a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
15405200e00eSIan Dowse 	struct in_addr laddr;
15415200e00eSIan Dowse 	u_short lport;
1542c3229e05SDavid Greenman 	int error;
1543a0292f23SGarrett Wollman 
1544c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
15458501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1546fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1547623dce13SRobert Watson 
154825102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
15494616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15504616026fSErmal Luçi 		if (error)
1551fa046d87SRobert Watson 			goto out;
1552a0292f23SGarrett Wollman 	}
1553a0292f23SGarrett Wollman 
1554a0292f23SGarrett Wollman 	/*
1555a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1556a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1557a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1558a0292f23SGarrett Wollman 	 */
15595200e00eSIan Dowse 	laddr = inp->inp_laddr;
15605200e00eSIan Dowse 	lport = inp->inp_lport;
15615200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1562b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
15635200e00eSIan Dowse 	if (error && oinp == NULL)
1564fa046d87SRobert Watson 		goto out;
1565fa046d87SRobert Watson 	if (oinp) {
1566fa046d87SRobert Watson 		error = EADDRINUSE;
1567fa046d87SRobert Watson 		goto out;
1568fa046d87SRobert Watson 	}
156925102351SMike Karels 	/* Handle initial bind if it hadn't been done in advance. */
157025102351SMike Karels 	if (inp->inp_lport == 0) {
157125102351SMike Karels 		inp->inp_lport = lport;
157225102351SMike Karels 		if (in_pcbinshash(inp) != 0) {
157325102351SMike Karels 			inp->inp_lport = 0;
157425102351SMike Karels 			error = EAGAIN;
157525102351SMike Karels 			goto out;
157625102351SMike Karels 		}
157725102351SMike Karels 	}
15785200e00eSIan Dowse 	inp->inp_laddr = laddr;
157915bd2b43SDavid Greenman 	in_pcbrehash(inp);
1580fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1581a0292f23SGarrett Wollman 
1582087b55eaSAndre Oppermann 	/*
1583087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1584087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1585087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1586087b55eaSAndre Oppermann 	 */
1587a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15889b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1589a0292f23SGarrett Wollman 		tp->request_r_scale++;
1590a0292f23SGarrett Wollman 
1591a0292f23SGarrett Wollman 	soisconnecting(so);
159278b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
159357f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15948e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15958e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15968e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1597a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1598a45d2726SAndras Olah 
1599a0292f23SGarrett Wollman 	return 0;
1600fa046d87SRobert Watson 
1601fa046d87SRobert Watson out:
1602fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1603fa046d87SRobert Watson 	return (error);
1604a0292f23SGarrett Wollman }
1605b287c6c7SBjoern A. Zeeb #endif /* INET */
1606a0292f23SGarrett Wollman 
1607fb59c426SYoshinobu Inoue #ifdef INET6
1608fb59c426SYoshinobu Inoue static int
1609ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1610fb59c426SYoshinobu Inoue {
1611a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1612fb59c426SYoshinobu Inoue 	int error;
1613fb59c426SYoshinobu Inoue 
16148501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1615fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1616623dce13SRobert Watson 
161725102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
16184616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
16194616026fSErmal Luçi 		if (error)
1620fa046d87SRobert Watson 			goto out;
1621fb59c426SYoshinobu Inoue 	}
1622a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1623a7e201bbSAndrey V. Elsukov 	if (error != 0)
1624b598155aSRobert Watson 		goto out;
1625fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1626fb59c426SYoshinobu Inoue 
1627fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1628fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1629970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1630fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1631fb59c426SYoshinobu Inoue 
1632a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
163378b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
163457f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
16358e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
16368e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
16378e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1638fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1639fb59c426SYoshinobu Inoue 
1640fb59c426SYoshinobu Inoue 	return 0;
1641fa046d87SRobert Watson 
1642fa046d87SRobert Watson out:
1643fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1644fa046d87SRobert Watson 	return error;
1645fb59c426SYoshinobu Inoue }
1646fb59c426SYoshinobu Inoue #endif /* INET6 */
1647fb59c426SYoshinobu Inoue 
1648cfe8b629SGarrett Wollman /*
1649b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1650b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1651b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1652b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1653b8af5dfaSRobert Watson  * from Linux.
1654b8af5dfaSRobert Watson  */
1655b8af5dfaSRobert Watson static void
1656ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1657b8af5dfaSRobert Watson {
1658b8af5dfaSRobert Watson 
16598501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1660b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1661b8af5dfaSRobert Watson 
1662b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1663b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1664b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
16653529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1666b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1667b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1668b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1669b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1670b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1671b8af5dfaSRobert Watson 	}
16723f169c54SRichard Scheffenegger 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
16735a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
16741baaf834SBruce M Simpson 
167543d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
16763ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
16771baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
16781baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16791baaf834SBruce M Simpson 
1680b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1681b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1682b8af5dfaSRobert Watson 
1683b8af5dfaSRobert Watson 	/*
1684b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1685b8af5dfaSRobert Watson 	 */
1686c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1687535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1688b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16891c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1690535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
169143d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
169243d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1693f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1694f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1695f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1696a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1697a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1698a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1699a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1700a6456410SNavdeep Parhar 	}
1701a6456410SNavdeep Parhar #endif
1702b8af5dfaSRobert Watson }
1703b8af5dfaSRobert Watson 
1704b8af5dfaSRobert Watson /*
17051e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
17061e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
17071e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
17081e8f5ffaSRobert Watson  * option.
1709cfe8b629SGarrett Wollman  */
1710bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
17118501a69cSRobert Watson 	INP_WLOCK(inp);							\
1712ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
17138501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1714bac5bedfSConrad Meyer 		cleanup;						\
17151e8f5ffaSRobert Watson 		return (ECONNRESET);					\
17161e8f5ffaSRobert Watson 	}								\
17171e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
17181e8f5ffaSRobert Watson } while(0)
1719bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
17201e8f5ffaSRobert Watson 
1721fd7daa72SMichael Tuexen int
1722fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1723df8bae1dSRodney W. Grimes {
1724fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1725fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1726fc4d53ccSGleb Smirnoff 	int error = 0;
1727df8bae1dSRodney W. Grimes 
1728fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
17293b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
1730fd7daa72SMichael Tuexen 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1731fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1732fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1733fc4d53ccSGleb Smirnoff 
1734cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
17353b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1736fb59c426SYoshinobu Inoue #ifdef INET6
1737de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1738fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1739de156263SGleb Smirnoff #endif
1740de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1741de156263SGleb Smirnoff 		else
1742de156263SGleb Smirnoff #endif
1743de156263SGleb Smirnoff #ifdef INET
1744fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1745de156263SGleb Smirnoff #endif
17465dff1c38SMichael Tuexen 		/*
1747de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1748de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1749de156263SGleb Smirnoff 		 * IP level returned.
17505dff1c38SMichael Tuexen 		 */
1751de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1752de156263SGleb Smirnoff #ifdef INET6
1753de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1754de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1755de156263SGleb Smirnoff 				return (error);
1756de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1757de156263SGleb Smirnoff 			case IPV6_TCLASS:
1758de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1759de156263SGleb Smirnoff 				break;
1760de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1761f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1762f581a26eSGleb Smirnoff 				break;
1763de156263SGleb Smirnoff 			default:
1764de156263SGleb Smirnoff 				return (error);
17655dff1c38SMichael Tuexen 			}
1766de156263SGleb Smirnoff 			break;
1767b287c6c7SBjoern A. Zeeb #endif
1768b287c6c7SBjoern A. Zeeb #ifdef INET
1769de156263SGleb Smirnoff 		case IPPROTO_IP:
1770de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1771de156263SGleb Smirnoff 			case IP_TOS:
17723b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
17733b0ee680SRichard Scheffenegger 				break;
1774de156263SGleb Smirnoff 			case IP_TTL:
1775de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1776de156263SGleb Smirnoff 				break;
1777de156263SGleb Smirnoff 			default:
1778df8bae1dSRodney W. Grimes 				return (error);
1779de156263SGleb Smirnoff 			}
1780de156263SGleb Smirnoff 			break;
1781de156263SGleb Smirnoff #endif
1782de156263SGleb Smirnoff 		default:
1783de156263SGleb Smirnoff 			return (error);
1784de156263SGleb Smirnoff 		}
17853b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
17863b3c08c1SMichael Tuexen 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17873b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
17883b3c08c1SMichael Tuexen 			return (ECONNRESET);
17893b3c08c1SMichael Tuexen 		}
1790fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1791fc4d53ccSGleb Smirnoff 		/*
1792fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1793fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1794fc4d53ccSGleb Smirnoff 		 */
1795fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1796fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
1797fc4d53ccSGleb Smirnoff 
17983b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1799fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1800fc4d53ccSGleb Smirnoff 		if (error)
1801fc4d53ccSGleb Smirnoff 			return (error);
1802fc4d53ccSGleb Smirnoff 
180368cea2b1SJohn Baldwin 		INP_WLOCK(inp);
1804ad71fe3cSRobert Watson 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
18058501a69cSRobert Watson 			INP_WUNLOCK(inp);
18061e8f5ffaSRobert Watson 			return (ECONNRESET);
1807623dce13SRobert Watson 		}
180855bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1809fc4d53ccSGleb Smirnoff 
181055bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
181155bceb1eSRandall Stewart 		if (blk == NULL) {
181255bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
181355bceb1eSRandall Stewart 			return (ENOENT);
181455bceb1eSRandall Stewart 		}
1815587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1816587d67c0SRandall Stewart 			/* You already have this */
1817587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1818587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1819587d67c0SRandall Stewart 			return (0);
1820587d67c0SRandall Stewart 		}
1821587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1822587d67c0SRandall Stewart 			/*
1823587d67c0SRandall Stewart 			 * The user has advanced the state
1824587d67c0SRandall Stewart 			 * past the initial point, we may not
1825587d67c0SRandall Stewart 			 * be able to switch.
1826587d67c0SRandall Stewart 			 */
1827587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1828587d67c0SRandall Stewart 				/*
1829587d67c0SRandall Stewart 				 * Does the stack provide a
1830587d67c0SRandall Stewart 				 * query mechanism, if so it may
1831587d67c0SRandall Stewart 				 * still be possible?
1832587d67c0SRandall Stewart 				 */
1833587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1834c6c0be27SMichael Tuexen 			} else
1835c6c0be27SMichael Tuexen 				error = EINVAL;
1836587d67c0SRandall Stewart 			if (error) {
1837587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1838587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1839587d67c0SRandall Stewart 				return(error);
1840587d67c0SRandall Stewart 			}
1841587d67c0SRandall Stewart 		}
184255bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
184355bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
184455bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
184555bceb1eSRandall Stewart 			return (ENOENT);
184655bceb1eSRandall Stewart 		}
184755bceb1eSRandall Stewart 		/*
184855bceb1eSRandall Stewart 		 * Release the old refcnt, the
1849587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1850587d67c0SRandall Stewart 		 * new one already.
185155bceb1eSRandall Stewart 		 */
1852587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1853086a3556SAndrew Gallatin 			struct epoch_tracker et;
1854587d67c0SRandall Stewart 			/*
1855587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1856587d67c0SRandall Stewart 			 * the tcb is not going away.
1857587d67c0SRandall Stewart 			 */
1858086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1859587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1860086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1861587d67c0SRandall Stewart 		}
18623ee9c3c4SRandall Stewart #ifdef TCPHPTS
18633ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
1864a370832bSGleb Smirnoff 		tcp_hpts_remove(tp->t_inpcb);
18653ee9c3c4SRandall Stewart #endif
18663ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
18673ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
18683ee9c3c4SRandall Stewart 			if (error) {
18693ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
18703ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
18713ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
18723ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
18733ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
1874fd7daa72SMichael Tuexen 						soabort(so);
18753ee9c3c4SRandall Stewart 						return (error);
18763ee9c3c4SRandall Stewart 					}
18773ee9c3c4SRandall Stewart 				}
18783ee9c3c4SRandall Stewart 				goto err_out;
18793ee9c3c4SRandall Stewart 			}
18803ee9c3c4SRandall Stewart 		}
188155bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
188255bceb1eSRandall Stewart 		tp->t_fb = blk;
188355bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
188455bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
188555bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
188655bceb1eSRandall Stewart 			     sopt->sopt_name);
188755bceb1eSRandall Stewart 		}
188855bceb1eSRandall Stewart #endif
18893ee9c3c4SRandall Stewart err_out:
189055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
189155bceb1eSRandall Stewart 		return (error);
1892fc4d53ccSGleb Smirnoff 	}
1893fc4d53ccSGleb Smirnoff 
18943b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
18953b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1896fc4d53ccSGleb Smirnoff }
1897fc4d53ccSGleb Smirnoff 
1898fc4d53ccSGleb Smirnoff static int
1899fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1900fc4d53ccSGleb Smirnoff {
1901fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1902fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1903fc4d53ccSGleb Smirnoff 	int error = 0;
1904fc4d53ccSGleb Smirnoff 
1905fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
19063b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
1907fd7daa72SMichael Tuexen 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1908fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1909fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1910fc4d53ccSGleb Smirnoff 
1911fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
19123b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1913fc4d53ccSGleb Smirnoff #ifdef INET6
1914fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1915fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1916fc4d53ccSGleb Smirnoff #endif /* INET6 */
1917fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1918fc4d53ccSGleb Smirnoff 		else
1919fc4d53ccSGleb Smirnoff #endif
1920fc4d53ccSGleb Smirnoff #ifdef INET
1921fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1922fc4d53ccSGleb Smirnoff #endif
1923fc4d53ccSGleb Smirnoff 		return (error);
1924fc4d53ccSGleb Smirnoff 	}
1925fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1926e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1927fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1928fc4d53ccSGleb Smirnoff 
1929e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1930e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1931e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1932e2833083SPeter Lei 		} else {
1933e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1934e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1935c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1936c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1937e2833083SPeter Lei 		}
193855bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
193955bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
194055bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
194155bceb1eSRandall Stewart 		return (error);
194255bceb1eSRandall Stewart 	}
1943fc4d53ccSGleb Smirnoff 
19443b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
19453b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1946fc4d53ccSGleb Smirnoff }
1947fc4d53ccSGleb Smirnoff 
1948fc4d53ccSGleb Smirnoff int
1949fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1950fc4d53ccSGleb Smirnoff {
1951fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1952fc4d53ccSGleb Smirnoff 
1953fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1954fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1955fc4d53ccSGleb Smirnoff 
19563b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
19573b3c08c1SMichael Tuexen 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
19583b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
19593b3c08c1SMichael Tuexen 		return (ECONNRESET);
19603b3c08c1SMichael Tuexen 	}
1961fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1962fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1963fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1964fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1965fc4d53ccSGleb Smirnoff 	else
1966fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
196755bceb1eSRandall Stewart }
196855bceb1eSRandall Stewart 
19692529f56eSJonathan T. Looney /*
19702529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
19712529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
19722529f56eSJonathan T. Looney  */
19732529f56eSJonathan T. Looney #ifdef CTASSERT
19742529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
19752529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
19762529f56eSJonathan T. Looney #endif
19772529f56eSJonathan T. Looney 
1978ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1979ec1db6e1SJohn Baldwin static int
1980ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1981ec1db6e1SJohn Baldwin {
1982ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1983ec1db6e1SJohn Baldwin 	int error;
1984ec1db6e1SJohn Baldwin 
1985ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1986ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1987ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1988ec1db6e1SJohn Baldwin 		if (error)
1989ec1db6e1SJohn Baldwin 			return (error);
1990ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1991ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1992ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1993ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1994ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1995ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1996ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1997ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1998ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1999ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
2000ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
2001ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
2002ec1db6e1SJohn Baldwin 		return (0);
2003ec1db6e1SJohn Baldwin 	}
2004ec1db6e1SJohn Baldwin 
2005ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
2006ec1db6e1SJohn Baldwin }
2007ec1db6e1SJohn Baldwin #endif
2008ec1db6e1SJohn Baldwin 
2009b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
2010b8d60729SRandall Stewart 
2011b8d60729SRandall Stewart static int
2012ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
2013b8d60729SRandall Stewart {
2014b8d60729SRandall Stewart 	struct cc_algo *algo;
2015b8d60729SRandall Stewart 	void *ptr = NULL;
20163b3c08c1SMichael Tuexen 	struct tcpcb *tp;
2017b8d60729SRandall Stewart 	struct cc_var cc_mem;
2018b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
2019b8d60729SRandall Stewart 	size_t mem_sz;
2020b8d60729SRandall Stewart 	int error;
2021b8d60729SRandall Stewart 
2022b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2023b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2024b8d60729SRandall Stewart 	if (error)
2025b8d60729SRandall Stewart 		return(error);
2026b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
2027b8d60729SRandall Stewart 	CC_LIST_RLOCK();
2028ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
2029b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
2030b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
2031b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
2032b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
2033b8d60729SRandall Stewart 				continue;
2034b8d60729SRandall Stewart 			}
2035b8d60729SRandall Stewart 			break;
2036b8d60729SRandall Stewart 		}
2037ea9017fbSRandall Stewart 	}
2038b8d60729SRandall Stewart 	if (algo == NULL) {
2039b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
2040b8d60729SRandall Stewart 		return(ESRCH);
2041b8d60729SRandall Stewart 	}
2042ea9017fbSRandall Stewart 	/*
2043ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
2044ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
2045ea9017fbSRandall Stewart 	 */
2046ea9017fbSRandall Stewart 	cc_refer(algo);
2047ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2048b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
2049b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
2050b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
2051b8d60729SRandall Stewart 		if (mem_sz == 0) {
2052b8d60729SRandall Stewart 			goto no_mem_needed;
2053b8d60729SRandall Stewart 		}
2054b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
2055b8d60729SRandall Stewart 	} else {
2056b8d60729SRandall Stewart no_mem_needed:
2057b8d60729SRandall Stewart 		mem_sz = 0;
2058b8d60729SRandall Stewart 		ptr = NULL;
2059b8d60729SRandall Stewart 	}
2060b8d60729SRandall Stewart 	/*
2061b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
2062b8d60729SRandall Stewart 	 * back the inplock.
2063b8d60729SRandall Stewart 	 */
2064b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
2065df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
2066df07bfdaSMichael Tuexen 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2067df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
2068ea9017fbSRandall Stewart 		if (ptr)
2069df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
2070ea9017fbSRandall Stewart 		/* Release our temp reference */
2071ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
2072ea9017fbSRandall Stewart 		cc_release(algo);
2073ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
2074df07bfdaSMichael Tuexen 		return (ECONNRESET);
2075df07bfdaSMichael Tuexen 	}
2076df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
2077df07bfdaSMichael Tuexen 	if (ptr != NULL)
2078b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
2079b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
2080b8d60729SRandall Stewart 	/*
2081b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
2082b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
2083b8d60729SRandall Stewart 	 * Note here we init into stack memory.
2084b8d60729SRandall Stewart 	 */
2085b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
2086b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
2087b8d60729SRandall Stewart 	else
2088b8d60729SRandall Stewart 		error = 0;
2089b8d60729SRandall Stewart 	/*
2090b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
2091b8d60729SRandall Stewart 	 * should not fail we could in theory have a
2092b8d60729SRandall Stewart 	 * KASSERT here.
2093b8d60729SRandall Stewart 	 */
2094b8d60729SRandall Stewart 	if (error == 0) {
2095b8d60729SRandall Stewart 		/*
2096b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
2097b8d60729SRandall Stewart 		 * connection to the new CC module by
2098b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
2099b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
2100b8d60729SRandall Stewart 		 */
2101b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
2102b8d60729SRandall Stewart 			CC_ALGO(tp)->cb_destroy(tp->ccv);
2103ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
2104ea9017fbSRandall Stewart 		cc_detach(tp);
2105ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
2106b8d60729SRandall Stewart 		memcpy(tp->ccv, &cc_mem, sizeof(struct cc_var));
2107ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
2108ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2109b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2110b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2111b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2112b8d60729SRandall Stewart 			CC_ALGO(tp)->conn_init(tp->ccv);
2113b8d60729SRandall Stewart 		}
2114b8d60729SRandall Stewart 	} else if (ptr)
2115b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2116b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2117ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2118ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2119ea9017fbSRandall Stewart 	cc_release(algo);
2120ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2121b8d60729SRandall Stewart 	return (error);
2122b8d60729SRandall Stewart }
2123b8d60729SRandall Stewart 
212455bceb1eSRandall Stewart int
21253b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
212655bceb1eSRandall Stewart {
2127fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
212855bceb1eSRandall Stewart 	int	error, opt, optval;
212955bceb1eSRandall Stewart 	u_int	ui;
213055bceb1eSRandall Stewart 	struct	tcp_info ti;
2131b2e60773SJohn Baldwin #ifdef KERN_TLS
2132b2e60773SJohn Baldwin 	struct tls_enable tls;
2133528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2134b2e60773SJohn Baldwin #endif
21352529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2136adc56f5aSEdward Tomasz Napierala #ifdef STATS
2137adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2138adc56f5aSEdward Tomasz Napierala #endif
2139af6fef3aSGleb Smirnoff 	size_t	len;
2140df8bae1dSRodney W. Grimes 
2141f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
2142fd7daa72SMichael Tuexen 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
2143fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2144528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2145f581a26eSGleb Smirnoff 
2146f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2147f581a26eSGleb Smirnoff #ifdef INET6
2148f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2149f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2150f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2151f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2152f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2153f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2154f581a26eSGleb Smirnoff 		}
2155f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2156f581a26eSGleb Smirnoff 		return (0);
2157f581a26eSGleb Smirnoff #endif
2158f581a26eSGleb Smirnoff #ifdef INET
2159f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2160f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2161f581a26eSGleb Smirnoff 		return (0);
2162f581a26eSGleb Smirnoff #endif
2163f581a26eSGleb Smirnoff 	}
2164f581a26eSGleb Smirnoff 
2165d519cedbSGleb Smirnoff 	/*
2166d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2167d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2168d519cedbSGleb Smirnoff 	 */
2169d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2170d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2171d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2172c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2173c8b53cedSMichael Tuexen 			return (EINVAL);
2174af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2175af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2176d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2177d519cedbSGleb Smirnoff 		if (error) {
2178af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2179d519cedbSGleb Smirnoff 			return (error);
2180d519cedbSGleb Smirnoff 		}
2181bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2182d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2183af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
2184d519cedbSGleb Smirnoff 		else
2185d519cedbSGleb Smirnoff 			error = ENOENT;
2186d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2187d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2188af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2189af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2190d519cedbSGleb Smirnoff 		return (error);
2191d519cedbSGleb Smirnoff 	}
2192d519cedbSGleb Smirnoff 
2193cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2194cfe8b629SGarrett Wollman 	case SOPT_SET:
2195cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2196fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
219788f6b043SBruce M Simpson 		case TCP_MD5SIG:
21988501a69cSRobert Watson 			INP_WUNLOCK(inp);
219997453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2200fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2201fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
22021cfd4b53SBruce M Simpson 			if (error)
22031e8f5ffaSRobert Watson 				return (error);
220497453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
220509fe6320SNavdeep Parhar 			goto unlock_and_done;
2206fcf59617SAndrey V. Elsukov #endif /* IPSEC */
220709fe6320SNavdeep Parhar 
2208df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2209cfe8b629SGarrett Wollman 		case TCP_NOOPT:
22100471a8c7SRichard Scheffenegger 		case TCP_LRD:
22118501a69cSRobert Watson 			INP_WUNLOCK(inp);
2212cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2213cfe8b629SGarrett Wollman 			    sizeof optval);
2214cfe8b629SGarrett Wollman 			if (error)
22151e8f5ffaSRobert Watson 				return (error);
2216cfe8b629SGarrett Wollman 
22178501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2218cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2219cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2220cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2221cfe8b629SGarrett Wollman 				break;
2222cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2223cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2224cfe8b629SGarrett Wollman 				break;
22250471a8c7SRichard Scheffenegger 			case TCP_LRD:
22260471a8c7SRichard Scheffenegger 				opt = TF_LRD;
22270471a8c7SRichard Scheffenegger 				break;
2228cfe8b629SGarrett Wollman 			default:
2229cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2230cfe8b629SGarrett Wollman 				break;
2231cfe8b629SGarrett Wollman 			}
2232cfe8b629SGarrett Wollman 
2233cfe8b629SGarrett Wollman 			if (optval)
2234cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2235df8bae1dSRodney W. Grimes 			else
2236cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
223709fe6320SNavdeep Parhar unlock_and_done:
223809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
223909fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
224009fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
224109fe6320SNavdeep Parhar 				    sopt->sopt_name);
224209fe6320SNavdeep Parhar 			}
224309fe6320SNavdeep Parhar #endif
22448501a69cSRobert Watson 			INP_WUNLOCK(inp);
2245df8bae1dSRodney W. Grimes 			break;
2246df8bae1dSRodney W. Grimes 
2247007581c0SJonathan Lemon 		case TCP_NOPUSH:
22488501a69cSRobert Watson 			INP_WUNLOCK(inp);
2249007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2250007581c0SJonathan Lemon 			    sizeof optval);
2251007581c0SJonathan Lemon 			if (error)
22521e8f5ffaSRobert Watson 				return (error);
2253007581c0SJonathan Lemon 
22548501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2255007581c0SJonathan Lemon 			if (optval)
2256007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2257d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2258007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2259109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2260109eb549SGleb Smirnoff 					struct epoch_tracker et;
2261109eb549SGleb Smirnoff 
2262109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2263f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2264109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2265109eb549SGleb Smirnoff 				}
2266007581c0SJonathan Lemon 			}
226709fe6320SNavdeep Parhar 			goto unlock_and_done;
2268007581c0SJonathan Lemon 
22699e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
22709e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
22719e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
22729e644c23SMichael Tuexen 			    sizeof optval);
22739e644c23SMichael Tuexen 			if (error)
22749e644c23SMichael Tuexen 				return (error);
22759e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
22769e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
22779e644c23SMichael Tuexen 				/* Its got to be in range */
22789e644c23SMichael Tuexen 				return (EINVAL);
22799e644c23SMichael Tuexen 			}
22809e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
22819e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
22829e644c23SMichael Tuexen 				return (EINVAL);
22839e644c23SMichael Tuexen 			}
22849e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
22859e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
22869e644c23SMichael Tuexen 				/* You can't change after you are connected */
22879e644c23SMichael Tuexen 				error = EINVAL;
22889e644c23SMichael Tuexen 			} else {
22899e644c23SMichael Tuexen 				/* Ok we are all good set the port */
22909e644c23SMichael Tuexen 				tp->t_port = htons(optval);
22919e644c23SMichael Tuexen 			}
22929e644c23SMichael Tuexen 			goto unlock_and_done;
22939e644c23SMichael Tuexen 
2294df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
22958501a69cSRobert Watson 			INP_WUNLOCK(inp);
2296cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2297cfe8b629SGarrett Wollman 			    sizeof optval);
2298cfe8b629SGarrett Wollman 			if (error)
22991e8f5ffaSRobert Watson 				return (error);
2300df8bae1dSRodney W. Grimes 
23018501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
230253369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2303603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2304cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2305a0292f23SGarrett Wollman 			else
2306a0292f23SGarrett Wollman 				error = EINVAL;
230709fe6320SNavdeep Parhar 			goto unlock_and_done;
2308a0292f23SGarrett Wollman 
2309b8af5dfaSRobert Watson 		case TCP_INFO:
23108501a69cSRobert Watson 			INP_WUNLOCK(inp);
2311b8af5dfaSRobert Watson 			error = EINVAL;
2312b8af5dfaSRobert Watson 			break;
2313b8af5dfaSRobert Watson 
2314adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2315adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2316adc56f5aSEdward Tomasz Napierala #ifdef STATS
2317adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2318adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2319adc56f5aSEdward Tomasz Napierala 			if (error)
2320adc56f5aSEdward Tomasz Napierala 				return (error);
2321adc56f5aSEdward Tomasz Napierala 
2322adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2323adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2324adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2325adc56f5aSEdward Tomasz Napierala 			else
2326adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2327adc56f5aSEdward Tomasz Napierala 
2328adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2329adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2330adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2331adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2332adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2333adc56f5aSEdward Tomasz Napierala 				sbp = t;
2334adc56f5aSEdward Tomasz Napierala 			}
2335adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2336adc56f5aSEdward Tomasz Napierala 
2337adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2338adc56f5aSEdward Tomasz Napierala #else
2339adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2340adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2341adc56f5aSEdward Tomasz Napierala 			break;
2342adc56f5aSEdward Tomasz Napierala 
2343dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2344ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
234573e263b1SGleb Smirnoff 			break;
2346dbc42409SLawrence Stewart 
2347a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2348a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2349a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2350a034518aSAndrew Gallatin 			    sizeof(optval));
2351a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2352a034518aSAndrew Gallatin 			if (!error)
2353a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2354a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2355a034518aSAndrew Gallatin 			break;
2356a034518aSAndrew Gallatin 
2357b2e60773SJohn Baldwin #ifdef KERN_TLS
2358b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2359b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2360ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2361b2e60773SJohn Baldwin 			if (error)
2362b2e60773SJohn Baldwin 				break;
2363fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2364b2e60773SJohn Baldwin 			break;
2365b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2366b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2367b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2368b2e60773SJohn Baldwin 			if (error)
2369b2e60773SJohn Baldwin 				return (error);
2370b2e60773SJohn Baldwin 
2371b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2372fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2373b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2374b2e60773SJohn Baldwin 			break;
2375f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2376f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2377f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2378f1f93475SJohn Baldwin 			    sizeof(tls));
2379f1f93475SJohn Baldwin 			if (error)
2380f1f93475SJohn Baldwin 				break;
2381fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2382f1f93475SJohn Baldwin 			break;
2383b2e60773SJohn Baldwin #endif
2384b2e60773SJohn Baldwin 
23859077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
23869077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
23879077f387SGleb Smirnoff 		case TCP_KEEPINIT:
23889077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
23899077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
23909077f387SGleb Smirnoff 			if (error)
23919077f387SGleb Smirnoff 				return (error);
23929077f387SGleb Smirnoff 
23939077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
23949077f387SGleb Smirnoff 				error = EINVAL;
23959077f387SGleb Smirnoff 				break;
23969077f387SGleb Smirnoff 			}
23979077f387SGleb Smirnoff 			ui *= hz;
23989077f387SGleb Smirnoff 
23999077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
24009077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
24019077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
24029077f387SGleb Smirnoff 				tp->t_keepidle = ui;
24039077f387SGleb Smirnoff 				/*
24049077f387SGleb Smirnoff 				 * XXX: better check current remaining
24059077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
24069077f387SGleb Smirnoff 				 */
24079077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
24089077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
24099077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
24109077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
24119077f387SGleb Smirnoff 				break;
24129077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
24139077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
24149077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
24159077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
24169077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
24179077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
24189077f387SGleb Smirnoff 				break;
24199077f387SGleb Smirnoff 			case TCP_KEEPINIT:
24209077f387SGleb Smirnoff 				tp->t_keepinit = ui;
24219077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
24229077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
24239077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
24249077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
24259077f387SGleb Smirnoff 				break;
24269077f387SGleb Smirnoff 			}
242709fe6320SNavdeep Parhar 			goto unlock_and_done;
24289077f387SGleb Smirnoff 
242985c05144SGleb Smirnoff 		case TCP_KEEPCNT:
243085c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
243185c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
243285c05144SGleb Smirnoff 			if (error)
243385c05144SGleb Smirnoff 				return (error);
243485c05144SGleb Smirnoff 
243585c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
243685c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
243785c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
243885c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
243985c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
244085c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
244185c05144SGleb Smirnoff 			goto unlock_and_done;
244285c05144SGleb Smirnoff 
244386a996e6SHiren Panchasara #ifdef TCPPCAP
244486a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
244586a996e6SHiren Panchasara 		case TCP_PCAP_IN:
244686a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
244786a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
244886a996e6SHiren Panchasara 			    sizeof optval);
244986a996e6SHiren Panchasara 			if (error)
245086a996e6SHiren Panchasara 				return (error);
245186a996e6SHiren Panchasara 
245286a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
245386a996e6SHiren Panchasara 			if (optval >= 0)
245486a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
245586a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
245686a996e6SHiren Panchasara 					optval);
245786a996e6SHiren Panchasara 			else
245886a996e6SHiren Panchasara 				error = EINVAL;
245986a996e6SHiren Panchasara 			goto unlock_and_done;
246086a996e6SHiren Panchasara #endif
246186a996e6SHiren Panchasara 
2462c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2463c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2464c560df6fSPatrick Kelsey 
2465281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2466c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2467c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2468281a0fd4SPatrick Kelsey 				return (EPERM);
2469281a0fd4SPatrick Kelsey 
2470c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2471c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2472281a0fd4SPatrick Kelsey 			if (error)
2473281a0fd4SPatrick Kelsey 				return (error);
2474281a0fd4SPatrick Kelsey 
2475281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2476d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2477d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2478d442a657SMichael Tuexen 				error = EINVAL;
2479d442a657SMichael Tuexen 				goto unlock_and_done;
2480d442a657SMichael Tuexen 			}
2481c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2482c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2483c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2484c560df6fSPatrick Kelsey 						error = EPERM;
2485c560df6fSPatrick Kelsey 						goto unlock_and_done;
2486c560df6fSPatrick Kelsey 					}
2487c560df6fSPatrick Kelsey 
2488c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2489281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2490281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2491c560df6fSPatrick Kelsey 				} else {
2492c560df6fSPatrick Kelsey 					/*
2493c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2494c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2495c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2496c560df6fSPatrick Kelsey 					 * connect.
2497c560df6fSPatrick Kelsey 					 */
2498c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2499c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2500c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2501c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2502c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2503c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2504c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2505c560df6fSPatrick Kelsey 					}
2506c560df6fSPatrick Kelsey 				}
2507d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2508281a0fd4SPatrick Kelsey 			} else
2509281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2510281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2511c560df6fSPatrick Kelsey 		}
2512281a0fd4SPatrick Kelsey 
2513e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
25142529f56eSJonathan T. Looney 		case TCP_LOG:
25152529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25162529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
25172529f56eSJonathan T. Looney 			    sizeof optval);
25182529f56eSJonathan T. Looney 			if (error)
25192529f56eSJonathan T. Looney 				return (error);
25202529f56eSJonathan T. Looney 
25212529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
25222529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
25232529f56eSJonathan T. Looney 			goto unlock_and_done;
25242529f56eSJonathan T. Looney 
25252529f56eSJonathan T. Looney 		case TCP_LOGBUF:
25262529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25272529f56eSJonathan T. Looney 			error = EINVAL;
25282529f56eSJonathan T. Looney 			break;
25292529f56eSJonathan T. Looney 
25302529f56eSJonathan T. Looney 		case TCP_LOGID:
25312529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25322529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
25332529f56eSJonathan T. Looney 			if (error)
25342529f56eSJonathan T. Looney 				break;
25352529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
25362529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
25372529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
25382529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
25392529f56eSJonathan T. Looney 			break;
25402529f56eSJonathan T. Looney 
25412529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
25422529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
25432529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25442529f56eSJonathan T. Looney 			error =
25452529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
25462529f56eSJonathan T. Looney 			if (error)
25472529f56eSJonathan T. Looney 				break;
25482529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
25492529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
25502529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
25512529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
25522529f56eSJonathan T. Looney 				    M_WAITOK, true);
25532529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
25542529f56eSJonathan T. Looney 			} else {
25552529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
25562529f56eSJonathan T. Looney 				/*
25572529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
25582529f56eSJonathan T. Looney 				 * INP lock.
25592529f56eSJonathan T. Looney 				 */
25602529f56eSJonathan T. Looney 			}
25612529f56eSJonathan T. Looney 			break;
2562e24e5683SJonathan T. Looney #endif
25632529f56eSJonathan T. Looney 
2564df8bae1dSRodney W. Grimes 		default:
25658501a69cSRobert Watson 			INP_WUNLOCK(inp);
2566df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2567df8bae1dSRodney W. Grimes 			break;
2568df8bae1dSRodney W. Grimes 		}
2569df8bae1dSRodney W. Grimes 		break;
2570df8bae1dSRodney W. Grimes 
2571cfe8b629SGarrett Wollman 	case SOPT_GET:
25721e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2573cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2574fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
257588f6b043SBruce M Simpson 		case TCP_MD5SIG:
25768501a69cSRobert Watson 			INP_WUNLOCK(inp);
257797453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2578fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2579fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
25801cfd4b53SBruce M Simpson 			break;
2581265ed012SBruce M Simpson #endif
25821e8f5ffaSRobert Watson 
2583df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2584cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
25858501a69cSRobert Watson 			INP_WUNLOCK(inp);
2586b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2587df8bae1dSRodney W. Grimes 			break;
2588df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2589cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
25908501a69cSRobert Watson 			INP_WUNLOCK(inp);
2591b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2592df8bae1dSRodney W. Grimes 			break;
25939e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
25949e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
25959e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
25969e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25979e644c23SMichael Tuexen 			break;
2598a0292f23SGarrett Wollman 		case TCP_NOOPT:
2599cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
26008501a69cSRobert Watson 			INP_WUNLOCK(inp);
2601b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2602a0292f23SGarrett Wollman 			break;
2603a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2604cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
26058501a69cSRobert Watson 			INP_WUNLOCK(inp);
2606b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2607b8af5dfaSRobert Watson 			break;
2608b8af5dfaSRobert Watson 		case TCP_INFO:
2609b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
26108501a69cSRobert Watson 			INP_WUNLOCK(inp);
2611b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2612a0292f23SGarrett Wollman 			break;
2613adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2614adc56f5aSEdward Tomasz Napierala 			{
2615adc56f5aSEdward Tomasz Napierala #ifdef STATS
2616adc56f5aSEdward Tomasz Napierala 			int nheld;
2617adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2618adc56f5aSEdward Tomasz Napierala 
2619adc56f5aSEdward Tomasz Napierala 			error = 0;
2620adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2621adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2622adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2623adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2624adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2625adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2626adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2627adc56f5aSEdward Tomasz Napierala 			else
2628adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2629adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2630adc56f5aSEdward Tomasz Napierala 			if (error)
2631adc56f5aSEdward Tomasz Napierala 				break;
2632adc56f5aSEdward Tomasz Napierala 
2633adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2634adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2635adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2636adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2637adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2638adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2639adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2640adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2641adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2642adc56f5aSEdward Tomasz Napierala 				break;
2643adc56f5aSEdward Tomasz Napierala 			}
2644adc56f5aSEdward Tomasz Napierala 
2645adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2646adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2647adc56f5aSEdward Tomasz Napierala 				goto unhold;
2648adc56f5aSEdward Tomasz Napierala 
2649adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2650adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2651adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2652adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2653adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2654adc56f5aSEdward Tomasz Napierala unhold:
2655adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2656adc56f5aSEdward Tomasz Napierala #else
2657adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2658adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2659adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2660adc56f5aSEdward Tomasz Napierala 			break;
2661adc56f5aSEdward Tomasz Napierala 			}
2662dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2663af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2664dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2665af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2666dbc42409SLawrence Stewart 			break;
26672f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
26682f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
26692f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
26702f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
26712f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
26722f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
26735a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
26742f3eb7f4SGleb Smirnoff 				break;
26752f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
26765a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
26772f3eb7f4SGleb Smirnoff 				break;
26782f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
26795a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
26802f3eb7f4SGleb Smirnoff 				break;
26812f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
26825a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
26832f3eb7f4SGleb Smirnoff 				break;
26842f3eb7f4SGleb Smirnoff 			}
26852f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
26862f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
26872f3eb7f4SGleb Smirnoff 			break;
268886a996e6SHiren Panchasara #ifdef TCPPCAP
268986a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
269086a996e6SHiren Panchasara 		case TCP_PCAP_IN:
269186a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
269286a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
269386a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
269486a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
269586a996e6SHiren Panchasara 			break;
269686a996e6SHiren Panchasara #endif
2697281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2698281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2699281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2700281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2701281a0fd4SPatrick Kelsey 			break;
2702e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
27032529f56eSJonathan T. Looney 		case TCP_LOG:
27042529f56eSJonathan T. Looney 			optval = tp->t_logstate;
27052529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
27062529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
27072529f56eSJonathan T. Looney 			break;
27082529f56eSJonathan T. Looney 		case TCP_LOGBUF:
27092529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
27102529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
27112529f56eSJonathan T. Looney 			break;
27122529f56eSJonathan T. Looney 		case TCP_LOGID:
27132529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
27142529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
27152529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
27162529f56eSJonathan T. Looney 			break;
27172529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
27182529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
27192529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
27202529f56eSJonathan T. Looney 			error = EINVAL;
27212529f56eSJonathan T. Looney 			break;
2722e24e5683SJonathan T. Looney #endif
2723b2e60773SJohn Baldwin #ifdef KERN_TLS
2724b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2725fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2726b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2727bf256782SMark Johnston 			if (error == 0)
2728bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2729bf256782SMark Johnston 				    sizeof(optval));
2730b2e60773SJohn Baldwin 			break;
2731f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2732fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2733f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2734bf256782SMark Johnston 			if (error == 0)
2735bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2736bf256782SMark Johnston 				    sizeof(optval));
2737f1f93475SJohn Baldwin 			break;
2738b2e60773SJohn Baldwin #endif
27390471a8c7SRichard Scheffenegger 		case TCP_LRD:
27400471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
27410471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
27420471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
27430471a8c7SRichard Scheffenegger 			break;
2744df8bae1dSRodney W. Grimes 		default:
27458501a69cSRobert Watson 			INP_WUNLOCK(inp);
2746df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2747df8bae1dSRodney W. Grimes 			break;
2748df8bae1dSRodney W. Grimes 		}
2749df8bae1dSRodney W. Grimes 		break;
2750df8bae1dSRodney W. Grimes 	}
2751df8bae1dSRodney W. Grimes 	return (error);
2752df8bae1dSRodney W. Grimes }
27538501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2754bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2755df8bae1dSRodney W. Grimes 
275626e30fbbSDavid Greenman /*
2757df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2758df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2759df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2760df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2761df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2762df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2763df8bae1dSRodney W. Grimes  */
2764623dce13SRobert Watson static void
2765ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2766df8bae1dSRodney W. Grimes {
2767e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2768e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2769e6e0b5ffSRobert Watson 
277097a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27718501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2772df8bae1dSRodney W. Grimes 
2773623dce13SRobert Watson 	/*
2774623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2775623dce13SRobert Watson 	 * socket is still open.
2776623dce13SRobert Watson 	 */
27778db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
27788db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2779df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2780623dce13SRobert Watson 		KASSERT(tp != NULL,
2781623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2782623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2783243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2784623dce13SRobert Watson 		KASSERT(tp != NULL,
2785623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2786623dce13SRobert Watson 	} else {
2787df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2788df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2789623dce13SRobert Watson 		tcp_usrclosed(tp);
2790ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2791f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2792f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2793df8bae1dSRodney W. Grimes 	}
2794df8bae1dSRodney W. Grimes }
2795df8bae1dSRodney W. Grimes 
2796df8bae1dSRodney W. Grimes /*
2797df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2798df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2799df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2800df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2801df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2802df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2803df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2804df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2805df8bae1dSRodney W. Grimes  */
2806623dce13SRobert Watson static void
2807ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2808df8bae1dSRodney W. Grimes {
2809df8bae1dSRodney W. Grimes 
281097a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
28118501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2812e6e0b5ffSRobert Watson 
2813df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2814df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
281509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
281609fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
281709fe6320SNavdeep Parhar #endif
2818550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2819bc65987aSKip Macy 		/* FALLTHROUGH */
2820bc65987aSKip Macy 	case TCPS_CLOSED:
2821df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2822623dce13SRobert Watson 		/*
2823623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2824623dce13SRobert Watson 		 * still open.
2825623dce13SRobert Watson 		 */
2826623dce13SRobert Watson 		KASSERT(tp != NULL,
2827623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2828df8bae1dSRodney W. Grimes 		break;
2829df8bae1dSRodney W. Grimes 
2830a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2831df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2832a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2833a0292f23SGarrett Wollman 		break;
2834a0292f23SGarrett Wollman 
2835df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
283657f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2837df8bae1dSRodney W. Grimes 		break;
2838df8bae1dSRodney W. Grimes 
2839df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
284057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2841df8bae1dSRodney W. Grimes 		break;
2842df8bae1dSRodney W. Grimes 	}
2843abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2844df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2845abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
28467c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
28477c72af87SMohan Srinivasan 			int timeout;
28487c72af87SMohan Srinivasan 
28497c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
28509077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2851b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2852b6239c4aSAndras Olah 		}
2853df8bae1dSRodney W. Grimes 	}
28547c72af87SMohan Srinivasan }
2855497057eeSRobert Watson 
2856497057eeSRobert Watson #ifdef DDB
2857497057eeSRobert Watson static void
2858497057eeSRobert Watson db_print_indent(int indent)
2859497057eeSRobert Watson {
2860497057eeSRobert Watson 	int i;
2861497057eeSRobert Watson 
2862497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2863497057eeSRobert Watson 		db_printf(" ");
2864497057eeSRobert Watson }
2865497057eeSRobert Watson 
2866497057eeSRobert Watson static void
2867497057eeSRobert Watson db_print_tstate(int t_state)
2868497057eeSRobert Watson {
2869497057eeSRobert Watson 
2870497057eeSRobert Watson 	switch (t_state) {
2871497057eeSRobert Watson 	case TCPS_CLOSED:
2872497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2873497057eeSRobert Watson 		return;
2874497057eeSRobert Watson 
2875497057eeSRobert Watson 	case TCPS_LISTEN:
2876497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2877497057eeSRobert Watson 		return;
2878497057eeSRobert Watson 
2879497057eeSRobert Watson 	case TCPS_SYN_SENT:
2880497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2881497057eeSRobert Watson 		return;
2882497057eeSRobert Watson 
2883497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2884497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2885497057eeSRobert Watson 		return;
2886497057eeSRobert Watson 
2887497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2888497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2889497057eeSRobert Watson 		return;
2890497057eeSRobert Watson 
2891497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2892497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2893497057eeSRobert Watson 		return;
2894497057eeSRobert Watson 
2895497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2896497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2897497057eeSRobert Watson 		return;
2898497057eeSRobert Watson 
2899497057eeSRobert Watson 	case TCPS_CLOSING:
2900497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2901497057eeSRobert Watson 		return;
2902497057eeSRobert Watson 
2903497057eeSRobert Watson 	case TCPS_LAST_ACK:
2904497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2905497057eeSRobert Watson 		return;
2906497057eeSRobert Watson 
2907497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2908497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2909497057eeSRobert Watson 		return;
2910497057eeSRobert Watson 
2911497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2912497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2913497057eeSRobert Watson 		return;
2914497057eeSRobert Watson 
2915497057eeSRobert Watson 	default:
2916497057eeSRobert Watson 		db_printf("unknown");
2917497057eeSRobert Watson 		return;
2918497057eeSRobert Watson 	}
2919497057eeSRobert Watson }
2920497057eeSRobert Watson 
2921497057eeSRobert Watson static void
2922497057eeSRobert Watson db_print_tflags(u_int t_flags)
2923497057eeSRobert Watson {
2924497057eeSRobert Watson 	int comma;
2925497057eeSRobert Watson 
2926497057eeSRobert Watson 	comma = 0;
2927497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2928497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2929497057eeSRobert Watson 		comma = 1;
2930497057eeSRobert Watson 	}
2931497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2932497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2933497057eeSRobert Watson 		comma = 1;
2934497057eeSRobert Watson 	}
2935497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2936497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2937497057eeSRobert Watson 		comma = 1;
2938497057eeSRobert Watson 	}
2939497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2940497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2941497057eeSRobert Watson 		comma = 1;
2942497057eeSRobert Watson 	}
2943497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2944497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2945497057eeSRobert Watson 		comma = 1;
2946497057eeSRobert Watson 	}
2947497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2948497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2949497057eeSRobert Watson 		comma = 1;
2950497057eeSRobert Watson 	}
2951497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2952497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2953497057eeSRobert Watson 		comma = 1;
2954497057eeSRobert Watson 	}
2955497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2956497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2957497057eeSRobert Watson 		comma = 1;
2958497057eeSRobert Watson 	}
2959497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2960497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2961497057eeSRobert Watson 		comma = 1;
2962497057eeSRobert Watson 	}
2963497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2964497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2965497057eeSRobert Watson 		comma = 1;
2966497057eeSRobert Watson 	}
2967497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2968497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2969497057eeSRobert Watson 		comma = 1;
2970497057eeSRobert Watson 	}
2971497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2972497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2973497057eeSRobert Watson 		comma = 1;
2974497057eeSRobert Watson 	}
2975497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2976497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2977497057eeSRobert Watson 		comma = 1;
2978497057eeSRobert Watson 	}
29793f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
29803f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
29813f169c54SRichard Scheffenegger 		comma = 1;
29823f169c54SRichard Scheffenegger 	}
2983497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2984497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2985497057eeSRobert Watson 		comma = 1;
2986497057eeSRobert Watson 	}
2987497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2988497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2989497057eeSRobert Watson 		comma = 1;
2990497057eeSRobert Watson 	}
2991497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2992497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2993497057eeSRobert Watson 		comma = 1;
2994497057eeSRobert Watson 	}
2995497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2996497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2997497057eeSRobert Watson 		comma = 1;
2998497057eeSRobert Watson 	}
2999497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
3000497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
3001497057eeSRobert Watson 		comma = 1;
3002497057eeSRobert Watson 	}
3003dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
3004dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
3005dbc42409SLawrence Stewart 		comma = 1;
3006dbc42409SLawrence Stewart 	}
3007497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
3008497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
3009497057eeSRobert Watson 		comma = 1;
3010497057eeSRobert Watson 	}
30113f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
30123f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
30133f169c54SRichard Scheffenegger 		comma = 1;
30143f169c54SRichard Scheffenegger 	}
3015497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
3016497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
3017497057eeSRobert Watson 		comma = 1;
3018497057eeSRobert Watson 	}
3019497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
3020497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
3021497057eeSRobert Watson 		comma = 1;
3022497057eeSRobert Watson 	}
3023497057eeSRobert Watson 	if (t_flags & TF_TSO) {
3024497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
3025497057eeSRobert Watson 		comma = 1;
3026497057eeSRobert Watson 	}
3027281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
3028281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
3029281a0fd4SPatrick Kelsey 		comma = 1;
3030281a0fd4SPatrick Kelsey 	}
3031497057eeSRobert Watson }
3032497057eeSRobert Watson 
3033497057eeSRobert Watson static void
30343cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
30353cf38784SMichael Tuexen {
30363cf38784SMichael Tuexen 	int comma;
30373cf38784SMichael Tuexen 
30383cf38784SMichael Tuexen 	comma = 0;
30393f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
30403f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
30413f169c54SRichard Scheffenegger 		comma = 1;
30423f169c54SRichard Scheffenegger 	}
30433f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
30443f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
30453f169c54SRichard Scheffenegger 		comma = 1;
30463f169c54SRichard Scheffenegger 	}
30473f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
30483f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
30493f169c54SRichard Scheffenegger 		comma = 1;
30503f169c54SRichard Scheffenegger 	}
30513f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
30523f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
30533f169c54SRichard Scheffenegger 		comma = 1;
30543f169c54SRichard Scheffenegger 	}
30553f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
30563f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
30573f169c54SRichard Scheffenegger 		comma = 1;
30583f169c54SRichard Scheffenegger 	}
30593cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
30603cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
30613cf38784SMichael Tuexen 		comma = 1;
30623cf38784SMichael Tuexen 	}
30633f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
30643f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
30653f169c54SRichard Scheffenegger 		comma = 1;
30663f169c54SRichard Scheffenegger 	}
30673f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
30683f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
30693f169c54SRichard Scheffenegger 		comma = 1;
30703f169c54SRichard Scheffenegger 	}
30713f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
30723f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
30733f169c54SRichard Scheffenegger 		comma = 1;
30743f169c54SRichard Scheffenegger 	}
30753f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
30763f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30773f169c54SRichard Scheffenegger 		comma = 1;
30783f169c54SRichard Scheffenegger 	}
30793cf38784SMichael Tuexen }
30803cf38784SMichael Tuexen 
30813cf38784SMichael Tuexen static void
3082497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3083497057eeSRobert Watson {
3084497057eeSRobert Watson 	int comma;
3085497057eeSRobert Watson 
3086497057eeSRobert Watson 	comma = 0;
3087497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3088497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3089497057eeSRobert Watson 		comma = 1;
3090497057eeSRobert Watson 	}
3091497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3092497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3093497057eeSRobert Watson 		comma = 1;
3094497057eeSRobert Watson 	}
3095497057eeSRobert Watson }
3096497057eeSRobert Watson 
3097497057eeSRobert Watson static void
3098497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3099497057eeSRobert Watson {
3100497057eeSRobert Watson 
3101497057eeSRobert Watson 	db_print_indent(indent);
3102497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3103497057eeSRobert Watson 
3104497057eeSRobert Watson 	indent += 2;
3105497057eeSRobert Watson 
3106497057eeSRobert Watson 	db_print_indent(indent);
3107497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3108c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3109497057eeSRobert Watson 
3110497057eeSRobert Watson 	db_print_indent(indent);
311185d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
3112e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
3113497057eeSRobert Watson 
3114497057eeSRobert Watson 	db_print_indent(indent);
3115e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
3116e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
3117497057eeSRobert Watson 
3118497057eeSRobert Watson 	db_print_indent(indent);
3119497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3120497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3121497057eeSRobert Watson 	db_printf(")\n");
3122497057eeSRobert Watson 
3123497057eeSRobert Watson 	db_print_indent(indent);
3124497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3125497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3126497057eeSRobert Watson 	db_printf(")\n");
3127497057eeSRobert Watson 
3128497057eeSRobert Watson 	db_print_indent(indent);
31293cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
31303cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
31313cf38784SMichael Tuexen 	db_printf(")\n");
31323cf38784SMichael Tuexen 
31333cf38784SMichael Tuexen 	db_print_indent(indent);
3134497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
3135497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3136497057eeSRobert Watson 
3137497057eeSRobert Watson 	db_print_indent(indent);
3138497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3139497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3140497057eeSRobert Watson 
3141497057eeSRobert Watson 	db_print_indent(indent);
3142497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3143497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3144497057eeSRobert Watson 
3145497057eeSRobert Watson 	db_print_indent(indent);
31463ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3147497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3148497057eeSRobert Watson 
3149497057eeSRobert Watson 	db_print_indent(indent);
31503ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
31511c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3152497057eeSRobert Watson 
3153497057eeSRobert Watson 	db_print_indent(indent);
31543ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
31551c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3156497057eeSRobert Watson 
3157497057eeSRobert Watson 	db_print_indent(indent);
31580c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
31590c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3160497057eeSRobert Watson 
3161497057eeSRobert Watson 	db_print_indent(indent);
31621c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
31631c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3164497057eeSRobert Watson 
3165497057eeSRobert Watson 	db_print_indent(indent);
31661c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
31671c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3168497057eeSRobert Watson 
3169497057eeSRobert Watson 	db_print_indent(indent);
3170497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
3171497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
3172497057eeSRobert Watson 	    tp->t_rttbest);
3173497057eeSRobert Watson 
3174497057eeSRobert Watson 	db_print_indent(indent);
31753ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
3176497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3177497057eeSRobert Watson 
3178497057eeSRobert Watson 	db_print_indent(indent);
3179497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3180497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3181497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3182497057eeSRobert Watson 
3183497057eeSRobert Watson 	db_print_indent(indent);
3184497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3185497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3186497057eeSRobert Watson 
3187497057eeSRobert Watson 	db_print_indent(indent);
31889f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31891a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3190497057eeSRobert Watson 
3191497057eeSRobert Watson 	db_print_indent(indent);
3192497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31933ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3194497057eeSRobert Watson 
3195497057eeSRobert Watson 	db_print_indent(indent);
31963ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31979f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3198497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3199497057eeSRobert Watson 
3200497057eeSRobert Watson 	db_print_indent(indent);
32013529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
32023529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3203497057eeSRobert Watson 
3204497057eeSRobert Watson 	db_print_indent(indent);
3205a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3206a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3207497057eeSRobert Watson 
3208497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3209497057eeSRobert Watson 
3210497057eeSRobert Watson 	db_print_indent(indent);
3211497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3212497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3213497057eeSRobert Watson }
3214497057eeSRobert Watson 
3215497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3216497057eeSRobert Watson {
3217497057eeSRobert Watson 	struct tcpcb *tp;
3218497057eeSRobert Watson 
3219497057eeSRobert Watson 	if (!have_addr) {
3220497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3221497057eeSRobert Watson 		return;
3222497057eeSRobert Watson 	}
3223497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3224497057eeSRobert Watson 
3225497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3226497057eeSRobert Watson }
3227497057eeSRobert Watson #endif
3228