xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 6e76489098c6dc415ac3f2ae084154c3c22558ec)
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>
9809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
99bc65987aSKip Macy #include <netinet/tcp_offload.h>
10009fe6320SNavdeep Parhar #endif
101fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
102df8bae1dSRodney W. Grimes 
103adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
104adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
105adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
106adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
107adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
108adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
109adc56f5aSEdward Tomasz Napierala 
110df8bae1dSRodney W. Grimes /*
111df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
112df8bae1dSRodney W. Grimes  */
113b287c6c7SBjoern A. Zeeb #ifdef INET
114a9d22cceSGleb Smirnoff static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
1154d77a549SAlfred Perlstein 		    struct thread *td);
116b287c6c7SBjoern A. Zeeb #endif /* INET */
117fb59c426SYoshinobu Inoue #ifdef INET6
118a9d22cceSGleb Smirnoff static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
1194d77a549SAlfred Perlstein 		    struct thread *td);
120fb59c426SYoshinobu Inoue #endif /* INET6 */
121623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
122623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
1238c6104c4SMarius Strobl static void	tcp_fill_info(const struct tcpcb *, struct tcp_info *);
1242c37256eSGarrett Wollman 
125d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
126d3b6c96bSRandall Stewart 
12700812bbdSMichael Tuexen static void
tcp_bblog_pru(struct tcpcb * tp,uint32_t pru,int error)12800812bbdSMichael Tuexen tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
12900812bbdSMichael Tuexen {
13000812bbdSMichael Tuexen 	struct tcp_log_buffer *lgb;
13100812bbdSMichael Tuexen 
132c2399dd2SMichael Tuexen 	KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
13300812bbdSMichael Tuexen 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13469c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
13569c7c811SRandall Stewart 		lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
13600812bbdSMichael Tuexen 		    0, NULL, false, NULL, NULL, 0, NULL);
13700812bbdSMichael Tuexen 	} else {
13800812bbdSMichael Tuexen 		lgb = NULL;
13900812bbdSMichael Tuexen 	}
14000812bbdSMichael Tuexen 	if (lgb != NULL) {
14100812bbdSMichael Tuexen 		if (error >= 0) {
14200812bbdSMichael Tuexen 			lgb->tlb_errno = (uint32_t)error;
14300812bbdSMichael Tuexen 		}
14400812bbdSMichael Tuexen 		lgb->tlb_flex1 = pru;
14500812bbdSMichael Tuexen 	}
14600812bbdSMichael Tuexen }
14700812bbdSMichael Tuexen 
1482c37256eSGarrett Wollman /*
1492c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1502c37256eSGarrett Wollman  * and an internet control block.
1512c37256eSGarrett Wollman  */
1522c37256eSGarrett Wollman static int
tcp_usr_attach(struct socket * so,int proto,struct thread * td)153b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1542c37256eSGarrett Wollman {
155f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
156623dce13SRobert Watson 	struct tcpcb *tp = NULL;
157623dce13SRobert Watson 	int error;
1582c37256eSGarrett Wollman 
159623dce13SRobert Watson 	inp = sotoinpcb(so);
160623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1612c37256eSGarrett Wollman 
1620f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1632c37256eSGarrett Wollman 	if (error)
1642c37256eSGarrett Wollman 		goto out;
1652c37256eSGarrett Wollman 
1660f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1670f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1680f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1697669c586SGleb Smirnoff 	if (error)
1700f6385e7SGleb Smirnoff 		goto out;
1710f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
172baee801cSMichael Tuexen 	tp = tcp_newtcpcb(inp, NULL);
1730f6385e7SGleb Smirnoff 	if (tp == NULL) {
1747669c586SGleb Smirnoff 		error = ENOBUFS;
1750f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1760f6385e7SGleb Smirnoff 		goto out;
1770f6385e7SGleb Smirnoff 	}
1780f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
17900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ATTACH, error);
1800f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1810f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1822c37256eSGarrett Wollman out:
1835d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1840f6385e7SGleb Smirnoff 	return (error);
1852c37256eSGarrett Wollman }
1862c37256eSGarrett Wollman 
1872c37256eSGarrett Wollman /*
1883fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
189a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
190a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
191a152f8a3SRobert Watson  * inpcb state: time wait.
1922c37256eSGarrett Wollman  */
193bc725eafSRobert Watson static void
tcp_usr_detach(struct socket * so)1943fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
1952c37256eSGarrett Wollman {
1963fed74e9SGleb Smirnoff 	struct inpcb *inp;
1972c37256eSGarrett Wollman 	struct tcpcb *tp;
1982c37256eSGarrett Wollman 
1993fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2003fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2013fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2023fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2033fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
204953b5606SRobert Watson 
205a152f8a3SRobert Watson 	tp = intotcpcb(inp);
206a152f8a3SRobert Watson 
2071b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2081b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2091b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2109c3507f9SGleb Smirnoff 
211623dce13SRobert Watson 	tcp_discardcb(tp);
2120206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
213623dce13SRobert Watson }
214c78cbc7bSRobert Watson 
215b287c6c7SBjoern A. Zeeb #ifdef INET
2162c37256eSGarrett Wollman /*
2172c37256eSGarrett Wollman  * Give the socket an address.
2182c37256eSGarrett Wollman  */
2192c37256eSGarrett Wollman static int
tcp_usr_bind(struct socket * so,struct sockaddr * nam,struct thread * td)220b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2212c37256eSGarrett Wollman {
2222c37256eSGarrett Wollman 	int error = 0;
223f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
224c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2252c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2262c37256eSGarrett Wollman 
227c2399dd2SMichael Tuexen 	inp = sotoinpcb(so);
228c2399dd2SMichael Tuexen 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
229c2399dd2SMichael Tuexen 	INP_WLOCK(inp);
230c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
231c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
232c2399dd2SMichael Tuexen 		return (EINVAL);
233c2399dd2SMichael Tuexen 	}
234c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
235c2399dd2SMichael Tuexen 
23652710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
237f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
238f96603b5SMark Johnston 		/*
239f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
240f96603b5SMark Johnston 		 */
241f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2423f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
243c2399dd2SMichael Tuexen 		    sinp->sin_addr.s_addr != INADDR_ANY) {
244c2399dd2SMichael Tuexen 			error = EAFNOSUPPORT;
245c2399dd2SMichael Tuexen 			goto out;
246c2399dd2SMichael Tuexen 		}
247f96603b5SMark Johnston 		nam->sa_family = AF_INET;
248f96603b5SMark Johnston 	}
249c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sinp)) {
250c2399dd2SMichael Tuexen 		error = EINVAL;
251c2399dd2SMichael Tuexen 		goto out;
252c2399dd2SMichael Tuexen 	}
2532c37256eSGarrett Wollman 	/*
2542c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2552c37256eSGarrett Wollman 	 * to them.
2562c37256eSGarrett Wollman 	 */
257c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
258c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
2592c37256eSGarrett Wollman 		goto out;
260623dce13SRobert Watson 	}
261fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
2625dc99e9bSMark Johnston 	error = in_pcbbind(inp, sinp, V_tcp_bind_all_fibs ? 0 : INPBIND_FIB,
2635dc99e9bSMark Johnston 	    td->td_ucred);
264fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
265623dce13SRobert Watson out:
26600812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
2675d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2688501a69cSRobert Watson 	INP_WUNLOCK(inp);
269623dce13SRobert Watson 
270623dce13SRobert Watson 	return (error);
2712c37256eSGarrett Wollman }
272b287c6c7SBjoern A. Zeeb #endif /* INET */
2732c37256eSGarrett Wollman 
274fb59c426SYoshinobu Inoue #ifdef INET6
275fb59c426SYoshinobu Inoue static int
tcp6_usr_bind(struct socket * so,struct sockaddr * nam,struct thread * td)276b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
277fb59c426SYoshinobu Inoue {
278fb59c426SYoshinobu Inoue 	int error = 0;
279f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
280c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2810ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2824a91aa8fSMichael Tuexen 	u_char vflagsav;
283fb59c426SYoshinobu Inoue 
284623dce13SRobert Watson 	inp = sotoinpcb(so);
285623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2868501a69cSRobert Watson 	INP_WLOCK(inp);
28753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
288c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
289c2399dd2SMichael Tuexen 		return (EINVAL);
290c2399dd2SMichael Tuexen 	}
291c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
292c2399dd2SMichael Tuexen 
293c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
294c2399dd2SMichael Tuexen 
295c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
296c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
297c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
298c2399dd2SMichael Tuexen 		goto out;
299c2399dd2SMichael Tuexen 	}
300c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sin6)) {
301623dce13SRobert Watson 		error = EINVAL;
302623dce13SRobert Watson 		goto out;
303623dce13SRobert Watson 	}
304c2399dd2SMichael Tuexen 	/*
305c2399dd2SMichael Tuexen 	 * Must check for multicast addresses and disallow binding
306c2399dd2SMichael Tuexen 	 * to them.
307c2399dd2SMichael Tuexen 	 */
308c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
309c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
310c2399dd2SMichael Tuexen 		goto out;
311c2399dd2SMichael Tuexen 	}
312c2399dd2SMichael Tuexen 
313fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
314fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
315fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
316b287c6c7SBjoern A. Zeeb #ifdef INET
31766ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3180ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
319fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3200ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
321fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
322fb59c426SYoshinobu Inoue 
3230ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
324888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
325888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
326888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
327888973f5SMichael Tuexen 				goto out;
328888973f5SMichael Tuexen 			}
329fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
330fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
331bbd0084bSMark Johnston 			error = in_pcbbind(inp, &sin, 0, td->td_ucred);
332fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
333fb59c426SYoshinobu Inoue 			goto out;
334fb59c426SYoshinobu Inoue 		}
335fb59c426SYoshinobu Inoue 	}
336b287c6c7SBjoern A. Zeeb #endif
3375dc99e9bSMark Johnston 	error = in6_pcbbind(inp, sin6, V_tcp_bind_all_fibs ? 0 : INPBIND_FIB,
3385dc99e9bSMark Johnston 	    td->td_ucred);
339fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
340623dce13SRobert Watson out:
3414a91aa8fSMichael Tuexen 	if (error != 0)
3424a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
34300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
3445d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3458501a69cSRobert Watson 	INP_WUNLOCK(inp);
346623dce13SRobert Watson 	return (error);
347fb59c426SYoshinobu Inoue }
348fb59c426SYoshinobu Inoue #endif /* INET6 */
349fb59c426SYoshinobu Inoue 
350b287c6c7SBjoern A. Zeeb #ifdef INET
3512c37256eSGarrett Wollman /*
3522c37256eSGarrett Wollman  * Prepare to accept connections.
3532c37256eSGarrett Wollman  */
3542c37256eSGarrett Wollman static int
tcp_usr_listen(struct socket * so,int backlog,struct thread * td)355d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3562c37256eSGarrett Wollman {
357f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
358c2399dd2SMichael Tuexen 	struct tcpcb *tp;
35906bf119fSGleb Smirnoff 	int error = 0;
36006bf119fSGleb Smirnoff 	bool already_listening;
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);
37206bf119fSGleb Smirnoff 	already_listening = SOLISTENING(so);
3730daccb9cSRobert Watson 	error = solisten_proto_check(so);
374bd4a39ccSMark Johnston 	if (error != 0) {
375bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
376bd4a39ccSMark Johnston 		goto out;
377bd4a39ccSMark Johnston 	}
378bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
379fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
3805dc99e9bSMark Johnston 		error = in_pcbbind(inp, NULL,
3815dc99e9bSMark Johnston 		    V_tcp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
382fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
383bd4a39ccSMark Johnston 	}
3840daccb9cSRobert Watson 	if (error == 0) {
38557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
386d374e81eSRobert Watson 		solisten_proto(so, backlog);
38709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
38837cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
38909fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
39009fe6320SNavdeep Parhar #endif
391bd4a39ccSMark Johnston 	} else {
392bd4a39ccSMark Johnston 		solisten_proto_abort(so);
3930daccb9cSRobert Watson 	}
3940daccb9cSRobert Watson 	SOCK_UNLOCK(so);
39506bf119fSGleb Smirnoff 	if (already_listening)
39606bf119fSGleb Smirnoff 		goto out;
397623dce13SRobert Watson 
3987cbb6b6eSMark Johnston 	if (error == 0)
3997cbb6b6eSMark Johnston 		in_pcblisten(inp);
400dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
401281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
40218a75309SPatrick Kelsey 
403623dce13SRobert Watson out:
40400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4055d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4068501a69cSRobert Watson 	INP_WUNLOCK(inp);
407623dce13SRobert Watson 	return (error);
4082c37256eSGarrett Wollman }
409b287c6c7SBjoern A. Zeeb #endif /* INET */
4102c37256eSGarrett Wollman 
411fb59c426SYoshinobu Inoue #ifdef INET6
412fb59c426SYoshinobu Inoue static int
tcp6_usr_listen(struct socket * so,int backlog,struct thread * td)413d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
414fb59c426SYoshinobu Inoue {
415f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
416c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4174a91aa8fSMichael Tuexen 	u_char vflagsav;
41806bf119fSGleb Smirnoff 	int error = 0;
41906bf119fSGleb Smirnoff 	bool already_listening;
420fb59c426SYoshinobu Inoue 
421623dce13SRobert Watson 	inp = sotoinpcb(so);
422623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4238501a69cSRobert Watson 	INP_WLOCK(inp);
42453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
425c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
426c2399dd2SMichael Tuexen 		return (EINVAL);
427623dce13SRobert Watson 	}
428623dce13SRobert Watson 	tp = intotcpcb(inp);
429c2399dd2SMichael Tuexen 
430c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
431c2399dd2SMichael Tuexen 
4320daccb9cSRobert Watson 	SOCK_LOCK(so);
43306bf119fSGleb Smirnoff 	already_listening = SOLISTENING(so);
4340daccb9cSRobert Watson 	error = solisten_proto_check(so);
435bd4a39ccSMark Johnston 	if (error != 0) {
436bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
437bd4a39ccSMark Johnston 		goto out;
438bd4a39ccSMark Johnston 	}
439fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
440bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
441fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
44266ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
443fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
4445dc99e9bSMark Johnston 		error = in6_pcbbind(inp, NULL,
4455dc99e9bSMark Johnston 		    V_tcp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
446fb59c426SYoshinobu Inoue 	}
447fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4480daccb9cSRobert Watson 	if (error == 0) {
44957f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
450d374e81eSRobert Watson 		solisten_proto(so, backlog);
45109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
45237cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45309fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45409fe6320SNavdeep Parhar #endif
455bd4a39ccSMark Johnston 	} else {
456bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4570daccb9cSRobert Watson 	}
4580daccb9cSRobert Watson 	SOCK_UNLOCK(so);
45906bf119fSGleb Smirnoff 	if (already_listening)
46006bf119fSGleb Smirnoff 		goto out;
461623dce13SRobert Watson 
4627cbb6b6eSMark Johnston 	if (error == 0)
4637cbb6b6eSMark Johnston 		in_pcblisten(inp);
464dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
465281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
46618a75309SPatrick Kelsey 
4674a91aa8fSMichael Tuexen 	if (error != 0)
4684a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4694a91aa8fSMichael Tuexen 
470623dce13SRobert Watson out:
47100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4725d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4738501a69cSRobert Watson 	INP_WUNLOCK(inp);
474623dce13SRobert Watson 	return (error);
475fb59c426SYoshinobu Inoue }
476fb59c426SYoshinobu Inoue #endif /* INET6 */
477fb59c426SYoshinobu Inoue 
478b287c6c7SBjoern A. Zeeb #ifdef INET
4792c37256eSGarrett Wollman /*
4802c37256eSGarrett Wollman  * Initiate connection to peer.
4812c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4822c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4832c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4842c37256eSGarrett Wollman  * Send initial segment on connection.
4852c37256eSGarrett Wollman  */
4862c37256eSGarrett Wollman static int
tcp_usr_connect(struct socket * so,struct sockaddr * nam,struct thread * td)487b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4882c37256eSGarrett Wollman {
489109eb549SGleb Smirnoff 	struct epoch_tracker et;
4902c37256eSGarrett Wollman 	int error = 0;
491f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
492c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4932c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4942c37256eSGarrett Wollman 
495623dce13SRobert Watson 	inp = sotoinpcb(so);
496623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4978501a69cSRobert Watson 	INP_WLOCK(inp);
498eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
499c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
500c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
501c2399dd2SMichael Tuexen 	}
502c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
503c2399dd2SMichael Tuexen 
504c2399dd2SMichael Tuexen 	sinp = (struct sockaddr_in *)nam;
505c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET) {
506c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
507623dce13SRobert Watson 		goto out;
508623dce13SRobert Watson 	}
509c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sinp)) {
510c2399dd2SMichael Tuexen 		error = EINVAL;
511c2399dd2SMichael Tuexen 		goto out;
512c2399dd2SMichael Tuexen 	}
513c2399dd2SMichael Tuexen 	/*
514c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
515c2399dd2SMichael Tuexen 	 */
516c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
517c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
518c2399dd2SMichael Tuexen 		goto out;
519c2399dd2SMichael Tuexen 	}
520c2399dd2SMichael Tuexen 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
521c2399dd2SMichael Tuexen 		error = EACCES;
522c2399dd2SMichael Tuexen 		goto out;
523c2399dd2SMichael Tuexen 	}
524c2399dd2SMichael Tuexen 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
525c2399dd2SMichael Tuexen 		goto out;
526*c7f803c7SGleb Smirnoff 	if (SOLISTENING(so) || so->so_options & SO_REUSEPORT_LB) {
527bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
528bd4a39ccSMark Johnston 		goto out;
529bd4a39ccSMark Johnston 	}
530c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
531a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
532c1604fe4SGleb Smirnoff 		goto out_in_epoch;
53309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
53409fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
53537cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
53609fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
537c1604fe4SGleb Smirnoff 		goto out_in_epoch;
53809fe6320SNavdeep Parhar #endif
53909fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
54040fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5411d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5421d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
543c1604fe4SGleb Smirnoff out_in_epoch:
544109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
545623dce13SRobert Watson out:
54600812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
547e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5488501a69cSRobert Watson 	INP_WUNLOCK(inp);
549623dce13SRobert Watson 	return (error);
5502c37256eSGarrett Wollman }
551b287c6c7SBjoern A. Zeeb #endif /* INET */
5522c37256eSGarrett Wollman 
553fb59c426SYoshinobu Inoue #ifdef INET6
554fb59c426SYoshinobu Inoue static int
tcp6_usr_connect(struct socket * so,struct sockaddr * nam,struct thread * td)555b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
556fb59c426SYoshinobu Inoue {
557109eb549SGleb Smirnoff 	struct epoch_tracker et;
558fb59c426SYoshinobu Inoue 	int error = 0;
559f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
560c2399dd2SMichael Tuexen 	struct tcpcb *tp;
5610ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5624a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5634a91aa8fSMichael Tuexen 	u_char vflagsav;
564623dce13SRobert Watson 
565623dce13SRobert Watson 	inp = sotoinpcb(so);
566623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5678501a69cSRobert Watson 	INP_WLOCK(inp);
568c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
569c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
570c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
571c2399dd2SMichael Tuexen 	}
572c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
573c2399dd2SMichael Tuexen 
5744a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5754a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
576c2399dd2SMichael Tuexen 
577c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
578c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
579c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
580c2399dd2SMichael Tuexen 		goto out;
581c2399dd2SMichael Tuexen 	}
582c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sin6)) {
583c2399dd2SMichael Tuexen 		error = EINVAL;
584c2399dd2SMichael Tuexen 		goto out;
585c2399dd2SMichael Tuexen 	}
586c2399dd2SMichael Tuexen 	/*
587c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
588c2399dd2SMichael Tuexen 	 */
589c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
590c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
591623dce13SRobert Watson 		goto out;
592623dce13SRobert Watson 	}
593*c7f803c7SGleb Smirnoff 	if (SOLISTENING(so) || so->so_options & SO_REUSEPORT_LB) {
594e92a78adSGleb Smirnoff 		error = EOPNOTSUPP;
595bd4a39ccSMark Johnston 		goto out;
596bd4a39ccSMark Johnston 	}
597b287c6c7SBjoern A. Zeeb #ifdef INET
598fa046d87SRobert Watson 	/*
599fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
600fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
601fa046d87SRobert Watson 	 * Is this a significant problem?
602fa046d87SRobert Watson 	 */
6030ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
604fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
605fb59c426SYoshinobu Inoue 
606d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
607d46a5312SMaxim Konovalov 			error = EINVAL;
608d46a5312SMaxim Konovalov 			goto out;
609d46a5312SMaxim Konovalov 		}
6105dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6115dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6125dba6adaSMichael Tuexen 			goto out;
6135dba6adaSMichael Tuexen 		}
61433841545SHajimu UMEMOTO 
6150ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
616888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
617888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
618888973f5SMichael Tuexen 			goto out;
619888973f5SMichael Tuexen 		}
620f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
621f903a308SMichael Tuexen 			error = EACCES;
6222cf21ae5SRandall Stewart 			goto out;
6232cf21ae5SRandall Stewart 		}
624b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
625b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
626413628a7SBjoern A. Zeeb 			goto out;
6274a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6284a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
629c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
630a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
631c1604fe4SGleb Smirnoff 			goto out_in_epoch;
63209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
63309fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
634adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
63509fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
636c1604fe4SGleb Smirnoff 			goto out_in_epoch;
63709fe6320SNavdeep Parhar #endif
63840fa3e40SGleb Smirnoff 		error = tcp_output(tp);
639c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6405dba6adaSMichael Tuexen 	} else {
6415dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6425dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6435dba6adaSMichael Tuexen 			goto out;
6445dba6adaSMichael Tuexen 		}
645fb59c426SYoshinobu Inoue 	}
646b287c6c7SBjoern A. Zeeb #endif
6474a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6484a91aa8fSMichael Tuexen 		goto out;
649fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
650fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
651dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6520773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
653a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6540773b44eSGleb Smirnoff 		goto out_in_epoch;
65509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
65609fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
657adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
65809fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6590773b44eSGleb Smirnoff 		goto out_in_epoch;
66009fe6320SNavdeep Parhar #endif
66109fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
66240fa3e40SGleb Smirnoff 	error = tcp_output(tp);
663c1604fe4SGleb Smirnoff out_in_epoch:
664109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
665623dce13SRobert Watson out:
6661d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6671d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6684a91aa8fSMichael Tuexen 	/*
6694a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6704a91aa8fSMichael Tuexen 	 * the flags we modified.
6714a91aa8fSMichael Tuexen 	 */
6724a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6734a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6744a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6754a91aa8fSMichael Tuexen 	}
6764a91aa8fSMichael Tuexen 
67700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6785d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6798501a69cSRobert Watson 	INP_WUNLOCK(inp);
680623dce13SRobert Watson 	return (error);
681fb59c426SYoshinobu Inoue }
682fb59c426SYoshinobu Inoue #endif /* INET6 */
683fb59c426SYoshinobu Inoue 
6842c37256eSGarrett Wollman /*
6852c37256eSGarrett Wollman  * Initiate disconnect from peer.
6862c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6872c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6882c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6892c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6902c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6912c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6922c37256eSGarrett Wollman  *
6932c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6942c37256eSGarrett Wollman  */
6952c37256eSGarrett Wollman static int
tcp_usr_disconnect(struct socket * so)6962c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6972c37256eSGarrett Wollman {
698f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
699623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7006573d758SMatt Macy 	struct epoch_tracker et;
7012c37256eSGarrett Wollman 
70297a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
703623dce13SRobert Watson 	inp = sotoinpcb(so);
704623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7058501a69cSRobert Watson 	INP_WLOCK(inp);
706623dce13SRobert Watson 	tp = intotcpcb(inp);
707c2399dd2SMichael Tuexen 
708cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
709cda6bdbaSJohn Baldwin 		goto out;
710623dce13SRobert Watson 	tcp_disconnect(tp);
711623dce13SRobert Watson out:
712c91dd7a0SGleb Smirnoff 	tcp_bblog_pru(tp, PRU_DISCONNECT, 0);
7135d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7148501a69cSRobert Watson 	INP_WUNLOCK(inp);
71597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
716c91dd7a0SGleb Smirnoff 	return (0);
7172c37256eSGarrett Wollman }
7182c37256eSGarrett Wollman 
719b287c6c7SBjoern A. Zeeb #ifdef INET
7202c37256eSGarrett Wollman /*
7218296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7228296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7232c37256eSGarrett Wollman  */
7242c37256eSGarrett Wollman static int
tcp_usr_accept(struct socket * so,struct sockaddr * sa)725cfb1e929SGleb Smirnoff tcp_usr_accept(struct socket *so, struct sockaddr *sa)
7262c37256eSGarrett Wollman {
727c2399dd2SMichael Tuexen 	struct inpcb *inp;
728c2399dd2SMichael Tuexen 	struct tcpcb *tp;
729cfb1e929SGleb Smirnoff 	int error = 0;
7302c37256eSGarrett Wollman 
731f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
732623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7338501a69cSRobert Watson 	INP_WLOCK(inp);
73453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
735c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
736c2399dd2SMichael Tuexen 		return (ECONNABORTED);
737623dce13SRobert Watson 	}
7381db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
739f76fcf6dSJeffrey Hsu 
740cfb1e929SGleb Smirnoff 	if (so->so_state & SS_ISDISCONNECTED)
741c2399dd2SMichael Tuexen 		error = ECONNABORTED;
742cfb1e929SGleb Smirnoff 	else
743cfb1e929SGleb Smirnoff 		*(struct sockaddr_in *)sa = (struct sockaddr_in ){
744cfb1e929SGleb Smirnoff 			.sin_family = AF_INET,
745cfb1e929SGleb Smirnoff 			.sin_len = sizeof(struct sockaddr_in),
746cfb1e929SGleb Smirnoff 			.sin_port = inp->inp_fport,
747cfb1e929SGleb Smirnoff 			.sin_addr = inp->inp_faddr,
748cfb1e929SGleb Smirnoff 		};
74900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7505d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7518501a69cSRobert Watson 	INP_WUNLOCK(inp);
752cfb1e929SGleb Smirnoff 
753cfb1e929SGleb Smirnoff 	return (error);
7542c37256eSGarrett Wollman }
755b287c6c7SBjoern A. Zeeb #endif /* INET */
7562c37256eSGarrett Wollman 
757fb59c426SYoshinobu Inoue #ifdef INET6
758fb59c426SYoshinobu Inoue static int
tcp6_usr_accept(struct socket * so,struct sockaddr * sa)759cfb1e929SGleb Smirnoff tcp6_usr_accept(struct socket *so, struct sockaddr *sa)
760fb59c426SYoshinobu Inoue {
761c2399dd2SMichael Tuexen 	struct inpcb *inp;
762c2399dd2SMichael Tuexen 	struct tcpcb *tp;
763cfb1e929SGleb Smirnoff 	int error = 0;
764fb59c426SYoshinobu Inoue 
765f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
766623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
7678501a69cSRobert Watson 	INP_WLOCK(inp);
76853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
769c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
770c2399dd2SMichael Tuexen 		return (ECONNABORTED);
771623dce13SRobert Watson 	}
7721db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
773623dce13SRobert Watson 
774c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
775c2399dd2SMichael Tuexen 		error = ECONNABORTED;
77626ef6ac4SDon Lewis 	} else {
777cfb1e929SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV4) {
778cfb1e929SGleb Smirnoff 			struct sockaddr_in sin = {
779cfb1e929SGleb Smirnoff 				.sin_family = AF_INET,
780cfb1e929SGleb Smirnoff 				.sin_len = sizeof(struct sockaddr_in),
781cfb1e929SGleb Smirnoff 				.sin_port = inp->inp_fport,
782cfb1e929SGleb Smirnoff 				.sin_addr = inp->inp_faddr,
783cfb1e929SGleb Smirnoff 			};
784cfb1e929SGleb Smirnoff 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
785cfb1e929SGleb Smirnoff 		} else {
786cfb1e929SGleb Smirnoff 			*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
787cfb1e929SGleb Smirnoff 				.sin6_family = AF_INET6,
788cfb1e929SGleb Smirnoff 				.sin6_len = sizeof(struct sockaddr_in6),
789cfb1e929SGleb Smirnoff 				.sin6_port = inp->inp_fport,
790cfb1e929SGleb Smirnoff 				.sin6_addr = inp->in6p_faddr,
791cfb1e929SGleb Smirnoff 			};
792cfb1e929SGleb Smirnoff 			/* XXX: should catch errors */
793cfb1e929SGleb Smirnoff 			(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
794cfb1e929SGleb Smirnoff 		}
79526ef6ac4SDon Lewis 	}
79626ef6ac4SDon Lewis 
79700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7985d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7998501a69cSRobert Watson 	INP_WUNLOCK(inp);
800cfb1e929SGleb Smirnoff 
801cfb1e929SGleb Smirnoff 	return (error);
802fb59c426SYoshinobu Inoue }
803fb59c426SYoshinobu Inoue #endif /* INET6 */
804f76fcf6dSJeffrey Hsu 
805f76fcf6dSJeffrey Hsu /*
8062c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8072c37256eSGarrett Wollman  */
8082c37256eSGarrett Wollman static int
tcp_usr_shutdown(struct socket * so,enum shutdown_how how)8095bba2728SGleb Smirnoff tcp_usr_shutdown(struct socket *so, enum shutdown_how how)
8102c37256eSGarrett Wollman {
8116573d758SMatt Macy 	struct epoch_tracker et;
8125bba2728SGleb Smirnoff 	struct inpcb *inp = sotoinpcb(so);
8135bba2728SGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
8145bba2728SGleb Smirnoff 	int error = 0;
8152c37256eSGarrett Wollman 
8165bba2728SGleb Smirnoff 	SOCK_LOCK(so);
8175bba2728SGleb Smirnoff 	if (SOLISTENING(so)) {
8185bba2728SGleb Smirnoff 		if (how != SHUT_WR) {
8195bba2728SGleb Smirnoff 			so->so_error = ECONNABORTED;
8205bba2728SGleb Smirnoff 			solisten_wakeup(so);	/* unlocks so */
8215bba2728SGleb Smirnoff 		} else
8225bba2728SGleb Smirnoff 			SOCK_UNLOCK(so);
823abe8379bSGleb Smirnoff 		return (ENOTCONN);
824abe8379bSGleb Smirnoff 	} else if ((so->so_state &
825abe8379bSGleb Smirnoff 	    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
826abe8379bSGleb Smirnoff 		SOCK_UNLOCK(so);
827abe8379bSGleb Smirnoff 		return (ENOTCONN);
8285bba2728SGleb Smirnoff 	}
8295bba2728SGleb Smirnoff 	SOCK_UNLOCK(so);
8305bba2728SGleb Smirnoff 
8315bba2728SGleb Smirnoff 	switch (how) {
8325bba2728SGleb Smirnoff 	case SHUT_RD:
833ce69e373SGleb Smirnoff 		sorflush(so);
8345bba2728SGleb Smirnoff 		break;
8355bba2728SGleb Smirnoff 	case SHUT_RDWR:
836ce69e373SGleb Smirnoff 		sorflush(so);
8375bba2728SGleb Smirnoff 		/* FALLTHROUGH */
8385bba2728SGleb Smirnoff 	case SHUT_WR:
8395bba2728SGleb Smirnoff 		/*
8405bba2728SGleb Smirnoff 		 * XXXGL: mimicing old soshutdown() here. But shouldn't we
8415bba2728SGleb Smirnoff 		 * return ECONNRESEST for SHUT_RD as well?
8425bba2728SGleb Smirnoff 		 */
8438501a69cSRobert Watson 		INP_WLOCK(inp);
84453af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
8450af4ce45SGleb Smirnoff 			INP_WUNLOCK(inp);
8460af4ce45SGleb Smirnoff 			return (ECONNRESET);
847623dce13SRobert Watson 		}
848c2399dd2SMichael Tuexen 
8492c37256eSGarrett Wollman 		socantsendmore(so);
8505bba2728SGleb Smirnoff 		NET_EPOCH_ENTER(et);
851623dce13SRobert Watson 		tcp_usrclosed(tp);
852f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
85300812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8545d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
855f64dc2abSGleb Smirnoff 		error = tcp_unlock_or_drop(tp, error);
85697a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
8575bba2728SGleb Smirnoff 	}
8585bba2728SGleb Smirnoff 	wakeup(&so->so_timeo);
859623dce13SRobert Watson 
860623dce13SRobert Watson 	return (error);
8612c37256eSGarrett Wollman }
8622c37256eSGarrett Wollman 
8632c37256eSGarrett Wollman /*
8642c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8652c37256eSGarrett Wollman  */
8662c37256eSGarrett Wollman static int
tcp_usr_rcvd(struct socket * so,int flags)8672c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8682c37256eSGarrett Wollman {
869109eb549SGleb Smirnoff 	struct epoch_tracker et;
870f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
871c2399dd2SMichael Tuexen 	struct tcpcb *tp;
872f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8732c37256eSGarrett Wollman 
874623dce13SRobert Watson 	inp = sotoinpcb(so);
875623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8768501a69cSRobert Watson 	INP_WLOCK(inp);
87753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
87837a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
87937a7f557SGleb Smirnoff 		return (ECONNRESET);
880623dce13SRobert Watson 	}
88137a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
882c2399dd2SMichael Tuexen 
88337a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
884281a0fd4SPatrick Kelsey 	/*
885281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
886281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
887281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
888281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
889281a0fd4SPatrick Kelsey 	 * expires.
890281a0fd4SPatrick Kelsey 	 */
891dd7b86e2SGleb Smirnoff 	if ((tp->t_flags & TF_FASTOPEN) && (tp->t_state == TCPS_SYN_RECEIVED))
892281a0fd4SPatrick Kelsey 		goto out;
89309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
89409fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
89509fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
896460cf046SNavdeep Parhar 	else
89709fe6320SNavdeep Parhar #endif
898f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
899623dce13SRobert Watson out:
90000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
9015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
902f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
903f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
904623dce13SRobert Watson 	return (error);
9052c37256eSGarrett Wollman }
9062c37256eSGarrett Wollman 
9072c37256eSGarrett Wollman /*
9082c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9099c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9109c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9119c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9129c9906e9SPeter Wemm  * generally are caller-frees.
9132c37256eSGarrett Wollman  */
9142c37256eSGarrett Wollman static int
tcp_usr_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)91557bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
916b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9172c37256eSGarrett Wollman {
91897a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9192c37256eSGarrett Wollman 	int error = 0;
920f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
921c2399dd2SMichael Tuexen 	struct tcpcb *tp;
922888973f5SMichael Tuexen #ifdef INET
92351e08d53SMichael Tuexen #ifdef INET6
92451e08d53SMichael Tuexen 	struct sockaddr_in sin;
92551e08d53SMichael Tuexen #endif
92651e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
927888973f5SMichael Tuexen #endif
928fb59c426SYoshinobu Inoue #ifdef INET6
929a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
930fb59c426SYoshinobu Inoue 	int isipv6;
931fb59c426SYoshinobu Inoue #endif
9324a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9334a91aa8fSMichael Tuexen 	u_char vflagsav;
9344a91aa8fSMichael Tuexen 	bool restoreflags;
9352c37256eSGarrett Wollman 
9364287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9374287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9384287aa56SGleb Smirnoff 	INP_WLOCK(inp);
93953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9404287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9414287aa56SGleb Smirnoff 			m_freem(m);
9424287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9434287aa56SGleb Smirnoff 		return (ECONNRESET);
9444287aa56SGleb Smirnoff 	}
945c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
9464287aa56SGleb Smirnoff 
9474287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9484287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9494287aa56SGleb Smirnoff 	restoreflags = false;
9504287aa56SGleb Smirnoff 
9514287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
952c2399dd2SMichael Tuexen 	if (control != NULL) {
953c2399dd2SMichael Tuexen 		/* TCP doesn't do control messages (rights, creds, etc) */
954c2399dd2SMichael Tuexen 		if (control->m_len > 0) {
955c2399dd2SMichael Tuexen 			m_freem(control);
956c2399dd2SMichael Tuexen 			error = EINVAL;
957c2399dd2SMichael Tuexen 			goto out;
958c2399dd2SMichael Tuexen 		}
959c2399dd2SMichael Tuexen 		m_freem(control);	/* empty control, just free it */
960c2399dd2SMichael Tuexen 	}
961c2399dd2SMichael Tuexen 
9627d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9637d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
964d3b6c96bSRandall Stewart 		goto out;
9657d2608a5SMark Johnston 
966888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
967bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
968bd4a39ccSMark Johnston 			error = EINVAL;
969bd4a39ccSMark Johnston 			goto out;
970bd4a39ccSMark Johnston 		}
971888973f5SMichael Tuexen 		switch (nam->sa_family) {
972888973f5SMichael Tuexen #ifdef INET
973888973f5SMichael Tuexen 		case AF_INET:
974888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
975888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
976888973f5SMichael Tuexen 				error = EINVAL;
977888973f5SMichael Tuexen 				goto out;
978888973f5SMichael Tuexen 			}
979888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
980888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
981888973f5SMichael Tuexen 				goto out;
982888973f5SMichael Tuexen 			}
983888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
984888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
985888973f5SMichael Tuexen 				goto out;
986888973f5SMichael Tuexen 			}
987f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
988f903a308SMichael Tuexen 				error = EACCES;
9892cf21ae5SRandall Stewart 				goto out;
9902cf21ae5SRandall Stewart 			}
991888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9927d2608a5SMark Johnston 			    &sinp->sin_addr)))
993888973f5SMichael Tuexen 				goto out;
994888973f5SMichael Tuexen #ifdef INET6
995888973f5SMichael Tuexen 			isipv6 = 0;
996888973f5SMichael Tuexen #endif
997888973f5SMichael Tuexen 			break;
998888973f5SMichael Tuexen #endif /* INET */
999888973f5SMichael Tuexen #ifdef INET6
1000888973f5SMichael Tuexen 		case AF_INET6:
10010ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10020ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1003888973f5SMichael Tuexen 				error = EINVAL;
1004888973f5SMichael Tuexen 				goto out;
1005888973f5SMichael Tuexen 			}
1006e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1007e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1008e240ce42SMichael Tuexen 				goto out;
1009e240ce42SMichael Tuexen 			}
10100ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1011888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1012888973f5SMichael Tuexen 				goto out;
1013888973f5SMichael Tuexen 			}
10140ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1015888973f5SMichael Tuexen #ifdef INET
1016888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1017888973f5SMichael Tuexen 					error = EINVAL;
1018888973f5SMichael Tuexen 					goto out;
1019888973f5SMichael Tuexen 				}
1020888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1021888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1022888973f5SMichael Tuexen 					goto out;
1023888973f5SMichael Tuexen 				}
10244a91aa8fSMichael Tuexen 				restoreflags = true;
1025888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1026888973f5SMichael Tuexen 				sinp = &sin;
10270ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1028888973f5SMichael Tuexen 				if (IN_MULTICAST(
1029888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1030888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1031888973f5SMichael Tuexen 					goto out;
1032888973f5SMichael Tuexen 				}
1033888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10347d2608a5SMark Johnston 				    &sinp->sin_addr)))
1035888973f5SMichael Tuexen 					goto out;
1036888973f5SMichael Tuexen 				isipv6 = 0;
1037888973f5SMichael Tuexen #else /* !INET */
1038888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1039888973f5SMichael Tuexen 				goto out;
1040888973f5SMichael Tuexen #endif /* INET */
1041888973f5SMichael Tuexen 			} else {
1042888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1043888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1044888973f5SMichael Tuexen 					goto out;
1045888973f5SMichael Tuexen 				}
10464a91aa8fSMichael Tuexen 				restoreflags = true;
1047888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1048888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1049888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10507d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1051888973f5SMichael Tuexen 					goto out;
1052888973f5SMichael Tuexen 				isipv6 = 1;
1053888973f5SMichael Tuexen 			}
1054888973f5SMichael Tuexen 			break;
1055888973f5SMichael Tuexen #endif /* INET6 */
1056888973f5SMichael Tuexen 		default:
1057888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1058888973f5SMichael Tuexen 			goto out;
1059888973f5SMichael Tuexen 		}
1060888973f5SMichael Tuexen 	}
10612c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
106208af8aacSRandall Stewart 		if (tp->t_acktime == 0)
106308af8aacSRandall Stewart 			tp->t_acktime = ticks;
1064651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10657d2608a5SMark Johnston 		m = NULL;
10662c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1067bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1068bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1069bd4a39ccSMark Johnston 
10702c37256eSGarrett Wollman 			/*
10712c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10722c37256eSGarrett Wollman 			 * initialize window to default value, and
10730c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10742c37256eSGarrett Wollman 			 */
1075fb59c426SYoshinobu Inoue #ifdef INET6
1076fb59c426SYoshinobu Inoue 			if (isipv6)
1077a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1078fb59c426SYoshinobu Inoue #endif /* INET6 */
1079b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1080b287c6c7SBjoern A. Zeeb 			else
1081b287c6c7SBjoern A. Zeeb #endif
1082b287c6c7SBjoern A. Zeeb #ifdef INET
1083a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1084b287c6c7SBjoern A. Zeeb #endif
10854a91aa8fSMichael Tuexen 			/*
10864a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10874a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10884a91aa8fSMichael Tuexen 			 * operations fail.
10894a91aa8fSMichael Tuexen 			 */
10904a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10914a91aa8fSMichael Tuexen 				restoreflags = false;
10924a91aa8fSMichael Tuexen 
10937d2608a5SMark Johnston 			if (error) {
10947d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10957d2608a5SMark Johnston 				sbflush(&so->so_snd);
10962c37256eSGarrett Wollman 				goto out;
10977d2608a5SMark Johnston 			}
1098dd7b86e2SGleb Smirnoff 			if (tp->t_flags & TF_FASTOPEN)
1099c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
110018a75309SPatrick Kelsey 			else {
11012c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11022c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11032c37256eSGarrett Wollman 			}
1104c560df6fSPatrick Kelsey 		}
11052c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11062c37256eSGarrett Wollman 			/*
11072c37256eSGarrett Wollman 			 * Close the send side of the connection after
11082c37256eSGarrett Wollman 			 * the data is sent.
11092c37256eSGarrett Wollman 			 */
11102c37256eSGarrett Wollman 			socantsendmore(so);
1111623dce13SRobert Watson 			tcp_usrclosed(tp);
11122c37256eSGarrett Wollman 		}
1113e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1114e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1115e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1116e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1117e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1118e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1119e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1120e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1121e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1122e854dd38SRandall Stewart 		}
11232cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11242cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1125b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1126b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1127f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1128b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1129b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1130b0acefa8SBill Fenner 		}
11312c37256eSGarrett Wollman 	} else {
1132623dce13SRobert Watson 		/*
1133623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1134623dce13SRobert Watson 		 */
1135dded4e9eSRichard Scheffenegger 		SOCK_SENDBUF_LOCK(so);
11362c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1137dded4e9eSRichard Scheffenegger 			SOCK_SENDBUF_UNLOCK(so);
11382c37256eSGarrett Wollman 			error = ENOBUFS;
11392c37256eSGarrett Wollman 			goto out;
11402c37256eSGarrett Wollman 		}
11412c37256eSGarrett Wollman 		/*
11422c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11432c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11442c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11452c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11462c37256eSGarrett Wollman 		 * of data past the urgent section.
11472c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11482c37256eSGarrett Wollman 		 */
114908af8aacSRandall Stewart 		if (tp->t_acktime == 0)
115008af8aacSRandall Stewart 			tp->t_acktime = ticks;
1151651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1152dded4e9eSRichard Scheffenegger 		SOCK_SENDBUF_UNLOCK(so);
11537d2608a5SMark Johnston 		m = NULL;
1154ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1155ef53690bSGarrett Wollman 			/*
1156ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1157ef53690bSGarrett Wollman 			 * initialize window to default value, and
11580c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1159ef53690bSGarrett Wollman 			 */
116018a75309SPatrick Kelsey 
1161c560df6fSPatrick Kelsey 			/*
1162c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1163c560df6fSPatrick Kelsey 			 */
1164dd7b86e2SGleb Smirnoff 			if (tp->t_flags & TF_FASTOPEN)
1165c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1166fb59c426SYoshinobu Inoue #ifdef INET6
1167fb59c426SYoshinobu Inoue 			if (isipv6)
1168a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1169fb59c426SYoshinobu Inoue #endif /* INET6 */
1170b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1171b287c6c7SBjoern A. Zeeb 			else
1172b287c6c7SBjoern A. Zeeb #endif
1173b287c6c7SBjoern A. Zeeb #ifdef INET
1174a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1175b287c6c7SBjoern A. Zeeb #endif
11764a91aa8fSMichael Tuexen 			/*
11774a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11784a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11794a91aa8fSMichael Tuexen 			 * operations fail.
11804a91aa8fSMichael Tuexen 			 */
11814a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11824a91aa8fSMichael Tuexen 				restoreflags = false;
11834a91aa8fSMichael Tuexen 
11847d2608a5SMark Johnston 			if (error != 0) {
11857d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11867d2608a5SMark Johnston 				sbflush(&so->so_snd);
1187ef53690bSGarrett Wollman 				goto out;
11887d2608a5SMark Johnston 			}
1189ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1190ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1191623dce13SRobert Watson 		}
1192300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11937d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11942cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1195f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11962cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11972c37256eSGarrett Wollman 		}
11982cbcd3c1SGleb Smirnoff 	}
11992529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12002529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12012529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12022529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12032529f56eSJonathan T. Looney 	    0, NULL, false);
12047d2608a5SMark Johnston 
1205d1401c90SRobert Watson out:
12064a91aa8fSMichael Tuexen 	/*
12077d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
12087d2608a5SMark Johnston 	 * responsible for freeing memory.
12097d2608a5SMark Johnston 	 */
12107d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12117d2608a5SMark Johnston 		m_freem(m);
12127d2608a5SMark Johnston 
12137d2608a5SMark Johnston 	/*
12144a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12154a91aa8fSMichael Tuexen 	 * restore the original flags.
12164a91aa8fSMichael Tuexen 	 */
12174a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12184a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12194a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12204a91aa8fSMichael Tuexen 	}
122100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
122200812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
12235d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12245d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1225f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
122697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
122773fddedaSPeter Grehan 	return (error);
12282c37256eSGarrett Wollman }
12292c37256eSGarrett Wollman 
12302cbcd3c1SGleb Smirnoff static int
tcp_usr_ready(struct socket * so,struct mbuf * m,int count)12312cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12322cbcd3c1SGleb Smirnoff {
1233109eb549SGleb Smirnoff 	struct epoch_tracker et;
12342cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12352cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12362cbcd3c1SGleb Smirnoff 	int error;
12372cbcd3c1SGleb Smirnoff 
12382cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12392cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
124053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12412cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
124282334850SJohn Baldwin 		mb_free_notready(m, count);
12432cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12442cbcd3c1SGleb Smirnoff 	}
12452cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12462cbcd3c1SGleb Smirnoff 
1247dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_LOCK(so);
12482cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
1249dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_UNLOCK(so);
1250f64dc2abSGleb Smirnoff 	if (error) {
12512cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1252f64dc2abSGleb Smirnoff 		return (error);
1253f64dc2abSGleb Smirnoff 	}
1254f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1255f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1256f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12572cbcd3c1SGleb Smirnoff 
12582cbcd3c1SGleb Smirnoff 	return (error);
12592cbcd3c1SGleb Smirnoff }
12602cbcd3c1SGleb Smirnoff 
12612c37256eSGarrett Wollman /*
1262a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12632c37256eSGarrett Wollman  */
1264ac45e92fSRobert Watson static void
tcp_usr_abort(struct socket * so)12652c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12662c37256eSGarrett Wollman {
1267f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1268c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12696573d758SMatt Macy 	struct epoch_tracker et;
1270c78cbc7bSRobert Watson 
1271ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1272c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1273c78cbc7bSRobert Watson 
127497a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12758501a69cSRobert Watson 	INP_WLOCK(inp);
1276c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1277c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1278c78cbc7bSRobert Watson 
1279c78cbc7bSRobert Watson 	/*
1280a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1281c78cbc7bSRobert Watson 	 */
128253af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1283c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12848fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12858fa799bdSJonathan T. Looney 		if (tp == NULL)
12868fa799bdSJonathan T. Looney 			goto dropped;
128700812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12885d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1289c78cbc7bSRobert Watson 	}
1290ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1291d8596171SGleb Smirnoff 		soref(so);
1292ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1293a152f8a3SRobert Watson 	}
12948501a69cSRobert Watson 	INP_WUNLOCK(inp);
12958fa799bdSJonathan T. Looney dropped:
129697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1297a152f8a3SRobert Watson }
1298a152f8a3SRobert Watson 
1299a152f8a3SRobert Watson /*
1300a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1301a152f8a3SRobert Watson  */
1302a152f8a3SRobert Watson static void
tcp_usr_close(struct socket * so)1303a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1304a152f8a3SRobert Watson {
1305a152f8a3SRobert Watson 	struct inpcb *inp;
1306c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13076573d758SMatt Macy 	struct epoch_tracker et;
1308a152f8a3SRobert Watson 
1309a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1310a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1311a152f8a3SRobert Watson 
131297a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13138501a69cSRobert Watson 	INP_WLOCK(inp);
1314a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1315a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1316a152f8a3SRobert Watson 
1317a152f8a3SRobert Watson 	/*
1318cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1319a152f8a3SRobert Watson 	 * a disconnect.
1320a152f8a3SRobert Watson 	 */
132153af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1322a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1323cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
132474703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1325a152f8a3SRobert Watson 			tcp_disconnect(tp);
132600812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
13275d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1328a152f8a3SRobert Watson 		}
1329cda6bdbaSJohn Baldwin 	}
1330ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1331d8596171SGleb Smirnoff 		soref(so);
1332ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1333a152f8a3SRobert Watson 	}
13348501a69cSRobert Watson 	INP_WUNLOCK(inp);
133597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13362c37256eSGarrett Wollman }
13372c37256eSGarrett Wollman 
1338d3b6c96bSRandall Stewart static int
tcp_pru_options_support(struct tcpcb * tp,int flags)1339d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1340d3b6c96bSRandall Stewart {
1341d3b6c96bSRandall Stewart 	/*
1342d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1343d3b6c96bSRandall Stewart 	 * specified then it does not always support
1344d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1345d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1346d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1347d3b6c96bSRandall Stewart 	 */
1348d3b6c96bSRandall Stewart 	int ret = 0;
1349d3b6c96bSRandall Stewart 
1350d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1351d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1352d3b6c96bSRandall Stewart 	}
1353d3b6c96bSRandall Stewart 	return (ret);
1354d3b6c96bSRandall Stewart }
1355d3b6c96bSRandall Stewart 
13562c37256eSGarrett Wollman /*
13572c37256eSGarrett Wollman  * Receive out-of-band data.
13582c37256eSGarrett Wollman  */
13592c37256eSGarrett Wollman static int
tcp_usr_rcvoob(struct socket * so,struct mbuf * m,int flags)13602c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13612c37256eSGarrett Wollman {
13622c37256eSGarrett Wollman 	int error = 0;
1363f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1364c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13652c37256eSGarrett Wollman 
1366623dce13SRobert Watson 	inp = sotoinpcb(so);
1367623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13688501a69cSRobert Watson 	INP_WLOCK(inp);
136953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1370c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
1371c2399dd2SMichael Tuexen 		return (ECONNRESET);
1372623dce13SRobert Watson 	}
1373623dce13SRobert Watson 	tp = intotcpcb(inp);
1374c2399dd2SMichael Tuexen 
1375d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1376d3b6c96bSRandall Stewart 	if (error) {
1377d3b6c96bSRandall Stewart 		goto out;
1378d3b6c96bSRandall Stewart 	}
13792c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1380c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13814cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13824cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13832c37256eSGarrett Wollman 		error = EINVAL;
13842c37256eSGarrett Wollman 		goto out;
13852c37256eSGarrett Wollman 	}
13862c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13872c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13882c37256eSGarrett Wollman 		goto out;
13892c37256eSGarrett Wollman 	}
13902c37256eSGarrett Wollman 	m->m_len = 1;
13912c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13922c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13932c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1394623dce13SRobert Watson 
1395623dce13SRobert Watson out:
139600812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
13975d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13988501a69cSRobert Watson 	INP_WUNLOCK(inp);
1399623dce13SRobert Watson 	return (error);
14002c37256eSGarrett Wollman }
14012c37256eSGarrett Wollman 
1402b287c6c7SBjoern A. Zeeb #ifdef INET
1403e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1404e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1405e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1406e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1407e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1408e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1409e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1410e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1411e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1412e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1413e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1414e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1415e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1416e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1417e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1418e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1419e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1420e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1421e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1422e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1423e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1424e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1425e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1426e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14272c37256eSGarrett Wollman };
1428b287c6c7SBjoern A. Zeeb #endif /* INET */
1429df8bae1dSRodney W. Grimes 
1430fb59c426SYoshinobu Inoue #ifdef INET6
1431e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1432e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1433e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1434e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1435e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1436e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1437e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1438e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1439e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1440e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1441e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1442e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1443e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1444e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1445e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1446e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1447e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1448e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1449e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1450e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1451e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1452e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1453e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1454e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1455fb59c426SYoshinobu Inoue };
1456fb59c426SYoshinobu Inoue #endif /* INET6 */
1457fb59c426SYoshinobu Inoue 
1458b287c6c7SBjoern A. Zeeb #ifdef INET
1459a0292f23SGarrett Wollman /*
1460a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1461dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1462dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1463dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1464a0292f23SGarrett Wollman  */
14650312fbe9SPoul-Henning Kamp static int
tcp_connect(struct tcpcb * tp,struct sockaddr_in * sin,struct thread * td)1466a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1467a0292f23SGarrett Wollman {
14689e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14699eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1470c3229e05SDavid Greenman 	int error;
1471a0292f23SGarrett Wollman 
1472c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14738501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
147476f1499fSGleb Smirnoff 
1475636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1476de0a2eb2SMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1477de0a2eb2SMark Johnston 	    SS_ISDISCONNECTED)) != 0))
1478636b19eaSMark Johnston 		return (EISCONN);
1479636b19eaSMark Johnston 
1480fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
14810b4539eeSGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred);
14829e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1483dfc4d218SGleb Smirnoff 	if (error != 0)
14849e46ff4dSGleb Smirnoff 		return (error);
1485a0292f23SGarrett Wollman 
1486087b55eaSAndre Oppermann 	/*
1487087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1488087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1489087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1490087b55eaSAndre Oppermann 	 */
1491a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14929b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1493a0292f23SGarrett Wollman 		tp->request_r_scale++;
1494a0292f23SGarrett Wollman 
1495a0292f23SGarrett Wollman 	soisconnecting(so);
149678b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
149757f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14988e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14998e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15008e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1501a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1502a45d2726SAndras Olah 
15039e46ff4dSGleb Smirnoff 	return (0);
1504a0292f23SGarrett Wollman }
1505b287c6c7SBjoern A. Zeeb #endif /* INET */
1506a0292f23SGarrett Wollman 
1507fb59c426SYoshinobu Inoue #ifdef INET6
1508fb59c426SYoshinobu Inoue static int
tcp6_connect(struct tcpcb * tp,struct sockaddr_in6 * sin6,struct thread * td)1509a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1510fb59c426SYoshinobu Inoue {
15119eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1512636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1513fb59c426SYoshinobu Inoue 	int error;
1514fb59c426SYoshinobu Inoue 
1515775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
15168501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1517623dce13SRobert Watson 
1518636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1519636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1520636b19eaSMark Johnston 		return (EISCONN);
1521636b19eaSMark Johnston 
152276f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1523a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1524fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
152576f1499fSGleb Smirnoff 	if (error != 0)
152676f1499fSGleb Smirnoff 		return (error);
1527fb59c426SYoshinobu Inoue 
1528fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1529fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1530970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1531fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1532fb59c426SYoshinobu Inoue 
1533636b19eaSMark Johnston 	soisconnecting(so);
153478b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
153557f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15368e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15378e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15388e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1539fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1540fb59c426SYoshinobu Inoue 
154176f1499fSGleb Smirnoff 	return (0);
1542fb59c426SYoshinobu Inoue }
1543fb59c426SYoshinobu Inoue #endif /* INET6 */
1544fb59c426SYoshinobu Inoue 
1545cfe8b629SGarrett Wollman /*
1546b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1547b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1548b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1549b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1550b8af5dfaSRobert Watson  * from Linux.
1551b8af5dfaSRobert Watson  */
15528c6104c4SMarius Strobl void
tcp_fill_info(const struct tcpcb * tp,struct tcp_info * ti)15538c6104c4SMarius Strobl tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1554b8af5dfaSRobert Watson {
1555b8af5dfaSRobert Watson 
15568c6104c4SMarius Strobl 	INP_LOCK_ASSERT(tptoinpcb(tp));
1557b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1558b8af5dfaSRobert Watson 
1559b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1560b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1561b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15623529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1563b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1564b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1565b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1566b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1567b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1568b8af5dfaSRobert Watson 	}
156904682968SRichard Scheffenegger 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
157004682968SRichard Scheffenegger 		case TF2_ECN_PERMIT:
15715a17b6adSMichael Tuexen 			ti->tcpi_options |= TCPI_OPT_ECN;
157204682968SRichard Scheffenegger 			break;
157304682968SRichard Scheffenegger 		case TF2_ACE_PERMIT:
157404682968SRichard Scheffenegger 			/* FALLTHROUGH */
157504682968SRichard Scheffenegger 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
157604682968SRichard Scheffenegger 			ti->tcpi_options |= TCPI_OPT_ACE;
157704682968SRichard Scheffenegger 			break;
157804682968SRichard Scheffenegger 		default:
157904682968SRichard Scheffenegger 			break;
158004682968SRichard Scheffenegger 	}
1581dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
158204682968SRichard Scheffenegger 		ti->tcpi_options |= TCPI_OPT_TFO;
15831baaf834SBruce M Simpson 
158443d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15853ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15861baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15871baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15881baaf834SBruce M Simpson 
1589b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1590b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1591b8af5dfaSRobert Watson 
1592b8af5dfaSRobert Watson 	/*
1593b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1594b8af5dfaSRobert Watson 	 */
1595c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1596535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1597b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15981c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1599535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
160043d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
160143d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1602f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1603f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1604f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1605dc485b96SMarius Strobl 	ti->tcpi_snd_una = tp->snd_una;
1606dc485b96SMarius Strobl 	ti->tcpi_snd_max = tp->snd_max;
1607dc485b96SMarius Strobl 	ti->tcpi_rcv_numsacks = tp->rcv_numsacks;
1608dc485b96SMarius Strobl 	ti->tcpi_rcv_adv = tp->rcv_adv;
1609dc485b96SMarius Strobl 	ti->tcpi_dupacks = tp->t_dupacks;
1610e18b97bdSRandall Stewart 	ti->tcpi_rttmin = tp->t_rttlow;
1611a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1612a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1613a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1614a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1615a6456410SNavdeep Parhar 	}
1616a6456410SNavdeep Parhar #endif
161722c81cc5SRichard Scheffenegger 	/*
161822c81cc5SRichard Scheffenegger 	 * AccECN related counters.
161922c81cc5SRichard Scheffenegger 	 */
162022c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
162122c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
162222c81cc5SRichard Scheffenegger 		/*
162322c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
162422c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
162522c81cc5SRichard Scheffenegger 		 */
162622c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
162722c81cc5SRichard Scheffenegger 	else
162822c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
162922c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1630b8af5dfaSRobert Watson }
1631b8af5dfaSRobert Watson 
1632b8af5dfaSRobert Watson /*
16331e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16341e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16351e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16361e8f5ffaSRobert Watson  * option.
1637cfe8b629SGarrett Wollman  */
1638bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16398501a69cSRobert Watson 	INP_WLOCK(inp);							\
164053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16418501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1642bac5bedfSConrad Meyer 		cleanup;						\
16431e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16441e8f5ffaSRobert Watson 	}								\
16451e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16461e8f5ffaSRobert Watson } while(0)
1647bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16481e8f5ffaSRobert Watson 
1649fd7daa72SMichael Tuexen int
tcp_ctloutput_set(struct inpcb * inp,struct sockopt * sopt)1650fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1651df8bae1dSRodney W. Grimes {
1652fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1653fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1654fc4d53ccSGleb Smirnoff 	int error = 0;
1655df8bae1dSRodney W. Grimes 
1656fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16573b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
165853af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1659fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1660fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1661fc4d53ccSGleb Smirnoff 
1662cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16633b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1664fb59c426SYoshinobu Inoue #ifdef INET6
1665de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1666fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1667de156263SGleb Smirnoff #endif
1668de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1669de156263SGleb Smirnoff 		else
1670de156263SGleb Smirnoff #endif
1671de156263SGleb Smirnoff #ifdef INET
1672fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1673de156263SGleb Smirnoff #endif
16745dff1c38SMichael Tuexen 		/*
1675de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1676de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1677de156263SGleb Smirnoff 		 * IP level returned.
16785dff1c38SMichael Tuexen 		 */
1679de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1680de156263SGleb Smirnoff #ifdef INET6
1681de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1682de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1683de156263SGleb Smirnoff 				return (error);
1684de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1685de156263SGleb Smirnoff 			case IPV6_TCLASS:
1686de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1687de156263SGleb Smirnoff 				break;
1688de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1689f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1690f581a26eSGleb Smirnoff 				break;
1691de156263SGleb Smirnoff 			default:
1692de156263SGleb Smirnoff 				return (error);
16935dff1c38SMichael Tuexen 			}
1694de156263SGleb Smirnoff 			break;
1695b287c6c7SBjoern A. Zeeb #endif
1696b287c6c7SBjoern A. Zeeb #ifdef INET
1697de156263SGleb Smirnoff 		case IPPROTO_IP:
1698de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1699de156263SGleb Smirnoff 			case IP_TOS:
17003b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
17013b0ee680SRichard Scheffenegger 				break;
1702de156263SGleb Smirnoff 			case IP_TTL:
1703de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1704de156263SGleb Smirnoff 				break;
1705de156263SGleb Smirnoff 			default:
1706df8bae1dSRodney W. Grimes 				return (error);
1707de156263SGleb Smirnoff 			}
1708de156263SGleb Smirnoff 			break;
1709de156263SGleb Smirnoff #endif
1710de156263SGleb Smirnoff 		default:
1711de156263SGleb Smirnoff 			return (error);
1712de156263SGleb Smirnoff 		}
1713fe136aecSMichael Tuexen 		INP_WLOCK_RECHECK(inp);
1714fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1715fc4d53ccSGleb Smirnoff 		/*
1716fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1717fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1718fc4d53ccSGleb Smirnoff 		 */
1719fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1720fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
172173ee5756SRandall Stewart 		void *ptr = NULL;
1722fc4d53ccSGleb Smirnoff 
17233b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1724fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1725fc4d53ccSGleb Smirnoff 		if (error)
1726fc4d53ccSGleb Smirnoff 			return (error);
1727fc4d53ccSGleb Smirnoff 
1728fe136aecSMichael Tuexen 		INP_WLOCK_RECHECK(inp);
1729fc4d53ccSGleb Smirnoff 
173055bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
173155bceb1eSRandall Stewart 		if (blk == NULL) {
173255bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
173355bceb1eSRandall Stewart 			return (ENOENT);
173455bceb1eSRandall Stewart 		}
1735587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1736587d67c0SRandall Stewart 			/* You already have this */
1737587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1738587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1739587d67c0SRandall Stewart 			return (0);
1740587d67c0SRandall Stewart 		}
174155bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
174255bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
174355bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
174455bceb1eSRandall Stewart 			return (ENOENT);
174555bceb1eSRandall Stewart 		}
174686c9325dSMichael Tuexen 		error = (*blk->tfb_tcp_handoff_ok)(tp);
174786c9325dSMichael Tuexen 		if (error) {
174886c9325dSMichael Tuexen 			refcount_release(&blk->tfb_refcnt);
174986c9325dSMichael Tuexen 			INP_WUNLOCK(inp);
175086c9325dSMichael Tuexen 			return (error);
175186c9325dSMichael Tuexen 		}
175255bceb1eSRandall Stewart 		/*
175373ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
175473ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
175555bceb1eSRandall Stewart 		 */
1756d2ef52efSGleb Smirnoff 		if (tp->t_fb->tfb_tcp_timer_stop_all != NULL)
1757d2ef52efSGleb Smirnoff 			tp->t_fb->tfb_tcp_timer_stop_all(tp);
175873ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
175973ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
176073ee5756SRandall Stewart 			if (error) {
176173ee5756SRandall Stewart 				/*
176273ee5756SRandall Stewart 				 * Release the ref count the lookup
176373ee5756SRandall Stewart 				 * acquired.
176473ee5756SRandall Stewart 				 */
176573ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
176673ee5756SRandall Stewart 				/*
176773ee5756SRandall Stewart 				 * Now there is a chance that the
176873ee5756SRandall Stewart 				 * init() function mucked with some
176973ee5756SRandall Stewart 				 * things before it failed, such as
177073ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
177173ee5756SRandall Stewart 				 * It should not of, but lets give the old
177273ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
177373ee5756SRandall Stewart 				 */
177473ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
177573ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
177673ee5756SRandall Stewart 				}
177773ee5756SRandall Stewart 			 	goto err_out;
177873ee5756SRandall Stewart 			}
177973ee5756SRandall Stewart 		}
1780587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1781086a3556SAndrew Gallatin 			struct epoch_tracker et;
1782587d67c0SRandall Stewart 			/*
1783587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1784587d67c0SRandall Stewart 			 * the tcb is not going away.
1785587d67c0SRandall Stewart 			 */
1786086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1787587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1788086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1789587d67c0SRandall Stewart 		}
179073ee5756SRandall Stewart 		/*
179173ee5756SRandall Stewart 		 * Release the old refcnt, the
179273ee5756SRandall Stewart 		 * lookup acquired a ref on the
179373ee5756SRandall Stewart 		 * new one already.
179473ee5756SRandall Stewart 		 */
179555bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
179673ee5756SRandall Stewart 		/*
179773ee5756SRandall Stewart 		 * Set in the new stack.
179873ee5756SRandall Stewart 		 */
179955bceb1eSRandall Stewart 		tp->t_fb = blk;
180073ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
180155bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
180255bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
180355bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
180455bceb1eSRandall Stewart 			     sopt->sopt_name);
180555bceb1eSRandall Stewart 		}
180655bceb1eSRandall Stewart #endif
18073ee9c3c4SRandall Stewart err_out:
180855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
180955bceb1eSRandall Stewart 		return (error);
181073ee5756SRandall Stewart 
1811fc4d53ccSGleb Smirnoff 	}
1812fc4d53ccSGleb Smirnoff 
18133b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
181466fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1815fc4d53ccSGleb Smirnoff }
1816fc4d53ccSGleb Smirnoff 
1817fc4d53ccSGleb Smirnoff static int
tcp_ctloutput_get(struct inpcb * inp,struct sockopt * sopt)1818fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1819fc4d53ccSGleb Smirnoff {
1820fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1821fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1822fc4d53ccSGleb Smirnoff 	int error = 0;
1823fc4d53ccSGleb Smirnoff 
1824fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18253b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
182653af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1827fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1828fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1829fc4d53ccSGleb Smirnoff 
1830fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18313b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1832fc4d53ccSGleb Smirnoff #ifdef INET6
1833fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1834fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1835fc4d53ccSGleb Smirnoff #endif /* INET6 */
1836fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1837fc4d53ccSGleb Smirnoff 		else
1838fc4d53ccSGleb Smirnoff #endif
1839fc4d53ccSGleb Smirnoff #ifdef INET
1840fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1841fc4d53ccSGleb Smirnoff #endif
1842fc4d53ccSGleb Smirnoff 		return (error);
1843fc4d53ccSGleb Smirnoff 	}
1844fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1845e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1846fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1847fc4d53ccSGleb Smirnoff 
1848e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1849e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1850e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1851e2833083SPeter Lei 		} else {
1852e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1853e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1854c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1855c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1856e2833083SPeter Lei 		}
185755bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
185855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
185955bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
186055bceb1eSRandall Stewart 		return (error);
186155bceb1eSRandall Stewart 	}
1862fc4d53ccSGleb Smirnoff 
18633b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
186466fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1865fc4d53ccSGleb Smirnoff }
1866fc4d53ccSGleb Smirnoff 
1867fc4d53ccSGleb Smirnoff int
tcp_ctloutput(struct socket * so,struct sockopt * sopt)1868fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1869fc4d53ccSGleb Smirnoff {
1870fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1871fc4d53ccSGleb Smirnoff 
1872fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1873fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1874fc4d53ccSGleb Smirnoff 
18753b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
187653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18773b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18783b3c08c1SMichael Tuexen 		return (ECONNRESET);
18793b3c08c1SMichael Tuexen 	}
1880fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1881fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1882fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1883fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1884fc4d53ccSGleb Smirnoff 	else
1885fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
188655bceb1eSRandall Stewart }
188755bceb1eSRandall Stewart 
18882529f56eSJonathan T. Looney /*
18892529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18902529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18912529f56eSJonathan T. Looney  */
18922529f56eSJonathan T. Looney #ifdef CTASSERT
18932529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18942529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18952529f56eSJonathan T. Looney #endif
18962529f56eSJonathan T. Looney 
1897b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1898b8d60729SRandall Stewart 
1899b8d60729SRandall Stewart static int
tcp_set_cc_mod(struct inpcb * inp,struct sockopt * sopt)1900ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1901b8d60729SRandall Stewart {
1902b8d60729SRandall Stewart 	struct cc_algo *algo;
1903b8d60729SRandall Stewart 	void *ptr = NULL;
19043b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1905b8d60729SRandall Stewart 	struct cc_var cc_mem;
1906b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1907b8d60729SRandall Stewart 	size_t mem_sz;
1908b8d60729SRandall Stewart 	int error;
1909b8d60729SRandall Stewart 
1910b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1911b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1912b8d60729SRandall Stewart 	if (error)
1913b8d60729SRandall Stewart 		return(error);
1914b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1915b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1916ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1917b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1918b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1919b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1920b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1921b8d60729SRandall Stewart 				continue;
1922b8d60729SRandall Stewart 			}
1923b8d60729SRandall Stewart 			break;
1924b8d60729SRandall Stewart 		}
1925ea9017fbSRandall Stewart 	}
1926b8d60729SRandall Stewart 	if (algo == NULL) {
1927b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1928b8d60729SRandall Stewart 		return(ESRCH);
1929b8d60729SRandall Stewart 	}
1930ea9017fbSRandall Stewart 	/*
1931ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1932ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1933ea9017fbSRandall Stewart 	 */
1934ea9017fbSRandall Stewart 	cc_refer(algo);
1935ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1936b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1937b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1938b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1939b8d60729SRandall Stewart 		if (mem_sz == 0) {
1940b8d60729SRandall Stewart 			goto no_mem_needed;
1941b8d60729SRandall Stewart 		}
1942b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1943b8d60729SRandall Stewart 	} else {
1944b8d60729SRandall Stewart no_mem_needed:
1945b8d60729SRandall Stewart 		mem_sz = 0;
1946b8d60729SRandall Stewart 		ptr = NULL;
1947b8d60729SRandall Stewart 	}
1948b8d60729SRandall Stewart 	/*
1949b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1950b8d60729SRandall Stewart 	 * back the inplock.
1951b8d60729SRandall Stewart 	 */
1952b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1953df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
195453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1955df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1956ea9017fbSRandall Stewart 		if (ptr)
1957df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1958ea9017fbSRandall Stewart 		/* Release our temp reference */
1959ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1960ea9017fbSRandall Stewart 		cc_release(algo);
1961ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
1962df07bfdaSMichael Tuexen 		return (ECONNRESET);
1963df07bfdaSMichael Tuexen 	}
1964df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
1965df07bfdaSMichael Tuexen 	if (ptr != NULL)
1966b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
196700d3b744SMichael Tuexen 	cc_mem.tp = tp;
1968b8d60729SRandall Stewart 	/*
1969b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
1970b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
1971b8d60729SRandall Stewart 	 * Note here we init into stack memory.
1972b8d60729SRandall Stewart 	 */
1973b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
1974b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
1975b8d60729SRandall Stewart 	else
1976b8d60729SRandall Stewart 		error = 0;
1977b8d60729SRandall Stewart 	/*
1978b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
1979b8d60729SRandall Stewart 	 * should not fail we could in theory have a
1980b8d60729SRandall Stewart 	 * KASSERT here.
1981b8d60729SRandall Stewart 	 */
1982b8d60729SRandall Stewart 	if (error == 0) {
1983b8d60729SRandall Stewart 		/*
1984b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
1985b8d60729SRandall Stewart 		 * connection to the new CC module by
1986b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
1987b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
1988b8d60729SRandall Stewart 		 */
1989b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
1990e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1991ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
1992ea9017fbSRandall Stewart 		cc_detach(tp);
1993ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
1994e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1995ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
1996ea9017fbSRandall Stewart 		cc_attach(tp, algo);
1997b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
1998b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1999b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2000e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2001b8d60729SRandall Stewart 		}
2002b8d60729SRandall Stewart 	} else if (ptr)
2003b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2004b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2005ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2006ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2007ea9017fbSRandall Stewart 	cc_release(algo);
2008ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2009b8d60729SRandall Stewart 	return (error);
2010b8d60729SRandall Stewart }
2011b8d60729SRandall Stewart 
201255bceb1eSRandall Stewart int
tcp_default_ctloutput(struct tcpcb * tp,struct sockopt * sopt)201366fbc19fSGleb Smirnoff tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
201455bceb1eSRandall Stewart {
201566fbc19fSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
201655bceb1eSRandall Stewart 	int	error, opt, optval;
201755bceb1eSRandall Stewart 	u_int	ui;
201855bceb1eSRandall Stewart 	struct	tcp_info ti;
2019b2e60773SJohn Baldwin #ifdef KERN_TLS
2020b2e60773SJohn Baldwin 	struct tls_enable tls;
2021528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2022b2e60773SJohn Baldwin #endif
20232529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2024adc56f5aSEdward Tomasz Napierala #ifdef STATS
2025adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2026adc56f5aSEdward Tomasz Napierala #endif
2027af6fef3aSGleb Smirnoff 	size_t	len;
2028df8bae1dSRodney W. Grimes 
2029f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
203053af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2031fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2032528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2033f581a26eSGleb Smirnoff 
2034f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2035f581a26eSGleb Smirnoff #ifdef INET6
2036f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2037f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2038f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2039f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2040f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2041f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2042f581a26eSGleb Smirnoff 		}
2043f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2044f581a26eSGleb Smirnoff 		return (0);
2045f581a26eSGleb Smirnoff #endif
2046f581a26eSGleb Smirnoff #ifdef INET
2047f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2048f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2049f581a26eSGleb Smirnoff 		return (0);
2050f581a26eSGleb Smirnoff #endif
2051f581a26eSGleb Smirnoff 	}
2052f581a26eSGleb Smirnoff 
2053d519cedbSGleb Smirnoff 	/*
2054d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2055d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2056d519cedbSGleb Smirnoff 	 */
2057d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2058d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2059d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2060c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2061c8b53cedSMichael Tuexen 			return (EINVAL);
2062af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2063af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2064d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2065d519cedbSGleb Smirnoff 		if (error) {
2066af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2067d519cedbSGleb Smirnoff 			return (error);
2068d519cedbSGleb Smirnoff 		}
2069bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2070d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2071e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2072d519cedbSGleb Smirnoff 		else
2073d519cedbSGleb Smirnoff 			error = ENOENT;
2074d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2075d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2076af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2077af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2078d519cedbSGleb Smirnoff 		return (error);
2079d519cedbSGleb Smirnoff 	}
2080d519cedbSGleb Smirnoff 
2081cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2082cfe8b629SGarrett Wollman 	case SOPT_SET:
2083cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2084fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
208588f6b043SBruce M Simpson 		case TCP_MD5SIG:
20868501a69cSRobert Watson 			INP_WUNLOCK(inp);
208797453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2088fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2089fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
20901cfd4b53SBruce M Simpson 			if (error)
20911e8f5ffaSRobert Watson 				return (error);
209297453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
209309fe6320SNavdeep Parhar 			goto unlock_and_done;
2094fcf59617SAndrey V. Elsukov #endif /* IPSEC */
209509fe6320SNavdeep Parhar 
2096df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2097cfe8b629SGarrett Wollman 		case TCP_NOOPT:
20988501a69cSRobert Watson 			INP_WUNLOCK(inp);
2099cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2100cfe8b629SGarrett Wollman 			    sizeof optval);
2101cfe8b629SGarrett Wollman 			if (error)
21021e8f5ffaSRobert Watson 				return (error);
2103cfe8b629SGarrett Wollman 
21048501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2105cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2106cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2107cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2108cfe8b629SGarrett Wollman 				break;
2109cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2110cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2111cfe8b629SGarrett Wollman 				break;
2112cfe8b629SGarrett Wollman 			default:
2113cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2114cfe8b629SGarrett Wollman 				break;
2115cfe8b629SGarrett Wollman 			}
2116cfe8b629SGarrett Wollman 
2117cfe8b629SGarrett Wollman 			if (optval)
2118cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2119df8bae1dSRodney W. Grimes 			else
2120cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
212109fe6320SNavdeep Parhar unlock_and_done:
212209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
212309fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
212409fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
212509fe6320SNavdeep Parhar 				    sopt->sopt_name);
212609fe6320SNavdeep Parhar 			}
212709fe6320SNavdeep Parhar #endif
21288501a69cSRobert Watson 			INP_WUNLOCK(inp);
2129df8bae1dSRodney W. Grimes 			break;
2130df8bae1dSRodney W. Grimes 
2131007581c0SJonathan Lemon 		case TCP_NOPUSH:
21328501a69cSRobert Watson 			INP_WUNLOCK(inp);
2133007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2134007581c0SJonathan Lemon 			    sizeof optval);
2135007581c0SJonathan Lemon 			if (error)
21361e8f5ffaSRobert Watson 				return (error);
2137007581c0SJonathan Lemon 
21388501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2139007581c0SJonathan Lemon 			if (optval)
2140007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2141d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2142007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2143109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2144109eb549SGleb Smirnoff 					struct epoch_tracker et;
2145109eb549SGleb Smirnoff 
2146109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2147f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2148109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2149109eb549SGleb Smirnoff 				}
2150007581c0SJonathan Lemon 			}
215109fe6320SNavdeep Parhar 			goto unlock_and_done;
2152007581c0SJonathan Lemon 
21539e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21549e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21559e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21569e644c23SMichael Tuexen 			    sizeof optval);
21579e644c23SMichael Tuexen 			if (error)
21589e644c23SMichael Tuexen 				return (error);
21599e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
21609e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
21619e644c23SMichael Tuexen 				/* Its got to be in range */
21629e644c23SMichael Tuexen 				return (EINVAL);
21639e644c23SMichael Tuexen 			}
21649e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
21659e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
21669e644c23SMichael Tuexen 				return (EINVAL);
21679e644c23SMichael Tuexen 			}
21689e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
21699e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
21709e644c23SMichael Tuexen 				/* You can't change after you are connected */
21719e644c23SMichael Tuexen 				error = EINVAL;
21729e644c23SMichael Tuexen 			} else {
21739e644c23SMichael Tuexen 				/* Ok we are all good set the port */
21749e644c23SMichael Tuexen 				tp->t_port = htons(optval);
21759e644c23SMichael Tuexen 			}
21769e644c23SMichael Tuexen 			goto unlock_and_done;
21779e644c23SMichael Tuexen 
2178df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
21798501a69cSRobert Watson 			INP_WUNLOCK(inp);
2180cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2181cfe8b629SGarrett Wollman 			    sizeof optval);
2182cfe8b629SGarrett Wollman 			if (error)
21831e8f5ffaSRobert Watson 				return (error);
2184df8bae1dSRodney W. Grimes 
21858501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
218653369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2187fce03f85SRandall Stewart 			    optval + 40 >= V_tcp_minmss) {
2188cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2189fce03f85SRandall Stewart 				if (tp->t_maxseg < V_tcp_mssdflt) {
2190fce03f85SRandall Stewart 					/*
2191fce03f85SRandall Stewart 					 * The MSS is so small we should not process incoming
2192fce03f85SRandall Stewart 					 * SACK's since we are subject to attack in such a
2193fce03f85SRandall Stewart 					 * case.
2194fce03f85SRandall Stewart 					 */
2195fce03f85SRandall Stewart 					tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
2196fce03f85SRandall Stewart 				} else {
2197fce03f85SRandall Stewart 					tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
2198fce03f85SRandall Stewart 				}
2199fce03f85SRandall Stewart 			} else
2200a0292f23SGarrett Wollman 				error = EINVAL;
220109fe6320SNavdeep Parhar 			goto unlock_and_done;
2202a0292f23SGarrett Wollman 
2203b8af5dfaSRobert Watson 		case TCP_INFO:
22048501a69cSRobert Watson 			INP_WUNLOCK(inp);
2205b8af5dfaSRobert Watson 			error = EINVAL;
2206b8af5dfaSRobert Watson 			break;
2207b8af5dfaSRobert Watson 
2208adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2209adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2210adc56f5aSEdward Tomasz Napierala #ifdef STATS
2211adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2212adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2213adc56f5aSEdward Tomasz Napierala 			if (error)
2214adc56f5aSEdward Tomasz Napierala 				return (error);
2215adc56f5aSEdward Tomasz Napierala 
2216adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2217adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2218adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2219adc56f5aSEdward Tomasz Napierala 			else
2220adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2221adc56f5aSEdward Tomasz Napierala 
2222adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2223adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2224adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2225adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2226adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2227adc56f5aSEdward Tomasz Napierala 				sbp = t;
2228adc56f5aSEdward Tomasz Napierala 			}
2229adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2230adc56f5aSEdward Tomasz Napierala 
2231adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2232adc56f5aSEdward Tomasz Napierala #else
2233adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2234adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2235adc56f5aSEdward Tomasz Napierala 			break;
2236adc56f5aSEdward Tomasz Napierala 
2237dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2238ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
223973e263b1SGleb Smirnoff 			break;
2240dbc42409SLawrence Stewart 
2241a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2242a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2243a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2244a034518aSAndrew Gallatin 			    sizeof(optval));
2245a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2246a034518aSAndrew Gallatin 			if (!error)
2247a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2248a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2249a034518aSAndrew Gallatin 			break;
2250a034518aSAndrew Gallatin 
2251b2e60773SJohn Baldwin #ifdef KERN_TLS
2252b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2253b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
225485df11a1SRichard Scheffenegger 			error = ktls_copyin_tls_enable(sopt, &tls);
225585df11a1SRichard Scheffenegger 			if (error != 0)
2256b2e60773SJohn Baldwin 				break;
2257fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
225885df11a1SRichard Scheffenegger 			ktls_cleanup_tls_enable(&tls);
2259b2e60773SJohn Baldwin 			break;
2260b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2261b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2262b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
226385df11a1SRichard Scheffenegger 			if (error != 0)
2264b2e60773SJohn Baldwin 				return (error);
2265b2e60773SJohn Baldwin 
2266b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2267fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2268b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2269b2e60773SJohn Baldwin 			break;
2270f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2271f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
227285df11a1SRichard Scheffenegger 			error = ktls_copyin_tls_enable(sopt, &tls);
227385df11a1SRichard Scheffenegger 			if (error != 0)
2274f1f93475SJohn Baldwin 				break;
2275fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
227685df11a1SRichard Scheffenegger 			ktls_cleanup_tls_enable(&tls);
2277f1f93475SJohn Baldwin 			break;
2278b2e60773SJohn Baldwin #endif
227908af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
22809077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
22819077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
22829077f387SGleb Smirnoff 		case TCP_KEEPINIT:
22839077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
22849077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
22859077f387SGleb Smirnoff 			if (error)
22869077f387SGleb Smirnoff 				return (error);
22879077f387SGleb Smirnoff 
22889077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
22899077f387SGleb Smirnoff 				error = EINVAL;
22909077f387SGleb Smirnoff 				break;
22919077f387SGleb Smirnoff 			}
22929077f387SGleb Smirnoff 			ui *= hz;
22939077f387SGleb Smirnoff 
22949077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
22959077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
229608af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
229708af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
229808af8aacSRandall Stewart 				break;
229908af8aacSRandall Stewart 
23009077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23019077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23029077f387SGleb Smirnoff 				/*
23039077f387SGleb Smirnoff 				 * XXX: better check current remaining
23049077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
23059077f387SGleb Smirnoff 				 */
23069077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
23079077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
23089077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23099077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23109077f387SGleb Smirnoff 				break;
23119077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23129077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23139077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23149077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23159077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23169077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23179077f387SGleb Smirnoff 				break;
23189077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23199077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23209077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23219077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23229077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23239077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23249077f387SGleb Smirnoff 				break;
23259077f387SGleb Smirnoff 			}
232609fe6320SNavdeep Parhar 			goto unlock_and_done;
23279077f387SGleb Smirnoff 
232885c05144SGleb Smirnoff 		case TCP_KEEPCNT:
232985c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
233085c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
233185c05144SGleb Smirnoff 			if (error)
233285c05144SGleb Smirnoff 				return (error);
233385c05144SGleb Smirnoff 
233485c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
233585c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
233685c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
233785c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
233885c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
233985c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
234085c05144SGleb Smirnoff 			goto unlock_and_done;
234185c05144SGleb Smirnoff 
2342c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2343c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2344c560df6fSPatrick Kelsey 
2345281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2346c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2347c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2348281a0fd4SPatrick Kelsey 				return (EPERM);
2349281a0fd4SPatrick Kelsey 
2350c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2351c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2352281a0fd4SPatrick Kelsey 			if (error)
2353281a0fd4SPatrick Kelsey 				return (error);
2354281a0fd4SPatrick Kelsey 
2355281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2356d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2357d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2358d442a657SMichael Tuexen 				error = EINVAL;
2359d442a657SMichael Tuexen 				goto unlock_and_done;
2360d442a657SMichael Tuexen 			}
2361c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2362c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2363c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2364c560df6fSPatrick Kelsey 						error = EPERM;
2365c560df6fSPatrick Kelsey 						goto unlock_and_done;
2366c560df6fSPatrick Kelsey 					}
2367c560df6fSPatrick Kelsey 
2368c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2369281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2370281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2371c560df6fSPatrick Kelsey 				} else {
2372c560df6fSPatrick Kelsey 					/*
2373c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2374c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2375c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2376c560df6fSPatrick Kelsey 					 * connect.
2377c560df6fSPatrick Kelsey 					 */
2378c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2379c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2380c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2381c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2382c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2383c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2384c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2385c560df6fSPatrick Kelsey 					}
2386c560df6fSPatrick Kelsey 				}
2387d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2388281a0fd4SPatrick Kelsey 			} else
2389281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2390281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2391c560df6fSPatrick Kelsey 		}
2392281a0fd4SPatrick Kelsey 
2393e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
23942529f56eSJonathan T. Looney 		case TCP_LOG:
23952529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23962529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
23972529f56eSJonathan T. Looney 			    sizeof optval);
23982529f56eSJonathan T. Looney 			if (error)
23992529f56eSJonathan T. Looney 				return (error);
24002529f56eSJonathan T. Looney 
24012529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24022529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24032529f56eSJonathan T. Looney 			goto unlock_and_done;
24042529f56eSJonathan T. Looney 
24052529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24062529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24072529f56eSJonathan T. Looney 			error = EINVAL;
24082529f56eSJonathan T. Looney 			break;
24092529f56eSJonathan T. Looney 
24102529f56eSJonathan T. Looney 		case TCP_LOGID:
24112529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24122529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24132529f56eSJonathan T. Looney 			if (error)
24142529f56eSJonathan T. Looney 				break;
24152529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24162529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24172529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24182529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24192529f56eSJonathan T. Looney 			break;
24202529f56eSJonathan T. Looney 
24212529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24222529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24232529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24242529f56eSJonathan T. Looney 			error =
24252529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
24262529f56eSJonathan T. Looney 			if (error)
24272529f56eSJonathan T. Looney 				break;
24282529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24292529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24302529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
24312529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24322529f56eSJonathan T. Looney 				    M_WAITOK, true);
24332529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24342529f56eSJonathan T. Looney 			} else {
24352529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24362529f56eSJonathan T. Looney 				/*
24372529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24382529f56eSJonathan T. Looney 				 * INP lock.
24392529f56eSJonathan T. Looney 				 */
24402529f56eSJonathan T. Looney 			}
24412529f56eSJonathan T. Looney 			break;
2442e24e5683SJonathan T. Looney #endif
24432529f56eSJonathan T. Looney 
2444df8bae1dSRodney W. Grimes 		default:
24458501a69cSRobert Watson 			INP_WUNLOCK(inp);
2446df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2447df8bae1dSRodney W. Grimes 			break;
2448df8bae1dSRodney W. Grimes 		}
2449df8bae1dSRodney W. Grimes 		break;
2450df8bae1dSRodney W. Grimes 
2451cfe8b629SGarrett Wollman 	case SOPT_GET:
24521e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2453cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2454fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
245588f6b043SBruce M Simpson 		case TCP_MD5SIG:
24568501a69cSRobert Watson 			INP_WUNLOCK(inp);
245797453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2458fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2459fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
24601cfd4b53SBruce M Simpson 			break;
2461265ed012SBruce M Simpson #endif
24621e8f5ffaSRobert Watson 
2463df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2464cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
24658501a69cSRobert Watson 			INP_WUNLOCK(inp);
2466b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2467df8bae1dSRodney W. Grimes 			break;
2468df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2469cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
24708501a69cSRobert Watson 			INP_WUNLOCK(inp);
2471b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2472df8bae1dSRodney W. Grimes 			break;
24739e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
24749e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
24759e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
24769e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
24779e644c23SMichael Tuexen 			break;
2478a0292f23SGarrett Wollman 		case TCP_NOOPT:
2479cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
24808501a69cSRobert Watson 			INP_WUNLOCK(inp);
2481b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2482a0292f23SGarrett Wollman 			break;
2483a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2484cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
24858501a69cSRobert Watson 			INP_WUNLOCK(inp);
2486b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2487b8af5dfaSRobert Watson 			break;
2488b8af5dfaSRobert Watson 		case TCP_INFO:
2489b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
24908501a69cSRobert Watson 			INP_WUNLOCK(inp);
2491b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2492a0292f23SGarrett Wollman 			break;
2493adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2494adc56f5aSEdward Tomasz Napierala 			{
2495adc56f5aSEdward Tomasz Napierala #ifdef STATS
2496adc56f5aSEdward Tomasz Napierala 			int nheld;
2497adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2498adc56f5aSEdward Tomasz Napierala 
2499adc56f5aSEdward Tomasz Napierala 			error = 0;
2500adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2501adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2502adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2503adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2504adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2505adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2506adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2507adc56f5aSEdward Tomasz Napierala 			else
2508adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2509adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2510adc56f5aSEdward Tomasz Napierala 			if (error)
2511adc56f5aSEdward Tomasz Napierala 				break;
2512adc56f5aSEdward Tomasz Napierala 
2513adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2514adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2515adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2516adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2517adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2518adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2519adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2520adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2521adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2522adc56f5aSEdward Tomasz Napierala 				break;
2523adc56f5aSEdward Tomasz Napierala 			}
2524adc56f5aSEdward Tomasz Napierala 
2525adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2526adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2527adc56f5aSEdward Tomasz Napierala 				goto unhold;
2528adc56f5aSEdward Tomasz Napierala 
2529adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2530adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2531adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2532adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2533adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2534adc56f5aSEdward Tomasz Napierala unhold:
2535adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2536adc56f5aSEdward Tomasz Napierala #else
2537adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2538adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2539adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2540adc56f5aSEdward Tomasz Napierala 			break;
2541adc56f5aSEdward Tomasz Napierala 			}
2542dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2543af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2544dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2545af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2546dbc42409SLawrence Stewart 			break;
254708af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25482f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
25492f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
25502f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
25512f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
25522f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
255308af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
255408af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
255508af8aacSRandall Stewart 				break;
25562f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
25575a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
25582f3eb7f4SGleb Smirnoff 				break;
25592f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
25605a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
25612f3eb7f4SGleb Smirnoff 				break;
25622f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
25635a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
25642f3eb7f4SGleb Smirnoff 				break;
25652f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
25665a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
25672f3eb7f4SGleb Smirnoff 				break;
25682f3eb7f4SGleb Smirnoff 			}
25692f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
25702f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
25712f3eb7f4SGleb Smirnoff 			break;
2572281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2573281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2574281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2575281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2576281a0fd4SPatrick Kelsey 			break;
2577e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
25782529f56eSJonathan T. Looney 		case TCP_LOG:
257969c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
25802529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25812529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
25822529f56eSJonathan T. Looney 			break;
25832529f56eSJonathan T. Looney 		case TCP_LOGBUF:
25842529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
25852529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
25862529f56eSJonathan T. Looney 			break;
25872529f56eSJonathan T. Looney 		case TCP_LOGID:
25882529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
25892529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25902529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
25912529f56eSJonathan T. Looney 			break;
25922529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
25932529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
25942529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25952529f56eSJonathan T. Looney 			error = EINVAL;
25962529f56eSJonathan T. Looney 			break;
2597e24e5683SJonathan T. Looney #endif
2598b2e60773SJohn Baldwin #ifdef KERN_TLS
2599b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2600fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2601b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2602bf256782SMark Johnston 			if (error == 0)
2603bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2604bf256782SMark Johnston 				    sizeof(optval));
2605b2e60773SJohn Baldwin 			break;
2606f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2607fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2608f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2609bf256782SMark Johnston 			if (error == 0)
2610bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2611bf256782SMark Johnston 				    sizeof(optval));
2612f1f93475SJohn Baldwin 			break;
2613b2e60773SJohn Baldwin #endif
2614df8bae1dSRodney W. Grimes 		default:
26158501a69cSRobert Watson 			INP_WUNLOCK(inp);
2616df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2617df8bae1dSRodney W. Grimes 			break;
2618df8bae1dSRodney W. Grimes 		}
2619df8bae1dSRodney W. Grimes 		break;
2620df8bae1dSRodney W. Grimes 	}
2621df8bae1dSRodney W. Grimes 	return (error);
2622df8bae1dSRodney W. Grimes }
26238501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2624bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2625df8bae1dSRodney W. Grimes 
262626e30fbbSDavid Greenman /*
2627df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2628df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2629df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2630df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2631df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2632df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2633df8bae1dSRodney W. Grimes  */
2634623dce13SRobert Watson static void
tcp_disconnect(struct tcpcb * tp)2635ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2636df8bae1dSRodney W. Grimes {
26379eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
26389eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2639e6e0b5ffSRobert Watson 
264097a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26418501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2642df8bae1dSRodney W. Grimes 
2643623dce13SRobert Watson 	/*
2644623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2645623dce13SRobert Watson 	 * socket is still open.
2646623dce13SRobert Watson 	 */
26478db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
2648dd7b86e2SGleb Smirnoff 	    !(tp->t_state > TCPS_LISTEN && (tp->t_flags & TF_FASTOPEN))) {
2649df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2650623dce13SRobert Watson 		KASSERT(tp != NULL,
2651623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2652623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2653243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2654623dce13SRobert Watson 		KASSERT(tp != NULL,
2655623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2656623dce13SRobert Watson 	} else {
2657df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2658df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2659623dce13SRobert Watson 		tcp_usrclosed(tp);
2660ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2661f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2662f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2663df8bae1dSRodney W. Grimes 	}
2664df8bae1dSRodney W. Grimes }
2665df8bae1dSRodney W. Grimes 
2666df8bae1dSRodney W. Grimes /*
2667df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2668df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2669df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2670df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2671df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2672df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2673df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2674df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2675df8bae1dSRodney W. Grimes  */
2676623dce13SRobert Watson static void
tcp_usrclosed(struct tcpcb * tp)2677ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2678df8bae1dSRodney W. Grimes {
2679df8bae1dSRodney W. Grimes 
268097a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26819eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2682e6e0b5ffSRobert Watson 
2683df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2684df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
268509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
268609fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
268709fe6320SNavdeep Parhar #endif
2688550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2689bc65987aSKip Macy 		/* FALLTHROUGH */
2690bc65987aSKip Macy 	case TCPS_CLOSED:
2691df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2692623dce13SRobert Watson 		/*
2693623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2694623dce13SRobert Watson 		 * still open.
2695623dce13SRobert Watson 		 */
2696623dce13SRobert Watson 		KASSERT(tp != NULL,
2697623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2698df8bae1dSRodney W. Grimes 		break;
2699df8bae1dSRodney W. Grimes 
2700a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2701df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2702a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2703a0292f23SGarrett Wollman 		break;
2704a0292f23SGarrett Wollman 
2705df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
270657f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2707df8bae1dSRodney W. Grimes 		break;
2708df8bae1dSRodney W. Grimes 
2709df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
271057f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2711df8bae1dSRodney W. Grimes 		break;
2712df8bae1dSRodney W. Grimes 	}
271308af8aacSRandall Stewart 	if (tp->t_acktime == 0)
271408af8aacSRandall Stewart 		tp->t_acktime = ticks;
2715abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27163eeb22cbSRichard Scheffenegger 		tcp_free_sackholes(tp);
27179eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2718abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27197c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27207c72af87SMohan Srinivasan 			int timeout;
27217c72af87SMohan Srinivasan 
27227c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27239077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2724b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2725b6239c4aSAndras Olah 		}
2726df8bae1dSRodney W. Grimes 	}
27277c72af87SMohan Srinivasan }
2728497057eeSRobert Watson 
2729497057eeSRobert Watson #ifdef DDB
2730497057eeSRobert Watson static void
db_print_indent(int indent)2731497057eeSRobert Watson db_print_indent(int indent)
2732497057eeSRobert Watson {
2733497057eeSRobert Watson 	int i;
2734497057eeSRobert Watson 
2735497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2736497057eeSRobert Watson 		db_printf(" ");
2737497057eeSRobert Watson }
2738497057eeSRobert Watson 
2739497057eeSRobert Watson static void
db_print_tstate(int t_state)2740497057eeSRobert Watson db_print_tstate(int t_state)
2741497057eeSRobert Watson {
2742497057eeSRobert Watson 
2743497057eeSRobert Watson 	switch (t_state) {
2744497057eeSRobert Watson 	case TCPS_CLOSED:
2745497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2746497057eeSRobert Watson 		return;
2747497057eeSRobert Watson 
2748497057eeSRobert Watson 	case TCPS_LISTEN:
2749497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2750497057eeSRobert Watson 		return;
2751497057eeSRobert Watson 
2752497057eeSRobert Watson 	case TCPS_SYN_SENT:
2753497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2754497057eeSRobert Watson 		return;
2755497057eeSRobert Watson 
2756497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2757497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2758497057eeSRobert Watson 		return;
2759497057eeSRobert Watson 
2760497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2761497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2762497057eeSRobert Watson 		return;
2763497057eeSRobert Watson 
2764497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2765497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2766497057eeSRobert Watson 		return;
2767497057eeSRobert Watson 
2768497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2769497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2770497057eeSRobert Watson 		return;
2771497057eeSRobert Watson 
2772497057eeSRobert Watson 	case TCPS_CLOSING:
2773497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2774497057eeSRobert Watson 		return;
2775497057eeSRobert Watson 
2776497057eeSRobert Watson 	case TCPS_LAST_ACK:
2777497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2778497057eeSRobert Watson 		return;
2779497057eeSRobert Watson 
2780497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2781497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2782497057eeSRobert Watson 		return;
2783497057eeSRobert Watson 
2784497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2785497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2786497057eeSRobert Watson 		return;
2787497057eeSRobert Watson 
2788497057eeSRobert Watson 	default:
2789497057eeSRobert Watson 		db_printf("unknown");
2790497057eeSRobert Watson 		return;
2791497057eeSRobert Watson 	}
2792497057eeSRobert Watson }
2793497057eeSRobert Watson 
2794497057eeSRobert Watson static void
db_print_tflags(u_int t_flags)2795497057eeSRobert Watson db_print_tflags(u_int t_flags)
2796497057eeSRobert Watson {
2797497057eeSRobert Watson 	int comma;
2798497057eeSRobert Watson 
2799497057eeSRobert Watson 	comma = 0;
2800497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2801497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2802497057eeSRobert Watson 		comma = 1;
2803497057eeSRobert Watson 	}
2804497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2805497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2806497057eeSRobert Watson 		comma = 1;
2807497057eeSRobert Watson 	}
2808497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2809497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2810497057eeSRobert Watson 		comma = 1;
2811497057eeSRobert Watson 	}
2812497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2813497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2814497057eeSRobert Watson 		comma = 1;
2815497057eeSRobert Watson 	}
2816497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2817497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2818497057eeSRobert Watson 		comma = 1;
2819497057eeSRobert Watson 	}
2820497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2821497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2822497057eeSRobert Watson 		comma = 1;
2823497057eeSRobert Watson 	}
2824497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2825497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2826497057eeSRobert Watson 		comma = 1;
2827497057eeSRobert Watson 	}
2828497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2829497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2830497057eeSRobert Watson 		comma = 1;
2831497057eeSRobert Watson 	}
2832497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2833497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2834497057eeSRobert Watson 		comma = 1;
2835497057eeSRobert Watson 	}
2836497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2837497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2838497057eeSRobert Watson 		comma = 1;
2839497057eeSRobert Watson 	}
2840497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2841497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2842497057eeSRobert Watson 		comma = 1;
2843497057eeSRobert Watson 	}
2844497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2845497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2846497057eeSRobert Watson 		comma = 1;
2847497057eeSRobert Watson 	}
2848497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2849497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2850497057eeSRobert Watson 		comma = 1;
2851497057eeSRobert Watson 	}
28523f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
28533f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
28543f169c54SRichard Scheffenegger 		comma = 1;
28553f169c54SRichard Scheffenegger 	}
2856093d9b46SMichael Tuexen 	if (t_flags & TF_WAKESOR) {
2857093d9b46SMichael Tuexen 		db_printf("%sTF_WAKESOR", comma ? ", " : "");
2858093d9b46SMichael Tuexen 		comma = 1;
2859093d9b46SMichael Tuexen 	}
2860093d9b46SMichael Tuexen 	if (t_flags & TF_GPUTINPROG) {
2861093d9b46SMichael Tuexen 		db_printf("%sTF_GPUTINPROG", comma ? ", " : "");
2862093d9b46SMichael Tuexen 		comma = 1;
2863093d9b46SMichael Tuexen 	}
2864497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2865497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2866497057eeSRobert Watson 		comma = 1;
2867497057eeSRobert Watson 	}
2868493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2869493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2870497057eeSRobert Watson 		comma = 1;
2871497057eeSRobert Watson 	}
2872497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2873497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2874497057eeSRobert Watson 		comma = 1;
2875497057eeSRobert Watson 	}
2876497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2877497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2878497057eeSRobert Watson 		comma = 1;
2879497057eeSRobert Watson 	}
2880497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2881497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2882497057eeSRobert Watson 		comma = 1;
2883497057eeSRobert Watson 	}
2884497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2885497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2886497057eeSRobert Watson 		comma = 1;
2887497057eeSRobert Watson 	}
2888497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2889497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2890497057eeSRobert Watson 		comma = 1;
2891497057eeSRobert Watson 	}
2892497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2893497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2894497057eeSRobert Watson 		comma = 1;
2895497057eeSRobert Watson 	}
2896497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2897497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2898497057eeSRobert Watson 		comma = 1;
2899497057eeSRobert Watson 	}
2900093d9b46SMichael Tuexen 	if (t_flags & TF_TOE) {
2901093d9b46SMichael Tuexen 		db_printf("%sTF_TOE", comma ? ", " : "");
2902093d9b46SMichael Tuexen 		comma = 1;
2903093d9b46SMichael Tuexen 	}
2904093d9b46SMichael Tuexen 	if (t_flags & TF_CLOSED) {
2905093d9b46SMichael Tuexen 		db_printf("%sTF_CLOSED", comma ? ", " : "");
2906093d9b46SMichael Tuexen 		comma = 1;
2907093d9b46SMichael Tuexen 	}
2908093d9b46SMichael Tuexen 	if (t_flags & TF_SENTSYN) {
2909093d9b46SMichael Tuexen 		db_printf("%sTF_SENTSYN", comma ? ", " : "");
2910093d9b46SMichael Tuexen 		comma = 1;
2911093d9b46SMichael Tuexen 	}
2912093d9b46SMichael Tuexen 	if (t_flags & TF_LRD) {
2913093d9b46SMichael Tuexen 		db_printf("%sTF_LRD", comma ? ", " : "");
2914093d9b46SMichael Tuexen 		comma = 1;
2915093d9b46SMichael Tuexen 	}
2916093d9b46SMichael Tuexen 	if (t_flags & TF_CONGRECOVERY) {
2917093d9b46SMichael Tuexen 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2918093d9b46SMichael Tuexen 		comma = 1;
2919093d9b46SMichael Tuexen 	}
2920093d9b46SMichael Tuexen 	if (t_flags & TF_WASCRECOVERY) {
2921093d9b46SMichael Tuexen 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2922093d9b46SMichael Tuexen 		comma = 1;
2923093d9b46SMichael Tuexen 	}
2924281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2925281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2926281a0fd4SPatrick Kelsey 		comma = 1;
2927281a0fd4SPatrick Kelsey 	}
2928497057eeSRobert Watson }
2929497057eeSRobert Watson 
2930497057eeSRobert Watson static void
db_print_tflags2(u_int t_flags2)29313cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29323cf38784SMichael Tuexen {
29333cf38784SMichael Tuexen 	int comma;
29343cf38784SMichael Tuexen 
29353cf38784SMichael Tuexen 	comma = 0;
29363f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29373f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29383f169c54SRichard Scheffenegger 		comma = 1;
29393f169c54SRichard Scheffenegger 	}
29403f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29413f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29423f169c54SRichard Scheffenegger 		comma = 1;
29433f169c54SRichard Scheffenegger 	}
29443f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29453f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29463f169c54SRichard Scheffenegger 		comma = 1;
29473f169c54SRichard Scheffenegger 	}
29483f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29493f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29503f169c54SRichard Scheffenegger 		comma = 1;
29513f169c54SRichard Scheffenegger 	}
29523f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29533f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29543f169c54SRichard Scheffenegger 		comma = 1;
29553f169c54SRichard Scheffenegger 	}
29563cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29573cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29583cf38784SMichael Tuexen 		comma = 1;
29593cf38784SMichael Tuexen 	}
29603f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
29613f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
29623f169c54SRichard Scheffenegger 		comma = 1;
29633f169c54SRichard Scheffenegger 	}
29643f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
29653f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
29663f169c54SRichard Scheffenegger 		comma = 1;
29673f169c54SRichard Scheffenegger 	}
29683f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
29693f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
29703f169c54SRichard Scheffenegger 		comma = 1;
29713f169c54SRichard Scheffenegger 	}
2972093d9b46SMichael Tuexen 	if (t_flags2 & TF2_HPTS_CPU_SET) {
2973093d9b46SMichael Tuexen 		db_printf("%sTF2_HPTS_CPU_SET", comma ? ", " : "");
2974093d9b46SMichael Tuexen 		comma = 1;
2975093d9b46SMichael Tuexen 	}
29763f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
29773f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
29783f169c54SRichard Scheffenegger 		comma = 1;
29793f169c54SRichard Scheffenegger 	}
2980093d9b46SMichael Tuexen 	if (t_flags2 & TF2_ECN_USE_ECT1) {
2981093d9b46SMichael Tuexen 		db_printf("%sTF2_ECN_USE_ECT1", comma ? ", " : "");
2982093d9b46SMichael Tuexen 		comma = 1;
2983093d9b46SMichael Tuexen 	}
2984093d9b46SMichael Tuexen 	if (t_flags2 & TF2_TCP_ACCOUNTING) {
2985093d9b46SMichael Tuexen 		db_printf("%sTF2_TCP_ACCOUNTING", comma ? ", " : "");
2986093d9b46SMichael Tuexen 		comma = 1;
2987093d9b46SMichael Tuexen 	}
2988093d9b46SMichael Tuexen 	if (t_flags2 & TF2_HPTS_CALLS) {
2989093d9b46SMichael Tuexen 		db_printf("%sTF2_HPTS_CALLS", comma ? ", " : "");
2990093d9b46SMichael Tuexen 		comma = 1;
2991093d9b46SMichael Tuexen 	}
2992093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_L_ACKS) {
2993093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_L_ACKS", comma ? ", " : "");
2994093d9b46SMichael Tuexen 		comma = 1;
2995093d9b46SMichael Tuexen 	}
2996093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_ACKCMP) {
2997093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_ACKCMP", comma ? ", " : "");
2998093d9b46SMichael Tuexen 		comma = 1;
2999093d9b46SMichael Tuexen 	}
3000093d9b46SMichael Tuexen 	if (t_flags2 & TF2_SUPPORTS_MBUFQ) {
3001093d9b46SMichael Tuexen 		db_printf("%sTF2_SUPPORTS_MBUFQ", comma ? ", " : "");
3002093d9b46SMichael Tuexen 		comma = 1;
3003093d9b46SMichael Tuexen 	}
3004093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_QUEUE_READY) {
3005093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_QUEUE_READY", comma ? ", " : "");
3006093d9b46SMichael Tuexen 		comma = 1;
3007093d9b46SMichael Tuexen 	}
3008093d9b46SMichael Tuexen 	if (t_flags2 & TF2_DONT_SACK_QUEUE) {
3009093d9b46SMichael Tuexen 		db_printf("%sTF2_DONT_SACK_QUEUE", comma ? ", " : "");
3010093d9b46SMichael Tuexen 		comma = 1;
3011093d9b46SMichael Tuexen 	}
3012093d9b46SMichael Tuexen 	if (t_flags2 & TF2_CANNOT_DO_ECN) {
3013093d9b46SMichael Tuexen 		db_printf("%sTF2_CANNOT_DO_ECN", comma ? ", " : "");
3014093d9b46SMichael Tuexen 		comma = 1;
3015093d9b46SMichael Tuexen 	}
3016093d9b46SMichael Tuexen 	if (t_flags2 & TF2_PROC_SACK_PROHIBIT) {
3017093d9b46SMichael Tuexen 		db_printf("%sTF2_PROC_SACK_PROHIBIT", comma ? ", " : "");
3018093d9b46SMichael Tuexen 		comma = 1;
3019093d9b46SMichael Tuexen 	}
3020093d9b46SMichael Tuexen 	if (t_flags2 & TF2_IPSEC_TSO) {
3021093d9b46SMichael Tuexen 		db_printf("%sTF2_IPSEC_TSO", comma ? ", " : "");
3022093d9b46SMichael Tuexen 		comma = 1;
3023093d9b46SMichael Tuexen 	}
3024093d9b46SMichael Tuexen 	if (t_flags2 & TF2_NO_ISS_CHECK) {
3025093d9b46SMichael Tuexen 		db_printf("%sTF2_NO_ISS_CHECK", comma ? ", " : "");
3026093d9b46SMichael Tuexen 		comma = 1;
3027093d9b46SMichael Tuexen 	}
30283cf38784SMichael Tuexen }
30293cf38784SMichael Tuexen 
30303cf38784SMichael Tuexen static void
db_print_toobflags(char t_oobflags)3031497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3032497057eeSRobert Watson {
3033497057eeSRobert Watson 	int comma;
3034497057eeSRobert Watson 
3035497057eeSRobert Watson 	comma = 0;
3036497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3037497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3038497057eeSRobert Watson 		comma = 1;
3039497057eeSRobert Watson 	}
3040497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3041497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3042497057eeSRobert Watson 		comma = 1;
3043497057eeSRobert Watson 	}
3044497057eeSRobert Watson }
3045497057eeSRobert Watson 
3046497057eeSRobert Watson static void
db_print_tcpcb(struct tcpcb * tp,const char * name,int indent)3047497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3048497057eeSRobert Watson {
3049497057eeSRobert Watson 
3050497057eeSRobert Watson 	db_print_indent(indent);
3051497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3052497057eeSRobert Watson 
3053497057eeSRobert Watson 	indent += 2;
3054497057eeSRobert Watson 
3055497057eeSRobert Watson 	db_print_indent(indent);
3056497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3057c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3058497057eeSRobert Watson 
3059497057eeSRobert Watson 	db_print_indent(indent);
3060446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3061446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3062497057eeSRobert Watson 
3063497057eeSRobert Watson 	db_print_indent(indent);
3064497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3065497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3066497057eeSRobert Watson 	db_printf(")\n");
3067497057eeSRobert Watson 
3068497057eeSRobert Watson 	db_print_indent(indent);
3069497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3070497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3071497057eeSRobert Watson 	db_printf(")\n");
3072497057eeSRobert Watson 
3073497057eeSRobert Watson 	db_print_indent(indent);
30743cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30753cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30763cf38784SMichael Tuexen 	db_printf(")\n");
30773cf38784SMichael Tuexen 
30783cf38784SMichael Tuexen 	db_print_indent(indent);
3079fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3080497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3081497057eeSRobert Watson 
3082497057eeSRobert Watson 	db_print_indent(indent);
3083497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3084497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3085497057eeSRobert Watson 
3086497057eeSRobert Watson 	db_print_indent(indent);
3087497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3088497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3089497057eeSRobert Watson 
3090497057eeSRobert Watson 	db_print_indent(indent);
30913ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3092497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3093497057eeSRobert Watson 
3094497057eeSRobert Watson 	db_print_indent(indent);
30953ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
30961c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3097497057eeSRobert Watson 
3098497057eeSRobert Watson 	db_print_indent(indent);
30993ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
31001c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3101497057eeSRobert Watson 
3102497057eeSRobert Watson 	db_print_indent(indent);
31030c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
31040c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3105497057eeSRobert Watson 
3106497057eeSRobert Watson 	db_print_indent(indent);
31071c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
31081c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3109497057eeSRobert Watson 
3110497057eeSRobert Watson 	db_print_indent(indent);
31111c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
31121c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3113497057eeSRobert Watson 
3114497057eeSRobert Watson 	db_print_indent(indent);
3115bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3116bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3117497057eeSRobert Watson 
3118497057eeSRobert Watson 	db_print_indent(indent);
311918b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3120497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3121497057eeSRobert Watson 
3122497057eeSRobert Watson 	db_print_indent(indent);
3123497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3124497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3125497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3126497057eeSRobert Watson 
3127497057eeSRobert Watson 	db_print_indent(indent);
3128497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3129497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3130497057eeSRobert Watson 
3131497057eeSRobert Watson 	db_print_indent(indent);
31329f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31331a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3134497057eeSRobert Watson 
3135497057eeSRobert Watson 	db_print_indent(indent);
3136497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31373ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3138497057eeSRobert Watson 
3139497057eeSRobert Watson 	db_print_indent(indent);
31403ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31419f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3142497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3143497057eeSRobert Watson 
3144497057eeSRobert Watson 	db_print_indent(indent);
31453529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31463529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3147497057eeSRobert Watson 
3148497057eeSRobert Watson 	db_print_indent(indent);
3149a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3150a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3151497057eeSRobert Watson 
3152497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3153497057eeSRobert Watson 
3154497057eeSRobert Watson 	db_print_indent(indent);
3155497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3156497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3157497057eeSRobert Watson }
3158497057eeSRobert Watson 
DB_SHOW_COMMAND(tcpcb,db_show_tcpcb)3159497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3160497057eeSRobert Watson {
3161497057eeSRobert Watson 	struct tcpcb *tp;
3162497057eeSRobert Watson 
3163497057eeSRobert Watson 	if (!have_addr) {
3164497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3165497057eeSRobert Watson 		return;
3166497057eeSRobert Watson 	}
3167497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3168497057eeSRobert Watson 
3169497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3170497057eeSRobert Watson }
3171497057eeSRobert Watson #endif
3172