xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 0773b44e829421a806a89087009b294f16ee51fd)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
5623dce13SRobert Watson  *	The Regents of the University of California.
6497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
7fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8623dce13SRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
10fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
11fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
12fa046d87SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
371fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
404b421e2dSMike Silbersack #include <sys/cdefs.h>
414b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
424b421e2dSMike Silbersack 
43497057eeSRobert Watson #include "opt_ddb.h"
441cfd4b53SBruce M Simpson #include "opt_inet.h"
45fb59c426SYoshinobu Inoue #include "opt_inet6.h"
46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
47b2e60773SJohn Baldwin #include "opt_kern_tls.h"
480cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
490cc12cc5SJoerg Wunsch 
50df8bae1dSRodney W. Grimes #include <sys/param.h>
51df8bae1dSRodney W. Grimes #include <sys/systm.h>
52adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
539077f387SGleb Smirnoff #include <sys/limits.h>
54f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5555bceb1eSRandall Stewart #include <sys/refcount.h>
56c7a82f90SGarrett Wollman #include <sys/kernel.h>
57b2e60773SJohn Baldwin #include <sys/ktls.h>
58adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5998163b98SPoul-Henning Kamp #include <sys/sysctl.h>
60df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
61fb59c426SYoshinobu Inoue #ifdef INET6
62fb59c426SYoshinobu Inoue #include <sys/domain.h>
63fb59c426SYoshinobu Inoue #endif /* INET6 */
64df8bae1dSRodney W. Grimes #include <sys/socket.h>
65df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
66df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6791421ba2SRobert Watson #include <sys/proc.h>
6891421ba2SRobert Watson #include <sys/jail.h>
69adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
70df8bae1dSRodney W. Grimes 
71497057eeSRobert Watson #ifdef DDB
72497057eeSRobert Watson #include <ddb/ddb.h>
73497057eeSRobert Watson #endif
74497057eeSRobert Watson 
75df8bae1dSRodney W. Grimes #include <net/if.h>
7676039bc8SGleb Smirnoff #include <net/if_var.h>
77df8bae1dSRodney W. Grimes #include <net/route.h>
78530c0060SRobert Watson #include <net/vnet.h>
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes #include <netinet/in.h>
815d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
82df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
83b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
84b5e8ce9fSBruce Evans #include <netinet/in_var.h>
853b0ee680SRichard Scheffenegger #include <netinet/ip.h>
86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
87fb59c426SYoshinobu Inoue #ifdef INET6
88b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
89b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
90fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
91a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
92fb59c426SYoshinobu Inoue #endif
932de3e790SGleb Smirnoff #include <netinet/tcp.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
95df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
96df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
97df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
982529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
99df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1004644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
101c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
102fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
10386a996e6SHiren Panchasara #ifdef TCPPCAP
10486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10586a996e6SHiren Panchasara #endif
106df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
10709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
108bc65987aSKip Macy #include <netinet/tcp_offload.h>
10909fe6320SNavdeep Parhar #endif
110fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
111df8bae1dSRodney W. Grimes 
112adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
113adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
114adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
115adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
116adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
117adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
118adc56f5aSEdward Tomasz Napierala 
119df8bae1dSRodney W. Grimes /*
120df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
121df8bae1dSRodney W. Grimes  */
122b287c6c7SBjoern A. Zeeb #ifdef INET
1234d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1244d77a549SAlfred Perlstein 		    struct thread *td);
125b287c6c7SBjoern A. Zeeb #endif /* INET */
126fb59c426SYoshinobu Inoue #ifdef INET6
1274d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1284d77a549SAlfred Perlstein 		    struct thread *td);
129fb59c426SYoshinobu Inoue #endif /* INET6 */
130623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
131623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
132b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1332c37256eSGarrett Wollman 
134d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
135d3b6c96bSRandall Stewart 
1362c37256eSGarrett Wollman #ifdef TCPDEBUG
1371db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1382c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1394cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1404cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1412c37256eSGarrett Wollman #else
1422c37256eSGarrett Wollman #define	TCPDEBUG0
1432c37256eSGarrett Wollman #define	TCPDEBUG1()
1442c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1452c37256eSGarrett Wollman #endif
1462c37256eSGarrett Wollman 
1472c37256eSGarrett Wollman /*
14825102351SMike Karels  * tcp_require_unique port requires a globally-unique source port for each
14925102351SMike Karels  * outgoing connection.  The default is to require the 4-tuple to be unique.
15025102351SMike Karels  */
15125102351SMike Karels VNET_DEFINE(int, tcp_require_unique_port) = 0;
15225102351SMike Karels SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
15325102351SMike Karels     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
15425102351SMike Karels     "Require globally-unique ephemeral port for outgoing connections");
15525102351SMike Karels #define	V_tcp_require_unique_port	VNET(tcp_require_unique_port)
15625102351SMike Karels 
15725102351SMike Karels /*
1582c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1592c37256eSGarrett Wollman  * and an internet control block.
1602c37256eSGarrett Wollman  */
1612c37256eSGarrett Wollman static int
162b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1632c37256eSGarrett Wollman {
164f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
165623dce13SRobert Watson 	struct tcpcb *tp = NULL;
166623dce13SRobert Watson 	int error;
1672c37256eSGarrett Wollman 	TCPDEBUG0;
1682c37256eSGarrett Wollman 
169623dce13SRobert Watson 	inp = sotoinpcb(so);
170623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1712c37256eSGarrett Wollman 	TCPDEBUG1();
1722c37256eSGarrett Wollman 
1730f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1742c37256eSGarrett Wollman 	if (error)
1752c37256eSGarrett Wollman 		goto out;
1762c37256eSGarrett Wollman 
1770f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1780f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1790f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1807669c586SGleb Smirnoff 	if (error)
1810f6385e7SGleb Smirnoff 		goto out;
1820f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1830f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1840f6385e7SGleb Smirnoff 	if (tp == NULL) {
1857669c586SGleb Smirnoff 		error = ENOBUFS;
1860f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1870f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1880f6385e7SGleb Smirnoff 		goto out;
1890f6385e7SGleb Smirnoff 	}
1900f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
1910f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1920f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1932c37256eSGarrett Wollman out:
1942c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
1955d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1960f6385e7SGleb Smirnoff 	return (error);
1972c37256eSGarrett Wollman }
1982c37256eSGarrett Wollman 
1992c37256eSGarrett Wollman /*
2003fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
201a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
202a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
203a152f8a3SRobert Watson  * inpcb state: time wait.
2042c37256eSGarrett Wollman  */
205bc725eafSRobert Watson static void
2063fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2072c37256eSGarrett Wollman {
2083fed74e9SGleb Smirnoff 	struct inpcb *inp;
2092c37256eSGarrett Wollman 	struct tcpcb *tp;
2102c37256eSGarrett Wollman 
2113fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2123fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2133fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2143fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2153fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
216953b5606SRobert Watson 
217a152f8a3SRobert Watson 	tp = intotcpcb(inp);
218a152f8a3SRobert Watson 
219ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
220623dce13SRobert Watson 		/*
221a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
222a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
223a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
224a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
225a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
226a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
227a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
228a152f8a3SRobert Watson 		 * present until timewait ends.
229623dce13SRobert Watson 		 *
230a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
231cea40c48SJulien Charbon 		 *
232cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
233dd388cfdSGleb Smirnoff 		 * four cases to consider:
234cea40c48SJulien Charbon 		 *
2353fed74e9SGleb Smirnoff 		 * #1 tcp_usr_detach is called at tcptw creation time by
236cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
237cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
2383fed74e9SGleb Smirnoff 		 * #2 tcp_usr_detach is called at tcptw creation time by
239dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
240dd388cfdSGleb Smirnoff 		 *  discarded immediately
2413fed74e9SGleb Smirnoff 		 * #3 tcp_usr_detach is called at timewait end (or reuse) by
242cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
243ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
2443fed74e9SGleb Smirnoff 		 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
245cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
246ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
247cea40c48SJulien Charbon 		 *
248cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
249623dce13SRobert Watson 		 */
250ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
251ef396441SGleb Smirnoff 			KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
252ef396441SGleb Smirnoff 			    "INP_DROPPED && tp != NULL"));
253623dce13SRobert Watson 			in_pcbdetach(inp);
2540206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
2550206cdb8SBjoern A. Zeeb 		} else {
256623dce13SRobert Watson 			in_pcbdetach(inp);
2578501a69cSRobert Watson 			INP_WUNLOCK(inp);
258623dce13SRobert Watson 		}
259623dce13SRobert Watson 	} else {
260e6e65783SRobert Watson 		/*
2611b91978fSGleb Smirnoff 		 * If the connection is not in timewait, it must be either
2621b91978fSGleb Smirnoff 		 * dropped or embryonic.
263e6e65783SRobert Watson 		 */
2641b91978fSGleb Smirnoff 		KASSERT(inp->inp_flags & INP_DROPPED ||
2651b91978fSGleb Smirnoff 		    tp->t_state < TCPS_SYN_SENT,
2661b91978fSGleb Smirnoff 		    ("%s: inp %p not dropped or embryonic", __func__, inp));
267623dce13SRobert Watson 		tcp_discardcb(tp);
268623dce13SRobert Watson 		in_pcbdetach(inp);
2690206cdb8SBjoern A. Zeeb 		in_pcbfree(inp);
270623dce13SRobert Watson 	}
271623dce13SRobert Watson }
272c78cbc7bSRobert Watson 
273b287c6c7SBjoern A. Zeeb #ifdef INET
2742c37256eSGarrett Wollman /*
2752c37256eSGarrett Wollman  * Give the socket an address.
2762c37256eSGarrett Wollman  */
2772c37256eSGarrett Wollman static int
278b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2792c37256eSGarrett Wollman {
2802c37256eSGarrett Wollman 	int error = 0;
281f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
282b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
283623dce13SRobert Watson 	struct tcpcb *tp = NULL;
284b338b1fdSMateusz Guzik #endif
2852c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2862c37256eSGarrett Wollman 
28752710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
288f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
289f96603b5SMark Johnston 		/*
290f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
291f96603b5SMark Johnston 		 */
292f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2933f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
294f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
295f161d294SMark Johnston 			return (EAFNOSUPPORT);
296f96603b5SMark Johnston 		nam->sa_family = AF_INET;
297f96603b5SMark Johnston 	}
29852710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof(*sinp))
29952710de1SPawel Jakub Dawidek 		return (EINVAL);
300f161d294SMark Johnston 
3012c37256eSGarrett Wollman 	/*
3022c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3032c37256eSGarrett Wollman 	 * to them.
3042c37256eSGarrett Wollman 	 */
305f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
30652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
30752710de1SPawel Jakub Dawidek 
308623dce13SRobert Watson 	TCPDEBUG0;
309623dce13SRobert Watson 	inp = sotoinpcb(so);
310623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3118501a69cSRobert Watson 	INP_WLOCK(inp);
312ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
313623dce13SRobert Watson 		error = EINVAL;
3142c37256eSGarrett Wollman 		goto out;
315623dce13SRobert Watson 	}
316b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
317623dce13SRobert Watson 	tp = intotcpcb(inp);
318b338b1fdSMateusz Guzik #endif
319623dce13SRobert Watson 	TCPDEBUG1();
320fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
321623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
322fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
323623dce13SRobert Watson out:
324623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3255d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3268501a69cSRobert Watson 	INP_WUNLOCK(inp);
327623dce13SRobert Watson 
328623dce13SRobert Watson 	return (error);
3292c37256eSGarrett Wollman }
330b287c6c7SBjoern A. Zeeb #endif /* INET */
3312c37256eSGarrett Wollman 
332fb59c426SYoshinobu Inoue #ifdef INET6
333fb59c426SYoshinobu Inoue static int
334b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
335fb59c426SYoshinobu Inoue {
336fb59c426SYoshinobu Inoue 	int error = 0;
337f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
338b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
339623dce13SRobert Watson 	struct tcpcb *tp = NULL;
340b338b1fdSMateusz Guzik #endif
3410ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
3424a91aa8fSMichael Tuexen 	u_char vflagsav;
343fb59c426SYoshinobu Inoue 
3440ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
345f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
346f161d294SMark Johnston 		return (EAFNOSUPPORT);
3470ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof(*sin6))
34852710de1SPawel Jakub Dawidek 		return (EINVAL);
349f161d294SMark Johnston 
350fb59c426SYoshinobu Inoue 	/*
351fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
352fb59c426SYoshinobu Inoue 	 * to them.
353fb59c426SYoshinobu Inoue 	 */
354f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
35552710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
35652710de1SPawel Jakub Dawidek 
357623dce13SRobert Watson 	TCPDEBUG0;
358623dce13SRobert Watson 	inp = sotoinpcb(so);
359623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3608501a69cSRobert Watson 	INP_WLOCK(inp);
3614a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
362ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
363623dce13SRobert Watson 		error = EINVAL;
364623dce13SRobert Watson 		goto out;
365623dce13SRobert Watson 	}
366b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
367623dce13SRobert Watson 	tp = intotcpcb(inp);
368b338b1fdSMateusz Guzik #endif
369623dce13SRobert Watson 	TCPDEBUG1();
370fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
371fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
372fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
373b287c6c7SBjoern A. Zeeb #ifdef INET
37466ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3750ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
376fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3770ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
378fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
379fb59c426SYoshinobu Inoue 
3800ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
381888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
382888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
383888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
384888973f5SMichael Tuexen 				goto out;
385888973f5SMichael Tuexen 			}
386fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
387fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
388b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
389b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
390fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
391fb59c426SYoshinobu Inoue 			goto out;
392fb59c426SYoshinobu Inoue 		}
393fb59c426SYoshinobu Inoue 	}
394b287c6c7SBjoern A. Zeeb #endif
395b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
396fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
397623dce13SRobert Watson out:
3984a91aa8fSMichael Tuexen 	if (error != 0)
3994a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
400623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
4015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4028501a69cSRobert Watson 	INP_WUNLOCK(inp);
403623dce13SRobert Watson 	return (error);
404fb59c426SYoshinobu Inoue }
405fb59c426SYoshinobu Inoue #endif /* INET6 */
406fb59c426SYoshinobu Inoue 
407b287c6c7SBjoern A. Zeeb #ifdef INET
4082c37256eSGarrett Wollman /*
4092c37256eSGarrett Wollman  * Prepare to accept connections.
4102c37256eSGarrett Wollman  */
4112c37256eSGarrett Wollman static int
412d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4132c37256eSGarrett Wollman {
4142c37256eSGarrett Wollman 	int error = 0;
415f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
416623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4172c37256eSGarrett Wollman 
418623dce13SRobert Watson 	TCPDEBUG0;
419623dce13SRobert Watson 	inp = sotoinpcb(so);
420623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4218501a69cSRobert Watson 	INP_WLOCK(inp);
422ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
423623dce13SRobert Watson 		error = EINVAL;
424623dce13SRobert Watson 		goto out;
425623dce13SRobert Watson 	}
426623dce13SRobert Watson 	tp = intotcpcb(inp);
427623dce13SRobert Watson 	TCPDEBUG1();
4280daccb9cSRobert Watson 	SOCK_LOCK(so);
4290daccb9cSRobert Watson 	error = solisten_proto_check(so);
430bd4a39ccSMark Johnston 	if (error != 0) {
431bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
432bd4a39ccSMark Johnston 		goto out;
433bd4a39ccSMark Johnston 	}
434bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
435fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
436bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
437fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
438bd4a39ccSMark Johnston 	}
4390daccb9cSRobert Watson 	if (error == 0) {
44057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
441d374e81eSRobert Watson 		solisten_proto(so, backlog);
44209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44337cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
44409fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
44509fe6320SNavdeep Parhar #endif
446bd4a39ccSMark Johnston 	} else {
447bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4480daccb9cSRobert Watson 	}
4490daccb9cSRobert Watson 	SOCK_UNLOCK(so);
450623dce13SRobert Watson 
45168bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
452281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45318a75309SPatrick Kelsey 
454623dce13SRobert Watson out:
455623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4565d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4578501a69cSRobert Watson 	INP_WUNLOCK(inp);
458623dce13SRobert Watson 	return (error);
4592c37256eSGarrett Wollman }
460b287c6c7SBjoern A. Zeeb #endif /* INET */
4612c37256eSGarrett Wollman 
462fb59c426SYoshinobu Inoue #ifdef INET6
463fb59c426SYoshinobu Inoue static int
464d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
465fb59c426SYoshinobu Inoue {
466fb59c426SYoshinobu Inoue 	int error = 0;
467f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
468623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4694a91aa8fSMichael Tuexen 	u_char vflagsav;
470fb59c426SYoshinobu Inoue 
471623dce13SRobert Watson 	TCPDEBUG0;
472623dce13SRobert Watson 	inp = sotoinpcb(so);
473623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4748501a69cSRobert Watson 	INP_WLOCK(inp);
475ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
476623dce13SRobert Watson 		error = EINVAL;
477623dce13SRobert Watson 		goto out;
478623dce13SRobert Watson 	}
4794a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
480623dce13SRobert Watson 	tp = intotcpcb(inp);
481623dce13SRobert Watson 	TCPDEBUG1();
4820daccb9cSRobert Watson 	SOCK_LOCK(so);
4830daccb9cSRobert Watson 	error = solisten_proto_check(so);
484bd4a39ccSMark Johnston 	if (error != 0) {
485bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
486bd4a39ccSMark Johnston 		goto out;
487bd4a39ccSMark Johnston 	}
488fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
489bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
490fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
49166ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
492fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
493bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
494fb59c426SYoshinobu Inoue 	}
495fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4960daccb9cSRobert Watson 	if (error == 0) {
49757f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
498d374e81eSRobert Watson 		solisten_proto(so, backlog);
49909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
50037cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
50109fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
50209fe6320SNavdeep Parhar #endif
503bd4a39ccSMark Johnston 	} else {
504bd4a39ccSMark Johnston 		solisten_proto_abort(so);
5050daccb9cSRobert Watson 	}
5060daccb9cSRobert Watson 	SOCK_UNLOCK(so);
507623dce13SRobert Watson 
50868bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
509281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
51018a75309SPatrick Kelsey 
5114a91aa8fSMichael Tuexen 	if (error != 0)
5124a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
5134a91aa8fSMichael Tuexen 
514623dce13SRobert Watson out:
515623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
5165d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
5178501a69cSRobert Watson 	INP_WUNLOCK(inp);
518623dce13SRobert Watson 	return (error);
519fb59c426SYoshinobu Inoue }
520fb59c426SYoshinobu Inoue #endif /* INET6 */
521fb59c426SYoshinobu Inoue 
522b287c6c7SBjoern A. Zeeb #ifdef INET
5232c37256eSGarrett Wollman /*
5242c37256eSGarrett Wollman  * Initiate connection to peer.
5252c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5262c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5272c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5282c37256eSGarrett Wollman  * Send initial segment on connection.
5292c37256eSGarrett Wollman  */
5302c37256eSGarrett Wollman static int
531b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5322c37256eSGarrett Wollman {
533109eb549SGleb Smirnoff 	struct epoch_tracker et;
5342c37256eSGarrett Wollman 	int error = 0;
535f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
536623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5372c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5382c37256eSGarrett Wollman 
53957bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
540f161d294SMark Johnston 	if (nam->sa_family != AF_INET)
541f161d294SMark Johnston 		return (EAFNOSUPPORT);
542e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
543e29ef13fSDon Lewis 		return (EINVAL);
544f161d294SMark Johnston 
54552710de1SPawel Jakub Dawidek 	/*
54652710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
54752710de1SPawel Jakub Dawidek 	 */
548f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
54952710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
550f161d294SMark Johnston 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
551f903a308SMichael Tuexen 		return (EACCES);
552b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
553b89e82ddSJamie Gritton 		return (error);
55475c13541SPoul-Henning Kamp 
555623dce13SRobert Watson 	TCPDEBUG0;
556623dce13SRobert Watson 	inp = sotoinpcb(so);
557623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5588501a69cSRobert Watson 	INP_WLOCK(inp);
559eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
560eb96dc33SJulien Charbon 		error = EADDRINUSE;
561eb96dc33SJulien Charbon 		goto out;
562eb96dc33SJulien Charbon 	}
563eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
564eb96dc33SJulien Charbon 		error = ECONNREFUSED;
565623dce13SRobert Watson 		goto out;
566623dce13SRobert Watson 	}
567bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
568bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
569bd4a39ccSMark Johnston 		goto out;
570bd4a39ccSMark Johnston 	}
571623dce13SRobert Watson 	tp = intotcpcb(inp);
572623dce13SRobert Watson 	TCPDEBUG1();
573c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
574b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
575c1604fe4SGleb Smirnoff 		goto out_in_epoch;
57609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
57709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
57837cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
57909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
580c1604fe4SGleb Smirnoff 		goto out_in_epoch;
58109fe6320SNavdeep Parhar #endif
58209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
58340fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5841d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5851d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
586c1604fe4SGleb Smirnoff out_in_epoch:
587109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
588623dce13SRobert Watson out:
589623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
590e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5918501a69cSRobert Watson 	INP_WUNLOCK(inp);
592623dce13SRobert Watson 	return (error);
5932c37256eSGarrett Wollman }
594b287c6c7SBjoern A. Zeeb #endif /* INET */
5952c37256eSGarrett Wollman 
596fb59c426SYoshinobu Inoue #ifdef INET6
597fb59c426SYoshinobu Inoue static int
598b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
599fb59c426SYoshinobu Inoue {
600109eb549SGleb Smirnoff 	struct epoch_tracker et;
601fb59c426SYoshinobu Inoue 	int error = 0;
602f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
603623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6040ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
6054a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
6064a91aa8fSMichael Tuexen 	u_char vflagsav;
607623dce13SRobert Watson 
608623dce13SRobert Watson 	TCPDEBUG0;
609fb59c426SYoshinobu Inoue 
6100ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
611f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
612f161d294SMark Johnston 		return (EAFNOSUPPORT);
6130ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
614e29ef13fSDon Lewis 		return (EINVAL);
615f161d294SMark Johnston 
61652710de1SPawel Jakub Dawidek 	/*
61752710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
61852710de1SPawel Jakub Dawidek 	 */
619f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
62052710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
621fb59c426SYoshinobu Inoue 
622623dce13SRobert Watson 	inp = sotoinpcb(so);
623623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6248501a69cSRobert Watson 	INP_WLOCK(inp);
6254a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6264a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
627eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
628eb96dc33SJulien Charbon 		error = EADDRINUSE;
629eb96dc33SJulien Charbon 		goto out;
630eb96dc33SJulien Charbon 	}
631eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
632eb96dc33SJulien Charbon 		error = ECONNREFUSED;
633623dce13SRobert Watson 		goto out;
634623dce13SRobert Watson 	}
635bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
636bd4a39ccSMark Johnston 		error = EINVAL;
637bd4a39ccSMark Johnston 		goto out;
638bd4a39ccSMark Johnston 	}
639623dce13SRobert Watson 	tp = intotcpcb(inp);
640623dce13SRobert Watson 	TCPDEBUG1();
641b287c6c7SBjoern A. Zeeb #ifdef INET
642fa046d87SRobert Watson 	/*
643fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
644fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
645fa046d87SRobert Watson 	 * Is this a significant problem?
646fa046d87SRobert Watson 	 */
6470ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
648fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
649fb59c426SYoshinobu Inoue 
650d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
651d46a5312SMaxim Konovalov 			error = EINVAL;
652d46a5312SMaxim Konovalov 			goto out;
653d46a5312SMaxim Konovalov 		}
6545dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6555dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6565dba6adaSMichael Tuexen 			goto out;
6575dba6adaSMichael Tuexen 		}
65833841545SHajimu UMEMOTO 
6590ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
660888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
661888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
662888973f5SMichael Tuexen 			goto out;
663888973f5SMichael Tuexen 		}
664f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
665f903a308SMichael Tuexen 			error = EACCES;
6662cf21ae5SRandall Stewart 			goto out;
6672cf21ae5SRandall Stewart 		}
668b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
669b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
670413628a7SBjoern A. Zeeb 			goto out;
6714a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6724a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
673c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
674b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
675c1604fe4SGleb Smirnoff 			goto out_in_epoch;
67609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
67709fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
678adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
67909fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
680c1604fe4SGleb Smirnoff 			goto out_in_epoch;
68109fe6320SNavdeep Parhar #endif
68240fa3e40SGleb Smirnoff 		error = tcp_output(tp);
683c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6845dba6adaSMichael Tuexen 	} else {
6855dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6865dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6875dba6adaSMichael Tuexen 			goto out;
6885dba6adaSMichael Tuexen 		}
689fb59c426SYoshinobu Inoue 	}
690b287c6c7SBjoern A. Zeeb #endif
6914a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6924a91aa8fSMichael Tuexen 		goto out;
693fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
694fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
695dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
696*0773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
697b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
698*0773b44eSGleb Smirnoff 		goto out_in_epoch;
69909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
70009fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
701adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
70209fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
703*0773b44eSGleb Smirnoff 		goto out_in_epoch;
70409fe6320SNavdeep Parhar #endif
70509fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
70640fa3e40SGleb Smirnoff 	error = tcp_output(tp);
707c1604fe4SGleb Smirnoff out_in_epoch:
708109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
709623dce13SRobert Watson out:
7101d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
7111d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
7124a91aa8fSMichael Tuexen 	/*
7134a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
7144a91aa8fSMichael Tuexen 	 * the flags we modified.
7154a91aa8fSMichael Tuexen 	 */
7164a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
7174a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
7184a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
7194a91aa8fSMichael Tuexen 	}
7204a91aa8fSMichael Tuexen 
721623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
7225d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
7238501a69cSRobert Watson 	INP_WUNLOCK(inp);
724623dce13SRobert Watson 	return (error);
725fb59c426SYoshinobu Inoue }
726fb59c426SYoshinobu Inoue #endif /* INET6 */
727fb59c426SYoshinobu Inoue 
7282c37256eSGarrett Wollman /*
7292c37256eSGarrett Wollman  * Initiate disconnect from peer.
7302c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7312c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7322c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7332c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7342c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7352c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7362c37256eSGarrett Wollman  *
7372c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7382c37256eSGarrett Wollman  */
7392c37256eSGarrett Wollman static int
7402c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7412c37256eSGarrett Wollman {
742f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
743623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7446573d758SMatt Macy 	struct epoch_tracker et;
745623dce13SRobert Watson 	int error = 0;
7462c37256eSGarrett Wollman 
747623dce13SRobert Watson 	TCPDEBUG0;
74897a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
749623dce13SRobert Watson 	inp = sotoinpcb(so);
750623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7518501a69cSRobert Watson 	INP_WLOCK(inp);
752489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
753489dcc92SJulien Charbon 		goto out;
754489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
75521367f63SSam Leffler 		error = ECONNRESET;
756623dce13SRobert Watson 		goto out;
757623dce13SRobert Watson 	}
758623dce13SRobert Watson 	tp = intotcpcb(inp);
759623dce13SRobert Watson 	TCPDEBUG1();
760623dce13SRobert Watson 	tcp_disconnect(tp);
761623dce13SRobert Watson out:
762623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7635d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7648501a69cSRobert Watson 	INP_WUNLOCK(inp);
76597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
766623dce13SRobert Watson 	return (error);
7672c37256eSGarrett Wollman }
7682c37256eSGarrett Wollman 
769b287c6c7SBjoern A. Zeeb #ifdef INET
7702c37256eSGarrett Wollman /*
7718296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7728296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7732c37256eSGarrett Wollman  */
7742c37256eSGarrett Wollman static int
77557bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7762c37256eSGarrett Wollman {
7772c37256eSGarrett Wollman 	int error = 0;
778f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
779b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7801db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
781b338b1fdSMateusz Guzik #endif
78226ef6ac4SDon Lewis 	struct in_addr addr;
78326ef6ac4SDon Lewis 	in_port_t port = 0;
7841db24ffbSJonathan Lemon 	TCPDEBUG0;
7852c37256eSGarrett Wollman 
7863d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7873d2d3ef4SRobert Watson 		return (ECONNABORTED);
788f76fcf6dSJeffrey Hsu 
789f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
790623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7918501a69cSRobert Watson 	INP_WLOCK(inp);
792ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7933d2d3ef4SRobert Watson 		error = ECONNABORTED;
794623dce13SRobert Watson 		goto out;
795623dce13SRobert Watson 	}
796b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7971db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
798b338b1fdSMateusz Guzik #endif
7991db24ffbSJonathan Lemon 	TCPDEBUG1();
800f76fcf6dSJeffrey Hsu 
801f76fcf6dSJeffrey Hsu 	/*
80254d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
80326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
80426ef6ac4SDon Lewis 	 * release the lock.
805f76fcf6dSJeffrey Hsu 	 */
80626ef6ac4SDon Lewis 	port = inp->inp_fport;
80726ef6ac4SDon Lewis 	addr = inp->inp_faddr;
808f76fcf6dSJeffrey Hsu 
809623dce13SRobert Watson out:
810623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8115d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8128501a69cSRobert Watson 	INP_WUNLOCK(inp);
81326ef6ac4SDon Lewis 	if (error == 0)
81426ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
81526ef6ac4SDon Lewis 	return error;
8162c37256eSGarrett Wollman }
817b287c6c7SBjoern A. Zeeb #endif /* INET */
8182c37256eSGarrett Wollman 
819fb59c426SYoshinobu Inoue #ifdef INET6
820fb59c426SYoshinobu Inoue static int
821fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
822fb59c426SYoshinobu Inoue {
823f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
824fb59c426SYoshinobu Inoue 	int error = 0;
825b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
8261db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
827b338b1fdSMateusz Guzik #endif
82826ef6ac4SDon Lewis 	struct in_addr addr;
82926ef6ac4SDon Lewis 	struct in6_addr addr6;
8306573d758SMatt Macy 	struct epoch_tracker et;
83126ef6ac4SDon Lewis 	in_port_t port = 0;
83226ef6ac4SDon Lewis 	int v4 = 0;
8331db24ffbSJonathan Lemon 	TCPDEBUG0;
834fb59c426SYoshinobu Inoue 
835b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
836b4470c16SRobert Watson 		return (ECONNABORTED);
837f76fcf6dSJeffrey Hsu 
838f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
839623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
84097a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8418501a69cSRobert Watson 	INP_WLOCK(inp);
842ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
84321367f63SSam Leffler 		error = ECONNABORTED;
844623dce13SRobert Watson 		goto out;
845623dce13SRobert Watson 	}
846b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
8471db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
848b338b1fdSMateusz Guzik #endif
8491db24ffbSJonathan Lemon 	TCPDEBUG1();
850623dce13SRobert Watson 
85126ef6ac4SDon Lewis 	/*
85226ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
85326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
85426ef6ac4SDon Lewis 	 * release the lock.
85526ef6ac4SDon Lewis 	 */
85626ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
85726ef6ac4SDon Lewis 		v4 = 1;
85826ef6ac4SDon Lewis 		port = inp->inp_fport;
85926ef6ac4SDon Lewis 		addr = inp->inp_faddr;
86026ef6ac4SDon Lewis 	} else {
86126ef6ac4SDon Lewis 		port = inp->inp_fport;
86226ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
86326ef6ac4SDon Lewis 	}
86426ef6ac4SDon Lewis 
865623dce13SRobert Watson out:
866623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8675d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8688501a69cSRobert Watson 	INP_WUNLOCK(inp);
86997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
87026ef6ac4SDon Lewis 	if (error == 0) {
87126ef6ac4SDon Lewis 		if (v4)
87226ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
87326ef6ac4SDon Lewis 		else
87426ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
87526ef6ac4SDon Lewis 	}
87626ef6ac4SDon Lewis 	return error;
877fb59c426SYoshinobu Inoue }
878fb59c426SYoshinobu Inoue #endif /* INET6 */
879f76fcf6dSJeffrey Hsu 
880f76fcf6dSJeffrey Hsu /*
8812c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8822c37256eSGarrett Wollman  */
8832c37256eSGarrett Wollman static int
8842c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8852c37256eSGarrett Wollman {
8862c37256eSGarrett Wollman 	int error = 0;
887f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
888623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8896573d758SMatt Macy 	struct epoch_tracker et;
8902c37256eSGarrett Wollman 
891623dce13SRobert Watson 	TCPDEBUG0;
892623dce13SRobert Watson 	inp = sotoinpcb(so);
893623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8948501a69cSRobert Watson 	INP_WLOCK(inp);
895ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
8960af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
8970af4ce45SGleb Smirnoff 		return (ECONNRESET);
898623dce13SRobert Watson 	}
8990af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
9000af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
901623dce13SRobert Watson 	TCPDEBUG1();
9022c37256eSGarrett Wollman 	socantsendmore(so);
903623dce13SRobert Watson 	tcp_usrclosed(tp);
904ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
905f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
906623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
9075d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
908f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
90997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
910623dce13SRobert Watson 
911623dce13SRobert Watson 	return (error);
9122c37256eSGarrett Wollman }
9132c37256eSGarrett Wollman 
9142c37256eSGarrett Wollman /*
9152c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
9162c37256eSGarrett Wollman  */
9172c37256eSGarrett Wollman static int
9182c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
9192c37256eSGarrett Wollman {
920109eb549SGleb Smirnoff 	struct epoch_tracker et;
921f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
922623dce13SRobert Watson 	struct tcpcb *tp = NULL;
923f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
9242c37256eSGarrett Wollman 
925623dce13SRobert Watson 	TCPDEBUG0;
926623dce13SRobert Watson 	inp = sotoinpcb(so);
927623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
9288501a69cSRobert Watson 	INP_WLOCK(inp);
929ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
93037a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
93137a7f557SGleb Smirnoff 		return (ECONNRESET);
932623dce13SRobert Watson 	}
93337a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
93437a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
935623dce13SRobert Watson 	TCPDEBUG1();
936281a0fd4SPatrick Kelsey 	/*
937281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
938281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
939281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
940281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
941281a0fd4SPatrick Kelsey 	 * expires.
942281a0fd4SPatrick Kelsey 	 */
94368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
944281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
945281a0fd4SPatrick Kelsey 		goto out;
94609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
94709fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
94809fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
949460cf046SNavdeep Parhar 	else
95009fe6320SNavdeep Parhar #endif
951f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
952623dce13SRobert Watson out:
953623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9545d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
955f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
956f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
957623dce13SRobert Watson 	return (error);
9582c37256eSGarrett Wollman }
9592c37256eSGarrett Wollman 
9602c37256eSGarrett Wollman /*
9612c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9629c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9639c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9649c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9659c9906e9SPeter Wemm  * generally are caller-frees.
9662c37256eSGarrett Wollman  */
9672c37256eSGarrett Wollman static int
96857bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
969b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9702c37256eSGarrett Wollman {
97197a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9722c37256eSGarrett Wollman 	int error = 0;
973f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
974623dce13SRobert Watson 	struct tcpcb *tp = NULL;
975888973f5SMichael Tuexen #ifdef INET
97651e08d53SMichael Tuexen #ifdef INET6
97751e08d53SMichael Tuexen 	struct sockaddr_in sin;
97851e08d53SMichael Tuexen #endif
97951e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
980888973f5SMichael Tuexen #endif
981fb59c426SYoshinobu Inoue #ifdef INET6
982fb59c426SYoshinobu Inoue 	int isipv6;
983fb59c426SYoshinobu Inoue #endif
9844a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9854a91aa8fSMichael Tuexen 	u_char vflagsav;
9864a91aa8fSMichael Tuexen 	bool restoreflags;
9879c9906e9SPeter Wemm 	TCPDEBUG0;
9882c37256eSGarrett Wollman 
989d8acd268SMark Johnston 	if (control != NULL) {
990d8acd268SMark Johnston 		/* TCP doesn't do control messages (rights, creds, etc) */
991d8acd268SMark Johnston 		if (control->m_len) {
992d8acd268SMark Johnston 			m_freem(control);
9934287aa56SGleb Smirnoff 			return (EINVAL);
994d8acd268SMark Johnston 		}
995d8acd268SMark Johnston 		m_freem(control);	/* empty control, just free it */
996d8acd268SMark Johnston 	}
9974287aa56SGleb Smirnoff 
9984287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9994287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
10004287aa56SGleb Smirnoff 	INP_WLOCK(inp);
10014287aa56SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
10024287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
10034287aa56SGleb Smirnoff 			m_freem(m);
10044287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
10054287aa56SGleb Smirnoff 		return (ECONNRESET);
10064287aa56SGleb Smirnoff 	}
10074287aa56SGleb Smirnoff 
10084287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
10094287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
10104287aa56SGleb Smirnoff 	restoreflags = false;
10114287aa56SGleb Smirnoff 	tp = intotcpcb(inp);
10124287aa56SGleb Smirnoff 
10134287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
10147d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
10157d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
1016d3b6c96bSRandall Stewart 		goto out;
10177d2608a5SMark Johnston 
10189c9906e9SPeter Wemm 	TCPDEBUG1();
1019888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1020bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
1021bd4a39ccSMark Johnston 			error = EINVAL;
1022bd4a39ccSMark Johnston 			goto out;
1023bd4a39ccSMark Johnston 		}
1024888973f5SMichael Tuexen 		switch (nam->sa_family) {
1025888973f5SMichael Tuexen #ifdef INET
1026888973f5SMichael Tuexen 		case AF_INET:
1027888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
1028888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1029888973f5SMichael Tuexen 				error = EINVAL;
1030888973f5SMichael Tuexen 				goto out;
1031888973f5SMichael Tuexen 			}
1032888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
1033888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1034888973f5SMichael Tuexen 				goto out;
1035888973f5SMichael Tuexen 			}
1036888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1037888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1038888973f5SMichael Tuexen 				goto out;
1039888973f5SMichael Tuexen 			}
1040f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
1041f903a308SMichael Tuexen 				error = EACCES;
10422cf21ae5SRandall Stewart 				goto out;
10432cf21ae5SRandall Stewart 			}
1044888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
10457d2608a5SMark Johnston 			    &sinp->sin_addr)))
1046888973f5SMichael Tuexen 				goto out;
1047888973f5SMichael Tuexen #ifdef INET6
1048888973f5SMichael Tuexen 			isipv6 = 0;
1049888973f5SMichael Tuexen #endif
1050888973f5SMichael Tuexen 			break;
1051888973f5SMichael Tuexen #endif /* INET */
1052888973f5SMichael Tuexen #ifdef INET6
1053888973f5SMichael Tuexen 		case AF_INET6:
1054888973f5SMichael Tuexen 		{
10550ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1056888973f5SMichael Tuexen 
10570ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10580ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1059888973f5SMichael Tuexen 				error = EINVAL;
1060888973f5SMichael Tuexen 				goto out;
1061888973f5SMichael Tuexen 			}
1062e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1063e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1064e240ce42SMichael Tuexen 				goto out;
1065e240ce42SMichael Tuexen 			}
10660ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1067888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1068888973f5SMichael Tuexen 				goto out;
1069888973f5SMichael Tuexen 			}
10700ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1071888973f5SMichael Tuexen #ifdef INET
1072888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1073888973f5SMichael Tuexen 					error = EINVAL;
1074888973f5SMichael Tuexen 					goto out;
1075888973f5SMichael Tuexen 				}
1076888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1077888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1078888973f5SMichael Tuexen 					goto out;
1079888973f5SMichael Tuexen 				}
10804a91aa8fSMichael Tuexen 				restoreflags = true;
1081888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1082888973f5SMichael Tuexen 				sinp = &sin;
10830ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1084888973f5SMichael Tuexen 				if (IN_MULTICAST(
1085888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1086888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1087888973f5SMichael Tuexen 					goto out;
1088888973f5SMichael Tuexen 				}
1089888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10907d2608a5SMark Johnston 				    &sinp->sin_addr)))
1091888973f5SMichael Tuexen 					goto out;
1092888973f5SMichael Tuexen 				isipv6 = 0;
1093888973f5SMichael Tuexen #else /* !INET */
1094888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1095888973f5SMichael Tuexen 				goto out;
1096888973f5SMichael Tuexen #endif /* INET */
1097888973f5SMichael Tuexen 			} else {
1098888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1099888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1100888973f5SMichael Tuexen 					goto out;
1101888973f5SMichael Tuexen 				}
11024a91aa8fSMichael Tuexen 				restoreflags = true;
1103888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1104888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1105888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
11067d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1107888973f5SMichael Tuexen 					goto out;
1108888973f5SMichael Tuexen 				isipv6 = 1;
1109888973f5SMichael Tuexen 			}
1110888973f5SMichael Tuexen 			break;
1111888973f5SMichael Tuexen 		}
1112888973f5SMichael Tuexen #endif /* INET6 */
1113888973f5SMichael Tuexen 		default:
1114888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1115888973f5SMichael Tuexen 			goto out;
1116888973f5SMichael Tuexen 		}
1117888973f5SMichael Tuexen 	}
11182c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1119651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
11207d2608a5SMark Johnston 		m = NULL;
11212c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1122bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1123bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1124bd4a39ccSMark Johnston 
11252c37256eSGarrett Wollman 			/*
11262c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11272c37256eSGarrett Wollman 			 * initialize window to default value, and
11280c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11292c37256eSGarrett Wollman 			 */
1130fb59c426SYoshinobu Inoue #ifdef INET6
1131fb59c426SYoshinobu Inoue 			if (isipv6)
1132b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1133fb59c426SYoshinobu Inoue #endif /* INET6 */
1134b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1135b287c6c7SBjoern A. Zeeb 			else
1136b287c6c7SBjoern A. Zeeb #endif
1137b287c6c7SBjoern A. Zeeb #ifdef INET
1138888973f5SMichael Tuexen 				error = tcp_connect(tp,
1139888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1140b287c6c7SBjoern A. Zeeb #endif
11414a91aa8fSMichael Tuexen 			/*
11424a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11434a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11444a91aa8fSMichael Tuexen 			 * operations fail.
11454a91aa8fSMichael Tuexen 			 */
11464a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11474a91aa8fSMichael Tuexen 				restoreflags = false;
11484a91aa8fSMichael Tuexen 
11497d2608a5SMark Johnston 			if (error) {
11507d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11517d2608a5SMark Johnston 				sbflush(&so->so_snd);
11522c37256eSGarrett Wollman 				goto out;
11537d2608a5SMark Johnston 			}
1154c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1155c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
115618a75309SPatrick Kelsey 			else {
11572c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11582c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11592c37256eSGarrett Wollman 			}
1160c560df6fSPatrick Kelsey 		}
11612c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11622c37256eSGarrett Wollman 			/*
11632c37256eSGarrett Wollman 			 * Close the send side of the connection after
11642c37256eSGarrett Wollman 			 * the data is sent.
11652c37256eSGarrett Wollman 			 */
11662c37256eSGarrett Wollman 			socantsendmore(so);
1167623dce13SRobert Watson 			tcp_usrclosed(tp);
11682c37256eSGarrett Wollman 		}
1169e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1170e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1171e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1172e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1173e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1174e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1175e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1176e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1177e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1178e854dd38SRandall Stewart 		}
11792cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11802cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1181b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1182b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1183f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1184b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1185b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1186b0acefa8SBill Fenner 		}
11872c37256eSGarrett Wollman 	} else {
1188623dce13SRobert Watson 		/*
1189623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1190623dce13SRobert Watson 		 */
1191d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11922c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1193d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11942c37256eSGarrett Wollman 			error = ENOBUFS;
11952c37256eSGarrett Wollman 			goto out;
11962c37256eSGarrett Wollman 		}
11972c37256eSGarrett Wollman 		/*
11982c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11992c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
12002c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
12012c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
12022c37256eSGarrett Wollman 		 * of data past the urgent section.
12032c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
12042c37256eSGarrett Wollman 		 */
1205651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1206d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
12077d2608a5SMark Johnston 		m = NULL;
1208ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1209ef53690bSGarrett Wollman 			/*
1210ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1211ef53690bSGarrett Wollman 			 * initialize window to default value, and
12120c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1213ef53690bSGarrett Wollman 			 */
121418a75309SPatrick Kelsey 
1215c560df6fSPatrick Kelsey 			/*
1216c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1217c560df6fSPatrick Kelsey 			 */
1218c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1219c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1220fb59c426SYoshinobu Inoue #ifdef INET6
1221fb59c426SYoshinobu Inoue 			if (isipv6)
1222b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1223fb59c426SYoshinobu Inoue #endif /* INET6 */
1224b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1225b287c6c7SBjoern A. Zeeb 			else
1226b287c6c7SBjoern A. Zeeb #endif
1227b287c6c7SBjoern A. Zeeb #ifdef INET
1228888973f5SMichael Tuexen 				error = tcp_connect(tp,
1229888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1230b287c6c7SBjoern A. Zeeb #endif
12314a91aa8fSMichael Tuexen 			/*
12324a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12334a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12344a91aa8fSMichael Tuexen 			 * operations fail.
12354a91aa8fSMichael Tuexen 			 */
12364a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12374a91aa8fSMichael Tuexen 				restoreflags = false;
12384a91aa8fSMichael Tuexen 
12397d2608a5SMark Johnston 			if (error != 0) {
12407d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
12417d2608a5SMark Johnston 				sbflush(&so->so_snd);
1242ef53690bSGarrett Wollman 				goto out;
12437d2608a5SMark Johnston 			}
1244ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1245ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1246623dce13SRobert Watson 		}
1247300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12487d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
12492cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1250f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
12512cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12522c37256eSGarrett Wollman 		}
12532cbcd3c1SGleb Smirnoff 	}
12542529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12552529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12562529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12572529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12582529f56eSJonathan T. Looney 	    0, NULL, false);
12597d2608a5SMark Johnston 
1260d1401c90SRobert Watson out:
12614a91aa8fSMichael Tuexen 	/*
12627d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
12637d2608a5SMark Johnston 	 * responsible for freeing memory.
12647d2608a5SMark Johnston 	 */
12657d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12667d2608a5SMark Johnston 		m_freem(m);
12677d2608a5SMark Johnston 
12687d2608a5SMark Johnston 	/*
12694a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12704a91aa8fSMichael Tuexen 	 * restore the original flags.
12714a91aa8fSMichael Tuexen 	 */
12724a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12734a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12744a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12754a91aa8fSMichael Tuexen 	}
1276d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12772c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12785d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12795d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1280f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
128197a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
128273fddedaSPeter Grehan 	return (error);
12832c37256eSGarrett Wollman }
12842c37256eSGarrett Wollman 
12852cbcd3c1SGleb Smirnoff static int
12862cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12872cbcd3c1SGleb Smirnoff {
1288109eb549SGleb Smirnoff 	struct epoch_tracker et;
12892cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12902cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12912cbcd3c1SGleb Smirnoff 	int error;
12922cbcd3c1SGleb Smirnoff 
12932cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12942cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
12952cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12962cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
129782334850SJohn Baldwin 		mb_free_notready(m, count);
12982cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12992cbcd3c1SGleb Smirnoff 	}
13002cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
13012cbcd3c1SGleb Smirnoff 
13022cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
13032cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
13042cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1305f64dc2abSGleb Smirnoff 	if (error) {
13062cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1307f64dc2abSGleb Smirnoff 		return (error);
1308f64dc2abSGleb Smirnoff 	}
1309f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1310f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1311f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
13122cbcd3c1SGleb Smirnoff 
13132cbcd3c1SGleb Smirnoff 	return (error);
13142cbcd3c1SGleb Smirnoff }
13152cbcd3c1SGleb Smirnoff 
13162c37256eSGarrett Wollman /*
1317a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
13182c37256eSGarrett Wollman  */
1319ac45e92fSRobert Watson static void
13202c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
13212c37256eSGarrett Wollman {
1322f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1323a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13246573d758SMatt Macy 	struct epoch_tracker et;
1325623dce13SRobert Watson 	TCPDEBUG0;
1326c78cbc7bSRobert Watson 
1327ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1328c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1329c78cbc7bSRobert Watson 
133097a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13318501a69cSRobert Watson 	INP_WLOCK(inp);
1332c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1333c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1334c78cbc7bSRobert Watson 
1335c78cbc7bSRobert Watson 	/*
1336a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1337c78cbc7bSRobert Watson 	 */
1338ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1339ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1340c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1341a152f8a3SRobert Watson 		TCPDEBUG1();
13428fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13438fa799bdSJonathan T. Looney 		if (tp == NULL)
13448fa799bdSJonathan T. Looney 			goto dropped;
1345a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13465d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1347c78cbc7bSRobert Watson 	}
1348ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1349d8596171SGleb Smirnoff 		soref(so);
1350ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1351a152f8a3SRobert Watson 	}
13528501a69cSRobert Watson 	INP_WUNLOCK(inp);
13538fa799bdSJonathan T. Looney dropped:
135497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1355a152f8a3SRobert Watson }
1356a152f8a3SRobert Watson 
1357a152f8a3SRobert Watson /*
1358a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1359a152f8a3SRobert Watson  */
1360a152f8a3SRobert Watson static void
1361a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1362a152f8a3SRobert Watson {
1363a152f8a3SRobert Watson 	struct inpcb *inp;
1364a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13656573d758SMatt Macy 	struct epoch_tracker et;
1366a152f8a3SRobert Watson 	TCPDEBUG0;
1367a152f8a3SRobert Watson 
1368a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1369a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1370a152f8a3SRobert Watson 
137197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13728501a69cSRobert Watson 	INP_WLOCK(inp);
1373a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1374a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1375a152f8a3SRobert Watson 
1376a152f8a3SRobert Watson 	/*
1377a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1378a152f8a3SRobert Watson 	 * a disconnect.
1379a152f8a3SRobert Watson 	 */
1380ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1381ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1382a152f8a3SRobert Watson 		tp = intotcpcb(inp);
138374703901SGleb Smirnoff 		tp->t_flags |= TF_CLOSED;
1384a152f8a3SRobert Watson 		TCPDEBUG1();
1385a152f8a3SRobert Watson 		tcp_disconnect(tp);
1386a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
13875d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1388a152f8a3SRobert Watson 	}
1389ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1390d8596171SGleb Smirnoff 		soref(so);
1391ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1392a152f8a3SRobert Watson 	}
13938501a69cSRobert Watson 	INP_WUNLOCK(inp);
139497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13952c37256eSGarrett Wollman }
13962c37256eSGarrett Wollman 
1397d3b6c96bSRandall Stewart static int
1398d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1399d3b6c96bSRandall Stewart {
1400d3b6c96bSRandall Stewart 	/*
1401d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1402d3b6c96bSRandall Stewart 	 * specified then it does not always support
1403d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1404d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1405d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1406d3b6c96bSRandall Stewart 	 */
1407d3b6c96bSRandall Stewart 	int ret = 0;
1408d3b6c96bSRandall Stewart 
1409d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1410d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1411d3b6c96bSRandall Stewart 	}
1412d3b6c96bSRandall Stewart 	return (ret);
1413d3b6c96bSRandall Stewart }
1414d3b6c96bSRandall Stewart 
14152c37256eSGarrett Wollman /*
14162c37256eSGarrett Wollman  * Receive out-of-band data.
14172c37256eSGarrett Wollman  */
14182c37256eSGarrett Wollman static int
14192c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
14202c37256eSGarrett Wollman {
14212c37256eSGarrett Wollman 	int error = 0;
1422f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1423623dce13SRobert Watson 	struct tcpcb *tp = NULL;
14242c37256eSGarrett Wollman 
1425623dce13SRobert Watson 	TCPDEBUG0;
1426623dce13SRobert Watson 	inp = sotoinpcb(so);
1427623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
14288501a69cSRobert Watson 	INP_WLOCK(inp);
1429ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
143021367f63SSam Leffler 		error = ECONNRESET;
1431623dce13SRobert Watson 		goto out;
1432623dce13SRobert Watson 	}
1433623dce13SRobert Watson 	tp = intotcpcb(inp);
1434d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1435d3b6c96bSRandall Stewart 	if (error) {
1436d3b6c96bSRandall Stewart 		goto out;
1437d3b6c96bSRandall Stewart 	}
1438623dce13SRobert Watson 	TCPDEBUG1();
14392c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1440c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
14414cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
14424cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
14432c37256eSGarrett Wollman 		error = EINVAL;
14442c37256eSGarrett Wollman 		goto out;
14452c37256eSGarrett Wollman 	}
14462c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
14472c37256eSGarrett Wollman 		error = EWOULDBLOCK;
14482c37256eSGarrett Wollman 		goto out;
14492c37256eSGarrett Wollman 	}
14502c37256eSGarrett Wollman 	m->m_len = 1;
14512c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
14522c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
14532c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1454623dce13SRobert Watson 
1455623dce13SRobert Watson out:
1456623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14575d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14588501a69cSRobert Watson 	INP_WUNLOCK(inp);
1459623dce13SRobert Watson 	return (error);
14602c37256eSGarrett Wollman }
14612c37256eSGarrett Wollman 
1462b287c6c7SBjoern A. Zeeb #ifdef INET
1463e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1464e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1465e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1466e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1467e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1468e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1469e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1470e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1471e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1472e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1473e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1474e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1475e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1476e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1477e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1478e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1479e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1480e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1481e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1482e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1483e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1484e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1485e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1486e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14872c37256eSGarrett Wollman };
1488b287c6c7SBjoern A. Zeeb #endif /* INET */
1489df8bae1dSRodney W. Grimes 
1490fb59c426SYoshinobu Inoue #ifdef INET6
1491e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1492e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1493e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1494e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1495e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1496e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1497e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1498e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1499e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1500e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1501e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1502e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1503e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1504e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1505e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1506e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1507e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1508e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1509e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1510e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1511e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1512e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1513e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1514e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1515fb59c426SYoshinobu Inoue };
1516fb59c426SYoshinobu Inoue #endif /* INET6 */
1517fb59c426SYoshinobu Inoue 
1518b287c6c7SBjoern A. Zeeb #ifdef INET
1519a0292f23SGarrett Wollman /*
1520a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1521a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
15225200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
15235200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
15245200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
15255200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1526a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1527a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1528a0292f23SGarrett Wollman  */
15290312fbe9SPoul-Henning Kamp static int
1530ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1531a0292f23SGarrett Wollman {
1532a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1533a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
15345200e00eSIan Dowse 	struct in_addr laddr;
15355200e00eSIan Dowse 	u_short lport;
1536c3229e05SDavid Greenman 	int error;
1537a0292f23SGarrett Wollman 
1538c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
15398501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1540fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1541623dce13SRobert Watson 
154225102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
15434616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15444616026fSErmal Luçi 		if (error)
1545fa046d87SRobert Watson 			goto out;
1546a0292f23SGarrett Wollman 	}
1547a0292f23SGarrett Wollman 
1548a0292f23SGarrett Wollman 	/*
1549a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1550a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1551a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1552a0292f23SGarrett Wollman 	 */
15535200e00eSIan Dowse 	laddr = inp->inp_laddr;
15545200e00eSIan Dowse 	lport = inp->inp_lport;
15555200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1556b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
15575200e00eSIan Dowse 	if (error && oinp == NULL)
1558fa046d87SRobert Watson 		goto out;
1559fa046d87SRobert Watson 	if (oinp) {
1560fa046d87SRobert Watson 		error = EADDRINUSE;
1561fa046d87SRobert Watson 		goto out;
1562fa046d87SRobert Watson 	}
156325102351SMike Karels 	/* Handle initial bind if it hadn't been done in advance. */
156425102351SMike Karels 	if (inp->inp_lport == 0) {
156525102351SMike Karels 		inp->inp_lport = lport;
156625102351SMike Karels 		if (in_pcbinshash(inp) != 0) {
156725102351SMike Karels 			inp->inp_lport = 0;
156825102351SMike Karels 			error = EAGAIN;
156925102351SMike Karels 			goto out;
157025102351SMike Karels 		}
157125102351SMike Karels 	}
15725200e00eSIan Dowse 	inp->inp_laddr = laddr;
157315bd2b43SDavid Greenman 	in_pcbrehash(inp);
1574fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1575a0292f23SGarrett Wollman 
1576087b55eaSAndre Oppermann 	/*
1577087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1578087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1579087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1580087b55eaSAndre Oppermann 	 */
1581a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15829b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1583a0292f23SGarrett Wollman 		tp->request_r_scale++;
1584a0292f23SGarrett Wollman 
1585a0292f23SGarrett Wollman 	soisconnecting(so);
158678b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
158757f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15888e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15898e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15908e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1591a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1592a45d2726SAndras Olah 
1593a0292f23SGarrett Wollman 	return 0;
1594fa046d87SRobert Watson 
1595fa046d87SRobert Watson out:
1596fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1597fa046d87SRobert Watson 	return (error);
1598a0292f23SGarrett Wollman }
1599b287c6c7SBjoern A. Zeeb #endif /* INET */
1600a0292f23SGarrett Wollman 
1601fb59c426SYoshinobu Inoue #ifdef INET6
1602fb59c426SYoshinobu Inoue static int
1603ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1604fb59c426SYoshinobu Inoue {
1605a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1606fb59c426SYoshinobu Inoue 	int error;
1607fb59c426SYoshinobu Inoue 
16088501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1609fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1610623dce13SRobert Watson 
161125102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
16124616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
16134616026fSErmal Luçi 		if (error)
1614fa046d87SRobert Watson 			goto out;
1615fb59c426SYoshinobu Inoue 	}
1616a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1617a7e201bbSAndrey V. Elsukov 	if (error != 0)
1618b598155aSRobert Watson 		goto out;
1619fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1620fb59c426SYoshinobu Inoue 
1621fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1622fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1623970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1624fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1625fb59c426SYoshinobu Inoue 
1626a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
162778b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
162857f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
16298e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
16308e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
16318e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1632fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1633fb59c426SYoshinobu Inoue 
1634fb59c426SYoshinobu Inoue 	return 0;
1635fa046d87SRobert Watson 
1636fa046d87SRobert Watson out:
1637fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1638fa046d87SRobert Watson 	return error;
1639fb59c426SYoshinobu Inoue }
1640fb59c426SYoshinobu Inoue #endif /* INET6 */
1641fb59c426SYoshinobu Inoue 
1642cfe8b629SGarrett Wollman /*
1643b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1644b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1645b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1646b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1647b8af5dfaSRobert Watson  * from Linux.
1648b8af5dfaSRobert Watson  */
1649b8af5dfaSRobert Watson static void
1650ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1651b8af5dfaSRobert Watson {
1652b8af5dfaSRobert Watson 
16538501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1654b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1655b8af5dfaSRobert Watson 
1656b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1657b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1658b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
16593529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1660b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1661b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1662b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1663b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1664b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1665b8af5dfaSRobert Watson 	}
16663f169c54SRichard Scheffenegger 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
16675a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
16681baaf834SBruce M Simpson 
166943d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
16703ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
16711baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
16721baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16731baaf834SBruce M Simpson 
1674b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1675b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1676b8af5dfaSRobert Watson 
1677b8af5dfaSRobert Watson 	/*
1678b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1679b8af5dfaSRobert Watson 	 */
1680c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1681535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1682b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16831c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1684535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
168543d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
168643d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1687f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1688f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1689f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1690a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1691a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1692a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1693a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1694a6456410SNavdeep Parhar 	}
1695a6456410SNavdeep Parhar #endif
1696b8af5dfaSRobert Watson }
1697b8af5dfaSRobert Watson 
1698b8af5dfaSRobert Watson /*
16991e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
17001e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
17011e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
17021e8f5ffaSRobert Watson  * option.
1703cfe8b629SGarrett Wollman  */
1704bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
17058501a69cSRobert Watson 	INP_WLOCK(inp);							\
1706ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
17078501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1708bac5bedfSConrad Meyer 		cleanup;						\
17091e8f5ffaSRobert Watson 		return (ECONNRESET);					\
17101e8f5ffaSRobert Watson 	}								\
17111e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
17121e8f5ffaSRobert Watson } while(0)
1713bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
17141e8f5ffaSRobert Watson 
1715fd7daa72SMichael Tuexen int
1716fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1717df8bae1dSRodney W. Grimes {
1718fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1719fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1720fc4d53ccSGleb Smirnoff 	int error = 0;
1721df8bae1dSRodney W. Grimes 
1722fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
17233b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
1724fd7daa72SMichael Tuexen 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1725fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1726fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1727fc4d53ccSGleb Smirnoff 
1728cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
17293b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1730fb59c426SYoshinobu Inoue #ifdef INET6
1731de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1732fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1733de156263SGleb Smirnoff #endif
1734de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1735de156263SGleb Smirnoff 		else
1736de156263SGleb Smirnoff #endif
1737de156263SGleb Smirnoff #ifdef INET
1738fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1739de156263SGleb Smirnoff #endif
17405dff1c38SMichael Tuexen 		/*
1741de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1742de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1743de156263SGleb Smirnoff 		 * IP level returned.
17445dff1c38SMichael Tuexen 		 */
1745de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1746de156263SGleb Smirnoff #ifdef INET6
1747de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1748de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1749de156263SGleb Smirnoff 				return (error);
1750de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1751de156263SGleb Smirnoff 			case IPV6_TCLASS:
1752de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1753de156263SGleb Smirnoff 				break;
1754de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1755f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1756f581a26eSGleb Smirnoff 				break;
1757de156263SGleb Smirnoff 			default:
1758de156263SGleb Smirnoff 				return (error);
17595dff1c38SMichael Tuexen 			}
1760de156263SGleb Smirnoff 			break;
1761b287c6c7SBjoern A. Zeeb #endif
1762b287c6c7SBjoern A. Zeeb #ifdef INET
1763de156263SGleb Smirnoff 		case IPPROTO_IP:
1764de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1765de156263SGleb Smirnoff 			case IP_TOS:
17663b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
17673b0ee680SRichard Scheffenegger 				break;
1768de156263SGleb Smirnoff 			case IP_TTL:
1769de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1770de156263SGleb Smirnoff 				break;
1771de156263SGleb Smirnoff 			default:
1772df8bae1dSRodney W. Grimes 				return (error);
1773de156263SGleb Smirnoff 			}
1774de156263SGleb Smirnoff 			break;
1775de156263SGleb Smirnoff #endif
1776de156263SGleb Smirnoff 		default:
1777de156263SGleb Smirnoff 			return (error);
1778de156263SGleb Smirnoff 		}
17793b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
17803b3c08c1SMichael Tuexen 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17813b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
17823b3c08c1SMichael Tuexen 			return (ECONNRESET);
17833b3c08c1SMichael Tuexen 		}
1784fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1785fc4d53ccSGleb Smirnoff 		/*
1786fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1787fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1788fc4d53ccSGleb Smirnoff 		 */
1789fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1790fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
1791fc4d53ccSGleb Smirnoff 
17923b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1793fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1794fc4d53ccSGleb Smirnoff 		if (error)
1795fc4d53ccSGleb Smirnoff 			return (error);
1796fc4d53ccSGleb Smirnoff 
179768cea2b1SJohn Baldwin 		INP_WLOCK(inp);
1798ad71fe3cSRobert Watson 		if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17998501a69cSRobert Watson 			INP_WUNLOCK(inp);
18001e8f5ffaSRobert Watson 			return (ECONNRESET);
1801623dce13SRobert Watson 		}
180255bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1803fc4d53ccSGleb Smirnoff 
180455bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
180555bceb1eSRandall Stewart 		if (blk == NULL) {
180655bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
180755bceb1eSRandall Stewart 			return (ENOENT);
180855bceb1eSRandall Stewart 		}
1809587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1810587d67c0SRandall Stewart 			/* You already have this */
1811587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1812587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1813587d67c0SRandall Stewart 			return (0);
1814587d67c0SRandall Stewart 		}
1815587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1816587d67c0SRandall Stewart 			/*
1817587d67c0SRandall Stewart 			 * The user has advanced the state
1818587d67c0SRandall Stewart 			 * past the initial point, we may not
1819587d67c0SRandall Stewart 			 * be able to switch.
1820587d67c0SRandall Stewart 			 */
1821587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1822587d67c0SRandall Stewart 				/*
1823587d67c0SRandall Stewart 				 * Does the stack provide a
1824587d67c0SRandall Stewart 				 * query mechanism, if so it may
1825587d67c0SRandall Stewart 				 * still be possible?
1826587d67c0SRandall Stewart 				 */
1827587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1828c6c0be27SMichael Tuexen 			} else
1829c6c0be27SMichael Tuexen 				error = EINVAL;
1830587d67c0SRandall Stewart 			if (error) {
1831587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1832587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1833587d67c0SRandall Stewart 				return(error);
1834587d67c0SRandall Stewart 			}
1835587d67c0SRandall Stewart 		}
183655bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
183755bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
183855bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
183955bceb1eSRandall Stewart 			return (ENOENT);
184055bceb1eSRandall Stewart 		}
184155bceb1eSRandall Stewart 		/*
184255bceb1eSRandall Stewart 		 * Release the old refcnt, the
1843587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1844587d67c0SRandall Stewart 		 * new one already.
184555bceb1eSRandall Stewart 		 */
1846587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1847086a3556SAndrew Gallatin 			struct epoch_tracker et;
1848587d67c0SRandall Stewart 			/*
1849587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1850587d67c0SRandall Stewart 			 * the tcb is not going away.
1851587d67c0SRandall Stewart 			 */
1852086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1853587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1854086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1855587d67c0SRandall Stewart 		}
18563ee9c3c4SRandall Stewart #ifdef TCPHPTS
18573ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
1858a370832bSGleb Smirnoff 		tcp_hpts_remove(tp->t_inpcb);
18593ee9c3c4SRandall Stewart #endif
18603ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
18613ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
18623ee9c3c4SRandall Stewart 			if (error) {
18633ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
18643ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
18653ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
18663ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
18673ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
1868fd7daa72SMichael Tuexen 						soabort(so);
18693ee9c3c4SRandall Stewart 						return (error);
18703ee9c3c4SRandall Stewart 					}
18713ee9c3c4SRandall Stewart 				}
18723ee9c3c4SRandall Stewart 				goto err_out;
18733ee9c3c4SRandall Stewart 			}
18743ee9c3c4SRandall Stewart 		}
187555bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
187655bceb1eSRandall Stewart 		tp->t_fb = blk;
187755bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
187855bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
187955bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
188055bceb1eSRandall Stewart 			     sopt->sopt_name);
188155bceb1eSRandall Stewart 		}
188255bceb1eSRandall Stewart #endif
18833ee9c3c4SRandall Stewart err_out:
188455bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
188555bceb1eSRandall Stewart 		return (error);
1886fc4d53ccSGleb Smirnoff 	}
1887fc4d53ccSGleb Smirnoff 
18883b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
18893b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1890fc4d53ccSGleb Smirnoff }
1891fc4d53ccSGleb Smirnoff 
1892fc4d53ccSGleb Smirnoff static int
1893fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1894fc4d53ccSGleb Smirnoff {
1895fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1896fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1897fc4d53ccSGleb Smirnoff 	int error = 0;
1898fc4d53ccSGleb Smirnoff 
1899fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
19003b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
1901fd7daa72SMichael Tuexen 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1902fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1903fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1904fc4d53ccSGleb Smirnoff 
1905fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
19063b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1907fc4d53ccSGleb Smirnoff #ifdef INET6
1908fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1909fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1910fc4d53ccSGleb Smirnoff #endif /* INET6 */
1911fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1912fc4d53ccSGleb Smirnoff 		else
1913fc4d53ccSGleb Smirnoff #endif
1914fc4d53ccSGleb Smirnoff #ifdef INET
1915fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1916fc4d53ccSGleb Smirnoff #endif
1917fc4d53ccSGleb Smirnoff 		return (error);
1918fc4d53ccSGleb Smirnoff 	}
1919fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1920e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1921fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1922fc4d53ccSGleb Smirnoff 
1923e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1924e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1925e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1926e2833083SPeter Lei 		} else {
1927e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1928e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1929c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1930c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1931e2833083SPeter Lei 		}
193255bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
193355bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
193455bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
193555bceb1eSRandall Stewart 		return (error);
193655bceb1eSRandall Stewart 	}
1937fc4d53ccSGleb Smirnoff 
19383b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
19393b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1940fc4d53ccSGleb Smirnoff }
1941fc4d53ccSGleb Smirnoff 
1942fc4d53ccSGleb Smirnoff int
1943fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1944fc4d53ccSGleb Smirnoff {
1945fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1946fc4d53ccSGleb Smirnoff 
1947fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1948fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1949fc4d53ccSGleb Smirnoff 
19503b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
19513b3c08c1SMichael Tuexen 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
19523b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
19533b3c08c1SMichael Tuexen 		return (ECONNRESET);
19543b3c08c1SMichael Tuexen 	}
1955fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1956fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1957fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1958fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1959fc4d53ccSGleb Smirnoff 	else
1960fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
196155bceb1eSRandall Stewart }
196255bceb1eSRandall Stewart 
19632529f56eSJonathan T. Looney /*
19642529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
19652529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
19662529f56eSJonathan T. Looney  */
19672529f56eSJonathan T. Looney #ifdef CTASSERT
19682529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
19692529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
19702529f56eSJonathan T. Looney #endif
19712529f56eSJonathan T. Looney 
1972ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1973ec1db6e1SJohn Baldwin static int
1974ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1975ec1db6e1SJohn Baldwin {
1976ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1977ec1db6e1SJohn Baldwin 	int error;
1978ec1db6e1SJohn Baldwin 
1979ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1980ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1981ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1982ec1db6e1SJohn Baldwin 		if (error)
1983ec1db6e1SJohn Baldwin 			return (error);
1984ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1985ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1986ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1987ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1988ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1989ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1990ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1991ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1992ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1993ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1994ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1995ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1996ec1db6e1SJohn Baldwin 		return (0);
1997ec1db6e1SJohn Baldwin 	}
1998ec1db6e1SJohn Baldwin 
1999ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
2000ec1db6e1SJohn Baldwin }
2001ec1db6e1SJohn Baldwin #endif
2002ec1db6e1SJohn Baldwin 
2003b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
2004b8d60729SRandall Stewart 
2005b8d60729SRandall Stewart static int
2006ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
2007b8d60729SRandall Stewart {
2008b8d60729SRandall Stewart 	struct cc_algo *algo;
2009b8d60729SRandall Stewart 	void *ptr = NULL;
20103b3c08c1SMichael Tuexen 	struct tcpcb *tp;
2011b8d60729SRandall Stewart 	struct cc_var cc_mem;
2012b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
2013b8d60729SRandall Stewart 	size_t mem_sz;
2014b8d60729SRandall Stewart 	int error;
2015b8d60729SRandall Stewart 
2016b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2017b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2018b8d60729SRandall Stewart 	if (error)
2019b8d60729SRandall Stewart 		return(error);
2020b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
2021b8d60729SRandall Stewart 	CC_LIST_RLOCK();
2022ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
2023b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
2024b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
2025b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
2026b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
2027b8d60729SRandall Stewart 				continue;
2028b8d60729SRandall Stewart 			}
2029b8d60729SRandall Stewart 			break;
2030b8d60729SRandall Stewart 		}
2031ea9017fbSRandall Stewart 	}
2032b8d60729SRandall Stewart 	if (algo == NULL) {
2033b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
2034b8d60729SRandall Stewart 		return(ESRCH);
2035b8d60729SRandall Stewart 	}
2036ea9017fbSRandall Stewart 	/*
2037ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
2038ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
2039ea9017fbSRandall Stewart 	 */
2040ea9017fbSRandall Stewart 	cc_refer(algo);
2041ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2042b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
2043b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
2044b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
2045b8d60729SRandall Stewart 		if (mem_sz == 0) {
2046b8d60729SRandall Stewart 			goto no_mem_needed;
2047b8d60729SRandall Stewart 		}
2048b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
2049b8d60729SRandall Stewart 	} else {
2050b8d60729SRandall Stewart no_mem_needed:
2051b8d60729SRandall Stewart 		mem_sz = 0;
2052b8d60729SRandall Stewart 		ptr = NULL;
2053b8d60729SRandall Stewart 	}
2054b8d60729SRandall Stewart 	/*
2055b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
2056b8d60729SRandall Stewart 	 * back the inplock.
2057b8d60729SRandall Stewart 	 */
2058b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
2059df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
2060df07bfdaSMichael Tuexen 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2061df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
2062ea9017fbSRandall Stewart 		if (ptr)
2063df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
2064ea9017fbSRandall Stewart 		/* Release our temp reference */
2065ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
2066ea9017fbSRandall Stewart 		cc_release(algo);
2067ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
2068df07bfdaSMichael Tuexen 		return (ECONNRESET);
2069df07bfdaSMichael Tuexen 	}
2070df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
2071df07bfdaSMichael Tuexen 	if (ptr != NULL)
2072b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
2073b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
2074b8d60729SRandall Stewart 	/*
2075b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
2076b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
2077b8d60729SRandall Stewart 	 * Note here we init into stack memory.
2078b8d60729SRandall Stewart 	 */
2079b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
2080b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
2081b8d60729SRandall Stewart 	else
2082b8d60729SRandall Stewart 		error = 0;
2083b8d60729SRandall Stewart 	/*
2084b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
2085b8d60729SRandall Stewart 	 * should not fail we could in theory have a
2086b8d60729SRandall Stewart 	 * KASSERT here.
2087b8d60729SRandall Stewart 	 */
2088b8d60729SRandall Stewart 	if (error == 0) {
2089b8d60729SRandall Stewart 		/*
2090b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
2091b8d60729SRandall Stewart 		 * connection to the new CC module by
2092b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
2093b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
2094b8d60729SRandall Stewart 		 */
2095b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
2096b8d60729SRandall Stewart 			CC_ALGO(tp)->cb_destroy(tp->ccv);
2097ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
2098ea9017fbSRandall Stewart 		cc_detach(tp);
2099ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
2100b8d60729SRandall Stewart 		memcpy(tp->ccv, &cc_mem, sizeof(struct cc_var));
2101ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
2102ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2103b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2104b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2105b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2106b8d60729SRandall Stewart 			CC_ALGO(tp)->conn_init(tp->ccv);
2107b8d60729SRandall Stewart 		}
2108b8d60729SRandall Stewart 	} else if (ptr)
2109b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2110b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2111ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2112ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2113ea9017fbSRandall Stewart 	cc_release(algo);
2114ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2115b8d60729SRandall Stewart 	return (error);
2116b8d60729SRandall Stewart }
2117b8d60729SRandall Stewart 
211855bceb1eSRandall Stewart int
21193b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
212055bceb1eSRandall Stewart {
2121fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
212255bceb1eSRandall Stewart 	int	error, opt, optval;
212355bceb1eSRandall Stewart 	u_int	ui;
212455bceb1eSRandall Stewart 	struct	tcp_info ti;
2125b2e60773SJohn Baldwin #ifdef KERN_TLS
2126b2e60773SJohn Baldwin 	struct tls_enable tls;
2127528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2128b2e60773SJohn Baldwin #endif
21292529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2130adc56f5aSEdward Tomasz Napierala #ifdef STATS
2131adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2132adc56f5aSEdward Tomasz Napierala #endif
2133af6fef3aSGleb Smirnoff 	size_t	len;
2134df8bae1dSRodney W. Grimes 
2135f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
2136fd7daa72SMichael Tuexen 	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
2137fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2138528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2139f581a26eSGleb Smirnoff 
2140f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2141f581a26eSGleb Smirnoff #ifdef INET6
2142f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2143f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2144f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2145f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2146f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2147f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2148f581a26eSGleb Smirnoff 		}
2149f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2150f581a26eSGleb Smirnoff 		return (0);
2151f581a26eSGleb Smirnoff #endif
2152f581a26eSGleb Smirnoff #ifdef INET
2153f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2154f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2155f581a26eSGleb Smirnoff 		return (0);
2156f581a26eSGleb Smirnoff #endif
2157f581a26eSGleb Smirnoff 	}
2158f581a26eSGleb Smirnoff 
2159d519cedbSGleb Smirnoff 	/*
2160d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2161d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2162d519cedbSGleb Smirnoff 	 */
2163d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2164d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2165d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2166c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2167c8b53cedSMichael Tuexen 			return (EINVAL);
2168af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2169af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2170d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2171d519cedbSGleb Smirnoff 		if (error) {
2172af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2173d519cedbSGleb Smirnoff 			return (error);
2174d519cedbSGleb Smirnoff 		}
2175bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2176d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2177af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
2178d519cedbSGleb Smirnoff 		else
2179d519cedbSGleb Smirnoff 			error = ENOENT;
2180d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2181d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2182af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2183af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2184d519cedbSGleb Smirnoff 		return (error);
2185d519cedbSGleb Smirnoff 	}
2186d519cedbSGleb Smirnoff 
2187cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2188cfe8b629SGarrett Wollman 	case SOPT_SET:
2189cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2190fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
219188f6b043SBruce M Simpson 		case TCP_MD5SIG:
21928501a69cSRobert Watson 			INP_WUNLOCK(inp);
219397453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2194fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2195fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
21961cfd4b53SBruce M Simpson 			if (error)
21971e8f5ffaSRobert Watson 				return (error);
219897453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
219909fe6320SNavdeep Parhar 			goto unlock_and_done;
2200fcf59617SAndrey V. Elsukov #endif /* IPSEC */
220109fe6320SNavdeep Parhar 
2202df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2203cfe8b629SGarrett Wollman 		case TCP_NOOPT:
22040471a8c7SRichard Scheffenegger 		case TCP_LRD:
22058501a69cSRobert Watson 			INP_WUNLOCK(inp);
2206cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2207cfe8b629SGarrett Wollman 			    sizeof optval);
2208cfe8b629SGarrett Wollman 			if (error)
22091e8f5ffaSRobert Watson 				return (error);
2210cfe8b629SGarrett Wollman 
22118501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2212cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2213cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2214cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2215cfe8b629SGarrett Wollman 				break;
2216cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2217cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2218cfe8b629SGarrett Wollman 				break;
22190471a8c7SRichard Scheffenegger 			case TCP_LRD:
22200471a8c7SRichard Scheffenegger 				opt = TF_LRD;
22210471a8c7SRichard Scheffenegger 				break;
2222cfe8b629SGarrett Wollman 			default:
2223cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2224cfe8b629SGarrett Wollman 				break;
2225cfe8b629SGarrett Wollman 			}
2226cfe8b629SGarrett Wollman 
2227cfe8b629SGarrett Wollman 			if (optval)
2228cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2229df8bae1dSRodney W. Grimes 			else
2230cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
223109fe6320SNavdeep Parhar unlock_and_done:
223209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
223309fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
223409fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
223509fe6320SNavdeep Parhar 				    sopt->sopt_name);
223609fe6320SNavdeep Parhar 			}
223709fe6320SNavdeep Parhar #endif
22388501a69cSRobert Watson 			INP_WUNLOCK(inp);
2239df8bae1dSRodney W. Grimes 			break;
2240df8bae1dSRodney W. Grimes 
2241007581c0SJonathan Lemon 		case TCP_NOPUSH:
22428501a69cSRobert Watson 			INP_WUNLOCK(inp);
2243007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2244007581c0SJonathan Lemon 			    sizeof optval);
2245007581c0SJonathan Lemon 			if (error)
22461e8f5ffaSRobert Watson 				return (error);
2247007581c0SJonathan Lemon 
22488501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2249007581c0SJonathan Lemon 			if (optval)
2250007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2251d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2252007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2253109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2254109eb549SGleb Smirnoff 					struct epoch_tracker et;
2255109eb549SGleb Smirnoff 
2256109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2257f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2258109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2259109eb549SGleb Smirnoff 				}
2260007581c0SJonathan Lemon 			}
226109fe6320SNavdeep Parhar 			goto unlock_and_done;
2262007581c0SJonathan Lemon 
22639e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
22649e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
22659e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
22669e644c23SMichael Tuexen 			    sizeof optval);
22679e644c23SMichael Tuexen 			if (error)
22689e644c23SMichael Tuexen 				return (error);
22699e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
22709e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
22719e644c23SMichael Tuexen 				/* Its got to be in range */
22729e644c23SMichael Tuexen 				return (EINVAL);
22739e644c23SMichael Tuexen 			}
22749e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
22759e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
22769e644c23SMichael Tuexen 				return (EINVAL);
22779e644c23SMichael Tuexen 			}
22789e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
22799e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
22809e644c23SMichael Tuexen 				/* You can't change after you are connected */
22819e644c23SMichael Tuexen 				error = EINVAL;
22829e644c23SMichael Tuexen 			} else {
22839e644c23SMichael Tuexen 				/* Ok we are all good set the port */
22849e644c23SMichael Tuexen 				tp->t_port = htons(optval);
22859e644c23SMichael Tuexen 			}
22869e644c23SMichael Tuexen 			goto unlock_and_done;
22879e644c23SMichael Tuexen 
2288df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
22898501a69cSRobert Watson 			INP_WUNLOCK(inp);
2290cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2291cfe8b629SGarrett Wollman 			    sizeof optval);
2292cfe8b629SGarrett Wollman 			if (error)
22931e8f5ffaSRobert Watson 				return (error);
2294df8bae1dSRodney W. Grimes 
22958501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
229653369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2297603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2298cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2299a0292f23SGarrett Wollman 			else
2300a0292f23SGarrett Wollman 				error = EINVAL;
230109fe6320SNavdeep Parhar 			goto unlock_and_done;
2302a0292f23SGarrett Wollman 
2303b8af5dfaSRobert Watson 		case TCP_INFO:
23048501a69cSRobert Watson 			INP_WUNLOCK(inp);
2305b8af5dfaSRobert Watson 			error = EINVAL;
2306b8af5dfaSRobert Watson 			break;
2307b8af5dfaSRobert Watson 
2308adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2309adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2310adc56f5aSEdward Tomasz Napierala #ifdef STATS
2311adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2312adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2313adc56f5aSEdward Tomasz Napierala 			if (error)
2314adc56f5aSEdward Tomasz Napierala 				return (error);
2315adc56f5aSEdward Tomasz Napierala 
2316adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2317adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2318adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2319adc56f5aSEdward Tomasz Napierala 			else
2320adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2321adc56f5aSEdward Tomasz Napierala 
2322adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2323adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2324adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2325adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2326adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2327adc56f5aSEdward Tomasz Napierala 				sbp = t;
2328adc56f5aSEdward Tomasz Napierala 			}
2329adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2330adc56f5aSEdward Tomasz Napierala 
2331adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2332adc56f5aSEdward Tomasz Napierala #else
2333adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2334adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2335adc56f5aSEdward Tomasz Napierala 			break;
2336adc56f5aSEdward Tomasz Napierala 
2337dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2338ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
233973e263b1SGleb Smirnoff 			break;
2340dbc42409SLawrence Stewart 
2341a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2342a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2343a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2344a034518aSAndrew Gallatin 			    sizeof(optval));
2345a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2346a034518aSAndrew Gallatin 			if (!error)
2347a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2348a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2349a034518aSAndrew Gallatin 			break;
2350a034518aSAndrew Gallatin 
2351b2e60773SJohn Baldwin #ifdef KERN_TLS
2352b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2353b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2354ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2355b2e60773SJohn Baldwin 			if (error)
2356b2e60773SJohn Baldwin 				break;
2357fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2358b2e60773SJohn Baldwin 			break;
2359b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2360b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2361b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2362b2e60773SJohn Baldwin 			if (error)
2363b2e60773SJohn Baldwin 				return (error);
2364b2e60773SJohn Baldwin 
2365b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2366fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2367b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2368b2e60773SJohn Baldwin 			break;
2369f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2370f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2371f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2372f1f93475SJohn Baldwin 			    sizeof(tls));
2373f1f93475SJohn Baldwin 			if (error)
2374f1f93475SJohn Baldwin 				break;
2375fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2376f1f93475SJohn Baldwin 			break;
2377b2e60773SJohn Baldwin #endif
2378b2e60773SJohn Baldwin 
23799077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
23809077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
23819077f387SGleb Smirnoff 		case TCP_KEEPINIT:
23829077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
23839077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
23849077f387SGleb Smirnoff 			if (error)
23859077f387SGleb Smirnoff 				return (error);
23869077f387SGleb Smirnoff 
23879077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
23889077f387SGleb Smirnoff 				error = EINVAL;
23899077f387SGleb Smirnoff 				break;
23909077f387SGleb Smirnoff 			}
23919077f387SGleb Smirnoff 			ui *= hz;
23929077f387SGleb Smirnoff 
23939077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
23949077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
23959077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23969077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23979077f387SGleb Smirnoff 				/*
23989077f387SGleb Smirnoff 				 * XXX: better check current remaining
23999077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
24009077f387SGleb Smirnoff 				 */
24019077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
24029077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
24039077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
24049077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
24059077f387SGleb Smirnoff 				break;
24069077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
24079077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
24089077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
24099077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
24109077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
24119077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
24129077f387SGleb Smirnoff 				break;
24139077f387SGleb Smirnoff 			case TCP_KEEPINIT:
24149077f387SGleb Smirnoff 				tp->t_keepinit = ui;
24159077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
24169077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
24179077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
24189077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
24199077f387SGleb Smirnoff 				break;
24209077f387SGleb Smirnoff 			}
242109fe6320SNavdeep Parhar 			goto unlock_and_done;
24229077f387SGleb Smirnoff 
242385c05144SGleb Smirnoff 		case TCP_KEEPCNT:
242485c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
242585c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
242685c05144SGleb Smirnoff 			if (error)
242785c05144SGleb Smirnoff 				return (error);
242885c05144SGleb Smirnoff 
242985c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
243085c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
243185c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
243285c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
243385c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
243485c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
243585c05144SGleb Smirnoff 			goto unlock_and_done;
243685c05144SGleb Smirnoff 
243786a996e6SHiren Panchasara #ifdef TCPPCAP
243886a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
243986a996e6SHiren Panchasara 		case TCP_PCAP_IN:
244086a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
244186a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
244286a996e6SHiren Panchasara 			    sizeof optval);
244386a996e6SHiren Panchasara 			if (error)
244486a996e6SHiren Panchasara 				return (error);
244586a996e6SHiren Panchasara 
244686a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
244786a996e6SHiren Panchasara 			if (optval >= 0)
244886a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
244986a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
245086a996e6SHiren Panchasara 					optval);
245186a996e6SHiren Panchasara 			else
245286a996e6SHiren Panchasara 				error = EINVAL;
245386a996e6SHiren Panchasara 			goto unlock_and_done;
245486a996e6SHiren Panchasara #endif
245586a996e6SHiren Panchasara 
2456c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2457c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2458c560df6fSPatrick Kelsey 
2459281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2460c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2461c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2462281a0fd4SPatrick Kelsey 				return (EPERM);
2463281a0fd4SPatrick Kelsey 
2464c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2465c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2466281a0fd4SPatrick Kelsey 			if (error)
2467281a0fd4SPatrick Kelsey 				return (error);
2468281a0fd4SPatrick Kelsey 
2469281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2470d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2471d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2472d442a657SMichael Tuexen 				error = EINVAL;
2473d442a657SMichael Tuexen 				goto unlock_and_done;
2474d442a657SMichael Tuexen 			}
2475c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2476c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2477c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2478c560df6fSPatrick Kelsey 						error = EPERM;
2479c560df6fSPatrick Kelsey 						goto unlock_and_done;
2480c560df6fSPatrick Kelsey 					}
2481c560df6fSPatrick Kelsey 
2482c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2483281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2484281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2485c560df6fSPatrick Kelsey 				} else {
2486c560df6fSPatrick Kelsey 					/*
2487c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2488c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2489c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2490c560df6fSPatrick Kelsey 					 * connect.
2491c560df6fSPatrick Kelsey 					 */
2492c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2493c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2494c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2495c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2496c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2497c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2498c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2499c560df6fSPatrick Kelsey 					}
2500c560df6fSPatrick Kelsey 				}
2501d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2502281a0fd4SPatrick Kelsey 			} else
2503281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2504281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2505c560df6fSPatrick Kelsey 		}
2506281a0fd4SPatrick Kelsey 
2507e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
25082529f56eSJonathan T. Looney 		case TCP_LOG:
25092529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25102529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
25112529f56eSJonathan T. Looney 			    sizeof optval);
25122529f56eSJonathan T. Looney 			if (error)
25132529f56eSJonathan T. Looney 				return (error);
25142529f56eSJonathan T. Looney 
25152529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
25162529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
25172529f56eSJonathan T. Looney 			goto unlock_and_done;
25182529f56eSJonathan T. Looney 
25192529f56eSJonathan T. Looney 		case TCP_LOGBUF:
25202529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25212529f56eSJonathan T. Looney 			error = EINVAL;
25222529f56eSJonathan T. Looney 			break;
25232529f56eSJonathan T. Looney 
25242529f56eSJonathan T. Looney 		case TCP_LOGID:
25252529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25262529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
25272529f56eSJonathan T. Looney 			if (error)
25282529f56eSJonathan T. Looney 				break;
25292529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
25302529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
25312529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
25322529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
25332529f56eSJonathan T. Looney 			break;
25342529f56eSJonathan T. Looney 
25352529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
25362529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
25372529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25382529f56eSJonathan T. Looney 			error =
25392529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
25402529f56eSJonathan T. Looney 			if (error)
25412529f56eSJonathan T. Looney 				break;
25422529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
25432529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
25442529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
25452529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
25462529f56eSJonathan T. Looney 				    M_WAITOK, true);
25472529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
25482529f56eSJonathan T. Looney 			} else {
25492529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
25502529f56eSJonathan T. Looney 				/*
25512529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
25522529f56eSJonathan T. Looney 				 * INP lock.
25532529f56eSJonathan T. Looney 				 */
25542529f56eSJonathan T. Looney 			}
25552529f56eSJonathan T. Looney 			break;
2556e24e5683SJonathan T. Looney #endif
25572529f56eSJonathan T. Looney 
2558df8bae1dSRodney W. Grimes 		default:
25598501a69cSRobert Watson 			INP_WUNLOCK(inp);
2560df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2561df8bae1dSRodney W. Grimes 			break;
2562df8bae1dSRodney W. Grimes 		}
2563df8bae1dSRodney W. Grimes 		break;
2564df8bae1dSRodney W. Grimes 
2565cfe8b629SGarrett Wollman 	case SOPT_GET:
25661e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2567cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2568fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
256988f6b043SBruce M Simpson 		case TCP_MD5SIG:
25708501a69cSRobert Watson 			INP_WUNLOCK(inp);
257197453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2572fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2573fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
25741cfd4b53SBruce M Simpson 			break;
2575265ed012SBruce M Simpson #endif
25761e8f5ffaSRobert Watson 
2577df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2578cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
25798501a69cSRobert Watson 			INP_WUNLOCK(inp);
2580b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2581df8bae1dSRodney W. Grimes 			break;
2582df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2583cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
25848501a69cSRobert Watson 			INP_WUNLOCK(inp);
2585b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2586df8bae1dSRodney W. Grimes 			break;
25879e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
25889e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
25899e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
25909e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25919e644c23SMichael Tuexen 			break;
2592a0292f23SGarrett Wollman 		case TCP_NOOPT:
2593cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
25948501a69cSRobert Watson 			INP_WUNLOCK(inp);
2595b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2596a0292f23SGarrett Wollman 			break;
2597a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2598cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
25998501a69cSRobert Watson 			INP_WUNLOCK(inp);
2600b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2601b8af5dfaSRobert Watson 			break;
2602b8af5dfaSRobert Watson 		case TCP_INFO:
2603b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
26048501a69cSRobert Watson 			INP_WUNLOCK(inp);
2605b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2606a0292f23SGarrett Wollman 			break;
2607adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2608adc56f5aSEdward Tomasz Napierala 			{
2609adc56f5aSEdward Tomasz Napierala #ifdef STATS
2610adc56f5aSEdward Tomasz Napierala 			int nheld;
2611adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2612adc56f5aSEdward Tomasz Napierala 
2613adc56f5aSEdward Tomasz Napierala 			error = 0;
2614adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2615adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2616adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2617adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2618adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2619adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2620adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2621adc56f5aSEdward Tomasz Napierala 			else
2622adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2623adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2624adc56f5aSEdward Tomasz Napierala 			if (error)
2625adc56f5aSEdward Tomasz Napierala 				break;
2626adc56f5aSEdward Tomasz Napierala 
2627adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2628adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2629adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2630adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2631adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2632adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2633adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2634adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2635adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2636adc56f5aSEdward Tomasz Napierala 				break;
2637adc56f5aSEdward Tomasz Napierala 			}
2638adc56f5aSEdward Tomasz Napierala 
2639adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2640adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2641adc56f5aSEdward Tomasz Napierala 				goto unhold;
2642adc56f5aSEdward Tomasz Napierala 
2643adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2644adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2645adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2646adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2647adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2648adc56f5aSEdward Tomasz Napierala unhold:
2649adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2650adc56f5aSEdward Tomasz Napierala #else
2651adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2652adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2653adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2654adc56f5aSEdward Tomasz Napierala 			break;
2655adc56f5aSEdward Tomasz Napierala 			}
2656dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2657af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2658dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2659af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2660dbc42409SLawrence Stewart 			break;
26612f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
26622f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
26632f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
26642f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
26652f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
26662f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
26675a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
26682f3eb7f4SGleb Smirnoff 				break;
26692f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
26705a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
26712f3eb7f4SGleb Smirnoff 				break;
26722f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
26735a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
26742f3eb7f4SGleb Smirnoff 				break;
26752f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
26765a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
26772f3eb7f4SGleb Smirnoff 				break;
26782f3eb7f4SGleb Smirnoff 			}
26792f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
26802f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
26812f3eb7f4SGleb Smirnoff 			break;
268286a996e6SHiren Panchasara #ifdef TCPPCAP
268386a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
268486a996e6SHiren Panchasara 		case TCP_PCAP_IN:
268586a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
268686a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
268786a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
268886a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
268986a996e6SHiren Panchasara 			break;
269086a996e6SHiren Panchasara #endif
2691281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2692281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2693281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2694281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2695281a0fd4SPatrick Kelsey 			break;
2696e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
26972529f56eSJonathan T. Looney 		case TCP_LOG:
26982529f56eSJonathan T. Looney 			optval = tp->t_logstate;
26992529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
27002529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
27012529f56eSJonathan T. Looney 			break;
27022529f56eSJonathan T. Looney 		case TCP_LOGBUF:
27032529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
27042529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
27052529f56eSJonathan T. Looney 			break;
27062529f56eSJonathan T. Looney 		case TCP_LOGID:
27072529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
27082529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
27092529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
27102529f56eSJonathan T. Looney 			break;
27112529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
27122529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
27132529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
27142529f56eSJonathan T. Looney 			error = EINVAL;
27152529f56eSJonathan T. Looney 			break;
2716e24e5683SJonathan T. Looney #endif
2717b2e60773SJohn Baldwin #ifdef KERN_TLS
2718b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2719fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2720b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2721bf256782SMark Johnston 			if (error == 0)
2722bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2723bf256782SMark Johnston 				    sizeof(optval));
2724b2e60773SJohn Baldwin 			break;
2725f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2726fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2727f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2728bf256782SMark Johnston 			if (error == 0)
2729bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2730bf256782SMark Johnston 				    sizeof(optval));
2731f1f93475SJohn Baldwin 			break;
2732b2e60773SJohn Baldwin #endif
27330471a8c7SRichard Scheffenegger 		case TCP_LRD:
27340471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
27350471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
27360471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
27370471a8c7SRichard Scheffenegger 			break;
2738df8bae1dSRodney W. Grimes 		default:
27398501a69cSRobert Watson 			INP_WUNLOCK(inp);
2740df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2741df8bae1dSRodney W. Grimes 			break;
2742df8bae1dSRodney W. Grimes 		}
2743df8bae1dSRodney W. Grimes 		break;
2744df8bae1dSRodney W. Grimes 	}
2745df8bae1dSRodney W. Grimes 	return (error);
2746df8bae1dSRodney W. Grimes }
27478501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2748bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2749df8bae1dSRodney W. Grimes 
275026e30fbbSDavid Greenman /*
2751df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2752df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2753df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2754df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2755df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2756df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2757df8bae1dSRodney W. Grimes  */
2758623dce13SRobert Watson static void
2759ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2760df8bae1dSRodney W. Grimes {
2761e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2762e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2763e6e0b5ffSRobert Watson 
276497a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27658501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2766df8bae1dSRodney W. Grimes 
2767623dce13SRobert Watson 	/*
2768623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2769623dce13SRobert Watson 	 * socket is still open.
2770623dce13SRobert Watson 	 */
27718db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
27728db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2773df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2774623dce13SRobert Watson 		KASSERT(tp != NULL,
2775623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2776623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2777243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2778623dce13SRobert Watson 		KASSERT(tp != NULL,
2779623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2780623dce13SRobert Watson 	} else {
2781df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2782df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2783623dce13SRobert Watson 		tcp_usrclosed(tp);
2784ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2785f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2786f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2787df8bae1dSRodney W. Grimes 	}
2788df8bae1dSRodney W. Grimes }
2789df8bae1dSRodney W. Grimes 
2790df8bae1dSRodney W. Grimes /*
2791df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2792df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2793df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2794df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2795df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2796df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2797df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2798df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2799df8bae1dSRodney W. Grimes  */
2800623dce13SRobert Watson static void
2801ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2802df8bae1dSRodney W. Grimes {
2803df8bae1dSRodney W. Grimes 
280497a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
28058501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2806e6e0b5ffSRobert Watson 
2807df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2808df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
280909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
281009fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
281109fe6320SNavdeep Parhar #endif
2812550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2813bc65987aSKip Macy 		/* FALLTHROUGH */
2814bc65987aSKip Macy 	case TCPS_CLOSED:
2815df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2816623dce13SRobert Watson 		/*
2817623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2818623dce13SRobert Watson 		 * still open.
2819623dce13SRobert Watson 		 */
2820623dce13SRobert Watson 		KASSERT(tp != NULL,
2821623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2822df8bae1dSRodney W. Grimes 		break;
2823df8bae1dSRodney W. Grimes 
2824a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2825df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2826a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2827a0292f23SGarrett Wollman 		break;
2828a0292f23SGarrett Wollman 
2829df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
283057f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2831df8bae1dSRodney W. Grimes 		break;
2832df8bae1dSRodney W. Grimes 
2833df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
283457f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2835df8bae1dSRodney W. Grimes 		break;
2836df8bae1dSRodney W. Grimes 	}
2837abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2838df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2839abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
28407c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
28417c72af87SMohan Srinivasan 			int timeout;
28427c72af87SMohan Srinivasan 
28437c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
28449077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2845b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2846b6239c4aSAndras Olah 		}
2847df8bae1dSRodney W. Grimes 	}
28487c72af87SMohan Srinivasan }
2849497057eeSRobert Watson 
2850497057eeSRobert Watson #ifdef DDB
2851497057eeSRobert Watson static void
2852497057eeSRobert Watson db_print_indent(int indent)
2853497057eeSRobert Watson {
2854497057eeSRobert Watson 	int i;
2855497057eeSRobert Watson 
2856497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2857497057eeSRobert Watson 		db_printf(" ");
2858497057eeSRobert Watson }
2859497057eeSRobert Watson 
2860497057eeSRobert Watson static void
2861497057eeSRobert Watson db_print_tstate(int t_state)
2862497057eeSRobert Watson {
2863497057eeSRobert Watson 
2864497057eeSRobert Watson 	switch (t_state) {
2865497057eeSRobert Watson 	case TCPS_CLOSED:
2866497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2867497057eeSRobert Watson 		return;
2868497057eeSRobert Watson 
2869497057eeSRobert Watson 	case TCPS_LISTEN:
2870497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2871497057eeSRobert Watson 		return;
2872497057eeSRobert Watson 
2873497057eeSRobert Watson 	case TCPS_SYN_SENT:
2874497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2875497057eeSRobert Watson 		return;
2876497057eeSRobert Watson 
2877497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2878497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2879497057eeSRobert Watson 		return;
2880497057eeSRobert Watson 
2881497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2882497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2883497057eeSRobert Watson 		return;
2884497057eeSRobert Watson 
2885497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2886497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2887497057eeSRobert Watson 		return;
2888497057eeSRobert Watson 
2889497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2890497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2891497057eeSRobert Watson 		return;
2892497057eeSRobert Watson 
2893497057eeSRobert Watson 	case TCPS_CLOSING:
2894497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2895497057eeSRobert Watson 		return;
2896497057eeSRobert Watson 
2897497057eeSRobert Watson 	case TCPS_LAST_ACK:
2898497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2899497057eeSRobert Watson 		return;
2900497057eeSRobert Watson 
2901497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2902497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2903497057eeSRobert Watson 		return;
2904497057eeSRobert Watson 
2905497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2906497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2907497057eeSRobert Watson 		return;
2908497057eeSRobert Watson 
2909497057eeSRobert Watson 	default:
2910497057eeSRobert Watson 		db_printf("unknown");
2911497057eeSRobert Watson 		return;
2912497057eeSRobert Watson 	}
2913497057eeSRobert Watson }
2914497057eeSRobert Watson 
2915497057eeSRobert Watson static void
2916497057eeSRobert Watson db_print_tflags(u_int t_flags)
2917497057eeSRobert Watson {
2918497057eeSRobert Watson 	int comma;
2919497057eeSRobert Watson 
2920497057eeSRobert Watson 	comma = 0;
2921497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2922497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2923497057eeSRobert Watson 		comma = 1;
2924497057eeSRobert Watson 	}
2925497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2926497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2927497057eeSRobert Watson 		comma = 1;
2928497057eeSRobert Watson 	}
2929497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2930497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2931497057eeSRobert Watson 		comma = 1;
2932497057eeSRobert Watson 	}
2933497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2934497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2935497057eeSRobert Watson 		comma = 1;
2936497057eeSRobert Watson 	}
2937497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2938497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2939497057eeSRobert Watson 		comma = 1;
2940497057eeSRobert Watson 	}
2941497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2942497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2943497057eeSRobert Watson 		comma = 1;
2944497057eeSRobert Watson 	}
2945497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2946497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2947497057eeSRobert Watson 		comma = 1;
2948497057eeSRobert Watson 	}
2949497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2950497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2951497057eeSRobert Watson 		comma = 1;
2952497057eeSRobert Watson 	}
2953497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2954497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2955497057eeSRobert Watson 		comma = 1;
2956497057eeSRobert Watson 	}
2957497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2958497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2959497057eeSRobert Watson 		comma = 1;
2960497057eeSRobert Watson 	}
2961497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2962497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2963497057eeSRobert Watson 		comma = 1;
2964497057eeSRobert Watson 	}
2965497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2966497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2967497057eeSRobert Watson 		comma = 1;
2968497057eeSRobert Watson 	}
2969497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2970497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2971497057eeSRobert Watson 		comma = 1;
2972497057eeSRobert Watson 	}
29733f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
29743f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
29753f169c54SRichard Scheffenegger 		comma = 1;
29763f169c54SRichard Scheffenegger 	}
2977497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2978497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2979497057eeSRobert Watson 		comma = 1;
2980497057eeSRobert Watson 	}
2981497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2982497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2983497057eeSRobert Watson 		comma = 1;
2984497057eeSRobert Watson 	}
2985497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2986497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2987497057eeSRobert Watson 		comma = 1;
2988497057eeSRobert Watson 	}
2989497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2990497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2991497057eeSRobert Watson 		comma = 1;
2992497057eeSRobert Watson 	}
2993497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2994497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2995497057eeSRobert Watson 		comma = 1;
2996497057eeSRobert Watson 	}
2997dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2998dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2999dbc42409SLawrence Stewart 		comma = 1;
3000dbc42409SLawrence Stewart 	}
3001497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
3002497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
3003497057eeSRobert Watson 		comma = 1;
3004497057eeSRobert Watson 	}
30053f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
30063f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
30073f169c54SRichard Scheffenegger 		comma = 1;
30083f169c54SRichard Scheffenegger 	}
3009497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
3010497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
3011497057eeSRobert Watson 		comma = 1;
3012497057eeSRobert Watson 	}
3013497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
3014497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
3015497057eeSRobert Watson 		comma = 1;
3016497057eeSRobert Watson 	}
3017497057eeSRobert Watson 	if (t_flags & TF_TSO) {
3018497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
3019497057eeSRobert Watson 		comma = 1;
3020497057eeSRobert Watson 	}
3021281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
3022281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
3023281a0fd4SPatrick Kelsey 		comma = 1;
3024281a0fd4SPatrick Kelsey 	}
3025497057eeSRobert Watson }
3026497057eeSRobert Watson 
3027497057eeSRobert Watson static void
30283cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
30293cf38784SMichael Tuexen {
30303cf38784SMichael Tuexen 	int comma;
30313cf38784SMichael Tuexen 
30323cf38784SMichael Tuexen 	comma = 0;
30333f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
30343f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
30353f169c54SRichard Scheffenegger 		comma = 1;
30363f169c54SRichard Scheffenegger 	}
30373f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
30383f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
30393f169c54SRichard Scheffenegger 		comma = 1;
30403f169c54SRichard Scheffenegger 	}
30413f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
30423f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
30433f169c54SRichard Scheffenegger 		comma = 1;
30443f169c54SRichard Scheffenegger 	}
30453f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
30463f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
30473f169c54SRichard Scheffenegger 		comma = 1;
30483f169c54SRichard Scheffenegger 	}
30493f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
30503f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
30513f169c54SRichard Scheffenegger 		comma = 1;
30523f169c54SRichard Scheffenegger 	}
30533cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
30543cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
30553cf38784SMichael Tuexen 		comma = 1;
30563cf38784SMichael Tuexen 	}
30573f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
30583f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
30593f169c54SRichard Scheffenegger 		comma = 1;
30603f169c54SRichard Scheffenegger 	}
30613f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
30623f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
30633f169c54SRichard Scheffenegger 		comma = 1;
30643f169c54SRichard Scheffenegger 	}
30653f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
30663f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
30673f169c54SRichard Scheffenegger 		comma = 1;
30683f169c54SRichard Scheffenegger 	}
30693f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
30703f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30713f169c54SRichard Scheffenegger 		comma = 1;
30723f169c54SRichard Scheffenegger 	}
30733cf38784SMichael Tuexen }
30743cf38784SMichael Tuexen 
30753cf38784SMichael Tuexen static void
3076497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3077497057eeSRobert Watson {
3078497057eeSRobert Watson 	int comma;
3079497057eeSRobert Watson 
3080497057eeSRobert Watson 	comma = 0;
3081497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3082497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3083497057eeSRobert Watson 		comma = 1;
3084497057eeSRobert Watson 	}
3085497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3086497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3087497057eeSRobert Watson 		comma = 1;
3088497057eeSRobert Watson 	}
3089497057eeSRobert Watson }
3090497057eeSRobert Watson 
3091497057eeSRobert Watson static void
3092497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3093497057eeSRobert Watson {
3094497057eeSRobert Watson 
3095497057eeSRobert Watson 	db_print_indent(indent);
3096497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3097497057eeSRobert Watson 
3098497057eeSRobert Watson 	indent += 2;
3099497057eeSRobert Watson 
3100497057eeSRobert Watson 	db_print_indent(indent);
3101497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3102c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3103497057eeSRobert Watson 
3104497057eeSRobert Watson 	db_print_indent(indent);
310585d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
3106e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
3107497057eeSRobert Watson 
3108497057eeSRobert Watson 	db_print_indent(indent);
3109e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
3110e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
3111497057eeSRobert Watson 
3112497057eeSRobert Watson 	db_print_indent(indent);
3113497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3114497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3115497057eeSRobert Watson 	db_printf(")\n");
3116497057eeSRobert Watson 
3117497057eeSRobert Watson 	db_print_indent(indent);
3118497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3119497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3120497057eeSRobert Watson 	db_printf(")\n");
3121497057eeSRobert Watson 
3122497057eeSRobert Watson 	db_print_indent(indent);
31233cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
31243cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
31253cf38784SMichael Tuexen 	db_printf(")\n");
31263cf38784SMichael Tuexen 
31273cf38784SMichael Tuexen 	db_print_indent(indent);
3128497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
3129497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3130497057eeSRobert Watson 
3131497057eeSRobert Watson 	db_print_indent(indent);
3132497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3133497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3134497057eeSRobert Watson 
3135497057eeSRobert Watson 	db_print_indent(indent);
3136497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3137497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3138497057eeSRobert Watson 
3139497057eeSRobert Watson 	db_print_indent(indent);
31403ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3141497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3142497057eeSRobert Watson 
3143497057eeSRobert Watson 	db_print_indent(indent);
31443ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
31451c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3146497057eeSRobert Watson 
3147497057eeSRobert Watson 	db_print_indent(indent);
31483ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
31491c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3150497057eeSRobert Watson 
3151497057eeSRobert Watson 	db_print_indent(indent);
31520c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
31530c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3154497057eeSRobert Watson 
3155497057eeSRobert Watson 	db_print_indent(indent);
31561c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
31571c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3158497057eeSRobert Watson 
3159497057eeSRobert Watson 	db_print_indent(indent);
31601c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
31611c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3162497057eeSRobert Watson 
3163497057eeSRobert Watson 	db_print_indent(indent);
3164497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
3165497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
3166497057eeSRobert Watson 	    tp->t_rttbest);
3167497057eeSRobert Watson 
3168497057eeSRobert Watson 	db_print_indent(indent);
31693ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
3170497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3171497057eeSRobert Watson 
3172497057eeSRobert Watson 	db_print_indent(indent);
3173497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3174497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3175497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3176497057eeSRobert Watson 
3177497057eeSRobert Watson 	db_print_indent(indent);
3178497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3179497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3180497057eeSRobert Watson 
3181497057eeSRobert Watson 	db_print_indent(indent);
31829f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31831a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3184497057eeSRobert Watson 
3185497057eeSRobert Watson 	db_print_indent(indent);
3186497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31873ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3188497057eeSRobert Watson 
3189497057eeSRobert Watson 	db_print_indent(indent);
31903ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31919f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3192497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3193497057eeSRobert Watson 
3194497057eeSRobert Watson 	db_print_indent(indent);
31953529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31963529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3197497057eeSRobert Watson 
3198497057eeSRobert Watson 	db_print_indent(indent);
3199a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3200a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3201497057eeSRobert Watson 
3202497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3203497057eeSRobert Watson 
3204497057eeSRobert Watson 	db_print_indent(indent);
3205497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3206497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3207497057eeSRobert Watson }
3208497057eeSRobert Watson 
3209497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3210497057eeSRobert Watson {
3211497057eeSRobert Watson 	struct tcpcb *tp;
3212497057eeSRobert Watson 
3213497057eeSRobert Watson 	if (!have_addr) {
3214497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3215497057eeSRobert Watson 		return;
3216497057eeSRobert Watson 	}
3217497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3218497057eeSRobert Watson 
3219497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3220497057eeSRobert Watson }
3221497057eeSRobert Watson #endif
3222