xref: /freebsd/sys/netinet/tcp_usrreq.c (revision e854dd38acaf041dc1ea57f36d31ae749552b941)
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);
5552cf21ae5SRandall Stewart 	if ((sinp->sin_family == AF_INET) &&
5562cf21ae5SRandall Stewart 	    ((ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) ||
5572cf21ae5SRandall Stewart 	     (sinp->sin_addr.s_addr == INADDR_ANY)))
5582cf21ae5SRandall Stewart 		return(EAFNOSUPPORT);
559b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
560b89e82ddSJamie Gritton 		return (error);
56175c13541SPoul-Henning Kamp 
562623dce13SRobert Watson 	TCPDEBUG0;
563623dce13SRobert Watson 	inp = sotoinpcb(so);
564623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5658501a69cSRobert Watson 	INP_WLOCK(inp);
566eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
567eb96dc33SJulien Charbon 		error = EADDRINUSE;
568eb96dc33SJulien Charbon 		goto out;
569eb96dc33SJulien Charbon 	}
570eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
571eb96dc33SJulien Charbon 		error = ECONNREFUSED;
572623dce13SRobert Watson 		goto out;
573623dce13SRobert Watson 	}
574623dce13SRobert Watson 	tp = intotcpcb(inp);
575623dce13SRobert Watson 	TCPDEBUG1();
576c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
577b40ce416SJulian Elischer 	if ((error = tcp_connect(tp, nam, td)) != 0)
578c1604fe4SGleb Smirnoff 		goto out_in_epoch;
57909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
58009fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
58137cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
58209fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
583c1604fe4SGleb Smirnoff 		goto out_in_epoch;
58409fe6320SNavdeep Parhar #endif
58509fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
58655bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
587c1604fe4SGleb Smirnoff out_in_epoch:
588109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
589623dce13SRobert Watson out:
590623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
591e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5928501a69cSRobert Watson 	INP_WUNLOCK(inp);
593623dce13SRobert Watson 	return (error);
5942c37256eSGarrett Wollman }
595b287c6c7SBjoern A. Zeeb #endif /* INET */
5962c37256eSGarrett Wollman 
597fb59c426SYoshinobu Inoue #ifdef INET6
598fb59c426SYoshinobu Inoue static int
599b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
600fb59c426SYoshinobu Inoue {
601109eb549SGleb Smirnoff 	struct epoch_tracker et;
602fb59c426SYoshinobu Inoue 	int error = 0;
603f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
604623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6050ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
6064a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
6074a91aa8fSMichael Tuexen 	u_char vflagsav;
608623dce13SRobert Watson 
609623dce13SRobert Watson 	TCPDEBUG0;
610fb59c426SYoshinobu Inoue 
6110ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
6120ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
613e29ef13fSDon Lewis 		return (EINVAL);
61452710de1SPawel Jakub Dawidek 	/*
61552710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
61652710de1SPawel Jakub Dawidek 	 */
6170ecd976eSBjoern A. Zeeb 	if (sin6->sin6_family == AF_INET6
6180ecd976eSBjoern A. Zeeb 	    && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
61952710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
620fb59c426SYoshinobu Inoue 
621623dce13SRobert Watson 	inp = sotoinpcb(so);
622623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
6238501a69cSRobert Watson 	INP_WLOCK(inp);
6244a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
6254a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
626eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT) {
627eb96dc33SJulien Charbon 		error = EADDRINUSE;
628eb96dc33SJulien Charbon 		goto out;
629eb96dc33SJulien Charbon 	}
630eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
631eb96dc33SJulien Charbon 		error = ECONNREFUSED;
632623dce13SRobert Watson 		goto out;
633623dce13SRobert Watson 	}
634623dce13SRobert Watson 	tp = intotcpcb(inp);
635623dce13SRobert Watson 	TCPDEBUG1();
636b287c6c7SBjoern A. Zeeb #ifdef INET
637fa046d87SRobert Watson 	/*
638fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
639fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
640fa046d87SRobert Watson 	 * Is this a significant problem?
641fa046d87SRobert Watson 	 */
6420ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
643fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
644fb59c426SYoshinobu Inoue 
645d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
646d46a5312SMaxim Konovalov 			error = EINVAL;
647d46a5312SMaxim Konovalov 			goto out;
648d46a5312SMaxim Konovalov 		}
6495dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6505dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6515dba6adaSMichael Tuexen 			goto out;
6525dba6adaSMichael Tuexen 		}
65333841545SHajimu UMEMOTO 
6540ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
655888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
656888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
657888973f5SMichael Tuexen 			goto out;
658888973f5SMichael Tuexen 		}
6592cf21ae5SRandall Stewart 		if ((ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) ||
6602cf21ae5SRandall Stewart 		    (sin.sin_addr.s_addr == INADDR_ANY)) {
6612cf21ae5SRandall Stewart 			error = EAFNOSUPPORT;
6622cf21ae5SRandall Stewart 			goto out;
6632cf21ae5SRandall Stewart 		}
664b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
665b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
666413628a7SBjoern A. Zeeb 			goto out;
6674a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6684a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
669c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
670b40ce416SJulian Elischer 		if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
671c1604fe4SGleb Smirnoff 			goto out_in_epoch;
67209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
67309fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
674adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
67509fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
676c1604fe4SGleb Smirnoff 			goto out_in_epoch;
67709fe6320SNavdeep Parhar #endif
67855bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
679c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6805dba6adaSMichael Tuexen 	} else {
6815dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6825dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6835dba6adaSMichael Tuexen 			goto out;
6845dba6adaSMichael Tuexen 		}
685fb59c426SYoshinobu Inoue 	}
686b287c6c7SBjoern A. Zeeb #endif
6874a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6884a91aa8fSMichael Tuexen 		goto out;
689fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
690fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
691dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
692b40ce416SJulian Elischer 	if ((error = tcp6_connect(tp, nam, td)) != 0)
693fb59c426SYoshinobu Inoue 		goto out;
69409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
69509fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
696adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
69709fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
69809fe6320SNavdeep Parhar 		goto out;
69909fe6320SNavdeep Parhar #endif
70009fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
701109eb549SGleb Smirnoff 	NET_EPOCH_ENTER(et);
70255bceb1eSRandall Stewart 	error = tp->t_fb->tfb_tcp_output(tp);
7037754e281SBjoern A. Zeeb #ifdef INET
704c1604fe4SGleb Smirnoff out_in_epoch:
7057754e281SBjoern A. Zeeb #endif
706109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
707623dce13SRobert Watson out:
7084a91aa8fSMichael Tuexen 	/*
7094a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
7104a91aa8fSMichael Tuexen 	 * the flags we modified.
7114a91aa8fSMichael Tuexen 	 */
7124a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
7134a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
7144a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
7154a91aa8fSMichael Tuexen 	}
7164a91aa8fSMichael Tuexen 
717623dce13SRobert Watson 	TCPDEBUG2(PRU_CONNECT);
7185d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
7198501a69cSRobert Watson 	INP_WUNLOCK(inp);
720623dce13SRobert Watson 	return (error);
721fb59c426SYoshinobu Inoue }
722fb59c426SYoshinobu Inoue #endif /* INET6 */
723fb59c426SYoshinobu Inoue 
7242c37256eSGarrett Wollman /*
7252c37256eSGarrett Wollman  * Initiate disconnect from peer.
7262c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
7272c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
7282c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
7292c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
7302c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
7312c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
7322c37256eSGarrett Wollman  *
7332c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
7342c37256eSGarrett Wollman  */
7352c37256eSGarrett Wollman static int
7362c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7372c37256eSGarrett Wollman {
738f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
739623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7406573d758SMatt Macy 	struct epoch_tracker et;
741623dce13SRobert Watson 	int error = 0;
7422c37256eSGarrett Wollman 
743623dce13SRobert Watson 	TCPDEBUG0;
74497a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
745623dce13SRobert Watson 	inp = sotoinpcb(so);
746623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7478501a69cSRobert Watson 	INP_WLOCK(inp);
748489dcc92SJulien Charbon 	if (inp->inp_flags & INP_TIMEWAIT)
749489dcc92SJulien Charbon 		goto out;
750489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
75121367f63SSam Leffler 		error = ECONNRESET;
752623dce13SRobert Watson 		goto out;
753623dce13SRobert Watson 	}
754623dce13SRobert Watson 	tp = intotcpcb(inp);
755623dce13SRobert Watson 	TCPDEBUG1();
756623dce13SRobert Watson 	tcp_disconnect(tp);
757623dce13SRobert Watson out:
758623dce13SRobert Watson 	TCPDEBUG2(PRU_DISCONNECT);
7595d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7608501a69cSRobert Watson 	INP_WUNLOCK(inp);
76197a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
762623dce13SRobert Watson 	return (error);
7632c37256eSGarrett Wollman }
7642c37256eSGarrett Wollman 
765b287c6c7SBjoern A. Zeeb #ifdef INET
7662c37256eSGarrett Wollman /*
7678296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7688296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7692c37256eSGarrett Wollman  */
7702c37256eSGarrett Wollman static int
77157bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7722c37256eSGarrett Wollman {
7732c37256eSGarrett Wollman 	int error = 0;
774f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7751db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
77626ef6ac4SDon Lewis 	struct in_addr addr;
77726ef6ac4SDon Lewis 	in_port_t port = 0;
7781db24ffbSJonathan Lemon 	TCPDEBUG0;
7792c37256eSGarrett Wollman 
7803d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7813d2d3ef4SRobert Watson 		return (ECONNABORTED);
782f76fcf6dSJeffrey Hsu 
783f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
784623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7858501a69cSRobert Watson 	INP_WLOCK(inp);
786ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
7873d2d3ef4SRobert Watson 		error = ECONNABORTED;
788623dce13SRobert Watson 		goto out;
789623dce13SRobert Watson 	}
7901db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
7911db24ffbSJonathan Lemon 	TCPDEBUG1();
792f76fcf6dSJeffrey Hsu 
793f76fcf6dSJeffrey Hsu 	/*
79454d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
79526ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
79626ef6ac4SDon Lewis 	 * release the lock.
797f76fcf6dSJeffrey Hsu 	 */
79826ef6ac4SDon Lewis 	port = inp->inp_fport;
79926ef6ac4SDon Lewis 	addr = inp->inp_faddr;
800f76fcf6dSJeffrey Hsu 
801623dce13SRobert Watson out:
802623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8035d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8048501a69cSRobert Watson 	INP_WUNLOCK(inp);
80526ef6ac4SDon Lewis 	if (error == 0)
80626ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
80726ef6ac4SDon Lewis 	return error;
8082c37256eSGarrett Wollman }
809b287c6c7SBjoern A. Zeeb #endif /* INET */
8102c37256eSGarrett Wollman 
811fb59c426SYoshinobu Inoue #ifdef INET6
812fb59c426SYoshinobu Inoue static int
813fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
814fb59c426SYoshinobu Inoue {
815f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
816fb59c426SYoshinobu Inoue 	int error = 0;
8171db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
81826ef6ac4SDon Lewis 	struct in_addr addr;
81926ef6ac4SDon Lewis 	struct in6_addr addr6;
8206573d758SMatt Macy 	struct epoch_tracker et;
82126ef6ac4SDon Lewis 	in_port_t port = 0;
82226ef6ac4SDon Lewis 	int v4 = 0;
8231db24ffbSJonathan Lemon 	TCPDEBUG0;
824fb59c426SYoshinobu Inoue 
825b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
826b4470c16SRobert Watson 		return (ECONNABORTED);
827f76fcf6dSJeffrey Hsu 
828f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
829623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
83097a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8318501a69cSRobert Watson 	INP_WLOCK(inp);
832ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
83321367f63SSam Leffler 		error = ECONNABORTED;
834623dce13SRobert Watson 		goto out;
835623dce13SRobert Watson 	}
8361db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
8371db24ffbSJonathan Lemon 	TCPDEBUG1();
838623dce13SRobert Watson 
83926ef6ac4SDon Lewis 	/*
84026ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
84126ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
84226ef6ac4SDon Lewis 	 * release the lock.
84326ef6ac4SDon Lewis 	 */
84426ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
84526ef6ac4SDon Lewis 		v4 = 1;
84626ef6ac4SDon Lewis 		port = inp->inp_fport;
84726ef6ac4SDon Lewis 		addr = inp->inp_faddr;
84826ef6ac4SDon Lewis 	} else {
84926ef6ac4SDon Lewis 		port = inp->inp_fport;
85026ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
85126ef6ac4SDon Lewis 	}
85226ef6ac4SDon Lewis 
853623dce13SRobert Watson out:
854623dce13SRobert Watson 	TCPDEBUG2(PRU_ACCEPT);
8555d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8568501a69cSRobert Watson 	INP_WUNLOCK(inp);
85797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
85826ef6ac4SDon Lewis 	if (error == 0) {
85926ef6ac4SDon Lewis 		if (v4)
86026ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
86126ef6ac4SDon Lewis 		else
86226ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
86326ef6ac4SDon Lewis 	}
86426ef6ac4SDon Lewis 	return error;
865fb59c426SYoshinobu Inoue }
866fb59c426SYoshinobu Inoue #endif /* INET6 */
867f76fcf6dSJeffrey Hsu 
868f76fcf6dSJeffrey Hsu /*
8692c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8702c37256eSGarrett Wollman  */
8712c37256eSGarrett Wollman static int
8722c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8732c37256eSGarrett Wollman {
8742c37256eSGarrett Wollman 	int error = 0;
875f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
876623dce13SRobert Watson 	struct tcpcb *tp = NULL;
8776573d758SMatt Macy 	struct epoch_tracker et;
8782c37256eSGarrett Wollman 
879623dce13SRobert Watson 	TCPDEBUG0;
88097a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
881623dce13SRobert Watson 	inp = sotoinpcb(so);
882623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8838501a69cSRobert Watson 	INP_WLOCK(inp);
884ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
88521367f63SSam Leffler 		error = ECONNRESET;
886623dce13SRobert Watson 		goto out;
887623dce13SRobert Watson 	}
888623dce13SRobert Watson 	tp = intotcpcb(inp);
889623dce13SRobert Watson 	TCPDEBUG1();
8902c37256eSGarrett Wollman 	socantsendmore(so);
891623dce13SRobert Watson 	tcp_usrclosed(tp);
892ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
89355bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
894623dce13SRobert Watson 
895623dce13SRobert Watson out:
896623dce13SRobert Watson 	TCPDEBUG2(PRU_SHUTDOWN);
8975d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
8988501a69cSRobert Watson 	INP_WUNLOCK(inp);
89997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
900623dce13SRobert Watson 
901623dce13SRobert Watson 	return (error);
9022c37256eSGarrett Wollman }
9032c37256eSGarrett Wollman 
9042c37256eSGarrett Wollman /*
9052c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
9062c37256eSGarrett Wollman  */
9072c37256eSGarrett Wollman static int
9082c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
9092c37256eSGarrett Wollman {
910109eb549SGleb Smirnoff 	struct epoch_tracker et;
911f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
912623dce13SRobert Watson 	struct tcpcb *tp = NULL;
913623dce13SRobert Watson 	int error = 0;
9142c37256eSGarrett Wollman 
915623dce13SRobert Watson 	TCPDEBUG0;
916623dce13SRobert Watson 	inp = sotoinpcb(so);
917623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
9188501a69cSRobert Watson 	INP_WLOCK(inp);
919ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
92021367f63SSam Leffler 		error = ECONNRESET;
921623dce13SRobert Watson 		goto out;
922623dce13SRobert Watson 	}
923623dce13SRobert Watson 	tp = intotcpcb(inp);
924623dce13SRobert Watson 	TCPDEBUG1();
925281a0fd4SPatrick Kelsey 	/*
926281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
927281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
928281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
929281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
930281a0fd4SPatrick Kelsey 	 * expires.
931281a0fd4SPatrick Kelsey 	 */
93268bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
933281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
934281a0fd4SPatrick Kelsey 		goto out;
93542ce7937SGleb Smirnoff 	NET_EPOCH_ENTER(et);
93609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
93709fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
93809fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
939460cf046SNavdeep Parhar 	else
94009fe6320SNavdeep Parhar #endif
94155bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_output(tp);
942109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
943623dce13SRobert Watson out:
944623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVD);
9455d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
9468501a69cSRobert Watson 	INP_WUNLOCK(inp);
947623dce13SRobert Watson 	return (error);
9482c37256eSGarrett Wollman }
9492c37256eSGarrett Wollman 
9502c37256eSGarrett Wollman /*
9512c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9529c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9539c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9549c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9559c9906e9SPeter Wemm  * generally are caller-frees.
9562c37256eSGarrett Wollman  */
9572c37256eSGarrett Wollman static int
95857bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
959b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9602c37256eSGarrett Wollman {
96197a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9622c37256eSGarrett Wollman 	int error = 0;
963f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
964623dce13SRobert Watson 	struct tcpcb *tp = NULL;
965888973f5SMichael Tuexen #ifdef INET
96651e08d53SMichael Tuexen #ifdef INET6
96751e08d53SMichael Tuexen 	struct sockaddr_in sin;
96851e08d53SMichael Tuexen #endif
96951e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
970888973f5SMichael Tuexen #endif
971fb59c426SYoshinobu Inoue #ifdef INET6
972fb59c426SYoshinobu Inoue 	int isipv6;
973fb59c426SYoshinobu Inoue #endif
9744a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9754a91aa8fSMichael Tuexen 	u_char vflagsav;
9764a91aa8fSMichael Tuexen 	bool restoreflags;
9779c9906e9SPeter Wemm 	TCPDEBUG0;
9782c37256eSGarrett Wollman 
979f76fcf6dSJeffrey Hsu 	/*
98097a95ee1SGleb Smirnoff 	 * We require the pcbinfo "read lock" if we will close the socket
98197a95ee1SGleb Smirnoff 	 * as part of this call.
982f76fcf6dSJeffrey Hsu 	 */
98397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
984f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
985623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9868501a69cSRobert Watson 	INP_WLOCK(inp);
9874a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
9884a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
9894a91aa8fSMichael Tuexen 	restoreflags = false;
990ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
9917ff0b850SAndre Oppermann 		if (control)
9927ff0b850SAndre Oppermann 			m_freem(control);
9932cbcd3c1SGleb Smirnoff 		/*
9942cbcd3c1SGleb Smirnoff 		 * In case of PRUS_NOTREADY, tcp_usr_ready() is responsible
9952cbcd3c1SGleb Smirnoff 		 * for freeing memory.
9962cbcd3c1SGleb Smirnoff 		 */
9972cbcd3c1SGleb Smirnoff 		if (m && (flags & PRUS_NOTREADY) == 0)
9987ff0b850SAndre Oppermann 			m_freem(m);
99921367f63SSam Leffler 		error = ECONNRESET;
10009c9906e9SPeter Wemm 		goto out;
10019c9906e9SPeter Wemm 	}
10029c9906e9SPeter Wemm 	tp = intotcpcb(inp);
1003d3b6c96bSRandall Stewart 	if (flags & PRUS_OOB) {
1004d3b6c96bSRandall Stewart 		if ((error = tcp_pru_options_support(tp, PRUS_OOB)) != 0) {
1005d3b6c96bSRandall Stewart 			if (control)
1006d3b6c96bSRandall Stewart 				m_freem(control);
1007d3b6c96bSRandall Stewart 			if (m && (flags & PRUS_NOTREADY) == 0)
1008d3b6c96bSRandall Stewart 				m_freem(m);
1009d3b6c96bSRandall Stewart 			goto out;
1010d3b6c96bSRandall Stewart 		}
1011d3b6c96bSRandall Stewart 	}
10129c9906e9SPeter Wemm 	TCPDEBUG1();
1013888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
1014888973f5SMichael Tuexen 		switch (nam->sa_family) {
1015888973f5SMichael Tuexen #ifdef INET
1016888973f5SMichael Tuexen 		case AF_INET:
1017888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
1018888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
1019888973f5SMichael Tuexen 				if (m)
1020888973f5SMichael Tuexen 					m_freem(m);
1021888973f5SMichael Tuexen 				error = EINVAL;
1022888973f5SMichael Tuexen 				goto out;
1023888973f5SMichael Tuexen 			}
1024888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
1025888973f5SMichael Tuexen 				if (m)
1026888973f5SMichael Tuexen 					m_freem(m);
1027888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1028888973f5SMichael Tuexen 				goto out;
1029888973f5SMichael Tuexen 			}
1030888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
1031888973f5SMichael Tuexen 				if (m)
1032888973f5SMichael Tuexen 					m_freem(m);
1033888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1034888973f5SMichael Tuexen 				goto out;
1035888973f5SMichael Tuexen 			}
10362cf21ae5SRandall Stewart 			if ((ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) ||
10372cf21ae5SRandall Stewart 			    (sinp->sin_addr.s_addr == INADDR_ANY)) {
10382cf21ae5SRandall Stewart 				if (m)
10392cf21ae5SRandall Stewart 					m_freem(m);
10402cf21ae5SRandall Stewart 				error = EAFNOSUPPORT;
10412cf21ae5SRandall Stewart 				goto out;
10422cf21ae5SRandall Stewart 			}
1043888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
1044888973f5SMichael Tuexen 			    &sinp->sin_addr))) {
1045888973f5SMichael Tuexen 				if (m)
1046888973f5SMichael Tuexen 					m_freem(m);
1047888973f5SMichael Tuexen 				goto out;
1048888973f5SMichael Tuexen 			}
1049888973f5SMichael Tuexen #ifdef INET6
1050888973f5SMichael Tuexen 			isipv6 = 0;
1051888973f5SMichael Tuexen #endif
1052888973f5SMichael Tuexen 			break;
1053888973f5SMichael Tuexen #endif /* INET */
1054888973f5SMichael Tuexen #ifdef INET6
1055888973f5SMichael Tuexen 		case AF_INET6:
1056888973f5SMichael Tuexen 		{
10570ecd976eSBjoern A. Zeeb 			struct sockaddr_in6 *sin6;
1058888973f5SMichael Tuexen 
10590ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10600ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1061888973f5SMichael Tuexen 				if (m)
1062888973f5SMichael Tuexen 					m_freem(m);
1063888973f5SMichael Tuexen 				error = EINVAL;
1064888973f5SMichael Tuexen 				goto out;
1065888973f5SMichael Tuexen 			}
1066e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1067e240ce42SMichael Tuexen 				if (m != NULL)
1068e240ce42SMichael Tuexen 					m_freem(m);
1069e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1070e240ce42SMichael Tuexen 				goto out;
1071e240ce42SMichael Tuexen 			}
10720ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1073888973f5SMichael Tuexen 				if (m)
1074888973f5SMichael Tuexen 					m_freem(m);
1075888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1076888973f5SMichael Tuexen 				goto out;
1077888973f5SMichael Tuexen 			}
10780ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1079888973f5SMichael Tuexen #ifdef INET
1080888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1081888973f5SMichael Tuexen 					error = EINVAL;
1082888973f5SMichael Tuexen 					if (m)
1083888973f5SMichael Tuexen 						m_freem(m);
1084888973f5SMichael Tuexen 					goto out;
1085888973f5SMichael Tuexen 				}
1086888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1087888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1088888973f5SMichael Tuexen 					if (m)
1089888973f5SMichael Tuexen 						m_freem(m);
1090888973f5SMichael Tuexen 					goto out;
1091888973f5SMichael Tuexen 				}
10924a91aa8fSMichael Tuexen 				restoreflags = true;
1093888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1094888973f5SMichael Tuexen 				sinp = &sin;
10950ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1096888973f5SMichael Tuexen 				if (IN_MULTICAST(
1097888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1098888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1099888973f5SMichael Tuexen 					if (m)
1100888973f5SMichael Tuexen 						m_freem(m);
1101888973f5SMichael Tuexen 					goto out;
1102888973f5SMichael Tuexen 				}
1103888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
1104888973f5SMichael Tuexen 				    &sinp->sin_addr))) {
1105888973f5SMichael Tuexen 					if (m)
1106888973f5SMichael Tuexen 						m_freem(m);
1107888973f5SMichael Tuexen 					goto out;
1108888973f5SMichael Tuexen 				}
1109888973f5SMichael Tuexen 				isipv6 = 0;
1110888973f5SMichael Tuexen #else /* !INET */
1111888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1112888973f5SMichael Tuexen 				if (m)
1113888973f5SMichael Tuexen 					m_freem(m);
1114888973f5SMichael Tuexen 				goto out;
1115888973f5SMichael Tuexen #endif /* INET */
1116888973f5SMichael Tuexen 			} else {
1117888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1118888973f5SMichael Tuexen 					if (m)
1119888973f5SMichael Tuexen 						m_freem(m);
1120888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1121888973f5SMichael Tuexen 					goto out;
1122888973f5SMichael Tuexen 				}
11234a91aa8fSMichael Tuexen 				restoreflags = true;
1124888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1125888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1126888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
11270ecd976eSBjoern A. Zeeb 				    &sin6->sin6_addr))) {
1128888973f5SMichael Tuexen 					if (m)
1129888973f5SMichael Tuexen 						m_freem(m);
1130888973f5SMichael Tuexen 					goto out;
1131888973f5SMichael Tuexen 				}
1132888973f5SMichael Tuexen 				isipv6 = 1;
1133888973f5SMichael Tuexen 			}
1134888973f5SMichael Tuexen 			break;
1135888973f5SMichael Tuexen 		}
1136888973f5SMichael Tuexen #endif /* INET6 */
1137888973f5SMichael Tuexen 		default:
1138888973f5SMichael Tuexen 			if (m)
1139888973f5SMichael Tuexen 				m_freem(m);
1140888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1141888973f5SMichael Tuexen 			goto out;
1142888973f5SMichael Tuexen 		}
1143888973f5SMichael Tuexen 	}
11449c9906e9SPeter Wemm 	if (control) {
11459c9906e9SPeter Wemm 		/* TCP doesn't do control messages (rights, creds, etc) */
11469c9906e9SPeter Wemm 		if (control->m_len) {
11479c9906e9SPeter Wemm 			m_freem(control);
11482c37256eSGarrett Wollman 			if (m)
11492c37256eSGarrett Wollman 				m_freem(m);
1150744f87eaSDavid Greenman 			error = EINVAL;
1151744f87eaSDavid Greenman 			goto out;
11522c37256eSGarrett Wollman 		}
11539c9906e9SPeter Wemm 		m_freem(control);	/* empty control, just free it */
11549c9906e9SPeter Wemm 	}
11552c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
1156651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
11572c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
11582c37256eSGarrett Wollman 			/*
11592c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
11602c37256eSGarrett Wollman 			 * initialize window to default value, and
11610c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
11622c37256eSGarrett Wollman 			 */
1163fb59c426SYoshinobu Inoue #ifdef INET6
1164fb59c426SYoshinobu Inoue 			if (isipv6)
1165b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1166fb59c426SYoshinobu Inoue #endif /* INET6 */
1167b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1168b287c6c7SBjoern A. Zeeb 			else
1169b287c6c7SBjoern A. Zeeb #endif
1170b287c6c7SBjoern A. Zeeb #ifdef INET
1171888973f5SMichael Tuexen 				error = tcp_connect(tp,
1172888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1173b287c6c7SBjoern A. Zeeb #endif
11744a91aa8fSMichael Tuexen 			/*
11754a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11764a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11774a91aa8fSMichael Tuexen 			 * operations fail.
11784a91aa8fSMichael Tuexen 			 */
11794a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11804a91aa8fSMichael Tuexen 				restoreflags = false;
11814a91aa8fSMichael Tuexen 
11822c37256eSGarrett Wollman 			if (error)
11832c37256eSGarrett Wollman 				goto out;
1184c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1185c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
118618a75309SPatrick Kelsey 			else {
11872c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11882c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11892c37256eSGarrett Wollman 			}
1190c560df6fSPatrick Kelsey 		}
11912c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11922c37256eSGarrett Wollman 			/*
11932c37256eSGarrett Wollman 			 * Close the send side of the connection after
11942c37256eSGarrett Wollman 			 * the data is sent.
11952c37256eSGarrett Wollman 			 */
11962c37256eSGarrett Wollman 			socantsendmore(so);
1197623dce13SRobert Watson 			tcp_usrclosed(tp);
11982c37256eSGarrett Wollman 		}
1199*e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1200*e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1201*e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1202*e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1203*e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1204*e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1205*e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1206*e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1207*e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1208*e854dd38SRandall Stewart 		}
12092cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
12102cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1211b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1212b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
121355bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
1214b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1215b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1216b0acefa8SBill Fenner 		}
12172c37256eSGarrett Wollman 	} else {
1218623dce13SRobert Watson 		/*
1219623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1220623dce13SRobert Watson 		 */
1221d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
12222c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1223d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
12242c37256eSGarrett Wollman 			m_freem(m);
12252c37256eSGarrett Wollman 			error = ENOBUFS;
12262c37256eSGarrett Wollman 			goto out;
12272c37256eSGarrett Wollman 		}
12282c37256eSGarrett Wollman 		/*
12292c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
12302c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
12312c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
12322c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
12332c37256eSGarrett Wollman 		 * of data past the urgent section.
12342c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
12352c37256eSGarrett Wollman 		 */
1236651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1237d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
1238ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1239ef53690bSGarrett Wollman 			/*
1240ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1241ef53690bSGarrett Wollman 			 * initialize window to default value, and
12420c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1243ef53690bSGarrett Wollman 			 */
124418a75309SPatrick Kelsey 
1245c560df6fSPatrick Kelsey 			/*
1246c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1247c560df6fSPatrick Kelsey 			 */
1248c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1249c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1250fb59c426SYoshinobu Inoue #ifdef INET6
1251fb59c426SYoshinobu Inoue 			if (isipv6)
1252b40ce416SJulian Elischer 				error = tcp6_connect(tp, nam, td);
1253fb59c426SYoshinobu Inoue #endif /* INET6 */
1254b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1255b287c6c7SBjoern A. Zeeb 			else
1256b287c6c7SBjoern A. Zeeb #endif
1257b287c6c7SBjoern A. Zeeb #ifdef INET
1258888973f5SMichael Tuexen 				error = tcp_connect(tp,
1259888973f5SMichael Tuexen 				    (struct sockaddr *)sinp, td);
1260b287c6c7SBjoern A. Zeeb #endif
12614a91aa8fSMichael Tuexen 			/*
12624a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
12634a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
12644a91aa8fSMichael Tuexen 			 * operations fail.
12654a91aa8fSMichael Tuexen 			 */
12664a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
12674a91aa8fSMichael Tuexen 				restoreflags = false;
12684a91aa8fSMichael Tuexen 
1269ef53690bSGarrett Wollman 			if (error)
1270ef53690bSGarrett Wollman 				goto out;
1271ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1272ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1273623dce13SRobert Watson 		}
1274300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
12752cbcd3c1SGleb Smirnoff 		if (!(flags & PRUS_NOTREADY)) {
12762cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
127755bceb1eSRandall Stewart 			error = tp->t_fb->tfb_tcp_output(tp);
12782cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12792c37256eSGarrett Wollman 		}
12802cbcd3c1SGleb Smirnoff 	}
12812529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12822529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12832529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12842529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12852529f56eSJonathan T. Looney 	    0, NULL, false);
1286d1401c90SRobert Watson out:
12874a91aa8fSMichael Tuexen 	/*
12884a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12894a91aa8fSMichael Tuexen 	 * restore the original flags.
12904a91aa8fSMichael Tuexen 	 */
12914a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12924a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12934a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12944a91aa8fSMichael Tuexen 	}
1295d1401c90SRobert Watson 	TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
12962c37256eSGarrett Wollman 		  ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12975d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12985d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
12998501a69cSRobert Watson 	INP_WUNLOCK(inp);
130097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
130173fddedaSPeter Grehan 	return (error);
13022c37256eSGarrett Wollman }
13032c37256eSGarrett Wollman 
13042cbcd3c1SGleb Smirnoff static int
13052cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
13062cbcd3c1SGleb Smirnoff {
1307109eb549SGleb Smirnoff 	struct epoch_tracker et;
13082cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
13092cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
13102cbcd3c1SGleb Smirnoff 	int error;
13112cbcd3c1SGleb Smirnoff 
13122cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
13132cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
13142cbcd3c1SGleb Smirnoff 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
13152cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
131682334850SJohn Baldwin 		mb_free_notready(m, count);
13172cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
13182cbcd3c1SGleb Smirnoff 	}
13192cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
13202cbcd3c1SGleb Smirnoff 
13212cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
13222cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
13232cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1324109eb549SGleb Smirnoff 	if (error == 0) {
1325109eb549SGleb Smirnoff 		NET_EPOCH_ENTER(et);
132655bceb1eSRandall Stewart 		error = tp->t_fb->tfb_tcp_output(tp);
1327109eb549SGleb Smirnoff 		NET_EPOCH_EXIT(et);
1328109eb549SGleb Smirnoff 	}
13292cbcd3c1SGleb Smirnoff 	INP_WUNLOCK(inp);
13302cbcd3c1SGleb Smirnoff 
13312cbcd3c1SGleb Smirnoff 	return (error);
13322cbcd3c1SGleb Smirnoff }
13332cbcd3c1SGleb Smirnoff 
13342c37256eSGarrett Wollman /*
1335a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
13362c37256eSGarrett Wollman  */
1337ac45e92fSRobert Watson static void
13382c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
13392c37256eSGarrett Wollman {
1340f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1341a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13426573d758SMatt Macy 	struct epoch_tracker et;
1343623dce13SRobert Watson 	TCPDEBUG0;
1344c78cbc7bSRobert Watson 
1345ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1346c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1347c78cbc7bSRobert Watson 
134897a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13498501a69cSRobert Watson 	INP_WLOCK(inp);
1350c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1351c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1352c78cbc7bSRobert Watson 
1353c78cbc7bSRobert Watson 	/*
1354a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1355c78cbc7bSRobert Watson 	 */
1356ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1357ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1358c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
1359a152f8a3SRobert Watson 		TCPDEBUG1();
13608fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
13618fa799bdSJonathan T. Looney 		if (tp == NULL)
13628fa799bdSJonathan T. Looney 			goto dropped;
1363a152f8a3SRobert Watson 		TCPDEBUG2(PRU_ABORT);
13645d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1365c78cbc7bSRobert Watson 	}
1366ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1367a152f8a3SRobert Watson 		SOCK_LOCK(so);
1368a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1369a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1370ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1371a152f8a3SRobert Watson 	}
13728501a69cSRobert Watson 	INP_WUNLOCK(inp);
13738fa799bdSJonathan T. Looney dropped:
137497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1375a152f8a3SRobert Watson }
1376a152f8a3SRobert Watson 
1377a152f8a3SRobert Watson /*
1378a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1379a152f8a3SRobert Watson  */
1380a152f8a3SRobert Watson static void
1381a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1382a152f8a3SRobert Watson {
1383a152f8a3SRobert Watson 	struct inpcb *inp;
1384a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
13856573d758SMatt Macy 	struct epoch_tracker et;
1386a152f8a3SRobert Watson 	TCPDEBUG0;
1387a152f8a3SRobert Watson 
1388a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1389a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1390a152f8a3SRobert Watson 
139197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13928501a69cSRobert Watson 	INP_WLOCK(inp);
1393a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1394a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1395a152f8a3SRobert Watson 
1396a152f8a3SRobert Watson 	/*
1397a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1398a152f8a3SRobert Watson 	 * a disconnect.
1399a152f8a3SRobert Watson 	 */
1400ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_TIMEWAIT) &&
1401ad71fe3cSRobert Watson 	    !(inp->inp_flags & INP_DROPPED)) {
1402a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1403a152f8a3SRobert Watson 		TCPDEBUG1();
1404a152f8a3SRobert Watson 		tcp_disconnect(tp);
1405a152f8a3SRobert Watson 		TCPDEBUG2(PRU_CLOSE);
14065d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1407a152f8a3SRobert Watson 	}
1408ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1409a152f8a3SRobert Watson 		SOCK_LOCK(so);
1410a152f8a3SRobert Watson 		so->so_state |= SS_PROTOREF;
1411a152f8a3SRobert Watson 		SOCK_UNLOCK(so);
1412ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1413a152f8a3SRobert Watson 	}
14148501a69cSRobert Watson 	INP_WUNLOCK(inp);
141597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
14162c37256eSGarrett Wollman }
14172c37256eSGarrett Wollman 
1418d3b6c96bSRandall Stewart static int
1419d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1420d3b6c96bSRandall Stewart {
1421d3b6c96bSRandall Stewart 	/*
1422d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1423d3b6c96bSRandall Stewart 	 * specified then it does not always support
1424d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1425d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1426d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1427d3b6c96bSRandall Stewart 	 */
1428d3b6c96bSRandall Stewart 	int ret = 0;
1429d3b6c96bSRandall Stewart 
1430d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1431d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1432d3b6c96bSRandall Stewart 	}
1433d3b6c96bSRandall Stewart 	return (ret);
1434d3b6c96bSRandall Stewart }
1435d3b6c96bSRandall Stewart 
14362c37256eSGarrett Wollman /*
14372c37256eSGarrett Wollman  * Receive out-of-band data.
14382c37256eSGarrett Wollman  */
14392c37256eSGarrett Wollman static int
14402c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
14412c37256eSGarrett Wollman {
14422c37256eSGarrett Wollman 	int error = 0;
1443f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1444623dce13SRobert Watson 	struct tcpcb *tp = NULL;
14452c37256eSGarrett Wollman 
1446623dce13SRobert Watson 	TCPDEBUG0;
1447623dce13SRobert Watson 	inp = sotoinpcb(so);
1448623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
14498501a69cSRobert Watson 	INP_WLOCK(inp);
1450ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
145121367f63SSam Leffler 		error = ECONNRESET;
1452623dce13SRobert Watson 		goto out;
1453623dce13SRobert Watson 	}
1454623dce13SRobert Watson 	tp = intotcpcb(inp);
1455d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1456d3b6c96bSRandall Stewart 	if (error) {
1457d3b6c96bSRandall Stewart 		goto out;
1458d3b6c96bSRandall Stewart 	}
1459623dce13SRobert Watson 	TCPDEBUG1();
14602c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1461c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
14624cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
14634cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
14642c37256eSGarrett Wollman 		error = EINVAL;
14652c37256eSGarrett Wollman 		goto out;
14662c37256eSGarrett Wollman 	}
14672c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
14682c37256eSGarrett Wollman 		error = EWOULDBLOCK;
14692c37256eSGarrett Wollman 		goto out;
14702c37256eSGarrett Wollman 	}
14712c37256eSGarrett Wollman 	m->m_len = 1;
14722c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
14732c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
14742c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1475623dce13SRobert Watson 
1476623dce13SRobert Watson out:
1477623dce13SRobert Watson 	TCPDEBUG2(PRU_RCVOOB);
14785d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14798501a69cSRobert Watson 	INP_WUNLOCK(inp);
1480623dce13SRobert Watson 	return (error);
14812c37256eSGarrett Wollman }
14822c37256eSGarrett Wollman 
1483b287c6c7SBjoern A. Zeeb #ifdef INET
14842c37256eSGarrett Wollman struct pr_usrreqs tcp_usrreqs = {
1485756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1486756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp_usr_accept,
1487756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1488756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp_usr_bind,
1489756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp_usr_connect,
1490756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1491756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1492756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1493756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp_usr_listen,
149454d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1495756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1496756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1497756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
14982cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1499756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
150054d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1501a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1502a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
15032c37256eSGarrett Wollman };
1504b287c6c7SBjoern A. Zeeb #endif /* INET */
1505df8bae1dSRodney W. Grimes 
1506fb59c426SYoshinobu Inoue #ifdef INET6
1507fb59c426SYoshinobu Inoue struct pr_usrreqs tcp6_usrreqs = {
1508756d52a1SPoul-Henning Kamp 	.pru_abort =		tcp_usr_abort,
1509756d52a1SPoul-Henning Kamp 	.pru_accept =		tcp6_usr_accept,
1510756d52a1SPoul-Henning Kamp 	.pru_attach =		tcp_usr_attach,
1511756d52a1SPoul-Henning Kamp 	.pru_bind =		tcp6_usr_bind,
1512756d52a1SPoul-Henning Kamp 	.pru_connect =		tcp6_usr_connect,
1513756d52a1SPoul-Henning Kamp 	.pru_control =		in6_control,
1514756d52a1SPoul-Henning Kamp 	.pru_detach =		tcp_usr_detach,
1515756d52a1SPoul-Henning Kamp 	.pru_disconnect =	tcp_usr_disconnect,
1516756d52a1SPoul-Henning Kamp 	.pru_listen =		tcp6_usr_listen,
1517756d52a1SPoul-Henning Kamp 	.pru_peeraddr =		in6_mapped_peeraddr,
1518756d52a1SPoul-Henning Kamp 	.pru_rcvd =		tcp_usr_rcvd,
1519756d52a1SPoul-Henning Kamp 	.pru_rcvoob =		tcp_usr_rcvoob,
1520756d52a1SPoul-Henning Kamp 	.pru_send =		tcp_usr_send,
15212cbcd3c1SGleb Smirnoff 	.pru_ready =		tcp_usr_ready,
1522756d52a1SPoul-Henning Kamp 	.pru_shutdown =		tcp_usr_shutdown,
1523756d52a1SPoul-Henning Kamp 	.pru_sockaddr =		in6_mapped_sockaddr,
1524a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1525a152f8a3SRobert Watson 	.pru_close =		tcp_usr_close,
1526fb59c426SYoshinobu Inoue };
1527fb59c426SYoshinobu Inoue #endif /* INET6 */
1528fb59c426SYoshinobu Inoue 
1529b287c6c7SBjoern A. Zeeb #ifdef INET
1530a0292f23SGarrett Wollman /*
1531a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1532a0292f23SGarrett Wollman  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
15335200e00eSIan Dowse  * port number if needed.  Call in_pcbconnect_setup to do the routing and
15345200e00eSIan Dowse  * to choose a local host address (interface).  If there is an existing
15355200e00eSIan Dowse  * incarnation of the same connection in TIME-WAIT state and if the remote
15365200e00eSIan Dowse  * host was sending CC options and if the connection duration was < MSL, then
1537a0292f23SGarrett Wollman  * truncate the previous TIME-WAIT state and proceed.
1538a0292f23SGarrett Wollman  * Initialize connection parameters and enter SYN-SENT state.
1539a0292f23SGarrett Wollman  */
15400312fbe9SPoul-Henning Kamp static int
1541ad3f9ab3SAndre Oppermann tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1542a0292f23SGarrett Wollman {
1543a0292f23SGarrett Wollman 	struct inpcb *inp = tp->t_inpcb, *oinp;
1544a0292f23SGarrett Wollman 	struct socket *so = inp->inp_socket;
15455200e00eSIan Dowse 	struct in_addr laddr;
15465200e00eSIan Dowse 	u_short lport;
1547c3229e05SDavid Greenman 	int error;
1548a0292f23SGarrett Wollman 
1549c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
15508501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1551fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1552623dce13SRobert Watson 
155325102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
15544616026fSErmal Luçi 		error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
15554616026fSErmal Luçi 		if (error)
1556fa046d87SRobert Watson 			goto out;
1557a0292f23SGarrett Wollman 	}
1558a0292f23SGarrett Wollman 
1559a0292f23SGarrett Wollman 	/*
1560a0292f23SGarrett Wollman 	 * Cannot simply call in_pcbconnect, because there might be an
1561a0292f23SGarrett Wollman 	 * earlier incarnation of this same connection still in
1562a0292f23SGarrett Wollman 	 * TIME_WAIT state, creating an ADDRINUSE error.
1563a0292f23SGarrett Wollman 	 */
15645200e00eSIan Dowse 	laddr = inp->inp_laddr;
15655200e00eSIan Dowse 	lport = inp->inp_lport;
15665200e00eSIan Dowse 	error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1567b0330ed9SPawel Jakub Dawidek 	    &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
15685200e00eSIan Dowse 	if (error && oinp == NULL)
1569fa046d87SRobert Watson 		goto out;
1570fa046d87SRobert Watson 	if (oinp) {
1571fa046d87SRobert Watson 		error = EADDRINUSE;
1572fa046d87SRobert Watson 		goto out;
1573fa046d87SRobert Watson 	}
157425102351SMike Karels 	/* Handle initial bind if it hadn't been done in advance. */
157525102351SMike Karels 	if (inp->inp_lport == 0) {
157625102351SMike Karels 		inp->inp_lport = lport;
157725102351SMike Karels 		if (in_pcbinshash(inp) != 0) {
157825102351SMike Karels 			inp->inp_lport = 0;
157925102351SMike Karels 			error = EAGAIN;
158025102351SMike Karels 			goto out;
158125102351SMike Karels 		}
158225102351SMike Karels 	}
15835200e00eSIan Dowse 	inp->inp_laddr = laddr;
158415bd2b43SDavid Greenman 	in_pcbrehash(inp);
1585fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1586a0292f23SGarrett Wollman 
1587087b55eaSAndre Oppermann 	/*
1588087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1589087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1590087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1591087b55eaSAndre Oppermann 	 */
1592a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
15939b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1594a0292f23SGarrett Wollman 		tp->request_r_scale++;
1595a0292f23SGarrett Wollman 
1596a0292f23SGarrett Wollman 	soisconnecting(so);
159778b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
159857f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15998e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
16008e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
16018e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1602a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1603a45d2726SAndras Olah 
1604a0292f23SGarrett Wollman 	return 0;
1605fa046d87SRobert Watson 
1606fa046d87SRobert Watson out:
1607fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1608fa046d87SRobert Watson 	return (error);
1609a0292f23SGarrett Wollman }
1610b287c6c7SBjoern A. Zeeb #endif /* INET */
1611a0292f23SGarrett Wollman 
1612fb59c426SYoshinobu Inoue #ifdef INET6
1613fb59c426SYoshinobu Inoue static int
1614ad3f9ab3SAndre Oppermann tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1615fb59c426SYoshinobu Inoue {
1616a7e201bbSAndrey V. Elsukov 	struct inpcb *inp = tp->t_inpcb;
1617fb59c426SYoshinobu Inoue 	int error;
1618fb59c426SYoshinobu Inoue 
16198501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1620fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1621623dce13SRobert Watson 
162225102351SMike Karels 	if (V_tcp_require_unique_port && inp->inp_lport == 0) {
16234616026fSErmal Luçi 		error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
16244616026fSErmal Luçi 		if (error)
1625fa046d87SRobert Watson 			goto out;
1626fb59c426SYoshinobu Inoue 	}
1627a7e201bbSAndrey V. Elsukov 	error = in6_pcbconnect(inp, nam, td->td_ucred);
1628a7e201bbSAndrey V. Elsukov 	if (error != 0)
1629b598155aSRobert Watson 		goto out;
1630fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1631fb59c426SYoshinobu Inoue 
1632fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1633fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1634970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1635fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1636fb59c426SYoshinobu Inoue 
1637a7e201bbSAndrey V. Elsukov 	soisconnecting(inp->inp_socket);
163878b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
163957f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
16408e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
16418e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
16428e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1643fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1644fb59c426SYoshinobu Inoue 
1645fb59c426SYoshinobu Inoue 	return 0;
1646fa046d87SRobert Watson 
1647fa046d87SRobert Watson out:
1648fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
1649fa046d87SRobert Watson 	return error;
1650fb59c426SYoshinobu Inoue }
1651fb59c426SYoshinobu Inoue #endif /* INET6 */
1652fb59c426SYoshinobu Inoue 
1653cfe8b629SGarrett Wollman /*
1654b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1655b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1656b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1657b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1658b8af5dfaSRobert Watson  * from Linux.
1659b8af5dfaSRobert Watson  */
1660b8af5dfaSRobert Watson static void
1661ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1662b8af5dfaSRobert Watson {
1663b8af5dfaSRobert Watson 
16648501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1665b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1666b8af5dfaSRobert Watson 
1667b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1668b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1669b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
16703529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1671b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1672b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1673b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1674b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1675b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1676b8af5dfaSRobert Watson 	}
16773cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT)
16785a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
16791baaf834SBruce M Simpson 
168043d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
16813ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
16821baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
16831baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
16841baaf834SBruce M Simpson 
1685b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1686b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1687b8af5dfaSRobert Watson 
1688b8af5dfaSRobert Watson 	/*
1689b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1690b8af5dfaSRobert Watson 	 */
1691c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1692535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1693b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16941c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1695535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
169643d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
169743d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1698f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1699f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1700f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1701a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1702a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1703a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1704a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1705a6456410SNavdeep Parhar 	}
1706a6456410SNavdeep Parhar #endif
1707b8af5dfaSRobert Watson }
1708b8af5dfaSRobert Watson 
1709b8af5dfaSRobert Watson /*
17101e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
17111e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
17121e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
17131e8f5ffaSRobert Watson  * option.
1714cfe8b629SGarrett Wollman  */
1715bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
17168501a69cSRobert Watson 	INP_WLOCK(inp);							\
1717ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {		\
17188501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1719bac5bedfSConrad Meyer 		cleanup;						\
17201e8f5ffaSRobert Watson 		return (ECONNRESET);					\
17211e8f5ffaSRobert Watson 	}								\
17221e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
17231e8f5ffaSRobert Watson } while(0)
1724bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
17251e8f5ffaSRobert Watson 
1726df8bae1dSRodney W. Grimes int
1727ad3f9ab3SAndre Oppermann tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1728df8bae1dSRodney W. Grimes {
172955bceb1eSRandall Stewart 	int	error;
1730df8bae1dSRodney W. Grimes 	struct	inpcb *inp;
1731cfe8b629SGarrett Wollman 	struct	tcpcb *tp;
173255bceb1eSRandall Stewart 	struct tcp_function_block *blk;
173355bceb1eSRandall Stewart 	struct tcp_function_set fsn;
1734df8bae1dSRodney W. Grimes 
1735cfe8b629SGarrett Wollman 	error = 0;
1736df8bae1dSRodney W. Grimes 	inp = sotoinpcb(so);
1737623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1738cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
1739fb59c426SYoshinobu Inoue #ifdef INET6
17405cd54324SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6PROTO) {
1741fb59c426SYoshinobu Inoue 			error = ip6_ctloutput(so, sopt);
17425dff1c38SMichael Tuexen 			/*
17435dff1c38SMichael Tuexen 			 * In case of the IPV6_USE_MIN_MTU socket option,
17445dff1c38SMichael Tuexen 			 * the INC_IPV6MINMTU flag to announce a corresponding
17455dff1c38SMichael Tuexen 			 * MSS during the initial handshake.
17465dff1c38SMichael Tuexen 			 * If the TCP connection is not in the front states,
17475dff1c38SMichael Tuexen 			 * just reduce the MSS being used.
17485dff1c38SMichael Tuexen 			 * This avoids the sending of TCP segments which will
17495dff1c38SMichael Tuexen 			 * be fragmented at the IPv6 layer.
17505dff1c38SMichael Tuexen 			 */
17515dff1c38SMichael Tuexen 			if ((error == 0) &&
17525dff1c38SMichael Tuexen 			    (sopt->sopt_dir == SOPT_SET) &&
17535dff1c38SMichael Tuexen 			    (sopt->sopt_level == IPPROTO_IPV6) &&
17545dff1c38SMichael Tuexen 			    (sopt->sopt_name == IPV6_USE_MIN_MTU)) {
17555dff1c38SMichael Tuexen 				INP_WLOCK(inp);
17565dff1c38SMichael Tuexen 				if ((inp->inp_flags &
17575dff1c38SMichael Tuexen 				    (INP_TIMEWAIT | INP_DROPPED))) {
17585dff1c38SMichael Tuexen 					INP_WUNLOCK(inp);
17595dff1c38SMichael Tuexen 					return (ECONNRESET);
17605dff1c38SMichael Tuexen 				}
17615dff1c38SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_IPV6MINMTU;
17625dff1c38SMichael Tuexen 				tp = intotcpcb(inp);
17635dff1c38SMichael Tuexen 				if ((tp->t_state >= TCPS_SYN_SENT) &&
17645dff1c38SMichael Tuexen 				    (inp->inp_inc.inc_flags & INC_ISIPV6)) {
17655dff1c38SMichael Tuexen 					struct ip6_pktopts *opt;
17665dff1c38SMichael Tuexen 
17675dff1c38SMichael Tuexen 					opt = inp->in6p_outputopts;
17685dff1c38SMichael Tuexen 					if ((opt != NULL) &&
17695dff1c38SMichael Tuexen 					    (opt->ip6po_minmtu ==
17705dff1c38SMichael Tuexen 					    IP6PO_MINMTU_ALL)) {
17715dff1c38SMichael Tuexen 						if (tp->t_maxseg > TCP6_MSS) {
17725dff1c38SMichael Tuexen 							tp->t_maxseg = TCP6_MSS;
17735dff1c38SMichael Tuexen 						}
17745dff1c38SMichael Tuexen 					}
17755dff1c38SMichael Tuexen 				}
17765dff1c38SMichael Tuexen 				INP_WUNLOCK(inp);
17775dff1c38SMichael Tuexen 			}
1778b287c6c7SBjoern A. Zeeb 		}
1779fb59c426SYoshinobu Inoue #endif /* INET6 */
1780b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1781b287c6c7SBjoern A. Zeeb 		else
1782b287c6c7SBjoern A. Zeeb #endif
1783b287c6c7SBjoern A. Zeeb #ifdef INET
1784b287c6c7SBjoern A. Zeeb 		{
1785cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
17861e8f5ffaSRobert Watson 		}
17871e8f5ffaSRobert Watson #endif
1788df8bae1dSRodney W. Grimes 		return (error);
1789df8bae1dSRodney W. Grimes 	}
179068cea2b1SJohn Baldwin 	INP_WLOCK(inp);
1791ad71fe3cSRobert Watson 	if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
17928501a69cSRobert Watson 		INP_WUNLOCK(inp);
17931e8f5ffaSRobert Watson 		return (ECONNRESET);
1794623dce13SRobert Watson 	}
179555bceb1eSRandall Stewart 	tp = intotcpcb(inp);
179655bceb1eSRandall Stewart 	/*
179755bceb1eSRandall Stewart 	 * Protect the TCP option TCP_FUNCTION_BLK so
179855bceb1eSRandall Stewart 	 * that a sub-function can *never* overwrite this.
179955bceb1eSRandall Stewart 	 */
180055bceb1eSRandall Stewart 	if ((sopt->sopt_dir == SOPT_SET) &&
180155bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
180255bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
180355bceb1eSRandall Stewart 		error = sooptcopyin(sopt, &fsn, sizeof fsn,
180455bceb1eSRandall Stewart 		    sizeof fsn);
180555bceb1eSRandall Stewart 		if (error)
180655bceb1eSRandall Stewart 			return (error);
180755bceb1eSRandall Stewart 		INP_WLOCK_RECHECK(inp);
180855bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
180955bceb1eSRandall Stewart 		if (blk == NULL) {
181055bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
181155bceb1eSRandall Stewart 			return (ENOENT);
181255bceb1eSRandall Stewart 		}
1813587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1814587d67c0SRandall Stewart 			/* You already have this */
1815587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1816587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1817587d67c0SRandall Stewart 			return (0);
1818587d67c0SRandall Stewart 		}
1819587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1820587d67c0SRandall Stewart 			/*
1821587d67c0SRandall Stewart 			 * The user has advanced the state
1822587d67c0SRandall Stewart 			 * past the initial point, we may not
1823587d67c0SRandall Stewart 			 * be able to switch.
1824587d67c0SRandall Stewart 			 */
1825587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1826587d67c0SRandall Stewart 				/*
1827587d67c0SRandall Stewart 				 * Does the stack provide a
1828587d67c0SRandall Stewart 				 * query mechanism, if so it may
1829587d67c0SRandall Stewart 				 * still be possible?
1830587d67c0SRandall Stewart 				 */
1831587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1832c6c0be27SMichael Tuexen 			} else
1833c6c0be27SMichael Tuexen 				error = EINVAL;
1834587d67c0SRandall Stewart 			if (error) {
1835587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1836587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1837587d67c0SRandall Stewart 				return(error);
1838587d67c0SRandall Stewart 			}
1839587d67c0SRandall Stewart 		}
184055bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
184155bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
184255bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
184355bceb1eSRandall Stewart 			return (ENOENT);
184455bceb1eSRandall Stewart 		}
184555bceb1eSRandall Stewart 		/*
184655bceb1eSRandall Stewart 		 * Release the old refcnt, the
1847587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1848587d67c0SRandall Stewart 		 * new one already.
184955bceb1eSRandall Stewart 		 */
1850587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1851587d67c0SRandall Stewart 			/*
1852587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1853587d67c0SRandall Stewart 			 * the tcb is not going away.
1854587d67c0SRandall Stewart 			 */
1855587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1856587d67c0SRandall Stewart 		}
18573ee9c3c4SRandall Stewart #ifdef TCPHPTS
18583ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
18593ee9c3c4SRandall Stewart 		tcp_hpts_remove(tp->t_inpcb, HPTS_REMOVE_ALL);
18603ee9c3c4SRandall Stewart #endif
18613ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
18623ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
18633ee9c3c4SRandall Stewart 			if (error) {
18643ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
18653ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
18663ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
18673ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
18683ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
18693ee9c3c4SRandall Stewart 						soabort(so);
18703ee9c3c4SRandall Stewart 						return(error);
18713ee9c3c4SRandall Stewart 					}
18723ee9c3c4SRandall Stewart 				}
18733ee9c3c4SRandall Stewart 				goto err_out;
18743ee9c3c4SRandall Stewart 			}
18753ee9c3c4SRandall Stewart 		}
187655bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
187755bceb1eSRandall Stewart 		tp->t_fb = blk;
187855bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
187955bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
188055bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
188155bceb1eSRandall Stewart 			     sopt->sopt_name);
188255bceb1eSRandall Stewart 		}
188355bceb1eSRandall Stewart #endif
18843ee9c3c4SRandall Stewart err_out:
188555bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
188655bceb1eSRandall Stewart 		return (error);
188755bceb1eSRandall Stewart 	} else if ((sopt->sopt_dir == SOPT_GET) &&
188855bceb1eSRandall Stewart 	    (sopt->sopt_name == TCP_FUNCTION_BLK)) {
1889c73b6f4dSEd Maste 		strncpy(fsn.function_set_name, tp->t_fb->tfb_tcp_block_name,
1890c73b6f4dSEd Maste 		    TCP_FUNCTION_NAME_LEN_MAX);
1891c73b6f4dSEd Maste 		fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
189255bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
189355bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
189455bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
189555bceb1eSRandall Stewart 		return (error);
189655bceb1eSRandall Stewart 	}
189755bceb1eSRandall Stewart 	/* Pass in the INP locked, called must unlock it */
189855bceb1eSRandall Stewart 	return (tp->t_fb->tfb_tcp_ctloutput(so, sopt, inp, tp));
189955bceb1eSRandall Stewart }
190055bceb1eSRandall Stewart 
19012529f56eSJonathan T. Looney /*
19022529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
19032529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
19042529f56eSJonathan T. Looney  */
19052529f56eSJonathan T. Looney #ifdef CTASSERT
19062529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
19072529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
19082529f56eSJonathan T. Looney #endif
19092529f56eSJonathan T. Looney 
1910ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1911ec1db6e1SJohn Baldwin static int
1912ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1913ec1db6e1SJohn Baldwin {
1914ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1915ec1db6e1SJohn Baldwin 	int error;
1916ec1db6e1SJohn Baldwin 
1917ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1918ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1919ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1920ec1db6e1SJohn Baldwin 		if (error)
1921ec1db6e1SJohn Baldwin 			return (error);
1922ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1923ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1924ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1925ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1926ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1927ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1928ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1929ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1930ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1931ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1932ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1933ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1934ec1db6e1SJohn Baldwin 		return (0);
1935ec1db6e1SJohn Baldwin 	}
1936ec1db6e1SJohn Baldwin 
1937ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1938ec1db6e1SJohn Baldwin }
1939ec1db6e1SJohn Baldwin #endif
1940ec1db6e1SJohn Baldwin 
194155bceb1eSRandall Stewart int
194255bceb1eSRandall Stewart tcp_default_ctloutput(struct socket *so, struct sockopt *sopt, struct inpcb *inp, struct tcpcb *tp)
194355bceb1eSRandall Stewart {
194455bceb1eSRandall Stewart 	int	error, opt, optval;
194555bceb1eSRandall Stewart 	u_int	ui;
194655bceb1eSRandall Stewart 	struct	tcp_info ti;
1947b2e60773SJohn Baldwin #ifdef KERN_TLS
1948b2e60773SJohn Baldwin 	struct tls_enable tls;
1949b2e60773SJohn Baldwin #endif
195055bceb1eSRandall Stewart 	struct cc_algo *algo;
19512529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1952adc56f5aSEdward Tomasz Napierala #ifdef STATS
1953adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
1954adc56f5aSEdward Tomasz Napierala #endif
1955af6fef3aSGleb Smirnoff 	size_t	len;
1956df8bae1dSRodney W. Grimes 
1957d519cedbSGleb Smirnoff 	/*
1958d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
1959d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
1960d519cedbSGleb Smirnoff 	 */
1961d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
1962d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
1963d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1964c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
1965c8b53cedSMichael Tuexen 			return (EINVAL);
1966af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
1967af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
1968d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
1969d519cedbSGleb Smirnoff 		if (error) {
1970af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
1971d519cedbSGleb Smirnoff 			return (error);
1972d519cedbSGleb Smirnoff 		}
1973bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
1974d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
1975af6fef3aSGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(tp->ccv, sopt, pbuf);
1976d519cedbSGleb Smirnoff 		else
1977d519cedbSGleb Smirnoff 			error = ENOENT;
1978d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
1979d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
1980af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
1981af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
1982d519cedbSGleb Smirnoff 		return (error);
1983d519cedbSGleb Smirnoff 	}
1984d519cedbSGleb Smirnoff 
1985cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1986cfe8b629SGarrett Wollman 	case SOPT_SET:
1987cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1988fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
198988f6b043SBruce M Simpson 		case TCP_MD5SIG:
1990fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
19918501a69cSRobert Watson 				INP_WUNLOCK(inp);
1992fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
1993fcf59617SAndrey V. Elsukov 			}
1994fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
19951cfd4b53SBruce M Simpson 			if (error)
19961e8f5ffaSRobert Watson 				return (error);
199709fe6320SNavdeep Parhar 			goto unlock_and_done;
1998fcf59617SAndrey V. Elsukov #endif /* IPSEC */
199909fe6320SNavdeep Parhar 
2000df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2001cfe8b629SGarrett Wollman 		case TCP_NOOPT:
20028501a69cSRobert Watson 			INP_WUNLOCK(inp);
2003cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2004cfe8b629SGarrett Wollman 			    sizeof optval);
2005cfe8b629SGarrett Wollman 			if (error)
20061e8f5ffaSRobert Watson 				return (error);
2007cfe8b629SGarrett Wollman 
20088501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2009cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2010cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2011cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2012cfe8b629SGarrett Wollman 				break;
2013cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2014cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2015cfe8b629SGarrett Wollman 				break;
2016cfe8b629SGarrett Wollman 			default:
2017cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2018cfe8b629SGarrett Wollman 				break;
2019cfe8b629SGarrett Wollman 			}
2020cfe8b629SGarrett Wollman 
2021cfe8b629SGarrett Wollman 			if (optval)
2022cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2023df8bae1dSRodney W. Grimes 			else
2024cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
202509fe6320SNavdeep Parhar unlock_and_done:
202609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
202709fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
202809fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
202909fe6320SNavdeep Parhar 				    sopt->sopt_name);
203009fe6320SNavdeep Parhar 			}
203109fe6320SNavdeep Parhar #endif
20328501a69cSRobert Watson 			INP_WUNLOCK(inp);
2033df8bae1dSRodney W. Grimes 			break;
2034df8bae1dSRodney W. Grimes 
2035007581c0SJonathan Lemon 		case TCP_NOPUSH:
20368501a69cSRobert Watson 			INP_WUNLOCK(inp);
2037007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2038007581c0SJonathan Lemon 			    sizeof optval);
2039007581c0SJonathan Lemon 			if (error)
20401e8f5ffaSRobert Watson 				return (error);
2041007581c0SJonathan Lemon 
20428501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2043007581c0SJonathan Lemon 			if (optval)
2044007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2045d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2046007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2047109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2048109eb549SGleb Smirnoff 					struct epoch_tracker et;
2049109eb549SGleb Smirnoff 
2050109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
205155bceb1eSRandall Stewart 					error = tp->t_fb->tfb_tcp_output(tp);
2052109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2053109eb549SGleb Smirnoff 				}
2054007581c0SJonathan Lemon 			}
205509fe6320SNavdeep Parhar 			goto unlock_and_done;
2056007581c0SJonathan Lemon 
2057df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
20588501a69cSRobert Watson 			INP_WUNLOCK(inp);
2059cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2060cfe8b629SGarrett Wollman 			    sizeof optval);
2061cfe8b629SGarrett Wollman 			if (error)
20621e8f5ffaSRobert Watson 				return (error);
2063df8bae1dSRodney W. Grimes 
20648501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
206553369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2066603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2067cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2068a0292f23SGarrett Wollman 			else
2069a0292f23SGarrett Wollman 				error = EINVAL;
207009fe6320SNavdeep Parhar 			goto unlock_and_done;
2071a0292f23SGarrett Wollman 
2072b8af5dfaSRobert Watson 		case TCP_INFO:
20738501a69cSRobert Watson 			INP_WUNLOCK(inp);
2074b8af5dfaSRobert Watson 			error = EINVAL;
2075b8af5dfaSRobert Watson 			break;
2076b8af5dfaSRobert Watson 
2077adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2078adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2079adc56f5aSEdward Tomasz Napierala #ifdef STATS
2080adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2081adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2082adc56f5aSEdward Tomasz Napierala 			if (error)
2083adc56f5aSEdward Tomasz Napierala 				return (error);
2084adc56f5aSEdward Tomasz Napierala 
2085adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2086adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2087adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2088adc56f5aSEdward Tomasz Napierala 			else
2089adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2090adc56f5aSEdward Tomasz Napierala 
2091adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2092adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2093adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2094adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2095adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2096adc56f5aSEdward Tomasz Napierala 				sbp = t;
2097adc56f5aSEdward Tomasz Napierala 			}
2098adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2099adc56f5aSEdward Tomasz Napierala 
2100adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2101adc56f5aSEdward Tomasz Napierala #else
2102adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2103adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2104adc56f5aSEdward Tomasz Napierala 			break;
2105adc56f5aSEdward Tomasz Napierala 
2106dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2107dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2108af6fef3aSGleb Smirnoff 			error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
2109af6fef3aSGleb Smirnoff 			if (error)
2110dbc42409SLawrence Stewart 				break;
2111af6fef3aSGleb Smirnoff 			buf[sopt->sopt_valsize] = '\0';
2112af6fef3aSGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
211373e263b1SGleb Smirnoff 			CC_LIST_RLOCK();
211473e263b1SGleb Smirnoff 			STAILQ_FOREACH(algo, &cc_list, entries)
211573e263b1SGleb Smirnoff 				if (strncmp(buf, algo->name,
211673e263b1SGleb Smirnoff 				    TCP_CA_NAME_MAX) == 0)
211773e263b1SGleb Smirnoff 					break;
211873e263b1SGleb Smirnoff 			CC_LIST_RUNLOCK();
211973e263b1SGleb Smirnoff 			if (algo == NULL) {
2120af6fef3aSGleb Smirnoff 				INP_WUNLOCK(inp);
212173e263b1SGleb Smirnoff 				error = EINVAL;
212273e263b1SGleb Smirnoff 				break;
212373e263b1SGleb Smirnoff 			}
2124dbc42409SLawrence Stewart 			/*
212573e263b1SGleb Smirnoff 			 * We hold a write lock over the tcb so it's safe to
212673e263b1SGleb Smirnoff 			 * do these things without ordering concerns.
2127dbc42409SLawrence Stewart 			 */
2128dbc42409SLawrence Stewart 			if (CC_ALGO(tp)->cb_destroy != NULL)
2129dbc42409SLawrence Stewart 				CC_ALGO(tp)->cb_destroy(tp->ccv);
213022699887SMatt Macy 			CC_DATA(tp) = NULL;
2131dbc42409SLawrence Stewart 			CC_ALGO(tp) = algo;
2132dbc42409SLawrence Stewart 			/*
213373e263b1SGleb Smirnoff 			 * If something goes pear shaped initialising the new
213473e263b1SGleb Smirnoff 			 * algo, fall back to newreno (which does not
213573e263b1SGleb Smirnoff 			 * require initialisation).
2136dbc42409SLawrence Stewart 			 */
213773e263b1SGleb Smirnoff 			if (algo->cb_init != NULL &&
213873e263b1SGleb Smirnoff 			    algo->cb_init(tp->ccv) != 0) {
2139dbc42409SLawrence Stewart 				CC_ALGO(tp) = &newreno_cc_algo;
2140dbc42409SLawrence Stewart 				/*
214173e263b1SGleb Smirnoff 				 * The only reason init should fail is
2142dbc42409SLawrence Stewart 				 * because of malloc.
2143dbc42409SLawrence Stewart 				 */
2144dbc42409SLawrence Stewart 				error = ENOMEM;
2145dbc42409SLawrence Stewart 			}
214673e263b1SGleb Smirnoff 			INP_WUNLOCK(inp);
214773e263b1SGleb Smirnoff 			break;
2148dbc42409SLawrence Stewart 
2149b2e60773SJohn Baldwin #ifdef KERN_TLS
2150b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2151b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2152ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2153b2e60773SJohn Baldwin 			if (error)
2154b2e60773SJohn Baldwin 				break;
2155b2e60773SJohn Baldwin 			error = ktls_enable_tx(so, &tls);
2156b2e60773SJohn Baldwin 			break;
2157b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2158b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2159b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2160b2e60773SJohn Baldwin 			if (error)
2161b2e60773SJohn Baldwin 				return (error);
2162b2e60773SJohn Baldwin 
2163b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2164b2e60773SJohn Baldwin 			error = ktls_set_tx_mode(so, ui);
2165b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2166b2e60773SJohn Baldwin 			break;
2167f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2168f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2169f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2170f1f93475SJohn Baldwin 			    sizeof(tls));
2171f1f93475SJohn Baldwin 			if (error)
2172f1f93475SJohn Baldwin 				break;
2173f1f93475SJohn Baldwin 			error = ktls_enable_rx(so, &tls);
2174f1f93475SJohn Baldwin 			break;
2175b2e60773SJohn Baldwin #endif
2176b2e60773SJohn Baldwin 
21779077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
21789077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
21799077f387SGleb Smirnoff 		case TCP_KEEPINIT:
21809077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
21819077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
21829077f387SGleb Smirnoff 			if (error)
21839077f387SGleb Smirnoff 				return (error);
21849077f387SGleb Smirnoff 
21859077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
21869077f387SGleb Smirnoff 				error = EINVAL;
21879077f387SGleb Smirnoff 				break;
21889077f387SGleb Smirnoff 			}
21899077f387SGleb Smirnoff 			ui *= hz;
21909077f387SGleb Smirnoff 
21919077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
21929077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
21939077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
21949077f387SGleb Smirnoff 				tp->t_keepidle = ui;
21959077f387SGleb Smirnoff 				/*
21969077f387SGleb Smirnoff 				 * XXX: better check current remaining
21979077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
21989077f387SGleb Smirnoff 				 */
21999077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
22009077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
22019077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
22029077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
22039077f387SGleb Smirnoff 				break;
22049077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
22059077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
22069077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
22079077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
22089077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
22099077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
22109077f387SGleb Smirnoff 				break;
22119077f387SGleb Smirnoff 			case TCP_KEEPINIT:
22129077f387SGleb Smirnoff 				tp->t_keepinit = ui;
22139077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
22149077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
22159077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
22169077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
22179077f387SGleb Smirnoff 				break;
22189077f387SGleb Smirnoff 			}
221909fe6320SNavdeep Parhar 			goto unlock_and_done;
22209077f387SGleb Smirnoff 
222185c05144SGleb Smirnoff 		case TCP_KEEPCNT:
222285c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
222385c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
222485c05144SGleb Smirnoff 			if (error)
222585c05144SGleb Smirnoff 				return (error);
222685c05144SGleb Smirnoff 
222785c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
222885c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
222985c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
223085c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
223185c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
223285c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
223385c05144SGleb Smirnoff 			goto unlock_and_done;
223485c05144SGleb Smirnoff 
223586a996e6SHiren Panchasara #ifdef TCPPCAP
223686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
223786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
223886a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
223986a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
224086a996e6SHiren Panchasara 			    sizeof optval);
224186a996e6SHiren Panchasara 			if (error)
224286a996e6SHiren Panchasara 				return (error);
224386a996e6SHiren Panchasara 
224486a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
224586a996e6SHiren Panchasara 			if (optval >= 0)
224686a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
224786a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
224886a996e6SHiren Panchasara 					optval);
224986a996e6SHiren Panchasara 			else
225086a996e6SHiren Panchasara 				error = EINVAL;
225186a996e6SHiren Panchasara 			goto unlock_and_done;
225286a996e6SHiren Panchasara #endif
225386a996e6SHiren Panchasara 
2254c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2255c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2256c560df6fSPatrick Kelsey 
2257281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2258c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2259c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2260281a0fd4SPatrick Kelsey 				return (EPERM);
2261281a0fd4SPatrick Kelsey 
2262c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2263c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2264281a0fd4SPatrick Kelsey 			if (error)
2265281a0fd4SPatrick Kelsey 				return (error);
2266281a0fd4SPatrick Kelsey 
2267281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2268d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2269d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2270d442a657SMichael Tuexen 				error = EINVAL;
2271d442a657SMichael Tuexen 				goto unlock_and_done;
2272d442a657SMichael Tuexen 			}
2273c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2274c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2275c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2276c560df6fSPatrick Kelsey 						error = EPERM;
2277c560df6fSPatrick Kelsey 						goto unlock_and_done;
2278c560df6fSPatrick Kelsey 					}
2279c560df6fSPatrick Kelsey 
2280c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2281281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2282281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2283c560df6fSPatrick Kelsey 				} else {
2284c560df6fSPatrick Kelsey 					/*
2285c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2286c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2287c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2288c560df6fSPatrick Kelsey 					 * connect.
2289c560df6fSPatrick Kelsey 					 */
2290c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2291c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2292c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2293c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2294c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2295c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2296c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2297c560df6fSPatrick Kelsey 					}
2298c560df6fSPatrick Kelsey 				}
2299d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2300281a0fd4SPatrick Kelsey 			} else
2301281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2302281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2303c560df6fSPatrick Kelsey 		}
2304281a0fd4SPatrick Kelsey 
2305e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
23062529f56eSJonathan T. Looney 		case TCP_LOG:
23072529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23082529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
23092529f56eSJonathan T. Looney 			    sizeof optval);
23102529f56eSJonathan T. Looney 			if (error)
23112529f56eSJonathan T. Looney 				return (error);
23122529f56eSJonathan T. Looney 
23132529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23142529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
23152529f56eSJonathan T. Looney 			goto unlock_and_done;
23162529f56eSJonathan T. Looney 
23172529f56eSJonathan T. Looney 		case TCP_LOGBUF:
23182529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23192529f56eSJonathan T. Looney 			error = EINVAL;
23202529f56eSJonathan T. Looney 			break;
23212529f56eSJonathan T. Looney 
23222529f56eSJonathan T. Looney 		case TCP_LOGID:
23232529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23242529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
23252529f56eSJonathan T. Looney 			if (error)
23262529f56eSJonathan T. Looney 				break;
23272529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23282529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23292529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
23302529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
23312529f56eSJonathan T. Looney 			break;
23322529f56eSJonathan T. Looney 
23332529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
23342529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
23352529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23362529f56eSJonathan T. Looney 			error =
23372529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
23382529f56eSJonathan T. Looney 			if (error)
23392529f56eSJonathan T. Looney 				break;
23402529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23412529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23422529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
23432529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
23442529f56eSJonathan T. Looney 				    M_WAITOK, true);
23452529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
23462529f56eSJonathan T. Looney 			} else {
23472529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
23482529f56eSJonathan T. Looney 				/*
23492529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
23502529f56eSJonathan T. Looney 				 * INP lock.
23512529f56eSJonathan T. Looney 				 */
23522529f56eSJonathan T. Looney 			}
23532529f56eSJonathan T. Looney 			break;
2354e24e5683SJonathan T. Looney #endif
23552529f56eSJonathan T. Looney 
2356df8bae1dSRodney W. Grimes 		default:
23578501a69cSRobert Watson 			INP_WUNLOCK(inp);
2358df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2359df8bae1dSRodney W. Grimes 			break;
2360df8bae1dSRodney W. Grimes 		}
2361df8bae1dSRodney W. Grimes 		break;
2362df8bae1dSRodney W. Grimes 
2363cfe8b629SGarrett Wollman 	case SOPT_GET:
23641e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2365cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2366fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
236788f6b043SBruce M Simpson 		case TCP_MD5SIG:
2368fcf59617SAndrey V. Elsukov 			if (!TCPMD5_ENABLED()) {
23698501a69cSRobert Watson 				INP_WUNLOCK(inp);
2370fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2371fcf59617SAndrey V. Elsukov 			}
2372fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
23731cfd4b53SBruce M Simpson 			break;
2374265ed012SBruce M Simpson #endif
23751e8f5ffaSRobert Watson 
2376df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2377cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
23788501a69cSRobert Watson 			INP_WUNLOCK(inp);
2379b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2380df8bae1dSRodney W. Grimes 			break;
2381df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2382cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
23838501a69cSRobert Watson 			INP_WUNLOCK(inp);
2384b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2385df8bae1dSRodney W. Grimes 			break;
2386a0292f23SGarrett Wollman 		case TCP_NOOPT:
2387cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
23888501a69cSRobert Watson 			INP_WUNLOCK(inp);
2389b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2390a0292f23SGarrett Wollman 			break;
2391a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2392cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
23938501a69cSRobert Watson 			INP_WUNLOCK(inp);
2394b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2395b8af5dfaSRobert Watson 			break;
2396b8af5dfaSRobert Watson 		case TCP_INFO:
2397b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
23988501a69cSRobert Watson 			INP_WUNLOCK(inp);
2399b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2400a0292f23SGarrett Wollman 			break;
2401adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2402adc56f5aSEdward Tomasz Napierala 			{
2403adc56f5aSEdward Tomasz Napierala #ifdef STATS
2404adc56f5aSEdward Tomasz Napierala 			int nheld;
2405adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2406adc56f5aSEdward Tomasz Napierala 
2407adc56f5aSEdward Tomasz Napierala 			error = 0;
2408adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2409adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2410adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2411adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2412adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2413adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2414adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2415adc56f5aSEdward Tomasz Napierala 			else
2416adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2417adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2418adc56f5aSEdward Tomasz Napierala 			if (error)
2419adc56f5aSEdward Tomasz Napierala 				break;
2420adc56f5aSEdward Tomasz Napierala 
2421adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2422adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2423adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2424adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2425adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2426adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2427adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2428adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2429adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2430adc56f5aSEdward Tomasz Napierala 				break;
2431adc56f5aSEdward Tomasz Napierala 			}
2432adc56f5aSEdward Tomasz Napierala 
2433adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2434adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2435adc56f5aSEdward Tomasz Napierala 				goto unhold;
2436adc56f5aSEdward Tomasz Napierala 
2437adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2438adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2439adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2440adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2441adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2442adc56f5aSEdward Tomasz Napierala unhold:
2443adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2444adc56f5aSEdward Tomasz Napierala #else
2445adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2446adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2447adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2448adc56f5aSEdward Tomasz Napierala 			break;
2449adc56f5aSEdward Tomasz Napierala 			}
2450dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2451af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2452dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2453af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2454dbc42409SLawrence Stewart 			break;
24552f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
24562f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
24572f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
24582f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
24592f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
24602f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
24615a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
24622f3eb7f4SGleb Smirnoff 				break;
24632f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
24645a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
24652f3eb7f4SGleb Smirnoff 				break;
24662f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
24675a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
24682f3eb7f4SGleb Smirnoff 				break;
24692f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
24705a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
24712f3eb7f4SGleb Smirnoff 				break;
24722f3eb7f4SGleb Smirnoff 			}
24732f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
24742f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
24752f3eb7f4SGleb Smirnoff 			break;
247686a996e6SHiren Panchasara #ifdef TCPPCAP
247786a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
247886a996e6SHiren Panchasara 		case TCP_PCAP_IN:
247986a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
248086a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
248186a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
248286a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
248386a996e6SHiren Panchasara 			break;
248486a996e6SHiren Panchasara #endif
2485281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2486281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2487281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2488281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2489281a0fd4SPatrick Kelsey 			break;
2490e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24912529f56eSJonathan T. Looney 		case TCP_LOG:
24922529f56eSJonathan T. Looney 			optval = tp->t_logstate;
24932529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24942529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
24952529f56eSJonathan T. Looney 			break;
24962529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24972529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
24982529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
24992529f56eSJonathan T. Looney 			break;
25002529f56eSJonathan T. Looney 		case TCP_LOGID:
25012529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
25022529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25032529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
25042529f56eSJonathan T. Looney 			break;
25052529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
25062529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
25072529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25082529f56eSJonathan T. Looney 			error = EINVAL;
25092529f56eSJonathan T. Looney 			break;
2510e24e5683SJonathan T. Looney #endif
2511b2e60773SJohn Baldwin #ifdef KERN_TLS
2512b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2513b2e60773SJohn Baldwin 			optval = ktls_get_tx_mode(so);
2514b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2515b2e60773SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2516b2e60773SJohn Baldwin 			break;
2517f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2518f1f93475SJohn Baldwin 			optval = ktls_get_rx_mode(so);
2519f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2520f1f93475SJohn Baldwin 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2521f1f93475SJohn Baldwin 			break;
2522b2e60773SJohn Baldwin #endif
2523df8bae1dSRodney W. Grimes 		default:
25248501a69cSRobert Watson 			INP_WUNLOCK(inp);
2525df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2526df8bae1dSRodney W. Grimes 			break;
2527df8bae1dSRodney W. Grimes 		}
2528df8bae1dSRodney W. Grimes 		break;
2529df8bae1dSRodney W. Grimes 	}
2530df8bae1dSRodney W. Grimes 	return (error);
2531df8bae1dSRodney W. Grimes }
25328501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2533bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2534df8bae1dSRodney W. Grimes 
253526e30fbbSDavid Greenman /*
2536df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2537df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2538df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2539df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2540df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2541df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2542df8bae1dSRodney W. Grimes  */
2543623dce13SRobert Watson static void
2544ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2545df8bae1dSRodney W. Grimes {
2546e6e0b5ffSRobert Watson 	struct inpcb *inp = tp->t_inpcb;
2547e6e0b5ffSRobert Watson 	struct socket *so = inp->inp_socket;
2548e6e0b5ffSRobert Watson 
254997a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
25508501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2551df8bae1dSRodney W. Grimes 
2552623dce13SRobert Watson 	/*
2553623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2554623dce13SRobert Watson 	 * socket is still open.
2555623dce13SRobert Watson 	 */
25568db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
25578db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2558df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2559623dce13SRobert Watson 		KASSERT(tp != NULL,
2560623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2561623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2562243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2563623dce13SRobert Watson 		KASSERT(tp != NULL,
2564623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2565623dce13SRobert Watson 	} else {
2566df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2567df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2568623dce13SRobert Watson 		tcp_usrclosed(tp);
2569ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
257055bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_output(tp);
2571df8bae1dSRodney W. Grimes 	}
2572df8bae1dSRodney W. Grimes }
2573df8bae1dSRodney W. Grimes 
2574df8bae1dSRodney W. Grimes /*
2575df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2576df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2577df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2578df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2579df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2580df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2581df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2582df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2583df8bae1dSRodney W. Grimes  */
2584623dce13SRobert Watson static void
2585ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2586df8bae1dSRodney W. Grimes {
2587df8bae1dSRodney W. Grimes 
258897a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
25898501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
2590e6e0b5ffSRobert Watson 
2591df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2592df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
259309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
259409fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
259509fe6320SNavdeep Parhar #endif
2596550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2597bc65987aSKip Macy 		/* FALLTHROUGH */
2598bc65987aSKip Macy 	case TCPS_CLOSED:
2599df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2600623dce13SRobert Watson 		/*
2601623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2602623dce13SRobert Watson 		 * still open.
2603623dce13SRobert Watson 		 */
2604623dce13SRobert Watson 		KASSERT(tp != NULL,
2605623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2606df8bae1dSRodney W. Grimes 		break;
2607df8bae1dSRodney W. Grimes 
2608a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2609df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2610a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2611a0292f23SGarrett Wollman 		break;
2612a0292f23SGarrett Wollman 
2613df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
261457f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2615df8bae1dSRodney W. Grimes 		break;
2616df8bae1dSRodney W. Grimes 
2617df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
261857f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2619df8bae1dSRodney W. Grimes 		break;
2620df8bae1dSRodney W. Grimes 	}
2621abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
2622df8bae1dSRodney W. Grimes 		soisdisconnected(tp->t_inpcb->inp_socket);
2623abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
26247c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
26257c72af87SMohan Srinivasan 			int timeout;
26267c72af87SMohan Srinivasan 
26277c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
26289077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2629b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2630b6239c4aSAndras Olah 		}
2631df8bae1dSRodney W. Grimes 	}
26327c72af87SMohan Srinivasan }
2633497057eeSRobert Watson 
2634497057eeSRobert Watson #ifdef DDB
2635497057eeSRobert Watson static void
2636497057eeSRobert Watson db_print_indent(int indent)
2637497057eeSRobert Watson {
2638497057eeSRobert Watson 	int i;
2639497057eeSRobert Watson 
2640497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2641497057eeSRobert Watson 		db_printf(" ");
2642497057eeSRobert Watson }
2643497057eeSRobert Watson 
2644497057eeSRobert Watson static void
2645497057eeSRobert Watson db_print_tstate(int t_state)
2646497057eeSRobert Watson {
2647497057eeSRobert Watson 
2648497057eeSRobert Watson 	switch (t_state) {
2649497057eeSRobert Watson 	case TCPS_CLOSED:
2650497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2651497057eeSRobert Watson 		return;
2652497057eeSRobert Watson 
2653497057eeSRobert Watson 	case TCPS_LISTEN:
2654497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2655497057eeSRobert Watson 		return;
2656497057eeSRobert Watson 
2657497057eeSRobert Watson 	case TCPS_SYN_SENT:
2658497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2659497057eeSRobert Watson 		return;
2660497057eeSRobert Watson 
2661497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2662497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2663497057eeSRobert Watson 		return;
2664497057eeSRobert Watson 
2665497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2666497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2667497057eeSRobert Watson 		return;
2668497057eeSRobert Watson 
2669497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2670497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2671497057eeSRobert Watson 		return;
2672497057eeSRobert Watson 
2673497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2674497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2675497057eeSRobert Watson 		return;
2676497057eeSRobert Watson 
2677497057eeSRobert Watson 	case TCPS_CLOSING:
2678497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2679497057eeSRobert Watson 		return;
2680497057eeSRobert Watson 
2681497057eeSRobert Watson 	case TCPS_LAST_ACK:
2682497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2683497057eeSRobert Watson 		return;
2684497057eeSRobert Watson 
2685497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2686497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2687497057eeSRobert Watson 		return;
2688497057eeSRobert Watson 
2689497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2690497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2691497057eeSRobert Watson 		return;
2692497057eeSRobert Watson 
2693497057eeSRobert Watson 	default:
2694497057eeSRobert Watson 		db_printf("unknown");
2695497057eeSRobert Watson 		return;
2696497057eeSRobert Watson 	}
2697497057eeSRobert Watson }
2698497057eeSRobert Watson 
2699497057eeSRobert Watson static void
2700497057eeSRobert Watson db_print_tflags(u_int t_flags)
2701497057eeSRobert Watson {
2702497057eeSRobert Watson 	int comma;
2703497057eeSRobert Watson 
2704497057eeSRobert Watson 	comma = 0;
2705497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2706497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2707497057eeSRobert Watson 		comma = 1;
2708497057eeSRobert Watson 	}
2709497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2710497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2711497057eeSRobert Watson 		comma = 1;
2712497057eeSRobert Watson 	}
2713497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2714497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2715497057eeSRobert Watson 		comma = 1;
2716497057eeSRobert Watson 	}
2717497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2718497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2719497057eeSRobert Watson 		comma = 1;
2720497057eeSRobert Watson 	}
2721497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2722497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2723497057eeSRobert Watson 		comma = 1;
2724497057eeSRobert Watson 	}
2725497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2726497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2727497057eeSRobert Watson 		comma = 1;
2728497057eeSRobert Watson 	}
2729497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2730497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2731497057eeSRobert Watson 		comma = 1;
2732497057eeSRobert Watson 	}
2733497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2734497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2735497057eeSRobert Watson 		comma = 1;
2736497057eeSRobert Watson 	}
2737497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2738497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2739497057eeSRobert Watson 		comma = 1;
2740497057eeSRobert Watson 	}
2741497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2742497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2743497057eeSRobert Watson 		comma = 1;
2744497057eeSRobert Watson 	}
2745497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2746497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2747497057eeSRobert Watson 		comma = 1;
2748497057eeSRobert Watson 	}
2749497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2750497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2751497057eeSRobert Watson 		comma = 1;
2752497057eeSRobert Watson 	}
2753497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2754497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2755497057eeSRobert Watson 		comma = 1;
2756497057eeSRobert Watson 	}
2757497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2758497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2759497057eeSRobert Watson 		comma = 1;
2760497057eeSRobert Watson 	}
2761497057eeSRobert Watson 	if (t_flags & TF_LQ_OVERFLOW) {
2762497057eeSRobert Watson 		db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
2763497057eeSRobert Watson 		comma = 1;
2764497057eeSRobert Watson 	}
2765497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2766497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2767497057eeSRobert Watson 		comma = 1;
2768497057eeSRobert Watson 	}
2769497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2770497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2771497057eeSRobert Watson 		comma = 1;
2772497057eeSRobert Watson 	}
2773497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2774497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2775497057eeSRobert Watson 		comma = 1;
2776497057eeSRobert Watson 	}
2777dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2778dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2779dbc42409SLawrence Stewart 		comma = 1;
2780dbc42409SLawrence Stewart 	}
2781497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2782497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2783497057eeSRobert Watson 		comma = 1;
2784497057eeSRobert Watson 	}
2785497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2786497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2787497057eeSRobert Watson 		comma = 1;
2788497057eeSRobert Watson 	}
2789497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2790497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2791497057eeSRobert Watson 		comma = 1;
2792497057eeSRobert Watson 	}
2793497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2794497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2795497057eeSRobert Watson 		comma = 1;
2796497057eeSRobert Watson 	}
2797281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2798281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2799281a0fd4SPatrick Kelsey 		comma = 1;
2800281a0fd4SPatrick Kelsey 	}
2801497057eeSRobert Watson }
2802497057eeSRobert Watson 
2803497057eeSRobert Watson static void
28043cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
28053cf38784SMichael Tuexen {
28063cf38784SMichael Tuexen 	int comma;
28073cf38784SMichael Tuexen 
28083cf38784SMichael Tuexen 	comma = 0;
28093cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
28103cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
28113cf38784SMichael Tuexen 		comma = 1;
28123cf38784SMichael Tuexen 	}
28133cf38784SMichael Tuexen }
28143cf38784SMichael Tuexen 
28153cf38784SMichael Tuexen 
28163cf38784SMichael Tuexen static void
2817497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2818497057eeSRobert Watson {
2819497057eeSRobert Watson 	int comma;
2820497057eeSRobert Watson 
2821497057eeSRobert Watson 	comma = 0;
2822497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2823497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2824497057eeSRobert Watson 		comma = 1;
2825497057eeSRobert Watson 	}
2826497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2827497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2828497057eeSRobert Watson 		comma = 1;
2829497057eeSRobert Watson 	}
2830497057eeSRobert Watson }
2831497057eeSRobert Watson 
2832497057eeSRobert Watson static void
2833497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2834497057eeSRobert Watson {
2835497057eeSRobert Watson 
2836497057eeSRobert Watson 	db_print_indent(indent);
2837497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2838497057eeSRobert Watson 
2839497057eeSRobert Watson 	indent += 2;
2840497057eeSRobert Watson 
2841497057eeSRobert Watson 	db_print_indent(indent);
2842497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2843c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2844497057eeSRobert Watson 
2845497057eeSRobert Watson 	db_print_indent(indent);
284685d94372SRobert Watson 	db_printf("tt_rexmt: %p   tt_persist: %p   tt_keep: %p\n",
2847e2f2059fSMike Silbersack 	    &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
2848497057eeSRobert Watson 
2849497057eeSRobert Watson 	db_print_indent(indent);
2850e2f2059fSMike Silbersack 	db_printf("tt_2msl: %p   tt_delack: %p   t_inpcb: %p\n", &tp->t_timers->tt_2msl,
2851e2f2059fSMike Silbersack 	    &tp->t_timers->tt_delack, tp->t_inpcb);
2852497057eeSRobert Watson 
2853497057eeSRobert Watson 	db_print_indent(indent);
2854497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2855497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2856497057eeSRobert Watson 	db_printf(")\n");
2857497057eeSRobert Watson 
2858497057eeSRobert Watson 	db_print_indent(indent);
2859497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2860497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2861497057eeSRobert Watson 	db_printf(")\n");
2862497057eeSRobert Watson 
2863497057eeSRobert Watson 	db_print_indent(indent);
28643cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
28653cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
28663cf38784SMichael Tuexen 	db_printf(")\n");
28673cf38784SMichael Tuexen 
28683cf38784SMichael Tuexen 	db_print_indent(indent);
2869497057eeSRobert Watson 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: x0%08x\n",
2870497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2871497057eeSRobert Watson 
2872497057eeSRobert Watson 	db_print_indent(indent);
2873497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2874497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2875497057eeSRobert Watson 
2876497057eeSRobert Watson 	db_print_indent(indent);
2877497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2878497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2879497057eeSRobert Watson 
2880497057eeSRobert Watson 	db_print_indent(indent);
28813ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2882497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2883497057eeSRobert Watson 
2884497057eeSRobert Watson 	db_print_indent(indent);
28853ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
28861c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2887497057eeSRobert Watson 
2888497057eeSRobert Watson 	db_print_indent(indent);
28893ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
28901c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
2891497057eeSRobert Watson 
2892497057eeSRobert Watson 	db_print_indent(indent);
28930c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
28940c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
2895497057eeSRobert Watson 
2896497057eeSRobert Watson 	db_print_indent(indent);
28971c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
28981c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
2899497057eeSRobert Watson 
2900497057eeSRobert Watson 	db_print_indent(indent);
29011c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
29021c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
2903497057eeSRobert Watson 
2904497057eeSRobert Watson 	db_print_indent(indent);
2905497057eeSRobert Watson 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u   "
2906497057eeSRobert Watson 	    "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
2907497057eeSRobert Watson 	    tp->t_rttbest);
2908497057eeSRobert Watson 
2909497057eeSRobert Watson 	db_print_indent(indent);
29103ac12506SJonathan T. Looney 	db_printf("t_rttupdated: %lu   max_sndwnd: %u   t_softerror: %d\n",
2911497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
2912497057eeSRobert Watson 
2913497057eeSRobert Watson 	db_print_indent(indent);
2914497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
2915497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
2916497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
2917497057eeSRobert Watson 
2918497057eeSRobert Watson 	db_print_indent(indent);
2919497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
2920497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
2921497057eeSRobert Watson 
2922497057eeSRobert Watson 	db_print_indent(indent);
29239f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
29241a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
2925497057eeSRobert Watson 
2926497057eeSRobert Watson 	db_print_indent(indent);
2927497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
29283ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
2929497057eeSRobert Watson 
2930497057eeSRobert Watson 	db_print_indent(indent);
29313ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
29329f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
2933497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
2934497057eeSRobert Watson 
2935497057eeSRobert Watson 	db_print_indent(indent);
29363529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
29373529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
2938497057eeSRobert Watson 
2939497057eeSRobert Watson 	db_print_indent(indent);
2940a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
2941a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
2942497057eeSRobert Watson 
2943497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
2944497057eeSRobert Watson 
2945497057eeSRobert Watson 	db_print_indent(indent);
2946497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
2947497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
2948497057eeSRobert Watson }
2949497057eeSRobert Watson 
2950497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
2951497057eeSRobert Watson {
2952497057eeSRobert Watson 	struct tcpcb *tp;
2953497057eeSRobert Watson 
2954497057eeSRobert Watson 	if (!have_addr) {
2955497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
2956497057eeSRobert Watson 		return;
2957497057eeSRobert Watson 	}
2958497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
2959497057eeSRobert Watson 
2960497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
2961497057eeSRobert Watson }
2962497057eeSRobert Watson #endif
2963