xref: /freebsd/sys/netinet/tcp_usrreq.c (revision d442a657333c865232942ed2d5d2bc4a37aa7794)
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 	if ((so->so_options & SO_LINGER) && so->so_linger == 0)
2083879597fSAndrey A. Chernov 		so->so_linger = TCP_LINGERTIME;
2092c37256eSGarrett Wollman out:
2102c37256eSGarrett Wollman 	TCPDEBUG2(PRU_ATTACH);
2115d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
2120f6385e7SGleb Smirnoff 	return (error);
2132c37256eSGarrett Wollman }
2142c37256eSGarrett Wollman 
2152c37256eSGarrett Wollman /*
2163fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
217a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
218a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
219a152f8a3SRobert Watson  * inpcb state: time wait.
2202c37256eSGarrett Wollman  */
221bc725eafSRobert Watson static void
2223fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2232c37256eSGarrett Wollman {
2243fed74e9SGleb Smirnoff 	struct inpcb *inp;
2252c37256eSGarrett Wollman 	struct tcpcb *tp;
2262c37256eSGarrett Wollman 
2273fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2283fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2293fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2303fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2313fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
232953b5606SRobert Watson 
233a152f8a3SRobert Watson 	tp = intotcpcb(inp);
234a152f8a3SRobert Watson 
235ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
236623dce13SRobert Watson 		/*
237a152f8a3SRobert Watson 		 * There are two cases to handle: one in which the time wait
238a152f8a3SRobert Watson 		 * state is being discarded (INP_DROPPED), and one in which
239a152f8a3SRobert Watson 		 * this connection will remain in timewait.  In the former,
240a152f8a3SRobert Watson 		 * it is time to discard all state (except tcptw, which has
241a152f8a3SRobert Watson 		 * already been discarded by the timewait close code, which
242a152f8a3SRobert Watson 		 * should be further up the call stack somewhere).  In the
243a152f8a3SRobert Watson 		 * latter case, we detach from the socket, but leave the pcb
244a152f8a3SRobert Watson 		 * present until timewait ends.
245623dce13SRobert Watson 		 *
246a152f8a3SRobert Watson 		 * XXXRW: Would it be cleaner to free the tcptw here?
247cea40c48SJulien Charbon 		 *
248cea40c48SJulien Charbon 		 * Astute question indeed, from twtcp perspective there are
249dd388cfdSGleb Smirnoff 		 * four cases to consider:
250cea40c48SJulien Charbon 		 *
2513fed74e9SGleb Smirnoff 		 * #1 tcp_usr_detach is called at tcptw creation time by
252cea40c48SJulien Charbon 		 *  tcp_twstart, then do not discard the newly created tcptw
253cea40c48SJulien Charbon 		 *  and leave inpcb present until timewait ends
2543fed74e9SGleb Smirnoff 		 * #2 tcp_usr_detach is called at tcptw creation time by
255dd388cfdSGleb Smirnoff 		 *  tcp_twstart, but connection is local and tw will be
256dd388cfdSGleb Smirnoff 		 *  discarded immediately
2573fed74e9SGleb Smirnoff 		 * #3 tcp_usr_detach is called at timewait end (or reuse) by
258cea40c48SJulien Charbon 		 *  tcp_twclose, then the tcptw has already been discarded
259ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
2603fed74e9SGleb Smirnoff 		 * #4 tcp_usr_detach is called() after timewait ends (or reuse)
261cea40c48SJulien Charbon 		 *  (e.g. by soclose), then tcptw has already been discarded
262ff9b006dSJulien Charbon 		 *  (or reused) and inpcb is freed here
263cea40c48SJulien Charbon 		 *
264cea40c48SJulien Charbon 		 *  In all three cases the tcptw should not be freed here.
265623dce13SRobert Watson 		 */
266ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED) {
267623dce13SRobert Watson 			in_pcbdetach(inp);
268f5cf1e5fSJulien Charbon 			if (__predict_true(tp == NULL)) {
2690206cdb8SBjoern A. Zeeb 				in_pcbfree(inp);
2700206cdb8SBjoern A. Zeeb 			} else {
271f5cf1e5fSJulien Charbon 				/*
272f5cf1e5fSJulien Charbon 				 * This case should not happen as in TIMEWAIT
273f5cf1e5fSJulien Charbon 				 * state the inp should not be destroyed before
274f5cf1e5fSJulien Charbon 				 * its tcptw.  If INVARIANTS is defined, panic.
275f5cf1e5fSJulien Charbon 				 */
276f5cf1e5fSJulien Charbon #ifdef INVARIANTS
277f5cf1e5fSJulien Charbon 				panic("%s: Panic before an inp double-free: "
278f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
279f5cf1e5fSJulien Charbon 				    , __func__);
280f5cf1e5fSJulien Charbon #else
281f5cf1e5fSJulien Charbon 				log(LOG_ERR, "%s: Avoid an inp double-free: "
282f5cf1e5fSJulien Charbon 				    "INP_TIMEWAIT && INP_DROPPED && tp != NULL"
283f5cf1e5fSJulien Charbon 				    , __func__);
284f5cf1e5fSJulien Charbon #endif
285f5cf1e5fSJulien Charbon 				INP_WUNLOCK(inp);
286f5cf1e5fSJulien Charbon 			}
287f5cf1e5fSJulien Charbon 		} else {
288623dce13SRobert Watson 			in_pcbdetach(inp);
2898501a69cSRobert Watson 			INP_WUNLOCK(inp);
290623dce13SRobert Watson 		}
291623dce13SRobert Watson 	} else {
292e6e65783SRobert Watson 		/*
293a152f8a3SRobert Watson 		 * If the connection is not in timewait, we consider two
294a152f8a3SRobert Watson 		 * two conditions: one in which no further processing is
295a152f8a3SRobert Watson 		 * necessary (dropped || embryonic), and one in which TCP is
296a152f8a3SRobert Watson 		 * not yet done, but no longer requires the socket, so the
297a152f8a3SRobert Watson 		 * pcb will persist for the time being.
298a152f8a3SRobert Watson 		 *
299a152f8a3SRobert Watson 		 * XXXRW: Does the second case still occur?
300e6e65783SRobert Watson 		 */
301ad71fe3cSRobert Watson 		if (inp->inp_flags & INP_DROPPED ||
302623dce13SRobert Watson 		    tp->t_state < TCPS_SYN_SENT) {
303623dce13SRobert Watson 			tcp_discardcb(tp);
304623dce13SRobert Watson 			in_pcbdetach(inp);
3050206cdb8SBjoern A. Zeeb 			in_pcbfree(inp);
306db3cee51SNavdeep Parhar 		} else {
307a152f8a3SRobert Watson 			in_pcbdetach(inp);
308db3cee51SNavdeep Parhar 			INP_WUNLOCK(inp);
309db3cee51SNavdeep Parhar 		}
310623dce13SRobert Watson 	}
311623dce13SRobert Watson }
312c78cbc7bSRobert Watson 
313b287c6c7SBjoern A. Zeeb #ifdef INET
3142c37256eSGarrett Wollman /*
3152c37256eSGarrett Wollman  * Give the socket an address.
3162c37256eSGarrett Wollman  */
3172c37256eSGarrett Wollman static int
318b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
3192c37256eSGarrett Wollman {
3202c37256eSGarrett Wollman 	int error = 0;
321f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
322623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3232c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
3242c37256eSGarrett Wollman 
32552710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
32652710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof (*sinp))
32752710de1SPawel Jakub Dawidek 		return (EINVAL);
3282c37256eSGarrett Wollman 	/*
3292c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
3302c37256eSGarrett Wollman 	 * to them.
3312c37256eSGarrett Wollman 	 */
3322c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET &&
33352710de1SPawel Jakub Dawidek 	    IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
33452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
33552710de1SPawel Jakub Dawidek 
336623dce13SRobert Watson 	TCPDEBUG0;
337623dce13SRobert Watson 	inp = sotoinpcb(so);
338623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
3398501a69cSRobert Watson 	INP_WLOCK(inp);
340ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
341623dce13SRobert Watson 		error = EINVAL;
3422c37256eSGarrett Wollman 		goto out;
343623dce13SRobert Watson 	}
344623dce13SRobert Watson 	tp = intotcpcb(inp);
345623dce13SRobert Watson 	TCPDEBUG1();
346fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
347623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
348fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
349623dce13SRobert Watson out:
350623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
3515d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3528501a69cSRobert Watson 	INP_WUNLOCK(inp);
353623dce13SRobert Watson 
354623dce13SRobert Watson 	return (error);
3552c37256eSGarrett Wollman }
356b287c6c7SBjoern A. Zeeb #endif /* INET */
3572c37256eSGarrett Wollman 
358fb59c426SYoshinobu Inoue #ifdef INET6
359fb59c426SYoshinobu Inoue static int
360b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
361fb59c426SYoshinobu Inoue {
362fb59c426SYoshinobu Inoue 	int error = 0;
363f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
364623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3650ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
3664a91aa8fSMichael Tuexen 	u_char vflagsav;
367fb59c426SYoshinobu Inoue 
3680ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
3690ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
37052710de1SPawel Jakub Dawidek 		return (EINVAL);
371fb59c426SYoshinobu Inoue 	/*
372fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
373fb59c426SYoshinobu Inoue 	 * to them.
374fb59c426SYoshinobu Inoue 	 */
3750ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6 &&
3760ecd976eSBjoern A. Zeeb 	    IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
37752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
37852710de1SPawel Jakub Dawidek 
379623dce13SRobert Watson 	TCPDEBUG0;
380623dce13SRobert Watson 	inp = sotoinpcb(so);
381623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3828501a69cSRobert Watson 	INP_WLOCK(inp);
3834a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
384ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
385623dce13SRobert Watson 		error = EINVAL;
386623dce13SRobert Watson 		goto out;
387623dce13SRobert Watson 	}
388623dce13SRobert Watson 	tp = intotcpcb(inp);
389623dce13SRobert Watson 	TCPDEBUG1();
390fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
391fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
392fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
393b287c6c7SBjoern A. Zeeb #ifdef INET
39466ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3950ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
396fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3970ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
398fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
399fb59c426SYoshinobu Inoue 
4000ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
401888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
402888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
403888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
404888973f5SMichael Tuexen 				goto out;
405888973f5SMichael Tuexen 			}
406fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
407fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
408b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
409b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
410fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
411fb59c426SYoshinobu Inoue 			goto out;
412fb59c426SYoshinobu Inoue 		}
413fb59c426SYoshinobu Inoue 	}
414b287c6c7SBjoern A. Zeeb #endif
415b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
416fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
417623dce13SRobert Watson out:
4184a91aa8fSMichael Tuexen 	if (error != 0)
4194a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
420623dce13SRobert Watson 	TCPDEBUG2(PRU_BIND);
4215d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
4228501a69cSRobert Watson 	INP_WUNLOCK(inp);
423623dce13SRobert Watson 	return (error);
424fb59c426SYoshinobu Inoue }
425fb59c426SYoshinobu Inoue #endif /* INET6 */
426fb59c426SYoshinobu Inoue 
427b287c6c7SBjoern A. Zeeb #ifdef INET
4282c37256eSGarrett Wollman /*
4292c37256eSGarrett Wollman  * Prepare to accept connections.
4302c37256eSGarrett Wollman  */
4312c37256eSGarrett Wollman static int
432d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
4332c37256eSGarrett Wollman {
4342c37256eSGarrett Wollman 	int error = 0;
435f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
436623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4372c37256eSGarrett Wollman 
438623dce13SRobert Watson 	TCPDEBUG0;
439623dce13SRobert Watson 	inp = sotoinpcb(so);
440623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
4418501a69cSRobert Watson 	INP_WLOCK(inp);
442ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
443623dce13SRobert Watson 		error = EINVAL;
444623dce13SRobert Watson 		goto out;
445623dce13SRobert Watson 	}
446623dce13SRobert Watson 	tp = intotcpcb(inp);
447623dce13SRobert Watson 	TCPDEBUG1();
4480daccb9cSRobert Watson 	SOCK_LOCK(so);
4490daccb9cSRobert Watson 	error = solisten_proto_check(so);
450fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4510daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0)
452b0330ed9SPawel Jakub Dawidek 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
453fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4540daccb9cSRobert Watson 	if (error == 0) {
45557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
456d374e81eSRobert Watson 		solisten_proto(so, backlog);
45709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
45837cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45909fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
46009fe6320SNavdeep Parhar #endif
4610daccb9cSRobert Watson 	}
4620daccb9cSRobert Watson 	SOCK_UNLOCK(so);
463623dce13SRobert Watson 
46468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
465281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
46618a75309SPatrick Kelsey 
467623dce13SRobert Watson out:
468623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
4695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4708501a69cSRobert Watson 	INP_WUNLOCK(inp);
471623dce13SRobert Watson 	return (error);
4722c37256eSGarrett Wollman }
473b287c6c7SBjoern A. Zeeb #endif /* INET */
4742c37256eSGarrett Wollman 
475fb59c426SYoshinobu Inoue #ifdef INET6
476fb59c426SYoshinobu Inoue static int
477d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
478fb59c426SYoshinobu Inoue {
479fb59c426SYoshinobu Inoue 	int error = 0;
480f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
481623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4824a91aa8fSMichael Tuexen 	u_char vflagsav;
483fb59c426SYoshinobu Inoue 
484623dce13SRobert Watson 	TCPDEBUG0;
485623dce13SRobert Watson 	inp = sotoinpcb(so);
486623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4878501a69cSRobert Watson 	INP_WLOCK(inp);
488ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
489623dce13SRobert Watson 		error = EINVAL;
490623dce13SRobert Watson 		goto out;
491623dce13SRobert Watson 	}
4924a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
493623dce13SRobert Watson 	tp = intotcpcb(inp);
494623dce13SRobert Watson 	TCPDEBUG1();
4950daccb9cSRobert Watson 	SOCK_LOCK(so);
4960daccb9cSRobert Watson 	error = solisten_proto_check(so);
497fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
4980daccb9cSRobert Watson 	if (error == 0 && inp->inp_lport == 0) {
499fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
50066ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
501fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
502b0330ed9SPawel Jakub Dawidek 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
503fb59c426SYoshinobu Inoue 	}
504fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
5050daccb9cSRobert Watson 	if (error == 0) {
50657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
507d374e81eSRobert Watson 		solisten_proto(so, backlog);
50809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
50937cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
51009fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
51109fe6320SNavdeep Parhar #endif
5120daccb9cSRobert Watson 	}
5130daccb9cSRobert Watson 	SOCK_UNLOCK(so);
514623dce13SRobert Watson 
51568bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
516281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
51718a75309SPatrick Kelsey 
5184a91aa8fSMichael Tuexen 	if (error != 0)
5194a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
5204a91aa8fSMichael Tuexen 
521623dce13SRobert Watson out:
522623dce13SRobert Watson 	TCPDEBUG2(PRU_LISTEN);
5235d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
5248501a69cSRobert Watson 	INP_WUNLOCK(inp);
525623dce13SRobert Watson 	return (error);
526fb59c426SYoshinobu Inoue }
527fb59c426SYoshinobu Inoue #endif /* INET6 */
528fb59c426SYoshinobu Inoue 
529b287c6c7SBjoern A. Zeeb #ifdef INET
5302c37256eSGarrett Wollman /*
5312c37256eSGarrett Wollman  * Initiate connection to peer.
5322c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
5332c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
5342c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
5352c37256eSGarrett Wollman  * Send initial segment on connection.
5362c37256eSGarrett Wollman  */
5372c37256eSGarrett Wollman static int
538b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
5392c37256eSGarrett Wollman {
540109eb549SGleb Smirnoff 	struct epoch_tracker et;
5412c37256eSGarrett Wollman 	int error = 0;
542f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
543623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5442c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
5452c37256eSGarrett Wollman 
54657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
547e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
548e29ef13fSDon Lewis 		return (EINVAL);
54952710de1SPawel Jakub Dawidek 	/*
55052710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
55152710de1SPawel Jakub Dawidek 	 */
5522c37256eSGarrett Wollman 	if (sinp->sin_family == AF_INET
55352710de1SPawel Jakub Dawidek 	    && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
55452710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
555b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
556b89e82ddSJamie Gritton 		return (error);
55775c13541SPoul-Henning Kamp 
558623dce13SRobert Watson 	TCPDEBUG0;
559623dce13SRobert Watson 	inp = sotoinpcb(so);
560623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5618501a69cSRobert Watson 	INP_WLOCK(inp);
562eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
563eb96dc33SJulien Charbon 		error = EADDRINUSE;
564eb96dc33SJulien Charbon 		goto out;
565eb96dc33SJulien Charbon 	}
566eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
567eb96dc33SJulien Charbon 		error = ECONNREFUSED;
568623dce13SRobert Watson 		goto out;
569623dce13SRobert Watson 	}
570623dce13SRobert Watson 	tp = intotcpcb(inp);
571623dce13SRobert Watson 	TCPDEBUG1();
572c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
573b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
574c1604fe4SGleb Smirnoff 		goto out_in_epoch;
57509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
57609fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
57737cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
57809fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
579c1604fe4SGleb Smirnoff 		goto out_in_epoch;
58009fe6320SNavdeep Parhar #endif
58109fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
58255bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
583c1604fe4SGleb Smirnoff out_in_epoch:
584109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
585623dce13SRobert Watson out:
586623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
587e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5888501a69cSRobert Watson 	INP_WUNLOCK(inp);
589623dce13SRobert Watson 	return (error);
5902c37256eSGarrett Wollman }
591b287c6c7SBjoern A. Zeeb #endif /* INET */
5922c37256eSGarrett Wollman 
593fb59c426SYoshinobu Inoue #ifdef INET6
594fb59c426SYoshinobu Inoue static int
595b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
596fb59c426SYoshinobu Inoue {
597109eb549SGleb Smirnoff 	struct epoch_tracker et;
598fb59c426SYoshinobu Inoue 	int error = 0;
599f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
600623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6010ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
6024a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
6034a91aa8fSMichael Tuexen 	u_char vflagsav;
604623dce13SRobert Watson 
605623dce13SRobert Watson 	TCPDEBUG0;
606fb59c426SYoshinobu Inoue 
6070ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
6080ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
609e29ef13fSDon Lewis 		return (EINVAL);
61052710de1SPawel Jakub Dawidek 	/*
61152710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
61252710de1SPawel Jakub Dawidek 	 */
6130ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6
6140ecd976eSBjoern A. Zeeb 	    && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
61552710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
616fb59c426SYoshinobu Inoue 
617623dce13SRobert Watson 	inp = sotoinpcb(so);
618623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6198501a69cSRobert Watson 	INP_WLOCK(inp);
6204a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6214a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
622eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
623eb96dc33SJulien Charbon 		error = EADDRINUSE;
624eb96dc33SJulien Charbon 		goto out;
625eb96dc33SJulien Charbon 	}
626eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
627eb96dc33SJulien Charbon 		error = ECONNREFUSED;
628623dce13SRobert Watson 		goto out;
629623dce13SRobert Watson 	}
630623dce13SRobert Watson 	tp = intotcpcb(inp);
631623dce13SRobert Watson 	TCPDEBUG1();
632b287c6c7SBjoern A. Zeeb #ifdef INET
633fa046d87SRobert Watson 	/*
634fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
635fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
636fa046d87SRobert Watson 	 * Is this a significant problem?
637fa046d87SRobert Watson 	 */
6380ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
639fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
640fb59c426SYoshinobu Inoue 
641d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
642d46a5312SMaxim Konovalov 			error = EINVAL;
643d46a5312SMaxim Konovalov 			goto out;
644d46a5312SMaxim Konovalov 		}
6455dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6465dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6475dba6adaSMichael Tuexen 			goto out;
6485dba6adaSMichael Tuexen 		}
64933841545SHajimu UMEMOTO 
6500ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
651888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
652888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
653888973f5SMichael Tuexen 			goto out;
654888973f5SMichael Tuexen 		}
655b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
656b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
657413628a7SBjoern A. Zeeb 			goto out;
6584a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6594a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
660c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
661b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
662c1604fe4SGleb Smirnoff 			goto out_in_epoch;
66309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
66409fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
665adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
66609fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
667c1604fe4SGleb Smirnoff 			goto out_in_epoch;
66809fe6320SNavdeep Parhar #endif
66955bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
670c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6715dba6adaSMichael Tuexen 	} else {
6725dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6735dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6745dba6adaSMichael Tuexen 			goto out;
6755dba6adaSMichael Tuexen 		}
676fb59c426SYoshinobu Inoue 	}
677b287c6c7SBjoern A. Zeeb #endif
6784a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6794a91aa8fSMichael Tuexen 		goto out;
680fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
681fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
682dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
683b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
684fb59c426SYoshinobu Inoue 		goto out;
68509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
68609fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
687adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
68809fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
68909fe6320SNavdeep Parhar 		goto out;
69009fe6320SNavdeep Parhar #endif
69109fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
692109eb549SGleb Smirnoff 	NET_EPOCH_ENTER(et);
69355bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
6947754e281SBjoern A. Zeeb #ifdef INET
695c1604fe4SGleb Smirnoff out_in_epoch:
6967754e281SBjoern A. Zeeb #endif
697109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
698623dce13SRobert Watson out:
6994a91aa8fSMichael Tuexen 	/*
7004a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
7014a91aa8fSMichael Tuexen 	 * the flags we modified.
7024a91aa8fSMichael Tuexen 	 */
7034a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
7044a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
7054a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
7064a91aa8fSMichael Tuexen 	}
7074a91aa8fSMichael Tuexen 
708623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
7095d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
7108501a69cSRobert Watson 	INP_WUNLOCK(inp);
711623dce13SRobert Watson 	return (error);
712fb59c426SYoshinobu Inoue }
713fb59c426SYoshinobu Inoue #endif /* INET6 */
714fb59c426SYoshinobu Inoue 
7152c37256eSGarrett Wollman /*
7162c37256eSGarrett Wollman  * Initiate disconnect from peer.
7172c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7182c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7192c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7202c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7212c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7222c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7232c37256eSGarrett Wollman  *
7242c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7252c37256eSGarrett Wollman  */
7262c37256eSGarrett Wollman static int
7272c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7282c37256eSGarrett Wollman {
729f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
730623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7316573d758SMatt Macy 	struct epoch_tracker et;
732623dce13SRobert Watson 	int error = 0;
7332c37256eSGarrett Wollman 
734623dce13SRobert Watson 	TCPDEBUG0;
73597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
736623dce13SRobert Watson 	inp = sotoinpcb(so);
737623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7388501a69cSRobert Watson 	INP_WLOCK(inp);
739489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
740489dcc92SJulien Charbon 		goto out;
741489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
74221367f63SSam Leffler 		error = ECONNRESET;
743623dce13SRobert Watson 		goto out;
744623dce13SRobert Watson 	}
745623dce13SRobert Watson 	tp = intotcpcb(inp);
746623dce13SRobert Watson 	TCPDEBUG1();
747623dce13SRobert Watson 	tcp_disconnect(tp);
748623dce13SRobert Watson out:
749623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7505d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7518501a69cSRobert Watson 	INP_WUNLOCK(inp);
75297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
753623dce13SRobert Watson 	return (error);
7542c37256eSGarrett Wollman }
7552c37256eSGarrett Wollman 
756b287c6c7SBjoern A. Zeeb #ifdef INET
7572c37256eSGarrett Wollman /*
7588296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7598296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7602c37256eSGarrett Wollman  */
7612c37256eSGarrett Wollman static int
76257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7632c37256eSGarrett Wollman {
7642c37256eSGarrett Wollman 	int error = 0;
765f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7661db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
76726ef6ac4SDon Lewis 	struct in_addr addr;
76826ef6ac4SDon Lewis 	in_port_t port = 0;
7691db24ffbSJonathan Lemon 	TCPDEBUG0;
7702c37256eSGarrett Wollman 
7713d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7723d2d3ef4SRobert Watson 		return (ECONNABORTED);
773f76fcf6dSJeffrey Hsu 
774f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
775623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7768501a69cSRobert Watson 	INP_WLOCK(inp);
777ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7783d2d3ef4SRobert Watson 		error = ECONNABORTED;
779623dce13SRobert Watson 		goto out;
780623dce13SRobert Watson 	}
7811db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7821db24ffbSJonathan Lemon 	TCPDEBUG1();
783f76fcf6dSJeffrey Hsu 
784f76fcf6dSJeffrey Hsu 	/*
78554d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
78626ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
78726ef6ac4SDon Lewis 	 * release the lock.
788f76fcf6dSJeffrey Hsu 	 */
78926ef6ac4SDon Lewis 	port = inp->inp_fport;
79026ef6ac4SDon Lewis 	addr = inp->inp_faddr;
791f76fcf6dSJeffrey Hsu 
792623dce13SRobert Watson out:
793623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
7945d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7958501a69cSRobert Watson 	INP_WUNLOCK(inp);
79626ef6ac4SDon Lewis 	if (error == 0)
79726ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
79826ef6ac4SDon Lewis 	return error;
7992c37256eSGarrett Wollman }
800b287c6c7SBjoern A. Zeeb #endif /* INET */
8012c37256eSGarrett Wollman 
802fb59c426SYoshinobu Inoue #ifdef INET6
803fb59c426SYoshinobu Inoue static int
804fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
805fb59c426SYoshinobu Inoue {
806f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
807fb59c426SYoshinobu Inoue 	int error = 0;
8081db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
80926ef6ac4SDon Lewis 	struct in_addr addr;
81026ef6ac4SDon Lewis 	struct in6_addr addr6;
8116573d758SMatt Macy 	struct epoch_tracker et;
81226ef6ac4SDon Lewis 	in_port_t port = 0;
81326ef6ac4SDon Lewis 	int v4 = 0;
8141db24ffbSJonathan Lemon 	TCPDEBUG0;
815fb59c426SYoshinobu Inoue 
816b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
817b4470c16SRobert Watson 		return (ECONNABORTED);
818f76fcf6dSJeffrey Hsu 
819f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
820623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
82197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8228501a69cSRobert Watson 	INP_WLOCK(inp);
823ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
82421367f63SSam Leffler 		error = ECONNABORTED;
825623dce13SRobert Watson 		goto out;
826623dce13SRobert Watson 	}
8271db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
8281db24ffbSJonathan Lemon 	TCPDEBUG1();
829623dce13SRobert Watson 
83026ef6ac4SDon Lewis 	/*
83126ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
83226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
83326ef6ac4SDon Lewis 	 * release the lock.
83426ef6ac4SDon Lewis 	 */
83526ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
83626ef6ac4SDon Lewis 		v4 = 1;
83726ef6ac4SDon Lewis 		port = inp->inp_fport;
83826ef6ac4SDon Lewis 		addr = inp->inp_faddr;
83926ef6ac4SDon Lewis 	} else {
84026ef6ac4SDon Lewis 		port = inp->inp_fport;
84126ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
84226ef6ac4SDon Lewis 	}
84326ef6ac4SDon Lewis 
844623dce13SRobert Watson out:
845623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8465d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8478501a69cSRobert Watson 	INP_WUNLOCK(inp);
84897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
84926ef6ac4SDon Lewis 	if (error == 0) {
85026ef6ac4SDon Lewis 		if (v4)
85126ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
85226ef6ac4SDon Lewis 		else
85326ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
85426ef6ac4SDon Lewis 	}
85526ef6ac4SDon Lewis 	return error;
856fb59c426SYoshinobu Inoue }
857fb59c426SYoshinobu Inoue #endif /* INET6 */
858f76fcf6dSJeffrey Hsu 
859f76fcf6dSJeffrey Hsu /*
8602c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8612c37256eSGarrett Wollman  */
8622c37256eSGarrett Wollman static int
8632c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8642c37256eSGarrett Wollman {
8652c37256eSGarrett Wollman 	int error = 0;
866f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
867623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8686573d758SMatt Macy 	struct epoch_tracker et;
8692c37256eSGarrett Wollman 
870623dce13SRobert Watson 	TCPDEBUG0;
87197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
872623dce13SRobert Watson 	inp = sotoinpcb(so);
873623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8748501a69cSRobert Watson 	INP_WLOCK(inp);
875ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
87621367f63SSam Leffler 		error = ECONNRESET;
877623dce13SRobert Watson 		goto out;
878623dce13SRobert Watson 	}
879623dce13SRobert Watson 	tp = intotcpcb(inp);
880623dce13SRobert Watson 	TCPDEBUG1();
8812c37256eSGarrett Wollman 	socantsendmore(so);
882623dce13SRobert Watson 	tcp_usrclosed(tp);
883ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
88455bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
885623dce13SRobert Watson 
886623dce13SRobert Watson out:
887623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8885d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8898501a69cSRobert Watson 	INP_WUNLOCK(inp);
89097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
891623dce13SRobert Watson 
892623dce13SRobert Watson 	return (error);
8932c37256eSGarrett Wollman }
8942c37256eSGarrett Wollman 
8952c37256eSGarrett Wollman /*
8962c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8972c37256eSGarrett Wollman  */
8982c37256eSGarrett Wollman static int
8992c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
9002c37256eSGarrett Wollman {
901109eb549SGleb Smirnoff 	struct epoch_tracker et;
902f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
903623dce13SRobert Watson 	struct tcpcb *tp = NULL;
904623dce13SRobert Watson 	int error = 0;
9052c37256eSGarrett Wollman 
906623dce13SRobert Watson 	TCPDEBUG0;
907623dce13SRobert Watson 	inp = sotoinpcb(so);
908623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
9098501a69cSRobert Watson 	INP_WLOCK(inp);
910ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
91121367f63SSam Leffler 		error = ECONNRESET;
912623dce13SRobert Watson 		goto out;
913623dce13SRobert Watson 	}
914623dce13SRobert Watson 	tp = intotcpcb(inp);
915623dce13SRobert Watson 	TCPDEBUG1();
916281a0fd4SPatrick Kelsey 	/*
917281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
918281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
919281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
920281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
921281a0fd4SPatrick Kelsey 	 * expires.
922281a0fd4SPatrick Kelsey 	 */
92368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
924281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
925281a0fd4SPatrick Kelsey 		goto out;
92642ce7937SGleb Smirnoff 	NET_EPOCH_ENTER(et);
92709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
92809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
92909fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
930460cf046SNavdeep Parhar 	else
93109fe6320SNavdeep Parhar #endif
93255bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
933109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
934623dce13SRobert Watson out:
935623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9365d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
9378501a69cSRobert Watson 	INP_WUNLOCK(inp);
938623dce13SRobert Watson 	return (error);
9392c37256eSGarrett Wollman }
9402c37256eSGarrett Wollman 
9412c37256eSGarrett Wollman /*
9422c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9439c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9449c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9459c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9469c9906e9SPeter Wemm  * generally are caller-frees.
9472c37256eSGarrett Wollman  */
9482c37256eSGarrett Wollman static int
94957bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
950b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9512c37256eSGarrett Wollman {
95297a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9532c37256eSGarrett Wollman 	int error = 0;
954f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
955623dce13SRobert Watson 	struct tcpcb *tp = NULL;
956888973f5SMichael Tuexen #ifdef INET
95751e08d53SMichael Tuexen #ifdef INET6
95851e08d53SMichael Tuexen 	struct sockaddr_in sin;
95951e08d53SMichael Tuexen #endif
96051e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
961888973f5SMichael Tuexen #endif
962fb59c426SYoshinobu Inoue #ifdef INET6
963fb59c426SYoshinobu Inoue 	int isipv6;
964fb59c426SYoshinobu Inoue #endif
9654a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9664a91aa8fSMichael Tuexen 	u_char vflagsav;
9674a91aa8fSMichael Tuexen 	bool restoreflags;
9689c9906e9SPeter Wemm 	TCPDEBUG0;
9692c37256eSGarrett Wollman 
970f76fcf6dSJeffrey Hsu 	/*
97197a95ee1SGleb Smirnoff 	 * We require the pcbinfo "read lock" if we will close the socket
97297a95ee1SGleb Smirnoff 	 * as part of this call.
973f76fcf6dSJeffrey Hsu 	 */
97497a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
975f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
976623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9778501a69cSRobert Watson 	INP_WLOCK(inp);
9784a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
9794a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
9804a91aa8fSMichael Tuexen 	restoreflags = false;
981ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9827ff0b850SAndre Oppermann 		if (control)
9837ff0b850SAndre Oppermann 			m_freem(control);
9842cbcd3c1SGleb Smirnoff 		/*
9852cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9862cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9872cbcd3c1SGleb Smirnoff 		 */
9882cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9897ff0b850SAndre Oppermann 			m_freem(m);
99021367f63SSam Leffler 		error = ECONNRESET;
9919c9906e9SPeter Wemm 		goto out;
9929c9906e9SPeter Wemm 	}
9939c9906e9SPeter Wemm 	tp = intotcpcb(inp);
994d3b6c96bSRandall Stewart 	if (flags & PRUS_OOB) {
995d3b6c96bSRandall Stewart 		if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) {
996d3b6c96bSRandall Stewart 			if (control)
997d3b6c96bSRandall Stewart 				m_freem(control);
998d3b6c96bSRandall Stewart 			if (m && (flags & PRUS_NOTREADY) == 0)
999d3b6c96bSRandall Stewart 				m_freem(m);
1000d3b6c96bSRandall Stewart 			goto out;
1001d3b6c96bSRandall Stewart 		}
1002d3b6c96bSRandall Stewart 	}
10039c9906e9SPeter Wemm 	TCPDEBUG1();
1004888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1005888973f5SMichael Tuexen 		switch (nam->sa_family) {
1006888973f5SMichael Tuexen #ifdef INET
1007888973f5SMichael Tuexen 		case AF_INET:
1008888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
1009888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1010888973f5SMichael Tuexen 				if (m)
1011888973f5SMichael Tuexen 					m_freem(m);
1012888973f5SMichael Tuexen 				error = EINVAL;
1013888973f5SMichael Tuexen 				goto out;
1014888973f5SMichael Tuexen 			}
1015888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
1016888973f5SMichael Tuexen 				if (m)
1017888973f5SMichael Tuexen 					m_freem(m);
1018888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1019888973f5SMichael Tuexen 				goto out;
1020888973f5SMichael Tuexen 			}
1021888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1022888973f5SMichael Tuexen 				if (m)
1023888973f5SMichael Tuexen 					m_freem(m);
1024888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1025888973f5SMichael Tuexen 				goto out;
1026888973f5SMichael Tuexen 			}
1027888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
1028888973f5SMichael Tuexen 			    &sinp->sin_addr))) {
1029888973f5SMichael Tuexen 				if (m)
1030888973f5SMichael Tuexen 					m_freem(m);
1031888973f5SMichael Tuexen 				goto out;
1032888973f5SMichael Tuexen 			}
1033888973f5SMichael Tuexen #ifdef INET6
1034888973f5SMichael Tuexen 			isipv6 = 0;
1035888973f5SMichael Tuexen #endif
1036888973f5SMichael Tuexen 			break;
1037888973f5SMichael Tuexen #endif /* INET */
1038888973f5SMichael Tuexen #ifdef INET6
1039888973f5SMichael Tuexen 		case AF_INET6:
1040888973f5SMichael Tuexen 		{
10410ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1042888973f5SMichael Tuexen 
10430ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10440ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1045888973f5SMichael Tuexen 				if (m)
1046888973f5SMichael Tuexen 					m_freem(m);
1047888973f5SMichael Tuexen 				error = EINVAL;
1048888973f5SMichael Tuexen 				goto out;
1049888973f5SMichael Tuexen 			}
1050e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1051e240ce42SMichael Tuexen 				if (m != NULL)
1052e240ce42SMichael Tuexen 					m_freem(m);
1053e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1054e240ce42SMichael Tuexen 				goto out;
1055e240ce42SMichael Tuexen 			}
10560ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1057888973f5SMichael Tuexen 				if (m)
1058888973f5SMichael Tuexen 					m_freem(m);
1059888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1060888973f5SMichael Tuexen 				goto out;
1061888973f5SMichael Tuexen 			}
10620ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1063888973f5SMichael Tuexen #ifdef INET
1064888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1065888973f5SMichael Tuexen 					error = EINVAL;
1066888973f5SMichael Tuexen 					if (m)
1067888973f5SMichael Tuexen 						m_freem(m);
1068888973f5SMichael Tuexen 					goto out;
1069888973f5SMichael Tuexen 				}
1070888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1071888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1072888973f5SMichael Tuexen 					if (m)
1073888973f5SMichael Tuexen 						m_freem(m);
1074888973f5SMichael Tuexen 					goto out;
1075888973f5SMichael Tuexen 				}
10764a91aa8fSMichael Tuexen 				restoreflags = true;
1077888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1078888973f5SMichael Tuexen 				sinp = &sin;
10790ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1080888973f5SMichael Tuexen 				if (IN_MULTICAST(
1081888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1082888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1083888973f5SMichael Tuexen 					if (m)
1084888973f5SMichael Tuexen 						m_freem(m);
1085888973f5SMichael Tuexen 					goto out;
1086888973f5SMichael Tuexen 				}
1087888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
1088888973f5SMichael Tuexen 				    &sinp->sin_addr))) {
1089888973f5SMichael Tuexen 					if (m)
1090888973f5SMichael Tuexen 						m_freem(m);
1091888973f5SMichael Tuexen 					goto out;
1092888973f5SMichael Tuexen 				}
1093888973f5SMichael Tuexen 				isipv6 = 0;
1094888973f5SMichael Tuexen #else /* !INET */
1095888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1096888973f5SMichael Tuexen 				if (m)
1097888973f5SMichael Tuexen 					m_freem(m);
1098888973f5SMichael Tuexen 				goto out;
1099888973f5SMichael Tuexen #endif /* INET */
1100888973f5SMichael Tuexen 			} else {
1101888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1102888973f5SMichael Tuexen 					if (m)
1103888973f5SMichael Tuexen 						m_freem(m);
1104888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1105888973f5SMichael Tuexen 					goto out;
1106888973f5SMichael Tuexen 				}
11074a91aa8fSMichael Tuexen 				restoreflags = true;
1108888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1109888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1110888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
11110ecd976eSBjoern A. Zeeb 				    &sin6->sin6_addr))) {
1112888973f5SMichael Tuexen 					if (m)
1113888973f5SMichael Tuexen 						m_freem(m);
1114888973f5SMichael Tuexen 					goto out;
1115888973f5SMichael Tuexen 				}
1116888973f5SMichael Tuexen 				isipv6 = 1;
1117888973f5SMichael Tuexen 			}
1118888973f5SMichael Tuexen 			break;
1119888973f5SMichael Tuexen 		}
1120888973f5SMichael Tuexen #endif /* INET6 */
1121888973f5SMichael Tuexen 		default:
1122888973f5SMichael Tuexen 			if (m)
1123888973f5SMichael Tuexen 				m_freem(m);
1124888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1125888973f5SMichael Tuexen 			goto out;
1126888973f5SMichael Tuexen 		}
1127888973f5SMichael Tuexen 	}
11289c9906e9SPeter Wemm 	if (control) {
11299c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
11309c9906e9SPeter Wemm 		if (control->m_len) {
11319c9906e9SPeter Wemm 			m_freem(control);
11322c37256eSGarrett Wollman 			if (m)
11332c37256eSGarrett Wollman 				m_freem(m);
1134744f87eaSDavid Greenman 			error = EINVAL;
1135744f87eaSDavid Greenman 			goto out;
11362c37256eSGarrett Wollman 		}
11379c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
11389c9906e9SPeter Wemm 	}
11392c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1140651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
11412c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
11422c37256eSGarrett Wollman 			/*
11432c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11442c37256eSGarrett Wollman 			 * initialize window to default value, and
11450c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11462c37256eSGarrett Wollman 			 */
1147fb59c426SYoshinobu Inoue #ifdef INET6
1148fb59c426SYoshinobu Inoue 			if (isipv6)
1149b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1150fb59c426SYoshinobu Inoue #endif /* INET6 */
1151b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1152b287c6c7SBjoern A. Zeeb 			else
1153b287c6c7SBjoern A. Zeeb #endif
1154b287c6c7SBjoern A. Zeeb #ifdef INET
1155888973f5SMichael Tuexen 				error = tcp_connect(tp,
1156888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1157b287c6c7SBjoern A. Zeeb #endif
11584a91aa8fSMichael Tuexen 			/*
11594a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11604a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11614a91aa8fSMichael Tuexen 			 * operations fail.
11624a91aa8fSMichael Tuexen 			 */
11634a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11644a91aa8fSMichael Tuexen 				restoreflags = false;
11654a91aa8fSMichael Tuexen 
11662c37256eSGarrett Wollman 			if (error)
11672c37256eSGarrett Wollman 				goto out;
1168c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1169c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
117018a75309SPatrick Kelsey 			else {
11712c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11722c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11732c37256eSGarrett Wollman 			}
1174c560df6fSPatrick Kelsey 		}
11752c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11762c37256eSGarrett Wollman 			/*
11772c37256eSGarrett Wollman 			 * Close the send side of the connection after
11782c37256eSGarrett Wollman 			 * the data is sent.
11792c37256eSGarrett Wollman 			 */
11802c37256eSGarrett Wollman 			socantsendmore(so);
1181623dce13SRobert Watson 			tcp_usrclosed(tp);
11822c37256eSGarrett Wollman 		}
11832cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11842cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1185b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1186b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
118755bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
1188b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1189b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1190b0acefa8SBill Fenner 		}
11912c37256eSGarrett Wollman 	} else {
1192623dce13SRobert Watson 		/*
1193623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1194623dce13SRobert Watson 		 */
1195d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11962c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1197d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11982c37256eSGarrett Wollman 			m_freem(m);
11992c37256eSGarrett Wollman 			error = ENOBUFS;
12002c37256eSGarrett Wollman 			goto out;
12012c37256eSGarrett Wollman 		}
12022c37256eSGarrett Wollman 		/*
12032c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
12042c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
12052c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
12062c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
12072c37256eSGarrett Wollman 		 * of data past the urgent section.
12082c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
12092c37256eSGarrett Wollman 		 */
1210651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1211d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1212ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1213ef53690bSGarrett Wollman 			/*
1214ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1215ef53690bSGarrett Wollman 			 * initialize window to default value, and
12160c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1217ef53690bSGarrett Wollman 			 */
121818a75309SPatrick Kelsey 
1219c560df6fSPatrick Kelsey 			/*
1220c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1221c560df6fSPatrick Kelsey 			 */
1222c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1223c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1224fb59c426SYoshinobu Inoue #ifdef INET6
1225fb59c426SYoshinobu Inoue 			if (isipv6)
1226b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1227fb59c426SYoshinobu Inoue #endif /* INET6 */
1228b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1229b287c6c7SBjoern A. Zeeb 			else
1230b287c6c7SBjoern A. Zeeb #endif
1231b287c6c7SBjoern A. Zeeb #ifdef INET
1232888973f5SMichael Tuexen 				error = tcp_connect(tp,
1233888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1234b287c6c7SBjoern A. Zeeb #endif
12354a91aa8fSMichael Tuexen 			/*
12364a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12374a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12384a91aa8fSMichael Tuexen 			 * operations fail.
12394a91aa8fSMichael Tuexen 			 */
12404a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12414a91aa8fSMichael Tuexen 				restoreflags = false;
12424a91aa8fSMichael Tuexen 
1243ef53690bSGarrett Wollman 			if (error)
1244ef53690bSGarrett Wollman 				goto out;
1245ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1246ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1247623dce13SRobert Watson 		}
1248300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12492cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
12502cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
125155bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
12522cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12532c37256eSGarrett Wollman 		}
12542cbcd3c1SGleb Smirnoff 	}
12552529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12562529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12572529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12582529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12592529f56eSJonathan T. Looney 	    0, NULL, false);
1260d1401c90SRobert Watson out:
12614a91aa8fSMichael Tuexen 	/*
12624a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12634a91aa8fSMichael Tuexen 	 * restore the original flags.
12644a91aa8fSMichael Tuexen 	 */
12654a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12664a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12674a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12684a91aa8fSMichael Tuexen 	}
1269d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12702c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12715d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12725d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12738501a69cSRobert Watson 	INP_WUNLOCK(inp);
127497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
127573fddedaSPeter Grehan 	return (error);
12762c37256eSGarrett Wollman }
12772c37256eSGarrett Wollman 
12782cbcd3c1SGleb Smirnoff static int
12792cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12802cbcd3c1SGleb Smirnoff {
1281109eb549SGleb Smirnoff 	struct epoch_tracker et;
12822cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12832cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12842cbcd3c1SGleb Smirnoff 	int error;
12852cbcd3c1SGleb Smirnoff 
12862cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12872cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
12882cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
12892cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
129082334850SJohn Baldwin 		mb_free_notready(m, count);
12912cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12922cbcd3c1SGleb Smirnoff 	}
12932cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12942cbcd3c1SGleb Smirnoff 
12952cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12962cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12972cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1298109eb549SGleb Smirnoff 	if (error == 0) {
1299109eb549SGleb Smirnoff 		NET_EPOCH_ENTER(et);
130055bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
1301109eb549SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1302109eb549SGleb Smirnoff 	}
13032cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
13042cbcd3c1SGleb Smirnoff 
13052cbcd3c1SGleb Smirnoff 	return (error);
13062cbcd3c1SGleb Smirnoff }
13072cbcd3c1SGleb Smirnoff 
13082c37256eSGarrett Wollman /*
1309a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
13102c37256eSGarrett Wollman  */
1311ac45e92fSRobert Watson static void
13122c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
13132c37256eSGarrett Wollman {
1314f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1315a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13166573d758SMatt Macy 	struct epoch_tracker et;
1317623dce13SRobert Watson 	TCPDEBUG0;
1318c78cbc7bSRobert Watson 
1319ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1320c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1321c78cbc7bSRobert Watson 
132297a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13238501a69cSRobert Watson 	INP_WLOCK(inp);
1324c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1325c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1326c78cbc7bSRobert Watson 
1327c78cbc7bSRobert Watson 	/*
1328a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1329c78cbc7bSRobert Watson 	 */
1330ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1331ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1332c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1333a152f8a3SRobert Watson 		TCPDEBUG1();
13348fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13358fa799bdSJonathan T. Looney 		if (tp == NULL)
13368fa799bdSJonathan T. Looney 			goto dropped;
1337a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13385d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1339c78cbc7bSRobert Watson 	}
1340ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1341a152f8a3SRobert Watson 		SOCK_LOCK(so);
1342a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1343a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1344ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1345a152f8a3SRobert Watson 	}
13468501a69cSRobert Watson 	INP_WUNLOCK(inp);
13478fa799bdSJonathan T. Looney dropped:
134897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1349a152f8a3SRobert Watson }
1350a152f8a3SRobert Watson 
1351a152f8a3SRobert Watson /*
1352a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1353a152f8a3SRobert Watson  */
1354a152f8a3SRobert Watson static void
1355a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1356a152f8a3SRobert Watson {
1357a152f8a3SRobert Watson 	struct inpcb *inp;
1358a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13596573d758SMatt Macy 	struct epoch_tracker et;
1360a152f8a3SRobert Watson 	TCPDEBUG0;
1361a152f8a3SRobert Watson 
1362a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1363a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1364a152f8a3SRobert Watson 
136597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13668501a69cSRobert Watson 	INP_WLOCK(inp);
1367a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1368a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1369a152f8a3SRobert Watson 
1370a152f8a3SRobert Watson 	/*
1371a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1372a152f8a3SRobert Watson 	 * a disconnect.
1373a152f8a3SRobert Watson 	 */
1374ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1375ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1376a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1377a152f8a3SRobert Watson 		TCPDEBUG1();
1378a152f8a3SRobert Watson 		tcp_disconnect(tp);
1379a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
13805d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1381a152f8a3SRobert Watson 	}
1382ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1383a152f8a3SRobert Watson 		SOCK_LOCK(so);
1384a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1385a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1386ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1387a152f8a3SRobert Watson 	}
13888501a69cSRobert Watson 	INP_WUNLOCK(inp);
138997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13902c37256eSGarrett Wollman }
13912c37256eSGarrett Wollman 
1392d3b6c96bSRandall Stewart static int
1393d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1394d3b6c96bSRandall Stewart {
1395d3b6c96bSRandall Stewart 	/*
1396d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1397d3b6c96bSRandall Stewart 	 * specified then it does not always support
1398d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1399d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1400d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1401d3b6c96bSRandall Stewart 	 */
1402d3b6c96bSRandall Stewart 	int ret = 0;
1403d3b6c96bSRandall Stewart 
1404d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1405d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1406d3b6c96bSRandall Stewart 	}
1407d3b6c96bSRandall Stewart 	return (ret);
1408d3b6c96bSRandall Stewart }
1409d3b6c96bSRandall Stewart 
14102c37256eSGarrett Wollman /*
14112c37256eSGarrett Wollman  * Receive out-of-band data.
14122c37256eSGarrett Wollman  */
14132c37256eSGarrett Wollman static int
14142c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
14152c37256eSGarrett Wollman {
14162c37256eSGarrett Wollman 	int error = 0;
1417f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1418623dce13SRobert Watson 	struct tcpcb *tp = NULL;
14192c37256eSGarrett Wollman 
1420623dce13SRobert Watson 	TCPDEBUG0;
1421623dce13SRobert Watson 	inp = sotoinpcb(so);
1422623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
14238501a69cSRobert Watson 	INP_WLOCK(inp);
1424ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
142521367f63SSam Leffler 		error = ECONNRESET;
1426623dce13SRobert Watson 		goto out;
1427623dce13SRobert Watson 	}
1428623dce13SRobert Watson 	tp = intotcpcb(inp);
1429d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1430d3b6c96bSRandall Stewart 	if (error) {
1431d3b6c96bSRandall Stewart 		goto out;
1432d3b6c96bSRandall Stewart 	}
1433623dce13SRobert Watson 	TCPDEBUG1();
14342c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1435c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
14364cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
14374cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
14382c37256eSGarrett Wollman 		error = EINVAL;
14392c37256eSGarrett Wollman 		goto out;
14402c37256eSGarrett Wollman 	}
14412c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
14422c37256eSGarrett Wollman 		error = EWOULDBLOCK;
14432c37256eSGarrett Wollman 		goto out;
14442c37256eSGarrett Wollman 	}
14452c37256eSGarrett Wollman 	m->m_len = 1;
14462c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
14472c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
14482c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1449623dce13SRobert Watson 
1450623dce13SRobert Watson out:
1451623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14525d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14538501a69cSRobert Watson 	INP_WUNLOCK(inp);
1454623dce13SRobert Watson 	return (error);
14552c37256eSGarrett Wollman }
14562c37256eSGarrett Wollman 
1457b287c6c7SBjoern A. Zeeb #ifdef INET
14582c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1459756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1460756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1461756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1462756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1463756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1464756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1465756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1466756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1467756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
146854d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1469756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1470756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1471756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14722cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1473756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
147454d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1475a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1476a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
14772c37256eSGarrett Wollman };
1478b287c6c7SBjoern A. Zeeb #endif /* INET */
1479df8bae1dSRodney W. Grimes 
1480fb59c426SYoshinobu Inoue #ifdef INET6
1481fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1482756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1483756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1484756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1485756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1486756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1487756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1488756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1489756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1490756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1491756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1492756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1493756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1494756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14952cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1496756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1497756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1498a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1499a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1500fb59c426SYoshinobu Inoue };
1501fb59c426SYoshinobu Inoue #endif /* INET6 */
1502fb59c426SYoshinobu Inoue 
1503b287c6c7SBjoern A. Zeeb #ifdef INET
1504a0292f23SGarrett Wollman /*
1505a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1506a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
15075200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
15085200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
15095200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
15105200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1511a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1512a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1513a0292f23SGarrett Wollman  */
15140312fbe9SPoul-Henning Kamp static int
1515ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1516a0292f23SGarrett Wollman {
1517a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1518a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
15195200e00eSIan Dowse 	struct in_addr laddr;
15205200e00eSIan Dowse 	u_short lport;
1521c3229e05SDavid Greenman 	int error;
1522a0292f23SGarrett Wollman 
1523c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
15248501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1525fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1526623dce13SRobert Watson 
152725102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
15284616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15294616026fSErmal Luçi 		if (error)
1530fa046d87SRobert Watson 			goto out;
1531a0292f23SGarrett Wollman 	}
1532a0292f23SGarrett Wollman 
1533a0292f23SGarrett Wollman 	/*
1534a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1535a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1536a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1537a0292f23SGarrett Wollman 	 */
15385200e00eSIan Dowse 	laddr = inp->inp_laddr;
15395200e00eSIan Dowse 	lport = inp->inp_lport;
15405200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1541b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
15425200e00eSIan Dowse 	if (error && oinp == NULL)
1543fa046d87SRobert Watson 		goto out;
1544fa046d87SRobert Watson 	if (oinp) {
1545fa046d87SRobert Watson 		error = EADDRINUSE;
1546fa046d87SRobert Watson 		goto out;
1547fa046d87SRobert Watson 	}
154825102351SMike Karels 	/* Handle initial bind if it hadn't been done in advance. */
154925102351SMike Karels 	if (inp->inp_lport == 0) {
155025102351SMike Karels 		inp->inp_lport = lport;
155125102351SMike Karels 		if (in_pcbinshash(inp) != 0) {
155225102351SMike Karels 			inp->inp_lport = 0;
155325102351SMike Karels 			error = EAGAIN;
155425102351SMike Karels 			goto out;
155525102351SMike Karels 		}
155625102351SMike Karels 	}
15575200e00eSIan Dowse 	inp->inp_laddr = laddr;
155815bd2b43SDavid Greenman 	in_pcbrehash(inp);
1559fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1560a0292f23SGarrett Wollman 
1561087b55eaSAndre Oppermann 	/*
1562087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1563087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1564087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1565087b55eaSAndre Oppermann 	 */
1566a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15679b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1568a0292f23SGarrett Wollman 		tp->request_r_scale++;
1569a0292f23SGarrett Wollman 
1570a0292f23SGarrett Wollman 	soisconnecting(so);
157178b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
157257f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15738e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15748e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15758e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1576a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1577a45d2726SAndras Olah 
1578a0292f23SGarrett Wollman 	return 0;
1579fa046d87SRobert Watson 
1580fa046d87SRobert Watson out:
1581fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1582fa046d87SRobert Watson 	return (error);
1583a0292f23SGarrett Wollman }
1584b287c6c7SBjoern A. Zeeb #endif /* INET */
1585a0292f23SGarrett Wollman 
1586fb59c426SYoshinobu Inoue #ifdef INET6
1587fb59c426SYoshinobu Inoue static int
1588ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1589fb59c426SYoshinobu Inoue {
1590a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1591fb59c426SYoshinobu Inoue 	int error;
1592fb59c426SYoshinobu Inoue 
15938501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1594fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1595623dce13SRobert Watson 
159625102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
15974616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15984616026fSErmal Luçi 		if (error)
1599fa046d87SRobert Watson 			goto out;
1600fb59c426SYoshinobu Inoue 	}
1601a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1602a7e201bbSAndrey V. Elsukov 	if (error != 0)
1603b598155aSRobert Watson 		goto out;
1604fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1605fb59c426SYoshinobu Inoue 
1606fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1607fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1608970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1609fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1610fb59c426SYoshinobu Inoue 
1611a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
161278b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
161357f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
16148e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
16158e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
16168e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1617fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1618fb59c426SYoshinobu Inoue 
1619fb59c426SYoshinobu Inoue 	return 0;
1620fa046d87SRobert Watson 
1621fa046d87SRobert Watson out:
1622fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1623fa046d87SRobert Watson 	return error;
1624fb59c426SYoshinobu Inoue }
1625fb59c426SYoshinobu Inoue #endif /* INET6 */
1626fb59c426SYoshinobu Inoue 
1627cfe8b629SGarrett Wollman /*
1628b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1629b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1630b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1631b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1632b8af5dfaSRobert Watson  * from Linux.
1633b8af5dfaSRobert Watson  */
1634b8af5dfaSRobert Watson static void
1635ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1636b8af5dfaSRobert Watson {
1637b8af5dfaSRobert Watson 
16388501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1639b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1640b8af5dfaSRobert Watson 
1641b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1642b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1643b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
16443529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1645b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1646b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1647b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1648b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1649b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1650b8af5dfaSRobert Watson 	}
16513cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT)
16525a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
16531baaf834SBruce M Simpson 
165443d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
16553ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
16561baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
16571baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16581baaf834SBruce M Simpson 
1659b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1660b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1661b8af5dfaSRobert Watson 
1662b8af5dfaSRobert Watson 	/*
1663b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1664b8af5dfaSRobert Watson 	 */
1665c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1666535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1667b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16681c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1669535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
167043d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
167143d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1672f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1673f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1674f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1675a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1676a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1677a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1678a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1679a6456410SNavdeep Parhar 	}
1680a6456410SNavdeep Parhar #endif
1681b8af5dfaSRobert Watson }
1682b8af5dfaSRobert Watson 
1683b8af5dfaSRobert Watson /*
16841e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16851e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16861e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16871e8f5ffaSRobert Watson  * option.
1688cfe8b629SGarrett Wollman  */
1689bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16908501a69cSRobert Watson 	INP_WLOCK(inp);							\
1691ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
16928501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1693bac5bedfSConrad Meyer 		cleanup;						\
16941e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16951e8f5ffaSRobert Watson 	}								\
16961e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16971e8f5ffaSRobert Watson } while(0)
1698bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16991e8f5ffaSRobert Watson 
1700df8bae1dSRodney W. Grimes int
1701ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1702df8bae1dSRodney W. Grimes {
170355bceb1eSRandall Stewart 	int	error;
1704df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1705cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
170655bceb1eSRandall Stewart 	struct tcp_function_block *blk;
170755bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1708df8bae1dSRodney W. Grimes 
1709cfe8b629SGarrett Wollman 	error = 0;
1710df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1711623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1712cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1713fb59c426SYoshinobu Inoue #ifdef INET6
17145cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
1715fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
17165dff1c38SMichael Tuexen 			/*
17175dff1c38SMichael Tuexen 			 * In case of the IPV6_USE_MIN_MTU socket option,
17185dff1c38SMichael Tuexen 			 * the INC_IPV6MINMTU flag to announce a corresponding
17195dff1c38SMichael Tuexen 			 * MSS during the initial handshake.
17205dff1c38SMichael Tuexen 			 * If the TCP connection is not in the front states,
17215dff1c38SMichael Tuexen 			 * just reduce the MSS being used.
17225dff1c38SMichael Tuexen 			 * This avoids the sending of TCP segments which will
17235dff1c38SMichael Tuexen 			 * be fragmented at the IPv6 layer.
17245dff1c38SMichael Tuexen 			 */
17255dff1c38SMichael Tuexen 			if ((error == 0) &&
17265dff1c38SMichael Tuexen 			    (sopt->sopt_dir == SOPT_SET) &&
17275dff1c38SMichael Tuexen 			    (sopt->sopt_level == IPPROTO_IPV6) &&
17285dff1c38SMichael Tuexen 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
17295dff1c38SMichael Tuexen 				INP_WLOCK(inp);
17305dff1c38SMichael Tuexen 				if ((inp->inp_flags &
17315dff1c38SMichael Tuexen 				    (INP_TIMEWAIT | INP_DROPPED))) {
17325dff1c38SMichael Tuexen 					INP_WUNLOCK(inp);
17335dff1c38SMichael Tuexen 					return (ECONNRESET);
17345dff1c38SMichael Tuexen 				}
17355dff1c38SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
17365dff1c38SMichael Tuexen 				tp = intotcpcb(inp);
17375dff1c38SMichael Tuexen 				if ((tp->t_state >= TCPS_SYN_SENT) &&
17385dff1c38SMichael Tuexen 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
17395dff1c38SMichael Tuexen 					struct ip6_pktopts *opt;
17405dff1c38SMichael Tuexen 
17415dff1c38SMichael Tuexen 					opt = inp->in6p_outputopts;
17425dff1c38SMichael Tuexen 					if ((opt != NULL) &&
17435dff1c38SMichael Tuexen 					    (opt->ip6po_minmtu ==
17445dff1c38SMichael Tuexen 					    IP6PO_MINMTU_ALL)) {
17455dff1c38SMichael Tuexen 						if (tp->t_maxseg > TCP6_MSS) {
17465dff1c38SMichael Tuexen 							tp->t_maxseg = TCP6_MSS;
17475dff1c38SMichael Tuexen 						}
17485dff1c38SMichael Tuexen 					}
17495dff1c38SMichael Tuexen 				}
17505dff1c38SMichael Tuexen 				INP_WUNLOCK(inp);
17515dff1c38SMichael Tuexen 			}
1752b287c6c7SBjoern A. Zeeb 		}
1753fb59c426SYoshinobu Inoue #endif /* INET6 */
1754b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1755b287c6c7SBjoern A. Zeeb 		else
1756b287c6c7SBjoern A. Zeeb #endif
1757b287c6c7SBjoern A. Zeeb #ifdef INET
1758b287c6c7SBjoern A. Zeeb 		{
1759cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
17601e8f5ffaSRobert Watson 		}
17611e8f5ffaSRobert Watson #endif
1762df8bae1dSRodney W. Grimes 		return (error);
1763df8bae1dSRodney W. Grimes 	}
176468cea2b1SJohn Baldwin 	INP_WLOCK(inp);
1765ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17668501a69cSRobert Watson 		INP_WUNLOCK(inp);
17671e8f5ffaSRobert Watson 		return (ECONNRESET);
1768623dce13SRobert Watson 	}
176955bceb1eSRandall Stewart 	tp = intotcpcb(inp);
177055bceb1eSRandall Stewart 	/*
177155bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
177255bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
177355bceb1eSRandall Stewart 	 */
177455bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
177555bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
177655bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
177755bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
177855bceb1eSRandall Stewart 		    sizeof fsn);
177955bceb1eSRandall Stewart 		if (error)
178055bceb1eSRandall Stewart 			return (error);
178155bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
178255bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
178355bceb1eSRandall Stewart 		if (blk == NULL) {
178455bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
178555bceb1eSRandall Stewart 			return (ENOENT);
178655bceb1eSRandall Stewart 		}
1787587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1788587d67c0SRandall Stewart 			/* You already have this */
1789587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1790587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1791587d67c0SRandall Stewart 			return (0);
1792587d67c0SRandall Stewart 		}
1793587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1794587d67c0SRandall Stewart 			/*
1795587d67c0SRandall Stewart 			 * The user has advanced the state
1796587d67c0SRandall Stewart 			 * past the initial point, we may not
1797587d67c0SRandall Stewart 			 * be able to switch.
1798587d67c0SRandall Stewart 			 */
1799587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1800587d67c0SRandall Stewart 				/*
1801587d67c0SRandall Stewart 				 * Does the stack provide a
1802587d67c0SRandall Stewart 				 * query mechanism, if so it may
1803587d67c0SRandall Stewart 				 * still be possible?
1804587d67c0SRandall Stewart 				 */
1805587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1806c6c0be27SMichael Tuexen 			} else
1807c6c0be27SMichael Tuexen 				error = EINVAL;
1808587d67c0SRandall Stewart 			if (error) {
1809587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1810587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1811587d67c0SRandall Stewart 				return(error);
1812587d67c0SRandall Stewart 			}
1813587d67c0SRandall Stewart 		}
181455bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
181555bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
181655bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
181755bceb1eSRandall Stewart 			return (ENOENT);
181855bceb1eSRandall Stewart 		}
181955bceb1eSRandall Stewart 		/*
182055bceb1eSRandall Stewart 		 * Release the old refcnt, the
1821587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1822587d67c0SRandall Stewart 		 * new one already.
182355bceb1eSRandall Stewart 		 */
1824587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1825587d67c0SRandall Stewart 			/*
1826587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1827587d67c0SRandall Stewart 			 * the tcb is not going away.
1828587d67c0SRandall Stewart 			 */
1829587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1830587d67c0SRandall Stewart 		}
18313ee9c3c4SRandall Stewart #ifdef TCPHPTS
18323ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
18333ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
18343ee9c3c4SRandall Stewart #endif
18353ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
18363ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
18373ee9c3c4SRandall Stewart 			if (error) {
18383ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
18393ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
18403ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
18413ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
18423ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
18433ee9c3c4SRandall Stewart 						soabort(so);
18443ee9c3c4SRandall Stewart 						return(error);
18453ee9c3c4SRandall Stewart 					}
18463ee9c3c4SRandall Stewart 				}
18473ee9c3c4SRandall Stewart 				goto err_out;
18483ee9c3c4SRandall Stewart 			}
18493ee9c3c4SRandall Stewart 		}
185055bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
185155bceb1eSRandall Stewart 		tp->t_fb = blk;
185255bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
185355bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
185455bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
185555bceb1eSRandall Stewart 			     sopt->sopt_name);
185655bceb1eSRandall Stewart 		}
185755bceb1eSRandall Stewart #endif
18583ee9c3c4SRandall Stewart err_out:
185955bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
186055bceb1eSRandall Stewart 		return (error);
186155bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
186255bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1863c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1864c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1865c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
186655bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
186755bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
186855bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
186955bceb1eSRandall Stewart 		return (error);
187055bceb1eSRandall Stewart 	}
187155bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
187255bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
187355bceb1eSRandall Stewart }
187455bceb1eSRandall Stewart 
18752529f56eSJonathan T. Looney /*
18762529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18772529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18782529f56eSJonathan T. Looney  */
18792529f56eSJonathan T. Looney #ifdef CTASSERT
18802529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18812529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18822529f56eSJonathan T. Looney #endif
18832529f56eSJonathan T. Looney 
1884ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1885ec1db6e1SJohn Baldwin static int
1886ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1887ec1db6e1SJohn Baldwin {
1888ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1889ec1db6e1SJohn Baldwin 	int error;
1890ec1db6e1SJohn Baldwin 
1891ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1892ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1893ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1894ec1db6e1SJohn Baldwin 		if (error)
1895ec1db6e1SJohn Baldwin 			return (error);
1896ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1897ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1898ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1899ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1900ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1901ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1902ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1903ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1904ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1905ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1906ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1907ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1908ec1db6e1SJohn Baldwin 		return (0);
1909ec1db6e1SJohn Baldwin 	}
1910ec1db6e1SJohn Baldwin 
1911ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1912ec1db6e1SJohn Baldwin }
1913ec1db6e1SJohn Baldwin #endif
1914ec1db6e1SJohn Baldwin 
191555bceb1eSRandall Stewart int
191655bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
191755bceb1eSRandall Stewart {
191855bceb1eSRandall Stewart 	int	error, opt, optval;
191955bceb1eSRandall Stewart 	u_int	ui;
192055bceb1eSRandall Stewart 	struct	tcp_info ti;
1921b2e60773SJohn Baldwin #ifdef KERN_TLS
1922b2e60773SJohn Baldwin 	struct tls_enable tls;
1923b2e60773SJohn Baldwin #endif
192455bceb1eSRandall Stewart 	struct cc_algo *algo;
19252529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1926adc56f5aSEdward Tomasz Napierala #ifdef STATS
1927adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
1928adc56f5aSEdward Tomasz Napierala #endif
1929af6fef3aSGleb Smirnoff 	size_t	len;
1930df8bae1dSRodney W. Grimes 
1931d519cedbSGleb Smirnoff 	/*
1932d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1933d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1934d519cedbSGleb Smirnoff 	 */
1935d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1936d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1937d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1938c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1939c8b53cedSMichael Tuexen 			return (EINVAL);
1940af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1941af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1942d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1943d519cedbSGleb Smirnoff 		if (error) {
1944af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1945d519cedbSGleb Smirnoff 			return (error);
1946d519cedbSGleb Smirnoff 		}
1947bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1948d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1949af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1950d519cedbSGleb Smirnoff 		else
1951d519cedbSGleb Smirnoff 			error = ENOENT;
1952d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1953d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1954af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1955af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1956d519cedbSGleb Smirnoff 		return (error);
1957d519cedbSGleb Smirnoff 	}
1958d519cedbSGleb Smirnoff 
1959cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1960cfe8b629SGarrett Wollman 	case SOPT_SET:
1961cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1962fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
196388f6b043SBruce M Simpson 		case TCP_MD5SIG:
1964fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19658501a69cSRobert Watson 				INP_WUNLOCK(inp);
1966fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1967fcf59617SAndrey V. Elsukov 			}
1968fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19691cfd4b53SBruce M Simpson 			if (error)
19701e8f5ffaSRobert Watson 				return (error);
197109fe6320SNavdeep Parhar 			goto unlock_and_done;
1972fcf59617SAndrey V. Elsukov #endif /* IPSEC */
197309fe6320SNavdeep Parhar 
1974df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
1975cfe8b629SGarrett Wollman 		case TCP_NOOPT:
19768501a69cSRobert Watson 			INP_WUNLOCK(inp);
1977cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1978cfe8b629SGarrett Wollman 			    sizeof optval);
1979cfe8b629SGarrett Wollman 			if (error)
19801e8f5ffaSRobert Watson 				return (error);
1981cfe8b629SGarrett Wollman 
19828501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
1983cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1984cfe8b629SGarrett Wollman 			case TCP_NODELAY:
1985cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
1986cfe8b629SGarrett Wollman 				break;
1987cfe8b629SGarrett Wollman 			case TCP_NOOPT:
1988cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
1989cfe8b629SGarrett Wollman 				break;
1990cfe8b629SGarrett Wollman 			default:
1991cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
1992cfe8b629SGarrett Wollman 				break;
1993cfe8b629SGarrett Wollman 			}
1994cfe8b629SGarrett Wollman 
1995cfe8b629SGarrett Wollman 			if (optval)
1996cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
1997df8bae1dSRodney W. Grimes 			else
1998cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
199909fe6320SNavdeep Parhar unlock_and_done:
200009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
200109fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
200209fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
200309fe6320SNavdeep Parhar 				    sopt->sopt_name);
200409fe6320SNavdeep Parhar 			}
200509fe6320SNavdeep Parhar #endif
20068501a69cSRobert Watson 			INP_WUNLOCK(inp);
2007df8bae1dSRodney W. Grimes 			break;
2008df8bae1dSRodney W. Grimes 
2009007581c0SJonathan Lemon 		case TCP_NOPUSH:
20108501a69cSRobert Watson 			INP_WUNLOCK(inp);
2011007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2012007581c0SJonathan Lemon 			    sizeof optval);
2013007581c0SJonathan Lemon 			if (error)
20141e8f5ffaSRobert Watson 				return (error);
2015007581c0SJonathan Lemon 
20168501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2017007581c0SJonathan Lemon 			if (optval)
2018007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2019d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2020007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2021109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2022109eb549SGleb Smirnoff 					struct epoch_tracker et;
2023109eb549SGleb Smirnoff 
2024109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
202555bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
2026109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2027109eb549SGleb Smirnoff 				}
2028007581c0SJonathan Lemon 			}
202909fe6320SNavdeep Parhar 			goto unlock_and_done;
2030007581c0SJonathan Lemon 
2031df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
20328501a69cSRobert Watson 			INP_WUNLOCK(inp);
2033cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2034cfe8b629SGarrett Wollman 			    sizeof optval);
2035cfe8b629SGarrett Wollman 			if (error)
20361e8f5ffaSRobert Watson 				return (error);
2037df8bae1dSRodney W. Grimes 
20388501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
203953369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2040603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2041cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2042a0292f23SGarrett Wollman 			else
2043a0292f23SGarrett Wollman 				error = EINVAL;
204409fe6320SNavdeep Parhar 			goto unlock_and_done;
2045a0292f23SGarrett Wollman 
2046b8af5dfaSRobert Watson 		case TCP_INFO:
20478501a69cSRobert Watson 			INP_WUNLOCK(inp);
2048b8af5dfaSRobert Watson 			error = EINVAL;
2049b8af5dfaSRobert Watson 			break;
2050b8af5dfaSRobert Watson 
2051adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2052adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2053adc56f5aSEdward Tomasz Napierala #ifdef STATS
2054adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2055adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2056adc56f5aSEdward Tomasz Napierala 			if (error)
2057adc56f5aSEdward Tomasz Napierala 				return (error);
2058adc56f5aSEdward Tomasz Napierala 
2059adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2060adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2061adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2062adc56f5aSEdward Tomasz Napierala 			else
2063adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2064adc56f5aSEdward Tomasz Napierala 
2065adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2066adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2067adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2068adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2069adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2070adc56f5aSEdward Tomasz Napierala 				sbp = t;
2071adc56f5aSEdward Tomasz Napierala 			}
2072adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2073adc56f5aSEdward Tomasz Napierala 
2074adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2075adc56f5aSEdward Tomasz Napierala #else
2076adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2077adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2078adc56f5aSEdward Tomasz Napierala 			break;
2079adc56f5aSEdward Tomasz Napierala 
2080dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2081dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2082af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2083af6fef3aSGleb Smirnoff 			if (error)
2084dbc42409SLawrence Stewart 				break;
2085af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
2086af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
208773e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
208873e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
208973e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
209073e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
209173e263b1SGleb Smirnoff 					break;
209273e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
209373e263b1SGleb Smirnoff 			if (algo == NULL) {
2094af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
209573e263b1SGleb Smirnoff 				error = EINVAL;
209673e263b1SGleb Smirnoff 				break;
209773e263b1SGleb Smirnoff 			}
2098dbc42409SLawrence Stewart 			/*
209973e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
210073e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
2101dbc42409SLawrence Stewart 			 */
2102dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
2103dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
210422699887SMatt Macy 			CC_DATA(tp) = NULL;
2105dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
2106dbc42409SLawrence Stewart 			/*
210773e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
210873e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
210973e263b1SGleb Smirnoff 			 * require initialisation).
2110dbc42409SLawrence Stewart 			 */
211173e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
211273e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
2113dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
2114dbc42409SLawrence Stewart 				/*
211573e263b1SGleb Smirnoff 				 * The only reason init should fail is
2116dbc42409SLawrence Stewart 				 * because of malloc.
2117dbc42409SLawrence Stewart 				 */
2118dbc42409SLawrence Stewart 				error = ENOMEM;
2119dbc42409SLawrence Stewart 			}
212073e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
212173e263b1SGleb Smirnoff 			break;
2122dbc42409SLawrence Stewart 
2123b2e60773SJohn Baldwin #ifdef KERN_TLS
2124b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2125b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2126ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2127b2e60773SJohn Baldwin 			if (error)
2128b2e60773SJohn Baldwin 				break;
2129b2e60773SJohn Baldwin 			error = ktls_enable_tx(so, &tls);
2130b2e60773SJohn Baldwin 			break;
2131b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2132b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2133b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2134b2e60773SJohn Baldwin 			if (error)
2135b2e60773SJohn Baldwin 				return (error);
2136b2e60773SJohn Baldwin 
2137b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2138b2e60773SJohn Baldwin 			error = ktls_set_tx_mode(so, ui);
2139b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2140b2e60773SJohn Baldwin 			break;
2141f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2142f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2143f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2144f1f93475SJohn Baldwin 			    sizeof(tls));
2145f1f93475SJohn Baldwin 			if (error)
2146f1f93475SJohn Baldwin 				break;
2147f1f93475SJohn Baldwin 			error = ktls_enable_rx(so, &tls);
2148f1f93475SJohn Baldwin 			break;
2149b2e60773SJohn Baldwin #endif
2150b2e60773SJohn Baldwin 
21519077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
21529077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
21539077f387SGleb Smirnoff 		case TCP_KEEPINIT:
21549077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
21559077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
21569077f387SGleb Smirnoff 			if (error)
21579077f387SGleb Smirnoff 				return (error);
21589077f387SGleb Smirnoff 
21599077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
21609077f387SGleb Smirnoff 				error = EINVAL;
21619077f387SGleb Smirnoff 				break;
21629077f387SGleb Smirnoff 			}
21639077f387SGleb Smirnoff 			ui *= hz;
21649077f387SGleb Smirnoff 
21659077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
21669077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
21679077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
21689077f387SGleb Smirnoff 				tp->t_keepidle = ui;
21699077f387SGleb Smirnoff 				/*
21709077f387SGleb Smirnoff 				 * XXX: better check current remaining
21719077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
21729077f387SGleb Smirnoff 				 */
21739077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
21749077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
21759077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
21769077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
21779077f387SGleb Smirnoff 				break;
21789077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
21799077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
21809077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
21819077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
21829077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
21839077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
21849077f387SGleb Smirnoff 				break;
21859077f387SGleb Smirnoff 			case TCP_KEEPINIT:
21869077f387SGleb Smirnoff 				tp->t_keepinit = ui;
21879077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
21889077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
21899077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
21909077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
21919077f387SGleb Smirnoff 				break;
21929077f387SGleb Smirnoff 			}
219309fe6320SNavdeep Parhar 			goto unlock_and_done;
21949077f387SGleb Smirnoff 
219585c05144SGleb Smirnoff 		case TCP_KEEPCNT:
219685c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
219785c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
219885c05144SGleb Smirnoff 			if (error)
219985c05144SGleb Smirnoff 				return (error);
220085c05144SGleb Smirnoff 
220185c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
220285c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
220385c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
220485c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
220585c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
220685c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
220785c05144SGleb Smirnoff 			goto unlock_and_done;
220885c05144SGleb Smirnoff 
220986a996e6SHiren Panchasara #ifdef TCPPCAP
221086a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
221186a996e6SHiren Panchasara 		case TCP_PCAP_IN:
221286a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
221386a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
221486a996e6SHiren Panchasara 			    sizeof optval);
221586a996e6SHiren Panchasara 			if (error)
221686a996e6SHiren Panchasara 				return (error);
221786a996e6SHiren Panchasara 
221886a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
221986a996e6SHiren Panchasara 			if (optval >= 0)
222086a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
222186a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
222286a996e6SHiren Panchasara 					optval);
222386a996e6SHiren Panchasara 			else
222486a996e6SHiren Panchasara 				error = EINVAL;
222586a996e6SHiren Panchasara 			goto unlock_and_done;
222686a996e6SHiren Panchasara #endif
222786a996e6SHiren Panchasara 
2228c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2229c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2230c560df6fSPatrick Kelsey 
2231281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2232c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2233c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2234281a0fd4SPatrick Kelsey 				return (EPERM);
2235281a0fd4SPatrick Kelsey 
2236c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2237c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2238281a0fd4SPatrick Kelsey 			if (error)
2239281a0fd4SPatrick Kelsey 				return (error);
2240281a0fd4SPatrick Kelsey 
2241281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2242*d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2243*d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2244*d442a657SMichael Tuexen 				error = EINVAL;
2245*d442a657SMichael Tuexen 				goto unlock_and_done;
2246*d442a657SMichael Tuexen 			}
2247c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2248c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2249c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2250c560df6fSPatrick Kelsey 						error = EPERM;
2251c560df6fSPatrick Kelsey 						goto unlock_and_done;
2252c560df6fSPatrick Kelsey 					}
2253c560df6fSPatrick Kelsey 
2254c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2255281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2256281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2257c560df6fSPatrick Kelsey 				} else {
2258c560df6fSPatrick Kelsey 					/*
2259c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2260c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2261c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2262c560df6fSPatrick Kelsey 					 * connect.
2263c560df6fSPatrick Kelsey 					 */
2264c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2265c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2266c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2267c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2268c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2269c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2270c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2271c560df6fSPatrick Kelsey 					}
2272c560df6fSPatrick Kelsey 				}
2273*d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2274281a0fd4SPatrick Kelsey 			} else
2275281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2276281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2277c560df6fSPatrick Kelsey 		}
2278281a0fd4SPatrick Kelsey 
2279e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
22802529f56eSJonathan T. Looney 		case TCP_LOG:
22812529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22822529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
22832529f56eSJonathan T. Looney 			    sizeof optval);
22842529f56eSJonathan T. Looney 			if (error)
22852529f56eSJonathan T. Looney 				return (error);
22862529f56eSJonathan T. Looney 
22872529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
22882529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
22892529f56eSJonathan T. Looney 			goto unlock_and_done;
22902529f56eSJonathan T. Looney 
22912529f56eSJonathan T. Looney 		case TCP_LOGBUF:
22922529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22932529f56eSJonathan T. Looney 			error = EINVAL;
22942529f56eSJonathan T. Looney 			break;
22952529f56eSJonathan T. Looney 
22962529f56eSJonathan T. Looney 		case TCP_LOGID:
22972529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
22982529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
22992529f56eSJonathan T. Looney 			if (error)
23002529f56eSJonathan T. Looney 				break;
23012529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23022529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23032529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
23042529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
23052529f56eSJonathan T. Looney 			break;
23062529f56eSJonathan T. Looney 
23072529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
23082529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
23092529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23102529f56eSJonathan T. Looney 			error =
23112529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
23122529f56eSJonathan T. Looney 			if (error)
23132529f56eSJonathan T. Looney 				break;
23142529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23152529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23162529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
23172529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
23182529f56eSJonathan T. Looney 				    M_WAITOK, true);
23192529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
23202529f56eSJonathan T. Looney 			} else {
23212529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
23222529f56eSJonathan T. Looney 				/*
23232529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
23242529f56eSJonathan T. Looney 				 * INP lock.
23252529f56eSJonathan T. Looney 				 */
23262529f56eSJonathan T. Looney 			}
23272529f56eSJonathan T. Looney 			break;
2328e24e5683SJonathan T. Looney #endif
23292529f56eSJonathan T. Looney 
2330df8bae1dSRodney W. Grimes 		default:
23318501a69cSRobert Watson 			INP_WUNLOCK(inp);
2332df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2333df8bae1dSRodney W. Grimes 			break;
2334df8bae1dSRodney W. Grimes 		}
2335df8bae1dSRodney W. Grimes 		break;
2336df8bae1dSRodney W. Grimes 
2337cfe8b629SGarrett Wollman 	case SOPT_GET:
23381e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2339cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2340fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
234188f6b043SBruce M Simpson 		case TCP_MD5SIG:
2342fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
23438501a69cSRobert Watson 				INP_WUNLOCK(inp);
2344fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2345fcf59617SAndrey V. Elsukov 			}
2346fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
23471cfd4b53SBruce M Simpson 			break;
2348265ed012SBruce M Simpson #endif
23491e8f5ffaSRobert Watson 
2350df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2351cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
23528501a69cSRobert Watson 			INP_WUNLOCK(inp);
2353b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2354df8bae1dSRodney W. Grimes 			break;
2355df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2356cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
23578501a69cSRobert Watson 			INP_WUNLOCK(inp);
2358b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2359df8bae1dSRodney W. Grimes 			break;
2360a0292f23SGarrett Wollman 		case TCP_NOOPT:
2361cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
23628501a69cSRobert Watson 			INP_WUNLOCK(inp);
2363b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2364a0292f23SGarrett Wollman 			break;
2365a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2366cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
23678501a69cSRobert Watson 			INP_WUNLOCK(inp);
2368b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2369b8af5dfaSRobert Watson 			break;
2370b8af5dfaSRobert Watson 		case TCP_INFO:
2371b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
23728501a69cSRobert Watson 			INP_WUNLOCK(inp);
2373b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2374a0292f23SGarrett Wollman 			break;
2375adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2376adc56f5aSEdward Tomasz Napierala 			{
2377adc56f5aSEdward Tomasz Napierala #ifdef STATS
2378adc56f5aSEdward Tomasz Napierala 			int nheld;
2379adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2380adc56f5aSEdward Tomasz Napierala 
2381adc56f5aSEdward Tomasz Napierala 			error = 0;
2382adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2383adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2384adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2385adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2386adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2387adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2388adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2389adc56f5aSEdward Tomasz Napierala 			else
2390adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2391adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2392adc56f5aSEdward Tomasz Napierala 			if (error)
2393adc56f5aSEdward Tomasz Napierala 				break;
2394adc56f5aSEdward Tomasz Napierala 
2395adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2396adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2397adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2398adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2399adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2400adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2401adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2402adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2403adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2404adc56f5aSEdward Tomasz Napierala 				break;
2405adc56f5aSEdward Tomasz Napierala 			}
2406adc56f5aSEdward Tomasz Napierala 
2407adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2408adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2409adc56f5aSEdward Tomasz Napierala 				goto unhold;
2410adc56f5aSEdward Tomasz Napierala 
2411adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2412adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2413adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2414adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2415adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2416adc56f5aSEdward Tomasz Napierala unhold:
2417adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2418adc56f5aSEdward Tomasz Napierala #else
2419adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2420adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2421adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2422adc56f5aSEdward Tomasz Napierala 			break;
2423adc56f5aSEdward Tomasz Napierala 			}
2424dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2425af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2426dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2427af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2428dbc42409SLawrence Stewart 			break;
24292f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
24302f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
24312f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
24322f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
24332f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
24342f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
24355a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
24362f3eb7f4SGleb Smirnoff 				break;
24372f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
24385a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
24392f3eb7f4SGleb Smirnoff 				break;
24402f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
24415a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
24422f3eb7f4SGleb Smirnoff 				break;
24432f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
24445a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
24452f3eb7f4SGleb Smirnoff 				break;
24462f3eb7f4SGleb Smirnoff 			}
24472f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
24482f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
24492f3eb7f4SGleb Smirnoff 			break;
245086a996e6SHiren Panchasara #ifdef TCPPCAP
245186a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
245286a996e6SHiren Panchasara 		case TCP_PCAP_IN:
245386a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
245486a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
245586a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
245686a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
245786a996e6SHiren Panchasara 			break;
245886a996e6SHiren Panchasara #endif
2459281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2460281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2461281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2462281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2463281a0fd4SPatrick Kelsey 			break;
2464e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24652529f56eSJonathan T. Looney 		case TCP_LOG:
24662529f56eSJonathan T. Looney 			optval = tp->t_logstate;
24672529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24682529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
24692529f56eSJonathan T. Looney 			break;
24702529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24712529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
24722529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
24732529f56eSJonathan T. Looney 			break;
24742529f56eSJonathan T. Looney 		case TCP_LOGID:
24752529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
24762529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24772529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
24782529f56eSJonathan T. Looney 			break;
24792529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24802529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24812529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24822529f56eSJonathan T. Looney 			error = EINVAL;
24832529f56eSJonathan T. Looney 			break;
2484e24e5683SJonathan T. Looney #endif
2485b2e60773SJohn Baldwin #ifdef KERN_TLS
2486b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2487b2e60773SJohn Baldwin 			optval = ktls_get_tx_mode(so);
2488b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2489b2e60773SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2490b2e60773SJohn Baldwin 			break;
2491f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2492f1f93475SJohn Baldwin 			optval = ktls_get_rx_mode(so);
2493f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2494f1f93475SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2495f1f93475SJohn Baldwin 			break;
2496b2e60773SJohn Baldwin #endif
2497df8bae1dSRodney W. Grimes 		default:
24988501a69cSRobert Watson 			INP_WUNLOCK(inp);
2499df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2500df8bae1dSRodney W. Grimes 			break;
2501df8bae1dSRodney W. Grimes 		}
2502df8bae1dSRodney W. Grimes 		break;
2503df8bae1dSRodney W. Grimes 	}
2504df8bae1dSRodney W. Grimes 	return (error);
2505df8bae1dSRodney W. Grimes }
25068501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2507bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2508df8bae1dSRodney W. Grimes 
250926e30fbbSDavid Greenman /*
2510df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2511df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2512df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2513df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2514df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2515df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2516df8bae1dSRodney W. Grimes  */
2517623dce13SRobert Watson static void
2518ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2519df8bae1dSRodney W. Grimes {
2520e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2521e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2522e6e0b5ffSRobert Watson 
252397a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
25248501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2525df8bae1dSRodney W. Grimes 
2526623dce13SRobert Watson 	/*
2527623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2528623dce13SRobert Watson 	 * socket is still open.
2529623dce13SRobert Watson 	 */
25308db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
25318db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2532df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2533623dce13SRobert Watson 		KASSERT(tp != NULL,
2534623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2535623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2536243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2537623dce13SRobert Watson 		KASSERT(tp != NULL,
2538623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2539623dce13SRobert Watson 	} else {
2540df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2541df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2542623dce13SRobert Watson 		tcp_usrclosed(tp);
2543ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
254455bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2545df8bae1dSRodney W. Grimes 	}
2546df8bae1dSRodney W. Grimes }
2547df8bae1dSRodney W. Grimes 
2548df8bae1dSRodney W. Grimes /*
2549df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2550df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2551df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2552df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2553df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2554df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2555df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2556df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2557df8bae1dSRodney W. Grimes  */
2558623dce13SRobert Watson static void
2559ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2560df8bae1dSRodney W. Grimes {
2561df8bae1dSRodney W. Grimes 
256297a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
25638501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2564e6e0b5ffSRobert Watson 
2565df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2566df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
256709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
256809fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
256909fe6320SNavdeep Parhar #endif
2570550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2571bc65987aSKip Macy 		/* FALLTHROUGH */
2572bc65987aSKip Macy 	case TCPS_CLOSED:
2573df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2574623dce13SRobert Watson 		/*
2575623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2576623dce13SRobert Watson 		 * still open.
2577623dce13SRobert Watson 		 */
2578623dce13SRobert Watson 		KASSERT(tp != NULL,
2579623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2580df8bae1dSRodney W. Grimes 		break;
2581df8bae1dSRodney W. Grimes 
2582a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2583df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2584a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2585a0292f23SGarrett Wollman 		break;
2586a0292f23SGarrett Wollman 
2587df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
258857f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2589df8bae1dSRodney W. Grimes 		break;
2590df8bae1dSRodney W. Grimes 
2591df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
259257f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2593df8bae1dSRodney W. Grimes 		break;
2594df8bae1dSRodney W. Grimes 	}
2595abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2596df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2597abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
25987c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
25997c72af87SMohan Srinivasan 			int timeout;
26007c72af87SMohan Srinivasan 
26017c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
26029077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2603b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2604b6239c4aSAndras Olah 		}
2605df8bae1dSRodney W. Grimes 	}
26067c72af87SMohan Srinivasan }
2607497057eeSRobert Watson 
2608497057eeSRobert Watson #ifdef DDB
2609497057eeSRobert Watson static void
2610497057eeSRobert Watson db_print_indent(int indent)
2611497057eeSRobert Watson {
2612497057eeSRobert Watson 	int i;
2613497057eeSRobert Watson 
2614497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2615497057eeSRobert Watson 		db_printf(" ");
2616497057eeSRobert Watson }
2617497057eeSRobert Watson 
2618497057eeSRobert Watson static void
2619497057eeSRobert Watson db_print_tstate(int t_state)
2620497057eeSRobert Watson {
2621497057eeSRobert Watson 
2622497057eeSRobert Watson 	switch (t_state) {
2623497057eeSRobert Watson 	case TCPS_CLOSED:
2624497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2625497057eeSRobert Watson 		return;
2626497057eeSRobert Watson 
2627497057eeSRobert Watson 	case TCPS_LISTEN:
2628497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2629497057eeSRobert Watson 		return;
2630497057eeSRobert Watson 
2631497057eeSRobert Watson 	case TCPS_SYN_SENT:
2632497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2633497057eeSRobert Watson 		return;
2634497057eeSRobert Watson 
2635497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2636497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2637497057eeSRobert Watson 		return;
2638497057eeSRobert Watson 
2639497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2640497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2641497057eeSRobert Watson 		return;
2642497057eeSRobert Watson 
2643497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2644497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2645497057eeSRobert Watson 		return;
2646497057eeSRobert Watson 
2647497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2648497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2649497057eeSRobert Watson 		return;
2650497057eeSRobert Watson 
2651497057eeSRobert Watson 	case TCPS_CLOSING:
2652497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2653497057eeSRobert Watson 		return;
2654497057eeSRobert Watson 
2655497057eeSRobert Watson 	case TCPS_LAST_ACK:
2656497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2657497057eeSRobert Watson 		return;
2658497057eeSRobert Watson 
2659497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2660497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2661497057eeSRobert Watson 		return;
2662497057eeSRobert Watson 
2663497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2664497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2665497057eeSRobert Watson 		return;
2666497057eeSRobert Watson 
2667497057eeSRobert Watson 	default:
2668497057eeSRobert Watson 		db_printf("unknown");
2669497057eeSRobert Watson 		return;
2670497057eeSRobert Watson 	}
2671497057eeSRobert Watson }
2672497057eeSRobert Watson 
2673497057eeSRobert Watson static void
2674497057eeSRobert Watson db_print_tflags(u_int t_flags)
2675497057eeSRobert Watson {
2676497057eeSRobert Watson 	int comma;
2677497057eeSRobert Watson 
2678497057eeSRobert Watson 	comma = 0;
2679497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2680497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2681497057eeSRobert Watson 		comma = 1;
2682497057eeSRobert Watson 	}
2683497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2684497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2685497057eeSRobert Watson 		comma = 1;
2686497057eeSRobert Watson 	}
2687497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2688497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2689497057eeSRobert Watson 		comma = 1;
2690497057eeSRobert Watson 	}
2691497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2692497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2693497057eeSRobert Watson 		comma = 1;
2694497057eeSRobert Watson 	}
2695497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2696497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2697497057eeSRobert Watson 		comma = 1;
2698497057eeSRobert Watson 	}
2699497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2700497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2701497057eeSRobert Watson 		comma = 1;
2702497057eeSRobert Watson 	}
2703497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2704497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2705497057eeSRobert Watson 		comma = 1;
2706497057eeSRobert Watson 	}
2707497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2708497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2709497057eeSRobert Watson 		comma = 1;
2710497057eeSRobert Watson 	}
2711497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2712497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2713497057eeSRobert Watson 		comma = 1;
2714497057eeSRobert Watson 	}
2715497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2716497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2717497057eeSRobert Watson 		comma = 1;
2718497057eeSRobert Watson 	}
2719497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2720497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2721497057eeSRobert Watson 		comma = 1;
2722497057eeSRobert Watson 	}
2723497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2724497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2725497057eeSRobert Watson 		comma = 1;
2726497057eeSRobert Watson 	}
2727497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2728497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2729497057eeSRobert Watson 		comma = 1;
2730497057eeSRobert Watson 	}
2731497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2732497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2733497057eeSRobert Watson 		comma = 1;
2734497057eeSRobert Watson 	}
2735497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2736497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2737497057eeSRobert Watson 		comma = 1;
2738497057eeSRobert Watson 	}
2739497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2740497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2741497057eeSRobert Watson 		comma = 1;
2742497057eeSRobert Watson 	}
2743497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2744497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2745497057eeSRobert Watson 		comma = 1;
2746497057eeSRobert Watson 	}
2747497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2748497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2749497057eeSRobert Watson 		comma = 1;
2750497057eeSRobert Watson 	}
2751dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2752dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2753dbc42409SLawrence Stewart 		comma = 1;
2754dbc42409SLawrence Stewart 	}
2755497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2756497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2757497057eeSRobert Watson 		comma = 1;
2758497057eeSRobert Watson 	}
2759497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2760497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2761497057eeSRobert Watson 		comma = 1;
2762497057eeSRobert Watson 	}
2763497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2764497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2765497057eeSRobert Watson 		comma = 1;
2766497057eeSRobert Watson 	}
2767497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2768497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2769497057eeSRobert Watson 		comma = 1;
2770497057eeSRobert Watson 	}
2771281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2772281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2773281a0fd4SPatrick Kelsey 		comma = 1;
2774281a0fd4SPatrick Kelsey 	}
2775497057eeSRobert Watson }
2776497057eeSRobert Watson 
2777497057eeSRobert Watson static void
27783cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
27793cf38784SMichael Tuexen {
27803cf38784SMichael Tuexen 	int comma;
27813cf38784SMichael Tuexen 
27823cf38784SMichael Tuexen 	comma = 0;
27833cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
27843cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
27853cf38784SMichael Tuexen 		comma = 1;
27863cf38784SMichael Tuexen 	}
27873cf38784SMichael Tuexen }
27883cf38784SMichael Tuexen 
27893cf38784SMichael Tuexen 
27903cf38784SMichael Tuexen static void
2791497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2792497057eeSRobert Watson {
2793497057eeSRobert Watson 	int comma;
2794497057eeSRobert Watson 
2795497057eeSRobert Watson 	comma = 0;
2796497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2797497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2798497057eeSRobert Watson 		comma = 1;
2799497057eeSRobert Watson 	}
2800497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2801497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2802497057eeSRobert Watson 		comma = 1;
2803497057eeSRobert Watson 	}
2804497057eeSRobert Watson }
2805497057eeSRobert Watson 
2806497057eeSRobert Watson static void
2807497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2808497057eeSRobert Watson {
2809497057eeSRobert Watson 
2810497057eeSRobert Watson 	db_print_indent(indent);
2811497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2812497057eeSRobert Watson 
2813497057eeSRobert Watson 	indent += 2;
2814497057eeSRobert Watson 
2815497057eeSRobert Watson 	db_print_indent(indent);
2816497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2817c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2818497057eeSRobert Watson 
2819497057eeSRobert Watson 	db_print_indent(indent);
282085d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2821e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2822497057eeSRobert Watson 
2823497057eeSRobert Watson 	db_print_indent(indent);
2824e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2825e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2826497057eeSRobert Watson 
2827497057eeSRobert Watson 	db_print_indent(indent);
2828497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2829497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2830497057eeSRobert Watson 	db_printf(")\n");
2831497057eeSRobert Watson 
2832497057eeSRobert Watson 	db_print_indent(indent);
2833497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2834497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2835497057eeSRobert Watson 	db_printf(")\n");
2836497057eeSRobert Watson 
2837497057eeSRobert Watson 	db_print_indent(indent);
28383cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
28393cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
28403cf38784SMichael Tuexen 	db_printf(")\n");
28413cf38784SMichael Tuexen 
28423cf38784SMichael Tuexen 	db_print_indent(indent);
2843497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2844497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2845497057eeSRobert Watson 
2846497057eeSRobert Watson 	db_print_indent(indent);
2847497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2848497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2849497057eeSRobert Watson 
2850497057eeSRobert Watson 	db_print_indent(indent);
2851497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2852497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2853497057eeSRobert Watson 
2854497057eeSRobert Watson 	db_print_indent(indent);
28553ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2856497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2857497057eeSRobert Watson 
2858497057eeSRobert Watson 	db_print_indent(indent);
28593ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
28601c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2861497057eeSRobert Watson 
2862497057eeSRobert Watson 	db_print_indent(indent);
28633ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
28641c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2865497057eeSRobert Watson 
2866497057eeSRobert Watson 	db_print_indent(indent);
28670c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
28680c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2869497057eeSRobert Watson 
2870497057eeSRobert Watson 	db_print_indent(indent);
28711c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
28721c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2873497057eeSRobert Watson 
2874497057eeSRobert Watson 	db_print_indent(indent);
28751c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
28761c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2877497057eeSRobert Watson 
2878497057eeSRobert Watson 	db_print_indent(indent);
2879497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2880497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2881497057eeSRobert Watson 	    tp->t_rttbest);
2882497057eeSRobert Watson 
2883497057eeSRobert Watson 	db_print_indent(indent);
28843ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2885497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2886497057eeSRobert Watson 
2887497057eeSRobert Watson 	db_print_indent(indent);
2888497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2889497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2890497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2891497057eeSRobert Watson 
2892497057eeSRobert Watson 	db_print_indent(indent);
2893497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2894497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2895497057eeSRobert Watson 
2896497057eeSRobert Watson 	db_print_indent(indent);
28979f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
28981a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2899497057eeSRobert Watson 
2900497057eeSRobert Watson 	db_print_indent(indent);
2901497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
29023ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2903497057eeSRobert Watson 
2904497057eeSRobert Watson 	db_print_indent(indent);
29053ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
29069f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2907497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2908497057eeSRobert Watson 
2909497057eeSRobert Watson 	db_print_indent(indent);
29103529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
29113529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2912497057eeSRobert Watson 
2913497057eeSRobert Watson 	db_print_indent(indent);
2914a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
2915a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
2916497057eeSRobert Watson 
2917497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2918497057eeSRobert Watson 
2919497057eeSRobert Watson 	db_print_indent(indent);
2920497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2921497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2922497057eeSRobert Watson }
2923497057eeSRobert Watson 
2924497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2925497057eeSRobert Watson {
2926497057eeSRobert Watson 	struct tcpcb *tp;
2927497057eeSRobert Watson 
2928497057eeSRobert Watson 	if (!have_addr) {
2929497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2930497057eeSRobert Watson 		return;
2931497057eeSRobert Watson 	}
2932497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2933497057eeSRobert Watson 
2934497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2935497057eeSRobert Watson }
2936497057eeSRobert Watson #endif
2937