xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 73ee5756dee6b2110eb6fb2b2ef3cde39a1fcb4f)
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 
139453aa7faSMichael Tuexen 	if (tp == NULL) {
140453aa7faSMichael Tuexen 		return;
141453aa7faSMichael Tuexen 	}
14200812bbdSMichael Tuexen 	INP_WLOCK_ASSERT(tptoinpcb(tp));
14369c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
14469c7c811SRandall Stewart 		lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
14500812bbdSMichael Tuexen 		    0, NULL, false, NULL, NULL, 0, NULL);
14600812bbdSMichael Tuexen 	} else {
14700812bbdSMichael Tuexen 		lgb = NULL;
14800812bbdSMichael Tuexen 	}
14900812bbdSMichael Tuexen 	if (lgb != NULL) {
15000812bbdSMichael Tuexen 		if (error >= 0) {
15100812bbdSMichael Tuexen 			lgb->tlb_errno = (uint32_t)error;
15200812bbdSMichael Tuexen 		}
15300812bbdSMichael Tuexen 		lgb->tlb_flex1 = pru;
15400812bbdSMichael Tuexen 	}
15500812bbdSMichael Tuexen }
15600812bbdSMichael Tuexen 
1572c37256eSGarrett Wollman /*
1582c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1592c37256eSGarrett Wollman  * and an internet control block.
1602c37256eSGarrett Wollman  */
1612c37256eSGarrett Wollman static int
162b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1632c37256eSGarrett Wollman {
164f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
165623dce13SRobert Watson 	struct tcpcb *tp = NULL;
166623dce13SRobert Watson 	int error;
1672c37256eSGarrett Wollman 
168623dce13SRobert Watson 	inp = sotoinpcb(so);
169623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1702c37256eSGarrett Wollman 
1710f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1722c37256eSGarrett Wollman 	if (error)
1732c37256eSGarrett Wollman 		goto out;
1742c37256eSGarrett Wollman 
1750f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1760f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1770f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1787669c586SGleb Smirnoff 	if (error)
1790f6385e7SGleb Smirnoff 		goto out;
1800f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1810f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1820f6385e7SGleb Smirnoff 	if (tp == NULL) {
1837669c586SGleb Smirnoff 		error = ENOBUFS;
1840f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1850f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1860f6385e7SGleb Smirnoff 		goto out;
1870f6385e7SGleb Smirnoff 	}
1880f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
18900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ATTACH, error);
1900f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1910f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1922c37256eSGarrett Wollman out:
1935d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1940f6385e7SGleb Smirnoff 	return (error);
1952c37256eSGarrett Wollman }
1962c37256eSGarrett Wollman 
1972c37256eSGarrett Wollman /*
1983fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
199a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
200a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
201a152f8a3SRobert Watson  * inpcb state: time wait.
2022c37256eSGarrett Wollman  */
203bc725eafSRobert Watson static void
2043fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2052c37256eSGarrett Wollman {
2063fed74e9SGleb Smirnoff 	struct inpcb *inp;
2072c37256eSGarrett Wollman 	struct tcpcb *tp;
2082c37256eSGarrett Wollman 
2093fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2103fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2113fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2123fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2133fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
214953b5606SRobert Watson 
215a152f8a3SRobert Watson 	tp = intotcpcb(inp);
216a152f8a3SRobert Watson 
2171b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2181b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2191b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2209c3507f9SGleb Smirnoff 
221623dce13SRobert Watson 	tcp_discardcb(tp);
222623dce13SRobert Watson 	in_pcbdetach(inp);
2230206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
224623dce13SRobert Watson }
225c78cbc7bSRobert Watson 
226b287c6c7SBjoern A. Zeeb #ifdef INET
2272c37256eSGarrett Wollman /*
2282c37256eSGarrett Wollman  * Give the socket an address.
2292c37256eSGarrett Wollman  */
2302c37256eSGarrett Wollman static int
231b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2322c37256eSGarrett Wollman {
2332c37256eSGarrett Wollman 	int error = 0;
234f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
235623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2362c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2372c37256eSGarrett Wollman 
23852710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
239f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
240f96603b5SMark Johnston 		/*
241f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
242f96603b5SMark Johnston 		 */
243f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2443f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
245f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
246f161d294SMark Johnston 			return (EAFNOSUPPORT);
247f96603b5SMark Johnston 		nam->sa_family = AF_INET;
248f96603b5SMark Johnston 	}
24952710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof(*sinp))
25052710de1SPawel Jakub Dawidek 		return (EINVAL);
251f161d294SMark Johnston 
2522c37256eSGarrett Wollman 	/*
2532c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2542c37256eSGarrett Wollman 	 * to them.
2552c37256eSGarrett Wollman 	 */
256f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
25752710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
25852710de1SPawel Jakub Dawidek 
259623dce13SRobert Watson 	inp = sotoinpcb(so);
260623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2618501a69cSRobert Watson 	INP_WLOCK(inp);
26253af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
263623dce13SRobert Watson 		error = EINVAL;
2642c37256eSGarrett Wollman 		goto out;
265623dce13SRobert Watson 	}
266623dce13SRobert Watson 	tp = intotcpcb(inp);
267fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
26896871af0SGleb Smirnoff 	error = in_pcbbind(inp, sinp, td->td_ucred);
269fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
270623dce13SRobert Watson out:
27100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
2725d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2738501a69cSRobert Watson 	INP_WUNLOCK(inp);
274623dce13SRobert Watson 
275623dce13SRobert Watson 	return (error);
2762c37256eSGarrett Wollman }
277b287c6c7SBjoern A. Zeeb #endif /* INET */
2782c37256eSGarrett Wollman 
279fb59c426SYoshinobu Inoue #ifdef INET6
280fb59c426SYoshinobu Inoue static int
281b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
282fb59c426SYoshinobu Inoue {
283fb59c426SYoshinobu Inoue 	int error = 0;
284f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
285623dce13SRobert Watson 	struct tcpcb *tp = NULL;
2860ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2874a91aa8fSMichael Tuexen 	u_char vflagsav;
288fb59c426SYoshinobu Inoue 
2890ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
290f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
291f161d294SMark Johnston 		return (EAFNOSUPPORT);
2920ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof(*sin6))
29352710de1SPawel Jakub Dawidek 		return (EINVAL);
294f161d294SMark Johnston 
295fb59c426SYoshinobu Inoue 	/*
296fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
297fb59c426SYoshinobu Inoue 	 * to them.
298fb59c426SYoshinobu Inoue 	 */
299f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
30052710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
30152710de1SPawel Jakub Dawidek 
302623dce13SRobert Watson 	inp = sotoinpcb(so);
303623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
3048501a69cSRobert Watson 	INP_WLOCK(inp);
3054a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
30653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
307623dce13SRobert Watson 		error = EINVAL;
308623dce13SRobert Watson 		goto out;
309623dce13SRobert Watson 	}
310623dce13SRobert Watson 	tp = intotcpcb(inp);
311fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
312fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
313fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
314b287c6c7SBjoern A. Zeeb #ifdef INET
31566ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3160ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
317fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3180ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
319fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
320fb59c426SYoshinobu Inoue 
3210ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
322888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
323888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
324888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
325888973f5SMichael Tuexen 				goto out;
326888973f5SMichael Tuexen 			}
327fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
328fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
32996871af0SGleb Smirnoff 			error = in_pcbbind(inp, &sin, td->td_ucred);
330fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
331fb59c426SYoshinobu Inoue 			goto out;
332fb59c426SYoshinobu Inoue 		}
333fb59c426SYoshinobu Inoue 	}
334b287c6c7SBjoern A. Zeeb #endif
33596871af0SGleb Smirnoff 	error = in6_pcbbind(inp, sin6, td->td_ucred);
336fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
337623dce13SRobert Watson out:
3384a91aa8fSMichael Tuexen 	if (error != 0)
3394a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
34000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
3415d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3428501a69cSRobert Watson 	INP_WUNLOCK(inp);
343623dce13SRobert Watson 	return (error);
344fb59c426SYoshinobu Inoue }
345fb59c426SYoshinobu Inoue #endif /* INET6 */
346fb59c426SYoshinobu Inoue 
347b287c6c7SBjoern A. Zeeb #ifdef INET
3482c37256eSGarrett Wollman /*
3492c37256eSGarrett Wollman  * Prepare to accept connections.
3502c37256eSGarrett Wollman  */
3512c37256eSGarrett Wollman static int
352d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3532c37256eSGarrett Wollman {
3542c37256eSGarrett Wollman 	int error = 0;
355f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
356623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3572c37256eSGarrett Wollman 
358623dce13SRobert Watson 	inp = sotoinpcb(so);
359623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3608501a69cSRobert Watson 	INP_WLOCK(inp);
36153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
362623dce13SRobert Watson 		error = EINVAL;
363623dce13SRobert Watson 		goto out;
364623dce13SRobert Watson 	}
365623dce13SRobert Watson 	tp = intotcpcb(inp);
3660daccb9cSRobert Watson 	SOCK_LOCK(so);
3670daccb9cSRobert Watson 	error = solisten_proto_check(so);
368bd4a39ccSMark Johnston 	if (error != 0) {
369bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
370bd4a39ccSMark Johnston 		goto out;
371bd4a39ccSMark Johnston 	}
372bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
373fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
374bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
375fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
376bd4a39ccSMark Johnston 	}
3770daccb9cSRobert Watson 	if (error == 0) {
37857f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
379d374e81eSRobert Watson 		solisten_proto(so, backlog);
38009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
38137cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
38209fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
38309fe6320SNavdeep Parhar #endif
384bd4a39ccSMark Johnston 	} else {
385bd4a39ccSMark Johnston 		solisten_proto_abort(so);
3860daccb9cSRobert Watson 	}
3870daccb9cSRobert Watson 	SOCK_UNLOCK(so);
388623dce13SRobert Watson 
38968bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
390281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
39118a75309SPatrick Kelsey 
392623dce13SRobert Watson out:
39300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
3945d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
3958501a69cSRobert Watson 	INP_WUNLOCK(inp);
396623dce13SRobert Watson 	return (error);
3972c37256eSGarrett Wollman }
398b287c6c7SBjoern A. Zeeb #endif /* INET */
3992c37256eSGarrett Wollman 
400fb59c426SYoshinobu Inoue #ifdef INET6
401fb59c426SYoshinobu Inoue static int
402d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
403fb59c426SYoshinobu Inoue {
404fb59c426SYoshinobu Inoue 	int error = 0;
405f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
406623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4074a91aa8fSMichael Tuexen 	u_char vflagsav;
408fb59c426SYoshinobu Inoue 
409623dce13SRobert Watson 	inp = sotoinpcb(so);
410623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4118501a69cSRobert Watson 	INP_WLOCK(inp);
41253af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
413623dce13SRobert Watson 		error = EINVAL;
414623dce13SRobert Watson 		goto out;
415623dce13SRobert Watson 	}
4164a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
417623dce13SRobert Watson 	tp = intotcpcb(inp);
4180daccb9cSRobert Watson 	SOCK_LOCK(so);
4190daccb9cSRobert Watson 	error = solisten_proto_check(so);
420bd4a39ccSMark Johnston 	if (error != 0) {
421bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
422bd4a39ccSMark Johnston 		goto out;
423bd4a39ccSMark Johnston 	}
424fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
425bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
426fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
42766ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
428fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
429bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
430fb59c426SYoshinobu Inoue 	}
431fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4320daccb9cSRobert Watson 	if (error == 0) {
43357f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
434d374e81eSRobert Watson 		solisten_proto(so, backlog);
43509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
43637cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
43709fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
43809fe6320SNavdeep Parhar #endif
439bd4a39ccSMark Johnston 	} else {
440bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4410daccb9cSRobert Watson 	}
4420daccb9cSRobert Watson 	SOCK_UNLOCK(so);
443623dce13SRobert Watson 
44468bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
445281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
44618a75309SPatrick Kelsey 
4474a91aa8fSMichael Tuexen 	if (error != 0)
4484a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4494a91aa8fSMichael Tuexen 
450623dce13SRobert Watson out:
45100812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4525d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4538501a69cSRobert Watson 	INP_WUNLOCK(inp);
454623dce13SRobert Watson 	return (error);
455fb59c426SYoshinobu Inoue }
456fb59c426SYoshinobu Inoue #endif /* INET6 */
457fb59c426SYoshinobu Inoue 
458b287c6c7SBjoern A. Zeeb #ifdef INET
4592c37256eSGarrett Wollman /*
4602c37256eSGarrett Wollman  * Initiate connection to peer.
4612c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4622c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4632c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4642c37256eSGarrett Wollman  * Send initial segment on connection.
4652c37256eSGarrett Wollman  */
4662c37256eSGarrett Wollman static int
467b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4682c37256eSGarrett Wollman {
469109eb549SGleb Smirnoff 	struct epoch_tracker et;
4702c37256eSGarrett Wollman 	int error = 0;
471f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
472623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4732c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4742c37256eSGarrett Wollman 
47557bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
476f161d294SMark Johnston 	if (nam->sa_family != AF_INET)
477f161d294SMark Johnston 		return (EAFNOSUPPORT);
478e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
479e29ef13fSDon Lewis 		return (EINVAL);
480f161d294SMark Johnston 
48152710de1SPawel Jakub Dawidek 	/*
48252710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
48352710de1SPawel Jakub Dawidek 	 */
484f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
48552710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
486f161d294SMark Johnston 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
487f903a308SMichael Tuexen 		return (EACCES);
488b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
489b89e82ddSJamie Gritton 		return (error);
49075c13541SPoul-Henning Kamp 
491623dce13SRobert Watson 	inp = sotoinpcb(so);
492623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4938501a69cSRobert Watson 	INP_WLOCK(inp);
494eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
495eb96dc33SJulien Charbon 		error = ECONNREFUSED;
496623dce13SRobert Watson 		goto out;
497623dce13SRobert Watson 	}
498bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
499bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
500bd4a39ccSMark Johnston 		goto out;
501bd4a39ccSMark Johnston 	}
502623dce13SRobert Watson 	tp = intotcpcb(inp);
503c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
504a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
505c1604fe4SGleb Smirnoff 		goto out_in_epoch;
50609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
50709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
50837cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
50909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
510c1604fe4SGleb Smirnoff 		goto out_in_epoch;
51109fe6320SNavdeep Parhar #endif
51209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
51340fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5141d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5151d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
516c1604fe4SGleb Smirnoff out_in_epoch:
517109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
518623dce13SRobert Watson out:
51900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
520e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5218501a69cSRobert Watson 	INP_WUNLOCK(inp);
522623dce13SRobert Watson 	return (error);
5232c37256eSGarrett Wollman }
524b287c6c7SBjoern A. Zeeb #endif /* INET */
5252c37256eSGarrett Wollman 
526fb59c426SYoshinobu Inoue #ifdef INET6
527fb59c426SYoshinobu Inoue static int
528b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
529fb59c426SYoshinobu Inoue {
530109eb549SGleb Smirnoff 	struct epoch_tracker et;
531fb59c426SYoshinobu Inoue 	int error = 0;
532f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
533623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5340ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5354a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5364a91aa8fSMichael Tuexen 	u_char vflagsav;
537623dce13SRobert Watson 
5380ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
539f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
540f161d294SMark Johnston 		return (EAFNOSUPPORT);
5410ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
542e29ef13fSDon Lewis 		return (EINVAL);
543f161d294SMark Johnston 
54452710de1SPawel Jakub Dawidek 	/*
54552710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
54652710de1SPawel Jakub Dawidek 	 */
547f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
54852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
549fb59c426SYoshinobu Inoue 
550623dce13SRobert Watson 	inp = sotoinpcb(so);
551623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5528501a69cSRobert Watson 	INP_WLOCK(inp);
5534a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5544a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
555eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
556eb96dc33SJulien Charbon 		error = ECONNREFUSED;
557623dce13SRobert Watson 		goto out;
558623dce13SRobert Watson 	}
559bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
560bd4a39ccSMark Johnston 		error = EINVAL;
561bd4a39ccSMark Johnston 		goto out;
562bd4a39ccSMark Johnston 	}
563623dce13SRobert Watson 	tp = intotcpcb(inp);
564b287c6c7SBjoern A. Zeeb #ifdef INET
565fa046d87SRobert Watson 	/*
566fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
567fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
568fa046d87SRobert Watson 	 * Is this a significant problem?
569fa046d87SRobert Watson 	 */
5700ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
571fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
572fb59c426SYoshinobu Inoue 
573d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
574d46a5312SMaxim Konovalov 			error = EINVAL;
575d46a5312SMaxim Konovalov 			goto out;
576d46a5312SMaxim Konovalov 		}
5775dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
5785dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
5795dba6adaSMichael Tuexen 			goto out;
5805dba6adaSMichael Tuexen 		}
58133841545SHajimu UMEMOTO 
5820ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
583888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
584888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
585888973f5SMichael Tuexen 			goto out;
586888973f5SMichael Tuexen 		}
587f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
588f903a308SMichael Tuexen 			error = EACCES;
5892cf21ae5SRandall Stewart 			goto out;
5902cf21ae5SRandall Stewart 		}
591b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
592b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
593413628a7SBjoern A. Zeeb 			goto out;
5944a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
5954a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
596c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
597a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
598c1604fe4SGleb Smirnoff 			goto out_in_epoch;
59909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
60009fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
601adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
60209fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
603c1604fe4SGleb Smirnoff 			goto out_in_epoch;
60409fe6320SNavdeep Parhar #endif
60540fa3e40SGleb Smirnoff 		error = tcp_output(tp);
606c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6075dba6adaSMichael Tuexen 	} else {
6085dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6095dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6105dba6adaSMichael Tuexen 			goto out;
6115dba6adaSMichael Tuexen 		}
612fb59c426SYoshinobu Inoue 	}
613b287c6c7SBjoern A. Zeeb #endif
6144a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6154a91aa8fSMichael Tuexen 		goto out;
616fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
617fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
618dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6190773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
620a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6210773b44eSGleb Smirnoff 		goto out_in_epoch;
62209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62309fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
624adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62509fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6260773b44eSGleb Smirnoff 		goto out_in_epoch;
62709fe6320SNavdeep Parhar #endif
62809fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
62940fa3e40SGleb Smirnoff 	error = tcp_output(tp);
630c1604fe4SGleb Smirnoff out_in_epoch:
631109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
632623dce13SRobert Watson out:
6331d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6341d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6354a91aa8fSMichael Tuexen 	/*
6364a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6374a91aa8fSMichael Tuexen 	 * the flags we modified.
6384a91aa8fSMichael Tuexen 	 */
6394a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6404a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6414a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6424a91aa8fSMichael Tuexen 	}
6434a91aa8fSMichael Tuexen 
64400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6455d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6468501a69cSRobert Watson 	INP_WUNLOCK(inp);
647623dce13SRobert Watson 	return (error);
648fb59c426SYoshinobu Inoue }
649fb59c426SYoshinobu Inoue #endif /* INET6 */
650fb59c426SYoshinobu Inoue 
6512c37256eSGarrett Wollman /*
6522c37256eSGarrett Wollman  * Initiate disconnect from peer.
6532c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6542c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6552c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6562c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6572c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6582c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6592c37256eSGarrett Wollman  *
6602c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6612c37256eSGarrett Wollman  */
6622c37256eSGarrett Wollman static int
6632c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6642c37256eSGarrett Wollman {
665f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
666623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6676573d758SMatt Macy 	struct epoch_tracker et;
668623dce13SRobert Watson 	int error = 0;
6692c37256eSGarrett Wollman 
67097a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
671623dce13SRobert Watson 	inp = sotoinpcb(so);
672623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6738501a69cSRobert Watson 	INP_WLOCK(inp);
674489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
67521367f63SSam Leffler 		error = ECONNRESET;
676623dce13SRobert Watson 		goto out;
677623dce13SRobert Watson 	}
678623dce13SRobert Watson 	tp = intotcpcb(inp);
679cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
680cda6bdbaSJohn Baldwin 		goto out;
681623dce13SRobert Watson 	tcp_disconnect(tp);
682623dce13SRobert Watson out:
68300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_DISCONNECT, error);
6845d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
6858501a69cSRobert Watson 	INP_WUNLOCK(inp);
68697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
687623dce13SRobert Watson 	return (error);
6882c37256eSGarrett Wollman }
6892c37256eSGarrett Wollman 
690b287c6c7SBjoern A. Zeeb #ifdef INET
6912c37256eSGarrett Wollman /*
6928296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
6938296cddfSRobert Watson  * just return the address of the peer, storing through addr.
6942c37256eSGarrett Wollman  */
6952c37256eSGarrett Wollman static int
69657bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
6972c37256eSGarrett Wollman {
6982c37256eSGarrett Wollman 	int error = 0;
699f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
7001db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
70126ef6ac4SDon Lewis 	struct in_addr addr;
70226ef6ac4SDon Lewis 	in_port_t port = 0;
7032c37256eSGarrett Wollman 
7043d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
7053d2d3ef4SRobert Watson 		return (ECONNABORTED);
706f76fcf6dSJeffrey Hsu 
707f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
708623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7098501a69cSRobert Watson 	INP_WLOCK(inp);
71053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
7113d2d3ef4SRobert Watson 		error = ECONNABORTED;
712623dce13SRobert Watson 		goto out;
713623dce13SRobert Watson 	}
7141db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
715f76fcf6dSJeffrey Hsu 
716f76fcf6dSJeffrey Hsu 	/*
71754d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
71826ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
71926ef6ac4SDon Lewis 	 * release the lock.
720f76fcf6dSJeffrey Hsu 	 */
72126ef6ac4SDon Lewis 	port = inp->inp_fport;
72226ef6ac4SDon Lewis 	addr = inp->inp_faddr;
723f76fcf6dSJeffrey Hsu 
724623dce13SRobert Watson out:
72500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7265d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7278501a69cSRobert Watson 	INP_WUNLOCK(inp);
72826ef6ac4SDon Lewis 	if (error == 0)
72926ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
73026ef6ac4SDon Lewis 	return error;
7312c37256eSGarrett Wollman }
732b287c6c7SBjoern A. Zeeb #endif /* INET */
7332c37256eSGarrett Wollman 
734fb59c426SYoshinobu Inoue #ifdef INET6
735fb59c426SYoshinobu Inoue static int
736fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
737fb59c426SYoshinobu Inoue {
738f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
739fb59c426SYoshinobu Inoue 	int error = 0;
7401db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
74126ef6ac4SDon Lewis 	struct in_addr addr;
74226ef6ac4SDon Lewis 	struct in6_addr addr6;
7436573d758SMatt Macy 	struct epoch_tracker et;
74426ef6ac4SDon Lewis 	in_port_t port = 0;
74526ef6ac4SDon Lewis 	int v4 = 0;
746fb59c426SYoshinobu Inoue 
747b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
748b4470c16SRobert Watson 		return (ECONNABORTED);
749f76fcf6dSJeffrey Hsu 
750f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
751623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
75297a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
7538501a69cSRobert Watson 	INP_WLOCK(inp);
75453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
75521367f63SSam Leffler 		error = ECONNABORTED;
756623dce13SRobert Watson 		goto out;
757623dce13SRobert Watson 	}
7581db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
759623dce13SRobert Watson 
76026ef6ac4SDon Lewis 	/*
76126ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
76226ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
76326ef6ac4SDon Lewis 	 * release the lock.
76426ef6ac4SDon Lewis 	 */
76526ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
76626ef6ac4SDon Lewis 		v4 = 1;
76726ef6ac4SDon Lewis 		port = inp->inp_fport;
76826ef6ac4SDon Lewis 		addr = inp->inp_faddr;
76926ef6ac4SDon Lewis 	} else {
77026ef6ac4SDon Lewis 		port = inp->inp_fport;
77126ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
77226ef6ac4SDon Lewis 	}
77326ef6ac4SDon Lewis 
774623dce13SRobert Watson out:
77500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7765d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7778501a69cSRobert Watson 	INP_WUNLOCK(inp);
77897a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
77926ef6ac4SDon Lewis 	if (error == 0) {
78026ef6ac4SDon Lewis 		if (v4)
78126ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
78226ef6ac4SDon Lewis 		else
78326ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
78426ef6ac4SDon Lewis 	}
78526ef6ac4SDon Lewis 	return error;
786fb59c426SYoshinobu Inoue }
787fb59c426SYoshinobu Inoue #endif /* INET6 */
788f76fcf6dSJeffrey Hsu 
789f76fcf6dSJeffrey Hsu /*
7902c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7912c37256eSGarrett Wollman  */
7922c37256eSGarrett Wollman static int
7932c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
7942c37256eSGarrett Wollman {
7952c37256eSGarrett Wollman 	int error = 0;
796f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
797623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7986573d758SMatt Macy 	struct epoch_tracker et;
7992c37256eSGarrett Wollman 
800623dce13SRobert Watson 	inp = sotoinpcb(so);
801623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8028501a69cSRobert Watson 	INP_WLOCK(inp);
80353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
8040af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
8050af4ce45SGleb Smirnoff 		return (ECONNRESET);
806623dce13SRobert Watson 	}
8070af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
8080af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8092c37256eSGarrett Wollman 	socantsendmore(so);
810623dce13SRobert Watson 	tcp_usrclosed(tp);
811ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
812f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
81300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8145d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
815f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
81697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
817623dce13SRobert Watson 
818623dce13SRobert Watson 	return (error);
8192c37256eSGarrett Wollman }
8202c37256eSGarrett Wollman 
8212c37256eSGarrett Wollman /*
8222c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8232c37256eSGarrett Wollman  */
8242c37256eSGarrett Wollman static int
8252c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8262c37256eSGarrett Wollman {
827109eb549SGleb Smirnoff 	struct epoch_tracker et;
828f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
829623dce13SRobert Watson 	struct tcpcb *tp = NULL;
830f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8312c37256eSGarrett Wollman 
832623dce13SRobert Watson 	inp = sotoinpcb(so);
833623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8348501a69cSRobert Watson 	INP_WLOCK(inp);
83553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
83637a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
83737a7f557SGleb Smirnoff 		return (ECONNRESET);
838623dce13SRobert Watson 	}
83937a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
84037a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
841281a0fd4SPatrick Kelsey 	/*
842281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
843281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
844281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
845281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
846281a0fd4SPatrick Kelsey 	 * expires.
847281a0fd4SPatrick Kelsey 	 */
84868bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
849281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
850281a0fd4SPatrick Kelsey 		goto out;
85109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
85209fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
85309fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
854460cf046SNavdeep Parhar 	else
85509fe6320SNavdeep Parhar #endif
856f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
857623dce13SRobert Watson out:
85800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
8595d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
860f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
861f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
862623dce13SRobert Watson 	return (error);
8632c37256eSGarrett Wollman }
8642c37256eSGarrett Wollman 
8652c37256eSGarrett Wollman /*
8662c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8679c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8689c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8699c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8709c9906e9SPeter Wemm  * generally are caller-frees.
8712c37256eSGarrett Wollman  */
8722c37256eSGarrett Wollman static int
87357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
874b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8752c37256eSGarrett Wollman {
87697a95ee1SGleb Smirnoff 	struct epoch_tracker et;
8772c37256eSGarrett Wollman 	int error = 0;
878f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
879623dce13SRobert Watson 	struct tcpcb *tp = NULL;
880888973f5SMichael Tuexen #ifdef INET
88151e08d53SMichael Tuexen #ifdef INET6
88251e08d53SMichael Tuexen 	struct sockaddr_in sin;
88351e08d53SMichael Tuexen #endif
88451e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
885888973f5SMichael Tuexen #endif
886fb59c426SYoshinobu Inoue #ifdef INET6
887a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
888fb59c426SYoshinobu Inoue 	int isipv6;
889fb59c426SYoshinobu Inoue #endif
8904a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
8914a91aa8fSMichael Tuexen 	u_char vflagsav;
8924a91aa8fSMichael Tuexen 	bool restoreflags;
8932c37256eSGarrett Wollman 
894d8acd268SMark Johnston 	if (control != NULL) {
895d8acd268SMark Johnston 		/* TCP doesn't do control messages (rights, creds, etc) */
896d8acd268SMark Johnston 		if (control->m_len) {
897d8acd268SMark Johnston 			m_freem(control);
8984287aa56SGleb Smirnoff 			return (EINVAL);
899d8acd268SMark Johnston 		}
900d8acd268SMark Johnston 		m_freem(control);	/* empty control, just free it */
901d8acd268SMark Johnston 	}
9024287aa56SGleb Smirnoff 
9034287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9044287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9054287aa56SGleb Smirnoff 	INP_WLOCK(inp);
90653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9074287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9084287aa56SGleb Smirnoff 			m_freem(m);
9094287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9104287aa56SGleb Smirnoff 		return (ECONNRESET);
9114287aa56SGleb Smirnoff 	}
9124287aa56SGleb Smirnoff 
9134287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9144287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9154287aa56SGleb Smirnoff 	restoreflags = false;
9164287aa56SGleb Smirnoff 	tp = intotcpcb(inp);
9174287aa56SGleb Smirnoff 
9184287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
9197d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9207d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
921d3b6c96bSRandall Stewart 		goto out;
9227d2608a5SMark Johnston 
923888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
924bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
925bd4a39ccSMark Johnston 			error = EINVAL;
926bd4a39ccSMark Johnston 			goto out;
927bd4a39ccSMark Johnston 		}
928888973f5SMichael Tuexen 		switch (nam->sa_family) {
929888973f5SMichael Tuexen #ifdef INET
930888973f5SMichael Tuexen 		case AF_INET:
931888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
932888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
933888973f5SMichael Tuexen 				error = EINVAL;
934888973f5SMichael Tuexen 				goto out;
935888973f5SMichael Tuexen 			}
936888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
937888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
938888973f5SMichael Tuexen 				goto out;
939888973f5SMichael Tuexen 			}
940888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
941888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
942888973f5SMichael Tuexen 				goto out;
943888973f5SMichael Tuexen 			}
944f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
945f903a308SMichael Tuexen 				error = EACCES;
9462cf21ae5SRandall Stewart 				goto out;
9472cf21ae5SRandall Stewart 			}
948888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9497d2608a5SMark Johnston 			    &sinp->sin_addr)))
950888973f5SMichael Tuexen 				goto out;
951888973f5SMichael Tuexen #ifdef INET6
952888973f5SMichael Tuexen 			isipv6 = 0;
953888973f5SMichael Tuexen #endif
954888973f5SMichael Tuexen 			break;
955888973f5SMichael Tuexen #endif /* INET */
956888973f5SMichael Tuexen #ifdef INET6
957888973f5SMichael Tuexen 		case AF_INET6:
9580ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
9590ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
960888973f5SMichael Tuexen 				error = EINVAL;
961888973f5SMichael Tuexen 				goto out;
962888973f5SMichael Tuexen 			}
963e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
964e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
965e240ce42SMichael Tuexen 				goto out;
966e240ce42SMichael Tuexen 			}
9670ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
968888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
969888973f5SMichael Tuexen 				goto out;
970888973f5SMichael Tuexen 			}
9710ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
972888973f5SMichael Tuexen #ifdef INET
973888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
974888973f5SMichael Tuexen 					error = EINVAL;
975888973f5SMichael Tuexen 					goto out;
976888973f5SMichael Tuexen 				}
977888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
978888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
979888973f5SMichael Tuexen 					goto out;
980888973f5SMichael Tuexen 				}
9814a91aa8fSMichael Tuexen 				restoreflags = true;
982888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
983888973f5SMichael Tuexen 				sinp = &sin;
9840ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
985888973f5SMichael Tuexen 				if (IN_MULTICAST(
986888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
987888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
988888973f5SMichael Tuexen 					goto out;
989888973f5SMichael Tuexen 				}
990888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
9917d2608a5SMark Johnston 				    &sinp->sin_addr)))
992888973f5SMichael Tuexen 					goto out;
993888973f5SMichael Tuexen 				isipv6 = 0;
994888973f5SMichael Tuexen #else /* !INET */
995888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
996888973f5SMichael Tuexen 				goto out;
997888973f5SMichael Tuexen #endif /* INET */
998888973f5SMichael Tuexen 			} else {
999888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1000888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1001888973f5SMichael Tuexen 					goto out;
1002888973f5SMichael Tuexen 				}
10034a91aa8fSMichael Tuexen 				restoreflags = true;
1004888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1005888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1006888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10077d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1008888973f5SMichael Tuexen 					goto out;
1009888973f5SMichael Tuexen 				isipv6 = 1;
1010888973f5SMichael Tuexen 			}
1011888973f5SMichael Tuexen 			break;
1012888973f5SMichael Tuexen #endif /* INET6 */
1013888973f5SMichael Tuexen 		default:
1014888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1015888973f5SMichael Tuexen 			goto out;
1016888973f5SMichael Tuexen 		}
1017888973f5SMichael Tuexen 	}
10182c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
101908af8aacSRandall Stewart 		if (tp->t_acktime == 0)
102008af8aacSRandall Stewart 			tp->t_acktime = ticks;
1021651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10227d2608a5SMark Johnston 		m = NULL;
10232c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1024bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1025bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1026bd4a39ccSMark Johnston 
10272c37256eSGarrett Wollman 			/*
10282c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10292c37256eSGarrett Wollman 			 * initialize window to default value, and
10300c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10312c37256eSGarrett Wollman 			 */
1032fb59c426SYoshinobu Inoue #ifdef INET6
1033fb59c426SYoshinobu Inoue 			if (isipv6)
1034a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1035fb59c426SYoshinobu Inoue #endif /* INET6 */
1036b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1037b287c6c7SBjoern A. Zeeb 			else
1038b287c6c7SBjoern A. Zeeb #endif
1039b287c6c7SBjoern A. Zeeb #ifdef INET
1040a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1041b287c6c7SBjoern A. Zeeb #endif
10424a91aa8fSMichael Tuexen 			/*
10434a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10444a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10454a91aa8fSMichael Tuexen 			 * operations fail.
10464a91aa8fSMichael Tuexen 			 */
10474a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10484a91aa8fSMichael Tuexen 				restoreflags = false;
10494a91aa8fSMichael Tuexen 
10507d2608a5SMark Johnston 			if (error) {
10517d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10527d2608a5SMark Johnston 				sbflush(&so->so_snd);
10532c37256eSGarrett Wollman 				goto out;
10547d2608a5SMark Johnston 			}
1055c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1056c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
105718a75309SPatrick Kelsey 			else {
10582c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10592c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10602c37256eSGarrett Wollman 			}
1061c560df6fSPatrick Kelsey 		}
10622c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
10632c37256eSGarrett Wollman 			/*
10642c37256eSGarrett Wollman 			 * Close the send side of the connection after
10652c37256eSGarrett Wollman 			 * the data is sent.
10662c37256eSGarrett Wollman 			 */
10672c37256eSGarrett Wollman 			socantsendmore(so);
1068623dce13SRobert Watson 			tcp_usrclosed(tp);
10692c37256eSGarrett Wollman 		}
1070e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1071e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1072e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1073e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1074e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1075e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1076e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1077e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1078e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1079e854dd38SRandall Stewart 		}
10802cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
10812cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1082b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1083b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1084f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1085b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1086b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1087b0acefa8SBill Fenner 		}
10882c37256eSGarrett Wollman 	} else {
1089623dce13SRobert Watson 		/*
1090623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1091623dce13SRobert Watson 		 */
1092d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
10932c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1094d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
10952c37256eSGarrett Wollman 			error = ENOBUFS;
10962c37256eSGarrett Wollman 			goto out;
10972c37256eSGarrett Wollman 		}
10982c37256eSGarrett Wollman 		/*
10992c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11002c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11012c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11022c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11032c37256eSGarrett Wollman 		 * of data past the urgent section.
11042c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11052c37256eSGarrett Wollman 		 */
110608af8aacSRandall Stewart 		if (tp->t_acktime == 0)
110708af8aacSRandall Stewart 			tp->t_acktime = ticks;
1108651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1109d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
11107d2608a5SMark Johnston 		m = NULL;
1111ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1112ef53690bSGarrett Wollman 			/*
1113ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1114ef53690bSGarrett Wollman 			 * initialize window to default value, and
11150c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1116ef53690bSGarrett Wollman 			 */
111718a75309SPatrick Kelsey 
1118c560df6fSPatrick Kelsey 			/*
1119c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1120c560df6fSPatrick Kelsey 			 */
1121c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1122c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1123fb59c426SYoshinobu Inoue #ifdef INET6
1124fb59c426SYoshinobu Inoue 			if (isipv6)
1125a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1126fb59c426SYoshinobu Inoue #endif /* INET6 */
1127b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1128b287c6c7SBjoern A. Zeeb 			else
1129b287c6c7SBjoern A. Zeeb #endif
1130b287c6c7SBjoern A. Zeeb #ifdef INET
1131a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1132b287c6c7SBjoern A. Zeeb #endif
11334a91aa8fSMichael Tuexen 			/*
11344a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11354a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11364a91aa8fSMichael Tuexen 			 * operations fail.
11374a91aa8fSMichael Tuexen 			 */
11384a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11394a91aa8fSMichael Tuexen 				restoreflags = false;
11404a91aa8fSMichael Tuexen 
11417d2608a5SMark Johnston 			if (error != 0) {
11427d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11437d2608a5SMark Johnston 				sbflush(&so->so_snd);
1144ef53690bSGarrett Wollman 				goto out;
11457d2608a5SMark Johnston 			}
1146ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1147ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1148623dce13SRobert Watson 		}
1149300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11507d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11512cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1152f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11532cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11542c37256eSGarrett Wollman 		}
11552cbcd3c1SGleb Smirnoff 	}
11562529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11572529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11582529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11592529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11602529f56eSJonathan T. Looney 	    0, NULL, false);
11617d2608a5SMark Johnston 
1162d1401c90SRobert Watson out:
11634a91aa8fSMichael Tuexen 	/*
11647d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
11657d2608a5SMark Johnston 	 * responsible for freeing memory.
11667d2608a5SMark Johnston 	 */
11677d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
11687d2608a5SMark Johnston 		m_freem(m);
11697d2608a5SMark Johnston 
11707d2608a5SMark Johnston 	/*
11714a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
11724a91aa8fSMichael Tuexen 	 * restore the original flags.
11734a91aa8fSMichael Tuexen 	 */
11744a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
11754a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
11764a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
11774a91aa8fSMichael Tuexen 	}
117800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
117900812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
11805d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
11815d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1182f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
118397a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
118473fddedaSPeter Grehan 	return (error);
11852c37256eSGarrett Wollman }
11862c37256eSGarrett Wollman 
11872cbcd3c1SGleb Smirnoff static int
11882cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
11892cbcd3c1SGleb Smirnoff {
1190109eb549SGleb Smirnoff 	struct epoch_tracker et;
11912cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
11922cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
11932cbcd3c1SGleb Smirnoff 	int error;
11942cbcd3c1SGleb Smirnoff 
11952cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
11962cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
119753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
11982cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
119982334850SJohn Baldwin 		mb_free_notready(m, count);
12002cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12012cbcd3c1SGleb Smirnoff 	}
12022cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12032cbcd3c1SGleb Smirnoff 
12042cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12052cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12062cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1207f64dc2abSGleb Smirnoff 	if (error) {
12082cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1209f64dc2abSGleb Smirnoff 		return (error);
1210f64dc2abSGleb Smirnoff 	}
1211f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1212f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1213f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12142cbcd3c1SGleb Smirnoff 
12152cbcd3c1SGleb Smirnoff 	return (error);
12162cbcd3c1SGleb Smirnoff }
12172cbcd3c1SGleb Smirnoff 
12182c37256eSGarrett Wollman /*
1219a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12202c37256eSGarrett Wollman  */
1221ac45e92fSRobert Watson static void
12222c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12232c37256eSGarrett Wollman {
1224f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1225a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12266573d758SMatt Macy 	struct epoch_tracker et;
1227c78cbc7bSRobert Watson 
1228ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1229c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1230c78cbc7bSRobert Watson 
123197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12328501a69cSRobert Watson 	INP_WLOCK(inp);
1233c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1234c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1235c78cbc7bSRobert Watson 
1236c78cbc7bSRobert Watson 	/*
1237a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1238c78cbc7bSRobert Watson 	 */
123953af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1240c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12418fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12428fa799bdSJonathan T. Looney 		if (tp == NULL)
12438fa799bdSJonathan T. Looney 			goto dropped;
124400812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12455d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1246c78cbc7bSRobert Watson 	}
1247ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1248d8596171SGleb Smirnoff 		soref(so);
1249ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1250a152f8a3SRobert Watson 	}
12518501a69cSRobert Watson 	INP_WUNLOCK(inp);
12528fa799bdSJonathan T. Looney dropped:
125397a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1254a152f8a3SRobert Watson }
1255a152f8a3SRobert Watson 
1256a152f8a3SRobert Watson /*
1257a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1258a152f8a3SRobert Watson  */
1259a152f8a3SRobert Watson static void
1260a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1261a152f8a3SRobert Watson {
1262a152f8a3SRobert Watson 	struct inpcb *inp;
1263a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12646573d758SMatt Macy 	struct epoch_tracker et;
1265a152f8a3SRobert Watson 
1266a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1267a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1268a152f8a3SRobert Watson 
126997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12708501a69cSRobert Watson 	INP_WLOCK(inp);
1271a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1272a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1273a152f8a3SRobert Watson 
1274a152f8a3SRobert Watson 	/*
1275cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1276a152f8a3SRobert Watson 	 * a disconnect.
1277a152f8a3SRobert Watson 	 */
127853af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1279a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1280cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
128174703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1282a152f8a3SRobert Watson 			tcp_disconnect(tp);
128300812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
12845d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1285a152f8a3SRobert Watson 		}
1286cda6bdbaSJohn Baldwin 	}
1287ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1288d8596171SGleb Smirnoff 		soref(so);
1289ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1290a152f8a3SRobert Watson 	}
12918501a69cSRobert Watson 	INP_WUNLOCK(inp);
129297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
12932c37256eSGarrett Wollman }
12942c37256eSGarrett Wollman 
1295d3b6c96bSRandall Stewart static int
1296d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1297d3b6c96bSRandall Stewart {
1298d3b6c96bSRandall Stewart 	/*
1299d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1300d3b6c96bSRandall Stewart 	 * specified then it does not always support
1301d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1302d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1303d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1304d3b6c96bSRandall Stewart 	 */
1305d3b6c96bSRandall Stewart 	int ret = 0;
1306d3b6c96bSRandall Stewart 
1307d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1308d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1309d3b6c96bSRandall Stewart 	}
1310d3b6c96bSRandall Stewart 	return (ret);
1311d3b6c96bSRandall Stewart }
1312d3b6c96bSRandall Stewart 
13132c37256eSGarrett Wollman /*
13142c37256eSGarrett Wollman  * Receive out-of-band data.
13152c37256eSGarrett Wollman  */
13162c37256eSGarrett Wollman static int
13172c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13182c37256eSGarrett Wollman {
13192c37256eSGarrett Wollman 	int error = 0;
1320f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1321623dce13SRobert Watson 	struct tcpcb *tp = NULL;
13222c37256eSGarrett Wollman 
1323623dce13SRobert Watson 	inp = sotoinpcb(so);
1324623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13258501a69cSRobert Watson 	INP_WLOCK(inp);
132653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
132721367f63SSam Leffler 		error = ECONNRESET;
1328623dce13SRobert Watson 		goto out;
1329623dce13SRobert Watson 	}
1330623dce13SRobert Watson 	tp = intotcpcb(inp);
1331d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1332d3b6c96bSRandall Stewart 	if (error) {
1333d3b6c96bSRandall Stewart 		goto out;
1334d3b6c96bSRandall Stewart 	}
13352c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1336c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13374cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13384cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13392c37256eSGarrett Wollman 		error = EINVAL;
13402c37256eSGarrett Wollman 		goto out;
13412c37256eSGarrett Wollman 	}
13422c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13432c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13442c37256eSGarrett Wollman 		goto out;
13452c37256eSGarrett Wollman 	}
13462c37256eSGarrett Wollman 	m->m_len = 1;
13472c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13482c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13492c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1350623dce13SRobert Watson 
1351623dce13SRobert Watson out:
135200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
13535d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13548501a69cSRobert Watson 	INP_WUNLOCK(inp);
1355623dce13SRobert Watson 	return (error);
13562c37256eSGarrett Wollman }
13572c37256eSGarrett Wollman 
1358b287c6c7SBjoern A. Zeeb #ifdef INET
1359e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1360e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1361e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1362e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1363e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1364e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1365e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1366e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1367e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1368e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1369e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1370e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1371e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1372e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1373e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1374e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1375e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1376e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1377e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1378e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1379e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1380e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1381e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1382e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
13832c37256eSGarrett Wollman };
1384b287c6c7SBjoern A. Zeeb #endif /* INET */
1385df8bae1dSRodney W. Grimes 
1386fb59c426SYoshinobu Inoue #ifdef INET6
1387e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1388e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1389e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1390e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1391e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1392e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1393e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1394e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1395e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1396e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1397e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1398e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1399e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1400e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1401e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1402e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1403e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1404e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1405e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1406e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1407e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1408e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1409e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1410e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1411fb59c426SYoshinobu Inoue };
1412fb59c426SYoshinobu Inoue #endif /* INET6 */
1413fb59c426SYoshinobu Inoue 
1414b287c6c7SBjoern A. Zeeb #ifdef INET
1415a0292f23SGarrett Wollman /*
1416a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1417dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1418dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1419dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1420a0292f23SGarrett Wollman  */
14210312fbe9SPoul-Henning Kamp static int
1422a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1423a0292f23SGarrett Wollman {
14249e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14259eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1426c3229e05SDavid Greenman 	int error;
1427a0292f23SGarrett Wollman 
1428c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14298501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
143076f1499fSGleb Smirnoff 
1431636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1432636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1433636b19eaSMark Johnston 		return (EISCONN);
1434636b19eaSMark Johnston 
1435fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1436dfc4d218SGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred, true);
14379e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1438dfc4d218SGleb Smirnoff 	if (error != 0)
14399e46ff4dSGleb Smirnoff 		return (error);
1440a0292f23SGarrett Wollman 
1441087b55eaSAndre Oppermann 	/*
1442087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1443087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1444087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1445087b55eaSAndre Oppermann 	 */
1446a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14479b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1448a0292f23SGarrett Wollman 		tp->request_r_scale++;
1449a0292f23SGarrett Wollman 
1450a0292f23SGarrett Wollman 	soisconnecting(so);
145178b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
145257f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14538e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14548e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14558e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1456a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1457a45d2726SAndras Olah 
14589e46ff4dSGleb Smirnoff 	return (0);
1459a0292f23SGarrett Wollman }
1460b287c6c7SBjoern A. Zeeb #endif /* INET */
1461a0292f23SGarrett Wollman 
1462fb59c426SYoshinobu Inoue #ifdef INET6
1463fb59c426SYoshinobu Inoue static int
1464a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1465fb59c426SYoshinobu Inoue {
14669eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1467636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1468fb59c426SYoshinobu Inoue 	int error;
1469fb59c426SYoshinobu Inoue 
1470775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
14718501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1472623dce13SRobert Watson 
1473636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1474636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1475636b19eaSMark Johnston 		return (EISCONN);
1476636b19eaSMark Johnston 
147776f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1478a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1479fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
148076f1499fSGleb Smirnoff 	if (error != 0)
148176f1499fSGleb Smirnoff 		return (error);
1482fb59c426SYoshinobu Inoue 
1483fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1484fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1485970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1486fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1487fb59c426SYoshinobu Inoue 
1488636b19eaSMark Johnston 	soisconnecting(so);
148978b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
149057f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14918e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14928e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14938e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1494fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1495fb59c426SYoshinobu Inoue 
149676f1499fSGleb Smirnoff 	return (0);
1497fb59c426SYoshinobu Inoue }
1498fb59c426SYoshinobu Inoue #endif /* INET6 */
1499fb59c426SYoshinobu Inoue 
1500cfe8b629SGarrett Wollman /*
1501b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1502b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1503b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1504b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1505b8af5dfaSRobert Watson  * from Linux.
1506b8af5dfaSRobert Watson  */
1507b8af5dfaSRobert Watson static void
1508ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1509b8af5dfaSRobert Watson {
1510b8af5dfaSRobert Watson 
15119eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1512b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1513b8af5dfaSRobert Watson 
1514b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1515b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1516b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15173529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1518b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1519b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1520b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1521b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1522b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1523b8af5dfaSRobert Watson 	}
15243f169c54SRichard Scheffenegger 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
15255a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
15261baaf834SBruce M Simpson 
152743d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15283ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15291baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15301baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15311baaf834SBruce M Simpson 
1532b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1533b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1534b8af5dfaSRobert Watson 
1535b8af5dfaSRobert Watson 	/*
1536b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1537b8af5dfaSRobert Watson 	 */
1538c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1539535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1540b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15411c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1542535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
154343d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
154443d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1545f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1546f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1547f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1548a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1549a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1550a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1551a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1552a6456410SNavdeep Parhar 	}
1553a6456410SNavdeep Parhar #endif
155422c81cc5SRichard Scheffenegger 	/*
155522c81cc5SRichard Scheffenegger 	 * AccECN related counters.
155622c81cc5SRichard Scheffenegger 	 */
155722c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
155822c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
155922c81cc5SRichard Scheffenegger 		/*
156022c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
156122c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
156222c81cc5SRichard Scheffenegger 		 */
156322c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
156422c81cc5SRichard Scheffenegger 	else
156522c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
156622c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1567b8af5dfaSRobert Watson }
1568b8af5dfaSRobert Watson 
1569b8af5dfaSRobert Watson /*
15701e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
15711e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
15721e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
15731e8f5ffaSRobert Watson  * option.
1574cfe8b629SGarrett Wollman  */
1575bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
15768501a69cSRobert Watson 	INP_WLOCK(inp);							\
157753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
15788501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1579bac5bedfSConrad Meyer 		cleanup;						\
15801e8f5ffaSRobert Watson 		return (ECONNRESET);					\
15811e8f5ffaSRobert Watson 	}								\
15821e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
15831e8f5ffaSRobert Watson } while(0)
1584bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
15851e8f5ffaSRobert Watson 
1586fd7daa72SMichael Tuexen int
1587fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1588df8bae1dSRodney W. Grimes {
1589fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1590fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1591fc4d53ccSGleb Smirnoff 	int error = 0;
1592df8bae1dSRodney W. Grimes 
1593fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
15943b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
159553af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1596fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1597fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1598fc4d53ccSGleb Smirnoff 
1599cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16003b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1601fb59c426SYoshinobu Inoue #ifdef INET6
1602de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1603fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1604de156263SGleb Smirnoff #endif
1605de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1606de156263SGleb Smirnoff 		else
1607de156263SGleb Smirnoff #endif
1608de156263SGleb Smirnoff #ifdef INET
1609fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1610de156263SGleb Smirnoff #endif
16115dff1c38SMichael Tuexen 		/*
1612de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1613de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1614de156263SGleb Smirnoff 		 * IP level returned.
16155dff1c38SMichael Tuexen 		 */
1616de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1617de156263SGleb Smirnoff #ifdef INET6
1618de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1619de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1620de156263SGleb Smirnoff 				return (error);
1621de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1622de156263SGleb Smirnoff 			case IPV6_TCLASS:
1623de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1624de156263SGleb Smirnoff 				break;
1625de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1626f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1627f581a26eSGleb Smirnoff 				break;
1628de156263SGleb Smirnoff 			default:
1629de156263SGleb Smirnoff 				return (error);
16305dff1c38SMichael Tuexen 			}
1631de156263SGleb Smirnoff 			break;
1632b287c6c7SBjoern A. Zeeb #endif
1633b287c6c7SBjoern A. Zeeb #ifdef INET
1634de156263SGleb Smirnoff 		case IPPROTO_IP:
1635de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1636de156263SGleb Smirnoff 			case IP_TOS:
16373b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
16383b0ee680SRichard Scheffenegger 				break;
1639de156263SGleb Smirnoff 			case IP_TTL:
1640de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1641de156263SGleb Smirnoff 				break;
1642de156263SGleb Smirnoff 			default:
1643df8bae1dSRodney W. Grimes 				return (error);
1644de156263SGleb Smirnoff 			}
1645de156263SGleb Smirnoff 			break;
1646de156263SGleb Smirnoff #endif
1647de156263SGleb Smirnoff 		default:
1648de156263SGleb Smirnoff 			return (error);
1649de156263SGleb Smirnoff 		}
16503b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
165153af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
16523b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
16533b3c08c1SMichael Tuexen 			return (ECONNRESET);
16543b3c08c1SMichael Tuexen 		}
1655fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1656fc4d53ccSGleb Smirnoff 		/*
1657fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1658fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1659fc4d53ccSGleb Smirnoff 		 */
1660fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1661fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
1662*73ee5756SRandall Stewart 		void *ptr = NULL;
1663fc4d53ccSGleb Smirnoff 
16643b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1665fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1666fc4d53ccSGleb Smirnoff 		if (error)
1667fc4d53ccSGleb Smirnoff 			return (error);
1668fc4d53ccSGleb Smirnoff 
166968cea2b1SJohn Baldwin 		INP_WLOCK(inp);
167055bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1671fc4d53ccSGleb Smirnoff 
167255bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
167355bceb1eSRandall Stewart 		if (blk == NULL) {
167455bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
167555bceb1eSRandall Stewart 			return (ENOENT);
167655bceb1eSRandall Stewart 		}
1677587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1678587d67c0SRandall Stewart 			/* You already have this */
1679587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1680587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1681587d67c0SRandall Stewart 			return (0);
1682587d67c0SRandall Stewart 		}
1683587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1684587d67c0SRandall Stewart 			/*
1685587d67c0SRandall Stewart 			 * The user has advanced the state
1686587d67c0SRandall Stewart 			 * past the initial point, we may not
1687587d67c0SRandall Stewart 			 * be able to switch.
1688587d67c0SRandall Stewart 			 */
1689587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1690587d67c0SRandall Stewart 				/*
1691587d67c0SRandall Stewart 				 * Does the stack provide a
1692587d67c0SRandall Stewart 				 * query mechanism, if so it may
1693587d67c0SRandall Stewart 				 * still be possible?
1694587d67c0SRandall Stewart 				 */
1695587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1696c6c0be27SMichael Tuexen 			} else
1697c6c0be27SMichael Tuexen 				error = EINVAL;
1698587d67c0SRandall Stewart 			if (error) {
1699587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1700587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1701587d67c0SRandall Stewart 				return(error);
1702587d67c0SRandall Stewart 			}
1703587d67c0SRandall Stewart 		}
170455bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
170555bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
170655bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
170755bceb1eSRandall Stewart 			return (ENOENT);
170855bceb1eSRandall Stewart 		}
170955bceb1eSRandall Stewart 		/*
1710*73ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
1711*73ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
171255bceb1eSRandall Stewart 		 */
1713*73ee5756SRandall Stewart 		tp->t_peakrate_thr = 0;
1714*73ee5756SRandall Stewart #ifdef TCPHPTS
1715*73ee5756SRandall Stewart 		/* Assure that we are not on any hpts */
1716*73ee5756SRandall Stewart 		tcp_hpts_remove(tptoinpcb(tp));
1717*73ee5756SRandall Stewart #endif
1718*73ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
1719*73ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
1720*73ee5756SRandall Stewart 			if (error) {
1721*73ee5756SRandall Stewart 				/*
1722*73ee5756SRandall Stewart 				 * Release the ref count the lookup
1723*73ee5756SRandall Stewart 				 * acquired.
1724*73ee5756SRandall Stewart 				 */
1725*73ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1726*73ee5756SRandall Stewart 				/*
1727*73ee5756SRandall Stewart 				 * Now there is a chance that the
1728*73ee5756SRandall Stewart 				 * init() function mucked with some
1729*73ee5756SRandall Stewart 				 * things before it failed, such as
1730*73ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
1731*73ee5756SRandall Stewart 				 * It should not of, but lets give the old
1732*73ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
1733*73ee5756SRandall Stewart 				 */
1734*73ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
1735*73ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
1736*73ee5756SRandall Stewart 				}
1737*73ee5756SRandall Stewart 			 	goto err_out;
1738*73ee5756SRandall Stewart 			}
1739*73ee5756SRandall Stewart 		}
1740587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1741086a3556SAndrew Gallatin 			struct epoch_tracker et;
1742587d67c0SRandall Stewart 			/*
1743587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1744587d67c0SRandall Stewart 			 * the tcb is not going away.
1745587d67c0SRandall Stewart 			 */
1746086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1747587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1748086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1749587d67c0SRandall Stewart 		}
1750*73ee5756SRandall Stewart 		/*
1751*73ee5756SRandall Stewart 		 * Release the old refcnt, the
1752*73ee5756SRandall Stewart 		 * lookup acquired a ref on the
1753*73ee5756SRandall Stewart 		 * new one already.
1754*73ee5756SRandall Stewart 		 */
175555bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
1756*73ee5756SRandall Stewart 		/*
1757*73ee5756SRandall Stewart 		 * Set in the new stack.
1758*73ee5756SRandall Stewart 		 */
175955bceb1eSRandall Stewart 		tp->t_fb = blk;
1760*73ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
176155bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
176255bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
176355bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
176455bceb1eSRandall Stewart 			     sopt->sopt_name);
176555bceb1eSRandall Stewart 		}
176655bceb1eSRandall Stewart #endif
17673ee9c3c4SRandall Stewart err_out:
176855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
176955bceb1eSRandall Stewart 		return (error);
1770*73ee5756SRandall Stewart 
1771fc4d53ccSGleb Smirnoff 	}
1772fc4d53ccSGleb Smirnoff 
17733b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
17743b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1775fc4d53ccSGleb Smirnoff }
1776fc4d53ccSGleb Smirnoff 
1777fc4d53ccSGleb Smirnoff static int
1778fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1779fc4d53ccSGleb Smirnoff {
1780fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1781fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1782fc4d53ccSGleb Smirnoff 	int error = 0;
1783fc4d53ccSGleb Smirnoff 
1784fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
17853b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
178653af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1787fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1788fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1789fc4d53ccSGleb Smirnoff 
1790fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
17913b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1792fc4d53ccSGleb Smirnoff #ifdef INET6
1793fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1794fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1795fc4d53ccSGleb Smirnoff #endif /* INET6 */
1796fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1797fc4d53ccSGleb Smirnoff 		else
1798fc4d53ccSGleb Smirnoff #endif
1799fc4d53ccSGleb Smirnoff #ifdef INET
1800fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1801fc4d53ccSGleb Smirnoff #endif
1802fc4d53ccSGleb Smirnoff 		return (error);
1803fc4d53ccSGleb Smirnoff 	}
1804fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1805e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1806fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1807fc4d53ccSGleb Smirnoff 
1808e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1809e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1810e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1811e2833083SPeter Lei 		} else {
1812e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1813e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1814c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1815c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1816e2833083SPeter Lei 		}
181755bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
181855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
181955bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
182055bceb1eSRandall Stewart 		return (error);
182155bceb1eSRandall Stewart 	}
1822fc4d53ccSGleb Smirnoff 
18233b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
18243b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1825fc4d53ccSGleb Smirnoff }
1826fc4d53ccSGleb Smirnoff 
1827fc4d53ccSGleb Smirnoff int
1828fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1829fc4d53ccSGleb Smirnoff {
1830fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1831fc4d53ccSGleb Smirnoff 
1832fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1833fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1834fc4d53ccSGleb Smirnoff 
18353b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
183653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18373b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18383b3c08c1SMichael Tuexen 		return (ECONNRESET);
18393b3c08c1SMichael Tuexen 	}
1840fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1841fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1842fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1843fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1844fc4d53ccSGleb Smirnoff 	else
1845fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
184655bceb1eSRandall Stewart }
184755bceb1eSRandall Stewart 
18482529f56eSJonathan T. Looney /*
18492529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18502529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18512529f56eSJonathan T. Looney  */
18522529f56eSJonathan T. Looney #ifdef CTASSERT
18532529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18542529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18552529f56eSJonathan T. Looney #endif
18562529f56eSJonathan T. Looney 
1857ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1858ec1db6e1SJohn Baldwin static int
1859ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1860ec1db6e1SJohn Baldwin {
1861ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1862ec1db6e1SJohn Baldwin 	int error;
1863ec1db6e1SJohn Baldwin 
1864ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1865ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1866ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1867ec1db6e1SJohn Baldwin 		if (error)
1868ec1db6e1SJohn Baldwin 			return (error);
1869ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1870ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1871ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1872ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1873ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1874ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1875ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1876ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1877ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1878ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1879ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1880ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1881ec1db6e1SJohn Baldwin 		return (0);
1882ec1db6e1SJohn Baldwin 	}
1883ec1db6e1SJohn Baldwin 
1884ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1885ec1db6e1SJohn Baldwin }
1886ec1db6e1SJohn Baldwin #endif
1887ec1db6e1SJohn Baldwin 
1888b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1889b8d60729SRandall Stewart 
1890b8d60729SRandall Stewart static int
1891ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1892b8d60729SRandall Stewart {
1893b8d60729SRandall Stewart 	struct cc_algo *algo;
1894b8d60729SRandall Stewart 	void *ptr = NULL;
18953b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1896b8d60729SRandall Stewart 	struct cc_var cc_mem;
1897b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1898b8d60729SRandall Stewart 	size_t mem_sz;
1899b8d60729SRandall Stewart 	int error;
1900b8d60729SRandall Stewart 
1901b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1902b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1903b8d60729SRandall Stewart 	if (error)
1904b8d60729SRandall Stewart 		return(error);
1905b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1906b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1907ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1908b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1909b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1910b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1911b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1912b8d60729SRandall Stewart 				continue;
1913b8d60729SRandall Stewart 			}
1914b8d60729SRandall Stewart 			break;
1915b8d60729SRandall Stewart 		}
1916ea9017fbSRandall Stewart 	}
1917b8d60729SRandall Stewart 	if (algo == NULL) {
1918b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1919b8d60729SRandall Stewart 		return(ESRCH);
1920b8d60729SRandall Stewart 	}
1921ea9017fbSRandall Stewart 	/*
1922ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1923ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1924ea9017fbSRandall Stewart 	 */
1925ea9017fbSRandall Stewart 	cc_refer(algo);
1926ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1927b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1928b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1929b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1930b8d60729SRandall Stewart 		if (mem_sz == 0) {
1931b8d60729SRandall Stewart 			goto no_mem_needed;
1932b8d60729SRandall Stewart 		}
1933b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1934b8d60729SRandall Stewart 	} else {
1935b8d60729SRandall Stewart no_mem_needed:
1936b8d60729SRandall Stewart 		mem_sz = 0;
1937b8d60729SRandall Stewart 		ptr = NULL;
1938b8d60729SRandall Stewart 	}
1939b8d60729SRandall Stewart 	/*
1940b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1941b8d60729SRandall Stewart 	 * back the inplock.
1942b8d60729SRandall Stewart 	 */
1943b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1944df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
194553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1946df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1947ea9017fbSRandall Stewart 		if (ptr)
1948df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1949ea9017fbSRandall Stewart 		/* Release our temp reference */
1950ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1951ea9017fbSRandall Stewart 		cc_release(algo);
1952ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
1953df07bfdaSMichael Tuexen 		return (ECONNRESET);
1954df07bfdaSMichael Tuexen 	}
1955df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
1956df07bfdaSMichael Tuexen 	if (ptr != NULL)
1957b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
1958b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
1959b8d60729SRandall Stewart 	/*
1960b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
1961b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
1962b8d60729SRandall Stewart 	 * Note here we init into stack memory.
1963b8d60729SRandall Stewart 	 */
1964b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
1965b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
1966b8d60729SRandall Stewart 	else
1967b8d60729SRandall Stewart 		error = 0;
1968b8d60729SRandall Stewart 	/*
1969b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
1970b8d60729SRandall Stewart 	 * should not fail we could in theory have a
1971b8d60729SRandall Stewart 	 * KASSERT here.
1972b8d60729SRandall Stewart 	 */
1973b8d60729SRandall Stewart 	if (error == 0) {
1974b8d60729SRandall Stewart 		/*
1975b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
1976b8d60729SRandall Stewart 		 * connection to the new CC module by
1977b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
1978b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
1979b8d60729SRandall Stewart 		 */
1980b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
1981e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1982ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
1983ea9017fbSRandall Stewart 		cc_detach(tp);
1984ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
1985e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1986ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
1987ea9017fbSRandall Stewart 		cc_attach(tp, algo);
1988b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
1989b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1990b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
1991e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
1992b8d60729SRandall Stewart 		}
1993b8d60729SRandall Stewart 	} else if (ptr)
1994b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
1995b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1996ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
1997ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
1998ea9017fbSRandall Stewart 	cc_release(algo);
1999ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2000b8d60729SRandall Stewart 	return (error);
2001b8d60729SRandall Stewart }
2002b8d60729SRandall Stewart 
200355bceb1eSRandall Stewart int
20043b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
200555bceb1eSRandall Stewart {
2006fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
200755bceb1eSRandall Stewart 	int	error, opt, optval;
200855bceb1eSRandall Stewart 	u_int	ui;
200955bceb1eSRandall Stewart 	struct	tcp_info ti;
2010b2e60773SJohn Baldwin #ifdef KERN_TLS
2011b2e60773SJohn Baldwin 	struct tls_enable tls;
2012528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2013b2e60773SJohn Baldwin #endif
20142529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2015adc56f5aSEdward Tomasz Napierala #ifdef STATS
2016adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2017adc56f5aSEdward Tomasz Napierala #endif
2018af6fef3aSGleb Smirnoff 	size_t	len;
2019df8bae1dSRodney W. Grimes 
2020f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
202153af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2022fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2023528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2024f581a26eSGleb Smirnoff 
2025f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2026f581a26eSGleb Smirnoff #ifdef INET6
2027f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2028f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2029f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2030f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2031f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2032f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2033f581a26eSGleb Smirnoff 		}
2034f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2035f581a26eSGleb Smirnoff 		return (0);
2036f581a26eSGleb Smirnoff #endif
2037f581a26eSGleb Smirnoff #ifdef INET
2038f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2039f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2040f581a26eSGleb Smirnoff 		return (0);
2041f581a26eSGleb Smirnoff #endif
2042f581a26eSGleb Smirnoff 	}
2043f581a26eSGleb Smirnoff 
2044d519cedbSGleb Smirnoff 	/*
2045d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2046d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2047d519cedbSGleb Smirnoff 	 */
2048d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2049d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2050d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2051c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2052c8b53cedSMichael Tuexen 			return (EINVAL);
2053af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2054af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2055d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2056d519cedbSGleb Smirnoff 		if (error) {
2057af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2058d519cedbSGleb Smirnoff 			return (error);
2059d519cedbSGleb Smirnoff 		}
2060bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2061d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2062e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2063d519cedbSGleb Smirnoff 		else
2064d519cedbSGleb Smirnoff 			error = ENOENT;
2065d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2066d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2067af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2068af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2069d519cedbSGleb Smirnoff 		return (error);
2070d519cedbSGleb Smirnoff 	}
2071d519cedbSGleb Smirnoff 
2072cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2073cfe8b629SGarrett Wollman 	case SOPT_SET:
2074cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2075fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
207688f6b043SBruce M Simpson 		case TCP_MD5SIG:
20778501a69cSRobert Watson 			INP_WUNLOCK(inp);
207897453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2079fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2080fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
20811cfd4b53SBruce M Simpson 			if (error)
20821e8f5ffaSRobert Watson 				return (error);
208397453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
208409fe6320SNavdeep Parhar 			goto unlock_and_done;
2085fcf59617SAndrey V. Elsukov #endif /* IPSEC */
208609fe6320SNavdeep Parhar 
2087df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2088cfe8b629SGarrett Wollman 		case TCP_NOOPT:
20890471a8c7SRichard Scheffenegger 		case TCP_LRD:
20908501a69cSRobert Watson 			INP_WUNLOCK(inp);
2091cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2092cfe8b629SGarrett Wollman 			    sizeof optval);
2093cfe8b629SGarrett Wollman 			if (error)
20941e8f5ffaSRobert Watson 				return (error);
2095cfe8b629SGarrett Wollman 
20968501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2097cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2098cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2099cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2100cfe8b629SGarrett Wollman 				break;
2101cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2102cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2103cfe8b629SGarrett Wollman 				break;
21040471a8c7SRichard Scheffenegger 			case TCP_LRD:
21050471a8c7SRichard Scheffenegger 				opt = TF_LRD;
21060471a8c7SRichard Scheffenegger 				break;
2107cfe8b629SGarrett Wollman 			default:
2108cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2109cfe8b629SGarrett Wollman 				break;
2110cfe8b629SGarrett Wollman 			}
2111cfe8b629SGarrett Wollman 
2112cfe8b629SGarrett Wollman 			if (optval)
2113cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2114df8bae1dSRodney W. Grimes 			else
2115cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
211609fe6320SNavdeep Parhar unlock_and_done:
211709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
211809fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
211909fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
212009fe6320SNavdeep Parhar 				    sopt->sopt_name);
212109fe6320SNavdeep Parhar 			}
212209fe6320SNavdeep Parhar #endif
21238501a69cSRobert Watson 			INP_WUNLOCK(inp);
2124df8bae1dSRodney W. Grimes 			break;
2125df8bae1dSRodney W. Grimes 
2126007581c0SJonathan Lemon 		case TCP_NOPUSH:
21278501a69cSRobert Watson 			INP_WUNLOCK(inp);
2128007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2129007581c0SJonathan Lemon 			    sizeof optval);
2130007581c0SJonathan Lemon 			if (error)
21311e8f5ffaSRobert Watson 				return (error);
2132007581c0SJonathan Lemon 
21338501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2134007581c0SJonathan Lemon 			if (optval)
2135007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2136d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2137007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2138109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2139109eb549SGleb Smirnoff 					struct epoch_tracker et;
2140109eb549SGleb Smirnoff 
2141109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2142f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2143109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2144109eb549SGleb Smirnoff 				}
2145007581c0SJonathan Lemon 			}
214609fe6320SNavdeep Parhar 			goto unlock_and_done;
2147007581c0SJonathan Lemon 
21489e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21499e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21509e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21519e644c23SMichael Tuexen 			    sizeof optval);
21529e644c23SMichael Tuexen 			if (error)
21539e644c23SMichael Tuexen 				return (error);
21549e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
21559e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
21569e644c23SMichael Tuexen 				/* Its got to be in range */
21579e644c23SMichael Tuexen 				return (EINVAL);
21589e644c23SMichael Tuexen 			}
21599e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
21609e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
21619e644c23SMichael Tuexen 				return (EINVAL);
21629e644c23SMichael Tuexen 			}
21639e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
21649e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
21659e644c23SMichael Tuexen 				/* You can't change after you are connected */
21669e644c23SMichael Tuexen 				error = EINVAL;
21679e644c23SMichael Tuexen 			} else {
21689e644c23SMichael Tuexen 				/* Ok we are all good set the port */
21699e644c23SMichael Tuexen 				tp->t_port = htons(optval);
21709e644c23SMichael Tuexen 			}
21719e644c23SMichael Tuexen 			goto unlock_and_done;
21729e644c23SMichael Tuexen 
2173df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
21748501a69cSRobert Watson 			INP_WUNLOCK(inp);
2175cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2176cfe8b629SGarrett Wollman 			    sizeof optval);
2177cfe8b629SGarrett Wollman 			if (error)
21781e8f5ffaSRobert Watson 				return (error);
2179df8bae1dSRodney W. Grimes 
21808501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
218153369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2182603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2183cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2184a0292f23SGarrett Wollman 			else
2185a0292f23SGarrett Wollman 				error = EINVAL;
218609fe6320SNavdeep Parhar 			goto unlock_and_done;
2187a0292f23SGarrett Wollman 
2188b8af5dfaSRobert Watson 		case TCP_INFO:
21898501a69cSRobert Watson 			INP_WUNLOCK(inp);
2190b8af5dfaSRobert Watson 			error = EINVAL;
2191b8af5dfaSRobert Watson 			break;
2192b8af5dfaSRobert Watson 
2193adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2194adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2195adc56f5aSEdward Tomasz Napierala #ifdef STATS
2196adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2197adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2198adc56f5aSEdward Tomasz Napierala 			if (error)
2199adc56f5aSEdward Tomasz Napierala 				return (error);
2200adc56f5aSEdward Tomasz Napierala 
2201adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2202adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2203adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2204adc56f5aSEdward Tomasz Napierala 			else
2205adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2206adc56f5aSEdward Tomasz Napierala 
2207adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2208adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2209adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2210adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2211adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2212adc56f5aSEdward Tomasz Napierala 				sbp = t;
2213adc56f5aSEdward Tomasz Napierala 			}
2214adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2215adc56f5aSEdward Tomasz Napierala 
2216adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2217adc56f5aSEdward Tomasz Napierala #else
2218adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2219adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2220adc56f5aSEdward Tomasz Napierala 			break;
2221adc56f5aSEdward Tomasz Napierala 
2222dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2223ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
222473e263b1SGleb Smirnoff 			break;
2225dbc42409SLawrence Stewart 
2226a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2227a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2228a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2229a034518aSAndrew Gallatin 			    sizeof(optval));
2230a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2231a034518aSAndrew Gallatin 			if (!error)
2232a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2233a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2234a034518aSAndrew Gallatin 			break;
2235a034518aSAndrew Gallatin 
2236b2e60773SJohn Baldwin #ifdef KERN_TLS
2237b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2238b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2239ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2240b2e60773SJohn Baldwin 			if (error)
2241b2e60773SJohn Baldwin 				break;
2242fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2243b2e60773SJohn Baldwin 			break;
2244b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2245b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2246b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2247b2e60773SJohn Baldwin 			if (error)
2248b2e60773SJohn Baldwin 				return (error);
2249b2e60773SJohn Baldwin 
2250b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2251fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2252b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2253b2e60773SJohn Baldwin 			break;
2254f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2255f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2256f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2257f1f93475SJohn Baldwin 			    sizeof(tls));
2258f1f93475SJohn Baldwin 			if (error)
2259f1f93475SJohn Baldwin 				break;
2260fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2261f1f93475SJohn Baldwin 			break;
2262b2e60773SJohn Baldwin #endif
226308af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
22649077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
22659077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
22669077f387SGleb Smirnoff 		case TCP_KEEPINIT:
22679077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
22689077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
22699077f387SGleb Smirnoff 			if (error)
22709077f387SGleb Smirnoff 				return (error);
22719077f387SGleb Smirnoff 
22729077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
22739077f387SGleb Smirnoff 				error = EINVAL;
22749077f387SGleb Smirnoff 				break;
22759077f387SGleb Smirnoff 			}
22769077f387SGleb Smirnoff 			ui *= hz;
22779077f387SGleb Smirnoff 
22789077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
22799077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
228008af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
228108af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
228208af8aacSRandall Stewart 				break;
228308af8aacSRandall Stewart 
22849077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
22859077f387SGleb Smirnoff 				tp->t_keepidle = ui;
22869077f387SGleb Smirnoff 				/*
22879077f387SGleb Smirnoff 				 * XXX: better check current remaining
22889077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
22899077f387SGleb Smirnoff 				 */
22909077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
22919077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
22929077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
22939077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
22949077f387SGleb Smirnoff 				break;
22959077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
22969077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
22979077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
22989077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
22999077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23009077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23019077f387SGleb Smirnoff 				break;
23029077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23039077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23049077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23059077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23069077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23079077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23089077f387SGleb Smirnoff 				break;
23099077f387SGleb Smirnoff 			}
231009fe6320SNavdeep Parhar 			goto unlock_and_done;
23119077f387SGleb Smirnoff 
231285c05144SGleb Smirnoff 		case TCP_KEEPCNT:
231385c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
231485c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
231585c05144SGleb Smirnoff 			if (error)
231685c05144SGleb Smirnoff 				return (error);
231785c05144SGleb Smirnoff 
231885c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
231985c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
232085c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
232185c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
232285c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
232385c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
232485c05144SGleb Smirnoff 			goto unlock_and_done;
232585c05144SGleb Smirnoff 
232686a996e6SHiren Panchasara #ifdef TCPPCAP
232786a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
232886a996e6SHiren Panchasara 		case TCP_PCAP_IN:
232986a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
233086a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
233186a996e6SHiren Panchasara 			    sizeof optval);
233286a996e6SHiren Panchasara 			if (error)
233386a996e6SHiren Panchasara 				return (error);
233486a996e6SHiren Panchasara 
233586a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
233686a996e6SHiren Panchasara 			if (optval >= 0)
2337399a5655SRichard Scheffenegger 				tcp_pcap_set_sock_max(
2338399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
233986a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
234086a996e6SHiren Panchasara 					optval);
234186a996e6SHiren Panchasara 			else
234286a996e6SHiren Panchasara 				error = EINVAL;
234386a996e6SHiren Panchasara 			goto unlock_and_done;
234486a996e6SHiren Panchasara #endif
234586a996e6SHiren Panchasara 
2346c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2347c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2348c560df6fSPatrick Kelsey 
2349281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2350c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2351c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2352281a0fd4SPatrick Kelsey 				return (EPERM);
2353281a0fd4SPatrick Kelsey 
2354c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2355c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2356281a0fd4SPatrick Kelsey 			if (error)
2357281a0fd4SPatrick Kelsey 				return (error);
2358281a0fd4SPatrick Kelsey 
2359281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2360d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2361d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2362d442a657SMichael Tuexen 				error = EINVAL;
2363d442a657SMichael Tuexen 				goto unlock_and_done;
2364d442a657SMichael Tuexen 			}
2365c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2366c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2367c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2368c560df6fSPatrick Kelsey 						error = EPERM;
2369c560df6fSPatrick Kelsey 						goto unlock_and_done;
2370c560df6fSPatrick Kelsey 					}
2371c560df6fSPatrick Kelsey 
2372c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2373281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2374281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2375c560df6fSPatrick Kelsey 				} else {
2376c560df6fSPatrick Kelsey 					/*
2377c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2378c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2379c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2380c560df6fSPatrick Kelsey 					 * connect.
2381c560df6fSPatrick Kelsey 					 */
2382c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2383c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2384c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2385c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2386c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2387c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2388c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2389c560df6fSPatrick Kelsey 					}
2390c560df6fSPatrick Kelsey 				}
2391d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2392281a0fd4SPatrick Kelsey 			} else
2393281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2394281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2395c560df6fSPatrick Kelsey 		}
2396281a0fd4SPatrick Kelsey 
2397e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
23982529f56eSJonathan T. Looney 		case TCP_LOG:
23992529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24002529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24012529f56eSJonathan T. Looney 			    sizeof optval);
24022529f56eSJonathan T. Looney 			if (error)
24032529f56eSJonathan T. Looney 				return (error);
24042529f56eSJonathan T. Looney 
24052529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24062529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24072529f56eSJonathan T. Looney 			goto unlock_and_done;
24082529f56eSJonathan T. Looney 
24092529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24102529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24112529f56eSJonathan T. Looney 			error = EINVAL;
24122529f56eSJonathan T. Looney 			break;
24132529f56eSJonathan T. Looney 
24142529f56eSJonathan T. Looney 		case TCP_LOGID:
24152529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24162529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24172529f56eSJonathan T. Looney 			if (error)
24182529f56eSJonathan T. Looney 				break;
24192529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24202529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24212529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24222529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24232529f56eSJonathan T. Looney 			break;
24242529f56eSJonathan T. Looney 
24252529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24262529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24272529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24282529f56eSJonathan T. Looney 			error =
24292529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
24302529f56eSJonathan T. Looney 			if (error)
24312529f56eSJonathan T. Looney 				break;
24322529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24332529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24342529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
24352529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24362529f56eSJonathan T. Looney 				    M_WAITOK, true);
24372529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24382529f56eSJonathan T. Looney 			} else {
24392529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24402529f56eSJonathan T. Looney 				/*
24412529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24422529f56eSJonathan T. Looney 				 * INP lock.
24432529f56eSJonathan T. Looney 				 */
24442529f56eSJonathan T. Looney 			}
24452529f56eSJonathan T. Looney 			break;
2446e24e5683SJonathan T. Looney #endif
24472529f56eSJonathan T. Looney 
2448df8bae1dSRodney W. Grimes 		default:
24498501a69cSRobert Watson 			INP_WUNLOCK(inp);
2450df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2451df8bae1dSRodney W. Grimes 			break;
2452df8bae1dSRodney W. Grimes 		}
2453df8bae1dSRodney W. Grimes 		break;
2454df8bae1dSRodney W. Grimes 
2455cfe8b629SGarrett Wollman 	case SOPT_GET:
24561e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2457cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2458fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
245988f6b043SBruce M Simpson 		case TCP_MD5SIG:
24608501a69cSRobert Watson 			INP_WUNLOCK(inp);
246197453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2462fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2463fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
24641cfd4b53SBruce M Simpson 			break;
2465265ed012SBruce M Simpson #endif
24661e8f5ffaSRobert Watson 
2467df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2468cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
24698501a69cSRobert Watson 			INP_WUNLOCK(inp);
2470b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2471df8bae1dSRodney W. Grimes 			break;
2472df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2473cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
24748501a69cSRobert Watson 			INP_WUNLOCK(inp);
2475b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2476df8bae1dSRodney W. Grimes 			break;
24779e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
24789e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
24799e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
24809e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
24819e644c23SMichael Tuexen 			break;
2482a0292f23SGarrett Wollman 		case TCP_NOOPT:
2483cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
24848501a69cSRobert Watson 			INP_WUNLOCK(inp);
2485b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2486a0292f23SGarrett Wollman 			break;
2487a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2488cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
24898501a69cSRobert Watson 			INP_WUNLOCK(inp);
2490b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2491b8af5dfaSRobert Watson 			break;
2492b8af5dfaSRobert Watson 		case TCP_INFO:
2493b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
24948501a69cSRobert Watson 			INP_WUNLOCK(inp);
2495b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2496a0292f23SGarrett Wollman 			break;
2497adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2498adc56f5aSEdward Tomasz Napierala 			{
2499adc56f5aSEdward Tomasz Napierala #ifdef STATS
2500adc56f5aSEdward Tomasz Napierala 			int nheld;
2501adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2502adc56f5aSEdward Tomasz Napierala 
2503adc56f5aSEdward Tomasz Napierala 			error = 0;
2504adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2505adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2506adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2507adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2508adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2509adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2510adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2511adc56f5aSEdward Tomasz Napierala 			else
2512adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2513adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2514adc56f5aSEdward Tomasz Napierala 			if (error)
2515adc56f5aSEdward Tomasz Napierala 				break;
2516adc56f5aSEdward Tomasz Napierala 
2517adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2518adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2519adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2520adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2521adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2522adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2523adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2524adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2525adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2526adc56f5aSEdward Tomasz Napierala 				break;
2527adc56f5aSEdward Tomasz Napierala 			}
2528adc56f5aSEdward Tomasz Napierala 
2529adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2530adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2531adc56f5aSEdward Tomasz Napierala 				goto unhold;
2532adc56f5aSEdward Tomasz Napierala 
2533adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2534adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2535adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2536adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2537adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2538adc56f5aSEdward Tomasz Napierala unhold:
2539adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2540adc56f5aSEdward Tomasz Napierala #else
2541adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2542adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2543adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2544adc56f5aSEdward Tomasz Napierala 			break;
2545adc56f5aSEdward Tomasz Napierala 			}
2546dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2547af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2548dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2549af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2550dbc42409SLawrence Stewart 			break;
255108af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25522f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
25532f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
25542f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
25552f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
25562f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
255708af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
255808af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
255908af8aacSRandall Stewart 				break;
25602f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
25615a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
25622f3eb7f4SGleb Smirnoff 				break;
25632f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
25645a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
25652f3eb7f4SGleb Smirnoff 				break;
25662f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
25675a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
25682f3eb7f4SGleb Smirnoff 				break;
25692f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
25705a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
25712f3eb7f4SGleb Smirnoff 				break;
25722f3eb7f4SGleb Smirnoff 			}
25732f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
25742f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
25752f3eb7f4SGleb Smirnoff 			break;
257686a996e6SHiren Panchasara #ifdef TCPPCAP
257786a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
257886a996e6SHiren Panchasara 		case TCP_PCAP_IN:
2579399a5655SRichard Scheffenegger 			optval = tcp_pcap_get_sock_max(
2580399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
258186a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
258286a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
258386a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
258486a996e6SHiren Panchasara 			break;
258586a996e6SHiren Panchasara #endif
2586281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2587281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2588281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2589281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2590281a0fd4SPatrick Kelsey 			break;
2591e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
25922529f56eSJonathan T. Looney 		case TCP_LOG:
259369c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
25942529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25952529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
25962529f56eSJonathan T. Looney 			break;
25972529f56eSJonathan T. Looney 		case TCP_LOGBUF:
25982529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
25992529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26002529f56eSJonathan T. Looney 			break;
26012529f56eSJonathan T. Looney 		case TCP_LOGID:
26022529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26032529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26042529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26052529f56eSJonathan T. Looney 			break;
26062529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26072529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26082529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26092529f56eSJonathan T. Looney 			error = EINVAL;
26102529f56eSJonathan T. Looney 			break;
2611e24e5683SJonathan T. Looney #endif
2612b2e60773SJohn Baldwin #ifdef KERN_TLS
2613b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2614fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2615b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2616bf256782SMark Johnston 			if (error == 0)
2617bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2618bf256782SMark Johnston 				    sizeof(optval));
2619b2e60773SJohn Baldwin 			break;
2620f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2621fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2622f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2623bf256782SMark Johnston 			if (error == 0)
2624bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2625bf256782SMark Johnston 				    sizeof(optval));
2626f1f93475SJohn Baldwin 			break;
2627b2e60773SJohn Baldwin #endif
26280471a8c7SRichard Scheffenegger 		case TCP_LRD:
26290471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
26300471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
26310471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
26320471a8c7SRichard Scheffenegger 			break;
2633df8bae1dSRodney W. Grimes 		default:
26348501a69cSRobert Watson 			INP_WUNLOCK(inp);
2635df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2636df8bae1dSRodney W. Grimes 			break;
2637df8bae1dSRodney W. Grimes 		}
2638df8bae1dSRodney W. Grimes 		break;
2639df8bae1dSRodney W. Grimes 	}
2640df8bae1dSRodney W. Grimes 	return (error);
2641df8bae1dSRodney W. Grimes }
26428501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2643bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2644df8bae1dSRodney W. Grimes 
264526e30fbbSDavid Greenman /*
2646df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2647df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2648df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2649df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2650df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2651df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2652df8bae1dSRodney W. Grimes  */
2653623dce13SRobert Watson static void
2654ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2655df8bae1dSRodney W. Grimes {
26569eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
26579eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2658e6e0b5ffSRobert Watson 
265997a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26608501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2661df8bae1dSRodney W. Grimes 
2662623dce13SRobert Watson 	/*
2663623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2664623dce13SRobert Watson 	 * socket is still open.
2665623dce13SRobert Watson 	 */
26668db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
26678db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2668df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2669623dce13SRobert Watson 		KASSERT(tp != NULL,
2670623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2671623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2672243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2673623dce13SRobert Watson 		KASSERT(tp != NULL,
2674623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2675623dce13SRobert Watson 	} else {
2676df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2677df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2678623dce13SRobert Watson 		tcp_usrclosed(tp);
2679ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2680f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2681f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2682df8bae1dSRodney W. Grimes 	}
2683df8bae1dSRodney W. Grimes }
2684df8bae1dSRodney W. Grimes 
2685df8bae1dSRodney W. Grimes /*
2686df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2687df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2688df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2689df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2690df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2691df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2692df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2693df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2694df8bae1dSRodney W. Grimes  */
2695623dce13SRobert Watson static void
2696ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2697df8bae1dSRodney W. Grimes {
2698df8bae1dSRodney W. Grimes 
269997a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27009eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2701e6e0b5ffSRobert Watson 
2702df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2703df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
270409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
270509fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
270609fe6320SNavdeep Parhar #endif
2707550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2708bc65987aSKip Macy 		/* FALLTHROUGH */
2709bc65987aSKip Macy 	case TCPS_CLOSED:
2710df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2711623dce13SRobert Watson 		/*
2712623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2713623dce13SRobert Watson 		 * still open.
2714623dce13SRobert Watson 		 */
2715623dce13SRobert Watson 		KASSERT(tp != NULL,
2716623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2717df8bae1dSRodney W. Grimes 		break;
2718df8bae1dSRodney W. Grimes 
2719a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2720df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2721a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2722a0292f23SGarrett Wollman 		break;
2723a0292f23SGarrett Wollman 
2724df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
272557f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2726df8bae1dSRodney W. Grimes 		break;
2727df8bae1dSRodney W. Grimes 
2728df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
272957f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2730df8bae1dSRodney W. Grimes 		break;
2731df8bae1dSRodney W. Grimes 	}
273208af8aacSRandall Stewart 	if (tp->t_acktime == 0)
273308af8aacSRandall Stewart 		tp->t_acktime = ticks;
2734abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27359eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2736abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27377c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27387c72af87SMohan Srinivasan 			int timeout;
27397c72af87SMohan Srinivasan 
27407c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27419077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2742b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2743b6239c4aSAndras Olah 		}
2744df8bae1dSRodney W. Grimes 	}
27457c72af87SMohan Srinivasan }
2746497057eeSRobert Watson 
2747497057eeSRobert Watson #ifdef DDB
2748497057eeSRobert Watson static void
2749497057eeSRobert Watson db_print_indent(int indent)
2750497057eeSRobert Watson {
2751497057eeSRobert Watson 	int i;
2752497057eeSRobert Watson 
2753497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2754497057eeSRobert Watson 		db_printf(" ");
2755497057eeSRobert Watson }
2756497057eeSRobert Watson 
2757497057eeSRobert Watson static void
2758497057eeSRobert Watson db_print_tstate(int t_state)
2759497057eeSRobert Watson {
2760497057eeSRobert Watson 
2761497057eeSRobert Watson 	switch (t_state) {
2762497057eeSRobert Watson 	case TCPS_CLOSED:
2763497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2764497057eeSRobert Watson 		return;
2765497057eeSRobert Watson 
2766497057eeSRobert Watson 	case TCPS_LISTEN:
2767497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2768497057eeSRobert Watson 		return;
2769497057eeSRobert Watson 
2770497057eeSRobert Watson 	case TCPS_SYN_SENT:
2771497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2772497057eeSRobert Watson 		return;
2773497057eeSRobert Watson 
2774497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2775497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2776497057eeSRobert Watson 		return;
2777497057eeSRobert Watson 
2778497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2779497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2780497057eeSRobert Watson 		return;
2781497057eeSRobert Watson 
2782497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2783497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2784497057eeSRobert Watson 		return;
2785497057eeSRobert Watson 
2786497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2787497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2788497057eeSRobert Watson 		return;
2789497057eeSRobert Watson 
2790497057eeSRobert Watson 	case TCPS_CLOSING:
2791497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2792497057eeSRobert Watson 		return;
2793497057eeSRobert Watson 
2794497057eeSRobert Watson 	case TCPS_LAST_ACK:
2795497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2796497057eeSRobert Watson 		return;
2797497057eeSRobert Watson 
2798497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2799497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2800497057eeSRobert Watson 		return;
2801497057eeSRobert Watson 
2802497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2803497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2804497057eeSRobert Watson 		return;
2805497057eeSRobert Watson 
2806497057eeSRobert Watson 	default:
2807497057eeSRobert Watson 		db_printf("unknown");
2808497057eeSRobert Watson 		return;
2809497057eeSRobert Watson 	}
2810497057eeSRobert Watson }
2811497057eeSRobert Watson 
2812497057eeSRobert Watson static void
2813497057eeSRobert Watson db_print_tflags(u_int t_flags)
2814497057eeSRobert Watson {
2815497057eeSRobert Watson 	int comma;
2816497057eeSRobert Watson 
2817497057eeSRobert Watson 	comma = 0;
2818497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2819497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2820497057eeSRobert Watson 		comma = 1;
2821497057eeSRobert Watson 	}
2822497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2823497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2824497057eeSRobert Watson 		comma = 1;
2825497057eeSRobert Watson 	}
2826497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2827497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2828497057eeSRobert Watson 		comma = 1;
2829497057eeSRobert Watson 	}
2830497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2831497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2832497057eeSRobert Watson 		comma = 1;
2833497057eeSRobert Watson 	}
2834497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2835497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2836497057eeSRobert Watson 		comma = 1;
2837497057eeSRobert Watson 	}
2838497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2839497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2840497057eeSRobert Watson 		comma = 1;
2841497057eeSRobert Watson 	}
2842497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2843497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2844497057eeSRobert Watson 		comma = 1;
2845497057eeSRobert Watson 	}
2846497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2847497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2848497057eeSRobert Watson 		comma = 1;
2849497057eeSRobert Watson 	}
2850497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2851497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2852497057eeSRobert Watson 		comma = 1;
2853497057eeSRobert Watson 	}
2854497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2855497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2856497057eeSRobert Watson 		comma = 1;
2857497057eeSRobert Watson 	}
2858497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2859497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2860497057eeSRobert Watson 		comma = 1;
2861497057eeSRobert Watson 	}
2862497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2863497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2864497057eeSRobert Watson 		comma = 1;
2865497057eeSRobert Watson 	}
2866497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2867497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2868497057eeSRobert Watson 		comma = 1;
2869497057eeSRobert Watson 	}
28703f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
28713f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
28723f169c54SRichard Scheffenegger 		comma = 1;
28733f169c54SRichard Scheffenegger 	}
2874497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2875497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2876497057eeSRobert Watson 		comma = 1;
2877497057eeSRobert Watson 	}
2878493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2879493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2880497057eeSRobert Watson 		comma = 1;
2881497057eeSRobert Watson 	}
2882497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2883497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2884497057eeSRobert Watson 		comma = 1;
2885497057eeSRobert Watson 	}
2886497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2887497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2888497057eeSRobert Watson 		comma = 1;
2889497057eeSRobert Watson 	}
2890497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2891497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2892497057eeSRobert Watson 		comma = 1;
2893497057eeSRobert Watson 	}
2894dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2895dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2896dbc42409SLawrence Stewart 		comma = 1;
2897dbc42409SLawrence Stewart 	}
2898497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2899497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2900497057eeSRobert Watson 		comma = 1;
2901497057eeSRobert Watson 	}
29023f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
29033f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
29043f169c54SRichard Scheffenegger 		comma = 1;
29053f169c54SRichard Scheffenegger 	}
2906497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2907497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2908497057eeSRobert Watson 		comma = 1;
2909497057eeSRobert Watson 	}
2910497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2911497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2912497057eeSRobert Watson 		comma = 1;
2913497057eeSRobert Watson 	}
2914497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2915497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2916497057eeSRobert Watson 		comma = 1;
2917497057eeSRobert Watson 	}
2918281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2919281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2920281a0fd4SPatrick Kelsey 		comma = 1;
2921281a0fd4SPatrick Kelsey 	}
2922497057eeSRobert Watson }
2923497057eeSRobert Watson 
2924497057eeSRobert Watson static void
29253cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29263cf38784SMichael Tuexen {
29273cf38784SMichael Tuexen 	int comma;
29283cf38784SMichael Tuexen 
29293cf38784SMichael Tuexen 	comma = 0;
29303f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29313f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29323f169c54SRichard Scheffenegger 		comma = 1;
29333f169c54SRichard Scheffenegger 	}
29343f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29353f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29363f169c54SRichard Scheffenegger 		comma = 1;
29373f169c54SRichard Scheffenegger 	}
29383f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29393f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29403f169c54SRichard Scheffenegger 		comma = 1;
29413f169c54SRichard Scheffenegger 	}
29423f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29433f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29443f169c54SRichard Scheffenegger 		comma = 1;
29453f169c54SRichard Scheffenegger 	}
29463f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29473f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29483f169c54SRichard Scheffenegger 		comma = 1;
29493f169c54SRichard Scheffenegger 	}
29503cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29513cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29523cf38784SMichael Tuexen 		comma = 1;
29533cf38784SMichael Tuexen 	}
29543f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
29553f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
29563f169c54SRichard Scheffenegger 		comma = 1;
29573f169c54SRichard Scheffenegger 	}
29583f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
29593f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
29603f169c54SRichard Scheffenegger 		comma = 1;
29613f169c54SRichard Scheffenegger 	}
29623f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
29633f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
29643f169c54SRichard Scheffenegger 		comma = 1;
29653f169c54SRichard Scheffenegger 	}
29663f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
29673f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
29683f169c54SRichard Scheffenegger 		comma = 1;
29693f169c54SRichard Scheffenegger 	}
29703cf38784SMichael Tuexen }
29713cf38784SMichael Tuexen 
29723cf38784SMichael Tuexen static void
2973497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2974497057eeSRobert Watson {
2975497057eeSRobert Watson 	int comma;
2976497057eeSRobert Watson 
2977497057eeSRobert Watson 	comma = 0;
2978497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2979497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2980497057eeSRobert Watson 		comma = 1;
2981497057eeSRobert Watson 	}
2982497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2983497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2984497057eeSRobert Watson 		comma = 1;
2985497057eeSRobert Watson 	}
2986497057eeSRobert Watson }
2987497057eeSRobert Watson 
2988497057eeSRobert Watson static void
2989497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2990497057eeSRobert Watson {
2991497057eeSRobert Watson 
2992497057eeSRobert Watson 	db_print_indent(indent);
2993497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2994497057eeSRobert Watson 
2995497057eeSRobert Watson 	indent += 2;
2996497057eeSRobert Watson 
2997497057eeSRobert Watson 	db_print_indent(indent);
2998497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2999c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3000497057eeSRobert Watson 
3001497057eeSRobert Watson 	db_print_indent(indent);
3002446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3003446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3004497057eeSRobert Watson 
3005497057eeSRobert Watson 	db_print_indent(indent);
3006497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3007497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3008497057eeSRobert Watson 	db_printf(")\n");
3009497057eeSRobert Watson 
3010497057eeSRobert Watson 	db_print_indent(indent);
3011497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3012497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3013497057eeSRobert Watson 	db_printf(")\n");
3014497057eeSRobert Watson 
3015497057eeSRobert Watson 	db_print_indent(indent);
30163cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30173cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30183cf38784SMichael Tuexen 	db_printf(")\n");
30193cf38784SMichael Tuexen 
30203cf38784SMichael Tuexen 	db_print_indent(indent);
3021fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3022497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3023497057eeSRobert Watson 
3024497057eeSRobert Watson 	db_print_indent(indent);
3025497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3026497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3027497057eeSRobert Watson 
3028497057eeSRobert Watson 	db_print_indent(indent);
3029497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3030497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3031497057eeSRobert Watson 
3032497057eeSRobert Watson 	db_print_indent(indent);
30333ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3034497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3035497057eeSRobert Watson 
3036497057eeSRobert Watson 	db_print_indent(indent);
30373ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
30381c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3039497057eeSRobert Watson 
3040497057eeSRobert Watson 	db_print_indent(indent);
30413ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
30421c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3043497057eeSRobert Watson 
3044497057eeSRobert Watson 	db_print_indent(indent);
30450c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
30460c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3047497057eeSRobert Watson 
3048497057eeSRobert Watson 	db_print_indent(indent);
30491c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
30501c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3051497057eeSRobert Watson 
3052497057eeSRobert Watson 	db_print_indent(indent);
30531c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
30541c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3055497057eeSRobert Watson 
3056497057eeSRobert Watson 	db_print_indent(indent);
3057bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3058bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3059497057eeSRobert Watson 
3060497057eeSRobert Watson 	db_print_indent(indent);
306118b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3062497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3063497057eeSRobert Watson 
3064497057eeSRobert Watson 	db_print_indent(indent);
3065497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3066497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3067497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3068497057eeSRobert Watson 
3069497057eeSRobert Watson 	db_print_indent(indent);
3070497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3071497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3072497057eeSRobert Watson 
3073497057eeSRobert Watson 	db_print_indent(indent);
30749f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
30751a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3076497057eeSRobert Watson 
3077497057eeSRobert Watson 	db_print_indent(indent);
3078497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
30793ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3080497057eeSRobert Watson 
3081497057eeSRobert Watson 	db_print_indent(indent);
30823ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
30839f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3084497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3085497057eeSRobert Watson 
3086497057eeSRobert Watson 	db_print_indent(indent);
30873529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
30883529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3089497057eeSRobert Watson 
3090497057eeSRobert Watson 	db_print_indent(indent);
3091a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3092a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3093497057eeSRobert Watson 
3094497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3095497057eeSRobert Watson 
3096497057eeSRobert Watson 	db_print_indent(indent);
3097497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3098497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3099497057eeSRobert Watson }
3100497057eeSRobert Watson 
3101497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3102497057eeSRobert Watson {
3103497057eeSRobert Watson 	struct tcpcb *tp;
3104497057eeSRobert Watson 
3105497057eeSRobert Watson 	if (!have_addr) {
3106497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3107497057eeSRobert Watson 		return;
3108497057eeSRobert Watson 	}
3109497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3110497057eeSRobert Watson 
3111497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3112497057eeSRobert Watson }
3113497057eeSRobert Watson #endif
3114