xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 3f1f6b6ef7f6fea63583a4b6e917b3cc0f0d525e)
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>
69f5cf1e5fSJulien Charbon #include <sys/syslog.h>
70adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
71df8bae1dSRodney W. Grimes 
72497057eeSRobert Watson #ifdef DDB
73497057eeSRobert Watson #include <ddb/ddb.h>
74497057eeSRobert Watson #endif
75497057eeSRobert Watson 
76df8bae1dSRodney W. Grimes #include <net/if.h>
7776039bc8SGleb Smirnoff #include <net/if_var.h>
78df8bae1dSRodney W. Grimes #include <net/route.h>
79530c0060SRobert Watson #include <net/vnet.h>
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes #include <netinet/in.h>
825d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
83df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
84b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
85b5e8ce9fSBruce Evans #include <netinet/in_var.h>
86df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
87fb59c426SYoshinobu Inoue #ifdef INET6
88b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
89b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
90fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
91a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
92fb59c426SYoshinobu Inoue #endif
932de3e790SGleb Smirnoff #include <netinet/tcp.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
95df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
96df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
97df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
982529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
99df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1004644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
101c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
102fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
10386a996e6SHiren Panchasara #ifdef TCPPCAP
10486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10586a996e6SHiren Panchasara #endif
106610ee2f9SDavid Greenman #ifdef TCPDEBUG
107df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
108610ee2f9SDavid Greenman #endif
10909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
110bc65987aSKip Macy #include <netinet/tcp_offload.h>
11109fe6320SNavdeep Parhar #endif
112fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
113df8bae1dSRodney W. Grimes 
114adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
115adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
116adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
117adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
118adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
119adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
120adc56f5aSEdward Tomasz Napierala 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
123df8bae1dSRodney W. Grimes  */
124b287c6c7SBjoern A. Zeeb #ifdef INET
1254d77a549SAlfred Perlstein static int	tcp_connect(struct tcpcb *, struct sockaddr *,
1264d77a549SAlfred Perlstein 		    struct thread *td);
127b287c6c7SBjoern A. Zeeb #endif /* INET */
128fb59c426SYoshinobu Inoue #ifdef INET6
1294d77a549SAlfred Perlstein static int	tcp6_connect(struct tcpcb *, struct sockaddr *,
1304d77a549SAlfred Perlstein 		    struct thread *td);
131fb59c426SYoshinobu Inoue #endif /* INET6 */
132623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
133623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
134b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1352c37256eSGarrett Wollman 
136d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
137d3b6c96bSRandall Stewart 
1382c37256eSGarrett Wollman #ifdef TCPDEBUG
1391db24ffbSJonathan Lemon #define	TCPDEBUG0	int ostate = 0
1402c37256eSGarrett Wollman #define	TCPDEBUG1()	ostate = tp ? tp->t_state : 0
1414cc20ab1SSeigo Tanimura #define	TCPDEBUG2(req)	if (tp && (so->so_options & SO_DEBUG)) \
1424cc20ab1SSeigo Tanimura 				tcp_trace(TA_USER, ostate, tp, 0, 0, req)
1432c37256eSGarrett Wollman #else
1442c37256eSGarrett Wollman #define	TCPDEBUG0
1452c37256eSGarrett Wollman #define	TCPDEBUG1()
1462c37256eSGarrett Wollman #define	TCPDEBUG2(req)
1472c37256eSGarrett Wollman #endif
1482c37256eSGarrett Wollman 
1492c37256eSGarrett Wollman /*
15025102351SMike Karels  * tcp_require_unique port requires a globally-unique source port for each
15125102351SMike Karels  * outgoing connection.  The default is to require the 4-tuple to be unique.
15225102351SMike Karels  */
15325102351SMike Karels VNET_DEFINE(int, tcp_require_unique_port) = 0;
15425102351SMike Karels SYSCTL_INT(_net_inet_tcp, OID_AUTO, require_unique_port,
15525102351SMike Karels     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_require_unique_port), 0,
15625102351SMike Karels     "Require globally-unique ephemeral port for outgoing connections");
15725102351SMike Karels #define	V_tcp_require_unique_port	VNET(tcp_require_unique_port)
15825102351SMike Karels 
15925102351SMike Karels /*
1602c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1612c37256eSGarrett Wollman  * and an internet control block.
1622c37256eSGarrett Wollman  */
1632c37256eSGarrett Wollman static int
164b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1652c37256eSGarrett Wollman {
166f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
167623dce13SRobert Watson 	struct tcpcb *tp = NULL;
168623dce13SRobert Watson 	int error;
1692c37256eSGarrett Wollman 	TCPDEBUG0;
1702c37256eSGarrett Wollman 
171623dce13SRobert Watson 	inp = sotoinpcb(so);
172623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1732c37256eSGarrett Wollman 	TCPDEBUG1();
1742c37256eSGarrett Wollman 
1750f6385e7SGleb Smirnoff 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1760f6385e7SGleb Smirnoff 		error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1772c37256eSGarrett Wollman 		if (error)
1782c37256eSGarrett Wollman 			goto out;
1790f6385e7SGleb Smirnoff 	}
1802c37256eSGarrett Wollman 
1810f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1820f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1830f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1847669c586SGleb Smirnoff 	if (error)
1850f6385e7SGleb Smirnoff 		goto out;
1860f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1870f6385e7SGleb Smirnoff #ifdef INET6
1880f6385e7SGleb Smirnoff 	if (inp->inp_vflag & INP_IPV6PROTO) {
1890f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV6;
1900f6385e7SGleb Smirnoff 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1910f6385e7SGleb Smirnoff 			inp->inp_vflag |= INP_IPV4;
1920f6385e7SGleb Smirnoff 		inp->in6p_hops = -1;	/* use kernel default */
1930f6385e7SGleb Smirnoff 	}
1940f6385e7SGleb Smirnoff 	else
1950f6385e7SGleb Smirnoff #endif
1960f6385e7SGleb Smirnoff 		inp->inp_vflag |= INP_IPV4;
1970f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1980f6385e7SGleb Smirnoff 	if (tp == NULL) {
1997669c586SGleb Smirnoff 		error = ENOBUFS;
2000f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
2010f6385e7SGleb Smirnoff 		in_pcbfree(inp);
2020f6385e7SGleb Smirnoff 		goto out;
2030f6385e7SGleb Smirnoff 	}
2040f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
2050f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
2060f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
2072c37256eSGarrett Wollman out:
2082c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
2095d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
2100f6385e7SGleb Smirnoff 	return (error);
2112c37256eSGarrett Wollman }
2122c37256eSGarrett Wollman 
2132c37256eSGarrett Wollman /*
2143fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
215a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
216a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
217a152f8a3SRobert Watson  * inpcb state: time wait.
2182c37256eSGarrett Wollman  */
219bc725eafSRobert Watson static void
2203fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2212c37256eSGarrett Wollman {
2223fed74e9SGleb Smirnoff 	struct inpcb *inp;
2232c37256eSGarrett Wollman 	struct tcpcb *tp;
2242c37256eSGarrett Wollman 
2253fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2263fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2273fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2283fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2293fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
230953b5606SRobert Watson 
231a152f8a3SRobert Watson 	tp = intotcpcb(inp);
232a152f8a3SRobert Watson 
233ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
234623dce13SRobert Watson 		/*
235a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
236a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
237a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
238a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
239a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
240a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
241a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
242a152f8a3SRobert Watson 		 * present until timewait ends.
243623dce13SRobert Watson 		 *
244a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
245cea40c48SJulien Charbon 		 *
246cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
247dd388cfdSGleb Smirnoff 		 * four cases to consider:
248cea40c48SJulien Charbon 		 *
2493fed74e9SGleb Smirnoff 		 * #1 tcp_usr_detach is called at tcptw creation time by
250cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
251cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
2523fed74e9SGleb Smirnoff 		 * #2 tcp_usr_detach is called at tcptw creation time by
253dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
254dd388cfdSGleb Smirnoff 		 *  discarded immediately
2553fed74e9SGleb Smirnoff 		 * #3 tcp_usr_detach is called at timewait end (or reuse) by
256cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
257ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
2583fed74e9SGleb Smirnoff 		 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
259cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
260ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
261cea40c48SJulien Charbon 		 *
262cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
263623dce13SRobert Watson 		 */
264ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
265623dce13SRobert Watson 			in_pcbdetach(inp);
266f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2670206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2680206cdb8SBjoern A. Zeeb 			} else {
269f5cf1e5fSJulien Charbon 				/*
270f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
271f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
272f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
273f5cf1e5fSJulien Charbon 				 */
274f5cf1e5fSJulien Charbon #ifdef INVARIANTS
275f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
276f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
277f5cf1e5fSJulien Charbon 				    , __func__);
278f5cf1e5fSJulien Charbon #else
279f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
280f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
281f5cf1e5fSJulien Charbon 				    , __func__);
282f5cf1e5fSJulien Charbon #endif
283f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
284f5cf1e5fSJulien Charbon 			}
285f5cf1e5fSJulien Charbon 		} else {
286623dce13SRobert Watson 			in_pcbdetach(inp);
2878501a69cSRobert Watson 			INP_WUNLOCK(inp);
288623dce13SRobert Watson 		}
289623dce13SRobert Watson 	} else {
290e6e65783SRobert Watson 		/*
291a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
292a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
293a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
294a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
295a152f8a3SRobert Watson 		 * pcb will persist for the time being.
296a152f8a3SRobert Watson 		 *
297a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
298e6e65783SRobert Watson 		 */
299ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
300623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
301623dce13SRobert Watson 			tcp_discardcb(tp);
302623dce13SRobert Watson 			in_pcbdetach(inp);
3030206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
304db3cee51SNavdeep Parhar 		} else {
305a152f8a3SRobert Watson 			in_pcbdetach(inp);
306db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
307db3cee51SNavdeep Parhar 		}
308623dce13SRobert Watson 	}
309623dce13SRobert Watson }
310c78cbc7bSRobert Watson 
311b287c6c7SBjoern A. Zeeb #ifdef INET
3122c37256eSGarrett Wollman /*
3132c37256eSGarrett Wollman  * Give the socket an address.
3142c37256eSGarrett Wollman  */
3152c37256eSGarrett Wollman static int
316b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
3172c37256eSGarrett Wollman {
3182c37256eSGarrett Wollman 	int error = 0;
319f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
320623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3212c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3222c37256eSGarrett Wollman 
32352710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
324f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
325f96603b5SMark Johnston 		/*
326f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
327f96603b5SMark Johnston 		 */
328f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
329*3f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
330f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
331f161d294SMark Johnston 			return (EAFNOSUPPORT);
332f96603b5SMark Johnston 		nam->sa_family = AF_INET;
333f96603b5SMark Johnston 	}
33452710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof(*sinp))
33552710de1SPawel Jakub Dawidek 		return (EINVAL);
336f161d294SMark Johnston 
3372c37256eSGarrett Wollman 	/*
3382c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3392c37256eSGarrett Wollman 	 * to them.
3402c37256eSGarrett Wollman 	 */
341f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
34252710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
34352710de1SPawel Jakub Dawidek 
344623dce13SRobert Watson 	TCPDEBUG0;
345623dce13SRobert Watson 	inp = sotoinpcb(so);
346623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3478501a69cSRobert Watson 	INP_WLOCK(inp);
348ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
349623dce13SRobert Watson 		error = EINVAL;
3502c37256eSGarrett Wollman 		goto out;
351623dce13SRobert Watson 	}
352623dce13SRobert Watson 	tp = intotcpcb(inp);
353623dce13SRobert Watson 	TCPDEBUG1();
354fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
355623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
356fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
357623dce13SRobert Watson out:
358623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3595d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3608501a69cSRobert Watson 	INP_WUNLOCK(inp);
361623dce13SRobert Watson 
362623dce13SRobert Watson 	return (error);
3632c37256eSGarrett Wollman }
364b287c6c7SBjoern A. Zeeb #endif /* INET */
3652c37256eSGarrett Wollman 
366fb59c426SYoshinobu Inoue #ifdef INET6
367fb59c426SYoshinobu Inoue static int
368b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
369fb59c426SYoshinobu Inoue {
370fb59c426SYoshinobu Inoue 	int error = 0;
371f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
372623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3730ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
3744a91aa8fSMichael Tuexen 	u_char vflagsav;
375fb59c426SYoshinobu Inoue 
3760ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
377f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
378f161d294SMark Johnston 		return (EAFNOSUPPORT);
3790ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof(*sin6))
38052710de1SPawel Jakub Dawidek 		return (EINVAL);
381f161d294SMark Johnston 
382fb59c426SYoshinobu Inoue 	/*
383fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
384fb59c426SYoshinobu Inoue 	 * to them.
385fb59c426SYoshinobu Inoue 	 */
386f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
38752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
38852710de1SPawel Jakub Dawidek 
389623dce13SRobert Watson 	TCPDEBUG0;
390623dce13SRobert Watson 	inp = sotoinpcb(so);
391623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3928501a69cSRobert Watson 	INP_WLOCK(inp);
3934a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
394ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
395623dce13SRobert Watson 		error = EINVAL;
396623dce13SRobert Watson 		goto out;
397623dce13SRobert Watson 	}
398623dce13SRobert Watson 	tp = intotcpcb(inp);
399623dce13SRobert Watson 	TCPDEBUG1();
400fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
401fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
402fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
403b287c6c7SBjoern A. Zeeb #ifdef INET
40466ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
4050ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
406fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
4070ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
408fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
409fb59c426SYoshinobu Inoue 
4100ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
411888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
412888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
413888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
414888973f5SMichael Tuexen 				goto out;
415888973f5SMichael Tuexen 			}
416fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
417fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
418b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
419b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
420fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
421fb59c426SYoshinobu Inoue 			goto out;
422fb59c426SYoshinobu Inoue 		}
423fb59c426SYoshinobu Inoue 	}
424b287c6c7SBjoern A. Zeeb #endif
425b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
426fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
427623dce13SRobert Watson out:
4284a91aa8fSMichael Tuexen 	if (error != 0)
4294a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
430623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
4315d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4328501a69cSRobert Watson 	INP_WUNLOCK(inp);
433623dce13SRobert Watson 	return (error);
434fb59c426SYoshinobu Inoue }
435fb59c426SYoshinobu Inoue #endif /* INET6 */
436fb59c426SYoshinobu Inoue 
437b287c6c7SBjoern A. Zeeb #ifdef INET
4382c37256eSGarrett Wollman /*
4392c37256eSGarrett Wollman  * Prepare to accept connections.
4402c37256eSGarrett Wollman  */
4412c37256eSGarrett Wollman static int
442d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4432c37256eSGarrett Wollman {
4442c37256eSGarrett Wollman 	int error = 0;
445f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
446623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4472c37256eSGarrett Wollman 
448623dce13SRobert Watson 	TCPDEBUG0;
449623dce13SRobert Watson 	inp = sotoinpcb(so);
450623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4518501a69cSRobert Watson 	INP_WLOCK(inp);
452ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
453623dce13SRobert Watson 		error = EINVAL;
454623dce13SRobert Watson 		goto out;
455623dce13SRobert Watson 	}
456623dce13SRobert Watson 	tp = intotcpcb(inp);
457623dce13SRobert Watson 	TCPDEBUG1();
4580daccb9cSRobert Watson 	SOCK_LOCK(so);
4590daccb9cSRobert Watson 	error = solisten_proto_check(so);
460fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4610daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
462b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
463fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4640daccb9cSRobert Watson 	if (error == 0) {
46557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
466d374e81eSRobert Watson 		solisten_proto(so, backlog);
46709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
46837cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
46909fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
47009fe6320SNavdeep Parhar #endif
4710daccb9cSRobert Watson 	}
4720daccb9cSRobert Watson 	SOCK_UNLOCK(so);
473623dce13SRobert Watson 
47468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
475281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
47618a75309SPatrick Kelsey 
477623dce13SRobert Watson out:
478623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4795d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4808501a69cSRobert Watson 	INP_WUNLOCK(inp);
481623dce13SRobert Watson 	return (error);
4822c37256eSGarrett Wollman }
483b287c6c7SBjoern A. Zeeb #endif /* INET */
4842c37256eSGarrett Wollman 
485fb59c426SYoshinobu Inoue #ifdef INET6
486fb59c426SYoshinobu Inoue static int
487d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
488fb59c426SYoshinobu Inoue {
489fb59c426SYoshinobu Inoue 	int error = 0;
490f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
491623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4924a91aa8fSMichael Tuexen 	u_char vflagsav;
493fb59c426SYoshinobu Inoue 
494623dce13SRobert Watson 	TCPDEBUG0;
495623dce13SRobert Watson 	inp = sotoinpcb(so);
496623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4978501a69cSRobert Watson 	INP_WLOCK(inp);
498ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
499623dce13SRobert Watson 		error = EINVAL;
500623dce13SRobert Watson 		goto out;
501623dce13SRobert Watson 	}
5024a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
503623dce13SRobert Watson 	tp = intotcpcb(inp);
504623dce13SRobert Watson 	TCPDEBUG1();
5050daccb9cSRobert Watson 	SOCK_LOCK(so);
5060daccb9cSRobert Watson 	error = solisten_proto_check(so);
507fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
5080daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
509fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
51066ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
511fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
512b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
513fb59c426SYoshinobu Inoue 	}
514fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
5150daccb9cSRobert Watson 	if (error == 0) {
51657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
517d374e81eSRobert Watson 		solisten_proto(so, backlog);
51809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
51937cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
52009fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
52109fe6320SNavdeep Parhar #endif
5220daccb9cSRobert Watson 	}
5230daccb9cSRobert Watson 	SOCK_UNLOCK(so);
524623dce13SRobert Watson 
52568bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
526281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
52718a75309SPatrick Kelsey 
5284a91aa8fSMichael Tuexen 	if (error != 0)
5294a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
5304a91aa8fSMichael Tuexen 
531623dce13SRobert Watson out:
532623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
5335d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
5348501a69cSRobert Watson 	INP_WUNLOCK(inp);
535623dce13SRobert Watson 	return (error);
536fb59c426SYoshinobu Inoue }
537fb59c426SYoshinobu Inoue #endif /* INET6 */
538fb59c426SYoshinobu Inoue 
539b287c6c7SBjoern A. Zeeb #ifdef INET
5402c37256eSGarrett Wollman /*
5412c37256eSGarrett Wollman  * Initiate connection to peer.
5422c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5432c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5442c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5452c37256eSGarrett Wollman  * Send initial segment on connection.
5462c37256eSGarrett Wollman  */
5472c37256eSGarrett Wollman static int
548b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5492c37256eSGarrett Wollman {
550109eb549SGleb Smirnoff 	struct epoch_tracker et;
5512c37256eSGarrett Wollman 	int error = 0;
552f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
553623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5542c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5552c37256eSGarrett Wollman 
55657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
557f161d294SMark Johnston 	if (nam->sa_family != AF_INET)
558f161d294SMark Johnston 		return (EAFNOSUPPORT);
559e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
560e29ef13fSDon Lewis 		return (EINVAL);
561f161d294SMark Johnston 
56252710de1SPawel Jakub Dawidek 	/*
56352710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
56452710de1SPawel Jakub Dawidek 	 */
565f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
56652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
567f161d294SMark Johnston 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
568f903a308SMichael Tuexen 		return (EACCES);
569b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
570b89e82ddSJamie Gritton 		return (error);
57175c13541SPoul-Henning Kamp 
572623dce13SRobert Watson 	TCPDEBUG0;
573623dce13SRobert Watson 	inp = sotoinpcb(so);
574623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5758501a69cSRobert Watson 	INP_WLOCK(inp);
576eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
577eb96dc33SJulien Charbon 		error = EADDRINUSE;
578eb96dc33SJulien Charbon 		goto out;
579eb96dc33SJulien Charbon 	}
580eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
581eb96dc33SJulien Charbon 		error = ECONNREFUSED;
582623dce13SRobert Watson 		goto out;
583623dce13SRobert Watson 	}
584623dce13SRobert Watson 	tp = intotcpcb(inp);
585623dce13SRobert Watson 	TCPDEBUG1();
586c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
587b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
588c1604fe4SGleb Smirnoff 		goto out_in_epoch;
58909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
59009fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
59137cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
59209fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
593c1604fe4SGleb Smirnoff 		goto out_in_epoch;
59409fe6320SNavdeep Parhar #endif
59509fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
59655bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
597c1604fe4SGleb Smirnoff out_in_epoch:
598109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
599623dce13SRobert Watson out:
600623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
601e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6028501a69cSRobert Watson 	INP_WUNLOCK(inp);
603623dce13SRobert Watson 	return (error);
6042c37256eSGarrett Wollman }
605b287c6c7SBjoern A. Zeeb #endif /* INET */
6062c37256eSGarrett Wollman 
607fb59c426SYoshinobu Inoue #ifdef INET6
608fb59c426SYoshinobu Inoue static int
609b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
610fb59c426SYoshinobu Inoue {
611109eb549SGleb Smirnoff 	struct epoch_tracker et;
612fb59c426SYoshinobu Inoue 	int error = 0;
613f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
614623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6150ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
6164a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
6174a91aa8fSMichael Tuexen 	u_char vflagsav;
618623dce13SRobert Watson 
619623dce13SRobert Watson 	TCPDEBUG0;
620fb59c426SYoshinobu Inoue 
6210ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
622f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
623f161d294SMark Johnston 		return (EAFNOSUPPORT);
6240ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
625e29ef13fSDon Lewis 		return (EINVAL);
626f161d294SMark Johnston 
62752710de1SPawel Jakub Dawidek 	/*
62852710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
62952710de1SPawel Jakub Dawidek 	 */
630f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
63152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
632fb59c426SYoshinobu Inoue 
633623dce13SRobert Watson 	inp = sotoinpcb(so);
634623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6358501a69cSRobert Watson 	INP_WLOCK(inp);
6364a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6374a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
638eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
639eb96dc33SJulien Charbon 		error = EADDRINUSE;
640eb96dc33SJulien Charbon 		goto out;
641eb96dc33SJulien Charbon 	}
642eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
643eb96dc33SJulien Charbon 		error = ECONNREFUSED;
644623dce13SRobert Watson 		goto out;
645623dce13SRobert Watson 	}
646623dce13SRobert Watson 	tp = intotcpcb(inp);
647623dce13SRobert Watson 	TCPDEBUG1();
648b287c6c7SBjoern A. Zeeb #ifdef INET
649fa046d87SRobert Watson 	/*
650fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
651fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
652fa046d87SRobert Watson 	 * Is this a significant problem?
653fa046d87SRobert Watson 	 */
6540ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
655fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
656fb59c426SYoshinobu Inoue 
657d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
658d46a5312SMaxim Konovalov 			error = EINVAL;
659d46a5312SMaxim Konovalov 			goto out;
660d46a5312SMaxim Konovalov 		}
6615dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6625dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6635dba6adaSMichael Tuexen 			goto out;
6645dba6adaSMichael Tuexen 		}
66533841545SHajimu UMEMOTO 
6660ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
667888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
668888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
669888973f5SMichael Tuexen 			goto out;
670888973f5SMichael Tuexen 		}
671f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
672f903a308SMichael Tuexen 			error = EACCES;
6732cf21ae5SRandall Stewart 			goto out;
6742cf21ae5SRandall Stewart 		}
675b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
676b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
677413628a7SBjoern A. Zeeb 			goto out;
6784a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6794a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
680c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
681b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
682c1604fe4SGleb Smirnoff 			goto out_in_epoch;
68309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
68409fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
685adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
68609fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
687c1604fe4SGleb Smirnoff 			goto out_in_epoch;
68809fe6320SNavdeep Parhar #endif
68955bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
690c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6915dba6adaSMichael Tuexen 	} else {
6925dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6935dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6945dba6adaSMichael Tuexen 			goto out;
6955dba6adaSMichael Tuexen 		}
696fb59c426SYoshinobu Inoue 	}
697b287c6c7SBjoern A. Zeeb #endif
6984a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6994a91aa8fSMichael Tuexen 		goto out;
700fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
701fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
702dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
703b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
704fb59c426SYoshinobu Inoue 		goto out;
70509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
70609fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
707adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
70809fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
70909fe6320SNavdeep Parhar 		goto out;
71009fe6320SNavdeep Parhar #endif
71109fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
712109eb549SGleb Smirnoff 	NET_EPOCH_ENTER(et);
71355bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
7147754e281SBjoern A. Zeeb #ifdef INET
715c1604fe4SGleb Smirnoff out_in_epoch:
7167754e281SBjoern A. Zeeb #endif
717109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
718623dce13SRobert Watson out:
7194a91aa8fSMichael Tuexen 	/*
7204a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
7214a91aa8fSMichael Tuexen 	 * the flags we modified.
7224a91aa8fSMichael Tuexen 	 */
7234a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
7244a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
7254a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
7264a91aa8fSMichael Tuexen 	}
7274a91aa8fSMichael Tuexen 
728623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
7295d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
7308501a69cSRobert Watson 	INP_WUNLOCK(inp);
731623dce13SRobert Watson 	return (error);
732fb59c426SYoshinobu Inoue }
733fb59c426SYoshinobu Inoue #endif /* INET6 */
734fb59c426SYoshinobu Inoue 
7352c37256eSGarrett Wollman /*
7362c37256eSGarrett Wollman  * Initiate disconnect from peer.
7372c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7382c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7392c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7402c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7412c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7422c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7432c37256eSGarrett Wollman  *
7442c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7452c37256eSGarrett Wollman  */
7462c37256eSGarrett Wollman static int
7472c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7482c37256eSGarrett Wollman {
749f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
750623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7516573d758SMatt Macy 	struct epoch_tracker et;
752623dce13SRobert Watson 	int error = 0;
7532c37256eSGarrett Wollman 
754623dce13SRobert Watson 	TCPDEBUG0;
75597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
756623dce13SRobert Watson 	inp = sotoinpcb(so);
757623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7588501a69cSRobert Watson 	INP_WLOCK(inp);
759489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
760489dcc92SJulien Charbon 		goto out;
761489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
76221367f63SSam Leffler 		error = ECONNRESET;
763623dce13SRobert Watson 		goto out;
764623dce13SRobert Watson 	}
765623dce13SRobert Watson 	tp = intotcpcb(inp);
766623dce13SRobert Watson 	TCPDEBUG1();
767623dce13SRobert Watson 	tcp_disconnect(tp);
768623dce13SRobert Watson out:
769623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7705d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7718501a69cSRobert Watson 	INP_WUNLOCK(inp);
77297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
773623dce13SRobert Watson 	return (error);
7742c37256eSGarrett Wollman }
7752c37256eSGarrett Wollman 
776b287c6c7SBjoern A. Zeeb #ifdef INET
7772c37256eSGarrett Wollman /*
7788296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7798296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7802c37256eSGarrett Wollman  */
7812c37256eSGarrett Wollman static int
78257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7832c37256eSGarrett Wollman {
7842c37256eSGarrett Wollman 	int error = 0;
785f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7861db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
78726ef6ac4SDon Lewis 	struct in_addr addr;
78826ef6ac4SDon Lewis 	in_port_t port = 0;
7891db24ffbSJonathan Lemon 	TCPDEBUG0;
7902c37256eSGarrett Wollman 
7913d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7923d2d3ef4SRobert Watson 		return (ECONNABORTED);
793f76fcf6dSJeffrey Hsu 
794f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
795623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7968501a69cSRobert Watson 	INP_WLOCK(inp);
797ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7983d2d3ef4SRobert Watson 		error = ECONNABORTED;
799623dce13SRobert Watson 		goto out;
800623dce13SRobert Watson 	}
8011db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
8021db24ffbSJonathan Lemon 	TCPDEBUG1();
803f76fcf6dSJeffrey Hsu 
804f76fcf6dSJeffrey Hsu 	/*
80554d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
80626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
80726ef6ac4SDon Lewis 	 * release the lock.
808f76fcf6dSJeffrey Hsu 	 */
80926ef6ac4SDon Lewis 	port = inp->inp_fport;
81026ef6ac4SDon Lewis 	addr = inp->inp_faddr;
811f76fcf6dSJeffrey Hsu 
812623dce13SRobert Watson out:
813623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8145d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8158501a69cSRobert Watson 	INP_WUNLOCK(inp);
81626ef6ac4SDon Lewis 	if (error == 0)
81726ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
81826ef6ac4SDon Lewis 	return error;
8192c37256eSGarrett Wollman }
820b287c6c7SBjoern A. Zeeb #endif /* INET */
8212c37256eSGarrett Wollman 
822fb59c426SYoshinobu Inoue #ifdef INET6
823fb59c426SYoshinobu Inoue static int
824fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
825fb59c426SYoshinobu Inoue {
826f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
827fb59c426SYoshinobu Inoue 	int error = 0;
8281db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
82926ef6ac4SDon Lewis 	struct in_addr addr;
83026ef6ac4SDon Lewis 	struct in6_addr addr6;
8316573d758SMatt Macy 	struct epoch_tracker et;
83226ef6ac4SDon Lewis 	in_port_t port = 0;
83326ef6ac4SDon Lewis 	int v4 = 0;
8341db24ffbSJonathan Lemon 	TCPDEBUG0;
835fb59c426SYoshinobu Inoue 
836b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
837b4470c16SRobert Watson 		return (ECONNABORTED);
838f76fcf6dSJeffrey Hsu 
839f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
840623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
84197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8428501a69cSRobert Watson 	INP_WLOCK(inp);
843ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
84421367f63SSam Leffler 		error = ECONNABORTED;
845623dce13SRobert Watson 		goto out;
846623dce13SRobert Watson 	}
8471db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
8481db24ffbSJonathan Lemon 	TCPDEBUG1();
849623dce13SRobert Watson 
85026ef6ac4SDon Lewis 	/*
85126ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
85226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
85326ef6ac4SDon Lewis 	 * release the lock.
85426ef6ac4SDon Lewis 	 */
85526ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
85626ef6ac4SDon Lewis 		v4 = 1;
85726ef6ac4SDon Lewis 		port = inp->inp_fport;
85826ef6ac4SDon Lewis 		addr = inp->inp_faddr;
85926ef6ac4SDon Lewis 	} else {
86026ef6ac4SDon Lewis 		port = inp->inp_fport;
86126ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
86226ef6ac4SDon Lewis 	}
86326ef6ac4SDon Lewis 
864623dce13SRobert Watson out:
865623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8665d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8678501a69cSRobert Watson 	INP_WUNLOCK(inp);
86897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
86926ef6ac4SDon Lewis 	if (error == 0) {
87026ef6ac4SDon Lewis 		if (v4)
87126ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
87226ef6ac4SDon Lewis 		else
87326ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
87426ef6ac4SDon Lewis 	}
87526ef6ac4SDon Lewis 	return error;
876fb59c426SYoshinobu Inoue }
877fb59c426SYoshinobu Inoue #endif /* INET6 */
878f76fcf6dSJeffrey Hsu 
879f76fcf6dSJeffrey Hsu /*
8802c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8812c37256eSGarrett Wollman  */
8822c37256eSGarrett Wollman static int
8832c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8842c37256eSGarrett Wollman {
8852c37256eSGarrett Wollman 	int error = 0;
886f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
887623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8886573d758SMatt Macy 	struct epoch_tracker et;
8892c37256eSGarrett Wollman 
890623dce13SRobert Watson 	TCPDEBUG0;
89197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
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)) {
89621367f63SSam Leffler 		error = ECONNRESET;
897623dce13SRobert Watson 		goto out;
898623dce13SRobert Watson 	}
899623dce13SRobert Watson 	tp = intotcpcb(inp);
900623dce13SRobert Watson 	TCPDEBUG1();
9012c37256eSGarrett Wollman 	socantsendmore(so);
902623dce13SRobert Watson 	tcp_usrclosed(tp);
903ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
90455bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
905623dce13SRobert Watson 
906623dce13SRobert Watson out:
907623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
9085d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
9098501a69cSRobert Watson 	INP_WUNLOCK(inp);
91097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
911623dce13SRobert Watson 
912623dce13SRobert Watson 	return (error);
9132c37256eSGarrett Wollman }
9142c37256eSGarrett Wollman 
9152c37256eSGarrett Wollman /*
9162c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
9172c37256eSGarrett Wollman  */
9182c37256eSGarrett Wollman static int
9192c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
9202c37256eSGarrett Wollman {
921109eb549SGleb Smirnoff 	struct epoch_tracker et;
922f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
923623dce13SRobert Watson 	struct tcpcb *tp = NULL;
924623dce13SRobert Watson 	int error = 0;
9252c37256eSGarrett Wollman 
926623dce13SRobert Watson 	TCPDEBUG0;
927623dce13SRobert Watson 	inp = sotoinpcb(so);
928623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
9298501a69cSRobert Watson 	INP_WLOCK(inp);
930ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
93121367f63SSam Leffler 		error = ECONNRESET;
932623dce13SRobert Watson 		goto out;
933623dce13SRobert Watson 	}
934623dce13SRobert Watson 	tp = intotcpcb(inp);
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;
94642ce7937SGleb Smirnoff 	NET_EPOCH_ENTER(et);
94709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
94809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
94909fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
950460cf046SNavdeep Parhar 	else
95109fe6320SNavdeep Parhar #endif
95255bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
953109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
954623dce13SRobert Watson out:
955623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9565d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
9578501a69cSRobert Watson 	INP_WUNLOCK(inp);
958623dce13SRobert Watson 	return (error);
9592c37256eSGarrett Wollman }
9602c37256eSGarrett Wollman 
9612c37256eSGarrett Wollman /*
9622c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9639c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9649c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9659c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9669c9906e9SPeter Wemm  * generally are caller-frees.
9672c37256eSGarrett Wollman  */
9682c37256eSGarrett Wollman static int
96957bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
970b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9712c37256eSGarrett Wollman {
97297a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9732c37256eSGarrett Wollman 	int error = 0;
974f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
975623dce13SRobert Watson 	struct tcpcb *tp = NULL;
976888973f5SMichael Tuexen #ifdef INET
97751e08d53SMichael Tuexen #ifdef INET6
97851e08d53SMichael Tuexen 	struct sockaddr_in sin;
97951e08d53SMichael Tuexen #endif
98051e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
981888973f5SMichael Tuexen #endif
982fb59c426SYoshinobu Inoue #ifdef INET6
983fb59c426SYoshinobu Inoue 	int isipv6;
984fb59c426SYoshinobu Inoue #endif
9854a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9864a91aa8fSMichael Tuexen 	u_char vflagsav;
9874a91aa8fSMichael Tuexen 	bool restoreflags;
9889c9906e9SPeter Wemm 	TCPDEBUG0;
9892c37256eSGarrett Wollman 
990f76fcf6dSJeffrey Hsu 	/*
99197a95ee1SGleb Smirnoff 	 * We require the pcbinfo "read lock" if we will close the socket
99297a95ee1SGleb Smirnoff 	 * as part of this call.
993f76fcf6dSJeffrey Hsu 	 */
99497a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
995f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
996623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9978501a69cSRobert Watson 	INP_WLOCK(inp);
9984a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
9994a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
10004a91aa8fSMichael Tuexen 	restoreflags = false;
1001ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
10027ff0b850SAndre Oppermann 		if (control)
10037ff0b850SAndre Oppermann 			m_freem(control);
100421367f63SSam Leffler 		error = ECONNRESET;
10059c9906e9SPeter Wemm 		goto out;
10069c9906e9SPeter Wemm 	}
1007d8acd268SMark Johnston 	if (control != NULL) {
1008d8acd268SMark Johnston 		/* TCP doesn't do control messages (rights, creds, etc) */
1009d8acd268SMark Johnston 		if (control->m_len) {
1010d8acd268SMark Johnston 			m_freem(control);
1011d8acd268SMark Johnston 			error = EINVAL;
1012d8acd268SMark Johnston 			goto out;
1013d8acd268SMark Johnston 		}
1014d8acd268SMark Johnston 		m_freem(control);	/* empty control, just free it */
1015d8acd268SMark Johnston 		control = NULL;
1016d8acd268SMark Johnston 	}
10179c9906e9SPeter Wemm 	tp = intotcpcb(inp);
10187d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
10197d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
1020d3b6c96bSRandall Stewart 		goto out;
10217d2608a5SMark Johnston 
10229c9906e9SPeter Wemm 	TCPDEBUG1();
1023888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
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) {
11222c37256eSGarrett Wollman 			/*
11232c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11242c37256eSGarrett Wollman 			 * initialize window to default value, and
11250c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11262c37256eSGarrett Wollman 			 */
1127fb59c426SYoshinobu Inoue #ifdef INET6
1128fb59c426SYoshinobu Inoue 			if (isipv6)
1129b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1130fb59c426SYoshinobu Inoue #endif /* INET6 */
1131b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1132b287c6c7SBjoern A. Zeeb 			else
1133b287c6c7SBjoern A. Zeeb #endif
1134b287c6c7SBjoern A. Zeeb #ifdef INET
1135888973f5SMichael Tuexen 				error = tcp_connect(tp,
1136888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1137b287c6c7SBjoern A. Zeeb #endif
11384a91aa8fSMichael Tuexen 			/*
11394a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11404a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11414a91aa8fSMichael Tuexen 			 * operations fail.
11424a91aa8fSMichael Tuexen 			 */
11434a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11444a91aa8fSMichael Tuexen 				restoreflags = false;
11454a91aa8fSMichael Tuexen 
11467d2608a5SMark Johnston 			if (error) {
11477d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11487d2608a5SMark Johnston 				sbflush(&so->so_snd);
11492c37256eSGarrett Wollman 				goto out;
11507d2608a5SMark Johnston 			}
1151c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1152c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
115318a75309SPatrick Kelsey 			else {
11542c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11552c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11562c37256eSGarrett Wollman 			}
1157c560df6fSPatrick Kelsey 		}
11582c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11592c37256eSGarrett Wollman 			/*
11602c37256eSGarrett Wollman 			 * Close the send side of the connection after
11612c37256eSGarrett Wollman 			 * the data is sent.
11622c37256eSGarrett Wollman 			 */
11632c37256eSGarrett Wollman 			socantsendmore(so);
1164623dce13SRobert Watson 			tcp_usrclosed(tp);
11652c37256eSGarrett Wollman 		}
1166e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1167e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1168e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1169e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1170e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1171e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1172e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1173e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1174e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1175e854dd38SRandall Stewart 		}
11762cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11772cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1178b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1179b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
118055bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
1181b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1182b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1183b0acefa8SBill Fenner 		}
11842c37256eSGarrett Wollman 	} else {
1185623dce13SRobert Watson 		/*
1186623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1187623dce13SRobert Watson 		 */
1188d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11892c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1190d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11912c37256eSGarrett Wollman 			error = ENOBUFS;
11922c37256eSGarrett Wollman 			goto out;
11932c37256eSGarrett Wollman 		}
11942c37256eSGarrett Wollman 		/*
11952c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11962c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11972c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11982c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11992c37256eSGarrett Wollman 		 * of data past the urgent section.
12002c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
12012c37256eSGarrett Wollman 		 */
1202651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1203d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
12047d2608a5SMark Johnston 		m = NULL;
1205ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1206ef53690bSGarrett Wollman 			/*
1207ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1208ef53690bSGarrett Wollman 			 * initialize window to default value, and
12090c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1210ef53690bSGarrett Wollman 			 */
121118a75309SPatrick Kelsey 
1212c560df6fSPatrick Kelsey 			/*
1213c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1214c560df6fSPatrick Kelsey 			 */
1215c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1216c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1217fb59c426SYoshinobu Inoue #ifdef INET6
1218fb59c426SYoshinobu Inoue 			if (isipv6)
1219b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1220fb59c426SYoshinobu Inoue #endif /* INET6 */
1221b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1222b287c6c7SBjoern A. Zeeb 			else
1223b287c6c7SBjoern A. Zeeb #endif
1224b287c6c7SBjoern A. Zeeb #ifdef INET
1225888973f5SMichael Tuexen 				error = tcp_connect(tp,
1226888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1227b287c6c7SBjoern A. Zeeb #endif
12284a91aa8fSMichael Tuexen 			/*
12294a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12304a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12314a91aa8fSMichael Tuexen 			 * operations fail.
12324a91aa8fSMichael Tuexen 			 */
12334a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12344a91aa8fSMichael Tuexen 				restoreflags = false;
12354a91aa8fSMichael Tuexen 
12367d2608a5SMark Johnston 			if (error != 0) {
12377d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
12387d2608a5SMark Johnston 				sbflush(&so->so_snd);
1239ef53690bSGarrett Wollman 				goto out;
12407d2608a5SMark Johnston 			}
1241ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1242ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1243623dce13SRobert Watson 		}
1244300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12457d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
12462cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
124755bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
12482cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12492c37256eSGarrett Wollman 		}
12502cbcd3c1SGleb Smirnoff 	}
12512529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12522529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12532529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12542529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12552529f56eSJonathan T. Looney 	    0, NULL, false);
12567d2608a5SMark Johnston 
1257d1401c90SRobert Watson out:
12584a91aa8fSMichael Tuexen 	/*
12597d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
12607d2608a5SMark Johnston 	 * responsible for freeing memory.
12617d2608a5SMark Johnston 	 */
12627d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12637d2608a5SMark Johnston 		m_freem(m);
12647d2608a5SMark Johnston 
12657d2608a5SMark Johnston 	/*
12664a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12674a91aa8fSMichael Tuexen 	 * restore the original flags.
12684a91aa8fSMichael Tuexen 	 */
12694a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12704a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12714a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12724a91aa8fSMichael Tuexen 	}
1273d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12742c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12755d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12765d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12778501a69cSRobert Watson 	INP_WUNLOCK(inp);
127897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
127973fddedaSPeter Grehan 	return (error);
12802c37256eSGarrett Wollman }
12812c37256eSGarrett Wollman 
12822cbcd3c1SGleb Smirnoff static int
12832cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12842cbcd3c1SGleb Smirnoff {
1285109eb549SGleb Smirnoff 	struct epoch_tracker et;
12862cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12872cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12882cbcd3c1SGleb Smirnoff 	int error;
12892cbcd3c1SGleb Smirnoff 
12902cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12912cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
12922cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12932cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
129482334850SJohn Baldwin 		mb_free_notready(m, count);
12952cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12962cbcd3c1SGleb Smirnoff 	}
12972cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12982cbcd3c1SGleb Smirnoff 
12992cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
13002cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
13012cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1302109eb549SGleb Smirnoff 	if (error == 0) {
1303109eb549SGleb Smirnoff 		NET_EPOCH_ENTER(et);
130455bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
1305109eb549SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1306109eb549SGleb Smirnoff 	}
13072cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
13082cbcd3c1SGleb Smirnoff 
13092cbcd3c1SGleb Smirnoff 	return (error);
13102cbcd3c1SGleb Smirnoff }
13112cbcd3c1SGleb Smirnoff 
13122c37256eSGarrett Wollman /*
1313a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
13142c37256eSGarrett Wollman  */
1315ac45e92fSRobert Watson static void
13162c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
13172c37256eSGarrett Wollman {
1318f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1319a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13206573d758SMatt Macy 	struct epoch_tracker et;
1321623dce13SRobert Watson 	TCPDEBUG0;
1322c78cbc7bSRobert Watson 
1323ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1324c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1325c78cbc7bSRobert Watson 
132697a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13278501a69cSRobert Watson 	INP_WLOCK(inp);
1328c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1329c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1330c78cbc7bSRobert Watson 
1331c78cbc7bSRobert Watson 	/*
1332a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1333c78cbc7bSRobert Watson 	 */
1334ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1335ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1336c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1337a152f8a3SRobert Watson 		TCPDEBUG1();
13388fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13398fa799bdSJonathan T. Looney 		if (tp == NULL)
13408fa799bdSJonathan T. Looney 			goto dropped;
1341a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13425d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1343c78cbc7bSRobert Watson 	}
1344ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1345a152f8a3SRobert Watson 		SOCK_LOCK(so);
1346a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1347a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1348ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1349a152f8a3SRobert Watson 	}
13508501a69cSRobert Watson 	INP_WUNLOCK(inp);
13518fa799bdSJonathan T. Looney dropped:
135297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1353a152f8a3SRobert Watson }
1354a152f8a3SRobert Watson 
1355a152f8a3SRobert Watson /*
1356a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1357a152f8a3SRobert Watson  */
1358a152f8a3SRobert Watson static void
1359a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1360a152f8a3SRobert Watson {
1361a152f8a3SRobert Watson 	struct inpcb *inp;
1362a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13636573d758SMatt Macy 	struct epoch_tracker et;
1364a152f8a3SRobert Watson 	TCPDEBUG0;
1365a152f8a3SRobert Watson 
1366a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1367a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1368a152f8a3SRobert Watson 
136997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13708501a69cSRobert Watson 	INP_WLOCK(inp);
1371a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1372a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1373a152f8a3SRobert Watson 
1374a152f8a3SRobert Watson 	/*
1375a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1376a152f8a3SRobert Watson 	 * a disconnect.
1377a152f8a3SRobert Watson 	 */
1378ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1379ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1380a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1381a152f8a3SRobert Watson 		TCPDEBUG1();
1382a152f8a3SRobert Watson 		tcp_disconnect(tp);
1383a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
13845d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1385a152f8a3SRobert Watson 	}
1386ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1387a152f8a3SRobert Watson 		SOCK_LOCK(so);
1388a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1389a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1390ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1391a152f8a3SRobert Watson 	}
13928501a69cSRobert Watson 	INP_WUNLOCK(inp);
139397a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13942c37256eSGarrett Wollman }
13952c37256eSGarrett Wollman 
1396d3b6c96bSRandall Stewart static int
1397d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1398d3b6c96bSRandall Stewart {
1399d3b6c96bSRandall Stewart 	/*
1400d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1401d3b6c96bSRandall Stewart 	 * specified then it does not always support
1402d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1403d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1404d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1405d3b6c96bSRandall Stewart 	 */
1406d3b6c96bSRandall Stewart 	int ret = 0;
1407d3b6c96bSRandall Stewart 
1408d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1409d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1410d3b6c96bSRandall Stewart 	}
1411d3b6c96bSRandall Stewart 	return (ret);
1412d3b6c96bSRandall Stewart }
1413d3b6c96bSRandall Stewart 
14142c37256eSGarrett Wollman /*
14152c37256eSGarrett Wollman  * Receive out-of-band data.
14162c37256eSGarrett Wollman  */
14172c37256eSGarrett Wollman static int
14182c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
14192c37256eSGarrett Wollman {
14202c37256eSGarrett Wollman 	int error = 0;
1421f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1422623dce13SRobert Watson 	struct tcpcb *tp = NULL;
14232c37256eSGarrett Wollman 
1424623dce13SRobert Watson 	TCPDEBUG0;
1425623dce13SRobert Watson 	inp = sotoinpcb(so);
1426623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
14278501a69cSRobert Watson 	INP_WLOCK(inp);
1428ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
142921367f63SSam Leffler 		error = ECONNRESET;
1430623dce13SRobert Watson 		goto out;
1431623dce13SRobert Watson 	}
1432623dce13SRobert Watson 	tp = intotcpcb(inp);
1433d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1434d3b6c96bSRandall Stewart 	if (error) {
1435d3b6c96bSRandall Stewart 		goto out;
1436d3b6c96bSRandall Stewart 	}
1437623dce13SRobert Watson 	TCPDEBUG1();
14382c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1439c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
14404cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
14414cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
14422c37256eSGarrett Wollman 		error = EINVAL;
14432c37256eSGarrett Wollman 		goto out;
14442c37256eSGarrett Wollman 	}
14452c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
14462c37256eSGarrett Wollman 		error = EWOULDBLOCK;
14472c37256eSGarrett Wollman 		goto out;
14482c37256eSGarrett Wollman 	}
14492c37256eSGarrett Wollman 	m->m_len = 1;
14502c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
14512c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
14522c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1453623dce13SRobert Watson 
1454623dce13SRobert Watson out:
1455623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14565d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14578501a69cSRobert Watson 	INP_WUNLOCK(inp);
1458623dce13SRobert Watson 	return (error);
14592c37256eSGarrett Wollman }
14602c37256eSGarrett Wollman 
1461b287c6c7SBjoern A. Zeeb #ifdef INET
14622c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1463756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1464756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1465756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1466756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1467756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1468756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1469756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1470756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1471756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
147254d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1473756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1474756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1475756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14762cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1477756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
147854d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1479a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1480a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
14812c37256eSGarrett Wollman };
1482b287c6c7SBjoern A. Zeeb #endif /* INET */
1483df8bae1dSRodney W. Grimes 
1484fb59c426SYoshinobu Inoue #ifdef INET6
1485fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1486756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1487756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1488756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1489756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1490756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1491756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1492756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1493756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1494756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1495756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1496756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1497756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1498756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14992cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1500756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1501756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1502a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1503a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1504fb59c426SYoshinobu Inoue };
1505fb59c426SYoshinobu Inoue #endif /* INET6 */
1506fb59c426SYoshinobu Inoue 
1507b287c6c7SBjoern A. Zeeb #ifdef INET
1508a0292f23SGarrett Wollman /*
1509a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1510a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
15115200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
15125200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
15135200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
15145200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1515a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1516a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1517a0292f23SGarrett Wollman  */
15180312fbe9SPoul-Henning Kamp static int
1519ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1520a0292f23SGarrett Wollman {
1521a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1522a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
15235200e00eSIan Dowse 	struct in_addr laddr;
15245200e00eSIan Dowse 	u_short lport;
1525c3229e05SDavid Greenman 	int error;
1526a0292f23SGarrett Wollman 
1527c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
15288501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1529fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1530623dce13SRobert Watson 
153125102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
15324616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15334616026fSErmal Luçi 		if (error)
1534fa046d87SRobert Watson 			goto out;
1535a0292f23SGarrett Wollman 	}
1536a0292f23SGarrett Wollman 
1537a0292f23SGarrett Wollman 	/*
1538a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1539a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1540a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1541a0292f23SGarrett Wollman 	 */
15425200e00eSIan Dowse 	laddr = inp->inp_laddr;
15435200e00eSIan Dowse 	lport = inp->inp_lport;
15445200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1545b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
15465200e00eSIan Dowse 	if (error && oinp == NULL)
1547fa046d87SRobert Watson 		goto out;
1548fa046d87SRobert Watson 	if (oinp) {
1549fa046d87SRobert Watson 		error = EADDRINUSE;
1550fa046d87SRobert Watson 		goto out;
1551fa046d87SRobert Watson 	}
155225102351SMike Karels 	/* Handle initial bind if it hadn't been done in advance. */
155325102351SMike Karels 	if (inp->inp_lport == 0) {
155425102351SMike Karels 		inp->inp_lport = lport;
155525102351SMike Karels 		if (in_pcbinshash(inp) != 0) {
155625102351SMike Karels 			inp->inp_lport = 0;
155725102351SMike Karels 			error = EAGAIN;
155825102351SMike Karels 			goto out;
155925102351SMike Karels 		}
156025102351SMike Karels 	}
15615200e00eSIan Dowse 	inp->inp_laddr = laddr;
156215bd2b43SDavid Greenman 	in_pcbrehash(inp);
1563fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1564a0292f23SGarrett Wollman 
1565087b55eaSAndre Oppermann 	/*
1566087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1567087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1568087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1569087b55eaSAndre Oppermann 	 */
1570a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15719b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1572a0292f23SGarrett Wollman 		tp->request_r_scale++;
1573a0292f23SGarrett Wollman 
1574a0292f23SGarrett Wollman 	soisconnecting(so);
157578b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
157657f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15778e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15788e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15798e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1580a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1581a45d2726SAndras Olah 
1582a0292f23SGarrett Wollman 	return 0;
1583fa046d87SRobert Watson 
1584fa046d87SRobert Watson out:
1585fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1586fa046d87SRobert Watson 	return (error);
1587a0292f23SGarrett Wollman }
1588b287c6c7SBjoern A. Zeeb #endif /* INET */
1589a0292f23SGarrett Wollman 
1590fb59c426SYoshinobu Inoue #ifdef INET6
1591fb59c426SYoshinobu Inoue static int
1592ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1593fb59c426SYoshinobu Inoue {
1594a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1595fb59c426SYoshinobu Inoue 	int error;
1596fb59c426SYoshinobu Inoue 
15978501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1598fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1599623dce13SRobert Watson 
160025102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
16014616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
16024616026fSErmal Luçi 		if (error)
1603fa046d87SRobert Watson 			goto out;
1604fb59c426SYoshinobu Inoue 	}
1605a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1606a7e201bbSAndrey V. Elsukov 	if (error != 0)
1607b598155aSRobert Watson 		goto out;
1608fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1609fb59c426SYoshinobu Inoue 
1610fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1611fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1612970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1613fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1614fb59c426SYoshinobu Inoue 
1615a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
161678b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
161757f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
16188e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
16198e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
16208e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1621fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1622fb59c426SYoshinobu Inoue 
1623fb59c426SYoshinobu Inoue 	return 0;
1624fa046d87SRobert Watson 
1625fa046d87SRobert Watson out:
1626fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1627fa046d87SRobert Watson 	return error;
1628fb59c426SYoshinobu Inoue }
1629fb59c426SYoshinobu Inoue #endif /* INET6 */
1630fb59c426SYoshinobu Inoue 
1631cfe8b629SGarrett Wollman /*
1632b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1633b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1634b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1635b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1636b8af5dfaSRobert Watson  * from Linux.
1637b8af5dfaSRobert Watson  */
1638b8af5dfaSRobert Watson static void
1639ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1640b8af5dfaSRobert Watson {
1641b8af5dfaSRobert Watson 
16428501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1643b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1644b8af5dfaSRobert Watson 
1645b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1646b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1647b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
16483529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1649b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1650b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1651b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1652b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1653b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1654b8af5dfaSRobert Watson 	}
16553cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT)
16565a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
16571baaf834SBruce M Simpson 
165843d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
16593ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
16601baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
16611baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16621baaf834SBruce M Simpson 
1663b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1664b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1665b8af5dfaSRobert Watson 
1666b8af5dfaSRobert Watson 	/*
1667b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1668b8af5dfaSRobert Watson 	 */
1669c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1670535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1671b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16721c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1673535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
167443d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
167543d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1676f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1677f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1678f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1679a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1680a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1681a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1682a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1683a6456410SNavdeep Parhar 	}
1684a6456410SNavdeep Parhar #endif
1685b8af5dfaSRobert Watson }
1686b8af5dfaSRobert Watson 
1687b8af5dfaSRobert Watson /*
16881e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16891e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16901e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16911e8f5ffaSRobert Watson  * option.
1692cfe8b629SGarrett Wollman  */
1693bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16948501a69cSRobert Watson 	INP_WLOCK(inp);							\
1695ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
16968501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1697bac5bedfSConrad Meyer 		cleanup;						\
16981e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16991e8f5ffaSRobert Watson 	}								\
17001e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
17011e8f5ffaSRobert Watson } while(0)
1702bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
17031e8f5ffaSRobert Watson 
1704df8bae1dSRodney W. Grimes int
1705ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1706df8bae1dSRodney W. Grimes {
170755bceb1eSRandall Stewart 	int	error;
1708df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1709cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
171055bceb1eSRandall Stewart 	struct tcp_function_block *blk;
171155bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1712df8bae1dSRodney W. Grimes 
1713cfe8b629SGarrett Wollman 	error = 0;
1714df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1715623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1716cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1717fb59c426SYoshinobu Inoue #ifdef INET6
17185cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
1719fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
17205dff1c38SMichael Tuexen 			/*
17215dff1c38SMichael Tuexen 			 * In case of the IPV6_USE_MIN_MTU socket option,
17225dff1c38SMichael Tuexen 			 * the INC_IPV6MINMTU flag to announce a corresponding
17235dff1c38SMichael Tuexen 			 * MSS during the initial handshake.
17245dff1c38SMichael Tuexen 			 * If the TCP connection is not in the front states,
17255dff1c38SMichael Tuexen 			 * just reduce the MSS being used.
17265dff1c38SMichael Tuexen 			 * This avoids the sending of TCP segments which will
17275dff1c38SMichael Tuexen 			 * be fragmented at the IPv6 layer.
17285dff1c38SMichael Tuexen 			 */
17295dff1c38SMichael Tuexen 			if ((error == 0) &&
17305dff1c38SMichael Tuexen 			    (sopt->sopt_dir == SOPT_SET) &&
17315dff1c38SMichael Tuexen 			    (sopt->sopt_level == IPPROTO_IPV6) &&
17325dff1c38SMichael Tuexen 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
17335dff1c38SMichael Tuexen 				INP_WLOCK(inp);
17345dff1c38SMichael Tuexen 				if ((inp->inp_flags &
17355dff1c38SMichael Tuexen 				    (INP_TIMEWAIT | INP_DROPPED))) {
17365dff1c38SMichael Tuexen 					INP_WUNLOCK(inp);
17375dff1c38SMichael Tuexen 					return (ECONNRESET);
17385dff1c38SMichael Tuexen 				}
17395dff1c38SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
17405dff1c38SMichael Tuexen 				tp = intotcpcb(inp);
17415dff1c38SMichael Tuexen 				if ((tp->t_state >= TCPS_SYN_SENT) &&
17425dff1c38SMichael Tuexen 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
17435dff1c38SMichael Tuexen 					struct ip6_pktopts *opt;
17445dff1c38SMichael Tuexen 
17455dff1c38SMichael Tuexen 					opt = inp->in6p_outputopts;
17465dff1c38SMichael Tuexen 					if ((opt != NULL) &&
17475dff1c38SMichael Tuexen 					    (opt->ip6po_minmtu ==
17485dff1c38SMichael Tuexen 					    IP6PO_MINMTU_ALL)) {
17495dff1c38SMichael Tuexen 						if (tp->t_maxseg > TCP6_MSS) {
17505dff1c38SMichael Tuexen 							tp->t_maxseg = TCP6_MSS;
17515dff1c38SMichael Tuexen 						}
17525dff1c38SMichael Tuexen 					}
17535dff1c38SMichael Tuexen 				}
17545dff1c38SMichael Tuexen 				INP_WUNLOCK(inp);
17555dff1c38SMichael Tuexen 			}
1756b287c6c7SBjoern A. Zeeb 		}
1757fb59c426SYoshinobu Inoue #endif /* INET6 */
1758b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1759b287c6c7SBjoern A. Zeeb 		else
1760b287c6c7SBjoern A. Zeeb #endif
1761b287c6c7SBjoern A. Zeeb #ifdef INET
1762b287c6c7SBjoern A. Zeeb 		{
1763cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
17641e8f5ffaSRobert Watson 		}
17651e8f5ffaSRobert Watson #endif
1766df8bae1dSRodney W. Grimes 		return (error);
1767df8bae1dSRodney W. Grimes 	}
176868cea2b1SJohn Baldwin 	INP_WLOCK(inp);
1769ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17708501a69cSRobert Watson 		INP_WUNLOCK(inp);
17711e8f5ffaSRobert Watson 		return (ECONNRESET);
1772623dce13SRobert Watson 	}
177355bceb1eSRandall Stewart 	tp = intotcpcb(inp);
177455bceb1eSRandall Stewart 	/*
177555bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
177655bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
177755bceb1eSRandall Stewart 	 */
177855bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
177955bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
178055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
178155bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
178255bceb1eSRandall Stewart 		    sizeof fsn);
178355bceb1eSRandall Stewart 		if (error)
178455bceb1eSRandall Stewart 			return (error);
178555bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
178655bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
178755bceb1eSRandall Stewart 		if (blk == NULL) {
178855bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
178955bceb1eSRandall Stewart 			return (ENOENT);
179055bceb1eSRandall Stewart 		}
1791587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1792587d67c0SRandall Stewart 			/* You already have this */
1793587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1794587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1795587d67c0SRandall Stewart 			return (0);
1796587d67c0SRandall Stewart 		}
1797587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1798587d67c0SRandall Stewart 			/*
1799587d67c0SRandall Stewart 			 * The user has advanced the state
1800587d67c0SRandall Stewart 			 * past the initial point, we may not
1801587d67c0SRandall Stewart 			 * be able to switch.
1802587d67c0SRandall Stewart 			 */
1803587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1804587d67c0SRandall Stewart 				/*
1805587d67c0SRandall Stewart 				 * Does the stack provide a
1806587d67c0SRandall Stewart 				 * query mechanism, if so it may
1807587d67c0SRandall Stewart 				 * still be possible?
1808587d67c0SRandall Stewart 				 */
1809587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1810c6c0be27SMichael Tuexen 			} else
1811c6c0be27SMichael Tuexen 				error = EINVAL;
1812587d67c0SRandall Stewart 			if (error) {
1813587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1814587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1815587d67c0SRandall Stewart 				return(error);
1816587d67c0SRandall Stewart 			}
1817587d67c0SRandall Stewart 		}
181855bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
181955bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
182055bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
182155bceb1eSRandall Stewart 			return (ENOENT);
182255bceb1eSRandall Stewart 		}
182355bceb1eSRandall Stewart 		/*
182455bceb1eSRandall Stewart 		 * Release the old refcnt, the
1825587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1826587d67c0SRandall Stewart 		 * new one already.
182755bceb1eSRandall Stewart 		 */
1828587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1829086a3556SAndrew Gallatin 			struct epoch_tracker et;
1830587d67c0SRandall Stewart 			/*
1831587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1832587d67c0SRandall Stewart 			 * the tcb is not going away.
1833587d67c0SRandall Stewart 			 */
1834086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1835587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1836086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1837587d67c0SRandall Stewart 		}
18383ee9c3c4SRandall Stewart #ifdef TCPHPTS
18393ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
18403ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
18413ee9c3c4SRandall Stewart #endif
18423ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
18433ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
18443ee9c3c4SRandall Stewart 			if (error) {
18453ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
18463ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
18473ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
18483ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
18493ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
18503ee9c3c4SRandall Stewart 						soabort(so);
18513ee9c3c4SRandall Stewart 						return(error);
18523ee9c3c4SRandall Stewart 					}
18533ee9c3c4SRandall Stewart 				}
18543ee9c3c4SRandall Stewart 				goto err_out;
18553ee9c3c4SRandall Stewart 			}
18563ee9c3c4SRandall Stewart 		}
185755bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
185855bceb1eSRandall Stewart 		tp->t_fb = blk;
185955bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
186055bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
186155bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
186255bceb1eSRandall Stewart 			     sopt->sopt_name);
186355bceb1eSRandall Stewart 		}
186455bceb1eSRandall Stewart #endif
18653ee9c3c4SRandall Stewart err_out:
186655bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
186755bceb1eSRandall Stewart 		return (error);
186855bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
186955bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1870c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1871c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1872c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
187355bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
187455bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
187555bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
187655bceb1eSRandall Stewart 		return (error);
187755bceb1eSRandall Stewart 	}
187855bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
187955bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
188055bceb1eSRandall Stewart }
188155bceb1eSRandall Stewart 
18822529f56eSJonathan T. Looney /*
18832529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18842529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18852529f56eSJonathan T. Looney  */
18862529f56eSJonathan T. Looney #ifdef CTASSERT
18872529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18882529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18892529f56eSJonathan T. Looney #endif
18902529f56eSJonathan T. Looney 
1891ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1892ec1db6e1SJohn Baldwin static int
1893ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1894ec1db6e1SJohn Baldwin {
1895ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1896ec1db6e1SJohn Baldwin 	int error;
1897ec1db6e1SJohn Baldwin 
1898ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1899ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1900ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1901ec1db6e1SJohn Baldwin 		if (error)
1902ec1db6e1SJohn Baldwin 			return (error);
1903ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1904ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1905ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1906ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1907ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1908ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1909ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1910ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1911ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1912ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1913ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1914ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1915ec1db6e1SJohn Baldwin 		return (0);
1916ec1db6e1SJohn Baldwin 	}
1917ec1db6e1SJohn Baldwin 
1918ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1919ec1db6e1SJohn Baldwin }
1920ec1db6e1SJohn Baldwin #endif
1921ec1db6e1SJohn Baldwin 
192255bceb1eSRandall Stewart int
192355bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
192455bceb1eSRandall Stewart {
192555bceb1eSRandall Stewart 	int	error, opt, optval;
192655bceb1eSRandall Stewart 	u_int	ui;
192755bceb1eSRandall Stewart 	struct	tcp_info ti;
1928b2e60773SJohn Baldwin #ifdef KERN_TLS
1929b2e60773SJohn Baldwin 	struct tls_enable tls;
1930b2e60773SJohn Baldwin #endif
193155bceb1eSRandall Stewart 	struct cc_algo *algo;
19322529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1933adc56f5aSEdward Tomasz Napierala #ifdef STATS
1934adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
1935adc56f5aSEdward Tomasz Napierala #endif
1936af6fef3aSGleb Smirnoff 	size_t	len;
1937df8bae1dSRodney W. Grimes 
1938d519cedbSGleb Smirnoff 	/*
1939d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1940d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1941d519cedbSGleb Smirnoff 	 */
1942d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1943d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1944d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1945c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1946c8b53cedSMichael Tuexen 			return (EINVAL);
1947af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1948af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1949d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1950d519cedbSGleb Smirnoff 		if (error) {
1951af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1952d519cedbSGleb Smirnoff 			return (error);
1953d519cedbSGleb Smirnoff 		}
1954bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1955d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1956af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1957d519cedbSGleb Smirnoff 		else
1958d519cedbSGleb Smirnoff 			error = ENOENT;
1959d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1960d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1961af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1962af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1963d519cedbSGleb Smirnoff 		return (error);
1964d519cedbSGleb Smirnoff 	}
1965d519cedbSGleb Smirnoff 
1966cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1967cfe8b629SGarrett Wollman 	case SOPT_SET:
1968cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1969fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
197088f6b043SBruce M Simpson 		case TCP_MD5SIG:
1971fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19728501a69cSRobert Watson 				INP_WUNLOCK(inp);
1973fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1974fcf59617SAndrey V. Elsukov 			}
1975fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19761cfd4b53SBruce M Simpson 			if (error)
19771e8f5ffaSRobert Watson 				return (error);
197809fe6320SNavdeep Parhar 			goto unlock_and_done;
1979fcf59617SAndrey V. Elsukov #endif /* IPSEC */
198009fe6320SNavdeep Parhar 
1981df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1982cfe8b629SGarrett Wollman 		case TCP_NOOPT:
19830471a8c7SRichard Scheffenegger 		case TCP_LRD:
19848501a69cSRobert Watson 			INP_WUNLOCK(inp);
1985cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1986cfe8b629SGarrett Wollman 			    sizeof optval);
1987cfe8b629SGarrett Wollman 			if (error)
19881e8f5ffaSRobert Watson 				return (error);
1989cfe8b629SGarrett Wollman 
19908501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1991cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1992cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1993cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1994cfe8b629SGarrett Wollman 				break;
1995cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1996cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1997cfe8b629SGarrett Wollman 				break;
19980471a8c7SRichard Scheffenegger 			case TCP_LRD:
19990471a8c7SRichard Scheffenegger 				opt = TF_LRD;
20000471a8c7SRichard Scheffenegger 				break;
2001cfe8b629SGarrett Wollman 			default:
2002cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2003cfe8b629SGarrett Wollman 				break;
2004cfe8b629SGarrett Wollman 			}
2005cfe8b629SGarrett Wollman 
2006cfe8b629SGarrett Wollman 			if (optval)
2007cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2008df8bae1dSRodney W. Grimes 			else
2009cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
201009fe6320SNavdeep Parhar unlock_and_done:
201109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
201209fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
201309fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
201409fe6320SNavdeep Parhar 				    sopt->sopt_name);
201509fe6320SNavdeep Parhar 			}
201609fe6320SNavdeep Parhar #endif
20178501a69cSRobert Watson 			INP_WUNLOCK(inp);
2018df8bae1dSRodney W. Grimes 			break;
2019df8bae1dSRodney W. Grimes 
2020007581c0SJonathan Lemon 		case TCP_NOPUSH:
20218501a69cSRobert Watson 			INP_WUNLOCK(inp);
2022007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2023007581c0SJonathan Lemon 			    sizeof optval);
2024007581c0SJonathan Lemon 			if (error)
20251e8f5ffaSRobert Watson 				return (error);
2026007581c0SJonathan Lemon 
20278501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2028007581c0SJonathan Lemon 			if (optval)
2029007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2030d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2031007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2032109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2033109eb549SGleb Smirnoff 					struct epoch_tracker et;
2034109eb549SGleb Smirnoff 
2035109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
203655bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
2037109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2038109eb549SGleb Smirnoff 				}
2039007581c0SJonathan Lemon 			}
204009fe6320SNavdeep Parhar 			goto unlock_and_done;
2041007581c0SJonathan Lemon 
20429e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
20439e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
20449e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
20459e644c23SMichael Tuexen 			    sizeof optval);
20469e644c23SMichael Tuexen 			if (error)
20479e644c23SMichael Tuexen 				return (error);
20489e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
20499e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
20509e644c23SMichael Tuexen 				/* Its got to be in range */
20519e644c23SMichael Tuexen 				return (EINVAL);
20529e644c23SMichael Tuexen 			}
20539e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
20549e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
20559e644c23SMichael Tuexen 				return (EINVAL);
20569e644c23SMichael Tuexen 			}
20579e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
20589e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
20599e644c23SMichael Tuexen 				/* You can't change after you are connected */
20609e644c23SMichael Tuexen 				error = EINVAL;
20619e644c23SMichael Tuexen 			} else {
20629e644c23SMichael Tuexen 				/* Ok we are all good set the port */
20639e644c23SMichael Tuexen 				tp->t_port = htons(optval);
20649e644c23SMichael Tuexen 			}
20659e644c23SMichael Tuexen 			goto unlock_and_done;
20669e644c23SMichael Tuexen 
2067df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
20688501a69cSRobert Watson 			INP_WUNLOCK(inp);
2069cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2070cfe8b629SGarrett Wollman 			    sizeof optval);
2071cfe8b629SGarrett Wollman 			if (error)
20721e8f5ffaSRobert Watson 				return (error);
2073df8bae1dSRodney W. Grimes 
20748501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
207553369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2076603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2077cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2078a0292f23SGarrett Wollman 			else
2079a0292f23SGarrett Wollman 				error = EINVAL;
208009fe6320SNavdeep Parhar 			goto unlock_and_done;
2081a0292f23SGarrett Wollman 
2082b8af5dfaSRobert Watson 		case TCP_INFO:
20838501a69cSRobert Watson 			INP_WUNLOCK(inp);
2084b8af5dfaSRobert Watson 			error = EINVAL;
2085b8af5dfaSRobert Watson 			break;
2086b8af5dfaSRobert Watson 
2087adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2088adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2089adc56f5aSEdward Tomasz Napierala #ifdef STATS
2090adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2091adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2092adc56f5aSEdward Tomasz Napierala 			if (error)
2093adc56f5aSEdward Tomasz Napierala 				return (error);
2094adc56f5aSEdward Tomasz Napierala 
2095adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2096adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2097adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2098adc56f5aSEdward Tomasz Napierala 			else
2099adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2100adc56f5aSEdward Tomasz Napierala 
2101adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2102adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2103adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2104adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2105adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2106adc56f5aSEdward Tomasz Napierala 				sbp = t;
2107adc56f5aSEdward Tomasz Napierala 			}
2108adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2109adc56f5aSEdward Tomasz Napierala 
2110adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2111adc56f5aSEdward Tomasz Napierala #else
2112adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2113adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2114adc56f5aSEdward Tomasz Napierala 			break;
2115adc56f5aSEdward Tomasz Napierala 
2116dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2117dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2118af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2119af6fef3aSGleb Smirnoff 			if (error)
2120dbc42409SLawrence Stewart 				break;
2121af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
2122af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
212373e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
212473e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
212573e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
212673e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
212773e263b1SGleb Smirnoff 					break;
212873e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
212973e263b1SGleb Smirnoff 			if (algo == NULL) {
2130af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
213173e263b1SGleb Smirnoff 				error = EINVAL;
213273e263b1SGleb Smirnoff 				break;
213373e263b1SGleb Smirnoff 			}
2134dbc42409SLawrence Stewart 			/*
213573e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
213673e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
2137dbc42409SLawrence Stewart 			 */
2138dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
2139dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
214022699887SMatt Macy 			CC_DATA(tp) = NULL;
2141dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
2142dbc42409SLawrence Stewart 			/*
214373e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
214473e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
214573e263b1SGleb Smirnoff 			 * require initialisation).
2146dbc42409SLawrence Stewart 			 */
214773e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
214873e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
2149dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
2150dbc42409SLawrence Stewart 				/*
215173e263b1SGleb Smirnoff 				 * The only reason init should fail is
2152dbc42409SLawrence Stewart 				 * because of malloc.
2153dbc42409SLawrence Stewart 				 */
2154dbc42409SLawrence Stewart 				error = ENOMEM;
2155dbc42409SLawrence Stewart 			}
215673e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
215773e263b1SGleb Smirnoff 			break;
2158dbc42409SLawrence Stewart 
2159a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2160a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2161a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2162a034518aSAndrew Gallatin 			    sizeof(optval));
2163a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2164a034518aSAndrew Gallatin 			if (!error)
2165a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2166a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2167a034518aSAndrew Gallatin 			break;
2168a034518aSAndrew Gallatin 
2169b2e60773SJohn Baldwin #ifdef KERN_TLS
2170b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2171b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2172ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2173b2e60773SJohn Baldwin 			if (error)
2174b2e60773SJohn Baldwin 				break;
2175b2e60773SJohn Baldwin 			error = ktls_enable_tx(so, &tls);
2176b2e60773SJohn Baldwin 			break;
2177b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2178b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2179b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2180b2e60773SJohn Baldwin 			if (error)
2181b2e60773SJohn Baldwin 				return (error);
2182b2e60773SJohn Baldwin 
2183b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2184b2e60773SJohn Baldwin 			error = ktls_set_tx_mode(so, ui);
2185b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2186b2e60773SJohn Baldwin 			break;
2187f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2188f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2189f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2190f1f93475SJohn Baldwin 			    sizeof(tls));
2191f1f93475SJohn Baldwin 			if (error)
2192f1f93475SJohn Baldwin 				break;
2193f1f93475SJohn Baldwin 			error = ktls_enable_rx(so, &tls);
2194f1f93475SJohn Baldwin 			break;
2195b2e60773SJohn Baldwin #endif
2196b2e60773SJohn Baldwin 
21979077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
21989077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
21999077f387SGleb Smirnoff 		case TCP_KEEPINIT:
22009077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
22019077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
22029077f387SGleb Smirnoff 			if (error)
22039077f387SGleb Smirnoff 				return (error);
22049077f387SGleb Smirnoff 
22059077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
22069077f387SGleb Smirnoff 				error = EINVAL;
22079077f387SGleb Smirnoff 				break;
22089077f387SGleb Smirnoff 			}
22099077f387SGleb Smirnoff 			ui *= hz;
22109077f387SGleb Smirnoff 
22119077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
22129077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
22139077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
22149077f387SGleb Smirnoff 				tp->t_keepidle = ui;
22159077f387SGleb Smirnoff 				/*
22169077f387SGleb Smirnoff 				 * XXX: better check current remaining
22179077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
22189077f387SGleb Smirnoff 				 */
22199077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
22209077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
22219077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
22229077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
22239077f387SGleb Smirnoff 				break;
22249077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
22259077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
22269077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
22279077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
22289077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
22299077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
22309077f387SGleb Smirnoff 				break;
22319077f387SGleb Smirnoff 			case TCP_KEEPINIT:
22329077f387SGleb Smirnoff 				tp->t_keepinit = ui;
22339077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
22349077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
22359077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
22369077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
22379077f387SGleb Smirnoff 				break;
22389077f387SGleb Smirnoff 			}
223909fe6320SNavdeep Parhar 			goto unlock_and_done;
22409077f387SGleb Smirnoff 
224185c05144SGleb Smirnoff 		case TCP_KEEPCNT:
224285c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
224385c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
224485c05144SGleb Smirnoff 			if (error)
224585c05144SGleb Smirnoff 				return (error);
224685c05144SGleb Smirnoff 
224785c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
224885c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
224985c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
225085c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
225185c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
225285c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
225385c05144SGleb Smirnoff 			goto unlock_and_done;
225485c05144SGleb Smirnoff 
225586a996e6SHiren Panchasara #ifdef TCPPCAP
225686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
225786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
225886a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
225986a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
226086a996e6SHiren Panchasara 			    sizeof optval);
226186a996e6SHiren Panchasara 			if (error)
226286a996e6SHiren Panchasara 				return (error);
226386a996e6SHiren Panchasara 
226486a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
226586a996e6SHiren Panchasara 			if (optval >= 0)
226686a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
226786a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
226886a996e6SHiren Panchasara 					optval);
226986a996e6SHiren Panchasara 			else
227086a996e6SHiren Panchasara 				error = EINVAL;
227186a996e6SHiren Panchasara 			goto unlock_and_done;
227286a996e6SHiren Panchasara #endif
227386a996e6SHiren Panchasara 
2274c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2275c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2276c560df6fSPatrick Kelsey 
2277281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2278c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2279c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2280281a0fd4SPatrick Kelsey 				return (EPERM);
2281281a0fd4SPatrick Kelsey 
2282c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2283c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2284281a0fd4SPatrick Kelsey 			if (error)
2285281a0fd4SPatrick Kelsey 				return (error);
2286281a0fd4SPatrick Kelsey 
2287281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2288d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2289d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2290d442a657SMichael Tuexen 				error = EINVAL;
2291d442a657SMichael Tuexen 				goto unlock_and_done;
2292d442a657SMichael Tuexen 			}
2293c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2294c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2295c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2296c560df6fSPatrick Kelsey 						error = EPERM;
2297c560df6fSPatrick Kelsey 						goto unlock_and_done;
2298c560df6fSPatrick Kelsey 					}
2299c560df6fSPatrick Kelsey 
2300c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2301281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2302281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2303c560df6fSPatrick Kelsey 				} else {
2304c560df6fSPatrick Kelsey 					/*
2305c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2306c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2307c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2308c560df6fSPatrick Kelsey 					 * connect.
2309c560df6fSPatrick Kelsey 					 */
2310c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2311c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2312c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2313c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2314c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2315c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2316c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2317c560df6fSPatrick Kelsey 					}
2318c560df6fSPatrick Kelsey 				}
2319d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2320281a0fd4SPatrick Kelsey 			} else
2321281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2322281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2323c560df6fSPatrick Kelsey 		}
2324281a0fd4SPatrick Kelsey 
2325e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
23262529f56eSJonathan T. Looney 		case TCP_LOG:
23272529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23282529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
23292529f56eSJonathan T. Looney 			    sizeof optval);
23302529f56eSJonathan T. Looney 			if (error)
23312529f56eSJonathan T. Looney 				return (error);
23322529f56eSJonathan T. Looney 
23332529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23342529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
23352529f56eSJonathan T. Looney 			goto unlock_and_done;
23362529f56eSJonathan T. Looney 
23372529f56eSJonathan T. Looney 		case TCP_LOGBUF:
23382529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23392529f56eSJonathan T. Looney 			error = EINVAL;
23402529f56eSJonathan T. Looney 			break;
23412529f56eSJonathan T. Looney 
23422529f56eSJonathan T. Looney 		case TCP_LOGID:
23432529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23442529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
23452529f56eSJonathan T. Looney 			if (error)
23462529f56eSJonathan T. Looney 				break;
23472529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23482529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23492529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
23502529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
23512529f56eSJonathan T. Looney 			break;
23522529f56eSJonathan T. Looney 
23532529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
23542529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
23552529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23562529f56eSJonathan T. Looney 			error =
23572529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
23582529f56eSJonathan T. Looney 			if (error)
23592529f56eSJonathan T. Looney 				break;
23602529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23612529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23622529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
23632529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
23642529f56eSJonathan T. Looney 				    M_WAITOK, true);
23652529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
23662529f56eSJonathan T. Looney 			} else {
23672529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
23682529f56eSJonathan T. Looney 				/*
23692529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
23702529f56eSJonathan T. Looney 				 * INP lock.
23712529f56eSJonathan T. Looney 				 */
23722529f56eSJonathan T. Looney 			}
23732529f56eSJonathan T. Looney 			break;
2374e24e5683SJonathan T. Looney #endif
23752529f56eSJonathan T. Looney 
2376df8bae1dSRodney W. Grimes 		default:
23778501a69cSRobert Watson 			INP_WUNLOCK(inp);
2378df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2379df8bae1dSRodney W. Grimes 			break;
2380df8bae1dSRodney W. Grimes 		}
2381df8bae1dSRodney W. Grimes 		break;
2382df8bae1dSRodney W. Grimes 
2383cfe8b629SGarrett Wollman 	case SOPT_GET:
23841e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2385cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2386fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
238788f6b043SBruce M Simpson 		case TCP_MD5SIG:
2388fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
23898501a69cSRobert Watson 				INP_WUNLOCK(inp);
2390fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2391fcf59617SAndrey V. Elsukov 			}
2392fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
23931cfd4b53SBruce M Simpson 			break;
2394265ed012SBruce M Simpson #endif
23951e8f5ffaSRobert Watson 
2396df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2397cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
23988501a69cSRobert Watson 			INP_WUNLOCK(inp);
2399b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2400df8bae1dSRodney W. Grimes 			break;
2401df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2402cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
24038501a69cSRobert Watson 			INP_WUNLOCK(inp);
2404b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2405df8bae1dSRodney W. Grimes 			break;
24069e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
24079e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
24089e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
24099e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
24109e644c23SMichael Tuexen 			break;
2411a0292f23SGarrett Wollman 		case TCP_NOOPT:
2412cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
24138501a69cSRobert Watson 			INP_WUNLOCK(inp);
2414b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2415a0292f23SGarrett Wollman 			break;
2416a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2417cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
24188501a69cSRobert Watson 			INP_WUNLOCK(inp);
2419b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2420b8af5dfaSRobert Watson 			break;
2421b8af5dfaSRobert Watson 		case TCP_INFO:
2422b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
24238501a69cSRobert Watson 			INP_WUNLOCK(inp);
2424b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2425a0292f23SGarrett Wollman 			break;
2426adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2427adc56f5aSEdward Tomasz Napierala 			{
2428adc56f5aSEdward Tomasz Napierala #ifdef STATS
2429adc56f5aSEdward Tomasz Napierala 			int nheld;
2430adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2431adc56f5aSEdward Tomasz Napierala 
2432adc56f5aSEdward Tomasz Napierala 			error = 0;
2433adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2434adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2435adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2436adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2437adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2438adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2439adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2440adc56f5aSEdward Tomasz Napierala 			else
2441adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2442adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2443adc56f5aSEdward Tomasz Napierala 			if (error)
2444adc56f5aSEdward Tomasz Napierala 				break;
2445adc56f5aSEdward Tomasz Napierala 
2446adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2447adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2448adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2449adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2450adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2451adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2452adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2453adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2454adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2455adc56f5aSEdward Tomasz Napierala 				break;
2456adc56f5aSEdward Tomasz Napierala 			}
2457adc56f5aSEdward Tomasz Napierala 
2458adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2459adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2460adc56f5aSEdward Tomasz Napierala 				goto unhold;
2461adc56f5aSEdward Tomasz Napierala 
2462adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2463adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2464adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2465adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2466adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2467adc56f5aSEdward Tomasz Napierala unhold:
2468adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2469adc56f5aSEdward Tomasz Napierala #else
2470adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2471adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2472adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2473adc56f5aSEdward Tomasz Napierala 			break;
2474adc56f5aSEdward Tomasz Napierala 			}
2475dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2476af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2477dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2478af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2479dbc42409SLawrence Stewart 			break;
24802f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
24812f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
24822f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
24832f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
24842f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
24852f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
24865a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
24872f3eb7f4SGleb Smirnoff 				break;
24882f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
24895a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
24902f3eb7f4SGleb Smirnoff 				break;
24912f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
24925a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
24932f3eb7f4SGleb Smirnoff 				break;
24942f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
24955a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
24962f3eb7f4SGleb Smirnoff 				break;
24972f3eb7f4SGleb Smirnoff 			}
24982f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
24992f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
25002f3eb7f4SGleb Smirnoff 			break;
250186a996e6SHiren Panchasara #ifdef TCPPCAP
250286a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
250386a996e6SHiren Panchasara 		case TCP_PCAP_IN:
250486a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
250586a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
250686a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
250786a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
250886a996e6SHiren Panchasara 			break;
250986a996e6SHiren Panchasara #endif
2510281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2511281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2512281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2513281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2514281a0fd4SPatrick Kelsey 			break;
2515e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
25162529f56eSJonathan T. Looney 		case TCP_LOG:
25172529f56eSJonathan T. Looney 			optval = tp->t_logstate;
25182529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25192529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
25202529f56eSJonathan T. Looney 			break;
25212529f56eSJonathan T. Looney 		case TCP_LOGBUF:
25222529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
25232529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
25242529f56eSJonathan T. Looney 			break;
25252529f56eSJonathan T. Looney 		case TCP_LOGID:
25262529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
25272529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25282529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
25292529f56eSJonathan T. Looney 			break;
25302529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
25312529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
25322529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25332529f56eSJonathan T. Looney 			error = EINVAL;
25342529f56eSJonathan T. Looney 			break;
2535e24e5683SJonathan T. Looney #endif
2536b2e60773SJohn Baldwin #ifdef KERN_TLS
2537b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2538b2e60773SJohn Baldwin 			optval = ktls_get_tx_mode(so);
2539b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2540b2e60773SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2541b2e60773SJohn Baldwin 			break;
2542f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2543f1f93475SJohn Baldwin 			optval = ktls_get_rx_mode(so);
2544f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2545f1f93475SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2546f1f93475SJohn Baldwin 			break;
2547b2e60773SJohn Baldwin #endif
25480471a8c7SRichard Scheffenegger 		case TCP_LRD:
25490471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
25500471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
25510471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
25520471a8c7SRichard Scheffenegger 			break;
2553df8bae1dSRodney W. Grimes 		default:
25548501a69cSRobert Watson 			INP_WUNLOCK(inp);
2555df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2556df8bae1dSRodney W. Grimes 			break;
2557df8bae1dSRodney W. Grimes 		}
2558df8bae1dSRodney W. Grimes 		break;
2559df8bae1dSRodney W. Grimes 	}
2560df8bae1dSRodney W. Grimes 	return (error);
2561df8bae1dSRodney W. Grimes }
25628501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2563bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2564df8bae1dSRodney W. Grimes 
256526e30fbbSDavid Greenman /*
2566df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2567df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2568df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2569df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2570df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2571df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2572df8bae1dSRodney W. Grimes  */
2573623dce13SRobert Watson static void
2574ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2575df8bae1dSRodney W. Grimes {
2576e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2577e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2578e6e0b5ffSRobert Watson 
257997a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
25808501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2581df8bae1dSRodney W. Grimes 
2582623dce13SRobert Watson 	/*
2583623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2584623dce13SRobert Watson 	 * socket is still open.
2585623dce13SRobert Watson 	 */
25868db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
25878db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2588df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2589623dce13SRobert Watson 		KASSERT(tp != NULL,
2590623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2591623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2592243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2593623dce13SRobert Watson 		KASSERT(tp != NULL,
2594623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2595623dce13SRobert Watson 	} else {
2596df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2597df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2598623dce13SRobert Watson 		tcp_usrclosed(tp);
2599ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
260055bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2601df8bae1dSRodney W. Grimes 	}
2602df8bae1dSRodney W. Grimes }
2603df8bae1dSRodney W. Grimes 
2604df8bae1dSRodney W. Grimes /*
2605df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2606df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2607df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2608df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2609df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2610df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2611df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2612df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2613df8bae1dSRodney W. Grimes  */
2614623dce13SRobert Watson static void
2615ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2616df8bae1dSRodney W. Grimes {
2617df8bae1dSRodney W. Grimes 
261897a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26198501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2620e6e0b5ffSRobert Watson 
2621df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2622df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
262309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
262409fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
262509fe6320SNavdeep Parhar #endif
2626550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2627bc65987aSKip Macy 		/* FALLTHROUGH */
2628bc65987aSKip Macy 	case TCPS_CLOSED:
2629df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2630623dce13SRobert Watson 		/*
2631623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2632623dce13SRobert Watson 		 * still open.
2633623dce13SRobert Watson 		 */
2634623dce13SRobert Watson 		KASSERT(tp != NULL,
2635623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2636df8bae1dSRodney W. Grimes 		break;
2637df8bae1dSRodney W. Grimes 
2638a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2639df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2640a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2641a0292f23SGarrett Wollman 		break;
2642a0292f23SGarrett Wollman 
2643df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
264457f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2645df8bae1dSRodney W. Grimes 		break;
2646df8bae1dSRodney W. Grimes 
2647df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
264857f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2649df8bae1dSRodney W. Grimes 		break;
2650df8bae1dSRodney W. Grimes 	}
2651abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2652df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2653abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
26547c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
26557c72af87SMohan Srinivasan 			int timeout;
26567c72af87SMohan Srinivasan 
26577c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
26589077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2659b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2660b6239c4aSAndras Olah 		}
2661df8bae1dSRodney W. Grimes 	}
26627c72af87SMohan Srinivasan }
2663497057eeSRobert Watson 
2664497057eeSRobert Watson #ifdef DDB
2665497057eeSRobert Watson static void
2666497057eeSRobert Watson db_print_indent(int indent)
2667497057eeSRobert Watson {
2668497057eeSRobert Watson 	int i;
2669497057eeSRobert Watson 
2670497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2671497057eeSRobert Watson 		db_printf(" ");
2672497057eeSRobert Watson }
2673497057eeSRobert Watson 
2674497057eeSRobert Watson static void
2675497057eeSRobert Watson db_print_tstate(int t_state)
2676497057eeSRobert Watson {
2677497057eeSRobert Watson 
2678497057eeSRobert Watson 	switch (t_state) {
2679497057eeSRobert Watson 	case TCPS_CLOSED:
2680497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2681497057eeSRobert Watson 		return;
2682497057eeSRobert Watson 
2683497057eeSRobert Watson 	case TCPS_LISTEN:
2684497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2685497057eeSRobert Watson 		return;
2686497057eeSRobert Watson 
2687497057eeSRobert Watson 	case TCPS_SYN_SENT:
2688497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2689497057eeSRobert Watson 		return;
2690497057eeSRobert Watson 
2691497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2692497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2693497057eeSRobert Watson 		return;
2694497057eeSRobert Watson 
2695497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2696497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2697497057eeSRobert Watson 		return;
2698497057eeSRobert Watson 
2699497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2700497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2701497057eeSRobert Watson 		return;
2702497057eeSRobert Watson 
2703497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2704497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2705497057eeSRobert Watson 		return;
2706497057eeSRobert Watson 
2707497057eeSRobert Watson 	case TCPS_CLOSING:
2708497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2709497057eeSRobert Watson 		return;
2710497057eeSRobert Watson 
2711497057eeSRobert Watson 	case TCPS_LAST_ACK:
2712497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2713497057eeSRobert Watson 		return;
2714497057eeSRobert Watson 
2715497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2716497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2717497057eeSRobert Watson 		return;
2718497057eeSRobert Watson 
2719497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2720497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2721497057eeSRobert Watson 		return;
2722497057eeSRobert Watson 
2723497057eeSRobert Watson 	default:
2724497057eeSRobert Watson 		db_printf("unknown");
2725497057eeSRobert Watson 		return;
2726497057eeSRobert Watson 	}
2727497057eeSRobert Watson }
2728497057eeSRobert Watson 
2729497057eeSRobert Watson static void
2730497057eeSRobert Watson db_print_tflags(u_int t_flags)
2731497057eeSRobert Watson {
2732497057eeSRobert Watson 	int comma;
2733497057eeSRobert Watson 
2734497057eeSRobert Watson 	comma = 0;
2735497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2736497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2737497057eeSRobert Watson 		comma = 1;
2738497057eeSRobert Watson 	}
2739497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2740497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2741497057eeSRobert Watson 		comma = 1;
2742497057eeSRobert Watson 	}
2743497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2744497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2745497057eeSRobert Watson 		comma = 1;
2746497057eeSRobert Watson 	}
2747497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2748497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2749497057eeSRobert Watson 		comma = 1;
2750497057eeSRobert Watson 	}
2751497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2752497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2753497057eeSRobert Watson 		comma = 1;
2754497057eeSRobert Watson 	}
2755497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2756497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2757497057eeSRobert Watson 		comma = 1;
2758497057eeSRobert Watson 	}
2759497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2760497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2761497057eeSRobert Watson 		comma = 1;
2762497057eeSRobert Watson 	}
2763497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2764497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2765497057eeSRobert Watson 		comma = 1;
2766497057eeSRobert Watson 	}
2767497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2768497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2769497057eeSRobert Watson 		comma = 1;
2770497057eeSRobert Watson 	}
2771497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2772497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2773497057eeSRobert Watson 		comma = 1;
2774497057eeSRobert Watson 	}
2775497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2776497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2777497057eeSRobert Watson 		comma = 1;
2778497057eeSRobert Watson 	}
2779497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2780497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2781497057eeSRobert Watson 		comma = 1;
2782497057eeSRobert Watson 	}
2783497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2784497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2785497057eeSRobert Watson 		comma = 1;
2786497057eeSRobert Watson 	}
2787497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2788497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2789497057eeSRobert Watson 		comma = 1;
2790497057eeSRobert Watson 	}
2791497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2792497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2793497057eeSRobert Watson 		comma = 1;
2794497057eeSRobert Watson 	}
2795497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2796497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2797497057eeSRobert Watson 		comma = 1;
2798497057eeSRobert Watson 	}
2799497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2800497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2801497057eeSRobert Watson 		comma = 1;
2802497057eeSRobert Watson 	}
2803497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2804497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2805497057eeSRobert Watson 		comma = 1;
2806497057eeSRobert Watson 	}
2807dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2808dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2809dbc42409SLawrence Stewart 		comma = 1;
2810dbc42409SLawrence Stewart 	}
2811497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2812497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2813497057eeSRobert Watson 		comma = 1;
2814497057eeSRobert Watson 	}
2815497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2816497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2817497057eeSRobert Watson 		comma = 1;
2818497057eeSRobert Watson 	}
2819497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2820497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2821497057eeSRobert Watson 		comma = 1;
2822497057eeSRobert Watson 	}
2823497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2824497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2825497057eeSRobert Watson 		comma = 1;
2826497057eeSRobert Watson 	}
2827281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2828281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2829281a0fd4SPatrick Kelsey 		comma = 1;
2830281a0fd4SPatrick Kelsey 	}
2831497057eeSRobert Watson }
2832497057eeSRobert Watson 
2833497057eeSRobert Watson static void
28343cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
28353cf38784SMichael Tuexen {
28363cf38784SMichael Tuexen 	int comma;
28373cf38784SMichael Tuexen 
28383cf38784SMichael Tuexen 	comma = 0;
28393cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
28403cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
28413cf38784SMichael Tuexen 		comma = 1;
28423cf38784SMichael Tuexen 	}
28433cf38784SMichael Tuexen }
28443cf38784SMichael Tuexen 
28453cf38784SMichael Tuexen static void
2846497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2847497057eeSRobert Watson {
2848497057eeSRobert Watson 	int comma;
2849497057eeSRobert Watson 
2850497057eeSRobert Watson 	comma = 0;
2851497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2852497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2853497057eeSRobert Watson 		comma = 1;
2854497057eeSRobert Watson 	}
2855497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2856497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2857497057eeSRobert Watson 		comma = 1;
2858497057eeSRobert Watson 	}
2859497057eeSRobert Watson }
2860497057eeSRobert Watson 
2861497057eeSRobert Watson static void
2862497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2863497057eeSRobert Watson {
2864497057eeSRobert Watson 
2865497057eeSRobert Watson 	db_print_indent(indent);
2866497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2867497057eeSRobert Watson 
2868497057eeSRobert Watson 	indent += 2;
2869497057eeSRobert Watson 
2870497057eeSRobert Watson 	db_print_indent(indent);
2871497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2872c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2873497057eeSRobert Watson 
2874497057eeSRobert Watson 	db_print_indent(indent);
287585d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2876e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2877497057eeSRobert Watson 
2878497057eeSRobert Watson 	db_print_indent(indent);
2879e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2880e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2881497057eeSRobert Watson 
2882497057eeSRobert Watson 	db_print_indent(indent);
2883497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2884497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2885497057eeSRobert Watson 	db_printf(")\n");
2886497057eeSRobert Watson 
2887497057eeSRobert Watson 	db_print_indent(indent);
2888497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2889497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2890497057eeSRobert Watson 	db_printf(")\n");
2891497057eeSRobert Watson 
2892497057eeSRobert Watson 	db_print_indent(indent);
28933cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
28943cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
28953cf38784SMichael Tuexen 	db_printf(")\n");
28963cf38784SMichael Tuexen 
28973cf38784SMichael Tuexen 	db_print_indent(indent);
2898497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2899497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2900497057eeSRobert Watson 
2901497057eeSRobert Watson 	db_print_indent(indent);
2902497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2903497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2904497057eeSRobert Watson 
2905497057eeSRobert Watson 	db_print_indent(indent);
2906497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2907497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2908497057eeSRobert Watson 
2909497057eeSRobert Watson 	db_print_indent(indent);
29103ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2911497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2912497057eeSRobert Watson 
2913497057eeSRobert Watson 	db_print_indent(indent);
29143ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
29151c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2916497057eeSRobert Watson 
2917497057eeSRobert Watson 	db_print_indent(indent);
29183ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
29191c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2920497057eeSRobert Watson 
2921497057eeSRobert Watson 	db_print_indent(indent);
29220c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
29230c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2924497057eeSRobert Watson 
2925497057eeSRobert Watson 	db_print_indent(indent);
29261c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
29271c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2928497057eeSRobert Watson 
2929497057eeSRobert Watson 	db_print_indent(indent);
29301c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
29311c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2932497057eeSRobert Watson 
2933497057eeSRobert Watson 	db_print_indent(indent);
2934497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2935497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2936497057eeSRobert Watson 	    tp->t_rttbest);
2937497057eeSRobert Watson 
2938497057eeSRobert Watson 	db_print_indent(indent);
29393ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2940497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2941497057eeSRobert Watson 
2942497057eeSRobert Watson 	db_print_indent(indent);
2943497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2944497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2945497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2946497057eeSRobert Watson 
2947497057eeSRobert Watson 	db_print_indent(indent);
2948497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2949497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2950497057eeSRobert Watson 
2951497057eeSRobert Watson 	db_print_indent(indent);
29529f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
29531a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2954497057eeSRobert Watson 
2955497057eeSRobert Watson 	db_print_indent(indent);
2956497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
29573ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2958497057eeSRobert Watson 
2959497057eeSRobert Watson 	db_print_indent(indent);
29603ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
29619f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2962497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2963497057eeSRobert Watson 
2964497057eeSRobert Watson 	db_print_indent(indent);
29653529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
29663529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2967497057eeSRobert Watson 
2968497057eeSRobert Watson 	db_print_indent(indent);
2969a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
2970a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
2971497057eeSRobert Watson 
2972497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2973497057eeSRobert Watson 
2974497057eeSRobert Watson 	db_print_indent(indent);
2975497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2976497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2977497057eeSRobert Watson }
2978497057eeSRobert Watson 
2979497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2980497057eeSRobert Watson {
2981497057eeSRobert Watson 	struct tcpcb *tp;
2982497057eeSRobert Watson 
2983497057eeSRobert Watson 	if (!have_addr) {
2984497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2985497057eeSRobert Watson 		return;
2986497057eeSRobert Watson 	}
2987497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2988497057eeSRobert Watson 
2989497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2990497057eeSRobert Watson }
2991497057eeSRobert Watson #endif
2992