xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 7cbb6b6e28db33095a1cf7a8887921a5ec969824)
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);
175baee801cSMichael Tuexen 	tp = tcp_newtcpcb(inp, NULL);
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 
394*7cbb6b6eSMark Johnston 	if (error == 0)
395*7cbb6b6eSMark Johnston 		in_pcblisten(inp);
396dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
397281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
39818a75309SPatrick Kelsey 
399623dce13SRobert Watson out:
40000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4028501a69cSRobert Watson 	INP_WUNLOCK(inp);
403623dce13SRobert Watson 	return (error);
4042c37256eSGarrett Wollman }
405b287c6c7SBjoern A. Zeeb #endif /* INET */
4062c37256eSGarrett Wollman 
407fb59c426SYoshinobu Inoue #ifdef INET6
408fb59c426SYoshinobu Inoue static int
409d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
410fb59c426SYoshinobu Inoue {
411fb59c426SYoshinobu Inoue 	int error = 0;
412f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
413c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4144a91aa8fSMichael Tuexen 	u_char vflagsav;
415fb59c426SYoshinobu Inoue 
416623dce13SRobert Watson 	inp = sotoinpcb(so);
417623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4188501a69cSRobert Watson 	INP_WLOCK(inp);
41953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
420c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
421c2399dd2SMichael Tuexen 		return (EINVAL);
422623dce13SRobert Watson 	}
423623dce13SRobert Watson 	tp = intotcpcb(inp);
424c2399dd2SMichael Tuexen 
425c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
426c2399dd2SMichael Tuexen 
4270daccb9cSRobert Watson 	SOCK_LOCK(so);
4280daccb9cSRobert Watson 	error = solisten_proto_check(so);
429bd4a39ccSMark Johnston 	if (error != 0) {
430bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
431bd4a39ccSMark Johnston 		goto out;
432bd4a39ccSMark Johnston 	}
433fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
434bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
435fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
43666ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
437fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
438bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
439fb59c426SYoshinobu Inoue 	}
440fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4410daccb9cSRobert Watson 	if (error == 0) {
44257f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
443d374e81eSRobert Watson 		solisten_proto(so, backlog);
44409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44537cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
44609fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
44709fe6320SNavdeep Parhar #endif
448bd4a39ccSMark Johnston 	} else {
449bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4500daccb9cSRobert Watson 	}
4510daccb9cSRobert Watson 	SOCK_UNLOCK(so);
452623dce13SRobert Watson 
453*7cbb6b6eSMark Johnston 	if (error == 0)
454*7cbb6b6eSMark Johnston 		in_pcblisten(inp);
455dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
456281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45718a75309SPatrick Kelsey 
4584a91aa8fSMichael Tuexen 	if (error != 0)
4594a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4604a91aa8fSMichael Tuexen 
461623dce13SRobert Watson out:
46200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4635d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4648501a69cSRobert Watson 	INP_WUNLOCK(inp);
465623dce13SRobert Watson 	return (error);
466fb59c426SYoshinobu Inoue }
467fb59c426SYoshinobu Inoue #endif /* INET6 */
468fb59c426SYoshinobu Inoue 
469b287c6c7SBjoern A. Zeeb #ifdef INET
4702c37256eSGarrett Wollman /*
4712c37256eSGarrett Wollman  * Initiate connection to peer.
4722c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4732c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4742c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4752c37256eSGarrett Wollman  * Send initial segment on connection.
4762c37256eSGarrett Wollman  */
4772c37256eSGarrett Wollman static int
478b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4792c37256eSGarrett Wollman {
480109eb549SGleb Smirnoff 	struct epoch_tracker et;
4812c37256eSGarrett Wollman 	int error = 0;
482f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
483c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4842c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4852c37256eSGarrett Wollman 
486623dce13SRobert Watson 	inp = sotoinpcb(so);
487623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4888501a69cSRobert Watson 	INP_WLOCK(inp);
489eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
490c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
491c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
492c2399dd2SMichael Tuexen 	}
493c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
494c2399dd2SMichael Tuexen 
495c2399dd2SMichael Tuexen 	sinp = (struct sockaddr_in *)nam;
496c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET) {
497c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
498623dce13SRobert Watson 		goto out;
499623dce13SRobert Watson 	}
500c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sinp)) {
501c2399dd2SMichael Tuexen 		error = EINVAL;
502c2399dd2SMichael Tuexen 		goto out;
503c2399dd2SMichael Tuexen 	}
504c2399dd2SMichael Tuexen 	/*
505c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
506c2399dd2SMichael Tuexen 	 */
507c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
508c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
509c2399dd2SMichael Tuexen 		goto out;
510c2399dd2SMichael Tuexen 	}
511c2399dd2SMichael Tuexen 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
512c2399dd2SMichael Tuexen 		error = EACCES;
513c2399dd2SMichael Tuexen 		goto out;
514c2399dd2SMichael Tuexen 	}
515c2399dd2SMichael Tuexen 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
516c2399dd2SMichael Tuexen 		goto out;
517bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
518bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
519bd4a39ccSMark Johnston 		goto out;
520bd4a39ccSMark Johnston 	}
521c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
522a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
523c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
52509fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
52637cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
52709fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
528c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52909fe6320SNavdeep Parhar #endif
53009fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
53140fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5321d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5331d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
534c1604fe4SGleb Smirnoff out_in_epoch:
535109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
536623dce13SRobert Watson out:
53700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
538e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5398501a69cSRobert Watson 	INP_WUNLOCK(inp);
540623dce13SRobert Watson 	return (error);
5412c37256eSGarrett Wollman }
542b287c6c7SBjoern A. Zeeb #endif /* INET */
5432c37256eSGarrett Wollman 
544fb59c426SYoshinobu Inoue #ifdef INET6
545fb59c426SYoshinobu Inoue static int
546b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
547fb59c426SYoshinobu Inoue {
548109eb549SGleb Smirnoff 	struct epoch_tracker et;
549fb59c426SYoshinobu Inoue 	int error = 0;
550f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
551c2399dd2SMichael Tuexen 	struct tcpcb *tp;
5520ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5534a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5544a91aa8fSMichael Tuexen 	u_char vflagsav;
555623dce13SRobert Watson 
556623dce13SRobert Watson 	inp = sotoinpcb(so);
557623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5588501a69cSRobert Watson 	INP_WLOCK(inp);
559c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
560c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
561c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
562c2399dd2SMichael Tuexen 	}
563c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
564c2399dd2SMichael Tuexen 
5654a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5664a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
567c2399dd2SMichael Tuexen 
568c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
569c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
570c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
571c2399dd2SMichael Tuexen 		goto out;
572c2399dd2SMichael Tuexen 	}
573c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sin6)) {
574c2399dd2SMichael Tuexen 		error = EINVAL;
575c2399dd2SMichael Tuexen 		goto out;
576c2399dd2SMichael Tuexen 	}
577c2399dd2SMichael Tuexen 	/*
578c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
579c2399dd2SMichael Tuexen 	 */
580c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
581c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
582623dce13SRobert Watson 		goto out;
583623dce13SRobert Watson 	}
584bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
585bd4a39ccSMark Johnston 		error = EINVAL;
586bd4a39ccSMark Johnston 		goto out;
587bd4a39ccSMark Johnston 	}
588b287c6c7SBjoern A. Zeeb #ifdef INET
589fa046d87SRobert Watson 	/*
590fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
591fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
592fa046d87SRobert Watson 	 * Is this a significant problem?
593fa046d87SRobert Watson 	 */
5940ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
595fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
596fb59c426SYoshinobu Inoue 
597d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
598d46a5312SMaxim Konovalov 			error = EINVAL;
599d46a5312SMaxim Konovalov 			goto out;
600d46a5312SMaxim Konovalov 		}
6015dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6025dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6035dba6adaSMichael Tuexen 			goto out;
6045dba6adaSMichael Tuexen 		}
60533841545SHajimu UMEMOTO 
6060ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
607888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
608888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
609888973f5SMichael Tuexen 			goto out;
610888973f5SMichael Tuexen 		}
611f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
612f903a308SMichael Tuexen 			error = EACCES;
6132cf21ae5SRandall Stewart 			goto out;
6142cf21ae5SRandall Stewart 		}
615b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
616b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
617413628a7SBjoern A. Zeeb 			goto out;
6184a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6194a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
620c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
621a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
622c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62409fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
625adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62609fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
627c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62809fe6320SNavdeep Parhar #endif
62940fa3e40SGleb Smirnoff 		error = tcp_output(tp);
630c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6315dba6adaSMichael Tuexen 	} else {
6325dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6335dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6345dba6adaSMichael Tuexen 			goto out;
6355dba6adaSMichael Tuexen 		}
636fb59c426SYoshinobu Inoue 	}
637b287c6c7SBjoern A. Zeeb #endif
6384a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6394a91aa8fSMichael Tuexen 		goto out;
640fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
641fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
642dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6430773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
644a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6450773b44eSGleb Smirnoff 		goto out_in_epoch;
64609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
648adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
64909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6500773b44eSGleb Smirnoff 		goto out_in_epoch;
65109fe6320SNavdeep Parhar #endif
65209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
65340fa3e40SGleb Smirnoff 	error = tcp_output(tp);
654c1604fe4SGleb Smirnoff out_in_epoch:
655109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
656623dce13SRobert Watson out:
6571d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6581d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6594a91aa8fSMichael Tuexen 	/*
6604a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6614a91aa8fSMichael Tuexen 	 * the flags we modified.
6624a91aa8fSMichael Tuexen 	 */
6634a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6644a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6654a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6664a91aa8fSMichael Tuexen 	}
6674a91aa8fSMichael Tuexen 
66800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6708501a69cSRobert Watson 	INP_WUNLOCK(inp);
671623dce13SRobert Watson 	return (error);
672fb59c426SYoshinobu Inoue }
673fb59c426SYoshinobu Inoue #endif /* INET6 */
674fb59c426SYoshinobu Inoue 
6752c37256eSGarrett Wollman /*
6762c37256eSGarrett Wollman  * Initiate disconnect from peer.
6772c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6782c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6792c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6802c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6812c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6822c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6832c37256eSGarrett Wollman  *
6842c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6852c37256eSGarrett Wollman  */
6862c37256eSGarrett Wollman static int
6872c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6882c37256eSGarrett Wollman {
689f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
690623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6916573d758SMatt Macy 	struct epoch_tracker et;
6922c37256eSGarrett Wollman 
69397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
694623dce13SRobert Watson 	inp = sotoinpcb(so);
695623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6968501a69cSRobert Watson 	INP_WLOCK(inp);
697623dce13SRobert Watson 	tp = intotcpcb(inp);
698c2399dd2SMichael Tuexen 
699cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
700cda6bdbaSJohn Baldwin 		goto out;
701623dce13SRobert Watson 	tcp_disconnect(tp);
702623dce13SRobert Watson out:
703c91dd7a0SGleb Smirnoff 	tcp_bblog_pru(tp, PRU_DISCONNECT, 0);
7045d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7058501a69cSRobert Watson 	INP_WUNLOCK(inp);
70697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
707c91dd7a0SGleb Smirnoff 	return (0);
7082c37256eSGarrett Wollman }
7092c37256eSGarrett Wollman 
710b287c6c7SBjoern A. Zeeb #ifdef INET
7112c37256eSGarrett Wollman /*
7128296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7138296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7142c37256eSGarrett Wollman  */
7152c37256eSGarrett Wollman static int
716cfb1e929SGleb Smirnoff tcp_usr_accept(struct socket *so, struct sockaddr *sa)
7172c37256eSGarrett Wollman {
718c2399dd2SMichael Tuexen 	struct inpcb *inp;
719c2399dd2SMichael Tuexen 	struct tcpcb *tp;
720cfb1e929SGleb Smirnoff 	int error = 0;
7212c37256eSGarrett Wollman 
722f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
723623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7248501a69cSRobert Watson 	INP_WLOCK(inp);
72553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
726c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
727c2399dd2SMichael Tuexen 		return (ECONNABORTED);
728623dce13SRobert Watson 	}
7291db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
730f76fcf6dSJeffrey Hsu 
731cfb1e929SGleb Smirnoff 	if (so->so_state & SS_ISDISCONNECTED)
732c2399dd2SMichael Tuexen 		error = ECONNABORTED;
733cfb1e929SGleb Smirnoff 	else
734cfb1e929SGleb Smirnoff 		*(struct sockaddr_in *)sa = (struct sockaddr_in ){
735cfb1e929SGleb Smirnoff 			.sin_family = AF_INET,
736cfb1e929SGleb Smirnoff 			.sin_len = sizeof(struct sockaddr_in),
737cfb1e929SGleb Smirnoff 			.sin_port = inp->inp_fport,
738cfb1e929SGleb Smirnoff 			.sin_addr = inp->inp_faddr,
739cfb1e929SGleb Smirnoff 		};
74000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7415d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7428501a69cSRobert Watson 	INP_WUNLOCK(inp);
743cfb1e929SGleb Smirnoff 
744cfb1e929SGleb Smirnoff 	return (error);
7452c37256eSGarrett Wollman }
746b287c6c7SBjoern A. Zeeb #endif /* INET */
7472c37256eSGarrett Wollman 
748fb59c426SYoshinobu Inoue #ifdef INET6
749fb59c426SYoshinobu Inoue static int
750cfb1e929SGleb Smirnoff tcp6_usr_accept(struct socket *so, struct sockaddr *sa)
751fb59c426SYoshinobu Inoue {
752c2399dd2SMichael Tuexen 	struct inpcb *inp;
753c2399dd2SMichael Tuexen 	struct tcpcb *tp;
754cfb1e929SGleb Smirnoff 	int error = 0;
755fb59c426SYoshinobu Inoue 
756f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
757623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
7588501a69cSRobert Watson 	INP_WLOCK(inp);
75953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
760c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
761c2399dd2SMichael Tuexen 		return (ECONNABORTED);
762623dce13SRobert Watson 	}
7631db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
764623dce13SRobert Watson 
765c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
766c2399dd2SMichael Tuexen 		error = ECONNABORTED;
76726ef6ac4SDon Lewis 	} else {
768cfb1e929SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV4) {
769cfb1e929SGleb Smirnoff 			struct sockaddr_in sin = {
770cfb1e929SGleb Smirnoff 				.sin_family = AF_INET,
771cfb1e929SGleb Smirnoff 				.sin_len = sizeof(struct sockaddr_in),
772cfb1e929SGleb Smirnoff 				.sin_port = inp->inp_fport,
773cfb1e929SGleb Smirnoff 				.sin_addr = inp->inp_faddr,
774cfb1e929SGleb Smirnoff 			};
775cfb1e929SGleb Smirnoff 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
776cfb1e929SGleb Smirnoff 		} else {
777cfb1e929SGleb Smirnoff 			*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
778cfb1e929SGleb Smirnoff 				.sin6_family = AF_INET6,
779cfb1e929SGleb Smirnoff 				.sin6_len = sizeof(struct sockaddr_in6),
780cfb1e929SGleb Smirnoff 				.sin6_port = inp->inp_fport,
781cfb1e929SGleb Smirnoff 				.sin6_addr = inp->in6p_faddr,
782cfb1e929SGleb Smirnoff 			};
783cfb1e929SGleb Smirnoff 			/* XXX: should catch errors */
784cfb1e929SGleb Smirnoff 			(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
785cfb1e929SGleb Smirnoff 		}
78626ef6ac4SDon Lewis 	}
78726ef6ac4SDon Lewis 
78800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7895d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7908501a69cSRobert Watson 	INP_WUNLOCK(inp);
791cfb1e929SGleb Smirnoff 
792cfb1e929SGleb Smirnoff 	return (error);
793fb59c426SYoshinobu Inoue }
794fb59c426SYoshinobu Inoue #endif /* INET6 */
795f76fcf6dSJeffrey Hsu 
796f76fcf6dSJeffrey Hsu /*
7972c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7982c37256eSGarrett Wollman  */
7992c37256eSGarrett Wollman static int
8005bba2728SGleb Smirnoff tcp_usr_shutdown(struct socket *so, enum shutdown_how how)
8012c37256eSGarrett Wollman {
8026573d758SMatt Macy 	struct epoch_tracker et;
8035bba2728SGleb Smirnoff 	struct inpcb *inp = sotoinpcb(so);
8045bba2728SGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
8055bba2728SGleb Smirnoff 	int error = 0;
8062c37256eSGarrett Wollman 
8075bba2728SGleb Smirnoff 	SOCK_LOCK(so);
8085bba2728SGleb Smirnoff 	if (SOLISTENING(so)) {
8095bba2728SGleb Smirnoff 		if (how != SHUT_WR) {
8105bba2728SGleb Smirnoff 			so->so_error = ECONNABORTED;
8115bba2728SGleb Smirnoff 			solisten_wakeup(so);	/* unlocks so */
8125bba2728SGleb Smirnoff 		} else
8135bba2728SGleb Smirnoff 			SOCK_UNLOCK(so);
814abe8379bSGleb Smirnoff 		return (ENOTCONN);
815abe8379bSGleb Smirnoff 	} else if ((so->so_state &
816abe8379bSGleb Smirnoff 	    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
817abe8379bSGleb Smirnoff 		SOCK_UNLOCK(so);
818abe8379bSGleb Smirnoff 		return (ENOTCONN);
8195bba2728SGleb Smirnoff 	}
8205bba2728SGleb Smirnoff 	SOCK_UNLOCK(so);
8215bba2728SGleb Smirnoff 
8225bba2728SGleb Smirnoff 	switch (how) {
8235bba2728SGleb Smirnoff 	case SHUT_RD:
824ce69e373SGleb Smirnoff 		sorflush(so);
8255bba2728SGleb Smirnoff 		break;
8265bba2728SGleb Smirnoff 	case SHUT_RDWR:
827ce69e373SGleb Smirnoff 		sorflush(so);
8285bba2728SGleb Smirnoff 		/* FALLTHROUGH */
8295bba2728SGleb Smirnoff 	case SHUT_WR:
8305bba2728SGleb Smirnoff 		/*
8315bba2728SGleb Smirnoff 		 * XXXGL: mimicing old soshutdown() here. But shouldn't we
8325bba2728SGleb Smirnoff 		 * return ECONNRESEST for SHUT_RD as well?
8335bba2728SGleb Smirnoff 		 */
8348501a69cSRobert Watson 		INP_WLOCK(inp);
83553af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
8360af4ce45SGleb Smirnoff 			INP_WUNLOCK(inp);
8370af4ce45SGleb Smirnoff 			return (ECONNRESET);
838623dce13SRobert Watson 		}
839c2399dd2SMichael Tuexen 
8402c37256eSGarrett Wollman 		socantsendmore(so);
8415bba2728SGleb Smirnoff 		NET_EPOCH_ENTER(et);
842623dce13SRobert Watson 		tcp_usrclosed(tp);
843f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
84400812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8455d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
846f64dc2abSGleb Smirnoff 		error = tcp_unlock_or_drop(tp, error);
84797a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
8485bba2728SGleb Smirnoff 	}
8495bba2728SGleb Smirnoff 	wakeup(&so->so_timeo);
850623dce13SRobert Watson 
851623dce13SRobert Watson 	return (error);
8522c37256eSGarrett Wollman }
8532c37256eSGarrett Wollman 
8542c37256eSGarrett Wollman /*
8552c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8562c37256eSGarrett Wollman  */
8572c37256eSGarrett Wollman static int
8582c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8592c37256eSGarrett Wollman {
860109eb549SGleb Smirnoff 	struct epoch_tracker et;
861f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
862c2399dd2SMichael Tuexen 	struct tcpcb *tp;
863f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8642c37256eSGarrett Wollman 
865623dce13SRobert Watson 	inp = sotoinpcb(so);
866623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8678501a69cSRobert Watson 	INP_WLOCK(inp);
86853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
86937a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
87037a7f557SGleb Smirnoff 		return (ECONNRESET);
871623dce13SRobert Watson 	}
87237a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
873c2399dd2SMichael Tuexen 
87437a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
875281a0fd4SPatrick Kelsey 	/*
876281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
877281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
878281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
879281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
880281a0fd4SPatrick Kelsey 	 * expires.
881281a0fd4SPatrick Kelsey 	 */
882dd7b86e2SGleb Smirnoff 	if ((tp->t_flags & TF_FASTOPEN) && (tp->t_state == TCPS_SYN_RECEIVED))
883281a0fd4SPatrick Kelsey 		goto out;
88409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
88509fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
88609fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
887460cf046SNavdeep Parhar 	else
88809fe6320SNavdeep Parhar #endif
889f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
890623dce13SRobert Watson out:
89100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
8925d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
893f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
894f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
895623dce13SRobert Watson 	return (error);
8962c37256eSGarrett Wollman }
8972c37256eSGarrett Wollman 
8982c37256eSGarrett Wollman /*
8992c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9009c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9019c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9029c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9039c9906e9SPeter Wemm  * generally are caller-frees.
9042c37256eSGarrett Wollman  */
9052c37256eSGarrett Wollman static int
90657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
907b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9082c37256eSGarrett Wollman {
90997a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9102c37256eSGarrett Wollman 	int error = 0;
911f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
912c2399dd2SMichael Tuexen 	struct tcpcb *tp;
913888973f5SMichael Tuexen #ifdef INET
91451e08d53SMichael Tuexen #ifdef INET6
91551e08d53SMichael Tuexen 	struct sockaddr_in sin;
91651e08d53SMichael Tuexen #endif
91751e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
918888973f5SMichael Tuexen #endif
919fb59c426SYoshinobu Inoue #ifdef INET6
920a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
921fb59c426SYoshinobu Inoue 	int isipv6;
922fb59c426SYoshinobu Inoue #endif
9234a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9244a91aa8fSMichael Tuexen 	u_char vflagsav;
9254a91aa8fSMichael Tuexen 	bool restoreflags;
9262c37256eSGarrett Wollman 
9274287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9284287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9294287aa56SGleb Smirnoff 	INP_WLOCK(inp);
93053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9314287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9324287aa56SGleb Smirnoff 			m_freem(m);
9334287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9344287aa56SGleb Smirnoff 		return (ECONNRESET);
9354287aa56SGleb Smirnoff 	}
936c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
9374287aa56SGleb Smirnoff 
9384287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9394287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9404287aa56SGleb Smirnoff 	restoreflags = false;
9414287aa56SGleb Smirnoff 
9424287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
943c2399dd2SMichael Tuexen 	if (control != NULL) {
944c2399dd2SMichael Tuexen 		/* TCP doesn't do control messages (rights, creds, etc) */
945c2399dd2SMichael Tuexen 		if (control->m_len > 0) {
946c2399dd2SMichael Tuexen 			m_freem(control);
947c2399dd2SMichael Tuexen 			error = EINVAL;
948c2399dd2SMichael Tuexen 			goto out;
949c2399dd2SMichael Tuexen 		}
950c2399dd2SMichael Tuexen 		m_freem(control);	/* empty control, just free it */
951c2399dd2SMichael Tuexen 	}
952c2399dd2SMichael Tuexen 
9537d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9547d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
955d3b6c96bSRandall Stewart 		goto out;
9567d2608a5SMark Johnston 
957888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
958bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
959bd4a39ccSMark Johnston 			error = EINVAL;
960bd4a39ccSMark Johnston 			goto out;
961bd4a39ccSMark Johnston 		}
962888973f5SMichael Tuexen 		switch (nam->sa_family) {
963888973f5SMichael Tuexen #ifdef INET
964888973f5SMichael Tuexen 		case AF_INET:
965888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
966888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
967888973f5SMichael Tuexen 				error = EINVAL;
968888973f5SMichael Tuexen 				goto out;
969888973f5SMichael Tuexen 			}
970888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
971888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
972888973f5SMichael Tuexen 				goto out;
973888973f5SMichael Tuexen 			}
974888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
975888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
976888973f5SMichael Tuexen 				goto out;
977888973f5SMichael Tuexen 			}
978f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
979f903a308SMichael Tuexen 				error = EACCES;
9802cf21ae5SRandall Stewart 				goto out;
9812cf21ae5SRandall Stewart 			}
982888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9837d2608a5SMark Johnston 			    &sinp->sin_addr)))
984888973f5SMichael Tuexen 				goto out;
985888973f5SMichael Tuexen #ifdef INET6
986888973f5SMichael Tuexen 			isipv6 = 0;
987888973f5SMichael Tuexen #endif
988888973f5SMichael Tuexen 			break;
989888973f5SMichael Tuexen #endif /* INET */
990888973f5SMichael Tuexen #ifdef INET6
991888973f5SMichael Tuexen 		case AF_INET6:
9920ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
9930ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
994888973f5SMichael Tuexen 				error = EINVAL;
995888973f5SMichael Tuexen 				goto out;
996888973f5SMichael Tuexen 			}
997e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
998e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
999e240ce42SMichael Tuexen 				goto out;
1000e240ce42SMichael Tuexen 			}
10010ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1002888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1003888973f5SMichael Tuexen 				goto out;
1004888973f5SMichael Tuexen 			}
10050ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1006888973f5SMichael Tuexen #ifdef INET
1007888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1008888973f5SMichael Tuexen 					error = EINVAL;
1009888973f5SMichael Tuexen 					goto out;
1010888973f5SMichael Tuexen 				}
1011888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1012888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1013888973f5SMichael Tuexen 					goto out;
1014888973f5SMichael Tuexen 				}
10154a91aa8fSMichael Tuexen 				restoreflags = true;
1016888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1017888973f5SMichael Tuexen 				sinp = &sin;
10180ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1019888973f5SMichael Tuexen 				if (IN_MULTICAST(
1020888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1021888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1022888973f5SMichael Tuexen 					goto out;
1023888973f5SMichael Tuexen 				}
1024888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10257d2608a5SMark Johnston 				    &sinp->sin_addr)))
1026888973f5SMichael Tuexen 					goto out;
1027888973f5SMichael Tuexen 				isipv6 = 0;
1028888973f5SMichael Tuexen #else /* !INET */
1029888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1030888973f5SMichael Tuexen 				goto out;
1031888973f5SMichael Tuexen #endif /* INET */
1032888973f5SMichael Tuexen 			} else {
1033888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1034888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1035888973f5SMichael Tuexen 					goto out;
1036888973f5SMichael Tuexen 				}
10374a91aa8fSMichael Tuexen 				restoreflags = true;
1038888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1039888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1040888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10417d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1042888973f5SMichael Tuexen 					goto out;
1043888973f5SMichael Tuexen 				isipv6 = 1;
1044888973f5SMichael Tuexen 			}
1045888973f5SMichael Tuexen 			break;
1046888973f5SMichael Tuexen #endif /* INET6 */
1047888973f5SMichael Tuexen 		default:
1048888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1049888973f5SMichael Tuexen 			goto out;
1050888973f5SMichael Tuexen 		}
1051888973f5SMichael Tuexen 	}
10522c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
105308af8aacSRandall Stewart 		if (tp->t_acktime == 0)
105408af8aacSRandall Stewart 			tp->t_acktime = ticks;
1055651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10567d2608a5SMark Johnston 		m = NULL;
10572c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1058bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1059bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1060bd4a39ccSMark Johnston 
10612c37256eSGarrett Wollman 			/*
10622c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10632c37256eSGarrett Wollman 			 * initialize window to default value, and
10640c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10652c37256eSGarrett Wollman 			 */
1066fb59c426SYoshinobu Inoue #ifdef INET6
1067fb59c426SYoshinobu Inoue 			if (isipv6)
1068a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1069fb59c426SYoshinobu Inoue #endif /* INET6 */
1070b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1071b287c6c7SBjoern A. Zeeb 			else
1072b287c6c7SBjoern A. Zeeb #endif
1073b287c6c7SBjoern A. Zeeb #ifdef INET
1074a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1075b287c6c7SBjoern A. Zeeb #endif
10764a91aa8fSMichael Tuexen 			/*
10774a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10784a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10794a91aa8fSMichael Tuexen 			 * operations fail.
10804a91aa8fSMichael Tuexen 			 */
10814a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10824a91aa8fSMichael Tuexen 				restoreflags = false;
10834a91aa8fSMichael Tuexen 
10847d2608a5SMark Johnston 			if (error) {
10857d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10867d2608a5SMark Johnston 				sbflush(&so->so_snd);
10872c37256eSGarrett Wollman 				goto out;
10887d2608a5SMark Johnston 			}
1089dd7b86e2SGleb Smirnoff 			if (tp->t_flags & TF_FASTOPEN)
1090c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
109118a75309SPatrick Kelsey 			else {
10922c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10932c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10942c37256eSGarrett Wollman 			}
1095c560df6fSPatrick Kelsey 		}
10962c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
10972c37256eSGarrett Wollman 			/*
10982c37256eSGarrett Wollman 			 * Close the send side of the connection after
10992c37256eSGarrett Wollman 			 * the data is sent.
11002c37256eSGarrett Wollman 			 */
11012c37256eSGarrett Wollman 			socantsendmore(so);
1102623dce13SRobert Watson 			tcp_usrclosed(tp);
11032c37256eSGarrett Wollman 		}
1104e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1105e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1106e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1107e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1108e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1109e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1110e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1111e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1112e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1113e854dd38SRandall Stewart 		}
11142cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11152cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1116b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1117b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1118f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1119b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1120b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1121b0acefa8SBill Fenner 		}
11222c37256eSGarrett Wollman 	} else {
1123623dce13SRobert Watson 		/*
1124623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1125623dce13SRobert Watson 		 */
1126dded4e9eSRichard Scheffenegger 		SOCK_SENDBUF_LOCK(so);
11272c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1128dded4e9eSRichard Scheffenegger 			SOCK_SENDBUF_UNLOCK(so);
11292c37256eSGarrett Wollman 			error = ENOBUFS;
11302c37256eSGarrett Wollman 			goto out;
11312c37256eSGarrett Wollman 		}
11322c37256eSGarrett Wollman 		/*
11332c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11342c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11352c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11362c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11372c37256eSGarrett Wollman 		 * of data past the urgent section.
11382c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11392c37256eSGarrett Wollman 		 */
114008af8aacSRandall Stewart 		if (tp->t_acktime == 0)
114108af8aacSRandall Stewart 			tp->t_acktime = ticks;
1142651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1143dded4e9eSRichard Scheffenegger 		SOCK_SENDBUF_UNLOCK(so);
11447d2608a5SMark Johnston 		m = NULL;
1145ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1146ef53690bSGarrett Wollman 			/*
1147ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1148ef53690bSGarrett Wollman 			 * initialize window to default value, and
11490c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1150ef53690bSGarrett Wollman 			 */
115118a75309SPatrick Kelsey 
1152c560df6fSPatrick Kelsey 			/*
1153c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1154c560df6fSPatrick Kelsey 			 */
1155dd7b86e2SGleb Smirnoff 			if (tp->t_flags & TF_FASTOPEN)
1156c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1157fb59c426SYoshinobu Inoue #ifdef INET6
1158fb59c426SYoshinobu Inoue 			if (isipv6)
1159a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1160fb59c426SYoshinobu Inoue #endif /* INET6 */
1161b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1162b287c6c7SBjoern A. Zeeb 			else
1163b287c6c7SBjoern A. Zeeb #endif
1164b287c6c7SBjoern A. Zeeb #ifdef INET
1165a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1166b287c6c7SBjoern A. Zeeb #endif
11674a91aa8fSMichael Tuexen 			/*
11684a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11694a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11704a91aa8fSMichael Tuexen 			 * operations fail.
11714a91aa8fSMichael Tuexen 			 */
11724a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11734a91aa8fSMichael Tuexen 				restoreflags = false;
11744a91aa8fSMichael Tuexen 
11757d2608a5SMark Johnston 			if (error != 0) {
11767d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11777d2608a5SMark Johnston 				sbflush(&so->so_snd);
1178ef53690bSGarrett Wollman 				goto out;
11797d2608a5SMark Johnston 			}
1180ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1181ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1182623dce13SRobert Watson 		}
1183300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11847d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11852cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1186f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11872cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11882c37256eSGarrett Wollman 		}
11892cbcd3c1SGleb Smirnoff 	}
11902529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11912529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11922529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11932529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11942529f56eSJonathan T. Looney 	    0, NULL, false);
11957d2608a5SMark Johnston 
1196d1401c90SRobert Watson out:
11974a91aa8fSMichael Tuexen 	/*
11987d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
11997d2608a5SMark Johnston 	 * responsible for freeing memory.
12007d2608a5SMark Johnston 	 */
12017d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12027d2608a5SMark Johnston 		m_freem(m);
12037d2608a5SMark Johnston 
12047d2608a5SMark Johnston 	/*
12054a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12064a91aa8fSMichael Tuexen 	 * restore the original flags.
12074a91aa8fSMichael Tuexen 	 */
12084a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12094a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12104a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12114a91aa8fSMichael Tuexen 	}
121200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
121300812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
12145d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12155d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1216f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
121797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
121873fddedaSPeter Grehan 	return (error);
12192c37256eSGarrett Wollman }
12202c37256eSGarrett Wollman 
12212cbcd3c1SGleb Smirnoff static int
12222cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12232cbcd3c1SGleb Smirnoff {
1224109eb549SGleb Smirnoff 	struct epoch_tracker et;
12252cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12262cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12272cbcd3c1SGleb Smirnoff 	int error;
12282cbcd3c1SGleb Smirnoff 
12292cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12302cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
123153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12322cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
123382334850SJohn Baldwin 		mb_free_notready(m, count);
12342cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12352cbcd3c1SGleb Smirnoff 	}
12362cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12372cbcd3c1SGleb Smirnoff 
1238dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_LOCK(so);
12392cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
1240dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_UNLOCK(so);
1241f64dc2abSGleb Smirnoff 	if (error) {
12422cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1243f64dc2abSGleb Smirnoff 		return (error);
1244f64dc2abSGleb Smirnoff 	}
1245f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1246f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1247f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12482cbcd3c1SGleb Smirnoff 
12492cbcd3c1SGleb Smirnoff 	return (error);
12502cbcd3c1SGleb Smirnoff }
12512cbcd3c1SGleb Smirnoff 
12522c37256eSGarrett Wollman /*
1253a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12542c37256eSGarrett Wollman  */
1255ac45e92fSRobert Watson static void
12562c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12572c37256eSGarrett Wollman {
1258f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1259c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12606573d758SMatt Macy 	struct epoch_tracker et;
1261c78cbc7bSRobert Watson 
1262ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1263c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1264c78cbc7bSRobert Watson 
126597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12668501a69cSRobert Watson 	INP_WLOCK(inp);
1267c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1268c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1269c78cbc7bSRobert Watson 
1270c78cbc7bSRobert Watson 	/*
1271a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1272c78cbc7bSRobert Watson 	 */
127353af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1274c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12758fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12768fa799bdSJonathan T. Looney 		if (tp == NULL)
12778fa799bdSJonathan T. Looney 			goto dropped;
127800812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12795d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1280c78cbc7bSRobert Watson 	}
1281ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1282d8596171SGleb Smirnoff 		soref(so);
1283ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1284a152f8a3SRobert Watson 	}
12858501a69cSRobert Watson 	INP_WUNLOCK(inp);
12868fa799bdSJonathan T. Looney dropped:
128797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1288a152f8a3SRobert Watson }
1289a152f8a3SRobert Watson 
1290a152f8a3SRobert Watson /*
1291a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1292a152f8a3SRobert Watson  */
1293a152f8a3SRobert Watson static void
1294a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1295a152f8a3SRobert Watson {
1296a152f8a3SRobert Watson 	struct inpcb *inp;
1297c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12986573d758SMatt Macy 	struct epoch_tracker et;
1299a152f8a3SRobert Watson 
1300a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1301a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1302a152f8a3SRobert Watson 
130397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13048501a69cSRobert Watson 	INP_WLOCK(inp);
1305a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1306a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1307a152f8a3SRobert Watson 
1308a152f8a3SRobert Watson 	/*
1309cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1310a152f8a3SRobert Watson 	 * a disconnect.
1311a152f8a3SRobert Watson 	 */
131253af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1313a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1314cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
131574703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1316a152f8a3SRobert Watson 			tcp_disconnect(tp);
131700812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
13185d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1319a152f8a3SRobert Watson 		}
1320cda6bdbaSJohn Baldwin 	}
1321ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1322d8596171SGleb Smirnoff 		soref(so);
1323ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1324a152f8a3SRobert Watson 	}
13258501a69cSRobert Watson 	INP_WUNLOCK(inp);
132697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13272c37256eSGarrett Wollman }
13282c37256eSGarrett Wollman 
1329d3b6c96bSRandall Stewart static int
1330d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1331d3b6c96bSRandall Stewart {
1332d3b6c96bSRandall Stewart 	/*
1333d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1334d3b6c96bSRandall Stewart 	 * specified then it does not always support
1335d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1336d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1337d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1338d3b6c96bSRandall Stewart 	 */
1339d3b6c96bSRandall Stewart 	int ret = 0;
1340d3b6c96bSRandall Stewart 
1341d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1342d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1343d3b6c96bSRandall Stewart 	}
1344d3b6c96bSRandall Stewart 	return (ret);
1345d3b6c96bSRandall Stewart }
1346d3b6c96bSRandall Stewart 
13472c37256eSGarrett Wollman /*
13482c37256eSGarrett Wollman  * Receive out-of-band data.
13492c37256eSGarrett Wollman  */
13502c37256eSGarrett Wollman static int
13512c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13522c37256eSGarrett Wollman {
13532c37256eSGarrett Wollman 	int error = 0;
1354f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1355c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13562c37256eSGarrett Wollman 
1357623dce13SRobert Watson 	inp = sotoinpcb(so);
1358623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13598501a69cSRobert Watson 	INP_WLOCK(inp);
136053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1361c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
1362c2399dd2SMichael Tuexen 		return (ECONNRESET);
1363623dce13SRobert Watson 	}
1364623dce13SRobert Watson 	tp = intotcpcb(inp);
1365c2399dd2SMichael Tuexen 
1366d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1367d3b6c96bSRandall Stewart 	if (error) {
1368d3b6c96bSRandall Stewart 		goto out;
1369d3b6c96bSRandall Stewart 	}
13702c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1371c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13724cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13734cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13742c37256eSGarrett Wollman 		error = EINVAL;
13752c37256eSGarrett Wollman 		goto out;
13762c37256eSGarrett Wollman 	}
13772c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13782c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13792c37256eSGarrett Wollman 		goto out;
13802c37256eSGarrett Wollman 	}
13812c37256eSGarrett Wollman 	m->m_len = 1;
13822c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13832c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13842c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1385623dce13SRobert Watson 
1386623dce13SRobert Watson out:
138700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
13885d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13898501a69cSRobert Watson 	INP_WUNLOCK(inp);
1390623dce13SRobert Watson 	return (error);
13912c37256eSGarrett Wollman }
13922c37256eSGarrett Wollman 
1393b287c6c7SBjoern A. Zeeb #ifdef INET
1394e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1395e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1396e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1397e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1398e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1399e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1400e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1401e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1402e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1403e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1404e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1405e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1406e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1407e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1408e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1409e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1410e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1411e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1412e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1413e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1414e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1415e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1416e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1417e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14182c37256eSGarrett Wollman };
1419b287c6c7SBjoern A. Zeeb #endif /* INET */
1420df8bae1dSRodney W. Grimes 
1421fb59c426SYoshinobu Inoue #ifdef INET6
1422e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1423e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1424e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1425e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1426e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1427e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1428e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1429e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1430e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1431e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1432e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1433e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1434e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1435e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1436e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1437e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1438e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1439e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1440e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1441e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1442e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1443e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1444e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1445e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1446fb59c426SYoshinobu Inoue };
1447fb59c426SYoshinobu Inoue #endif /* INET6 */
1448fb59c426SYoshinobu Inoue 
1449b287c6c7SBjoern A. Zeeb #ifdef INET
1450a0292f23SGarrett Wollman /*
1451a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1452dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1453dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1454dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1455a0292f23SGarrett Wollman  */
14560312fbe9SPoul-Henning Kamp static int
1457a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1458a0292f23SGarrett Wollman {
14599e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14609eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1461c3229e05SDavid Greenman 	int error;
1462a0292f23SGarrett Wollman 
1463c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14648501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
146576f1499fSGleb Smirnoff 
1466636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1467de0a2eb2SMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1468de0a2eb2SMark Johnston 	    SS_ISDISCONNECTED)) != 0))
1469636b19eaSMark Johnston 		return (EISCONN);
1470636b19eaSMark Johnston 
1471fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
14720b4539eeSGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred);
14739e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1474dfc4d218SGleb Smirnoff 	if (error != 0)
14759e46ff4dSGleb Smirnoff 		return (error);
1476a0292f23SGarrett Wollman 
1477087b55eaSAndre Oppermann 	/*
1478087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1479087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1480087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1481087b55eaSAndre Oppermann 	 */
1482a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14839b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1484a0292f23SGarrett Wollman 		tp->request_r_scale++;
1485a0292f23SGarrett Wollman 
1486a0292f23SGarrett Wollman 	soisconnecting(so);
148778b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
148857f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14898e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14908e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14918e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1492a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1493a45d2726SAndras Olah 
14949e46ff4dSGleb Smirnoff 	return (0);
1495a0292f23SGarrett Wollman }
1496b287c6c7SBjoern A. Zeeb #endif /* INET */
1497a0292f23SGarrett Wollman 
1498fb59c426SYoshinobu Inoue #ifdef INET6
1499fb59c426SYoshinobu Inoue static int
1500a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1501fb59c426SYoshinobu Inoue {
15029eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1503636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1504fb59c426SYoshinobu Inoue 	int error;
1505fb59c426SYoshinobu Inoue 
1506775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
15078501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1508623dce13SRobert Watson 
1509636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1510636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1511636b19eaSMark Johnston 		return (EISCONN);
1512636b19eaSMark Johnston 
151376f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1514a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1515fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
151676f1499fSGleb Smirnoff 	if (error != 0)
151776f1499fSGleb Smirnoff 		return (error);
1518fb59c426SYoshinobu Inoue 
1519fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1520fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1521970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1522fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1523fb59c426SYoshinobu Inoue 
1524636b19eaSMark Johnston 	soisconnecting(so);
152578b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
152657f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15278e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15288e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15298e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1530fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1531fb59c426SYoshinobu Inoue 
153276f1499fSGleb Smirnoff 	return (0);
1533fb59c426SYoshinobu Inoue }
1534fb59c426SYoshinobu Inoue #endif /* INET6 */
1535fb59c426SYoshinobu Inoue 
1536cfe8b629SGarrett Wollman /*
1537b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1538b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1539b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1540b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1541b8af5dfaSRobert Watson  * from Linux.
1542b8af5dfaSRobert Watson  */
15438c6104c4SMarius Strobl void
15448c6104c4SMarius Strobl tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1545b8af5dfaSRobert Watson {
1546b8af5dfaSRobert Watson 
15478c6104c4SMarius Strobl 	INP_LOCK_ASSERT(tptoinpcb(tp));
1548b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1549b8af5dfaSRobert Watson 
1550b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1551b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1552b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15533529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1554b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1555b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1556b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1557b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1558b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1559b8af5dfaSRobert Watson 	}
156004682968SRichard Scheffenegger 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
156104682968SRichard Scheffenegger 		case TF2_ECN_PERMIT:
15625a17b6adSMichael Tuexen 			ti->tcpi_options |= TCPI_OPT_ECN;
156304682968SRichard Scheffenegger 			break;
156404682968SRichard Scheffenegger 		case TF2_ACE_PERMIT:
156504682968SRichard Scheffenegger 			/* FALLTHROUGH */
156604682968SRichard Scheffenegger 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
156704682968SRichard Scheffenegger 			ti->tcpi_options |= TCPI_OPT_ACE;
156804682968SRichard Scheffenegger 			break;
156904682968SRichard Scheffenegger 		default:
157004682968SRichard Scheffenegger 			break;
157104682968SRichard Scheffenegger 	}
1572dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
157304682968SRichard Scheffenegger 		ti->tcpi_options |= TCPI_OPT_TFO;
15741baaf834SBruce M Simpson 
157543d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15763ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15771baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15781baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15791baaf834SBruce M Simpson 
1580b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1581b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1582b8af5dfaSRobert Watson 
1583b8af5dfaSRobert Watson 	/*
1584b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1585b8af5dfaSRobert Watson 	 */
1586c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1587535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1588b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15891c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1590535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
159143d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
159243d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1593f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1594f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1595f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1596dc485b96SMarius Strobl 	ti->tcpi_snd_una = tp->snd_una;
1597dc485b96SMarius Strobl 	ti->tcpi_snd_max = tp->snd_max;
1598dc485b96SMarius Strobl 	ti->tcpi_rcv_numsacks = tp->rcv_numsacks;
1599dc485b96SMarius Strobl 	ti->tcpi_rcv_adv = tp->rcv_adv;
1600dc485b96SMarius Strobl 	ti->tcpi_dupacks = tp->t_dupacks;
1601e18b97bdSRandall Stewart 	ti->tcpi_rttmin = tp->t_rttlow;
1602a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1603a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1604a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1605a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1606a6456410SNavdeep Parhar 	}
1607a6456410SNavdeep Parhar #endif
160822c81cc5SRichard Scheffenegger 	/*
160922c81cc5SRichard Scheffenegger 	 * AccECN related counters.
161022c81cc5SRichard Scheffenegger 	 */
161122c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
161222c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
161322c81cc5SRichard Scheffenegger 		/*
161422c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
161522c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
161622c81cc5SRichard Scheffenegger 		 */
161722c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
161822c81cc5SRichard Scheffenegger 	else
161922c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
162022c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1621b8af5dfaSRobert Watson }
1622b8af5dfaSRobert Watson 
1623b8af5dfaSRobert Watson /*
16241e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16251e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16261e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16271e8f5ffaSRobert Watson  * option.
1628cfe8b629SGarrett Wollman  */
1629bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16308501a69cSRobert Watson 	INP_WLOCK(inp);							\
163153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16328501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1633bac5bedfSConrad Meyer 		cleanup;						\
16341e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16351e8f5ffaSRobert Watson 	}								\
16361e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16371e8f5ffaSRobert Watson } while(0)
1638bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16391e8f5ffaSRobert Watson 
1640fd7daa72SMichael Tuexen int
1641fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1642df8bae1dSRodney W. Grimes {
1643fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1644fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1645fc4d53ccSGleb Smirnoff 	int error = 0;
1646df8bae1dSRodney W. Grimes 
1647fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16483b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
164953af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1650fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1651fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1652fc4d53ccSGleb Smirnoff 
1653cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16543b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1655fb59c426SYoshinobu Inoue #ifdef INET6
1656de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1657fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1658de156263SGleb Smirnoff #endif
1659de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1660de156263SGleb Smirnoff 		else
1661de156263SGleb Smirnoff #endif
1662de156263SGleb Smirnoff #ifdef INET
1663fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1664de156263SGleb Smirnoff #endif
16655dff1c38SMichael Tuexen 		/*
1666de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1667de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1668de156263SGleb Smirnoff 		 * IP level returned.
16695dff1c38SMichael Tuexen 		 */
1670de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1671de156263SGleb Smirnoff #ifdef INET6
1672de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1673de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1674de156263SGleb Smirnoff 				return (error);
1675de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1676de156263SGleb Smirnoff 			case IPV6_TCLASS:
1677de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1678de156263SGleb Smirnoff 				break;
1679de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1680f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1681f581a26eSGleb Smirnoff 				break;
1682de156263SGleb Smirnoff 			default:
1683de156263SGleb Smirnoff 				return (error);
16845dff1c38SMichael Tuexen 			}
1685de156263SGleb Smirnoff 			break;
1686b287c6c7SBjoern A. Zeeb #endif
1687b287c6c7SBjoern A. Zeeb #ifdef INET
1688de156263SGleb Smirnoff 		case IPPROTO_IP:
1689de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1690de156263SGleb Smirnoff 			case IP_TOS:
16913b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
16923b0ee680SRichard Scheffenegger 				break;
1693de156263SGleb Smirnoff 			case IP_TTL:
1694de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1695de156263SGleb Smirnoff 				break;
1696de156263SGleb Smirnoff 			default:
1697df8bae1dSRodney W. Grimes 				return (error);
1698de156263SGleb Smirnoff 			}
1699de156263SGleb Smirnoff 			break;
1700de156263SGleb Smirnoff #endif
1701de156263SGleb Smirnoff 		default:
1702de156263SGleb Smirnoff 			return (error);
1703de156263SGleb Smirnoff 		}
1704fe136aecSMichael Tuexen 		INP_WLOCK_RECHECK(inp);
1705fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1706fc4d53ccSGleb Smirnoff 		/*
1707fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1708fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1709fc4d53ccSGleb Smirnoff 		 */
1710fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1711fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
171273ee5756SRandall Stewart 		void *ptr = NULL;
1713fc4d53ccSGleb Smirnoff 
17143b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1715fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1716fc4d53ccSGleb Smirnoff 		if (error)
1717fc4d53ccSGleb Smirnoff 			return (error);
1718fc4d53ccSGleb Smirnoff 
1719fe136aecSMichael Tuexen 		INP_WLOCK_RECHECK(inp);
1720fc4d53ccSGleb Smirnoff 
172155bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
172255bceb1eSRandall Stewart 		if (blk == NULL) {
172355bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
172455bceb1eSRandall Stewart 			return (ENOENT);
172555bceb1eSRandall Stewart 		}
1726587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1727587d67c0SRandall Stewart 			/* You already have this */
1728587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1729587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1730587d67c0SRandall Stewart 			return (0);
1731587d67c0SRandall Stewart 		}
173255bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
173355bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
173455bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
173555bceb1eSRandall Stewart 			return (ENOENT);
173655bceb1eSRandall Stewart 		}
173786c9325dSMichael Tuexen 		error = (*blk->tfb_tcp_handoff_ok)(tp);
173886c9325dSMichael Tuexen 		if (error) {
173986c9325dSMichael Tuexen 			refcount_release(&blk->tfb_refcnt);
174086c9325dSMichael Tuexen 			INP_WUNLOCK(inp);
174186c9325dSMichael Tuexen 			return (error);
174286c9325dSMichael Tuexen 		}
174355bceb1eSRandall Stewart 		/*
174473ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
174573ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
174655bceb1eSRandall Stewart 		 */
1747d2ef52efSGleb Smirnoff 		if (tp->t_fb->tfb_tcp_timer_stop_all != NULL)
1748d2ef52efSGleb Smirnoff 			tp->t_fb->tfb_tcp_timer_stop_all(tp);
174973ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
175073ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
175173ee5756SRandall Stewart 			if (error) {
175273ee5756SRandall Stewart 				/*
175373ee5756SRandall Stewart 				 * Release the ref count the lookup
175473ee5756SRandall Stewart 				 * acquired.
175573ee5756SRandall Stewart 				 */
175673ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
175773ee5756SRandall Stewart 				/*
175873ee5756SRandall Stewart 				 * Now there is a chance that the
175973ee5756SRandall Stewart 				 * init() function mucked with some
176073ee5756SRandall Stewart 				 * things before it failed, such as
176173ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
176273ee5756SRandall Stewart 				 * It should not of, but lets give the old
176373ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
176473ee5756SRandall Stewart 				 */
176573ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
176673ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
176773ee5756SRandall Stewart 				}
176873ee5756SRandall Stewart 			 	goto err_out;
176973ee5756SRandall Stewart 			}
177073ee5756SRandall Stewart 		}
1771587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1772086a3556SAndrew Gallatin 			struct epoch_tracker et;
1773587d67c0SRandall Stewart 			/*
1774587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1775587d67c0SRandall Stewart 			 * the tcb is not going away.
1776587d67c0SRandall Stewart 			 */
1777086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1778587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1779086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1780587d67c0SRandall Stewart 		}
178173ee5756SRandall Stewart 		/*
178273ee5756SRandall Stewart 		 * Release the old refcnt, the
178373ee5756SRandall Stewart 		 * lookup acquired a ref on the
178473ee5756SRandall Stewart 		 * new one already.
178573ee5756SRandall Stewart 		 */
178655bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
178773ee5756SRandall Stewart 		/*
178873ee5756SRandall Stewart 		 * Set in the new stack.
178973ee5756SRandall Stewart 		 */
179055bceb1eSRandall Stewart 		tp->t_fb = blk;
179173ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
179255bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
179355bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
179455bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
179555bceb1eSRandall Stewart 			     sopt->sopt_name);
179655bceb1eSRandall Stewart 		}
179755bceb1eSRandall Stewart #endif
17983ee9c3c4SRandall Stewart err_out:
179955bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
180055bceb1eSRandall Stewart 		return (error);
180173ee5756SRandall Stewart 
1802fc4d53ccSGleb Smirnoff 	}
1803fc4d53ccSGleb Smirnoff 
18043b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
180566fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1806fc4d53ccSGleb Smirnoff }
1807fc4d53ccSGleb Smirnoff 
1808fc4d53ccSGleb Smirnoff static int
1809fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1810fc4d53ccSGleb Smirnoff {
1811fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1812fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1813fc4d53ccSGleb Smirnoff 	int error = 0;
1814fc4d53ccSGleb Smirnoff 
1815fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18163b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
181753af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1818fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1819fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1820fc4d53ccSGleb Smirnoff 
1821fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18223b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1823fc4d53ccSGleb Smirnoff #ifdef INET6
1824fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1825fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1826fc4d53ccSGleb Smirnoff #endif /* INET6 */
1827fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1828fc4d53ccSGleb Smirnoff 		else
1829fc4d53ccSGleb Smirnoff #endif
1830fc4d53ccSGleb Smirnoff #ifdef INET
1831fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1832fc4d53ccSGleb Smirnoff #endif
1833fc4d53ccSGleb Smirnoff 		return (error);
1834fc4d53ccSGleb Smirnoff 	}
1835fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1836e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1837fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1838fc4d53ccSGleb Smirnoff 
1839e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1840e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1841e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1842e2833083SPeter Lei 		} else {
1843e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1844e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1845c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1846c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1847e2833083SPeter Lei 		}
184855bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
184955bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
185055bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
185155bceb1eSRandall Stewart 		return (error);
185255bceb1eSRandall Stewart 	}
1853fc4d53ccSGleb Smirnoff 
18543b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
185566fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1856fc4d53ccSGleb Smirnoff }
1857fc4d53ccSGleb Smirnoff 
1858fc4d53ccSGleb Smirnoff int
1859fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1860fc4d53ccSGleb Smirnoff {
1861fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1862fc4d53ccSGleb Smirnoff 
1863fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1864fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1865fc4d53ccSGleb Smirnoff 
18663b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
186753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18683b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18693b3c08c1SMichael Tuexen 		return (ECONNRESET);
18703b3c08c1SMichael Tuexen 	}
1871fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1872fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1873fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1874fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1875fc4d53ccSGleb Smirnoff 	else
1876fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
187755bceb1eSRandall Stewart }
187855bceb1eSRandall Stewart 
18792529f56eSJonathan T. Looney /*
18802529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18812529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18822529f56eSJonathan T. Looney  */
18832529f56eSJonathan T. Looney #ifdef CTASSERT
18842529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18852529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18862529f56eSJonathan T. Looney #endif
18872529f56eSJonathan T. Looney 
1888b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1889b8d60729SRandall Stewart 
1890b8d60729SRandall Stewart static int
1891ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1892b8d60729SRandall Stewart {
1893b8d60729SRandall Stewart 	struct cc_algo *algo;
1894b8d60729SRandall Stewart 	void *ptr = NULL;
18953b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1896b8d60729SRandall Stewart 	struct cc_var cc_mem;
1897b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1898b8d60729SRandall Stewart 	size_t mem_sz;
1899b8d60729SRandall Stewart 	int error;
1900b8d60729SRandall Stewart 
1901b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1902b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1903b8d60729SRandall Stewart 	if (error)
1904b8d60729SRandall Stewart 		return(error);
1905b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1906b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1907ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1908b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1909b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1910b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1911b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1912b8d60729SRandall Stewart 				continue;
1913b8d60729SRandall Stewart 			}
1914b8d60729SRandall Stewart 			break;
1915b8d60729SRandall Stewart 		}
1916ea9017fbSRandall Stewart 	}
1917b8d60729SRandall Stewart 	if (algo == NULL) {
1918b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1919b8d60729SRandall Stewart 		return(ESRCH);
1920b8d60729SRandall Stewart 	}
1921ea9017fbSRandall Stewart 	/*
1922ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1923ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1924ea9017fbSRandall Stewart 	 */
1925ea9017fbSRandall Stewart 	cc_refer(algo);
1926ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1927b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1928b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1929b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1930b8d60729SRandall Stewart 		if (mem_sz == 0) {
1931b8d60729SRandall Stewart 			goto no_mem_needed;
1932b8d60729SRandall Stewart 		}
1933b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1934b8d60729SRandall Stewart 	} else {
1935b8d60729SRandall Stewart no_mem_needed:
1936b8d60729SRandall Stewart 		mem_sz = 0;
1937b8d60729SRandall Stewart 		ptr = NULL;
1938b8d60729SRandall Stewart 	}
1939b8d60729SRandall Stewart 	/*
1940b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1941b8d60729SRandall Stewart 	 * back the inplock.
1942b8d60729SRandall Stewart 	 */
1943b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1944df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
194553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1946df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1947ea9017fbSRandall Stewart 		if (ptr)
1948df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1949ea9017fbSRandall Stewart 		/* Release our temp reference */
1950ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1951ea9017fbSRandall Stewart 		cc_release(algo);
1952ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
1953df07bfdaSMichael Tuexen 		return (ECONNRESET);
1954df07bfdaSMichael Tuexen 	}
1955df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
1956df07bfdaSMichael Tuexen 	if (ptr != NULL)
1957b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
195800d3b744SMichael Tuexen 	cc_mem.tp = tp;
1959b8d60729SRandall Stewart 	/*
1960b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
1961b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
1962b8d60729SRandall Stewart 	 * Note here we init into stack memory.
1963b8d60729SRandall Stewart 	 */
1964b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
1965b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
1966b8d60729SRandall Stewart 	else
1967b8d60729SRandall Stewart 		error = 0;
1968b8d60729SRandall Stewart 	/*
1969b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
1970b8d60729SRandall Stewart 	 * should not fail we could in theory have a
1971b8d60729SRandall Stewart 	 * KASSERT here.
1972b8d60729SRandall Stewart 	 */
1973b8d60729SRandall Stewart 	if (error == 0) {
1974b8d60729SRandall Stewart 		/*
1975b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
1976b8d60729SRandall Stewart 		 * connection to the new CC module by
1977b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
1978b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
1979b8d60729SRandall Stewart 		 */
1980b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
1981e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1982ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
1983ea9017fbSRandall Stewart 		cc_detach(tp);
1984ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
1985e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1986ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
1987ea9017fbSRandall Stewart 		cc_attach(tp, algo);
1988b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
1989b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1990b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
1991e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
1992b8d60729SRandall Stewart 		}
1993b8d60729SRandall Stewart 	} else if (ptr)
1994b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
1995b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1996ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
1997ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
1998ea9017fbSRandall Stewart 	cc_release(algo);
1999ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2000b8d60729SRandall Stewart 	return (error);
2001b8d60729SRandall Stewart }
2002b8d60729SRandall Stewart 
200355bceb1eSRandall Stewart int
200466fbc19fSGleb Smirnoff tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
200555bceb1eSRandall Stewart {
200666fbc19fSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
200755bceb1eSRandall Stewart 	int	error, opt, optval;
200855bceb1eSRandall Stewart 	u_int	ui;
200955bceb1eSRandall Stewart 	struct	tcp_info ti;
2010b2e60773SJohn Baldwin #ifdef KERN_TLS
2011b2e60773SJohn Baldwin 	struct tls_enable tls;
2012528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2013b2e60773SJohn Baldwin #endif
20142529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2015adc56f5aSEdward Tomasz Napierala #ifdef STATS
2016adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2017adc56f5aSEdward Tomasz Napierala #endif
2018af6fef3aSGleb Smirnoff 	size_t	len;
2019df8bae1dSRodney W. Grimes 
2020f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
202153af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2022fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2023528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2024f581a26eSGleb Smirnoff 
2025f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2026f581a26eSGleb Smirnoff #ifdef INET6
2027f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2028f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2029f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2030f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2031f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2032f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2033f581a26eSGleb Smirnoff 		}
2034f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2035f581a26eSGleb Smirnoff 		return (0);
2036f581a26eSGleb Smirnoff #endif
2037f581a26eSGleb Smirnoff #ifdef INET
2038f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2039f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2040f581a26eSGleb Smirnoff 		return (0);
2041f581a26eSGleb Smirnoff #endif
2042f581a26eSGleb Smirnoff 	}
2043f581a26eSGleb Smirnoff 
2044d519cedbSGleb Smirnoff 	/*
2045d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2046d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2047d519cedbSGleb Smirnoff 	 */
2048d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2049d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2050d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2051c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2052c8b53cedSMichael Tuexen 			return (EINVAL);
2053af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2054af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2055d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2056d519cedbSGleb Smirnoff 		if (error) {
2057af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2058d519cedbSGleb Smirnoff 			return (error);
2059d519cedbSGleb Smirnoff 		}
2060bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2061d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2062e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2063d519cedbSGleb Smirnoff 		else
2064d519cedbSGleb Smirnoff 			error = ENOENT;
2065d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2066d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2067af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2068af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2069d519cedbSGleb Smirnoff 		return (error);
2070d519cedbSGleb Smirnoff 	}
2071d519cedbSGleb Smirnoff 
2072cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2073cfe8b629SGarrett Wollman 	case SOPT_SET:
2074cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2075fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
207688f6b043SBruce M Simpson 		case TCP_MD5SIG:
20778501a69cSRobert Watson 			INP_WUNLOCK(inp);
207897453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2079fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2080fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
20811cfd4b53SBruce M Simpson 			if (error)
20821e8f5ffaSRobert Watson 				return (error);
208397453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
208409fe6320SNavdeep Parhar 			goto unlock_and_done;
2085fcf59617SAndrey V. Elsukov #endif /* IPSEC */
208609fe6320SNavdeep Parhar 
2087df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2088cfe8b629SGarrett Wollman 		case TCP_NOOPT:
20898501a69cSRobert Watson 			INP_WUNLOCK(inp);
2090cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2091cfe8b629SGarrett Wollman 			    sizeof optval);
2092cfe8b629SGarrett Wollman 			if (error)
20931e8f5ffaSRobert Watson 				return (error);
2094cfe8b629SGarrett Wollman 
20958501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2096cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2097cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2098cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2099cfe8b629SGarrett Wollman 				break;
2100cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2101cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2102cfe8b629SGarrett Wollman 				break;
2103cfe8b629SGarrett Wollman 			default:
2104cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2105cfe8b629SGarrett Wollman 				break;
2106cfe8b629SGarrett Wollman 			}
2107cfe8b629SGarrett Wollman 
2108cfe8b629SGarrett Wollman 			if (optval)
2109cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2110df8bae1dSRodney W. Grimes 			else
2111cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
211209fe6320SNavdeep Parhar unlock_and_done:
211309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
211409fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
211509fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
211609fe6320SNavdeep Parhar 				    sopt->sopt_name);
211709fe6320SNavdeep Parhar 			}
211809fe6320SNavdeep Parhar #endif
21198501a69cSRobert Watson 			INP_WUNLOCK(inp);
2120df8bae1dSRodney W. Grimes 			break;
2121df8bae1dSRodney W. Grimes 
2122007581c0SJonathan Lemon 		case TCP_NOPUSH:
21238501a69cSRobert Watson 			INP_WUNLOCK(inp);
2124007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2125007581c0SJonathan Lemon 			    sizeof optval);
2126007581c0SJonathan Lemon 			if (error)
21271e8f5ffaSRobert Watson 				return (error);
2128007581c0SJonathan Lemon 
21298501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2130007581c0SJonathan Lemon 			if (optval)
2131007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2132d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2133007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2134109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2135109eb549SGleb Smirnoff 					struct epoch_tracker et;
2136109eb549SGleb Smirnoff 
2137109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2138f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2139109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2140109eb549SGleb Smirnoff 				}
2141007581c0SJonathan Lemon 			}
214209fe6320SNavdeep Parhar 			goto unlock_and_done;
2143007581c0SJonathan Lemon 
21449e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21459e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21469e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21479e644c23SMichael Tuexen 			    sizeof optval);
21489e644c23SMichael Tuexen 			if (error)
21499e644c23SMichael Tuexen 				return (error);
21509e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
21519e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
21529e644c23SMichael Tuexen 				/* Its got to be in range */
21539e644c23SMichael Tuexen 				return (EINVAL);
21549e644c23SMichael Tuexen 			}
21559e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
21569e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
21579e644c23SMichael Tuexen 				return (EINVAL);
21589e644c23SMichael Tuexen 			}
21599e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
21609e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
21619e644c23SMichael Tuexen 				/* You can't change after you are connected */
21629e644c23SMichael Tuexen 				error = EINVAL;
21639e644c23SMichael Tuexen 			} else {
21649e644c23SMichael Tuexen 				/* Ok we are all good set the port */
21659e644c23SMichael Tuexen 				tp->t_port = htons(optval);
21669e644c23SMichael Tuexen 			}
21679e644c23SMichael Tuexen 			goto unlock_and_done;
21689e644c23SMichael Tuexen 
2169df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
21708501a69cSRobert Watson 			INP_WUNLOCK(inp);
2171cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2172cfe8b629SGarrett Wollman 			    sizeof optval);
2173cfe8b629SGarrett Wollman 			if (error)
21741e8f5ffaSRobert Watson 				return (error);
2175df8bae1dSRodney W. Grimes 
21768501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
217753369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2178fce03f85SRandall Stewart 			    optval + 40 >= V_tcp_minmss) {
2179cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2180fce03f85SRandall Stewart 				if (tp->t_maxseg < V_tcp_mssdflt) {
2181fce03f85SRandall Stewart 					/*
2182fce03f85SRandall Stewart 					 * The MSS is so small we should not process incoming
2183fce03f85SRandall Stewart 					 * SACK's since we are subject to attack in such a
2184fce03f85SRandall Stewart 					 * case.
2185fce03f85SRandall Stewart 					 */
2186fce03f85SRandall Stewart 					tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
2187fce03f85SRandall Stewart 				} else {
2188fce03f85SRandall Stewart 					tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
2189fce03f85SRandall Stewart 				}
2190fce03f85SRandall Stewart 			} else
2191a0292f23SGarrett Wollman 				error = EINVAL;
219209fe6320SNavdeep Parhar 			goto unlock_and_done;
2193a0292f23SGarrett Wollman 
2194b8af5dfaSRobert Watson 		case TCP_INFO:
21958501a69cSRobert Watson 			INP_WUNLOCK(inp);
2196b8af5dfaSRobert Watson 			error = EINVAL;
2197b8af5dfaSRobert Watson 			break;
2198b8af5dfaSRobert Watson 
2199adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2200adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2201adc56f5aSEdward Tomasz Napierala #ifdef STATS
2202adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2203adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2204adc56f5aSEdward Tomasz Napierala 			if (error)
2205adc56f5aSEdward Tomasz Napierala 				return (error);
2206adc56f5aSEdward Tomasz Napierala 
2207adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2208adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2209adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2210adc56f5aSEdward Tomasz Napierala 			else
2211adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2212adc56f5aSEdward Tomasz Napierala 
2213adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2214adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2215adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2216adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2217adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2218adc56f5aSEdward Tomasz Napierala 				sbp = t;
2219adc56f5aSEdward Tomasz Napierala 			}
2220adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2221adc56f5aSEdward Tomasz Napierala 
2222adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2223adc56f5aSEdward Tomasz Napierala #else
2224adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2225adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2226adc56f5aSEdward Tomasz Napierala 			break;
2227adc56f5aSEdward Tomasz Napierala 
2228dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2229ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
223073e263b1SGleb Smirnoff 			break;
2231dbc42409SLawrence Stewart 
2232a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2233a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2234a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2235a034518aSAndrew Gallatin 			    sizeof(optval));
2236a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2237a034518aSAndrew Gallatin 			if (!error)
2238a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2239a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2240a034518aSAndrew Gallatin 			break;
2241a034518aSAndrew Gallatin 
2242b2e60773SJohn Baldwin #ifdef KERN_TLS
2243b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2244b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
224585df11a1SRichard Scheffenegger 			error = ktls_copyin_tls_enable(sopt, &tls);
224685df11a1SRichard Scheffenegger 			if (error != 0)
2247b2e60773SJohn Baldwin 				break;
2248fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
224985df11a1SRichard Scheffenegger 			ktls_cleanup_tls_enable(&tls);
2250b2e60773SJohn Baldwin 			break;
2251b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2252b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2253b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
225485df11a1SRichard Scheffenegger 			if (error != 0)
2255b2e60773SJohn Baldwin 				return (error);
2256b2e60773SJohn Baldwin 
2257b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2258fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2259b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2260b2e60773SJohn Baldwin 			break;
2261f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2262f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
226385df11a1SRichard Scheffenegger 			error = ktls_copyin_tls_enable(sopt, &tls);
226485df11a1SRichard Scheffenegger 			if (error != 0)
2265f1f93475SJohn Baldwin 				break;
2266fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
226785df11a1SRichard Scheffenegger 			ktls_cleanup_tls_enable(&tls);
2268f1f93475SJohn Baldwin 			break;
2269b2e60773SJohn Baldwin #endif
227008af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
22719077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
22729077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
22739077f387SGleb Smirnoff 		case TCP_KEEPINIT:
22749077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
22759077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
22769077f387SGleb Smirnoff 			if (error)
22779077f387SGleb Smirnoff 				return (error);
22789077f387SGleb Smirnoff 
22799077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
22809077f387SGleb Smirnoff 				error = EINVAL;
22819077f387SGleb Smirnoff 				break;
22829077f387SGleb Smirnoff 			}
22839077f387SGleb Smirnoff 			ui *= hz;
22849077f387SGleb Smirnoff 
22859077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
22869077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
228708af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
228808af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
228908af8aacSRandall Stewart 				break;
229008af8aacSRandall Stewart 
22919077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
22929077f387SGleb Smirnoff 				tp->t_keepidle = ui;
22939077f387SGleb Smirnoff 				/*
22949077f387SGleb Smirnoff 				 * XXX: better check current remaining
22959077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
22969077f387SGleb Smirnoff 				 */
22979077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
22989077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
22999077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23009077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23019077f387SGleb Smirnoff 				break;
23029077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23039077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23049077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23059077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23069077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23079077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23089077f387SGleb Smirnoff 				break;
23099077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23109077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23119077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23129077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23139077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23149077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23159077f387SGleb Smirnoff 				break;
23169077f387SGleb Smirnoff 			}
231709fe6320SNavdeep Parhar 			goto unlock_and_done;
23189077f387SGleb Smirnoff 
231985c05144SGleb Smirnoff 		case TCP_KEEPCNT:
232085c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
232185c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
232285c05144SGleb Smirnoff 			if (error)
232385c05144SGleb Smirnoff 				return (error);
232485c05144SGleb Smirnoff 
232585c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
232685c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
232785c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
232885c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
232985c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
233085c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
233185c05144SGleb Smirnoff 			goto unlock_and_done;
233285c05144SGleb Smirnoff 
233386a996e6SHiren Panchasara #ifdef TCPPCAP
233486a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
233586a996e6SHiren Panchasara 		case TCP_PCAP_IN:
233686a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
233786a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
233886a996e6SHiren Panchasara 			    sizeof optval);
233986a996e6SHiren Panchasara 			if (error)
234086a996e6SHiren Panchasara 				return (error);
234186a996e6SHiren Panchasara 
234286a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
234386a996e6SHiren Panchasara 			if (optval >= 0)
2344399a5655SRichard Scheffenegger 				tcp_pcap_set_sock_max(
2345399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
234686a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
234786a996e6SHiren Panchasara 					optval);
234886a996e6SHiren Panchasara 			else
234986a996e6SHiren Panchasara 				error = EINVAL;
235086a996e6SHiren Panchasara 			goto unlock_and_done;
235186a996e6SHiren Panchasara #endif
235286a996e6SHiren Panchasara 
2353c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2354c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2355c560df6fSPatrick Kelsey 
2356281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2357c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2358c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2359281a0fd4SPatrick Kelsey 				return (EPERM);
2360281a0fd4SPatrick Kelsey 
2361c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2362c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2363281a0fd4SPatrick Kelsey 			if (error)
2364281a0fd4SPatrick Kelsey 				return (error);
2365281a0fd4SPatrick Kelsey 
2366281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2367d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2368d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2369d442a657SMichael Tuexen 				error = EINVAL;
2370d442a657SMichael Tuexen 				goto unlock_and_done;
2371d442a657SMichael Tuexen 			}
2372c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2373c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2374c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2375c560df6fSPatrick Kelsey 						error = EPERM;
2376c560df6fSPatrick Kelsey 						goto unlock_and_done;
2377c560df6fSPatrick Kelsey 					}
2378c560df6fSPatrick Kelsey 
2379c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2380281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2381281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2382c560df6fSPatrick Kelsey 				} else {
2383c560df6fSPatrick Kelsey 					/*
2384c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2385c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2386c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2387c560df6fSPatrick Kelsey 					 * connect.
2388c560df6fSPatrick Kelsey 					 */
2389c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2390c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2391c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2392c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2393c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2394c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2395c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2396c560df6fSPatrick Kelsey 					}
2397c560df6fSPatrick Kelsey 				}
2398d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2399281a0fd4SPatrick Kelsey 			} else
2400281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2401281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2402c560df6fSPatrick Kelsey 		}
2403281a0fd4SPatrick Kelsey 
2404e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24052529f56eSJonathan T. Looney 		case TCP_LOG:
24062529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24072529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24082529f56eSJonathan T. Looney 			    sizeof optval);
24092529f56eSJonathan T. Looney 			if (error)
24102529f56eSJonathan T. Looney 				return (error);
24112529f56eSJonathan T. Looney 
24122529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24132529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24142529f56eSJonathan T. Looney 			goto unlock_and_done;
24152529f56eSJonathan T. Looney 
24162529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24172529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24182529f56eSJonathan T. Looney 			error = EINVAL;
24192529f56eSJonathan T. Looney 			break;
24202529f56eSJonathan T. Looney 
24212529f56eSJonathan T. Looney 		case TCP_LOGID:
24222529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24232529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24242529f56eSJonathan T. Looney 			if (error)
24252529f56eSJonathan T. Looney 				break;
24262529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24272529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24282529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24292529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24302529f56eSJonathan T. Looney 			break;
24312529f56eSJonathan T. Looney 
24322529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24332529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24342529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24352529f56eSJonathan T. Looney 			error =
24362529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
24372529f56eSJonathan T. Looney 			if (error)
24382529f56eSJonathan T. Looney 				break;
24392529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24402529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24412529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
24422529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24432529f56eSJonathan T. Looney 				    M_WAITOK, true);
24442529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24452529f56eSJonathan T. Looney 			} else {
24462529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24472529f56eSJonathan T. Looney 				/*
24482529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24492529f56eSJonathan T. Looney 				 * INP lock.
24502529f56eSJonathan T. Looney 				 */
24512529f56eSJonathan T. Looney 			}
24522529f56eSJonathan T. Looney 			break;
2453e24e5683SJonathan T. Looney #endif
24542529f56eSJonathan T. Looney 
2455df8bae1dSRodney W. Grimes 		default:
24568501a69cSRobert Watson 			INP_WUNLOCK(inp);
2457df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2458df8bae1dSRodney W. Grimes 			break;
2459df8bae1dSRodney W. Grimes 		}
2460df8bae1dSRodney W. Grimes 		break;
2461df8bae1dSRodney W. Grimes 
2462cfe8b629SGarrett Wollman 	case SOPT_GET:
24631e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2464cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2465fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
246688f6b043SBruce M Simpson 		case TCP_MD5SIG:
24678501a69cSRobert Watson 			INP_WUNLOCK(inp);
246897453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2469fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2470fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
24711cfd4b53SBruce M Simpson 			break;
2472265ed012SBruce M Simpson #endif
24731e8f5ffaSRobert Watson 
2474df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2475cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
24768501a69cSRobert Watson 			INP_WUNLOCK(inp);
2477b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2478df8bae1dSRodney W. Grimes 			break;
2479df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2480cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
24818501a69cSRobert Watson 			INP_WUNLOCK(inp);
2482b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2483df8bae1dSRodney W. Grimes 			break;
24849e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
24859e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
24869e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
24879e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
24889e644c23SMichael Tuexen 			break;
2489a0292f23SGarrett Wollman 		case TCP_NOOPT:
2490cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
24918501a69cSRobert Watson 			INP_WUNLOCK(inp);
2492b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2493a0292f23SGarrett Wollman 			break;
2494a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2495cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
24968501a69cSRobert Watson 			INP_WUNLOCK(inp);
2497b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2498b8af5dfaSRobert Watson 			break;
2499b8af5dfaSRobert Watson 		case TCP_INFO:
2500b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
25018501a69cSRobert Watson 			INP_WUNLOCK(inp);
2502b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2503a0292f23SGarrett Wollman 			break;
2504adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2505adc56f5aSEdward Tomasz Napierala 			{
2506adc56f5aSEdward Tomasz Napierala #ifdef STATS
2507adc56f5aSEdward Tomasz Napierala 			int nheld;
2508adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2509adc56f5aSEdward Tomasz Napierala 
2510adc56f5aSEdward Tomasz Napierala 			error = 0;
2511adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2512adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2513adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2514adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2515adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2516adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2517adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2518adc56f5aSEdward Tomasz Napierala 			else
2519adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2520adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2521adc56f5aSEdward Tomasz Napierala 			if (error)
2522adc56f5aSEdward Tomasz Napierala 				break;
2523adc56f5aSEdward Tomasz Napierala 
2524adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2525adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2526adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2527adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2528adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2529adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2530adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2531adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2532adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2533adc56f5aSEdward Tomasz Napierala 				break;
2534adc56f5aSEdward Tomasz Napierala 			}
2535adc56f5aSEdward Tomasz Napierala 
2536adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2537adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2538adc56f5aSEdward Tomasz Napierala 				goto unhold;
2539adc56f5aSEdward Tomasz Napierala 
2540adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2541adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2542adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2543adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2544adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2545adc56f5aSEdward Tomasz Napierala unhold:
2546adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2547adc56f5aSEdward Tomasz Napierala #else
2548adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2549adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2550adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2551adc56f5aSEdward Tomasz Napierala 			break;
2552adc56f5aSEdward Tomasz Napierala 			}
2553dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2554af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2555dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2556af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2557dbc42409SLawrence Stewart 			break;
255808af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25592f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
25602f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
25612f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
25622f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
25632f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
256408af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
256508af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
256608af8aacSRandall Stewart 				break;
25672f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
25685a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
25692f3eb7f4SGleb Smirnoff 				break;
25702f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
25715a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
25722f3eb7f4SGleb Smirnoff 				break;
25732f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
25745a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
25752f3eb7f4SGleb Smirnoff 				break;
25762f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
25775a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
25782f3eb7f4SGleb Smirnoff 				break;
25792f3eb7f4SGleb Smirnoff 			}
25802f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
25812f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
25822f3eb7f4SGleb Smirnoff 			break;
258386a996e6SHiren Panchasara #ifdef TCPPCAP
258486a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
258586a996e6SHiren Panchasara 		case TCP_PCAP_IN:
2586399a5655SRichard Scheffenegger 			optval = tcp_pcap_get_sock_max(
2587399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
258886a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
258986a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
259086a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
259186a996e6SHiren Panchasara 			break;
259286a996e6SHiren Panchasara #endif
2593281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2594281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2595281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2596281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2597281a0fd4SPatrick Kelsey 			break;
2598e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
25992529f56eSJonathan T. Looney 		case TCP_LOG:
260069c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
26012529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26022529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
26032529f56eSJonathan T. Looney 			break;
26042529f56eSJonathan T. Looney 		case TCP_LOGBUF:
26052529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
26062529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26072529f56eSJonathan T. Looney 			break;
26082529f56eSJonathan T. Looney 		case TCP_LOGID:
26092529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26102529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26112529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26122529f56eSJonathan T. Looney 			break;
26132529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26142529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26152529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26162529f56eSJonathan T. Looney 			error = EINVAL;
26172529f56eSJonathan T. Looney 			break;
2618e24e5683SJonathan T. Looney #endif
2619b2e60773SJohn Baldwin #ifdef KERN_TLS
2620b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2621fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2622b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2623bf256782SMark Johnston 			if (error == 0)
2624bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2625bf256782SMark Johnston 				    sizeof(optval));
2626b2e60773SJohn Baldwin 			break;
2627f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2628fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2629f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2630bf256782SMark Johnston 			if (error == 0)
2631bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2632bf256782SMark Johnston 				    sizeof(optval));
2633f1f93475SJohn Baldwin 			break;
2634b2e60773SJohn Baldwin #endif
2635df8bae1dSRodney W. Grimes 		default:
26368501a69cSRobert Watson 			INP_WUNLOCK(inp);
2637df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2638df8bae1dSRodney W. Grimes 			break;
2639df8bae1dSRodney W. Grimes 		}
2640df8bae1dSRodney W. Grimes 		break;
2641df8bae1dSRodney W. Grimes 	}
2642df8bae1dSRodney W. Grimes 	return (error);
2643df8bae1dSRodney W. Grimes }
26448501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2645bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2646df8bae1dSRodney W. Grimes 
264726e30fbbSDavid Greenman /*
2648df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2649df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2650df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2651df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2652df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2653df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2654df8bae1dSRodney W. Grimes  */
2655623dce13SRobert Watson static void
2656ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2657df8bae1dSRodney W. Grimes {
26589eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
26599eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2660e6e0b5ffSRobert Watson 
266197a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26628501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2663df8bae1dSRodney W. Grimes 
2664623dce13SRobert Watson 	/*
2665623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2666623dce13SRobert Watson 	 * socket is still open.
2667623dce13SRobert Watson 	 */
26688db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
2669dd7b86e2SGleb Smirnoff 	    !(tp->t_state > TCPS_LISTEN && (tp->t_flags & TF_FASTOPEN))) {
2670df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2671623dce13SRobert Watson 		KASSERT(tp != NULL,
2672623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2673623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2674243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2675623dce13SRobert Watson 		KASSERT(tp != NULL,
2676623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2677623dce13SRobert Watson 	} else {
2678df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2679df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2680623dce13SRobert Watson 		tcp_usrclosed(tp);
2681ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2682f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2683f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2684df8bae1dSRodney W. Grimes 	}
2685df8bae1dSRodney W. Grimes }
2686df8bae1dSRodney W. Grimes 
2687df8bae1dSRodney W. Grimes /*
2688df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2689df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2690df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2691df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2692df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2693df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2694df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2695df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2696df8bae1dSRodney W. Grimes  */
2697623dce13SRobert Watson static void
2698ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2699df8bae1dSRodney W. Grimes {
2700df8bae1dSRodney W. Grimes 
270197a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27029eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2703e6e0b5ffSRobert Watson 
2704df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2705df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
270609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
270709fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
270809fe6320SNavdeep Parhar #endif
2709550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2710bc65987aSKip Macy 		/* FALLTHROUGH */
2711bc65987aSKip Macy 	case TCPS_CLOSED:
2712df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2713623dce13SRobert Watson 		/*
2714623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2715623dce13SRobert Watson 		 * still open.
2716623dce13SRobert Watson 		 */
2717623dce13SRobert Watson 		KASSERT(tp != NULL,
2718623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2719df8bae1dSRodney W. Grimes 		break;
2720df8bae1dSRodney W. Grimes 
2721a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2722df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2723a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2724a0292f23SGarrett Wollman 		break;
2725a0292f23SGarrett Wollman 
2726df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
272757f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2728df8bae1dSRodney W. Grimes 		break;
2729df8bae1dSRodney W. Grimes 
2730df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
273157f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2732df8bae1dSRodney W. Grimes 		break;
2733df8bae1dSRodney W. Grimes 	}
273408af8aacSRandall Stewart 	if (tp->t_acktime == 0)
273508af8aacSRandall Stewart 		tp->t_acktime = ticks;
2736abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27373eeb22cbSRichard Scheffenegger 		tcp_free_sackholes(tp);
27389eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2739abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27407c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27417c72af87SMohan Srinivasan 			int timeout;
27427c72af87SMohan Srinivasan 
27437c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27449077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2745b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2746b6239c4aSAndras Olah 		}
2747df8bae1dSRodney W. Grimes 	}
27487c72af87SMohan Srinivasan }
2749497057eeSRobert Watson 
2750497057eeSRobert Watson #ifdef DDB
2751497057eeSRobert Watson static void
2752497057eeSRobert Watson db_print_indent(int indent)
2753497057eeSRobert Watson {
2754497057eeSRobert Watson 	int i;
2755497057eeSRobert Watson 
2756497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2757497057eeSRobert Watson 		db_printf(" ");
2758497057eeSRobert Watson }
2759497057eeSRobert Watson 
2760497057eeSRobert Watson static void
2761497057eeSRobert Watson db_print_tstate(int t_state)
2762497057eeSRobert Watson {
2763497057eeSRobert Watson 
2764497057eeSRobert Watson 	switch (t_state) {
2765497057eeSRobert Watson 	case TCPS_CLOSED:
2766497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2767497057eeSRobert Watson 		return;
2768497057eeSRobert Watson 
2769497057eeSRobert Watson 	case TCPS_LISTEN:
2770497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2771497057eeSRobert Watson 		return;
2772497057eeSRobert Watson 
2773497057eeSRobert Watson 	case TCPS_SYN_SENT:
2774497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2775497057eeSRobert Watson 		return;
2776497057eeSRobert Watson 
2777497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2778497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2779497057eeSRobert Watson 		return;
2780497057eeSRobert Watson 
2781497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2782497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2783497057eeSRobert Watson 		return;
2784497057eeSRobert Watson 
2785497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2786497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2787497057eeSRobert Watson 		return;
2788497057eeSRobert Watson 
2789497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2790497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2791497057eeSRobert Watson 		return;
2792497057eeSRobert Watson 
2793497057eeSRobert Watson 	case TCPS_CLOSING:
2794497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2795497057eeSRobert Watson 		return;
2796497057eeSRobert Watson 
2797497057eeSRobert Watson 	case TCPS_LAST_ACK:
2798497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2799497057eeSRobert Watson 		return;
2800497057eeSRobert Watson 
2801497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2802497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2803497057eeSRobert Watson 		return;
2804497057eeSRobert Watson 
2805497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2806497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2807497057eeSRobert Watson 		return;
2808497057eeSRobert Watson 
2809497057eeSRobert Watson 	default:
2810497057eeSRobert Watson 		db_printf("unknown");
2811497057eeSRobert Watson 		return;
2812497057eeSRobert Watson 	}
2813497057eeSRobert Watson }
2814497057eeSRobert Watson 
2815497057eeSRobert Watson static void
2816497057eeSRobert Watson db_print_tflags(u_int t_flags)
2817497057eeSRobert Watson {
2818497057eeSRobert Watson 	int comma;
2819497057eeSRobert Watson 
2820497057eeSRobert Watson 	comma = 0;
2821497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2822497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2823497057eeSRobert Watson 		comma = 1;
2824497057eeSRobert Watson 	}
2825497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2826497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2827497057eeSRobert Watson 		comma = 1;
2828497057eeSRobert Watson 	}
2829497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2830497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2831497057eeSRobert Watson 		comma = 1;
2832497057eeSRobert Watson 	}
2833497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2834497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2835497057eeSRobert Watson 		comma = 1;
2836497057eeSRobert Watson 	}
2837497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2838497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2839497057eeSRobert Watson 		comma = 1;
2840497057eeSRobert Watson 	}
2841497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2842497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2843497057eeSRobert Watson 		comma = 1;
2844497057eeSRobert Watson 	}
2845497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2846497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2847497057eeSRobert Watson 		comma = 1;
2848497057eeSRobert Watson 	}
2849497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2850497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2851497057eeSRobert Watson 		comma = 1;
2852497057eeSRobert Watson 	}
2853497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2854497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2855497057eeSRobert Watson 		comma = 1;
2856497057eeSRobert Watson 	}
2857497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2858497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2859497057eeSRobert Watson 		comma = 1;
2860497057eeSRobert Watson 	}
2861497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2862497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2863497057eeSRobert Watson 		comma = 1;
2864497057eeSRobert Watson 	}
2865497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2866497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2867497057eeSRobert Watson 		comma = 1;
2868497057eeSRobert Watson 	}
2869497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2870497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2871497057eeSRobert Watson 		comma = 1;
2872497057eeSRobert Watson 	}
28733f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
28743f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
28753f169c54SRichard Scheffenegger 		comma = 1;
28763f169c54SRichard Scheffenegger 	}
2877093d9b46SMichael Tuexen 	if (t_flags & TF_WAKESOR) {
2878093d9b46SMichael Tuexen 		db_printf("%sTF_WAKESOR", comma ? ", " : "");
2879093d9b46SMichael Tuexen 		comma = 1;
2880093d9b46SMichael Tuexen 	}
2881093d9b46SMichael Tuexen 	if (t_flags & TF_GPUTINPROG) {
2882093d9b46SMichael Tuexen 		db_printf("%sTF_GPUTINPROG", comma ? ", " : "");
2883093d9b46SMichael Tuexen 		comma = 1;
2884093d9b46SMichael Tuexen 	}
2885497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2886497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2887497057eeSRobert Watson 		comma = 1;
2888497057eeSRobert Watson 	}
2889493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2890493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2891497057eeSRobert Watson 		comma = 1;
2892497057eeSRobert Watson 	}
2893497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2894497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2895497057eeSRobert Watson 		comma = 1;
2896497057eeSRobert Watson 	}
2897497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2898497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2899497057eeSRobert Watson 		comma = 1;
2900497057eeSRobert Watson 	}
2901497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2902497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2903497057eeSRobert Watson 		comma = 1;
2904497057eeSRobert Watson 	}
2905497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2906497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2907497057eeSRobert Watson 		comma = 1;
2908497057eeSRobert Watson 	}
2909497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2910497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2911497057eeSRobert Watson 		comma = 1;
2912497057eeSRobert Watson 	}
2913497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2914497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2915497057eeSRobert Watson 		comma = 1;
2916497057eeSRobert Watson 	}
2917497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2918497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2919497057eeSRobert Watson 		comma = 1;
2920497057eeSRobert Watson 	}
2921093d9b46SMichael Tuexen 	if (t_flags & TF_TOE) {
2922093d9b46SMichael Tuexen 		db_printf("%sTF_TOE", comma ? ", " : "");
2923093d9b46SMichael Tuexen 		comma = 1;
2924093d9b46SMichael Tuexen 	}
2925093d9b46SMichael Tuexen 	if (t_flags & TF_CLOSED) {
2926093d9b46SMichael Tuexen 		db_printf("%sTF_CLOSED", comma ? ", " : "");
2927093d9b46SMichael Tuexen 		comma = 1;
2928093d9b46SMichael Tuexen 	}
2929093d9b46SMichael Tuexen 	if (t_flags & TF_SENTSYN) {
2930093d9b46SMichael Tuexen 		db_printf("%sTF_SENTSYN", comma ? ", " : "");
2931093d9b46SMichael Tuexen 		comma = 1;
2932093d9b46SMichael Tuexen 	}
2933093d9b46SMichael Tuexen 	if (t_flags & TF_LRD) {
2934093d9b46SMichael Tuexen 		db_printf("%sTF_LRD", comma ? ", " : "");
2935093d9b46SMichael Tuexen 		comma = 1;
2936093d9b46SMichael Tuexen 	}
2937093d9b46SMichael Tuexen 	if (t_flags & TF_CONGRECOVERY) {
2938093d9b46SMichael Tuexen 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2939093d9b46SMichael Tuexen 		comma = 1;
2940093d9b46SMichael Tuexen 	}
2941093d9b46SMichael Tuexen 	if (t_flags & TF_WASCRECOVERY) {
2942093d9b46SMichael Tuexen 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2943093d9b46SMichael Tuexen 		comma = 1;
2944093d9b46SMichael Tuexen 	}
2945281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2946281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2947281a0fd4SPatrick Kelsey 		comma = 1;
2948281a0fd4SPatrick Kelsey 	}
2949497057eeSRobert Watson }
2950497057eeSRobert Watson 
2951497057eeSRobert Watson static void
29523cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29533cf38784SMichael Tuexen {
29543cf38784SMichael Tuexen 	int comma;
29553cf38784SMichael Tuexen 
29563cf38784SMichael Tuexen 	comma = 0;
29573f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29583f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29593f169c54SRichard Scheffenegger 		comma = 1;
29603f169c54SRichard Scheffenegger 	}
29613f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29623f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29633f169c54SRichard Scheffenegger 		comma = 1;
29643f169c54SRichard Scheffenegger 	}
29653f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29663f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29673f169c54SRichard Scheffenegger 		comma = 1;
29683f169c54SRichard Scheffenegger 	}
29693f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29703f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29713f169c54SRichard Scheffenegger 		comma = 1;
29723f169c54SRichard Scheffenegger 	}
29733f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29743f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29753f169c54SRichard Scheffenegger 		comma = 1;
29763f169c54SRichard Scheffenegger 	}
29773cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29783cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29793cf38784SMichael Tuexen 		comma = 1;
29803cf38784SMichael Tuexen 	}
29813f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
29823f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
29833f169c54SRichard Scheffenegger 		comma = 1;
29843f169c54SRichard Scheffenegger 	}
29853f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
29863f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
29873f169c54SRichard Scheffenegger 		comma = 1;
29883f169c54SRichard Scheffenegger 	}
29893f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
29903f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
29913f169c54SRichard Scheffenegger 		comma = 1;
29923f169c54SRichard Scheffenegger 	}
2993093d9b46SMichael Tuexen 	if (t_flags2 & TF2_HPTS_CPU_SET) {
2994093d9b46SMichael Tuexen 		db_printf("%sTF2_HPTS_CPU_SET", comma ? ", " : "");
2995093d9b46SMichael Tuexen 		comma = 1;
2996093d9b46SMichael Tuexen 	}
29973f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
29983f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
29993f169c54SRichard Scheffenegger 		comma = 1;
30003f169c54SRichard Scheffenegger 	}
3001093d9b46SMichael Tuexen 	if (t_flags2 & TF2_ECN_USE_ECT1) {
3002093d9b46SMichael Tuexen 		db_printf("%sTF2_ECN_USE_ECT1", comma ? ", " : "");
3003093d9b46SMichael Tuexen 		comma = 1;
3004093d9b46SMichael Tuexen 	}
3005093d9b46SMichael Tuexen 	if (t_flags2 & TF2_TCP_ACCOUNTING) {
3006093d9b46SMichael Tuexen 		db_printf("%sTF2_TCP_ACCOUNTING", comma ? ", " : "");
3007093d9b46SMichael Tuexen 		comma = 1;
3008093d9b46SMichael Tuexen 	}
3009093d9b46SMichael Tuexen 	if (t_flags2 & TF2_HPTS_CALLS) {
3010093d9b46SMichael Tuexen 		db_printf("%sTF2_HPTS_CALLS", comma ? ", " : "");
3011093d9b46SMichael Tuexen 		comma = 1;
3012093d9b46SMichael Tuexen 	}
3013093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_L_ACKS) {
3014093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_L_ACKS", comma ? ", " : "");
3015093d9b46SMichael Tuexen 		comma = 1;
3016093d9b46SMichael Tuexen 	}
3017093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_ACKCMP) {
3018093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_ACKCMP", comma ? ", " : "");
3019093d9b46SMichael Tuexen 		comma = 1;
3020093d9b46SMichael Tuexen 	}
3021093d9b46SMichael Tuexen 	if (t_flags2 & TF2_SUPPORTS_MBUFQ) {
3022093d9b46SMichael Tuexen 		db_printf("%sTF2_SUPPORTS_MBUFQ", comma ? ", " : "");
3023093d9b46SMichael Tuexen 		comma = 1;
3024093d9b46SMichael Tuexen 	}
3025093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_QUEUE_READY) {
3026093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_QUEUE_READY", comma ? ", " : "");
3027093d9b46SMichael Tuexen 		comma = 1;
3028093d9b46SMichael Tuexen 	}
3029093d9b46SMichael Tuexen 	if (t_flags2 & TF2_DONT_SACK_QUEUE) {
3030093d9b46SMichael Tuexen 		db_printf("%sTF2_DONT_SACK_QUEUE", comma ? ", " : "");
3031093d9b46SMichael Tuexen 		comma = 1;
3032093d9b46SMichael Tuexen 	}
3033093d9b46SMichael Tuexen 	if (t_flags2 & TF2_CANNOT_DO_ECN) {
3034093d9b46SMichael Tuexen 		db_printf("%sTF2_CANNOT_DO_ECN", comma ? ", " : "");
3035093d9b46SMichael Tuexen 		comma = 1;
3036093d9b46SMichael Tuexen 	}
3037093d9b46SMichael Tuexen 	if (t_flags2 & TF2_PROC_SACK_PROHIBIT) {
3038093d9b46SMichael Tuexen 		db_printf("%sTF2_PROC_SACK_PROHIBIT", comma ? ", " : "");
3039093d9b46SMichael Tuexen 		comma = 1;
3040093d9b46SMichael Tuexen 	}
3041093d9b46SMichael Tuexen 	if (t_flags2 & TF2_IPSEC_TSO) {
3042093d9b46SMichael Tuexen 		db_printf("%sTF2_IPSEC_TSO", comma ? ", " : "");
3043093d9b46SMichael Tuexen 		comma = 1;
3044093d9b46SMichael Tuexen 	}
3045093d9b46SMichael Tuexen 	if (t_flags2 & TF2_NO_ISS_CHECK) {
3046093d9b46SMichael Tuexen 		db_printf("%sTF2_NO_ISS_CHECK", comma ? ", " : "");
3047093d9b46SMichael Tuexen 		comma = 1;
3048093d9b46SMichael Tuexen 	}
30493cf38784SMichael Tuexen }
30503cf38784SMichael Tuexen 
30513cf38784SMichael Tuexen static void
3052497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3053497057eeSRobert Watson {
3054497057eeSRobert Watson 	int comma;
3055497057eeSRobert Watson 
3056497057eeSRobert Watson 	comma = 0;
3057497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3058497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3059497057eeSRobert Watson 		comma = 1;
3060497057eeSRobert Watson 	}
3061497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3062497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3063497057eeSRobert Watson 		comma = 1;
3064497057eeSRobert Watson 	}
3065497057eeSRobert Watson }
3066497057eeSRobert Watson 
3067497057eeSRobert Watson static void
3068497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3069497057eeSRobert Watson {
3070497057eeSRobert Watson 
3071497057eeSRobert Watson 	db_print_indent(indent);
3072497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3073497057eeSRobert Watson 
3074497057eeSRobert Watson 	indent += 2;
3075497057eeSRobert Watson 
3076497057eeSRobert Watson 	db_print_indent(indent);
3077497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3078c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3079497057eeSRobert Watson 
3080497057eeSRobert Watson 	db_print_indent(indent);
3081446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3082446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3083497057eeSRobert Watson 
3084497057eeSRobert Watson 	db_print_indent(indent);
3085497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3086497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3087497057eeSRobert Watson 	db_printf(")\n");
3088497057eeSRobert Watson 
3089497057eeSRobert Watson 	db_print_indent(indent);
3090497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3091497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3092497057eeSRobert Watson 	db_printf(")\n");
3093497057eeSRobert Watson 
3094497057eeSRobert Watson 	db_print_indent(indent);
30953cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30963cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30973cf38784SMichael Tuexen 	db_printf(")\n");
30983cf38784SMichael Tuexen 
30993cf38784SMichael Tuexen 	db_print_indent(indent);
3100fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3101497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3102497057eeSRobert Watson 
3103497057eeSRobert Watson 	db_print_indent(indent);
3104497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3105497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3106497057eeSRobert Watson 
3107497057eeSRobert Watson 	db_print_indent(indent);
3108497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3109497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3110497057eeSRobert Watson 
3111497057eeSRobert Watson 	db_print_indent(indent);
31123ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3113497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3114497057eeSRobert Watson 
3115497057eeSRobert Watson 	db_print_indent(indent);
31163ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
31171c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3118497057eeSRobert Watson 
3119497057eeSRobert Watson 	db_print_indent(indent);
31203ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
31211c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3122497057eeSRobert Watson 
3123497057eeSRobert Watson 	db_print_indent(indent);
31240c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
31250c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3126497057eeSRobert Watson 
3127497057eeSRobert Watson 	db_print_indent(indent);
31281c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
31291c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3130497057eeSRobert Watson 
3131497057eeSRobert Watson 	db_print_indent(indent);
31321c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
31331c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3134497057eeSRobert Watson 
3135497057eeSRobert Watson 	db_print_indent(indent);
3136bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3137bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3138497057eeSRobert Watson 
3139497057eeSRobert Watson 	db_print_indent(indent);
314018b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3141497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3142497057eeSRobert Watson 
3143497057eeSRobert Watson 	db_print_indent(indent);
3144497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3145497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3146497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3147497057eeSRobert Watson 
3148497057eeSRobert Watson 	db_print_indent(indent);
3149497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3150497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3151497057eeSRobert Watson 
3152497057eeSRobert Watson 	db_print_indent(indent);
31539f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31541a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3155497057eeSRobert Watson 
3156497057eeSRobert Watson 	db_print_indent(indent);
3157497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31583ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3159497057eeSRobert Watson 
3160497057eeSRobert Watson 	db_print_indent(indent);
31613ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31629f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3163497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3164497057eeSRobert Watson 
3165497057eeSRobert Watson 	db_print_indent(indent);
31663529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31673529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3168497057eeSRobert Watson 
3169497057eeSRobert Watson 	db_print_indent(indent);
3170a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3171a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3172497057eeSRobert Watson 
3173497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3174497057eeSRobert Watson 
3175497057eeSRobert Watson 	db_print_indent(indent);
3176497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3177497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3178497057eeSRobert Watson }
3179497057eeSRobert Watson 
3180497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3181497057eeSRobert Watson {
3182497057eeSRobert Watson 	struct tcpcb *tp;
3183497057eeSRobert Watson 
3184497057eeSRobert Watson 	if (!have_addr) {
3185497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3186497057eeSRobert Watson 		return;
3187497057eeSRobert Watson 	}
3188497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3189497057eeSRobert Watson 
3190497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3191497057eeSRobert Watson }
3192497057eeSRobert Watson #endif
3193