xref: /freebsd/sys/netinet/tcp_usrreq.c (revision abe8379b4f244aa12f13a603124eb6b41faabec5)
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  */
37df8bae1dSRodney W. Grimes 
384b421e2dSMike Silbersack #include <sys/cdefs.h>
39497057eeSRobert Watson #include "opt_ddb.h"
401cfd4b53SBruce M Simpson #include "opt_inet.h"
41fb59c426SYoshinobu Inoue #include "opt_inet6.h"
42fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
43b2e60773SJohn Baldwin #include "opt_kern_tls.h"
440cc12cc5SJoerg Wunsch 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
489077f387SGleb Smirnoff #include <sys/limits.h>
49f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5055bceb1eSRandall Stewart #include <sys/refcount.h>
51c7a82f90SGarrett Wollman #include <sys/kernel.h>
52b2e60773SJohn Baldwin #include <sys/ktls.h>
53adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5498163b98SPoul-Henning Kamp #include <sys/sysctl.h>
55df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
56fb59c426SYoshinobu Inoue #ifdef INET6
57fb59c426SYoshinobu Inoue #include <sys/domain.h>
58fb59c426SYoshinobu Inoue #endif /* INET6 */
59df8bae1dSRodney W. Grimes #include <sys/socket.h>
60df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
61df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6291421ba2SRobert Watson #include <sys/proc.h>
6391421ba2SRobert Watson #include <sys/jail.h>
64adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
65df8bae1dSRodney W. Grimes 
66497057eeSRobert Watson #ifdef DDB
67497057eeSRobert Watson #include <ddb/ddb.h>
68497057eeSRobert Watson #endif
69497057eeSRobert Watson 
70df8bae1dSRodney W. Grimes #include <net/if.h>
7176039bc8SGleb Smirnoff #include <net/if_var.h>
72df8bae1dSRodney W. Grimes #include <net/route.h>
73530c0060SRobert Watson #include <net/vnet.h>
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes #include <netinet/in.h>
765d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
77df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
78b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
79b5e8ce9fSBruce Evans #include <netinet/in_var.h>
803b0ee680SRichard Scheffenegger #include <netinet/ip.h>
81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
82fb59c426SYoshinobu Inoue #ifdef INET6
83b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
84b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
85fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
86a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
87fb59c426SYoshinobu Inoue #endif
882de3e790SGleb Smirnoff #include <netinet/tcp.h>
89df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
90df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
91df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
92df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
932529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
954644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
96c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
97fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
9886a996e6SHiren Panchasara #ifdef TCPPCAP
9986a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10086a996e6SHiren Panchasara #endif
10109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
102bc65987aSKip Macy #include <netinet/tcp_offload.h>
10309fe6320SNavdeep Parhar #endif
104fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
105df8bae1dSRodney W. Grimes 
106adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
107adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
108adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
109adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
110adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
111adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
112adc56f5aSEdward Tomasz Napierala 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
115df8bae1dSRodney W. Grimes  */
116b287c6c7SBjoern A. Zeeb #ifdef INET
117a9d22cceSGleb Smirnoff static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
1184d77a549SAlfred Perlstein 		    struct thread *td);
119b287c6c7SBjoern A. Zeeb #endif /* INET */
120fb59c426SYoshinobu Inoue #ifdef INET6
121a9d22cceSGleb Smirnoff static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
1224d77a549SAlfred Perlstein 		    struct thread *td);
123fb59c426SYoshinobu Inoue #endif /* INET6 */
124623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
125623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
1268c6104c4SMarius Strobl static void	tcp_fill_info(const struct tcpcb *, struct tcp_info *);
1272c37256eSGarrett Wollman 
128d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
129d3b6c96bSRandall Stewart 
13000812bbdSMichael Tuexen static void
13100812bbdSMichael Tuexen tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
13200812bbdSMichael Tuexen {
13300812bbdSMichael Tuexen 	struct tcp_log_buffer *lgb;
13400812bbdSMichael Tuexen 
135c2399dd2SMichael Tuexen 	KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
13600812bbdSMichael Tuexen 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13769c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
13869c7c811SRandall Stewart 		lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
13900812bbdSMichael Tuexen 		    0, NULL, false, NULL, NULL, 0, NULL);
14000812bbdSMichael Tuexen 	} else {
14100812bbdSMichael Tuexen 		lgb = NULL;
14200812bbdSMichael Tuexen 	}
14300812bbdSMichael Tuexen 	if (lgb != NULL) {
14400812bbdSMichael Tuexen 		if (error >= 0) {
14500812bbdSMichael Tuexen 			lgb->tlb_errno = (uint32_t)error;
14600812bbdSMichael Tuexen 		}
14700812bbdSMichael Tuexen 		lgb->tlb_flex1 = pru;
14800812bbdSMichael Tuexen 	}
14900812bbdSMichael Tuexen }
15000812bbdSMichael Tuexen 
1512c37256eSGarrett Wollman /*
1522c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1532c37256eSGarrett Wollman  * and an internet control block.
1542c37256eSGarrett Wollman  */
1552c37256eSGarrett Wollman static int
156b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1572c37256eSGarrett Wollman {
158f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
159623dce13SRobert Watson 	struct tcpcb *tp = NULL;
160623dce13SRobert Watson 	int error;
1612c37256eSGarrett Wollman 
162623dce13SRobert Watson 	inp = sotoinpcb(so);
163623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1642c37256eSGarrett Wollman 
1650f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1662c37256eSGarrett Wollman 	if (error)
1672c37256eSGarrett Wollman 		goto out;
1682c37256eSGarrett Wollman 
1690f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1700f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1710f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1727669c586SGleb Smirnoff 	if (error)
1730f6385e7SGleb Smirnoff 		goto out;
1740f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1750f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1760f6385e7SGleb Smirnoff 	if (tp == NULL) {
1777669c586SGleb Smirnoff 		error = ENOBUFS;
1780f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1790f6385e7SGleb Smirnoff 		goto out;
1800f6385e7SGleb Smirnoff 	}
1810f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
18200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ATTACH, error);
1830f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1840f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1852c37256eSGarrett Wollman out:
1865d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1870f6385e7SGleb Smirnoff 	return (error);
1882c37256eSGarrett Wollman }
1892c37256eSGarrett Wollman 
1902c37256eSGarrett Wollman /*
1913fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
192a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
193a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
194a152f8a3SRobert Watson  * inpcb state: time wait.
1952c37256eSGarrett Wollman  */
196bc725eafSRobert Watson static void
1973fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
1982c37256eSGarrett Wollman {
1993fed74e9SGleb Smirnoff 	struct inpcb *inp;
2002c37256eSGarrett Wollman 	struct tcpcb *tp;
2012c37256eSGarrett Wollman 
2023fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2033fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2043fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2053fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2063fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
207953b5606SRobert Watson 
208a152f8a3SRobert Watson 	tp = intotcpcb(inp);
209a152f8a3SRobert Watson 
2101b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2111b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2121b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2139c3507f9SGleb Smirnoff 
214623dce13SRobert Watson 	tcp_discardcb(tp);
2150206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
216623dce13SRobert Watson }
217c78cbc7bSRobert Watson 
218b287c6c7SBjoern A. Zeeb #ifdef INET
2192c37256eSGarrett Wollman /*
2202c37256eSGarrett Wollman  * Give the socket an address.
2212c37256eSGarrett Wollman  */
2222c37256eSGarrett Wollman static int
223b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2242c37256eSGarrett Wollman {
2252c37256eSGarrett Wollman 	int error = 0;
226f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
227c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2282c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2292c37256eSGarrett Wollman 
230c2399dd2SMichael Tuexen 	inp = sotoinpcb(so);
231c2399dd2SMichael Tuexen 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
232c2399dd2SMichael Tuexen 	INP_WLOCK(inp);
233c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
234c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
235c2399dd2SMichael Tuexen 		return (EINVAL);
236c2399dd2SMichael Tuexen 	}
237c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
238c2399dd2SMichael Tuexen 
23952710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
240f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
241f96603b5SMark Johnston 		/*
242f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
243f96603b5SMark Johnston 		 */
244f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2453f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
246c2399dd2SMichael Tuexen 		    sinp->sin_addr.s_addr != INADDR_ANY) {
247c2399dd2SMichael Tuexen 			error = EAFNOSUPPORT;
248c2399dd2SMichael Tuexen 			goto out;
249c2399dd2SMichael Tuexen 		}
250f96603b5SMark Johnston 		nam->sa_family = AF_INET;
251f96603b5SMark Johnston 	}
252c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sinp)) {
253c2399dd2SMichael Tuexen 		error = EINVAL;
254c2399dd2SMichael Tuexen 		goto out;
255c2399dd2SMichael Tuexen 	}
2562c37256eSGarrett Wollman 	/*
2572c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2582c37256eSGarrett Wollman 	 * to them.
2592c37256eSGarrett Wollman 	 */
260c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
261c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
2622c37256eSGarrett Wollman 		goto out;
263623dce13SRobert Watson 	}
264fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
26596871af0SGleb Smirnoff 	error = in_pcbbind(inp, sinp, td->td_ucred);
266fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
267623dce13SRobert Watson out:
26800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
2695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2708501a69cSRobert Watson 	INP_WUNLOCK(inp);
271623dce13SRobert Watson 
272623dce13SRobert Watson 	return (error);
2732c37256eSGarrett Wollman }
274b287c6c7SBjoern A. Zeeb #endif /* INET */
2752c37256eSGarrett Wollman 
276fb59c426SYoshinobu Inoue #ifdef INET6
277fb59c426SYoshinobu Inoue static int
278b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
279fb59c426SYoshinobu Inoue {
280fb59c426SYoshinobu Inoue 	int error = 0;
281f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
282c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2830ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2844a91aa8fSMichael Tuexen 	u_char vflagsav;
285fb59c426SYoshinobu Inoue 
286623dce13SRobert Watson 	inp = sotoinpcb(so);
287623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2888501a69cSRobert Watson 	INP_WLOCK(inp);
28953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
290c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
291c2399dd2SMichael Tuexen 		return (EINVAL);
292c2399dd2SMichael Tuexen 	}
293c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
294c2399dd2SMichael Tuexen 
295c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
296c2399dd2SMichael Tuexen 
297c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
298c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
299c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
300c2399dd2SMichael Tuexen 		goto out;
301c2399dd2SMichael Tuexen 	}
302c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sin6)) {
303623dce13SRobert Watson 		error = EINVAL;
304623dce13SRobert Watson 		goto out;
305623dce13SRobert Watson 	}
306c2399dd2SMichael Tuexen 	/*
307c2399dd2SMichael Tuexen 	 * Must check for multicast addresses and disallow binding
308c2399dd2SMichael Tuexen 	 * to them.
309c2399dd2SMichael Tuexen 	 */
310c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
311c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
312c2399dd2SMichael Tuexen 		goto out;
313c2399dd2SMichael Tuexen 	}
314c2399dd2SMichael Tuexen 
315fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
316fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
317fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
318b287c6c7SBjoern A. Zeeb #ifdef INET
31966ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3200ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
321fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3220ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
323fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
324fb59c426SYoshinobu Inoue 
3250ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
326888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
327888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
328888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
329888973f5SMichael Tuexen 				goto out;
330888973f5SMichael Tuexen 			}
331fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
332fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
33396871af0SGleb Smirnoff 			error = in_pcbbind(inp, &sin, td->td_ucred);
334fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
335fb59c426SYoshinobu Inoue 			goto out;
336fb59c426SYoshinobu Inoue 		}
337fb59c426SYoshinobu Inoue 	}
338b287c6c7SBjoern A. Zeeb #endif
33996871af0SGleb Smirnoff 	error = in6_pcbbind(inp, sin6, td->td_ucred);
340fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
341623dce13SRobert Watson out:
3424a91aa8fSMichael Tuexen 	if (error != 0)
3434a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
34400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
3455d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3468501a69cSRobert Watson 	INP_WUNLOCK(inp);
347623dce13SRobert Watson 	return (error);
348fb59c426SYoshinobu Inoue }
349fb59c426SYoshinobu Inoue #endif /* INET6 */
350fb59c426SYoshinobu Inoue 
351b287c6c7SBjoern A. Zeeb #ifdef INET
3522c37256eSGarrett Wollman /*
3532c37256eSGarrett Wollman  * Prepare to accept connections.
3542c37256eSGarrett Wollman  */
3552c37256eSGarrett Wollman static int
356d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3572c37256eSGarrett Wollman {
3582c37256eSGarrett Wollman 	int error = 0;
359f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
360c2399dd2SMichael Tuexen 	struct tcpcb *tp;
3612c37256eSGarrett Wollman 
362623dce13SRobert Watson 	inp = sotoinpcb(so);
363623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3648501a69cSRobert Watson 	INP_WLOCK(inp);
36553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
366c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
367c2399dd2SMichael Tuexen 		return (EINVAL);
368623dce13SRobert Watson 	}
369623dce13SRobert Watson 	tp = intotcpcb(inp);
370c2399dd2SMichael Tuexen 
3710daccb9cSRobert Watson 	SOCK_LOCK(so);
3720daccb9cSRobert Watson 	error = solisten_proto_check(so);
373bd4a39ccSMark Johnston 	if (error != 0) {
374bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
375bd4a39ccSMark Johnston 		goto out;
376bd4a39ccSMark Johnston 	}
377bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
378fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
379bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
380fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
381bd4a39ccSMark Johnston 	}
3820daccb9cSRobert Watson 	if (error == 0) {
38357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
384d374e81eSRobert Watson 		solisten_proto(so, backlog);
38509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
38637cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
38709fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
38809fe6320SNavdeep Parhar #endif
389bd4a39ccSMark Johnston 	} else {
390bd4a39ccSMark Johnston 		solisten_proto_abort(so);
3910daccb9cSRobert Watson 	}
3920daccb9cSRobert Watson 	SOCK_UNLOCK(so);
393623dce13SRobert Watson 
39468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
395281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
39618a75309SPatrick Kelsey 
397623dce13SRobert Watson out:
39800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
3995d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4008501a69cSRobert Watson 	INP_WUNLOCK(inp);
401623dce13SRobert Watson 	return (error);
4022c37256eSGarrett Wollman }
403b287c6c7SBjoern A. Zeeb #endif /* INET */
4042c37256eSGarrett Wollman 
405fb59c426SYoshinobu Inoue #ifdef INET6
406fb59c426SYoshinobu Inoue static int
407d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
408fb59c426SYoshinobu Inoue {
409fb59c426SYoshinobu Inoue 	int error = 0;
410f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
411c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4124a91aa8fSMichael Tuexen 	u_char vflagsav;
413fb59c426SYoshinobu Inoue 
414623dce13SRobert Watson 	inp = sotoinpcb(so);
415623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4168501a69cSRobert Watson 	INP_WLOCK(inp);
41753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
418c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
419c2399dd2SMichael Tuexen 		return (EINVAL);
420623dce13SRobert Watson 	}
421623dce13SRobert Watson 	tp = intotcpcb(inp);
422c2399dd2SMichael Tuexen 
423c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
424c2399dd2SMichael Tuexen 
4250daccb9cSRobert Watson 	SOCK_LOCK(so);
4260daccb9cSRobert Watson 	error = solisten_proto_check(so);
427bd4a39ccSMark Johnston 	if (error != 0) {
428bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
429bd4a39ccSMark Johnston 		goto out;
430bd4a39ccSMark Johnston 	}
431fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
432bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
433fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
43466ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
435fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
436bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
437fb59c426SYoshinobu Inoue 	}
438fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4390daccb9cSRobert Watson 	if (error == 0) {
44057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
441d374e81eSRobert Watson 		solisten_proto(so, backlog);
44209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44337cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
44409fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
44509fe6320SNavdeep Parhar #endif
446bd4a39ccSMark Johnston 	} else {
447bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4480daccb9cSRobert Watson 	}
4490daccb9cSRobert Watson 	SOCK_UNLOCK(so);
450623dce13SRobert Watson 
45168bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
452281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45318a75309SPatrick Kelsey 
4544a91aa8fSMichael Tuexen 	if (error != 0)
4554a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4564a91aa8fSMichael Tuexen 
457623dce13SRobert Watson out:
45800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4595d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4608501a69cSRobert Watson 	INP_WUNLOCK(inp);
461623dce13SRobert Watson 	return (error);
462fb59c426SYoshinobu Inoue }
463fb59c426SYoshinobu Inoue #endif /* INET6 */
464fb59c426SYoshinobu Inoue 
465b287c6c7SBjoern A. Zeeb #ifdef INET
4662c37256eSGarrett Wollman /*
4672c37256eSGarrett Wollman  * Initiate connection to peer.
4682c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4692c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4702c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4712c37256eSGarrett Wollman  * Send initial segment on connection.
4722c37256eSGarrett Wollman  */
4732c37256eSGarrett Wollman static int
474b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4752c37256eSGarrett Wollman {
476109eb549SGleb Smirnoff 	struct epoch_tracker et;
4772c37256eSGarrett Wollman 	int error = 0;
478f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
479c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4802c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4812c37256eSGarrett Wollman 
482623dce13SRobert Watson 	inp = sotoinpcb(so);
483623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4848501a69cSRobert Watson 	INP_WLOCK(inp);
485eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
486c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
487c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
488c2399dd2SMichael Tuexen 	}
489c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
490c2399dd2SMichael Tuexen 
491c2399dd2SMichael Tuexen 	sinp = (struct sockaddr_in *)nam;
492c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET) {
493c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
494623dce13SRobert Watson 		goto out;
495623dce13SRobert Watson 	}
496c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sinp)) {
497c2399dd2SMichael Tuexen 		error = EINVAL;
498c2399dd2SMichael Tuexen 		goto out;
499c2399dd2SMichael Tuexen 	}
500c2399dd2SMichael Tuexen 	/*
501c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
502c2399dd2SMichael Tuexen 	 */
503c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
504c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
505c2399dd2SMichael Tuexen 		goto out;
506c2399dd2SMichael Tuexen 	}
507c2399dd2SMichael Tuexen 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
508c2399dd2SMichael Tuexen 		error = EACCES;
509c2399dd2SMichael Tuexen 		goto out;
510c2399dd2SMichael Tuexen 	}
511c2399dd2SMichael Tuexen 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
512c2399dd2SMichael Tuexen 		goto out;
513bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
514bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
515bd4a39ccSMark Johnston 		goto out;
516bd4a39ccSMark Johnston 	}
517c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
518a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
519c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
52109fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
52237cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
52309fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
524c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52509fe6320SNavdeep Parhar #endif
52609fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
52740fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5281d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5291d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
530c1604fe4SGleb Smirnoff out_in_epoch:
531109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
532623dce13SRobert Watson out:
53300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
534e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5358501a69cSRobert Watson 	INP_WUNLOCK(inp);
536623dce13SRobert Watson 	return (error);
5372c37256eSGarrett Wollman }
538b287c6c7SBjoern A. Zeeb #endif /* INET */
5392c37256eSGarrett Wollman 
540fb59c426SYoshinobu Inoue #ifdef INET6
541fb59c426SYoshinobu Inoue static int
542b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
543fb59c426SYoshinobu Inoue {
544109eb549SGleb Smirnoff 	struct epoch_tracker et;
545fb59c426SYoshinobu Inoue 	int error = 0;
546f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
547c2399dd2SMichael Tuexen 	struct tcpcb *tp;
5480ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5494a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5504a91aa8fSMichael Tuexen 	u_char vflagsav;
551623dce13SRobert Watson 
552623dce13SRobert Watson 	inp = sotoinpcb(so);
553623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5548501a69cSRobert Watson 	INP_WLOCK(inp);
555c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
556c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
557c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
558c2399dd2SMichael Tuexen 	}
559c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
560c2399dd2SMichael Tuexen 
5614a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5624a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
563c2399dd2SMichael Tuexen 
564c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
565c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
566c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
567c2399dd2SMichael Tuexen 		goto out;
568c2399dd2SMichael Tuexen 	}
569c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sin6)) {
570c2399dd2SMichael Tuexen 		error = EINVAL;
571c2399dd2SMichael Tuexen 		goto out;
572c2399dd2SMichael Tuexen 	}
573c2399dd2SMichael Tuexen 	/*
574c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
575c2399dd2SMichael Tuexen 	 */
576c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
577c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
578623dce13SRobert Watson 		goto out;
579623dce13SRobert Watson 	}
580bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
581bd4a39ccSMark Johnston 		error = EINVAL;
582bd4a39ccSMark Johnston 		goto out;
583bd4a39ccSMark Johnston 	}
584b287c6c7SBjoern A. Zeeb #ifdef INET
585fa046d87SRobert Watson 	/*
586fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
587fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
588fa046d87SRobert Watson 	 * Is this a significant problem?
589fa046d87SRobert Watson 	 */
5900ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
591fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
592fb59c426SYoshinobu Inoue 
593d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
594d46a5312SMaxim Konovalov 			error = EINVAL;
595d46a5312SMaxim Konovalov 			goto out;
596d46a5312SMaxim Konovalov 		}
5975dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
5985dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
5995dba6adaSMichael Tuexen 			goto out;
6005dba6adaSMichael Tuexen 		}
60133841545SHajimu UMEMOTO 
6020ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
603888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
604888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
605888973f5SMichael Tuexen 			goto out;
606888973f5SMichael Tuexen 		}
607f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
608f903a308SMichael Tuexen 			error = EACCES;
6092cf21ae5SRandall Stewart 			goto out;
6102cf21ae5SRandall Stewart 		}
611b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
612b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
613413628a7SBjoern A. Zeeb 			goto out;
6144a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6154a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
616c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
617a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
618c1604fe4SGleb Smirnoff 			goto out_in_epoch;
61909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62009fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
621adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62209fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
623c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62409fe6320SNavdeep Parhar #endif
62540fa3e40SGleb Smirnoff 		error = tcp_output(tp);
626c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6275dba6adaSMichael Tuexen 	} else {
6285dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6295dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6305dba6adaSMichael Tuexen 			goto out;
6315dba6adaSMichael Tuexen 		}
632fb59c426SYoshinobu Inoue 	}
633b287c6c7SBjoern A. Zeeb #endif
6344a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6354a91aa8fSMichael Tuexen 		goto out;
636fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
637fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
638dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6390773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
640a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6410773b44eSGleb Smirnoff 		goto out_in_epoch;
64209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64309fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
644adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
64509fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6460773b44eSGleb Smirnoff 		goto out_in_epoch;
64709fe6320SNavdeep Parhar #endif
64809fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
64940fa3e40SGleb Smirnoff 	error = tcp_output(tp);
650c1604fe4SGleb Smirnoff out_in_epoch:
651109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
652623dce13SRobert Watson out:
6531d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6541d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6554a91aa8fSMichael Tuexen 	/*
6564a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6574a91aa8fSMichael Tuexen 	 * the flags we modified.
6584a91aa8fSMichael Tuexen 	 */
6594a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6604a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6614a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6624a91aa8fSMichael Tuexen 	}
6634a91aa8fSMichael Tuexen 
66400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6655d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6668501a69cSRobert Watson 	INP_WUNLOCK(inp);
667623dce13SRobert Watson 	return (error);
668fb59c426SYoshinobu Inoue }
669fb59c426SYoshinobu Inoue #endif /* INET6 */
670fb59c426SYoshinobu Inoue 
6712c37256eSGarrett Wollman /*
6722c37256eSGarrett Wollman  * Initiate disconnect from peer.
6732c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6742c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6752c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6762c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6772c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6782c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6792c37256eSGarrett Wollman  *
6802c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6812c37256eSGarrett Wollman  */
6822c37256eSGarrett Wollman static int
6832c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6842c37256eSGarrett Wollman {
685f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
686623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6876573d758SMatt Macy 	struct epoch_tracker et;
688623dce13SRobert Watson 	int error = 0;
6892c37256eSGarrett Wollman 
69097a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
691623dce13SRobert Watson 	inp = sotoinpcb(so);
692623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6938501a69cSRobert Watson 	INP_WLOCK(inp);
694489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
695c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
696c2399dd2SMichael Tuexen 		NET_EPOCH_EXIT(et);
697c2399dd2SMichael Tuexen 		return (ECONNRESET);
698623dce13SRobert Watson 	}
699623dce13SRobert Watson 	tp = intotcpcb(inp);
700c2399dd2SMichael Tuexen 
701cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
702cda6bdbaSJohn Baldwin 		goto out;
703623dce13SRobert Watson 	tcp_disconnect(tp);
704623dce13SRobert Watson out:
70500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_DISCONNECT, error);
7065d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7078501a69cSRobert Watson 	INP_WUNLOCK(inp);
70897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
709623dce13SRobert Watson 	return (error);
7102c37256eSGarrett Wollman }
7112c37256eSGarrett Wollman 
712b287c6c7SBjoern A. Zeeb #ifdef INET
7132c37256eSGarrett Wollman /*
7148296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7158296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7162c37256eSGarrett Wollman  */
7172c37256eSGarrett Wollman static int
718cfb1e929SGleb Smirnoff tcp_usr_accept(struct socket *so, struct sockaddr *sa)
7192c37256eSGarrett Wollman {
720c2399dd2SMichael Tuexen 	struct inpcb *inp;
721c2399dd2SMichael Tuexen 	struct tcpcb *tp;
722cfb1e929SGleb Smirnoff 	int error = 0;
7232c37256eSGarrett Wollman 
724f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
725623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7268501a69cSRobert Watson 	INP_WLOCK(inp);
72753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
728c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
729c2399dd2SMichael Tuexen 		return (ECONNABORTED);
730623dce13SRobert Watson 	}
7311db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
732f76fcf6dSJeffrey Hsu 
733cfb1e929SGleb Smirnoff 	if (so->so_state & SS_ISDISCONNECTED)
734c2399dd2SMichael Tuexen 		error = ECONNABORTED;
735cfb1e929SGleb Smirnoff 	else
736cfb1e929SGleb Smirnoff 		*(struct sockaddr_in *)sa = (struct sockaddr_in ){
737cfb1e929SGleb Smirnoff 			.sin_family = AF_INET,
738cfb1e929SGleb Smirnoff 			.sin_len = sizeof(struct sockaddr_in),
739cfb1e929SGleb Smirnoff 			.sin_port = inp->inp_fport,
740cfb1e929SGleb Smirnoff 			.sin_addr = inp->inp_faddr,
741cfb1e929SGleb Smirnoff 		};
74200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7435d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7448501a69cSRobert Watson 	INP_WUNLOCK(inp);
745cfb1e929SGleb Smirnoff 
746cfb1e929SGleb Smirnoff 	return (error);
7472c37256eSGarrett Wollman }
748b287c6c7SBjoern A. Zeeb #endif /* INET */
7492c37256eSGarrett Wollman 
750fb59c426SYoshinobu Inoue #ifdef INET6
751fb59c426SYoshinobu Inoue static int
752cfb1e929SGleb Smirnoff tcp6_usr_accept(struct socket *so, struct sockaddr *sa)
753fb59c426SYoshinobu Inoue {
754c2399dd2SMichael Tuexen 	struct inpcb *inp;
755c2399dd2SMichael Tuexen 	struct tcpcb *tp;
756cfb1e929SGleb Smirnoff 	int error = 0;
757fb59c426SYoshinobu Inoue 
758f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
759623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
7608501a69cSRobert Watson 	INP_WLOCK(inp);
76153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
762c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
763c2399dd2SMichael Tuexen 		return (ECONNABORTED);
764623dce13SRobert Watson 	}
7651db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
766623dce13SRobert Watson 
767c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
768c2399dd2SMichael Tuexen 		error = ECONNABORTED;
76926ef6ac4SDon Lewis 	} else {
770cfb1e929SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV4) {
771cfb1e929SGleb Smirnoff 			struct sockaddr_in sin = {
772cfb1e929SGleb Smirnoff 				.sin_family = AF_INET,
773cfb1e929SGleb Smirnoff 				.sin_len = sizeof(struct sockaddr_in),
774cfb1e929SGleb Smirnoff 				.sin_port = inp->inp_fport,
775cfb1e929SGleb Smirnoff 				.sin_addr = inp->inp_faddr,
776cfb1e929SGleb Smirnoff 			};
777cfb1e929SGleb Smirnoff 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
778cfb1e929SGleb Smirnoff 		} else {
779cfb1e929SGleb Smirnoff 			*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
780cfb1e929SGleb Smirnoff 				.sin6_family = AF_INET6,
781cfb1e929SGleb Smirnoff 				.sin6_len = sizeof(struct sockaddr_in6),
782cfb1e929SGleb Smirnoff 				.sin6_port = inp->inp_fport,
783cfb1e929SGleb Smirnoff 				.sin6_addr = inp->in6p_faddr,
784cfb1e929SGleb Smirnoff 			};
785cfb1e929SGleb Smirnoff 			/* XXX: should catch errors */
786cfb1e929SGleb Smirnoff 			(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
787cfb1e929SGleb Smirnoff 		}
78826ef6ac4SDon Lewis 	}
78926ef6ac4SDon Lewis 
79000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7915d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7928501a69cSRobert Watson 	INP_WUNLOCK(inp);
793cfb1e929SGleb Smirnoff 
794cfb1e929SGleb Smirnoff 	return (error);
795fb59c426SYoshinobu Inoue }
796fb59c426SYoshinobu Inoue #endif /* INET6 */
797f76fcf6dSJeffrey Hsu 
798f76fcf6dSJeffrey Hsu /*
7992c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8002c37256eSGarrett Wollman  */
8012c37256eSGarrett Wollman static int
8025bba2728SGleb Smirnoff tcp_usr_shutdown(struct socket *so, enum shutdown_how how)
8032c37256eSGarrett Wollman {
8046573d758SMatt Macy 	struct epoch_tracker et;
8055bba2728SGleb Smirnoff 	struct inpcb *inp = sotoinpcb(so);
8065bba2728SGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
8075bba2728SGleb Smirnoff 	int error = 0;
8082c37256eSGarrett Wollman 
8095bba2728SGleb Smirnoff 	SOCK_LOCK(so);
8105bba2728SGleb Smirnoff 	if (SOLISTENING(so)) {
8115bba2728SGleb Smirnoff 		if (how != SHUT_WR) {
8125bba2728SGleb Smirnoff 			so->so_error = ECONNABORTED;
8135bba2728SGleb Smirnoff 			solisten_wakeup(so);	/* unlocks so */
8145bba2728SGleb Smirnoff 		} else
8155bba2728SGleb Smirnoff 			SOCK_UNLOCK(so);
816*abe8379bSGleb Smirnoff 		return (ENOTCONN);
817*abe8379bSGleb Smirnoff 	} else if ((so->so_state &
818*abe8379bSGleb Smirnoff 	    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
819*abe8379bSGleb Smirnoff 		SOCK_UNLOCK(so);
820*abe8379bSGleb Smirnoff 		return (ENOTCONN);
8215bba2728SGleb Smirnoff 	}
8225bba2728SGleb Smirnoff 	SOCK_UNLOCK(so);
8235bba2728SGleb Smirnoff 
8245bba2728SGleb Smirnoff 	switch (how) {
8255bba2728SGleb Smirnoff 	case SHUT_RD:
826ce69e373SGleb Smirnoff 		sorflush(so);
8275bba2728SGleb Smirnoff 		break;
8285bba2728SGleb Smirnoff 	case SHUT_RDWR:
829ce69e373SGleb Smirnoff 		sorflush(so);
8305bba2728SGleb Smirnoff 		/* FALLTHROUGH */
8315bba2728SGleb Smirnoff 	case SHUT_WR:
8325bba2728SGleb Smirnoff 		/*
8335bba2728SGleb Smirnoff 		 * XXXGL: mimicing old soshutdown() here. But shouldn't we
8345bba2728SGleb Smirnoff 		 * return ECONNRESEST for SHUT_RD as well?
8355bba2728SGleb Smirnoff 		 */
8368501a69cSRobert Watson 		INP_WLOCK(inp);
83753af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
8380af4ce45SGleb Smirnoff 			INP_WUNLOCK(inp);
8390af4ce45SGleb Smirnoff 			return (ECONNRESET);
840623dce13SRobert Watson 		}
841c2399dd2SMichael Tuexen 
8422c37256eSGarrett Wollman 		socantsendmore(so);
8435bba2728SGleb Smirnoff 		NET_EPOCH_ENTER(et);
844623dce13SRobert Watson 		tcp_usrclosed(tp);
845f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
84600812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8475d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
848f64dc2abSGleb Smirnoff 		error = tcp_unlock_or_drop(tp, error);
84997a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
8505bba2728SGleb Smirnoff 	}
8515bba2728SGleb Smirnoff 	wakeup(&so->so_timeo);
852623dce13SRobert Watson 
853623dce13SRobert Watson 	return (error);
8542c37256eSGarrett Wollman }
8552c37256eSGarrett Wollman 
8562c37256eSGarrett Wollman /*
8572c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8582c37256eSGarrett Wollman  */
8592c37256eSGarrett Wollman static int
8602c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8612c37256eSGarrett Wollman {
862109eb549SGleb Smirnoff 	struct epoch_tracker et;
863f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
864c2399dd2SMichael Tuexen 	struct tcpcb *tp;
865f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8662c37256eSGarrett Wollman 
867623dce13SRobert Watson 	inp = sotoinpcb(so);
868623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8698501a69cSRobert Watson 	INP_WLOCK(inp);
87053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
87137a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
87237a7f557SGleb Smirnoff 		return (ECONNRESET);
873623dce13SRobert Watson 	}
87437a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
875c2399dd2SMichael Tuexen 
87637a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
877281a0fd4SPatrick Kelsey 	/*
878281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
879281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
880281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
881281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
882281a0fd4SPatrick Kelsey 	 * expires.
883281a0fd4SPatrick Kelsey 	 */
88468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
885281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
886281a0fd4SPatrick Kelsey 		goto out;
88709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
88809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
88909fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
890460cf046SNavdeep Parhar 	else
89109fe6320SNavdeep Parhar #endif
892f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
893623dce13SRobert Watson out:
89400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
8955d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
896f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
897f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
898623dce13SRobert Watson 	return (error);
8992c37256eSGarrett Wollman }
9002c37256eSGarrett Wollman 
9012c37256eSGarrett Wollman /*
9022c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9039c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9049c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9059c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9069c9906e9SPeter Wemm  * generally are caller-frees.
9072c37256eSGarrett Wollman  */
9082c37256eSGarrett Wollman static int
90957bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
910b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9112c37256eSGarrett Wollman {
91297a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9132c37256eSGarrett Wollman 	int error = 0;
914f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
915c2399dd2SMichael Tuexen 	struct tcpcb *tp;
916888973f5SMichael Tuexen #ifdef INET
91751e08d53SMichael Tuexen #ifdef INET6
91851e08d53SMichael Tuexen 	struct sockaddr_in sin;
91951e08d53SMichael Tuexen #endif
92051e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
921888973f5SMichael Tuexen #endif
922fb59c426SYoshinobu Inoue #ifdef INET6
923a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
924fb59c426SYoshinobu Inoue 	int isipv6;
925fb59c426SYoshinobu Inoue #endif
9264a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9274a91aa8fSMichael Tuexen 	u_char vflagsav;
9284a91aa8fSMichael Tuexen 	bool restoreflags;
9292c37256eSGarrett Wollman 
9304287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9314287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9324287aa56SGleb Smirnoff 	INP_WLOCK(inp);
93353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9344287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9354287aa56SGleb Smirnoff 			m_freem(m);
9364287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9374287aa56SGleb Smirnoff 		return (ECONNRESET);
9384287aa56SGleb Smirnoff 	}
939c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
9404287aa56SGleb Smirnoff 
9414287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9424287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9434287aa56SGleb Smirnoff 	restoreflags = false;
9444287aa56SGleb Smirnoff 
9454287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
946c2399dd2SMichael Tuexen 	if (control != NULL) {
947c2399dd2SMichael Tuexen 		/* TCP doesn't do control messages (rights, creds, etc) */
948c2399dd2SMichael Tuexen 		if (control->m_len > 0) {
949c2399dd2SMichael Tuexen 			m_freem(control);
950c2399dd2SMichael Tuexen 			error = EINVAL;
951c2399dd2SMichael Tuexen 			goto out;
952c2399dd2SMichael Tuexen 		}
953c2399dd2SMichael Tuexen 		m_freem(control);	/* empty control, just free it */
954c2399dd2SMichael Tuexen 	}
955c2399dd2SMichael Tuexen 
9567d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9577d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
958d3b6c96bSRandall Stewart 		goto out;
9597d2608a5SMark Johnston 
960888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
961bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
962bd4a39ccSMark Johnston 			error = EINVAL;
963bd4a39ccSMark Johnston 			goto out;
964bd4a39ccSMark Johnston 		}
965888973f5SMichael Tuexen 		switch (nam->sa_family) {
966888973f5SMichael Tuexen #ifdef INET
967888973f5SMichael Tuexen 		case AF_INET:
968888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
969888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
970888973f5SMichael Tuexen 				error = EINVAL;
971888973f5SMichael Tuexen 				goto out;
972888973f5SMichael Tuexen 			}
973888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
974888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
975888973f5SMichael Tuexen 				goto out;
976888973f5SMichael Tuexen 			}
977888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
978888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
979888973f5SMichael Tuexen 				goto out;
980888973f5SMichael Tuexen 			}
981f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
982f903a308SMichael Tuexen 				error = EACCES;
9832cf21ae5SRandall Stewart 				goto out;
9842cf21ae5SRandall Stewart 			}
985888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9867d2608a5SMark Johnston 			    &sinp->sin_addr)))
987888973f5SMichael Tuexen 				goto out;
988888973f5SMichael Tuexen #ifdef INET6
989888973f5SMichael Tuexen 			isipv6 = 0;
990888973f5SMichael Tuexen #endif
991888973f5SMichael Tuexen 			break;
992888973f5SMichael Tuexen #endif /* INET */
993888973f5SMichael Tuexen #ifdef INET6
994888973f5SMichael Tuexen 		case AF_INET6:
9950ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
9960ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
997888973f5SMichael Tuexen 				error = EINVAL;
998888973f5SMichael Tuexen 				goto out;
999888973f5SMichael Tuexen 			}
1000e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1001e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1002e240ce42SMichael Tuexen 				goto out;
1003e240ce42SMichael Tuexen 			}
10040ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1005888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1006888973f5SMichael Tuexen 				goto out;
1007888973f5SMichael Tuexen 			}
10080ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1009888973f5SMichael Tuexen #ifdef INET
1010888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1011888973f5SMichael Tuexen 					error = EINVAL;
1012888973f5SMichael Tuexen 					goto out;
1013888973f5SMichael Tuexen 				}
1014888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1015888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1016888973f5SMichael Tuexen 					goto out;
1017888973f5SMichael Tuexen 				}
10184a91aa8fSMichael Tuexen 				restoreflags = true;
1019888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1020888973f5SMichael Tuexen 				sinp = &sin;
10210ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1022888973f5SMichael Tuexen 				if (IN_MULTICAST(
1023888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1024888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1025888973f5SMichael Tuexen 					goto out;
1026888973f5SMichael Tuexen 				}
1027888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10287d2608a5SMark Johnston 				    &sinp->sin_addr)))
1029888973f5SMichael Tuexen 					goto out;
1030888973f5SMichael Tuexen 				isipv6 = 0;
1031888973f5SMichael Tuexen #else /* !INET */
1032888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1033888973f5SMichael Tuexen 				goto out;
1034888973f5SMichael Tuexen #endif /* INET */
1035888973f5SMichael Tuexen 			} else {
1036888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1037888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1038888973f5SMichael Tuexen 					goto out;
1039888973f5SMichael Tuexen 				}
10404a91aa8fSMichael Tuexen 				restoreflags = true;
1041888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1042888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1043888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10447d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1045888973f5SMichael Tuexen 					goto out;
1046888973f5SMichael Tuexen 				isipv6 = 1;
1047888973f5SMichael Tuexen 			}
1048888973f5SMichael Tuexen 			break;
1049888973f5SMichael Tuexen #endif /* INET6 */
1050888973f5SMichael Tuexen 		default:
1051888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1052888973f5SMichael Tuexen 			goto out;
1053888973f5SMichael Tuexen 		}
1054888973f5SMichael Tuexen 	}
10552c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
105608af8aacSRandall Stewart 		if (tp->t_acktime == 0)
105708af8aacSRandall Stewart 			tp->t_acktime = ticks;
1058651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10597d2608a5SMark Johnston 		m = NULL;
10602c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1061bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1062bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1063bd4a39ccSMark Johnston 
10642c37256eSGarrett Wollman 			/*
10652c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10662c37256eSGarrett Wollman 			 * initialize window to default value, and
10670c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10682c37256eSGarrett Wollman 			 */
1069fb59c426SYoshinobu Inoue #ifdef INET6
1070fb59c426SYoshinobu Inoue 			if (isipv6)
1071a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1072fb59c426SYoshinobu Inoue #endif /* INET6 */
1073b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1074b287c6c7SBjoern A. Zeeb 			else
1075b287c6c7SBjoern A. Zeeb #endif
1076b287c6c7SBjoern A. Zeeb #ifdef INET
1077a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1078b287c6c7SBjoern A. Zeeb #endif
10794a91aa8fSMichael Tuexen 			/*
10804a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10814a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10824a91aa8fSMichael Tuexen 			 * operations fail.
10834a91aa8fSMichael Tuexen 			 */
10844a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10854a91aa8fSMichael Tuexen 				restoreflags = false;
10864a91aa8fSMichael Tuexen 
10877d2608a5SMark Johnston 			if (error) {
10887d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10897d2608a5SMark Johnston 				sbflush(&so->so_snd);
10902c37256eSGarrett Wollman 				goto out;
10917d2608a5SMark Johnston 			}
1092c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1093c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
109418a75309SPatrick Kelsey 			else {
10952c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10962c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10972c37256eSGarrett Wollman 			}
1098c560df6fSPatrick Kelsey 		}
10992c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11002c37256eSGarrett Wollman 			/*
11012c37256eSGarrett Wollman 			 * Close the send side of the connection after
11022c37256eSGarrett Wollman 			 * the data is sent.
11032c37256eSGarrett Wollman 			 */
11042c37256eSGarrett Wollman 			socantsendmore(so);
1105623dce13SRobert Watson 			tcp_usrclosed(tp);
11062c37256eSGarrett Wollman 		}
1107e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1108e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1109e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1110e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1111e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1112e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1113e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1114e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1115e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1116e854dd38SRandall Stewart 		}
11172cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11182cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1119b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1120b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1121f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1122b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1123b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1124b0acefa8SBill Fenner 		}
11252c37256eSGarrett Wollman 	} else {
1126623dce13SRobert Watson 		/*
1127623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1128623dce13SRobert Watson 		 */
1129d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11302c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1131d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11322c37256eSGarrett Wollman 			error = ENOBUFS;
11332c37256eSGarrett Wollman 			goto out;
11342c37256eSGarrett Wollman 		}
11352c37256eSGarrett Wollman 		/*
11362c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11372c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11382c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11392c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11402c37256eSGarrett Wollman 		 * of data past the urgent section.
11412c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11422c37256eSGarrett Wollman 		 */
114308af8aacSRandall Stewart 		if (tp->t_acktime == 0)
114408af8aacSRandall Stewart 			tp->t_acktime = ticks;
1145651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1146d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
11477d2608a5SMark Johnston 		m = NULL;
1148ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1149ef53690bSGarrett Wollman 			/*
1150ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1151ef53690bSGarrett Wollman 			 * initialize window to default value, and
11520c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1153ef53690bSGarrett Wollman 			 */
115418a75309SPatrick Kelsey 
1155c560df6fSPatrick Kelsey 			/*
1156c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1157c560df6fSPatrick Kelsey 			 */
1158c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1159c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1160fb59c426SYoshinobu Inoue #ifdef INET6
1161fb59c426SYoshinobu Inoue 			if (isipv6)
1162a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1163fb59c426SYoshinobu Inoue #endif /* INET6 */
1164b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1165b287c6c7SBjoern A. Zeeb 			else
1166b287c6c7SBjoern A. Zeeb #endif
1167b287c6c7SBjoern A. Zeeb #ifdef INET
1168a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1169b287c6c7SBjoern A. Zeeb #endif
11704a91aa8fSMichael Tuexen 			/*
11714a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11724a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11734a91aa8fSMichael Tuexen 			 * operations fail.
11744a91aa8fSMichael Tuexen 			 */
11754a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11764a91aa8fSMichael Tuexen 				restoreflags = false;
11774a91aa8fSMichael Tuexen 
11787d2608a5SMark Johnston 			if (error != 0) {
11797d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11807d2608a5SMark Johnston 				sbflush(&so->so_snd);
1181ef53690bSGarrett Wollman 				goto out;
11827d2608a5SMark Johnston 			}
1183ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1184ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1185623dce13SRobert Watson 		}
1186300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11877d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11882cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1189f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11902cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11912c37256eSGarrett Wollman 		}
11922cbcd3c1SGleb Smirnoff 	}
11932529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11942529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11952529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11962529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11972529f56eSJonathan T. Looney 	    0, NULL, false);
11987d2608a5SMark Johnston 
1199d1401c90SRobert Watson out:
12004a91aa8fSMichael Tuexen 	/*
12017d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
12027d2608a5SMark Johnston 	 * responsible for freeing memory.
12037d2608a5SMark Johnston 	 */
12047d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12057d2608a5SMark Johnston 		m_freem(m);
12067d2608a5SMark Johnston 
12077d2608a5SMark Johnston 	/*
12084a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12094a91aa8fSMichael Tuexen 	 * restore the original flags.
12104a91aa8fSMichael Tuexen 	 */
12114a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12124a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12134a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12144a91aa8fSMichael Tuexen 	}
121500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
121600812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
12175d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12185d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1219f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
122097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
122173fddedaSPeter Grehan 	return (error);
12222c37256eSGarrett Wollman }
12232c37256eSGarrett Wollman 
12242cbcd3c1SGleb Smirnoff static int
12252cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12262cbcd3c1SGleb Smirnoff {
1227109eb549SGleb Smirnoff 	struct epoch_tracker et;
12282cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12292cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12302cbcd3c1SGleb Smirnoff 	int error;
12312cbcd3c1SGleb Smirnoff 
12322cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12332cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
123453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12352cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
123682334850SJohn Baldwin 		mb_free_notready(m, count);
12372cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12382cbcd3c1SGleb Smirnoff 	}
12392cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12402cbcd3c1SGleb Smirnoff 
12412cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12422cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12432cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1244f64dc2abSGleb Smirnoff 	if (error) {
12452cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1246f64dc2abSGleb Smirnoff 		return (error);
1247f64dc2abSGleb Smirnoff 	}
1248f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1249f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1250f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12512cbcd3c1SGleb Smirnoff 
12522cbcd3c1SGleb Smirnoff 	return (error);
12532cbcd3c1SGleb Smirnoff }
12542cbcd3c1SGleb Smirnoff 
12552c37256eSGarrett Wollman /*
1256a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12572c37256eSGarrett Wollman  */
1258ac45e92fSRobert Watson static void
12592c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12602c37256eSGarrett Wollman {
1261f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1262c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12636573d758SMatt Macy 	struct epoch_tracker et;
1264c78cbc7bSRobert Watson 
1265ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1266c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1267c78cbc7bSRobert Watson 
126897a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12698501a69cSRobert Watson 	INP_WLOCK(inp);
1270c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1271c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1272c78cbc7bSRobert Watson 
1273c78cbc7bSRobert Watson 	/*
1274a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1275c78cbc7bSRobert Watson 	 */
127653af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1277c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12788fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12798fa799bdSJonathan T. Looney 		if (tp == NULL)
12808fa799bdSJonathan T. Looney 			goto dropped;
128100812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12825d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1283c78cbc7bSRobert Watson 	}
1284ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1285d8596171SGleb Smirnoff 		soref(so);
1286ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1287a152f8a3SRobert Watson 	}
12888501a69cSRobert Watson 	INP_WUNLOCK(inp);
12898fa799bdSJonathan T. Looney dropped:
129097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1291a152f8a3SRobert Watson }
1292a152f8a3SRobert Watson 
1293a152f8a3SRobert Watson /*
1294a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1295a152f8a3SRobert Watson  */
1296a152f8a3SRobert Watson static void
1297a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1298a152f8a3SRobert Watson {
1299a152f8a3SRobert Watson 	struct inpcb *inp;
1300c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13016573d758SMatt Macy 	struct epoch_tracker et;
1302a152f8a3SRobert Watson 
1303a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1304a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1305a152f8a3SRobert Watson 
130697a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13078501a69cSRobert Watson 	INP_WLOCK(inp);
1308a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1309a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1310a152f8a3SRobert Watson 
1311a152f8a3SRobert Watson 	/*
1312cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1313a152f8a3SRobert Watson 	 * a disconnect.
1314a152f8a3SRobert Watson 	 */
131553af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1316a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1317cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
131874703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1319a152f8a3SRobert Watson 			tcp_disconnect(tp);
132000812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
13215d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1322a152f8a3SRobert Watson 		}
1323cda6bdbaSJohn Baldwin 	}
1324ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1325d8596171SGleb Smirnoff 		soref(so);
1326ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1327a152f8a3SRobert Watson 	}
13288501a69cSRobert Watson 	INP_WUNLOCK(inp);
132997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13302c37256eSGarrett Wollman }
13312c37256eSGarrett Wollman 
1332d3b6c96bSRandall Stewart static int
1333d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1334d3b6c96bSRandall Stewart {
1335d3b6c96bSRandall Stewart 	/*
1336d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1337d3b6c96bSRandall Stewart 	 * specified then it does not always support
1338d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1339d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1340d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1341d3b6c96bSRandall Stewart 	 */
1342d3b6c96bSRandall Stewart 	int ret = 0;
1343d3b6c96bSRandall Stewart 
1344d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1345d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1346d3b6c96bSRandall Stewart 	}
1347d3b6c96bSRandall Stewart 	return (ret);
1348d3b6c96bSRandall Stewart }
1349d3b6c96bSRandall Stewart 
13502c37256eSGarrett Wollman /*
13512c37256eSGarrett Wollman  * Receive out-of-band data.
13522c37256eSGarrett Wollman  */
13532c37256eSGarrett Wollman static int
13542c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13552c37256eSGarrett Wollman {
13562c37256eSGarrett Wollman 	int error = 0;
1357f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1358c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13592c37256eSGarrett Wollman 
1360623dce13SRobert Watson 	inp = sotoinpcb(so);
1361623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13628501a69cSRobert Watson 	INP_WLOCK(inp);
136353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1364c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
1365c2399dd2SMichael Tuexen 		return (ECONNRESET);
1366623dce13SRobert Watson 	}
1367623dce13SRobert Watson 	tp = intotcpcb(inp);
1368c2399dd2SMichael Tuexen 
1369d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1370d3b6c96bSRandall Stewart 	if (error) {
1371d3b6c96bSRandall Stewart 		goto out;
1372d3b6c96bSRandall Stewart 	}
13732c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1374c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13754cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13764cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13772c37256eSGarrett Wollman 		error = EINVAL;
13782c37256eSGarrett Wollman 		goto out;
13792c37256eSGarrett Wollman 	}
13802c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13812c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13822c37256eSGarrett Wollman 		goto out;
13832c37256eSGarrett Wollman 	}
13842c37256eSGarrett Wollman 	m->m_len = 1;
13852c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13862c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13872c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1388623dce13SRobert Watson 
1389623dce13SRobert Watson out:
139000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
13915d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13928501a69cSRobert Watson 	INP_WUNLOCK(inp);
1393623dce13SRobert Watson 	return (error);
13942c37256eSGarrett Wollman }
13952c37256eSGarrett Wollman 
1396b287c6c7SBjoern A. Zeeb #ifdef INET
1397e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1398e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1399e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1400e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1401e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1402e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1403e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1404e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1405e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1406e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1407e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1408e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1409e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1410e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1411e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1412e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1413e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1414e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1415e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1416e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1417e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1418e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1419e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1420e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14212c37256eSGarrett Wollman };
1422b287c6c7SBjoern A. Zeeb #endif /* INET */
1423df8bae1dSRodney W. Grimes 
1424fb59c426SYoshinobu Inoue #ifdef INET6
1425e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1426e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1427e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1428e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1429e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1430e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1431e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1432e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1433e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1434e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1435e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1436e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1437e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1438e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1439e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1440e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1441e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1442e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1443e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1444e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1445e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1446e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1447e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1448e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1449fb59c426SYoshinobu Inoue };
1450fb59c426SYoshinobu Inoue #endif /* INET6 */
1451fb59c426SYoshinobu Inoue 
1452b287c6c7SBjoern A. Zeeb #ifdef INET
1453a0292f23SGarrett Wollman /*
1454a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1455dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1456dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1457dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1458a0292f23SGarrett Wollman  */
14590312fbe9SPoul-Henning Kamp static int
1460a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1461a0292f23SGarrett Wollman {
14629e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14639eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1464c3229e05SDavid Greenman 	int error;
1465a0292f23SGarrett Wollman 
1466c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14678501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
146876f1499fSGleb Smirnoff 
1469636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1470de0a2eb2SMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1471de0a2eb2SMark Johnston 	    SS_ISDISCONNECTED)) != 0))
1472636b19eaSMark Johnston 		return (EISCONN);
1473636b19eaSMark Johnston 
1474fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1475dfc4d218SGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred, true);
14769e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1477dfc4d218SGleb Smirnoff 	if (error != 0)
14789e46ff4dSGleb Smirnoff 		return (error);
1479a0292f23SGarrett Wollman 
1480087b55eaSAndre Oppermann 	/*
1481087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1482087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1483087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1484087b55eaSAndre Oppermann 	 */
1485a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14869b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1487a0292f23SGarrett Wollman 		tp->request_r_scale++;
1488a0292f23SGarrett Wollman 
1489a0292f23SGarrett Wollman 	soisconnecting(so);
149078b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
149157f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14928e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14938e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14948e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1495a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1496a45d2726SAndras Olah 
14979e46ff4dSGleb Smirnoff 	return (0);
1498a0292f23SGarrett Wollman }
1499b287c6c7SBjoern A. Zeeb #endif /* INET */
1500a0292f23SGarrett Wollman 
1501fb59c426SYoshinobu Inoue #ifdef INET6
1502fb59c426SYoshinobu Inoue static int
1503a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1504fb59c426SYoshinobu Inoue {
15059eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1506636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1507fb59c426SYoshinobu Inoue 	int error;
1508fb59c426SYoshinobu Inoue 
1509775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
15108501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1511623dce13SRobert Watson 
1512636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1513636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1514636b19eaSMark Johnston 		return (EISCONN);
1515636b19eaSMark Johnston 
151676f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1517a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1518fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
151976f1499fSGleb Smirnoff 	if (error != 0)
152076f1499fSGleb Smirnoff 		return (error);
1521fb59c426SYoshinobu Inoue 
1522fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1523fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1524970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1525fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1526fb59c426SYoshinobu Inoue 
1527636b19eaSMark Johnston 	soisconnecting(so);
152878b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
152957f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15308e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15318e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15328e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1533fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1534fb59c426SYoshinobu Inoue 
153576f1499fSGleb Smirnoff 	return (0);
1536fb59c426SYoshinobu Inoue }
1537fb59c426SYoshinobu Inoue #endif /* INET6 */
1538fb59c426SYoshinobu Inoue 
1539cfe8b629SGarrett Wollman /*
1540b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1541b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1542b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1543b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1544b8af5dfaSRobert Watson  * from Linux.
1545b8af5dfaSRobert Watson  */
15468c6104c4SMarius Strobl void
15478c6104c4SMarius Strobl tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1548b8af5dfaSRobert Watson {
1549b8af5dfaSRobert Watson 
15508c6104c4SMarius Strobl 	INP_LOCK_ASSERT(tptoinpcb(tp));
1551b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1552b8af5dfaSRobert Watson 
1553b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1554b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1555b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15563529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1557b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1558b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1559b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1560b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1561b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1562b8af5dfaSRobert Watson 	}
156304682968SRichard Scheffenegger 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
156404682968SRichard Scheffenegger 		case TF2_ECN_PERMIT:
15655a17b6adSMichael Tuexen 			ti->tcpi_options |= TCPI_OPT_ECN;
156604682968SRichard Scheffenegger 			break;
156704682968SRichard Scheffenegger 		case TF2_ACE_PERMIT:
156804682968SRichard Scheffenegger 			/* FALLTHROUGH */
156904682968SRichard Scheffenegger 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
157004682968SRichard Scheffenegger 			ti->tcpi_options |= TCPI_OPT_ACE;
157104682968SRichard Scheffenegger 			break;
157204682968SRichard Scheffenegger 		default:
157304682968SRichard Scheffenegger 			break;
157404682968SRichard Scheffenegger 	}
157504682968SRichard Scheffenegger 	if (IS_FASTOPEN(tp->t_flags))
157604682968SRichard Scheffenegger 		ti->tcpi_options |= TCPI_OPT_TFO;
15771baaf834SBruce M Simpson 
157843d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15793ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15801baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15811baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15821baaf834SBruce M Simpson 
1583b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1584b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1585b8af5dfaSRobert Watson 
1586b8af5dfaSRobert Watson 	/*
1587b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1588b8af5dfaSRobert Watson 	 */
1589c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1590535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1591b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15921c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1593535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
159443d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
159543d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1596f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1597f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1598f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1599dc485b96SMarius Strobl 	ti->tcpi_snd_una = tp->snd_una;
1600dc485b96SMarius Strobl 	ti->tcpi_snd_max = tp->snd_max;
1601dc485b96SMarius Strobl 	ti->tcpi_rcv_numsacks = tp->rcv_numsacks;
1602dc485b96SMarius Strobl 	ti->tcpi_rcv_adv = tp->rcv_adv;
1603dc485b96SMarius Strobl 	ti->tcpi_dupacks = tp->t_dupacks;
1604a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1605a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1606a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1607a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1608a6456410SNavdeep Parhar 	}
1609a6456410SNavdeep Parhar #endif
161022c81cc5SRichard Scheffenegger 	/*
161122c81cc5SRichard Scheffenegger 	 * AccECN related counters.
161222c81cc5SRichard Scheffenegger 	 */
161322c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
161422c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
161522c81cc5SRichard Scheffenegger 		/*
161622c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
161722c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
161822c81cc5SRichard Scheffenegger 		 */
161922c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
162022c81cc5SRichard Scheffenegger 	else
162122c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
162222c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1623b8af5dfaSRobert Watson }
1624b8af5dfaSRobert Watson 
1625b8af5dfaSRobert Watson /*
16261e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16271e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16281e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16291e8f5ffaSRobert Watson  * option.
1630cfe8b629SGarrett Wollman  */
1631bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16328501a69cSRobert Watson 	INP_WLOCK(inp);							\
163353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16348501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1635bac5bedfSConrad Meyer 		cleanup;						\
16361e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16371e8f5ffaSRobert Watson 	}								\
16381e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16391e8f5ffaSRobert Watson } while(0)
1640bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16411e8f5ffaSRobert Watson 
1642fd7daa72SMichael Tuexen int
1643fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1644df8bae1dSRodney W. Grimes {
1645fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1646fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1647fc4d53ccSGleb Smirnoff 	int error = 0;
1648df8bae1dSRodney W. Grimes 
1649fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16503b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
165153af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1652fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1653fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1654fc4d53ccSGleb Smirnoff 
1655cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16563b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1657fb59c426SYoshinobu Inoue #ifdef INET6
1658de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1659fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1660de156263SGleb Smirnoff #endif
1661de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1662de156263SGleb Smirnoff 		else
1663de156263SGleb Smirnoff #endif
1664de156263SGleb Smirnoff #ifdef INET
1665fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1666de156263SGleb Smirnoff #endif
16675dff1c38SMichael Tuexen 		/*
1668de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1669de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1670de156263SGleb Smirnoff 		 * IP level returned.
16715dff1c38SMichael Tuexen 		 */
1672de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1673de156263SGleb Smirnoff #ifdef INET6
1674de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1675de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1676de156263SGleb Smirnoff 				return (error);
1677de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1678de156263SGleb Smirnoff 			case IPV6_TCLASS:
1679de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1680de156263SGleb Smirnoff 				break;
1681de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1682f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1683f581a26eSGleb Smirnoff 				break;
1684de156263SGleb Smirnoff 			default:
1685de156263SGleb Smirnoff 				return (error);
16865dff1c38SMichael Tuexen 			}
1687de156263SGleb Smirnoff 			break;
1688b287c6c7SBjoern A. Zeeb #endif
1689b287c6c7SBjoern A. Zeeb #ifdef INET
1690de156263SGleb Smirnoff 		case IPPROTO_IP:
1691de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1692de156263SGleb Smirnoff 			case IP_TOS:
16933b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
16943b0ee680SRichard Scheffenegger 				break;
1695de156263SGleb Smirnoff 			case IP_TTL:
1696de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1697de156263SGleb Smirnoff 				break;
1698de156263SGleb Smirnoff 			default:
1699df8bae1dSRodney W. Grimes 				return (error);
1700de156263SGleb Smirnoff 			}
1701de156263SGleb Smirnoff 			break;
1702de156263SGleb Smirnoff #endif
1703de156263SGleb Smirnoff 		default:
1704de156263SGleb Smirnoff 			return (error);
1705de156263SGleb Smirnoff 		}
17063b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
170753af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
17083b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
17093b3c08c1SMichael Tuexen 			return (ECONNRESET);
17103b3c08c1SMichael Tuexen 		}
1711fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1712fc4d53ccSGleb Smirnoff 		/*
1713fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1714fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1715fc4d53ccSGleb Smirnoff 		 */
1716fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1717fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
171873ee5756SRandall Stewart 		void *ptr = NULL;
1719fc4d53ccSGleb Smirnoff 
17203b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1721fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1722fc4d53ccSGleb Smirnoff 		if (error)
1723fc4d53ccSGleb Smirnoff 			return (error);
1724fc4d53ccSGleb Smirnoff 
172568cea2b1SJohn Baldwin 		INP_WLOCK(inp);
172655bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1727fc4d53ccSGleb Smirnoff 
172855bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
172955bceb1eSRandall Stewart 		if (blk == NULL) {
173055bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
173155bceb1eSRandall Stewart 			return (ENOENT);
173255bceb1eSRandall Stewart 		}
1733587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1734587d67c0SRandall Stewart 			/* You already have this */
1735587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1736587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1737587d67c0SRandall Stewart 			return (0);
1738587d67c0SRandall Stewart 		}
1739587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1740587d67c0SRandall Stewart 			/*
1741587d67c0SRandall Stewart 			 * The user has advanced the state
1742587d67c0SRandall Stewart 			 * past the initial point, we may not
1743587d67c0SRandall Stewart 			 * be able to switch.
1744587d67c0SRandall Stewart 			 */
1745587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1746587d67c0SRandall Stewart 				/*
1747587d67c0SRandall Stewart 				 * Does the stack provide a
1748587d67c0SRandall Stewart 				 * query mechanism, if so it may
1749587d67c0SRandall Stewart 				 * still be possible?
1750587d67c0SRandall Stewart 				 */
1751587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1752c6c0be27SMichael Tuexen 			} else
1753c6c0be27SMichael Tuexen 				error = EINVAL;
1754587d67c0SRandall Stewart 			if (error) {
1755587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1756587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1757587d67c0SRandall Stewart 				return(error);
1758587d67c0SRandall Stewart 			}
1759587d67c0SRandall Stewart 		}
176055bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
176155bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
176255bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
176355bceb1eSRandall Stewart 			return (ENOENT);
176455bceb1eSRandall Stewart 		}
176555bceb1eSRandall Stewart 		/*
176673ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
176773ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
176855bceb1eSRandall Stewart 		 */
1769d2ef52efSGleb Smirnoff 		if (tp->t_fb->tfb_tcp_timer_stop_all != NULL)
1770d2ef52efSGleb Smirnoff 			tp->t_fb->tfb_tcp_timer_stop_all(tp);
177173ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
177273ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
177373ee5756SRandall Stewart 			if (error) {
177473ee5756SRandall Stewart 				/*
177573ee5756SRandall Stewart 				 * Release the ref count the lookup
177673ee5756SRandall Stewart 				 * acquired.
177773ee5756SRandall Stewart 				 */
177873ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
177973ee5756SRandall Stewart 				/*
178073ee5756SRandall Stewart 				 * Now there is a chance that the
178173ee5756SRandall Stewart 				 * init() function mucked with some
178273ee5756SRandall Stewart 				 * things before it failed, such as
178373ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
178473ee5756SRandall Stewart 				 * It should not of, but lets give the old
178573ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
178673ee5756SRandall Stewart 				 */
178773ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
178873ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
178973ee5756SRandall Stewart 				}
179073ee5756SRandall Stewart 			 	goto err_out;
179173ee5756SRandall Stewart 			}
179273ee5756SRandall Stewart 		}
1793587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1794086a3556SAndrew Gallatin 			struct epoch_tracker et;
1795587d67c0SRandall Stewart 			/*
1796587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1797587d67c0SRandall Stewart 			 * the tcb is not going away.
1798587d67c0SRandall Stewart 			 */
1799086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1800587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1801086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1802587d67c0SRandall Stewart 		}
180373ee5756SRandall Stewart 		/*
180473ee5756SRandall Stewart 		 * Release the old refcnt, the
180573ee5756SRandall Stewart 		 * lookup acquired a ref on the
180673ee5756SRandall Stewart 		 * new one already.
180773ee5756SRandall Stewart 		 */
180855bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
180973ee5756SRandall Stewart 		/*
181073ee5756SRandall Stewart 		 * Set in the new stack.
181173ee5756SRandall Stewart 		 */
181255bceb1eSRandall Stewart 		tp->t_fb = blk;
181373ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
181455bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
181555bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
181655bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
181755bceb1eSRandall Stewart 			     sopt->sopt_name);
181855bceb1eSRandall Stewart 		}
181955bceb1eSRandall Stewart #endif
18203ee9c3c4SRandall Stewart err_out:
182155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
182255bceb1eSRandall Stewart 		return (error);
182373ee5756SRandall Stewart 
1824fc4d53ccSGleb Smirnoff 	}
1825fc4d53ccSGleb Smirnoff 
18263b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
182766fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1828fc4d53ccSGleb Smirnoff }
1829fc4d53ccSGleb Smirnoff 
1830fc4d53ccSGleb Smirnoff static int
1831fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1832fc4d53ccSGleb Smirnoff {
1833fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1834fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1835fc4d53ccSGleb Smirnoff 	int error = 0;
1836fc4d53ccSGleb Smirnoff 
1837fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18383b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
183953af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1840fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1841fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1842fc4d53ccSGleb Smirnoff 
1843fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18443b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1845fc4d53ccSGleb Smirnoff #ifdef INET6
1846fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1847fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1848fc4d53ccSGleb Smirnoff #endif /* INET6 */
1849fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1850fc4d53ccSGleb Smirnoff 		else
1851fc4d53ccSGleb Smirnoff #endif
1852fc4d53ccSGleb Smirnoff #ifdef INET
1853fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1854fc4d53ccSGleb Smirnoff #endif
1855fc4d53ccSGleb Smirnoff 		return (error);
1856fc4d53ccSGleb Smirnoff 	}
1857fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1858e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1859fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1860fc4d53ccSGleb Smirnoff 
1861e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1862e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1863e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1864e2833083SPeter Lei 		} else {
1865e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1866e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1867c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1868c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1869e2833083SPeter Lei 		}
187055bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
187155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
187255bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
187355bceb1eSRandall Stewart 		return (error);
187455bceb1eSRandall Stewart 	}
1875fc4d53ccSGleb Smirnoff 
18763b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
187766fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1878fc4d53ccSGleb Smirnoff }
1879fc4d53ccSGleb Smirnoff 
1880fc4d53ccSGleb Smirnoff int
1881fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1882fc4d53ccSGleb Smirnoff {
1883fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1884fc4d53ccSGleb Smirnoff 
1885fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1886fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1887fc4d53ccSGleb Smirnoff 
18883b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
188953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18903b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18913b3c08c1SMichael Tuexen 		return (ECONNRESET);
18923b3c08c1SMichael Tuexen 	}
1893fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1894fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1895fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1896fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1897fc4d53ccSGleb Smirnoff 	else
1898fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
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 
1941b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1942b8d60729SRandall Stewart 
1943b8d60729SRandall Stewart static int
1944ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1945b8d60729SRandall Stewart {
1946b8d60729SRandall Stewart 	struct cc_algo *algo;
1947b8d60729SRandall Stewart 	void *ptr = NULL;
19483b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1949b8d60729SRandall Stewart 	struct cc_var cc_mem;
1950b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1951b8d60729SRandall Stewart 	size_t mem_sz;
1952b8d60729SRandall Stewart 	int error;
1953b8d60729SRandall Stewart 
1954b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1955b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1956b8d60729SRandall Stewart 	if (error)
1957b8d60729SRandall Stewart 		return(error);
1958b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1959b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1960ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1961b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1962b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1963b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1964b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1965b8d60729SRandall Stewart 				continue;
1966b8d60729SRandall Stewart 			}
1967b8d60729SRandall Stewart 			break;
1968b8d60729SRandall Stewart 		}
1969ea9017fbSRandall Stewart 	}
1970b8d60729SRandall Stewart 	if (algo == NULL) {
1971b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1972b8d60729SRandall Stewart 		return(ESRCH);
1973b8d60729SRandall Stewart 	}
1974ea9017fbSRandall Stewart 	/*
1975ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1976ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1977ea9017fbSRandall Stewart 	 */
1978ea9017fbSRandall Stewart 	cc_refer(algo);
1979ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1980b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1981b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1982b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1983b8d60729SRandall Stewart 		if (mem_sz == 0) {
1984b8d60729SRandall Stewart 			goto no_mem_needed;
1985b8d60729SRandall Stewart 		}
1986b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1987b8d60729SRandall Stewart 	} else {
1988b8d60729SRandall Stewart no_mem_needed:
1989b8d60729SRandall Stewart 		mem_sz = 0;
1990b8d60729SRandall Stewart 		ptr = NULL;
1991b8d60729SRandall Stewart 	}
1992b8d60729SRandall Stewart 	/*
1993b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1994b8d60729SRandall Stewart 	 * back the inplock.
1995b8d60729SRandall Stewart 	 */
1996b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1997df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
199853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1999df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
2000ea9017fbSRandall Stewart 		if (ptr)
2001df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
2002ea9017fbSRandall Stewart 		/* Release our temp reference */
2003ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
2004ea9017fbSRandall Stewart 		cc_release(algo);
2005ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
2006df07bfdaSMichael Tuexen 		return (ECONNRESET);
2007df07bfdaSMichael Tuexen 	}
2008df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
2009df07bfdaSMichael Tuexen 	if (ptr != NULL)
2010b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
2011b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
2012b8d60729SRandall Stewart 	/*
2013b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
2014b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
2015b8d60729SRandall Stewart 	 * Note here we init into stack memory.
2016b8d60729SRandall Stewart 	 */
2017b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
2018b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
2019b8d60729SRandall Stewart 	else
2020b8d60729SRandall Stewart 		error = 0;
2021b8d60729SRandall Stewart 	/*
2022b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
2023b8d60729SRandall Stewart 	 * should not fail we could in theory have a
2024b8d60729SRandall Stewart 	 * KASSERT here.
2025b8d60729SRandall Stewart 	 */
2026b8d60729SRandall Stewart 	if (error == 0) {
2027b8d60729SRandall Stewart 		/*
2028b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
2029b8d60729SRandall Stewart 		 * connection to the new CC module by
2030b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
2031b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
2032b8d60729SRandall Stewart 		 */
2033b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
2034e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2035ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
2036ea9017fbSRandall Stewart 		cc_detach(tp);
2037ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
2038e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
2039ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
2040ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2041b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2042b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2043b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2044e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2045b8d60729SRandall Stewart 		}
2046b8d60729SRandall Stewart 	} else if (ptr)
2047b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2048b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2049ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2050ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2051ea9017fbSRandall Stewart 	cc_release(algo);
2052ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2053b8d60729SRandall Stewart 	return (error);
2054b8d60729SRandall Stewart }
2055b8d60729SRandall Stewart 
205655bceb1eSRandall Stewart int
205766fbc19fSGleb Smirnoff tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
205855bceb1eSRandall Stewart {
205966fbc19fSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
206055bceb1eSRandall Stewart 	int	error, opt, optval;
206155bceb1eSRandall Stewart 	u_int	ui;
206255bceb1eSRandall Stewart 	struct	tcp_info ti;
2063b2e60773SJohn Baldwin #ifdef KERN_TLS
2064b2e60773SJohn Baldwin 	struct tls_enable tls;
2065528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2066b2e60773SJohn Baldwin #endif
20672529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2068adc56f5aSEdward Tomasz Napierala #ifdef STATS
2069adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2070adc56f5aSEdward Tomasz Napierala #endif
2071af6fef3aSGleb Smirnoff 	size_t	len;
2072df8bae1dSRodney W. Grimes 
2073f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
207453af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2075fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2076528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2077f581a26eSGleb Smirnoff 
2078f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2079f581a26eSGleb Smirnoff #ifdef INET6
2080f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2081f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2082f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2083f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2084f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2085f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2086f581a26eSGleb Smirnoff 		}
2087f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2088f581a26eSGleb Smirnoff 		return (0);
2089f581a26eSGleb Smirnoff #endif
2090f581a26eSGleb Smirnoff #ifdef INET
2091f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2092f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2093f581a26eSGleb Smirnoff 		return (0);
2094f581a26eSGleb Smirnoff #endif
2095f581a26eSGleb Smirnoff 	}
2096f581a26eSGleb Smirnoff 
2097d519cedbSGleb Smirnoff 	/*
2098d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2099d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2100d519cedbSGleb Smirnoff 	 */
2101d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2102d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2103d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2104c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2105c8b53cedSMichael Tuexen 			return (EINVAL);
2106af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2107af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2108d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2109d519cedbSGleb Smirnoff 		if (error) {
2110af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2111d519cedbSGleb Smirnoff 			return (error);
2112d519cedbSGleb Smirnoff 		}
2113bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2114d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2115e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2116d519cedbSGleb Smirnoff 		else
2117d519cedbSGleb Smirnoff 			error = ENOENT;
2118d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2119d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2120af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2121af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2122d519cedbSGleb Smirnoff 		return (error);
2123d519cedbSGleb Smirnoff 	}
2124d519cedbSGleb Smirnoff 
2125cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2126cfe8b629SGarrett Wollman 	case SOPT_SET:
2127cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2128fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
212988f6b043SBruce M Simpson 		case TCP_MD5SIG:
21308501a69cSRobert Watson 			INP_WUNLOCK(inp);
213197453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2132fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2133fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
21341cfd4b53SBruce M Simpson 			if (error)
21351e8f5ffaSRobert Watson 				return (error);
213697453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
213709fe6320SNavdeep Parhar 			goto unlock_and_done;
2138fcf59617SAndrey V. Elsukov #endif /* IPSEC */
213909fe6320SNavdeep Parhar 
2140df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2141cfe8b629SGarrett Wollman 		case TCP_NOOPT:
21428501a69cSRobert Watson 			INP_WUNLOCK(inp);
2143cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2144cfe8b629SGarrett Wollman 			    sizeof optval);
2145cfe8b629SGarrett Wollman 			if (error)
21461e8f5ffaSRobert Watson 				return (error);
2147cfe8b629SGarrett Wollman 
21488501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2149cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2150cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2151cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2152cfe8b629SGarrett Wollman 				break;
2153cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2154cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2155cfe8b629SGarrett Wollman 				break;
2156cfe8b629SGarrett Wollman 			default:
2157cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2158cfe8b629SGarrett Wollman 				break;
2159cfe8b629SGarrett Wollman 			}
2160cfe8b629SGarrett Wollman 
2161cfe8b629SGarrett Wollman 			if (optval)
2162cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2163df8bae1dSRodney W. Grimes 			else
2164cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
216509fe6320SNavdeep Parhar unlock_and_done:
216609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
216709fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
216809fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
216909fe6320SNavdeep Parhar 				    sopt->sopt_name);
217009fe6320SNavdeep Parhar 			}
217109fe6320SNavdeep Parhar #endif
21728501a69cSRobert Watson 			INP_WUNLOCK(inp);
2173df8bae1dSRodney W. Grimes 			break;
2174df8bae1dSRodney W. Grimes 
2175007581c0SJonathan Lemon 		case TCP_NOPUSH:
21768501a69cSRobert Watson 			INP_WUNLOCK(inp);
2177007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2178007581c0SJonathan Lemon 			    sizeof optval);
2179007581c0SJonathan Lemon 			if (error)
21801e8f5ffaSRobert Watson 				return (error);
2181007581c0SJonathan Lemon 
21828501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2183007581c0SJonathan Lemon 			if (optval)
2184007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2185d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2186007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2187109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2188109eb549SGleb Smirnoff 					struct epoch_tracker et;
2189109eb549SGleb Smirnoff 
2190109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2191f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2192109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2193109eb549SGleb Smirnoff 				}
2194007581c0SJonathan Lemon 			}
219509fe6320SNavdeep Parhar 			goto unlock_and_done;
2196007581c0SJonathan Lemon 
21979e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21989e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21999e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
22009e644c23SMichael Tuexen 			    sizeof optval);
22019e644c23SMichael Tuexen 			if (error)
22029e644c23SMichael Tuexen 				return (error);
22039e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
22049e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
22059e644c23SMichael Tuexen 				/* Its got to be in range */
22069e644c23SMichael Tuexen 				return (EINVAL);
22079e644c23SMichael Tuexen 			}
22089e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
22099e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
22109e644c23SMichael Tuexen 				return (EINVAL);
22119e644c23SMichael Tuexen 			}
22129e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
22139e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
22149e644c23SMichael Tuexen 				/* You can't change after you are connected */
22159e644c23SMichael Tuexen 				error = EINVAL;
22169e644c23SMichael Tuexen 			} else {
22179e644c23SMichael Tuexen 				/* Ok we are all good set the port */
22189e644c23SMichael Tuexen 				tp->t_port = htons(optval);
22199e644c23SMichael Tuexen 			}
22209e644c23SMichael Tuexen 			goto unlock_and_done;
22219e644c23SMichael Tuexen 
2222df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
22238501a69cSRobert Watson 			INP_WUNLOCK(inp);
2224cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2225cfe8b629SGarrett Wollman 			    sizeof optval);
2226cfe8b629SGarrett Wollman 			if (error)
22271e8f5ffaSRobert Watson 				return (error);
2228df8bae1dSRodney W. Grimes 
22298501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
223053369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2231603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2232cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2233a0292f23SGarrett Wollman 			else
2234a0292f23SGarrett Wollman 				error = EINVAL;
223509fe6320SNavdeep Parhar 			goto unlock_and_done;
2236a0292f23SGarrett Wollman 
2237b8af5dfaSRobert Watson 		case TCP_INFO:
22388501a69cSRobert Watson 			INP_WUNLOCK(inp);
2239b8af5dfaSRobert Watson 			error = EINVAL;
2240b8af5dfaSRobert Watson 			break;
2241b8af5dfaSRobert Watson 
2242adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2243adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2244adc56f5aSEdward Tomasz Napierala #ifdef STATS
2245adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2246adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2247adc56f5aSEdward Tomasz Napierala 			if (error)
2248adc56f5aSEdward Tomasz Napierala 				return (error);
2249adc56f5aSEdward Tomasz Napierala 
2250adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2251adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2252adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2253adc56f5aSEdward Tomasz Napierala 			else
2254adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2255adc56f5aSEdward Tomasz Napierala 
2256adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2257adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2258adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2259adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2260adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2261adc56f5aSEdward Tomasz Napierala 				sbp = t;
2262adc56f5aSEdward Tomasz Napierala 			}
2263adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2264adc56f5aSEdward Tomasz Napierala 
2265adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2266adc56f5aSEdward Tomasz Napierala #else
2267adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2268adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2269adc56f5aSEdward Tomasz Napierala 			break;
2270adc56f5aSEdward Tomasz Napierala 
2271dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2272ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
227373e263b1SGleb Smirnoff 			break;
2274dbc42409SLawrence Stewart 
2275a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2276a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2277a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2278a034518aSAndrew Gallatin 			    sizeof(optval));
2279a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2280a034518aSAndrew Gallatin 			if (!error)
2281a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2282a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2283a034518aSAndrew Gallatin 			break;
2284a034518aSAndrew Gallatin 
2285b2e60773SJohn Baldwin #ifdef KERN_TLS
2286b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2287b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2288ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2289b2e60773SJohn Baldwin 			if (error)
2290b2e60773SJohn Baldwin 				break;
2291fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2292b2e60773SJohn Baldwin 			break;
2293b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2294b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2295b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2296b2e60773SJohn Baldwin 			if (error)
2297b2e60773SJohn Baldwin 				return (error);
2298b2e60773SJohn Baldwin 
2299b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2300fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2301b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2302b2e60773SJohn Baldwin 			break;
2303f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2304f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2305f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2306f1f93475SJohn Baldwin 			    sizeof(tls));
2307f1f93475SJohn Baldwin 			if (error)
2308f1f93475SJohn Baldwin 				break;
2309fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2310f1f93475SJohn Baldwin 			break;
2311b2e60773SJohn Baldwin #endif
231208af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
23139077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
23149077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
23159077f387SGleb Smirnoff 		case TCP_KEEPINIT:
23169077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
23179077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
23189077f387SGleb Smirnoff 			if (error)
23199077f387SGleb Smirnoff 				return (error);
23209077f387SGleb Smirnoff 
23219077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
23229077f387SGleb Smirnoff 				error = EINVAL;
23239077f387SGleb Smirnoff 				break;
23249077f387SGleb Smirnoff 			}
23259077f387SGleb Smirnoff 			ui *= hz;
23269077f387SGleb Smirnoff 
23279077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
23289077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
232908af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
233008af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
233108af8aacSRandall Stewart 				break;
233208af8aacSRandall Stewart 
23339077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23349077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23359077f387SGleb Smirnoff 				/*
23369077f387SGleb Smirnoff 				 * XXX: better check current remaining
23379077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
23389077f387SGleb Smirnoff 				 */
23399077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
23409077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
23419077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23429077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23439077f387SGleb Smirnoff 				break;
23449077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23459077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23469077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23479077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23489077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23499077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23509077f387SGleb Smirnoff 				break;
23519077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23529077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23539077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23549077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23559077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23569077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23579077f387SGleb Smirnoff 				break;
23589077f387SGleb Smirnoff 			}
235909fe6320SNavdeep Parhar 			goto unlock_and_done;
23609077f387SGleb Smirnoff 
236185c05144SGleb Smirnoff 		case TCP_KEEPCNT:
236285c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
236385c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
236485c05144SGleb Smirnoff 			if (error)
236585c05144SGleb Smirnoff 				return (error);
236685c05144SGleb Smirnoff 
236785c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
236885c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
236985c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
237085c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
237185c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
237285c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
237385c05144SGleb Smirnoff 			goto unlock_and_done;
237485c05144SGleb Smirnoff 
237586a996e6SHiren Panchasara #ifdef TCPPCAP
237686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
237786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
237886a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
237986a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
238086a996e6SHiren Panchasara 			    sizeof optval);
238186a996e6SHiren Panchasara 			if (error)
238286a996e6SHiren Panchasara 				return (error);
238386a996e6SHiren Panchasara 
238486a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
238586a996e6SHiren Panchasara 			if (optval >= 0)
2386399a5655SRichard Scheffenegger 				tcp_pcap_set_sock_max(
2387399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
238886a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
238986a996e6SHiren Panchasara 					optval);
239086a996e6SHiren Panchasara 			else
239186a996e6SHiren Panchasara 				error = EINVAL;
239286a996e6SHiren Panchasara 			goto unlock_and_done;
239386a996e6SHiren Panchasara #endif
239486a996e6SHiren Panchasara 
2395c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2396c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2397c560df6fSPatrick Kelsey 
2398281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2399c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2400c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2401281a0fd4SPatrick Kelsey 				return (EPERM);
2402281a0fd4SPatrick Kelsey 
2403c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2404c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2405281a0fd4SPatrick Kelsey 			if (error)
2406281a0fd4SPatrick Kelsey 				return (error);
2407281a0fd4SPatrick Kelsey 
2408281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2409d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2410d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2411d442a657SMichael Tuexen 				error = EINVAL;
2412d442a657SMichael Tuexen 				goto unlock_and_done;
2413d442a657SMichael Tuexen 			}
2414c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2415c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2416c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2417c560df6fSPatrick Kelsey 						error = EPERM;
2418c560df6fSPatrick Kelsey 						goto unlock_and_done;
2419c560df6fSPatrick Kelsey 					}
2420c560df6fSPatrick Kelsey 
2421c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2422281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2423281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2424c560df6fSPatrick Kelsey 				} else {
2425c560df6fSPatrick Kelsey 					/*
2426c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2427c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2428c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2429c560df6fSPatrick Kelsey 					 * connect.
2430c560df6fSPatrick Kelsey 					 */
2431c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2432c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2433c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2434c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2435c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2436c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2437c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2438c560df6fSPatrick Kelsey 					}
2439c560df6fSPatrick Kelsey 				}
2440d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2441281a0fd4SPatrick Kelsey 			} else
2442281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2443281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2444c560df6fSPatrick Kelsey 		}
2445281a0fd4SPatrick Kelsey 
2446e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24472529f56eSJonathan T. Looney 		case TCP_LOG:
24482529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24492529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24502529f56eSJonathan T. Looney 			    sizeof optval);
24512529f56eSJonathan T. Looney 			if (error)
24522529f56eSJonathan T. Looney 				return (error);
24532529f56eSJonathan T. Looney 
24542529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24552529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24562529f56eSJonathan T. Looney 			goto unlock_and_done;
24572529f56eSJonathan T. Looney 
24582529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24592529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24602529f56eSJonathan T. Looney 			error = EINVAL;
24612529f56eSJonathan T. Looney 			break;
24622529f56eSJonathan T. Looney 
24632529f56eSJonathan T. Looney 		case TCP_LOGID:
24642529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24652529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24662529f56eSJonathan T. Looney 			if (error)
24672529f56eSJonathan T. Looney 				break;
24682529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24692529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24702529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24712529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24722529f56eSJonathan T. Looney 			break;
24732529f56eSJonathan T. Looney 
24742529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24752529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24762529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24772529f56eSJonathan T. Looney 			error =
24782529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
24792529f56eSJonathan T. Looney 			if (error)
24802529f56eSJonathan T. Looney 				break;
24812529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24822529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24832529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
24842529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24852529f56eSJonathan T. Looney 				    M_WAITOK, true);
24862529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24872529f56eSJonathan T. Looney 			} else {
24882529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24892529f56eSJonathan T. Looney 				/*
24902529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24912529f56eSJonathan T. Looney 				 * INP lock.
24922529f56eSJonathan T. Looney 				 */
24932529f56eSJonathan T. Looney 			}
24942529f56eSJonathan T. Looney 			break;
2495e24e5683SJonathan T. Looney #endif
24962529f56eSJonathan T. Looney 
2497df8bae1dSRodney W. Grimes 		default:
24988501a69cSRobert Watson 			INP_WUNLOCK(inp);
2499df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2500df8bae1dSRodney W. Grimes 			break;
2501df8bae1dSRodney W. Grimes 		}
2502df8bae1dSRodney W. Grimes 		break;
2503df8bae1dSRodney W. Grimes 
2504cfe8b629SGarrett Wollman 	case SOPT_GET:
25051e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2506cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2507fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
250888f6b043SBruce M Simpson 		case TCP_MD5SIG:
25098501a69cSRobert Watson 			INP_WUNLOCK(inp);
251097453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2511fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2512fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
25131cfd4b53SBruce M Simpson 			break;
2514265ed012SBruce M Simpson #endif
25151e8f5ffaSRobert Watson 
2516df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2517cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
25188501a69cSRobert Watson 			INP_WUNLOCK(inp);
2519b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2520df8bae1dSRodney W. Grimes 			break;
2521df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2522cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
25238501a69cSRobert Watson 			INP_WUNLOCK(inp);
2524b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2525df8bae1dSRodney W. Grimes 			break;
25269e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
25279e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
25289e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
25299e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25309e644c23SMichael Tuexen 			break;
2531a0292f23SGarrett Wollman 		case TCP_NOOPT:
2532cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
25338501a69cSRobert Watson 			INP_WUNLOCK(inp);
2534b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2535a0292f23SGarrett Wollman 			break;
2536a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2537cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
25388501a69cSRobert Watson 			INP_WUNLOCK(inp);
2539b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2540b8af5dfaSRobert Watson 			break;
2541b8af5dfaSRobert Watson 		case TCP_INFO:
2542b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
25438501a69cSRobert Watson 			INP_WUNLOCK(inp);
2544b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2545a0292f23SGarrett Wollman 			break;
2546adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2547adc56f5aSEdward Tomasz Napierala 			{
2548adc56f5aSEdward Tomasz Napierala #ifdef STATS
2549adc56f5aSEdward Tomasz Napierala 			int nheld;
2550adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2551adc56f5aSEdward Tomasz Napierala 
2552adc56f5aSEdward Tomasz Napierala 			error = 0;
2553adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2554adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2555adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2556adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2557adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2558adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2559adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2560adc56f5aSEdward Tomasz Napierala 			else
2561adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2562adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2563adc56f5aSEdward Tomasz Napierala 			if (error)
2564adc56f5aSEdward Tomasz Napierala 				break;
2565adc56f5aSEdward Tomasz Napierala 
2566adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2567adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2568adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2569adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2570adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2571adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2572adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2573adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2574adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2575adc56f5aSEdward Tomasz Napierala 				break;
2576adc56f5aSEdward Tomasz Napierala 			}
2577adc56f5aSEdward Tomasz Napierala 
2578adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2579adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2580adc56f5aSEdward Tomasz Napierala 				goto unhold;
2581adc56f5aSEdward Tomasz Napierala 
2582adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2583adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2584adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2585adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2586adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2587adc56f5aSEdward Tomasz Napierala unhold:
2588adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2589adc56f5aSEdward Tomasz Napierala #else
2590adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2591adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2592adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2593adc56f5aSEdward Tomasz Napierala 			break;
2594adc56f5aSEdward Tomasz Napierala 			}
2595dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2596af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2597dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2598af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2599dbc42409SLawrence Stewart 			break;
260008af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
26012f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
26022f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
26032f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
26042f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
26052f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
260608af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
260708af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
260808af8aacSRandall Stewart 				break;
26092f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
26105a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
26112f3eb7f4SGleb Smirnoff 				break;
26122f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
26135a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
26142f3eb7f4SGleb Smirnoff 				break;
26152f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
26165a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
26172f3eb7f4SGleb Smirnoff 				break;
26182f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
26195a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
26202f3eb7f4SGleb Smirnoff 				break;
26212f3eb7f4SGleb Smirnoff 			}
26222f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
26232f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
26242f3eb7f4SGleb Smirnoff 			break;
262586a996e6SHiren Panchasara #ifdef TCPPCAP
262686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
262786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
2628399a5655SRichard Scheffenegger 			optval = tcp_pcap_get_sock_max(
2629399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
263086a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
263186a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
263286a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
263386a996e6SHiren Panchasara 			break;
263486a996e6SHiren Panchasara #endif
2635281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2636281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2637281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2638281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2639281a0fd4SPatrick Kelsey 			break;
2640e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
26412529f56eSJonathan T. Looney 		case TCP_LOG:
264269c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
26432529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26442529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
26452529f56eSJonathan T. Looney 			break;
26462529f56eSJonathan T. Looney 		case TCP_LOGBUF:
26472529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
26482529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26492529f56eSJonathan T. Looney 			break;
26502529f56eSJonathan T. Looney 		case TCP_LOGID:
26512529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26522529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26532529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26542529f56eSJonathan T. Looney 			break;
26552529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26562529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26572529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26582529f56eSJonathan T. Looney 			error = EINVAL;
26592529f56eSJonathan T. Looney 			break;
2660e24e5683SJonathan T. Looney #endif
2661b2e60773SJohn Baldwin #ifdef KERN_TLS
2662b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2663fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2664b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2665bf256782SMark Johnston 			if (error == 0)
2666bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2667bf256782SMark Johnston 				    sizeof(optval));
2668b2e60773SJohn Baldwin 			break;
2669f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2670fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2671f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2672bf256782SMark Johnston 			if (error == 0)
2673bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2674bf256782SMark Johnston 				    sizeof(optval));
2675f1f93475SJohn Baldwin 			break;
2676b2e60773SJohn Baldwin #endif
2677df8bae1dSRodney W. Grimes 		default:
26788501a69cSRobert Watson 			INP_WUNLOCK(inp);
2679df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2680df8bae1dSRodney W. Grimes 			break;
2681df8bae1dSRodney W. Grimes 		}
2682df8bae1dSRodney W. Grimes 		break;
2683df8bae1dSRodney W. Grimes 	}
2684df8bae1dSRodney W. Grimes 	return (error);
2685df8bae1dSRodney W. Grimes }
26868501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2687bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2688df8bae1dSRodney W. Grimes 
268926e30fbbSDavid Greenman /*
2690df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2691df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2692df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2693df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2694df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2695df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2696df8bae1dSRodney W. Grimes  */
2697623dce13SRobert Watson static void
2698ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2699df8bae1dSRodney W. Grimes {
27009eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
27019eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2702e6e0b5ffSRobert Watson 
270397a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27048501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2705df8bae1dSRodney W. Grimes 
2706623dce13SRobert Watson 	/*
2707623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2708623dce13SRobert Watson 	 * socket is still open.
2709623dce13SRobert Watson 	 */
27108db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
27118db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2712df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2713623dce13SRobert Watson 		KASSERT(tp != NULL,
2714623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2715623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2716243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2717623dce13SRobert Watson 		KASSERT(tp != NULL,
2718623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2719623dce13SRobert Watson 	} else {
2720df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2721df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2722623dce13SRobert Watson 		tcp_usrclosed(tp);
2723ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2724f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2725f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2726df8bae1dSRodney W. Grimes 	}
2727df8bae1dSRodney W. Grimes }
2728df8bae1dSRodney W. Grimes 
2729df8bae1dSRodney W. Grimes /*
2730df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2731df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2732df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2733df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2734df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2735df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2736df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2737df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2738df8bae1dSRodney W. Grimes  */
2739623dce13SRobert Watson static void
2740ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2741df8bae1dSRodney W. Grimes {
2742df8bae1dSRodney W. Grimes 
274397a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27449eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2745e6e0b5ffSRobert Watson 
2746df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2747df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
274809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
274909fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
275009fe6320SNavdeep Parhar #endif
2751550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2752bc65987aSKip Macy 		/* FALLTHROUGH */
2753bc65987aSKip Macy 	case TCPS_CLOSED:
2754df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2755623dce13SRobert Watson 		/*
2756623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2757623dce13SRobert Watson 		 * still open.
2758623dce13SRobert Watson 		 */
2759623dce13SRobert Watson 		KASSERT(tp != NULL,
2760623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2761df8bae1dSRodney W. Grimes 		break;
2762df8bae1dSRodney W. Grimes 
2763a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2764df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2765a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2766a0292f23SGarrett Wollman 		break;
2767a0292f23SGarrett Wollman 
2768df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
276957f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2770df8bae1dSRodney W. Grimes 		break;
2771df8bae1dSRodney W. Grimes 
2772df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
277357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2774df8bae1dSRodney W. Grimes 		break;
2775df8bae1dSRodney W. Grimes 	}
277608af8aacSRandall Stewart 	if (tp->t_acktime == 0)
277708af8aacSRandall Stewart 		tp->t_acktime = ticks;
2778abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27793eeb22cbSRichard Scheffenegger 		tcp_free_sackholes(tp);
27809eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2781abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27827c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27837c72af87SMohan Srinivasan 			int timeout;
27847c72af87SMohan Srinivasan 
27857c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27869077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2787b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2788b6239c4aSAndras Olah 		}
2789df8bae1dSRodney W. Grimes 	}
27907c72af87SMohan Srinivasan }
2791497057eeSRobert Watson 
2792497057eeSRobert Watson #ifdef DDB
2793497057eeSRobert Watson static void
2794497057eeSRobert Watson db_print_indent(int indent)
2795497057eeSRobert Watson {
2796497057eeSRobert Watson 	int i;
2797497057eeSRobert Watson 
2798497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2799497057eeSRobert Watson 		db_printf(" ");
2800497057eeSRobert Watson }
2801497057eeSRobert Watson 
2802497057eeSRobert Watson static void
2803497057eeSRobert Watson db_print_tstate(int t_state)
2804497057eeSRobert Watson {
2805497057eeSRobert Watson 
2806497057eeSRobert Watson 	switch (t_state) {
2807497057eeSRobert Watson 	case TCPS_CLOSED:
2808497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2809497057eeSRobert Watson 		return;
2810497057eeSRobert Watson 
2811497057eeSRobert Watson 	case TCPS_LISTEN:
2812497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2813497057eeSRobert Watson 		return;
2814497057eeSRobert Watson 
2815497057eeSRobert Watson 	case TCPS_SYN_SENT:
2816497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2817497057eeSRobert Watson 		return;
2818497057eeSRobert Watson 
2819497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2820497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2821497057eeSRobert Watson 		return;
2822497057eeSRobert Watson 
2823497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2824497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2825497057eeSRobert Watson 		return;
2826497057eeSRobert Watson 
2827497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2828497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2829497057eeSRobert Watson 		return;
2830497057eeSRobert Watson 
2831497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2832497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2833497057eeSRobert Watson 		return;
2834497057eeSRobert Watson 
2835497057eeSRobert Watson 	case TCPS_CLOSING:
2836497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2837497057eeSRobert Watson 		return;
2838497057eeSRobert Watson 
2839497057eeSRobert Watson 	case TCPS_LAST_ACK:
2840497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2841497057eeSRobert Watson 		return;
2842497057eeSRobert Watson 
2843497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2844497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2845497057eeSRobert Watson 		return;
2846497057eeSRobert Watson 
2847497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2848497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2849497057eeSRobert Watson 		return;
2850497057eeSRobert Watson 
2851497057eeSRobert Watson 	default:
2852497057eeSRobert Watson 		db_printf("unknown");
2853497057eeSRobert Watson 		return;
2854497057eeSRobert Watson 	}
2855497057eeSRobert Watson }
2856497057eeSRobert Watson 
2857497057eeSRobert Watson static void
2858497057eeSRobert Watson db_print_tflags(u_int t_flags)
2859497057eeSRobert Watson {
2860497057eeSRobert Watson 	int comma;
2861497057eeSRobert Watson 
2862497057eeSRobert Watson 	comma = 0;
2863497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2864497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2865497057eeSRobert Watson 		comma = 1;
2866497057eeSRobert Watson 	}
2867497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2868497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2869497057eeSRobert Watson 		comma = 1;
2870497057eeSRobert Watson 	}
2871497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2872497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2873497057eeSRobert Watson 		comma = 1;
2874497057eeSRobert Watson 	}
2875497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2876497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2877497057eeSRobert Watson 		comma = 1;
2878497057eeSRobert Watson 	}
2879497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2880497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2881497057eeSRobert Watson 		comma = 1;
2882497057eeSRobert Watson 	}
2883497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2884497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2885497057eeSRobert Watson 		comma = 1;
2886497057eeSRobert Watson 	}
2887497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2888497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2889497057eeSRobert Watson 		comma = 1;
2890497057eeSRobert Watson 	}
2891497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2892497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2893497057eeSRobert Watson 		comma = 1;
2894497057eeSRobert Watson 	}
2895497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2896497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2897497057eeSRobert Watson 		comma = 1;
2898497057eeSRobert Watson 	}
2899497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2900497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2901497057eeSRobert Watson 		comma = 1;
2902497057eeSRobert Watson 	}
2903497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2904497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2905497057eeSRobert Watson 		comma = 1;
2906497057eeSRobert Watson 	}
2907497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2908497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2909497057eeSRobert Watson 		comma = 1;
2910497057eeSRobert Watson 	}
2911497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2912497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2913497057eeSRobert Watson 		comma = 1;
2914497057eeSRobert Watson 	}
29153f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
29163f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
29173f169c54SRichard Scheffenegger 		comma = 1;
29183f169c54SRichard Scheffenegger 	}
2919497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2920497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2921497057eeSRobert Watson 		comma = 1;
2922497057eeSRobert Watson 	}
2923493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2924493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2925497057eeSRobert Watson 		comma = 1;
2926497057eeSRobert Watson 	}
2927497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2928497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2929497057eeSRobert Watson 		comma = 1;
2930497057eeSRobert Watson 	}
2931497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2932497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2933497057eeSRobert Watson 		comma = 1;
2934497057eeSRobert Watson 	}
2935497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2936497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2937497057eeSRobert Watson 		comma = 1;
2938497057eeSRobert Watson 	}
2939dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2940dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2941dbc42409SLawrence Stewart 		comma = 1;
2942dbc42409SLawrence Stewart 	}
2943497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2944497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2945497057eeSRobert Watson 		comma = 1;
2946497057eeSRobert Watson 	}
29473f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
29483f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
29493f169c54SRichard Scheffenegger 		comma = 1;
29503f169c54SRichard Scheffenegger 	}
2951497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2952497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2953497057eeSRobert Watson 		comma = 1;
2954497057eeSRobert Watson 	}
2955497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2956497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2957497057eeSRobert Watson 		comma = 1;
2958497057eeSRobert Watson 	}
2959497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2960497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2961497057eeSRobert Watson 		comma = 1;
2962497057eeSRobert Watson 	}
2963281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2964281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2965281a0fd4SPatrick Kelsey 		comma = 1;
2966281a0fd4SPatrick Kelsey 	}
2967497057eeSRobert Watson }
2968497057eeSRobert Watson 
2969497057eeSRobert Watson static void
29703cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29713cf38784SMichael Tuexen {
29723cf38784SMichael Tuexen 	int comma;
29733cf38784SMichael Tuexen 
29743cf38784SMichael Tuexen 	comma = 0;
29753f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29763f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29773f169c54SRichard Scheffenegger 		comma = 1;
29783f169c54SRichard Scheffenegger 	}
29793f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29803f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29813f169c54SRichard Scheffenegger 		comma = 1;
29823f169c54SRichard Scheffenegger 	}
29833f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29843f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29853f169c54SRichard Scheffenegger 		comma = 1;
29863f169c54SRichard Scheffenegger 	}
29873f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29883f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29893f169c54SRichard Scheffenegger 		comma = 1;
29903f169c54SRichard Scheffenegger 	}
29913f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29923f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29933f169c54SRichard Scheffenegger 		comma = 1;
29943f169c54SRichard Scheffenegger 	}
29953cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29963cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29973cf38784SMichael Tuexen 		comma = 1;
29983cf38784SMichael Tuexen 	}
29993f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
30003f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
30013f169c54SRichard Scheffenegger 		comma = 1;
30023f169c54SRichard Scheffenegger 	}
30033f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
30043f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
30053f169c54SRichard Scheffenegger 		comma = 1;
30063f169c54SRichard Scheffenegger 	}
30073f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
30083f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
30093f169c54SRichard Scheffenegger 		comma = 1;
30103f169c54SRichard Scheffenegger 	}
30113f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
30123f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30133f169c54SRichard Scheffenegger 		comma = 1;
30143f169c54SRichard Scheffenegger 	}
30153cf38784SMichael Tuexen }
30163cf38784SMichael Tuexen 
30173cf38784SMichael Tuexen static void
3018497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3019497057eeSRobert Watson {
3020497057eeSRobert Watson 	int comma;
3021497057eeSRobert Watson 
3022497057eeSRobert Watson 	comma = 0;
3023497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3024497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3025497057eeSRobert Watson 		comma = 1;
3026497057eeSRobert Watson 	}
3027497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3028497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3029497057eeSRobert Watson 		comma = 1;
3030497057eeSRobert Watson 	}
3031497057eeSRobert Watson }
3032497057eeSRobert Watson 
3033497057eeSRobert Watson static void
3034497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3035497057eeSRobert Watson {
3036497057eeSRobert Watson 
3037497057eeSRobert Watson 	db_print_indent(indent);
3038497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3039497057eeSRobert Watson 
3040497057eeSRobert Watson 	indent += 2;
3041497057eeSRobert Watson 
3042497057eeSRobert Watson 	db_print_indent(indent);
3043497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3044c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3045497057eeSRobert Watson 
3046497057eeSRobert Watson 	db_print_indent(indent);
3047446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3048446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3049497057eeSRobert Watson 
3050497057eeSRobert Watson 	db_print_indent(indent);
3051497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3052497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3053497057eeSRobert Watson 	db_printf(")\n");
3054497057eeSRobert Watson 
3055497057eeSRobert Watson 	db_print_indent(indent);
3056497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3057497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3058497057eeSRobert Watson 	db_printf(")\n");
3059497057eeSRobert Watson 
3060497057eeSRobert Watson 	db_print_indent(indent);
30613cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30623cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30633cf38784SMichael Tuexen 	db_printf(")\n");
30643cf38784SMichael Tuexen 
30653cf38784SMichael Tuexen 	db_print_indent(indent);
3066fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3067497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3068497057eeSRobert Watson 
3069497057eeSRobert Watson 	db_print_indent(indent);
3070497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3071497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3072497057eeSRobert Watson 
3073497057eeSRobert Watson 	db_print_indent(indent);
3074497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3075497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3076497057eeSRobert Watson 
3077497057eeSRobert Watson 	db_print_indent(indent);
30783ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3079497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3080497057eeSRobert Watson 
3081497057eeSRobert Watson 	db_print_indent(indent);
30823ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
30831c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3084497057eeSRobert Watson 
3085497057eeSRobert Watson 	db_print_indent(indent);
30863ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
30871c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3088497057eeSRobert Watson 
3089497057eeSRobert Watson 	db_print_indent(indent);
30900c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
30910c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3092497057eeSRobert Watson 
3093497057eeSRobert Watson 	db_print_indent(indent);
30941c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
30951c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3096497057eeSRobert Watson 
3097497057eeSRobert Watson 	db_print_indent(indent);
30981c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
30991c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3100497057eeSRobert Watson 
3101497057eeSRobert Watson 	db_print_indent(indent);
3102bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3103bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3104497057eeSRobert Watson 
3105497057eeSRobert Watson 	db_print_indent(indent);
310618b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3107497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3108497057eeSRobert Watson 
3109497057eeSRobert Watson 	db_print_indent(indent);
3110497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3111497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3112497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3113497057eeSRobert Watson 
3114497057eeSRobert Watson 	db_print_indent(indent);
3115497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3116497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3117497057eeSRobert Watson 
3118497057eeSRobert Watson 	db_print_indent(indent);
31199f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31201a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3121497057eeSRobert Watson 
3122497057eeSRobert Watson 	db_print_indent(indent);
3123497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31243ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3125497057eeSRobert Watson 
3126497057eeSRobert Watson 	db_print_indent(indent);
31273ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31289f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3129497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3130497057eeSRobert Watson 
3131497057eeSRobert Watson 	db_print_indent(indent);
31323529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31333529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3134497057eeSRobert Watson 
3135497057eeSRobert Watson 	db_print_indent(indent);
3136a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3137a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3138497057eeSRobert Watson 
3139497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3140497057eeSRobert Watson 
3141497057eeSRobert Watson 	db_print_indent(indent);
3142497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3143497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3144497057eeSRobert Watson }
3145497057eeSRobert Watson 
3146497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3147497057eeSRobert Watson {
3148497057eeSRobert Watson 	struct tcpcb *tp;
3149497057eeSRobert Watson 
3150497057eeSRobert Watson 	if (!have_addr) {
3151497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3152497057eeSRobert Watson 		return;
3153497057eeSRobert Watson 	}
3154497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3155497057eeSRobert Watson 
3156497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3157497057eeSRobert Watson }
3158497057eeSRobert Watson #endif
3159