xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 8c6104c48ea3f0ffaabeb9784b0f2327db04e7af)
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>
41497057eeSRobert Watson #include "opt_ddb.h"
421cfd4b53SBruce M Simpson #include "opt_inet.h"
43fb59c426SYoshinobu Inoue #include "opt_inet6.h"
44fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
45b2e60773SJohn Baldwin #include "opt_kern_tls.h"
460cc12cc5SJoerg Wunsch 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
48df8bae1dSRodney W. Grimes #include <sys/systm.h>
49adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
509077f387SGleb Smirnoff #include <sys/limits.h>
51f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5255bceb1eSRandall Stewart #include <sys/refcount.h>
53c7a82f90SGarrett Wollman #include <sys/kernel.h>
54b2e60773SJohn Baldwin #include <sys/ktls.h>
55adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5698163b98SPoul-Henning Kamp #include <sys/sysctl.h>
57df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
58fb59c426SYoshinobu Inoue #ifdef INET6
59fb59c426SYoshinobu Inoue #include <sys/domain.h>
60fb59c426SYoshinobu Inoue #endif /* INET6 */
61df8bae1dSRodney W. Grimes #include <sys/socket.h>
62df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
63df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6491421ba2SRobert Watson #include <sys/proc.h>
6591421ba2SRobert Watson #include <sys/jail.h>
66adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
67df8bae1dSRodney W. Grimes 
68497057eeSRobert Watson #ifdef DDB
69497057eeSRobert Watson #include <ddb/ddb.h>
70497057eeSRobert Watson #endif
71497057eeSRobert Watson 
72df8bae1dSRodney W. Grimes #include <net/if.h>
7376039bc8SGleb Smirnoff #include <net/if_var.h>
74df8bae1dSRodney W. Grimes #include <net/route.h>
75530c0060SRobert Watson #include <net/vnet.h>
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes #include <netinet/in.h>
785d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
79df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
80b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
81b5e8ce9fSBruce Evans #include <netinet/in_var.h>
823b0ee680SRichard Scheffenegger #include <netinet/ip.h>
83df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
84fb59c426SYoshinobu Inoue #ifdef INET6
85b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
86b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
87fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
88a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
89fb59c426SYoshinobu Inoue #endif
902de3e790SGleb Smirnoff #include <netinet/tcp.h>
91df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
92df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
93df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
952529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
96df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
974644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
98c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
99fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
10086a996e6SHiren Panchasara #ifdef TCPPCAP
10186a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10286a996e6SHiren Panchasara #endif
10309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
104bc65987aSKip Macy #include <netinet/tcp_offload.h>
10509fe6320SNavdeep Parhar #endif
106fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
107df8bae1dSRodney W. Grimes 
108adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
109adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
110adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
111adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
112adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
113adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
114adc56f5aSEdward Tomasz Napierala 
115df8bae1dSRodney W. Grimes /*
116df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
117df8bae1dSRodney W. Grimes  */
118b287c6c7SBjoern A. Zeeb #ifdef INET
119a9d22cceSGleb Smirnoff static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
1204d77a549SAlfred Perlstein 		    struct thread *td);
121b287c6c7SBjoern A. Zeeb #endif /* INET */
122fb59c426SYoshinobu Inoue #ifdef INET6
123a9d22cceSGleb Smirnoff static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
1244d77a549SAlfred Perlstein 		    struct thread *td);
125fb59c426SYoshinobu Inoue #endif /* INET6 */
126623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
127623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
128*8c6104c4SMarius Strobl static void	tcp_fill_info(const struct tcpcb *, struct tcp_info *);
1292c37256eSGarrett Wollman 
130d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
131d3b6c96bSRandall Stewart 
13200812bbdSMichael Tuexen static void
13300812bbdSMichael Tuexen tcp_bblog_pru(struct tcpcb *tp, uint32_t pru, int error)
13400812bbdSMichael Tuexen {
13500812bbdSMichael Tuexen 	struct tcp_log_buffer *lgb;
13600812bbdSMichael Tuexen 
137c2399dd2SMichael Tuexen 	KASSERT(tp != NULL, ("tcp_bblog_pru: tp == NULL"));
13800812bbdSMichael Tuexen 	INP_WLOCK_ASSERT(tptoinpcb(tp));
13969c7c811SRandall Stewart 	if (tcp_bblogging_on(tp)) {
14069c7c811SRandall Stewart 		lgb = tcp_log_event(tp, NULL, NULL, NULL, TCP_LOG_PRU, error,
14100812bbdSMichael Tuexen 		    0, NULL, false, NULL, NULL, 0, NULL);
14200812bbdSMichael Tuexen 	} else {
14300812bbdSMichael Tuexen 		lgb = NULL;
14400812bbdSMichael Tuexen 	}
14500812bbdSMichael Tuexen 	if (lgb != NULL) {
14600812bbdSMichael Tuexen 		if (error >= 0) {
14700812bbdSMichael Tuexen 			lgb->tlb_errno = (uint32_t)error;
14800812bbdSMichael Tuexen 		}
14900812bbdSMichael Tuexen 		lgb->tlb_flex1 = pru;
15000812bbdSMichael Tuexen 	}
15100812bbdSMichael Tuexen }
15200812bbdSMichael Tuexen 
1532c37256eSGarrett Wollman /*
1542c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1552c37256eSGarrett Wollman  * and an internet control block.
1562c37256eSGarrett Wollman  */
1572c37256eSGarrett Wollman static int
158b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1592c37256eSGarrett Wollman {
160f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
161623dce13SRobert Watson 	struct tcpcb *tp = NULL;
162623dce13SRobert Watson 	int error;
1632c37256eSGarrett Wollman 
164623dce13SRobert Watson 	inp = sotoinpcb(so);
165623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1662c37256eSGarrett Wollman 
1670f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1682c37256eSGarrett Wollman 	if (error)
1692c37256eSGarrett Wollman 		goto out;
1702c37256eSGarrett Wollman 
1710f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1720f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1730f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1747669c586SGleb Smirnoff 	if (error)
1750f6385e7SGleb Smirnoff 		goto out;
1760f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1770f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1780f6385e7SGleb Smirnoff 	if (tp == NULL) {
1797669c586SGleb Smirnoff 		error = ENOBUFS;
1800f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1810f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1820f6385e7SGleb Smirnoff 		goto out;
1830f6385e7SGleb Smirnoff 	}
1840f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
18500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ATTACH, error);
1860f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1870f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1882c37256eSGarrett Wollman out:
1895d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1900f6385e7SGleb Smirnoff 	return (error);
1912c37256eSGarrett Wollman }
1922c37256eSGarrett Wollman 
1932c37256eSGarrett Wollman /*
1943fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
195a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
196a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
197a152f8a3SRobert Watson  * inpcb state: time wait.
1982c37256eSGarrett Wollman  */
199bc725eafSRobert Watson static void
2003fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
2012c37256eSGarrett Wollman {
2023fed74e9SGleb Smirnoff 	struct inpcb *inp;
2032c37256eSGarrett Wollman 	struct tcpcb *tp;
2042c37256eSGarrett Wollman 
2053fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
2063fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
2073fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
2083fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
2093fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
210953b5606SRobert Watson 
211a152f8a3SRobert Watson 	tp = intotcpcb(inp);
212a152f8a3SRobert Watson 
2131b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
2141b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
2151b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
2169c3507f9SGleb Smirnoff 
217623dce13SRobert Watson 	tcp_discardcb(tp);
218623dce13SRobert Watson 	in_pcbdetach(inp);
2190206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
220623dce13SRobert Watson }
221c78cbc7bSRobert Watson 
222b287c6c7SBjoern A. Zeeb #ifdef INET
2232c37256eSGarrett Wollman /*
2242c37256eSGarrett Wollman  * Give the socket an address.
2252c37256eSGarrett Wollman  */
2262c37256eSGarrett Wollman static int
227b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2282c37256eSGarrett Wollman {
2292c37256eSGarrett Wollman 	int error = 0;
230f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
231c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2322c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2332c37256eSGarrett Wollman 
234c2399dd2SMichael Tuexen 	inp = sotoinpcb(so);
235c2399dd2SMichael Tuexen 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
236c2399dd2SMichael Tuexen 	INP_WLOCK(inp);
237c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
238c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
239c2399dd2SMichael Tuexen 		return (EINVAL);
240c2399dd2SMichael Tuexen 	}
241c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
242c2399dd2SMichael Tuexen 
24352710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
244f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
245f96603b5SMark Johnston 		/*
246f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
247f96603b5SMark Johnston 		 */
248f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2493f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
250c2399dd2SMichael Tuexen 		    sinp->sin_addr.s_addr != INADDR_ANY) {
251c2399dd2SMichael Tuexen 			error = EAFNOSUPPORT;
252c2399dd2SMichael Tuexen 			goto out;
253c2399dd2SMichael Tuexen 		}
254f96603b5SMark Johnston 		nam->sa_family = AF_INET;
255f96603b5SMark Johnston 	}
256c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sinp)) {
257c2399dd2SMichael Tuexen 		error = EINVAL;
258c2399dd2SMichael Tuexen 		goto out;
259c2399dd2SMichael Tuexen 	}
2602c37256eSGarrett Wollman 	/*
2612c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2622c37256eSGarrett Wollman 	 * to them.
2632c37256eSGarrett Wollman 	 */
264c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
265c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
2662c37256eSGarrett Wollman 		goto out;
267623dce13SRobert Watson 	}
268fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
26996871af0SGleb Smirnoff 	error = in_pcbbind(inp, sinp, td->td_ucred);
270fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
271623dce13SRobert Watson out:
27200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
2735d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2748501a69cSRobert Watson 	INP_WUNLOCK(inp);
275623dce13SRobert Watson 
276623dce13SRobert Watson 	return (error);
2772c37256eSGarrett Wollman }
278b287c6c7SBjoern A. Zeeb #endif /* INET */
2792c37256eSGarrett Wollman 
280fb59c426SYoshinobu Inoue #ifdef INET6
281fb59c426SYoshinobu Inoue static int
282b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
283fb59c426SYoshinobu Inoue {
284fb59c426SYoshinobu Inoue 	int error = 0;
285f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
286c2399dd2SMichael Tuexen 	struct tcpcb *tp;
2870ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2884a91aa8fSMichael Tuexen 	u_char vflagsav;
289fb59c426SYoshinobu Inoue 
290623dce13SRobert Watson 	inp = sotoinpcb(so);
291623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2928501a69cSRobert Watson 	INP_WLOCK(inp);
29353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
294c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
295c2399dd2SMichael Tuexen 		return (EINVAL);
296c2399dd2SMichael Tuexen 	}
297c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
298c2399dd2SMichael Tuexen 
299c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
300c2399dd2SMichael Tuexen 
301c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
302c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
303c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
304c2399dd2SMichael Tuexen 		goto out;
305c2399dd2SMichael Tuexen 	}
306c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof(*sin6)) {
307623dce13SRobert Watson 		error = EINVAL;
308623dce13SRobert Watson 		goto out;
309623dce13SRobert Watson 	}
310c2399dd2SMichael Tuexen 	/*
311c2399dd2SMichael Tuexen 	 * Must check for multicast addresses and disallow binding
312c2399dd2SMichael Tuexen 	 * to them.
313c2399dd2SMichael Tuexen 	 */
314c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
315c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
316c2399dd2SMichael Tuexen 		goto out;
317c2399dd2SMichael Tuexen 	}
318c2399dd2SMichael Tuexen 
319fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
320fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
321fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
322b287c6c7SBjoern A. Zeeb #ifdef INET
32366ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
3240ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
325fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3260ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
327fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
328fb59c426SYoshinobu Inoue 
3290ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
330888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
331888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
332888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
333888973f5SMichael Tuexen 				goto out;
334888973f5SMichael Tuexen 			}
335fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
336fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
33796871af0SGleb Smirnoff 			error = in_pcbbind(inp, &sin, td->td_ucred);
338fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
339fb59c426SYoshinobu Inoue 			goto out;
340fb59c426SYoshinobu Inoue 		}
341fb59c426SYoshinobu Inoue 	}
342b287c6c7SBjoern A. Zeeb #endif
34396871af0SGleb Smirnoff 	error = in6_pcbbind(inp, sin6, td->td_ucred);
344fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
345623dce13SRobert Watson out:
3464a91aa8fSMichael Tuexen 	if (error != 0)
3474a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
34800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_BIND, error);
3495d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3508501a69cSRobert Watson 	INP_WUNLOCK(inp);
351623dce13SRobert Watson 	return (error);
352fb59c426SYoshinobu Inoue }
353fb59c426SYoshinobu Inoue #endif /* INET6 */
354fb59c426SYoshinobu Inoue 
355b287c6c7SBjoern A. Zeeb #ifdef INET
3562c37256eSGarrett Wollman /*
3572c37256eSGarrett Wollman  * Prepare to accept connections.
3582c37256eSGarrett Wollman  */
3592c37256eSGarrett Wollman static int
360d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3612c37256eSGarrett Wollman {
3622c37256eSGarrett Wollman 	int error = 0;
363f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
364c2399dd2SMichael Tuexen 	struct tcpcb *tp;
3652c37256eSGarrett Wollman 
366623dce13SRobert Watson 	inp = sotoinpcb(so);
367623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3688501a69cSRobert Watson 	INP_WLOCK(inp);
36953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
370c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
371c2399dd2SMichael Tuexen 		return (EINVAL);
372623dce13SRobert Watson 	}
373623dce13SRobert Watson 	tp = intotcpcb(inp);
374c2399dd2SMichael Tuexen 
3750daccb9cSRobert Watson 	SOCK_LOCK(so);
3760daccb9cSRobert Watson 	error = solisten_proto_check(so);
377bd4a39ccSMark Johnston 	if (error != 0) {
378bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
379bd4a39ccSMark Johnston 		goto out;
380bd4a39ccSMark Johnston 	}
381bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
382fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
383bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
384fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
385bd4a39ccSMark Johnston 	}
3860daccb9cSRobert Watson 	if (error == 0) {
38757f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
388d374e81eSRobert Watson 		solisten_proto(so, backlog);
38909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
39037cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
39109fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
39209fe6320SNavdeep Parhar #endif
393bd4a39ccSMark Johnston 	} else {
394bd4a39ccSMark Johnston 		solisten_proto_abort(so);
3950daccb9cSRobert Watson 	}
3960daccb9cSRobert Watson 	SOCK_UNLOCK(so);
397623dce13SRobert Watson 
39868bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
399281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
40018a75309SPatrick Kelsey 
401623dce13SRobert Watson out:
40200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4035d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4048501a69cSRobert Watson 	INP_WUNLOCK(inp);
405623dce13SRobert Watson 	return (error);
4062c37256eSGarrett Wollman }
407b287c6c7SBjoern A. Zeeb #endif /* INET */
4082c37256eSGarrett Wollman 
409fb59c426SYoshinobu Inoue #ifdef INET6
410fb59c426SYoshinobu Inoue static int
411d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
412fb59c426SYoshinobu Inoue {
413fb59c426SYoshinobu Inoue 	int error = 0;
414f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
415c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4164a91aa8fSMichael Tuexen 	u_char vflagsav;
417fb59c426SYoshinobu Inoue 
418623dce13SRobert Watson 	inp = sotoinpcb(so);
419623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
4208501a69cSRobert Watson 	INP_WLOCK(inp);
42153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
422c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
423c2399dd2SMichael Tuexen 		return (EINVAL);
424623dce13SRobert Watson 	}
425623dce13SRobert Watson 	tp = intotcpcb(inp);
426c2399dd2SMichael Tuexen 
427c2399dd2SMichael Tuexen 	vflagsav = inp->inp_vflag;
428c2399dd2SMichael Tuexen 
4290daccb9cSRobert Watson 	SOCK_LOCK(so);
4300daccb9cSRobert Watson 	error = solisten_proto_check(so);
431bd4a39ccSMark Johnston 	if (error != 0) {
432bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
433bd4a39ccSMark Johnston 		goto out;
434bd4a39ccSMark Johnston 	}
435fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
436bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
437fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
43866ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
439fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
440bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
441fb59c426SYoshinobu Inoue 	}
442fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4430daccb9cSRobert Watson 	if (error == 0) {
44457f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
445d374e81eSRobert Watson 		solisten_proto(so, backlog);
44609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
44737cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
44809fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
44909fe6320SNavdeep Parhar #endif
450bd4a39ccSMark Johnston 	} else {
451bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4520daccb9cSRobert Watson 	}
4530daccb9cSRobert Watson 	SOCK_UNLOCK(so);
454623dce13SRobert Watson 
45568bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
456281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
45718a75309SPatrick Kelsey 
4584a91aa8fSMichael Tuexen 	if (error != 0)
4594a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4604a91aa8fSMichael Tuexen 
461623dce13SRobert Watson out:
46200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_LISTEN, error);
4635d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4648501a69cSRobert Watson 	INP_WUNLOCK(inp);
465623dce13SRobert Watson 	return (error);
466fb59c426SYoshinobu Inoue }
467fb59c426SYoshinobu Inoue #endif /* INET6 */
468fb59c426SYoshinobu Inoue 
469b287c6c7SBjoern A. Zeeb #ifdef INET
4702c37256eSGarrett Wollman /*
4712c37256eSGarrett Wollman  * Initiate connection to peer.
4722c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4732c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4742c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4752c37256eSGarrett Wollman  * Send initial segment on connection.
4762c37256eSGarrett Wollman  */
4772c37256eSGarrett Wollman static int
478b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4792c37256eSGarrett Wollman {
480109eb549SGleb Smirnoff 	struct epoch_tracker et;
4812c37256eSGarrett Wollman 	int error = 0;
482f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
483c2399dd2SMichael Tuexen 	struct tcpcb *tp;
4842c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4852c37256eSGarrett Wollman 
486623dce13SRobert Watson 	inp = sotoinpcb(so);
487623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4888501a69cSRobert Watson 	INP_WLOCK(inp);
489eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
490c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
491c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
492c2399dd2SMichael Tuexen 	}
493c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
494c2399dd2SMichael Tuexen 
495c2399dd2SMichael Tuexen 	sinp = (struct sockaddr_in *)nam;
496c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET) {
497c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
498623dce13SRobert Watson 		goto out;
499623dce13SRobert Watson 	}
500c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sinp)) {
501c2399dd2SMichael Tuexen 		error = EINVAL;
502c2399dd2SMichael Tuexen 		goto out;
503c2399dd2SMichael Tuexen 	}
504c2399dd2SMichael Tuexen 	/*
505c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
506c2399dd2SMichael Tuexen 	 */
507c2399dd2SMichael Tuexen 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
508c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
509c2399dd2SMichael Tuexen 		goto out;
510c2399dd2SMichael Tuexen 	}
511c2399dd2SMichael Tuexen 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
512c2399dd2SMichael Tuexen 		error = EACCES;
513c2399dd2SMichael Tuexen 		goto out;
514c2399dd2SMichael Tuexen 	}
515c2399dd2SMichael Tuexen 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
516c2399dd2SMichael Tuexen 		goto out;
517bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
518bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
519bd4a39ccSMark Johnston 		goto out;
520bd4a39ccSMark Johnston 	}
521c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
522a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
523c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
52509fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
52637cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
52709fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
528c1604fe4SGleb Smirnoff 		goto out_in_epoch;
52909fe6320SNavdeep Parhar #endif
53009fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
53140fa3e40SGleb Smirnoff 	error = tcp_output(tp);
5321d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
5331d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
534c1604fe4SGleb Smirnoff out_in_epoch:
535109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
536623dce13SRobert Watson out:
53700812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
538e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5398501a69cSRobert Watson 	INP_WUNLOCK(inp);
540623dce13SRobert Watson 	return (error);
5412c37256eSGarrett Wollman }
542b287c6c7SBjoern A. Zeeb #endif /* INET */
5432c37256eSGarrett Wollman 
544fb59c426SYoshinobu Inoue #ifdef INET6
545fb59c426SYoshinobu Inoue static int
546b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
547fb59c426SYoshinobu Inoue {
548109eb549SGleb Smirnoff 	struct epoch_tracker et;
549fb59c426SYoshinobu Inoue 	int error = 0;
550f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
551c2399dd2SMichael Tuexen 	struct tcpcb *tp;
5520ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5534a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5544a91aa8fSMichael Tuexen 	u_char vflagsav;
555623dce13SRobert Watson 
556623dce13SRobert Watson 	inp = sotoinpcb(so);
557623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5588501a69cSRobert Watson 	INP_WLOCK(inp);
559c2399dd2SMichael Tuexen 	if (inp->inp_flags & INP_DROPPED) {
560c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
561c2399dd2SMichael Tuexen 		return (ECONNREFUSED);
562c2399dd2SMichael Tuexen 	}
563c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
564c2399dd2SMichael Tuexen 
5654a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5664a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
567c2399dd2SMichael Tuexen 
568c2399dd2SMichael Tuexen 	sin6 = (struct sockaddr_in6 *)nam;
569c2399dd2SMichael Tuexen 	if (nam->sa_family != AF_INET6) {
570c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
571c2399dd2SMichael Tuexen 		goto out;
572c2399dd2SMichael Tuexen 	}
573c2399dd2SMichael Tuexen 	if (nam->sa_len != sizeof (*sin6)) {
574c2399dd2SMichael Tuexen 		error = EINVAL;
575c2399dd2SMichael Tuexen 		goto out;
576c2399dd2SMichael Tuexen 	}
577c2399dd2SMichael Tuexen 	/*
578c2399dd2SMichael Tuexen 	 * Must disallow TCP ``connections'' to multicast addresses.
579c2399dd2SMichael Tuexen 	 */
580c2399dd2SMichael Tuexen 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
581c2399dd2SMichael Tuexen 		error = EAFNOSUPPORT;
582623dce13SRobert Watson 		goto out;
583623dce13SRobert Watson 	}
584bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
585bd4a39ccSMark Johnston 		error = EINVAL;
586bd4a39ccSMark Johnston 		goto out;
587bd4a39ccSMark Johnston 	}
588b287c6c7SBjoern A. Zeeb #ifdef INET
589fa046d87SRobert Watson 	/*
590fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
591fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
592fa046d87SRobert Watson 	 * Is this a significant problem?
593fa046d87SRobert Watson 	 */
5940ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
595fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
596fb59c426SYoshinobu Inoue 
597d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
598d46a5312SMaxim Konovalov 			error = EINVAL;
599d46a5312SMaxim Konovalov 			goto out;
600d46a5312SMaxim Konovalov 		}
6015dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
6025dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6035dba6adaSMichael Tuexen 			goto out;
6045dba6adaSMichael Tuexen 		}
60533841545SHajimu UMEMOTO 
6060ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
607888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
608888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
609888973f5SMichael Tuexen 			goto out;
610888973f5SMichael Tuexen 		}
611f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
612f903a308SMichael Tuexen 			error = EACCES;
6132cf21ae5SRandall Stewart 			goto out;
6142cf21ae5SRandall Stewart 		}
615b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
616b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
617413628a7SBjoern A. Zeeb 			goto out;
6184a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
6194a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
620c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
621a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
622c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
62409fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
625adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
62609fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
627c1604fe4SGleb Smirnoff 			goto out_in_epoch;
62809fe6320SNavdeep Parhar #endif
62940fa3e40SGleb Smirnoff 		error = tcp_output(tp);
630c1604fe4SGleb Smirnoff 		goto out_in_epoch;
6315dba6adaSMichael Tuexen 	} else {
6325dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
6335dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
6345dba6adaSMichael Tuexen 			goto out;
6355dba6adaSMichael Tuexen 		}
636fb59c426SYoshinobu Inoue 	}
637b287c6c7SBjoern A. Zeeb #endif
6384a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
6394a91aa8fSMichael Tuexen 		goto out;
640fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
641fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
642dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
6430773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
644a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6450773b44eSGleb Smirnoff 		goto out_in_epoch;
64609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
64709fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
648adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
64909fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6500773b44eSGleb Smirnoff 		goto out_in_epoch;
65109fe6320SNavdeep Parhar #endif
65209fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
65340fa3e40SGleb Smirnoff 	error = tcp_output(tp);
654c1604fe4SGleb Smirnoff out_in_epoch:
655109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
656623dce13SRobert Watson out:
6571d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6581d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6594a91aa8fSMichael Tuexen 	/*
6604a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6614a91aa8fSMichael Tuexen 	 * the flags we modified.
6624a91aa8fSMichael Tuexen 	 */
6634a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6644a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6654a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6664a91aa8fSMichael Tuexen 	}
6674a91aa8fSMichael Tuexen 
66800812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_CONNECT, error);
6695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6708501a69cSRobert Watson 	INP_WUNLOCK(inp);
671623dce13SRobert Watson 	return (error);
672fb59c426SYoshinobu Inoue }
673fb59c426SYoshinobu Inoue #endif /* INET6 */
674fb59c426SYoshinobu Inoue 
6752c37256eSGarrett Wollman /*
6762c37256eSGarrett Wollman  * Initiate disconnect from peer.
6772c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6782c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6792c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6802c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6812c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6822c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6832c37256eSGarrett Wollman  *
6842c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6852c37256eSGarrett Wollman  */
6862c37256eSGarrett Wollman static int
6872c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6882c37256eSGarrett Wollman {
689f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
690623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6916573d758SMatt Macy 	struct epoch_tracker et;
692623dce13SRobert Watson 	int error = 0;
6932c37256eSGarrett Wollman 
69497a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
695623dce13SRobert Watson 	inp = sotoinpcb(so);
696623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6978501a69cSRobert Watson 	INP_WLOCK(inp);
698489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
699c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
700c2399dd2SMichael Tuexen 		NET_EPOCH_EXIT(et);
701c2399dd2SMichael Tuexen 		return (ECONNRESET);
702623dce13SRobert Watson 	}
703623dce13SRobert Watson 	tp = intotcpcb(inp);
704c2399dd2SMichael Tuexen 
705cda6bdbaSJohn Baldwin 	if (tp->t_state == TCPS_TIME_WAIT)
706cda6bdbaSJohn Baldwin 		goto out;
707623dce13SRobert Watson 	tcp_disconnect(tp);
708623dce13SRobert Watson out:
70900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_DISCONNECT, error);
7105d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
7118501a69cSRobert Watson 	INP_WUNLOCK(inp);
71297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
713623dce13SRobert Watson 	return (error);
7142c37256eSGarrett Wollman }
7152c37256eSGarrett Wollman 
716b287c6c7SBjoern A. Zeeb #ifdef INET
7172c37256eSGarrett Wollman /*
7188296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
7198296cddfSRobert Watson  * just return the address of the peer, storing through addr.
7202c37256eSGarrett Wollman  */
7212c37256eSGarrett Wollman static int
72257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
7232c37256eSGarrett Wollman {
7242c37256eSGarrett Wollman 	int error = 0;
725c2399dd2SMichael Tuexen 	struct inpcb *inp;
726c2399dd2SMichael Tuexen 	struct tcpcb *tp;
72726ef6ac4SDon Lewis 	struct in_addr addr;
72826ef6ac4SDon Lewis 	in_port_t port = 0;
7292c37256eSGarrett Wollman 
730f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
731623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
7328501a69cSRobert Watson 	INP_WLOCK(inp);
73353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
734c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
735c2399dd2SMichael Tuexen 		return (ECONNABORTED);
736623dce13SRobert Watson 	}
7371db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
738f76fcf6dSJeffrey Hsu 
739c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
740c2399dd2SMichael Tuexen 		error = ECONNABORTED;
741c2399dd2SMichael Tuexen 		goto out;
742c2399dd2SMichael Tuexen 	}
743f76fcf6dSJeffrey Hsu 	/*
74454d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
74526ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
74626ef6ac4SDon Lewis 	 * release the lock.
747f76fcf6dSJeffrey Hsu 	 */
74826ef6ac4SDon Lewis 	port = inp->inp_fport;
74926ef6ac4SDon Lewis 	addr = inp->inp_faddr;
750f76fcf6dSJeffrey Hsu 
751623dce13SRobert Watson out:
75200812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
7535d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7548501a69cSRobert Watson 	INP_WUNLOCK(inp);
75526ef6ac4SDon Lewis 	if (error == 0)
75626ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
75726ef6ac4SDon Lewis 	return error;
7582c37256eSGarrett Wollman }
759b287c6c7SBjoern A. Zeeb #endif /* INET */
7602c37256eSGarrett Wollman 
761fb59c426SYoshinobu Inoue #ifdef INET6
762fb59c426SYoshinobu Inoue static int
763fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
764fb59c426SYoshinobu Inoue {
765c2399dd2SMichael Tuexen 	struct inpcb *inp;
766fb59c426SYoshinobu Inoue 	int error = 0;
767c2399dd2SMichael Tuexen 	struct tcpcb *tp;
76826ef6ac4SDon Lewis 	struct in_addr addr;
76926ef6ac4SDon Lewis 	struct in6_addr addr6;
7706573d758SMatt Macy 	struct epoch_tracker et;
77126ef6ac4SDon Lewis 	in_port_t port = 0;
77226ef6ac4SDon Lewis 	int v4 = 0;
773fb59c426SYoshinobu Inoue 
774f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
775623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
776c2399dd2SMichael Tuexen 	NET_EPOCH_ENTER(et); /* XXXMT Why is this needed? */
7778501a69cSRobert Watson 	INP_WLOCK(inp);
77853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
779c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
780c2399dd2SMichael Tuexen 		NET_EPOCH_EXIT(et);
781c2399dd2SMichael Tuexen 		return (ECONNABORTED);
782623dce13SRobert Watson 	}
7831db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
784623dce13SRobert Watson 
785c2399dd2SMichael Tuexen 	if (so->so_state & SS_ISDISCONNECTED) {
786c2399dd2SMichael Tuexen 		error = ECONNABORTED;
787c2399dd2SMichael Tuexen 		goto out;
788c2399dd2SMichael Tuexen 	}
78926ef6ac4SDon Lewis 	/*
79026ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
79126ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
79226ef6ac4SDon Lewis 	 * release the lock.
79326ef6ac4SDon Lewis 	 */
79426ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
79526ef6ac4SDon Lewis 		v4 = 1;
79626ef6ac4SDon Lewis 		port = inp->inp_fport;
79726ef6ac4SDon Lewis 		addr = inp->inp_faddr;
79826ef6ac4SDon Lewis 	} else {
79926ef6ac4SDon Lewis 		port = inp->inp_fport;
80026ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
80126ef6ac4SDon Lewis 	}
80226ef6ac4SDon Lewis 
803623dce13SRobert Watson out:
80400812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_ACCEPT, error);
8055d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
8068501a69cSRobert Watson 	INP_WUNLOCK(inp);
80797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
80826ef6ac4SDon Lewis 	if (error == 0) {
80926ef6ac4SDon Lewis 		if (v4)
81026ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
81126ef6ac4SDon Lewis 		else
81226ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
81326ef6ac4SDon Lewis 	}
81426ef6ac4SDon Lewis 	return error;
815fb59c426SYoshinobu Inoue }
816fb59c426SYoshinobu Inoue #endif /* INET6 */
817f76fcf6dSJeffrey Hsu 
818f76fcf6dSJeffrey Hsu /*
8192c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
8202c37256eSGarrett Wollman  */
8212c37256eSGarrett Wollman static int
8222c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
8232c37256eSGarrett Wollman {
8242c37256eSGarrett Wollman 	int error = 0;
825f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
826c2399dd2SMichael Tuexen 	struct tcpcb *tp;
8276573d758SMatt Macy 	struct epoch_tracker et;
8282c37256eSGarrett Wollman 
829623dce13SRobert Watson 	inp = sotoinpcb(so);
830623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
8318501a69cSRobert Watson 	INP_WLOCK(inp);
83253af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
8330af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
8340af4ce45SGleb Smirnoff 		return (ECONNRESET);
835623dce13SRobert Watson 	}
8360af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
837c2399dd2SMichael Tuexen 
8380af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8392c37256eSGarrett Wollman 	socantsendmore(so);
840623dce13SRobert Watson 	tcp_usrclosed(tp);
841ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
842f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
84300812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_SHUTDOWN, error);
8445d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
845f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
84697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
847623dce13SRobert Watson 
848623dce13SRobert Watson 	return (error);
8492c37256eSGarrett Wollman }
8502c37256eSGarrett Wollman 
8512c37256eSGarrett Wollman /*
8522c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8532c37256eSGarrett Wollman  */
8542c37256eSGarrett Wollman static int
8552c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8562c37256eSGarrett Wollman {
857109eb549SGleb Smirnoff 	struct epoch_tracker et;
858f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
859c2399dd2SMichael Tuexen 	struct tcpcb *tp;
860f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8612c37256eSGarrett Wollman 
862623dce13SRobert Watson 	inp = sotoinpcb(so);
863623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8648501a69cSRobert Watson 	INP_WLOCK(inp);
86553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
86637a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
86737a7f557SGleb Smirnoff 		return (ECONNRESET);
868623dce13SRobert Watson 	}
86937a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
870c2399dd2SMichael Tuexen 
87137a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
872281a0fd4SPatrick Kelsey 	/*
873281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
874281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
875281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
876281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
877281a0fd4SPatrick Kelsey 	 * expires.
878281a0fd4SPatrick Kelsey 	 */
87968bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
880281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
881281a0fd4SPatrick Kelsey 		goto out;
88209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
88309fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
88409fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
885460cf046SNavdeep Parhar 	else
88609fe6320SNavdeep Parhar #endif
887f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
888623dce13SRobert Watson out:
88900812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVD, error);
8905d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
891f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
892f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
893623dce13SRobert Watson 	return (error);
8942c37256eSGarrett Wollman }
8952c37256eSGarrett Wollman 
8962c37256eSGarrett Wollman /*
8972c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8989c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8999c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
9009c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
9019c9906e9SPeter Wemm  * generally are caller-frees.
9022c37256eSGarrett Wollman  */
9032c37256eSGarrett Wollman static int
90457bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
905b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
9062c37256eSGarrett Wollman {
90797a95ee1SGleb Smirnoff 	struct epoch_tracker et;
9082c37256eSGarrett Wollman 	int error = 0;
909f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
910c2399dd2SMichael Tuexen 	struct tcpcb *tp;
911888973f5SMichael Tuexen #ifdef INET
91251e08d53SMichael Tuexen #ifdef INET6
91351e08d53SMichael Tuexen 	struct sockaddr_in sin;
91451e08d53SMichael Tuexen #endif
91551e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
916888973f5SMichael Tuexen #endif
917fb59c426SYoshinobu Inoue #ifdef INET6
918a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
919fb59c426SYoshinobu Inoue 	int isipv6;
920fb59c426SYoshinobu Inoue #endif
9214a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
9224a91aa8fSMichael Tuexen 	u_char vflagsav;
9234a91aa8fSMichael Tuexen 	bool restoreflags;
9242c37256eSGarrett Wollman 
9254287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
9264287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
9274287aa56SGleb Smirnoff 	INP_WLOCK(inp);
92853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
9294287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
9304287aa56SGleb Smirnoff 			m_freem(m);
9314287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
9324287aa56SGleb Smirnoff 		return (ECONNRESET);
9334287aa56SGleb Smirnoff 	}
934c2399dd2SMichael Tuexen 	tp = intotcpcb(inp);
9354287aa56SGleb Smirnoff 
9364287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
9374287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
9384287aa56SGleb Smirnoff 	restoreflags = false;
9394287aa56SGleb Smirnoff 
9404287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
941c2399dd2SMichael Tuexen 	if (control != NULL) {
942c2399dd2SMichael Tuexen 		/* TCP doesn't do control messages (rights, creds, etc) */
943c2399dd2SMichael Tuexen 		if (control->m_len > 0) {
944c2399dd2SMichael Tuexen 			m_freem(control);
945c2399dd2SMichael Tuexen 			error = EINVAL;
946c2399dd2SMichael Tuexen 			goto out;
947c2399dd2SMichael Tuexen 		}
948c2399dd2SMichael Tuexen 		m_freem(control);	/* empty control, just free it */
949c2399dd2SMichael Tuexen 	}
950c2399dd2SMichael Tuexen 
9517d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9527d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
953d3b6c96bSRandall Stewart 		goto out;
9547d2608a5SMark Johnston 
955888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
956bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
957bd4a39ccSMark Johnston 			error = EINVAL;
958bd4a39ccSMark Johnston 			goto out;
959bd4a39ccSMark Johnston 		}
960888973f5SMichael Tuexen 		switch (nam->sa_family) {
961888973f5SMichael Tuexen #ifdef INET
962888973f5SMichael Tuexen 		case AF_INET:
963888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
964888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
965888973f5SMichael Tuexen 				error = EINVAL;
966888973f5SMichael Tuexen 				goto out;
967888973f5SMichael Tuexen 			}
968888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
969888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
970888973f5SMichael Tuexen 				goto out;
971888973f5SMichael Tuexen 			}
972888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
973888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
974888973f5SMichael Tuexen 				goto out;
975888973f5SMichael Tuexen 			}
976f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
977f903a308SMichael Tuexen 				error = EACCES;
9782cf21ae5SRandall Stewart 				goto out;
9792cf21ae5SRandall Stewart 			}
980888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9817d2608a5SMark Johnston 			    &sinp->sin_addr)))
982888973f5SMichael Tuexen 				goto out;
983888973f5SMichael Tuexen #ifdef INET6
984888973f5SMichael Tuexen 			isipv6 = 0;
985888973f5SMichael Tuexen #endif
986888973f5SMichael Tuexen 			break;
987888973f5SMichael Tuexen #endif /* INET */
988888973f5SMichael Tuexen #ifdef INET6
989888973f5SMichael Tuexen 		case AF_INET6:
9900ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
9910ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
992888973f5SMichael Tuexen 				error = EINVAL;
993888973f5SMichael Tuexen 				goto out;
994888973f5SMichael Tuexen 			}
995e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
996e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
997e240ce42SMichael Tuexen 				goto out;
998e240ce42SMichael Tuexen 			}
9990ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
1000888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1001888973f5SMichael Tuexen 				goto out;
1002888973f5SMichael Tuexen 			}
10030ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1004888973f5SMichael Tuexen #ifdef INET
1005888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1006888973f5SMichael Tuexen 					error = EINVAL;
1007888973f5SMichael Tuexen 					goto out;
1008888973f5SMichael Tuexen 				}
1009888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
1010888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1011888973f5SMichael Tuexen 					goto out;
1012888973f5SMichael Tuexen 				}
10134a91aa8fSMichael Tuexen 				restoreflags = true;
1014888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
1015888973f5SMichael Tuexen 				sinp = &sin;
10160ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
1017888973f5SMichael Tuexen 				if (IN_MULTICAST(
1018888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
1019888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1020888973f5SMichael Tuexen 					goto out;
1021888973f5SMichael Tuexen 				}
1022888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
10237d2608a5SMark Johnston 				    &sinp->sin_addr)))
1024888973f5SMichael Tuexen 					goto out;
1025888973f5SMichael Tuexen 				isipv6 = 0;
1026888973f5SMichael Tuexen #else /* !INET */
1027888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
1028888973f5SMichael Tuexen 				goto out;
1029888973f5SMichael Tuexen #endif /* INET */
1030888973f5SMichael Tuexen 			} else {
1031888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
1032888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
1033888973f5SMichael Tuexen 					goto out;
1034888973f5SMichael Tuexen 				}
10354a91aa8fSMichael Tuexen 				restoreflags = true;
1036888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
1037888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
1038888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
10397d2608a5SMark Johnston 				    &sin6->sin6_addr)))
1040888973f5SMichael Tuexen 					goto out;
1041888973f5SMichael Tuexen 				isipv6 = 1;
1042888973f5SMichael Tuexen 			}
1043888973f5SMichael Tuexen 			break;
1044888973f5SMichael Tuexen #endif /* INET6 */
1045888973f5SMichael Tuexen 		default:
1046888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
1047888973f5SMichael Tuexen 			goto out;
1048888973f5SMichael Tuexen 		}
1049888973f5SMichael Tuexen 	}
10502c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
105108af8aacSRandall Stewart 		if (tp->t_acktime == 0)
105208af8aacSRandall Stewart 			tp->t_acktime = ticks;
1053651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10547d2608a5SMark Johnston 		m = NULL;
10552c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1056bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1057bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1058bd4a39ccSMark Johnston 
10592c37256eSGarrett Wollman 			/*
10602c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10612c37256eSGarrett Wollman 			 * initialize window to default value, and
10620c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10632c37256eSGarrett Wollman 			 */
1064fb59c426SYoshinobu Inoue #ifdef INET6
1065fb59c426SYoshinobu Inoue 			if (isipv6)
1066a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1067fb59c426SYoshinobu Inoue #endif /* INET6 */
1068b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1069b287c6c7SBjoern A. Zeeb 			else
1070b287c6c7SBjoern A. Zeeb #endif
1071b287c6c7SBjoern A. Zeeb #ifdef INET
1072a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1073b287c6c7SBjoern A. Zeeb #endif
10744a91aa8fSMichael Tuexen 			/*
10754a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10764a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10774a91aa8fSMichael Tuexen 			 * operations fail.
10784a91aa8fSMichael Tuexen 			 */
10794a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10804a91aa8fSMichael Tuexen 				restoreflags = false;
10814a91aa8fSMichael Tuexen 
10827d2608a5SMark Johnston 			if (error) {
10837d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10847d2608a5SMark Johnston 				sbflush(&so->so_snd);
10852c37256eSGarrett Wollman 				goto out;
10867d2608a5SMark Johnston 			}
1087c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1088c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
108918a75309SPatrick Kelsey 			else {
10902c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10912c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10922c37256eSGarrett Wollman 			}
1093c560df6fSPatrick Kelsey 		}
10942c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
10952c37256eSGarrett Wollman 			/*
10962c37256eSGarrett Wollman 			 * Close the send side of the connection after
10972c37256eSGarrett Wollman 			 * the data is sent.
10982c37256eSGarrett Wollman 			 */
10992c37256eSGarrett Wollman 			socantsendmore(so);
1100623dce13SRobert Watson 			tcp_usrclosed(tp);
11012c37256eSGarrett Wollman 		}
1102e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1103e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1104e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1105e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1106e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1107e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1108e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1109e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1110e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1111e854dd38SRandall Stewart 		}
11122cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
11132cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1114b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1115b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1116f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1117b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1118b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1119b0acefa8SBill Fenner 		}
11202c37256eSGarrett Wollman 	} else {
1121623dce13SRobert Watson 		/*
1122623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1123623dce13SRobert Watson 		 */
1124d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
11252c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1126d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
11272c37256eSGarrett Wollman 			error = ENOBUFS;
11282c37256eSGarrett Wollman 			goto out;
11292c37256eSGarrett Wollman 		}
11302c37256eSGarrett Wollman 		/*
11312c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
11322c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
11332c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
11342c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
11352c37256eSGarrett Wollman 		 * of data past the urgent section.
11362c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
11372c37256eSGarrett Wollman 		 */
113808af8aacSRandall Stewart 		if (tp->t_acktime == 0)
113908af8aacSRandall Stewart 			tp->t_acktime = ticks;
1140651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1141d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
11427d2608a5SMark Johnston 		m = NULL;
1143ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1144ef53690bSGarrett Wollman 			/*
1145ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1146ef53690bSGarrett Wollman 			 * initialize window to default value, and
11470c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1148ef53690bSGarrett Wollman 			 */
114918a75309SPatrick Kelsey 
1150c560df6fSPatrick Kelsey 			/*
1151c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1152c560df6fSPatrick Kelsey 			 */
1153c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1154c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1155fb59c426SYoshinobu Inoue #ifdef INET6
1156fb59c426SYoshinobu Inoue 			if (isipv6)
1157a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1158fb59c426SYoshinobu Inoue #endif /* INET6 */
1159b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1160b287c6c7SBjoern A. Zeeb 			else
1161b287c6c7SBjoern A. Zeeb #endif
1162b287c6c7SBjoern A. Zeeb #ifdef INET
1163a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1164b287c6c7SBjoern A. Zeeb #endif
11654a91aa8fSMichael Tuexen 			/*
11664a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11674a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11684a91aa8fSMichael Tuexen 			 * operations fail.
11694a91aa8fSMichael Tuexen 			 */
11704a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11714a91aa8fSMichael Tuexen 				restoreflags = false;
11724a91aa8fSMichael Tuexen 
11737d2608a5SMark Johnston 			if (error != 0) {
11747d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11757d2608a5SMark Johnston 				sbflush(&so->so_snd);
1176ef53690bSGarrett Wollman 				goto out;
11777d2608a5SMark Johnston 			}
1178ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1179ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1180623dce13SRobert Watson 		}
1181300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11827d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11832cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1184f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11852cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11862c37256eSGarrett Wollman 		}
11872cbcd3c1SGleb Smirnoff 	}
11882529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11892529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11902529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11912529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11922529f56eSJonathan T. Looney 	    0, NULL, false);
11937d2608a5SMark Johnston 
1194d1401c90SRobert Watson out:
11954a91aa8fSMichael Tuexen 	/*
11967d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
11977d2608a5SMark Johnston 	 * responsible for freeing memory.
11987d2608a5SMark Johnston 	 */
11997d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
12007d2608a5SMark Johnston 		m_freem(m);
12017d2608a5SMark Johnston 
12027d2608a5SMark Johnston 	/*
12034a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
12044a91aa8fSMichael Tuexen 	 * restore the original flags.
12054a91aa8fSMichael Tuexen 	 */
12064a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
12074a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
12084a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
12094a91aa8fSMichael Tuexen 	}
121000812bbdSMichael Tuexen 	tcp_bblog_pru(tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
121100812bbdSMichael Tuexen 		      ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND), error);
12125d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
12135d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1214f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
121597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
121673fddedaSPeter Grehan 	return (error);
12172c37256eSGarrett Wollman }
12182c37256eSGarrett Wollman 
12192cbcd3c1SGleb Smirnoff static int
12202cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
12212cbcd3c1SGleb Smirnoff {
1222109eb549SGleb Smirnoff 	struct epoch_tracker et;
12232cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
12242cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
12252cbcd3c1SGleb Smirnoff 	int error;
12262cbcd3c1SGleb Smirnoff 
12272cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
12282cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
122953af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
12302cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
123182334850SJohn Baldwin 		mb_free_notready(m, count);
12322cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
12332cbcd3c1SGleb Smirnoff 	}
12342cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
12352cbcd3c1SGleb Smirnoff 
12362cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
12372cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
12382cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1239f64dc2abSGleb Smirnoff 	if (error) {
12402cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1241f64dc2abSGleb Smirnoff 		return (error);
1242f64dc2abSGleb Smirnoff 	}
1243f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1244f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1245f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
12462cbcd3c1SGleb Smirnoff 
12472cbcd3c1SGleb Smirnoff 	return (error);
12482cbcd3c1SGleb Smirnoff }
12492cbcd3c1SGleb Smirnoff 
12502c37256eSGarrett Wollman /*
1251a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
12522c37256eSGarrett Wollman  */
1253ac45e92fSRobert Watson static void
12542c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12552c37256eSGarrett Wollman {
1256f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1257c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12586573d758SMatt Macy 	struct epoch_tracker et;
1259c78cbc7bSRobert Watson 
1260ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1261c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1262c78cbc7bSRobert Watson 
126397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12648501a69cSRobert Watson 	INP_WLOCK(inp);
1265c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1266c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1267c78cbc7bSRobert Watson 
1268c78cbc7bSRobert Watson 	/*
1269a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1270c78cbc7bSRobert Watson 	 */
127153af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1272c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12738fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12748fa799bdSJonathan T. Looney 		if (tp == NULL)
12758fa799bdSJonathan T. Looney 			goto dropped;
127600812bbdSMichael Tuexen 		tcp_bblog_pru(tp, PRU_ABORT, 0);
12775d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1278c78cbc7bSRobert Watson 	}
1279ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1280d8596171SGleb Smirnoff 		soref(so);
1281ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1282a152f8a3SRobert Watson 	}
12838501a69cSRobert Watson 	INP_WUNLOCK(inp);
12848fa799bdSJonathan T. Looney dropped:
128597a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1286a152f8a3SRobert Watson }
1287a152f8a3SRobert Watson 
1288a152f8a3SRobert Watson /*
1289a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1290a152f8a3SRobert Watson  */
1291a152f8a3SRobert Watson static void
1292a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1293a152f8a3SRobert Watson {
1294a152f8a3SRobert Watson 	struct inpcb *inp;
1295c2399dd2SMichael Tuexen 	struct tcpcb *tp;
12966573d758SMatt Macy 	struct epoch_tracker et;
1297a152f8a3SRobert Watson 
1298a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1299a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1300a152f8a3SRobert Watson 
130197a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
13028501a69cSRobert Watson 	INP_WLOCK(inp);
1303a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1304a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1305a152f8a3SRobert Watson 
1306a152f8a3SRobert Watson 	/*
1307cda6bdbaSJohn Baldwin 	 * If we are still connected and we're not dropped, initiate
1308a152f8a3SRobert Watson 	 * a disconnect.
1309a152f8a3SRobert Watson 	 */
131053af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1311a152f8a3SRobert Watson 		tp = intotcpcb(inp);
1312cda6bdbaSJohn Baldwin 		if (tp->t_state != TCPS_TIME_WAIT) {
131374703901SGleb Smirnoff 			tp->t_flags |= TF_CLOSED;
1314a152f8a3SRobert Watson 			tcp_disconnect(tp);
131500812bbdSMichael Tuexen 			tcp_bblog_pru(tp, PRU_CLOSE, 0);
13165d06879aSGeorge V. Neville-Neil 			TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1317a152f8a3SRobert Watson 		}
1318cda6bdbaSJohn Baldwin 	}
1319ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1320d8596171SGleb Smirnoff 		soref(so);
1321ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1322a152f8a3SRobert Watson 	}
13238501a69cSRobert Watson 	INP_WUNLOCK(inp);
132497a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
13252c37256eSGarrett Wollman }
13262c37256eSGarrett Wollman 
1327d3b6c96bSRandall Stewart static int
1328d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1329d3b6c96bSRandall Stewart {
1330d3b6c96bSRandall Stewart 	/*
1331d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1332d3b6c96bSRandall Stewart 	 * specified then it does not always support
1333d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1334d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1335d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1336d3b6c96bSRandall Stewart 	 */
1337d3b6c96bSRandall Stewart 	int ret = 0;
1338d3b6c96bSRandall Stewart 
1339d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1340d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1341d3b6c96bSRandall Stewart 	}
1342d3b6c96bSRandall Stewart 	return (ret);
1343d3b6c96bSRandall Stewart }
1344d3b6c96bSRandall Stewart 
13452c37256eSGarrett Wollman /*
13462c37256eSGarrett Wollman  * Receive out-of-band data.
13472c37256eSGarrett Wollman  */
13482c37256eSGarrett Wollman static int
13492c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
13502c37256eSGarrett Wollman {
13512c37256eSGarrett Wollman 	int error = 0;
1352f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1353c2399dd2SMichael Tuexen 	struct tcpcb *tp;
13542c37256eSGarrett Wollman 
1355623dce13SRobert Watson 	inp = sotoinpcb(so);
1356623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
13578501a69cSRobert Watson 	INP_WLOCK(inp);
135853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1359c2399dd2SMichael Tuexen 		INP_WUNLOCK(inp);
1360c2399dd2SMichael Tuexen 		return (ECONNRESET);
1361623dce13SRobert Watson 	}
1362623dce13SRobert Watson 	tp = intotcpcb(inp);
1363c2399dd2SMichael Tuexen 
1364d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1365d3b6c96bSRandall Stewart 	if (error) {
1366d3b6c96bSRandall Stewart 		goto out;
1367d3b6c96bSRandall Stewart 	}
13682c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1369c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13704cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13714cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13722c37256eSGarrett Wollman 		error = EINVAL;
13732c37256eSGarrett Wollman 		goto out;
13742c37256eSGarrett Wollman 	}
13752c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13762c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13772c37256eSGarrett Wollman 		goto out;
13782c37256eSGarrett Wollman 	}
13792c37256eSGarrett Wollman 	m->m_len = 1;
13802c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13812c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13822c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1383623dce13SRobert Watson 
1384623dce13SRobert Watson out:
138500812bbdSMichael Tuexen 	tcp_bblog_pru(tp, PRU_RCVOOB, error);
13865d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13878501a69cSRobert Watson 	INP_WUNLOCK(inp);
1388623dce13SRobert Watson 	return (error);
13892c37256eSGarrett Wollman }
13902c37256eSGarrett Wollman 
1391b287c6c7SBjoern A. Zeeb #ifdef INET
1392e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1393e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1394e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1395e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1396e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1397e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1398e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1399e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1400e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1401e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1402e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1403e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1404e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1405e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1406e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1407e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1408e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1409e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1410e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1411e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1412e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1413e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1414e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1415e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
14162c37256eSGarrett Wollman };
1417b287c6c7SBjoern A. Zeeb #endif /* INET */
1418df8bae1dSRodney W. Grimes 
1419fb59c426SYoshinobu Inoue #ifdef INET6
1420e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1421e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1422e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1423e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1424e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1425e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1426e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1427e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1428e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1429e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1430e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1431e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1432e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1433e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1434e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1435e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1436e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1437e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1438e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1439e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1440e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1441e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1442e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1443e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1444fb59c426SYoshinobu Inoue };
1445fb59c426SYoshinobu Inoue #endif /* INET6 */
1446fb59c426SYoshinobu Inoue 
1447b287c6c7SBjoern A. Zeeb #ifdef INET
1448a0292f23SGarrett Wollman /*
1449a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1450dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1451dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1452dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1453a0292f23SGarrett Wollman  */
14540312fbe9SPoul-Henning Kamp static int
1455a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1456a0292f23SGarrett Wollman {
14579e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
14589eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1459c3229e05SDavid Greenman 	int error;
1460a0292f23SGarrett Wollman 
1461c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14628501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
146376f1499fSGleb Smirnoff 
1464636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1465de0a2eb2SMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED | SS_ISDISCONNECTING |
1466de0a2eb2SMark Johnston 	    SS_ISDISCONNECTED)) != 0))
1467636b19eaSMark Johnston 		return (EISCONN);
1468636b19eaSMark Johnston 
1469fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1470dfc4d218SGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred, true);
14719e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1472dfc4d218SGleb Smirnoff 	if (error != 0)
14739e46ff4dSGleb Smirnoff 		return (error);
1474a0292f23SGarrett Wollman 
1475087b55eaSAndre Oppermann 	/*
1476087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1477087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1478087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1479087b55eaSAndre Oppermann 	 */
1480a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14819b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1482a0292f23SGarrett Wollman 		tp->request_r_scale++;
1483a0292f23SGarrett Wollman 
1484a0292f23SGarrett Wollman 	soisconnecting(so);
148578b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
148657f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14878e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14888e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14898e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1490a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1491a45d2726SAndras Olah 
14929e46ff4dSGleb Smirnoff 	return (0);
1493a0292f23SGarrett Wollman }
1494b287c6c7SBjoern A. Zeeb #endif /* INET */
1495a0292f23SGarrett Wollman 
1496fb59c426SYoshinobu Inoue #ifdef INET6
1497fb59c426SYoshinobu Inoue static int
1498a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1499fb59c426SYoshinobu Inoue {
15009eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1501636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1502fb59c426SYoshinobu Inoue 	int error;
1503fb59c426SYoshinobu Inoue 
1504775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
15058501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1506623dce13SRobert Watson 
1507636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1508636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1509636b19eaSMark Johnston 		return (EISCONN);
1510636b19eaSMark Johnston 
151176f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1512a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1513fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
151476f1499fSGleb Smirnoff 	if (error != 0)
151576f1499fSGleb Smirnoff 		return (error);
1516fb59c426SYoshinobu Inoue 
1517fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1518fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1519970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1520fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1521fb59c426SYoshinobu Inoue 
1522636b19eaSMark Johnston 	soisconnecting(so);
152378b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
152457f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
15258e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
15268e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
15278e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1528fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1529fb59c426SYoshinobu Inoue 
153076f1499fSGleb Smirnoff 	return (0);
1531fb59c426SYoshinobu Inoue }
1532fb59c426SYoshinobu Inoue #endif /* INET6 */
1533fb59c426SYoshinobu Inoue 
1534cfe8b629SGarrett Wollman /*
1535b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1536b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1537b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1538b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1539b8af5dfaSRobert Watson  * from Linux.
1540b8af5dfaSRobert Watson  */
1541*8c6104c4SMarius Strobl void
1542*8c6104c4SMarius Strobl tcp_fill_info(const struct tcpcb *tp, struct tcp_info *ti)
1543b8af5dfaSRobert Watson {
1544b8af5dfaSRobert Watson 
1545*8c6104c4SMarius Strobl 	INP_LOCK_ASSERT(tptoinpcb(tp));
1546b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1547b8af5dfaSRobert Watson 
1548b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1549b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1550b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
15513529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1552b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1553b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1554b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1555b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1556b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1557b8af5dfaSRobert Watson 	}
155804682968SRichard Scheffenegger 	switch (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) {
155904682968SRichard Scheffenegger 		case TF2_ECN_PERMIT:
15605a17b6adSMichael Tuexen 			ti->tcpi_options |= TCPI_OPT_ECN;
156104682968SRichard Scheffenegger 			break;
156204682968SRichard Scheffenegger 		case TF2_ACE_PERMIT:
156304682968SRichard Scheffenegger 			/* FALLTHROUGH */
156404682968SRichard Scheffenegger 		case TF2_ECN_PERMIT | TF2_ACE_PERMIT:
156504682968SRichard Scheffenegger 			ti->tcpi_options |= TCPI_OPT_ACE;
156604682968SRichard Scheffenegger 			break;
156704682968SRichard Scheffenegger 		default:
156804682968SRichard Scheffenegger 			break;
156904682968SRichard Scheffenegger 	}
157004682968SRichard Scheffenegger 	if (IS_FASTOPEN(tp->t_flags))
157104682968SRichard Scheffenegger 		ti->tcpi_options |= TCPI_OPT_TFO;
15721baaf834SBruce M Simpson 
157343d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15743ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15751baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15761baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15771baaf834SBruce M Simpson 
1578b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1579b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1580b8af5dfaSRobert Watson 
1581b8af5dfaSRobert Watson 	/*
1582b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1583b8af5dfaSRobert Watson 	 */
1584c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1585535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1586b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15871c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1588535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
158943d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
159043d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1591f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1592f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1593f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1594a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1595a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1596a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1597a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1598a6456410SNavdeep Parhar 	}
1599a6456410SNavdeep Parhar #endif
160022c81cc5SRichard Scheffenegger 	/*
160122c81cc5SRichard Scheffenegger 	 * AccECN related counters.
160222c81cc5SRichard Scheffenegger 	 */
160322c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
160422c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
160522c81cc5SRichard Scheffenegger 		/*
160622c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
160722c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
160822c81cc5SRichard Scheffenegger 		 */
160922c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
161022c81cc5SRichard Scheffenegger 	else
161122c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
161222c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1613b8af5dfaSRobert Watson }
1614b8af5dfaSRobert Watson 
1615b8af5dfaSRobert Watson /*
16161e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
16171e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
16181e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
16191e8f5ffaSRobert Watson  * option.
1620cfe8b629SGarrett Wollman  */
1621bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
16228501a69cSRobert Watson 	INP_WLOCK(inp);							\
162353af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
16248501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1625bac5bedfSConrad Meyer 		cleanup;						\
16261e8f5ffaSRobert Watson 		return (ECONNRESET);					\
16271e8f5ffaSRobert Watson 	}								\
16281e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
16291e8f5ffaSRobert Watson } while(0)
1630bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
16311e8f5ffaSRobert Watson 
1632fd7daa72SMichael Tuexen int
1633fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1634df8bae1dSRodney W. Grimes {
1635fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1636fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1637fc4d53ccSGleb Smirnoff 	int error = 0;
1638df8bae1dSRodney W. Grimes 
1639fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
16403b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
164153af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1642fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1643fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1644fc4d53ccSGleb Smirnoff 
1645cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
16463b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1647fb59c426SYoshinobu Inoue #ifdef INET6
1648de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1649fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1650de156263SGleb Smirnoff #endif
1651de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1652de156263SGleb Smirnoff 		else
1653de156263SGleb Smirnoff #endif
1654de156263SGleb Smirnoff #ifdef INET
1655fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1656de156263SGleb Smirnoff #endif
16575dff1c38SMichael Tuexen 		/*
1658de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1659de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1660de156263SGleb Smirnoff 		 * IP level returned.
16615dff1c38SMichael Tuexen 		 */
1662de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1663de156263SGleb Smirnoff #ifdef INET6
1664de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1665de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1666de156263SGleb Smirnoff 				return (error);
1667de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1668de156263SGleb Smirnoff 			case IPV6_TCLASS:
1669de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1670de156263SGleb Smirnoff 				break;
1671de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1672f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1673f581a26eSGleb Smirnoff 				break;
1674de156263SGleb Smirnoff 			default:
1675de156263SGleb Smirnoff 				return (error);
16765dff1c38SMichael Tuexen 			}
1677de156263SGleb Smirnoff 			break;
1678b287c6c7SBjoern A. Zeeb #endif
1679b287c6c7SBjoern A. Zeeb #ifdef INET
1680de156263SGleb Smirnoff 		case IPPROTO_IP:
1681de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1682de156263SGleb Smirnoff 			case IP_TOS:
16833b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
16843b0ee680SRichard Scheffenegger 				break;
1685de156263SGleb Smirnoff 			case IP_TTL:
1686de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1687de156263SGleb Smirnoff 				break;
1688de156263SGleb Smirnoff 			default:
1689df8bae1dSRodney W. Grimes 				return (error);
1690de156263SGleb Smirnoff 			}
1691de156263SGleb Smirnoff 			break;
1692de156263SGleb Smirnoff #endif
1693de156263SGleb Smirnoff 		default:
1694de156263SGleb Smirnoff 			return (error);
1695de156263SGleb Smirnoff 		}
16963b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
169753af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
16983b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
16993b3c08c1SMichael Tuexen 			return (ECONNRESET);
17003b3c08c1SMichael Tuexen 		}
1701fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1702fc4d53ccSGleb Smirnoff 		/*
1703fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1704fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1705fc4d53ccSGleb Smirnoff 		 */
1706fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1707fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
170873ee5756SRandall Stewart 		void *ptr = NULL;
1709fc4d53ccSGleb Smirnoff 
17103b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1711fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1712fc4d53ccSGleb Smirnoff 		if (error)
1713fc4d53ccSGleb Smirnoff 			return (error);
1714fc4d53ccSGleb Smirnoff 
171568cea2b1SJohn Baldwin 		INP_WLOCK(inp);
171655bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1717fc4d53ccSGleb Smirnoff 
171855bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
171955bceb1eSRandall Stewart 		if (blk == NULL) {
172055bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
172155bceb1eSRandall Stewart 			return (ENOENT);
172255bceb1eSRandall Stewart 		}
1723587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1724587d67c0SRandall Stewart 			/* You already have this */
1725587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1726587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1727587d67c0SRandall Stewart 			return (0);
1728587d67c0SRandall Stewart 		}
1729587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1730587d67c0SRandall Stewart 			/*
1731587d67c0SRandall Stewart 			 * The user has advanced the state
1732587d67c0SRandall Stewart 			 * past the initial point, we may not
1733587d67c0SRandall Stewart 			 * be able to switch.
1734587d67c0SRandall Stewart 			 */
1735587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1736587d67c0SRandall Stewart 				/*
1737587d67c0SRandall Stewart 				 * Does the stack provide a
1738587d67c0SRandall Stewart 				 * query mechanism, if so it may
1739587d67c0SRandall Stewart 				 * still be possible?
1740587d67c0SRandall Stewart 				 */
1741587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1742c6c0be27SMichael Tuexen 			} else
1743c6c0be27SMichael Tuexen 				error = EINVAL;
1744587d67c0SRandall Stewart 			if (error) {
1745587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1746587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1747587d67c0SRandall Stewart 				return(error);
1748587d67c0SRandall Stewart 			}
1749587d67c0SRandall Stewart 		}
175055bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
175155bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
175255bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
175355bceb1eSRandall Stewart 			return (ENOENT);
175455bceb1eSRandall Stewart 		}
175555bceb1eSRandall Stewart 		/*
175673ee5756SRandall Stewart 		 * Ensure the new stack takes ownership with a
175773ee5756SRandall Stewart 		 * clean slate on peak rate threshold.
175855bceb1eSRandall Stewart 		 */
175973ee5756SRandall Stewart #ifdef TCPHPTS
176073ee5756SRandall Stewart 		/* Assure that we are not on any hpts */
1761c2a69e84SGleb Smirnoff 		tcp_hpts_remove(tp);
176273ee5756SRandall Stewart #endif
176373ee5756SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
176473ee5756SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp, &ptr);
176573ee5756SRandall Stewart 			if (error) {
176673ee5756SRandall Stewart 				/*
176773ee5756SRandall Stewart 				 * Release the ref count the lookup
176873ee5756SRandall Stewart 				 * acquired.
176973ee5756SRandall Stewart 				 */
177073ee5756SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
177173ee5756SRandall Stewart 				/*
177273ee5756SRandall Stewart 				 * Now there is a chance that the
177373ee5756SRandall Stewart 				 * init() function mucked with some
177473ee5756SRandall Stewart 				 * things before it failed, such as
177573ee5756SRandall Stewart 				 * hpts or inp_flags2 or timer granularity.
177673ee5756SRandall Stewart 				 * It should not of, but lets give the old
177773ee5756SRandall Stewart 				 * stack a chance to reset to a known good state.
177873ee5756SRandall Stewart 				 */
177973ee5756SRandall Stewart 				if (tp->t_fb->tfb_switch_failed) {
178073ee5756SRandall Stewart 					(*tp->t_fb->tfb_switch_failed)(tp);
178173ee5756SRandall Stewart 				}
178273ee5756SRandall Stewart 			 	goto err_out;
178373ee5756SRandall Stewart 			}
178473ee5756SRandall Stewart 		}
1785587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1786086a3556SAndrew Gallatin 			struct epoch_tracker et;
1787587d67c0SRandall Stewart 			/*
1788587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1789587d67c0SRandall Stewart 			 * the tcb is not going away.
1790587d67c0SRandall Stewart 			 */
1791086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1792587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1793086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1794587d67c0SRandall Stewart 		}
179573ee5756SRandall Stewart 		/*
179673ee5756SRandall Stewart 		 * Release the old refcnt, the
179773ee5756SRandall Stewart 		 * lookup acquired a ref on the
179873ee5756SRandall Stewart 		 * new one already.
179973ee5756SRandall Stewart 		 */
180055bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
180173ee5756SRandall Stewart 		/*
180273ee5756SRandall Stewart 		 * Set in the new stack.
180373ee5756SRandall Stewart 		 */
180455bceb1eSRandall Stewart 		tp->t_fb = blk;
180573ee5756SRandall Stewart 		tp->t_fb_ptr = ptr;
180655bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
180755bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
180855bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
180955bceb1eSRandall Stewart 			     sopt->sopt_name);
181055bceb1eSRandall Stewart 		}
181155bceb1eSRandall Stewart #endif
18123ee9c3c4SRandall Stewart err_out:
181355bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
181455bceb1eSRandall Stewart 		return (error);
181573ee5756SRandall Stewart 
1816fc4d53ccSGleb Smirnoff 	}
1817fc4d53ccSGleb Smirnoff 
18183b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
181966fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1820fc4d53ccSGleb Smirnoff }
1821fc4d53ccSGleb Smirnoff 
1822fc4d53ccSGleb Smirnoff static int
1823fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1824fc4d53ccSGleb Smirnoff {
1825fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1826fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1827fc4d53ccSGleb Smirnoff 	int error = 0;
1828fc4d53ccSGleb Smirnoff 
1829fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
18303b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
183153af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1832fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1833fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1834fc4d53ccSGleb Smirnoff 
1835fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
18363b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1837fc4d53ccSGleb Smirnoff #ifdef INET6
1838fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1839fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1840fc4d53ccSGleb Smirnoff #endif /* INET6 */
1841fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1842fc4d53ccSGleb Smirnoff 		else
1843fc4d53ccSGleb Smirnoff #endif
1844fc4d53ccSGleb Smirnoff #ifdef INET
1845fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1846fc4d53ccSGleb Smirnoff #endif
1847fc4d53ccSGleb Smirnoff 		return (error);
1848fc4d53ccSGleb Smirnoff 	}
1849fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1850e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1851fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1852fc4d53ccSGleb Smirnoff 
1853e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1854e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1855e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1856e2833083SPeter Lei 		} else {
1857e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1858e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1859c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1860c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1861e2833083SPeter Lei 		}
186255bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
186355bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
186455bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
186555bceb1eSRandall Stewart 		return (error);
186655bceb1eSRandall Stewart 	}
1867fc4d53ccSGleb Smirnoff 
18683b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
186966fbc19fSGleb Smirnoff 	return (tp->t_fb->tfb_tcp_ctloutput(tp, sopt));
1870fc4d53ccSGleb Smirnoff }
1871fc4d53ccSGleb Smirnoff 
1872fc4d53ccSGleb Smirnoff int
1873fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1874fc4d53ccSGleb Smirnoff {
1875fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1876fc4d53ccSGleb Smirnoff 
1877fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1878fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1879fc4d53ccSGleb Smirnoff 
18803b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
188153af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
18823b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
18833b3c08c1SMichael Tuexen 		return (ECONNRESET);
18843b3c08c1SMichael Tuexen 	}
1885fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1886fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1887fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1888fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1889fc4d53ccSGleb Smirnoff 	else
1890fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
189155bceb1eSRandall Stewart }
189255bceb1eSRandall Stewart 
18932529f56eSJonathan T. Looney /*
18942529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18952529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18962529f56eSJonathan T. Looney  */
18972529f56eSJonathan T. Looney #ifdef CTASSERT
18982529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18992529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
19002529f56eSJonathan T. Looney #endif
19012529f56eSJonathan T. Looney 
1902ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1903ec1db6e1SJohn Baldwin static int
1904ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1905ec1db6e1SJohn Baldwin {
1906ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1907ec1db6e1SJohn Baldwin 	int error;
1908ec1db6e1SJohn Baldwin 
1909ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1910ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1911ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1912ec1db6e1SJohn Baldwin 		if (error)
1913ec1db6e1SJohn Baldwin 			return (error);
1914ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1915ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1916ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1917ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1918ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1919ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1920ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1921ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1922ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1923ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1924ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1925ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1926ec1db6e1SJohn Baldwin 		return (0);
1927ec1db6e1SJohn Baldwin 	}
1928ec1db6e1SJohn Baldwin 
1929ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1930ec1db6e1SJohn Baldwin }
1931ec1db6e1SJohn Baldwin #endif
1932ec1db6e1SJohn Baldwin 
1933b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1934b8d60729SRandall Stewart 
1935b8d60729SRandall Stewart static int
1936ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1937b8d60729SRandall Stewart {
1938b8d60729SRandall Stewart 	struct cc_algo *algo;
1939b8d60729SRandall Stewart 	void *ptr = NULL;
19403b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1941b8d60729SRandall Stewart 	struct cc_var cc_mem;
1942b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1943b8d60729SRandall Stewart 	size_t mem_sz;
1944b8d60729SRandall Stewart 	int error;
1945b8d60729SRandall Stewart 
1946b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1947b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1948b8d60729SRandall Stewart 	if (error)
1949b8d60729SRandall Stewart 		return(error);
1950b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1951b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1952ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1953b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1954b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1955b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1956b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1957b8d60729SRandall Stewart 				continue;
1958b8d60729SRandall Stewart 			}
1959b8d60729SRandall Stewart 			break;
1960b8d60729SRandall Stewart 		}
1961ea9017fbSRandall Stewart 	}
1962b8d60729SRandall Stewart 	if (algo == NULL) {
1963b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1964b8d60729SRandall Stewart 		return(ESRCH);
1965b8d60729SRandall Stewart 	}
1966ea9017fbSRandall Stewart 	/*
1967ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1968ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1969ea9017fbSRandall Stewart 	 */
1970ea9017fbSRandall Stewart 	cc_refer(algo);
1971ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1972b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1973b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1974b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1975b8d60729SRandall Stewart 		if (mem_sz == 0) {
1976b8d60729SRandall Stewart 			goto no_mem_needed;
1977b8d60729SRandall Stewart 		}
1978b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1979b8d60729SRandall Stewart 	} else {
1980b8d60729SRandall Stewart no_mem_needed:
1981b8d60729SRandall Stewart 		mem_sz = 0;
1982b8d60729SRandall Stewart 		ptr = NULL;
1983b8d60729SRandall Stewart 	}
1984b8d60729SRandall Stewart 	/*
1985b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1986b8d60729SRandall Stewart 	 * back the inplock.
1987b8d60729SRandall Stewart 	 */
1988b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1989df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
199053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1991df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1992ea9017fbSRandall Stewart 		if (ptr)
1993df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1994ea9017fbSRandall Stewart 		/* Release our temp reference */
1995ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1996ea9017fbSRandall Stewart 		cc_release(algo);
1997ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
1998df07bfdaSMichael Tuexen 		return (ECONNRESET);
1999df07bfdaSMichael Tuexen 	}
2000df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
2001df07bfdaSMichael Tuexen 	if (ptr != NULL)
2002b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
2003b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
2004b8d60729SRandall Stewart 	/*
2005b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
2006b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
2007b8d60729SRandall Stewart 	 * Note here we init into stack memory.
2008b8d60729SRandall Stewart 	 */
2009b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
2010b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
2011b8d60729SRandall Stewart 	else
2012b8d60729SRandall Stewart 		error = 0;
2013b8d60729SRandall Stewart 	/*
2014b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
2015b8d60729SRandall Stewart 	 * should not fail we could in theory have a
2016b8d60729SRandall Stewart 	 * KASSERT here.
2017b8d60729SRandall Stewart 	 */
2018b8d60729SRandall Stewart 	if (error == 0) {
2019b8d60729SRandall Stewart 		/*
2020b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
2021b8d60729SRandall Stewart 		 * connection to the new CC module by
2022b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
2023b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
2024b8d60729SRandall Stewart 		 */
2025b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
2026e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
2027ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
2028ea9017fbSRandall Stewart 		cc_detach(tp);
2029ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
2030e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
2031ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
2032ea9017fbSRandall Stewart 		cc_attach(tp, algo);
2033b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
2034b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
2035b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
2036e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
2037b8d60729SRandall Stewart 		}
2038b8d60729SRandall Stewart 	} else if (ptr)
2039b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
2040b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
2041ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
2042ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
2043ea9017fbSRandall Stewart 	cc_release(algo);
2044ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
2045b8d60729SRandall Stewart 	return (error);
2046b8d60729SRandall Stewart }
2047b8d60729SRandall Stewart 
204855bceb1eSRandall Stewart int
204966fbc19fSGleb Smirnoff tcp_default_ctloutput(struct tcpcb *tp, struct sockopt *sopt)
205055bceb1eSRandall Stewart {
205166fbc19fSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
205255bceb1eSRandall Stewart 	int	error, opt, optval;
205355bceb1eSRandall Stewart 	u_int	ui;
205455bceb1eSRandall Stewart 	struct	tcp_info ti;
2055b2e60773SJohn Baldwin #ifdef KERN_TLS
2056b2e60773SJohn Baldwin 	struct tls_enable tls;
2057528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
2058b2e60773SJohn Baldwin #endif
20592529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
2060adc56f5aSEdward Tomasz Napierala #ifdef STATS
2061adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
2062adc56f5aSEdward Tomasz Napierala #endif
2063af6fef3aSGleb Smirnoff 	size_t	len;
2064df8bae1dSRodney W. Grimes 
2065f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
206653af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2067fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
2068528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
2069f581a26eSGleb Smirnoff 
2070f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
2071f581a26eSGleb Smirnoff #ifdef INET6
2072f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
2073f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
2074f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
2075f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
2076f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
2077f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
2078f581a26eSGleb Smirnoff 		}
2079f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2080f581a26eSGleb Smirnoff 		return (0);
2081f581a26eSGleb Smirnoff #endif
2082f581a26eSGleb Smirnoff #ifdef INET
2083f581a26eSGleb Smirnoff 	case IPPROTO_IP:
2084f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
2085f581a26eSGleb Smirnoff 		return (0);
2086f581a26eSGleb Smirnoff #endif
2087f581a26eSGleb Smirnoff 	}
2088f581a26eSGleb Smirnoff 
2089d519cedbSGleb Smirnoff 	/*
2090d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2091d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2092d519cedbSGleb Smirnoff 	 */
2093d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2094d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2095d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2096c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2097c8b53cedSMichael Tuexen 			return (EINVAL);
2098af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2099af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2100d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2101d519cedbSGleb Smirnoff 		if (error) {
2102af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2103d519cedbSGleb Smirnoff 			return (error);
2104d519cedbSGleb Smirnoff 		}
2105bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2106d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2107e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2108d519cedbSGleb Smirnoff 		else
2109d519cedbSGleb Smirnoff 			error = ENOENT;
2110d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2111d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2112af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2113af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2114d519cedbSGleb Smirnoff 		return (error);
2115d519cedbSGleb Smirnoff 	}
2116d519cedbSGleb Smirnoff 
2117cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2118cfe8b629SGarrett Wollman 	case SOPT_SET:
2119cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2120fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
212188f6b043SBruce M Simpson 		case TCP_MD5SIG:
21228501a69cSRobert Watson 			INP_WUNLOCK(inp);
212397453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2124fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2125fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
21261cfd4b53SBruce M Simpson 			if (error)
21271e8f5ffaSRobert Watson 				return (error);
212897453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
212909fe6320SNavdeep Parhar 			goto unlock_and_done;
2130fcf59617SAndrey V. Elsukov #endif /* IPSEC */
213109fe6320SNavdeep Parhar 
2132df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2133cfe8b629SGarrett Wollman 		case TCP_NOOPT:
21340471a8c7SRichard Scheffenegger 		case TCP_LRD:
21358501a69cSRobert Watson 			INP_WUNLOCK(inp);
2136cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2137cfe8b629SGarrett Wollman 			    sizeof optval);
2138cfe8b629SGarrett Wollman 			if (error)
21391e8f5ffaSRobert Watson 				return (error);
2140cfe8b629SGarrett Wollman 
21418501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2142cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2143cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2144cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2145cfe8b629SGarrett Wollman 				break;
2146cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2147cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2148cfe8b629SGarrett Wollman 				break;
21490471a8c7SRichard Scheffenegger 			case TCP_LRD:
21500471a8c7SRichard Scheffenegger 				opt = TF_LRD;
21510471a8c7SRichard Scheffenegger 				break;
2152cfe8b629SGarrett Wollman 			default:
2153cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2154cfe8b629SGarrett Wollman 				break;
2155cfe8b629SGarrett Wollman 			}
2156cfe8b629SGarrett Wollman 
2157cfe8b629SGarrett Wollman 			if (optval)
2158cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2159df8bae1dSRodney W. Grimes 			else
2160cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
216109fe6320SNavdeep Parhar unlock_and_done:
216209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
216309fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
216409fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
216509fe6320SNavdeep Parhar 				    sopt->sopt_name);
216609fe6320SNavdeep Parhar 			}
216709fe6320SNavdeep Parhar #endif
21688501a69cSRobert Watson 			INP_WUNLOCK(inp);
2169df8bae1dSRodney W. Grimes 			break;
2170df8bae1dSRodney W. Grimes 
2171007581c0SJonathan Lemon 		case TCP_NOPUSH:
21728501a69cSRobert Watson 			INP_WUNLOCK(inp);
2173007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2174007581c0SJonathan Lemon 			    sizeof optval);
2175007581c0SJonathan Lemon 			if (error)
21761e8f5ffaSRobert Watson 				return (error);
2177007581c0SJonathan Lemon 
21788501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2179007581c0SJonathan Lemon 			if (optval)
2180007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2181d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2182007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2183109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2184109eb549SGleb Smirnoff 					struct epoch_tracker et;
2185109eb549SGleb Smirnoff 
2186109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2187f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2188109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2189109eb549SGleb Smirnoff 				}
2190007581c0SJonathan Lemon 			}
219109fe6320SNavdeep Parhar 			goto unlock_and_done;
2192007581c0SJonathan Lemon 
21939e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21949e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21959e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21969e644c23SMichael Tuexen 			    sizeof optval);
21979e644c23SMichael Tuexen 			if (error)
21989e644c23SMichael Tuexen 				return (error);
21999e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
22009e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
22019e644c23SMichael Tuexen 				/* Its got to be in range */
22029e644c23SMichael Tuexen 				return (EINVAL);
22039e644c23SMichael Tuexen 			}
22049e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
22059e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
22069e644c23SMichael Tuexen 				return (EINVAL);
22079e644c23SMichael Tuexen 			}
22089e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
22099e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
22109e644c23SMichael Tuexen 				/* You can't change after you are connected */
22119e644c23SMichael Tuexen 				error = EINVAL;
22129e644c23SMichael Tuexen 			} else {
22139e644c23SMichael Tuexen 				/* Ok we are all good set the port */
22149e644c23SMichael Tuexen 				tp->t_port = htons(optval);
22159e644c23SMichael Tuexen 			}
22169e644c23SMichael Tuexen 			goto unlock_and_done;
22179e644c23SMichael Tuexen 
2218df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
22198501a69cSRobert Watson 			INP_WUNLOCK(inp);
2220cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2221cfe8b629SGarrett Wollman 			    sizeof optval);
2222cfe8b629SGarrett Wollman 			if (error)
22231e8f5ffaSRobert Watson 				return (error);
2224df8bae1dSRodney W. Grimes 
22258501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
222653369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2227603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2228cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2229a0292f23SGarrett Wollman 			else
2230a0292f23SGarrett Wollman 				error = EINVAL;
223109fe6320SNavdeep Parhar 			goto unlock_and_done;
2232a0292f23SGarrett Wollman 
2233b8af5dfaSRobert Watson 		case TCP_INFO:
22348501a69cSRobert Watson 			INP_WUNLOCK(inp);
2235b8af5dfaSRobert Watson 			error = EINVAL;
2236b8af5dfaSRobert Watson 			break;
2237b8af5dfaSRobert Watson 
2238adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2239adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2240adc56f5aSEdward Tomasz Napierala #ifdef STATS
2241adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2242adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2243adc56f5aSEdward Tomasz Napierala 			if (error)
2244adc56f5aSEdward Tomasz Napierala 				return (error);
2245adc56f5aSEdward Tomasz Napierala 
2246adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2247adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2248adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2249adc56f5aSEdward Tomasz Napierala 			else
2250adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2251adc56f5aSEdward Tomasz Napierala 
2252adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2253adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2254adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2255adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2256adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2257adc56f5aSEdward Tomasz Napierala 				sbp = t;
2258adc56f5aSEdward Tomasz Napierala 			}
2259adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2260adc56f5aSEdward Tomasz Napierala 
2261adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2262adc56f5aSEdward Tomasz Napierala #else
2263adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2264adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2265adc56f5aSEdward Tomasz Napierala 			break;
2266adc56f5aSEdward Tomasz Napierala 
2267dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2268ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
226973e263b1SGleb Smirnoff 			break;
2270dbc42409SLawrence Stewart 
2271a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2272a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2273a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2274a034518aSAndrew Gallatin 			    sizeof(optval));
2275a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2276a034518aSAndrew Gallatin 			if (!error)
2277a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2278a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2279a034518aSAndrew Gallatin 			break;
2280a034518aSAndrew Gallatin 
2281b2e60773SJohn Baldwin #ifdef KERN_TLS
2282b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2283b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2284ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2285b2e60773SJohn Baldwin 			if (error)
2286b2e60773SJohn Baldwin 				break;
2287fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2288b2e60773SJohn Baldwin 			break;
2289b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2290b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2291b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2292b2e60773SJohn Baldwin 			if (error)
2293b2e60773SJohn Baldwin 				return (error);
2294b2e60773SJohn Baldwin 
2295b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2296fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2297b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2298b2e60773SJohn Baldwin 			break;
2299f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2300f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2301f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2302f1f93475SJohn Baldwin 			    sizeof(tls));
2303f1f93475SJohn Baldwin 			if (error)
2304f1f93475SJohn Baldwin 				break;
2305fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2306f1f93475SJohn Baldwin 			break;
2307b2e60773SJohn Baldwin #endif
230808af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
23099077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
23109077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
23119077f387SGleb Smirnoff 		case TCP_KEEPINIT:
23129077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
23139077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
23149077f387SGleb Smirnoff 			if (error)
23159077f387SGleb Smirnoff 				return (error);
23169077f387SGleb Smirnoff 
23179077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
23189077f387SGleb Smirnoff 				error = EINVAL;
23199077f387SGleb Smirnoff 				break;
23209077f387SGleb Smirnoff 			}
23219077f387SGleb Smirnoff 			ui *= hz;
23229077f387SGleb Smirnoff 
23239077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
23249077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
232508af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
232608af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
232708af8aacSRandall Stewart 				break;
232808af8aacSRandall Stewart 
23299077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
23309077f387SGleb Smirnoff 				tp->t_keepidle = ui;
23319077f387SGleb Smirnoff 				/*
23329077f387SGleb Smirnoff 				 * XXX: better check current remaining
23339077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
23349077f387SGleb Smirnoff 				 */
23359077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
23369077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
23379077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23389077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
23399077f387SGleb Smirnoff 				break;
23409077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
23419077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
23429077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
23439077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
23449077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
23459077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
23469077f387SGleb Smirnoff 				break;
23479077f387SGleb Smirnoff 			case TCP_KEEPINIT:
23489077f387SGleb Smirnoff 				tp->t_keepinit = ui;
23499077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
23509077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
23519077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
23529077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
23539077f387SGleb Smirnoff 				break;
23549077f387SGleb Smirnoff 			}
235509fe6320SNavdeep Parhar 			goto unlock_and_done;
23569077f387SGleb Smirnoff 
235785c05144SGleb Smirnoff 		case TCP_KEEPCNT:
235885c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
235985c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
236085c05144SGleb Smirnoff 			if (error)
236185c05144SGleb Smirnoff 				return (error);
236285c05144SGleb Smirnoff 
236385c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
236485c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
236585c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
236685c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
236785c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
236885c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
236985c05144SGleb Smirnoff 			goto unlock_and_done;
237085c05144SGleb Smirnoff 
237186a996e6SHiren Panchasara #ifdef TCPPCAP
237286a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
237386a996e6SHiren Panchasara 		case TCP_PCAP_IN:
237486a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
237586a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
237686a996e6SHiren Panchasara 			    sizeof optval);
237786a996e6SHiren Panchasara 			if (error)
237886a996e6SHiren Panchasara 				return (error);
237986a996e6SHiren Panchasara 
238086a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
238186a996e6SHiren Panchasara 			if (optval >= 0)
2382399a5655SRichard Scheffenegger 				tcp_pcap_set_sock_max(
2383399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
238486a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
238586a996e6SHiren Panchasara 					optval);
238686a996e6SHiren Panchasara 			else
238786a996e6SHiren Panchasara 				error = EINVAL;
238886a996e6SHiren Panchasara 			goto unlock_and_done;
238986a996e6SHiren Panchasara #endif
239086a996e6SHiren Panchasara 
2391c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2392c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2393c560df6fSPatrick Kelsey 
2394281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2395c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2396c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2397281a0fd4SPatrick Kelsey 				return (EPERM);
2398281a0fd4SPatrick Kelsey 
2399c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2400c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2401281a0fd4SPatrick Kelsey 			if (error)
2402281a0fd4SPatrick Kelsey 				return (error);
2403281a0fd4SPatrick Kelsey 
2404281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2405d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2406d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2407d442a657SMichael Tuexen 				error = EINVAL;
2408d442a657SMichael Tuexen 				goto unlock_and_done;
2409d442a657SMichael Tuexen 			}
2410c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2411c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2412c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2413c560df6fSPatrick Kelsey 						error = EPERM;
2414c560df6fSPatrick Kelsey 						goto unlock_and_done;
2415c560df6fSPatrick Kelsey 					}
2416c560df6fSPatrick Kelsey 
2417c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2418281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2419281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2420c560df6fSPatrick Kelsey 				} else {
2421c560df6fSPatrick Kelsey 					/*
2422c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2423c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2424c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2425c560df6fSPatrick Kelsey 					 * connect.
2426c560df6fSPatrick Kelsey 					 */
2427c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2428c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2429c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2430c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2431c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2432c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2433c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2434c560df6fSPatrick Kelsey 					}
2435c560df6fSPatrick Kelsey 				}
2436d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2437281a0fd4SPatrick Kelsey 			} else
2438281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2439281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2440c560df6fSPatrick Kelsey 		}
2441281a0fd4SPatrick Kelsey 
2442e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
24432529f56eSJonathan T. Looney 		case TCP_LOG:
24442529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24452529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
24462529f56eSJonathan T. Looney 			    sizeof optval);
24472529f56eSJonathan T. Looney 			if (error)
24482529f56eSJonathan T. Looney 				return (error);
24492529f56eSJonathan T. Looney 
24502529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24512529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
24522529f56eSJonathan T. Looney 			goto unlock_and_done;
24532529f56eSJonathan T. Looney 
24542529f56eSJonathan T. Looney 		case TCP_LOGBUF:
24552529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24562529f56eSJonathan T. Looney 			error = EINVAL;
24572529f56eSJonathan T. Looney 			break;
24582529f56eSJonathan T. Looney 
24592529f56eSJonathan T. Looney 		case TCP_LOGID:
24602529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24612529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
24622529f56eSJonathan T. Looney 			if (error)
24632529f56eSJonathan T. Looney 				break;
24642529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24652529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24662529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
24672529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
24682529f56eSJonathan T. Looney 			break;
24692529f56eSJonathan T. Looney 
24702529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
24712529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
24722529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
24732529f56eSJonathan T. Looney 			error =
24742529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
24752529f56eSJonathan T. Looney 			if (error)
24762529f56eSJonathan T. Looney 				break;
24772529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
24782529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
24792529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
24802529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
24812529f56eSJonathan T. Looney 				    M_WAITOK, true);
24822529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
24832529f56eSJonathan T. Looney 			} else {
24842529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
24852529f56eSJonathan T. Looney 				/*
24862529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24872529f56eSJonathan T. Looney 				 * INP lock.
24882529f56eSJonathan T. Looney 				 */
24892529f56eSJonathan T. Looney 			}
24902529f56eSJonathan T. Looney 			break;
2491e24e5683SJonathan T. Looney #endif
24922529f56eSJonathan T. Looney 
2493df8bae1dSRodney W. Grimes 		default:
24948501a69cSRobert Watson 			INP_WUNLOCK(inp);
2495df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2496df8bae1dSRodney W. Grimes 			break;
2497df8bae1dSRodney W. Grimes 		}
2498df8bae1dSRodney W. Grimes 		break;
2499df8bae1dSRodney W. Grimes 
2500cfe8b629SGarrett Wollman 	case SOPT_GET:
25011e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2502cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2503fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
250488f6b043SBruce M Simpson 		case TCP_MD5SIG:
25058501a69cSRobert Watson 			INP_WUNLOCK(inp);
250697453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2507fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2508fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
25091cfd4b53SBruce M Simpson 			break;
2510265ed012SBruce M Simpson #endif
25111e8f5ffaSRobert Watson 
2512df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2513cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
25148501a69cSRobert Watson 			INP_WUNLOCK(inp);
2515b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2516df8bae1dSRodney W. Grimes 			break;
2517df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2518cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
25198501a69cSRobert Watson 			INP_WUNLOCK(inp);
2520b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2521df8bae1dSRodney W. Grimes 			break;
25229e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
25239e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
25249e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
25259e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
25269e644c23SMichael Tuexen 			break;
2527a0292f23SGarrett Wollman 		case TCP_NOOPT:
2528cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
25298501a69cSRobert Watson 			INP_WUNLOCK(inp);
2530b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2531a0292f23SGarrett Wollman 			break;
2532a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2533cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
25348501a69cSRobert Watson 			INP_WUNLOCK(inp);
2535b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2536b8af5dfaSRobert Watson 			break;
2537b8af5dfaSRobert Watson 		case TCP_INFO:
2538b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
25398501a69cSRobert Watson 			INP_WUNLOCK(inp);
2540b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2541a0292f23SGarrett Wollman 			break;
2542adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2543adc56f5aSEdward Tomasz Napierala 			{
2544adc56f5aSEdward Tomasz Napierala #ifdef STATS
2545adc56f5aSEdward Tomasz Napierala 			int nheld;
2546adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2547adc56f5aSEdward Tomasz Napierala 
2548adc56f5aSEdward Tomasz Napierala 			error = 0;
2549adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2550adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2551adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2552adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2553adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2554adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2555adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2556adc56f5aSEdward Tomasz Napierala 			else
2557adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2558adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2559adc56f5aSEdward Tomasz Napierala 			if (error)
2560adc56f5aSEdward Tomasz Napierala 				break;
2561adc56f5aSEdward Tomasz Napierala 
2562adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2563adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2564adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2565adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2566adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2567adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2568adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2569adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2570adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2571adc56f5aSEdward Tomasz Napierala 				break;
2572adc56f5aSEdward Tomasz Napierala 			}
2573adc56f5aSEdward Tomasz Napierala 
2574adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2575adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2576adc56f5aSEdward Tomasz Napierala 				goto unhold;
2577adc56f5aSEdward Tomasz Napierala 
2578adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2579adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2580adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2581adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2582adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2583adc56f5aSEdward Tomasz Napierala unhold:
2584adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2585adc56f5aSEdward Tomasz Napierala #else
2586adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2587adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2588adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2589adc56f5aSEdward Tomasz Napierala 			break;
2590adc56f5aSEdward Tomasz Napierala 			}
2591dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2592af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2593dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2594af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2595dbc42409SLawrence Stewart 			break;
259608af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25972f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
25982f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
25992f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
26002f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
26012f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
260208af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
260308af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
260408af8aacSRandall Stewart 				break;
26052f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
26065a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
26072f3eb7f4SGleb Smirnoff 				break;
26082f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
26095a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
26102f3eb7f4SGleb Smirnoff 				break;
26112f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
26125a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
26132f3eb7f4SGleb Smirnoff 				break;
26142f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
26155a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
26162f3eb7f4SGleb Smirnoff 				break;
26172f3eb7f4SGleb Smirnoff 			}
26182f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
26192f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
26202f3eb7f4SGleb Smirnoff 			break;
262186a996e6SHiren Panchasara #ifdef TCPPCAP
262286a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
262386a996e6SHiren Panchasara 		case TCP_PCAP_IN:
2624399a5655SRichard Scheffenegger 			optval = tcp_pcap_get_sock_max(
2625399a5655SRichard Scheffenegger 					(sopt->sopt_name == TCP_PCAP_OUT) ?
262686a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
262786a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
262886a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
262986a996e6SHiren Panchasara 			break;
263086a996e6SHiren Panchasara #endif
2631281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2632281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2633281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2634281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2635281a0fd4SPatrick Kelsey 			break;
2636e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
26372529f56eSJonathan T. Looney 		case TCP_LOG:
263869c7c811SRandall Stewart 			optval = tcp_get_bblog_state(tp);
26392529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26402529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
26412529f56eSJonathan T. Looney 			break;
26422529f56eSJonathan T. Looney 		case TCP_LOGBUF:
26432529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
26442529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
26452529f56eSJonathan T. Looney 			break;
26462529f56eSJonathan T. Looney 		case TCP_LOGID:
26472529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
26482529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26492529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
26502529f56eSJonathan T. Looney 			break;
26512529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
26522529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
26532529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
26542529f56eSJonathan T. Looney 			error = EINVAL;
26552529f56eSJonathan T. Looney 			break;
2656e24e5683SJonathan T. Looney #endif
2657b2e60773SJohn Baldwin #ifdef KERN_TLS
2658b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2659fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2660b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2661bf256782SMark Johnston 			if (error == 0)
2662bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2663bf256782SMark Johnston 				    sizeof(optval));
2664b2e60773SJohn Baldwin 			break;
2665f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2666fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2667f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2668bf256782SMark Johnston 			if (error == 0)
2669bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2670bf256782SMark Johnston 				    sizeof(optval));
2671f1f93475SJohn Baldwin 			break;
2672b2e60773SJohn Baldwin #endif
26730471a8c7SRichard Scheffenegger 		case TCP_LRD:
26740471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
26750471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
26760471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
26770471a8c7SRichard Scheffenegger 			break;
2678df8bae1dSRodney W. Grimes 		default:
26798501a69cSRobert Watson 			INP_WUNLOCK(inp);
2680df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2681df8bae1dSRodney W. Grimes 			break;
2682df8bae1dSRodney W. Grimes 		}
2683df8bae1dSRodney W. Grimes 		break;
2684df8bae1dSRodney W. Grimes 	}
2685df8bae1dSRodney W. Grimes 	return (error);
2686df8bae1dSRodney W. Grimes }
26878501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2688bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2689df8bae1dSRodney W. Grimes 
269026e30fbbSDavid Greenman /*
2691df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2692df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2693df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2694df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2695df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2696df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2697df8bae1dSRodney W. Grimes  */
2698623dce13SRobert Watson static void
2699ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2700df8bae1dSRodney W. Grimes {
27019eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
27029eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2703e6e0b5ffSRobert Watson 
270497a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27058501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2706df8bae1dSRodney W. Grimes 
2707623dce13SRobert Watson 	/*
2708623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2709623dce13SRobert Watson 	 * socket is still open.
2710623dce13SRobert Watson 	 */
27118db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
27128db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2713df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2714623dce13SRobert Watson 		KASSERT(tp != NULL,
2715623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2716623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2717243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2718623dce13SRobert Watson 		KASSERT(tp != NULL,
2719623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2720623dce13SRobert Watson 	} else {
2721df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2722df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2723623dce13SRobert Watson 		tcp_usrclosed(tp);
2724ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2725f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2726f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2727df8bae1dSRodney W. Grimes 	}
2728df8bae1dSRodney W. Grimes }
2729df8bae1dSRodney W. Grimes 
2730df8bae1dSRodney W. Grimes /*
2731df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2732df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2733df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2734df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2735df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2736df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2737df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2738df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2739df8bae1dSRodney W. Grimes  */
2740623dce13SRobert Watson static void
2741ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2742df8bae1dSRodney W. Grimes {
2743df8bae1dSRodney W. Grimes 
274497a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
27459eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2746e6e0b5ffSRobert Watson 
2747df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2748df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
274909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
275009fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
275109fe6320SNavdeep Parhar #endif
2752550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2753bc65987aSKip Macy 		/* FALLTHROUGH */
2754bc65987aSKip Macy 	case TCPS_CLOSED:
2755df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2756623dce13SRobert Watson 		/*
2757623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2758623dce13SRobert Watson 		 * still open.
2759623dce13SRobert Watson 		 */
2760623dce13SRobert Watson 		KASSERT(tp != NULL,
2761623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2762df8bae1dSRodney W. Grimes 		break;
2763df8bae1dSRodney W. Grimes 
2764a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2765df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2766a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2767a0292f23SGarrett Wollman 		break;
2768a0292f23SGarrett Wollman 
2769df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
277057f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2771df8bae1dSRodney W. Grimes 		break;
2772df8bae1dSRodney W. Grimes 
2773df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
277457f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2775df8bae1dSRodney W. Grimes 		break;
2776df8bae1dSRodney W. Grimes 	}
277708af8aacSRandall Stewart 	if (tp->t_acktime == 0)
277808af8aacSRandall Stewart 		tp->t_acktime = ticks;
2779abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
27809eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2781abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
27827c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
27837c72af87SMohan Srinivasan 			int timeout;
27847c72af87SMohan Srinivasan 
27857c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
27869077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2787b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2788b6239c4aSAndras Olah 		}
2789df8bae1dSRodney W. Grimes 	}
27907c72af87SMohan Srinivasan }
2791497057eeSRobert Watson 
2792497057eeSRobert Watson #ifdef DDB
2793497057eeSRobert Watson static void
2794497057eeSRobert Watson db_print_indent(int indent)
2795497057eeSRobert Watson {
2796497057eeSRobert Watson 	int i;
2797497057eeSRobert Watson 
2798497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2799497057eeSRobert Watson 		db_printf(" ");
2800497057eeSRobert Watson }
2801497057eeSRobert Watson 
2802497057eeSRobert Watson static void
2803497057eeSRobert Watson db_print_tstate(int t_state)
2804497057eeSRobert Watson {
2805497057eeSRobert Watson 
2806497057eeSRobert Watson 	switch (t_state) {
2807497057eeSRobert Watson 	case TCPS_CLOSED:
2808497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2809497057eeSRobert Watson 		return;
2810497057eeSRobert Watson 
2811497057eeSRobert Watson 	case TCPS_LISTEN:
2812497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2813497057eeSRobert Watson 		return;
2814497057eeSRobert Watson 
2815497057eeSRobert Watson 	case TCPS_SYN_SENT:
2816497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2817497057eeSRobert Watson 		return;
2818497057eeSRobert Watson 
2819497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2820497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2821497057eeSRobert Watson 		return;
2822497057eeSRobert Watson 
2823497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2824497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2825497057eeSRobert Watson 		return;
2826497057eeSRobert Watson 
2827497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2828497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2829497057eeSRobert Watson 		return;
2830497057eeSRobert Watson 
2831497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2832497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2833497057eeSRobert Watson 		return;
2834497057eeSRobert Watson 
2835497057eeSRobert Watson 	case TCPS_CLOSING:
2836497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2837497057eeSRobert Watson 		return;
2838497057eeSRobert Watson 
2839497057eeSRobert Watson 	case TCPS_LAST_ACK:
2840497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2841497057eeSRobert Watson 		return;
2842497057eeSRobert Watson 
2843497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2844497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2845497057eeSRobert Watson 		return;
2846497057eeSRobert Watson 
2847497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2848497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2849497057eeSRobert Watson 		return;
2850497057eeSRobert Watson 
2851497057eeSRobert Watson 	default:
2852497057eeSRobert Watson 		db_printf("unknown");
2853497057eeSRobert Watson 		return;
2854497057eeSRobert Watson 	}
2855497057eeSRobert Watson }
2856497057eeSRobert Watson 
2857497057eeSRobert Watson static void
2858497057eeSRobert Watson db_print_tflags(u_int t_flags)
2859497057eeSRobert Watson {
2860497057eeSRobert Watson 	int comma;
2861497057eeSRobert Watson 
2862497057eeSRobert Watson 	comma = 0;
2863497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2864497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2865497057eeSRobert Watson 		comma = 1;
2866497057eeSRobert Watson 	}
2867497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2868497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2869497057eeSRobert Watson 		comma = 1;
2870497057eeSRobert Watson 	}
2871497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2872497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2873497057eeSRobert Watson 		comma = 1;
2874497057eeSRobert Watson 	}
2875497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2876497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2877497057eeSRobert Watson 		comma = 1;
2878497057eeSRobert Watson 	}
2879497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2880497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2881497057eeSRobert Watson 		comma = 1;
2882497057eeSRobert Watson 	}
2883497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2884497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2885497057eeSRobert Watson 		comma = 1;
2886497057eeSRobert Watson 	}
2887497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2888497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2889497057eeSRobert Watson 		comma = 1;
2890497057eeSRobert Watson 	}
2891497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2892497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2893497057eeSRobert Watson 		comma = 1;
2894497057eeSRobert Watson 	}
2895497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2896497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2897497057eeSRobert Watson 		comma = 1;
2898497057eeSRobert Watson 	}
2899497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2900497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2901497057eeSRobert Watson 		comma = 1;
2902497057eeSRobert Watson 	}
2903497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2904497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2905497057eeSRobert Watson 		comma = 1;
2906497057eeSRobert Watson 	}
2907497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2908497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2909497057eeSRobert Watson 		comma = 1;
2910497057eeSRobert Watson 	}
2911497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2912497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2913497057eeSRobert Watson 		comma = 1;
2914497057eeSRobert Watson 	}
29153f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
29163f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
29173f169c54SRichard Scheffenegger 		comma = 1;
29183f169c54SRichard Scheffenegger 	}
2919497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2920497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2921497057eeSRobert Watson 		comma = 1;
2922497057eeSRobert Watson 	}
2923493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2924493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2925497057eeSRobert Watson 		comma = 1;
2926497057eeSRobert Watson 	}
2927497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2928497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2929497057eeSRobert Watson 		comma = 1;
2930497057eeSRobert Watson 	}
2931497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2932497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2933497057eeSRobert Watson 		comma = 1;
2934497057eeSRobert Watson 	}
2935497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2936497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2937497057eeSRobert Watson 		comma = 1;
2938497057eeSRobert Watson 	}
2939dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2940dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2941dbc42409SLawrence Stewart 		comma = 1;
2942dbc42409SLawrence Stewart 	}
2943497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2944497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2945497057eeSRobert Watson 		comma = 1;
2946497057eeSRobert Watson 	}
29473f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
29483f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
29493f169c54SRichard Scheffenegger 		comma = 1;
29503f169c54SRichard Scheffenegger 	}
2951497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2952497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2953497057eeSRobert Watson 		comma = 1;
2954497057eeSRobert Watson 	}
2955497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2956497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2957497057eeSRobert Watson 		comma = 1;
2958497057eeSRobert Watson 	}
2959497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2960497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2961497057eeSRobert Watson 		comma = 1;
2962497057eeSRobert Watson 	}
2963281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2964281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2965281a0fd4SPatrick Kelsey 		comma = 1;
2966281a0fd4SPatrick Kelsey 	}
2967497057eeSRobert Watson }
2968497057eeSRobert Watson 
2969497057eeSRobert Watson static void
29703cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
29713cf38784SMichael Tuexen {
29723cf38784SMichael Tuexen 	int comma;
29733cf38784SMichael Tuexen 
29743cf38784SMichael Tuexen 	comma = 0;
29753f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
29763f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
29773f169c54SRichard Scheffenegger 		comma = 1;
29783f169c54SRichard Scheffenegger 	}
29793f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
29803f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
29813f169c54SRichard Scheffenegger 		comma = 1;
29823f169c54SRichard Scheffenegger 	}
29833f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
29843f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
29853f169c54SRichard Scheffenegger 		comma = 1;
29863f169c54SRichard Scheffenegger 	}
29873f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29883f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29893f169c54SRichard Scheffenegger 		comma = 1;
29903f169c54SRichard Scheffenegger 	}
29913f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29923f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29933f169c54SRichard Scheffenegger 		comma = 1;
29943f169c54SRichard Scheffenegger 	}
29953cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29963cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29973cf38784SMichael Tuexen 		comma = 1;
29983cf38784SMichael Tuexen 	}
29993f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
30003f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
30013f169c54SRichard Scheffenegger 		comma = 1;
30023f169c54SRichard Scheffenegger 	}
30033f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
30043f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
30053f169c54SRichard Scheffenegger 		comma = 1;
30063f169c54SRichard Scheffenegger 	}
30073f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
30083f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
30093f169c54SRichard Scheffenegger 		comma = 1;
30103f169c54SRichard Scheffenegger 	}
30113f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
30123f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
30133f169c54SRichard Scheffenegger 		comma = 1;
30143f169c54SRichard Scheffenegger 	}
30153cf38784SMichael Tuexen }
30163cf38784SMichael Tuexen 
30173cf38784SMichael Tuexen static void
3018497057eeSRobert Watson db_print_toobflags(char t_oobflags)
3019497057eeSRobert Watson {
3020497057eeSRobert Watson 	int comma;
3021497057eeSRobert Watson 
3022497057eeSRobert Watson 	comma = 0;
3023497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
3024497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
3025497057eeSRobert Watson 		comma = 1;
3026497057eeSRobert Watson 	}
3027497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
3028497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
3029497057eeSRobert Watson 		comma = 1;
3030497057eeSRobert Watson 	}
3031497057eeSRobert Watson }
3032497057eeSRobert Watson 
3033497057eeSRobert Watson static void
3034497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
3035497057eeSRobert Watson {
3036497057eeSRobert Watson 
3037497057eeSRobert Watson 	db_print_indent(indent);
3038497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
3039497057eeSRobert Watson 
3040497057eeSRobert Watson 	indent += 2;
3041497057eeSRobert Watson 
3042497057eeSRobert Watson 	db_print_indent(indent);
3043497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
3044c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
3045497057eeSRobert Watson 
3046497057eeSRobert Watson 	db_print_indent(indent);
3047446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
3048446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
3049497057eeSRobert Watson 
3050497057eeSRobert Watson 	db_print_indent(indent);
3051497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
3052497057eeSRobert Watson 	db_print_tstate(tp->t_state);
3053497057eeSRobert Watson 	db_printf(")\n");
3054497057eeSRobert Watson 
3055497057eeSRobert Watson 	db_print_indent(indent);
3056497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
3057497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
3058497057eeSRobert Watson 	db_printf(")\n");
3059497057eeSRobert Watson 
3060497057eeSRobert Watson 	db_print_indent(indent);
30613cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
30623cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
30633cf38784SMichael Tuexen 	db_printf(")\n");
30643cf38784SMichael Tuexen 
30653cf38784SMichael Tuexen 	db_print_indent(indent);
3066fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
3067497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
3068497057eeSRobert Watson 
3069497057eeSRobert Watson 	db_print_indent(indent);
3070497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
3071497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
3072497057eeSRobert Watson 
3073497057eeSRobert Watson 	db_print_indent(indent);
3074497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
3075497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
3076497057eeSRobert Watson 
3077497057eeSRobert Watson 	db_print_indent(indent);
30783ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
3079497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
3080497057eeSRobert Watson 
3081497057eeSRobert Watson 	db_print_indent(indent);
30823ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
30831c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
3084497057eeSRobert Watson 
3085497057eeSRobert Watson 	db_print_indent(indent);
30863ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
30871c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3088497057eeSRobert Watson 
3089497057eeSRobert Watson 	db_print_indent(indent);
30900c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
30910c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3092497057eeSRobert Watson 
3093497057eeSRobert Watson 	db_print_indent(indent);
30941c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
30951c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3096497057eeSRobert Watson 
3097497057eeSRobert Watson 	db_print_indent(indent);
30981c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
30991c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3100497057eeSRobert Watson 
3101497057eeSRobert Watson 	db_print_indent(indent);
3102bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3103bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3104497057eeSRobert Watson 
3105497057eeSRobert Watson 	db_print_indent(indent);
310618b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3107497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3108497057eeSRobert Watson 
3109497057eeSRobert Watson 	db_print_indent(indent);
3110497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3111497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3112497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3113497057eeSRobert Watson 
3114497057eeSRobert Watson 	db_print_indent(indent);
3115497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3116497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3117497057eeSRobert Watson 
3118497057eeSRobert Watson 	db_print_indent(indent);
31199f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
31201a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3121497057eeSRobert Watson 
3122497057eeSRobert Watson 	db_print_indent(indent);
3123497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
31243ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3125497057eeSRobert Watson 
3126497057eeSRobert Watson 	db_print_indent(indent);
31273ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
31289f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3129497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3130497057eeSRobert Watson 
3131497057eeSRobert Watson 	db_print_indent(indent);
31323529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
31333529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3134497057eeSRobert Watson 
3135497057eeSRobert Watson 	db_print_indent(indent);
3136a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3137a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3138497057eeSRobert Watson 
3139497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3140497057eeSRobert Watson 
3141497057eeSRobert Watson 	db_print_indent(indent);
3142497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3143497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3144497057eeSRobert Watson }
3145497057eeSRobert Watson 
3146497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3147497057eeSRobert Watson {
3148497057eeSRobert Watson 	struct tcpcb *tp;
3149497057eeSRobert Watson 
3150497057eeSRobert Watson 	if (!have_addr) {
3151497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3152497057eeSRobert Watson 		return;
3153497057eeSRobert Watson 	}
3154497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3155497057eeSRobert Watson 
3156497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3157497057eeSRobert Watson }
3158497057eeSRobert Watson #endif
3159