xref: /freebsd/sys/netinet/tcp_usrreq.c (revision 636b19ead43a722d8434776e0bd185bfc97819b6)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
5623dce13SRobert Watson  *	The Regents of the University of California.
6497057eeSRobert Watson  * Copyright (c) 2006-2007 Robert N. M. Watson
7fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
8623dce13SRobert Watson  * All rights reserved.
9df8bae1dSRodney W. Grimes  *
10fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
11fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
12fa046d87SRobert Watson  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
23df8bae1dSRodney W. Grimes  *    without specific prior written permission.
24df8bae1dSRodney W. Grimes  *
25df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
36df8bae1dSRodney W. Grimes  *
371fdbc7aeSGarrett Wollman  *	From: @(#)tcp_usrreq.c	8.2 (Berkeley) 1/3/94
38df8bae1dSRodney W. Grimes  */
39df8bae1dSRodney W. Grimes 
404b421e2dSMike Silbersack #include <sys/cdefs.h>
414b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
424b421e2dSMike Silbersack 
43497057eeSRobert Watson #include "opt_ddb.h"
441cfd4b53SBruce M Simpson #include "opt_inet.h"
45fb59c426SYoshinobu Inoue #include "opt_inet6.h"
46fcf59617SAndrey V. Elsukov #include "opt_ipsec.h"
47b2e60773SJohn Baldwin #include "opt_kern_tls.h"
480cc12cc5SJoerg Wunsch 
49df8bae1dSRodney W. Grimes #include <sys/param.h>
50df8bae1dSRodney W. Grimes #include <sys/systm.h>
51adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
529077f387SGleb Smirnoff #include <sys/limits.h>
53f76fcf6dSJeffrey Hsu #include <sys/malloc.h>
5455bceb1eSRandall Stewart #include <sys/refcount.h>
55c7a82f90SGarrett Wollman #include <sys/kernel.h>
56b2e60773SJohn Baldwin #include <sys/ktls.h>
57adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
5898163b98SPoul-Henning Kamp #include <sys/sysctl.h>
59df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
60fb59c426SYoshinobu Inoue #ifdef INET6
61fb59c426SYoshinobu Inoue #include <sys/domain.h>
62fb59c426SYoshinobu Inoue #endif /* INET6 */
63df8bae1dSRodney W. Grimes #include <sys/socket.h>
64df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
65df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6691421ba2SRobert Watson #include <sys/proc.h>
6791421ba2SRobert Watson #include <sys/jail.h>
68adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
69df8bae1dSRodney W. Grimes 
70497057eeSRobert Watson #ifdef DDB
71497057eeSRobert Watson #include <ddb/ddb.h>
72497057eeSRobert Watson #endif
73497057eeSRobert Watson 
74df8bae1dSRodney W. Grimes #include <net/if.h>
7576039bc8SGleb Smirnoff #include <net/if_var.h>
76df8bae1dSRodney W. Grimes #include <net/route.h>
77530c0060SRobert Watson #include <net/vnet.h>
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes #include <netinet/in.h>
805d06879aSGeorge V. Neville-Neil #include <netinet/in_kdtrace.h>
81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
82b287c6c7SBjoern A. Zeeb #include <netinet/in_systm.h>
83b5e8ce9fSBruce Evans #include <netinet/in_var.h>
843b0ee680SRichard Scheffenegger #include <netinet/ip.h>
85df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
86fb59c426SYoshinobu Inoue #ifdef INET6
87b287c6c7SBjoern A. Zeeb #include <netinet/ip6.h>
88b287c6c7SBjoern A. Zeeb #include <netinet6/in6_pcb.h>
89fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h>
90a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
91fb59c426SYoshinobu Inoue #endif
922de3e790SGleb Smirnoff #include <netinet/tcp.h>
93df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
94df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
95df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
96df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
972529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
98df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
994644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
100c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
101fd389e7cSRandall Stewart #include <netinet/tcp_hpts.h>
10286a996e6SHiren Panchasara #ifdef TCPPCAP
10386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
10486a996e6SHiren Panchasara #endif
10509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
106bc65987aSKip Macy #include <netinet/tcp_offload.h>
10709fe6320SNavdeep Parhar #endif
108fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
109df8bae1dSRodney W. Grimes 
110adc56f5aSEdward Tomasz Napierala #include <vm/vm.h>
111adc56f5aSEdward Tomasz Napierala #include <vm/vm_param.h>
112adc56f5aSEdward Tomasz Napierala #include <vm/pmap.h>
113adc56f5aSEdward Tomasz Napierala #include <vm/vm_extern.h>
114adc56f5aSEdward Tomasz Napierala #include <vm/vm_map.h>
115adc56f5aSEdward Tomasz Napierala #include <vm/vm_page.h>
116adc56f5aSEdward Tomasz Napierala 
117df8bae1dSRodney W. Grimes /*
118df8bae1dSRodney W. Grimes  * TCP protocol interface to socket abstraction.
119df8bae1dSRodney W. Grimes  */
120b287c6c7SBjoern A. Zeeb #ifdef INET
121a9d22cceSGleb Smirnoff static int	tcp_connect(struct tcpcb *, struct sockaddr_in *,
1224d77a549SAlfred Perlstein 		    struct thread *td);
123b287c6c7SBjoern A. Zeeb #endif /* INET */
124fb59c426SYoshinobu Inoue #ifdef INET6
125a9d22cceSGleb Smirnoff static int	tcp6_connect(struct tcpcb *, struct sockaddr_in6 *,
1264d77a549SAlfred Perlstein 		    struct thread *td);
127fb59c426SYoshinobu Inoue #endif /* INET6 */
128623dce13SRobert Watson static void	tcp_disconnect(struct tcpcb *);
129623dce13SRobert Watson static void	tcp_usrclosed(struct tcpcb *);
130b8af5dfaSRobert Watson static void	tcp_fill_info(struct tcpcb *, struct tcp_info *);
1312c37256eSGarrett Wollman 
132d3b6c96bSRandall Stewart static int	tcp_pru_options_support(struct tcpcb *tp, int flags);
133d3b6c96bSRandall Stewart 
1342c37256eSGarrett Wollman /*
1352c37256eSGarrett Wollman  * TCP attaches to socket via pru_attach(), reserving space,
1362c37256eSGarrett Wollman  * and an internet control block.
1372c37256eSGarrett Wollman  */
1382c37256eSGarrett Wollman static int
139b40ce416SJulian Elischer tcp_usr_attach(struct socket *so, int proto, struct thread *td)
1402c37256eSGarrett Wollman {
141f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
142623dce13SRobert Watson 	struct tcpcb *tp = NULL;
143623dce13SRobert Watson 	int error;
1442c37256eSGarrett Wollman 
145623dce13SRobert Watson 	inp = sotoinpcb(so);
146623dce13SRobert Watson 	KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
1472c37256eSGarrett Wollman 
1480f6385e7SGleb Smirnoff 	error = soreserve(so, V_tcp_sendspace, V_tcp_recvspace);
1492c37256eSGarrett Wollman 	if (error)
1502c37256eSGarrett Wollman 		goto out;
1512c37256eSGarrett Wollman 
1520f6385e7SGleb Smirnoff 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
1530f6385e7SGleb Smirnoff 	so->so_snd.sb_flags |= SB_AUTOSIZE;
1540f6385e7SGleb Smirnoff 	error = in_pcballoc(so, &V_tcbinfo);
1557669c586SGleb Smirnoff 	if (error)
1560f6385e7SGleb Smirnoff 		goto out;
1570f6385e7SGleb Smirnoff 	inp = sotoinpcb(so);
1580f6385e7SGleb Smirnoff 	tp = tcp_newtcpcb(inp);
1590f6385e7SGleb Smirnoff 	if (tp == NULL) {
1607669c586SGleb Smirnoff 		error = ENOBUFS;
1610f6385e7SGleb Smirnoff 		in_pcbdetach(inp);
1620f6385e7SGleb Smirnoff 		in_pcbfree(inp);
1630f6385e7SGleb Smirnoff 		goto out;
1640f6385e7SGleb Smirnoff 	}
1650f6385e7SGleb Smirnoff 	tp->t_state = TCPS_CLOSED;
1660f6385e7SGleb Smirnoff 	INP_WUNLOCK(inp);
1670f6385e7SGleb Smirnoff 	TCPSTATES_INC(TCPS_CLOSED);
1682c37256eSGarrett Wollman out:
1695d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ATTACH);
1700f6385e7SGleb Smirnoff 	return (error);
1712c37256eSGarrett Wollman }
1722c37256eSGarrett Wollman 
1732c37256eSGarrett Wollman /*
1743fed74e9SGleb Smirnoff  * tcp_usr_detach is called when the socket layer loses its final reference
175a152f8a3SRobert Watson  * to the socket, be it a file descriptor reference, a reference from TCP,
176a152f8a3SRobert Watson  * etc.  At this point, there is only one case in which we will keep around
177a152f8a3SRobert Watson  * inpcb state: time wait.
1782c37256eSGarrett Wollman  */
179bc725eafSRobert Watson static void
1803fed74e9SGleb Smirnoff tcp_usr_detach(struct socket *so)
1812c37256eSGarrett Wollman {
1823fed74e9SGleb Smirnoff 	struct inpcb *inp;
1832c37256eSGarrett Wollman 	struct tcpcb *tp;
1842c37256eSGarrett Wollman 
1853fed74e9SGleb Smirnoff 	inp = sotoinpcb(so);
1863fed74e9SGleb Smirnoff 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1873fed74e9SGleb Smirnoff 	INP_WLOCK(inp);
1883fed74e9SGleb Smirnoff 	KASSERT(so->so_pcb == inp && inp->inp_socket == so,
1893fed74e9SGleb Smirnoff 		("%s: socket %p inp %p mismatch", __func__, so, inp));
190953b5606SRobert Watson 
191a152f8a3SRobert Watson 	tp = intotcpcb(inp);
192a152f8a3SRobert Watson 
1931b91978fSGleb Smirnoff 	KASSERT(inp->inp_flags & INP_DROPPED ||
1941b91978fSGleb Smirnoff 	    tp->t_state < TCPS_SYN_SENT,
1951b91978fSGleb Smirnoff 	    ("%s: inp %p not dropped or embryonic", __func__, inp));
1969c3507f9SGleb Smirnoff 
197623dce13SRobert Watson 	tcp_discardcb(tp);
198623dce13SRobert Watson 	in_pcbdetach(inp);
1990206cdb8SBjoern A. Zeeb 	in_pcbfree(inp);
200623dce13SRobert Watson }
201c78cbc7bSRobert Watson 
202b287c6c7SBjoern A. Zeeb #ifdef INET
2032c37256eSGarrett Wollman /*
2042c37256eSGarrett Wollman  * Give the socket an address.
2052c37256eSGarrett Wollman  */
2062c37256eSGarrett Wollman static int
207b40ce416SJulian Elischer tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
2082c37256eSGarrett Wollman {
2092c37256eSGarrett Wollman 	int error = 0;
210f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
211b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
212623dce13SRobert Watson 	struct tcpcb *tp = NULL;
213b338b1fdSMateusz Guzik #endif
2142c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
2152c37256eSGarrett Wollman 
21652710de1SPawel Jakub Dawidek 	sinp = (struct sockaddr_in *)nam;
217f96603b5SMark Johnston 	if (nam->sa_family != AF_INET) {
218f96603b5SMark Johnston 		/*
219f96603b5SMark Johnston 		 * Preserve compatibility with old programs.
220f96603b5SMark Johnston 		 */
221f96603b5SMark Johnston 		if (nam->sa_family != AF_UNSPEC ||
2223f1f6b6eSMichael Tuexen 		    nam->sa_len < offsetof(struct sockaddr_in, sin_zero) ||
223f96603b5SMark Johnston 		    sinp->sin_addr.s_addr != INADDR_ANY)
224f161d294SMark Johnston 			return (EAFNOSUPPORT);
225f96603b5SMark Johnston 		nam->sa_family = AF_INET;
226f96603b5SMark Johnston 	}
22752710de1SPawel Jakub Dawidek 	if (nam->sa_len != sizeof(*sinp))
22852710de1SPawel Jakub Dawidek 		return (EINVAL);
229f161d294SMark Johnston 
2302c37256eSGarrett Wollman 	/*
2312c37256eSGarrett Wollman 	 * Must check for multicast addresses and disallow binding
2322c37256eSGarrett Wollman 	 * to them.
2332c37256eSGarrett Wollman 	 */
234f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
23552710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
23652710de1SPawel Jakub Dawidek 
237623dce13SRobert Watson 	inp = sotoinpcb(so);
238623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
2398501a69cSRobert Watson 	INP_WLOCK(inp);
24053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
241623dce13SRobert Watson 		error = EINVAL;
2422c37256eSGarrett Wollman 		goto out;
243623dce13SRobert Watson 	}
244b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
245623dce13SRobert Watson 	tp = intotcpcb(inp);
246b338b1fdSMateusz Guzik #endif
247fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
248623dce13SRobert Watson 	error = in_pcbbind(inp, nam, td->td_ucred);
249fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
250623dce13SRobert Watson out:
2515d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
2528501a69cSRobert Watson 	INP_WUNLOCK(inp);
253623dce13SRobert Watson 
254623dce13SRobert Watson 	return (error);
2552c37256eSGarrett Wollman }
256b287c6c7SBjoern A. Zeeb #endif /* INET */
2572c37256eSGarrett Wollman 
258fb59c426SYoshinobu Inoue #ifdef INET6
259fb59c426SYoshinobu Inoue static int
260b40ce416SJulian Elischer tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
261fb59c426SYoshinobu Inoue {
262fb59c426SYoshinobu Inoue 	int error = 0;
263f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
264b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
265623dce13SRobert Watson 	struct tcpcb *tp = NULL;
266b338b1fdSMateusz Guzik #endif
2670ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
2684a91aa8fSMichael Tuexen 	u_char vflagsav;
269fb59c426SYoshinobu Inoue 
2700ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
271f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
272f161d294SMark Johnston 		return (EAFNOSUPPORT);
2730ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof(*sin6))
27452710de1SPawel Jakub Dawidek 		return (EINVAL);
275f161d294SMark Johnston 
276fb59c426SYoshinobu Inoue 	/*
277fb59c426SYoshinobu Inoue 	 * Must check for multicast addresses and disallow binding
278fb59c426SYoshinobu Inoue 	 * to them.
279fb59c426SYoshinobu Inoue 	 */
280f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
28152710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
28252710de1SPawel Jakub Dawidek 
283623dce13SRobert Watson 	inp = sotoinpcb(so);
284623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
2858501a69cSRobert Watson 	INP_WLOCK(inp);
2864a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
28753af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
288623dce13SRobert Watson 		error = EINVAL;
289623dce13SRobert Watson 		goto out;
290623dce13SRobert Watson 	}
291b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
292623dce13SRobert Watson 	tp = intotcpcb(inp);
293b338b1fdSMateusz Guzik #endif
294fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
295fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
296fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
297b287c6c7SBjoern A. Zeeb #ifdef INET
29866ef17c4SHajimu UMEMOTO 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
2990ecd976eSBjoern A. Zeeb 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
300fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
3010ecd976eSBjoern A. Zeeb 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
302fb59c426SYoshinobu Inoue 			struct sockaddr_in sin;
303fb59c426SYoshinobu Inoue 
3040ecd976eSBjoern A. Zeeb 			in6_sin6_2_sin(&sin, sin6);
305888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
306888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
307888973f5SMichael Tuexen 				INP_HASH_WUNLOCK(&V_tcbinfo);
308888973f5SMichael Tuexen 				goto out;
309888973f5SMichael Tuexen 			}
310fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
311fb59c426SYoshinobu Inoue 			inp->inp_vflag &= ~INP_IPV6;
312b0330ed9SPawel Jakub Dawidek 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
313b0330ed9SPawel Jakub Dawidek 			    td->td_ucred);
314fa046d87SRobert Watson 			INP_HASH_WUNLOCK(&V_tcbinfo);
315fb59c426SYoshinobu Inoue 			goto out;
316fb59c426SYoshinobu Inoue 		}
317fb59c426SYoshinobu Inoue 	}
318b287c6c7SBjoern A. Zeeb #endif
319b0330ed9SPawel Jakub Dawidek 	error = in6_pcbbind(inp, nam, td->td_ucred);
320fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
321623dce13SRobert Watson out:
3224a91aa8fSMichael Tuexen 	if (error != 0)
3234a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
3245d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_BIND);
3258501a69cSRobert Watson 	INP_WUNLOCK(inp);
326623dce13SRobert Watson 	return (error);
327fb59c426SYoshinobu Inoue }
328fb59c426SYoshinobu Inoue #endif /* INET6 */
329fb59c426SYoshinobu Inoue 
330b287c6c7SBjoern A. Zeeb #ifdef INET
3312c37256eSGarrett Wollman /*
3322c37256eSGarrett Wollman  * Prepare to accept connections.
3332c37256eSGarrett Wollman  */
3342c37256eSGarrett Wollman static int
335d374e81eSRobert Watson tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
3362c37256eSGarrett Wollman {
3372c37256eSGarrett Wollman 	int error = 0;
338f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
339623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3402c37256eSGarrett Wollman 
341623dce13SRobert Watson 	inp = sotoinpcb(so);
342623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
3438501a69cSRobert Watson 	INP_WLOCK(inp);
34453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
345623dce13SRobert Watson 		error = EINVAL;
346623dce13SRobert Watson 		goto out;
347623dce13SRobert Watson 	}
348623dce13SRobert Watson 	tp = intotcpcb(inp);
3490daccb9cSRobert Watson 	SOCK_LOCK(so);
3500daccb9cSRobert Watson 	error = solisten_proto_check(so);
351bd4a39ccSMark Johnston 	if (error != 0) {
352bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
353bd4a39ccSMark Johnston 		goto out;
354bd4a39ccSMark Johnston 	}
355bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
356fa046d87SRobert Watson 		INP_HASH_WLOCK(&V_tcbinfo);
357bd4a39ccSMark Johnston 		error = in_pcbbind(inp, NULL, td->td_ucred);
358fa046d87SRobert Watson 		INP_HASH_WUNLOCK(&V_tcbinfo);
359bd4a39ccSMark Johnston 	}
3600daccb9cSRobert Watson 	if (error == 0) {
36157f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
362d374e81eSRobert Watson 		solisten_proto(so, backlog);
36309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
36437cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
36509fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
36609fe6320SNavdeep Parhar #endif
367bd4a39ccSMark Johnston 	} else {
368bd4a39ccSMark Johnston 		solisten_proto_abort(so);
3690daccb9cSRobert Watson 	}
3700daccb9cSRobert Watson 	SOCK_UNLOCK(so);
371623dce13SRobert Watson 
37268bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
373281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
37418a75309SPatrick Kelsey 
375623dce13SRobert Watson out:
3765d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
3778501a69cSRobert Watson 	INP_WUNLOCK(inp);
378623dce13SRobert Watson 	return (error);
3792c37256eSGarrett Wollman }
380b287c6c7SBjoern A. Zeeb #endif /* INET */
3812c37256eSGarrett Wollman 
382fb59c426SYoshinobu Inoue #ifdef INET6
383fb59c426SYoshinobu Inoue static int
384d374e81eSRobert Watson tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
385fb59c426SYoshinobu Inoue {
386fb59c426SYoshinobu Inoue 	int error = 0;
387f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
388623dce13SRobert Watson 	struct tcpcb *tp = NULL;
3894a91aa8fSMichael Tuexen 	u_char vflagsav;
390fb59c426SYoshinobu Inoue 
391623dce13SRobert Watson 	inp = sotoinpcb(so);
392623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
3938501a69cSRobert Watson 	INP_WLOCK(inp);
39453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
395623dce13SRobert Watson 		error = EINVAL;
396623dce13SRobert Watson 		goto out;
397623dce13SRobert Watson 	}
3984a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
399623dce13SRobert Watson 	tp = intotcpcb(inp);
4000daccb9cSRobert Watson 	SOCK_LOCK(so);
4010daccb9cSRobert Watson 	error = solisten_proto_check(so);
402bd4a39ccSMark Johnston 	if (error != 0) {
403bd4a39ccSMark Johnston 		SOCK_UNLOCK(so);
404bd4a39ccSMark Johnston 		goto out;
405bd4a39ccSMark Johnston 	}
406fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
407bd4a39ccSMark Johnston 	if (inp->inp_lport == 0) {
408fb59c426SYoshinobu Inoue 		inp->inp_vflag &= ~INP_IPV4;
40966ef17c4SHajimu UMEMOTO 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
410fb59c426SYoshinobu Inoue 			inp->inp_vflag |= INP_IPV4;
411bd4a39ccSMark Johnston 		error = in6_pcbbind(inp, NULL, td->td_ucred);
412fb59c426SYoshinobu Inoue 	}
413fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
4140daccb9cSRobert Watson 	if (error == 0) {
41557f60867SMark Johnston 		tcp_state_change(tp, TCPS_LISTEN);
416d374e81eSRobert Watson 		solisten_proto(so, backlog);
41709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
41837cc0ecbSNavdeep Parhar 		if ((so->so_options & SO_NO_OFFLOAD) == 0)
41909fe6320SNavdeep Parhar 			tcp_offload_listen_start(tp);
42009fe6320SNavdeep Parhar #endif
421bd4a39ccSMark Johnston 	} else {
422bd4a39ccSMark Johnston 		solisten_proto_abort(so);
4230daccb9cSRobert Watson 	}
4240daccb9cSRobert Watson 	SOCK_UNLOCK(so);
425623dce13SRobert Watson 
42668bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags))
427281a0fd4SPatrick Kelsey 		tp->t_tfo_pending = tcp_fastopen_alloc_counter();
42818a75309SPatrick Kelsey 
4294a91aa8fSMichael Tuexen 	if (error != 0)
4304a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
4314a91aa8fSMichael Tuexen 
432623dce13SRobert Watson out:
4335d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_LISTEN);
4348501a69cSRobert Watson 	INP_WUNLOCK(inp);
435623dce13SRobert Watson 	return (error);
436fb59c426SYoshinobu Inoue }
437fb59c426SYoshinobu Inoue #endif /* INET6 */
438fb59c426SYoshinobu Inoue 
439b287c6c7SBjoern A. Zeeb #ifdef INET
4402c37256eSGarrett Wollman /*
4412c37256eSGarrett Wollman  * Initiate connection to peer.
4422c37256eSGarrett Wollman  * Create a template for use in transmissions on this connection.
4432c37256eSGarrett Wollman  * Enter SYN_SENT state, and mark socket as connecting.
4442c37256eSGarrett Wollman  * Start keep-alive timer, and seed output sequence space.
4452c37256eSGarrett Wollman  * Send initial segment on connection.
4462c37256eSGarrett Wollman  */
4472c37256eSGarrett Wollman static int
448b40ce416SJulian Elischer tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
4492c37256eSGarrett Wollman {
450109eb549SGleb Smirnoff 	struct epoch_tracker et;
4512c37256eSGarrett Wollman 	int error = 0;
452f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
453623dce13SRobert Watson 	struct tcpcb *tp = NULL;
4542c37256eSGarrett Wollman 	struct sockaddr_in *sinp;
4552c37256eSGarrett Wollman 
45657bf258eSGarrett Wollman 	sinp = (struct sockaddr_in *)nam;
457f161d294SMark Johnston 	if (nam->sa_family != AF_INET)
458f161d294SMark Johnston 		return (EAFNOSUPPORT);
459e29ef13fSDon Lewis 	if (nam->sa_len != sizeof (*sinp))
460e29ef13fSDon Lewis 		return (EINVAL);
461f161d294SMark Johnston 
46252710de1SPawel Jakub Dawidek 	/*
46352710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
46452710de1SPawel Jakub Dawidek 	 */
465f161d294SMark Johnston 	if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
46652710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
467f161d294SMark Johnston 	if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST)
468f903a308SMichael Tuexen 		return (EACCES);
469b89e82ddSJamie Gritton 	if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
470b89e82ddSJamie Gritton 		return (error);
47175c13541SPoul-Henning Kamp 
472623dce13SRobert Watson 	inp = sotoinpcb(so);
473623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
4748501a69cSRobert Watson 	INP_WLOCK(inp);
475eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
476eb96dc33SJulien Charbon 		error = ECONNREFUSED;
477623dce13SRobert Watson 		goto out;
478623dce13SRobert Watson 	}
479bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
480bd4a39ccSMark Johnston 		error = EOPNOTSUPP;
481bd4a39ccSMark Johnston 		goto out;
482bd4a39ccSMark Johnston 	}
483623dce13SRobert Watson 	tp = intotcpcb(inp);
484c1604fe4SGleb Smirnoff 	NET_EPOCH_ENTER(et);
485a9d22cceSGleb Smirnoff 	if ((error = tcp_connect(tp, sinp, td)) != 0)
486c1604fe4SGleb Smirnoff 		goto out_in_epoch;
48709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
48809fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
48937cc0ecbSNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
49009fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
491c1604fe4SGleb Smirnoff 		goto out_in_epoch;
49209fe6320SNavdeep Parhar #endif
49309fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
49440fa3e40SGleb Smirnoff 	error = tcp_output(tp);
4951d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
4961d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
497c1604fe4SGleb Smirnoff out_in_epoch:
498109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
499623dce13SRobert Watson out:
500e79cb051SGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
5018501a69cSRobert Watson 	INP_WUNLOCK(inp);
502623dce13SRobert Watson 	return (error);
5032c37256eSGarrett Wollman }
504b287c6c7SBjoern A. Zeeb #endif /* INET */
5052c37256eSGarrett Wollman 
506fb59c426SYoshinobu Inoue #ifdef INET6
507fb59c426SYoshinobu Inoue static int
508b40ce416SJulian Elischer tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
509fb59c426SYoshinobu Inoue {
510109eb549SGleb Smirnoff 	struct epoch_tracker et;
511fb59c426SYoshinobu Inoue 	int error = 0;
512f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
513623dce13SRobert Watson 	struct tcpcb *tp = NULL;
5140ecd976eSBjoern A. Zeeb 	struct sockaddr_in6 *sin6;
5154a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
5164a91aa8fSMichael Tuexen 	u_char vflagsav;
517623dce13SRobert Watson 
5180ecd976eSBjoern A. Zeeb 	sin6 = (struct sockaddr_in6 *)nam;
519f161d294SMark Johnston 	if (nam->sa_family != AF_INET6)
520f161d294SMark Johnston 		return (EAFNOSUPPORT);
5210ecd976eSBjoern A. Zeeb 	if (nam->sa_len != sizeof (*sin6))
522e29ef13fSDon Lewis 		return (EINVAL);
523f161d294SMark Johnston 
52452710de1SPawel Jakub Dawidek 	/*
52552710de1SPawel Jakub Dawidek 	 * Must disallow TCP ``connections'' to multicast addresses.
52652710de1SPawel Jakub Dawidek 	 */
527f161d294SMark Johnston 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
52852710de1SPawel Jakub Dawidek 		return (EAFNOSUPPORT);
529fb59c426SYoshinobu Inoue 
530623dce13SRobert Watson 	inp = sotoinpcb(so);
531623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
5328501a69cSRobert Watson 	INP_WLOCK(inp);
5334a91aa8fSMichael Tuexen 	vflagsav = inp->inp_vflag;
5344a91aa8fSMichael Tuexen 	incflagsav = inp->inp_inc.inc_flags;
535eb96dc33SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
536eb96dc33SJulien Charbon 		error = ECONNREFUSED;
537623dce13SRobert Watson 		goto out;
538623dce13SRobert Watson 	}
539bd4a39ccSMark Johnston 	if (SOLISTENING(so)) {
540bd4a39ccSMark Johnston 		error = EINVAL;
541bd4a39ccSMark Johnston 		goto out;
542bd4a39ccSMark Johnston 	}
543623dce13SRobert Watson 	tp = intotcpcb(inp);
544b287c6c7SBjoern A. Zeeb #ifdef INET
545fa046d87SRobert Watson 	/*
546fa046d87SRobert Watson 	 * XXXRW: Some confusion: V4/V6 flags relate to binding, and
547fa046d87SRobert Watson 	 * therefore probably require the hash lock, which isn't held here.
548fa046d87SRobert Watson 	 * Is this a significant problem?
549fa046d87SRobert Watson 	 */
5500ecd976eSBjoern A. Zeeb 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
551fb59c426SYoshinobu Inoue 		struct sockaddr_in sin;
552fb59c426SYoshinobu Inoue 
553d46a5312SMaxim Konovalov 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
554d46a5312SMaxim Konovalov 			error = EINVAL;
555d46a5312SMaxim Konovalov 			goto out;
556d46a5312SMaxim Konovalov 		}
5575dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV4) == 0) {
5585dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
5595dba6adaSMichael Tuexen 			goto out;
5605dba6adaSMichael Tuexen 		}
56133841545SHajimu UMEMOTO 
5620ecd976eSBjoern A. Zeeb 		in6_sin6_2_sin(&sin, sin6);
563888973f5SMichael Tuexen 		if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
564888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
565888973f5SMichael Tuexen 			goto out;
566888973f5SMichael Tuexen 		}
567f903a308SMichael Tuexen 		if (ntohl(sin.sin_addr.s_addr) == INADDR_BROADCAST) {
568f903a308SMichael Tuexen 			error = EACCES;
5692cf21ae5SRandall Stewart 			goto out;
5702cf21ae5SRandall Stewart 		}
571b89e82ddSJamie Gritton 		if ((error = prison_remote_ip4(td->td_ucred,
572b89e82ddSJamie Gritton 		    &sin.sin_addr)) != 0)
573413628a7SBjoern A. Zeeb 			goto out;
5744a91aa8fSMichael Tuexen 		inp->inp_vflag |= INP_IPV4;
5754a91aa8fSMichael Tuexen 		inp->inp_vflag &= ~INP_IPV6;
576c1604fe4SGleb Smirnoff 		NET_EPOCH_ENTER(et);
577a9d22cceSGleb Smirnoff 		if ((error = tcp_connect(tp, &sin, td)) != 0)
578c1604fe4SGleb Smirnoff 			goto out_in_epoch;
57909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
58009fe6320SNavdeep Parhar 		if (registered_toedevs > 0 &&
581adfaf8f6SNavdeep Parhar 		    (so->so_options & SO_NO_OFFLOAD) == 0 &&
58209fe6320SNavdeep Parhar 		    (error = tcp_offload_connect(so, nam)) == 0)
583c1604fe4SGleb Smirnoff 			goto out_in_epoch;
58409fe6320SNavdeep Parhar #endif
58540fa3e40SGleb Smirnoff 		error = tcp_output(tp);
586c1604fe4SGleb Smirnoff 		goto out_in_epoch;
5875dba6adaSMichael Tuexen 	} else {
5885dba6adaSMichael Tuexen 		if ((inp->inp_vflag & INP_IPV6) == 0) {
5895dba6adaSMichael Tuexen 			error = EAFNOSUPPORT;
5905dba6adaSMichael Tuexen 			goto out;
5915dba6adaSMichael Tuexen 		}
592fb59c426SYoshinobu Inoue 	}
593b287c6c7SBjoern A. Zeeb #endif
5944a91aa8fSMichael Tuexen 	if ((error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr)) != 0)
5954a91aa8fSMichael Tuexen 		goto out;
596fb59c426SYoshinobu Inoue 	inp->inp_vflag &= ~INP_IPV4;
597fb59c426SYoshinobu Inoue 	inp->inp_vflag |= INP_IPV6;
598dcdb4371SBjoern A. Zeeb 	inp->inp_inc.inc_flags |= INC_ISIPV6;
5990773b44eSGleb Smirnoff 	NET_EPOCH_ENTER(et);
600a9d22cceSGleb Smirnoff 	if ((error = tcp6_connect(tp, sin6, td)) != 0)
6010773b44eSGleb Smirnoff 		goto out_in_epoch;
60209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
60309fe6320SNavdeep Parhar 	if (registered_toedevs > 0 &&
604adfaf8f6SNavdeep Parhar 	    (so->so_options & SO_NO_OFFLOAD) == 0 &&
60509fe6320SNavdeep Parhar 	    (error = tcp_offload_connect(so, nam)) == 0)
6060773b44eSGleb Smirnoff 		goto out_in_epoch;
60709fe6320SNavdeep Parhar #endif
60809fe6320SNavdeep Parhar 	tcp_timer_activate(tp, TT_KEEP, TP_KEEPINIT(tp));
60940fa3e40SGleb Smirnoff 	error = tcp_output(tp);
610c1604fe4SGleb Smirnoff out_in_epoch:
611109eb549SGleb Smirnoff 	NET_EPOCH_EXIT(et);
612623dce13SRobert Watson out:
6131d41a494SGleb Smirnoff 	KASSERT(error >= 0, ("TCP stack %s requested tcp_drop(%p) at connect()"
6141d41a494SGleb Smirnoff 	    ", error code %d", tp->t_fb->tfb_tcp_block_name, tp, -error));
6154a91aa8fSMichael Tuexen 	/*
6164a91aa8fSMichael Tuexen 	 * If the implicit bind in the connect call fails, restore
6174a91aa8fSMichael Tuexen 	 * the flags we modified.
6184a91aa8fSMichael Tuexen 	 */
6194a91aa8fSMichael Tuexen 	if (error != 0 && inp->inp_lport == 0) {
6204a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
6214a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
6224a91aa8fSMichael Tuexen 	}
6234a91aa8fSMichael Tuexen 
6245d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_CONNECT);
6258501a69cSRobert Watson 	INP_WUNLOCK(inp);
626623dce13SRobert Watson 	return (error);
627fb59c426SYoshinobu Inoue }
628fb59c426SYoshinobu Inoue #endif /* INET6 */
629fb59c426SYoshinobu Inoue 
6302c37256eSGarrett Wollman /*
6312c37256eSGarrett Wollman  * Initiate disconnect from peer.
6322c37256eSGarrett Wollman  * If connection never passed embryonic stage, just drop;
6332c37256eSGarrett Wollman  * else if don't need to let data drain, then can just drop anyways,
6342c37256eSGarrett Wollman  * else have to begin TCP shutdown process: mark socket disconnecting,
6352c37256eSGarrett Wollman  * drain unread data, state switch to reflect user close, and
6362c37256eSGarrett Wollman  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
6372c37256eSGarrett Wollman  * when peer sends FIN and acks ours.
6382c37256eSGarrett Wollman  *
6392c37256eSGarrett Wollman  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
6402c37256eSGarrett Wollman  */
6412c37256eSGarrett Wollman static int
6422c37256eSGarrett Wollman tcp_usr_disconnect(struct socket *so)
6432c37256eSGarrett Wollman {
644f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
645623dce13SRobert Watson 	struct tcpcb *tp = NULL;
6466573d758SMatt Macy 	struct epoch_tracker et;
647623dce13SRobert Watson 	int error = 0;
6482c37256eSGarrett Wollman 
64997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
650623dce13SRobert Watson 	inp = sotoinpcb(so);
651623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
6528501a69cSRobert Watson 	INP_WLOCK(inp);
653489dcc92SJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
65421367f63SSam Leffler 		error = ECONNRESET;
655623dce13SRobert Watson 		goto out;
656623dce13SRobert Watson 	}
657623dce13SRobert Watson 	tp = intotcpcb(inp);
658623dce13SRobert Watson 	tcp_disconnect(tp);
659623dce13SRobert Watson out:
6605d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_DISCONNECT);
6618501a69cSRobert Watson 	INP_WUNLOCK(inp);
66297a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
663623dce13SRobert Watson 	return (error);
6642c37256eSGarrett Wollman }
6652c37256eSGarrett Wollman 
666b287c6c7SBjoern A. Zeeb #ifdef INET
6672c37256eSGarrett Wollman /*
6688296cddfSRobert Watson  * Accept a connection.  Essentially all the work is done at higher levels;
6698296cddfSRobert Watson  * just return the address of the peer, storing through addr.
6702c37256eSGarrett Wollman  */
6712c37256eSGarrett Wollman static int
67257bf258eSGarrett Wollman tcp_usr_accept(struct socket *so, struct sockaddr **nam)
6732c37256eSGarrett Wollman {
6742c37256eSGarrett Wollman 	int error = 0;
675f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
676b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
6771db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
678b338b1fdSMateusz Guzik #endif
67926ef6ac4SDon Lewis 	struct in_addr addr;
68026ef6ac4SDon Lewis 	in_port_t port = 0;
6812c37256eSGarrett Wollman 
6823d2d3ef4SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
6833d2d3ef4SRobert Watson 		return (ECONNABORTED);
684f76fcf6dSJeffrey Hsu 
685f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
686623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
6878501a69cSRobert Watson 	INP_WLOCK(inp);
68853af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
6893d2d3ef4SRobert Watson 		error = ECONNABORTED;
690623dce13SRobert Watson 		goto out;
691623dce13SRobert Watson 	}
692b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
6931db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
694b338b1fdSMateusz Guzik #endif
695f76fcf6dSJeffrey Hsu 
696f76fcf6dSJeffrey Hsu 	/*
69754d642bbSRobert Watson 	 * We inline in_getpeeraddr and COMMON_END here, so that we can
69826ef6ac4SDon Lewis 	 * copy the data of interest and defer the malloc until after we
69926ef6ac4SDon Lewis 	 * release the lock.
700f76fcf6dSJeffrey Hsu 	 */
70126ef6ac4SDon Lewis 	port = inp->inp_fport;
70226ef6ac4SDon Lewis 	addr = inp->inp_faddr;
703f76fcf6dSJeffrey Hsu 
704623dce13SRobert Watson out:
7055d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7068501a69cSRobert Watson 	INP_WUNLOCK(inp);
70726ef6ac4SDon Lewis 	if (error == 0)
70826ef6ac4SDon Lewis 		*nam = in_sockaddr(port, &addr);
70926ef6ac4SDon Lewis 	return error;
7102c37256eSGarrett Wollman }
711b287c6c7SBjoern A. Zeeb #endif /* INET */
7122c37256eSGarrett Wollman 
713fb59c426SYoshinobu Inoue #ifdef INET6
714fb59c426SYoshinobu Inoue static int
715fb59c426SYoshinobu Inoue tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
716fb59c426SYoshinobu Inoue {
717f76fcf6dSJeffrey Hsu 	struct inpcb *inp = NULL;
718fb59c426SYoshinobu Inoue 	int error = 0;
719b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7201db24ffbSJonathan Lemon 	struct tcpcb *tp = NULL;
721b338b1fdSMateusz Guzik #endif
72226ef6ac4SDon Lewis 	struct in_addr addr;
72326ef6ac4SDon Lewis 	struct in6_addr addr6;
7246573d758SMatt Macy 	struct epoch_tracker et;
72526ef6ac4SDon Lewis 	in_port_t port = 0;
72626ef6ac4SDon Lewis 	int v4 = 0;
727fb59c426SYoshinobu Inoue 
728b4470c16SRobert Watson 	if (so->so_state & SS_ISDISCONNECTED)
729b4470c16SRobert Watson 		return (ECONNABORTED);
730f76fcf6dSJeffrey Hsu 
731f76fcf6dSJeffrey Hsu 	inp = sotoinpcb(so);
732623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
73397a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
7348501a69cSRobert Watson 	INP_WLOCK(inp);
73553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
73621367f63SSam Leffler 		error = ECONNABORTED;
737623dce13SRobert Watson 		goto out;
738623dce13SRobert Watson 	}
739b338b1fdSMateusz Guzik #ifdef KDTRACE_HOOKS
7401db24ffbSJonathan Lemon 	tp = intotcpcb(inp);
741b338b1fdSMateusz Guzik #endif
742623dce13SRobert Watson 
74326ef6ac4SDon Lewis 	/*
74426ef6ac4SDon Lewis 	 * We inline in6_mapped_peeraddr 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.
74726ef6ac4SDon Lewis 	 */
74826ef6ac4SDon Lewis 	if (inp->inp_vflag & INP_IPV4) {
74926ef6ac4SDon Lewis 		v4 = 1;
75026ef6ac4SDon Lewis 		port = inp->inp_fport;
75126ef6ac4SDon Lewis 		addr = inp->inp_faddr;
75226ef6ac4SDon Lewis 	} else {
75326ef6ac4SDon Lewis 		port = inp->inp_fport;
75426ef6ac4SDon Lewis 		addr6 = inp->in6p_faddr;
75526ef6ac4SDon Lewis 	}
75626ef6ac4SDon Lewis 
757623dce13SRobert Watson out:
7585d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_ACCEPT);
7598501a69cSRobert Watson 	INP_WUNLOCK(inp);
76097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
76126ef6ac4SDon Lewis 	if (error == 0) {
76226ef6ac4SDon Lewis 		if (v4)
76326ef6ac4SDon Lewis 			*nam = in6_v4mapsin6_sockaddr(port, &addr);
76426ef6ac4SDon Lewis 		else
76526ef6ac4SDon Lewis 			*nam = in6_sockaddr(port, &addr6);
76626ef6ac4SDon Lewis 	}
76726ef6ac4SDon Lewis 	return error;
768fb59c426SYoshinobu Inoue }
769fb59c426SYoshinobu Inoue #endif /* INET6 */
770f76fcf6dSJeffrey Hsu 
771f76fcf6dSJeffrey Hsu /*
7722c37256eSGarrett Wollman  * Mark the connection as being incapable of further output.
7732c37256eSGarrett Wollman  */
7742c37256eSGarrett Wollman static int
7752c37256eSGarrett Wollman tcp_usr_shutdown(struct socket *so)
7762c37256eSGarrett Wollman {
7772c37256eSGarrett Wollman 	int error = 0;
778f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
779623dce13SRobert Watson 	struct tcpcb *tp = NULL;
7806573d758SMatt Macy 	struct epoch_tracker et;
7812c37256eSGarrett Wollman 
782623dce13SRobert Watson 	inp = sotoinpcb(so);
783623dce13SRobert Watson 	KASSERT(inp != NULL, ("inp == NULL"));
7848501a69cSRobert Watson 	INP_WLOCK(inp);
78553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
7860af4ce45SGleb Smirnoff 		INP_WUNLOCK(inp);
7870af4ce45SGleb Smirnoff 		return (ECONNRESET);
788623dce13SRobert Watson 	}
7890af4ce45SGleb Smirnoff 	tp = intotcpcb(inp);
7900af4ce45SGleb Smirnoff 	NET_EPOCH_ENTER(et);
7912c37256eSGarrett Wollman 	socantsendmore(so);
792623dce13SRobert Watson 	tcp_usrclosed(tp);
793ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED))
794f64dc2abSGleb Smirnoff 		error = tcp_output_nodrop(tp);
7955d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_SHUTDOWN);
796f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
79797a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
798623dce13SRobert Watson 
799623dce13SRobert Watson 	return (error);
8002c37256eSGarrett Wollman }
8012c37256eSGarrett Wollman 
8022c37256eSGarrett Wollman /*
8032c37256eSGarrett Wollman  * After a receive, possibly send window update to peer.
8042c37256eSGarrett Wollman  */
8052c37256eSGarrett Wollman static int
8062c37256eSGarrett Wollman tcp_usr_rcvd(struct socket *so, int flags)
8072c37256eSGarrett Wollman {
808109eb549SGleb Smirnoff 	struct epoch_tracker et;
809f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
810623dce13SRobert Watson 	struct tcpcb *tp = NULL;
811f64dc2abSGleb Smirnoff 	int outrv = 0, error = 0;
8122c37256eSGarrett Wollman 
813623dce13SRobert Watson 	inp = sotoinpcb(so);
814623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
8158501a69cSRobert Watson 	INP_WLOCK(inp);
81653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
81737a7f557SGleb Smirnoff 		INP_WUNLOCK(inp);
81837a7f557SGleb Smirnoff 		return (ECONNRESET);
819623dce13SRobert Watson 	}
82037a7f557SGleb Smirnoff 	tp = intotcpcb(inp);
82137a7f557SGleb Smirnoff 	NET_EPOCH_ENTER(et);
822281a0fd4SPatrick Kelsey 	/*
823281a0fd4SPatrick Kelsey 	 * For passively-created TFO connections, don't attempt a window
824281a0fd4SPatrick Kelsey 	 * update while still in SYN_RECEIVED as this may trigger an early
825281a0fd4SPatrick Kelsey 	 * SYN|ACK.  It is preferable to have the SYN|ACK be sent along with
826281a0fd4SPatrick Kelsey 	 * application response data, or failing that, when the DELACK timer
827281a0fd4SPatrick Kelsey 	 * expires.
828281a0fd4SPatrick Kelsey 	 */
82968bd7ed1SJonathan T. Looney 	if (IS_FASTOPEN(tp->t_flags) &&
830281a0fd4SPatrick Kelsey 	    (tp->t_state == TCPS_SYN_RECEIVED))
831281a0fd4SPatrick Kelsey 		goto out;
83209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
83309fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE)
83409fe6320SNavdeep Parhar 		tcp_offload_rcvd(tp);
835460cf046SNavdeep Parhar 	else
83609fe6320SNavdeep Parhar #endif
837f64dc2abSGleb Smirnoff 		outrv = tcp_output_nodrop(tp);
838623dce13SRobert Watson out:
8395d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVD);
840f64dc2abSGleb Smirnoff 	(void) tcp_unlock_or_drop(tp, outrv);
841f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
842623dce13SRobert Watson 	return (error);
8432c37256eSGarrett Wollman }
8442c37256eSGarrett Wollman 
8452c37256eSGarrett Wollman /*
8462c37256eSGarrett Wollman  * Do a send by putting data in output queue and updating urgent
8479c9906e9SPeter Wemm  * marker if URG set.  Possibly send more data.  Unlike the other
8489c9906e9SPeter Wemm  * pru_*() routines, the mbuf chains are our responsibility.  We
8499c9906e9SPeter Wemm  * must either enqueue them or free them.  The other pru_* routines
8509c9906e9SPeter Wemm  * generally are caller-frees.
8512c37256eSGarrett Wollman  */
8522c37256eSGarrett Wollman static int
85357bf258eSGarrett Wollman tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
854b40ce416SJulian Elischer     struct sockaddr *nam, struct mbuf *control, struct thread *td)
8552c37256eSGarrett Wollman {
85697a95ee1SGleb Smirnoff 	struct epoch_tracker et;
8572c37256eSGarrett Wollman 	int error = 0;
858f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
859623dce13SRobert Watson 	struct tcpcb *tp = NULL;
860888973f5SMichael Tuexen #ifdef INET
86151e08d53SMichael Tuexen #ifdef INET6
86251e08d53SMichael Tuexen 	struct sockaddr_in sin;
86351e08d53SMichael Tuexen #endif
86451e08d53SMichael Tuexen 	struct sockaddr_in *sinp;
865888973f5SMichael Tuexen #endif
866fb59c426SYoshinobu Inoue #ifdef INET6
867a9d22cceSGleb Smirnoff 	struct sockaddr_in6 *sin6;
868fb59c426SYoshinobu Inoue 	int isipv6;
869fb59c426SYoshinobu Inoue #endif
8704a91aa8fSMichael Tuexen 	u_int8_t incflagsav;
8714a91aa8fSMichael Tuexen 	u_char vflagsav;
8724a91aa8fSMichael Tuexen 	bool restoreflags;
8732c37256eSGarrett Wollman 
874d8acd268SMark Johnston 	if (control != NULL) {
875d8acd268SMark Johnston 		/* TCP doesn't do control messages (rights, creds, etc) */
876d8acd268SMark Johnston 		if (control->m_len) {
877d8acd268SMark Johnston 			m_freem(control);
8784287aa56SGleb Smirnoff 			return (EINVAL);
879d8acd268SMark Johnston 		}
880d8acd268SMark Johnston 		m_freem(control);	/* empty control, just free it */
881d8acd268SMark Johnston 	}
8824287aa56SGleb Smirnoff 
8834287aa56SGleb Smirnoff 	inp = sotoinpcb(so);
8844287aa56SGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
8854287aa56SGleb Smirnoff 	INP_WLOCK(inp);
88653af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
8874287aa56SGleb Smirnoff 		if (m != NULL && (flags & PRUS_NOTREADY) == 0)
8884287aa56SGleb Smirnoff 			m_freem(m);
8894287aa56SGleb Smirnoff 		INP_WUNLOCK(inp);
8904287aa56SGleb Smirnoff 		return (ECONNRESET);
8914287aa56SGleb Smirnoff 	}
8924287aa56SGleb Smirnoff 
8934287aa56SGleb Smirnoff 	vflagsav = inp->inp_vflag;
8944287aa56SGleb Smirnoff 	incflagsav = inp->inp_inc.inc_flags;
8954287aa56SGleb Smirnoff 	restoreflags = false;
8964287aa56SGleb Smirnoff 	tp = intotcpcb(inp);
8974287aa56SGleb Smirnoff 
8984287aa56SGleb Smirnoff 	NET_EPOCH_ENTER(et);
8997d2608a5SMark Johnston 	if ((flags & PRUS_OOB) != 0 &&
9007d2608a5SMark Johnston 	    (error = tcp_pru_options_support(tp, PRUS_OOB)) != 0)
901d3b6c96bSRandall Stewart 		goto out;
9027d2608a5SMark Johnston 
903888973f5SMichael Tuexen 	if (nam != NULL && tp->t_state < TCPS_SYN_SENT) {
904bd4a39ccSMark Johnston 		if (tp->t_state == TCPS_LISTEN) {
905bd4a39ccSMark Johnston 			error = EINVAL;
906bd4a39ccSMark Johnston 			goto out;
907bd4a39ccSMark Johnston 		}
908888973f5SMichael Tuexen 		switch (nam->sa_family) {
909888973f5SMichael Tuexen #ifdef INET
910888973f5SMichael Tuexen 		case AF_INET:
911888973f5SMichael Tuexen 			sinp = (struct sockaddr_in *)nam;
912888973f5SMichael Tuexen 			if (sinp->sin_len != sizeof(struct sockaddr_in)) {
913888973f5SMichael Tuexen 				error = EINVAL;
914888973f5SMichael Tuexen 				goto out;
915888973f5SMichael Tuexen 			}
916888973f5SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6) != 0) {
917888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
918888973f5SMichael Tuexen 				goto out;
919888973f5SMichael Tuexen 			}
920888973f5SMichael Tuexen 			if (IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
921888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
922888973f5SMichael Tuexen 				goto out;
923888973f5SMichael Tuexen 			}
924f903a308SMichael Tuexen 			if (ntohl(sinp->sin_addr.s_addr) == INADDR_BROADCAST) {
925f903a308SMichael Tuexen 				error = EACCES;
9262cf21ae5SRandall Stewart 				goto out;
9272cf21ae5SRandall Stewart 			}
928888973f5SMichael Tuexen 			if ((error = prison_remote_ip4(td->td_ucred,
9297d2608a5SMark Johnston 			    &sinp->sin_addr)))
930888973f5SMichael Tuexen 				goto out;
931888973f5SMichael Tuexen #ifdef INET6
932888973f5SMichael Tuexen 			isipv6 = 0;
933888973f5SMichael Tuexen #endif
934888973f5SMichael Tuexen 			break;
935888973f5SMichael Tuexen #endif /* INET */
936888973f5SMichael Tuexen #ifdef INET6
937888973f5SMichael Tuexen 		case AF_INET6:
9380ecd976eSBjoern A. Zeeb 			sin6 = (struct sockaddr_in6 *)nam;
9390ecd976eSBjoern A. Zeeb 			if (sin6->sin6_len != sizeof(*sin6)) {
940888973f5SMichael Tuexen 				error = EINVAL;
941888973f5SMichael Tuexen 				goto out;
942888973f5SMichael Tuexen 			}
943e240ce42SMichael Tuexen 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0) {
944e240ce42SMichael Tuexen 				error = EAFNOSUPPORT;
945e240ce42SMichael Tuexen 				goto out;
946e240ce42SMichael Tuexen 			}
9470ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
948888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
949888973f5SMichael Tuexen 				goto out;
950888973f5SMichael Tuexen 			}
9510ecd976eSBjoern A. Zeeb 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
952888973f5SMichael Tuexen #ifdef INET
953888973f5SMichael Tuexen 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
954888973f5SMichael Tuexen 					error = EINVAL;
955888973f5SMichael Tuexen 					goto out;
956888973f5SMichael Tuexen 				}
957888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV4) == 0) {
958888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
959888973f5SMichael Tuexen 					goto out;
960888973f5SMichael Tuexen 				}
9614a91aa8fSMichael Tuexen 				restoreflags = true;
962888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV6;
963888973f5SMichael Tuexen 				sinp = &sin;
9640ecd976eSBjoern A. Zeeb 				in6_sin6_2_sin(sinp, sin6);
965888973f5SMichael Tuexen 				if (IN_MULTICAST(
966888973f5SMichael Tuexen 				    ntohl(sinp->sin_addr.s_addr))) {
967888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
968888973f5SMichael Tuexen 					goto out;
969888973f5SMichael Tuexen 				}
970888973f5SMichael Tuexen 				if ((error = prison_remote_ip4(td->td_ucred,
9717d2608a5SMark Johnston 				    &sinp->sin_addr)))
972888973f5SMichael Tuexen 					goto out;
973888973f5SMichael Tuexen 				isipv6 = 0;
974888973f5SMichael Tuexen #else /* !INET */
975888973f5SMichael Tuexen 				error = EAFNOSUPPORT;
976888973f5SMichael Tuexen 				goto out;
977888973f5SMichael Tuexen #endif /* INET */
978888973f5SMichael Tuexen 			} else {
979888973f5SMichael Tuexen 				if ((inp->inp_vflag & INP_IPV6) == 0) {
980888973f5SMichael Tuexen 					error = EAFNOSUPPORT;
981888973f5SMichael Tuexen 					goto out;
982888973f5SMichael Tuexen 				}
9834a91aa8fSMichael Tuexen 				restoreflags = true;
984888973f5SMichael Tuexen 				inp->inp_vflag &= ~INP_IPV4;
985888973f5SMichael Tuexen 				inp->inp_inc.inc_flags |= INC_ISIPV6;
986888973f5SMichael Tuexen 				if ((error = prison_remote_ip6(td->td_ucred,
9877d2608a5SMark Johnston 				    &sin6->sin6_addr)))
988888973f5SMichael Tuexen 					goto out;
989888973f5SMichael Tuexen 				isipv6 = 1;
990888973f5SMichael Tuexen 			}
991888973f5SMichael Tuexen 			break;
992888973f5SMichael Tuexen #endif /* INET6 */
993888973f5SMichael Tuexen 		default:
994888973f5SMichael Tuexen 			error = EAFNOSUPPORT;
995888973f5SMichael Tuexen 			goto out;
996888973f5SMichael Tuexen 		}
997888973f5SMichael Tuexen 	}
9982c37256eSGarrett Wollman 	if (!(flags & PRUS_OOB)) {
99908af8aacSRandall Stewart 		if (tp->t_acktime == 0)
100008af8aacSRandall Stewart 			tp->t_acktime = ticks;
1001651e4e6aSGleb Smirnoff 		sbappendstream(&so->so_snd, m, flags);
10027d2608a5SMark Johnston 		m = NULL;
10032c37256eSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1004bd4a39ccSMark Johnston 			KASSERT(tp->t_state == TCPS_CLOSED,
1005bd4a39ccSMark Johnston 			    ("%s: tp %p is listening", __func__, tp));
1006bd4a39ccSMark Johnston 
10072c37256eSGarrett Wollman 			/*
10082c37256eSGarrett Wollman 			 * Do implied connect if not yet connected,
10092c37256eSGarrett Wollman 			 * initialize window to default value, and
10100c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
10112c37256eSGarrett Wollman 			 */
1012fb59c426SYoshinobu Inoue #ifdef INET6
1013fb59c426SYoshinobu Inoue 			if (isipv6)
1014a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1015fb59c426SYoshinobu Inoue #endif /* INET6 */
1016b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1017b287c6c7SBjoern A. Zeeb 			else
1018b287c6c7SBjoern A. Zeeb #endif
1019b287c6c7SBjoern A. Zeeb #ifdef INET
1020a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1021b287c6c7SBjoern A. Zeeb #endif
10224a91aa8fSMichael Tuexen 			/*
10234a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
10244a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
10254a91aa8fSMichael Tuexen 			 * operations fail.
10264a91aa8fSMichael Tuexen 			 */
10274a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
10284a91aa8fSMichael Tuexen 				restoreflags = false;
10294a91aa8fSMichael Tuexen 
10307d2608a5SMark Johnston 			if (error) {
10317d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
10327d2608a5SMark Johnston 				sbflush(&so->so_snd);
10332c37256eSGarrett Wollman 				goto out;
10347d2608a5SMark Johnston 			}
1035c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1036c560df6fSPatrick Kelsey 				tcp_fastopen_connect(tp);
103718a75309SPatrick Kelsey 			else {
10382c37256eSGarrett Wollman 				tp->snd_wnd = TTCP_CLIENT_SND_WND;
10392c37256eSGarrett Wollman 				tcp_mss(tp, -1);
10402c37256eSGarrett Wollman 			}
1041c560df6fSPatrick Kelsey 		}
10422c37256eSGarrett Wollman 		if (flags & PRUS_EOF) {
10432c37256eSGarrett Wollman 			/*
10442c37256eSGarrett Wollman 			 * Close the send side of the connection after
10452c37256eSGarrett Wollman 			 * the data is sent.
10462c37256eSGarrett Wollman 			 */
10472c37256eSGarrett Wollman 			socantsendmore(so);
1048623dce13SRobert Watson 			tcp_usrclosed(tp);
10492c37256eSGarrett Wollman 		}
1050e854dd38SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) &&
1051e854dd38SRandall Stewart 		    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1052e854dd38SRandall Stewart 		    (tp->t_fbyte_out == 0) &&
1053e854dd38SRandall Stewart 		    (so->so_snd.sb_ccc > 0)) {
1054e854dd38SRandall Stewart 			tp->t_fbyte_out = ticks;
1055e854dd38SRandall Stewart 			if (tp->t_fbyte_out == 0)
1056e854dd38SRandall Stewart 				tp->t_fbyte_out = 1;
1057e854dd38SRandall Stewart 			if (tp->t_fbyte_out && tp->t_fbyte_in)
1058e854dd38SRandall Stewart 				tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1059e854dd38SRandall Stewart 		}
10602cbcd3c1SGleb Smirnoff 		if (!(inp->inp_flags & INP_DROPPED) &&
10612cbcd3c1SGleb Smirnoff 		    !(flags & PRUS_NOTREADY)) {
1062b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1063b0acefa8SBill Fenner 				tp->t_flags |= TF_MORETOCOME;
1064f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
1065b0acefa8SBill Fenner 			if (flags & PRUS_MORETOCOME)
1066b0acefa8SBill Fenner 				tp->t_flags &= ~TF_MORETOCOME;
1067b0acefa8SBill Fenner 		}
10682c37256eSGarrett Wollman 	} else {
1069623dce13SRobert Watson 		/*
1070623dce13SRobert Watson 		 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
1071623dce13SRobert Watson 		 */
1072d2bc35abSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
10732c37256eSGarrett Wollman 		if (sbspace(&so->so_snd) < -512) {
1074d2bc35abSRobert Watson 			SOCKBUF_UNLOCK(&so->so_snd);
10752c37256eSGarrett Wollman 			error = ENOBUFS;
10762c37256eSGarrett Wollman 			goto out;
10772c37256eSGarrett Wollman 		}
10782c37256eSGarrett Wollman 		/*
10792c37256eSGarrett Wollman 		 * According to RFC961 (Assigned Protocols),
10802c37256eSGarrett Wollman 		 * the urgent pointer points to the last octet
10812c37256eSGarrett Wollman 		 * of urgent data.  We continue, however,
10822c37256eSGarrett Wollman 		 * to consider it to indicate the first octet
10832c37256eSGarrett Wollman 		 * of data past the urgent section.
10842c37256eSGarrett Wollman 		 * Otherwise, snd_up should be one lower.
10852c37256eSGarrett Wollman 		 */
108608af8aacSRandall Stewart 		if (tp->t_acktime == 0)
108708af8aacSRandall Stewart 			tp->t_acktime = ticks;
1088651e4e6aSGleb Smirnoff 		sbappendstream_locked(&so->so_snd, m, flags);
1089d2bc35abSRobert Watson 		SOCKBUF_UNLOCK(&so->so_snd);
10907d2608a5SMark Johnston 		m = NULL;
1091ef53690bSGarrett Wollman 		if (nam && tp->t_state < TCPS_SYN_SENT) {
1092ef53690bSGarrett Wollman 			/*
1093ef53690bSGarrett Wollman 			 * Do implied connect if not yet connected,
1094ef53690bSGarrett Wollman 			 * initialize window to default value, and
10950c39d38dSGleb Smirnoff 			 * initialize maxseg using peer's cached MSS.
1096ef53690bSGarrett Wollman 			 */
109718a75309SPatrick Kelsey 
1098c560df6fSPatrick Kelsey 			/*
1099c560df6fSPatrick Kelsey 			 * Not going to contemplate SYN|URG
1100c560df6fSPatrick Kelsey 			 */
1101c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags))
1102c560df6fSPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
1103fb59c426SYoshinobu Inoue #ifdef INET6
1104fb59c426SYoshinobu Inoue 			if (isipv6)
1105a9d22cceSGleb Smirnoff 				error = tcp6_connect(tp, sin6, td);
1106fb59c426SYoshinobu Inoue #endif /* INET6 */
1107b287c6c7SBjoern A. Zeeb #if defined(INET6) && defined(INET)
1108b287c6c7SBjoern A. Zeeb 			else
1109b287c6c7SBjoern A. Zeeb #endif
1110b287c6c7SBjoern A. Zeeb #ifdef INET
1111a9d22cceSGleb Smirnoff 				error = tcp_connect(tp, sinp, td);
1112b287c6c7SBjoern A. Zeeb #endif
11134a91aa8fSMichael Tuexen 			/*
11144a91aa8fSMichael Tuexen 			 * The bind operation in tcp_connect succeeded. We
11154a91aa8fSMichael Tuexen 			 * no longer want to restore the flags if later
11164a91aa8fSMichael Tuexen 			 * operations fail.
11174a91aa8fSMichael Tuexen 			 */
11184a91aa8fSMichael Tuexen 			if (error == 0 || inp->inp_lport != 0)
11194a91aa8fSMichael Tuexen 				restoreflags = false;
11204a91aa8fSMichael Tuexen 
11217d2608a5SMark Johnston 			if (error != 0) {
11227d2608a5SMark Johnston 				/* m is freed if PRUS_NOTREADY is unset. */
11237d2608a5SMark Johnston 				sbflush(&so->so_snd);
1124ef53690bSGarrett Wollman 				goto out;
11257d2608a5SMark Johnston 			}
1126ef53690bSGarrett Wollman 			tp->snd_wnd = TTCP_CLIENT_SND_WND;
1127ef53690bSGarrett Wollman 			tcp_mss(tp, -1);
1128623dce13SRobert Watson 		}
1129300fa232SGleb Smirnoff 		tp->snd_up = tp->snd_una + sbavail(&so->so_snd);
11307d2608a5SMark Johnston 		if ((flags & PRUS_NOTREADY) == 0) {
11312cdbfa66SPaul Saab 			tp->t_flags |= TF_FORCEDATA;
1132f64dc2abSGleb Smirnoff 			error = tcp_output_nodrop(tp);
11332cdbfa66SPaul Saab 			tp->t_flags &= ~TF_FORCEDATA;
11342c37256eSGarrett Wollman 		}
11352cbcd3c1SGleb Smirnoff 	}
11362529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, NULL,
11372529f56eSJonathan T. Looney 	    &inp->inp_socket->so_rcv,
11382529f56eSJonathan T. Looney 	    &inp->inp_socket->so_snd,
11392529f56eSJonathan T. Looney 	    TCP_LOG_USERSEND, error,
11402529f56eSJonathan T. Looney 	    0, NULL, false);
11417d2608a5SMark Johnston 
1142d1401c90SRobert Watson out:
11434a91aa8fSMichael Tuexen 	/*
11447d2608a5SMark Johnston 	 * In case of PRUS_NOTREADY, the caller or tcp_usr_ready() is
11457d2608a5SMark Johnston 	 * responsible for freeing memory.
11467d2608a5SMark Johnston 	 */
11477d2608a5SMark Johnston 	if (m != NULL && (flags & PRUS_NOTREADY) == 0)
11487d2608a5SMark Johnston 		m_freem(m);
11497d2608a5SMark Johnston 
11507d2608a5SMark Johnston 	/*
11514a91aa8fSMichael Tuexen 	 * If the request was unsuccessful and we changed flags,
11524a91aa8fSMichael Tuexen 	 * restore the original flags.
11534a91aa8fSMichael Tuexen 	 */
11544a91aa8fSMichael Tuexen 	if (error != 0 && restoreflags) {
11554a91aa8fSMichael Tuexen 		inp->inp_vflag = vflagsav;
11564a91aa8fSMichael Tuexen 		inp->inp_inc.inc_flags = incflagsav;
11574a91aa8fSMichael Tuexen 	}
11585d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, (flags & PRUS_OOB) ? PRU_SENDOOB :
11595d06879aSGeorge V. Neville-Neil 		   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
1160f64dc2abSGleb Smirnoff 	error = tcp_unlock_or_drop(tp, error);
116197a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
116273fddedaSPeter Grehan 	return (error);
11632c37256eSGarrett Wollman }
11642c37256eSGarrett Wollman 
11652cbcd3c1SGleb Smirnoff static int
11662cbcd3c1SGleb Smirnoff tcp_usr_ready(struct socket *so, struct mbuf *m, int count)
11672cbcd3c1SGleb Smirnoff {
1168109eb549SGleb Smirnoff 	struct epoch_tracker et;
11692cbcd3c1SGleb Smirnoff 	struct inpcb *inp;
11702cbcd3c1SGleb Smirnoff 	struct tcpcb *tp;
11712cbcd3c1SGleb Smirnoff 	int error;
11722cbcd3c1SGleb Smirnoff 
11732cbcd3c1SGleb Smirnoff 	inp = sotoinpcb(so);
11742cbcd3c1SGleb Smirnoff 	INP_WLOCK(inp);
117553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
11762cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
117782334850SJohn Baldwin 		mb_free_notready(m, count);
11782cbcd3c1SGleb Smirnoff 		return (ECONNRESET);
11792cbcd3c1SGleb Smirnoff 	}
11802cbcd3c1SGleb Smirnoff 	tp = intotcpcb(inp);
11812cbcd3c1SGleb Smirnoff 
11822cbcd3c1SGleb Smirnoff 	SOCKBUF_LOCK(&so->so_snd);
11832cbcd3c1SGleb Smirnoff 	error = sbready(&so->so_snd, m, count);
11842cbcd3c1SGleb Smirnoff 	SOCKBUF_UNLOCK(&so->so_snd);
1185f64dc2abSGleb Smirnoff 	if (error) {
11862cbcd3c1SGleb Smirnoff 		INP_WUNLOCK(inp);
1187f64dc2abSGleb Smirnoff 		return (error);
1188f64dc2abSGleb Smirnoff 	}
1189f64dc2abSGleb Smirnoff 	NET_EPOCH_ENTER(et);
1190f64dc2abSGleb Smirnoff 	error = tcp_output_unlock(tp);
1191f64dc2abSGleb Smirnoff 	NET_EPOCH_EXIT(et);
11922cbcd3c1SGleb Smirnoff 
11932cbcd3c1SGleb Smirnoff 	return (error);
11942cbcd3c1SGleb Smirnoff }
11952cbcd3c1SGleb Smirnoff 
11962c37256eSGarrett Wollman /*
1197a152f8a3SRobert Watson  * Abort the TCP.  Drop the connection abruptly.
11982c37256eSGarrett Wollman  */
1199ac45e92fSRobert Watson static void
12002c37256eSGarrett Wollman tcp_usr_abort(struct socket *so)
12012c37256eSGarrett Wollman {
1202f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1203a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12046573d758SMatt Macy 	struct epoch_tracker et;
1205c78cbc7bSRobert Watson 
1206ac45e92fSRobert Watson 	inp = sotoinpcb(so);
1207c78cbc7bSRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
1208c78cbc7bSRobert Watson 
120997a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12108501a69cSRobert Watson 	INP_WLOCK(inp);
1211c78cbc7bSRobert Watson 	KASSERT(inp->inp_socket != NULL,
1212c78cbc7bSRobert Watson 	    ("tcp_usr_abort: inp_socket == NULL"));
1213c78cbc7bSRobert Watson 
1214c78cbc7bSRobert Watson 	/*
1215a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, drop.
1216c78cbc7bSRobert Watson 	 */
121753af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1218c78cbc7bSRobert Watson 		tp = intotcpcb(inp);
12198fa799bdSJonathan T. Looney 		tp = tcp_drop(tp, ECONNABORTED);
12208fa799bdSJonathan T. Looney 		if (tp == NULL)
12218fa799bdSJonathan T. Looney 			goto dropped;
12225d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_ABORT);
1223c78cbc7bSRobert Watson 	}
1224ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1225d8596171SGleb Smirnoff 		soref(so);
1226ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1227a152f8a3SRobert Watson 	}
12288501a69cSRobert Watson 	INP_WUNLOCK(inp);
12298fa799bdSJonathan T. Looney dropped:
123097a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
1231a152f8a3SRobert Watson }
1232a152f8a3SRobert Watson 
1233a152f8a3SRobert Watson /*
1234a152f8a3SRobert Watson  * TCP socket is closed.  Start friendly disconnect.
1235a152f8a3SRobert Watson  */
1236a152f8a3SRobert Watson static void
1237a152f8a3SRobert Watson tcp_usr_close(struct socket *so)
1238a152f8a3SRobert Watson {
1239a152f8a3SRobert Watson 	struct inpcb *inp;
1240a152f8a3SRobert Watson 	struct tcpcb *tp = NULL;
12416573d758SMatt Macy 	struct epoch_tracker et;
1242a152f8a3SRobert Watson 
1243a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1244a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
1245a152f8a3SRobert Watson 
124697a95ee1SGleb Smirnoff 	NET_EPOCH_ENTER(et);
12478501a69cSRobert Watson 	INP_WLOCK(inp);
1248a152f8a3SRobert Watson 	KASSERT(inp->inp_socket != NULL,
1249a152f8a3SRobert Watson 	    ("tcp_usr_close: inp_socket == NULL"));
1250a152f8a3SRobert Watson 
1251a152f8a3SRobert Watson 	/*
1252a152f8a3SRobert Watson 	 * If we still have full TCP state, and we're not dropped, initiate
1253a152f8a3SRobert Watson 	 * a disconnect.
1254a152f8a3SRobert Watson 	 */
125553af6903SGleb Smirnoff 	if (!(inp->inp_flags & INP_DROPPED)) {
1256a152f8a3SRobert Watson 		tp = intotcpcb(inp);
125774703901SGleb Smirnoff 		tp->t_flags |= TF_CLOSED;
1258a152f8a3SRobert Watson 		tcp_disconnect(tp);
12595d06879aSGeorge V. Neville-Neil 		TCP_PROBE2(debug__user, tp, PRU_CLOSE);
1260a152f8a3SRobert Watson 	}
1261ad71fe3cSRobert Watson 	if (!(inp->inp_flags & INP_DROPPED)) {
1262d8596171SGleb Smirnoff 		soref(so);
1263ad71fe3cSRobert Watson 		inp->inp_flags |= INP_SOCKREF;
1264a152f8a3SRobert Watson 	}
12658501a69cSRobert Watson 	INP_WUNLOCK(inp);
126697a95ee1SGleb Smirnoff 	NET_EPOCH_EXIT(et);
12672c37256eSGarrett Wollman }
12682c37256eSGarrett Wollman 
1269d3b6c96bSRandall Stewart static int
1270d3b6c96bSRandall Stewart tcp_pru_options_support(struct tcpcb *tp, int flags)
1271d3b6c96bSRandall Stewart {
1272d3b6c96bSRandall Stewart 	/*
1273d3b6c96bSRandall Stewart 	 * If the specific TCP stack has a pru_options
1274d3b6c96bSRandall Stewart 	 * specified then it does not always support
1275d3b6c96bSRandall Stewart 	 * all the PRU_XX options and we must ask it.
1276d3b6c96bSRandall Stewart 	 * If the function is not specified then all
1277d3b6c96bSRandall Stewart 	 * of the PRU_XX options are supported.
1278d3b6c96bSRandall Stewart 	 */
1279d3b6c96bSRandall Stewart 	int ret = 0;
1280d3b6c96bSRandall Stewart 
1281d3b6c96bSRandall Stewart 	if (tp->t_fb->tfb_pru_options) {
1282d3b6c96bSRandall Stewart 		ret = (*tp->t_fb->tfb_pru_options)(tp, flags);
1283d3b6c96bSRandall Stewart 	}
1284d3b6c96bSRandall Stewart 	return (ret);
1285d3b6c96bSRandall Stewart }
1286d3b6c96bSRandall Stewart 
12872c37256eSGarrett Wollman /*
12882c37256eSGarrett Wollman  * Receive out-of-band data.
12892c37256eSGarrett Wollman  */
12902c37256eSGarrett Wollman static int
12912c37256eSGarrett Wollman tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
12922c37256eSGarrett Wollman {
12932c37256eSGarrett Wollman 	int error = 0;
1294f76fcf6dSJeffrey Hsu 	struct inpcb *inp;
1295623dce13SRobert Watson 	struct tcpcb *tp = NULL;
12962c37256eSGarrett Wollman 
1297623dce13SRobert Watson 	inp = sotoinpcb(so);
1298623dce13SRobert Watson 	KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
12998501a69cSRobert Watson 	INP_WLOCK(inp);
130053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
130121367f63SSam Leffler 		error = ECONNRESET;
1302623dce13SRobert Watson 		goto out;
1303623dce13SRobert Watson 	}
1304623dce13SRobert Watson 	tp = intotcpcb(inp);
1305d3b6c96bSRandall Stewart 	error = tcp_pru_options_support(tp, PRUS_OOB);
1306d3b6c96bSRandall Stewart 	if (error) {
1307d3b6c96bSRandall Stewart 		goto out;
1308d3b6c96bSRandall Stewart 	}
13092c37256eSGarrett Wollman 	if ((so->so_oobmark == 0 &&
1310c0b99ffaSRobert Watson 	     (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
13114cc20ab1SSeigo Tanimura 	    so->so_options & SO_OOBINLINE ||
13124cc20ab1SSeigo Tanimura 	    tp->t_oobflags & TCPOOB_HADDATA) {
13132c37256eSGarrett Wollman 		error = EINVAL;
13142c37256eSGarrett Wollman 		goto out;
13152c37256eSGarrett Wollman 	}
13162c37256eSGarrett Wollman 	if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
13172c37256eSGarrett Wollman 		error = EWOULDBLOCK;
13182c37256eSGarrett Wollman 		goto out;
13192c37256eSGarrett Wollman 	}
13202c37256eSGarrett Wollman 	m->m_len = 1;
13212c37256eSGarrett Wollman 	*mtod(m, caddr_t) = tp->t_iobc;
13222c37256eSGarrett Wollman 	if ((flags & MSG_PEEK) == 0)
13232c37256eSGarrett Wollman 		tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1324623dce13SRobert Watson 
1325623dce13SRobert Watson out:
13265d06879aSGeorge V. Neville-Neil 	TCP_PROBE2(debug__user, tp, PRU_RCVOOB);
13278501a69cSRobert Watson 	INP_WUNLOCK(inp);
1328623dce13SRobert Watson 	return (error);
13292c37256eSGarrett Wollman }
13302c37256eSGarrett Wollman 
1331b287c6c7SBjoern A. Zeeb #ifdef INET
1332e7d02be1SGleb Smirnoff struct protosw tcp_protosw = {
1333e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1334e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1335e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL | PR_WANTRCVD |
1336e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1337e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1338e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1339e7d02be1SGleb Smirnoff 	.pr_accept =		tcp_usr_accept,
1340e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1341e7d02be1SGleb Smirnoff 	.pr_bind =		tcp_usr_bind,
1342e7d02be1SGleb Smirnoff 	.pr_connect =		tcp_usr_connect,
1343e7d02be1SGleb Smirnoff 	.pr_control =		in_control,
1344e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1345e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1346e7d02be1SGleb Smirnoff 	.pr_listen =		tcp_usr_listen,
1347e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in_getpeeraddr,
1348e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1349e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1350e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1351e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1352e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1353e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in_getsockaddr,
1354e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1355e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
13562c37256eSGarrett Wollman };
1357b287c6c7SBjoern A. Zeeb #endif /* INET */
1358df8bae1dSRodney W. Grimes 
1359fb59c426SYoshinobu Inoue #ifdef INET6
1360e7d02be1SGleb Smirnoff struct protosw tcp6_protosw = {
1361e7d02be1SGleb Smirnoff 	.pr_type =		SOCK_STREAM,
1362e7d02be1SGleb Smirnoff 	.pr_protocol =		IPPROTO_TCP,
1363e7d02be1SGleb Smirnoff 	.pr_flags =		PR_CONNREQUIRED | PR_IMPLOPCL |PR_WANTRCVD |
1364e7d02be1SGleb Smirnoff 				    PR_CAPATTACH,
1365e7d02be1SGleb Smirnoff 	.pr_ctloutput =		tcp_ctloutput,
1366e7d02be1SGleb Smirnoff 	.pr_abort =		tcp_usr_abort,
1367e7d02be1SGleb Smirnoff 	.pr_accept =		tcp6_usr_accept,
1368e7d02be1SGleb Smirnoff 	.pr_attach =		tcp_usr_attach,
1369e7d02be1SGleb Smirnoff 	.pr_bind =		tcp6_usr_bind,
1370e7d02be1SGleb Smirnoff 	.pr_connect =		tcp6_usr_connect,
1371e7d02be1SGleb Smirnoff 	.pr_control =		in6_control,
1372e7d02be1SGleb Smirnoff 	.pr_detach =		tcp_usr_detach,
1373e7d02be1SGleb Smirnoff 	.pr_disconnect =	tcp_usr_disconnect,
1374e7d02be1SGleb Smirnoff 	.pr_listen =		tcp6_usr_listen,
1375e7d02be1SGleb Smirnoff 	.pr_peeraddr =		in6_mapped_peeraddr,
1376e7d02be1SGleb Smirnoff 	.pr_rcvd =		tcp_usr_rcvd,
1377e7d02be1SGleb Smirnoff 	.pr_rcvoob =		tcp_usr_rcvoob,
1378e7d02be1SGleb Smirnoff 	.pr_send =		tcp_usr_send,
1379e7d02be1SGleb Smirnoff 	.pr_ready =		tcp_usr_ready,
1380e7d02be1SGleb Smirnoff 	.pr_shutdown =		tcp_usr_shutdown,
1381e7d02be1SGleb Smirnoff 	.pr_sockaddr =		in6_mapped_sockaddr,
1382e7d02be1SGleb Smirnoff 	.pr_sosetlabel =	in_pcbsosetlabel,
1383e7d02be1SGleb Smirnoff 	.pr_close =		tcp_usr_close,
1384fb59c426SYoshinobu Inoue };
1385fb59c426SYoshinobu Inoue #endif /* INET6 */
1386fb59c426SYoshinobu Inoue 
1387b287c6c7SBjoern A. Zeeb #ifdef INET
1388a0292f23SGarrett Wollman /*
1389a0292f23SGarrett Wollman  * Common subroutine to open a TCP connection to remote host specified
1390dfc4d218SGleb Smirnoff  * by struct sockaddr_in.  Call in_pcbconnect() to choose local host address
1391dfc4d218SGleb Smirnoff  * and assign a local port number and install the inpcb into the hash.
1392dfc4d218SGleb Smirnoff  * Initialize connection parameters and enter SYN-SENT state.
1393a0292f23SGarrett Wollman  */
13940312fbe9SPoul-Henning Kamp static int
1395a9d22cceSGleb Smirnoff tcp_connect(struct tcpcb *tp, struct sockaddr_in *sin, struct thread *td)
1396a0292f23SGarrett Wollman {
13979e46ff4dSGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
13989eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
1399c3229e05SDavid Greenman 	int error;
1400a0292f23SGarrett Wollman 
1401c1604fe4SGleb Smirnoff 	NET_EPOCH_ASSERT();
14028501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
140376f1499fSGleb Smirnoff 
1404*636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1405*636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1406*636b19eaSMark Johnston 		return (EISCONN);
1407*636b19eaSMark Johnston 
1408fa046d87SRobert Watson 	INP_HASH_WLOCK(&V_tcbinfo);
1409dfc4d218SGleb Smirnoff 	error = in_pcbconnect(inp, sin, td->td_ucred, true);
14109e46ff4dSGleb Smirnoff 	INP_HASH_WUNLOCK(&V_tcbinfo);
1411dfc4d218SGleb Smirnoff 	if (error != 0)
14129e46ff4dSGleb Smirnoff 		return (error);
1413a0292f23SGarrett Wollman 
1414087b55eaSAndre Oppermann 	/*
1415087b55eaSAndre Oppermann 	 * Compute window scaling to request:
1416087b55eaSAndre Oppermann 	 * Scale to fit into sweet spot.  See tcp_syncache.c.
1417087b55eaSAndre Oppermann 	 * XXX: This should move to tcp_output().
1418087b55eaSAndre Oppermann 	 */
1419a0292f23SGarrett Wollman 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
14209b3bc6bfSMike Silbersack 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1421a0292f23SGarrett Wollman 		tp->request_r_scale++;
1422a0292f23SGarrett Wollman 
1423a0292f23SGarrett Wollman 	soisconnecting(so);
142478b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
142557f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14268e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14278e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14288e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1429a0292f23SGarrett Wollman 	tcp_sendseqinit(tp);
1430a45d2726SAndras Olah 
14319e46ff4dSGleb Smirnoff 	return (0);
1432a0292f23SGarrett Wollman }
1433b287c6c7SBjoern A. Zeeb #endif /* INET */
1434a0292f23SGarrett Wollman 
1435fb59c426SYoshinobu Inoue #ifdef INET6
1436fb59c426SYoshinobu Inoue static int
1437a9d22cceSGleb Smirnoff tcp6_connect(struct tcpcb *tp, struct sockaddr_in6 *sin6, struct thread *td)
1438fb59c426SYoshinobu Inoue {
14399eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1440*636b19eaSMark Johnston 	struct socket *so = tptosocket(tp);
1441fb59c426SYoshinobu Inoue 	int error;
1442fb59c426SYoshinobu Inoue 
1443775da7f8SMark Johnston 	NET_EPOCH_ASSERT();
14448501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
1445623dce13SRobert Watson 
1446*636b19eaSMark Johnston 	if (__predict_false((so->so_state &
1447*636b19eaSMark Johnston 	    (SS_ISCONNECTING | SS_ISCONNECTED)) != 0))
1448*636b19eaSMark Johnston 		return (EISCONN);
1449*636b19eaSMark Johnston 
145076f1499fSGleb Smirnoff 	INP_HASH_WLOCK(&V_tcbinfo);
1451a9d22cceSGleb Smirnoff 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1452fa046d87SRobert Watson 	INP_HASH_WUNLOCK(&V_tcbinfo);
145376f1499fSGleb Smirnoff 	if (error != 0)
145476f1499fSGleb Smirnoff 		return (error);
1455fb59c426SYoshinobu Inoue 
1456fb59c426SYoshinobu Inoue 	/* Compute window scaling to request.  */
1457fb59c426SYoshinobu Inoue 	while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1458970caf60SBjoern A. Zeeb 	    (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1459fb59c426SYoshinobu Inoue 		tp->request_r_scale++;
1460fb59c426SYoshinobu Inoue 
1461*636b19eaSMark Johnston 	soisconnecting(so);
146278b50714SRobert Watson 	TCPSTAT_INC(tcps_connattempt);
146357f60867SMark Johnston 	tcp_state_change(tp, TCPS_SYN_SENT);
14648e02b4e0SMichael Tuexen 	tp->iss = tcp_new_isn(&inp->inp_inc);
14658e02b4e0SMichael Tuexen 	if (tp->t_flags & TF_REQ_TSTMP)
14668e02b4e0SMichael Tuexen 		tp->ts_offset = tcp_new_ts_offset(&inp->inp_inc);
1467fb59c426SYoshinobu Inoue 	tcp_sendseqinit(tp);
1468fb59c426SYoshinobu Inoue 
146976f1499fSGleb Smirnoff 	return (0);
1470fb59c426SYoshinobu Inoue }
1471fb59c426SYoshinobu Inoue #endif /* INET6 */
1472fb59c426SYoshinobu Inoue 
1473cfe8b629SGarrett Wollman /*
1474b8af5dfaSRobert Watson  * Export TCP internal state information via a struct tcp_info, based on the
1475b8af5dfaSRobert Watson  * Linux 2.6 API.  Not ABI compatible as our constants are mapped differently
1476b8af5dfaSRobert Watson  * (TCP state machine, etc).  We export all information using FreeBSD-native
1477b8af5dfaSRobert Watson  * constants -- for example, the numeric values for tcpi_state will differ
1478b8af5dfaSRobert Watson  * from Linux.
1479b8af5dfaSRobert Watson  */
1480b8af5dfaSRobert Watson static void
1481ad3f9ab3SAndre Oppermann tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1482b8af5dfaSRobert Watson {
1483b8af5dfaSRobert Watson 
14849eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1485b8af5dfaSRobert Watson 	bzero(ti, sizeof(*ti));
1486b8af5dfaSRobert Watson 
1487b8af5dfaSRobert Watson 	ti->tcpi_state = tp->t_state;
1488b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1489b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
14903529149eSAndre Oppermann 	if (tp->t_flags & TF_SACK_PERMIT)
1491b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_SACK;
1492b8af5dfaSRobert Watson 	if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1493b8af5dfaSRobert Watson 		ti->tcpi_options |= TCPI_OPT_WSCALE;
1494b8af5dfaSRobert Watson 		ti->tcpi_snd_wscale = tp->snd_scale;
1495b8af5dfaSRobert Watson 		ti->tcpi_rcv_wscale = tp->rcv_scale;
1496b8af5dfaSRobert Watson 	}
14973f169c54SRichard Scheffenegger 	if (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
14985a17b6adSMichael Tuexen 		ti->tcpi_options |= TCPI_OPT_ECN;
14991baaf834SBruce M Simpson 
150043d94734SJohn Baldwin 	ti->tcpi_rto = tp->t_rxtcur * tick;
15013ac12506SJonathan T. Looney 	ti->tcpi_last_data_recv = ((uint32_t)ticks - tp->t_rcvtime) * tick;
15021baaf834SBruce M Simpson 	ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
15031baaf834SBruce M Simpson 	ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
15041baaf834SBruce M Simpson 
1505b8af5dfaSRobert Watson 	ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1506b8af5dfaSRobert Watson 	ti->tcpi_snd_cwnd = tp->snd_cwnd;
1507b8af5dfaSRobert Watson 
1508b8af5dfaSRobert Watson 	/*
1509b8af5dfaSRobert Watson 	 * FreeBSD-specific extension fields for tcp_info.
1510b8af5dfaSRobert Watson 	 */
1511c8443a1dSRobert Watson 	ti->tcpi_rcv_space = tp->rcv_wnd;
1512535fbad6SKip Macy 	ti->tcpi_rcv_nxt = tp->rcv_nxt;
1513b8af5dfaSRobert Watson 	ti->tcpi_snd_wnd = tp->snd_wnd;
15141c18314dSAndre Oppermann 	ti->tcpi_snd_bwnd = 0;		/* Unused, kept for compat. */
1515535fbad6SKip Macy 	ti->tcpi_snd_nxt = tp->snd_nxt;
151643d94734SJohn Baldwin 	ti->tcpi_snd_mss = tp->t_maxseg;
151743d94734SJohn Baldwin 	ti->tcpi_rcv_mss = tp->t_maxseg;
1518f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_rexmitpack = tp->t_sndrexmitpack;
1519f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_rcv_ooopack = tp->t_rcvoopack;
1520f5d34df5SGeorge V. Neville-Neil 	ti->tcpi_snd_zerowin = tp->t_sndzerowin;
1521a6456410SNavdeep Parhar #ifdef TCP_OFFLOAD
1522a6456410SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
1523a6456410SNavdeep Parhar 		ti->tcpi_options |= TCPI_OPT_TOE;
1524a6456410SNavdeep Parhar 		tcp_offload_tcp_info(tp, ti);
1525a6456410SNavdeep Parhar 	}
1526a6456410SNavdeep Parhar #endif
152722c81cc5SRichard Scheffenegger 	/*
152822c81cc5SRichard Scheffenegger 	 * AccECN related counters.
152922c81cc5SRichard Scheffenegger 	 */
153022c81cc5SRichard Scheffenegger 	if ((tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT)) ==
153122c81cc5SRichard Scheffenegger 	    (TF2_ECN_PERMIT | TF2_ACE_PERMIT))
153222c81cc5SRichard Scheffenegger 		/*
153322c81cc5SRichard Scheffenegger 		 * Internal counter starts at 5 for AccECN
153422c81cc5SRichard Scheffenegger 		 * but 0 for RFC3168 ECN.
153522c81cc5SRichard Scheffenegger 		 */
153622c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep - 5;
153722c81cc5SRichard Scheffenegger 	else
153822c81cc5SRichard Scheffenegger 		ti->tcpi_delivered_ce = tp->t_scep;
153922c81cc5SRichard Scheffenegger 	ti->tcpi_received_ce = tp->t_rcep;
1540b8af5dfaSRobert Watson }
1541b8af5dfaSRobert Watson 
1542b8af5dfaSRobert Watson /*
15431e8f5ffaSRobert Watson  * tcp_ctloutput() must drop the inpcb lock before performing copyin on
15441e8f5ffaSRobert Watson  * socket option arguments.  When it re-acquires the lock after the copy, it
15451e8f5ffaSRobert Watson  * has to revalidate that the connection is still valid for the socket
15461e8f5ffaSRobert Watson  * option.
1547cfe8b629SGarrett Wollman  */
1548bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK_CLEANUP(inp, cleanup) do {			\
15498501a69cSRobert Watson 	INP_WLOCK(inp);							\
155053af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {				\
15518501a69cSRobert Watson 		INP_WUNLOCK(inp);					\
1552bac5bedfSConrad Meyer 		cleanup;						\
15531e8f5ffaSRobert Watson 		return (ECONNRESET);					\
15541e8f5ffaSRobert Watson 	}								\
15551e8f5ffaSRobert Watson 	tp = intotcpcb(inp);						\
15561e8f5ffaSRobert Watson } while(0)
1557bac5bedfSConrad Meyer #define INP_WLOCK_RECHECK(inp) INP_WLOCK_RECHECK_CLEANUP((inp), /* noop */)
15581e8f5ffaSRobert Watson 
1559fd7daa72SMichael Tuexen int
1560fc4d53ccSGleb Smirnoff tcp_ctloutput_set(struct inpcb *inp, struct sockopt *sopt)
1561df8bae1dSRodney W. Grimes {
1562fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1563fc4d53ccSGleb Smirnoff 	struct tcpcb *tp = intotcpcb(inp);
1564fc4d53ccSGleb Smirnoff 	int error = 0;
1565df8bae1dSRodney W. Grimes 
1566fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_SET);
15673b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
156853af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1569fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1570fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1571fc4d53ccSGleb Smirnoff 
1572cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_TCP) {
15733b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1574fb59c426SYoshinobu Inoue #ifdef INET6
1575de156263SGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1576fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1577de156263SGleb Smirnoff #endif
1578de156263SGleb Smirnoff #if defined(INET6) && defined(INET)
1579de156263SGleb Smirnoff 		else
1580de156263SGleb Smirnoff #endif
1581de156263SGleb Smirnoff #ifdef INET
1582fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1583de156263SGleb Smirnoff #endif
15845dff1c38SMichael Tuexen 		/*
1585de156263SGleb Smirnoff 		 * When an IP-level socket option affects TCP, pass control
1586de156263SGleb Smirnoff 		 * down to stack tfb_tcp_ctloutput, otherwise return what
1587de156263SGleb Smirnoff 		 * IP level returned.
15885dff1c38SMichael Tuexen 		 */
1589de156263SGleb Smirnoff 		switch (sopt->sopt_level) {
1590de156263SGleb Smirnoff #ifdef INET6
1591de156263SGleb Smirnoff 		case IPPROTO_IPV6:
1592de156263SGleb Smirnoff 			if ((inp->inp_vflag & INP_IPV6PROTO) == 0)
1593de156263SGleb Smirnoff 				return (error);
1594de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1595de156263SGleb Smirnoff 			case IPV6_TCLASS:
1596de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1597de156263SGleb Smirnoff 				break;
1598de156263SGleb Smirnoff 			case IPV6_USE_MIN_MTU:
1599f581a26eSGleb Smirnoff 				/* Update t_maxseg accordingly. */
1600f581a26eSGleb Smirnoff 				break;
1601de156263SGleb Smirnoff 			default:
1602de156263SGleb Smirnoff 				return (error);
16035dff1c38SMichael Tuexen 			}
1604de156263SGleb Smirnoff 			break;
1605b287c6c7SBjoern A. Zeeb #endif
1606b287c6c7SBjoern A. Zeeb #ifdef INET
1607de156263SGleb Smirnoff 		case IPPROTO_IP:
1608de156263SGleb Smirnoff 			switch (sopt->sopt_name) {
1609de156263SGleb Smirnoff 			case IP_TOS:
16103b0ee680SRichard Scheffenegger 				inp->inp_ip_tos &= ~IPTOS_ECN_MASK;
16113b0ee680SRichard Scheffenegger 				break;
1612de156263SGleb Smirnoff 			case IP_TTL:
1613de156263SGleb Smirnoff 				/* Notify tcp stacks that care (e.g. RACK). */
1614de156263SGleb Smirnoff 				break;
1615de156263SGleb Smirnoff 			default:
1616df8bae1dSRodney W. Grimes 				return (error);
1617de156263SGleb Smirnoff 			}
1618de156263SGleb Smirnoff 			break;
1619de156263SGleb Smirnoff #endif
1620de156263SGleb Smirnoff 		default:
1621de156263SGleb Smirnoff 			return (error);
1622de156263SGleb Smirnoff 		}
16233b3c08c1SMichael Tuexen 		INP_WLOCK(inp);
162453af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
16253b3c08c1SMichael Tuexen 			INP_WUNLOCK(inp);
16263b3c08c1SMichael Tuexen 			return (ECONNRESET);
16273b3c08c1SMichael Tuexen 		}
1628fc4d53ccSGleb Smirnoff 	} else if (sopt->sopt_name == TCP_FUNCTION_BLK) {
1629fc4d53ccSGleb Smirnoff 		/*
1630fc4d53ccSGleb Smirnoff 		 * Protect the TCP option TCP_FUNCTION_BLK so
1631fc4d53ccSGleb Smirnoff 		 * that a sub-function can *never* overwrite this.
1632fc4d53ccSGleb Smirnoff 		 */
1633fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1634fc4d53ccSGleb Smirnoff 		struct tcp_function_block *blk;
1635fc4d53ccSGleb Smirnoff 
16363b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1637fc4d53ccSGleb Smirnoff 		error = sooptcopyin(sopt, &fsn, sizeof fsn, sizeof fsn);
1638fc4d53ccSGleb Smirnoff 		if (error)
1639fc4d53ccSGleb Smirnoff 			return (error);
1640fc4d53ccSGleb Smirnoff 
164168cea2b1SJohn Baldwin 		INP_WLOCK(inp);
164253af6903SGleb Smirnoff 		if (inp->inp_flags & INP_DROPPED) {
16438501a69cSRobert Watson 			INP_WUNLOCK(inp);
16441e8f5ffaSRobert Watson 			return (ECONNRESET);
1645623dce13SRobert Watson 		}
164655bceb1eSRandall Stewart 		tp = intotcpcb(inp);
1647fc4d53ccSGleb Smirnoff 
164855bceb1eSRandall Stewart 		blk = find_and_ref_tcp_functions(&fsn);
164955bceb1eSRandall Stewart 		if (blk == NULL) {
165055bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
165155bceb1eSRandall Stewart 			return (ENOENT);
165255bceb1eSRandall Stewart 		}
1653587d67c0SRandall Stewart 		if (tp->t_fb == blk) {
1654587d67c0SRandall Stewart 			/* You already have this */
1655587d67c0SRandall Stewart 			refcount_release(&blk->tfb_refcnt);
1656587d67c0SRandall Stewart 			INP_WUNLOCK(inp);
1657587d67c0SRandall Stewart 			return (0);
1658587d67c0SRandall Stewart 		}
1659587d67c0SRandall Stewart 		if (tp->t_state != TCPS_CLOSED) {
1660587d67c0SRandall Stewart 			/*
1661587d67c0SRandall Stewart 			 * The user has advanced the state
1662587d67c0SRandall Stewart 			 * past the initial point, we may not
1663587d67c0SRandall Stewart 			 * be able to switch.
1664587d67c0SRandall Stewart 			 */
1665587d67c0SRandall Stewart 			if (blk->tfb_tcp_handoff_ok != NULL) {
1666587d67c0SRandall Stewart 				/*
1667587d67c0SRandall Stewart 				 * Does the stack provide a
1668587d67c0SRandall Stewart 				 * query mechanism, if so it may
1669587d67c0SRandall Stewart 				 * still be possible?
1670587d67c0SRandall Stewart 				 */
1671587d67c0SRandall Stewart 				error = (*blk->tfb_tcp_handoff_ok)(tp);
1672c6c0be27SMichael Tuexen 			} else
1673c6c0be27SMichael Tuexen 				error = EINVAL;
1674587d67c0SRandall Stewart 			if (error) {
1675587d67c0SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
1676587d67c0SRandall Stewart 				INP_WUNLOCK(inp);
1677587d67c0SRandall Stewart 				return(error);
1678587d67c0SRandall Stewart 			}
1679587d67c0SRandall Stewart 		}
168055bceb1eSRandall Stewart 		if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) {
168155bceb1eSRandall Stewart 			refcount_release(&blk->tfb_refcnt);
168255bceb1eSRandall Stewart 			INP_WUNLOCK(inp);
168355bceb1eSRandall Stewart 			return (ENOENT);
168455bceb1eSRandall Stewart 		}
168555bceb1eSRandall Stewart 		/*
168655bceb1eSRandall Stewart 		 * Release the old refcnt, the
1687587d67c0SRandall Stewart 		 * lookup acquired a ref on the
1688587d67c0SRandall Stewart 		 * new one already.
168955bceb1eSRandall Stewart 		 */
1690587d67c0SRandall Stewart 		if (tp->t_fb->tfb_tcp_fb_fini) {
1691086a3556SAndrew Gallatin 			struct epoch_tracker et;
1692587d67c0SRandall Stewart 			/*
1693587d67c0SRandall Stewart 			 * Tell the stack to cleanup with 0 i.e.
1694587d67c0SRandall Stewart 			 * the tcb is not going away.
1695587d67c0SRandall Stewart 			 */
1696086a3556SAndrew Gallatin 			NET_EPOCH_ENTER(et);
1697587d67c0SRandall Stewart 			(*tp->t_fb->tfb_tcp_fb_fini)(tp, 0);
1698086a3556SAndrew Gallatin 			NET_EPOCH_EXIT(et);
1699587d67c0SRandall Stewart 		}
17003ee9c3c4SRandall Stewart #ifdef TCPHPTS
17013ee9c3c4SRandall Stewart 		/* Assure that we are not on any hpts */
17029eb0e832SGleb Smirnoff 		tcp_hpts_remove(tptoinpcb(tp));
17033ee9c3c4SRandall Stewart #endif
17043ee9c3c4SRandall Stewart 		if (blk->tfb_tcp_fb_init) {
17053ee9c3c4SRandall Stewart 			error = (*blk->tfb_tcp_fb_init)(tp);
17063ee9c3c4SRandall Stewart 			if (error) {
17073ee9c3c4SRandall Stewart 				refcount_release(&blk->tfb_refcnt);
17083ee9c3c4SRandall Stewart 				if (tp->t_fb->tfb_tcp_fb_init) {
17093ee9c3c4SRandall Stewart 					if((*tp->t_fb->tfb_tcp_fb_init)(tp) != 0)  {
17103ee9c3c4SRandall Stewart 						/* Fall back failed, drop the connection */
17113ee9c3c4SRandall Stewart 						INP_WUNLOCK(inp);
1712fd7daa72SMichael Tuexen 						soabort(so);
17133ee9c3c4SRandall Stewart 						return (error);
17143ee9c3c4SRandall Stewart 					}
17153ee9c3c4SRandall Stewart 				}
17163ee9c3c4SRandall Stewart 				goto err_out;
17173ee9c3c4SRandall Stewart 			}
17183ee9c3c4SRandall Stewart 		}
171955bceb1eSRandall Stewart 		refcount_release(&tp->t_fb->tfb_refcnt);
172055bceb1eSRandall Stewart 		tp->t_fb = blk;
172155bceb1eSRandall Stewart #ifdef TCP_OFFLOAD
172255bceb1eSRandall Stewart 		if (tp->t_flags & TF_TOE) {
172355bceb1eSRandall Stewart 			tcp_offload_ctloutput(tp, sopt->sopt_dir,
172455bceb1eSRandall Stewart 			     sopt->sopt_name);
172555bceb1eSRandall Stewart 		}
172655bceb1eSRandall Stewart #endif
17273ee9c3c4SRandall Stewart err_out:
172855bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
172955bceb1eSRandall Stewart 		return (error);
1730fc4d53ccSGleb Smirnoff 	}
1731fc4d53ccSGleb Smirnoff 
17323b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
17333b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1734fc4d53ccSGleb Smirnoff }
1735fc4d53ccSGleb Smirnoff 
1736fc4d53ccSGleb Smirnoff static int
1737fc4d53ccSGleb Smirnoff tcp_ctloutput_get(struct inpcb *inp, struct sockopt *sopt)
1738fc4d53ccSGleb Smirnoff {
1739fd7daa72SMichael Tuexen 	struct socket *so = inp->inp_socket;
1740fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
1741fc4d53ccSGleb Smirnoff 	int error = 0;
1742fc4d53ccSGleb Smirnoff 
1743fc4d53ccSGleb Smirnoff 	MPASS(sopt->sopt_dir == SOPT_GET);
17443b3c08c1SMichael Tuexen 	INP_WLOCK_ASSERT(inp);
174553af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1746fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1747fd7daa72SMichael Tuexen 	KASSERT(so != NULL, ("inp_socket == NULL"));
1748fc4d53ccSGleb Smirnoff 
1749fc4d53ccSGleb Smirnoff 	if (sopt->sopt_level != IPPROTO_TCP) {
17503b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
1751fc4d53ccSGleb Smirnoff #ifdef INET6
1752fc4d53ccSGleb Smirnoff 		if (inp->inp_vflag & INP_IPV6PROTO)
1753fd7daa72SMichael Tuexen 			error = ip6_ctloutput(so, sopt);
1754fc4d53ccSGleb Smirnoff #endif /* INET6 */
1755fc4d53ccSGleb Smirnoff #if defined(INET6) && defined(INET)
1756fc4d53ccSGleb Smirnoff 		else
1757fc4d53ccSGleb Smirnoff #endif
1758fc4d53ccSGleb Smirnoff #ifdef INET
1759fd7daa72SMichael Tuexen 			error = ip_ctloutput(so, sopt);
1760fc4d53ccSGleb Smirnoff #endif
1761fc4d53ccSGleb Smirnoff 		return (error);
1762fc4d53ccSGleb Smirnoff 	}
1763fc4d53ccSGleb Smirnoff 	if (((sopt->sopt_name == TCP_FUNCTION_BLK) ||
1764e2833083SPeter Lei 	     (sopt->sopt_name == TCP_FUNCTION_ALIAS))) {
1765fc4d53ccSGleb Smirnoff 		struct tcp_function_set fsn;
1766fc4d53ccSGleb Smirnoff 
1767e2833083SPeter Lei 		if (sopt->sopt_name == TCP_FUNCTION_ALIAS) {
1768e2833083SPeter Lei 			memset(&fsn, 0, sizeof(fsn));
1769e2833083SPeter Lei 			find_tcp_function_alias(tp->t_fb, &fsn);
1770e2833083SPeter Lei 		} else {
1771e2833083SPeter Lei 			strncpy(fsn.function_set_name,
1772e2833083SPeter Lei 			    tp->t_fb->tfb_tcp_block_name,
1773c73b6f4dSEd Maste 			    TCP_FUNCTION_NAME_LEN_MAX);
1774c73b6f4dSEd Maste 			fsn.function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0';
1775e2833083SPeter Lei 		}
177655bceb1eSRandall Stewart 		fsn.pcbcnt = tp->t_fb->tfb_refcnt;
177755bceb1eSRandall Stewart 		INP_WUNLOCK(inp);
177855bceb1eSRandall Stewart 		error = sooptcopyout(sopt, &fsn, sizeof fsn);
177955bceb1eSRandall Stewart 		return (error);
178055bceb1eSRandall Stewart 	}
1781fc4d53ccSGleb Smirnoff 
17823b3c08c1SMichael Tuexen 	/* Pass in the INP locked, callee must unlock it. */
17833b3c08c1SMichael Tuexen 	return (tp->t_fb->tfb_tcp_ctloutput(inp, sopt));
1784fc4d53ccSGleb Smirnoff }
1785fc4d53ccSGleb Smirnoff 
1786fc4d53ccSGleb Smirnoff int
1787fc4d53ccSGleb Smirnoff tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1788fc4d53ccSGleb Smirnoff {
1789fc4d53ccSGleb Smirnoff 	struct	inpcb *inp;
1790fc4d53ccSGleb Smirnoff 
1791fc4d53ccSGleb Smirnoff 	inp = sotoinpcb(so);
1792fc4d53ccSGleb Smirnoff 	KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1793fc4d53ccSGleb Smirnoff 
17943b3c08c1SMichael Tuexen 	INP_WLOCK(inp);
179553af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
17963b3c08c1SMichael Tuexen 		INP_WUNLOCK(inp);
17973b3c08c1SMichael Tuexen 		return (ECONNRESET);
17983b3c08c1SMichael Tuexen 	}
1799fc4d53ccSGleb Smirnoff 	if (sopt->sopt_dir == SOPT_SET)
1800fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_set(inp, sopt));
1801fc4d53ccSGleb Smirnoff 	else if (sopt->sopt_dir == SOPT_GET)
1802fc4d53ccSGleb Smirnoff 		return (tcp_ctloutput_get(inp, sopt));
1803fc4d53ccSGleb Smirnoff 	else
1804fc4d53ccSGleb Smirnoff 		panic("%s: sopt_dir $%d", __func__, sopt->sopt_dir);
180555bceb1eSRandall Stewart }
180655bceb1eSRandall Stewart 
18072529f56eSJonathan T. Looney /*
18082529f56eSJonathan T. Looney  * If this assert becomes untrue, we need to change the size of the buf
18092529f56eSJonathan T. Looney  * variable in tcp_default_ctloutput().
18102529f56eSJonathan T. Looney  */
18112529f56eSJonathan T. Looney #ifdef CTASSERT
18122529f56eSJonathan T. Looney CTASSERT(TCP_CA_NAME_MAX <= TCP_LOG_ID_LEN);
18132529f56eSJonathan T. Looney CTASSERT(TCP_LOG_REASON_LEN <= TCP_LOG_ID_LEN);
18142529f56eSJonathan T. Looney #endif
18152529f56eSJonathan T. Looney 
1816ec1db6e1SJohn Baldwin #ifdef KERN_TLS
1817ec1db6e1SJohn Baldwin static int
1818ec1db6e1SJohn Baldwin copyin_tls_enable(struct sockopt *sopt, struct tls_enable *tls)
1819ec1db6e1SJohn Baldwin {
1820ec1db6e1SJohn Baldwin 	struct tls_enable_v0 tls_v0;
1821ec1db6e1SJohn Baldwin 	int error;
1822ec1db6e1SJohn Baldwin 
1823ec1db6e1SJohn Baldwin 	if (sopt->sopt_valsize == sizeof(tls_v0)) {
1824ec1db6e1SJohn Baldwin 		error = sooptcopyin(sopt, &tls_v0, sizeof(tls_v0),
1825ec1db6e1SJohn Baldwin 		    sizeof(tls_v0));
1826ec1db6e1SJohn Baldwin 		if (error)
1827ec1db6e1SJohn Baldwin 			return (error);
1828ec1db6e1SJohn Baldwin 		memset(tls, 0, sizeof(*tls));
1829ec1db6e1SJohn Baldwin 		tls->cipher_key = tls_v0.cipher_key;
1830ec1db6e1SJohn Baldwin 		tls->iv = tls_v0.iv;
1831ec1db6e1SJohn Baldwin 		tls->auth_key = tls_v0.auth_key;
1832ec1db6e1SJohn Baldwin 		tls->cipher_algorithm = tls_v0.cipher_algorithm;
1833ec1db6e1SJohn Baldwin 		tls->cipher_key_len = tls_v0.cipher_key_len;
1834ec1db6e1SJohn Baldwin 		tls->iv_len = tls_v0.iv_len;
1835ec1db6e1SJohn Baldwin 		tls->auth_algorithm = tls_v0.auth_algorithm;
1836ec1db6e1SJohn Baldwin 		tls->auth_key_len = tls_v0.auth_key_len;
1837ec1db6e1SJohn Baldwin 		tls->flags = tls_v0.flags;
1838ec1db6e1SJohn Baldwin 		tls->tls_vmajor = tls_v0.tls_vmajor;
1839ec1db6e1SJohn Baldwin 		tls->tls_vminor = tls_v0.tls_vminor;
1840ec1db6e1SJohn Baldwin 		return (0);
1841ec1db6e1SJohn Baldwin 	}
1842ec1db6e1SJohn Baldwin 
1843ec1db6e1SJohn Baldwin 	return (sooptcopyin(sopt, tls, sizeof(*tls), sizeof(*tls)));
1844ec1db6e1SJohn Baldwin }
1845ec1db6e1SJohn Baldwin #endif
1846ec1db6e1SJohn Baldwin 
1847b8d60729SRandall Stewart extern struct cc_algo newreno_cc_algo;
1848b8d60729SRandall Stewart 
1849b8d60729SRandall Stewart static int
1850ea9017fbSRandall Stewart tcp_set_cc_mod(struct inpcb *inp, struct sockopt *sopt)
1851b8d60729SRandall Stewart {
1852b8d60729SRandall Stewart 	struct cc_algo *algo;
1853b8d60729SRandall Stewart 	void *ptr = NULL;
18543b3c08c1SMichael Tuexen 	struct tcpcb *tp;
1855b8d60729SRandall Stewart 	struct cc_var cc_mem;
1856b8d60729SRandall Stewart 	char	buf[TCP_CA_NAME_MAX];
1857b8d60729SRandall Stewart 	size_t mem_sz;
1858b8d60729SRandall Stewart 	int error;
1859b8d60729SRandall Stewart 
1860b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1861b8d60729SRandall Stewart 	error = sooptcopyin(sopt, buf, TCP_CA_NAME_MAX - 1, 1);
1862b8d60729SRandall Stewart 	if (error)
1863b8d60729SRandall Stewart 		return(error);
1864b8d60729SRandall Stewart 	buf[sopt->sopt_valsize] = '\0';
1865b8d60729SRandall Stewart 	CC_LIST_RLOCK();
1866ea9017fbSRandall Stewart 	STAILQ_FOREACH(algo, &cc_list, entries) {
1867b8d60729SRandall Stewart 		if (strncmp(buf, algo->name,
1868b8d60729SRandall Stewart 			    TCP_CA_NAME_MAX) == 0) {
1869b8d60729SRandall Stewart 			if (algo->flags & CC_MODULE_BEING_REMOVED) {
1870b8d60729SRandall Stewart 				/* We can't "see" modules being unloaded */
1871b8d60729SRandall Stewart 				continue;
1872b8d60729SRandall Stewart 			}
1873b8d60729SRandall Stewart 			break;
1874b8d60729SRandall Stewart 		}
1875ea9017fbSRandall Stewart 	}
1876b8d60729SRandall Stewart 	if (algo == NULL) {
1877b8d60729SRandall Stewart 		CC_LIST_RUNLOCK();
1878b8d60729SRandall Stewart 		return(ESRCH);
1879b8d60729SRandall Stewart 	}
1880ea9017fbSRandall Stewart 	/*
1881ea9017fbSRandall Stewart 	 * With a reference the algorithm cannot be removed
1882ea9017fbSRandall Stewart 	 * so we hold a reference through the change process.
1883ea9017fbSRandall Stewart 	 */
1884ea9017fbSRandall Stewart 	cc_refer(algo);
1885ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1886b8d60729SRandall Stewart 	if (algo->cb_init != NULL) {
1887b8d60729SRandall Stewart 		/* We can now pre-get the memory for the CC */
1888b8d60729SRandall Stewart 		mem_sz = (*algo->cc_data_sz)();
1889b8d60729SRandall Stewart 		if (mem_sz == 0) {
1890b8d60729SRandall Stewart 			goto no_mem_needed;
1891b8d60729SRandall Stewart 		}
1892b8d60729SRandall Stewart 		ptr = malloc(mem_sz, M_CC_MEM, M_WAITOK);
1893b8d60729SRandall Stewart 	} else {
1894b8d60729SRandall Stewart no_mem_needed:
1895b8d60729SRandall Stewart 		mem_sz = 0;
1896b8d60729SRandall Stewart 		ptr = NULL;
1897b8d60729SRandall Stewart 	}
1898b8d60729SRandall Stewart 	/*
1899b8d60729SRandall Stewart 	 * Make sure its all clean and zero and also get
1900b8d60729SRandall Stewart 	 * back the inplock.
1901b8d60729SRandall Stewart 	 */
1902b8d60729SRandall Stewart 	memset(&cc_mem, 0, sizeof(cc_mem));
1903df07bfdaSMichael Tuexen 	INP_WLOCK(inp);
190453af6903SGleb Smirnoff 	if (inp->inp_flags & INP_DROPPED) {
1905df07bfdaSMichael Tuexen 		INP_WUNLOCK(inp);
1906ea9017fbSRandall Stewart 		if (ptr)
1907df07bfdaSMichael Tuexen 			free(ptr, M_CC_MEM);
1908ea9017fbSRandall Stewart 		/* Release our temp reference */
1909ea9017fbSRandall Stewart 		CC_LIST_RLOCK();
1910ea9017fbSRandall Stewart 		cc_release(algo);
1911ea9017fbSRandall Stewart 		CC_LIST_RUNLOCK();
1912df07bfdaSMichael Tuexen 		return (ECONNRESET);
1913df07bfdaSMichael Tuexen 	}
1914df07bfdaSMichael Tuexen 	tp = intotcpcb(inp);
1915df07bfdaSMichael Tuexen 	if (ptr != NULL)
1916b8d60729SRandall Stewart 		memset(ptr, 0, mem_sz);
1917b8d60729SRandall Stewart 	cc_mem.ccvc.tcp = tp;
1918b8d60729SRandall Stewart 	/*
1919b8d60729SRandall Stewart 	 * We once again hold a write lock over the tcb so it's
1920b8d60729SRandall Stewart 	 * safe to do these things without ordering concerns.
1921b8d60729SRandall Stewart 	 * Note here we init into stack memory.
1922b8d60729SRandall Stewart 	 */
1923b8d60729SRandall Stewart 	if (algo->cb_init != NULL)
1924b8d60729SRandall Stewart 		error = algo->cb_init(&cc_mem, ptr);
1925b8d60729SRandall Stewart 	else
1926b8d60729SRandall Stewart 		error = 0;
1927b8d60729SRandall Stewart 	/*
1928b8d60729SRandall Stewart 	 * The CC algorithms, when given their memory
1929b8d60729SRandall Stewart 	 * should not fail we could in theory have a
1930b8d60729SRandall Stewart 	 * KASSERT here.
1931b8d60729SRandall Stewart 	 */
1932b8d60729SRandall Stewart 	if (error == 0) {
1933b8d60729SRandall Stewart 		/*
1934b8d60729SRandall Stewart 		 * Touchdown, lets go ahead and move the
1935b8d60729SRandall Stewart 		 * connection to the new CC module by
1936b8d60729SRandall Stewart 		 * copying in the cc_mem after we call
1937b8d60729SRandall Stewart 		 * the old ones cleanup (if any).
1938b8d60729SRandall Stewart 		 */
1939b8d60729SRandall Stewart 		if (CC_ALGO(tp)->cb_destroy != NULL)
1940e68b3792SGleb Smirnoff 			CC_ALGO(tp)->cb_destroy(&tp->t_ccv);
1941ea9017fbSRandall Stewart 		/* Detach the old CC from the tcpcb  */
1942ea9017fbSRandall Stewart 		cc_detach(tp);
1943ea9017fbSRandall Stewart 		/* Copy in our temp memory that was inited */
1944e68b3792SGleb Smirnoff 		memcpy(&tp->t_ccv, &cc_mem, sizeof(struct cc_var));
1945ea9017fbSRandall Stewart 		/* Now attach the new, which takes a reference */
1946ea9017fbSRandall Stewart 		cc_attach(tp, algo);
1947b8d60729SRandall Stewart 		/* Ok now are we where we have gotten past any conn_init? */
1948b8d60729SRandall Stewart 		if (TCPS_HAVEESTABLISHED(tp->t_state) && (CC_ALGO(tp)->conn_init != NULL)) {
1949b8d60729SRandall Stewart 			/* Yep run the connection init for the new CC */
1950e68b3792SGleb Smirnoff 			CC_ALGO(tp)->conn_init(&tp->t_ccv);
1951b8d60729SRandall Stewart 		}
1952b8d60729SRandall Stewart 	} else if (ptr)
1953b8d60729SRandall Stewart 		free(ptr, M_CC_MEM);
1954b8d60729SRandall Stewart 	INP_WUNLOCK(inp);
1955ea9017fbSRandall Stewart 	/* Now lets release our temp reference */
1956ea9017fbSRandall Stewart 	CC_LIST_RLOCK();
1957ea9017fbSRandall Stewart 	cc_release(algo);
1958ea9017fbSRandall Stewart 	CC_LIST_RUNLOCK();
1959b8d60729SRandall Stewart 	return (error);
1960b8d60729SRandall Stewart }
1961b8d60729SRandall Stewart 
196255bceb1eSRandall Stewart int
19633b3c08c1SMichael Tuexen tcp_default_ctloutput(struct inpcb *inp, struct sockopt *sopt)
196455bceb1eSRandall Stewart {
1965fd7daa72SMichael Tuexen 	struct tcpcb *tp = intotcpcb(inp);
196655bceb1eSRandall Stewart 	int	error, opt, optval;
196755bceb1eSRandall Stewart 	u_int	ui;
196855bceb1eSRandall Stewart 	struct	tcp_info ti;
1969b2e60773SJohn Baldwin #ifdef KERN_TLS
1970b2e60773SJohn Baldwin 	struct tls_enable tls;
1971528c7649SMichael Tuexen 	struct socket *so = inp->inp_socket;
1972b2e60773SJohn Baldwin #endif
19732529f56eSJonathan T. Looney 	char	*pbuf, buf[TCP_LOG_ID_LEN];
1974adc56f5aSEdward Tomasz Napierala #ifdef STATS
1975adc56f5aSEdward Tomasz Napierala 	struct statsblob *sbp;
1976adc56f5aSEdward Tomasz Napierala #endif
1977af6fef3aSGleb Smirnoff 	size_t	len;
1978df8bae1dSRodney W. Grimes 
1979f581a26eSGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
198053af6903SGleb Smirnoff 	KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1981fd7daa72SMichael Tuexen 	    ("inp_flags == %x", inp->inp_flags));
1982528c7649SMichael Tuexen 	KASSERT(inp->inp_socket != NULL, ("inp_socket == NULL"));
1983f581a26eSGleb Smirnoff 
1984f581a26eSGleb Smirnoff 	switch (sopt->sopt_level) {
1985f581a26eSGleb Smirnoff #ifdef INET6
1986f581a26eSGleb Smirnoff 	case IPPROTO_IPV6:
1987f581a26eSGleb Smirnoff 		MPASS(inp->inp_vflag & INP_IPV6PROTO);
1988f581a26eSGleb Smirnoff 		switch (sopt->sopt_name) {
1989f581a26eSGleb Smirnoff 		case IPV6_USE_MIN_MTU:
1990f581a26eSGleb Smirnoff 			tcp6_use_min_mtu(tp);
1991f581a26eSGleb Smirnoff 			/* FALLTHROUGH */
1992f581a26eSGleb Smirnoff 		}
1993f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
1994f581a26eSGleb Smirnoff 		return (0);
1995f581a26eSGleb Smirnoff #endif
1996f581a26eSGleb Smirnoff #ifdef INET
1997f581a26eSGleb Smirnoff 	case IPPROTO_IP:
1998f581a26eSGleb Smirnoff 		INP_WUNLOCK(inp);
1999f581a26eSGleb Smirnoff 		return (0);
2000f581a26eSGleb Smirnoff #endif
2001f581a26eSGleb Smirnoff 	}
2002f581a26eSGleb Smirnoff 
2003d519cedbSGleb Smirnoff 	/*
2004d519cedbSGleb Smirnoff 	 * For TCP_CCALGOOPT forward the control to CC module, for both
2005d519cedbSGleb Smirnoff 	 * SOPT_SET and SOPT_GET.
2006d519cedbSGleb Smirnoff 	 */
2007d519cedbSGleb Smirnoff 	switch (sopt->sopt_name) {
2008d519cedbSGleb Smirnoff 	case TCP_CCALGOOPT:
2009d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2010c8b53cedSMichael Tuexen 		if (sopt->sopt_valsize > CC_ALGOOPT_LIMIT)
2011c8b53cedSMichael Tuexen 			return (EINVAL);
2012af6fef3aSGleb Smirnoff 		pbuf = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK | M_ZERO);
2013af6fef3aSGleb Smirnoff 		error = sooptcopyin(sopt, pbuf, sopt->sopt_valsize,
2014d519cedbSGleb Smirnoff 		    sopt->sopt_valsize);
2015d519cedbSGleb Smirnoff 		if (error) {
2016af6fef3aSGleb Smirnoff 			free(pbuf, M_TEMP);
2017d519cedbSGleb Smirnoff 			return (error);
2018d519cedbSGleb Smirnoff 		}
2019bac5bedfSConrad Meyer 		INP_WLOCK_RECHECK_CLEANUP(inp, free(pbuf, M_TEMP));
2020d519cedbSGleb Smirnoff 		if (CC_ALGO(tp)->ctl_output != NULL)
2021e68b3792SGleb Smirnoff 			error = CC_ALGO(tp)->ctl_output(&tp->t_ccv, sopt, pbuf);
2022d519cedbSGleb Smirnoff 		else
2023d519cedbSGleb Smirnoff 			error = ENOENT;
2024d519cedbSGleb Smirnoff 		INP_WUNLOCK(inp);
2025d519cedbSGleb Smirnoff 		if (error == 0 && sopt->sopt_dir == SOPT_GET)
2026af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, pbuf, sopt->sopt_valsize);
2027af6fef3aSGleb Smirnoff 		free(pbuf, M_TEMP);
2028d519cedbSGleb Smirnoff 		return (error);
2029d519cedbSGleb Smirnoff 	}
2030d519cedbSGleb Smirnoff 
2031cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
2032cfe8b629SGarrett Wollman 	case SOPT_SET:
2033cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2034fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
203588f6b043SBruce M Simpson 		case TCP_MD5SIG:
20368501a69cSRobert Watson 			INP_WUNLOCK(inp);
203797453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2038fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2039fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
20401cfd4b53SBruce M Simpson 			if (error)
20411e8f5ffaSRobert Watson 				return (error);
204297453e5eSClaudio Jeker 			INP_WLOCK_RECHECK(inp);
204309fe6320SNavdeep Parhar 			goto unlock_and_done;
2044fcf59617SAndrey V. Elsukov #endif /* IPSEC */
204509fe6320SNavdeep Parhar 
2046df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2047cfe8b629SGarrett Wollman 		case TCP_NOOPT:
20480471a8c7SRichard Scheffenegger 		case TCP_LRD:
20498501a69cSRobert Watson 			INP_WUNLOCK(inp);
2050cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2051cfe8b629SGarrett Wollman 			    sizeof optval);
2052cfe8b629SGarrett Wollman 			if (error)
20531e8f5ffaSRobert Watson 				return (error);
2054cfe8b629SGarrett Wollman 
20558501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2056cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
2057cfe8b629SGarrett Wollman 			case TCP_NODELAY:
2058cfe8b629SGarrett Wollman 				opt = TF_NODELAY;
2059cfe8b629SGarrett Wollman 				break;
2060cfe8b629SGarrett Wollman 			case TCP_NOOPT:
2061cfe8b629SGarrett Wollman 				opt = TF_NOOPT;
2062cfe8b629SGarrett Wollman 				break;
20630471a8c7SRichard Scheffenegger 			case TCP_LRD:
20640471a8c7SRichard Scheffenegger 				opt = TF_LRD;
20650471a8c7SRichard Scheffenegger 				break;
2066cfe8b629SGarrett Wollman 			default:
2067cfe8b629SGarrett Wollman 				opt = 0; /* dead code to fool gcc */
2068cfe8b629SGarrett Wollman 				break;
2069cfe8b629SGarrett Wollman 			}
2070cfe8b629SGarrett Wollman 
2071cfe8b629SGarrett Wollman 			if (optval)
2072cfe8b629SGarrett Wollman 				tp->t_flags |= opt;
2073df8bae1dSRodney W. Grimes 			else
2074cfe8b629SGarrett Wollman 				tp->t_flags &= ~opt;
207509fe6320SNavdeep Parhar unlock_and_done:
207609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
207709fe6320SNavdeep Parhar 			if (tp->t_flags & TF_TOE) {
207809fe6320SNavdeep Parhar 				tcp_offload_ctloutput(tp, sopt->sopt_dir,
207909fe6320SNavdeep Parhar 				    sopt->sopt_name);
208009fe6320SNavdeep Parhar 			}
208109fe6320SNavdeep Parhar #endif
20828501a69cSRobert Watson 			INP_WUNLOCK(inp);
2083df8bae1dSRodney W. Grimes 			break;
2084df8bae1dSRodney W. Grimes 
2085007581c0SJonathan Lemon 		case TCP_NOPUSH:
20868501a69cSRobert Watson 			INP_WUNLOCK(inp);
2087007581c0SJonathan Lemon 			error = sooptcopyin(sopt, &optval, sizeof optval,
2088007581c0SJonathan Lemon 			    sizeof optval);
2089007581c0SJonathan Lemon 			if (error)
20901e8f5ffaSRobert Watson 				return (error);
2091007581c0SJonathan Lemon 
20928501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
2093007581c0SJonathan Lemon 			if (optval)
2094007581c0SJonathan Lemon 				tp->t_flags |= TF_NOPUSH;
2095d28b9e89SJohn Baldwin 			else if (tp->t_flags & TF_NOPUSH) {
2096007581c0SJonathan Lemon 				tp->t_flags &= ~TF_NOPUSH;
2097109eb549SGleb Smirnoff 				if (TCPS_HAVEESTABLISHED(tp->t_state)) {
2098109eb549SGleb Smirnoff 					struct epoch_tracker et;
2099109eb549SGleb Smirnoff 
2100109eb549SGleb Smirnoff 					NET_EPOCH_ENTER(et);
2101f64dc2abSGleb Smirnoff 					error = tcp_output_nodrop(tp);
2102109eb549SGleb Smirnoff 					NET_EPOCH_EXIT(et);
2103109eb549SGleb Smirnoff 				}
2104007581c0SJonathan Lemon 			}
210509fe6320SNavdeep Parhar 			goto unlock_and_done;
2106007581c0SJonathan Lemon 
21079e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
21089e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
21099e644c23SMichael Tuexen 			error = sooptcopyin(sopt, &optval, sizeof optval,
21109e644c23SMichael Tuexen 			    sizeof optval);
21119e644c23SMichael Tuexen 			if (error)
21129e644c23SMichael Tuexen 				return (error);
21139e644c23SMichael Tuexen 			if ((optval < TCP_TUNNELING_PORT_MIN) ||
21149e644c23SMichael Tuexen 			    (optval > TCP_TUNNELING_PORT_MAX)) {
21159e644c23SMichael Tuexen 				/* Its got to be in range */
21169e644c23SMichael Tuexen 				return (EINVAL);
21179e644c23SMichael Tuexen 			}
21189e644c23SMichael Tuexen 			if ((V_tcp_udp_tunneling_port == 0) && (optval != 0)) {
21199e644c23SMichael Tuexen 				/* You have to have enabled a UDP tunneling port first */
21209e644c23SMichael Tuexen 				return (EINVAL);
21219e644c23SMichael Tuexen 			}
21229e644c23SMichael Tuexen 			INP_WLOCK_RECHECK(inp);
21239e644c23SMichael Tuexen 			if (tp->t_state != TCPS_CLOSED) {
21249e644c23SMichael Tuexen 				/* You can't change after you are connected */
21259e644c23SMichael Tuexen 				error = EINVAL;
21269e644c23SMichael Tuexen 			} else {
21279e644c23SMichael Tuexen 				/* Ok we are all good set the port */
21289e644c23SMichael Tuexen 				tp->t_port = htons(optval);
21299e644c23SMichael Tuexen 			}
21309e644c23SMichael Tuexen 			goto unlock_and_done;
21319e644c23SMichael Tuexen 
2132df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
21338501a69cSRobert Watson 			INP_WUNLOCK(inp);
2134cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
2135cfe8b629SGarrett Wollman 			    sizeof optval);
2136cfe8b629SGarrett Wollman 			if (error)
21371e8f5ffaSRobert Watson 				return (error);
2138df8bae1dSRodney W. Grimes 
21398501a69cSRobert Watson 			INP_WLOCK_RECHECK(inp);
214053369ac9SAndre Oppermann 			if (optval > 0 && optval <= tp->t_maxseg &&
2141603724d3SBjoern A. Zeeb 			    optval + 40 >= V_tcp_minmss)
2142cfe8b629SGarrett Wollman 				tp->t_maxseg = optval;
2143a0292f23SGarrett Wollman 			else
2144a0292f23SGarrett Wollman 				error = EINVAL;
214509fe6320SNavdeep Parhar 			goto unlock_and_done;
2146a0292f23SGarrett Wollman 
2147b8af5dfaSRobert Watson 		case TCP_INFO:
21488501a69cSRobert Watson 			INP_WUNLOCK(inp);
2149b8af5dfaSRobert Watson 			error = EINVAL;
2150b8af5dfaSRobert Watson 			break;
2151b8af5dfaSRobert Watson 
2152adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2153adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2154adc56f5aSEdward Tomasz Napierala #ifdef STATS
2155adc56f5aSEdward Tomasz Napierala 			error = sooptcopyin(sopt, &optval, sizeof optval,
2156adc56f5aSEdward Tomasz Napierala 			    sizeof optval);
2157adc56f5aSEdward Tomasz Napierala 			if (error)
2158adc56f5aSEdward Tomasz Napierala 				return (error);
2159adc56f5aSEdward Tomasz Napierala 
2160adc56f5aSEdward Tomasz Napierala 			if (optval > 0)
2161adc56f5aSEdward Tomasz Napierala 				sbp = stats_blob_alloc(
2162adc56f5aSEdward Tomasz Napierala 				    V_tcp_perconn_stats_dflt_tpl, 0);
2163adc56f5aSEdward Tomasz Napierala 			else
2164adc56f5aSEdward Tomasz Napierala 				sbp = NULL;
2165adc56f5aSEdward Tomasz Napierala 
2166adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2167adc56f5aSEdward Tomasz Napierala 			if ((tp->t_stats != NULL && sbp == NULL) ||
2168adc56f5aSEdward Tomasz Napierala 			    (tp->t_stats == NULL && sbp != NULL)) {
2169adc56f5aSEdward Tomasz Napierala 				struct statsblob *t = tp->t_stats;
2170adc56f5aSEdward Tomasz Napierala 				tp->t_stats = sbp;
2171adc56f5aSEdward Tomasz Napierala 				sbp = t;
2172adc56f5aSEdward Tomasz Napierala 			}
2173adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2174adc56f5aSEdward Tomasz Napierala 
2175adc56f5aSEdward Tomasz Napierala 			stats_blob_destroy(sbp);
2176adc56f5aSEdward Tomasz Napierala #else
2177adc56f5aSEdward Tomasz Napierala 			return (EOPNOTSUPP);
2178adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2179adc56f5aSEdward Tomasz Napierala 			break;
2180adc56f5aSEdward Tomasz Napierala 
2181dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2182ea9017fbSRandall Stewart 			error = tcp_set_cc_mod(inp, sopt);
218373e263b1SGleb Smirnoff 			break;
2184dbc42409SLawrence Stewart 
2185a034518aSAndrew Gallatin 		case TCP_REUSPORT_LB_NUMA:
2186a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2187a034518aSAndrew Gallatin 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2188a034518aSAndrew Gallatin 			    sizeof(optval));
2189a034518aSAndrew Gallatin 			INP_WLOCK_RECHECK(inp);
2190a034518aSAndrew Gallatin 			if (!error)
2191a034518aSAndrew Gallatin 				error = in_pcblbgroup_numa(inp, optval);
2192a034518aSAndrew Gallatin 			INP_WUNLOCK(inp);
2193a034518aSAndrew Gallatin 			break;
2194a034518aSAndrew Gallatin 
2195b2e60773SJohn Baldwin #ifdef KERN_TLS
2196b2e60773SJohn Baldwin 		case TCP_TXTLS_ENABLE:
2197b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2198ec1db6e1SJohn Baldwin 			error = copyin_tls_enable(sopt, &tls);
2199b2e60773SJohn Baldwin 			if (error)
2200b2e60773SJohn Baldwin 				break;
2201fd7daa72SMichael Tuexen 			error = ktls_enable_tx(so, &tls);
2202b2e60773SJohn Baldwin 			break;
2203b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2204b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2205b2e60773SJohn Baldwin 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
2206b2e60773SJohn Baldwin 			if (error)
2207b2e60773SJohn Baldwin 				return (error);
2208b2e60773SJohn Baldwin 
2209b2e60773SJohn Baldwin 			INP_WLOCK_RECHECK(inp);
2210fd7daa72SMichael Tuexen 			error = ktls_set_tx_mode(so, ui);
2211b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2212b2e60773SJohn Baldwin 			break;
2213f1f93475SJohn Baldwin 		case TCP_RXTLS_ENABLE:
2214f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2215f1f93475SJohn Baldwin 			error = sooptcopyin(sopt, &tls, sizeof(tls),
2216f1f93475SJohn Baldwin 			    sizeof(tls));
2217f1f93475SJohn Baldwin 			if (error)
2218f1f93475SJohn Baldwin 				break;
2219fd7daa72SMichael Tuexen 			error = ktls_enable_rx(so, &tls);
2220f1f93475SJohn Baldwin 			break;
2221b2e60773SJohn Baldwin #endif
222208af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
22239077f387SGleb Smirnoff 		case TCP_KEEPIDLE:
22249077f387SGleb Smirnoff 		case TCP_KEEPINTVL:
22259077f387SGleb Smirnoff 		case TCP_KEEPINIT:
22269077f387SGleb Smirnoff 			INP_WUNLOCK(inp);
22279077f387SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
22289077f387SGleb Smirnoff 			if (error)
22299077f387SGleb Smirnoff 				return (error);
22309077f387SGleb Smirnoff 
22319077f387SGleb Smirnoff 			if (ui > (UINT_MAX / hz)) {
22329077f387SGleb Smirnoff 				error = EINVAL;
22339077f387SGleb Smirnoff 				break;
22349077f387SGleb Smirnoff 			}
22359077f387SGleb Smirnoff 			ui *= hz;
22369077f387SGleb Smirnoff 
22379077f387SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
22389077f387SGleb Smirnoff 			switch (sopt->sopt_name) {
223908af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
224008af8aacSRandall Stewart 				tp->t_maxunacktime = ui;
224108af8aacSRandall Stewart 				break;
224208af8aacSRandall Stewart 
22439077f387SGleb Smirnoff 			case TCP_KEEPIDLE:
22449077f387SGleb Smirnoff 				tp->t_keepidle = ui;
22459077f387SGleb Smirnoff 				/*
22469077f387SGleb Smirnoff 				 * XXX: better check current remaining
22479077f387SGleb Smirnoff 				 * timeout and "merge" it with new value.
22489077f387SGleb Smirnoff 				 */
22499077f387SGleb Smirnoff 				if ((tp->t_state > TCPS_LISTEN) &&
22509077f387SGleb Smirnoff 				    (tp->t_state <= TCPS_CLOSING))
22519077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
22529077f387SGleb Smirnoff 					    TP_KEEPIDLE(tp));
22539077f387SGleb Smirnoff 				break;
22549077f387SGleb Smirnoff 			case TCP_KEEPINTVL:
22559077f387SGleb Smirnoff 				tp->t_keepintvl = ui;
22569077f387SGleb Smirnoff 				if ((tp->t_state == TCPS_FIN_WAIT_2) &&
22579077f387SGleb Smirnoff 				    (TP_MAXIDLE(tp) > 0))
22589077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
22599077f387SGleb Smirnoff 					    TP_MAXIDLE(tp));
22609077f387SGleb Smirnoff 				break;
22619077f387SGleb Smirnoff 			case TCP_KEEPINIT:
22629077f387SGleb Smirnoff 				tp->t_keepinit = ui;
22639077f387SGleb Smirnoff 				if (tp->t_state == TCPS_SYN_RECEIVED ||
22649077f387SGleb Smirnoff 				    tp->t_state == TCPS_SYN_SENT)
22659077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_KEEP,
22669077f387SGleb Smirnoff 					    TP_KEEPINIT(tp));
22679077f387SGleb Smirnoff 				break;
22689077f387SGleb Smirnoff 			}
226909fe6320SNavdeep Parhar 			goto unlock_and_done;
22709077f387SGleb Smirnoff 
227185c05144SGleb Smirnoff 		case TCP_KEEPCNT:
227285c05144SGleb Smirnoff 			INP_WUNLOCK(inp);
227385c05144SGleb Smirnoff 			error = sooptcopyin(sopt, &ui, sizeof(ui), sizeof(ui));
227485c05144SGleb Smirnoff 			if (error)
227585c05144SGleb Smirnoff 				return (error);
227685c05144SGleb Smirnoff 
227785c05144SGleb Smirnoff 			INP_WLOCK_RECHECK(inp);
227885c05144SGleb Smirnoff 			tp->t_keepcnt = ui;
227985c05144SGleb Smirnoff 			if ((tp->t_state == TCPS_FIN_WAIT_2) &&
228085c05144SGleb Smirnoff 			    (TP_MAXIDLE(tp) > 0))
228185c05144SGleb Smirnoff 				tcp_timer_activate(tp, TT_2MSL,
228285c05144SGleb Smirnoff 				    TP_MAXIDLE(tp));
228385c05144SGleb Smirnoff 			goto unlock_and_done;
228485c05144SGleb Smirnoff 
228586a996e6SHiren Panchasara #ifdef TCPPCAP
228686a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
228786a996e6SHiren Panchasara 		case TCP_PCAP_IN:
228886a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
228986a996e6SHiren Panchasara 			error = sooptcopyin(sopt, &optval, sizeof optval,
229086a996e6SHiren Panchasara 			    sizeof optval);
229186a996e6SHiren Panchasara 			if (error)
229286a996e6SHiren Panchasara 				return (error);
229386a996e6SHiren Panchasara 
229486a996e6SHiren Panchasara 			INP_WLOCK_RECHECK(inp);
229586a996e6SHiren Panchasara 			if (optval >= 0)
229686a996e6SHiren Panchasara 				tcp_pcap_set_sock_max(TCP_PCAP_OUT ?
229786a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts),
229886a996e6SHiren Panchasara 					optval);
229986a996e6SHiren Panchasara 			else
230086a996e6SHiren Panchasara 				error = EINVAL;
230186a996e6SHiren Panchasara 			goto unlock_and_done;
230286a996e6SHiren Panchasara #endif
230386a996e6SHiren Panchasara 
2304c560df6fSPatrick Kelsey 		case TCP_FASTOPEN: {
2305c560df6fSPatrick Kelsey 			struct tcp_fastopen tfo_optval;
2306c560df6fSPatrick Kelsey 
2307281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2308c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
2309c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
2310281a0fd4SPatrick Kelsey 				return (EPERM);
2311281a0fd4SPatrick Kelsey 
2312c560df6fSPatrick Kelsey 			error = sooptcopyin(sopt, &tfo_optval,
2313c560df6fSPatrick Kelsey 				    sizeof(tfo_optval), sizeof(int));
2314281a0fd4SPatrick Kelsey 			if (error)
2315281a0fd4SPatrick Kelsey 				return (error);
2316281a0fd4SPatrick Kelsey 
2317281a0fd4SPatrick Kelsey 			INP_WLOCK_RECHECK(inp);
2318d442a657SMichael Tuexen 			if ((tp->t_state != TCPS_CLOSED) &&
2319d442a657SMichael Tuexen 			    (tp->t_state != TCPS_LISTEN)) {
2320d442a657SMichael Tuexen 				error = EINVAL;
2321d442a657SMichael Tuexen 				goto unlock_and_done;
2322d442a657SMichael Tuexen 			}
2323c560df6fSPatrick Kelsey 			if (tfo_optval.enable) {
2324c560df6fSPatrick Kelsey 				if (tp->t_state == TCPS_LISTEN) {
2325c560df6fSPatrick Kelsey 					if (!V_tcp_fastopen_server_enable) {
2326c560df6fSPatrick Kelsey 						error = EPERM;
2327c560df6fSPatrick Kelsey 						goto unlock_and_done;
2328c560df6fSPatrick Kelsey 					}
2329c560df6fSPatrick Kelsey 
2330c560df6fSPatrick Kelsey 					if (tp->t_tfo_pending == NULL)
2331281a0fd4SPatrick Kelsey 						tp->t_tfo_pending =
2332281a0fd4SPatrick Kelsey 						    tcp_fastopen_alloc_counter();
2333c560df6fSPatrick Kelsey 				} else {
2334c560df6fSPatrick Kelsey 					/*
2335c560df6fSPatrick Kelsey 					 * If a pre-shared key was provided,
2336c560df6fSPatrick Kelsey 					 * stash it in the client cookie
2337c560df6fSPatrick Kelsey 					 * field of the tcpcb for use during
2338c560df6fSPatrick Kelsey 					 * connect.
2339c560df6fSPatrick Kelsey 					 */
2340c560df6fSPatrick Kelsey 					if (sopt->sopt_valsize ==
2341c560df6fSPatrick Kelsey 					    sizeof(tfo_optval)) {
2342c560df6fSPatrick Kelsey 						memcpy(tp->t_tfo_cookie.client,
2343c560df6fSPatrick Kelsey 						       tfo_optval.psk,
2344c560df6fSPatrick Kelsey 						       TCP_FASTOPEN_PSK_LEN);
2345c560df6fSPatrick Kelsey 						tp->t_tfo_client_cookie_len =
2346c560df6fSPatrick Kelsey 						    TCP_FASTOPEN_PSK_LEN;
2347c560df6fSPatrick Kelsey 					}
2348c560df6fSPatrick Kelsey 				}
2349d442a657SMichael Tuexen 				tp->t_flags |= TF_FASTOPEN;
2350281a0fd4SPatrick Kelsey 			} else
2351281a0fd4SPatrick Kelsey 				tp->t_flags &= ~TF_FASTOPEN;
2352281a0fd4SPatrick Kelsey 			goto unlock_and_done;
2353c560df6fSPatrick Kelsey 		}
2354281a0fd4SPatrick Kelsey 
2355e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
23562529f56eSJonathan T. Looney 		case TCP_LOG:
23572529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23582529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, &optval, sizeof optval,
23592529f56eSJonathan T. Looney 			    sizeof optval);
23602529f56eSJonathan T. Looney 			if (error)
23612529f56eSJonathan T. Looney 				return (error);
23622529f56eSJonathan T. Looney 
23632529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23642529f56eSJonathan T. Looney 			error = tcp_log_state_change(tp, optval);
23652529f56eSJonathan T. Looney 			goto unlock_and_done;
23662529f56eSJonathan T. Looney 
23672529f56eSJonathan T. Looney 		case TCP_LOGBUF:
23682529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23692529f56eSJonathan T. Looney 			error = EINVAL;
23702529f56eSJonathan T. Looney 			break;
23712529f56eSJonathan T. Looney 
23722529f56eSJonathan T. Looney 		case TCP_LOGID:
23732529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23742529f56eSJonathan T. Looney 			error = sooptcopyin(sopt, buf, TCP_LOG_ID_LEN - 1, 0);
23752529f56eSJonathan T. Looney 			if (error)
23762529f56eSJonathan T. Looney 				break;
23772529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23782529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23792529f56eSJonathan T. Looney 			error = tcp_log_set_id(tp, buf);
23802529f56eSJonathan T. Looney 			/* tcp_log_set_id() unlocks the INP. */
23812529f56eSJonathan T. Looney 			break;
23822529f56eSJonathan T. Looney 
23832529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
23842529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
23852529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
23862529f56eSJonathan T. Looney 			error =
23872529f56eSJonathan T. Looney 			    sooptcopyin(sopt, buf, TCP_LOG_REASON_LEN - 1, 0);
23882529f56eSJonathan T. Looney 			if (error)
23892529f56eSJonathan T. Looney 				break;
23902529f56eSJonathan T. Looney 			buf[sopt->sopt_valsize] = '\0';
23912529f56eSJonathan T. Looney 			INP_WLOCK_RECHECK(inp);
23922529f56eSJonathan T. Looney 			if (sopt->sopt_name == TCP_LOGDUMP) {
23932529f56eSJonathan T. Looney 				error = tcp_log_dump_tp_logbuf(tp, buf,
23942529f56eSJonathan T. Looney 				    M_WAITOK, true);
23952529f56eSJonathan T. Looney 				INP_WUNLOCK(inp);
23962529f56eSJonathan T. Looney 			} else {
23972529f56eSJonathan T. Looney 				tcp_log_dump_tp_bucket_logbufs(tp, buf);
23982529f56eSJonathan T. Looney 				/*
23992529f56eSJonathan T. Looney 				 * tcp_log_dump_tp_bucket_logbufs() drops the
24002529f56eSJonathan T. Looney 				 * INP lock.
24012529f56eSJonathan T. Looney 				 */
24022529f56eSJonathan T. Looney 			}
24032529f56eSJonathan T. Looney 			break;
2404e24e5683SJonathan T. Looney #endif
24052529f56eSJonathan T. Looney 
2406df8bae1dSRodney W. Grimes 		default:
24078501a69cSRobert Watson 			INP_WUNLOCK(inp);
2408df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2409df8bae1dSRodney W. Grimes 			break;
2410df8bae1dSRodney W. Grimes 		}
2411df8bae1dSRodney W. Grimes 		break;
2412df8bae1dSRodney W. Grimes 
2413cfe8b629SGarrett Wollman 	case SOPT_GET:
24141e8f5ffaSRobert Watson 		tp = intotcpcb(inp);
2415cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
2416fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
241788f6b043SBruce M Simpson 		case TCP_MD5SIG:
24188501a69cSRobert Watson 			INP_WUNLOCK(inp);
241997453e5eSClaudio Jeker 			if (!TCPMD5_ENABLED())
2420fcf59617SAndrey V. Elsukov 				return (ENOPROTOOPT);
2421fcf59617SAndrey V. Elsukov 			error = TCPMD5_PCBCTL(inp, sopt);
24221cfd4b53SBruce M Simpson 			break;
2423265ed012SBruce M Simpson #endif
24241e8f5ffaSRobert Watson 
2425df8bae1dSRodney W. Grimes 		case TCP_NODELAY:
2426cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NODELAY;
24278501a69cSRobert Watson 			INP_WUNLOCK(inp);
2428b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2429df8bae1dSRodney W. Grimes 			break;
2430df8bae1dSRodney W. Grimes 		case TCP_MAXSEG:
2431cfe8b629SGarrett Wollman 			optval = tp->t_maxseg;
24328501a69cSRobert Watson 			INP_WUNLOCK(inp);
2433b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2434df8bae1dSRodney W. Grimes 			break;
24359e644c23SMichael Tuexen 		case TCP_REMOTE_UDP_ENCAPS_PORT:
24369e644c23SMichael Tuexen 			optval = ntohs(tp->t_port);
24379e644c23SMichael Tuexen 			INP_WUNLOCK(inp);
24389e644c23SMichael Tuexen 			error = sooptcopyout(sopt, &optval, sizeof optval);
24399e644c23SMichael Tuexen 			break;
2440a0292f23SGarrett Wollman 		case TCP_NOOPT:
2441cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOOPT;
24428501a69cSRobert Watson 			INP_WUNLOCK(inp);
2443b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2444a0292f23SGarrett Wollman 			break;
2445a0292f23SGarrett Wollman 		case TCP_NOPUSH:
2446cfe8b629SGarrett Wollman 			optval = tp->t_flags & TF_NOPUSH;
24478501a69cSRobert Watson 			INP_WUNLOCK(inp);
2448b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &optval, sizeof optval);
2449b8af5dfaSRobert Watson 			break;
2450b8af5dfaSRobert Watson 		case TCP_INFO:
2451b8af5dfaSRobert Watson 			tcp_fill_info(tp, &ti);
24528501a69cSRobert Watson 			INP_WUNLOCK(inp);
2453b8af5dfaSRobert Watson 			error = sooptcopyout(sopt, &ti, sizeof ti);
2454a0292f23SGarrett Wollman 			break;
2455adc56f5aSEdward Tomasz Napierala 		case TCP_STATS:
2456adc56f5aSEdward Tomasz Napierala 			{
2457adc56f5aSEdward Tomasz Napierala #ifdef STATS
2458adc56f5aSEdward Tomasz Napierala 			int nheld;
2459adc56f5aSEdward Tomasz Napierala 			TYPEOF_MEMBER(struct statsblob, flags) sbflags = 0;
2460adc56f5aSEdward Tomasz Napierala 
2461adc56f5aSEdward Tomasz Napierala 			error = 0;
2462adc56f5aSEdward Tomasz Napierala 			socklen_t outsbsz = sopt->sopt_valsize;
2463adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats == NULL)
2464adc56f5aSEdward Tomasz Napierala 				error = ENOENT;
2465adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= tp->t_stats->cursz)
2466adc56f5aSEdward Tomasz Napierala 				outsbsz = tp->t_stats->cursz;
2467adc56f5aSEdward Tomasz Napierala 			else if (outsbsz >= sizeof(struct statsblob))
2468adc56f5aSEdward Tomasz Napierala 				outsbsz = sizeof(struct statsblob);
2469adc56f5aSEdward Tomasz Napierala 			else
2470adc56f5aSEdward Tomasz Napierala 				error = EINVAL;
2471adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2472adc56f5aSEdward Tomasz Napierala 			if (error)
2473adc56f5aSEdward Tomasz Napierala 				break;
2474adc56f5aSEdward Tomasz Napierala 
2475adc56f5aSEdward Tomasz Napierala 			sbp = sopt->sopt_val;
2476adc56f5aSEdward Tomasz Napierala 			nheld = atop(round_page(((vm_offset_t)sbp) +
2477adc56f5aSEdward Tomasz Napierala 			    (vm_size_t)outsbsz) - trunc_page((vm_offset_t)sbp));
2478adc56f5aSEdward Tomasz Napierala 			vm_page_t ma[nheld];
2479adc56f5aSEdward Tomasz Napierala 			if (vm_fault_quick_hold_pages(
2480adc56f5aSEdward Tomasz Napierala 			    &curproc->p_vmspace->vm_map, (vm_offset_t)sbp,
2481adc56f5aSEdward Tomasz Napierala 			    outsbsz, VM_PROT_READ | VM_PROT_WRITE, ma,
2482adc56f5aSEdward Tomasz Napierala 			    nheld) < 0) {
2483adc56f5aSEdward Tomasz Napierala 				error = EFAULT;
2484adc56f5aSEdward Tomasz Napierala 				break;
2485adc56f5aSEdward Tomasz Napierala 			}
2486adc56f5aSEdward Tomasz Napierala 
2487adc56f5aSEdward Tomasz Napierala 			if ((error = copyin_nofault(&(sbp->flags), &sbflags,
2488adc56f5aSEdward Tomasz Napierala 			    SIZEOF_MEMBER(struct statsblob, flags))))
2489adc56f5aSEdward Tomasz Napierala 				goto unhold;
2490adc56f5aSEdward Tomasz Napierala 
2491adc56f5aSEdward Tomasz Napierala 			INP_WLOCK_RECHECK(inp);
2492adc56f5aSEdward Tomasz Napierala 			error = stats_blob_snapshot(&sbp, outsbsz, tp->t_stats,
2493adc56f5aSEdward Tomasz Napierala 			    sbflags | SB_CLONE_USRDSTNOFAULT);
2494adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2495adc56f5aSEdward Tomasz Napierala 			sopt->sopt_valsize = outsbsz;
2496adc56f5aSEdward Tomasz Napierala unhold:
2497adc56f5aSEdward Tomasz Napierala 			vm_page_unhold_pages(ma, nheld);
2498adc56f5aSEdward Tomasz Napierala #else
2499adc56f5aSEdward Tomasz Napierala 			INP_WUNLOCK(inp);
2500adc56f5aSEdward Tomasz Napierala 			error = EOPNOTSUPP;
2501adc56f5aSEdward Tomasz Napierala #endif /* !STATS */
2502adc56f5aSEdward Tomasz Napierala 			break;
2503adc56f5aSEdward Tomasz Napierala 			}
2504dbc42409SLawrence Stewart 		case TCP_CONGESTION:
2505af6fef3aSGleb Smirnoff 			len = strlcpy(buf, CC_ALGO(tp)->name, TCP_CA_NAME_MAX);
2506dbc42409SLawrence Stewart 			INP_WUNLOCK(inp);
2507af6fef3aSGleb Smirnoff 			error = sooptcopyout(sopt, buf, len + 1);
2508dbc42409SLawrence Stewart 			break;
250908af8aacSRandall Stewart 		case TCP_MAXUNACKTIME:
25102f3eb7f4SGleb Smirnoff 		case TCP_KEEPIDLE:
25112f3eb7f4SGleb Smirnoff 		case TCP_KEEPINTVL:
25122f3eb7f4SGleb Smirnoff 		case TCP_KEEPINIT:
25132f3eb7f4SGleb Smirnoff 		case TCP_KEEPCNT:
25142f3eb7f4SGleb Smirnoff 			switch (sopt->sopt_name) {
251508af8aacSRandall Stewart 			case TCP_MAXUNACKTIME:
251608af8aacSRandall Stewart 				ui = TP_MAXUNACKTIME(tp) / hz;
251708af8aacSRandall Stewart 				break;
25182f3eb7f4SGleb Smirnoff 			case TCP_KEEPIDLE:
25195a17b6adSMichael Tuexen 				ui = TP_KEEPIDLE(tp) / hz;
25202f3eb7f4SGleb Smirnoff 				break;
25212f3eb7f4SGleb Smirnoff 			case TCP_KEEPINTVL:
25225a17b6adSMichael Tuexen 				ui = TP_KEEPINTVL(tp) / hz;
25232f3eb7f4SGleb Smirnoff 				break;
25242f3eb7f4SGleb Smirnoff 			case TCP_KEEPINIT:
25255a17b6adSMichael Tuexen 				ui = TP_KEEPINIT(tp) / hz;
25262f3eb7f4SGleb Smirnoff 				break;
25272f3eb7f4SGleb Smirnoff 			case TCP_KEEPCNT:
25285a17b6adSMichael Tuexen 				ui = TP_KEEPCNT(tp);
25292f3eb7f4SGleb Smirnoff 				break;
25302f3eb7f4SGleb Smirnoff 			}
25312f3eb7f4SGleb Smirnoff 			INP_WUNLOCK(inp);
25322f3eb7f4SGleb Smirnoff 			error = sooptcopyout(sopt, &ui, sizeof(ui));
25332f3eb7f4SGleb Smirnoff 			break;
253486a996e6SHiren Panchasara #ifdef TCPPCAP
253586a996e6SHiren Panchasara 		case TCP_PCAP_OUT:
253686a996e6SHiren Panchasara 		case TCP_PCAP_IN:
253786a996e6SHiren Panchasara 			optval = tcp_pcap_get_sock_max(TCP_PCAP_OUT ?
253886a996e6SHiren Panchasara 					&(tp->t_outpkts) : &(tp->t_inpkts));
253986a996e6SHiren Panchasara 			INP_WUNLOCK(inp);
254086a996e6SHiren Panchasara 			error = sooptcopyout(sopt, &optval, sizeof optval);
254186a996e6SHiren Panchasara 			break;
254286a996e6SHiren Panchasara #endif
2543281a0fd4SPatrick Kelsey 		case TCP_FASTOPEN:
2544281a0fd4SPatrick Kelsey 			optval = tp->t_flags & TF_FASTOPEN;
2545281a0fd4SPatrick Kelsey 			INP_WUNLOCK(inp);
2546281a0fd4SPatrick Kelsey 			error = sooptcopyout(sopt, &optval, sizeof optval);
2547281a0fd4SPatrick Kelsey 			break;
2548e24e5683SJonathan T. Looney #ifdef TCP_BLACKBOX
25492529f56eSJonathan T. Looney 		case TCP_LOG:
25502529f56eSJonathan T. Looney 			optval = tp->t_logstate;
25512529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25522529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, &optval, sizeof(optval));
25532529f56eSJonathan T. Looney 			break;
25542529f56eSJonathan T. Looney 		case TCP_LOGBUF:
25552529f56eSJonathan T. Looney 			/* tcp_log_getlogbuf() does INP_WUNLOCK(inp) */
25562529f56eSJonathan T. Looney 			error = tcp_log_getlogbuf(sopt, tp);
25572529f56eSJonathan T. Looney 			break;
25582529f56eSJonathan T. Looney 		case TCP_LOGID:
25592529f56eSJonathan T. Looney 			len = tcp_log_get_id(tp, buf);
25602529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25612529f56eSJonathan T. Looney 			error = sooptcopyout(sopt, buf, len + 1);
25622529f56eSJonathan T. Looney 			break;
25632529f56eSJonathan T. Looney 		case TCP_LOGDUMP:
25642529f56eSJonathan T. Looney 		case TCP_LOGDUMPID:
25652529f56eSJonathan T. Looney 			INP_WUNLOCK(inp);
25662529f56eSJonathan T. Looney 			error = EINVAL;
25672529f56eSJonathan T. Looney 			break;
2568e24e5683SJonathan T. Looney #endif
2569b2e60773SJohn Baldwin #ifdef KERN_TLS
2570b2e60773SJohn Baldwin 		case TCP_TXTLS_MODE:
2571fd7daa72SMichael Tuexen 			error = ktls_get_tx_mode(so, &optval);
2572b2e60773SJohn Baldwin 			INP_WUNLOCK(inp);
2573bf256782SMark Johnston 			if (error == 0)
2574bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2575bf256782SMark Johnston 				    sizeof(optval));
2576b2e60773SJohn Baldwin 			break;
2577f1f93475SJohn Baldwin 		case TCP_RXTLS_MODE:
2578fd7daa72SMichael Tuexen 			error = ktls_get_rx_mode(so, &optval);
2579f1f93475SJohn Baldwin 			INP_WUNLOCK(inp);
2580bf256782SMark Johnston 			if (error == 0)
2581bf256782SMark Johnston 				error = sooptcopyout(sopt, &optval,
2582bf256782SMark Johnston 				    sizeof(optval));
2583f1f93475SJohn Baldwin 			break;
2584b2e60773SJohn Baldwin #endif
25850471a8c7SRichard Scheffenegger 		case TCP_LRD:
25860471a8c7SRichard Scheffenegger 			optval = tp->t_flags & TF_LRD;
25870471a8c7SRichard Scheffenegger 			INP_WUNLOCK(inp);
25880471a8c7SRichard Scheffenegger 			error = sooptcopyout(sopt, &optval, sizeof optval);
25890471a8c7SRichard Scheffenegger 			break;
2590df8bae1dSRodney W. Grimes 		default:
25918501a69cSRobert Watson 			INP_WUNLOCK(inp);
2592df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
2593df8bae1dSRodney W. Grimes 			break;
2594df8bae1dSRodney W. Grimes 		}
2595df8bae1dSRodney W. Grimes 		break;
2596df8bae1dSRodney W. Grimes 	}
2597df8bae1dSRodney W. Grimes 	return (error);
2598df8bae1dSRodney W. Grimes }
25998501a69cSRobert Watson #undef INP_WLOCK_RECHECK
2600bac5bedfSConrad Meyer #undef INP_WLOCK_RECHECK_CLEANUP
2601df8bae1dSRodney W. Grimes 
260226e30fbbSDavid Greenman /*
2603df8bae1dSRodney W. Grimes  * Initiate (or continue) disconnect.
2604df8bae1dSRodney W. Grimes  * If embryonic state, just send reset (once).
2605df8bae1dSRodney W. Grimes  * If in ``let data drain'' option and linger null, just drop.
2606df8bae1dSRodney W. Grimes  * Otherwise (hard), mark socket disconnecting and drop
2607df8bae1dSRodney W. Grimes  * current input data; switch states based on user close, and
2608df8bae1dSRodney W. Grimes  * send segment to peer (with FIN).
2609df8bae1dSRodney W. Grimes  */
2610623dce13SRobert Watson static void
2611ad3f9ab3SAndre Oppermann tcp_disconnect(struct tcpcb *tp)
2612df8bae1dSRodney W. Grimes {
26139eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
26149eb0e832SGleb Smirnoff 	struct socket *so = tptosocket(tp);
2615e6e0b5ffSRobert Watson 
261697a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26178501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
2618df8bae1dSRodney W. Grimes 
2619623dce13SRobert Watson 	/*
2620623dce13SRobert Watson 	 * Neither tcp_close() nor tcp_drop() should return NULL, as the
2621623dce13SRobert Watson 	 * socket is still open.
2622623dce13SRobert Watson 	 */
26238db239dcSMichael Tuexen 	if (tp->t_state < TCPS_ESTABLISHED &&
26248db239dcSMichael Tuexen 	    !(tp->t_state > TCPS_LISTEN && IS_FASTOPEN(tp->t_flags))) {
2625df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2626623dce13SRobert Watson 		KASSERT(tp != NULL,
2627623dce13SRobert Watson 		    ("tcp_disconnect: tcp_close() returned NULL"));
2628623dce13SRobert Watson 	} else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
2629243917feSSeigo Tanimura 		tp = tcp_drop(tp, 0);
2630623dce13SRobert Watson 		KASSERT(tp != NULL,
2631623dce13SRobert Watson 		    ("tcp_disconnect: tcp_drop() returned NULL"));
2632623dce13SRobert Watson 	} else {
2633df8bae1dSRodney W. Grimes 		soisdisconnecting(so);
2634df8bae1dSRodney W. Grimes 		sbflush(&so->so_rcv);
2635623dce13SRobert Watson 		tcp_usrclosed(tp);
2636ad71fe3cSRobert Watson 		if (!(inp->inp_flags & INP_DROPPED))
2637f64dc2abSGleb Smirnoff 			/* Ignore stack's drop request, we already at it. */
2638f64dc2abSGleb Smirnoff 			(void)tcp_output_nodrop(tp);
2639df8bae1dSRodney W. Grimes 	}
2640df8bae1dSRodney W. Grimes }
2641df8bae1dSRodney W. Grimes 
2642df8bae1dSRodney W. Grimes /*
2643df8bae1dSRodney W. Grimes  * User issued close, and wish to trail through shutdown states:
2644df8bae1dSRodney W. Grimes  * if never received SYN, just forget it.  If got a SYN from peer,
2645df8bae1dSRodney W. Grimes  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
2646df8bae1dSRodney W. Grimes  * If already got a FIN from peer, then almost done; go to LAST_ACK
2647df8bae1dSRodney W. Grimes  * state.  In all other cases, have already sent FIN to peer (e.g.
2648df8bae1dSRodney W. Grimes  * after PRU_SHUTDOWN), and just have to play tedious game waiting
2649df8bae1dSRodney W. Grimes  * for peer to send FIN or not respond to keep-alives, etc.
2650df8bae1dSRodney W. Grimes  * We can let the user exit from the close as soon as the FIN is acked.
2651df8bae1dSRodney W. Grimes  */
2652623dce13SRobert Watson static void
2653ad3f9ab3SAndre Oppermann tcp_usrclosed(struct tcpcb *tp)
2654df8bae1dSRodney W. Grimes {
2655df8bae1dSRodney W. Grimes 
265697a95ee1SGleb Smirnoff 	NET_EPOCH_ASSERT();
26579eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
2658e6e0b5ffSRobert Watson 
2659df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2660df8bae1dSRodney W. Grimes 	case TCPS_LISTEN:
266109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
266209fe6320SNavdeep Parhar 		tcp_offload_listen_stop(tp);
266309fe6320SNavdeep Parhar #endif
2664550e9d42SHiren Panchasara 		tcp_state_change(tp, TCPS_CLOSED);
2665bc65987aSKip Macy 		/* FALLTHROUGH */
2666bc65987aSKip Macy 	case TCPS_CLOSED:
2667df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
2668623dce13SRobert Watson 		/*
2669623dce13SRobert Watson 		 * tcp_close() should never return NULL here as the socket is
2670623dce13SRobert Watson 		 * still open.
2671623dce13SRobert Watson 		 */
2672623dce13SRobert Watson 		KASSERT(tp != NULL,
2673623dce13SRobert Watson 		    ("tcp_usrclosed: tcp_close() returned NULL"));
2674df8bae1dSRodney W. Grimes 		break;
2675df8bae1dSRodney W. Grimes 
2676a0292f23SGarrett Wollman 	case TCPS_SYN_SENT:
2677df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2678a0292f23SGarrett Wollman 		tp->t_flags |= TF_NEEDFIN;
2679a0292f23SGarrett Wollman 		break;
2680a0292f23SGarrett Wollman 
2681df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
268257f60867SMark Johnston 		tcp_state_change(tp, TCPS_FIN_WAIT_1);
2683df8bae1dSRodney W. Grimes 		break;
2684df8bae1dSRodney W. Grimes 
2685df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
268657f60867SMark Johnston 		tcp_state_change(tp, TCPS_LAST_ACK);
2687df8bae1dSRodney W. Grimes 		break;
2688df8bae1dSRodney W. Grimes 	}
268908af8aacSRandall Stewart 	if (tp->t_acktime == 0)
269008af8aacSRandall Stewart 		tp->t_acktime = ticks;
2691abc7d910SRobert Watson 	if (tp->t_state >= TCPS_FIN_WAIT_2) {
26929eb0e832SGleb Smirnoff 		soisdisconnected(tptosocket(tp));
2693abc7d910SRobert Watson 		/* Prevent the connection hanging in FIN_WAIT_2 forever. */
26947c72af87SMohan Srinivasan 		if (tp->t_state == TCPS_FIN_WAIT_2) {
26957c72af87SMohan Srinivasan 			int timeout;
26967c72af87SMohan Srinivasan 
26977c72af87SMohan Srinivasan 			timeout = (tcp_fast_finwait2_recycle) ?
26989077f387SGleb Smirnoff 			    tcp_finwait2_timeout : TP_MAXIDLE(tp);
2699b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_2MSL, timeout);
2700b6239c4aSAndras Olah 		}
2701df8bae1dSRodney W. Grimes 	}
27027c72af87SMohan Srinivasan }
2703497057eeSRobert Watson 
2704497057eeSRobert Watson #ifdef DDB
2705497057eeSRobert Watson static void
2706497057eeSRobert Watson db_print_indent(int indent)
2707497057eeSRobert Watson {
2708497057eeSRobert Watson 	int i;
2709497057eeSRobert Watson 
2710497057eeSRobert Watson 	for (i = 0; i < indent; i++)
2711497057eeSRobert Watson 		db_printf(" ");
2712497057eeSRobert Watson }
2713497057eeSRobert Watson 
2714497057eeSRobert Watson static void
2715497057eeSRobert Watson db_print_tstate(int t_state)
2716497057eeSRobert Watson {
2717497057eeSRobert Watson 
2718497057eeSRobert Watson 	switch (t_state) {
2719497057eeSRobert Watson 	case TCPS_CLOSED:
2720497057eeSRobert Watson 		db_printf("TCPS_CLOSED");
2721497057eeSRobert Watson 		return;
2722497057eeSRobert Watson 
2723497057eeSRobert Watson 	case TCPS_LISTEN:
2724497057eeSRobert Watson 		db_printf("TCPS_LISTEN");
2725497057eeSRobert Watson 		return;
2726497057eeSRobert Watson 
2727497057eeSRobert Watson 	case TCPS_SYN_SENT:
2728497057eeSRobert Watson 		db_printf("TCPS_SYN_SENT");
2729497057eeSRobert Watson 		return;
2730497057eeSRobert Watson 
2731497057eeSRobert Watson 	case TCPS_SYN_RECEIVED:
2732497057eeSRobert Watson 		db_printf("TCPS_SYN_RECEIVED");
2733497057eeSRobert Watson 		return;
2734497057eeSRobert Watson 
2735497057eeSRobert Watson 	case TCPS_ESTABLISHED:
2736497057eeSRobert Watson 		db_printf("TCPS_ESTABLISHED");
2737497057eeSRobert Watson 		return;
2738497057eeSRobert Watson 
2739497057eeSRobert Watson 	case TCPS_CLOSE_WAIT:
2740497057eeSRobert Watson 		db_printf("TCPS_CLOSE_WAIT");
2741497057eeSRobert Watson 		return;
2742497057eeSRobert Watson 
2743497057eeSRobert Watson 	case TCPS_FIN_WAIT_1:
2744497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_1");
2745497057eeSRobert Watson 		return;
2746497057eeSRobert Watson 
2747497057eeSRobert Watson 	case TCPS_CLOSING:
2748497057eeSRobert Watson 		db_printf("TCPS_CLOSING");
2749497057eeSRobert Watson 		return;
2750497057eeSRobert Watson 
2751497057eeSRobert Watson 	case TCPS_LAST_ACK:
2752497057eeSRobert Watson 		db_printf("TCPS_LAST_ACK");
2753497057eeSRobert Watson 		return;
2754497057eeSRobert Watson 
2755497057eeSRobert Watson 	case TCPS_FIN_WAIT_2:
2756497057eeSRobert Watson 		db_printf("TCPS_FIN_WAIT_2");
2757497057eeSRobert Watson 		return;
2758497057eeSRobert Watson 
2759497057eeSRobert Watson 	case TCPS_TIME_WAIT:
2760497057eeSRobert Watson 		db_printf("TCPS_TIME_WAIT");
2761497057eeSRobert Watson 		return;
2762497057eeSRobert Watson 
2763497057eeSRobert Watson 	default:
2764497057eeSRobert Watson 		db_printf("unknown");
2765497057eeSRobert Watson 		return;
2766497057eeSRobert Watson 	}
2767497057eeSRobert Watson }
2768497057eeSRobert Watson 
2769497057eeSRobert Watson static void
2770497057eeSRobert Watson db_print_tflags(u_int t_flags)
2771497057eeSRobert Watson {
2772497057eeSRobert Watson 	int comma;
2773497057eeSRobert Watson 
2774497057eeSRobert Watson 	comma = 0;
2775497057eeSRobert Watson 	if (t_flags & TF_ACKNOW) {
2776497057eeSRobert Watson 		db_printf("%sTF_ACKNOW", comma ? ", " : "");
2777497057eeSRobert Watson 		comma = 1;
2778497057eeSRobert Watson 	}
2779497057eeSRobert Watson 	if (t_flags & TF_DELACK) {
2780497057eeSRobert Watson 		db_printf("%sTF_DELACK", comma ? ", " : "");
2781497057eeSRobert Watson 		comma = 1;
2782497057eeSRobert Watson 	}
2783497057eeSRobert Watson 	if (t_flags & TF_NODELAY) {
2784497057eeSRobert Watson 		db_printf("%sTF_NODELAY", comma ? ", " : "");
2785497057eeSRobert Watson 		comma = 1;
2786497057eeSRobert Watson 	}
2787497057eeSRobert Watson 	if (t_flags & TF_NOOPT) {
2788497057eeSRobert Watson 		db_printf("%sTF_NOOPT", comma ? ", " : "");
2789497057eeSRobert Watson 		comma = 1;
2790497057eeSRobert Watson 	}
2791497057eeSRobert Watson 	if (t_flags & TF_SENTFIN) {
2792497057eeSRobert Watson 		db_printf("%sTF_SENTFIN", comma ? ", " : "");
2793497057eeSRobert Watson 		comma = 1;
2794497057eeSRobert Watson 	}
2795497057eeSRobert Watson 	if (t_flags & TF_REQ_SCALE) {
2796497057eeSRobert Watson 		db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
2797497057eeSRobert Watson 		comma = 1;
2798497057eeSRobert Watson 	}
2799497057eeSRobert Watson 	if (t_flags & TF_RCVD_SCALE) {
2800497057eeSRobert Watson 		db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
2801497057eeSRobert Watson 		comma = 1;
2802497057eeSRobert Watson 	}
2803497057eeSRobert Watson 	if (t_flags & TF_REQ_TSTMP) {
2804497057eeSRobert Watson 		db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
2805497057eeSRobert Watson 		comma = 1;
2806497057eeSRobert Watson 	}
2807497057eeSRobert Watson 	if (t_flags & TF_RCVD_TSTMP) {
2808497057eeSRobert Watson 		db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
2809497057eeSRobert Watson 		comma = 1;
2810497057eeSRobert Watson 	}
2811497057eeSRobert Watson 	if (t_flags & TF_SACK_PERMIT) {
2812497057eeSRobert Watson 		db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
2813497057eeSRobert Watson 		comma = 1;
2814497057eeSRobert Watson 	}
2815497057eeSRobert Watson 	if (t_flags & TF_NEEDSYN) {
2816497057eeSRobert Watson 		db_printf("%sTF_NEEDSYN", comma ? ", " : "");
2817497057eeSRobert Watson 		comma = 1;
2818497057eeSRobert Watson 	}
2819497057eeSRobert Watson 	if (t_flags & TF_NEEDFIN) {
2820497057eeSRobert Watson 		db_printf("%sTF_NEEDFIN", comma ? ", " : "");
2821497057eeSRobert Watson 		comma = 1;
2822497057eeSRobert Watson 	}
2823497057eeSRobert Watson 	if (t_flags & TF_NOPUSH) {
2824497057eeSRobert Watson 		db_printf("%sTF_NOPUSH", comma ? ", " : "");
2825497057eeSRobert Watson 		comma = 1;
2826497057eeSRobert Watson 	}
28273f169c54SRichard Scheffenegger 	if (t_flags & TF_PREVVALID) {
28283f169c54SRichard Scheffenegger 		db_printf("%sTF_PREVVALID", comma ? ", " : "");
28293f169c54SRichard Scheffenegger 		comma = 1;
28303f169c54SRichard Scheffenegger 	}
2831497057eeSRobert Watson 	if (t_flags & TF_MORETOCOME) {
2832497057eeSRobert Watson 		db_printf("%sTF_MORETOCOME", comma ? ", " : "");
2833497057eeSRobert Watson 		comma = 1;
2834497057eeSRobert Watson 	}
2835493105c2SGleb Smirnoff 	if (t_flags & TF_SONOTCONN) {
2836493105c2SGleb Smirnoff 		db_printf("%sTF_SONOTCONN", comma ? ", " : "");
2837497057eeSRobert Watson 		comma = 1;
2838497057eeSRobert Watson 	}
2839497057eeSRobert Watson 	if (t_flags & TF_LASTIDLE) {
2840497057eeSRobert Watson 		db_printf("%sTF_LASTIDLE", comma ? ", " : "");
2841497057eeSRobert Watson 		comma = 1;
2842497057eeSRobert Watson 	}
2843497057eeSRobert Watson 	if (t_flags & TF_RXWIN0SENT) {
2844497057eeSRobert Watson 		db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
2845497057eeSRobert Watson 		comma = 1;
2846497057eeSRobert Watson 	}
2847497057eeSRobert Watson 	if (t_flags & TF_FASTRECOVERY) {
2848497057eeSRobert Watson 		db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
2849497057eeSRobert Watson 		comma = 1;
2850497057eeSRobert Watson 	}
2851dbc42409SLawrence Stewart 	if (t_flags & TF_CONGRECOVERY) {
2852dbc42409SLawrence Stewart 		db_printf("%sTF_CONGRECOVERY", comma ? ", " : "");
2853dbc42409SLawrence Stewart 		comma = 1;
2854dbc42409SLawrence Stewart 	}
2855497057eeSRobert Watson 	if (t_flags & TF_WASFRECOVERY) {
2856497057eeSRobert Watson 		db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
2857497057eeSRobert Watson 		comma = 1;
2858497057eeSRobert Watson 	}
28593f169c54SRichard Scheffenegger 	if (t_flags & TF_WASCRECOVERY) {
28603f169c54SRichard Scheffenegger 		db_printf("%sTF_WASCRECOVERY", comma ? ", " : "");
28613f169c54SRichard Scheffenegger 		comma = 1;
28623f169c54SRichard Scheffenegger 	}
2863497057eeSRobert Watson 	if (t_flags & TF_SIGNATURE) {
2864497057eeSRobert Watson 		db_printf("%sTF_SIGNATURE", comma ? ", " : "");
2865497057eeSRobert Watson 		comma = 1;
2866497057eeSRobert Watson 	}
2867497057eeSRobert Watson 	if (t_flags & TF_FORCEDATA) {
2868497057eeSRobert Watson 		db_printf("%sTF_FORCEDATA", comma ? ", " : "");
2869497057eeSRobert Watson 		comma = 1;
2870497057eeSRobert Watson 	}
2871497057eeSRobert Watson 	if (t_flags & TF_TSO) {
2872497057eeSRobert Watson 		db_printf("%sTF_TSO", comma ? ", " : "");
2873497057eeSRobert Watson 		comma = 1;
2874497057eeSRobert Watson 	}
2875281a0fd4SPatrick Kelsey 	if (t_flags & TF_FASTOPEN) {
2876281a0fd4SPatrick Kelsey 		db_printf("%sTF_FASTOPEN", comma ? ", " : "");
2877281a0fd4SPatrick Kelsey 		comma = 1;
2878281a0fd4SPatrick Kelsey 	}
2879497057eeSRobert Watson }
2880497057eeSRobert Watson 
2881497057eeSRobert Watson static void
28823cf38784SMichael Tuexen db_print_tflags2(u_int t_flags2)
28833cf38784SMichael Tuexen {
28843cf38784SMichael Tuexen 	int comma;
28853cf38784SMichael Tuexen 
28863cf38784SMichael Tuexen 	comma = 0;
28873f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_BLACKHOLE) {
28883f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_BLACKHOLE", comma ? ", " : "");
28893f169c54SRichard Scheffenegger 		comma = 1;
28903f169c54SRichard Scheffenegger 	}
28913f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_PMTUD) {
28923f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_PMTUD", comma ? ", " : "");
28933f169c54SRichard Scheffenegger 		comma = 1;
28943f169c54SRichard Scheffenegger 	}
28953f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_PLPMTU_MAXSEGSNT) {
28963f169c54SRichard Scheffenegger 		db_printf("%sTF2_PLPMTU_MAXSEGSNT", comma ? ", " : "");
28973f169c54SRichard Scheffenegger 		comma = 1;
28983f169c54SRichard Scheffenegger 	}
28993f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_LOG_AUTO) {
29003f169c54SRichard Scheffenegger 		db_printf("%sTF2_LOG_AUTO", comma ? ", " : "");
29013f169c54SRichard Scheffenegger 		comma = 1;
29023f169c54SRichard Scheffenegger 	}
29033f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_DROP_AF_DATA) {
29043f169c54SRichard Scheffenegger 		db_printf("%sTF2_DROP_AF_DATA", comma ? ", " : "");
29053f169c54SRichard Scheffenegger 		comma = 1;
29063f169c54SRichard Scheffenegger 	}
29073cf38784SMichael Tuexen 	if (t_flags2 & TF2_ECN_PERMIT) {
29083cf38784SMichael Tuexen 		db_printf("%sTF2_ECN_PERMIT", comma ? ", " : "");
29093cf38784SMichael Tuexen 		comma = 1;
29103cf38784SMichael Tuexen 	}
29113f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_CWR) {
29123f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_CWR", comma ? ", " : "");
29133f169c54SRichard Scheffenegger 		comma = 1;
29143f169c54SRichard Scheffenegger 	}
29153f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ECN_SND_ECE) {
29163f169c54SRichard Scheffenegger 		db_printf("%sTF2_ECN_SND_ECE", comma ? ", " : "");
29173f169c54SRichard Scheffenegger 		comma = 1;
29183f169c54SRichard Scheffenegger 	}
29193f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_ACE_PERMIT) {
29203f169c54SRichard Scheffenegger 		db_printf("%sTF2_ACE_PERMIT", comma ? ", " : "");
29213f169c54SRichard Scheffenegger 		comma = 1;
29223f169c54SRichard Scheffenegger 	}
29233f169c54SRichard Scheffenegger 	if (t_flags2 & TF2_FBYTES_COMPLETE) {
29243f169c54SRichard Scheffenegger 		db_printf("%sTF2_FBYTES_COMPLETE", comma ? ", " : "");
29253f169c54SRichard Scheffenegger 		comma = 1;
29263f169c54SRichard Scheffenegger 	}
29273cf38784SMichael Tuexen }
29283cf38784SMichael Tuexen 
29293cf38784SMichael Tuexen static void
2930497057eeSRobert Watson db_print_toobflags(char t_oobflags)
2931497057eeSRobert Watson {
2932497057eeSRobert Watson 	int comma;
2933497057eeSRobert Watson 
2934497057eeSRobert Watson 	comma = 0;
2935497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HAVEDATA) {
2936497057eeSRobert Watson 		db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
2937497057eeSRobert Watson 		comma = 1;
2938497057eeSRobert Watson 	}
2939497057eeSRobert Watson 	if (t_oobflags & TCPOOB_HADDATA) {
2940497057eeSRobert Watson 		db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
2941497057eeSRobert Watson 		comma = 1;
2942497057eeSRobert Watson 	}
2943497057eeSRobert Watson }
2944497057eeSRobert Watson 
2945497057eeSRobert Watson static void
2946497057eeSRobert Watson db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
2947497057eeSRobert Watson {
2948497057eeSRobert Watson 
2949497057eeSRobert Watson 	db_print_indent(indent);
2950497057eeSRobert Watson 	db_printf("%s at %p\n", name, tp);
2951497057eeSRobert Watson 
2952497057eeSRobert Watson 	indent += 2;
2953497057eeSRobert Watson 
2954497057eeSRobert Watson 	db_print_indent(indent);
2955497057eeSRobert Watson 	db_printf("t_segq first: %p   t_segqlen: %d   t_dupacks: %d\n",
2956c28440dbSRandall Stewart 	   TAILQ_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
2957497057eeSRobert Watson 
2958497057eeSRobert Watson 	db_print_indent(indent);
2959446ccdd0SGleb Smirnoff 	db_printf("t_callout: %p   t_timers: %p\n",
2960446ccdd0SGleb Smirnoff 	    &tp->t_callout, &tp->t_timers);
2961497057eeSRobert Watson 
2962497057eeSRobert Watson 	db_print_indent(indent);
2963497057eeSRobert Watson 	db_printf("t_state: %d (", tp->t_state);
2964497057eeSRobert Watson 	db_print_tstate(tp->t_state);
2965497057eeSRobert Watson 	db_printf(")\n");
2966497057eeSRobert Watson 
2967497057eeSRobert Watson 	db_print_indent(indent);
2968497057eeSRobert Watson 	db_printf("t_flags: 0x%x (", tp->t_flags);
2969497057eeSRobert Watson 	db_print_tflags(tp->t_flags);
2970497057eeSRobert Watson 	db_printf(")\n");
2971497057eeSRobert Watson 
2972497057eeSRobert Watson 	db_print_indent(indent);
29733cf38784SMichael Tuexen 	db_printf("t_flags2: 0x%x (", tp->t_flags2);
29743cf38784SMichael Tuexen 	db_print_tflags2(tp->t_flags2);
29753cf38784SMichael Tuexen 	db_printf(")\n");
29763cf38784SMichael Tuexen 
29773cf38784SMichael Tuexen 	db_print_indent(indent);
2978fb8f221aSMaxim Konovalov 	db_printf("snd_una: 0x%08x   snd_max: 0x%08x   snd_nxt: 0x%08x\n",
2979497057eeSRobert Watson 	    tp->snd_una, tp->snd_max, tp->snd_nxt);
2980497057eeSRobert Watson 
2981497057eeSRobert Watson 	db_print_indent(indent);
2982497057eeSRobert Watson 	db_printf("snd_up: 0x%08x   snd_wl1: 0x%08x   snd_wl2: 0x%08x\n",
2983497057eeSRobert Watson 	   tp->snd_up, tp->snd_wl1, tp->snd_wl2);
2984497057eeSRobert Watson 
2985497057eeSRobert Watson 	db_print_indent(indent);
2986497057eeSRobert Watson 	db_printf("iss: 0x%08x   irs: 0x%08x   rcv_nxt: 0x%08x\n",
2987497057eeSRobert Watson 	    tp->iss, tp->irs, tp->rcv_nxt);
2988497057eeSRobert Watson 
2989497057eeSRobert Watson 	db_print_indent(indent);
29903ac12506SJonathan T. Looney 	db_printf("rcv_adv: 0x%08x   rcv_wnd: %u   rcv_up: 0x%08x\n",
2991497057eeSRobert Watson 	    tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
2992497057eeSRobert Watson 
2993497057eeSRobert Watson 	db_print_indent(indent);
29943ac12506SJonathan T. Looney 	db_printf("snd_wnd: %u   snd_cwnd: %u\n",
29951c18314dSAndre Oppermann 	   tp->snd_wnd, tp->snd_cwnd);
2996497057eeSRobert Watson 
2997497057eeSRobert Watson 	db_print_indent(indent);
29983ac12506SJonathan T. Looney 	db_printf("snd_ssthresh: %u   snd_recover: "
29991c18314dSAndre Oppermann 	    "0x%08x\n", tp->snd_ssthresh, tp->snd_recover);
3000497057eeSRobert Watson 
3001497057eeSRobert Watson 	db_print_indent(indent);
30020c39d38dSGleb Smirnoff 	db_printf("t_rcvtime: %u   t_startime: %u\n",
30030c39d38dSGleb Smirnoff 	    tp->t_rcvtime, tp->t_starttime);
3004497057eeSRobert Watson 
3005497057eeSRobert Watson 	db_print_indent(indent);
30061c18314dSAndre Oppermann 	db_printf("t_rttime: %u   t_rtsq: 0x%08x\n",
30071c18314dSAndre Oppermann 	    tp->t_rtttime, tp->t_rtseq);
3008497057eeSRobert Watson 
3009497057eeSRobert Watson 	db_print_indent(indent);
30101c18314dSAndre Oppermann 	db_printf("t_rxtcur: %d   t_maxseg: %u   t_srtt: %d\n",
30111c18314dSAndre Oppermann 	    tp->t_rxtcur, tp->t_maxseg, tp->t_srtt);
3012497057eeSRobert Watson 
3013497057eeSRobert Watson 	db_print_indent(indent);
3014bd4f9866SMichael Tuexen 	db_printf("t_rttvar: %d   t_rxtshift: %d   t_rttmin: %u\n",
3015bd4f9866SMichael Tuexen 	    tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin);
3016497057eeSRobert Watson 
3017497057eeSRobert Watson 	db_print_indent(indent);
301818b83b62SRichard Scheffenegger 	db_printf("t_rttupdated: %u   max_sndwnd: %u   t_softerror: %d\n",
3019497057eeSRobert Watson 	    tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
3020497057eeSRobert Watson 
3021497057eeSRobert Watson 	db_print_indent(indent);
3022497057eeSRobert Watson 	db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
3023497057eeSRobert Watson 	db_print_toobflags(tp->t_oobflags);
3024497057eeSRobert Watson 	db_printf(")   t_iobc: 0x%02x\n", tp->t_iobc);
3025497057eeSRobert Watson 
3026497057eeSRobert Watson 	db_print_indent(indent);
3027497057eeSRobert Watson 	db_printf("snd_scale: %u   rcv_scale: %u   request_r_scale: %u\n",
3028497057eeSRobert Watson 	    tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
3029497057eeSRobert Watson 
3030497057eeSRobert Watson 	db_print_indent(indent);
30319f78a87aSJohn Baldwin 	db_printf("ts_recent: %u   ts_recent_age: %u\n",
30321a553740SAndre Oppermann 	    tp->ts_recent, tp->ts_recent_age);
3033497057eeSRobert Watson 
3034497057eeSRobert Watson 	db_print_indent(indent);
3035497057eeSRobert Watson 	db_printf("ts_offset: %u   last_ack_sent: 0x%08x   snd_cwnd_prev: "
30363ac12506SJonathan T. Looney 	    "%u\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
3037497057eeSRobert Watson 
3038497057eeSRobert Watson 	db_print_indent(indent);
30393ac12506SJonathan T. Looney 	db_printf("snd_ssthresh_prev: %u   snd_recover_prev: 0x%08x   "
30409f78a87aSJohn Baldwin 	    "t_badrxtwin: %u\n", tp->snd_ssthresh_prev,
3041497057eeSRobert Watson 	    tp->snd_recover_prev, tp->t_badrxtwin);
3042497057eeSRobert Watson 
3043497057eeSRobert Watson 	db_print_indent(indent);
30443529149eSAndre Oppermann 	db_printf("snd_numholes: %d  snd_holes first: %p\n",
30453529149eSAndre Oppermann 	    tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
3046497057eeSRobert Watson 
3047497057eeSRobert Watson 	db_print_indent(indent);
3048a3574665SMichael Tuexen 	db_printf("snd_fack: 0x%08x   rcv_numsacks: %d\n",
3049a3574665SMichael Tuexen 	    tp->snd_fack, tp->rcv_numsacks);
3050497057eeSRobert Watson 
3051497057eeSRobert Watson 	/* Skip sackblks, sackhint. */
3052497057eeSRobert Watson 
3053497057eeSRobert Watson 	db_print_indent(indent);
3054497057eeSRobert Watson 	db_printf("t_rttlow: %d   rfbuf_ts: %u   rfbuf_cnt: %d\n",
3055497057eeSRobert Watson 	    tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
3056497057eeSRobert Watson }
3057497057eeSRobert Watson 
3058497057eeSRobert Watson DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
3059497057eeSRobert Watson {
3060497057eeSRobert Watson 	struct tcpcb *tp;
3061497057eeSRobert Watson 
3062497057eeSRobert Watson 	if (!have_addr) {
3063497057eeSRobert Watson 		db_printf("usage: show tcpcb <addr>\n");
3064497057eeSRobert Watson 		return;
3065497057eeSRobert Watson 	}
3066497057eeSRobert Watson 	tp = (struct tcpcb *)addr;
3067497057eeSRobert Watson 
3068497057eeSRobert Watson 	db_print_tcpcb(tp, "tcpcb", 0);
3069497057eeSRobert Watson }
3070497057eeSRobert Watson #endif
3071