xref: /freebsd/sys/netinet/tcp_usrreq.c (revision de0a2eb2ef86f6c41157529b827da06f47190e8c)
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  *
371fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
404b421e2dSMike Silbersack #include <sys/cdefs.h>
414b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
424b421e2dSMike Silbersack 
43497057eeSRobert Watson #include "opt_ddb.h"
441cfd4b53SBruce M Simpson #include "opt_inet.h"
45fb59c426SYoshinobu Inoue #include "opt_inet6.h"
46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
47b2e60773SJohn Baldwin #include "opt_kern_tls.h"
480cc12cc5SJoerg Wunsch 
49df8bae1dSRodney W. Grimes #include <sys/param.h>
50df8bae1dSRodney W. Grimes #include <sys/systm.h>
51adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
529077f387SGleb Smirnoff #include <sys/limits.h>
53f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5455bceb1eSRandall Stewart #include <sys/refcount.h>
55c7a82f90SGarrett Wollman #include <sys/kernel.h>
56b2e60773SJohn Baldwin #include <sys/ktls.h>
57adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5898163b98SPoul-Henning Kamp #include <sys/sysctl.h>
59df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
60fb59c426SYoshinobu Inoue #ifdef INET6
61fb59c426SYoshinobu Inoue #include <sys/domain.h>
62fb59c426SYoshinobu Inoue #endif /* INET6 */
63df8bae1dSRodney W. Grimes #include <sys/socket.h>
64df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
65df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6691421ba2SRobert Watson #include <sys/proc.h>
6791421ba2SRobert Watson #include <sys/jail.h>
68adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
69df8bae1dSRodney W. Grimes 
70497057eeSRobert Watson #ifdef DDB
71497057eeSRobert Watson #include <ddb/ddb.h>
72497057eeSRobert Watson #endif
73497057eeSRobert Watson 
74df8bae1dSRodney W. Grimes #include <net/if.h>
7576039bc8SGleb Smirnoff #include <net/if_var.h>
76df8bae1dSRodney W. Grimes #include <net/route.h>
77530c0060SRobert Watson #include <net/vnet.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <netinet/in.h>
805d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
82b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
83b5e8ce9fSBruce Evans #include <netinet/in_var.h>
843b0ee680SRichard Scheffenegger #include <netinet/ip.h>
85df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
86fb59c426SYoshinobu Inoue #ifdef INET6
87b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
88b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
89fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
90a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
91fb59c426SYoshinobu Inoue #endif
922de3e790SGleb Smirnoff #include <netinet/tcp.h>
93df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
95df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
96df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
972529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
98df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
994644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
100c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
101fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
10286a996e6SHiren Panchasara #ifdef TCPPCAP
10386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10486a996e6SHiren Panchasara #endif
10509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
106bc65987aSKip Macy #include <netinet/tcp_offload.h>
10709fe6320SNavdeep Parhar #endif
108fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
109df8bae1dSRodney W. Grimes 
110adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
111adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
112adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
113adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
114adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
115adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
116adc56f5aSEdward Tomasz Napierala 
117df8bae1dSRodney W. Grimes /*
118df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
119df8bae1dSRodney W. Grimes  */
120b287c6c7SBjoern A. Zeeb #ifdef INET
121a9d22cceSGleb Smirnoff static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
1224d77a549SAlfred Perlstein 		    struct thread *td);
123b287c6c7SBjoern A. Zeeb #endif /* INET */
124fb59c426SYoshinobu Inoue #ifdef INET6
125a9d22cceSGleb Smirnoff static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
1264d77a549SAlfred Perlstein 		    struct thread *td);
127fb59c426SYoshinobu Inoue #endif /* INET6 */
128623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
129623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
130b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1312c37256eSGarrett Wollman 
132d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
133d3b6c96bSRandall Stewart 
13400812bbdSMichael Tuexen static void
13500812bbdSMichael Tuexen tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
13600812bbdSMichael Tuexen {
13700812bbdSMichael Tuexen 	struct tcp_log_buffer *lgb;
13800812bbdSMichael Tuexen 
139c2399dd2SMichael Tuexen 	KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
14000812bbdSMichael Tuexen 	INP_WLOCK_ASSERT(tptoinpcb(tp));
14169c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
14269c7c811SRandall Stewart 		lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
14300812bbdSMichael Tuexen 		    0, NULL, false, NULL, NULL, 0, NULL);
14400812bbdSMichael Tuexen 	} else {
14500812bbdSMichael Tuexen 		lgb = NULL;
14600812bbdSMichael Tuexen 	}
14700812bbdSMichael Tuexen 	if (lgb != NULL) {
14800812bbdSMichael Tuexen 		if (error >= 0) {
14900812bbdSMichael Tuexen 			lgb->tlb_errno = (uint32_t)error;
15000812bbdSMichael Tuexen 		}
15100812bbdSMichael Tuexen 		lgb->tlb_flex1 = pru;
15200812bbdSMichael Tuexen 	}
15300812bbdSMichael Tuexen }
15400812bbdSMichael Tuexen 
1552c37256eSGarrett Wollman /*
1562c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1572c37256eSGarrett Wollman  * and an internet control block.
1582c37256eSGarrett Wollman  */
1592c37256eSGarrett Wollman static int
160b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1612c37256eSGarrett Wollman {
162f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
163623dce13SRobert Watson 	struct tcpcb *tp = NULL;
164623dce13SRobert Watson 	int error;
1652c37256eSGarrett Wollman 
166623dce13SRobert Watson 	inp = sotoinpcb(so);
167623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1682c37256eSGarrett Wollman 
1690f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1702c37256eSGarrett Wollman 	if (error)
1712c37256eSGarrett Wollman 		goto out;
1722c37256eSGarrett Wollman 
1730f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1740f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1750f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1767669c586SGleb Smirnoff 	if (error)
1770f6385e7SGleb Smirnoff 		goto out;
1780f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1790f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1800f6385e7SGleb Smirnoff 	if (tp == NULL) {
1817669c586SGleb Smirnoff 		error = ENOBUFS;
1820f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1830f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1840f6385e7SGleb Smirnoff 		goto out;
1850f6385e7SGleb Smirnoff 	}
1860f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
18700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ATTACH, error);
1880f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1890f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1902c37256eSGarrett Wollman out:
1915d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1920f6385e7SGleb Smirnoff 	return (error);
1932c37256eSGarrett Wollman }
1942c37256eSGarrett Wollman 
1952c37256eSGarrett Wollman /*
1963fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
197a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
198a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
199a152f8a3SRobert Watson  * inpcb state: time wait.
2002c37256eSGarrett Wollman  */
201bc725eafSRobert Watson static void
2023fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2032c37256eSGarrett Wollman {
2043fed74e9SGleb Smirnoff 	struct inpcb *inp;
2052c37256eSGarrett Wollman 	struct tcpcb *tp;
2062c37256eSGarrett Wollman 
2073fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2083fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2093fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2103fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2113fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
212953b5606SRobert Watson 
213a152f8a3SRobert Watson 	tp = intotcpcb(inp);
214a152f8a3SRobert Watson 
2151b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2161b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2171b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2189c3507f9SGleb Smirnoff 
219623dce13SRobert Watson 	tcp_discardcb(tp);
220623dce13SRobert Watson 	in_pcbdetach(inp);
2210206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
222623dce13SRobert Watson }
223c78cbc7bSRobert Watson 
224b287c6c7SBjoern A. Zeeb #ifdef INET
2252c37256eSGarrett Wollman /*
2262c37256eSGarrett Wollman  * Give the socket an address.
2272c37256eSGarrett Wollman  */
2282c37256eSGarrett Wollman static int
229b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2302c37256eSGarrett Wollman {
2312c37256eSGarrett Wollman 	int error = 0;
232f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
233c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2342c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2352c37256eSGarrett Wollman 
236c2399dd2SMichael Tuexen 	inp = sotoinpcb(so);
237c2399dd2SMichael Tuexen 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
238c2399dd2SMichael Tuexen 	INP_WLOCK(inp);
239c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
240c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
241c2399dd2SMichael Tuexen 		return (EINVAL);
242c2399dd2SMichael Tuexen 	}
243c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
244c2399dd2SMichael Tuexen 
24552710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
246f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
247f96603b5SMark Johnston 		/*
248f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
249f96603b5SMark Johnston 		 */
250f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2513f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
252c2399dd2SMichael Tuexen 		    sinp->sin_addr.s_addr != INADDR_ANY) {
253c2399dd2SMichael Tuexen 			error = EAFNOSUPPORT;
254c2399dd2SMichael Tuexen 			goto out;
255c2399dd2SMichael Tuexen 		}
256f96603b5SMark Johnston 		nam->sa_family = AF_INET;
257f96603b5SMark Johnston 	}
258c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sinp)) {
259c2399dd2SMichael Tuexen 		error = EINVAL;
260c2399dd2SMichael Tuexen 		goto out;
261c2399dd2SMichael Tuexen 	}
2622c37256eSGarrett Wollman 	/*
2632c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2642c37256eSGarrett Wollman 	 * to them.
2652c37256eSGarrett Wollman 	 */
266c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
267c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
2682c37256eSGarrett Wollman 		goto out;
269623dce13SRobert Watson 	}
270fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
27196871af0SGleb Smirnoff 	error = in_pcbbind(inp, sinp, td->td_ucred);
272fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
273623dce13SRobert Watson out:
27400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
2755d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2768501a69cSRobert Watson 	INP_WUNLOCK(inp);
277623dce13SRobert Watson 
278623dce13SRobert Watson 	return (error);
2792c37256eSGarrett Wollman }
280b287c6c7SBjoern A. Zeeb #endif /* INET */
2812c37256eSGarrett Wollman 
282fb59c426SYoshinobu Inoue #ifdef INET6
283fb59c426SYoshinobu Inoue static int
284b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
285fb59c426SYoshinobu Inoue {
286fb59c426SYoshinobu Inoue 	int error = 0;
287f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
288c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2890ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2904a91aa8fSMichael Tuexen 	u_char vflagsav;
291fb59c426SYoshinobu Inoue 
292623dce13SRobert Watson 	inp = sotoinpcb(so);
293623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2948501a69cSRobert Watson 	INP_WLOCK(inp);
29553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
296c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
297c2399dd2SMichael Tuexen 		return (EINVAL);
298c2399dd2SMichael Tuexen 	}
299c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
300c2399dd2SMichael Tuexen 
301c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
302c2399dd2SMichael Tuexen 
303c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
304c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
305c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
306c2399dd2SMichael Tuexen 		goto out;
307c2399dd2SMichael Tuexen 	}
308c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sin6)) {
309623dce13SRobert Watson 		error = EINVAL;
310623dce13SRobert Watson 		goto out;
311623dce13SRobert Watson 	}
312c2399dd2SMichael Tuexen 	/*
313c2399dd2SMichael Tuexen 	 * Must check for multicast addresses and disallow binding
314c2399dd2SMichael Tuexen 	 * to them.
315c2399dd2SMichael Tuexen 	 */
316c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
317c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
318c2399dd2SMichael Tuexen 		goto out;
319c2399dd2SMichael Tuexen 	}
320c2399dd2SMichael Tuexen 
321fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
322fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
323fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
324b287c6c7SBjoern A. Zeeb #ifdef INET
32566ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3260ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
327fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3280ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
329fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
330fb59c426SYoshinobu Inoue 
3310ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
332888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
333888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
334888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
335888973f5SMichael Tuexen 				goto out;
336888973f5SMichael Tuexen 			}
337fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
338fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
33996871af0SGleb Smirnoff 			error = in_pcbbind(inp, &sin, td->td_ucred);
340fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
341fb59c426SYoshinobu Inoue 			goto out;
342fb59c426SYoshinobu Inoue 		}
343fb59c426SYoshinobu Inoue 	}
344b287c6c7SBjoern A. Zeeb #endif
34596871af0SGleb Smirnoff 	error = in6_pcbbind(inp, sin6, td->td_ucred);
346fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
347623dce13SRobert Watson out:
3484a91aa8fSMichael Tuexen 	if (error != 0)
3494a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
35000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
3515d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3528501a69cSRobert Watson 	INP_WUNLOCK(inp);
353623dce13SRobert Watson 	return (error);
354fb59c426SYoshinobu Inoue }
355fb59c426SYoshinobu Inoue #endif /* INET6 */
356fb59c426SYoshinobu Inoue 
357b287c6c7SBjoern A. Zeeb #ifdef INET
3582c37256eSGarrett Wollman /*
3592c37256eSGarrett Wollman  * Prepare to accept connections.
3602c37256eSGarrett Wollman  */
3612c37256eSGarrett Wollman static int
362d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3632c37256eSGarrett Wollman {
3642c37256eSGarrett Wollman 	int error = 0;
365f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
366c2399dd2SMichael Tuexen 	struct tcpcb *tp;
3672c37256eSGarrett Wollman 
368623dce13SRobert Watson 	inp = sotoinpcb(so);
369623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3708501a69cSRobert Watson 	INP_WLOCK(inp);
37153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
372c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
373c2399dd2SMichael Tuexen 		return (EINVAL);
374623dce13SRobert Watson 	}
375623dce13SRobert Watson 	tp = intotcpcb(inp);
376c2399dd2SMichael Tuexen 
3770daccb9cSRobert Watson 	SOCK_LOCK(so);
3780daccb9cSRobert Watson 	error = solisten_proto_check(so);
379bd4a39ccSMark Johnston 	if (error != 0) {
380bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
381bd4a39ccSMark Johnston 		goto out;
382bd4a39ccSMark Johnston 	}
383bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
384fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
385bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
386fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
387bd4a39ccSMark Johnston 	}
3880daccb9cSRobert Watson 	if (error == 0) {
38957f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
390d374e81eSRobert Watson 		solisten_proto(so, backlog);
39109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
39237cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
39309fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
39409fe6320SNavdeep Parhar #endif
395bd4a39ccSMark Johnston 	} else {
396bd4a39ccSMark Johnston 		solisten_proto_abort(so);
3970daccb9cSRobert Watson 	}
3980daccb9cSRobert Watson 	SOCK_UNLOCK(so);
399623dce13SRobert Watson 
40068bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
401281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
40218a75309SPatrick Kelsey 
403623dce13SRobert Watson out:
40400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4055d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4068501a69cSRobert Watson 	INP_WUNLOCK(inp);
407623dce13SRobert Watson 	return (error);
4082c37256eSGarrett Wollman }
409b287c6c7SBjoern A. Zeeb #endif /* INET */
4102c37256eSGarrett Wollman 
411fb59c426SYoshinobu Inoue #ifdef INET6
412fb59c426SYoshinobu Inoue static int
413d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
414fb59c426SYoshinobu Inoue {
415fb59c426SYoshinobu Inoue 	int error = 0;
416f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
417c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4184a91aa8fSMichael Tuexen 	u_char vflagsav;
419fb59c426SYoshinobu Inoue 
420623dce13SRobert Watson 	inp = sotoinpcb(so);
421623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4228501a69cSRobert Watson 	INP_WLOCK(inp);
42353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
424c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
425c2399dd2SMichael Tuexen 		return (EINVAL);
426623dce13SRobert Watson 	}
427623dce13SRobert Watson 	tp = intotcpcb(inp);
428c2399dd2SMichael Tuexen 
429c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
430c2399dd2SMichael Tuexen 
4310daccb9cSRobert Watson 	SOCK_LOCK(so);
4320daccb9cSRobert Watson 	error = solisten_proto_check(so);
433bd4a39ccSMark Johnston 	if (error != 0) {
434bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
435bd4a39ccSMark Johnston 		goto out;
436bd4a39ccSMark Johnston 	}
437fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
438bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
439fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
44066ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
441fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
442bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
443fb59c426SYoshinobu Inoue 	}
444fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4450daccb9cSRobert Watson 	if (error == 0) {
44657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
447d374e81eSRobert Watson 		solisten_proto(so, backlog);
44809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44937cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
45009fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
45109fe6320SNavdeep Parhar #endif
452bd4a39ccSMark Johnston 	} else {
453bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4540daccb9cSRobert Watson 	}
4550daccb9cSRobert Watson 	SOCK_UNLOCK(so);
456623dce13SRobert Watson 
45768bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
458281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45918a75309SPatrick Kelsey 
4604a91aa8fSMichael Tuexen 	if (error != 0)
4614a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4624a91aa8fSMichael Tuexen 
463623dce13SRobert Watson out:
46400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4655d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4668501a69cSRobert Watson 	INP_WUNLOCK(inp);
467623dce13SRobert Watson 	return (error);
468fb59c426SYoshinobu Inoue }
469fb59c426SYoshinobu Inoue #endif /* INET6 */
470fb59c426SYoshinobu Inoue 
471b287c6c7SBjoern A. Zeeb #ifdef INET
4722c37256eSGarrett Wollman /*
4732c37256eSGarrett Wollman  * Initiate connection to peer.
4742c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4752c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4762c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4772c37256eSGarrett Wollman  * Send initial segment on connection.
4782c37256eSGarrett Wollman  */
4792c37256eSGarrett Wollman static int
480b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4812c37256eSGarrett Wollman {
482109eb549SGleb Smirnoff 	struct epoch_tracker et;
4832c37256eSGarrett Wollman 	int error = 0;
484f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
485c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4862c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4872c37256eSGarrett Wollman 
488623dce13SRobert Watson 	inp = sotoinpcb(so);
489623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4908501a69cSRobert Watson 	INP_WLOCK(inp);
491eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
492c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
493c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
494c2399dd2SMichael Tuexen 	}
495c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
496c2399dd2SMichael Tuexen 
497c2399dd2SMichael Tuexen 	sinp = (struct sockaddr_in *)nam;
498c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET) {
499c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
500623dce13SRobert Watson 		goto out;
501623dce13SRobert Watson 	}
502c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sinp)) {
503c2399dd2SMichael Tuexen 		error = EINVAL;
504c2399dd2SMichael Tuexen 		goto out;
505c2399dd2SMichael Tuexen 	}
506c2399dd2SMichael Tuexen 	/*
507c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
508c2399dd2SMichael Tuexen 	 */
509c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
510c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
511c2399dd2SMichael Tuexen 		goto out;
512c2399dd2SMichael Tuexen 	}
513c2399dd2SMichael Tuexen 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
514c2399dd2SMichael Tuexen 		error = EACCES;
515c2399dd2SMichael Tuexen 		goto out;
516c2399dd2SMichael Tuexen 	}
517c2399dd2SMichael Tuexen 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
518c2399dd2SMichael Tuexen 		goto out;
519bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
520bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
521bd4a39ccSMark Johnston 		goto out;
522bd4a39ccSMark Johnston 	}
523c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
524a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
525c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
52709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
52837cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
52909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
530c1604fe4SGleb Smirnoff 		goto out_in_epoch;
53109fe6320SNavdeep Parhar #endif
53209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
53340fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5341d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5351d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
536c1604fe4SGleb Smirnoff out_in_epoch:
537109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
538623dce13SRobert Watson out:
53900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
540e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5418501a69cSRobert Watson 	INP_WUNLOCK(inp);
542623dce13SRobert Watson 	return (error);
5432c37256eSGarrett Wollman }
544b287c6c7SBjoern A. Zeeb #endif /* INET */
5452c37256eSGarrett Wollman 
546fb59c426SYoshinobu Inoue #ifdef INET6
547fb59c426SYoshinobu Inoue static int
548b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
549fb59c426SYoshinobu Inoue {
550109eb549SGleb Smirnoff 	struct epoch_tracker et;
551fb59c426SYoshinobu Inoue 	int error = 0;
552f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
553c2399dd2SMichael Tuexen 	struct tcpcb *tp;
5540ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5554a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5564a91aa8fSMichael Tuexen 	u_char vflagsav;
557623dce13SRobert Watson 
558623dce13SRobert Watson 	inp = sotoinpcb(so);
559623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5608501a69cSRobert Watson 	INP_WLOCK(inp);
561c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
562c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
563c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
564c2399dd2SMichael Tuexen 	}
565c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
566c2399dd2SMichael Tuexen 
5674a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5684a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
569c2399dd2SMichael Tuexen 
570c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
571c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
572c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
573c2399dd2SMichael Tuexen 		goto out;
574c2399dd2SMichael Tuexen 	}
575c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sin6)) {
576c2399dd2SMichael Tuexen 		error = EINVAL;
577c2399dd2SMichael Tuexen 		goto out;
578c2399dd2SMichael Tuexen 	}
579c2399dd2SMichael Tuexen 	/*
580c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
581c2399dd2SMichael Tuexen 	 */
582c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
583c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
584623dce13SRobert Watson 		goto out;
585623dce13SRobert Watson 	}
586bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
587bd4a39ccSMark Johnston 		error = EINVAL;
588bd4a39ccSMark Johnston 		goto out;
589bd4a39ccSMark Johnston 	}
590b287c6c7SBjoern A. Zeeb #ifdef INET
591fa046d87SRobert Watson 	/*
592fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
593fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
594fa046d87SRobert Watson 	 * Is this a significant problem?
595fa046d87SRobert Watson 	 */
5960ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
597fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
598fb59c426SYoshinobu Inoue 
599d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
600d46a5312SMaxim Konovalov 			error = EINVAL;
601d46a5312SMaxim Konovalov 			goto out;
602d46a5312SMaxim Konovalov 		}
6035dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6045dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6055dba6adaSMichael Tuexen 			goto out;
6065dba6adaSMichael Tuexen 		}
60733841545SHajimu UMEMOTO 
6080ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
609888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
610888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
611888973f5SMichael Tuexen 			goto out;
612888973f5SMichael Tuexen 		}
613f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
614f903a308SMichael Tuexen 			error = EACCES;
6152cf21ae5SRandall Stewart 			goto out;
6162cf21ae5SRandall Stewart 		}
617b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
618b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
619413628a7SBjoern A. Zeeb 			goto out;
6204a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6214a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
622c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
623a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
624c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62609fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
627adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62809fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
629c1604fe4SGleb Smirnoff 			goto out_in_epoch;
63009fe6320SNavdeep Parhar #endif
63140fa3e40SGleb Smirnoff 		error = tcp_output(tp);
632c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6335dba6adaSMichael Tuexen 	} else {
6345dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6355dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6365dba6adaSMichael Tuexen 			goto out;
6375dba6adaSMichael Tuexen 		}
638fb59c426SYoshinobu Inoue 	}
639b287c6c7SBjoern A. Zeeb #endif
6404a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6414a91aa8fSMichael Tuexen 		goto out;
642fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
643fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
644dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6450773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
646a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6470773b44eSGleb Smirnoff 		goto out_in_epoch;
64809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64909fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
650adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
65109fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6520773b44eSGleb Smirnoff 		goto out_in_epoch;
65309fe6320SNavdeep Parhar #endif
65409fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
65540fa3e40SGleb Smirnoff 	error = tcp_output(tp);
656c1604fe4SGleb Smirnoff out_in_epoch:
657109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
658623dce13SRobert Watson out:
6591d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6601d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6614a91aa8fSMichael Tuexen 	/*
6624a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6634a91aa8fSMichael Tuexen 	 * the flags we modified.
6644a91aa8fSMichael Tuexen 	 */
6654a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6664a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6674a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6684a91aa8fSMichael Tuexen 	}
6694a91aa8fSMichael Tuexen 
67000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6715d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6728501a69cSRobert Watson 	INP_WUNLOCK(inp);
673623dce13SRobert Watson 	return (error);
674fb59c426SYoshinobu Inoue }
675fb59c426SYoshinobu Inoue #endif /* INET6 */
676fb59c426SYoshinobu Inoue 
6772c37256eSGarrett Wollman /*
6782c37256eSGarrett Wollman  * Initiate disconnect from peer.
6792c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6802c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6812c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6822c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6832c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6842c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6852c37256eSGarrett Wollman  *
6862c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6872c37256eSGarrett Wollman  */
6882c37256eSGarrett Wollman static int
6892c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6902c37256eSGarrett Wollman {
691f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
692623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6936573d758SMatt Macy 	struct epoch_tracker et;
694623dce13SRobert Watson 	int error = 0;
6952c37256eSGarrett Wollman 
69697a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
697623dce13SRobert Watson 	inp = sotoinpcb(so);
698623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6998501a69cSRobert Watson 	INP_WLOCK(inp);
700489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
701c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
702c2399dd2SMichael Tuexen 		NET_EPOCH_EXIT(et);
703c2399dd2SMichael Tuexen 		return (ECONNRESET);
704623dce13SRobert Watson 	}
705623dce13SRobert Watson 	tp = intotcpcb(inp);
706c2399dd2SMichael Tuexen 
707cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
708cda6bdbaSJohn Baldwin 		goto out;
709623dce13SRobert Watson 	tcp_disconnect(tp);
710623dce13SRobert Watson out:
71100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_DISCONNECT, error);
7125d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7138501a69cSRobert Watson 	INP_WUNLOCK(inp);
71497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
715623dce13SRobert Watson 	return (error);
7162c37256eSGarrett Wollman }
7172c37256eSGarrett Wollman 
718b287c6c7SBjoern A. Zeeb #ifdef INET
7192c37256eSGarrett Wollman /*
7208296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7218296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7222c37256eSGarrett Wollman  */
7232c37256eSGarrett Wollman static int
72457bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7252c37256eSGarrett Wollman {
7262c37256eSGarrett Wollman 	int error = 0;
727c2399dd2SMichael Tuexen 	struct inpcb *inp;
728c2399dd2SMichael Tuexen 	struct tcpcb *tp;
72926ef6ac4SDon Lewis 	struct in_addr addr;
73026ef6ac4SDon Lewis 	in_port_t port = 0;
7312c37256eSGarrett Wollman 
732f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
733623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7348501a69cSRobert Watson 	INP_WLOCK(inp);
73553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
736c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
737c2399dd2SMichael Tuexen 		return (ECONNABORTED);
738623dce13SRobert Watson 	}
7391db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
740f76fcf6dSJeffrey Hsu 
741c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
742c2399dd2SMichael Tuexen 		error = ECONNABORTED;
743c2399dd2SMichael Tuexen 		goto out;
744c2399dd2SMichael Tuexen 	}
745f76fcf6dSJeffrey Hsu 	/*
74654d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
74726ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
74826ef6ac4SDon Lewis 	 * release the lock.
749f76fcf6dSJeffrey Hsu 	 */
75026ef6ac4SDon Lewis 	port = inp->inp_fport;
75126ef6ac4SDon Lewis 	addr = inp->inp_faddr;
752f76fcf6dSJeffrey Hsu 
753623dce13SRobert Watson out:
75400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7555d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7568501a69cSRobert Watson 	INP_WUNLOCK(inp);
75726ef6ac4SDon Lewis 	if (error == 0)
75826ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
75926ef6ac4SDon Lewis 	return error;
7602c37256eSGarrett Wollman }
761b287c6c7SBjoern A. Zeeb #endif /* INET */
7622c37256eSGarrett Wollman 
763fb59c426SYoshinobu Inoue #ifdef INET6
764fb59c426SYoshinobu Inoue static int
765fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
766fb59c426SYoshinobu Inoue {
767c2399dd2SMichael Tuexen 	struct inpcb *inp;
768fb59c426SYoshinobu Inoue 	int error = 0;
769c2399dd2SMichael Tuexen 	struct tcpcb *tp;
77026ef6ac4SDon Lewis 	struct in_addr addr;
77126ef6ac4SDon Lewis 	struct in6_addr addr6;
7726573d758SMatt Macy 	struct epoch_tracker et;
77326ef6ac4SDon Lewis 	in_port_t port = 0;
77426ef6ac4SDon Lewis 	int v4 = 0;
775fb59c426SYoshinobu Inoue 
776f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
777623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
778c2399dd2SMichael Tuexen 	NET_EPOCH_ENTER(et); /* XXXMT Why is this needed? */
7798501a69cSRobert Watson 	INP_WLOCK(inp);
78053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
781c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
782c2399dd2SMichael Tuexen 		NET_EPOCH_EXIT(et);
783c2399dd2SMichael Tuexen 		return (ECONNABORTED);
784623dce13SRobert Watson 	}
7851db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
786623dce13SRobert Watson 
787c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
788c2399dd2SMichael Tuexen 		error = ECONNABORTED;
789c2399dd2SMichael Tuexen 		goto out;
790c2399dd2SMichael Tuexen 	}
79126ef6ac4SDon Lewis 	/*
79226ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
79326ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
79426ef6ac4SDon Lewis 	 * release the lock.
79526ef6ac4SDon Lewis 	 */
79626ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
79726ef6ac4SDon Lewis 		v4 = 1;
79826ef6ac4SDon Lewis 		port = inp->inp_fport;
79926ef6ac4SDon Lewis 		addr = inp->inp_faddr;
80026ef6ac4SDon Lewis 	} else {
80126ef6ac4SDon Lewis 		port = inp->inp_fport;
80226ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
80326ef6ac4SDon Lewis 	}
80426ef6ac4SDon Lewis 
805623dce13SRobert Watson out:
80600812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
8075d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8088501a69cSRobert Watson 	INP_WUNLOCK(inp);
80997a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
81026ef6ac4SDon Lewis 	if (error == 0) {
81126ef6ac4SDon Lewis 		if (v4)
81226ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
81326ef6ac4SDon Lewis 		else
81426ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
81526ef6ac4SDon Lewis 	}
81626ef6ac4SDon Lewis 	return error;
817fb59c426SYoshinobu Inoue }
818fb59c426SYoshinobu Inoue #endif /* INET6 */
819f76fcf6dSJeffrey Hsu 
820f76fcf6dSJeffrey Hsu /*
8212c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8222c37256eSGarrett Wollman  */
8232c37256eSGarrett Wollman static int
8242c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8252c37256eSGarrett Wollman {
8262c37256eSGarrett Wollman 	int error = 0;
827f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
828c2399dd2SMichael Tuexen 	struct tcpcb *tp;
8296573d758SMatt Macy 	struct epoch_tracker et;
8302c37256eSGarrett Wollman 
831623dce13SRobert Watson 	inp = sotoinpcb(so);
832623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8338501a69cSRobert Watson 	INP_WLOCK(inp);
83453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
8350af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
8360af4ce45SGleb Smirnoff 		return (ECONNRESET);
837623dce13SRobert Watson 	}
8380af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
839c2399dd2SMichael Tuexen 
8400af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8412c37256eSGarrett Wollman 	socantsendmore(so);
842623dce13SRobert Watson 	tcp_usrclosed(tp);
843ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
844f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
84500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8465d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
847f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
84897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
849623dce13SRobert Watson 
850623dce13SRobert Watson 	return (error);
8512c37256eSGarrett Wollman }
8522c37256eSGarrett Wollman 
8532c37256eSGarrett Wollman /*
8542c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8552c37256eSGarrett Wollman  */
8562c37256eSGarrett Wollman static int
8572c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8582c37256eSGarrett Wollman {
859109eb549SGleb Smirnoff 	struct epoch_tracker et;
860f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
861c2399dd2SMichael Tuexen 	struct tcpcb *tp;
862f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8632c37256eSGarrett Wollman 
864623dce13SRobert Watson 	inp = sotoinpcb(so);
865623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8668501a69cSRobert Watson 	INP_WLOCK(inp);
86753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
86837a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
86937a7f557SGleb Smirnoff 		return (ECONNRESET);
870623dce13SRobert Watson 	}
87137a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
872c2399dd2SMichael Tuexen 
87337a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
874281a0fd4SPatrick Kelsey 	/*
875281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
876281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
877281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
878281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
879281a0fd4SPatrick Kelsey 	 * expires.
880281a0fd4SPatrick Kelsey 	 */
88168bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
882281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
883281a0fd4SPatrick Kelsey 		goto out;
88409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
88509fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
88609fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
887460cf046SNavdeep Parhar 	else
88809fe6320SNavdeep Parhar #endif
889f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
890623dce13SRobert Watson out:
89100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
8925d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
893f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
894f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
895623dce13SRobert Watson 	return (error);
8962c37256eSGarrett Wollman }
8972c37256eSGarrett Wollman 
8982c37256eSGarrett Wollman /*
8992c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
9009c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
9019c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9029c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9039c9906e9SPeter Wemm  * generally are caller-frees.
9042c37256eSGarrett Wollman  */
9052c37256eSGarrett Wollman static int
90657bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
907b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9082c37256eSGarrett Wollman {
90997a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9102c37256eSGarrett Wollman 	int error = 0;
911f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
912c2399dd2SMichael Tuexen 	struct tcpcb *tp;
913888973f5SMichael Tuexen #ifdef INET
91451e08d53SMichael Tuexen #ifdef INET6
91551e08d53SMichael Tuexen 	struct sockaddr_in sin;
91651e08d53SMichael Tuexen #endif
91751e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
918888973f5SMichael Tuexen #endif
919fb59c426SYoshinobu Inoue #ifdef INET6
920a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
921fb59c426SYoshinobu Inoue 	int isipv6;
922fb59c426SYoshinobu Inoue #endif
9234a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9244a91aa8fSMichael Tuexen 	u_char vflagsav;
9254a91aa8fSMichael Tuexen 	bool restoreflags;
9262c37256eSGarrett Wollman 
9274287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9284287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9294287aa56SGleb Smirnoff 	INP_WLOCK(inp);
93053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9314287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9324287aa56SGleb Smirnoff 			m_freem(m);
9334287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9344287aa56SGleb Smirnoff 		return (ECONNRESET);
9354287aa56SGleb Smirnoff 	}
936c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
9374287aa56SGleb Smirnoff 
9384287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9394287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9404287aa56SGleb Smirnoff 	restoreflags = false;
9414287aa56SGleb Smirnoff 
9424287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
943c2399dd2SMichael Tuexen 	if (control != NULL) {
944c2399dd2SMichael Tuexen 		/* TCP doesn't do control messages (rights, creds, etc) */
945c2399dd2SMichael Tuexen 		if (control->m_len > 0) {
946c2399dd2SMichael Tuexen 			m_freem(control);
947c2399dd2SMichael Tuexen 			error = EINVAL;
948c2399dd2SMichael Tuexen 			goto out;
949c2399dd2SMichael Tuexen 		}
950c2399dd2SMichael Tuexen 		m_freem(control);	/* empty control, just free it */
951c2399dd2SMichael Tuexen 	}
952c2399dd2SMichael Tuexen 
9537d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9547d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
955d3b6c96bSRandall Stewart 		goto out;
9567d2608a5SMark Johnston 
957888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
958bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
959bd4a39ccSMark Johnston 			error = EINVAL;
960bd4a39ccSMark Johnston 			goto out;
961bd4a39ccSMark Johnston 		}
962888973f5SMichael Tuexen 		switch (nam->sa_family) {
963888973f5SMichael Tuexen #ifdef INET
964888973f5SMichael Tuexen 		case AF_INET:
965888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
966888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
967888973f5SMichael Tuexen 				error = EINVAL;
968888973f5SMichael Tuexen 				goto out;
969888973f5SMichael Tuexen 			}
970888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
971888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
972888973f5SMichael Tuexen 				goto out;
973888973f5SMichael Tuexen 			}
974888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
975888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
976888973f5SMichael Tuexen 				goto out;
977888973f5SMichael Tuexen 			}
978f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
979f903a308SMichael Tuexen 				error = EACCES;
9802cf21ae5SRandall Stewart 				goto out;
9812cf21ae5SRandall Stewart 			}
982888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9837d2608a5SMark Johnston 			    &sinp->sin_addr)))
984888973f5SMichael Tuexen 				goto out;
985888973f5SMichael Tuexen #ifdef INET6
986888973f5SMichael Tuexen 			isipv6 = 0;
987888973f5SMichael Tuexen #endif
988888973f5SMichael Tuexen 			break;
989888973f5SMichael Tuexen #endif /* INET */
990888973f5SMichael Tuexen #ifdef INET6
991888973f5SMichael Tuexen 		case AF_INET6:
9920ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
9930ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
994888973f5SMichael Tuexen 				error = EINVAL;
995888973f5SMichael Tuexen 				goto out;
996888973f5SMichael Tuexen 			}
997e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
998e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
999e240ce42SMichael Tuexen 				goto out;
1000e240ce42SMichael Tuexen 			}
10010ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1002888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1003888973f5SMichael Tuexen 				goto out;
1004888973f5SMichael Tuexen 			}
10050ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1006888973f5SMichael Tuexen #ifdef INET
1007888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1008888973f5SMichael Tuexen 					error = EINVAL;
1009888973f5SMichael Tuexen 					goto out;
1010888973f5SMichael Tuexen 				}
1011888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1012888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1013888973f5SMichael Tuexen 					goto out;
1014888973f5SMichael Tuexen 				}
10154a91aa8fSMichael Tuexen 				restoreflags = true;
1016888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1017888973f5SMichael Tuexen 				sinp = &sin;
10180ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1019888973f5SMichael Tuexen 				if (IN_MULTICAST(
1020888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1021888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1022888973f5SMichael Tuexen 					goto out;
1023888973f5SMichael Tuexen 				}
1024888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10257d2608a5SMark Johnston 				    &sinp->sin_addr)))
1026888973f5SMichael Tuexen 					goto out;
1027888973f5SMichael Tuexen 				isipv6 = 0;
1028888973f5SMichael Tuexen #else /* !INET */
1029888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1030888973f5SMichael Tuexen 				goto out;
1031888973f5SMichael Tuexen #endif /* INET */
1032888973f5SMichael Tuexen 			} else {
1033888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1034888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1035888973f5SMichael Tuexen 					goto out;
1036888973f5SMichael Tuexen 				}
10374a91aa8fSMichael Tuexen 				restoreflags = true;
1038888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1039888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1040888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10417d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1042888973f5SMichael Tuexen 					goto out;
1043888973f5SMichael Tuexen 				isipv6 = 1;
1044888973f5SMichael Tuexen 			}
1045888973f5SMichael Tuexen 			break;
1046888973f5SMichael Tuexen #endif /* INET6 */
1047888973f5SMichael Tuexen 		default:
1048888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1049888973f5SMichael Tuexen 			goto out;
1050888973f5SMichael Tuexen 		}
1051888973f5SMichael Tuexen 	}
10522c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
105308af8aacSRandall Stewart 		if (tp->t_acktime == 0)
105408af8aacSRandall Stewart 			tp->t_acktime = ticks;
1055651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10567d2608a5SMark Johnston 		m = NULL;
10572c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1058bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1059bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1060bd4a39ccSMark Johnston 
10612c37256eSGarrett Wollman 			/*
10622c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10632c37256eSGarrett Wollman 			 * initialize window to default value, and
10640c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10652c37256eSGarrett Wollman 			 */
1066fb59c426SYoshinobu Inoue #ifdef INET6
1067fb59c426SYoshinobu Inoue 			if (isipv6)
1068a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1069fb59c426SYoshinobu Inoue #endif /* INET6 */
1070b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1071b287c6c7SBjoern A. Zeeb 			else
1072b287c6c7SBjoern A. Zeeb #endif
1073b287c6c7SBjoern A. Zeeb #ifdef INET
1074a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1075b287c6c7SBjoern A. Zeeb #endif
10764a91aa8fSMichael Tuexen 			/*
10774a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10784a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10794a91aa8fSMichael Tuexen 			 * operations fail.
10804a91aa8fSMichael Tuexen 			 */
10814a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10824a91aa8fSMichael Tuexen 				restoreflags = false;
10834a91aa8fSMichael Tuexen 
10847d2608a5SMark Johnston 			if (error) {
10857d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10867d2608a5SMark Johnston 				sbflush(&so->so_snd);
10872c37256eSGarrett Wollman 				goto out;
10887d2608a5SMark Johnston 			}
1089c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1090c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
109118a75309SPatrick Kelsey 			else {
10922c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10932c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10942c37256eSGarrett Wollman 			}
1095c560df6fSPatrick Kelsey 		}
10962c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
10972c37256eSGarrett Wollman 			/*
10982c37256eSGarrett Wollman 			 * Close the send side of the connection after
10992c37256eSGarrett Wollman 			 * the data is sent.
11002c37256eSGarrett Wollman 			 */
11012c37256eSGarrett Wollman 			socantsendmore(so);
1102623dce13SRobert Watson 			tcp_usrclosed(tp);
11032c37256eSGarrett Wollman 		}
1104e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1105e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1106e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1107e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1108e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1109e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1110e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1111e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1112e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1113e854dd38SRandall Stewart 		}
11142cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11152cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1116b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1117b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1118f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1119b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1120b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1121b0acefa8SBill Fenner 		}
11222c37256eSGarrett Wollman 	} else {
1123623dce13SRobert Watson 		/*
1124623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1125623dce13SRobert Watson 		 */
1126d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11272c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1128d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11292c37256eSGarrett Wollman 			error = ENOBUFS;
11302c37256eSGarrett Wollman 			goto out;
11312c37256eSGarrett Wollman 		}
11322c37256eSGarrett Wollman 		/*
11332c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11342c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11352c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11362c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11372c37256eSGarrett Wollman 		 * of data past the urgent section.
11382c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11392c37256eSGarrett Wollman 		 */
114008af8aacSRandall Stewart 		if (tp->t_acktime == 0)
114108af8aacSRandall Stewart 			tp->t_acktime = ticks;
1142651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1143d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
11447d2608a5SMark Johnston 		m = NULL;
1145ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1146ef53690bSGarrett Wollman 			/*
1147ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1148ef53690bSGarrett Wollman 			 * initialize window to default value, and
11490c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1150ef53690bSGarrett Wollman 			 */
115118a75309SPatrick Kelsey 
1152c560df6fSPatrick Kelsey 			/*
1153c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1154c560df6fSPatrick Kelsey 			 */
1155c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1156c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1157fb59c426SYoshinobu Inoue #ifdef INET6
1158fb59c426SYoshinobu Inoue 			if (isipv6)
1159a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1160fb59c426SYoshinobu Inoue #endif /* INET6 */
1161b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1162b287c6c7SBjoern A. Zeeb 			else
1163b287c6c7SBjoern A. Zeeb #endif
1164b287c6c7SBjoern A. Zeeb #ifdef INET
1165a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1166b287c6c7SBjoern A. Zeeb #endif
11674a91aa8fSMichael Tuexen 			/*
11684a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11694a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11704a91aa8fSMichael Tuexen 			 * operations fail.
11714a91aa8fSMichael Tuexen 			 */
11724a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11734a91aa8fSMichael Tuexen 				restoreflags = false;
11744a91aa8fSMichael Tuexen 
11757d2608a5SMark Johnston 			if (error != 0) {
11767d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11777d2608a5SMark Johnston 				sbflush(&so->so_snd);
1178ef53690bSGarrett Wollman 				goto out;
11797d2608a5SMark Johnston 			}
1180ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1181ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1182623dce13SRobert Watson 		}
1183300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11847d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11852cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1186f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11872cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11882c37256eSGarrett Wollman 		}
11892cbcd3c1SGleb Smirnoff 	}
11902529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11912529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11922529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11932529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11942529f56eSJonathan T. Looney 	    0, NULL, false);
11957d2608a5SMark Johnston 
1196d1401c90SRobert Watson out:
11974a91aa8fSMichael Tuexen 	/*
11987d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
11997d2608a5SMark Johnston 	 * responsible for freeing memory.
12007d2608a5SMark Johnston 	 */
12017d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12027d2608a5SMark Johnston 		m_freem(m);
12037d2608a5SMark Johnston 
12047d2608a5SMark Johnston 	/*
12054a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12064a91aa8fSMichael Tuexen 	 * restore the original flags.
12074a91aa8fSMichael Tuexen 	 */
12084a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12094a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12104a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12114a91aa8fSMichael Tuexen 	}
121200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
121300812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
12145d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12155d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1216f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
121797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
121873fddedaSPeter Grehan 	return (error);
12192c37256eSGarrett Wollman }
12202c37256eSGarrett Wollman 
12212cbcd3c1SGleb Smirnoff static int
12222cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12232cbcd3c1SGleb Smirnoff {
1224109eb549SGleb Smirnoff 	struct epoch_tracker et;
12252cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12262cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12272cbcd3c1SGleb Smirnoff 	int error;
12282cbcd3c1SGleb Smirnoff 
12292cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12302cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
123153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12322cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
123382334850SJohn Baldwin 		mb_free_notready(m, count);
12342cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12352cbcd3c1SGleb Smirnoff 	}
12362cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12372cbcd3c1SGleb Smirnoff 
12382cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12392cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12402cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1241f64dc2abSGleb Smirnoff 	if (error) {
12422cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1243f64dc2abSGleb Smirnoff 		return (error);
1244f64dc2abSGleb Smirnoff 	}
1245f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1246f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1247f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12482cbcd3c1SGleb Smirnoff 
12492cbcd3c1SGleb Smirnoff 	return (error);
12502cbcd3c1SGleb Smirnoff }
12512cbcd3c1SGleb Smirnoff 
12522c37256eSGarrett Wollman /*
1253a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12542c37256eSGarrett Wollman  */
1255ac45e92fSRobert Watson static void
12562c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12572c37256eSGarrett Wollman {
1258f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1259c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12606573d758SMatt Macy 	struct epoch_tracker et;
1261c78cbc7bSRobert Watson 
1262ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1263c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1264c78cbc7bSRobert Watson 
126597a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12668501a69cSRobert Watson 	INP_WLOCK(inp);
1267c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1268c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1269c78cbc7bSRobert Watson 
1270c78cbc7bSRobert Watson 	/*
1271a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1272c78cbc7bSRobert Watson 	 */
127353af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1274c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12758fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12768fa799bdSJonathan T. Looney 		if (tp == NULL)
12778fa799bdSJonathan T. Looney 			goto dropped;
127800812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12795d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1280c78cbc7bSRobert Watson 	}
1281ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1282d8596171SGleb Smirnoff 		soref(so);
1283ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1284a152f8a3SRobert Watson 	}
12858501a69cSRobert Watson 	INP_WUNLOCK(inp);
12868fa799bdSJonathan T. Looney dropped:
128797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1288a152f8a3SRobert Watson }
1289a152f8a3SRobert Watson 
1290a152f8a3SRobert Watson /*
1291a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1292a152f8a3SRobert Watson  */
1293a152f8a3SRobert Watson static void
1294a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1295a152f8a3SRobert Watson {
1296a152f8a3SRobert Watson 	struct inpcb *inp;
1297c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12986573d758SMatt Macy 	struct epoch_tracker et;
1299a152f8a3SRobert Watson 
1300a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1301a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1302a152f8a3SRobert Watson 
130397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13048501a69cSRobert Watson 	INP_WLOCK(inp);
1305a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1306a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1307a152f8a3SRobert Watson 
1308a152f8a3SRobert Watson 	/*
1309cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1310a152f8a3SRobert Watson 	 * a disconnect.
1311a152f8a3SRobert Watson 	 */
131253af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1313a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1314cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
131574703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1316a152f8a3SRobert Watson 			tcp_disconnect(tp);
131700812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
13185d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1319a152f8a3SRobert Watson 		}
1320cda6bdbaSJohn Baldwin 	}
1321ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1322d8596171SGleb Smirnoff 		soref(so);
1323ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1324a152f8a3SRobert Watson 	}
13258501a69cSRobert Watson 	INP_WUNLOCK(inp);
132697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13272c37256eSGarrett Wollman }
13282c37256eSGarrett Wollman 
1329d3b6c96bSRandall Stewart static int
1330d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1331d3b6c96bSRandall Stewart {
1332d3b6c96bSRandall Stewart 	/*
1333d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1334d3b6c96bSRandall Stewart 	 * specified then it does not always support
1335d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1336d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1337d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1338d3b6c96bSRandall Stewart 	 */
1339d3b6c96bSRandall Stewart 	int ret = 0;
1340d3b6c96bSRandall Stewart 
1341d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1342d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1343d3b6c96bSRandall Stewart 	}
1344d3b6c96bSRandall Stewart 	return (ret);
1345d3b6c96bSRandall Stewart }
1346d3b6c96bSRandall Stewart 
13472c37256eSGarrett Wollman /*
13482c37256eSGarrett Wollman  * Receive out-of-band data.
13492c37256eSGarrett Wollman  */
13502c37256eSGarrett Wollman static int
13512c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13522c37256eSGarrett Wollman {
13532c37256eSGarrett Wollman 	int error = 0;
1354f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1355c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13562c37256eSGarrett Wollman 
1357623dce13SRobert Watson 	inp = sotoinpcb(so);
1358623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13598501a69cSRobert Watson 	INP_WLOCK(inp);
136053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1361c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
1362c2399dd2SMichael Tuexen 		return (ECONNRESET);
1363623dce13SRobert Watson 	}
1364623dce13SRobert Watson 	tp = intotcpcb(inp);
1365c2399dd2SMichael Tuexen 
1366d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1367d3b6c96bSRandall Stewart 	if (error) {
1368d3b6c96bSRandall Stewart 		goto out;
1369d3b6c96bSRandall Stewart 	}
13702c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1371c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13724cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13734cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13742c37256eSGarrett Wollman 		error = EINVAL;
13752c37256eSGarrett Wollman 		goto out;
13762c37256eSGarrett Wollman 	}
13772c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13782c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13792c37256eSGarrett Wollman 		goto out;
13802c37256eSGarrett Wollman 	}
13812c37256eSGarrett Wollman 	m->m_len = 1;
13822c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13832c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13842c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1385623dce13SRobert Watson 
1386623dce13SRobert Watson out:
138700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
13885d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13898501a69cSRobert Watson 	INP_WUNLOCK(inp);
1390623dce13SRobert Watson 	return (error);
13912c37256eSGarrett Wollman }
13922c37256eSGarrett Wollman 
1393b287c6c7SBjoern A. Zeeb #ifdef INET
1394e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1395e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1396e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1397e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1398e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1399e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1400e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1401e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1402e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1403e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1404e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1405e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1406e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1407e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1408e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1409e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1410e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1411e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1412e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1413e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1414e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1415e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1416e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1417e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14182c37256eSGarrett Wollman };
1419b287c6c7SBjoern A. Zeeb #endif /* INET */
1420df8bae1dSRodney W. Grimes 
1421fb59c426SYoshinobu Inoue #ifdef INET6
1422e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1423e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1424e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1425e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1426e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1427e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1428e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1429e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1430e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1431e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1432e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1433e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1434e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1435e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1436e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1437e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1438e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1439e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1440e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1441e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1442e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1443e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1444e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1445e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1446fb59c426SYoshinobu Inoue };
1447fb59c426SYoshinobu Inoue #endif /* INET6 */
1448fb59c426SYoshinobu Inoue 
1449b287c6c7SBjoern A. Zeeb #ifdef INET
1450a0292f23SGarrett Wollman /*
1451a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1452dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1453dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1454dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1455a0292f23SGarrett Wollman  */
14560312fbe9SPoul-Henning Kamp static int
1457a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1458a0292f23SGarrett Wollman {
14599e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14609eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1461c3229e05SDavid Greenman 	int error;
1462a0292f23SGarrett Wollman 
1463c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14648501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
146576f1499fSGleb Smirnoff 
1466636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1467*de0a2eb2SMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1468*de0a2eb2SMark Johnston 	    SS_ISDISCONNECTED)) != 0))
1469636b19eaSMark Johnston 		return (EISCONN);
1470636b19eaSMark Johnston 
1471fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1472dfc4d218SGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred, true);
14739e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1474dfc4d218SGleb Smirnoff 	if (error != 0)
14759e46ff4dSGleb Smirnoff 		return (error);
1476a0292f23SGarrett Wollman 
1477087b55eaSAndre Oppermann 	/*
1478087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1479087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1480087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1481087b55eaSAndre Oppermann 	 */
1482a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14839b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1484a0292f23SGarrett Wollman 		tp->request_r_scale++;
1485a0292f23SGarrett Wollman 
1486a0292f23SGarrett Wollman 	soisconnecting(so);
148778b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
148857f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14898e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14908e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14918e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1492a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1493a45d2726SAndras Olah 
14949e46ff4dSGleb Smirnoff 	return (0);
1495a0292f23SGarrett Wollman }
1496b287c6c7SBjoern A. Zeeb #endif /* INET */
1497a0292f23SGarrett Wollman 
1498fb59c426SYoshinobu Inoue #ifdef INET6
1499fb59c426SYoshinobu Inoue static int
1500a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1501fb59c426SYoshinobu Inoue {
15029eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1503636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1504fb59c426SYoshinobu Inoue 	int error;
1505fb59c426SYoshinobu Inoue 
1506775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
15078501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1508623dce13SRobert Watson 
1509636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1510636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1511636b19eaSMark Johnston 		return (EISCONN);
1512636b19eaSMark Johnston 
151376f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1514a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1515fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
151676f1499fSGleb Smirnoff 	if (error != 0)
151776f1499fSGleb Smirnoff 		return (error);
1518fb59c426SYoshinobu Inoue 
1519fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1520fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1521970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1522fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1523fb59c426SYoshinobu Inoue 
1524636b19eaSMark Johnston 	soisconnecting(so);
152578b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
152657f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15278e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15288e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15298e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1530fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1531fb59c426SYoshinobu Inoue 
153276f1499fSGleb Smirnoff 	return (0);
1533fb59c426SYoshinobu Inoue }
1534fb59c426SYoshinobu Inoue #endif /* INET6 */
1535fb59c426SYoshinobu Inoue 
1536cfe8b629SGarrett Wollman /*
1537b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1538b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1539b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1540b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1541b8af5dfaSRobert Watson  * from Linux.
1542b8af5dfaSRobert Watson  */
1543b8af5dfaSRobert Watson static void
1544ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1545b8af5dfaSRobert Watson {
1546b8af5dfaSRobert Watson 
15479eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1548b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1549b8af5dfaSRobert Watson 
1550b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1551b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1552b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15533529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1554b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1555b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1556b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1557b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1558b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1559b8af5dfaSRobert Watson 	}
156004682968SRichard Scheffenegger 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
156104682968SRichard Scheffenegger 		case TF2_ECN_PERMIT:
15625a17b6adSMichael Tuexen 			ti->tcpi_options |= TCPI_OPT_ECN;
156304682968SRichard Scheffenegger 			break;
156404682968SRichard Scheffenegger 		case TF2_ACE_PERMIT:
156504682968SRichard Scheffenegger 			/* FALLTHROUGH */
156604682968SRichard Scheffenegger 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
156704682968SRichard Scheffenegger 			ti->tcpi_options |= TCPI_OPT_ACE;
156804682968SRichard Scheffenegger 			break;
156904682968SRichard Scheffenegger 		default:
157004682968SRichard Scheffenegger 			break;
157104682968SRichard Scheffenegger 	}
157204682968SRichard Scheffenegger 	if (IS_FASTOPEN(tp->t_flags))
157304682968SRichard Scheffenegger 		ti->tcpi_options |= TCPI_OPT_TFO;
15741baaf834SBruce M Simpson 
157543d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15763ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15771baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15781baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15791baaf834SBruce M Simpson 
1580b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1581b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1582b8af5dfaSRobert Watson 
1583b8af5dfaSRobert Watson 	/*
1584b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1585b8af5dfaSRobert Watson 	 */
1586c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1587535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1588b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15891c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1590535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
159143d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
159243d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1593f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1594f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1595f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1596a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1597a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1598a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1599a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1600a6456410SNavdeep Parhar 	}
1601a6456410SNavdeep Parhar #endif
160222c81cc5SRichard Scheffenegger 	/*
160322c81cc5SRichard Scheffenegger 	 * AccECN related counters.
160422c81cc5SRichard Scheffenegger 	 */
160522c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
160622c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
160722c81cc5SRichard Scheffenegger 		/*
160822c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
160922c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
161022c81cc5SRichard Scheffenegger 		 */
161122c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
161222c81cc5SRichard Scheffenegger 	else
161322c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
161422c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1615b8af5dfaSRobert Watson }
1616b8af5dfaSRobert Watson 
1617b8af5dfaSRobert Watson /*
16181e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16191e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16201e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16211e8f5ffaSRobert Watson  * option.
1622cfe8b629SGarrett Wollman  */
1623bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16248501a69cSRobert Watson 	INP_WLOCK(inp);							\
162553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16268501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1627bac5bedfSConrad Meyer 		cleanup;						\
16281e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16291e8f5ffaSRobert Watson 	}								\
16301e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16311e8f5ffaSRobert Watson } while(0)
1632bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16331e8f5ffaSRobert Watson 
1634fd7daa72SMichael Tuexen int
1635fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1636df8bae1dSRodney W. Grimes {
1637fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1638fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1639fc4d53ccSGleb Smirnoff 	int error = 0;
1640df8bae1dSRodney W. Grimes 
1641fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16423b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
164353af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1644fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1645fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1646fc4d53ccSGleb Smirnoff 
1647cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16483b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1649fb59c426SYoshinobu Inoue #ifdef INET6
1650de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1651fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1652de156263SGleb Smirnoff #endif
1653de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1654de156263SGleb Smirnoff 		else
1655de156263SGleb Smirnoff #endif
1656de156263SGleb Smirnoff #ifdef INET
1657fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1658de156263SGleb Smirnoff #endif
16595dff1c38SMichael Tuexen 		/*
1660de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1661de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1662de156263SGleb Smirnoff 		 * IP level returned.
16635dff1c38SMichael Tuexen 		 */
1664de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1665de156263SGleb Smirnoff #ifdef INET6
1666de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1667de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1668de156263SGleb Smirnoff 				return (error);
1669de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1670de156263SGleb Smirnoff 			case IPV6_TCLASS:
1671de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1672de156263SGleb Smirnoff 				break;
1673de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1674f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1675f581a26eSGleb Smirnoff 				break;
1676de156263SGleb Smirnoff 			default:
1677de156263SGleb Smirnoff 				return (error);
16785dff1c38SMichael Tuexen 			}
1679de156263SGleb Smirnoff 			break;
1680b287c6c7SBjoern A. Zeeb #endif
1681b287c6c7SBjoern A. Zeeb #ifdef INET
1682de156263SGleb Smirnoff 		case IPPROTO_IP:
1683de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1684de156263SGleb Smirnoff 			case IP_TOS:
16853b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
16863b0ee680SRichard Scheffenegger 				break;
1687de156263SGleb Smirnoff 			case IP_TTL:
1688de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1689de156263SGleb Smirnoff 				break;
1690de156263SGleb Smirnoff 			default:
1691df8bae1dSRodney W. Grimes 				return (error);
1692de156263SGleb Smirnoff 			}
1693de156263SGleb Smirnoff 			break;
1694de156263SGleb Smirnoff #endif
1695de156263SGleb Smirnoff 		default:
1696de156263SGleb Smirnoff 			return (error);
1697de156263SGleb Smirnoff 		}
16983b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
169953af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
17003b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
17013b3c08c1SMichael Tuexen 			return (ECONNRESET);
17023b3c08c1SMichael Tuexen 		}
1703fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1704fc4d53ccSGleb Smirnoff 		/*
1705fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1706fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1707fc4d53ccSGleb Smirnoff 		 */
1708fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1709fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
171073ee5756SRandall Stewart 		void *ptr = NULL;
1711fc4d53ccSGleb Smirnoff 
17123b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1713fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1714fc4d53ccSGleb Smirnoff 		if (error)
1715fc4d53ccSGleb Smirnoff 			return (error);
1716fc4d53ccSGleb Smirnoff 
171768cea2b1SJohn Baldwin 		INP_WLOCK(inp);
171855bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1719fc4d53ccSGleb Smirnoff 
172055bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
172155bceb1eSRandall Stewart 		if (blk == NULL) {
172255bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
172355bceb1eSRandall Stewart 			return (ENOENT);
172455bceb1eSRandall Stewart 		}
1725587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1726587d67c0SRandall Stewart 			/* You already have this */
1727587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1728587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1729587d67c0SRandall Stewart 			return (0);
1730587d67c0SRandall Stewart 		}
1731587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1732587d67c0SRandall Stewart 			/*
1733587d67c0SRandall Stewart 			 * The user has advanced the state
1734587d67c0SRandall Stewart 			 * past the initial point, we may not
1735587d67c0SRandall Stewart 			 * be able to switch.
1736587d67c0SRandall Stewart 			 */
1737587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1738587d67c0SRandall Stewart 				/*
1739587d67c0SRandall Stewart 				 * Does the stack provide a
1740587d67c0SRandall Stewart 				 * query mechanism, if so it may
1741587d67c0SRandall Stewart 				 * still be possible?
1742587d67c0SRandall Stewart 				 */
1743587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1744c6c0be27SMichael Tuexen 			} else
1745c6c0be27SMichael Tuexen 				error = EINVAL;
1746587d67c0SRandall Stewart 			if (error) {
1747587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1748587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1749587d67c0SRandall Stewart 				return(error);
1750587d67c0SRandall Stewart 			}
1751587d67c0SRandall Stewart 		}
175255bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
175355bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
175455bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
175555bceb1eSRandall Stewart 			return (ENOENT);
175655bceb1eSRandall Stewart 		}
175755bceb1eSRandall Stewart 		/*
175873ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
175973ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
176055bceb1eSRandall Stewart 		 */
176173ee5756SRandall Stewart #ifdef TCPHPTS
176273ee5756SRandall Stewart 		/* Assure that we are not on any hpts */
1763c2a69e84SGleb Smirnoff 		tcp_hpts_remove(tp);
176473ee5756SRandall Stewart #endif
176573ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
176673ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
176773ee5756SRandall Stewart 			if (error) {
176873ee5756SRandall Stewart 				/*
176973ee5756SRandall Stewart 				 * Release the ref count the lookup
177073ee5756SRandall Stewart 				 * acquired.
177173ee5756SRandall Stewart 				 */
177273ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
177373ee5756SRandall Stewart 				/*
177473ee5756SRandall Stewart 				 * Now there is a chance that the
177573ee5756SRandall Stewart 				 * init() function mucked with some
177673ee5756SRandall Stewart 				 * things before it failed, such as
177773ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
177873ee5756SRandall Stewart 				 * It should not of, but lets give the old
177973ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
178073ee5756SRandall Stewart 				 */
178173ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
178273ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
178373ee5756SRandall Stewart 				}
178473ee5756SRandall Stewart 			 	goto err_out;
178573ee5756SRandall Stewart 			}
178673ee5756SRandall Stewart 		}
1787587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1788086a3556SAndrew Gallatin 			struct epoch_tracker et;
1789587d67c0SRandall Stewart 			/*
1790587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1791587d67c0SRandall Stewart 			 * the tcb is not going away.
1792587d67c0SRandall Stewart 			 */
1793086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1794587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1795086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1796587d67c0SRandall Stewart 		}
179773ee5756SRandall Stewart 		/*
179873ee5756SRandall Stewart 		 * Release the old refcnt, the
179973ee5756SRandall Stewart 		 * lookup acquired a ref on the
180073ee5756SRandall Stewart 		 * new one already.
180173ee5756SRandall Stewart 		 */
180255bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
180373ee5756SRandall Stewart 		/*
180473ee5756SRandall Stewart 		 * Set in the new stack.
180573ee5756SRandall Stewart 		 */
180655bceb1eSRandall Stewart 		tp->t_fb = blk;
180773ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
180855bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
180955bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
181055bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
181155bceb1eSRandall Stewart 			     sopt->sopt_name);
181255bceb1eSRandall Stewart 		}
181355bceb1eSRandall Stewart #endif
18143ee9c3c4SRandall Stewart err_out:
181555bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
181655bceb1eSRandall Stewart 		return (error);
181773ee5756SRandall Stewart 
1818fc4d53ccSGleb Smirnoff 	}
1819fc4d53ccSGleb Smirnoff 
18203b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
182166fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1822fc4d53ccSGleb Smirnoff }
1823fc4d53ccSGleb Smirnoff 
1824fc4d53ccSGleb Smirnoff static int
1825fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1826fc4d53ccSGleb Smirnoff {
1827fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1828fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1829fc4d53ccSGleb Smirnoff 	int error = 0;
1830fc4d53ccSGleb Smirnoff 
1831fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18323b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
183353af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1834fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1835fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1836fc4d53ccSGleb Smirnoff 
1837fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18383b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1839fc4d53ccSGleb Smirnoff #ifdef INET6
1840fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1841fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1842fc4d53ccSGleb Smirnoff #endif /* INET6 */
1843fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1844fc4d53ccSGleb Smirnoff 		else
1845fc4d53ccSGleb Smirnoff #endif
1846fc4d53ccSGleb Smirnoff #ifdef INET
1847fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1848fc4d53ccSGleb Smirnoff #endif
1849fc4d53ccSGleb Smirnoff 		return (error);
1850fc4d53ccSGleb Smirnoff 	}
1851fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1852e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1853fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1854fc4d53ccSGleb Smirnoff 
1855e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1856e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1857e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1858e2833083SPeter Lei 		} else {
1859e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1860e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1861c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1862c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1863e2833083SPeter Lei 		}
186455bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
186555bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
186655bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
186755bceb1eSRandall Stewart 		return (error);
186855bceb1eSRandall Stewart 	}
1869fc4d53ccSGleb Smirnoff 
18703b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
187166fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1872fc4d53ccSGleb Smirnoff }
1873fc4d53ccSGleb Smirnoff 
1874fc4d53ccSGleb Smirnoff int
1875fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1876fc4d53ccSGleb Smirnoff {
1877fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1878fc4d53ccSGleb Smirnoff 
1879fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1880fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1881fc4d53ccSGleb Smirnoff 
18823b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
188353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18843b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18853b3c08c1SMichael Tuexen 		return (ECONNRESET);
18863b3c08c1SMichael Tuexen 	}
1887fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1888fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1889fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1890fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1891fc4d53ccSGleb Smirnoff 	else
1892fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
189355bceb1eSRandall Stewart }
189455bceb1eSRandall Stewart 
18952529f56eSJonathan T. Looney /*
18962529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18972529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18982529f56eSJonathan T. Looney  */
18992529f56eSJonathan T. Looney #ifdef CTASSERT
19002529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
19012529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
19022529f56eSJonathan T. Looney #endif
19032529f56eSJonathan T. Looney 
1904ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1905ec1db6e1SJohn Baldwin static int
1906ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1907ec1db6e1SJohn Baldwin {
1908ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1909ec1db6e1SJohn Baldwin 	int error;
1910ec1db6e1SJohn Baldwin 
1911ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1912ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1913ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1914ec1db6e1SJohn Baldwin 		if (error)
1915ec1db6e1SJohn Baldwin 			return (error);
1916ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1917ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1918ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1919ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1920ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1921ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1922ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1923ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1924ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1925ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1926ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1927ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1928ec1db6e1SJohn Baldwin 		return (0);
1929ec1db6e1SJohn Baldwin 	}
1930ec1db6e1SJohn Baldwin 
1931ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1932ec1db6e1SJohn Baldwin }
1933ec1db6e1SJohn Baldwin #endif
1934ec1db6e1SJohn Baldwin 
1935b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1936b8d60729SRandall Stewart 
1937b8d60729SRandall Stewart static int
1938ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1939b8d60729SRandall Stewart {
1940b8d60729SRandall Stewart 	struct cc_algo *algo;
1941b8d60729SRandall Stewart 	void *ptr = NULL;
19423b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1943b8d60729SRandall Stewart 	struct cc_var cc_mem;
1944b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1945b8d60729SRandall Stewart 	size_t mem_sz;
1946b8d60729SRandall Stewart 	int error;
1947b8d60729SRandall Stewart 
1948b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1949b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1950b8d60729SRandall Stewart 	if (error)
1951b8d60729SRandall Stewart 		return(error);
1952b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1953b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1954ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1955b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1956b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1957b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1958b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1959b8d60729SRandall Stewart 				continue;
1960b8d60729SRandall Stewart 			}
1961b8d60729SRandall Stewart 			break;
1962b8d60729SRandall Stewart 		}
1963ea9017fbSRandall Stewart 	}
1964b8d60729SRandall Stewart 	if (algo == NULL) {
1965b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1966b8d60729SRandall Stewart 		return(ESRCH);
1967b8d60729SRandall Stewart 	}
1968ea9017fbSRandall Stewart 	/*
1969ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1970ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1971ea9017fbSRandall Stewart 	 */
1972ea9017fbSRandall Stewart 	cc_refer(algo);
1973ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1974b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1975b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1976b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1977b8d60729SRandall Stewart 		if (mem_sz == 0) {
1978b8d60729SRandall Stewart 			goto no_mem_needed;
1979b8d60729SRandall Stewart 		}
1980b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1981b8d60729SRandall Stewart 	} else {
1982b8d60729SRandall Stewart no_mem_needed:
1983b8d60729SRandall Stewart 		mem_sz = 0;
1984b8d60729SRandall Stewart 		ptr = NULL;
1985b8d60729SRandall Stewart 	}
1986b8d60729SRandall Stewart 	/*
1987b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1988b8d60729SRandall Stewart 	 * back the inplock.
1989b8d60729SRandall Stewart 	 */
1990b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1991df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
199253af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1993df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1994ea9017fbSRandall Stewart 		if (ptr)
1995df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1996ea9017fbSRandall Stewart 		/* Release our temp reference */
1997ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1998ea9017fbSRandall Stewart 		cc_release(algo);
1999ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
2000df07bfdaSMichael Tuexen 		return (ECONNRESET);
2001df07bfdaSMichael Tuexen 	}
2002df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
2003df07bfdaSMichael Tuexen 	if (ptr != NULL)
2004b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
2005b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
2006b8d60729SRandall Stewart 	/*
2007b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
2008b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
2009b8d60729SRandall Stewart 	 * Note here we init into stack memory.
2010b8d60729SRandall Stewart 	 */
2011b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
2012b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
2013b8d60729SRandall Stewart 	else
2014b8d60729SRandall Stewart 		error = 0;
2015b8d60729SRandall Stewart 	/*
2016b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
2017b8d60729SRandall Stewart 	 * should not fail we could in theory have a
2018b8d60729SRandall Stewart 	 * KASSERT here.
2019b8d60729SRandall Stewart 	 */
2020b8d60729SRandall Stewart 	if (error == 0) {
2021b8d60729SRandall Stewart 		/*
2022b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
2023b8d60729SRandall Stewart 		 * connection to the new CC module by
2024b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
2025b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
2026b8d60729SRandall Stewart 		 */
2027b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
2028e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2029ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
2030ea9017fbSRandall Stewart 		cc_detach(tp);
2031ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
2032e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
2033ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
2034ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2035b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2036b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2037b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2038e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2039b8d60729SRandall Stewart 		}
2040b8d60729SRandall Stewart 	} else if (ptr)
2041b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2042b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2043ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2044ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2045ea9017fbSRandall Stewart 	cc_release(algo);
2046ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2047b8d60729SRandall Stewart 	return (error);
2048b8d60729SRandall Stewart }
2049b8d60729SRandall Stewart 
205055bceb1eSRandall Stewart int
205166fbc19fSGleb Smirnoff tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
205255bceb1eSRandall Stewart {
205366fbc19fSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
205455bceb1eSRandall Stewart 	int	error, opt, optval;
205555bceb1eSRandall Stewart 	u_int	ui;
205655bceb1eSRandall Stewart 	struct	tcp_info ti;
2057b2e60773SJohn Baldwin #ifdef KERN_TLS
2058b2e60773SJohn Baldwin 	struct tls_enable tls;
2059528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2060b2e60773SJohn Baldwin #endif
20612529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2062adc56f5aSEdward Tomasz Napierala #ifdef STATS
2063adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2064adc56f5aSEdward Tomasz Napierala #endif
2065af6fef3aSGleb Smirnoff 	size_t	len;
2066df8bae1dSRodney W. Grimes 
2067f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
206853af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2069fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2070528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2071f581a26eSGleb Smirnoff 
2072f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2073f581a26eSGleb Smirnoff #ifdef INET6
2074f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2075f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2076f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2077f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2078f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2079f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2080f581a26eSGleb Smirnoff 		}
2081f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2082f581a26eSGleb Smirnoff 		return (0);
2083f581a26eSGleb Smirnoff #endif
2084f581a26eSGleb Smirnoff #ifdef INET
2085f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2086f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2087f581a26eSGleb Smirnoff 		return (0);
2088f581a26eSGleb Smirnoff #endif
2089f581a26eSGleb Smirnoff 	}
2090f581a26eSGleb Smirnoff 
2091d519cedbSGleb Smirnoff 	/*
2092d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2093d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2094d519cedbSGleb Smirnoff 	 */
2095d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2096d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2097d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2098c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2099c8b53cedSMichael Tuexen 			return (EINVAL);
2100af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2101af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2102d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2103d519cedbSGleb Smirnoff 		if (error) {
2104af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2105d519cedbSGleb Smirnoff 			return (error);
2106d519cedbSGleb Smirnoff 		}
2107bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2108d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2109e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2110d519cedbSGleb Smirnoff 		else
2111d519cedbSGleb Smirnoff 			error = ENOENT;
2112d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2113d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2114af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2115af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2116d519cedbSGleb Smirnoff 		return (error);
2117d519cedbSGleb Smirnoff 	}
2118d519cedbSGleb Smirnoff 
2119cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2120cfe8b629SGarrett Wollman 	case SOPT_SET:
2121cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2122fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
212388f6b043SBruce M Simpson 		case TCP_MD5SIG:
21248501a69cSRobert Watson 			INP_WUNLOCK(inp);
212597453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2126fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2127fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
21281cfd4b53SBruce M Simpson 			if (error)
21291e8f5ffaSRobert Watson 				return (error);
213097453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
213109fe6320SNavdeep Parhar 			goto unlock_and_done;
2132fcf59617SAndrey V. Elsukov #endif /* IPSEC */
213309fe6320SNavdeep Parhar 
2134df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2135cfe8b629SGarrett Wollman 		case TCP_NOOPT:
21360471a8c7SRichard Scheffenegger 		case TCP_LRD:
21378501a69cSRobert Watson 			INP_WUNLOCK(inp);
2138cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2139cfe8b629SGarrett Wollman 			    sizeof optval);
2140cfe8b629SGarrett Wollman 			if (error)
21411e8f5ffaSRobert Watson 				return (error);
2142cfe8b629SGarrett Wollman 
21438501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2144cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2145cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2146cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2147cfe8b629SGarrett Wollman 				break;
2148cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2149cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2150cfe8b629SGarrett Wollman 				break;
21510471a8c7SRichard Scheffenegger 			case TCP_LRD:
21520471a8c7SRichard Scheffenegger 				opt = TF_LRD;
21530471a8c7SRichard Scheffenegger 				break;
2154cfe8b629SGarrett Wollman 			default:
2155cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2156cfe8b629SGarrett Wollman 				break;
2157cfe8b629SGarrett Wollman 			}
2158cfe8b629SGarrett Wollman 
2159cfe8b629SGarrett Wollman 			if (optval)
2160cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2161df8bae1dSRodney W. Grimes 			else
2162cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
216309fe6320SNavdeep Parhar unlock_and_done:
216409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
216509fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
216609fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
216709fe6320SNavdeep Parhar 				    sopt->sopt_name);
216809fe6320SNavdeep Parhar 			}
216909fe6320SNavdeep Parhar #endif
21708501a69cSRobert Watson 			INP_WUNLOCK(inp);
2171df8bae1dSRodney W. Grimes 			break;
2172df8bae1dSRodney W. Grimes 
2173007581c0SJonathan Lemon 		case TCP_NOPUSH:
21748501a69cSRobert Watson 			INP_WUNLOCK(inp);
2175007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2176007581c0SJonathan Lemon 			    sizeof optval);
2177007581c0SJonathan Lemon 			if (error)
21781e8f5ffaSRobert Watson 				return (error);
2179007581c0SJonathan Lemon 
21808501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2181007581c0SJonathan Lemon 			if (optval)
2182007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2183d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2184007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2185109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2186109eb549SGleb Smirnoff 					struct epoch_tracker et;
2187109eb549SGleb Smirnoff 
2188109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2189f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2190109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2191109eb549SGleb Smirnoff 				}
2192007581c0SJonathan Lemon 			}
219309fe6320SNavdeep Parhar 			goto unlock_and_done;
2194007581c0SJonathan Lemon 
21959e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21969e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21979e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21989e644c23SMichael Tuexen 			    sizeof optval);
21999e644c23SMichael Tuexen 			if (error)
22009e644c23SMichael Tuexen 				return (error);
22019e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
22029e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
22039e644c23SMichael Tuexen 				/* Its got to be in range */
22049e644c23SMichael Tuexen 				return (EINVAL);
22059e644c23SMichael Tuexen 			}
22069e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
22079e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
22089e644c23SMichael Tuexen 				return (EINVAL);
22099e644c23SMichael Tuexen 			}
22109e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
22119e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
22129e644c23SMichael Tuexen 				/* You can't change after you are connected */
22139e644c23SMichael Tuexen 				error = EINVAL;
22149e644c23SMichael Tuexen 			} else {
22159e644c23SMichael Tuexen 				/* Ok we are all good set the port */
22169e644c23SMichael Tuexen 				tp->t_port = htons(optval);
22179e644c23SMichael Tuexen 			}
22189e644c23SMichael Tuexen 			goto unlock_and_done;
22199e644c23SMichael Tuexen 
2220df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
22218501a69cSRobert Watson 			INP_WUNLOCK(inp);
2222cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2223cfe8b629SGarrett Wollman 			    sizeof optval);
2224cfe8b629SGarrett Wollman 			if (error)
22251e8f5ffaSRobert Watson 				return (error);
2226df8bae1dSRodney W. Grimes 
22278501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
222853369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2229603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2230cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2231a0292f23SGarrett Wollman 			else
2232a0292f23SGarrett Wollman 				error = EINVAL;
223309fe6320SNavdeep Parhar 			goto unlock_and_done;
2234a0292f23SGarrett Wollman 
2235b8af5dfaSRobert Watson 		case TCP_INFO:
22368501a69cSRobert Watson 			INP_WUNLOCK(inp);
2237b8af5dfaSRobert Watson 			error = EINVAL;
2238b8af5dfaSRobert Watson 			break;
2239b8af5dfaSRobert Watson 
2240adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2241adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2242adc56f5aSEdward Tomasz Napierala #ifdef STATS
2243adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2244adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2245adc56f5aSEdward Tomasz Napierala 			if (error)
2246adc56f5aSEdward Tomasz Napierala 				return (error);
2247adc56f5aSEdward Tomasz Napierala 
2248adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2249adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2250adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2251adc56f5aSEdward Tomasz Napierala 			else
2252adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2253adc56f5aSEdward Tomasz Napierala 
2254adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2255adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2256adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2257adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2258adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2259adc56f5aSEdward Tomasz Napierala 				sbp = t;
2260adc56f5aSEdward Tomasz Napierala 			}
2261adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2262adc56f5aSEdward Tomasz Napierala 
2263adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2264adc56f5aSEdward Tomasz Napierala #else
2265adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2266adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2267adc56f5aSEdward Tomasz Napierala 			break;
2268adc56f5aSEdward Tomasz Napierala 
2269dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2270ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
227173e263b1SGleb Smirnoff 			break;
2272dbc42409SLawrence Stewart 
2273a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2274a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2275a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2276a034518aSAndrew Gallatin 			    sizeof(optval));
2277a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2278a034518aSAndrew Gallatin 			if (!error)
2279a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2280a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2281a034518aSAndrew Gallatin 			break;
2282a034518aSAndrew Gallatin 
2283b2e60773SJohn Baldwin #ifdef KERN_TLS
2284b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2285b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2286ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2287b2e60773SJohn Baldwin 			if (error)
2288b2e60773SJohn Baldwin 				break;
2289fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2290b2e60773SJohn Baldwin 			break;
2291b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2292b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2293b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2294b2e60773SJohn Baldwin 			if (error)
2295b2e60773SJohn Baldwin 				return (error);
2296b2e60773SJohn Baldwin 
2297b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2298fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2299b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2300b2e60773SJohn Baldwin 			break;
2301f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2302f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2303f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2304f1f93475SJohn Baldwin 			    sizeof(tls));
2305f1f93475SJohn Baldwin 			if (error)
2306f1f93475SJohn Baldwin 				break;
2307fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2308f1f93475SJohn Baldwin 			break;
2309b2e60773SJohn Baldwin #endif
231008af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
23119077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
23129077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
23139077f387SGleb Smirnoff 		case TCP_KEEPINIT:
23149077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
23159077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
23169077f387SGleb Smirnoff 			if (error)
23179077f387SGleb Smirnoff 				return (error);
23189077f387SGleb Smirnoff 
23199077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
23209077f387SGleb Smirnoff 				error = EINVAL;
23219077f387SGleb Smirnoff 				break;
23229077f387SGleb Smirnoff 			}
23239077f387SGleb Smirnoff 			ui *= hz;
23249077f387SGleb Smirnoff 
23259077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
23269077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
232708af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
232808af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
232908af8aacSRandall Stewart 				break;
233008af8aacSRandall Stewart 
23319077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23329077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23339077f387SGleb Smirnoff 				/*
23349077f387SGleb Smirnoff 				 * XXX: better check current remaining
23359077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
23369077f387SGleb Smirnoff 				 */
23379077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
23389077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
23399077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23409077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23419077f387SGleb Smirnoff 				break;
23429077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23439077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23449077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23459077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23469077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23479077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23489077f387SGleb Smirnoff 				break;
23499077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23509077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23519077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23529077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23539077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23549077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23559077f387SGleb Smirnoff 				break;
23569077f387SGleb Smirnoff 			}
235709fe6320SNavdeep Parhar 			goto unlock_and_done;
23589077f387SGleb Smirnoff 
235985c05144SGleb Smirnoff 		case TCP_KEEPCNT:
236085c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
236185c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
236285c05144SGleb Smirnoff 			if (error)
236385c05144SGleb Smirnoff 				return (error);
236485c05144SGleb Smirnoff 
236585c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
236685c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
236785c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
236885c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
236985c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
237085c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
237185c05144SGleb Smirnoff 			goto unlock_and_done;
237285c05144SGleb Smirnoff 
237386a996e6SHiren Panchasara #ifdef TCPPCAP
237486a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
237586a996e6SHiren Panchasara 		case TCP_PCAP_IN:
237686a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
237786a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
237886a996e6SHiren Panchasara 			    sizeof optval);
237986a996e6SHiren Panchasara 			if (error)
238086a996e6SHiren Panchasara 				return (error);
238186a996e6SHiren Panchasara 
238286a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
238386a996e6SHiren Panchasara 			if (optval >= 0)
2384399a5655SRichard Scheffenegger 				tcp_pcap_set_sock_max(
2385399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
238686a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
238786a996e6SHiren Panchasara 					optval);
238886a996e6SHiren Panchasara 			else
238986a996e6SHiren Panchasara 				error = EINVAL;
239086a996e6SHiren Panchasara 			goto unlock_and_done;
239186a996e6SHiren Panchasara #endif
239286a996e6SHiren Panchasara 
2393c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2394c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2395c560df6fSPatrick Kelsey 
2396281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2397c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2398c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2399281a0fd4SPatrick Kelsey 				return (EPERM);
2400281a0fd4SPatrick Kelsey 
2401c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2402c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2403281a0fd4SPatrick Kelsey 			if (error)
2404281a0fd4SPatrick Kelsey 				return (error);
2405281a0fd4SPatrick Kelsey 
2406281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2407d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2408d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2409d442a657SMichael Tuexen 				error = EINVAL;
2410d442a657SMichael Tuexen 				goto unlock_and_done;
2411d442a657SMichael Tuexen 			}
2412c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2413c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2414c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2415c560df6fSPatrick Kelsey 						error = EPERM;
2416c560df6fSPatrick Kelsey 						goto unlock_and_done;
2417c560df6fSPatrick Kelsey 					}
2418c560df6fSPatrick Kelsey 
2419c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2420281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2421281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2422c560df6fSPatrick Kelsey 				} else {
2423c560df6fSPatrick Kelsey 					/*
2424c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2425c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2426c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2427c560df6fSPatrick Kelsey 					 * connect.
2428c560df6fSPatrick Kelsey 					 */
2429c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2430c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2431c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2432c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2433c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2434c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2435c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2436c560df6fSPatrick Kelsey 					}
2437c560df6fSPatrick Kelsey 				}
2438d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2439281a0fd4SPatrick Kelsey 			} else
2440281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2441281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2442c560df6fSPatrick Kelsey 		}
2443281a0fd4SPatrick Kelsey 
2444e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24452529f56eSJonathan T. Looney 		case TCP_LOG:
24462529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24472529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24482529f56eSJonathan T. Looney 			    sizeof optval);
24492529f56eSJonathan T. Looney 			if (error)
24502529f56eSJonathan T. Looney 				return (error);
24512529f56eSJonathan T. Looney 
24522529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24532529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24542529f56eSJonathan T. Looney 			goto unlock_and_done;
24552529f56eSJonathan T. Looney 
24562529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24572529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24582529f56eSJonathan T. Looney 			error = EINVAL;
24592529f56eSJonathan T. Looney 			break;
24602529f56eSJonathan T. Looney 
24612529f56eSJonathan T. Looney 		case TCP_LOGID:
24622529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24632529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24642529f56eSJonathan T. Looney 			if (error)
24652529f56eSJonathan T. Looney 				break;
24662529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24672529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24682529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24692529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24702529f56eSJonathan T. Looney 			break;
24712529f56eSJonathan T. Looney 
24722529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24732529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24742529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24752529f56eSJonathan T. Looney 			error =
24762529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
24772529f56eSJonathan T. Looney 			if (error)
24782529f56eSJonathan T. Looney 				break;
24792529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24802529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24812529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
24822529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24832529f56eSJonathan T. Looney 				    M_WAITOK, true);
24842529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24852529f56eSJonathan T. Looney 			} else {
24862529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24872529f56eSJonathan T. Looney 				/*
24882529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24892529f56eSJonathan T. Looney 				 * INP lock.
24902529f56eSJonathan T. Looney 				 */
24912529f56eSJonathan T. Looney 			}
24922529f56eSJonathan T. Looney 			break;
2493e24e5683SJonathan T. Looney #endif
24942529f56eSJonathan T. Looney 
2495df8bae1dSRodney W. Grimes 		default:
24968501a69cSRobert Watson 			INP_WUNLOCK(inp);
2497df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2498df8bae1dSRodney W. Grimes 			break;
2499df8bae1dSRodney W. Grimes 		}
2500df8bae1dSRodney W. Grimes 		break;
2501df8bae1dSRodney W. Grimes 
2502cfe8b629SGarrett Wollman 	case SOPT_GET:
25031e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2504cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2505fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
250688f6b043SBruce M Simpson 		case TCP_MD5SIG:
25078501a69cSRobert Watson 			INP_WUNLOCK(inp);
250897453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2509fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2510fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
25111cfd4b53SBruce M Simpson 			break;
2512265ed012SBruce M Simpson #endif
25131e8f5ffaSRobert Watson 
2514df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2515cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
25168501a69cSRobert Watson 			INP_WUNLOCK(inp);
2517b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2518df8bae1dSRodney W. Grimes 			break;
2519df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2520cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
25218501a69cSRobert Watson 			INP_WUNLOCK(inp);
2522b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2523df8bae1dSRodney W. Grimes 			break;
25249e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
25259e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
25269e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
25279e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25289e644c23SMichael Tuexen 			break;
2529a0292f23SGarrett Wollman 		case TCP_NOOPT:
2530cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
25318501a69cSRobert Watson 			INP_WUNLOCK(inp);
2532b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2533a0292f23SGarrett Wollman 			break;
2534a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2535cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
25368501a69cSRobert Watson 			INP_WUNLOCK(inp);
2537b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2538b8af5dfaSRobert Watson 			break;
2539b8af5dfaSRobert Watson 		case TCP_INFO:
2540b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
25418501a69cSRobert Watson 			INP_WUNLOCK(inp);
2542b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2543a0292f23SGarrett Wollman 			break;
2544adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2545adc56f5aSEdward Tomasz Napierala 			{
2546adc56f5aSEdward Tomasz Napierala #ifdef STATS
2547adc56f5aSEdward Tomasz Napierala 			int nheld;
2548adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2549adc56f5aSEdward Tomasz Napierala 
2550adc56f5aSEdward Tomasz Napierala 			error = 0;
2551adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2552adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2553adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2554adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2555adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2556adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2557adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2558adc56f5aSEdward Tomasz Napierala 			else
2559adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2560adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2561adc56f5aSEdward Tomasz Napierala 			if (error)
2562adc56f5aSEdward Tomasz Napierala 				break;
2563adc56f5aSEdward Tomasz Napierala 
2564adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2565adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2566adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2567adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2568adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2569adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2570adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2571adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2572adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2573adc56f5aSEdward Tomasz Napierala 				break;
2574adc56f5aSEdward Tomasz Napierala 			}
2575adc56f5aSEdward Tomasz Napierala 
2576adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2577adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2578adc56f5aSEdward Tomasz Napierala 				goto unhold;
2579adc56f5aSEdward Tomasz Napierala 
2580adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2581adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2582adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2583adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2584adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2585adc56f5aSEdward Tomasz Napierala unhold:
2586adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2587adc56f5aSEdward Tomasz Napierala #else
2588adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2589adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2590adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2591adc56f5aSEdward Tomasz Napierala 			break;
2592adc56f5aSEdward Tomasz Napierala 			}
2593dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2594af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2595dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2596af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2597dbc42409SLawrence Stewart 			break;
259808af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25992f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
26002f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
26012f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
26022f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
26032f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
260408af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
260508af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
260608af8aacSRandall Stewart 				break;
26072f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
26085a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
26092f3eb7f4SGleb Smirnoff 				break;
26102f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
26115a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
26122f3eb7f4SGleb Smirnoff 				break;
26132f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
26145a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
26152f3eb7f4SGleb Smirnoff 				break;
26162f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
26175a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
26182f3eb7f4SGleb Smirnoff 				break;
26192f3eb7f4SGleb Smirnoff 			}
26202f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
26212f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
26222f3eb7f4SGleb Smirnoff 			break;
262386a996e6SHiren Panchasara #ifdef TCPPCAP
262486a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
262586a996e6SHiren Panchasara 		case TCP_PCAP_IN:
2626399a5655SRichard Scheffenegger 			optval = tcp_pcap_get_sock_max(
2627399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
262886a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
262986a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
263086a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
263186a996e6SHiren Panchasara 			break;
263286a996e6SHiren Panchasara #endif
2633281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2634281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2635281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2636281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2637281a0fd4SPatrick Kelsey 			break;
2638e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
26392529f56eSJonathan T. Looney 		case TCP_LOG:
264069c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
26412529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26422529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
26432529f56eSJonathan T. Looney 			break;
26442529f56eSJonathan T. Looney 		case TCP_LOGBUF:
26452529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
26462529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26472529f56eSJonathan T. Looney 			break;
26482529f56eSJonathan T. Looney 		case TCP_LOGID:
26492529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26502529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26512529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26522529f56eSJonathan T. Looney 			break;
26532529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26542529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26552529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26562529f56eSJonathan T. Looney 			error = EINVAL;
26572529f56eSJonathan T. Looney 			break;
2658e24e5683SJonathan T. Looney #endif
2659b2e60773SJohn Baldwin #ifdef KERN_TLS
2660b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2661fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2662b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2663bf256782SMark Johnston 			if (error == 0)
2664bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2665bf256782SMark Johnston 				    sizeof(optval));
2666b2e60773SJohn Baldwin 			break;
2667f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2668fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2669f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2670bf256782SMark Johnston 			if (error == 0)
2671bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2672bf256782SMark Johnston 				    sizeof(optval));
2673f1f93475SJohn Baldwin 			break;
2674b2e60773SJohn Baldwin #endif
26750471a8c7SRichard Scheffenegger 		case TCP_LRD:
26760471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
26770471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
26780471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
26790471a8c7SRichard Scheffenegger 			break;
2680df8bae1dSRodney W. Grimes 		default:
26818501a69cSRobert Watson 			INP_WUNLOCK(inp);
2682df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2683df8bae1dSRodney W. Grimes 			break;
2684df8bae1dSRodney W. Grimes 		}
2685df8bae1dSRodney W. Grimes 		break;
2686df8bae1dSRodney W. Grimes 	}
2687df8bae1dSRodney W. Grimes 	return (error);
2688df8bae1dSRodney W. Grimes }
26898501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2690bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2691df8bae1dSRodney W. Grimes 
269226e30fbbSDavid Greenman /*
2693df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2694df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2695df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2696df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2697df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2698df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2699df8bae1dSRodney W. Grimes  */
2700623dce13SRobert Watson static void
2701ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2702df8bae1dSRodney W. Grimes {
27039eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
27049eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2705e6e0b5ffSRobert Watson 
270697a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27078501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2708df8bae1dSRodney W. Grimes 
2709623dce13SRobert Watson 	/*
2710623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2711623dce13SRobert Watson 	 * socket is still open.
2712623dce13SRobert Watson 	 */
27138db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
27148db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2715df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2716623dce13SRobert Watson 		KASSERT(tp != NULL,
2717623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2718623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2719243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2720623dce13SRobert Watson 		KASSERT(tp != NULL,
2721623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2722623dce13SRobert Watson 	} else {
2723df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2724df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2725623dce13SRobert Watson 		tcp_usrclosed(tp);
2726ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2727f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2728f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2729df8bae1dSRodney W. Grimes 	}
2730df8bae1dSRodney W. Grimes }
2731df8bae1dSRodney W. Grimes 
2732df8bae1dSRodney W. Grimes /*
2733df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2734df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2735df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2736df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2737df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2738df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2739df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2740df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2741df8bae1dSRodney W. Grimes  */
2742623dce13SRobert Watson static void
2743ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2744df8bae1dSRodney W. Grimes {
2745df8bae1dSRodney W. Grimes 
274697a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27479eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2748e6e0b5ffSRobert Watson 
2749df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2750df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
275109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
275209fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
275309fe6320SNavdeep Parhar #endif
2754550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2755bc65987aSKip Macy 		/* FALLTHROUGH */
2756bc65987aSKip Macy 	case TCPS_CLOSED:
2757df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2758623dce13SRobert Watson 		/*
2759623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2760623dce13SRobert Watson 		 * still open.
2761623dce13SRobert Watson 		 */
2762623dce13SRobert Watson 		KASSERT(tp != NULL,
2763623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2764df8bae1dSRodney W. Grimes 		break;
2765df8bae1dSRodney W. Grimes 
2766a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2767df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2768a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2769a0292f23SGarrett Wollman 		break;
2770a0292f23SGarrett Wollman 
2771df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
277257f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2773df8bae1dSRodney W. Grimes 		break;
2774df8bae1dSRodney W. Grimes 
2775df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
277657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2777df8bae1dSRodney W. Grimes 		break;
2778df8bae1dSRodney W. Grimes 	}
277908af8aacSRandall Stewart 	if (tp->t_acktime == 0)
278008af8aacSRandall Stewart 		tp->t_acktime = ticks;
2781abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27829eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2783abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27847c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27857c72af87SMohan Srinivasan 			int timeout;
27867c72af87SMohan Srinivasan 
27877c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27889077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2789b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2790b6239c4aSAndras Olah 		}
2791df8bae1dSRodney W. Grimes 	}
27927c72af87SMohan Srinivasan }
2793497057eeSRobert Watson 
2794497057eeSRobert Watson #ifdef DDB
2795497057eeSRobert Watson static void
2796497057eeSRobert Watson db_print_indent(int indent)
2797497057eeSRobert Watson {
2798497057eeSRobert Watson 	int i;
2799497057eeSRobert Watson 
2800497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2801497057eeSRobert Watson 		db_printf(" ");
2802497057eeSRobert Watson }
2803497057eeSRobert Watson 
2804497057eeSRobert Watson static void
2805497057eeSRobert Watson db_print_tstate(int t_state)
2806497057eeSRobert Watson {
2807497057eeSRobert Watson 
2808497057eeSRobert Watson 	switch (t_state) {
2809497057eeSRobert Watson 	case TCPS_CLOSED:
2810497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2811497057eeSRobert Watson 		return;
2812497057eeSRobert Watson 
2813497057eeSRobert Watson 	case TCPS_LISTEN:
2814497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2815497057eeSRobert Watson 		return;
2816497057eeSRobert Watson 
2817497057eeSRobert Watson 	case TCPS_SYN_SENT:
2818497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2819497057eeSRobert Watson 		return;
2820497057eeSRobert Watson 
2821497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2822497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2823497057eeSRobert Watson 		return;
2824497057eeSRobert Watson 
2825497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2826497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2827497057eeSRobert Watson 		return;
2828497057eeSRobert Watson 
2829497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2830497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2831497057eeSRobert Watson 		return;
2832497057eeSRobert Watson 
2833497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2834497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2835497057eeSRobert Watson 		return;
2836497057eeSRobert Watson 
2837497057eeSRobert Watson 	case TCPS_CLOSING:
2838497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2839497057eeSRobert Watson 		return;
2840497057eeSRobert Watson 
2841497057eeSRobert Watson 	case TCPS_LAST_ACK:
2842497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2843497057eeSRobert Watson 		return;
2844497057eeSRobert Watson 
2845497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2846497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2847497057eeSRobert Watson 		return;
2848497057eeSRobert Watson 
2849497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2850497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2851497057eeSRobert Watson 		return;
2852497057eeSRobert Watson 
2853497057eeSRobert Watson 	default:
2854497057eeSRobert Watson 		db_printf("unknown");
2855497057eeSRobert Watson 		return;
2856497057eeSRobert Watson 	}
2857497057eeSRobert Watson }
2858497057eeSRobert Watson 
2859497057eeSRobert Watson static void
2860497057eeSRobert Watson db_print_tflags(u_int t_flags)
2861497057eeSRobert Watson {
2862497057eeSRobert Watson 	int comma;
2863497057eeSRobert Watson 
2864497057eeSRobert Watson 	comma = 0;
2865497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2866497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2867497057eeSRobert Watson 		comma = 1;
2868497057eeSRobert Watson 	}
2869497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2870497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2871497057eeSRobert Watson 		comma = 1;
2872497057eeSRobert Watson 	}
2873497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2874497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2875497057eeSRobert Watson 		comma = 1;
2876497057eeSRobert Watson 	}
2877497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2878497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2879497057eeSRobert Watson 		comma = 1;
2880497057eeSRobert Watson 	}
2881497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2882497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2883497057eeSRobert Watson 		comma = 1;
2884497057eeSRobert Watson 	}
2885497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2886497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2887497057eeSRobert Watson 		comma = 1;
2888497057eeSRobert Watson 	}
2889497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2890497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2891497057eeSRobert Watson 		comma = 1;
2892497057eeSRobert Watson 	}
2893497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2894497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2895497057eeSRobert Watson 		comma = 1;
2896497057eeSRobert Watson 	}
2897497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2898497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2899497057eeSRobert Watson 		comma = 1;
2900497057eeSRobert Watson 	}
2901497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2902497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2903497057eeSRobert Watson 		comma = 1;
2904497057eeSRobert Watson 	}
2905497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2906497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2907497057eeSRobert Watson 		comma = 1;
2908497057eeSRobert Watson 	}
2909497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2910497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2911497057eeSRobert Watson 		comma = 1;
2912497057eeSRobert Watson 	}
2913497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2914497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2915497057eeSRobert Watson 		comma = 1;
2916497057eeSRobert Watson 	}
29173f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
29183f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
29193f169c54SRichard Scheffenegger 		comma = 1;
29203f169c54SRichard Scheffenegger 	}
2921497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2922497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2923497057eeSRobert Watson 		comma = 1;
2924497057eeSRobert Watson 	}
2925493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2926493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2927497057eeSRobert Watson 		comma = 1;
2928497057eeSRobert Watson 	}
2929497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2930497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2931497057eeSRobert Watson 		comma = 1;
2932497057eeSRobert Watson 	}
2933497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2934497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2935497057eeSRobert Watson 		comma = 1;
2936497057eeSRobert Watson 	}
2937497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2938497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2939497057eeSRobert Watson 		comma = 1;
2940497057eeSRobert Watson 	}
2941dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2942dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2943dbc42409SLawrence Stewart 		comma = 1;
2944dbc42409SLawrence Stewart 	}
2945497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2946497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2947497057eeSRobert Watson 		comma = 1;
2948497057eeSRobert Watson 	}
29493f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
29503f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
29513f169c54SRichard Scheffenegger 		comma = 1;
29523f169c54SRichard Scheffenegger 	}
2953497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2954497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2955497057eeSRobert Watson 		comma = 1;
2956497057eeSRobert Watson 	}
2957497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2958497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2959497057eeSRobert Watson 		comma = 1;
2960497057eeSRobert Watson 	}
2961497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2962497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2963497057eeSRobert Watson 		comma = 1;
2964497057eeSRobert Watson 	}
2965281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2966281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2967281a0fd4SPatrick Kelsey 		comma = 1;
2968281a0fd4SPatrick Kelsey 	}
2969497057eeSRobert Watson }
2970497057eeSRobert Watson 
2971497057eeSRobert Watson static void
29723cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29733cf38784SMichael Tuexen {
29743cf38784SMichael Tuexen 	int comma;
29753cf38784SMichael Tuexen 
29763cf38784SMichael Tuexen 	comma = 0;
29773f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29783f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29793f169c54SRichard Scheffenegger 		comma = 1;
29803f169c54SRichard Scheffenegger 	}
29813f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29823f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29833f169c54SRichard Scheffenegger 		comma = 1;
29843f169c54SRichard Scheffenegger 	}
29853f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29863f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29873f169c54SRichard Scheffenegger 		comma = 1;
29883f169c54SRichard Scheffenegger 	}
29893f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29903f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29913f169c54SRichard Scheffenegger 		comma = 1;
29923f169c54SRichard Scheffenegger 	}
29933f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29943f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29953f169c54SRichard Scheffenegger 		comma = 1;
29963f169c54SRichard Scheffenegger 	}
29973cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29983cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29993cf38784SMichael Tuexen 		comma = 1;
30003cf38784SMichael Tuexen 	}
30013f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
30023f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
30033f169c54SRichard Scheffenegger 		comma = 1;
30043f169c54SRichard Scheffenegger 	}
30053f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
30063f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
30073f169c54SRichard Scheffenegger 		comma = 1;
30083f169c54SRichard Scheffenegger 	}
30093f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
30103f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
30113f169c54SRichard Scheffenegger 		comma = 1;
30123f169c54SRichard Scheffenegger 	}
30133f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
30143f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30153f169c54SRichard Scheffenegger 		comma = 1;
30163f169c54SRichard Scheffenegger 	}
30173cf38784SMichael Tuexen }
30183cf38784SMichael Tuexen 
30193cf38784SMichael Tuexen static void
3020497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3021497057eeSRobert Watson {
3022497057eeSRobert Watson 	int comma;
3023497057eeSRobert Watson 
3024497057eeSRobert Watson 	comma = 0;
3025497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3026497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3027497057eeSRobert Watson 		comma = 1;
3028497057eeSRobert Watson 	}
3029497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3030497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3031497057eeSRobert Watson 		comma = 1;
3032497057eeSRobert Watson 	}
3033497057eeSRobert Watson }
3034497057eeSRobert Watson 
3035497057eeSRobert Watson static void
3036497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3037497057eeSRobert Watson {
3038497057eeSRobert Watson 
3039497057eeSRobert Watson 	db_print_indent(indent);
3040497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3041497057eeSRobert Watson 
3042497057eeSRobert Watson 	indent += 2;
3043497057eeSRobert Watson 
3044497057eeSRobert Watson 	db_print_indent(indent);
3045497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3046c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3047497057eeSRobert Watson 
3048497057eeSRobert Watson 	db_print_indent(indent);
3049446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3050446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3051497057eeSRobert Watson 
3052497057eeSRobert Watson 	db_print_indent(indent);
3053497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3054497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3055497057eeSRobert Watson 	db_printf(")\n");
3056497057eeSRobert Watson 
3057497057eeSRobert Watson 	db_print_indent(indent);
3058497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3059497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3060497057eeSRobert Watson 	db_printf(")\n");
3061497057eeSRobert Watson 
3062497057eeSRobert Watson 	db_print_indent(indent);
30633cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30643cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30653cf38784SMichael Tuexen 	db_printf(")\n");
30663cf38784SMichael Tuexen 
30673cf38784SMichael Tuexen 	db_print_indent(indent);
3068fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3069497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3070497057eeSRobert Watson 
3071497057eeSRobert Watson 	db_print_indent(indent);
3072497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3073497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3074497057eeSRobert Watson 
3075497057eeSRobert Watson 	db_print_indent(indent);
3076497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3077497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3078497057eeSRobert Watson 
3079497057eeSRobert Watson 	db_print_indent(indent);
30803ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3081497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3082497057eeSRobert Watson 
3083497057eeSRobert Watson 	db_print_indent(indent);
30843ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
30851c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3086497057eeSRobert Watson 
3087497057eeSRobert Watson 	db_print_indent(indent);
30883ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
30891c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3090497057eeSRobert Watson 
3091497057eeSRobert Watson 	db_print_indent(indent);
30920c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
30930c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3094497057eeSRobert Watson 
3095497057eeSRobert Watson 	db_print_indent(indent);
30961c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
30971c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3098497057eeSRobert Watson 
3099497057eeSRobert Watson 	db_print_indent(indent);
31001c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
31011c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3102497057eeSRobert Watson 
3103497057eeSRobert Watson 	db_print_indent(indent);
3104bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3105bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3106497057eeSRobert Watson 
3107497057eeSRobert Watson 	db_print_indent(indent);
310818b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3109497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3110497057eeSRobert Watson 
3111497057eeSRobert Watson 	db_print_indent(indent);
3112497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3113497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3114497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3115497057eeSRobert Watson 
3116497057eeSRobert Watson 	db_print_indent(indent);
3117497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3118497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3119497057eeSRobert Watson 
3120497057eeSRobert Watson 	db_print_indent(indent);
31219f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31221a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3123497057eeSRobert Watson 
3124497057eeSRobert Watson 	db_print_indent(indent);
3125497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31263ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3127497057eeSRobert Watson 
3128497057eeSRobert Watson 	db_print_indent(indent);
31293ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31309f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3131497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3132497057eeSRobert Watson 
3133497057eeSRobert Watson 	db_print_indent(indent);
31343529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31353529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3136497057eeSRobert Watson 
3137497057eeSRobert Watson 	db_print_indent(indent);
3138a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3139a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3140497057eeSRobert Watson 
3141497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3142497057eeSRobert Watson 
3143497057eeSRobert Watson 	db_print_indent(indent);
3144497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3145497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3146497057eeSRobert Watson }
3147497057eeSRobert Watson 
3148497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3149497057eeSRobert Watson {
3150497057eeSRobert Watson 	struct tcpcb *tp;
3151497057eeSRobert Watson 
3152497057eeSRobert Watson 	if (!have_addr) {
3153497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3154497057eeSRobert Watson 		return;
3155497057eeSRobert Watson 	}
3156497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3157497057eeSRobert Watson 
3158497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3159497057eeSRobert Watson }
3160497057eeSRobert Watson #endif
3161