xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 5dc99e9bb985dce58e8fc85c09ef4e49bf051971)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
5623dce13SRobert Watson  *	The Regents of the University of California.
6497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
7fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8623dce13SRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
10fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
11fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
12fa046d87SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  */
37df8bae1dSRodney W. Grimes 
384b421e2dSMike Silbersack #include <sys/cdefs.h>
39497057eeSRobert Watson #include "opt_ddb.h"
401cfd4b53SBruce M Simpson #include "opt_inet.h"
41fb59c426SYoshinobu Inoue #include "opt_inet6.h"
42fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
43b2e60773SJohn Baldwin #include "opt_kern_tls.h"
440cc12cc5SJoerg Wunsch 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
489077f387SGleb Smirnoff #include <sys/limits.h>
49f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5055bceb1eSRandall Stewart #include <sys/refcount.h>
51c7a82f90SGarrett Wollman #include <sys/kernel.h>
52b2e60773SJohn Baldwin #include <sys/ktls.h>
53adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5498163b98SPoul-Henning Kamp #include <sys/sysctl.h>
55df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
56fb59c426SYoshinobu Inoue #ifdef INET6
57fb59c426SYoshinobu Inoue #include <sys/domain.h>
58fb59c426SYoshinobu Inoue #endif /* INET6 */
59df8bae1dSRodney W. Grimes #include <sys/socket.h>
60df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
61df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6291421ba2SRobert Watson #include <sys/proc.h>
6391421ba2SRobert Watson #include <sys/jail.h>
64adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
65df8bae1dSRodney W. Grimes 
66497057eeSRobert Watson #ifdef DDB
67497057eeSRobert Watson #include <ddb/ddb.h>
68497057eeSRobert Watson #endif
69497057eeSRobert Watson 
70df8bae1dSRodney W. Grimes #include <net/if.h>
7176039bc8SGleb Smirnoff #include <net/if_var.h>
72df8bae1dSRodney W. Grimes #include <net/route.h>
73530c0060SRobert Watson #include <net/vnet.h>
74df8bae1dSRodney W. Grimes 
75df8bae1dSRodney W. Grimes #include <netinet/in.h>
765d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
77df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
78b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
79b5e8ce9fSBruce Evans #include <netinet/in_var.h>
803b0ee680SRichard Scheffenegger #include <netinet/ip.h>
81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
82fb59c426SYoshinobu Inoue #ifdef INET6
83b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
84b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
85fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
86a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
87fb59c426SYoshinobu Inoue #endif
882de3e790SGleb Smirnoff #include <netinet/tcp.h>
89df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
90df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
91df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
92df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
932529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
954644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
96c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
97fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
9886a996e6SHiren Panchasara #ifdef TCPPCAP
9986a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10086a996e6SHiren Panchasara #endif
10109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
102bc65987aSKip Macy #include <netinet/tcp_offload.h>
10309fe6320SNavdeep Parhar #endif
104fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
105df8bae1dSRodney W. Grimes 
106adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
107adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
108adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
109adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
110adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
111adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
112adc56f5aSEdward Tomasz Napierala 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
115df8bae1dSRodney W. Grimes  */
116b287c6c7SBjoern A. Zeeb #ifdef INET
117a9d22cceSGleb Smirnoff static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
1184d77a549SAlfred Perlstein 		    struct thread *td);
119b287c6c7SBjoern A. Zeeb #endif /* INET */
120fb59c426SYoshinobu Inoue #ifdef INET6
121a9d22cceSGleb Smirnoff static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
1224d77a549SAlfred Perlstein 		    struct thread *td);
123fb59c426SYoshinobu Inoue #endif /* INET6 */
124623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
125623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
1268c6104c4SMarius Strobl static void	tcp_fill_info(const struct tcpcb *, struct tcp_info *);
1272c37256eSGarrett Wollman 
128d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
129d3b6c96bSRandall Stewart 
13000812bbdSMichael Tuexen static void
13100812bbdSMichael Tuexen tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
13200812bbdSMichael Tuexen {
13300812bbdSMichael Tuexen 	struct tcp_log_buffer *lgb;
13400812bbdSMichael Tuexen 
135c2399dd2SMichael Tuexen 	KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
13600812bbdSMichael Tuexen 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13769c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
13869c7c811SRandall Stewart 		lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
13900812bbdSMichael Tuexen 		    0, NULL, false, NULL, NULL, 0, NULL);
14000812bbdSMichael Tuexen 	} else {
14100812bbdSMichael Tuexen 		lgb = NULL;
14200812bbdSMichael Tuexen 	}
14300812bbdSMichael Tuexen 	if (lgb != NULL) {
14400812bbdSMichael Tuexen 		if (error >= 0) {
14500812bbdSMichael Tuexen 			lgb->tlb_errno = (uint32_t)error;
14600812bbdSMichael Tuexen 		}
14700812bbdSMichael Tuexen 		lgb->tlb_flex1 = pru;
14800812bbdSMichael Tuexen 	}
14900812bbdSMichael Tuexen }
15000812bbdSMichael Tuexen 
1512c37256eSGarrett Wollman /*
1522c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1532c37256eSGarrett Wollman  * and an internet control block.
1542c37256eSGarrett Wollman  */
1552c37256eSGarrett Wollman static int
156b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1572c37256eSGarrett Wollman {
158f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
159623dce13SRobert Watson 	struct tcpcb *tp = NULL;
160623dce13SRobert Watson 	int error;
1612c37256eSGarrett Wollman 
162623dce13SRobert Watson 	inp = sotoinpcb(so);
163623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1642c37256eSGarrett Wollman 
1650f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1662c37256eSGarrett Wollman 	if (error)
1672c37256eSGarrett Wollman 		goto out;
1682c37256eSGarrett Wollman 
1690f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1700f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1710f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1727669c586SGleb Smirnoff 	if (error)
1730f6385e7SGleb Smirnoff 		goto out;
1740f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
175baee801cSMichael Tuexen 	tp = tcp_newtcpcb(inp, NULL);
1760f6385e7SGleb Smirnoff 	if (tp == NULL) {
1777669c586SGleb Smirnoff 		error = ENOBUFS;
1780f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1790f6385e7SGleb Smirnoff 		goto out;
1800f6385e7SGleb Smirnoff 	}
1810f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
18200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ATTACH, error);
1830f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1840f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1852c37256eSGarrett Wollman out:
1865d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1870f6385e7SGleb Smirnoff 	return (error);
1882c37256eSGarrett Wollman }
1892c37256eSGarrett Wollman 
1902c37256eSGarrett Wollman /*
1913fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
192a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
193a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
194a152f8a3SRobert Watson  * inpcb state: time wait.
1952c37256eSGarrett Wollman  */
196bc725eafSRobert Watson static void
1973fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
1982c37256eSGarrett Wollman {
1993fed74e9SGleb Smirnoff 	struct inpcb *inp;
2002c37256eSGarrett Wollman 	struct tcpcb *tp;
2012c37256eSGarrett Wollman 
2023fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2033fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2043fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2053fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2063fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
207953b5606SRobert Watson 
208a152f8a3SRobert Watson 	tp = intotcpcb(inp);
209a152f8a3SRobert Watson 
2101b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2111b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2121b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2139c3507f9SGleb Smirnoff 
214623dce13SRobert Watson 	tcp_discardcb(tp);
2150206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
216623dce13SRobert Watson }
217c78cbc7bSRobert Watson 
218b287c6c7SBjoern A. Zeeb #ifdef INET
2192c37256eSGarrett Wollman /*
2202c37256eSGarrett Wollman  * Give the socket an address.
2212c37256eSGarrett Wollman  */
2222c37256eSGarrett Wollman static int
223b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2242c37256eSGarrett Wollman {
2252c37256eSGarrett Wollman 	int error = 0;
226f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
227c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2282c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2292c37256eSGarrett Wollman 
230c2399dd2SMichael Tuexen 	inp = sotoinpcb(so);
231c2399dd2SMichael Tuexen 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
232c2399dd2SMichael Tuexen 	INP_WLOCK(inp);
233c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
234c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
235c2399dd2SMichael Tuexen 		return (EINVAL);
236c2399dd2SMichael Tuexen 	}
237c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
238c2399dd2SMichael Tuexen 
23952710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
240f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
241f96603b5SMark Johnston 		/*
242f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
243f96603b5SMark Johnston 		 */
244f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2453f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
246c2399dd2SMichael Tuexen 		    sinp->sin_addr.s_addr != INADDR_ANY) {
247c2399dd2SMichael Tuexen 			error = EAFNOSUPPORT;
248c2399dd2SMichael Tuexen 			goto out;
249c2399dd2SMichael Tuexen 		}
250f96603b5SMark Johnston 		nam->sa_family = AF_INET;
251f96603b5SMark Johnston 	}
252c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sinp)) {
253c2399dd2SMichael Tuexen 		error = EINVAL;
254c2399dd2SMichael Tuexen 		goto out;
255c2399dd2SMichael Tuexen 	}
2562c37256eSGarrett Wollman 	/*
2572c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2582c37256eSGarrett Wollman 	 * to them.
2592c37256eSGarrett Wollman 	 */
260c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
261c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
2622c37256eSGarrett Wollman 		goto out;
263623dce13SRobert Watson 	}
264fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
265*5dc99e9bSMark Johnston 	error = in_pcbbind(inp, sinp, V_tcp_bind_all_fibs ? 0 : INPBIND_FIB,
266*5dc99e9bSMark Johnston 	    td->td_ucred);
267fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
268623dce13SRobert Watson out:
26900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
2705d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2718501a69cSRobert Watson 	INP_WUNLOCK(inp);
272623dce13SRobert Watson 
273623dce13SRobert Watson 	return (error);
2742c37256eSGarrett Wollman }
275b287c6c7SBjoern A. Zeeb #endif /* INET */
2762c37256eSGarrett Wollman 
277fb59c426SYoshinobu Inoue #ifdef INET6
278fb59c426SYoshinobu Inoue static int
279b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
280fb59c426SYoshinobu Inoue {
281fb59c426SYoshinobu Inoue 	int error = 0;
282f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
283c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2840ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2854a91aa8fSMichael Tuexen 	u_char vflagsav;
286fb59c426SYoshinobu Inoue 
287623dce13SRobert Watson 	inp = sotoinpcb(so);
288623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2898501a69cSRobert Watson 	INP_WLOCK(inp);
29053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
291c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
292c2399dd2SMichael Tuexen 		return (EINVAL);
293c2399dd2SMichael Tuexen 	}
294c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
295c2399dd2SMichael Tuexen 
296c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
297c2399dd2SMichael Tuexen 
298c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
299c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
300c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
301c2399dd2SMichael Tuexen 		goto out;
302c2399dd2SMichael Tuexen 	}
303c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sin6)) {
304623dce13SRobert Watson 		error = EINVAL;
305623dce13SRobert Watson 		goto out;
306623dce13SRobert Watson 	}
307c2399dd2SMichael Tuexen 	/*
308c2399dd2SMichael Tuexen 	 * Must check for multicast addresses and disallow binding
309c2399dd2SMichael Tuexen 	 * to them.
310c2399dd2SMichael Tuexen 	 */
311c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
312c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
313c2399dd2SMichael Tuexen 		goto out;
314c2399dd2SMichael Tuexen 	}
315c2399dd2SMichael Tuexen 
316fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
317fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
318fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
319b287c6c7SBjoern A. Zeeb #ifdef INET
32066ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3210ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
322fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3230ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
324fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
325fb59c426SYoshinobu Inoue 
3260ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
327888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
328888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
329888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
330888973f5SMichael Tuexen 				goto out;
331888973f5SMichael Tuexen 			}
332fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
333fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
334bbd0084bSMark Johnston 			error = in_pcbbind(inp, &sin, 0, td->td_ucred);
335fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
336fb59c426SYoshinobu Inoue 			goto out;
337fb59c426SYoshinobu Inoue 		}
338fb59c426SYoshinobu Inoue 	}
339b287c6c7SBjoern A. Zeeb #endif
340*5dc99e9bSMark Johnston 	error = in6_pcbbind(inp, sin6, V_tcp_bind_all_fibs ? 0 : INPBIND_FIB,
341*5dc99e9bSMark Johnston 	    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 {
360f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
361c2399dd2SMichael Tuexen 	struct tcpcb *tp;
36206bf119fSGleb Smirnoff 	int error = 0;
36306bf119fSGleb Smirnoff 	bool already_listening;
3642c37256eSGarrett Wollman 
365623dce13SRobert Watson 	inp = sotoinpcb(so);
366623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3678501a69cSRobert Watson 	INP_WLOCK(inp);
36853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
369c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
370c2399dd2SMichael Tuexen 		return (EINVAL);
371623dce13SRobert Watson 	}
372623dce13SRobert Watson 	tp = intotcpcb(inp);
373c2399dd2SMichael Tuexen 
3740daccb9cSRobert Watson 	SOCK_LOCK(so);
37506bf119fSGleb Smirnoff 	already_listening = SOLISTENING(so);
3760daccb9cSRobert Watson 	error = solisten_proto_check(so);
377bd4a39ccSMark Johnston 	if (error != 0) {
378bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
379bd4a39ccSMark Johnston 		goto out;
380bd4a39ccSMark Johnston 	}
381bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
382fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
383*5dc99e9bSMark Johnston 		error = in_pcbbind(inp, NULL,
384*5dc99e9bSMark Johnston 		    V_tcp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
385fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
386bd4a39ccSMark Johnston 	}
3870daccb9cSRobert Watson 	if (error == 0) {
38857f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
389d374e81eSRobert Watson 		solisten_proto(so, backlog);
39009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
39137cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
39209fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
39309fe6320SNavdeep Parhar #endif
394bd4a39ccSMark Johnston 	} else {
395bd4a39ccSMark Johnston 		solisten_proto_abort(so);
3960daccb9cSRobert Watson 	}
3970daccb9cSRobert Watson 	SOCK_UNLOCK(so);
39806bf119fSGleb Smirnoff 	if (already_listening)
39906bf119fSGleb Smirnoff 		goto out;
400623dce13SRobert Watson 
4017cbb6b6eSMark Johnston 	if (error == 0)
4027cbb6b6eSMark Johnston 		in_pcblisten(inp);
403dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
404281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
40518a75309SPatrick Kelsey 
406623dce13SRobert Watson out:
40700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4085d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4098501a69cSRobert Watson 	INP_WUNLOCK(inp);
410623dce13SRobert Watson 	return (error);
4112c37256eSGarrett Wollman }
412b287c6c7SBjoern A. Zeeb #endif /* INET */
4132c37256eSGarrett Wollman 
414fb59c426SYoshinobu Inoue #ifdef INET6
415fb59c426SYoshinobu Inoue static int
416d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
417fb59c426SYoshinobu Inoue {
418f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
419c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4204a91aa8fSMichael Tuexen 	u_char vflagsav;
42106bf119fSGleb Smirnoff 	int error = 0;
42206bf119fSGleb Smirnoff 	bool already_listening;
423fb59c426SYoshinobu Inoue 
424623dce13SRobert Watson 	inp = sotoinpcb(so);
425623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4268501a69cSRobert Watson 	INP_WLOCK(inp);
42753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
428c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
429c2399dd2SMichael Tuexen 		return (EINVAL);
430623dce13SRobert Watson 	}
431623dce13SRobert Watson 	tp = intotcpcb(inp);
432c2399dd2SMichael Tuexen 
433c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
434c2399dd2SMichael Tuexen 
4350daccb9cSRobert Watson 	SOCK_LOCK(so);
43606bf119fSGleb Smirnoff 	already_listening = SOLISTENING(so);
4370daccb9cSRobert Watson 	error = solisten_proto_check(so);
438bd4a39ccSMark Johnston 	if (error != 0) {
439bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
440bd4a39ccSMark Johnston 		goto out;
441bd4a39ccSMark Johnston 	}
442fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
443bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
444fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
44566ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
446fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
447*5dc99e9bSMark Johnston 		error = in6_pcbbind(inp, NULL,
448*5dc99e9bSMark Johnston 		    V_tcp_bind_all_fibs ? 0 : INPBIND_FIB, td->td_ucred);
449fb59c426SYoshinobu Inoue 	}
450fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4510daccb9cSRobert Watson 	if (error == 0) {
45257f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
453d374e81eSRobert Watson 		solisten_proto(so, backlog);
45409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
45537cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45609fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45709fe6320SNavdeep Parhar #endif
458bd4a39ccSMark Johnston 	} else {
459bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4600daccb9cSRobert Watson 	}
4610daccb9cSRobert Watson 	SOCK_UNLOCK(so);
46206bf119fSGleb Smirnoff 	if (already_listening)
46306bf119fSGleb Smirnoff 		goto out;
464623dce13SRobert Watson 
4657cbb6b6eSMark Johnston 	if (error == 0)
4667cbb6b6eSMark Johnston 		in_pcblisten(inp);
467dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
468281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
46918a75309SPatrick Kelsey 
4704a91aa8fSMichael Tuexen 	if (error != 0)
4714a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4724a91aa8fSMichael Tuexen 
473623dce13SRobert Watson out:
47400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4755d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4768501a69cSRobert Watson 	INP_WUNLOCK(inp);
477623dce13SRobert Watson 	return (error);
478fb59c426SYoshinobu Inoue }
479fb59c426SYoshinobu Inoue #endif /* INET6 */
480fb59c426SYoshinobu Inoue 
481b287c6c7SBjoern A. Zeeb #ifdef INET
4822c37256eSGarrett Wollman /*
4832c37256eSGarrett Wollman  * Initiate connection to peer.
4842c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4852c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4862c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4872c37256eSGarrett Wollman  * Send initial segment on connection.
4882c37256eSGarrett Wollman  */
4892c37256eSGarrett Wollman static int
490b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4912c37256eSGarrett Wollman {
492109eb549SGleb Smirnoff 	struct epoch_tracker et;
4932c37256eSGarrett Wollman 	int error = 0;
494f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
495c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4962c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4972c37256eSGarrett Wollman 
498623dce13SRobert Watson 	inp = sotoinpcb(so);
499623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
5008501a69cSRobert Watson 	INP_WLOCK(inp);
501eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
502c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
503c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
504c2399dd2SMichael Tuexen 	}
505c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
506c2399dd2SMichael Tuexen 
507c2399dd2SMichael Tuexen 	sinp = (struct sockaddr_in *)nam;
508c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET) {
509c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
510623dce13SRobert Watson 		goto out;
511623dce13SRobert Watson 	}
512c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sinp)) {
513c2399dd2SMichael Tuexen 		error = EINVAL;
514c2399dd2SMichael Tuexen 		goto out;
515c2399dd2SMichael Tuexen 	}
516c2399dd2SMichael Tuexen 	/*
517c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
518c2399dd2SMichael Tuexen 	 */
519c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
520c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
521c2399dd2SMichael Tuexen 		goto out;
522c2399dd2SMichael Tuexen 	}
523c2399dd2SMichael Tuexen 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
524c2399dd2SMichael Tuexen 		error = EACCES;
525c2399dd2SMichael Tuexen 		goto out;
526c2399dd2SMichael Tuexen 	}
527c2399dd2SMichael Tuexen 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
528c2399dd2SMichael Tuexen 		goto out;
529bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
530bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
531bd4a39ccSMark Johnston 		goto out;
532bd4a39ccSMark Johnston 	}
533c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
534a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
535c1604fe4SGleb Smirnoff 		goto out_in_epoch;
53609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
53709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
53837cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
53909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
540c1604fe4SGleb Smirnoff 		goto out_in_epoch;
54109fe6320SNavdeep Parhar #endif
54209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
54340fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5441d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5451d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
546c1604fe4SGleb Smirnoff out_in_epoch:
547109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
548623dce13SRobert Watson out:
54900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
550e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5518501a69cSRobert Watson 	INP_WUNLOCK(inp);
552623dce13SRobert Watson 	return (error);
5532c37256eSGarrett Wollman }
554b287c6c7SBjoern A. Zeeb #endif /* INET */
5552c37256eSGarrett Wollman 
556fb59c426SYoshinobu Inoue #ifdef INET6
557fb59c426SYoshinobu Inoue static int
558b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
559fb59c426SYoshinobu Inoue {
560109eb549SGleb Smirnoff 	struct epoch_tracker et;
561fb59c426SYoshinobu Inoue 	int error = 0;
562f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
563c2399dd2SMichael Tuexen 	struct tcpcb *tp;
5640ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5654a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5664a91aa8fSMichael Tuexen 	u_char vflagsav;
567623dce13SRobert Watson 
568623dce13SRobert Watson 	inp = sotoinpcb(so);
569623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5708501a69cSRobert Watson 	INP_WLOCK(inp);
571c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
572c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
573c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
574c2399dd2SMichael Tuexen 	}
575c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
576c2399dd2SMichael Tuexen 
5774a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5784a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
579c2399dd2SMichael Tuexen 
580c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
581c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
582c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
583c2399dd2SMichael Tuexen 		goto out;
584c2399dd2SMichael Tuexen 	}
585c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sin6)) {
586c2399dd2SMichael Tuexen 		error = EINVAL;
587c2399dd2SMichael Tuexen 		goto out;
588c2399dd2SMichael Tuexen 	}
589c2399dd2SMichael Tuexen 	/*
590c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
591c2399dd2SMichael Tuexen 	 */
592c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
593c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
594623dce13SRobert Watson 		goto out;
595623dce13SRobert Watson 	}
596bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
597bd4a39ccSMark Johnston 		error = EINVAL;
598bd4a39ccSMark Johnston 		goto out;
599bd4a39ccSMark Johnston 	}
600b287c6c7SBjoern A. Zeeb #ifdef INET
601fa046d87SRobert Watson 	/*
602fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
603fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
604fa046d87SRobert Watson 	 * Is this a significant problem?
605fa046d87SRobert Watson 	 */
6060ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
607fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
608fb59c426SYoshinobu Inoue 
609d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
610d46a5312SMaxim Konovalov 			error = EINVAL;
611d46a5312SMaxim Konovalov 			goto out;
612d46a5312SMaxim Konovalov 		}
6135dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6145dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6155dba6adaSMichael Tuexen 			goto out;
6165dba6adaSMichael Tuexen 		}
61733841545SHajimu UMEMOTO 
6180ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
619888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
620888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
621888973f5SMichael Tuexen 			goto out;
622888973f5SMichael Tuexen 		}
623f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
624f903a308SMichael Tuexen 			error = EACCES;
6252cf21ae5SRandall Stewart 			goto out;
6262cf21ae5SRandall Stewart 		}
627b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
628b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
629413628a7SBjoern A. Zeeb 			goto out;
6304a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6314a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
632c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
633a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
634c1604fe4SGleb Smirnoff 			goto out_in_epoch;
63509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
63609fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
637adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
63809fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
639c1604fe4SGleb Smirnoff 			goto out_in_epoch;
64009fe6320SNavdeep Parhar #endif
64140fa3e40SGleb Smirnoff 		error = tcp_output(tp);
642c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6435dba6adaSMichael Tuexen 	} else {
6445dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6455dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6465dba6adaSMichael Tuexen 			goto out;
6475dba6adaSMichael Tuexen 		}
648fb59c426SYoshinobu Inoue 	}
649b287c6c7SBjoern A. Zeeb #endif
6504a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6514a91aa8fSMichael Tuexen 		goto out;
652fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
653fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
654dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6550773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
656a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6570773b44eSGleb Smirnoff 		goto out_in_epoch;
65809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
65909fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
660adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
66109fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6620773b44eSGleb Smirnoff 		goto out_in_epoch;
66309fe6320SNavdeep Parhar #endif
66409fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
66540fa3e40SGleb Smirnoff 	error = tcp_output(tp);
666c1604fe4SGleb Smirnoff out_in_epoch:
667109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
668623dce13SRobert Watson out:
6691d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6701d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6714a91aa8fSMichael Tuexen 	/*
6724a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6734a91aa8fSMichael Tuexen 	 * the flags we modified.
6744a91aa8fSMichael Tuexen 	 */
6754a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6764a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6774a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6784a91aa8fSMichael Tuexen 	}
6794a91aa8fSMichael Tuexen 
68000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6815d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6828501a69cSRobert Watson 	INP_WUNLOCK(inp);
683623dce13SRobert Watson 	return (error);
684fb59c426SYoshinobu Inoue }
685fb59c426SYoshinobu Inoue #endif /* INET6 */
686fb59c426SYoshinobu Inoue 
6872c37256eSGarrett Wollman /*
6882c37256eSGarrett Wollman  * Initiate disconnect from peer.
6892c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6902c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6912c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6922c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6932c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6942c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6952c37256eSGarrett Wollman  *
6962c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6972c37256eSGarrett Wollman  */
6982c37256eSGarrett Wollman static int
6992c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
7002c37256eSGarrett Wollman {
701f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
702623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7036573d758SMatt Macy 	struct epoch_tracker et;
7042c37256eSGarrett Wollman 
70597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
706623dce13SRobert Watson 	inp = sotoinpcb(so);
707623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
7088501a69cSRobert Watson 	INP_WLOCK(inp);
709623dce13SRobert Watson 	tp = intotcpcb(inp);
710c2399dd2SMichael Tuexen 
711cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
712cda6bdbaSJohn Baldwin 		goto out;
713623dce13SRobert Watson 	tcp_disconnect(tp);
714623dce13SRobert Watson out:
715c91dd7a0SGleb Smirnoff 	tcp_bblog_pru(tp, PRU_DISCONNECT, 0);
7165d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7178501a69cSRobert Watson 	INP_WUNLOCK(inp);
71897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
719c91dd7a0SGleb Smirnoff 	return (0);
7202c37256eSGarrett Wollman }
7212c37256eSGarrett Wollman 
722b287c6c7SBjoern A. Zeeb #ifdef INET
7232c37256eSGarrett Wollman /*
7248296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7258296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7262c37256eSGarrett Wollman  */
7272c37256eSGarrett Wollman static int
728cfb1e929SGleb Smirnoff tcp_usr_accept(struct socket *so, struct sockaddr *sa)
7292c37256eSGarrett Wollman {
730c2399dd2SMichael Tuexen 	struct inpcb *inp;
731c2399dd2SMichael Tuexen 	struct tcpcb *tp;
732cfb1e929SGleb Smirnoff 	int error = 0;
7332c37256eSGarrett Wollman 
734f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
735623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7368501a69cSRobert Watson 	INP_WLOCK(inp);
73753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
738c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
739c2399dd2SMichael Tuexen 		return (ECONNABORTED);
740623dce13SRobert Watson 	}
7411db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
742f76fcf6dSJeffrey Hsu 
743cfb1e929SGleb Smirnoff 	if (so->so_state & SS_ISDISCONNECTED)
744c2399dd2SMichael Tuexen 		error = ECONNABORTED;
745cfb1e929SGleb Smirnoff 	else
746cfb1e929SGleb Smirnoff 		*(struct sockaddr_in *)sa = (struct sockaddr_in ){
747cfb1e929SGleb Smirnoff 			.sin_family = AF_INET,
748cfb1e929SGleb Smirnoff 			.sin_len = sizeof(struct sockaddr_in),
749cfb1e929SGleb Smirnoff 			.sin_port = inp->inp_fport,
750cfb1e929SGleb Smirnoff 			.sin_addr = inp->inp_faddr,
751cfb1e929SGleb Smirnoff 		};
75200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7535d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7548501a69cSRobert Watson 	INP_WUNLOCK(inp);
755cfb1e929SGleb Smirnoff 
756cfb1e929SGleb Smirnoff 	return (error);
7572c37256eSGarrett Wollman }
758b287c6c7SBjoern A. Zeeb #endif /* INET */
7592c37256eSGarrett Wollman 
760fb59c426SYoshinobu Inoue #ifdef INET6
761fb59c426SYoshinobu Inoue static int
762cfb1e929SGleb Smirnoff tcp6_usr_accept(struct socket *so, struct sockaddr *sa)
763fb59c426SYoshinobu Inoue {
764c2399dd2SMichael Tuexen 	struct inpcb *inp;
765c2399dd2SMichael Tuexen 	struct tcpcb *tp;
766cfb1e929SGleb Smirnoff 	int error = 0;
767fb59c426SYoshinobu Inoue 
768f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
769623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
7708501a69cSRobert Watson 	INP_WLOCK(inp);
77153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
772c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
773c2399dd2SMichael Tuexen 		return (ECONNABORTED);
774623dce13SRobert Watson 	}
7751db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
776623dce13SRobert Watson 
777c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
778c2399dd2SMichael Tuexen 		error = ECONNABORTED;
77926ef6ac4SDon Lewis 	} else {
780cfb1e929SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV4) {
781cfb1e929SGleb Smirnoff 			struct sockaddr_in sin = {
782cfb1e929SGleb Smirnoff 				.sin_family = AF_INET,
783cfb1e929SGleb Smirnoff 				.sin_len = sizeof(struct sockaddr_in),
784cfb1e929SGleb Smirnoff 				.sin_port = inp->inp_fport,
785cfb1e929SGleb Smirnoff 				.sin_addr = inp->inp_faddr,
786cfb1e929SGleb Smirnoff 			};
787cfb1e929SGleb Smirnoff 			in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
788cfb1e929SGleb Smirnoff 		} else {
789cfb1e929SGleb Smirnoff 			*(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
790cfb1e929SGleb Smirnoff 				.sin6_family = AF_INET6,
791cfb1e929SGleb Smirnoff 				.sin6_len = sizeof(struct sockaddr_in6),
792cfb1e929SGleb Smirnoff 				.sin6_port = inp->inp_fport,
793cfb1e929SGleb Smirnoff 				.sin6_addr = inp->in6p_faddr,
794cfb1e929SGleb Smirnoff 			};
795cfb1e929SGleb Smirnoff 			/* XXX: should catch errors */
796cfb1e929SGleb Smirnoff 			(void)sa6_recoverscope((struct sockaddr_in6 *)sa);
797cfb1e929SGleb Smirnoff 		}
79826ef6ac4SDon Lewis 	}
79926ef6ac4SDon Lewis 
80000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
8015d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8028501a69cSRobert Watson 	INP_WUNLOCK(inp);
803cfb1e929SGleb Smirnoff 
804cfb1e929SGleb Smirnoff 	return (error);
805fb59c426SYoshinobu Inoue }
806fb59c426SYoshinobu Inoue #endif /* INET6 */
807f76fcf6dSJeffrey Hsu 
808f76fcf6dSJeffrey Hsu /*
8092c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8102c37256eSGarrett Wollman  */
8112c37256eSGarrett Wollman static int
8125bba2728SGleb Smirnoff tcp_usr_shutdown(struct socket *so, enum shutdown_how how)
8132c37256eSGarrett Wollman {
8146573d758SMatt Macy 	struct epoch_tracker et;
8155bba2728SGleb Smirnoff 	struct inpcb *inp = sotoinpcb(so);
8165bba2728SGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
8175bba2728SGleb Smirnoff 	int error = 0;
8182c37256eSGarrett Wollman 
8195bba2728SGleb Smirnoff 	SOCK_LOCK(so);
8205bba2728SGleb Smirnoff 	if (SOLISTENING(so)) {
8215bba2728SGleb Smirnoff 		if (how != SHUT_WR) {
8225bba2728SGleb Smirnoff 			so->so_error = ECONNABORTED;
8235bba2728SGleb Smirnoff 			solisten_wakeup(so);	/* unlocks so */
8245bba2728SGleb Smirnoff 		} else
8255bba2728SGleb Smirnoff 			SOCK_UNLOCK(so);
826abe8379bSGleb Smirnoff 		return (ENOTCONN);
827abe8379bSGleb Smirnoff 	} else if ((so->so_state &
828abe8379bSGleb Smirnoff 	    (SS_ISCONNECTED | SS_ISCONNECTING | SS_ISDISCONNECTING)) == 0) {
829abe8379bSGleb Smirnoff 		SOCK_UNLOCK(so);
830abe8379bSGleb Smirnoff 		return (ENOTCONN);
8315bba2728SGleb Smirnoff 	}
8325bba2728SGleb Smirnoff 	SOCK_UNLOCK(so);
8335bba2728SGleb Smirnoff 
8345bba2728SGleb Smirnoff 	switch (how) {
8355bba2728SGleb Smirnoff 	case SHUT_RD:
836ce69e373SGleb Smirnoff 		sorflush(so);
8375bba2728SGleb Smirnoff 		break;
8385bba2728SGleb Smirnoff 	case SHUT_RDWR:
839ce69e373SGleb Smirnoff 		sorflush(so);
8405bba2728SGleb Smirnoff 		/* FALLTHROUGH */
8415bba2728SGleb Smirnoff 	case SHUT_WR:
8425bba2728SGleb Smirnoff 		/*
8435bba2728SGleb Smirnoff 		 * XXXGL: mimicing old soshutdown() here. But shouldn't we
8445bba2728SGleb Smirnoff 		 * return ECONNRESEST for SHUT_RD as well?
8455bba2728SGleb Smirnoff 		 */
8468501a69cSRobert Watson 		INP_WLOCK(inp);
84753af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
8480af4ce45SGleb Smirnoff 			INP_WUNLOCK(inp);
8490af4ce45SGleb Smirnoff 			return (ECONNRESET);
850623dce13SRobert Watson 		}
851c2399dd2SMichael Tuexen 
8522c37256eSGarrett Wollman 		socantsendmore(so);
8535bba2728SGleb Smirnoff 		NET_EPOCH_ENTER(et);
854623dce13SRobert Watson 		tcp_usrclosed(tp);
855f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
85600812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8575d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
858f64dc2abSGleb Smirnoff 		error = tcp_unlock_or_drop(tp, error);
85997a95ee1SGleb Smirnoff 		NET_EPOCH_EXIT(et);
8605bba2728SGleb Smirnoff 	}
8615bba2728SGleb Smirnoff 	wakeup(&so->so_timeo);
862623dce13SRobert Watson 
863623dce13SRobert Watson 	return (error);
8642c37256eSGarrett Wollman }
8652c37256eSGarrett Wollman 
8662c37256eSGarrett Wollman /*
8672c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8682c37256eSGarrett Wollman  */
8692c37256eSGarrett Wollman static int
8702c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8712c37256eSGarrett Wollman {
872109eb549SGleb Smirnoff 	struct epoch_tracker et;
873f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
874c2399dd2SMichael Tuexen 	struct tcpcb *tp;
875f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8762c37256eSGarrett Wollman 
877623dce13SRobert Watson 	inp = sotoinpcb(so);
878623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8798501a69cSRobert Watson 	INP_WLOCK(inp);
88053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
88137a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
88237a7f557SGleb Smirnoff 		return (ECONNRESET);
883623dce13SRobert Watson 	}
88437a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
885c2399dd2SMichael Tuexen 
88637a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
887281a0fd4SPatrick Kelsey 	/*
888281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
889281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
890281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
891281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
892281a0fd4SPatrick Kelsey 	 * expires.
893281a0fd4SPatrick Kelsey 	 */
894dd7b86e2SGleb Smirnoff 	if ((tp->t_flags & TF_FASTOPEN) && (tp->t_state == TCPS_SYN_RECEIVED))
895281a0fd4SPatrick Kelsey 		goto out;
89609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
89709fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
89809fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
899460cf046SNavdeep Parhar 	else
90009fe6320SNavdeep Parhar #endif
901f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
902623dce13SRobert Watson out:
90300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
9045d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
905f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
906f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
907623dce13SRobert Watson 	return (error);
9082c37256eSGarrett Wollman }
9092c37256eSGarrett Wollman 
9102c37256eSGarrett Wollman /*
9112c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9129c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9139c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9149c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9159c9906e9SPeter Wemm  * generally are caller-frees.
9162c37256eSGarrett Wollman  */
9172c37256eSGarrett Wollman static int
91857bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
919b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9202c37256eSGarrett Wollman {
92197a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9222c37256eSGarrett Wollman 	int error = 0;
923f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
924c2399dd2SMichael Tuexen 	struct tcpcb *tp;
925888973f5SMichael Tuexen #ifdef INET
92651e08d53SMichael Tuexen #ifdef INET6
92751e08d53SMichael Tuexen 	struct sockaddr_in sin;
92851e08d53SMichael Tuexen #endif
92951e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
930888973f5SMichael Tuexen #endif
931fb59c426SYoshinobu Inoue #ifdef INET6
932a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
933fb59c426SYoshinobu Inoue 	int isipv6;
934fb59c426SYoshinobu Inoue #endif
9354a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9364a91aa8fSMichael Tuexen 	u_char vflagsav;
9374a91aa8fSMichael Tuexen 	bool restoreflags;
9382c37256eSGarrett Wollman 
9394287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9404287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9414287aa56SGleb Smirnoff 	INP_WLOCK(inp);
94253af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9434287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9444287aa56SGleb Smirnoff 			m_freem(m);
9454287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9464287aa56SGleb Smirnoff 		return (ECONNRESET);
9474287aa56SGleb Smirnoff 	}
948c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
9494287aa56SGleb Smirnoff 
9504287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9514287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9524287aa56SGleb Smirnoff 	restoreflags = false;
9534287aa56SGleb Smirnoff 
9544287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
955c2399dd2SMichael Tuexen 	if (control != NULL) {
956c2399dd2SMichael Tuexen 		/* TCP doesn't do control messages (rights, creds, etc) */
957c2399dd2SMichael Tuexen 		if (control->m_len > 0) {
958c2399dd2SMichael Tuexen 			m_freem(control);
959c2399dd2SMichael Tuexen 			error = EINVAL;
960c2399dd2SMichael Tuexen 			goto out;
961c2399dd2SMichael Tuexen 		}
962c2399dd2SMichael Tuexen 		m_freem(control);	/* empty control, just free it */
963c2399dd2SMichael Tuexen 	}
964c2399dd2SMichael Tuexen 
9657d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9667d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
967d3b6c96bSRandall Stewart 		goto out;
9687d2608a5SMark Johnston 
969888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
970bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
971bd4a39ccSMark Johnston 			error = EINVAL;
972bd4a39ccSMark Johnston 			goto out;
973bd4a39ccSMark Johnston 		}
974888973f5SMichael Tuexen 		switch (nam->sa_family) {
975888973f5SMichael Tuexen #ifdef INET
976888973f5SMichael Tuexen 		case AF_INET:
977888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
978888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
979888973f5SMichael Tuexen 				error = EINVAL;
980888973f5SMichael Tuexen 				goto out;
981888973f5SMichael Tuexen 			}
982888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
983888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
984888973f5SMichael Tuexen 				goto out;
985888973f5SMichael Tuexen 			}
986888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
987888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
988888973f5SMichael Tuexen 				goto out;
989888973f5SMichael Tuexen 			}
990f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
991f903a308SMichael Tuexen 				error = EACCES;
9922cf21ae5SRandall Stewart 				goto out;
9932cf21ae5SRandall Stewart 			}
994888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9957d2608a5SMark Johnston 			    &sinp->sin_addr)))
996888973f5SMichael Tuexen 				goto out;
997888973f5SMichael Tuexen #ifdef INET6
998888973f5SMichael Tuexen 			isipv6 = 0;
999888973f5SMichael Tuexen #endif
1000888973f5SMichael Tuexen 			break;
1001888973f5SMichael Tuexen #endif /* INET */
1002888973f5SMichael Tuexen #ifdef INET6
1003888973f5SMichael Tuexen 		case AF_INET6:
10040ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
10050ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
1006888973f5SMichael Tuexen 				error = EINVAL;
1007888973f5SMichael Tuexen 				goto out;
1008888973f5SMichael Tuexen 			}
1009e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
1010e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
1011e240ce42SMichael Tuexen 				goto out;
1012e240ce42SMichael Tuexen 			}
10130ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1014888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1015888973f5SMichael Tuexen 				goto out;
1016888973f5SMichael Tuexen 			}
10170ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1018888973f5SMichael Tuexen #ifdef INET
1019888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1020888973f5SMichael Tuexen 					error = EINVAL;
1021888973f5SMichael Tuexen 					goto out;
1022888973f5SMichael Tuexen 				}
1023888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1024888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1025888973f5SMichael Tuexen 					goto out;
1026888973f5SMichael Tuexen 				}
10274a91aa8fSMichael Tuexen 				restoreflags = true;
1028888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1029888973f5SMichael Tuexen 				sinp = &sin;
10300ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1031888973f5SMichael Tuexen 				if (IN_MULTICAST(
1032888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1033888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1034888973f5SMichael Tuexen 					goto out;
1035888973f5SMichael Tuexen 				}
1036888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10377d2608a5SMark Johnston 				    &sinp->sin_addr)))
1038888973f5SMichael Tuexen 					goto out;
1039888973f5SMichael Tuexen 				isipv6 = 0;
1040888973f5SMichael Tuexen #else /* !INET */
1041888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1042888973f5SMichael Tuexen 				goto out;
1043888973f5SMichael Tuexen #endif /* INET */
1044888973f5SMichael Tuexen 			} else {
1045888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1046888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1047888973f5SMichael Tuexen 					goto out;
1048888973f5SMichael Tuexen 				}
10494a91aa8fSMichael Tuexen 				restoreflags = true;
1050888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1051888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1052888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10537d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1054888973f5SMichael Tuexen 					goto out;
1055888973f5SMichael Tuexen 				isipv6 = 1;
1056888973f5SMichael Tuexen 			}
1057888973f5SMichael Tuexen 			break;
1058888973f5SMichael Tuexen #endif /* INET6 */
1059888973f5SMichael Tuexen 		default:
1060888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1061888973f5SMichael Tuexen 			goto out;
1062888973f5SMichael Tuexen 		}
1063888973f5SMichael Tuexen 	}
10642c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
106508af8aacSRandall Stewart 		if (tp->t_acktime == 0)
106608af8aacSRandall Stewart 			tp->t_acktime = ticks;
1067651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10687d2608a5SMark Johnston 		m = NULL;
10692c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1070bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1071bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1072bd4a39ccSMark Johnston 
10732c37256eSGarrett Wollman 			/*
10742c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10752c37256eSGarrett Wollman 			 * initialize window to default value, and
10760c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10772c37256eSGarrett Wollman 			 */
1078fb59c426SYoshinobu Inoue #ifdef INET6
1079fb59c426SYoshinobu Inoue 			if (isipv6)
1080a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1081fb59c426SYoshinobu Inoue #endif /* INET6 */
1082b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1083b287c6c7SBjoern A. Zeeb 			else
1084b287c6c7SBjoern A. Zeeb #endif
1085b287c6c7SBjoern A. Zeeb #ifdef INET
1086a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1087b287c6c7SBjoern A. Zeeb #endif
10884a91aa8fSMichael Tuexen 			/*
10894a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10904a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10914a91aa8fSMichael Tuexen 			 * operations fail.
10924a91aa8fSMichael Tuexen 			 */
10934a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10944a91aa8fSMichael Tuexen 				restoreflags = false;
10954a91aa8fSMichael Tuexen 
10967d2608a5SMark Johnston 			if (error) {
10977d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10987d2608a5SMark Johnston 				sbflush(&so->so_snd);
10992c37256eSGarrett Wollman 				goto out;
11007d2608a5SMark Johnston 			}
1101dd7b86e2SGleb Smirnoff 			if (tp->t_flags & TF_FASTOPEN)
1102c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
110318a75309SPatrick Kelsey 			else {
11042c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
11052c37256eSGarrett Wollman 				tcp_mss(tp, -1);
11062c37256eSGarrett Wollman 			}
1107c560df6fSPatrick Kelsey 		}
11082c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
11092c37256eSGarrett Wollman 			/*
11102c37256eSGarrett Wollman 			 * Close the send side of the connection after
11112c37256eSGarrett Wollman 			 * the data is sent.
11122c37256eSGarrett Wollman 			 */
11132c37256eSGarrett Wollman 			socantsendmore(so);
1114623dce13SRobert Watson 			tcp_usrclosed(tp);
11152c37256eSGarrett Wollman 		}
1116e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1117e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1118e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1119e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1120e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1121e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1122e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1123e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1124e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1125e854dd38SRandall Stewart 		}
11262cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11272cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1128b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1129b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1130f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1131b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1132b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1133b0acefa8SBill Fenner 		}
11342c37256eSGarrett Wollman 	} else {
1135623dce13SRobert Watson 		/*
1136623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1137623dce13SRobert Watson 		 */
1138dded4e9eSRichard Scheffenegger 		SOCK_SENDBUF_LOCK(so);
11392c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1140dded4e9eSRichard Scheffenegger 			SOCK_SENDBUF_UNLOCK(so);
11412c37256eSGarrett Wollman 			error = ENOBUFS;
11422c37256eSGarrett Wollman 			goto out;
11432c37256eSGarrett Wollman 		}
11442c37256eSGarrett Wollman 		/*
11452c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11462c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11472c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11482c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11492c37256eSGarrett Wollman 		 * of data past the urgent section.
11502c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11512c37256eSGarrett Wollman 		 */
115208af8aacSRandall Stewart 		if (tp->t_acktime == 0)
115308af8aacSRandall Stewart 			tp->t_acktime = ticks;
1154651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1155dded4e9eSRichard Scheffenegger 		SOCK_SENDBUF_UNLOCK(so);
11567d2608a5SMark Johnston 		m = NULL;
1157ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1158ef53690bSGarrett Wollman 			/*
1159ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1160ef53690bSGarrett Wollman 			 * initialize window to default value, and
11610c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1162ef53690bSGarrett Wollman 			 */
116318a75309SPatrick Kelsey 
1164c560df6fSPatrick Kelsey 			/*
1165c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1166c560df6fSPatrick Kelsey 			 */
1167dd7b86e2SGleb Smirnoff 			if (tp->t_flags & TF_FASTOPEN)
1168c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1169fb59c426SYoshinobu Inoue #ifdef INET6
1170fb59c426SYoshinobu Inoue 			if (isipv6)
1171a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1172fb59c426SYoshinobu Inoue #endif /* INET6 */
1173b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1174b287c6c7SBjoern A. Zeeb 			else
1175b287c6c7SBjoern A. Zeeb #endif
1176b287c6c7SBjoern A. Zeeb #ifdef INET
1177a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1178b287c6c7SBjoern A. Zeeb #endif
11794a91aa8fSMichael Tuexen 			/*
11804a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11814a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11824a91aa8fSMichael Tuexen 			 * operations fail.
11834a91aa8fSMichael Tuexen 			 */
11844a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11854a91aa8fSMichael Tuexen 				restoreflags = false;
11864a91aa8fSMichael Tuexen 
11877d2608a5SMark Johnston 			if (error != 0) {
11887d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11897d2608a5SMark Johnston 				sbflush(&so->so_snd);
1190ef53690bSGarrett Wollman 				goto out;
11917d2608a5SMark Johnston 			}
1192ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1193ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1194623dce13SRobert Watson 		}
1195300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11967d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11972cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1198f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11992cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
12002c37256eSGarrett Wollman 		}
12012cbcd3c1SGleb Smirnoff 	}
12022529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
12032529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
12042529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
12052529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
12062529f56eSJonathan T. Looney 	    0, NULL, false);
12077d2608a5SMark Johnston 
1208d1401c90SRobert Watson out:
12094a91aa8fSMichael Tuexen 	/*
12107d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
12117d2608a5SMark Johnston 	 * responsible for freeing memory.
12127d2608a5SMark Johnston 	 */
12137d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12147d2608a5SMark Johnston 		m_freem(m);
12157d2608a5SMark Johnston 
12167d2608a5SMark Johnston 	/*
12174a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12184a91aa8fSMichael Tuexen 	 * restore the original flags.
12194a91aa8fSMichael Tuexen 	 */
12204a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12214a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12224a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12234a91aa8fSMichael Tuexen 	}
122400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
122500812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
12265d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12275d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1228f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
122997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
123073fddedaSPeter Grehan 	return (error);
12312c37256eSGarrett Wollman }
12322c37256eSGarrett Wollman 
12332cbcd3c1SGleb Smirnoff static int
12342cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12352cbcd3c1SGleb Smirnoff {
1236109eb549SGleb Smirnoff 	struct epoch_tracker et;
12372cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12382cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12392cbcd3c1SGleb Smirnoff 	int error;
12402cbcd3c1SGleb Smirnoff 
12412cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12422cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
124353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12442cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
124582334850SJohn Baldwin 		mb_free_notready(m, count);
12462cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12472cbcd3c1SGleb Smirnoff 	}
12482cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12492cbcd3c1SGleb Smirnoff 
1250dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_LOCK(so);
12512cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
1252dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_UNLOCK(so);
1253f64dc2abSGleb Smirnoff 	if (error) {
12542cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1255f64dc2abSGleb Smirnoff 		return (error);
1256f64dc2abSGleb Smirnoff 	}
1257f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1258f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1259f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12602cbcd3c1SGleb Smirnoff 
12612cbcd3c1SGleb Smirnoff 	return (error);
12622cbcd3c1SGleb Smirnoff }
12632cbcd3c1SGleb Smirnoff 
12642c37256eSGarrett Wollman /*
1265a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12662c37256eSGarrett Wollman  */
1267ac45e92fSRobert Watson static void
12682c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12692c37256eSGarrett Wollman {
1270f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1271c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12726573d758SMatt Macy 	struct epoch_tracker et;
1273c78cbc7bSRobert Watson 
1274ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1275c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1276c78cbc7bSRobert Watson 
127797a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12788501a69cSRobert Watson 	INP_WLOCK(inp);
1279c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1280c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1281c78cbc7bSRobert Watson 
1282c78cbc7bSRobert Watson 	/*
1283a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1284c78cbc7bSRobert Watson 	 */
128553af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1286c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12878fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12888fa799bdSJonathan T. Looney 		if (tp == NULL)
12898fa799bdSJonathan T. Looney 			goto dropped;
129000812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12915d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1292c78cbc7bSRobert Watson 	}
1293ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1294d8596171SGleb Smirnoff 		soref(so);
1295ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1296a152f8a3SRobert Watson 	}
12978501a69cSRobert Watson 	INP_WUNLOCK(inp);
12988fa799bdSJonathan T. Looney dropped:
129997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1300a152f8a3SRobert Watson }
1301a152f8a3SRobert Watson 
1302a152f8a3SRobert Watson /*
1303a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1304a152f8a3SRobert Watson  */
1305a152f8a3SRobert Watson static void
1306a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1307a152f8a3SRobert Watson {
1308a152f8a3SRobert Watson 	struct inpcb *inp;
1309c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13106573d758SMatt Macy 	struct epoch_tracker et;
1311a152f8a3SRobert Watson 
1312a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1313a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1314a152f8a3SRobert Watson 
131597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13168501a69cSRobert Watson 	INP_WLOCK(inp);
1317a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1318a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1319a152f8a3SRobert Watson 
1320a152f8a3SRobert Watson 	/*
1321cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1322a152f8a3SRobert Watson 	 * a disconnect.
1323a152f8a3SRobert Watson 	 */
132453af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1325a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1326cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
132774703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1328a152f8a3SRobert Watson 			tcp_disconnect(tp);
132900812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
13305d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1331a152f8a3SRobert Watson 		}
1332cda6bdbaSJohn Baldwin 	}
1333ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1334d8596171SGleb Smirnoff 		soref(so);
1335ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1336a152f8a3SRobert Watson 	}
13378501a69cSRobert Watson 	INP_WUNLOCK(inp);
133897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13392c37256eSGarrett Wollman }
13402c37256eSGarrett Wollman 
1341d3b6c96bSRandall Stewart static int
1342d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1343d3b6c96bSRandall Stewart {
1344d3b6c96bSRandall Stewart 	/*
1345d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1346d3b6c96bSRandall Stewart 	 * specified then it does not always support
1347d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1348d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1349d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1350d3b6c96bSRandall Stewart 	 */
1351d3b6c96bSRandall Stewart 	int ret = 0;
1352d3b6c96bSRandall Stewart 
1353d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1354d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1355d3b6c96bSRandall Stewart 	}
1356d3b6c96bSRandall Stewart 	return (ret);
1357d3b6c96bSRandall Stewart }
1358d3b6c96bSRandall Stewart 
13592c37256eSGarrett Wollman /*
13602c37256eSGarrett Wollman  * Receive out-of-band data.
13612c37256eSGarrett Wollman  */
13622c37256eSGarrett Wollman static int
13632c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13642c37256eSGarrett Wollman {
13652c37256eSGarrett Wollman 	int error = 0;
1366f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1367c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13682c37256eSGarrett Wollman 
1369623dce13SRobert Watson 	inp = sotoinpcb(so);
1370623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13718501a69cSRobert Watson 	INP_WLOCK(inp);
137253af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1373c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
1374c2399dd2SMichael Tuexen 		return (ECONNRESET);
1375623dce13SRobert Watson 	}
1376623dce13SRobert Watson 	tp = intotcpcb(inp);
1377c2399dd2SMichael Tuexen 
1378d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1379d3b6c96bSRandall Stewart 	if (error) {
1380d3b6c96bSRandall Stewart 		goto out;
1381d3b6c96bSRandall Stewart 	}
13822c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1383c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13844cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13854cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13862c37256eSGarrett Wollman 		error = EINVAL;
13872c37256eSGarrett Wollman 		goto out;
13882c37256eSGarrett Wollman 	}
13892c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13902c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13912c37256eSGarrett Wollman 		goto out;
13922c37256eSGarrett Wollman 	}
13932c37256eSGarrett Wollman 	m->m_len = 1;
13942c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13952c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13962c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1397623dce13SRobert Watson 
1398623dce13SRobert Watson out:
139900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
14005d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
14018501a69cSRobert Watson 	INP_WUNLOCK(inp);
1402623dce13SRobert Watson 	return (error);
14032c37256eSGarrett Wollman }
14042c37256eSGarrett Wollman 
1405b287c6c7SBjoern A. Zeeb #ifdef INET
1406e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1407e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1408e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1409e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1410e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1411e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1412e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1413e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1414e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1415e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1416e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1417e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1418e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1419e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1420e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1421e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1422e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1423e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1424e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1425e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1426e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1427e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1428e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1429e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14302c37256eSGarrett Wollman };
1431b287c6c7SBjoern A. Zeeb #endif /* INET */
1432df8bae1dSRodney W. Grimes 
1433fb59c426SYoshinobu Inoue #ifdef INET6
1434e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1435e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1436e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1437e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1438e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1439e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1440e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1441e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1442e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1443e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1444e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1445e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1446e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1447e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1448e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1449e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1450e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1451e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1452e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1453e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1454e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1455e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1456e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1457e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1458fb59c426SYoshinobu Inoue };
1459fb59c426SYoshinobu Inoue #endif /* INET6 */
1460fb59c426SYoshinobu Inoue 
1461b287c6c7SBjoern A. Zeeb #ifdef INET
1462a0292f23SGarrett Wollman /*
1463a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1464dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1465dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1466dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1467a0292f23SGarrett Wollman  */
14680312fbe9SPoul-Henning Kamp static int
1469a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1470a0292f23SGarrett Wollman {
14719e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14729eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1473c3229e05SDavid Greenman 	int error;
1474a0292f23SGarrett Wollman 
1475c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14768501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
147776f1499fSGleb Smirnoff 
1478636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1479de0a2eb2SMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1480de0a2eb2SMark Johnston 	    SS_ISDISCONNECTED)) != 0))
1481636b19eaSMark Johnston 		return (EISCONN);
1482636b19eaSMark Johnston 
1483fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
14840b4539eeSGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred);
14859e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1486dfc4d218SGleb Smirnoff 	if (error != 0)
14879e46ff4dSGleb Smirnoff 		return (error);
1488a0292f23SGarrett Wollman 
1489087b55eaSAndre Oppermann 	/*
1490087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1491087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1492087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1493087b55eaSAndre Oppermann 	 */
1494a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14959b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1496a0292f23SGarrett Wollman 		tp->request_r_scale++;
1497a0292f23SGarrett Wollman 
1498a0292f23SGarrett Wollman 	soisconnecting(so);
149978b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
150057f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15018e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15028e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15038e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1504a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1505a45d2726SAndras Olah 
15069e46ff4dSGleb Smirnoff 	return (0);
1507a0292f23SGarrett Wollman }
1508b287c6c7SBjoern A. Zeeb #endif /* INET */
1509a0292f23SGarrett Wollman 
1510fb59c426SYoshinobu Inoue #ifdef INET6
1511fb59c426SYoshinobu Inoue static int
1512a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1513fb59c426SYoshinobu Inoue {
15149eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1515636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1516fb59c426SYoshinobu Inoue 	int error;
1517fb59c426SYoshinobu Inoue 
1518775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
15198501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1520623dce13SRobert Watson 
1521636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1522636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1523636b19eaSMark Johnston 		return (EISCONN);
1524636b19eaSMark Johnston 
152576f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1526a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1527fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
152876f1499fSGleb Smirnoff 	if (error != 0)
152976f1499fSGleb Smirnoff 		return (error);
1530fb59c426SYoshinobu Inoue 
1531fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1532fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1533970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1534fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1535fb59c426SYoshinobu Inoue 
1536636b19eaSMark Johnston 	soisconnecting(so);
153778b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
153857f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15398e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15408e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15418e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1542fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1543fb59c426SYoshinobu Inoue 
154476f1499fSGleb Smirnoff 	return (0);
1545fb59c426SYoshinobu Inoue }
1546fb59c426SYoshinobu Inoue #endif /* INET6 */
1547fb59c426SYoshinobu Inoue 
1548cfe8b629SGarrett Wollman /*
1549b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1550b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1551b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1552b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1553b8af5dfaSRobert Watson  * from Linux.
1554b8af5dfaSRobert Watson  */
15558c6104c4SMarius Strobl void
15568c6104c4SMarius Strobl tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1557b8af5dfaSRobert Watson {
1558b8af5dfaSRobert Watson 
15598c6104c4SMarius Strobl 	INP_LOCK_ASSERT(tptoinpcb(tp));
1560b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1561b8af5dfaSRobert Watson 
1562b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1563b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1564b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15653529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1566b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1567b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1568b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1569b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1570b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1571b8af5dfaSRobert Watson 	}
157204682968SRichard Scheffenegger 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
157304682968SRichard Scheffenegger 		case TF2_ECN_PERMIT:
15745a17b6adSMichael Tuexen 			ti->tcpi_options |= TCPI_OPT_ECN;
157504682968SRichard Scheffenegger 			break;
157604682968SRichard Scheffenegger 		case TF2_ACE_PERMIT:
157704682968SRichard Scheffenegger 			/* FALLTHROUGH */
157804682968SRichard Scheffenegger 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
157904682968SRichard Scheffenegger 			ti->tcpi_options |= TCPI_OPT_ACE;
158004682968SRichard Scheffenegger 			break;
158104682968SRichard Scheffenegger 		default:
158204682968SRichard Scheffenegger 			break;
158304682968SRichard Scheffenegger 	}
1584dd7b86e2SGleb Smirnoff 	if (tp->t_flags & TF_FASTOPEN)
158504682968SRichard Scheffenegger 		ti->tcpi_options |= TCPI_OPT_TFO;
15861baaf834SBruce M Simpson 
158743d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15883ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15891baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15901baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15911baaf834SBruce M Simpson 
1592b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1593b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1594b8af5dfaSRobert Watson 
1595b8af5dfaSRobert Watson 	/*
1596b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1597b8af5dfaSRobert Watson 	 */
1598c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1599535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1600b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
16011c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1602535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
160343d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
160443d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1605f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1606f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1607f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1608dc485b96SMarius Strobl 	ti->tcpi_snd_una = tp->snd_una;
1609dc485b96SMarius Strobl 	ti->tcpi_snd_max = tp->snd_max;
1610dc485b96SMarius Strobl 	ti->tcpi_rcv_numsacks = tp->rcv_numsacks;
1611dc485b96SMarius Strobl 	ti->tcpi_rcv_adv = tp->rcv_adv;
1612dc485b96SMarius Strobl 	ti->tcpi_dupacks = tp->t_dupacks;
1613e18b97bdSRandall Stewart 	ti->tcpi_rttmin = tp->t_rttlow;
1614a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1615a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1616a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1617a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1618a6456410SNavdeep Parhar 	}
1619a6456410SNavdeep Parhar #endif
162022c81cc5SRichard Scheffenegger 	/*
162122c81cc5SRichard Scheffenegger 	 * AccECN related counters.
162222c81cc5SRichard Scheffenegger 	 */
162322c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
162422c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
162522c81cc5SRichard Scheffenegger 		/*
162622c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
162722c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
162822c81cc5SRichard Scheffenegger 		 */
162922c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
163022c81cc5SRichard Scheffenegger 	else
163122c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
163222c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1633b8af5dfaSRobert Watson }
1634b8af5dfaSRobert Watson 
1635b8af5dfaSRobert Watson /*
16361e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16371e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16381e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16391e8f5ffaSRobert Watson  * option.
1640cfe8b629SGarrett Wollman  */
1641bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16428501a69cSRobert Watson 	INP_WLOCK(inp);							\
164353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16448501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1645bac5bedfSConrad Meyer 		cleanup;						\
16461e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16471e8f5ffaSRobert Watson 	}								\
16481e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16491e8f5ffaSRobert Watson } while(0)
1650bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16511e8f5ffaSRobert Watson 
1652fd7daa72SMichael Tuexen int
1653fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1654df8bae1dSRodney W. Grimes {
1655fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1656fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1657fc4d53ccSGleb Smirnoff 	int error = 0;
1658df8bae1dSRodney W. Grimes 
1659fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16603b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
166153af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1662fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1663fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1664fc4d53ccSGleb Smirnoff 
1665cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16663b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1667fb59c426SYoshinobu Inoue #ifdef INET6
1668de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1669fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1670de156263SGleb Smirnoff #endif
1671de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1672de156263SGleb Smirnoff 		else
1673de156263SGleb Smirnoff #endif
1674de156263SGleb Smirnoff #ifdef INET
1675fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1676de156263SGleb Smirnoff #endif
16775dff1c38SMichael Tuexen 		/*
1678de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1679de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1680de156263SGleb Smirnoff 		 * IP level returned.
16815dff1c38SMichael Tuexen 		 */
1682de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1683de156263SGleb Smirnoff #ifdef INET6
1684de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1685de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1686de156263SGleb Smirnoff 				return (error);
1687de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1688de156263SGleb Smirnoff 			case IPV6_TCLASS:
1689de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1690de156263SGleb Smirnoff 				break;
1691de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1692f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1693f581a26eSGleb Smirnoff 				break;
1694de156263SGleb Smirnoff 			default:
1695de156263SGleb Smirnoff 				return (error);
16965dff1c38SMichael Tuexen 			}
1697de156263SGleb Smirnoff 			break;
1698b287c6c7SBjoern A. Zeeb #endif
1699b287c6c7SBjoern A. Zeeb #ifdef INET
1700de156263SGleb Smirnoff 		case IPPROTO_IP:
1701de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1702de156263SGleb Smirnoff 			case IP_TOS:
17033b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
17043b0ee680SRichard Scheffenegger 				break;
1705de156263SGleb Smirnoff 			case IP_TTL:
1706de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1707de156263SGleb Smirnoff 				break;
1708de156263SGleb Smirnoff 			default:
1709df8bae1dSRodney W. Grimes 				return (error);
1710de156263SGleb Smirnoff 			}
1711de156263SGleb Smirnoff 			break;
1712de156263SGleb Smirnoff #endif
1713de156263SGleb Smirnoff 		default:
1714de156263SGleb Smirnoff 			return (error);
1715de156263SGleb Smirnoff 		}
1716fe136aecSMichael Tuexen 		INP_WLOCK_RECHECK(inp);
1717fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1718fc4d53ccSGleb Smirnoff 		/*
1719fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1720fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1721fc4d53ccSGleb Smirnoff 		 */
1722fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1723fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
172473ee5756SRandall Stewart 		void *ptr = NULL;
1725fc4d53ccSGleb Smirnoff 
17263b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1727fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1728fc4d53ccSGleb Smirnoff 		if (error)
1729fc4d53ccSGleb Smirnoff 			return (error);
1730fc4d53ccSGleb Smirnoff 
1731fe136aecSMichael Tuexen 		INP_WLOCK_RECHECK(inp);
1732fc4d53ccSGleb Smirnoff 
173355bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
173455bceb1eSRandall Stewart 		if (blk == NULL) {
173555bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
173655bceb1eSRandall Stewart 			return (ENOENT);
173755bceb1eSRandall Stewart 		}
1738587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1739587d67c0SRandall Stewart 			/* You already have this */
1740587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1741587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1742587d67c0SRandall Stewart 			return (0);
1743587d67c0SRandall Stewart 		}
174455bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
174555bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
174655bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
174755bceb1eSRandall Stewart 			return (ENOENT);
174855bceb1eSRandall Stewart 		}
174986c9325dSMichael Tuexen 		error = (*blk->tfb_tcp_handoff_ok)(tp);
175086c9325dSMichael Tuexen 		if (error) {
175186c9325dSMichael Tuexen 			refcount_release(&blk->tfb_refcnt);
175286c9325dSMichael Tuexen 			INP_WUNLOCK(inp);
175386c9325dSMichael Tuexen 			return (error);
175486c9325dSMichael Tuexen 		}
175555bceb1eSRandall Stewart 		/*
175673ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
175773ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
175855bceb1eSRandall Stewart 		 */
1759d2ef52efSGleb Smirnoff 		if (tp->t_fb->tfb_tcp_timer_stop_all != NULL)
1760d2ef52efSGleb Smirnoff 			tp->t_fb->tfb_tcp_timer_stop_all(tp);
176173ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
176273ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
176373ee5756SRandall Stewart 			if (error) {
176473ee5756SRandall Stewart 				/*
176573ee5756SRandall Stewart 				 * Release the ref count the lookup
176673ee5756SRandall Stewart 				 * acquired.
176773ee5756SRandall Stewart 				 */
176873ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
176973ee5756SRandall Stewart 				/*
177073ee5756SRandall Stewart 				 * Now there is a chance that the
177173ee5756SRandall Stewart 				 * init() function mucked with some
177273ee5756SRandall Stewart 				 * things before it failed, such as
177373ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
177473ee5756SRandall Stewart 				 * It should not of, but lets give the old
177573ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
177673ee5756SRandall Stewart 				 */
177773ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
177873ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
177973ee5756SRandall Stewart 				}
178073ee5756SRandall Stewart 			 	goto err_out;
178173ee5756SRandall Stewart 			}
178273ee5756SRandall Stewart 		}
1783587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1784086a3556SAndrew Gallatin 			struct epoch_tracker et;
1785587d67c0SRandall Stewart 			/*
1786587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1787587d67c0SRandall Stewart 			 * the tcb is not going away.
1788587d67c0SRandall Stewart 			 */
1789086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1790587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1791086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1792587d67c0SRandall Stewart 		}
179373ee5756SRandall Stewart 		/*
179473ee5756SRandall Stewart 		 * Release the old refcnt, the
179573ee5756SRandall Stewart 		 * lookup acquired a ref on the
179673ee5756SRandall Stewart 		 * new one already.
179773ee5756SRandall Stewart 		 */
179855bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
179973ee5756SRandall Stewart 		/*
180073ee5756SRandall Stewart 		 * Set in the new stack.
180173ee5756SRandall Stewart 		 */
180255bceb1eSRandall Stewart 		tp->t_fb = blk;
180373ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
180455bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
180555bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
180655bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
180755bceb1eSRandall Stewart 			     sopt->sopt_name);
180855bceb1eSRandall Stewart 		}
180955bceb1eSRandall Stewart #endif
18103ee9c3c4SRandall Stewart err_out:
181155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
181255bceb1eSRandall Stewart 		return (error);
181373ee5756SRandall Stewart 
1814fc4d53ccSGleb Smirnoff 	}
1815fc4d53ccSGleb Smirnoff 
18163b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
181766fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1818fc4d53ccSGleb Smirnoff }
1819fc4d53ccSGleb Smirnoff 
1820fc4d53ccSGleb Smirnoff static int
1821fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1822fc4d53ccSGleb Smirnoff {
1823fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1824fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1825fc4d53ccSGleb Smirnoff 	int error = 0;
1826fc4d53ccSGleb Smirnoff 
1827fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18283b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
182953af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1830fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1831fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1832fc4d53ccSGleb Smirnoff 
1833fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18343b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1835fc4d53ccSGleb Smirnoff #ifdef INET6
1836fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1837fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1838fc4d53ccSGleb Smirnoff #endif /* INET6 */
1839fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1840fc4d53ccSGleb Smirnoff 		else
1841fc4d53ccSGleb Smirnoff #endif
1842fc4d53ccSGleb Smirnoff #ifdef INET
1843fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1844fc4d53ccSGleb Smirnoff #endif
1845fc4d53ccSGleb Smirnoff 		return (error);
1846fc4d53ccSGleb Smirnoff 	}
1847fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1848e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1849fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1850fc4d53ccSGleb Smirnoff 
1851e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1852e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1853e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1854e2833083SPeter Lei 		} else {
1855e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1856e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1857c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1858c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1859e2833083SPeter Lei 		}
186055bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
186155bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
186255bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
186355bceb1eSRandall Stewart 		return (error);
186455bceb1eSRandall Stewart 	}
1865fc4d53ccSGleb Smirnoff 
18663b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
186766fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1868fc4d53ccSGleb Smirnoff }
1869fc4d53ccSGleb Smirnoff 
1870fc4d53ccSGleb Smirnoff int
1871fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1872fc4d53ccSGleb Smirnoff {
1873fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1874fc4d53ccSGleb Smirnoff 
1875fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1876fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1877fc4d53ccSGleb Smirnoff 
18783b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
187953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18803b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18813b3c08c1SMichael Tuexen 		return (ECONNRESET);
18823b3c08c1SMichael Tuexen 	}
1883fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1884fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1885fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1886fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1887fc4d53ccSGleb Smirnoff 	else
1888fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
188955bceb1eSRandall Stewart }
189055bceb1eSRandall Stewart 
18912529f56eSJonathan T. Looney /*
18922529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18932529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18942529f56eSJonathan T. Looney  */
18952529f56eSJonathan T. Looney #ifdef CTASSERT
18962529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18972529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18982529f56eSJonathan T. Looney #endif
18992529f56eSJonathan T. Looney 
1900b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1901b8d60729SRandall Stewart 
1902b8d60729SRandall Stewart static int
1903ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1904b8d60729SRandall Stewart {
1905b8d60729SRandall Stewart 	struct cc_algo *algo;
1906b8d60729SRandall Stewart 	void *ptr = NULL;
19073b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1908b8d60729SRandall Stewart 	struct cc_var cc_mem;
1909b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1910b8d60729SRandall Stewart 	size_t mem_sz;
1911b8d60729SRandall Stewart 	int error;
1912b8d60729SRandall Stewart 
1913b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1914b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1915b8d60729SRandall Stewart 	if (error)
1916b8d60729SRandall Stewart 		return(error);
1917b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1918b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1919ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1920b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1921b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1922b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1923b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1924b8d60729SRandall Stewart 				continue;
1925b8d60729SRandall Stewart 			}
1926b8d60729SRandall Stewart 			break;
1927b8d60729SRandall Stewart 		}
1928ea9017fbSRandall Stewart 	}
1929b8d60729SRandall Stewart 	if (algo == NULL) {
1930b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1931b8d60729SRandall Stewart 		return(ESRCH);
1932b8d60729SRandall Stewart 	}
1933ea9017fbSRandall Stewart 	/*
1934ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1935ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1936ea9017fbSRandall Stewart 	 */
1937ea9017fbSRandall Stewart 	cc_refer(algo);
1938ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1939b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1940b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1941b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1942b8d60729SRandall Stewart 		if (mem_sz == 0) {
1943b8d60729SRandall Stewart 			goto no_mem_needed;
1944b8d60729SRandall Stewart 		}
1945b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1946b8d60729SRandall Stewart 	} else {
1947b8d60729SRandall Stewart no_mem_needed:
1948b8d60729SRandall Stewart 		mem_sz = 0;
1949b8d60729SRandall Stewart 		ptr = NULL;
1950b8d60729SRandall Stewart 	}
1951b8d60729SRandall Stewart 	/*
1952b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1953b8d60729SRandall Stewart 	 * back the inplock.
1954b8d60729SRandall Stewart 	 */
1955b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1956df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
195753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1958df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1959ea9017fbSRandall Stewart 		if (ptr)
1960df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1961ea9017fbSRandall Stewart 		/* Release our temp reference */
1962ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1963ea9017fbSRandall Stewart 		cc_release(algo);
1964ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
1965df07bfdaSMichael Tuexen 		return (ECONNRESET);
1966df07bfdaSMichael Tuexen 	}
1967df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
1968df07bfdaSMichael Tuexen 	if (ptr != NULL)
1969b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
197000d3b744SMichael Tuexen 	cc_mem.tp = tp;
1971b8d60729SRandall Stewart 	/*
1972b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
1973b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
1974b8d60729SRandall Stewart 	 * Note here we init into stack memory.
1975b8d60729SRandall Stewart 	 */
1976b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
1977b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
1978b8d60729SRandall Stewart 	else
1979b8d60729SRandall Stewart 		error = 0;
1980b8d60729SRandall Stewart 	/*
1981b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
1982b8d60729SRandall Stewart 	 * should not fail we could in theory have a
1983b8d60729SRandall Stewart 	 * KASSERT here.
1984b8d60729SRandall Stewart 	 */
1985b8d60729SRandall Stewart 	if (error == 0) {
1986b8d60729SRandall Stewart 		/*
1987b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
1988b8d60729SRandall Stewart 		 * connection to the new CC module by
1989b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
1990b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
1991b8d60729SRandall Stewart 		 */
1992b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
1993e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1994ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
1995ea9017fbSRandall Stewart 		cc_detach(tp);
1996ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
1997e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1998ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
1999ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2000b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2001b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2002b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2003e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2004b8d60729SRandall Stewart 		}
2005b8d60729SRandall Stewart 	} else if (ptr)
2006b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2007b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2008ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2009ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2010ea9017fbSRandall Stewart 	cc_release(algo);
2011ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2012b8d60729SRandall Stewart 	return (error);
2013b8d60729SRandall Stewart }
2014b8d60729SRandall Stewart 
201555bceb1eSRandall Stewart int
201666fbc19fSGleb Smirnoff tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
201755bceb1eSRandall Stewart {
201866fbc19fSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
201955bceb1eSRandall Stewart 	int	error, opt, optval;
202055bceb1eSRandall Stewart 	u_int	ui;
202155bceb1eSRandall Stewart 	struct	tcp_info ti;
2022b2e60773SJohn Baldwin #ifdef KERN_TLS
2023b2e60773SJohn Baldwin 	struct tls_enable tls;
2024528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2025b2e60773SJohn Baldwin #endif
20262529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2027adc56f5aSEdward Tomasz Napierala #ifdef STATS
2028adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2029adc56f5aSEdward Tomasz Napierala #endif
2030af6fef3aSGleb Smirnoff 	size_t	len;
2031df8bae1dSRodney W. Grimes 
2032f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
203353af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2034fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2035528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2036f581a26eSGleb Smirnoff 
2037f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2038f581a26eSGleb Smirnoff #ifdef INET6
2039f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2040f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2041f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2042f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2043f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2044f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2045f581a26eSGleb Smirnoff 		}
2046f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2047f581a26eSGleb Smirnoff 		return (0);
2048f581a26eSGleb Smirnoff #endif
2049f581a26eSGleb Smirnoff #ifdef INET
2050f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2051f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2052f581a26eSGleb Smirnoff 		return (0);
2053f581a26eSGleb Smirnoff #endif
2054f581a26eSGleb Smirnoff 	}
2055f581a26eSGleb Smirnoff 
2056d519cedbSGleb Smirnoff 	/*
2057d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2058d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2059d519cedbSGleb Smirnoff 	 */
2060d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2061d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2062d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2063c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2064c8b53cedSMichael Tuexen 			return (EINVAL);
2065af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2066af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2067d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2068d519cedbSGleb Smirnoff 		if (error) {
2069af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2070d519cedbSGleb Smirnoff 			return (error);
2071d519cedbSGleb Smirnoff 		}
2072bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2073d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2074e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2075d519cedbSGleb Smirnoff 		else
2076d519cedbSGleb Smirnoff 			error = ENOENT;
2077d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2078d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2079af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2080af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2081d519cedbSGleb Smirnoff 		return (error);
2082d519cedbSGleb Smirnoff 	}
2083d519cedbSGleb Smirnoff 
2084cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2085cfe8b629SGarrett Wollman 	case SOPT_SET:
2086cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2087fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
208888f6b043SBruce M Simpson 		case TCP_MD5SIG:
20898501a69cSRobert Watson 			INP_WUNLOCK(inp);
209097453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2091fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2092fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
20931cfd4b53SBruce M Simpson 			if (error)
20941e8f5ffaSRobert Watson 				return (error);
209597453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
209609fe6320SNavdeep Parhar 			goto unlock_and_done;
2097fcf59617SAndrey V. Elsukov #endif /* IPSEC */
209809fe6320SNavdeep Parhar 
2099df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2100cfe8b629SGarrett Wollman 		case TCP_NOOPT:
21018501a69cSRobert Watson 			INP_WUNLOCK(inp);
2102cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2103cfe8b629SGarrett Wollman 			    sizeof optval);
2104cfe8b629SGarrett Wollman 			if (error)
21051e8f5ffaSRobert Watson 				return (error);
2106cfe8b629SGarrett Wollman 
21078501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2108cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2109cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2110cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2111cfe8b629SGarrett Wollman 				break;
2112cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2113cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2114cfe8b629SGarrett Wollman 				break;
2115cfe8b629SGarrett Wollman 			default:
2116cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2117cfe8b629SGarrett Wollman 				break;
2118cfe8b629SGarrett Wollman 			}
2119cfe8b629SGarrett Wollman 
2120cfe8b629SGarrett Wollman 			if (optval)
2121cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2122df8bae1dSRodney W. Grimes 			else
2123cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
212409fe6320SNavdeep Parhar unlock_and_done:
212509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
212609fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
212709fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
212809fe6320SNavdeep Parhar 				    sopt->sopt_name);
212909fe6320SNavdeep Parhar 			}
213009fe6320SNavdeep Parhar #endif
21318501a69cSRobert Watson 			INP_WUNLOCK(inp);
2132df8bae1dSRodney W. Grimes 			break;
2133df8bae1dSRodney W. Grimes 
2134007581c0SJonathan Lemon 		case TCP_NOPUSH:
21358501a69cSRobert Watson 			INP_WUNLOCK(inp);
2136007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2137007581c0SJonathan Lemon 			    sizeof optval);
2138007581c0SJonathan Lemon 			if (error)
21391e8f5ffaSRobert Watson 				return (error);
2140007581c0SJonathan Lemon 
21418501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2142007581c0SJonathan Lemon 			if (optval)
2143007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2144d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2145007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2146109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2147109eb549SGleb Smirnoff 					struct epoch_tracker et;
2148109eb549SGleb Smirnoff 
2149109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2150f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2151109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2152109eb549SGleb Smirnoff 				}
2153007581c0SJonathan Lemon 			}
215409fe6320SNavdeep Parhar 			goto unlock_and_done;
2155007581c0SJonathan Lemon 
21569e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21579e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21589e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21599e644c23SMichael Tuexen 			    sizeof optval);
21609e644c23SMichael Tuexen 			if (error)
21619e644c23SMichael Tuexen 				return (error);
21629e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
21639e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
21649e644c23SMichael Tuexen 				/* Its got to be in range */
21659e644c23SMichael Tuexen 				return (EINVAL);
21669e644c23SMichael Tuexen 			}
21679e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
21689e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
21699e644c23SMichael Tuexen 				return (EINVAL);
21709e644c23SMichael Tuexen 			}
21719e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
21729e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
21739e644c23SMichael Tuexen 				/* You can't change after you are connected */
21749e644c23SMichael Tuexen 				error = EINVAL;
21759e644c23SMichael Tuexen 			} else {
21769e644c23SMichael Tuexen 				/* Ok we are all good set the port */
21779e644c23SMichael Tuexen 				tp->t_port = htons(optval);
21789e644c23SMichael Tuexen 			}
21799e644c23SMichael Tuexen 			goto unlock_and_done;
21809e644c23SMichael Tuexen 
2181df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
21828501a69cSRobert Watson 			INP_WUNLOCK(inp);
2183cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2184cfe8b629SGarrett Wollman 			    sizeof optval);
2185cfe8b629SGarrett Wollman 			if (error)
21861e8f5ffaSRobert Watson 				return (error);
2187df8bae1dSRodney W. Grimes 
21888501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
218953369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2190fce03f85SRandall Stewart 			    optval + 40 >= V_tcp_minmss) {
2191cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2192fce03f85SRandall Stewart 				if (tp->t_maxseg < V_tcp_mssdflt) {
2193fce03f85SRandall Stewart 					/*
2194fce03f85SRandall Stewart 					 * The MSS is so small we should not process incoming
2195fce03f85SRandall Stewart 					 * SACK's since we are subject to attack in such a
2196fce03f85SRandall Stewart 					 * case.
2197fce03f85SRandall Stewart 					 */
2198fce03f85SRandall Stewart 					tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
2199fce03f85SRandall Stewart 				} else {
2200fce03f85SRandall Stewart 					tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
2201fce03f85SRandall Stewart 				}
2202fce03f85SRandall Stewart 			} else
2203a0292f23SGarrett Wollman 				error = EINVAL;
220409fe6320SNavdeep Parhar 			goto unlock_and_done;
2205a0292f23SGarrett Wollman 
2206b8af5dfaSRobert Watson 		case TCP_INFO:
22078501a69cSRobert Watson 			INP_WUNLOCK(inp);
2208b8af5dfaSRobert Watson 			error = EINVAL;
2209b8af5dfaSRobert Watson 			break;
2210b8af5dfaSRobert Watson 
2211adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2212adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2213adc56f5aSEdward Tomasz Napierala #ifdef STATS
2214adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2215adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2216adc56f5aSEdward Tomasz Napierala 			if (error)
2217adc56f5aSEdward Tomasz Napierala 				return (error);
2218adc56f5aSEdward Tomasz Napierala 
2219adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2220adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2221adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2222adc56f5aSEdward Tomasz Napierala 			else
2223adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2224adc56f5aSEdward Tomasz Napierala 
2225adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2226adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2227adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2228adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2229adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2230adc56f5aSEdward Tomasz Napierala 				sbp = t;
2231adc56f5aSEdward Tomasz Napierala 			}
2232adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2233adc56f5aSEdward Tomasz Napierala 
2234adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2235adc56f5aSEdward Tomasz Napierala #else
2236adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2237adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2238adc56f5aSEdward Tomasz Napierala 			break;
2239adc56f5aSEdward Tomasz Napierala 
2240dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2241ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
224273e263b1SGleb Smirnoff 			break;
2243dbc42409SLawrence Stewart 
2244a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2245a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2246a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2247a034518aSAndrew Gallatin 			    sizeof(optval));
2248a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2249a034518aSAndrew Gallatin 			if (!error)
2250a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2251a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2252a034518aSAndrew Gallatin 			break;
2253a034518aSAndrew Gallatin 
2254b2e60773SJohn Baldwin #ifdef KERN_TLS
2255b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2256b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
225785df11a1SRichard Scheffenegger 			error = ktls_copyin_tls_enable(sopt, &tls);
225885df11a1SRichard Scheffenegger 			if (error != 0)
2259b2e60773SJohn Baldwin 				break;
2260fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
226185df11a1SRichard Scheffenegger 			ktls_cleanup_tls_enable(&tls);
2262b2e60773SJohn Baldwin 			break;
2263b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2264b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2265b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
226685df11a1SRichard Scheffenegger 			if (error != 0)
2267b2e60773SJohn Baldwin 				return (error);
2268b2e60773SJohn Baldwin 
2269b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2270fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2271b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2272b2e60773SJohn Baldwin 			break;
2273f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2274f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
227585df11a1SRichard Scheffenegger 			error = ktls_copyin_tls_enable(sopt, &tls);
227685df11a1SRichard Scheffenegger 			if (error != 0)
2277f1f93475SJohn Baldwin 				break;
2278fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
227985df11a1SRichard Scheffenegger 			ktls_cleanup_tls_enable(&tls);
2280f1f93475SJohn Baldwin 			break;
2281b2e60773SJohn Baldwin #endif
228208af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
22839077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
22849077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
22859077f387SGleb Smirnoff 		case TCP_KEEPINIT:
22869077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
22879077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
22889077f387SGleb Smirnoff 			if (error)
22899077f387SGleb Smirnoff 				return (error);
22909077f387SGleb Smirnoff 
22919077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
22929077f387SGleb Smirnoff 				error = EINVAL;
22939077f387SGleb Smirnoff 				break;
22949077f387SGleb Smirnoff 			}
22959077f387SGleb Smirnoff 			ui *= hz;
22969077f387SGleb Smirnoff 
22979077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
22989077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
229908af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
230008af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
230108af8aacSRandall Stewart 				break;
230208af8aacSRandall Stewart 
23039077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23049077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23059077f387SGleb Smirnoff 				/*
23069077f387SGleb Smirnoff 				 * XXX: better check current remaining
23079077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
23089077f387SGleb Smirnoff 				 */
23099077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
23109077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
23119077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23129077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23139077f387SGleb Smirnoff 				break;
23149077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23159077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23169077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23179077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23189077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23199077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23209077f387SGleb Smirnoff 				break;
23219077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23229077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23239077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23249077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23259077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23269077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23279077f387SGleb Smirnoff 				break;
23289077f387SGleb Smirnoff 			}
232909fe6320SNavdeep Parhar 			goto unlock_and_done;
23309077f387SGleb Smirnoff 
233185c05144SGleb Smirnoff 		case TCP_KEEPCNT:
233285c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
233385c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
233485c05144SGleb Smirnoff 			if (error)
233585c05144SGleb Smirnoff 				return (error);
233685c05144SGleb Smirnoff 
233785c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
233885c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
233985c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
234085c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
234185c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
234285c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
234385c05144SGleb Smirnoff 			goto unlock_and_done;
234485c05144SGleb Smirnoff 
234586a996e6SHiren Panchasara #ifdef TCPPCAP
234686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
234786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
234886a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
234986a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
235086a996e6SHiren Panchasara 			    sizeof optval);
235186a996e6SHiren Panchasara 			if (error)
235286a996e6SHiren Panchasara 				return (error);
235386a996e6SHiren Panchasara 
235486a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
235586a996e6SHiren Panchasara 			if (optval >= 0)
2356399a5655SRichard Scheffenegger 				tcp_pcap_set_sock_max(
2357399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
235886a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
235986a996e6SHiren Panchasara 					optval);
236086a996e6SHiren Panchasara 			else
236186a996e6SHiren Panchasara 				error = EINVAL;
236286a996e6SHiren Panchasara 			goto unlock_and_done;
236386a996e6SHiren Panchasara #endif
236486a996e6SHiren Panchasara 
2365c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2366c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2367c560df6fSPatrick Kelsey 
2368281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2369c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2370c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2371281a0fd4SPatrick Kelsey 				return (EPERM);
2372281a0fd4SPatrick Kelsey 
2373c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2374c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2375281a0fd4SPatrick Kelsey 			if (error)
2376281a0fd4SPatrick Kelsey 				return (error);
2377281a0fd4SPatrick Kelsey 
2378281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2379d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2380d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2381d442a657SMichael Tuexen 				error = EINVAL;
2382d442a657SMichael Tuexen 				goto unlock_and_done;
2383d442a657SMichael Tuexen 			}
2384c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2385c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2386c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2387c560df6fSPatrick Kelsey 						error = EPERM;
2388c560df6fSPatrick Kelsey 						goto unlock_and_done;
2389c560df6fSPatrick Kelsey 					}
2390c560df6fSPatrick Kelsey 
2391c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2392281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2393281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2394c560df6fSPatrick Kelsey 				} else {
2395c560df6fSPatrick Kelsey 					/*
2396c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2397c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2398c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2399c560df6fSPatrick Kelsey 					 * connect.
2400c560df6fSPatrick Kelsey 					 */
2401c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2402c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2403c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2404c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2405c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2406c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2407c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2408c560df6fSPatrick Kelsey 					}
2409c560df6fSPatrick Kelsey 				}
2410d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2411281a0fd4SPatrick Kelsey 			} else
2412281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2413281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2414c560df6fSPatrick Kelsey 		}
2415281a0fd4SPatrick Kelsey 
2416e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24172529f56eSJonathan T. Looney 		case TCP_LOG:
24182529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24192529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24202529f56eSJonathan T. Looney 			    sizeof optval);
24212529f56eSJonathan T. Looney 			if (error)
24222529f56eSJonathan T. Looney 				return (error);
24232529f56eSJonathan T. Looney 
24242529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24252529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24262529f56eSJonathan T. Looney 			goto unlock_and_done;
24272529f56eSJonathan T. Looney 
24282529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24292529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24302529f56eSJonathan T. Looney 			error = EINVAL;
24312529f56eSJonathan T. Looney 			break;
24322529f56eSJonathan T. Looney 
24332529f56eSJonathan T. Looney 		case TCP_LOGID:
24342529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24352529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24362529f56eSJonathan T. Looney 			if (error)
24372529f56eSJonathan T. Looney 				break;
24382529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24392529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24402529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24412529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24422529f56eSJonathan T. Looney 			break;
24432529f56eSJonathan T. Looney 
24442529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24452529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24462529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24472529f56eSJonathan T. Looney 			error =
24482529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_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 			if (sopt->sopt_name == TCP_LOGDUMP) {
24542529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24552529f56eSJonathan T. Looney 				    M_WAITOK, true);
24562529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24572529f56eSJonathan T. Looney 			} else {
24582529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24592529f56eSJonathan T. Looney 				/*
24602529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24612529f56eSJonathan T. Looney 				 * INP lock.
24622529f56eSJonathan T. Looney 				 */
24632529f56eSJonathan T. Looney 			}
24642529f56eSJonathan T. Looney 			break;
2465e24e5683SJonathan T. Looney #endif
24662529f56eSJonathan T. Looney 
2467df8bae1dSRodney W. Grimes 		default:
24688501a69cSRobert Watson 			INP_WUNLOCK(inp);
2469df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2470df8bae1dSRodney W. Grimes 			break;
2471df8bae1dSRodney W. Grimes 		}
2472df8bae1dSRodney W. Grimes 		break;
2473df8bae1dSRodney W. Grimes 
2474cfe8b629SGarrett Wollman 	case SOPT_GET:
24751e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2476cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2477fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
247888f6b043SBruce M Simpson 		case TCP_MD5SIG:
24798501a69cSRobert Watson 			INP_WUNLOCK(inp);
248097453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2481fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2482fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
24831cfd4b53SBruce M Simpson 			break;
2484265ed012SBruce M Simpson #endif
24851e8f5ffaSRobert Watson 
2486df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2487cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
24888501a69cSRobert Watson 			INP_WUNLOCK(inp);
2489b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2490df8bae1dSRodney W. Grimes 			break;
2491df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2492cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
24938501a69cSRobert Watson 			INP_WUNLOCK(inp);
2494b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2495df8bae1dSRodney W. Grimes 			break;
24969e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
24979e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
24989e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
24999e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25009e644c23SMichael Tuexen 			break;
2501a0292f23SGarrett Wollman 		case TCP_NOOPT:
2502cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
25038501a69cSRobert Watson 			INP_WUNLOCK(inp);
2504b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2505a0292f23SGarrett Wollman 			break;
2506a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2507cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
25088501a69cSRobert Watson 			INP_WUNLOCK(inp);
2509b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2510b8af5dfaSRobert Watson 			break;
2511b8af5dfaSRobert Watson 		case TCP_INFO:
2512b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
25138501a69cSRobert Watson 			INP_WUNLOCK(inp);
2514b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2515a0292f23SGarrett Wollman 			break;
2516adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2517adc56f5aSEdward Tomasz Napierala 			{
2518adc56f5aSEdward Tomasz Napierala #ifdef STATS
2519adc56f5aSEdward Tomasz Napierala 			int nheld;
2520adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2521adc56f5aSEdward Tomasz Napierala 
2522adc56f5aSEdward Tomasz Napierala 			error = 0;
2523adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2524adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2525adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2526adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2527adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2528adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2529adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2530adc56f5aSEdward Tomasz Napierala 			else
2531adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2532adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2533adc56f5aSEdward Tomasz Napierala 			if (error)
2534adc56f5aSEdward Tomasz Napierala 				break;
2535adc56f5aSEdward Tomasz Napierala 
2536adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2537adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2538adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2539adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2540adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2541adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2542adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2543adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2544adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2545adc56f5aSEdward Tomasz Napierala 				break;
2546adc56f5aSEdward Tomasz Napierala 			}
2547adc56f5aSEdward Tomasz Napierala 
2548adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2549adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2550adc56f5aSEdward Tomasz Napierala 				goto unhold;
2551adc56f5aSEdward Tomasz Napierala 
2552adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2553adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2554adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2555adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2556adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2557adc56f5aSEdward Tomasz Napierala unhold:
2558adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2559adc56f5aSEdward Tomasz Napierala #else
2560adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2561adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2562adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2563adc56f5aSEdward Tomasz Napierala 			break;
2564adc56f5aSEdward Tomasz Napierala 			}
2565dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2566af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2567dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2568af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2569dbc42409SLawrence Stewart 			break;
257008af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25712f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
25722f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
25732f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
25742f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
25752f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
257608af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
257708af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
257808af8aacSRandall Stewart 				break;
25792f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
25805a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
25812f3eb7f4SGleb Smirnoff 				break;
25822f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
25835a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
25842f3eb7f4SGleb Smirnoff 				break;
25852f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
25865a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
25872f3eb7f4SGleb Smirnoff 				break;
25882f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
25895a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
25902f3eb7f4SGleb Smirnoff 				break;
25912f3eb7f4SGleb Smirnoff 			}
25922f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
25932f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
25942f3eb7f4SGleb Smirnoff 			break;
259586a996e6SHiren Panchasara #ifdef TCPPCAP
259686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
259786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
2598399a5655SRichard Scheffenegger 			optval = tcp_pcap_get_sock_max(
2599399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
260086a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
260186a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
260286a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
260386a996e6SHiren Panchasara 			break;
260486a996e6SHiren Panchasara #endif
2605281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2606281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2607281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2608281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2609281a0fd4SPatrick Kelsey 			break;
2610e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
26112529f56eSJonathan T. Looney 		case TCP_LOG:
261269c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
26132529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26142529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
26152529f56eSJonathan T. Looney 			break;
26162529f56eSJonathan T. Looney 		case TCP_LOGBUF:
26172529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
26182529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26192529f56eSJonathan T. Looney 			break;
26202529f56eSJonathan T. Looney 		case TCP_LOGID:
26212529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26222529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26232529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26242529f56eSJonathan T. Looney 			break;
26252529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26262529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26272529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26282529f56eSJonathan T. Looney 			error = EINVAL;
26292529f56eSJonathan T. Looney 			break;
2630e24e5683SJonathan T. Looney #endif
2631b2e60773SJohn Baldwin #ifdef KERN_TLS
2632b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2633fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2634b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2635bf256782SMark Johnston 			if (error == 0)
2636bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2637bf256782SMark Johnston 				    sizeof(optval));
2638b2e60773SJohn Baldwin 			break;
2639f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2640fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2641f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2642bf256782SMark Johnston 			if (error == 0)
2643bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2644bf256782SMark Johnston 				    sizeof(optval));
2645f1f93475SJohn Baldwin 			break;
2646b2e60773SJohn Baldwin #endif
2647df8bae1dSRodney W. Grimes 		default:
26488501a69cSRobert Watson 			INP_WUNLOCK(inp);
2649df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2650df8bae1dSRodney W. Grimes 			break;
2651df8bae1dSRodney W. Grimes 		}
2652df8bae1dSRodney W. Grimes 		break;
2653df8bae1dSRodney W. Grimes 	}
2654df8bae1dSRodney W. Grimes 	return (error);
2655df8bae1dSRodney W. Grimes }
26568501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2657bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2658df8bae1dSRodney W. Grimes 
265926e30fbbSDavid Greenman /*
2660df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2661df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2662df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2663df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2664df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2665df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2666df8bae1dSRodney W. Grimes  */
2667623dce13SRobert Watson static void
2668ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2669df8bae1dSRodney W. Grimes {
26709eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
26719eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2672e6e0b5ffSRobert Watson 
267397a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26748501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2675df8bae1dSRodney W. Grimes 
2676623dce13SRobert Watson 	/*
2677623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2678623dce13SRobert Watson 	 * socket is still open.
2679623dce13SRobert Watson 	 */
26808db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
2681dd7b86e2SGleb Smirnoff 	    !(tp->t_state > TCPS_LISTEN && (tp->t_flags & TF_FASTOPEN))) {
2682df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2683623dce13SRobert Watson 		KASSERT(tp != NULL,
2684623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2685623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2686243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2687623dce13SRobert Watson 		KASSERT(tp != NULL,
2688623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2689623dce13SRobert Watson 	} else {
2690df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2691df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2692623dce13SRobert Watson 		tcp_usrclosed(tp);
2693ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2694f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2695f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2696df8bae1dSRodney W. Grimes 	}
2697df8bae1dSRodney W. Grimes }
2698df8bae1dSRodney W. Grimes 
2699df8bae1dSRodney W. Grimes /*
2700df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2701df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2702df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2703df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2704df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2705df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2706df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2707df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2708df8bae1dSRodney W. Grimes  */
2709623dce13SRobert Watson static void
2710ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2711df8bae1dSRodney W. Grimes {
2712df8bae1dSRodney W. Grimes 
271397a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27149eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2715e6e0b5ffSRobert Watson 
2716df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2717df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
271809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
271909fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
272009fe6320SNavdeep Parhar #endif
2721550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2722bc65987aSKip Macy 		/* FALLTHROUGH */
2723bc65987aSKip Macy 	case TCPS_CLOSED:
2724df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2725623dce13SRobert Watson 		/*
2726623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2727623dce13SRobert Watson 		 * still open.
2728623dce13SRobert Watson 		 */
2729623dce13SRobert Watson 		KASSERT(tp != NULL,
2730623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2731df8bae1dSRodney W. Grimes 		break;
2732df8bae1dSRodney W. Grimes 
2733a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2734df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2735a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2736a0292f23SGarrett Wollman 		break;
2737a0292f23SGarrett Wollman 
2738df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
273957f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2740df8bae1dSRodney W. Grimes 		break;
2741df8bae1dSRodney W. Grimes 
2742df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
274357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2744df8bae1dSRodney W. Grimes 		break;
2745df8bae1dSRodney W. Grimes 	}
274608af8aacSRandall Stewart 	if (tp->t_acktime == 0)
274708af8aacSRandall Stewart 		tp->t_acktime = ticks;
2748abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27493eeb22cbSRichard Scheffenegger 		tcp_free_sackholes(tp);
27509eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2751abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27527c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27537c72af87SMohan Srinivasan 			int timeout;
27547c72af87SMohan Srinivasan 
27557c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27569077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2757b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2758b6239c4aSAndras Olah 		}
2759df8bae1dSRodney W. Grimes 	}
27607c72af87SMohan Srinivasan }
2761497057eeSRobert Watson 
2762497057eeSRobert Watson #ifdef DDB
2763497057eeSRobert Watson static void
2764497057eeSRobert Watson db_print_indent(int indent)
2765497057eeSRobert Watson {
2766497057eeSRobert Watson 	int i;
2767497057eeSRobert Watson 
2768497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2769497057eeSRobert Watson 		db_printf(" ");
2770497057eeSRobert Watson }
2771497057eeSRobert Watson 
2772497057eeSRobert Watson static void
2773497057eeSRobert Watson db_print_tstate(int t_state)
2774497057eeSRobert Watson {
2775497057eeSRobert Watson 
2776497057eeSRobert Watson 	switch (t_state) {
2777497057eeSRobert Watson 	case TCPS_CLOSED:
2778497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2779497057eeSRobert Watson 		return;
2780497057eeSRobert Watson 
2781497057eeSRobert Watson 	case TCPS_LISTEN:
2782497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2783497057eeSRobert Watson 		return;
2784497057eeSRobert Watson 
2785497057eeSRobert Watson 	case TCPS_SYN_SENT:
2786497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2787497057eeSRobert Watson 		return;
2788497057eeSRobert Watson 
2789497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2790497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2791497057eeSRobert Watson 		return;
2792497057eeSRobert Watson 
2793497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2794497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2795497057eeSRobert Watson 		return;
2796497057eeSRobert Watson 
2797497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2798497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2799497057eeSRobert Watson 		return;
2800497057eeSRobert Watson 
2801497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2802497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2803497057eeSRobert Watson 		return;
2804497057eeSRobert Watson 
2805497057eeSRobert Watson 	case TCPS_CLOSING:
2806497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2807497057eeSRobert Watson 		return;
2808497057eeSRobert Watson 
2809497057eeSRobert Watson 	case TCPS_LAST_ACK:
2810497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2811497057eeSRobert Watson 		return;
2812497057eeSRobert Watson 
2813497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2814497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2815497057eeSRobert Watson 		return;
2816497057eeSRobert Watson 
2817497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2818497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2819497057eeSRobert Watson 		return;
2820497057eeSRobert Watson 
2821497057eeSRobert Watson 	default:
2822497057eeSRobert Watson 		db_printf("unknown");
2823497057eeSRobert Watson 		return;
2824497057eeSRobert Watson 	}
2825497057eeSRobert Watson }
2826497057eeSRobert Watson 
2827497057eeSRobert Watson static void
2828497057eeSRobert Watson db_print_tflags(u_int t_flags)
2829497057eeSRobert Watson {
2830497057eeSRobert Watson 	int comma;
2831497057eeSRobert Watson 
2832497057eeSRobert Watson 	comma = 0;
2833497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2834497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2835497057eeSRobert Watson 		comma = 1;
2836497057eeSRobert Watson 	}
2837497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2838497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2839497057eeSRobert Watson 		comma = 1;
2840497057eeSRobert Watson 	}
2841497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2842497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2843497057eeSRobert Watson 		comma = 1;
2844497057eeSRobert Watson 	}
2845497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2846497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2847497057eeSRobert Watson 		comma = 1;
2848497057eeSRobert Watson 	}
2849497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2850497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2851497057eeSRobert Watson 		comma = 1;
2852497057eeSRobert Watson 	}
2853497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2854497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2855497057eeSRobert Watson 		comma = 1;
2856497057eeSRobert Watson 	}
2857497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2858497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2859497057eeSRobert Watson 		comma = 1;
2860497057eeSRobert Watson 	}
2861497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2862497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2863497057eeSRobert Watson 		comma = 1;
2864497057eeSRobert Watson 	}
2865497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2866497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2867497057eeSRobert Watson 		comma = 1;
2868497057eeSRobert Watson 	}
2869497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2870497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2871497057eeSRobert Watson 		comma = 1;
2872497057eeSRobert Watson 	}
2873497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2874497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2875497057eeSRobert Watson 		comma = 1;
2876497057eeSRobert Watson 	}
2877497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2878497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2879497057eeSRobert Watson 		comma = 1;
2880497057eeSRobert Watson 	}
2881497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2882497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2883497057eeSRobert Watson 		comma = 1;
2884497057eeSRobert Watson 	}
28853f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
28863f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
28873f169c54SRichard Scheffenegger 		comma = 1;
28883f169c54SRichard Scheffenegger 	}
2889093d9b46SMichael Tuexen 	if (t_flags & TF_WAKESOR) {
2890093d9b46SMichael Tuexen 		db_printf("%sTF_WAKESOR", comma ? ", " : "");
2891093d9b46SMichael Tuexen 		comma = 1;
2892093d9b46SMichael Tuexen 	}
2893093d9b46SMichael Tuexen 	if (t_flags & TF_GPUTINPROG) {
2894093d9b46SMichael Tuexen 		db_printf("%sTF_GPUTINPROG", comma ? ", " : "");
2895093d9b46SMichael Tuexen 		comma = 1;
2896093d9b46SMichael Tuexen 	}
2897497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2898497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2899497057eeSRobert Watson 		comma = 1;
2900497057eeSRobert Watson 	}
2901493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2902493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2903497057eeSRobert Watson 		comma = 1;
2904497057eeSRobert Watson 	}
2905497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2906497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2907497057eeSRobert Watson 		comma = 1;
2908497057eeSRobert Watson 	}
2909497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2910497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2911497057eeSRobert Watson 		comma = 1;
2912497057eeSRobert Watson 	}
2913497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2914497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2915497057eeSRobert Watson 		comma = 1;
2916497057eeSRobert Watson 	}
2917497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2918497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2919497057eeSRobert Watson 		comma = 1;
2920497057eeSRobert Watson 	}
2921497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2922497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2923497057eeSRobert Watson 		comma = 1;
2924497057eeSRobert Watson 	}
2925497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2926497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2927497057eeSRobert Watson 		comma = 1;
2928497057eeSRobert Watson 	}
2929497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2930497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2931497057eeSRobert Watson 		comma = 1;
2932497057eeSRobert Watson 	}
2933093d9b46SMichael Tuexen 	if (t_flags & TF_TOE) {
2934093d9b46SMichael Tuexen 		db_printf("%sTF_TOE", comma ? ", " : "");
2935093d9b46SMichael Tuexen 		comma = 1;
2936093d9b46SMichael Tuexen 	}
2937093d9b46SMichael Tuexen 	if (t_flags & TF_CLOSED) {
2938093d9b46SMichael Tuexen 		db_printf("%sTF_CLOSED", comma ? ", " : "");
2939093d9b46SMichael Tuexen 		comma = 1;
2940093d9b46SMichael Tuexen 	}
2941093d9b46SMichael Tuexen 	if (t_flags & TF_SENTSYN) {
2942093d9b46SMichael Tuexen 		db_printf("%sTF_SENTSYN", comma ? ", " : "");
2943093d9b46SMichael Tuexen 		comma = 1;
2944093d9b46SMichael Tuexen 	}
2945093d9b46SMichael Tuexen 	if (t_flags & TF_LRD) {
2946093d9b46SMichael Tuexen 		db_printf("%sTF_LRD", comma ? ", " : "");
2947093d9b46SMichael Tuexen 		comma = 1;
2948093d9b46SMichael Tuexen 	}
2949093d9b46SMichael Tuexen 	if (t_flags & TF_CONGRECOVERY) {
2950093d9b46SMichael Tuexen 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2951093d9b46SMichael Tuexen 		comma = 1;
2952093d9b46SMichael Tuexen 	}
2953093d9b46SMichael Tuexen 	if (t_flags & TF_WASCRECOVERY) {
2954093d9b46SMichael Tuexen 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
2955093d9b46SMichael Tuexen 		comma = 1;
2956093d9b46SMichael Tuexen 	}
2957281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2958281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2959281a0fd4SPatrick Kelsey 		comma = 1;
2960281a0fd4SPatrick Kelsey 	}
2961497057eeSRobert Watson }
2962497057eeSRobert Watson 
2963497057eeSRobert Watson static void
29643cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29653cf38784SMichael Tuexen {
29663cf38784SMichael Tuexen 	int comma;
29673cf38784SMichael Tuexen 
29683cf38784SMichael Tuexen 	comma = 0;
29693f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29703f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29713f169c54SRichard Scheffenegger 		comma = 1;
29723f169c54SRichard Scheffenegger 	}
29733f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29743f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29753f169c54SRichard Scheffenegger 		comma = 1;
29763f169c54SRichard Scheffenegger 	}
29773f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29783f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29793f169c54SRichard Scheffenegger 		comma = 1;
29803f169c54SRichard Scheffenegger 	}
29813f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29823f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29833f169c54SRichard Scheffenegger 		comma = 1;
29843f169c54SRichard Scheffenegger 	}
29853f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29863f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29873f169c54SRichard Scheffenegger 		comma = 1;
29883f169c54SRichard Scheffenegger 	}
29893cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29903cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29913cf38784SMichael Tuexen 		comma = 1;
29923cf38784SMichael Tuexen 	}
29933f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
29943f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
29953f169c54SRichard Scheffenegger 		comma = 1;
29963f169c54SRichard Scheffenegger 	}
29973f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
29983f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
29993f169c54SRichard Scheffenegger 		comma = 1;
30003f169c54SRichard Scheffenegger 	}
30013f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
30023f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
30033f169c54SRichard Scheffenegger 		comma = 1;
30043f169c54SRichard Scheffenegger 	}
3005093d9b46SMichael Tuexen 	if (t_flags2 & TF2_HPTS_CPU_SET) {
3006093d9b46SMichael Tuexen 		db_printf("%sTF2_HPTS_CPU_SET", comma ? ", " : "");
3007093d9b46SMichael Tuexen 		comma = 1;
3008093d9b46SMichael Tuexen 	}
30093f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
30103f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30113f169c54SRichard Scheffenegger 		comma = 1;
30123f169c54SRichard Scheffenegger 	}
3013093d9b46SMichael Tuexen 	if (t_flags2 & TF2_ECN_USE_ECT1) {
3014093d9b46SMichael Tuexen 		db_printf("%sTF2_ECN_USE_ECT1", comma ? ", " : "");
3015093d9b46SMichael Tuexen 		comma = 1;
3016093d9b46SMichael Tuexen 	}
3017093d9b46SMichael Tuexen 	if (t_flags2 & TF2_TCP_ACCOUNTING) {
3018093d9b46SMichael Tuexen 		db_printf("%sTF2_TCP_ACCOUNTING", comma ? ", " : "");
3019093d9b46SMichael Tuexen 		comma = 1;
3020093d9b46SMichael Tuexen 	}
3021093d9b46SMichael Tuexen 	if (t_flags2 & TF2_HPTS_CALLS) {
3022093d9b46SMichael Tuexen 		db_printf("%sTF2_HPTS_CALLS", comma ? ", " : "");
3023093d9b46SMichael Tuexen 		comma = 1;
3024093d9b46SMichael Tuexen 	}
3025093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_L_ACKS) {
3026093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_L_ACKS", comma ? ", " : "");
3027093d9b46SMichael Tuexen 		comma = 1;
3028093d9b46SMichael Tuexen 	}
3029093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_ACKCMP) {
3030093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_ACKCMP", comma ? ", " : "");
3031093d9b46SMichael Tuexen 		comma = 1;
3032093d9b46SMichael Tuexen 	}
3033093d9b46SMichael Tuexen 	if (t_flags2 & TF2_SUPPORTS_MBUFQ) {
3034093d9b46SMichael Tuexen 		db_printf("%sTF2_SUPPORTS_MBUFQ", comma ? ", " : "");
3035093d9b46SMichael Tuexen 		comma = 1;
3036093d9b46SMichael Tuexen 	}
3037093d9b46SMichael Tuexen 	if (t_flags2 & TF2_MBUF_QUEUE_READY) {
3038093d9b46SMichael Tuexen 		db_printf("%sTF2_MBUF_QUEUE_READY", comma ? ", " : "");
3039093d9b46SMichael Tuexen 		comma = 1;
3040093d9b46SMichael Tuexen 	}
3041093d9b46SMichael Tuexen 	if (t_flags2 & TF2_DONT_SACK_QUEUE) {
3042093d9b46SMichael Tuexen 		db_printf("%sTF2_DONT_SACK_QUEUE", comma ? ", " : "");
3043093d9b46SMichael Tuexen 		comma = 1;
3044093d9b46SMichael Tuexen 	}
3045093d9b46SMichael Tuexen 	if (t_flags2 & TF2_CANNOT_DO_ECN) {
3046093d9b46SMichael Tuexen 		db_printf("%sTF2_CANNOT_DO_ECN", comma ? ", " : "");
3047093d9b46SMichael Tuexen 		comma = 1;
3048093d9b46SMichael Tuexen 	}
3049093d9b46SMichael Tuexen 	if (t_flags2 & TF2_PROC_SACK_PROHIBIT) {
3050093d9b46SMichael Tuexen 		db_printf("%sTF2_PROC_SACK_PROHIBIT", comma ? ", " : "");
3051093d9b46SMichael Tuexen 		comma = 1;
3052093d9b46SMichael Tuexen 	}
3053093d9b46SMichael Tuexen 	if (t_flags2 & TF2_IPSEC_TSO) {
3054093d9b46SMichael Tuexen 		db_printf("%sTF2_IPSEC_TSO", comma ? ", " : "");
3055093d9b46SMichael Tuexen 		comma = 1;
3056093d9b46SMichael Tuexen 	}
3057093d9b46SMichael Tuexen 	if (t_flags2 & TF2_NO_ISS_CHECK) {
3058093d9b46SMichael Tuexen 		db_printf("%sTF2_NO_ISS_CHECK", comma ? ", " : "");
3059093d9b46SMichael Tuexen 		comma = 1;
3060093d9b46SMichael Tuexen 	}
30613cf38784SMichael Tuexen }
30623cf38784SMichael Tuexen 
30633cf38784SMichael Tuexen static void
3064497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3065497057eeSRobert Watson {
3066497057eeSRobert Watson 	int comma;
3067497057eeSRobert Watson 
3068497057eeSRobert Watson 	comma = 0;
3069497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3070497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3071497057eeSRobert Watson 		comma = 1;
3072497057eeSRobert Watson 	}
3073497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3074497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3075497057eeSRobert Watson 		comma = 1;
3076497057eeSRobert Watson 	}
3077497057eeSRobert Watson }
3078497057eeSRobert Watson 
3079497057eeSRobert Watson static void
3080497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3081497057eeSRobert Watson {
3082497057eeSRobert Watson 
3083497057eeSRobert Watson 	db_print_indent(indent);
3084497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3085497057eeSRobert Watson 
3086497057eeSRobert Watson 	indent += 2;
3087497057eeSRobert Watson 
3088497057eeSRobert Watson 	db_print_indent(indent);
3089497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3090c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3091497057eeSRobert Watson 
3092497057eeSRobert Watson 	db_print_indent(indent);
3093446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3094446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3095497057eeSRobert Watson 
3096497057eeSRobert Watson 	db_print_indent(indent);
3097497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3098497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3099497057eeSRobert Watson 	db_printf(")\n");
3100497057eeSRobert Watson 
3101497057eeSRobert Watson 	db_print_indent(indent);
3102497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3103497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3104497057eeSRobert Watson 	db_printf(")\n");
3105497057eeSRobert Watson 
3106497057eeSRobert Watson 	db_print_indent(indent);
31073cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
31083cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
31093cf38784SMichael Tuexen 	db_printf(")\n");
31103cf38784SMichael Tuexen 
31113cf38784SMichael Tuexen 	db_print_indent(indent);
3112fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3113497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3114497057eeSRobert Watson 
3115497057eeSRobert Watson 	db_print_indent(indent);
3116497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3117497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3118497057eeSRobert Watson 
3119497057eeSRobert Watson 	db_print_indent(indent);
3120497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3121497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3122497057eeSRobert Watson 
3123497057eeSRobert Watson 	db_print_indent(indent);
31243ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3125497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3126497057eeSRobert Watson 
3127497057eeSRobert Watson 	db_print_indent(indent);
31283ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
31291c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3130497057eeSRobert Watson 
3131497057eeSRobert Watson 	db_print_indent(indent);
31323ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
31331c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3134497057eeSRobert Watson 
3135497057eeSRobert Watson 	db_print_indent(indent);
31360c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
31370c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3138497057eeSRobert Watson 
3139497057eeSRobert Watson 	db_print_indent(indent);
31401c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
31411c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3142497057eeSRobert Watson 
3143497057eeSRobert Watson 	db_print_indent(indent);
31441c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
31451c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3146497057eeSRobert Watson 
3147497057eeSRobert Watson 	db_print_indent(indent);
3148bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3149bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3150497057eeSRobert Watson 
3151497057eeSRobert Watson 	db_print_indent(indent);
315218b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3153497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3154497057eeSRobert Watson 
3155497057eeSRobert Watson 	db_print_indent(indent);
3156497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3157497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3158497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3159497057eeSRobert Watson 
3160497057eeSRobert Watson 	db_print_indent(indent);
3161497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3162497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3163497057eeSRobert Watson 
3164497057eeSRobert Watson 	db_print_indent(indent);
31659f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31661a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3167497057eeSRobert Watson 
3168497057eeSRobert Watson 	db_print_indent(indent);
3169497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31703ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3171497057eeSRobert Watson 
3172497057eeSRobert Watson 	db_print_indent(indent);
31733ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31749f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3175497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3176497057eeSRobert Watson 
3177497057eeSRobert Watson 	db_print_indent(indent);
31783529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31793529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3180497057eeSRobert Watson 
3181497057eeSRobert Watson 	db_print_indent(indent);
3182a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3183a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3184497057eeSRobert Watson 
3185497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3186497057eeSRobert Watson 
3187497057eeSRobert Watson 	db_print_indent(indent);
3188497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3189497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3190497057eeSRobert Watson }
3191497057eeSRobert Watson 
3192497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3193497057eeSRobert Watson {
3194497057eeSRobert Watson 	struct tcpcb *tp;
3195497057eeSRobert Watson 
3196497057eeSRobert Watson 	if (!have_addr) {
3197497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3198497057eeSRobert Watson 		return;
3199497057eeSRobert Watson 	}
3200497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3201497057eeSRobert Watson 
3202497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3203497057eeSRobert Watson }
3204497057eeSRobert Watson #endif
3205