xref: /freebsd/sys/netinet/tcp_usrreq.c (revision cfb1e92912b4cf75360b7fbe86197cc29bc212c1)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
5623dce13SRobert Watson  *	The Regents of the University of California.
6497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
7fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8623dce13SRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
10fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
11fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
12fa046d87SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
384b421e2dSMike Silbersack #include <sys/cdefs.h>
39497057eeSRobert Watson #include "opt_ddb.h"
401cfd4b53SBruce M Simpson #include "opt_inet.h"
41fb59c426SYoshinobu Inoue #include "opt_inet6.h"
42fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
43b2e60773SJohn Baldwin #include "opt_kern_tls.h"
440cc12cc5SJoerg Wunsch 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
489077f387SGleb Smirnoff #include <sys/limits.h>
49f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5055bceb1eSRandall Stewart #include <sys/refcount.h>
51c7a82f90SGarrett Wollman #include <sys/kernel.h>
52b2e60773SJohn Baldwin #include <sys/ktls.h>
53adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5498163b98SPoul-Henning Kamp #include <sys/sysctl.h>
55df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
56fb59c426SYoshinobu Inoue #ifdef INET6
57fb59c426SYoshinobu Inoue #include <sys/domain.h>
58fb59c426SYoshinobu Inoue #endif /* INET6 */
59df8bae1dSRodney W. Grimes #include <sys/socket.h>
60df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
61df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6291421ba2SRobert Watson #include <sys/proc.h>
6391421ba2SRobert Watson #include <sys/jail.h>
64adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
65df8bae1dSRodney W. Grimes 
66497057eeSRobert Watson #ifdef DDB
67497057eeSRobert Watson #include <ddb/ddb.h>
68497057eeSRobert Watson #endif
69497057eeSRobert Watson 
70df8bae1dSRodney W. Grimes #include <net/if.h>
7176039bc8SGleb Smirnoff #include <net/if_var.h>
72df8bae1dSRodney W. Grimes #include <net/route.h>
73530c0060SRobert Watson #include <net/vnet.h>
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes #include <netinet/in.h>
765d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
77df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
78b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
79b5e8ce9fSBruce Evans #include <netinet/in_var.h>
803b0ee680SRichard Scheffenegger #include <netinet/ip.h>
81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
82fb59c426SYoshinobu Inoue #ifdef INET6
83b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
84b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
85fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
86a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
87fb59c426SYoshinobu Inoue #endif
882de3e790SGleb Smirnoff #include <netinet/tcp.h>
89df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
90df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
91df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
92df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
932529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
954644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
96c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
97fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
9886a996e6SHiren Panchasara #ifdef TCPPCAP
9986a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10086a996e6SHiren Panchasara #endif
10109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
102bc65987aSKip Macy #include <netinet/tcp_offload.h>
10309fe6320SNavdeep Parhar #endif
104fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
105df8bae1dSRodney W. Grimes 
106adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
107adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
108adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
109adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
110adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
111adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
112adc56f5aSEdward Tomasz Napierala 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
115df8bae1dSRodney W. Grimes  */
116b287c6c7SBjoern A. Zeeb #ifdef INET
117a9d22cceSGleb Smirnoff static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
1184d77a549SAlfred Perlstein 		    struct thread *td);
119b287c6c7SBjoern A. Zeeb #endif /* INET */
120fb59c426SYoshinobu Inoue #ifdef INET6
121a9d22cceSGleb Smirnoff static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
1224d77a549SAlfred Perlstein 		    struct thread *td);
123fb59c426SYoshinobu Inoue #endif /* INET6 */
124623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
125623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
1268c6104c4SMarius Strobl static void	tcp_fill_info(const struct tcpcb *, struct tcp_info *);
1272c37256eSGarrett Wollman 
128d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
129d3b6c96bSRandall Stewart 
13000812bbdSMichael Tuexen static void
13100812bbdSMichael Tuexen tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
13200812bbdSMichael Tuexen {
13300812bbdSMichael Tuexen 	struct tcp_log_buffer *lgb;
13400812bbdSMichael Tuexen 
135c2399dd2SMichael Tuexen 	KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
13600812bbdSMichael Tuexen 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13769c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
13869c7c811SRandall Stewart 		lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
13900812bbdSMichael Tuexen 		    0, NULL, false, NULL, NULL, 0, NULL);
14000812bbdSMichael Tuexen 	} else {
14100812bbdSMichael Tuexen 		lgb = NULL;
14200812bbdSMichael Tuexen 	}
14300812bbdSMichael Tuexen 	if (lgb != NULL) {
14400812bbdSMichael Tuexen 		if (error >= 0) {
14500812bbdSMichael Tuexen 			lgb->tlb_errno = (uint32_t)error;
14600812bbdSMichael Tuexen 		}
14700812bbdSMichael Tuexen 		lgb->tlb_flex1 = pru;
14800812bbdSMichael Tuexen 	}
14900812bbdSMichael Tuexen }
15000812bbdSMichael Tuexen 
1512c37256eSGarrett Wollman /*
1522c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1532c37256eSGarrett Wollman  * and an internet control block.
1542c37256eSGarrett Wollman  */
1552c37256eSGarrett Wollman static int
156b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1572c37256eSGarrett Wollman {
158f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
159623dce13SRobert Watson 	struct tcpcb *tp = NULL;
160623dce13SRobert Watson 	int error;
1612c37256eSGarrett Wollman 
162623dce13SRobert Watson 	inp = sotoinpcb(so);
163623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1642c37256eSGarrett Wollman 
1650f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1662c37256eSGarrett Wollman 	if (error)
1672c37256eSGarrett Wollman 		goto out;
1682c37256eSGarrett Wollman 
1690f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1700f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1710f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1727669c586SGleb Smirnoff 	if (error)
1730f6385e7SGleb Smirnoff 		goto out;
1740f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1750f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1760f6385e7SGleb Smirnoff 	if (tp == NULL) {
1777669c586SGleb Smirnoff 		error = ENOBUFS;
1780f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1790f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1800f6385e7SGleb Smirnoff 		goto out;
1810f6385e7SGleb Smirnoff 	}
1820f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
18300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ATTACH, error);
1840f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1850f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1862c37256eSGarrett Wollman out:
1875d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1880f6385e7SGleb Smirnoff 	return (error);
1892c37256eSGarrett Wollman }
1902c37256eSGarrett Wollman 
1912c37256eSGarrett Wollman /*
1923fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
193a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
194a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
195a152f8a3SRobert Watson  * inpcb state: time wait.
1962c37256eSGarrett Wollman  */
197bc725eafSRobert Watson static void
1983fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
1992c37256eSGarrett Wollman {
2003fed74e9SGleb Smirnoff 	struct inpcb *inp;
2012c37256eSGarrett Wollman 	struct tcpcb *tp;
2022c37256eSGarrett Wollman 
2033fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2043fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2053fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2063fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2073fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
208953b5606SRobert Watson 
209a152f8a3SRobert Watson 	tp = intotcpcb(inp);
210a152f8a3SRobert Watson 
2111b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2121b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2131b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2149c3507f9SGleb Smirnoff 
215623dce13SRobert Watson 	tcp_discardcb(tp);
216623dce13SRobert Watson 	in_pcbdetach(inp);
2170206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
218623dce13SRobert Watson }
219c78cbc7bSRobert Watson 
220b287c6c7SBjoern A. Zeeb #ifdef INET
2212c37256eSGarrett Wollman /*
2222c37256eSGarrett Wollman  * Give the socket an address.
2232c37256eSGarrett Wollman  */
2242c37256eSGarrett Wollman static int
225b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2262c37256eSGarrett Wollman {
2272c37256eSGarrett Wollman 	int error = 0;
228f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
229c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2302c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2312c37256eSGarrett Wollman 
232c2399dd2SMichael Tuexen 	inp = sotoinpcb(so);
233c2399dd2SMichael Tuexen 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
234c2399dd2SMichael Tuexen 	INP_WLOCK(inp);
235c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
236c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
237c2399dd2SMichael Tuexen 		return (EINVAL);
238c2399dd2SMichael Tuexen 	}
239c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
240c2399dd2SMichael Tuexen 
24152710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
242f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
243f96603b5SMark Johnston 		/*
244f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
245f96603b5SMark Johnston 		 */
246f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2473f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
248c2399dd2SMichael Tuexen 		    sinp->sin_addr.s_addr != INADDR_ANY) {
249c2399dd2SMichael Tuexen 			error = EAFNOSUPPORT;
250c2399dd2SMichael Tuexen 			goto out;
251c2399dd2SMichael Tuexen 		}
252f96603b5SMark Johnston 		nam->sa_family = AF_INET;
253f96603b5SMark Johnston 	}
254c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sinp)) {
255c2399dd2SMichael Tuexen 		error = EINVAL;
256c2399dd2SMichael Tuexen 		goto out;
257c2399dd2SMichael Tuexen 	}
2582c37256eSGarrett Wollman 	/*
2592c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2602c37256eSGarrett Wollman 	 * to them.
2612c37256eSGarrett Wollman 	 */
262c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
263c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
2642c37256eSGarrett Wollman 		goto out;
265623dce13SRobert Watson 	}
266fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
26796871af0SGleb Smirnoff 	error = in_pcbbind(inp, sinp, td->td_ucred);
268fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
269623dce13SRobert Watson out:
27000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
2715d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2728501a69cSRobert Watson 	INP_WUNLOCK(inp);
273623dce13SRobert Watson 
274623dce13SRobert Watson 	return (error);
2752c37256eSGarrett Wollman }
276b287c6c7SBjoern A. Zeeb #endif /* INET */
2772c37256eSGarrett Wollman 
278fb59c426SYoshinobu Inoue #ifdef INET6
279fb59c426SYoshinobu Inoue static int
280b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
281fb59c426SYoshinobu Inoue {
282fb59c426SYoshinobu Inoue 	int error = 0;
283f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
284c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2850ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2864a91aa8fSMichael Tuexen 	u_char vflagsav;
287fb59c426SYoshinobu Inoue 
288623dce13SRobert Watson 	inp = sotoinpcb(so);
289623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2908501a69cSRobert Watson 	INP_WLOCK(inp);
29153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
292c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
293c2399dd2SMichael Tuexen 		return (EINVAL);
294c2399dd2SMichael Tuexen 	}
295c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
296c2399dd2SMichael Tuexen 
297c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
298c2399dd2SMichael Tuexen 
299c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
300c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
301c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
302c2399dd2SMichael Tuexen 		goto out;
303c2399dd2SMichael Tuexen 	}
304c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sin6)) {
305623dce13SRobert Watson 		error = EINVAL;
306623dce13SRobert Watson 		goto out;
307623dce13SRobert Watson 	}
308c2399dd2SMichael Tuexen 	/*
309c2399dd2SMichael Tuexen 	 * Must check for multicast addresses and disallow binding
310c2399dd2SMichael Tuexen 	 * to them.
311c2399dd2SMichael Tuexen 	 */
312c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
313c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
314c2399dd2SMichael Tuexen 		goto out;
315c2399dd2SMichael Tuexen 	}
316c2399dd2SMichael Tuexen 
317fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
318fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
319fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
320b287c6c7SBjoern A. Zeeb #ifdef INET
32166ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3220ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
323fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3240ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
325fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
326fb59c426SYoshinobu Inoue 
3270ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
328888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
329888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
330888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
331888973f5SMichael Tuexen 				goto out;
332888973f5SMichael Tuexen 			}
333fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
334fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
33596871af0SGleb Smirnoff 			error = in_pcbbind(inp, &sin, td->td_ucred);
336fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
337fb59c426SYoshinobu Inoue 			goto out;
338fb59c426SYoshinobu Inoue 		}
339fb59c426SYoshinobu Inoue 	}
340b287c6c7SBjoern A. Zeeb #endif
34196871af0SGleb Smirnoff 	error = in6_pcbbind(inp, sin6, td->td_ucred);
342fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
343623dce13SRobert Watson out:
3444a91aa8fSMichael Tuexen 	if (error != 0)
3454a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
34600812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
3475d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3488501a69cSRobert Watson 	INP_WUNLOCK(inp);
349623dce13SRobert Watson 	return (error);
350fb59c426SYoshinobu Inoue }
351fb59c426SYoshinobu Inoue #endif /* INET6 */
352fb59c426SYoshinobu Inoue 
353b287c6c7SBjoern A. Zeeb #ifdef INET
3542c37256eSGarrett Wollman /*
3552c37256eSGarrett Wollman  * Prepare to accept connections.
3562c37256eSGarrett Wollman  */
3572c37256eSGarrett Wollman static int
358d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3592c37256eSGarrett Wollman {
3602c37256eSGarrett Wollman 	int error = 0;
361f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
362c2399dd2SMichael Tuexen 	struct tcpcb *tp;
3632c37256eSGarrett Wollman 
364623dce13SRobert Watson 	inp = sotoinpcb(so);
365623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3668501a69cSRobert Watson 	INP_WLOCK(inp);
36753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
368c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
369c2399dd2SMichael Tuexen 		return (EINVAL);
370623dce13SRobert Watson 	}
371623dce13SRobert Watson 	tp = intotcpcb(inp);
372c2399dd2SMichael Tuexen 
3730daccb9cSRobert Watson 	SOCK_LOCK(so);
3740daccb9cSRobert Watson 	error = solisten_proto_check(so);
375bd4a39ccSMark Johnston 	if (error != 0) {
376bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
377bd4a39ccSMark Johnston 		goto out;
378bd4a39ccSMark Johnston 	}
379bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
380fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
381bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, 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);
395623dce13SRobert Watson 
39668bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
397281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
39818a75309SPatrick Kelsey 
399623dce13SRobert Watson out:
40000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4028501a69cSRobert Watson 	INP_WUNLOCK(inp);
403623dce13SRobert Watson 	return (error);
4042c37256eSGarrett Wollman }
405b287c6c7SBjoern A. Zeeb #endif /* INET */
4062c37256eSGarrett Wollman 
407fb59c426SYoshinobu Inoue #ifdef INET6
408fb59c426SYoshinobu Inoue static int
409d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
410fb59c426SYoshinobu Inoue {
411fb59c426SYoshinobu Inoue 	int error = 0;
412f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
413c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4144a91aa8fSMichael Tuexen 	u_char vflagsav;
415fb59c426SYoshinobu Inoue 
416623dce13SRobert Watson 	inp = sotoinpcb(so);
417623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4188501a69cSRobert Watson 	INP_WLOCK(inp);
41953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
420c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
421c2399dd2SMichael Tuexen 		return (EINVAL);
422623dce13SRobert Watson 	}
423623dce13SRobert Watson 	tp = intotcpcb(inp);
424c2399dd2SMichael Tuexen 
425c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
426c2399dd2SMichael Tuexen 
4270daccb9cSRobert Watson 	SOCK_LOCK(so);
4280daccb9cSRobert Watson 	error = solisten_proto_check(so);
429bd4a39ccSMark Johnston 	if (error != 0) {
430bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
431bd4a39ccSMark Johnston 		goto out;
432bd4a39ccSMark Johnston 	}
433fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
434bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
435fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
43666ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
437fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
438bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
439fb59c426SYoshinobu Inoue 	}
440fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4410daccb9cSRobert Watson 	if (error == 0) {
44257f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
443d374e81eSRobert Watson 		solisten_proto(so, backlog);
44409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44537cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
44609fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
44709fe6320SNavdeep Parhar #endif
448bd4a39ccSMark Johnston 	} else {
449bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4500daccb9cSRobert Watson 	}
4510daccb9cSRobert Watson 	SOCK_UNLOCK(so);
452623dce13SRobert Watson 
45368bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
454281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45518a75309SPatrick Kelsey 
4564a91aa8fSMichael Tuexen 	if (error != 0)
4574a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4584a91aa8fSMichael Tuexen 
459623dce13SRobert Watson out:
46000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4615d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4628501a69cSRobert Watson 	INP_WUNLOCK(inp);
463623dce13SRobert Watson 	return (error);
464fb59c426SYoshinobu Inoue }
465fb59c426SYoshinobu Inoue #endif /* INET6 */
466fb59c426SYoshinobu Inoue 
467b287c6c7SBjoern A. Zeeb #ifdef INET
4682c37256eSGarrett Wollman /*
4692c37256eSGarrett Wollman  * Initiate connection to peer.
4702c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4712c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4722c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4732c37256eSGarrett Wollman  * Send initial segment on connection.
4742c37256eSGarrett Wollman  */
4752c37256eSGarrett Wollman static int
476b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4772c37256eSGarrett Wollman {
478109eb549SGleb Smirnoff 	struct epoch_tracker et;
4792c37256eSGarrett Wollman 	int error = 0;
480f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
481c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4822c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4832c37256eSGarrett Wollman 
484623dce13SRobert Watson 	inp = sotoinpcb(so);
485623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4868501a69cSRobert Watson 	INP_WLOCK(inp);
487eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
488c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
489c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
490c2399dd2SMichael Tuexen 	}
491c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
492c2399dd2SMichael Tuexen 
493c2399dd2SMichael Tuexen 	sinp = (struct sockaddr_in *)nam;
494c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET) {
495c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
496623dce13SRobert Watson 		goto out;
497623dce13SRobert Watson 	}
498c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sinp)) {
499c2399dd2SMichael Tuexen 		error = EINVAL;
500c2399dd2SMichael Tuexen 		goto out;
501c2399dd2SMichael Tuexen 	}
502c2399dd2SMichael Tuexen 	/*
503c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
504c2399dd2SMichael Tuexen 	 */
505c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
506c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
507c2399dd2SMichael Tuexen 		goto out;
508c2399dd2SMichael Tuexen 	}
509c2399dd2SMichael Tuexen 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
510c2399dd2SMichael Tuexen 		error = EACCES;
511c2399dd2SMichael Tuexen 		goto out;
512c2399dd2SMichael Tuexen 	}
513c2399dd2SMichael Tuexen 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
514c2399dd2SMichael Tuexen 		goto out;
515bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
516bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
517bd4a39ccSMark Johnston 		goto out;
518bd4a39ccSMark Johnston 	}
519c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
520a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
521c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
52309fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
52437cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
52509fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
526c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52709fe6320SNavdeep Parhar #endif
52809fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
52940fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5301d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5311d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
532c1604fe4SGleb Smirnoff out_in_epoch:
533109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
534623dce13SRobert Watson out:
53500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
536e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5378501a69cSRobert Watson 	INP_WUNLOCK(inp);
538623dce13SRobert Watson 	return (error);
5392c37256eSGarrett Wollman }
540b287c6c7SBjoern A. Zeeb #endif /* INET */
5412c37256eSGarrett Wollman 
542fb59c426SYoshinobu Inoue #ifdef INET6
543fb59c426SYoshinobu Inoue static int
544b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
545fb59c426SYoshinobu Inoue {
546109eb549SGleb Smirnoff 	struct epoch_tracker et;
547fb59c426SYoshinobu Inoue 	int error = 0;
548f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
549c2399dd2SMichael Tuexen 	struct tcpcb *tp;
5500ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5514a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5524a91aa8fSMichael Tuexen 	u_char vflagsav;
553623dce13SRobert Watson 
554623dce13SRobert Watson 	inp = sotoinpcb(so);
555623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5568501a69cSRobert Watson 	INP_WLOCK(inp);
557c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
558c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
559c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
560c2399dd2SMichael Tuexen 	}
561c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
562c2399dd2SMichael Tuexen 
5634a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5644a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
565c2399dd2SMichael Tuexen 
566c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
567c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
568c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
569c2399dd2SMichael Tuexen 		goto out;
570c2399dd2SMichael Tuexen 	}
571c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sin6)) {
572c2399dd2SMichael Tuexen 		error = EINVAL;
573c2399dd2SMichael Tuexen 		goto out;
574c2399dd2SMichael Tuexen 	}
575c2399dd2SMichael Tuexen 	/*
576c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
577c2399dd2SMichael Tuexen 	 */
578c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
579c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
580623dce13SRobert Watson 		goto out;
581623dce13SRobert Watson 	}
582bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
583bd4a39ccSMark Johnston 		error = EINVAL;
584bd4a39ccSMark Johnston 		goto out;
585bd4a39ccSMark Johnston 	}
586b287c6c7SBjoern A. Zeeb #ifdef INET
587fa046d87SRobert Watson 	/*
588fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
589fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
590fa046d87SRobert Watson 	 * Is this a significant problem?
591fa046d87SRobert Watson 	 */
5920ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
593fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
594fb59c426SYoshinobu Inoue 
595d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
596d46a5312SMaxim Konovalov 			error = EINVAL;
597d46a5312SMaxim Konovalov 			goto out;
598d46a5312SMaxim Konovalov 		}
5995dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6005dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6015dba6adaSMichael Tuexen 			goto out;
6025dba6adaSMichael Tuexen 		}
60333841545SHajimu UMEMOTO 
6040ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
605888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
606888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
607888973f5SMichael Tuexen 			goto out;
608888973f5SMichael Tuexen 		}
609f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
610f903a308SMichael Tuexen 			error = EACCES;
6112cf21ae5SRandall Stewart 			goto out;
6122cf21ae5SRandall Stewart 		}
613b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
614b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
615413628a7SBjoern A. Zeeb 			goto out;
6164a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6174a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
618c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
619a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
620c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62209fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
623adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62409fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
625c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62609fe6320SNavdeep Parhar #endif
62740fa3e40SGleb Smirnoff 		error = tcp_output(tp);
628c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6295dba6adaSMichael Tuexen 	} else {
6305dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6315dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6325dba6adaSMichael Tuexen 			goto out;
6335dba6adaSMichael Tuexen 		}
634fb59c426SYoshinobu Inoue 	}
635b287c6c7SBjoern A. Zeeb #endif
6364a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6374a91aa8fSMichael Tuexen 		goto out;
638fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
639fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
640dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6410773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
642a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6430773b44eSGleb Smirnoff 		goto out_in_epoch;
64409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64509fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
646adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
64709fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6480773b44eSGleb Smirnoff 		goto out_in_epoch;
64909fe6320SNavdeep Parhar #endif
65009fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
65140fa3e40SGleb Smirnoff 	error = tcp_output(tp);
652c1604fe4SGleb Smirnoff out_in_epoch:
653109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
654623dce13SRobert Watson out:
6551d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6561d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6574a91aa8fSMichael Tuexen 	/*
6584a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6594a91aa8fSMichael Tuexen 	 * the flags we modified.
6604a91aa8fSMichael Tuexen 	 */
6614a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6624a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6634a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6644a91aa8fSMichael Tuexen 	}
6654a91aa8fSMichael Tuexen 
66600812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6675d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6688501a69cSRobert Watson 	INP_WUNLOCK(inp);
669623dce13SRobert Watson 	return (error);
670fb59c426SYoshinobu Inoue }
671fb59c426SYoshinobu Inoue #endif /* INET6 */
672fb59c426SYoshinobu Inoue 
6732c37256eSGarrett Wollman /*
6742c37256eSGarrett Wollman  * Initiate disconnect from peer.
6752c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6762c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6772c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6782c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6792c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6802c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6812c37256eSGarrett Wollman  *
6822c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6832c37256eSGarrett Wollman  */
6842c37256eSGarrett Wollman static int
6852c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6862c37256eSGarrett Wollman {
687f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
688623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6896573d758SMatt Macy 	struct epoch_tracker et;
690623dce13SRobert Watson 	int error = 0;
6912c37256eSGarrett Wollman 
69297a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
693623dce13SRobert Watson 	inp = sotoinpcb(so);
694623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6958501a69cSRobert Watson 	INP_WLOCK(inp);
696489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
697c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
698c2399dd2SMichael Tuexen 		NET_EPOCH_EXIT(et);
699c2399dd2SMichael Tuexen 		return (ECONNRESET);
700623dce13SRobert Watson 	}
701623dce13SRobert Watson 	tp = intotcpcb(inp);
702c2399dd2SMichael Tuexen 
703cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
704cda6bdbaSJohn Baldwin 		goto out;
705623dce13SRobert Watson 	tcp_disconnect(tp);
706623dce13SRobert Watson out:
70700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_DISCONNECT, error);
7085d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7098501a69cSRobert Watson 	INP_WUNLOCK(inp);
71097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
711623dce13SRobert Watson 	return (error);
7122c37256eSGarrett Wollman }
7132c37256eSGarrett Wollman 
714b287c6c7SBjoern A. Zeeb #ifdef INET
7152c37256eSGarrett Wollman /*
7168296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7178296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7182c37256eSGarrett Wollman  */
7192c37256eSGarrett Wollman static int
720*cfb1e929SGleb Smirnoff tcp_usr_accept(struct socket *so, struct sockaddr *sa)
7212c37256eSGarrett Wollman {
722c2399dd2SMichael Tuexen 	struct inpcb *inp;
723c2399dd2SMichael Tuexen 	struct tcpcb *tp;
724*cfb1e929SGleb Smirnoff 	int error = 0;
7252c37256eSGarrett Wollman 
726f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
727623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7288501a69cSRobert Watson 	INP_WLOCK(inp);
72953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
730c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
731c2399dd2SMichael Tuexen 		return (ECONNABORTED);
732623dce13SRobert Watson 	}
7331db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
734f76fcf6dSJeffrey Hsu 
735*cfb1e929SGleb Smirnoff 	if (so->so_state & SS_ISDISCONNECTED)
736c2399dd2SMichael Tuexen 		error = ECONNABORTED;
737*cfb1e929SGleb Smirnoff 	else
738*cfb1e929SGleb Smirnoff 		*(struct sockaddr_in *)sa = (struct sockaddr_in ){
739*cfb1e929SGleb Smirnoff 			.sin_family = AF_INET,
740*cfb1e929SGleb Smirnoff 			.sin_len = sizeof(struct sockaddr_in),
741*cfb1e929SGleb Smirnoff 			.sin_port = inp->inp_fport,
742*cfb1e929SGleb Smirnoff 			.sin_addr = inp->inp_faddr,
743*cfb1e929SGleb Smirnoff 		};
74400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7455d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7468501a69cSRobert Watson 	INP_WUNLOCK(inp);
747*cfb1e929SGleb Smirnoff 
748*cfb1e929SGleb Smirnoff 	return (error);
7492c37256eSGarrett Wollman }
750b287c6c7SBjoern A. Zeeb #endif /* INET */
7512c37256eSGarrett Wollman 
752fb59c426SYoshinobu Inoue #ifdef INET6
753fb59c426SYoshinobu Inoue static int
754*cfb1e929SGleb Smirnoff tcp6_usr_accept(struct socket *so, struct sockaddr *sa)
755fb59c426SYoshinobu Inoue {
756c2399dd2SMichael Tuexen 	struct inpcb *inp;
757c2399dd2SMichael Tuexen 	struct tcpcb *tp;
758*cfb1e929SGleb Smirnoff 	int error = 0;
759fb59c426SYoshinobu Inoue 
760f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
761623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
7628501a69cSRobert Watson 	INP_WLOCK(inp);
76353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
764c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
765c2399dd2SMichael Tuexen 		return (ECONNABORTED);
766623dce13SRobert Watson 	}
7671db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
768623dce13SRobert Watson 
769c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
770c2399dd2SMichael Tuexen 		error = ECONNABORTED;
77126ef6ac4SDon Lewis 	} else {
772*cfb1e929SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV4) {
773*cfb1e929SGleb Smirnoff 			struct sockaddr_in sin = {
774*cfb1e929SGleb Smirnoff 				.sin_family = AF_INET,
775*cfb1e929SGleb Smirnoff 				.sin_len = sizeof(struct sockaddr_in),
776*cfb1e929SGleb Smirnoff 				.sin_port = inp->inp_fport,
777*cfb1e929SGleb Smirnoff 				.sin_addr = inp->inp_faddr,
778*cfb1e929SGleb Smirnoff 			};
779*cfb1e929SGleb Smirnoff 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
780*cfb1e929SGleb Smirnoff 		} else {
781*cfb1e929SGleb Smirnoff 			*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
782*cfb1e929SGleb Smirnoff 				.sin6_family = AF_INET6,
783*cfb1e929SGleb Smirnoff 				.sin6_len = sizeof(struct sockaddr_in6),
784*cfb1e929SGleb Smirnoff 				.sin6_port = inp->inp_fport,
785*cfb1e929SGleb Smirnoff 				.sin6_addr = inp->in6p_faddr,
786*cfb1e929SGleb Smirnoff 			};
787*cfb1e929SGleb Smirnoff 			/* XXX: should catch errors */
788*cfb1e929SGleb Smirnoff 			(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
789*cfb1e929SGleb Smirnoff 		}
79026ef6ac4SDon Lewis 	}
79126ef6ac4SDon Lewis 
79200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7935d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7948501a69cSRobert Watson 	INP_WUNLOCK(inp);
795*cfb1e929SGleb Smirnoff 
796*cfb1e929SGleb Smirnoff 	return (error);
797fb59c426SYoshinobu Inoue }
798fb59c426SYoshinobu Inoue #endif /* INET6 */
799f76fcf6dSJeffrey Hsu 
800f76fcf6dSJeffrey Hsu /*
8012c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8022c37256eSGarrett Wollman  */
8032c37256eSGarrett Wollman static int
8042c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8052c37256eSGarrett Wollman {
8062c37256eSGarrett Wollman 	int error = 0;
807f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
808c2399dd2SMichael Tuexen 	struct tcpcb *tp;
8096573d758SMatt Macy 	struct epoch_tracker et;
8102c37256eSGarrett Wollman 
811623dce13SRobert Watson 	inp = sotoinpcb(so);
812623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8138501a69cSRobert Watson 	INP_WLOCK(inp);
81453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
8150af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
8160af4ce45SGleb Smirnoff 		return (ECONNRESET);
817623dce13SRobert Watson 	}
8180af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
819c2399dd2SMichael Tuexen 
8200af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8212c37256eSGarrett Wollman 	socantsendmore(so);
822623dce13SRobert Watson 	tcp_usrclosed(tp);
823ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
824f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
82500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8265d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
827f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
82897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
829623dce13SRobert Watson 
830623dce13SRobert Watson 	return (error);
8312c37256eSGarrett Wollman }
8322c37256eSGarrett Wollman 
8332c37256eSGarrett Wollman /*
8342c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8352c37256eSGarrett Wollman  */
8362c37256eSGarrett Wollman static int
8372c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8382c37256eSGarrett Wollman {
839109eb549SGleb Smirnoff 	struct epoch_tracker et;
840f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
841c2399dd2SMichael Tuexen 	struct tcpcb *tp;
842f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8432c37256eSGarrett Wollman 
844623dce13SRobert Watson 	inp = sotoinpcb(so);
845623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8468501a69cSRobert Watson 	INP_WLOCK(inp);
84753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
84837a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
84937a7f557SGleb Smirnoff 		return (ECONNRESET);
850623dce13SRobert Watson 	}
85137a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
852c2399dd2SMichael Tuexen 
85337a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
854281a0fd4SPatrick Kelsey 	/*
855281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
856281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
857281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
858281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
859281a0fd4SPatrick Kelsey 	 * expires.
860281a0fd4SPatrick Kelsey 	 */
86168bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
862281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
863281a0fd4SPatrick Kelsey 		goto out;
86409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
86509fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
86609fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
867460cf046SNavdeep Parhar 	else
86809fe6320SNavdeep Parhar #endif
869f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
870623dce13SRobert Watson out:
87100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
8725d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
873f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
874f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
875623dce13SRobert Watson 	return (error);
8762c37256eSGarrett Wollman }
8772c37256eSGarrett Wollman 
8782c37256eSGarrett Wollman /*
8792c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8809c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8819c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8829c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8839c9906e9SPeter Wemm  * generally are caller-frees.
8842c37256eSGarrett Wollman  */
8852c37256eSGarrett Wollman static int
88657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
887b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8882c37256eSGarrett Wollman {
88997a95ee1SGleb Smirnoff 	struct epoch_tracker et;
8902c37256eSGarrett Wollman 	int error = 0;
891f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
892c2399dd2SMichael Tuexen 	struct tcpcb *tp;
893888973f5SMichael Tuexen #ifdef INET
89451e08d53SMichael Tuexen #ifdef INET6
89551e08d53SMichael Tuexen 	struct sockaddr_in sin;
89651e08d53SMichael Tuexen #endif
89751e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
898888973f5SMichael Tuexen #endif
899fb59c426SYoshinobu Inoue #ifdef INET6
900a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
901fb59c426SYoshinobu Inoue 	int isipv6;
902fb59c426SYoshinobu Inoue #endif
9034a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9044a91aa8fSMichael Tuexen 	u_char vflagsav;
9054a91aa8fSMichael Tuexen 	bool restoreflags;
9062c37256eSGarrett Wollman 
9074287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9084287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9094287aa56SGleb Smirnoff 	INP_WLOCK(inp);
91053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9114287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9124287aa56SGleb Smirnoff 			m_freem(m);
9134287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9144287aa56SGleb Smirnoff 		return (ECONNRESET);
9154287aa56SGleb Smirnoff 	}
916c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
9174287aa56SGleb Smirnoff 
9184287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9194287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9204287aa56SGleb Smirnoff 	restoreflags = false;
9214287aa56SGleb Smirnoff 
9224287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
923c2399dd2SMichael Tuexen 	if (control != NULL) {
924c2399dd2SMichael Tuexen 		/* TCP doesn't do control messages (rights, creds, etc) */
925c2399dd2SMichael Tuexen 		if (control->m_len > 0) {
926c2399dd2SMichael Tuexen 			m_freem(control);
927c2399dd2SMichael Tuexen 			error = EINVAL;
928c2399dd2SMichael Tuexen 			goto out;
929c2399dd2SMichael Tuexen 		}
930c2399dd2SMichael Tuexen 		m_freem(control);	/* empty control, just free it */
931c2399dd2SMichael Tuexen 	}
932c2399dd2SMichael Tuexen 
9337d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9347d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
935d3b6c96bSRandall Stewart 		goto out;
9367d2608a5SMark Johnston 
937888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
938bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
939bd4a39ccSMark Johnston 			error = EINVAL;
940bd4a39ccSMark Johnston 			goto out;
941bd4a39ccSMark Johnston 		}
942888973f5SMichael Tuexen 		switch (nam->sa_family) {
943888973f5SMichael Tuexen #ifdef INET
944888973f5SMichael Tuexen 		case AF_INET:
945888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
946888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
947888973f5SMichael Tuexen 				error = EINVAL;
948888973f5SMichael Tuexen 				goto out;
949888973f5SMichael Tuexen 			}
950888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
951888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
952888973f5SMichael Tuexen 				goto out;
953888973f5SMichael Tuexen 			}
954888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
955888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
956888973f5SMichael Tuexen 				goto out;
957888973f5SMichael Tuexen 			}
958f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
959f903a308SMichael Tuexen 				error = EACCES;
9602cf21ae5SRandall Stewart 				goto out;
9612cf21ae5SRandall Stewart 			}
962888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9637d2608a5SMark Johnston 			    &sinp->sin_addr)))
964888973f5SMichael Tuexen 				goto out;
965888973f5SMichael Tuexen #ifdef INET6
966888973f5SMichael Tuexen 			isipv6 = 0;
967888973f5SMichael Tuexen #endif
968888973f5SMichael Tuexen 			break;
969888973f5SMichael Tuexen #endif /* INET */
970888973f5SMichael Tuexen #ifdef INET6
971888973f5SMichael Tuexen 		case AF_INET6:
9720ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
9730ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
974888973f5SMichael Tuexen 				error = EINVAL;
975888973f5SMichael Tuexen 				goto out;
976888973f5SMichael Tuexen 			}
977e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
978e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
979e240ce42SMichael Tuexen 				goto out;
980e240ce42SMichael Tuexen 			}
9810ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
982888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
983888973f5SMichael Tuexen 				goto out;
984888973f5SMichael Tuexen 			}
9850ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
986888973f5SMichael Tuexen #ifdef INET
987888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
988888973f5SMichael Tuexen 					error = EINVAL;
989888973f5SMichael Tuexen 					goto out;
990888973f5SMichael Tuexen 				}
991888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
992888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
993888973f5SMichael Tuexen 					goto out;
994888973f5SMichael Tuexen 				}
9954a91aa8fSMichael Tuexen 				restoreflags = true;
996888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
997888973f5SMichael Tuexen 				sinp = &sin;
9980ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
999888973f5SMichael Tuexen 				if (IN_MULTICAST(
1000888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1001888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1002888973f5SMichael Tuexen 					goto out;
1003888973f5SMichael Tuexen 				}
1004888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10057d2608a5SMark Johnston 				    &sinp->sin_addr)))
1006888973f5SMichael Tuexen 					goto out;
1007888973f5SMichael Tuexen 				isipv6 = 0;
1008888973f5SMichael Tuexen #else /* !INET */
1009888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1010888973f5SMichael Tuexen 				goto out;
1011888973f5SMichael Tuexen #endif /* INET */
1012888973f5SMichael Tuexen 			} else {
1013888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1014888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1015888973f5SMichael Tuexen 					goto out;
1016888973f5SMichael Tuexen 				}
10174a91aa8fSMichael Tuexen 				restoreflags = true;
1018888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1019888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1020888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10217d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1022888973f5SMichael Tuexen 					goto out;
1023888973f5SMichael Tuexen 				isipv6 = 1;
1024888973f5SMichael Tuexen 			}
1025888973f5SMichael Tuexen 			break;
1026888973f5SMichael Tuexen #endif /* INET6 */
1027888973f5SMichael Tuexen 		default:
1028888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1029888973f5SMichael Tuexen 			goto out;
1030888973f5SMichael Tuexen 		}
1031888973f5SMichael Tuexen 	}
10322c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
103308af8aacSRandall Stewart 		if (tp->t_acktime == 0)
103408af8aacSRandall Stewart 			tp->t_acktime = ticks;
1035651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10367d2608a5SMark Johnston 		m = NULL;
10372c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1038bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1039bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1040bd4a39ccSMark Johnston 
10412c37256eSGarrett Wollman 			/*
10422c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10432c37256eSGarrett Wollman 			 * initialize window to default value, and
10440c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10452c37256eSGarrett Wollman 			 */
1046fb59c426SYoshinobu Inoue #ifdef INET6
1047fb59c426SYoshinobu Inoue 			if (isipv6)
1048a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1049fb59c426SYoshinobu Inoue #endif /* INET6 */
1050b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1051b287c6c7SBjoern A. Zeeb 			else
1052b287c6c7SBjoern A. Zeeb #endif
1053b287c6c7SBjoern A. Zeeb #ifdef INET
1054a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1055b287c6c7SBjoern A. Zeeb #endif
10564a91aa8fSMichael Tuexen 			/*
10574a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10584a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10594a91aa8fSMichael Tuexen 			 * operations fail.
10604a91aa8fSMichael Tuexen 			 */
10614a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10624a91aa8fSMichael Tuexen 				restoreflags = false;
10634a91aa8fSMichael Tuexen 
10647d2608a5SMark Johnston 			if (error) {
10657d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10667d2608a5SMark Johnston 				sbflush(&so->so_snd);
10672c37256eSGarrett Wollman 				goto out;
10687d2608a5SMark Johnston 			}
1069c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1070c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
107118a75309SPatrick Kelsey 			else {
10722c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10732c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10742c37256eSGarrett Wollman 			}
1075c560df6fSPatrick Kelsey 		}
10762c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
10772c37256eSGarrett Wollman 			/*
10782c37256eSGarrett Wollman 			 * Close the send side of the connection after
10792c37256eSGarrett Wollman 			 * the data is sent.
10802c37256eSGarrett Wollman 			 */
10812c37256eSGarrett Wollman 			socantsendmore(so);
1082623dce13SRobert Watson 			tcp_usrclosed(tp);
10832c37256eSGarrett Wollman 		}
1084e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1085e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1086e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1087e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1088e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1089e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1090e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1091e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1092e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1093e854dd38SRandall Stewart 		}
10942cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
10952cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1096b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1097b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1098f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1099b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1100b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1101b0acefa8SBill Fenner 		}
11022c37256eSGarrett Wollman 	} else {
1103623dce13SRobert Watson 		/*
1104623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1105623dce13SRobert Watson 		 */
1106d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11072c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1108d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11092c37256eSGarrett Wollman 			error = ENOBUFS;
11102c37256eSGarrett Wollman 			goto out;
11112c37256eSGarrett Wollman 		}
11122c37256eSGarrett Wollman 		/*
11132c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11142c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11152c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11162c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11172c37256eSGarrett Wollman 		 * of data past the urgent section.
11182c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11192c37256eSGarrett Wollman 		 */
112008af8aacSRandall Stewart 		if (tp->t_acktime == 0)
112108af8aacSRandall Stewart 			tp->t_acktime = ticks;
1122651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1123d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
11247d2608a5SMark Johnston 		m = NULL;
1125ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1126ef53690bSGarrett Wollman 			/*
1127ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1128ef53690bSGarrett Wollman 			 * initialize window to default value, and
11290c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1130ef53690bSGarrett Wollman 			 */
113118a75309SPatrick Kelsey 
1132c560df6fSPatrick Kelsey 			/*
1133c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1134c560df6fSPatrick Kelsey 			 */
1135c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1136c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1137fb59c426SYoshinobu Inoue #ifdef INET6
1138fb59c426SYoshinobu Inoue 			if (isipv6)
1139a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1140fb59c426SYoshinobu Inoue #endif /* INET6 */
1141b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1142b287c6c7SBjoern A. Zeeb 			else
1143b287c6c7SBjoern A. Zeeb #endif
1144b287c6c7SBjoern A. Zeeb #ifdef INET
1145a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1146b287c6c7SBjoern A. Zeeb #endif
11474a91aa8fSMichael Tuexen 			/*
11484a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11494a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11504a91aa8fSMichael Tuexen 			 * operations fail.
11514a91aa8fSMichael Tuexen 			 */
11524a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11534a91aa8fSMichael Tuexen 				restoreflags = false;
11544a91aa8fSMichael Tuexen 
11557d2608a5SMark Johnston 			if (error != 0) {
11567d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11577d2608a5SMark Johnston 				sbflush(&so->so_snd);
1158ef53690bSGarrett Wollman 				goto out;
11597d2608a5SMark Johnston 			}
1160ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1161ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1162623dce13SRobert Watson 		}
1163300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11647d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11652cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1166f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11672cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11682c37256eSGarrett Wollman 		}
11692cbcd3c1SGleb Smirnoff 	}
11702529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11712529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11722529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11732529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11742529f56eSJonathan T. Looney 	    0, NULL, false);
11757d2608a5SMark Johnston 
1176d1401c90SRobert Watson out:
11774a91aa8fSMichael Tuexen 	/*
11787d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
11797d2608a5SMark Johnston 	 * responsible for freeing memory.
11807d2608a5SMark Johnston 	 */
11817d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
11827d2608a5SMark Johnston 		m_freem(m);
11837d2608a5SMark Johnston 
11847d2608a5SMark Johnston 	/*
11854a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
11864a91aa8fSMichael Tuexen 	 * restore the original flags.
11874a91aa8fSMichael Tuexen 	 */
11884a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
11894a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
11904a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
11914a91aa8fSMichael Tuexen 	}
119200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
119300812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
11945d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
11955d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1196f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
119797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
119873fddedaSPeter Grehan 	return (error);
11992c37256eSGarrett Wollman }
12002c37256eSGarrett Wollman 
12012cbcd3c1SGleb Smirnoff static int
12022cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12032cbcd3c1SGleb Smirnoff {
1204109eb549SGleb Smirnoff 	struct epoch_tracker et;
12052cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12062cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12072cbcd3c1SGleb Smirnoff 	int error;
12082cbcd3c1SGleb Smirnoff 
12092cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12102cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
121153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12122cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
121382334850SJohn Baldwin 		mb_free_notready(m, count);
12142cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12152cbcd3c1SGleb Smirnoff 	}
12162cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12172cbcd3c1SGleb Smirnoff 
12182cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12192cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12202cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1221f64dc2abSGleb Smirnoff 	if (error) {
12222cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1223f64dc2abSGleb Smirnoff 		return (error);
1224f64dc2abSGleb Smirnoff 	}
1225f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1226f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1227f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12282cbcd3c1SGleb Smirnoff 
12292cbcd3c1SGleb Smirnoff 	return (error);
12302cbcd3c1SGleb Smirnoff }
12312cbcd3c1SGleb Smirnoff 
12322c37256eSGarrett Wollman /*
1233a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12342c37256eSGarrett Wollman  */
1235ac45e92fSRobert Watson static void
12362c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12372c37256eSGarrett Wollman {
1238f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1239c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12406573d758SMatt Macy 	struct epoch_tracker et;
1241c78cbc7bSRobert Watson 
1242ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1243c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1244c78cbc7bSRobert Watson 
124597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12468501a69cSRobert Watson 	INP_WLOCK(inp);
1247c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1248c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1249c78cbc7bSRobert Watson 
1250c78cbc7bSRobert Watson 	/*
1251a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1252c78cbc7bSRobert Watson 	 */
125353af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1254c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12558fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12568fa799bdSJonathan T. Looney 		if (tp == NULL)
12578fa799bdSJonathan T. Looney 			goto dropped;
125800812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12595d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1260c78cbc7bSRobert Watson 	}
1261ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1262d8596171SGleb Smirnoff 		soref(so);
1263ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1264a152f8a3SRobert Watson 	}
12658501a69cSRobert Watson 	INP_WUNLOCK(inp);
12668fa799bdSJonathan T. Looney dropped:
126797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1268a152f8a3SRobert Watson }
1269a152f8a3SRobert Watson 
1270a152f8a3SRobert Watson /*
1271a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1272a152f8a3SRobert Watson  */
1273a152f8a3SRobert Watson static void
1274a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1275a152f8a3SRobert Watson {
1276a152f8a3SRobert Watson 	struct inpcb *inp;
1277c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12786573d758SMatt Macy 	struct epoch_tracker et;
1279a152f8a3SRobert Watson 
1280a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1281a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1282a152f8a3SRobert Watson 
128397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12848501a69cSRobert Watson 	INP_WLOCK(inp);
1285a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1286a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1287a152f8a3SRobert Watson 
1288a152f8a3SRobert Watson 	/*
1289cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1290a152f8a3SRobert Watson 	 * a disconnect.
1291a152f8a3SRobert Watson 	 */
129253af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1293a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1294cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
129574703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1296a152f8a3SRobert Watson 			tcp_disconnect(tp);
129700812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
12985d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1299a152f8a3SRobert Watson 		}
1300cda6bdbaSJohn Baldwin 	}
1301ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1302d8596171SGleb Smirnoff 		soref(so);
1303ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1304a152f8a3SRobert Watson 	}
13058501a69cSRobert Watson 	INP_WUNLOCK(inp);
130697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13072c37256eSGarrett Wollman }
13082c37256eSGarrett Wollman 
1309d3b6c96bSRandall Stewart static int
1310d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1311d3b6c96bSRandall Stewart {
1312d3b6c96bSRandall Stewart 	/*
1313d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1314d3b6c96bSRandall Stewart 	 * specified then it does not always support
1315d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1316d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1317d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1318d3b6c96bSRandall Stewart 	 */
1319d3b6c96bSRandall Stewart 	int ret = 0;
1320d3b6c96bSRandall Stewart 
1321d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1322d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1323d3b6c96bSRandall Stewart 	}
1324d3b6c96bSRandall Stewart 	return (ret);
1325d3b6c96bSRandall Stewart }
1326d3b6c96bSRandall Stewart 
13272c37256eSGarrett Wollman /*
13282c37256eSGarrett Wollman  * Receive out-of-band data.
13292c37256eSGarrett Wollman  */
13302c37256eSGarrett Wollman static int
13312c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13322c37256eSGarrett Wollman {
13332c37256eSGarrett Wollman 	int error = 0;
1334f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1335c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13362c37256eSGarrett Wollman 
1337623dce13SRobert Watson 	inp = sotoinpcb(so);
1338623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13398501a69cSRobert Watson 	INP_WLOCK(inp);
134053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1341c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
1342c2399dd2SMichael Tuexen 		return (ECONNRESET);
1343623dce13SRobert Watson 	}
1344623dce13SRobert Watson 	tp = intotcpcb(inp);
1345c2399dd2SMichael Tuexen 
1346d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1347d3b6c96bSRandall Stewart 	if (error) {
1348d3b6c96bSRandall Stewart 		goto out;
1349d3b6c96bSRandall Stewart 	}
13502c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1351c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13524cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13534cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13542c37256eSGarrett Wollman 		error = EINVAL;
13552c37256eSGarrett Wollman 		goto out;
13562c37256eSGarrett Wollman 	}
13572c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13582c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13592c37256eSGarrett Wollman 		goto out;
13602c37256eSGarrett Wollman 	}
13612c37256eSGarrett Wollman 	m->m_len = 1;
13622c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13632c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13642c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1365623dce13SRobert Watson 
1366623dce13SRobert Watson out:
136700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
13685d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13698501a69cSRobert Watson 	INP_WUNLOCK(inp);
1370623dce13SRobert Watson 	return (error);
13712c37256eSGarrett Wollman }
13722c37256eSGarrett Wollman 
1373b287c6c7SBjoern A. Zeeb #ifdef INET
1374e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1375e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1376e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1377e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1378e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1379e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1380e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1381e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1382e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1383e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1384e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1385e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1386e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1387e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1388e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1389e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1390e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1391e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1392e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1393e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1394e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1395e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1396e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1397e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
13982c37256eSGarrett Wollman };
1399b287c6c7SBjoern A. Zeeb #endif /* INET */
1400df8bae1dSRodney W. Grimes 
1401fb59c426SYoshinobu Inoue #ifdef INET6
1402e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1403e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1404e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1405e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1406e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1407e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1408e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1409e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1410e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1411e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1412e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1413e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1414e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1415e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1416e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1417e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1418e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1419e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1420e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1421e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1422e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1423e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1424e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1425e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1426fb59c426SYoshinobu Inoue };
1427fb59c426SYoshinobu Inoue #endif /* INET6 */
1428fb59c426SYoshinobu Inoue 
1429b287c6c7SBjoern A. Zeeb #ifdef INET
1430a0292f23SGarrett Wollman /*
1431a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1432dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1433dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1434dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1435a0292f23SGarrett Wollman  */
14360312fbe9SPoul-Henning Kamp static int
1437a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1438a0292f23SGarrett Wollman {
14399e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14409eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1441c3229e05SDavid Greenman 	int error;
1442a0292f23SGarrett Wollman 
1443c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14448501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
144576f1499fSGleb Smirnoff 
1446636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1447de0a2eb2SMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1448de0a2eb2SMark Johnston 	    SS_ISDISCONNECTED)) != 0))
1449636b19eaSMark Johnston 		return (EISCONN);
1450636b19eaSMark Johnston 
1451fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1452dfc4d218SGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred, true);
14539e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1454dfc4d218SGleb Smirnoff 	if (error != 0)
14559e46ff4dSGleb Smirnoff 		return (error);
1456a0292f23SGarrett Wollman 
1457087b55eaSAndre Oppermann 	/*
1458087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1459087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1460087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1461087b55eaSAndre Oppermann 	 */
1462a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14639b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1464a0292f23SGarrett Wollman 		tp->request_r_scale++;
1465a0292f23SGarrett Wollman 
1466a0292f23SGarrett Wollman 	soisconnecting(so);
146778b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
146857f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14698e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14708e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14718e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1472a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1473a45d2726SAndras Olah 
14749e46ff4dSGleb Smirnoff 	return (0);
1475a0292f23SGarrett Wollman }
1476b287c6c7SBjoern A. Zeeb #endif /* INET */
1477a0292f23SGarrett Wollman 
1478fb59c426SYoshinobu Inoue #ifdef INET6
1479fb59c426SYoshinobu Inoue static int
1480a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1481fb59c426SYoshinobu Inoue {
14829eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1483636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1484fb59c426SYoshinobu Inoue 	int error;
1485fb59c426SYoshinobu Inoue 
1486775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
14878501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1488623dce13SRobert Watson 
1489636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1490636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1491636b19eaSMark Johnston 		return (EISCONN);
1492636b19eaSMark Johnston 
149376f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1494a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1495fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
149676f1499fSGleb Smirnoff 	if (error != 0)
149776f1499fSGleb Smirnoff 		return (error);
1498fb59c426SYoshinobu Inoue 
1499fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1500fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1501970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1502fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1503fb59c426SYoshinobu Inoue 
1504636b19eaSMark Johnston 	soisconnecting(so);
150578b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
150657f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15078e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15088e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15098e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1510fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1511fb59c426SYoshinobu Inoue 
151276f1499fSGleb Smirnoff 	return (0);
1513fb59c426SYoshinobu Inoue }
1514fb59c426SYoshinobu Inoue #endif /* INET6 */
1515fb59c426SYoshinobu Inoue 
1516cfe8b629SGarrett Wollman /*
1517b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1518b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1519b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1520b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1521b8af5dfaSRobert Watson  * from Linux.
1522b8af5dfaSRobert Watson  */
15238c6104c4SMarius Strobl void
15248c6104c4SMarius Strobl tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1525b8af5dfaSRobert Watson {
1526b8af5dfaSRobert Watson 
15278c6104c4SMarius Strobl 	INP_LOCK_ASSERT(tptoinpcb(tp));
1528b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1529b8af5dfaSRobert Watson 
1530b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1531b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1532b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15333529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1534b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1535b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1536b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1537b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1538b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1539b8af5dfaSRobert Watson 	}
154004682968SRichard Scheffenegger 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
154104682968SRichard Scheffenegger 		case TF2_ECN_PERMIT:
15425a17b6adSMichael Tuexen 			ti->tcpi_options |= TCPI_OPT_ECN;
154304682968SRichard Scheffenegger 			break;
154404682968SRichard Scheffenegger 		case TF2_ACE_PERMIT:
154504682968SRichard Scheffenegger 			/* FALLTHROUGH */
154604682968SRichard Scheffenegger 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
154704682968SRichard Scheffenegger 			ti->tcpi_options |= TCPI_OPT_ACE;
154804682968SRichard Scheffenegger 			break;
154904682968SRichard Scheffenegger 		default:
155004682968SRichard Scheffenegger 			break;
155104682968SRichard Scheffenegger 	}
155204682968SRichard Scheffenegger 	if (IS_FASTOPEN(tp->t_flags))
155304682968SRichard Scheffenegger 		ti->tcpi_options |= TCPI_OPT_TFO;
15541baaf834SBruce M Simpson 
155543d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15563ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15571baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15581baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15591baaf834SBruce M Simpson 
1560b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1561b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1562b8af5dfaSRobert Watson 
1563b8af5dfaSRobert Watson 	/*
1564b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1565b8af5dfaSRobert Watson 	 */
1566c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1567535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1568b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15691c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1570535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
157143d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
157243d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1573f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1574f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1575f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1576dc485b96SMarius Strobl 	ti->tcpi_snd_una = tp->snd_una;
1577dc485b96SMarius Strobl 	ti->tcpi_snd_max = tp->snd_max;
1578dc485b96SMarius Strobl 	ti->tcpi_rcv_numsacks = tp->rcv_numsacks;
1579dc485b96SMarius Strobl 	ti->tcpi_rcv_adv = tp->rcv_adv;
1580dc485b96SMarius Strobl 	ti->tcpi_dupacks = tp->t_dupacks;
1581a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1582a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1583a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1584a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1585a6456410SNavdeep Parhar 	}
1586a6456410SNavdeep Parhar #endif
158722c81cc5SRichard Scheffenegger 	/*
158822c81cc5SRichard Scheffenegger 	 * AccECN related counters.
158922c81cc5SRichard Scheffenegger 	 */
159022c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
159122c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
159222c81cc5SRichard Scheffenegger 		/*
159322c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
159422c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
159522c81cc5SRichard Scheffenegger 		 */
159622c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
159722c81cc5SRichard Scheffenegger 	else
159822c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
159922c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1600b8af5dfaSRobert Watson }
1601b8af5dfaSRobert Watson 
1602b8af5dfaSRobert Watson /*
16031e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16041e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16051e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16061e8f5ffaSRobert Watson  * option.
1607cfe8b629SGarrett Wollman  */
1608bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16098501a69cSRobert Watson 	INP_WLOCK(inp);							\
161053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16118501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1612bac5bedfSConrad Meyer 		cleanup;						\
16131e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16141e8f5ffaSRobert Watson 	}								\
16151e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16161e8f5ffaSRobert Watson } while(0)
1617bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16181e8f5ffaSRobert Watson 
1619fd7daa72SMichael Tuexen int
1620fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1621df8bae1dSRodney W. Grimes {
1622fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1623fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1624fc4d53ccSGleb Smirnoff 	int error = 0;
1625df8bae1dSRodney W. Grimes 
1626fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16273b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
162853af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1629fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1630fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1631fc4d53ccSGleb Smirnoff 
1632cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16333b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1634fb59c426SYoshinobu Inoue #ifdef INET6
1635de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1636fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1637de156263SGleb Smirnoff #endif
1638de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1639de156263SGleb Smirnoff 		else
1640de156263SGleb Smirnoff #endif
1641de156263SGleb Smirnoff #ifdef INET
1642fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1643de156263SGleb Smirnoff #endif
16445dff1c38SMichael Tuexen 		/*
1645de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1646de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1647de156263SGleb Smirnoff 		 * IP level returned.
16485dff1c38SMichael Tuexen 		 */
1649de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1650de156263SGleb Smirnoff #ifdef INET6
1651de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1652de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1653de156263SGleb Smirnoff 				return (error);
1654de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1655de156263SGleb Smirnoff 			case IPV6_TCLASS:
1656de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1657de156263SGleb Smirnoff 				break;
1658de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1659f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1660f581a26eSGleb Smirnoff 				break;
1661de156263SGleb Smirnoff 			default:
1662de156263SGleb Smirnoff 				return (error);
16635dff1c38SMichael Tuexen 			}
1664de156263SGleb Smirnoff 			break;
1665b287c6c7SBjoern A. Zeeb #endif
1666b287c6c7SBjoern A. Zeeb #ifdef INET
1667de156263SGleb Smirnoff 		case IPPROTO_IP:
1668de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1669de156263SGleb Smirnoff 			case IP_TOS:
16703b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
16713b0ee680SRichard Scheffenegger 				break;
1672de156263SGleb Smirnoff 			case IP_TTL:
1673de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1674de156263SGleb Smirnoff 				break;
1675de156263SGleb Smirnoff 			default:
1676df8bae1dSRodney W. Grimes 				return (error);
1677de156263SGleb Smirnoff 			}
1678de156263SGleb Smirnoff 			break;
1679de156263SGleb Smirnoff #endif
1680de156263SGleb Smirnoff 		default:
1681de156263SGleb Smirnoff 			return (error);
1682de156263SGleb Smirnoff 		}
16833b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
168453af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
16853b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
16863b3c08c1SMichael Tuexen 			return (ECONNRESET);
16873b3c08c1SMichael Tuexen 		}
1688fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1689fc4d53ccSGleb Smirnoff 		/*
1690fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1691fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1692fc4d53ccSGleb Smirnoff 		 */
1693fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1694fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
169573ee5756SRandall Stewart 		void *ptr = NULL;
1696fc4d53ccSGleb Smirnoff 
16973b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1698fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1699fc4d53ccSGleb Smirnoff 		if (error)
1700fc4d53ccSGleb Smirnoff 			return (error);
1701fc4d53ccSGleb Smirnoff 
170268cea2b1SJohn Baldwin 		INP_WLOCK(inp);
170355bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1704fc4d53ccSGleb Smirnoff 
170555bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
170655bceb1eSRandall Stewart 		if (blk == NULL) {
170755bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
170855bceb1eSRandall Stewart 			return (ENOENT);
170955bceb1eSRandall Stewart 		}
1710587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1711587d67c0SRandall Stewart 			/* You already have this */
1712587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1713587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1714587d67c0SRandall Stewart 			return (0);
1715587d67c0SRandall Stewart 		}
1716587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1717587d67c0SRandall Stewart 			/*
1718587d67c0SRandall Stewart 			 * The user has advanced the state
1719587d67c0SRandall Stewart 			 * past the initial point, we may not
1720587d67c0SRandall Stewart 			 * be able to switch.
1721587d67c0SRandall Stewart 			 */
1722587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1723587d67c0SRandall Stewart 				/*
1724587d67c0SRandall Stewart 				 * Does the stack provide a
1725587d67c0SRandall Stewart 				 * query mechanism, if so it may
1726587d67c0SRandall Stewart 				 * still be possible?
1727587d67c0SRandall Stewart 				 */
1728587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1729c6c0be27SMichael Tuexen 			} else
1730c6c0be27SMichael Tuexen 				error = EINVAL;
1731587d67c0SRandall Stewart 			if (error) {
1732587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1733587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1734587d67c0SRandall Stewart 				return(error);
1735587d67c0SRandall Stewart 			}
1736587d67c0SRandall Stewart 		}
173755bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
173855bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
173955bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
174055bceb1eSRandall Stewart 			return (ENOENT);
174155bceb1eSRandall Stewart 		}
174255bceb1eSRandall Stewart 		/*
174373ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
174473ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
174555bceb1eSRandall Stewart 		 */
174673ee5756SRandall Stewart #ifdef TCPHPTS
174773ee5756SRandall Stewart 		/* Assure that we are not on any hpts */
1748c2a69e84SGleb Smirnoff 		tcp_hpts_remove(tp);
174973ee5756SRandall Stewart #endif
175073ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
175173ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
175273ee5756SRandall Stewart 			if (error) {
175373ee5756SRandall Stewart 				/*
175473ee5756SRandall Stewart 				 * Release the ref count the lookup
175573ee5756SRandall Stewart 				 * acquired.
175673ee5756SRandall Stewart 				 */
175773ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
175873ee5756SRandall Stewart 				/*
175973ee5756SRandall Stewart 				 * Now there is a chance that the
176073ee5756SRandall Stewart 				 * init() function mucked with some
176173ee5756SRandall Stewart 				 * things before it failed, such as
176273ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
176373ee5756SRandall Stewart 				 * It should not of, but lets give the old
176473ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
176573ee5756SRandall Stewart 				 */
176673ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
176773ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
176873ee5756SRandall Stewart 				}
176973ee5756SRandall Stewart 			 	goto err_out;
177073ee5756SRandall Stewart 			}
177173ee5756SRandall Stewart 		}
1772587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1773086a3556SAndrew Gallatin 			struct epoch_tracker et;
1774587d67c0SRandall Stewart 			/*
1775587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1776587d67c0SRandall Stewart 			 * the tcb is not going away.
1777587d67c0SRandall Stewart 			 */
1778086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1779587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1780086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1781587d67c0SRandall Stewart 		}
178273ee5756SRandall Stewart 		/*
178373ee5756SRandall Stewart 		 * Release the old refcnt, the
178473ee5756SRandall Stewart 		 * lookup acquired a ref on the
178573ee5756SRandall Stewart 		 * new one already.
178673ee5756SRandall Stewart 		 */
178755bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
178873ee5756SRandall Stewart 		/*
178973ee5756SRandall Stewart 		 * Set in the new stack.
179073ee5756SRandall Stewart 		 */
179155bceb1eSRandall Stewart 		tp->t_fb = blk;
179273ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
179355bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
179455bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
179555bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
179655bceb1eSRandall Stewart 			     sopt->sopt_name);
179755bceb1eSRandall Stewart 		}
179855bceb1eSRandall Stewart #endif
17993ee9c3c4SRandall Stewart err_out:
180055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
180155bceb1eSRandall Stewart 		return (error);
180273ee5756SRandall Stewart 
1803fc4d53ccSGleb Smirnoff 	}
1804fc4d53ccSGleb Smirnoff 
18053b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
180666fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1807fc4d53ccSGleb Smirnoff }
1808fc4d53ccSGleb Smirnoff 
1809fc4d53ccSGleb Smirnoff static int
1810fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1811fc4d53ccSGleb Smirnoff {
1812fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1813fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1814fc4d53ccSGleb Smirnoff 	int error = 0;
1815fc4d53ccSGleb Smirnoff 
1816fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18173b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
181853af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1819fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1820fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1821fc4d53ccSGleb Smirnoff 
1822fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18233b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1824fc4d53ccSGleb Smirnoff #ifdef INET6
1825fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1826fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1827fc4d53ccSGleb Smirnoff #endif /* INET6 */
1828fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1829fc4d53ccSGleb Smirnoff 		else
1830fc4d53ccSGleb Smirnoff #endif
1831fc4d53ccSGleb Smirnoff #ifdef INET
1832fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1833fc4d53ccSGleb Smirnoff #endif
1834fc4d53ccSGleb Smirnoff 		return (error);
1835fc4d53ccSGleb Smirnoff 	}
1836fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1837e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1838fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1839fc4d53ccSGleb Smirnoff 
1840e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1841e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1842e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1843e2833083SPeter Lei 		} else {
1844e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1845e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1846c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1847c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1848e2833083SPeter Lei 		}
184955bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
185055bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
185155bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
185255bceb1eSRandall Stewart 		return (error);
185355bceb1eSRandall Stewart 	}
1854fc4d53ccSGleb Smirnoff 
18553b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
185666fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1857fc4d53ccSGleb Smirnoff }
1858fc4d53ccSGleb Smirnoff 
1859fc4d53ccSGleb Smirnoff int
1860fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1861fc4d53ccSGleb Smirnoff {
1862fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1863fc4d53ccSGleb Smirnoff 
1864fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1865fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1866fc4d53ccSGleb Smirnoff 
18673b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
186853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18693b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18703b3c08c1SMichael Tuexen 		return (ECONNRESET);
18713b3c08c1SMichael Tuexen 	}
1872fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1873fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1874fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1875fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1876fc4d53ccSGleb Smirnoff 	else
1877fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
187855bceb1eSRandall Stewart }
187955bceb1eSRandall Stewart 
18802529f56eSJonathan T. Looney /*
18812529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18822529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18832529f56eSJonathan T. Looney  */
18842529f56eSJonathan T. Looney #ifdef CTASSERT
18852529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18862529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18872529f56eSJonathan T. Looney #endif
18882529f56eSJonathan T. Looney 
1889ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1890ec1db6e1SJohn Baldwin static int
1891ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1892ec1db6e1SJohn Baldwin {
1893ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1894ec1db6e1SJohn Baldwin 	int error;
1895ec1db6e1SJohn Baldwin 
1896ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1897ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1898ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1899ec1db6e1SJohn Baldwin 		if (error)
1900ec1db6e1SJohn Baldwin 			return (error);
1901ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1902ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1903ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1904ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1905ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1906ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1907ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1908ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1909ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1910ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1911ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1912ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1913ec1db6e1SJohn Baldwin 		return (0);
1914ec1db6e1SJohn Baldwin 	}
1915ec1db6e1SJohn Baldwin 
1916ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1917ec1db6e1SJohn Baldwin }
1918ec1db6e1SJohn Baldwin #endif
1919ec1db6e1SJohn Baldwin 
1920b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1921b8d60729SRandall Stewart 
1922b8d60729SRandall Stewart static int
1923ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1924b8d60729SRandall Stewart {
1925b8d60729SRandall Stewart 	struct cc_algo *algo;
1926b8d60729SRandall Stewart 	void *ptr = NULL;
19273b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1928b8d60729SRandall Stewart 	struct cc_var cc_mem;
1929b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1930b8d60729SRandall Stewart 	size_t mem_sz;
1931b8d60729SRandall Stewart 	int error;
1932b8d60729SRandall Stewart 
1933b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1934b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1935b8d60729SRandall Stewart 	if (error)
1936b8d60729SRandall Stewart 		return(error);
1937b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1938b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1939ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1940b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1941b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1942b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1943b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1944b8d60729SRandall Stewart 				continue;
1945b8d60729SRandall Stewart 			}
1946b8d60729SRandall Stewart 			break;
1947b8d60729SRandall Stewart 		}
1948ea9017fbSRandall Stewart 	}
1949b8d60729SRandall Stewart 	if (algo == NULL) {
1950b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1951b8d60729SRandall Stewart 		return(ESRCH);
1952b8d60729SRandall Stewart 	}
1953ea9017fbSRandall Stewart 	/*
1954ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1955ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1956ea9017fbSRandall Stewart 	 */
1957ea9017fbSRandall Stewart 	cc_refer(algo);
1958ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1959b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1960b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1961b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1962b8d60729SRandall Stewart 		if (mem_sz == 0) {
1963b8d60729SRandall Stewart 			goto no_mem_needed;
1964b8d60729SRandall Stewart 		}
1965b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1966b8d60729SRandall Stewart 	} else {
1967b8d60729SRandall Stewart no_mem_needed:
1968b8d60729SRandall Stewart 		mem_sz = 0;
1969b8d60729SRandall Stewart 		ptr = NULL;
1970b8d60729SRandall Stewart 	}
1971b8d60729SRandall Stewart 	/*
1972b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1973b8d60729SRandall Stewart 	 * back the inplock.
1974b8d60729SRandall Stewart 	 */
1975b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1976df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
197753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1978df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1979ea9017fbSRandall Stewart 		if (ptr)
1980df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1981ea9017fbSRandall Stewart 		/* Release our temp reference */
1982ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1983ea9017fbSRandall Stewart 		cc_release(algo);
1984ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
1985df07bfdaSMichael Tuexen 		return (ECONNRESET);
1986df07bfdaSMichael Tuexen 	}
1987df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
1988df07bfdaSMichael Tuexen 	if (ptr != NULL)
1989b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
1990b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
1991b8d60729SRandall Stewart 	/*
1992b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
1993b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
1994b8d60729SRandall Stewart 	 * Note here we init into stack memory.
1995b8d60729SRandall Stewart 	 */
1996b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
1997b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
1998b8d60729SRandall Stewart 	else
1999b8d60729SRandall Stewart 		error = 0;
2000b8d60729SRandall Stewart 	/*
2001b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
2002b8d60729SRandall Stewart 	 * should not fail we could in theory have a
2003b8d60729SRandall Stewart 	 * KASSERT here.
2004b8d60729SRandall Stewart 	 */
2005b8d60729SRandall Stewart 	if (error == 0) {
2006b8d60729SRandall Stewart 		/*
2007b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
2008b8d60729SRandall Stewart 		 * connection to the new CC module by
2009b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
2010b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
2011b8d60729SRandall Stewart 		 */
2012b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
2013e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2014ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
2015ea9017fbSRandall Stewart 		cc_detach(tp);
2016ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
2017e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
2018ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
2019ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2020b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2021b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2022b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2023e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2024b8d60729SRandall Stewart 		}
2025b8d60729SRandall Stewart 	} else if (ptr)
2026b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2027b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2028ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2029ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2030ea9017fbSRandall Stewart 	cc_release(algo);
2031ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2032b8d60729SRandall Stewart 	return (error);
2033b8d60729SRandall Stewart }
2034b8d60729SRandall Stewart 
203555bceb1eSRandall Stewart int
203666fbc19fSGleb Smirnoff tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
203755bceb1eSRandall Stewart {
203866fbc19fSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
203955bceb1eSRandall Stewart 	int	error, opt, optval;
204055bceb1eSRandall Stewart 	u_int	ui;
204155bceb1eSRandall Stewart 	struct	tcp_info ti;
2042b2e60773SJohn Baldwin #ifdef KERN_TLS
2043b2e60773SJohn Baldwin 	struct tls_enable tls;
2044528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2045b2e60773SJohn Baldwin #endif
20462529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2047adc56f5aSEdward Tomasz Napierala #ifdef STATS
2048adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2049adc56f5aSEdward Tomasz Napierala #endif
2050af6fef3aSGleb Smirnoff 	size_t	len;
2051df8bae1dSRodney W. Grimes 
2052f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
205353af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2054fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2055528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2056f581a26eSGleb Smirnoff 
2057f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2058f581a26eSGleb Smirnoff #ifdef INET6
2059f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2060f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2061f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2062f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2063f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2064f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2065f581a26eSGleb Smirnoff 		}
2066f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2067f581a26eSGleb Smirnoff 		return (0);
2068f581a26eSGleb Smirnoff #endif
2069f581a26eSGleb Smirnoff #ifdef INET
2070f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2071f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2072f581a26eSGleb Smirnoff 		return (0);
2073f581a26eSGleb Smirnoff #endif
2074f581a26eSGleb Smirnoff 	}
2075f581a26eSGleb Smirnoff 
2076d519cedbSGleb Smirnoff 	/*
2077d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2078d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2079d519cedbSGleb Smirnoff 	 */
2080d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2081d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2082d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2083c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2084c8b53cedSMichael Tuexen 			return (EINVAL);
2085af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2086af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2087d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2088d519cedbSGleb Smirnoff 		if (error) {
2089af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2090d519cedbSGleb Smirnoff 			return (error);
2091d519cedbSGleb Smirnoff 		}
2092bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2093d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2094e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2095d519cedbSGleb Smirnoff 		else
2096d519cedbSGleb Smirnoff 			error = ENOENT;
2097d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2098d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2099af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2100af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2101d519cedbSGleb Smirnoff 		return (error);
2102d519cedbSGleb Smirnoff 	}
2103d519cedbSGleb Smirnoff 
2104cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2105cfe8b629SGarrett Wollman 	case SOPT_SET:
2106cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2107fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
210888f6b043SBruce M Simpson 		case TCP_MD5SIG:
21098501a69cSRobert Watson 			INP_WUNLOCK(inp);
211097453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2111fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2112fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
21131cfd4b53SBruce M Simpson 			if (error)
21141e8f5ffaSRobert Watson 				return (error);
211597453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
211609fe6320SNavdeep Parhar 			goto unlock_and_done;
2117fcf59617SAndrey V. Elsukov #endif /* IPSEC */
211809fe6320SNavdeep Parhar 
2119df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2120cfe8b629SGarrett Wollman 		case TCP_NOOPT:
21210471a8c7SRichard Scheffenegger 		case TCP_LRD:
21228501a69cSRobert Watson 			INP_WUNLOCK(inp);
2123cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2124cfe8b629SGarrett Wollman 			    sizeof optval);
2125cfe8b629SGarrett Wollman 			if (error)
21261e8f5ffaSRobert Watson 				return (error);
2127cfe8b629SGarrett Wollman 
21288501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2129cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2130cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2131cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2132cfe8b629SGarrett Wollman 				break;
2133cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2134cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2135cfe8b629SGarrett Wollman 				break;
21360471a8c7SRichard Scheffenegger 			case TCP_LRD:
21370471a8c7SRichard Scheffenegger 				opt = TF_LRD;
21380471a8c7SRichard Scheffenegger 				break;
2139cfe8b629SGarrett Wollman 			default:
2140cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2141cfe8b629SGarrett Wollman 				break;
2142cfe8b629SGarrett Wollman 			}
2143cfe8b629SGarrett Wollman 
2144cfe8b629SGarrett Wollman 			if (optval)
2145cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2146df8bae1dSRodney W. Grimes 			else
2147cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
214809fe6320SNavdeep Parhar unlock_and_done:
214909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
215009fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
215109fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
215209fe6320SNavdeep Parhar 				    sopt->sopt_name);
215309fe6320SNavdeep Parhar 			}
215409fe6320SNavdeep Parhar #endif
21558501a69cSRobert Watson 			INP_WUNLOCK(inp);
2156df8bae1dSRodney W. Grimes 			break;
2157df8bae1dSRodney W. Grimes 
2158007581c0SJonathan Lemon 		case TCP_NOPUSH:
21598501a69cSRobert Watson 			INP_WUNLOCK(inp);
2160007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2161007581c0SJonathan Lemon 			    sizeof optval);
2162007581c0SJonathan Lemon 			if (error)
21631e8f5ffaSRobert Watson 				return (error);
2164007581c0SJonathan Lemon 
21658501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2166007581c0SJonathan Lemon 			if (optval)
2167007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2168d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2169007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2170109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2171109eb549SGleb Smirnoff 					struct epoch_tracker et;
2172109eb549SGleb Smirnoff 
2173109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2174f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2175109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2176109eb549SGleb Smirnoff 				}
2177007581c0SJonathan Lemon 			}
217809fe6320SNavdeep Parhar 			goto unlock_and_done;
2179007581c0SJonathan Lemon 
21809e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21819e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21829e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21839e644c23SMichael Tuexen 			    sizeof optval);
21849e644c23SMichael Tuexen 			if (error)
21859e644c23SMichael Tuexen 				return (error);
21869e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
21879e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
21889e644c23SMichael Tuexen 				/* Its got to be in range */
21899e644c23SMichael Tuexen 				return (EINVAL);
21909e644c23SMichael Tuexen 			}
21919e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
21929e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
21939e644c23SMichael Tuexen 				return (EINVAL);
21949e644c23SMichael Tuexen 			}
21959e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
21969e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
21979e644c23SMichael Tuexen 				/* You can't change after you are connected */
21989e644c23SMichael Tuexen 				error = EINVAL;
21999e644c23SMichael Tuexen 			} else {
22009e644c23SMichael Tuexen 				/* Ok we are all good set the port */
22019e644c23SMichael Tuexen 				tp->t_port = htons(optval);
22029e644c23SMichael Tuexen 			}
22039e644c23SMichael Tuexen 			goto unlock_and_done;
22049e644c23SMichael Tuexen 
2205df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
22068501a69cSRobert Watson 			INP_WUNLOCK(inp);
2207cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2208cfe8b629SGarrett Wollman 			    sizeof optval);
2209cfe8b629SGarrett Wollman 			if (error)
22101e8f5ffaSRobert Watson 				return (error);
2211df8bae1dSRodney W. Grimes 
22128501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
221353369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2214603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2215cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2216a0292f23SGarrett Wollman 			else
2217a0292f23SGarrett Wollman 				error = EINVAL;
221809fe6320SNavdeep Parhar 			goto unlock_and_done;
2219a0292f23SGarrett Wollman 
2220b8af5dfaSRobert Watson 		case TCP_INFO:
22218501a69cSRobert Watson 			INP_WUNLOCK(inp);
2222b8af5dfaSRobert Watson 			error = EINVAL;
2223b8af5dfaSRobert Watson 			break;
2224b8af5dfaSRobert Watson 
2225adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2226adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2227adc56f5aSEdward Tomasz Napierala #ifdef STATS
2228adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2229adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2230adc56f5aSEdward Tomasz Napierala 			if (error)
2231adc56f5aSEdward Tomasz Napierala 				return (error);
2232adc56f5aSEdward Tomasz Napierala 
2233adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2234adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2235adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2236adc56f5aSEdward Tomasz Napierala 			else
2237adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2238adc56f5aSEdward Tomasz Napierala 
2239adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2240adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2241adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2242adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2243adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2244adc56f5aSEdward Tomasz Napierala 				sbp = t;
2245adc56f5aSEdward Tomasz Napierala 			}
2246adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2247adc56f5aSEdward Tomasz Napierala 
2248adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2249adc56f5aSEdward Tomasz Napierala #else
2250adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2251adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2252adc56f5aSEdward Tomasz Napierala 			break;
2253adc56f5aSEdward Tomasz Napierala 
2254dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2255ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
225673e263b1SGleb Smirnoff 			break;
2257dbc42409SLawrence Stewart 
2258a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2259a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2260a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2261a034518aSAndrew Gallatin 			    sizeof(optval));
2262a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2263a034518aSAndrew Gallatin 			if (!error)
2264a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2265a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2266a034518aSAndrew Gallatin 			break;
2267a034518aSAndrew Gallatin 
2268b2e60773SJohn Baldwin #ifdef KERN_TLS
2269b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2270b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2271ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2272b2e60773SJohn Baldwin 			if (error)
2273b2e60773SJohn Baldwin 				break;
2274fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2275b2e60773SJohn Baldwin 			break;
2276b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2277b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2278b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2279b2e60773SJohn Baldwin 			if (error)
2280b2e60773SJohn Baldwin 				return (error);
2281b2e60773SJohn Baldwin 
2282b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2283fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2284b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2285b2e60773SJohn Baldwin 			break;
2286f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2287f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2288f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2289f1f93475SJohn Baldwin 			    sizeof(tls));
2290f1f93475SJohn Baldwin 			if (error)
2291f1f93475SJohn Baldwin 				break;
2292fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2293f1f93475SJohn Baldwin 			break;
2294b2e60773SJohn Baldwin #endif
229508af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
22969077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
22979077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
22989077f387SGleb Smirnoff 		case TCP_KEEPINIT:
22999077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
23009077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
23019077f387SGleb Smirnoff 			if (error)
23029077f387SGleb Smirnoff 				return (error);
23039077f387SGleb Smirnoff 
23049077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
23059077f387SGleb Smirnoff 				error = EINVAL;
23069077f387SGleb Smirnoff 				break;
23079077f387SGleb Smirnoff 			}
23089077f387SGleb Smirnoff 			ui *= hz;
23099077f387SGleb Smirnoff 
23109077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
23119077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
231208af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
231308af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
231408af8aacSRandall Stewart 				break;
231508af8aacSRandall Stewart 
23169077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23179077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23189077f387SGleb Smirnoff 				/*
23199077f387SGleb Smirnoff 				 * XXX: better check current remaining
23209077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
23219077f387SGleb Smirnoff 				 */
23229077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
23239077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
23249077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23259077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23269077f387SGleb Smirnoff 				break;
23279077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23289077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23299077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23309077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23319077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23329077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23339077f387SGleb Smirnoff 				break;
23349077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23359077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23369077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23379077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23389077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23399077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23409077f387SGleb Smirnoff 				break;
23419077f387SGleb Smirnoff 			}
234209fe6320SNavdeep Parhar 			goto unlock_and_done;
23439077f387SGleb Smirnoff 
234485c05144SGleb Smirnoff 		case TCP_KEEPCNT:
234585c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
234685c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
234785c05144SGleb Smirnoff 			if (error)
234885c05144SGleb Smirnoff 				return (error);
234985c05144SGleb Smirnoff 
235085c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
235185c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
235285c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
235385c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
235485c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
235585c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
235685c05144SGleb Smirnoff 			goto unlock_and_done;
235785c05144SGleb Smirnoff 
235886a996e6SHiren Panchasara #ifdef TCPPCAP
235986a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
236086a996e6SHiren Panchasara 		case TCP_PCAP_IN:
236186a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
236286a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
236386a996e6SHiren Panchasara 			    sizeof optval);
236486a996e6SHiren Panchasara 			if (error)
236586a996e6SHiren Panchasara 				return (error);
236686a996e6SHiren Panchasara 
236786a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
236886a996e6SHiren Panchasara 			if (optval >= 0)
2369399a5655SRichard Scheffenegger 				tcp_pcap_set_sock_max(
2370399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
237186a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
237286a996e6SHiren Panchasara 					optval);
237386a996e6SHiren Panchasara 			else
237486a996e6SHiren Panchasara 				error = EINVAL;
237586a996e6SHiren Panchasara 			goto unlock_and_done;
237686a996e6SHiren Panchasara #endif
237786a996e6SHiren Panchasara 
2378c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2379c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2380c560df6fSPatrick Kelsey 
2381281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2382c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2383c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2384281a0fd4SPatrick Kelsey 				return (EPERM);
2385281a0fd4SPatrick Kelsey 
2386c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2387c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2388281a0fd4SPatrick Kelsey 			if (error)
2389281a0fd4SPatrick Kelsey 				return (error);
2390281a0fd4SPatrick Kelsey 
2391281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2392d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2393d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2394d442a657SMichael Tuexen 				error = EINVAL;
2395d442a657SMichael Tuexen 				goto unlock_and_done;
2396d442a657SMichael Tuexen 			}
2397c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2398c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2399c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2400c560df6fSPatrick Kelsey 						error = EPERM;
2401c560df6fSPatrick Kelsey 						goto unlock_and_done;
2402c560df6fSPatrick Kelsey 					}
2403c560df6fSPatrick Kelsey 
2404c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2405281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2406281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2407c560df6fSPatrick Kelsey 				} else {
2408c560df6fSPatrick Kelsey 					/*
2409c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2410c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2411c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2412c560df6fSPatrick Kelsey 					 * connect.
2413c560df6fSPatrick Kelsey 					 */
2414c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2415c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2416c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2417c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2418c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2419c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2420c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2421c560df6fSPatrick Kelsey 					}
2422c560df6fSPatrick Kelsey 				}
2423d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2424281a0fd4SPatrick Kelsey 			} else
2425281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2426281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2427c560df6fSPatrick Kelsey 		}
2428281a0fd4SPatrick Kelsey 
2429e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24302529f56eSJonathan T. Looney 		case TCP_LOG:
24312529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24322529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24332529f56eSJonathan T. Looney 			    sizeof optval);
24342529f56eSJonathan T. Looney 			if (error)
24352529f56eSJonathan T. Looney 				return (error);
24362529f56eSJonathan T. Looney 
24372529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24382529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24392529f56eSJonathan T. Looney 			goto unlock_and_done;
24402529f56eSJonathan T. Looney 
24412529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24422529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24432529f56eSJonathan T. Looney 			error = EINVAL;
24442529f56eSJonathan T. Looney 			break;
24452529f56eSJonathan T. Looney 
24462529f56eSJonathan T. Looney 		case TCP_LOGID:
24472529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24482529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24492529f56eSJonathan T. Looney 			if (error)
24502529f56eSJonathan T. Looney 				break;
24512529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24522529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24532529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24542529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24552529f56eSJonathan T. Looney 			break;
24562529f56eSJonathan T. Looney 
24572529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24582529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24592529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24602529f56eSJonathan T. Looney 			error =
24612529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
24622529f56eSJonathan T. Looney 			if (error)
24632529f56eSJonathan T. Looney 				break;
24642529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24652529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24662529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
24672529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24682529f56eSJonathan T. Looney 				    M_WAITOK, true);
24692529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24702529f56eSJonathan T. Looney 			} else {
24712529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24722529f56eSJonathan T. Looney 				/*
24732529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24742529f56eSJonathan T. Looney 				 * INP lock.
24752529f56eSJonathan T. Looney 				 */
24762529f56eSJonathan T. Looney 			}
24772529f56eSJonathan T. Looney 			break;
2478e24e5683SJonathan T. Looney #endif
24792529f56eSJonathan T. Looney 
2480df8bae1dSRodney W. Grimes 		default:
24818501a69cSRobert Watson 			INP_WUNLOCK(inp);
2482df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2483df8bae1dSRodney W. Grimes 			break;
2484df8bae1dSRodney W. Grimes 		}
2485df8bae1dSRodney W. Grimes 		break;
2486df8bae1dSRodney W. Grimes 
2487cfe8b629SGarrett Wollman 	case SOPT_GET:
24881e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2489cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2490fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
249188f6b043SBruce M Simpson 		case TCP_MD5SIG:
24928501a69cSRobert Watson 			INP_WUNLOCK(inp);
249397453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2494fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2495fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
24961cfd4b53SBruce M Simpson 			break;
2497265ed012SBruce M Simpson #endif
24981e8f5ffaSRobert Watson 
2499df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2500cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
25018501a69cSRobert Watson 			INP_WUNLOCK(inp);
2502b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2503df8bae1dSRodney W. Grimes 			break;
2504df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2505cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
25068501a69cSRobert Watson 			INP_WUNLOCK(inp);
2507b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2508df8bae1dSRodney W. Grimes 			break;
25099e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
25109e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
25119e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
25129e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25139e644c23SMichael Tuexen 			break;
2514a0292f23SGarrett Wollman 		case TCP_NOOPT:
2515cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
25168501a69cSRobert Watson 			INP_WUNLOCK(inp);
2517b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2518a0292f23SGarrett Wollman 			break;
2519a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2520cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
25218501a69cSRobert Watson 			INP_WUNLOCK(inp);
2522b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2523b8af5dfaSRobert Watson 			break;
2524b8af5dfaSRobert Watson 		case TCP_INFO:
2525b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
25268501a69cSRobert Watson 			INP_WUNLOCK(inp);
2527b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2528a0292f23SGarrett Wollman 			break;
2529adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2530adc56f5aSEdward Tomasz Napierala 			{
2531adc56f5aSEdward Tomasz Napierala #ifdef STATS
2532adc56f5aSEdward Tomasz Napierala 			int nheld;
2533adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2534adc56f5aSEdward Tomasz Napierala 
2535adc56f5aSEdward Tomasz Napierala 			error = 0;
2536adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2537adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2538adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2539adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2540adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2541adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2542adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2543adc56f5aSEdward Tomasz Napierala 			else
2544adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2545adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2546adc56f5aSEdward Tomasz Napierala 			if (error)
2547adc56f5aSEdward Tomasz Napierala 				break;
2548adc56f5aSEdward Tomasz Napierala 
2549adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2550adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2551adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2552adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2553adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2554adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2555adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2556adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2557adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2558adc56f5aSEdward Tomasz Napierala 				break;
2559adc56f5aSEdward Tomasz Napierala 			}
2560adc56f5aSEdward Tomasz Napierala 
2561adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2562adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2563adc56f5aSEdward Tomasz Napierala 				goto unhold;
2564adc56f5aSEdward Tomasz Napierala 
2565adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2566adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2567adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2568adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2569adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2570adc56f5aSEdward Tomasz Napierala unhold:
2571adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2572adc56f5aSEdward Tomasz Napierala #else
2573adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2574adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2575adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2576adc56f5aSEdward Tomasz Napierala 			break;
2577adc56f5aSEdward Tomasz Napierala 			}
2578dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2579af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2580dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2581af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2582dbc42409SLawrence Stewart 			break;
258308af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25842f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
25852f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
25862f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
25872f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
25882f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
258908af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
259008af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
259108af8aacSRandall Stewart 				break;
25922f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
25935a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
25942f3eb7f4SGleb Smirnoff 				break;
25952f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
25965a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
25972f3eb7f4SGleb Smirnoff 				break;
25982f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
25995a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
26002f3eb7f4SGleb Smirnoff 				break;
26012f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
26025a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
26032f3eb7f4SGleb Smirnoff 				break;
26042f3eb7f4SGleb Smirnoff 			}
26052f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
26062f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
26072f3eb7f4SGleb Smirnoff 			break;
260886a996e6SHiren Panchasara #ifdef TCPPCAP
260986a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
261086a996e6SHiren Panchasara 		case TCP_PCAP_IN:
2611399a5655SRichard Scheffenegger 			optval = tcp_pcap_get_sock_max(
2612399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
261386a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
261486a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
261586a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
261686a996e6SHiren Panchasara 			break;
261786a996e6SHiren Panchasara #endif
2618281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2619281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2620281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2621281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2622281a0fd4SPatrick Kelsey 			break;
2623e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
26242529f56eSJonathan T. Looney 		case TCP_LOG:
262569c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
26262529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26272529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
26282529f56eSJonathan T. Looney 			break;
26292529f56eSJonathan T. Looney 		case TCP_LOGBUF:
26302529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
26312529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26322529f56eSJonathan T. Looney 			break;
26332529f56eSJonathan T. Looney 		case TCP_LOGID:
26342529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26352529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26362529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26372529f56eSJonathan T. Looney 			break;
26382529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26392529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26402529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26412529f56eSJonathan T. Looney 			error = EINVAL;
26422529f56eSJonathan T. Looney 			break;
2643e24e5683SJonathan T. Looney #endif
2644b2e60773SJohn Baldwin #ifdef KERN_TLS
2645b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2646fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2647b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2648bf256782SMark Johnston 			if (error == 0)
2649bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2650bf256782SMark Johnston 				    sizeof(optval));
2651b2e60773SJohn Baldwin 			break;
2652f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2653fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2654f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2655bf256782SMark Johnston 			if (error == 0)
2656bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2657bf256782SMark Johnston 				    sizeof(optval));
2658f1f93475SJohn Baldwin 			break;
2659b2e60773SJohn Baldwin #endif
26600471a8c7SRichard Scheffenegger 		case TCP_LRD:
26610471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
26620471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
26630471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
26640471a8c7SRichard Scheffenegger 			break;
2665df8bae1dSRodney W. Grimes 		default:
26668501a69cSRobert Watson 			INP_WUNLOCK(inp);
2667df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2668df8bae1dSRodney W. Grimes 			break;
2669df8bae1dSRodney W. Grimes 		}
2670df8bae1dSRodney W. Grimes 		break;
2671df8bae1dSRodney W. Grimes 	}
2672df8bae1dSRodney W. Grimes 	return (error);
2673df8bae1dSRodney W. Grimes }
26748501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2675bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2676df8bae1dSRodney W. Grimes 
267726e30fbbSDavid Greenman /*
2678df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2679df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2680df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2681df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2682df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2683df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2684df8bae1dSRodney W. Grimes  */
2685623dce13SRobert Watson static void
2686ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2687df8bae1dSRodney W. Grimes {
26889eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
26899eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2690e6e0b5ffSRobert Watson 
269197a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26928501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2693df8bae1dSRodney W. Grimes 
2694623dce13SRobert Watson 	/*
2695623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2696623dce13SRobert Watson 	 * socket is still open.
2697623dce13SRobert Watson 	 */
26988db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
26998db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2700df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2701623dce13SRobert Watson 		KASSERT(tp != NULL,
2702623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2703623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2704243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2705623dce13SRobert Watson 		KASSERT(tp != NULL,
2706623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2707623dce13SRobert Watson 	} else {
2708df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2709df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2710623dce13SRobert Watson 		tcp_usrclosed(tp);
2711ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2712f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2713f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2714df8bae1dSRodney W. Grimes 	}
2715df8bae1dSRodney W. Grimes }
2716df8bae1dSRodney W. Grimes 
2717df8bae1dSRodney W. Grimes /*
2718df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2719df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2720df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2721df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2722df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2723df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2724df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2725df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2726df8bae1dSRodney W. Grimes  */
2727623dce13SRobert Watson static void
2728ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2729df8bae1dSRodney W. Grimes {
2730df8bae1dSRodney W. Grimes 
273197a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27329eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2733e6e0b5ffSRobert Watson 
2734df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2735df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
273609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
273709fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
273809fe6320SNavdeep Parhar #endif
2739550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2740bc65987aSKip Macy 		/* FALLTHROUGH */
2741bc65987aSKip Macy 	case TCPS_CLOSED:
2742df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2743623dce13SRobert Watson 		/*
2744623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2745623dce13SRobert Watson 		 * still open.
2746623dce13SRobert Watson 		 */
2747623dce13SRobert Watson 		KASSERT(tp != NULL,
2748623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2749df8bae1dSRodney W. Grimes 		break;
2750df8bae1dSRodney W. Grimes 
2751a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2752df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2753a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2754a0292f23SGarrett Wollman 		break;
2755a0292f23SGarrett Wollman 
2756df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
275757f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2758df8bae1dSRodney W. Grimes 		break;
2759df8bae1dSRodney W. Grimes 
2760df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
276157f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2762df8bae1dSRodney W. Grimes 		break;
2763df8bae1dSRodney W. Grimes 	}
276408af8aacSRandall Stewart 	if (tp->t_acktime == 0)
276508af8aacSRandall Stewart 		tp->t_acktime = ticks;
2766abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27679eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2768abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27697c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27707c72af87SMohan Srinivasan 			int timeout;
27717c72af87SMohan Srinivasan 
27727c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27739077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2774b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2775b6239c4aSAndras Olah 		}
2776df8bae1dSRodney W. Grimes 	}
27777c72af87SMohan Srinivasan }
2778497057eeSRobert Watson 
2779497057eeSRobert Watson #ifdef DDB
2780497057eeSRobert Watson static void
2781497057eeSRobert Watson db_print_indent(int indent)
2782497057eeSRobert Watson {
2783497057eeSRobert Watson 	int i;
2784497057eeSRobert Watson 
2785497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2786497057eeSRobert Watson 		db_printf(" ");
2787497057eeSRobert Watson }
2788497057eeSRobert Watson 
2789497057eeSRobert Watson static void
2790497057eeSRobert Watson db_print_tstate(int t_state)
2791497057eeSRobert Watson {
2792497057eeSRobert Watson 
2793497057eeSRobert Watson 	switch (t_state) {
2794497057eeSRobert Watson 	case TCPS_CLOSED:
2795497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2796497057eeSRobert Watson 		return;
2797497057eeSRobert Watson 
2798497057eeSRobert Watson 	case TCPS_LISTEN:
2799497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2800497057eeSRobert Watson 		return;
2801497057eeSRobert Watson 
2802497057eeSRobert Watson 	case TCPS_SYN_SENT:
2803497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2804497057eeSRobert Watson 		return;
2805497057eeSRobert Watson 
2806497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2807497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2808497057eeSRobert Watson 		return;
2809497057eeSRobert Watson 
2810497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2811497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2812497057eeSRobert Watson 		return;
2813497057eeSRobert Watson 
2814497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2815497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2816497057eeSRobert Watson 		return;
2817497057eeSRobert Watson 
2818497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2819497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2820497057eeSRobert Watson 		return;
2821497057eeSRobert Watson 
2822497057eeSRobert Watson 	case TCPS_CLOSING:
2823497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2824497057eeSRobert Watson 		return;
2825497057eeSRobert Watson 
2826497057eeSRobert Watson 	case TCPS_LAST_ACK:
2827497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2828497057eeSRobert Watson 		return;
2829497057eeSRobert Watson 
2830497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2831497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2832497057eeSRobert Watson 		return;
2833497057eeSRobert Watson 
2834497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2835497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2836497057eeSRobert Watson 		return;
2837497057eeSRobert Watson 
2838497057eeSRobert Watson 	default:
2839497057eeSRobert Watson 		db_printf("unknown");
2840497057eeSRobert Watson 		return;
2841497057eeSRobert Watson 	}
2842497057eeSRobert Watson }
2843497057eeSRobert Watson 
2844497057eeSRobert Watson static void
2845497057eeSRobert Watson db_print_tflags(u_int t_flags)
2846497057eeSRobert Watson {
2847497057eeSRobert Watson 	int comma;
2848497057eeSRobert Watson 
2849497057eeSRobert Watson 	comma = 0;
2850497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2851497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2852497057eeSRobert Watson 		comma = 1;
2853497057eeSRobert Watson 	}
2854497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2855497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2856497057eeSRobert Watson 		comma = 1;
2857497057eeSRobert Watson 	}
2858497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2859497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2860497057eeSRobert Watson 		comma = 1;
2861497057eeSRobert Watson 	}
2862497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2863497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2864497057eeSRobert Watson 		comma = 1;
2865497057eeSRobert Watson 	}
2866497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2867497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2868497057eeSRobert Watson 		comma = 1;
2869497057eeSRobert Watson 	}
2870497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2871497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2872497057eeSRobert Watson 		comma = 1;
2873497057eeSRobert Watson 	}
2874497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2875497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2876497057eeSRobert Watson 		comma = 1;
2877497057eeSRobert Watson 	}
2878497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2879497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2880497057eeSRobert Watson 		comma = 1;
2881497057eeSRobert Watson 	}
2882497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2883497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2884497057eeSRobert Watson 		comma = 1;
2885497057eeSRobert Watson 	}
2886497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2887497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2888497057eeSRobert Watson 		comma = 1;
2889497057eeSRobert Watson 	}
2890497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2891497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2892497057eeSRobert Watson 		comma = 1;
2893497057eeSRobert Watson 	}
2894497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2895497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2896497057eeSRobert Watson 		comma = 1;
2897497057eeSRobert Watson 	}
2898497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2899497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2900497057eeSRobert Watson 		comma = 1;
2901497057eeSRobert Watson 	}
29023f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
29033f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
29043f169c54SRichard Scheffenegger 		comma = 1;
29053f169c54SRichard Scheffenegger 	}
2906497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2907497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2908497057eeSRobert Watson 		comma = 1;
2909497057eeSRobert Watson 	}
2910493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2911493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2912497057eeSRobert Watson 		comma = 1;
2913497057eeSRobert Watson 	}
2914497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2915497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2916497057eeSRobert Watson 		comma = 1;
2917497057eeSRobert Watson 	}
2918497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2919497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2920497057eeSRobert Watson 		comma = 1;
2921497057eeSRobert Watson 	}
2922497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2923497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2924497057eeSRobert Watson 		comma = 1;
2925497057eeSRobert Watson 	}
2926dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2927dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2928dbc42409SLawrence Stewart 		comma = 1;
2929dbc42409SLawrence Stewart 	}
2930497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2931497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2932497057eeSRobert Watson 		comma = 1;
2933497057eeSRobert Watson 	}
29343f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
29353f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
29363f169c54SRichard Scheffenegger 		comma = 1;
29373f169c54SRichard Scheffenegger 	}
2938497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2939497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2940497057eeSRobert Watson 		comma = 1;
2941497057eeSRobert Watson 	}
2942497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2943497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2944497057eeSRobert Watson 		comma = 1;
2945497057eeSRobert Watson 	}
2946497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2947497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2948497057eeSRobert Watson 		comma = 1;
2949497057eeSRobert Watson 	}
2950281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2951281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2952281a0fd4SPatrick Kelsey 		comma = 1;
2953281a0fd4SPatrick Kelsey 	}
2954497057eeSRobert Watson }
2955497057eeSRobert Watson 
2956497057eeSRobert Watson static void
29573cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29583cf38784SMichael Tuexen {
29593cf38784SMichael Tuexen 	int comma;
29603cf38784SMichael Tuexen 
29613cf38784SMichael Tuexen 	comma = 0;
29623f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29633f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29643f169c54SRichard Scheffenegger 		comma = 1;
29653f169c54SRichard Scheffenegger 	}
29663f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29673f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29683f169c54SRichard Scheffenegger 		comma = 1;
29693f169c54SRichard Scheffenegger 	}
29703f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29713f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29723f169c54SRichard Scheffenegger 		comma = 1;
29733f169c54SRichard Scheffenegger 	}
29743f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29753f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29763f169c54SRichard Scheffenegger 		comma = 1;
29773f169c54SRichard Scheffenegger 	}
29783f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29793f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29803f169c54SRichard Scheffenegger 		comma = 1;
29813f169c54SRichard Scheffenegger 	}
29823cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29833cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29843cf38784SMichael Tuexen 		comma = 1;
29853cf38784SMichael Tuexen 	}
29863f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
29873f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
29883f169c54SRichard Scheffenegger 		comma = 1;
29893f169c54SRichard Scheffenegger 	}
29903f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
29913f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
29923f169c54SRichard Scheffenegger 		comma = 1;
29933f169c54SRichard Scheffenegger 	}
29943f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
29953f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
29963f169c54SRichard Scheffenegger 		comma = 1;
29973f169c54SRichard Scheffenegger 	}
29983f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
29993f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30003f169c54SRichard Scheffenegger 		comma = 1;
30013f169c54SRichard Scheffenegger 	}
30023cf38784SMichael Tuexen }
30033cf38784SMichael Tuexen 
30043cf38784SMichael Tuexen static void
3005497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3006497057eeSRobert Watson {
3007497057eeSRobert Watson 	int comma;
3008497057eeSRobert Watson 
3009497057eeSRobert Watson 	comma = 0;
3010497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3011497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3012497057eeSRobert Watson 		comma = 1;
3013497057eeSRobert Watson 	}
3014497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3015497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3016497057eeSRobert Watson 		comma = 1;
3017497057eeSRobert Watson 	}
3018497057eeSRobert Watson }
3019497057eeSRobert Watson 
3020497057eeSRobert Watson static void
3021497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3022497057eeSRobert Watson {
3023497057eeSRobert Watson 
3024497057eeSRobert Watson 	db_print_indent(indent);
3025497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3026497057eeSRobert Watson 
3027497057eeSRobert Watson 	indent += 2;
3028497057eeSRobert Watson 
3029497057eeSRobert Watson 	db_print_indent(indent);
3030497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3031c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3032497057eeSRobert Watson 
3033497057eeSRobert Watson 	db_print_indent(indent);
3034446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3035446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3036497057eeSRobert Watson 
3037497057eeSRobert Watson 	db_print_indent(indent);
3038497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3039497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3040497057eeSRobert Watson 	db_printf(")\n");
3041497057eeSRobert Watson 
3042497057eeSRobert Watson 	db_print_indent(indent);
3043497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3044497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3045497057eeSRobert Watson 	db_printf(")\n");
3046497057eeSRobert Watson 
3047497057eeSRobert Watson 	db_print_indent(indent);
30483cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30493cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30503cf38784SMichael Tuexen 	db_printf(")\n");
30513cf38784SMichael Tuexen 
30523cf38784SMichael Tuexen 	db_print_indent(indent);
3053fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3054497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3055497057eeSRobert Watson 
3056497057eeSRobert Watson 	db_print_indent(indent);
3057497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3058497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3059497057eeSRobert Watson 
3060497057eeSRobert Watson 	db_print_indent(indent);
3061497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3062497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3063497057eeSRobert Watson 
3064497057eeSRobert Watson 	db_print_indent(indent);
30653ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3066497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3067497057eeSRobert Watson 
3068497057eeSRobert Watson 	db_print_indent(indent);
30693ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
30701c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3071497057eeSRobert Watson 
3072497057eeSRobert Watson 	db_print_indent(indent);
30733ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
30741c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3075497057eeSRobert Watson 
3076497057eeSRobert Watson 	db_print_indent(indent);
30770c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
30780c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3079497057eeSRobert Watson 
3080497057eeSRobert Watson 	db_print_indent(indent);
30811c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
30821c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3083497057eeSRobert Watson 
3084497057eeSRobert Watson 	db_print_indent(indent);
30851c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
30861c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3087497057eeSRobert Watson 
3088497057eeSRobert Watson 	db_print_indent(indent);
3089bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3090bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3091497057eeSRobert Watson 
3092497057eeSRobert Watson 	db_print_indent(indent);
309318b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3094497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3095497057eeSRobert Watson 
3096497057eeSRobert Watson 	db_print_indent(indent);
3097497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3098497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3099497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3100497057eeSRobert Watson 
3101497057eeSRobert Watson 	db_print_indent(indent);
3102497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3103497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3104497057eeSRobert Watson 
3105497057eeSRobert Watson 	db_print_indent(indent);
31069f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31071a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3108497057eeSRobert Watson 
3109497057eeSRobert Watson 	db_print_indent(indent);
3110497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31113ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3112497057eeSRobert Watson 
3113497057eeSRobert Watson 	db_print_indent(indent);
31143ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31159f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3116497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3117497057eeSRobert Watson 
3118497057eeSRobert Watson 	db_print_indent(indent);
31193529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31203529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3121497057eeSRobert Watson 
3122497057eeSRobert Watson 	db_print_indent(indent);
3123a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3124a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3125497057eeSRobert Watson 
3126497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3127497057eeSRobert Watson 
3128497057eeSRobert Watson 	db_print_indent(indent);
3129497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3130497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3131497057eeSRobert Watson }
3132497057eeSRobert Watson 
3133497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3134497057eeSRobert Watson {
3135497057eeSRobert Watson 	struct tcpcb *tp;
3136497057eeSRobert Watson 
3137497057eeSRobert Watson 	if (!have_addr) {
3138497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3139497057eeSRobert Watson 		return;
3140497057eeSRobert Watson 	}
3141497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3142497057eeSRobert Watson 
3143497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3144497057eeSRobert Watson }
3145497057eeSRobert Watson #endif
3146